<div><br></div><div><includetail><div> First, you can't just call std::exit like that, you need to mangle its name first. Google "C++ name mangling"</div><div><br></div><div>wrt the second part, can you try to print out the function after your transform? I'm slightly lost in your code. ``<span style="white-space: pre-wrap; background-color: rgba(0, 0, 0, 0);">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</span></div><div><br></div><div><br></div><div><br></div><div>Zhang </div><div style="font:Verdana normal 14px;color:#000;"><div style="FONT-SIZE: 12px;FONT-FAMILY: Arial Narrow;padding:2px 0 2px 0;">------------------ Original ------------------</div><div style="FONT-SIZE: 12px;background:#efefef;padding:8px;"><div id="menu_sender"><b>From: </b> "张雨姗(ZHANG Yushan)-11310380 via llvm-dev"<llvm-dev@lists.llvm.org>;</div><div><b>Date: </b> Sat, Dec 29, 2018 00:38 AM</div><div><b>To: </b> "llvm-dev"<llvm-dev@lists.llvm.org>; <wbr></div><div></div><div><b>Subject: </b> [llvm-dev] [Instrumentation] Basic Block in function 'main' does not have terminator!</div></div><div> </div><div style="position:relative;"><div style="font-family:SimSun,STSong;font-size:large;;"><div><font editorwarp__="1" style="display: inline; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;">Hi all,</font></div><font editorwarp__="1" style="display: inline; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><div style="font-weight: normal; display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><br></div><div style="font-weight: normal; display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;">I want to add a new function to a module, which works as this:</div><div style="font-weight: normal; display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;"><blockquote formatblock="1" style="margin: 0.8em 0px 0.8em 2em; padding: 0px 0px 0px 0.7em; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(221, 221, 221); display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;">void cmpr(int l, int r) {<br>    if (i < l || i > r)<br>        std::exit(0);<br>}</blockquote></div></div><div style="font-weight: normal; display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;">Here is my code:</div><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><pre style="font-family: Verdana; font-size: 14px; display: block; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><span style="font-weight: normal;">// if required, instrument this function<br>BasicBlock * first = &func->getEntryBlock();<br>IRBuilder<> Builder(first);<br>CallInst* v = Builder.CreateCall2(</span><b>getTargetInst(), vl, vr</b>); </pre><pre style="font-weight: normal; font-family: Verdana; font-size: 14px; display: block; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><br></pre></div><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><pre style="font-family: Verdana; font-size: 14px; display: block; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><span style="font-weight: normal;">Value * </span><b>getTargetInst()</b> {</pre><pre style="font-weight: normal; font-family: Verdana; font-size: 14px; display: block; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;">// insert a new function to program<br>Constant *c = module->getOrInsertFunction("cmpr",<br>        FunctionType::getVoidTy(*context),<br>        Type::getInt64Ty(*context),<br>        Type::getInt64Ty(*context), NULL);<br>f = &cast<Function>(*c);<br><br>// define arguments<br>Function::arg_iterator args = f->arg_begin();<br>Value* l = args++;<br>l->setName("l"); // lower  bound<br>Value* r = args++;<br>r->setName("r"); // upper bound<br><br>BasicBlock *pb =  BasicBlock::Create(*context, "entry", f);<br>BasicBlock *blk =  BasicBlock::Create(*context, "cond_true", f);<br>BasicBlock *nblk =  BasicBlock::Create(*context, "cond_false", f);<br>IRBuilder<> Builder(pb);<br><br>// i < l<br>Value *lower = Builder.CreateICmpSLT(vt, l);<br>// i > r<br>Value *upper = Builder.CreateICmpSGT(vt, r);<br>// l - i || i - r (l > i or i > r)<br>Value *both = Builder.CreateOr(lower, upper);<br>// if (l > i || i > r), exit(0)<br>BranchInst *br = Builder.CreateCondBr(both, blk, nblk);<br>Builder.SetInsertPoint(ret);<br><br>// cond_true: call exit(0)<br>IRBuilder<> blkBuilder(blk);<br>Value *one = ConstantInt::get(Type::getInt64Ty(*context), 0, true);<br>Value *blkv = module->getOrInsertFunction("std::exit",<br>        FunctionType::getVoidTy(*context),<br>        Type::getInt64Ty(*context), NULL);<br><br>blkBuilder.CreateCall(blkv, one);<br></pre><pre style="font-weight: normal; font-family: Verdana; font-size: 14px; display: block; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;">    return f;​<br>}</pre></div><div style="font-weight: normal; display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-style: normal;"><pre style="font-family: Verdana; font-size: 14px; display: block; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;">However, I get this error (<span style="background-color: rgba(0, 0, 0, 0);">I tried to fix with adding "CreateRetVoid" and other solutions from StackOverflow but didn't work </span><span style="background-color: rgba(0, 0, 0, 0);">):</span></pre><blockquote formatblock="1" style="margin: 0.8em 0px 0.8em 2em; padding: 0px 0px 0px 0.7em; border-left-width: 2px; border-left-style: solid; border-left-color: rgb(221, 221, 221); display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;">Basic Block in function 'main' does not have terminator!<br>label %entry<br>LLVM ERROR: Broken function found, compilation aborted!</blockquote></div></font><div></div><font editorwarp__="1" style="display: inline; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;"><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;"><br></div><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;">Could anyone help me to fix this?  I appreciate any possible help.</div><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;">BTW, how could I call standard library in instrumented code?</div><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;"><br></div><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;">Thanks, </div><div style="display: block; font-size: 14px; font-family: Verdana; color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0); font-weight: normal; font-style: normal;">Yushan</div></font><div></div><div> </div><div></div></div></div></div><!--<![endif]--></includetail></div>