[LLVMdev] Fwd: Adding instructions to loop

Sean Peisert peisert at gmail.com
Fri Aug 12 18:09:58 PDT 2005

Please ignore the previous email; I can't believe the stupid coding
errors I made.

---------- Forwarded message ----------
From: Sean Peisert <peisert at gmail.com>
Date: Aug 12, 2005 5:28 PM
Subject: Adding instructions to loop
To: LLVM Developers Mailing List <llvmdev at cs.uiuc.edu>, Chris Lattner
<sabre at nondot.org>

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) {

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


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)

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() ?


More information about the llvm-dev mailing list