<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Jun 10, 2011, at 12:05 PM, David Blaikie wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; "><div style="word-wrap:break-word"><div><div class="h5">Ahh.. yes, I misunderstood your example. Here are the source comments, which best explain the use of llvm_unreachable:</div>
</div><div><br></div><div><span style="font-family:monospace;white-space:pre-wrap"><pre><i><b>/// Marks that the current location is not supposed to be reachable.
</b></i><i><b>/// In !NDEBUG builds, prints the message and location info to stderr.
</b></i><i><b>/// In NDEBUG builds, becomes an optimizer hint that the current location
</b></i><i><b>/// is not supposed to be reachable. On compilers that don't support
</b></i><i><b>/// such hints, prints a reduced message instead.
</b></i><i><b>///
</b></i><i><b>/// Use this instead of assert(0). It conveys intent more clearly and
</b></i><i><b>/// allows compilers to omit some unnecessary code.</b></i></pre></span><div>In the case of a release build an unreachable hint is emitted. The compiler then uses this hint to transform:</div></div><div><br>
</div><div><div class="im"><div><span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px"> } else if (Operands[i].isFP()) {<br> OS << "ConstantFP *f" << i;<br> } else {<br>
llvm_unreachable("Unknown operand kind!");<br> }</span></div><div><span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px"><br></span></div></div><div><span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px">to:</span></div>
<div class="im"><div><span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px"><br></span></div><div><span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px"> } else </span><span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px">if (Operands[i].isFP()) {</span></div>
<div><span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px"> OS << "ConstantFP *f" << i;<br> }</span></div></div></div><div><font face="arial, sans-serif" size="3"><span style="border-collapse:collapse;font-size:13px"><br>
</span></font></div><div><font face="arial, sans-serif" size="3"><span style="border-collapse:collapse;font-size:13px">I hope this helps answer your question. In the end the compiler is being smart about removing the unreachable code. For more details you could also checkout: <a href="http://www.nondot.org/sabre/LLVMNotes/UnreachableInstruction.txt" target="_blank">http://www.nondot.org/sabre/LLVMNotes/UnreachableInstruction.txt</a></span></font></div>
</div></blockquote><div><br></div><div>Ah, ok - thanks for the explanation. I didn't realize llvm_unreachable was that advanced/involved (though I suppose I Should've expected as much). </div><div><br></div><div>So both versions (the assert & the unreachable) seem to have similar semantics (performance & correctness) given a sufficiently advanced compiler (that uses the unreachable to remove the isFP() call) - makes sense.</div>
<div><br></div><div>[is there any policy/recommendations on which way to go in this case? I guess it's not a problem to leave this up to the author of any particular piece of LLVM code about which expression they prefer/find more clear in a given situation]</div></div></blockquote><div><br></div><div>Like the comment says, llvm_unreachable() should be used in lieu of assert (0). Otherwise, assert (cond && "msg") should be used for code that is expected to execute.</div><br><blockquote type="cite"><div class="gmail_quote"><div>Thanks again for the help,</div><div>- David </div></div><br>
</blockquote></div><br></body></html>