[LLVMdev] L->isLoopInvariant giving wrong results?

Sreeraj a writetosrj at gmail.com
Wed Nov 17 15:46:00 PST 2010


 my changed code.
namespace {
class MyLoopPass:public LoopPass {
bool changed;
public:
static char ID;
Loop* curLoop;
//     AnalysisType* AA;
    DominatorTree* DT;
    LoopInfo* LI;
       MyLoopPass() : LoopPass(ID){}
bool isLoopInvariantInst(Instruction &I) ;
bool runOnLoop(Loop * L, LPPassManager &lpm);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  AU.setPreservesCFG();
   AU.addRequired<DominatorTree>();
  AU.addRequired<LoopInfo>();
  AU.addRequiredID(LoopSimplifyID);
  AU.addPreserved<ScalarEvolution>();
  AU.addPreservedID(LoopSimplifyID);
}
};
}

char MyLoopPass::ID=0;
INITIALIZE_PASS(MyLoopPass, "myLICM", "simple LICM pass", false, false);

bool MyLoopPass::runOnLoop(Loop * L, LPPassManager &lpm){
//find loop pre-header
BasicBlock* pre=L->getLoopPreheader();
curLoop=L;
changed=false;
BasicBlock* lat=L->getLoopLatch();
LI = &getAnalysis<LoopInfo>();
DT = &getAnalysis<DominatorTree>();
 errs() <<"Loop Pre-Header:" << *pre;
errs() <<"Loop Latch:"  << *lat;
 for (BasicBlock::iterator i = lat->begin(), e = lat->end(); i != e; ++i){
  Instruction* hijk= i;
// find out if the statement is a branch or whatever.
  if(hijk->isSafeToSpeculativelyExecute()){
    if(isLoopInvariantInst(*hijk)){
      errs() << " " << *hijk << " is loop invariant\n";
      errs() << " hoisting to " << pre->getName() <<  "\n";
// This is where all the trouble happens.
       hijk->moveBefore(pre->getTerminator());
      changed=true;
    }
  }
}

 return changed;
}

bool MyLoopPass::isLoopInvariantInst(Instruction &I) {
  // The instruction is loop invariant if all of its operands are
loop-invariant
  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
    if (!curLoop->isLoopInvariant(I.getOperand(i)))
      return false;
  // If we got this far, the instruction is loop invariant!
  return true;
}


On Thu, Nov 18, 2010 at 5:12 AM, Sreeraj a <writetosrj at gmail.com> wrote:

> i am getting seg fault on functions like I->eraseFromParent also.
> I'm assuming that the problem comes when i change the loop structure.
>
>
> On Thu, Nov 18, 2010 at 4:05 AM, Sreeraj a <writetosrj at gmail.com> wrote:
>
>> The funny thing is that i am manually able to hoist the Loop invariant
>> instruction to the basicBlock terminator, by editing the human readable form
>> and then using llvm-as to convert it into bytecode.
>>
>>
>> On Thu, Nov 18, 2010 at 4:01 AM, Chris Lattner <clattner at apple.com>wrote:
>>
>>>
>>> On Nov 17, 2010, at 1:38 PM, Sreeraj a wrote:
>>>
>>> > 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?
>>>
>>> No idea, sorry.
>>>
>>> -Chris
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20101118/82352487/attachment.html>


More information about the llvm-dev mailing list