phi instuction example

Hello. I'm a newbie for LLVM.

What I'm trying to do is to write the simplest program containing phi IR.
However, even though I wrote and if statements, what I get is LLVM IR not containing phi node. I tried the examples shown in LLVM documentation but it didn't work.

At first, I thought it is because of the optimization level but without optimization, it doesn't produce phi, and when using optimization level higher and equal to O1, the meaningless while loops are just removed, so I have a problem.

I think it is easy problem but, for some reasons that I don't know, I failed making one.

Can any give me simplest program producing phi?

Thanks.

An easy way to get a phi is to run the mem2reg pass with opt. For example,
this C code:

int foo(int a, int b) {
  if (a > b) return a + b + 2;
  else return a * b * 17;
}

When compiled with Clang to LLVM IR, and run through 'opt -mem2reg',
produces:

; ModuleID = 'f.opt.bc'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind
define i32 @foo(i32 %a, i32 %b) #0 {
entry:
  %cmp = icmp sgt i32 %a, %b
  br i1 %cmp, label %if.then, label %if.else

if.then: ; preds = %entry
  %add = add nsw i32 %a, %b
  %add1 = add nsw i32 %add, 2
  br label %return

if.else: ; preds = %entry
  %mul = mul nsw i32 %a, %b
  %mul2 = mul nsw i32 %mul, 17
  br label %return

return: ; preds = %if.else,
%if.then
  %retval.0 = phi i32 [ %add1, %if.then ], [ %mul2, %if.else ]
  ret i32 %retval.0
}

attributes #0 = { nounwind "less-precise-fpmad"="false"
"no-frame-pointer-elim"="false" "no-infs-fp-math"="false"
"no-nans-fp-math"="false" "no-realign-stack"
"stack-protector-buffer-size"="8" "unsafe-fp-math"="false"
"use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = metadata !{metadata !"clang version 3.5.0 "}

Eli

How did you terminate the phi instruction? Unconditional branch somewhere else? I had the same problem recently and discovered I was missing a terminate instruction...