]> granicus.if.org Git - clang/commitdiff
Wire up CFG improvements for while when the condition is known.
authorMike Stump <mrs@apple.com>
Tue, 21 Jul 2009 00:38:52 +0000 (00:38 +0000)
committerMike Stump <mrs@apple.com>
Tue, 21 Jul 2009 00:38:52 +0000 (00:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76522 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CFG.cpp
test/Analysis/dead-stores.c

index 11deec4a23291e85adde5432802f88fa1425a027..3dc364292e7cb1a197b738796233219d9e7499b4 100644 (file)
@@ -701,7 +701,7 @@ CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
 }
 
 CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
-  // See if this is a known constant first.
+  // See if this is a known constant.
   bool KnownTrue = false;
   bool KnownFalse = false;
   Expr::EvalResult Result;
@@ -1102,6 +1102,18 @@ 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;
+  Expr::EvalResult Result;
+  if (W->getCond()->Evaluate(Result, *Context)
+      && Result.Val.isInt()) {
+    if (Result.Val.getInt().getBoolValue())
+      KnownTrue = true;
+    else
+      KnownFalse = true;
+  }
+
   CFGBlock* LoopSuccessor = NULL;
 
   if (Block) {
@@ -1170,13 +1182,21 @@ CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
         return 0;
     }
 
-    // Add the loop body entry as a successor to the condition.
-    ExitConditionBlock->addSuccessor(BodyBlock);
+    if (KnownFalse)
+      ExitConditionBlock->addSuccessor(0);
+    else {
+      // Add the loop body entry as a successor to the condition.
+      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);
+  }
 
   // There can be no more statements in the condition block since we loop back
   // to this block.  NULL out Block to force lazy creation of another block.
index f8677e1cfaf7a5e737b141166650a2b6add3c8bc..4798eb152a1d18dbd32cd431b80a939c6b424035 100644 (file)
@@ -199,6 +199,8 @@ void f22() {
   int y8 = 4;
   int y9 = 4;
   int y10 = 4;
+  int y11 = 4;
+  int y12 = 4;
 
   ++x; // expected-warning{{never read}}
   ++y1;
@@ -211,6 +213,8 @@ void f22() {
   ++y8;
   ++y9;
   ++y10;
+  ++y11;
+  ++y12;
 
   switch (j) {
   case 1:
@@ -265,5 +269,18 @@ void f22() {
   case 9:
     (void)(1 || x);
     (void)y10;
+    break;
+  case 10:
+    while (1) {
+      (void)y11;
+    }
+    (void)x;
+    break;
+  case 11:
+    while (0) {
+      (void)x;
+    }
+    (void)y12;
+    break;
   }
 }