]> granicus.if.org Git - clang/commitdiff
Detect when the current generation point is unreachable after emitting
authorDaniel Dunbar <daniel@zuster.org>
Sun, 19 Jul 2009 08:23:12 +0000 (08:23 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Sun, 19 Jul 2009 08:23:12 +0000 (08:23 +0000)
expressions.
 - This generally catches the important case of noreturn functions.

 - With the last two changes, we are down to 152 unreachable blocks emitted on
   403.gcc, vs the 1805 we started with.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76364 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGStmt.cpp
test/CodeGen/unreachable.c

index 4e6ec82eb5288ace85b461d7dfcbed9d5e2fdcf4..04b26ea80ae52eb6e9b36d9bfd047fe8caf08ee1 100644 (file)
@@ -68,10 +68,19 @@ void CodeGenFunction::EmitStmt(const Stmt *S) {
   default:
     // Must be an expression in a stmt context.  Emit the value (to get
     // side-effects) and ignore the result.
-    if (const Expr *E = dyn_cast<Expr>(S)) {
-      EmitAnyExpr(E, 0, false, true);
-    } else {
+    if (!isa<Expr>(S))
       ErrorUnsupported(S, "statement");
+
+    EmitAnyExpr(cast<Expr>(S), 0, false, true);
+    
+    // Expression emitters don't handle unreachable blocks yet, so look for one
+    // explicitly here. This handles the common case of a call to a noreturn
+    // function.
+    if (llvm::BasicBlock *CurBB = Builder.GetInsertBlock()) {
+      if (CurBB->empty() && CurBB->use_empty()) {
+        CurBB->eraseFromParent();
+        Builder.ClearInsertionPoint();
+      }
     }
     break;
   case Stmt::IndirectGotoStmtClass:  
index ab029b613da7a97ec32c8b97562ca497b2092c21..ea4f0478bbd49389d3f527092da3c58b871ef51f 100644 (file)
@@ -1,6 +1,7 @@
 // RUN: clang-cc -emit-llvm -o %t %s &&
 // RUN: grep '@unreachable' %t | count 0
 
+extern void abort() __attribute__((noreturn));
 extern int unreachable();
 
 int f0() {
@@ -24,3 +25,13 @@ int f2(int i) {
   a = i + 1;
   return a;
 }
+
+int f3(int i) {
+  if (i) {
+    return 0;
+  } else {
+    abort();
+  }
+  unreachable();
+  return 3;
+}