<div>Hi,</div>
<div> </div>
<div>I am using various LLVM optimization passes and I noticed a strange behaviour in Mem2Reg Optimization. These pass is used for SSA construction and basically removed alloca of the corresponding pointers are just used in load/stores.</div>

<div> </div>
<div>I tried the following .ll file </div>
<div><br><em>define i32 @test(i32 %y,i32 %z) {<br>entry:<br>        %X = alloca i32           ; type of %X is i32*.<br>        %X.0 = add i32 %y ,%z<br>        store i32 %X.0, i32* %X   ; Update X<br>        br label %cond_next</em></div>

<div><em></em> </div>
<div><em>cond_next:<br>        %tmp1 = alloca i32<br>        %tmp2 = sub i32  %y, 3<br>        store i32 %tmp2,i32* %tmp1<br>        ret i32 0<br>}</em></div>
<div><em></em> </div>
<div>I tried the Mem2Reg pass on above file and I got following output:</div>
<div> <br><em>; ModuleID = 'test4.bc'</em></div>
<div><em>define i32 @test(i32 %y, i32 %z) {<br>entry:<br>        %X.0 = add i32 %y, %z           ; <i32> [#uses=0]<br>        br label %cond_next</em></div>
<div><em></em> </div>
<div><em>cond_next:              ; preds = %entry<br>        %tmp1 = alloca i32              ; <i32*> [#uses=1]<br>        %tmp2 = sub i32 %y, 3           ; <i32> [#uses=1]<br>        store i32 %tmp2, i32* %tmp1<br>
        ret i32 0<br>}</em></div>
<div> </div>
<div>So, it removed the alloca from first basic block and not from others. If I remove the alloca  from cond_next basic block and instead put that also in first Basic Block, the mem2Reg optimizes this and I get following output</div>

<div><br><em>; ModuleID = 'test4.bc'</em></div>
<div><em>define i32 @test(i32 %y, i32 %z) {<br>entry:<br>        %X.0 = add i32 %y, %z           ; <i32> [#uses=0]<br>        br label %cond_next</em></div>
<div><em>cond_next:              ; preds = %entry<br>        %tmp2 = sub i32 %y, 3           ; <i32> [#uses=0]<br>        ret i32 0<br>}</em></div>
<div><em></em> </div>
<div><em></em> </div>
<div>So, is there any restriction that all the alloca should be put in first basic block?</div>
<div> </div>
<div>Thanks</div>
<div> </div>
<div> </div>
<div>Kapil</div>
<div> </div>
<div><em><br> </em></div>