<div dir="ltr"><div><div>I apologize for this message. It was based on a wrong precondition.<br><br></div>I was doing a replacement on original module while at the same time I ran JIT on a clone of that original module. Now everything is working perfectly.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 18, 2017 at 4:08 PM, Stanislav Pankevich <span dir="ltr"><<a href="mailto:s.pankevich@gmail.com" target="_blank">s.pankevich@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div>Hello,<br><br>I understand that my question is very specific, but I do hope to find someone who could have an insight into this matter.<br><br></div>I am trying to replace a branch instruction that represents Cond1 && Cond2 with a branch instruction that would represent a Cond1 || Cond2.<br><br>int testee_and_operator(int a, int b, int c) {<br>  if (a < b && b < c) {<br>    printf("left branch\n");<br>    return a;<br>  } else {<br>    printf("right branch\n");<br>    return b;<br>  }<br>}<br><br></div><div>I use the following code to test the flow inside the function above:<br><br>int test_and_operator() {<br>  int result = (testee_and_operator(1, 3, 2) == 3);<br>  return result;<br>}<br><br></div><div>Before replacement result is 1, the code goes through the right branch. After replacement I expect the code to return 0 and go through the left branch.<br><br></div>IR-wise, this code appears to be the following:<br><br>define i32 @testee_and_operator(i32, i32, i32) #0 {<br>  %4 = alloca i32, align 4<br>  %5 = alloca i32, align 4<br>  %6 = alloca i32, align 4<br>  %7 = alloca i32, align 4<br>  store i32 %0, i32* %5, align 4<br>  store i32 %1, i32* %6, align 4<br>  store i32 %2, i32* %7, align 4<br>  %8 = load i32, i32* %5, align 4<br>  %9 = load i32, i32* %6, align 4<br>  %10 = icmp slt i32 %8, %9<br>  br i1 %10, label %11, label %17 <-- replacing with another BranchInst pointing to "br i1 %10, label %15, label %11"<br><br>; <label>:11:                   <wbr>                  ; preds = %3<br>  %12 = load i32, i32* %6, align 4<br>  %13 = load i32, i32* %7, align 4<br>  %14 = icmp slt i32 %12, %13<br>  br i1 %14, label %15, label %17<br><br>; <label>:15:                   <wbr>                  ; preds = %11<br>  %16 = load i32, i32* %5, align 4<br>  store i32 %16, i32* %4, align 4<br>  br label %19<br><br>; <label>:17:                   <wbr>                  ; preds = %11, %3<br>  %18 = load i32, i32* %6, align 4<br>  store i32 %18, i32* %4, align 4<br>  br label %19<br><br>; <label>:19:                   <wbr>                  ; preds = %17, %15<br>  %20 = load i32, i32* %4, align 4<br>  ret i32 %20<br><br></div>I am trying to switch the branches of a BranchInst using the following code:<br><br>  BranchInst *replacement = BranchInst::Create(<wbr>leftBranchSubbranchInst_<wbr>leftBranchBB,<br>                              <wbr>                 leftBranchBB,<br>                              <wbr>                 cmpInst);<br><br>  replacement->insertAfter(<wbr>branchInst);<br>  branchInst-><wbr>replaceAllUsesWith(<wbr>replacement);<br>  branchInst->eraseFromParent();<br><br>  replacement->getFunction()-><wbr>dump();<br><br></div>And I am indeed seeing a dump that does correspond to LLVM bitcode created manually from a code that has if (a < b || b < c).<br><br></div>However, when I run this code using JIT, the code still goes through a right branch as it used to.<br><br></div><div>It is important to note that if I put the dump of the modified code to an .ll file I do see my code going through the left branch which makes me think that there is some sort of caching: dependency which exists between original `br` instruction and its basic blocks which still hangs in memory this is why I do not see my replacement to take any effect.<br><br></div><div>What am I missing in my code to make this transformation correct? My dump is valid but JIT seems to execute old, unmodified code.<br><br></div><div>Thanks,<br><br></div><div>Stanislav<br></div><br><div><div><div><br></div></div></div></div>
</blockquote></div><br></div>