In a pass I would like to insert a ICmpInst/BranchInst pair to check if 2 GlobalVariables are equal or not. If they are not I would like to call a function. I've tried splitting the current block and then inserting before the existing instructions, but for some reason this seems to ruin the iterator(i). What is the correct way todo something like this? void checkShadowPtr(Module &M, Function &F, inst_iterator i, GlobalVariable *shadow, Value *val) { /* Split old Block */ BasicBlock *bb = i->getParent(); BasicBlock *bb_after = i->getParent()->splitBasicBlock(&*i); bb->getTerminator()->eraseFromParent(); /* Test */ ICmpInst *test = new ICmpInst(*bb, CmpInst::ICMP_NE, shadow, val, "Shadow check"); /* Fail */ BasicBlock *trueBlock = BasicBlock::Create(M.getContext(), "Shadow Check FAIL", &F); CallInst::Create(qv_segFault, "", trueBlock); BranchInst::Create(bb_after, trueBlock); /* Profit */ BranchInst::Create(trueBlock, bb_after, test, bb); }
On Tue, Oct 11, 2011 at 7:22 PM, ret val <retval386 at gmail.com> wrote:> In a pass I would like to insert a ICmpInst/BranchInst pair to check > if 2 GlobalVariables are equal or not. If they are not I would like to > call a function. I've tried splitting the current block and then > inserting before the existing instructions, but for some reason this > seems to ruin the iterator(i). What is the correct way todo something > like this? > > void checkShadowPtr(Module &M, Function &F, inst_iterator i, > GlobalVariable *shadow, Value *val) {inst_iterator is hiding iterators which aren't going to stay consistent across this kind of change. I'd suggest explicitly using Function::iterator and BasicBlock::iterator to keep track of where you are in the function; that should make it obvious what is going wrong. -Eli
All I really see from this change is that I somehow created a infinite loop. Arn't these implemented as linked lists? Shouldn't they be stable? I am very confused. I also can't seem to find a example where much is being add. Thank you On Tue, Oct 11, 2011 at 10:36 PM, Eli Friedman <eli.friedman at gmail.com> wrote:> On Tue, Oct 11, 2011 at 7:22 PM, ret val <retval386 at gmail.com> wrote: >> In a pass I would like to insert a ICmpInst/BranchInst pair to check >> if 2 GlobalVariables are equal or not. If they are not I would like to >> call a function. I've tried splitting the current block and then >> inserting before the existing instructions, but for some reason this >> seems to ruin the iterator(i). What is the correct way todo something >> like this? >> >> void checkShadowPtr(Module &M, Function &F, inst_iterator i, >> GlobalVariable *shadow, Value *val) { > > inst_iterator is hiding iterators which aren't going to stay > consistent across this kind of change. I'd suggest explicitly using > Function::iterator and BasicBlock::iterator to keep track of where you > are in the function; that should make it obvious what is going wrong. > > -Eli >