Thanks Chris,<div><br></div><div>I was able to weed out loop invariant arithmetic instructions using isLoopInvariant function.</div><div> when i try to do an instruction->moveBefore(bb->getTerminator()) i'm getting a seg fault. </div>
<div>any obvious reasons that i'm missing out on?</div><div><br></div><div>Thanks in advance</div><div><br><div class="gmail_quote">On Wed, Nov 17, 2010 at 10:41 PM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@apple.com" target="_blank">clattner@apple.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><br>
On Nov 17, 2010, at 2:20 AM, Sreeraj a wrote:<br>
<br>
> Hi,<br>
> I'm trying to write a simple pass to print out Loop invariant instructions, using the<br>
> Loop::isLoopInvariant(Instruction *I)<br>
> function. it is giving me false value on instructions which should be loop invariant.<br>
><br>
> the code i am using is:<br>
<br>
</div>The isLoopInvariant method just works for scalar operations like 'add' and 'multiply'.  It doesn't apply to memory operations, because hoisting them requires more analysis (e.g. mod-ref analysis for the rest of the loop).  Please see the LICM pass to see how to do this.<br>


<br>
-Chris<br>
<div><br>
><br>
> bool MyLoopPass::runOnLoop(Loop * L, LPPassManager &lpm){<br>
>       BasicBlock* lat=L->getLoopLatch();<br>
>       for (BasicBlock::iterator i = lat->begin(), e = lat->end(); i != e; ++i){<br>
>         Instruction* hijk= i;<br>
>         if(L->isLoopInvariant(hijk))<br>
>           errs() << "hurray " << *hijk << " is loop invariant\n";<br>
>         else<br>
>           errs() << "bad luck\n";<br>
>        }<br>
>        return false;<br>
> }<br>
><br>
> the loop latch prints as:<br>
> bb:                                               ; preds = %bb1<br>
>   store i32 21, i32* %ij, align 4<br>
>   store i32 10, i32* %j, align 4<br>
>   %0 = load i32* %i, align 4<br>
>   %1 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4 x i8]* @.str, i32 0, i32 0), i32 %0) nounwind<br>
>   %2 = load i32* %i, align 4<br>
>   %3 = sub nsw i32 %2, 1<br>
>   store i32 %3, i32* %i, align 4<br>
>   br label %bb1<br>
><br>
> the first instruction "  store i32 21, i32* %ij, align 4"<br>
> is clearly loop invariant (corresponds to j=10; in c-code)<br>
> but all i am getting is "bad luck".<br>
><br>
> am i doing something wrong here? or is the function incomplete/wrong?<br>
><br>
</div>> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br>
</blockquote></div><br></div>