[cfe-commits] r126592 - in /cfe/trunk/lib/CodeGen: CGExpr.cpp CGExprScalar.cpp CGStmt.cpp CodeGenFunction.cpp CodeGenFunction.h

Chris Lattner sabre at nondot.org
Sun Feb 27 15:02:32 PST 2011


Author: lattner
Date: Sun Feb 27 17:02:32 2011
New Revision: 126592

URL: http://llvm.org/viewvc/llvm-project?rev=126592&view=rev
Log:
Change the interface to ConstantFoldsToSimpleInteger to not encode 
a bool + success into one tri-state integer, simplifying things.

Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/CodeGen/CGStmt.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=126592&r1=126591&r2=126592&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Sun Feb 27 17:02:32 2011
@@ -1715,10 +1715,10 @@
   }
 
   const Expr *condExpr = expr->getCond();
-
-  if (int condValue = ConstantFoldsToSimpleInteger(condExpr)) {
+  bool CondExprBool;
+  if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
     const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
-    if (condValue == -1) std::swap(live, dead);
+    if (!CondExprBool) std::swap(live, dead);
 
     if (!ContainsLabel(dead))
       return EmitLValue(live);

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=126592&r1=126591&r2=126592&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Sun Feb 27 17:02:32 2011
@@ -2257,8 +2257,9 @@
   
   // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
   // If we have 1 && X, just emit X without inserting the control flow.
-  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
-    if (Cond == 1) { // If we have 1 && X, just emit X.
+  bool LHSCondVal;
+  if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
+    if (LHSCondVal) { // If we have 1 && X, just emit X.
       Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
       // ZExt result to int or bool.
       return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext");
@@ -2309,8 +2310,9 @@
   
   // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
   // If we have 0 || X, just emit X without inserting the control flow.
-  if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
-    if (Cond == -1) { // If we have 0 || X, just emit X.
+  bool LHSCondVal;
+  if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
+    if (!LHSCondVal) { // If we have 0 || X, just emit X.
       Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
       // ZExt result to int or bool.
       return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext");
@@ -2409,9 +2411,10 @@
 
   // If the condition constant folds and can be elided, try to avoid emitting
   // the condition and the dead arm.
-  if (int Cond = CGF.ConstantFoldsToSimpleInteger(condExpr)){
+  bool CondExprBool;
+  if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
     Expr *live = lhsExpr, *dead = rhsExpr;
-    if (Cond == -1) std::swap(live, dead);
+    if (!CondExprBool) std::swap(live, dead);
 
     // If the dead side doesn't have labels we need, and if the Live side isn't
     // the gnu missing ?: extension (which we could handle, but don't bother

Modified: cfe/trunk/lib/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGStmt.cpp?rev=126592&r1=126591&r2=126592&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGStmt.cpp Sun Feb 27 17:02:32 2011
@@ -359,10 +359,12 @@
 
   // If the condition constant folds and can be elided, try to avoid emitting
   // the condition and the dead arm of the if/else.
-  if (int Cond = ConstantFoldsToSimpleInteger(S.getCond())) {
+  bool CondConstant;
+  if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant)) {
     // Figure out which block (then or else) is executed.
-    const Stmt *Executed = S.getThen(), *Skipped  = S.getElse();
-    if (Cond == -1)  // Condition false?
+    const Stmt *Executed = S.getThen();
+    const Stmt *Skipped  = S.getElse();
+    if (!CondConstant)  // Condition false?
       std::swap(Executed, Skipped);
 
     // If the skipped block has no labels in it, just emit the executed block.

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=126592&r1=126591&r2=126592&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Sun Feb 27 17:02:32 2011
@@ -408,22 +408,23 @@
 }
 
 
-/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
-/// a constant, or if it does but contains a label, return 0.  If it constant
-/// folds to 'true' and does not contain a label, return 1, if it constant folds
-/// to 'false' and does not contain a label, return -1.
-int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
+/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
+/// to a constant, or if it does but contains a label, return false.  If it
+/// constant folds return true and set the boolean result in Result.
+bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
+                                                   bool &ResultBool) {
   // FIXME: Rename and handle conversion of other evaluatable things
   // to bool.
   Expr::EvalResult Result;
   if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
       Result.HasSideEffects)
-    return 0;  // Not foldable, not integer or not fully evaluatable.
+    return false;  // Not foldable, not integer or not fully evaluatable.
 
   if (CodeGenFunction::ContainsLabel(Cond))
-    return 0;  // Contains a label.
+    return false;  // Contains a label.
 
-  return Result.Val.getInt().getBoolValue() ? 1 : -1;
+  ResultBool = Result.Val.getInt().getBoolValue();
+  return true;
 }
 
 
@@ -442,14 +443,17 @@
     if (CondBOp->getOpcode() == BO_LAnd) {
       // If we have "1 && X", simplify the code.  "0 && X" would have constant
       // folded if the case was simple enough.
-      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
+      bool ConstantBool;
+      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
+          ConstantBool) {
         // br(1 && X) -> br(X).
         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
       }
 
       // If we have "X && 1", simplify the code to use an uncond branch.
       // "X && 0" would have been constant folded to 0.
-      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
+      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
+          ConstantBool) {
         // br(X && 1) -> br(X).
         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
       }
@@ -468,17 +472,22 @@
       eval.end(*this);
 
       return;
-    } else if (CondBOp->getOpcode() == BO_LOr) {
+    }
+    
+    if (CondBOp->getOpcode() == BO_LOr) {
       // If we have "0 || X", simplify the code.  "1 || X" would have constant
       // folded if the case was simple enough.
-      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
+      bool ConstantBool;
+      if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
+          !ConstantBool) {
         // br(0 || X) -> br(X).
         return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
       }
 
       // If we have "X || 0", simplify the code to use an uncond branch.
       // "X || 1" would have been constant folded to 1.
-      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
+      if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
+          !ConstantBool) {
         // br(X || 0) -> br(X).
         return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
       }

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=126592&r1=126591&r2=126592&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Sun Feb 27 17:02:32 2011
@@ -2045,10 +2045,9 @@
   static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
 
   /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
-  /// to a constant, or if it does but contains a label, return 0.  If it
-  /// constant folds to 'true' and does not contain a label, return 1, if it
-  /// constant folds to 'false' and does not contain a label, return -1.
-  int ConstantFoldsToSimpleInteger(const Expr *Cond);
+  /// to a constant, or if it does but contains a label, return false.  If it
+  /// constant folds return true and set the boolean result in Result.
+  bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
 
   /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
   /// if statement) to the specified blocks.  Based on the condition, this might





More information about the cfe-commits mailing list