[llvm-commits] [llvm] r132732 - /llvm/trunk/utils/TableGen/FastISelEmitter.cpp

Chad Rosier mcrosier at apple.com
Fri Jun 10 11:57:40 PDT 2011


On Jun 10, 2011, at 11:22 AM, David Blaikie wrote:

>> Just wondering about coding conventions - would it be more appropriate to change this from:
>>   else if (cond) { ... } else { /* unreachable */ }
>> to:
>>   else { assert(cond); ... };
>> This would be marginally more efficient in release builds, potentially. 
>> 
> 
> You're correct in pointing out that most sanity checks should be written with asserts (e.g., assert (cond && "msg")).  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.
> 
> In general, if you see something like assert(0 && "I should have never gotten here.."); it should be replaced with llvm_unreachable("I should have never gotten here..").  Notice that the condition is hardcoded to zero and thus the assert will always fire.
> 
> 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?
> 
> Perhaps my example was oversimplified/unclear, what I meant was from this:
> 
>       } else if (Operands[i].isFP()) {
>         OS << "ConstantFP *f" << i;
>       } else {
>         llvm_unreachable("Unknown operand kind!");
>       }
> 
> to this:
> 
>       } else {
>         assert(Operands[i].isFP() && "Unknown operator kind!");
>         OS << "ConstantFP *f" << i;
>       }

Ahh.. yes, I misunderstood your example.  Here are the source comments, which best explain the use of llvm_unreachable:

/// Marks that the current location is not supposed to be reachable.
/// In !NDEBUG builds, prints the message and location info to stderr.
/// In NDEBUG builds, becomes an optimizer hint that the current location
/// is not supposed to be reachable.  On compilers that don't support
/// such hints, prints a reduced message instead.
///
/// Use this instead of assert(0).  It conveys intent more clearly and
/// allows compilers to omit some unnecessary code.
In the case of a release build an unreachable hint is emitted.  The compiler then uses this hint to transform:

      } else if (Operands[i].isFP()) {
        OS << "ConstantFP *f" << i;
      } else {
        llvm_unreachable("Unknown operand kind!");
      }

to:

      } else if (Operands[i].isFP()) {
        OS << "ConstantFP *f" << i;
      }

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: http://www.nondot.org/sabre/LLVMNotes/UnreachableInstruction.txt

 Chad

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20110610/94bfa110/attachment.html>


More information about the llvm-commits mailing list