[LLVMdev] L->isLoopInvariant giving wrong results?

Chris Lattner clattner at apple.com
Wed Nov 17 09:11:31 PST 2010


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





More information about the llvm-dev mailing list