<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Sep 5, 2014 at 2:53 PM, Manish Gupta <span dir="ltr"><<a href="mailto:manishg@cs.ucsd.edu" target="_blank">manishg@cs.ucsd.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div>Hi All,</div><div><br></div><div>I am transforming  LLVM IR, by adding new BasicBlocks. The new BasicBlock is called based on some condition say <b>%<a href="http://re.def.bb" target="_blank">re.def.bb</a></b>. If the condition<b> %<a href="http://re.def.bb" target="_blank">re.def.bb</a></b> is true I re-define some SSA variables in new BB <b>re.def.true</b> ow. I copy old instructions in <b>re.def.false</b> basicblock. The SSA needs to be updated for all the successors basic-blocks because of this re-definitions. I am tying to explain the problem using an example below:</div><div><br></div><div><b><u>ORIGINAL BB:</u></b></div><div>if.end:                       ; preds = %if.end, %entry</div><div>  %j.07 = phi i32 [ %shl, %if.end ], [ 1, %entry ]</div><div>  %i.06 = phi i32 [ %inc, %if.end ], [ 0, %entry ]</div><div>  %shl = shl i32 %j.07, 1</div><div>  %inc = add nsw i32 %i.06, 1</div><div>  %cmp = icmp sgt i32 %i.06, 13</div><div>  %cmp1 = icmp sgt i32 %shl, %val</div><div>  %or.cond = or i1 %cmp, %cmp1</div><div>  br i1 %or.cond, label %for.end, label %if.end</div><div><br></div><div>%for.end: ...</div><div><br></div><div><b><u>TRANSFORMATION:</u></b></div><div><div>if.end:                                           ; preds = %orig.brInst, %entry</div><div>  %j.07 = phi i32 [ %shl, %orig.brInst ], [ 1, %entry ]</div><div>  %i.06 = phi i32 [ %inc, %orig.brInst ], [ 0, %entry ]</div><div>  ....</div><div><b>  %<a href="http://re.def.bb" target="_blank">re.def.bb</a> = call i1 (i32, i64, ...)* isCond()</b></div><div><b>  br i1 %<a href="http://re.def.bb" target="_blank">re.def.bb</a>, label %re.def.true, label %re.def.false</b></div></div><div><br></div><div><div><b>re.def.false</b><b>:</b></div><div>  //Original Code                                    ; preds = %if.end</div><div>  %shl = shl i32 %j.07, 1</div><div>  %inc = add nsw i32 %i.06, 1</div><div>  %cmp = icmp sgt i32 %i.06, 13</div><div>  %cmp1 = icmp sgt i32 %shl, %val</div><div>  %or.cond = or i1 %cmp, %cmp1</div><div><b>  br label %orig.brInst</b></div></div><div><br></div><div><b>re.def.true</b><b>:</b></div><div><b> //re-definitions of %shl, %inc, %or.cond</b></div><div><b> %4 = re-define(%shl)</b></div><div><b> %5 = re-define(%inc)</b></div><div><b> %6 = re-define(%or.cond)</b></div><div><br></div><div><b>orig.brInst:</b> </div><div>  br i1 %or.cond, label %for.end, label %if.end</div><div><br></div><div>%for.end: ...</div><div><br></div><div>Code added by transformation is highlight above. And it is not complete yet as phinodes are messed up. <b>orig.brInst</b> is the branch instruction from original code. Which should execute the same way to keep the control flow intact. To fix the phinodes I used SSAUpdater class the same way instruction sink function is using it in LICM. <br></div><div><br></div><div>SSAUpdate: </div><div><b>Step 1:</b> Add one more def. available for %inc, %shl and %or.cond  using AddAvailableValue in new BasicBlock <b>re.def.true</b><b>.</b></div><div>SSA.AddAvailableValue(<b>re.def.true</b><b>:</b>, NewDefInsts(shl));<br></div><div>SSA.AddAvailableValue(<b>re.def.true</b><b>:</b>, NewDefInsts(inc));<br></div><div>SSA.AddAvailableValue(<b>re.def.true</b><b>:</b>, NewDefInsts(or.cond));<br></div><div><br></div><div><b>Step 2:</b> Rewrite all the uses of %shl, %inc %or.cond to update SSA/PHI nodes</div><div>SSA.RewriteUseAfterInsertions(Users of %shl);<br></div><div>SSA.RewriteUseAfterInsertions(Users of %inc);<br></div><div>SSA.RewriteUseAfterInsertions(Users of %or.cond);<br></div><div><br></div><div>This adds some phi nodes but they are not correct. RewriteUseAfterInsertions is not able to update existing PHINodes in if.end BasicBlock. Any leads on how to fix it. Am I going the right direction or is there some other way as well. </div><div><br></div><div>Thanks!</div><span class=""><font color="#888888"><div>Manish</div><div><br></div><div><br></div><div><br></div><div><br></div></font></span></div>
</blockquote></div><br></div></div>