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

Jeremy Lakeman via llvm-dev llvm-dev at lists.llvm.org
Fri Dec 28 19:34:40 PST 2018


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.

On Sat, 29 Dec 2018 at 03:08, 张雨姗(ZHANG Yushan)-11310380 via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> 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
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20181229/4fee3b80/attachment.html>


More information about the llvm-dev mailing list