[llvm-commits] [llvm] r82389 - in /llvm/trunk: lib/Target/README.txt lib/VMCore/ConstantFold.cpp test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll

Nick Lewycky nicholas at mxc.ca
Sun Sep 20 00:31:26 PDT 2009


Author: nicholas
Date: Sun Sep 20 02:31:25 2009
New Revision: 82389

URL: http://llvm.org/viewvc/llvm-project?rev=82389&view=rev
Log:
Peer through zext and sext to eliminate them when it is safe to do so.

Modified:
    llvm/trunk/lib/Target/README.txt
    llvm/trunk/lib/VMCore/ConstantFold.cpp
    llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll

Modified: llvm/trunk/lib/Target/README.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/README.txt?rev=82389&r1=82388&r2=82389&view=diff

==============================================================================
--- llvm/trunk/lib/Target/README.txt (original)
+++ llvm/trunk/lib/Target/README.txt Sun Sep 20 02:31:25 2009
@@ -1656,25 +1656,3 @@
 See also PR4973
 
 //===---------------------------------------------------------------------===//
-
-I saw this constant expression in real code after llvm-g++ -O2:
-
-declare extern_weak i32 @0(i64)
-
-define void @foo() {
-  br i1 icmp eq (i32 zext (i1 icmp ne (i32 (i64)* @0, i32 (i64)* null) to i32),
-i32 0), label %cond_true, label %cond_false
-cond_true:
-  ret void
-cond_false:
-  ret void
-}
-
-That branch expression should be reduced to:
-
-  i1 icmp eq (i32 (i64)* @0, i32 (i64)* null)
-
-It's probably not a perf issue, I just happened to see it while examining
-something else and didn't want to forget about it.
-
-//===---------------------------------------------------------------------===//

Modified: llvm/trunk/lib/VMCore/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantFold.cpp?rev=82389&r1=82388&r2=82389&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/ConstantFold.cpp (original)
+++ llvm/trunk/lib/VMCore/ConstantFold.cpp Sun Sep 20 02:31:25 2009
@@ -1665,6 +1665,22 @@
       }
     }
 
+    // If the left hand side is an extension, try eliminating it.
+    if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
+      if (CE1->getOpcode() == Instruction::SExt ||
+          CE1->getOpcode() == Instruction::ZExt) {
+        Constant *CE1Op0 = CE1->getOperand(0);
+        Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
+        if (CE1Inverse == CE1Op0) {
+          // Check whether we can safely truncate the right hand side.
+          Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
+          if (ConstantExpr::getZExt(C2Inverse, C2->getType()) == C2) {
+            return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
+          }
+        }
+      }
+    }
+
     if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
       // If C2 is a constant expr and C1 isn't, flip them around and fold the
       // other way if possible.

Modified: llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll?rev=82389&r1=82388&r2=82389&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll (original)
+++ llvm/trunk/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll Sun Sep 20 02:31:25 2009
@@ -36,3 +36,6 @@
 ; CHECK: @M = global i1 icmp uge (i8* @X, i8* @Y) ; <i1*> [#uses=0]
 @N = global i1 icmp ne (i1 icmp ult (i8* @X, i8* @Y), i1 false)
 ; CHECK: @N = global i1 icmp ult (i8* @X, i8* @Y) ; <i1*> [#uses=0]
+
+ at O = global i1 icmp eq (i32 zext (i1 icmp ult (i8* @X, i8* @Y) to i32), i32 0)
+; CHECK: @O = global i1 icmp uge (i8* @X, i8* @Y) ; <i1*> [#uses=0]





More information about the llvm-commits mailing list