Hello, On Tue, Oct 16, 2012 at 11:53 PM, Xi Wang <xi.wang@xxxxxxxxx> wrote: > On 10/16/12 4:14 PM, Jonathan Neuschäfer wrote: >> Actually, the situation of Phi nodes in LLVM is actually slightly more >> complex: They require "one pair (of value and BB) for each predecessor >> basic block of the current block"[1]. This mean that we'll sometimes >> need to insert phi nodes into BBs that don't directly use a value. > > I ran into the same problem before. I would suggest a simpler and safer > way: don't emit LLVM phi; instead emit load/store (of some alloca). > > phi => load from some alloca > phisrc => store to some alloca > > You can find sample code from splay here (emit_phi & emit_phisrc): > > https://github.com/xiw/splay/blob/master/function.c#L356 > >> Consider the following piece of C code: >> >> extern int done(void); >> extern void foo(int); >> >> static void test(void) { >> int i; >> for (i = 0; ; i++) { >> if (done()) >> break; >> foo(i); >> } >> } > > This is what splay emits: > > define internal void @test() { > entry: > %0 = alloca i32 > store i32 0, i32* %0 > br label %bb > > bb: ; preds = %bb1, %entry > %1 = call i32 @done() > %2 = icmp ne i32 %1, 0 > br i1 %2, label %bb2, label %bb1 > > bb1: ; preds = %bb > %3 = load i32* %0 > call void @foo(i32 %3) > %4 = add nsw i32 %3, 1 > store i32 %4, i32* %0 > br label %bb > > bb2: ; preds = %bb > ret void > } > > After "-mem2reg" you get: > > define internal void @test() { > entry: > br label %bb > > bb: ; preds = %bb1, %entry > %.0 = phi i32 [ 0, %entry ], [ %2, %bb1 ] > %0 = call i32 @done() > %1 = icmp ne i32 %0, 0 > br i1 %1, label %bb2, label %bb1 > > bb1: ; preds = %bb > call void @foo(i32 %.0) > %2 = add nsw i32 %.0, 1 > br label %bb > > bb2: ; preds = %bb > ret void > } Ping, anyone interested in turning Xi's suggestion into a patch against current sparse master? Pekka -- To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html