[LLVMdev] Adding instructions to loop

Sean Peisert peisert at gmail.com
Fri Aug 12 17:28:25 PDT 2005


I am attempting to add instructions before and after all loops.  To do
this, I've added a simple function as follows:

    void addToAllLoops(Loop * IL) {
        addToAllLoops(IL);

        BasicBlock * PH = IL->getLoopPreheader();
        Instruction *InstrCallPre = new CallInst(PrintLoopBegin, "",
PH->getPrev());

        return;
    }

The function added is trivial:

void printloopbegin() {
    fprintf (stderr,"Loop Beginning\n");
}

The function is called from runOnFunction() as follows:

      PrintLoopBegin = F.getParent()->getOrInsertFunction("printloopbegin",
                                                    Type::VoidTy, NULL);
      for (LoopInfo::iterator IL = LI->begin(), E = LI->end(); IL!= E; ++IL)
        addToAllLoops(*IL);

Unfortunately, compiling with this results in an "Illegal instruction
(core dumped)."  Is PrintLoopBegin defined in the wrong place (should
it be in the addToAllLoops() function somehow)?  What about the method
of attempting to insert the instruction right before the Preheader by
doing getLoopPreheader()->getPrev() ?

Thanks,
Sean




More information about the llvm-dev mailing list