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

张雨姗(ZHANG Yushan)-11310380 via llvm-dev llvm-dev at lists.llvm.org
Fri Dec 28 08:38:09 PST 2018


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)
        std::exit(0);
}

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",
        FunctionType::getVoidTy(*context),
        Type::getInt64Ty(*context),
        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);
Builder.SetInsertPoint(ret);

// cond_true: call exit(0)
IRBuilder<> blkBuilder(blk);
Value *one = ConstantInt::get(Type::getInt64Ty(*context), 0, true);
Value *blkv = module->getOrInsertFunction("std::exit",
        FunctionType::getVoidTy(*context),
        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?


Thanks, 
Yushan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20181229/b4a4f826/attachment.html>


More information about the llvm-dev mailing list