Hi,
I'm trying to write a simple pass to print out Loop invariant instructions,
using the
Loop::isLoopInvariant(Instruction *I)
function. it is giving me false value on instructions which should be loop
invariant.
the code i am using is:
bool MyLoopPass::runOnLoop(Loop * L, LPPassManager &lpm){
BasicBlock* lat=L->getLoopLatch();
for (BasicBlock::iterator i = lat->begin(), e = lat->end(); i != e; ++i){
Instruction* hijk= i;
if(L->isLoopInvariant(hijk))
errs() << "hurray " << *hijk << " is loop
invariant\n";
else
errs() << "bad luck\n";
}
return false;
}
the loop latch prints as:
bb: ; preds = %bb1
store i32 21, i32* %ij, align 4
store i32 10, i32* %j, align 4
%0 = load i32* %i, align 4
%1 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4 x
i8]* @.str, i32 0, i32 0), i32 %0) nounwind
%2 = load i32* %i, align 4
%3 = sub nsw i32 %2, 1
store i32 %3, i32* %i, align 4
br label %bb1
the first instruction " store i32 21, i32* %ij, align 4"
is clearly loop invariant (corresponds to j=10; in c-code)
but all i am getting is "bad luck".
am i doing something wrong here? or is the function incomplete/wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20101117/f0430294/attachment.html>
On Nov 17, 2010, at 2:20 AM, Sreeraj a wrote:> Hi, > I'm trying to write a simple pass to print out Loop invariant instructions, using the > Loop::isLoopInvariant(Instruction *I) > function. it is giving me false value on instructions which should be loop invariant. > > the code i am using is: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. -Chris> > bool MyLoopPass::runOnLoop(Loop * L, LPPassManager &lpm){ > BasicBlock* lat=L->getLoopLatch(); > for (BasicBlock::iterator i = lat->begin(), e = lat->end(); i != e; ++i){ > Instruction* hijk= i; > if(L->isLoopInvariant(hijk)) > errs() << "hurray " << *hijk << " is loop invariant\n"; > else > errs() << "bad luck\n"; > } > return false; > } > > the loop latch prints as: > bb: ; preds = %bb1 > store i32 21, i32* %ij, align 4 > store i32 10, i32* %j, align 4 > %0 = load i32* %i, align 4 > %1 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4 x i8]* @.str, i32 0, i32 0), i32 %0) nounwind > %2 = load i32* %i, align 4 > %3 = sub nsw i32 %2, 1 > store i32 %3, i32* %i, align 4 > br label %bb1 > > the first instruction " store i32 21, i32* %ij, align 4" > is clearly loop invariant (corresponds to j=10; in c-code) > but all i am getting is "bad luck". > > am i doing something wrong here? or is the function incomplete/wrong? > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Thanks Chris, I was able to weed out loop invariant arithmetic instructions using isLoopInvariant function. when i try to do an instruction->moveBefore(bb->getTerminator()) i'm getting a seg fault. any obvious reasons that i'm missing out on? Thanks in advance On Wed, Nov 17, 2010 at 10:41 PM, Chris Lattner <clattner at apple.com> wrote:> > On Nov 17, 2010, at 2:20 AM, Sreeraj a wrote: > > > Hi, > > I'm trying to write a simple pass to print out Loop invariant > instructions, using the > > Loop::isLoopInvariant(Instruction *I) > > function. it is giving me false value on instructions which should be > loop invariant. > > > > the code i am using is: > > 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. > > -Chris > > > > > bool MyLoopPass::runOnLoop(Loop * L, LPPassManager &lpm){ > > BasicBlock* lat=L->getLoopLatch(); > > for (BasicBlock::iterator i = lat->begin(), e = lat->end(); i != e; > ++i){ > > Instruction* hijk= i; > > if(L->isLoopInvariant(hijk)) > > errs() << "hurray " << *hijk << " is loop invariant\n"; > > else > > errs() << "bad luck\n"; > > } > > return false; > > } > > > > the loop latch prints as: > > bb: ; preds = %bb1 > > store i32 21, i32* %ij, align 4 > > store i32 10, i32* %j, align 4 > > %0 = load i32* %i, align 4 > > %1 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds > ([4 x i8]* @.str, i32 0, i32 0), i32 %0) nounwind > > %2 = load i32* %i, align 4 > > %3 = sub nsw i32 %2, 1 > > store i32 %3, i32* %i, align 4 > > br label %bb1 > > > > the first instruction " store i32 21, i32* %ij, align 4" > > is clearly loop invariant (corresponds to j=10; in c-code) > > but all i am getting is "bad luck". > > > > am i doing something wrong here? or is the function incomplete/wrong? > > > > _______________________________________________ > > LLVM Developers mailing list > > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20101118/22da45a9/attachment.html>