my changed code.<div><div>namespace {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>class MyLoopPass:public LoopPass {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>bool changed;</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>public:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>static char ID;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Loop* curLoop;</div>
<div>// AnalysisType* AA;</div><div> DominatorTree* DT;</div><div> LoopInfo* LI;</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>MyLoopPass() : LoopPass(ID){}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>bool isLoopInvariantInst(Instruction &I) ;</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>bool runOnLoop(Loop * L, LPPassManager &lpm);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>virtual void getAnalysisUsage(AnalysisUsage &AU) const {</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> AU.setPreservesCFG();</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span> AU.addRequired<DominatorTree>();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> AU.addRequired<LoopInfo>();</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> AU.addRequiredID(LoopSimplifyID);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> AU.addPreserved<ScalarEvolution>();</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> AU.addPreservedID(LoopSimplifyID);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>};</div>
<div>}</div><div><br></div><div>char MyLoopPass::ID=0;</div><div>INITIALIZE_PASS(MyLoopPass, "myLICM", "simple LICM pass", false, false);</div><div><br></div><div>bool MyLoopPass::runOnLoop(Loop * L, LPPassManager &lpm){</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>//find loop pre-header</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>BasicBlock* pre=L->getLoopPreheader();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>curLoop=L;</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>changed=false;</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>LI = &getAnalysis<LoopInfo>();</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>DT = &getAnalysis<DominatorTree>();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>errs() <<"Loop Pre-Header:" << *pre;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>errs() <<"Loop Latch:" << *lat;</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span></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>// find out if the statement is a branch or whatever.</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> if(hijk->isSafeToSpeculativelyExecute()){</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> if(isLoopInvariantInst(*hijk)){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> errs() << " " << *hijk << " is loop invariant\n";</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> errs() << " hoisting to " << pre->getName() << "\n";</div><div>// This is where all the trouble happens.</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span> hijk->moveBefore(pre->getTerminator());</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> changed=true;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> }</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> }</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> return changed;</div>
<div>}</div><div><br></div><div>bool MyLoopPass::isLoopInvariantInst(Instruction &I) {</div><div> // The instruction is loop invariant if all of its operands are loop-invariant</div><div> for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)</div>
<div> if (!curLoop->isLoopInvariant(I.getOperand(i)))</div><div> return false;</div><div> // If we got this far, the instruction is loop invariant!</div><div> return true;</div><div>}</div><div><br></div><br>
<div class="gmail_quote">On Thu, Nov 18, 2010 at 5:12 AM, Sreeraj a <span dir="ltr"><<a href="mailto:writetosrj@gmail.com">writetosrj@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
i am getting seg fault on functions like I->eraseFromParent also.<div>I'm assuming that the problem comes when i change the loop structure.<div><div></div><div class="h5"><br><br><div class="gmail_quote">On Thu, Nov 18, 2010 at 4:05 AM, Sreeraj a <span dir="ltr"><<a href="mailto:writetosrj@gmail.com" target="_blank">writetosrj@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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.<div>
<div></div><div><div><br></div><div><br>
<div class="gmail_quote">On Thu, Nov 18, 2010 at 4:01 AM, 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 1:38 PM, Sreeraj a wrote:<br>
<br>
> Thanks Chris,<br>
><br>
> I was able to weed out loop invariant arithmetic instructions using isLoopInvariant function.<br>
> when i try to do an instruction->moveBefore(bb->getTerminator()) i'm getting a seg fault.<br>
> any obvious reasons that i'm missing out on?<br>
<br>
</div>No idea, sorry.<br>
<font color="#888888"><br>
-Chris<br>
<br>
</font></blockquote></div><br></div>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br></div>