aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2006-11-14 07:46:50 +0000
committerChris Lattner <sabre@nondot.org>2006-11-14 07:46:50 +0000
commite511b74f4aa0b74a0d1923beb8b95b966eed2050 (patch)
tree565a6f8689975cd27f674ff2de0cabc9bd2cc467
parent9961cf1fed0ab71cf4a7cfd8646e474c2c1ef480 (diff)
implement InstCombine/shift-simplify.ll by transforming:
(X >> Z) op (Y >> Z) -> (X op Y) >> Z for all shifts and all ops={and/or/xor}. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31729 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp49
1 files changed, 46 insertions, 3 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index 2e3f9aaa10..80a4860553 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -3310,9 +3310,9 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
}
// fold (and (cast A), (cast B)) -> (cast (and A, B))
- if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
- const Type *SrcTy = Op0C->getOperand(0)->getType();
- if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
+ if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) {
+ if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
+ const Type *SrcTy = Op0C->getOperand(0)->getType();
if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegral() &&
// Only do this if the casts both really cause code to be generated.
ValueRequiresCast(Op0C->getOperand(0), I.getType(), TD) &&
@@ -3323,6 +3323,21 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
InsertNewInstBefore(NewOp, I);
return new CastInst(NewOp, I.getType());
}
+ }
+ }
+
+ // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts.
+ if (ShiftInst *SI1 = dyn_cast<ShiftInst>(Op1)) {
+ if (ShiftInst *SI0 = dyn_cast<ShiftInst>(Op0))
+ if (SI0->getOpcode() == SI1->getOpcode() &&
+ SI0->getOperand(1) == SI1->getOperand(1) &&
+ (SI0->hasOneUse() || SI1->hasOneUse())) {
+ Instruction *NewOp =
+ InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0),
+ SI1->getOperand(0),
+ SI0->getName()), I);
+ return new ShiftInst(SI1->getOpcode(), NewOp, SI1->getOperand(1));
+ }
}
return Changed ? &I : 0;
@@ -3567,6 +3582,20 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
}
}
}
+
+ // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts.
+ if (ShiftInst *SI1 = dyn_cast<ShiftInst>(Op1)) {
+ if (ShiftInst *SI0 = dyn_cast<ShiftInst>(Op0))
+ if (SI0->getOpcode() == SI1->getOpcode() &&
+ SI0->getOperand(1) == SI1->getOperand(1) &&
+ (SI0->hasOneUse() || SI1->hasOneUse())) {
+ Instruction *NewOp =
+ InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0),
+ SI1->getOperand(0),
+ SI0->getName()), I);
+ return new ShiftInst(SI1->getOpcode(), NewOp, SI1->getOperand(1));
+ }
+ }
if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1
if (A == Op1) // ~A | A == -1
@@ -3879,6 +3908,20 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
return new CastInst(NewOp, I.getType());
}
}
+
+ // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts.
+ if (ShiftInst *SI1 = dyn_cast<ShiftInst>(Op1)) {
+ if (ShiftInst *SI0 = dyn_cast<ShiftInst>(Op0))
+ if (SI0->getOpcode() == SI1->getOpcode() &&
+ SI0->getOperand(1) == SI1->getOperand(1) &&
+ (SI0->hasOneUse() || SI1->hasOneUse())) {
+ Instruction *NewOp =
+ InsertNewInstBefore(BinaryOperator::createXor(SI0->getOperand(0),
+ SI1->getOperand(0),
+ SI0->getName()), I);
+ return new ShiftInst(SI1->getOpcode(), NewOp, SI1->getOperand(1));
+ }
+ }
return Changed ? &I : 0;
}