<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><includetail><!--<![endif]--></includetail></div></div>