[cfe-commits] r82433 - in /cfe/trunk: docs/LanguageExtensions.html include/clang/Basic/Builtins.def lib/CodeGen/CGBuiltin.cpp test/Sema/builtins.c

Chris Lattner sabre at nondot.org
Sun Sep 20 20:10:00 PDT 2009


Author: lattner
Date: Sun Sep 20 22:09:59 2009
New Revision: 82433

URL: http://llvm.org/viewvc/llvm-project?rev=82433&view=rev
Log:
Implement __builtin_unreachable(), a GCC 4.5 extension.


Modified:
    cfe/trunk/docs/LanguageExtensions.html
    cfe/trunk/include/clang/Basic/Builtins.def
    cfe/trunk/lib/CodeGen/CGBuiltin.cpp
    cfe/trunk/test/Sema/builtins.c

Modified: cfe/trunk/docs/LanguageExtensions.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LanguageExtensions.html?rev=82433&r1=82432&r2=82433&view=diff

==============================================================================
--- cfe/trunk/docs/LanguageExtensions.html (original)
+++ cfe/trunk/docs/LanguageExtensions.html Sun Sep 20 22:09:59 2009
@@ -27,6 +27,7 @@
 <li><a href="#builtins">Builtin Functions</a>
   <ul>
   <li><a href="#__builtin_shufflevector">__builtin_shufflevector</a></li>
+  <li><a href="#__builtin_unreachable">__builtin_unreachable</a></li>
   </ul>
 </li>
 <li><a href="#targetspecific">Target-Specific Extensions</a>
@@ -310,6 +311,47 @@
 the number of indices specified.
 </p>
 
+<p>Query for this feature with __has_builtin(__builtin_shufflevector).</p>
+
+<!-- ======================================================================= -->
+<h3 id="__builtin_unreachable">__builtin_unreachable</h3>
+<!-- ======================================================================= -->
+
+<p><tt>__builtin_unreachable</tt> is used to indicate that a specific point in
+the program cannot be reached, even if the compiler might otherwise think it
+can.  This is useful to improve optimization and eliminates certain warnings.
+For example, without the <tt>__builtin_unreachable</tt> in the example below,
+the compiler assumes that the inline asm can fall through and prints a "function
+declared 'noreturn' should not return" warning.
+</p>
+
+<p><b>Syntax:</b></p>
+
+<pre>
+__builtin_unreachable()
+</pre>
+
+<p><b>Example of Use:</b></p>
+
+<pre>
+void myabort(void) __attribute__((noreturn));
+void myabort(void) {
+    asm("int3");
+    __builtin_unreachable();
+}
+</pre>
+
+<p><b>Description:</b></p>
+
+<p>The __builtin_unreachable() builtin has completely undefined behavior.  Since
+it has undefined behavior, it is a statement that it is never reached and the
+optimizer can take advantage of this to produce better code.  This builtin takes
+no arguments and produces a void result.
+</p>
+
+<p>Query for this feature with __has_builtin(__builtin_unreachable).</p>
+
+
 <!-- ======================================================================= -->
 <h2 id="targetspecific">Target-Specific Extensions</h2>
 <!-- ======================================================================= -->

Modified: cfe/trunk/include/clang/Basic/Builtins.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Builtins.def?rev=82433&r1=82432&r2=82433&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/Builtins.def (original)
+++ cfe/trunk/include/clang/Basic/Builtins.def Sun Sep 20 22:09:59 2009
@@ -271,6 +271,7 @@
 BUILTIN(__builtin_expect, "iii"   , "nc")
 BUILTIN(__builtin_prefetch, "vvC*.", "nc")
 BUILTIN(__builtin_trap, "v", "n")
+BUILTIN(__builtin_unreachable, "v", "nr")
 
 BUILTIN(__builtin_shufflevector, "v."   , "nc")
 

Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=82433&r1=82432&r2=82433&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Sun Sep 20 22:09:59 2009
@@ -224,7 +224,12 @@
     Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0);
     return RValue::get(Builder.CreateCall(F));
   }
-
+  case Builtin::BI__builtin_unreachable: {
+    Value *V = Builder.CreateUnreachable();
+    Builder.ClearInsertionPoint();
+    return RValue::get(V);
+  }
+      
   case Builtin::BI__builtin_powi:
   case Builtin::BI__builtin_powif:
   case Builtin::BI__builtin_powil: {

Modified: cfe/trunk/test/Sema/builtins.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/builtins.c?rev=82433&r1=82432&r2=82433&view=diff

==============================================================================
--- cfe/trunk/test/Sema/builtins.c (original)
+++ cfe/trunk/test/Sema/builtins.c Sun Sep 20 22:09:59 2009
@@ -41,3 +41,14 @@
   old = __sync_fetch_and_add(&old);  // expected-error {{too few arguments to function call}}
   old = __sync_fetch_and_add((int**)0, 42i); // expected-warning {{imaginary constants are an extension}}
 }
+
+
+// rdar://7236819
+void test10(void) __attribute__((noreturn));
+
+void test10(void) {
+  __asm__("int3");  
+  __builtin_unreachable();
+ 
+  // No warning about falling off the end of a noreturn function.
+}





More information about the cfe-commits mailing list