Hi, <div>I'm trying to write a simple pass to print out Loop invariant instructions, using the </div><div>Loop::isLoopInvariant(Instruction *I)</div><div>function. it is giving me false value on instructions which should be loop invariant.</div>
<div><br></div><div>the code i am using is:</div><div><br></div><div><div>bool MyLoopPass::runOnLoop(Loop * L, LPPassManager &lpm){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>BasicBlock* lat=L->getLoopLatch();</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>for (BasicBlock::iterator i = lat->begin(), e = lat->end(); i != e; ++i){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> Instruction* hijk= i;</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> if(L->isLoopInvariant(hijk))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> errs() << "hurray " << *hijk << " is loop invariant\n";</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> else</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> errs() << "bad luck\n";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> }</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> return false;</div><div>}</div></div><div><br></div><div>the loop latch prints as:</div><div><div>bb: ; preds = %bb1</div>
<div> store i32 21, i32* %ij, align 4</div><div> store i32 10, i32* %j, align 4</div><div> %0 = load i32* %i, align 4</div><div> %1 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4 x i8]* @.str, i32 0, i32 0), i32 %0) nounwind</div>
<div> %2 = load i32* %i, align 4</div><div> %3 = sub nsw i32 %2, 1</div><div> store i32 %3, i32* %i, align 4</div><div> br label %bb1</div></div><div><br></div><div>the first instruction " store i32 21, i32* %ij, align 4"</div>
<div>is clearly loop invariant (corresponds to j=10; in c-code)</div><div>but all i am getting is "bad luck".</div><div><br></div><div>am i doing something wrong here? or is the function incomplete/wrong?</div><div>
<br></div><meta http-equiv="content-type" content="text/html; charset=utf-8">