diff options
author | Nick Lewycky <nicholas@mxc.ca> | 2009-09-20 07:31:25 +0000 |
---|---|---|
committer | Nick Lewycky <nicholas@mxc.ca> | 2009-09-20 07:31:25 +0000 |
commit | 5086a10b582cf8d9f0f1ac4d7569c24d260092c8 (patch) | |
tree | 345b1810b674b10357557c479b166a520f62c28c | |
parent | a7e959d869e698bf1b8d22044d96655632f6072e (diff) | |
download | external_llvm-5086a10b582cf8d9f0f1ac4d7569c24d260092c8.zip external_llvm-5086a10b582cf8d9f0f1ac4d7569c24d260092c8.tar.gz external_llvm-5086a10b582cf8d9f0f1ac4d7569c24d260092c8.tar.bz2 |
Peer through zext and sext to eliminate them when it is safe to do so.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82389 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Target/README.txt | 22 | ||||
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 16 | ||||
-rw-r--r-- | test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll | 3 |
3 files changed, 19 insertions, 22 deletions
diff --git a/lib/Target/README.txt b/lib/Target/README.txt index 9dd2b36..f180c2e 100644 --- a/lib/Target/README.txt +++ b/lib/Target/README.txt @@ -1656,25 +1656,3 @@ Instcombine should be able to optimize away the loads (and thus the globals). 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. - -//===---------------------------------------------------------------------===// diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index d9ef2c9..5411549 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -1665,6 +1665,22 @@ Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context, } } + // 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. diff --git a/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll b/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll index 4cf1320..8d92c3f 100644 --- a/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll +++ b/test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll @@ -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] + +@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] |