Peng Yu via llvm-dev
2019-Jan-25 16:55 UTC
[llvm-dev] minimal C code that would generate IR code using phi
Hi, I am trying to understand phi better. I know what it means. https://en.wikipedia.org/wiki/Static_single_assignment_form But when I tried to create a minimal code using if-else with unbalanced statements to change a variable in C, it still did not generate IR code using phi. Does anybody have a minimal C code that would generate IR code using phi? Thanks. -- Regards, Peng
Tim Northover via llvm-dev
2019-Jan-25 17:03 UTC
[llvm-dev] minimal C code that would generate IR code using phi
On Fri, 25 Jan 2019 at 16:55, Peng Yu via llvm-dev <llvm-dev at lists.llvm.org> wrote:> Does anybody have a minimal C code that would generate IR code using > phi? Thanks.Clang never really generates phis itself because it's easier to generate code that simply uses a stack variable (you don't have to keep track of control flow). The Mem2Reg pass in LLVM is what converts Clang's output into a form with phis. If you run Clang at an optimization above -O0 (e.g. "clang -O1 -Xclang -disable-llvm-passes -emit-llvm ...") then run "opt -mem2reg" manually you'll closest to what Clang would have emitted if it did do phis. Cheers. Tim.
Krzysztof Parzyszek via llvm-dev
2019-Jan-25 17:05 UTC
[llvm-dev] minimal C code that would generate IR code using phi
On 1/25/2019 10:55 AM, Peng Yu via llvm-dev wrote:> > Does anybody have a minimal C code that would generate IR code using > phi? Thanks.I don't know if this is minimal, but it's short: --- p.c --- int bar(int a); int foo(int a) { return a > 0 ? bar(a-1) : -bar(0); } ----------- $ clang -S -emit-llvm -O2 p.c -o - ; ModuleID = 'p.c' source_filename = "p.c" target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" ; Function Attrs: nounwind uwtable define dso_local i32 @foo(i32 %a) local_unnamed_addr #0 { entry: %cmp = icmp sgt i32 %a, 0 br i1 %cmp, label %cond.true, label %cond.false cond.true: ; preds = %entry %sub = add nsw i32 %a, -1 %call = tail call i32 @bar(i32 %sub) #2 br label %cond.end cond.false: ; preds = %entry %call1 = tail call i32 @bar(i32 0) #2 %sub2 = sub nsw i32 0, %call1 br label %cond.end cond.end: ; preds = %cond.false, %cond.true %cond = phi i32 [ %call, %cond.true ], [ %sub2, %cond.false ] ret i32 %cond } [...] -Krzysztof