[Instrumentation] Basic Block in function 'main' does not have terminator!

Hi all,

I want to add a new function to a module, which works as this:

void cmpr(int l, int r) {
if (i < l || i > r)

Here is my code:

// if required, instrument this function
BasicBlock * first = &func->getEntryBlock();
IRBuilder<> Builder(first);
CallInst* v = Builder.CreateCall2(**getTargetInst(), vl, vr**); 

Value * **getTargetInst()** {
// insert a new function to program
Constant *c = module->getOrInsertFunction("cmpr",
        Type::getInt64Ty(*context), NULL);
f = &cast<Function>(*c);

// define arguments
Function::arg_iterator args = f->arg_begin();
Value* l = args++;
l->setName("l"); // lower  bound
Value* r = args++;
r->setName("r"); // upper bound

BasicBlock *pb =  BasicBlock::Create(*context, "entry", f);
BasicBlock *blk =  BasicBlock::Create(*context, "cond_true", f);
BasicBlock *nblk =  BasicBlock::Create(*context, "cond_false", f);
IRBuilder<> Builder(pb);

// i < l
Value *lower = Builder.CreateICmpSLT(vt, l);
// i > r
Value *upper = Builder.CreateICmpSGT(vt, r);
// l - i || i - r (l > i or i > r)
Value *both = Builder.CreateOr(lower, upper);
// if (l > i || i > r), exit(0)
BranchInst *br = Builder.CreateCondBr(both, blk, nblk);

// cond_true: call exit(0)
IRBuilder<> blkBuilder(blk);
Value *one = ConstantInt::get(Type::getInt64Ty(*context), 0, true);
Value *blkv = module->getOrInsertFunction("std::exit",
        Type::getInt64Ty(*context), NULL);

blkBuilder.CreateCall(blkv, one);

    return f;​
However, I get this error (I tried to fix with adding "CreateRetVoid" and other solutions from StackOverflow but didn't work ):

Basic Block in function ‘main’ does not have terminator!
label %entry
LLVM ERROR: Broken function found, compilation aborted!

Could anyone help me to fix this? I appreciate any possible help.
BTW, how could I call standard library in instrumented code?


First, you can't just call std::exit like that, you need to mangle its name first. Google "C++ name mangling"

wrt the second part, can you try to print out the function after your transform? I'm slightly lost in your code. ``blkBuilder.CreateCall `` does need a following-up terminator, however since you mentioned you added it and still had no luck, again, I suggest you print out the IR after your transform

------------------ Original ------------------

Blocks cond_true & cond_false with both need terminators eg unreachable & ret void. But note that the error is complaining about the entry block of main.