[llvm-commits] [llvm] r75531 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/zext-bool-add-sub.ll
Eli Friedman
eli.friedman at gmail.com
Mon Jul 13 15:27:52 PDT 2009
Author: efriedma
Date: Mon Jul 13 17:27:52 2009
New Revision: 75531
URL: http://llvm.org/viewvc/llvm-project?rev=75531&view=rev
Log:
Canonicalize boolean +/- a constant to a select.
(I think it's reasonably clear that we want to have a canonical form for
constructs like this; if anyone thinks that a select is not the best
canonical form, please tell me.)
Added:
llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=75531&r1=75530&r2=75531&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Mon Jul 13 17:27:52 2009
@@ -2101,13 +2101,10 @@
if (SimplifyDemandedInstructionBits(I))
return &I;
- // zext(i1) - 1 -> select i1, 0, -1
+ // zext(bool) + C -> bool ? C + 1 : C
if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
- if (CI->isAllOnesValue() &&
- ZI->getOperand(0)->getType() == Type::Int1Ty)
- return SelectInst::Create(ZI->getOperand(0),
- Context->getNullValue(I.getType()),
- Context->getAllOnesValue(I.getType()));
+ if (ZI->getSrcTy() == Type::Int1Ty)
+ return SelectInst::Create(ZI->getOperand(0), AddOne(CI, Context), CI);
}
if (isa<PHINode>(LHS))
@@ -2525,6 +2522,11 @@
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
return R;
+
+ // C - zext(bool) -> bool ? C - 1 : C
+ if (ZExtInst *ZI = dyn_cast<ZExtInst>(Op1))
+ if (ZI->getSrcTy() == Type::Int1Ty)
+ return SelectInst::Create(ZI->getOperand(0), SubOne(C, Context), C);
}
if (I.getType() == Type::Int1Ty)
Added: llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll?rev=75531&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll Mon Jul 13 17:27:52 2009
@@ -0,0 +1,31 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep {zext } | count 0
+
+define i32 @a(i1 %x) {
+entry:
+ %y = zext i1 %x to i32
+ %res = add i32 %y, 1
+ ret i32 %res
+}
+
+define i32 @b(i1 %x) {
+entry:
+ %y = zext i1 %x to i32
+ %res = add i32 %y, -1
+ ret i32 %res
+}
+
+define i32 @c(i1 %x) {
+entry:
+ %y = zext i1 %x to i32
+ %res = sub i32 0, %y
+ ret i32 %res
+}
+
+define i32 @d(i1 %x) {
+entry:
+ %y = zext i1 %x to i32
+ %res = sub i32 3, %y
+ ret i32 %res
+}
+
+
More information about the llvm-commits
mailing list