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

Mike Stump mrs at apple.com
Mon Jul 20 18:12:52 PDT 2009


Author: mrs
Date: Mon Jul 20 20:12:51 2009
New Revision: 76529

URL: http://llvm.org/viewvc/llvm-project?rev=76529&view=rev
Log:
Wire up for statement CFG improvements for conditionals that are 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=76529&r1=76528&r2=76529&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Mon Jul 20 20:12:51 2009
@@ -863,10 +863,24 @@
 }
 
 CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
-  // "for" 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 (F->getCond() && F->getCond()->Evaluate(Result, *Context)
+      && Result.Val.isInt()) {
+    if (Result.Val.getInt().getBoolValue())
+      KnownTrue = true;
+    else
+      KnownFalse = true;
+  }
+  if (F->getCond() == 0)
+    KnownTrue = true;
+
   CFGBlock* LoopSuccessor = NULL;
 
+  // "for" is a control-flow statement.  Thus we stop processing the current
+  // block.
   if (Block) {
     if (!FinishBlock(Block))
       return 0;
@@ -949,13 +963,21 @@
         return 0;
     }
 
-    // This new body block is a successor to our "exit" condition block.
-    ExitConditionBlock->addSuccessor(BodyBlock);
+    if (KnownFalse)
+      ExitConditionBlock->addSuccessor(0);
+    else {
+      // This new body block is a successor to our "exit" condition block.
+      ExitConditionBlock->addSuccessor(BodyBlock);
+    }
   }
 
-  // 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);
+  }
 
   // If the loop contains initialization, create a new block for those
   // statements.  This block can also contain statements that precede the loop.
@@ -1099,9 +1121,6 @@
 }
 
 CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
-  // "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;
@@ -1116,6 +1135,8 @@
 
   CFGBlock* LoopSuccessor = NULL;
 
+  // "while" is a control-flow statement.  Thus we stop processing the current
+  // block.
   if (Block) {
     if (!FinishBlock(Block))
       return 0;

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

==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.c (original)
+++ cfe/trunk/test/Analysis/dead-stores.c Mon Jul 20 20:12:51 2009
@@ -201,6 +201,9 @@
   int y10 = 4;
   int y11 = 4;
   int y12 = 4;
+  int y13 = 4;
+  int y14 = 4;
+  int y15 = 4;
 
   ++x; // expected-warning{{never read}}
   ++y1;
@@ -215,6 +218,9 @@
   ++y10;
   ++y11;
   ++y12;
+  ++y13;
+  ++y14;
+  ++y15;
 
   switch (j) {
   case 1:
@@ -282,5 +288,23 @@
     }
     (void)y12;
     break;
+  case 12:
+    for (;;) {
+      (void)y13;
+    }
+    (void)x;    
+    break;
+  case 13:
+    for (;1;) {
+      (void)y14;
+    }
+    (void)x;
+    break;
+  case 14:
+    for (;0;) {
+      (void)x;
+    }
+    (void)y15;
+    break;
   }
 }





More information about the cfe-commits mailing list