From e3c35ea549d5c3f3c5c54f32dbf1d791719286a9 Mon Sep 17 00:00:00 2001
From: Justin Bogner <mail@justinbogner.com>
Date: Fri, 22 Nov 2013 10:20:43 +0000
Subject: [PATCH] Revert r193994 and part of r193995

Not long ago I made the CodeGen of for loops simplify the condition at
-O0 in the same way we do for if and conditionals. Unfortunately this
ties how loops and simple conditions work together too tightly, which
makes features such as instrumentation based PGO awkward.

Ultimately, we should find a more general way to simplify the logic in
a given condition, but for now we'll just avoid using EmitBranchOnBool
for loops, like we already do for while and do loops.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@195438 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/CodeGen/CGStmt.cpp        | 6 ++++--
 test/CodeGen/branch-on-bool.c | 8 --------
 2 files changed, 4 insertions(+), 10 deletions(-)

diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp
index 1d91e926865..8325dddbb8a 100644
--- a/lib/CodeGen/CGStmt.cpp
+++ b/lib/CodeGen/CGStmt.cpp
@@ -657,7 +657,8 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S) {
 
     // C99 6.8.5p2/p4: The first substatement is executed if the expression
     // compares unequal to 0.  The condition must be a scalar type.
-    EmitBranchOnBoolExpr(S.getCond(), ForBody, ExitBlock);
+    llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
+    Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
 
     if (ExitBlock != LoopExit.getBlock()) {
       EmitBlock(ExitBlock);
@@ -737,7 +738,8 @@ void CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) {
 
   // The body is executed if the expression, contextually converted
   // to bool, is true.
-  EmitBranchOnBoolExpr(S.getCond(), ForBody, ExitBlock);
+  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
+  Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock);
 
   if (ExitBlock != LoopExit.getBlock()) {
     EmitBlock(ExitBlock);
diff --git a/test/CodeGen/branch-on-bool.c b/test/CodeGen/branch-on-bool.c
index 78dae1b1fd7..98a3845fba2 100644
--- a/test/CodeGen/branch-on-bool.c
+++ b/test/CodeGen/branch-on-bool.c
@@ -12,11 +12,3 @@ void fold_if(int a, int b) {
   else
     bar();
 }
-
-void fold_for(int a, int b) {
-  // CHECK: define {{.*}} @fold_for(
-  // CHECK-NOT: = phi
-  // CHECK: }
-  for (int i = 0; a && i < b; ++i) foo();
-  for (int i = 0; a || i < b; ++i) bar();
-}
-- 
GitLab