[cfe-commits] r76530 - in /cfe/trunk: lib/Analysis/CFG.cpp test/Analysis/dead-stores.c

Mike Stump mrs at apple.com
Mon Jul 20 18:27:56 PDT 2009


Author: mrs
Date: Mon Jul 20 20:27:50 2009
New Revision: 76530

URL: http://llvm.org/viewvc/llvm-project?rev=76530&view=rev
Log:
Wire up CFG improvements for do { } while () when the condition is known.

Modified:
    cfe/trunk/lib/Analysis/CFG.cpp
    cfe/trunk/test/Analysis/dead-stores.c

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=76530&r1=76529&r2=76530&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Mon Jul 20 20:27:50 2009
@@ -1256,11 +1256,22 @@
 }
 
 CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
-  // "do...while" is a control-flow statement.  Thus we stop processing the
-  // current block.
+  // See if this is a known constant.
+  bool KnownTrue = false;
+  bool KnownFalse = false;
+  Expr::EvalResult Result;
+  if (D->getCond()->Evaluate(Result, *Context)
+      && Result.Val.isInt()) {
+    if (Result.Val.getInt().getBoolValue())
+      KnownTrue = true;
+    else
+      KnownFalse = true;
+  }
 
   CFGBlock* LoopSuccessor = NULL;
 
+  // "do...while" is a control-flow statement.  Thus we stop processing the
+  // current block.
   if (Block) {
     if (!FinishBlock(Block))
       return 0;
@@ -1330,13 +1341,21 @@
     CFGBlock *LoopBackBlock = createBlock();
     LoopBackBlock->setLoopTarget(D);
 
-    // Add the loop body entry as a successor to the condition.
-    ExitConditionBlock->addSuccessor(LoopBackBlock);
+    if (KnownFalse)
+      ExitConditionBlock->addSuccessor(0);
+    else {
+      // Add the loop body entry as a successor to the condition.
+      ExitConditionBlock->addSuccessor(LoopBackBlock);
+    }
   }
 
-  // Link up the condition block with the code that follows the loop.
-  // (the false branch).
-  ExitConditionBlock->addSuccessor(LoopSuccessor);
+  if (KnownTrue)
+    ExitConditionBlock->addSuccessor(0);
+  else {
+    // Link up the condition block with the code that follows the loop.  (the
+    // false branch).
+    ExitConditionBlock->addSuccessor(LoopSuccessor);
+  }
 
   // There can be no more statements in the body block(s) since we loop back to
   // the body.  NULL out Block to force lazy creation of another block.

Modified: cfe/trunk/test/Analysis/dead-stores.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/dead-stores.c?rev=76530&r1=76529&r2=76530&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.c (original)
+++ cfe/trunk/test/Analysis/dead-stores.c Mon Jul 20 20:27:50 2009
@@ -204,6 +204,9 @@
   int y13 = 4;
   int y14 = 4;
   int y15 = 4;
+  int y16 = 4;
+  int y17 = 4;
+  int y18 = 4;
 
   ++x; // expected-warning{{never read}}
   ++y1;
@@ -221,6 +224,9 @@
   ++y13;
   ++y14;
   ++y15;
+  ++y16;
+  ++y17;
+  ++y18;
 
   switch (j) {
   case 1:
@@ -277,34 +283,46 @@
     (void)y10;
     break;
   case 10:
-    while (1) {
-      (void)y11;
+    while (0) {
+      (void)x;
     }
-    (void)x;
+    (void)y11;
     break;
   case 11:
-    while (0) {
-      (void)x;
+    while (1) {
+      (void)y12;
     }
-    (void)y12;
+    (void)x;
     break;
   case 12:
-    for (;;) {
+    do {
       (void)y13;
+    } while (0);
+    (void)y14;
+    break;
+  case 13:
+    do {
+      (void)y15;
+    } while (1);
+    (void)x;
+    break;
+  case 14:
+    for (;;) {
+      (void)y16;
     }
     (void)x;    
     break;
-  case 13:
+  case 15:
     for (;1;) {
-      (void)y14;
+      (void)y17;
     }
     (void)x;
     break;
-  case 14:
+  case 16:
     for (;0;) {
       (void)x;
     }
-    (void)y15;
+    (void)y18;
     break;
   }
 }





More information about the cfe-commits mailing list