[LLVMdev] insert ICmpInst/BranchIns in Pass?

ret val retval386 at gmail.com
Tue Oct 11 19:22:07 PDT 2011


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);
}



More information about the llvm-dev mailing list