]> granicus.if.org Git - clang/commitdiff
make switch constant folding a bit stronger, handling a missed case.
authorChris Lattner <sabre@nondot.org>
Mon, 28 Feb 2011 07:16:14 +0000 (07:16 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 28 Feb 2011 07:16:14 +0000 (07:16 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126638 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGStmt.cpp
test/CodeGen/switch-dce.c

index c67d1d1b0da67f5e8ad324cd2e32793d852fb391..21fb36a72d646bc7f7ca4356045cf3484535f926 100644 (file)
@@ -895,9 +895,14 @@ static CSFC_Result CollectStatementsForCase(const Stmt *S,
         case CSFC_Success:
           // A successful result means that either 1) that the statement doesn't
           // have the case and is skippable, or 2) does contain the case value
-          // and also contains the break to exit the switch.  In either case,
-          // we continue scanning the body of the compound statement to see if
-          // the rest are skippable or have the case.
+          // and also contains the break to exit the switch.  In the later case,
+          // we just verify the rest of the statements are elidable.
+          if (FoundCase) {
+            for (++I; I != E; ++I)
+              if (CodeGenFunction::ContainsLabel(*I, true))
+                return CSFC_Failure;
+            return CSFC_Success;
+          }
           break;
         case CSFC_FallThrough:
           // If we have a fallthrough condition, then we must have found the
index 95c5e03633ab627791ac206a7cac1f8f9e7bb9c3..82b206149b9e13a27f13cf9dc0fd568028629b54 100644 (file)
@@ -167,7 +167,9 @@ void test9(int i) {
   } 
 }
 
-
+// CHECK: @test10
+// CHECK-NOT: switch
+// CHECK: ret i32
 int test10(void) {
        switch(8) {
                case 8:
@@ -180,3 +182,17 @@ int test10(void) {
        
        return 0;
 }
+
+// CHECK: @test11
+// CHECK-NOT: switch
+// CHECK: ret void
+void test11() {
+  switch (1) {
+    case 1:
+      break;
+    case 42: ;
+      int x;  // eliding var decl?
+      x = 4;
+      break;
+  }
+}