[llvm-dev] [Instrumentation] Basic Block in function 'main' does not have terminator!
Zhang via llvm-dev
llvm-dev at lists.llvm.org
Fri Dec 28 16:59:09 PST 2018
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
Zhang
------------------ Original ------------------
From: "张雨姗(ZHANG Yushan)-11310380 via llvm-dev"<llvm-dev at lists.llvm.org>;
Date: Sat, Dec 29, 2018 00:38 AM
To: "llvm-dev"<llvm-dev at lists.llvm.org>;
Subject: [llvm-dev] [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)
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/83b49a3f/attachment.html>
More information about the llvm-dev
mailing list