[cfe-commits] r126639 - in /cfe/trunk: lib/CodeGen/CGStmt.cpp test/CodeGen/switch-dce.c

Chris Lattner sabre at nondot.org
Sun Feb 27 23:22:45 PST 2011


Author: lattner
Date: Mon Feb 28 01:22:44 2011
New Revision: 126639

URL: http://llvm.org/viewvc/llvm-project?rev=126639&view=rev
Log:
Make skipping of vardecls more precise: it's ok to skip a decl if the entire
compound stmt containing the decl is skipped.

Modified:
    cfe/trunk/lib/CodeGen/CGStmt.cpp
    cfe/trunk/test/CodeGen/switch-dce.c

Modified: cfe/trunk/lib/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGStmt.cpp?rev=126639&r1=126638&r2=126639&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGStmt.cpp Mon Feb 28 01:22:44 2011
@@ -887,9 +887,16 @@
     // the skipped statements must be skippable) or we might already have it.
     CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end();
     if (Case) {
+      // Keep track of whether we see a skipped declaration.  The code could be
+      // using the declaration even if it is skipped, so we can't optimize out
+      // the decl if the kept statements might refer to it.
+      bool HadSkippedDecl = false;
+      
       // If we're looking for the case, just see if we can skip each of the
       // substatements.
       for (; Case && I != E; ++I) {
+        HadSkippedDecl |= isa<DeclStmt>(I);
+        
         switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) {
         case CSFC_Failure: return CSFC_Failure;
         case CSFC_Success:
@@ -898,6 +905,11 @@
           // 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) {
+            // If we found the case and skipped declarations, we can't do the
+            // optimization.
+            if (HadSkippedDecl)
+              return CSFC_Failure;
+            
             for (++I; I != E; ++I)
               if (CodeGenFunction::ContainsLabel(*I, true))
                 return CSFC_Failure;
@@ -911,6 +923,11 @@
           assert(FoundCase && "Didn't find case but returned fallthrough?");
           // We recursively found Case, so we're not looking for it anymore.
           Case = 0;
+            
+          // If we found the case and skipped declarations, we can't do the
+          // optimization.
+          if (HadSkippedDecl)
+            return CSFC_Failure;
           break;
         }
       }
@@ -943,9 +960,7 @@
   // for statement or increment etc.  If we are skipping over this statement,
   // just verify it doesn't have labels, which would make it invalid to elide.
   if (Case) {
-    if (CodeGenFunction::ContainsLabel(S, true) ||
-        // Don't skip over DeclStmts, which can be used even if skipped over.
-        isa<DeclStmt>(S))
+    if (CodeGenFunction::ContainsLabel(S, true))
       return CSFC_Failure;
     return CSFC_Success;
   }

Modified: cfe/trunk/test/CodeGen/switch-dce.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/switch-dce.c?rev=126639&r1=126638&r2=126639&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/switch-dce.c (original)
+++ cfe/trunk/test/CodeGen/switch-dce.c Mon Feb 28 01:22:44 2011
@@ -87,6 +87,7 @@
     int x;  // eliding var decl?
     case 1:
       x = 4;
+      i = x;
       break;
   } 
 }
@@ -196,3 +197,22 @@
       break;
   }
 }
+
+// CHECK: @test12
+// CHECK-NOT: switch
+// CHECK: ret void
+void test12() {
+  switch (1) {
+  case 2: {
+     int a;   // Ok to skip this vardecl.
+     a = 42;
+   }
+  case 1:
+    break;
+  case 42: ;
+    int x;  // eliding var decl?
+    x = 4;
+    break;
+  }
+}
+





More information about the cfe-commits mailing list