aboutsummaryrefslogtreecommitdiff
path: root/lib/Analysis/CFG.cpp
diff options
context:
space:
mode:
authorMike Stump <mrs@apple.com>2009-07-21 00:38:52 +0000
committerMike Stump <mrs@apple.com>2009-07-21 00:38:52 +0000
commit5f20363dc8ea094b3f6139f52084beb10d6fcd85 (patch)
treeecad34a5ecb06cbf26513a48a0a2ab951e32a9f4 /lib/Analysis/CFG.cpp
parent6133c12166ec5376f2209180c908588970f4aebc (diff)
Wire up CFG improvements for while when the condition is known.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76522 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/CFG.cpp')
-rw-r--r--lib/Analysis/CFG.cpp32
1 files changed, 26 insertions, 6 deletions
diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp
index 11deec4a23..3dc364292e 100644
--- a/lib/Analysis/CFG.cpp
+++ b/lib/Analysis/CFG.cpp
@@ -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.