Adding instructions to loop

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


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