<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 11:22 AM, 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"><div><blockquote type="cite"><div class="gmail_quote">
<div>Just wondering about coding conventions - would it be more appropriate to change this from:</div><div>  else if (cond) { ... } else { /* unreachable */ }<br>
to:<br>  else { assert(cond); ... };<br>This would be marginally more efficient in release builds, potentially. </div></div><br>
</blockquote></div><br></div></div><div>You're correct in pointing out that most sanity checks should be written with asserts (e.g., <font face="'Courier New'">assert (cond && "msg")</font>).  However, in the case of unreachable code we know that doom and gloom are sure to follow and thus we should gracefully abort and notify the user accordingly.  There's no performance to be gained because if we've reached this state the game is already over.</div>
<div><br></div><div>In general, if you see something like <font face="'Courier New'">assert(0 && "I should have never gotten here..");</font> it should be replaced with <font face="'Courier New'">llvm_unreachable("I should have never gotten here..")</font>.  Notice that the condition is hardcoded to zero and thus the assert will always fire.</div>
</div></blockquote><div><br></div><div>Right - but that's my point, there's performance to be gained by not at all checking 'cond' in retail builds - whereas the code as you have it now will check it unnecessarily & then go to llvm_unreachable if it's false. The perf gain would be that retail builds would never check the condition because it should never be false. That sounds like an assert to me, or am I missing something?</div>
<div><br></div><div>Perhaps my example was oversimplified/unclear, what I meant was from this:</div><div><br></div><div><span class="Apple-style-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 class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br>
</span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">to this:</span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br>
</span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">      } else {</span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">        assert(Operands[i].isFP() && "Unknown operator kind!");<br>
        OS << "ConstantFP *f" << i;<br>      }</span></div></div>
</blockquote></div><br><div>Ahh.. yes, I misunderstood your example.  Here are the source comments, which best explain the use of llvm_unreachable:</div><div><br></div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; "><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><span class="Apple-style-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 class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">to:</span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">      } else </span><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">if (Operands[i].isFP()) {</span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">        OS << "ConstantFP *f" << i;<br>      }</span></div></div><div><font class="Apple-style-span" face="arial, sans-serif" size="3"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif" size="3"><span class="Apple-style-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">http://www.nondot.org/sabre/LLVMNotes/UnreachableInstruction.txt</a></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif" size="3"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif" size="3"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px;"> Chad</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif" size="3"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px;"><br></span></font></div></body></html>