<div dir="ltr"><div>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.<br></div><br><div class="gmail_quote"><div dir="ltr">On Sat, 29 Dec 2018 at 03:08, 张雨姗(ZHANG Yushan)-11310380 via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="font-family:SimSun,STSong;font-size:large"><div><font 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 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 style="margin:0.8em 0px 0.8em 2em;padding:0px 0px 0px 0.7em;border-left:2px solid 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 style="margin:0.8em 0px 0.8em 2em;padding:0px 0px 0px 0.7em;border-left:2px solid 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 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><u></u><u></u></div></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>