aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Transforms
diff options
context:
space:
mode:
authorBill Wendling <isanbard@gmail.com>2008-12-01 08:23:25 +0000
committerBill Wendling <isanbard@gmail.com>2008-12-01 08:23:25 +0000
commitdae376a7bf1da40e40831de324e24a31c30cc848 (patch)
tree88f5d3e73fe833ef37b3f9885af166e1f52ebdcd /lib/Transforms
parentc1f311332398f5f02b12e147f25d93cbba1c9c5a (diff)
downloadexternal_llvm-dae376a7bf1da40e40831de324e24a31c30cc848.zip
external_llvm-dae376a7bf1da40e40831de324e24a31c30cc848.tar.gz
external_llvm-dae376a7bf1da40e40831de324e24a31c30cc848.tar.bz2
Reduce copy-and-paste code by splitting out the code into its own function.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60343 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp108
1 files changed, 50 insertions, 58 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index 47d9c25..dac37f0 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -183,6 +183,8 @@ namespace {
Instruction *FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS);
Instruction *visitAnd(BinaryOperator &I);
Instruction *FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS);
+ Instruction *FoldOrWithConstants(BinaryOperator &I,
+ Value *A, Value *B, Value *C);
Instruction *visitOr (BinaryOperator &I);
Instruction *visitXor(BinaryOperator &I);
Instruction *visitShl(BinaryOperator &I);
@@ -4445,6 +4447,50 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I,
return 0;
}
+/// FoldOrWithConstants - This helper function folds:
+///
+/// ((A|B)&1)|(B&-2)
+///
+/// into:
+///
+/// (A&1) | B
+Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I,
+ Value *A, Value *B, Value *C) {
+ Value *Op1 = I.getOperand(1);
+
+ if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
+ if (CI->getValue() == 1) {
+ Value *V1 = 0, *C2 = 0;
+ if (match(Op1, m_And(m_Value(V1), m_Value(C2)))) {
+ ConstantInt *CI2 = dyn_cast<ConstantInt>(C2);
+
+ if (!CI2) {
+ std::swap(V1, C2);
+ CI2 = dyn_cast<ConstantInt>(C2);
+ }
+
+ if (CI2) {
+ APInt NegTwo = -APInt(CI2->getValue().getBitWidth(), 2, true);
+ if (CI2->getValue().eq(NegTwo)) {
+ if (V1 == B) {
+ Instruction *NewOp =
+ InsertNewInstBefore(BinaryOperator::CreateAnd(A, CI), I);
+ return BinaryOperator::CreateOr(NewOp, B);
+ }
+ if (V1 == A) {
+ Instruction *NewOp =
+ InsertNewInstBefore(BinaryOperator::CreateAnd(B, CI), I);
+ return BinaryOperator::CreateOr(NewOp, A);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
Instruction *InstCombiner::visitOr(BinaryOperator &I) {
bool Changed = SimplifyCommutative(I);
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
@@ -4639,68 +4685,14 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
// ((A|B)&1)|(B&-2) -> (A&1) | B
if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
- if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
- if (CI->getValue() == 1) {
- Value *V1 = 0, *C2 = 0;
- if (match(Op1, m_And(m_Value(V1), m_Value(C2)))) {
- ConstantInt *CI2 = dyn_cast<ConstantInt>(C2);
-
- if (!CI2) {
- std::swap(V1, C2);
- CI2 = dyn_cast<ConstantInt>(C2);
- }
-
- if (CI2) {
- APInt NegTwo = -APInt(CI2->getValue().getBitWidth(), 2, true);
- if (CI2->getValue().eq(NegTwo)) {
- if (V1 == B) {
- Instruction *NewOp =
- InsertNewInstBefore(BinaryOperator::CreateAnd(A, CI), I);
- return BinaryOperator::CreateOr(NewOp, B);
- }
- if (V1 == A) {
- Instruction *NewOp =
- InsertNewInstBefore(BinaryOperator::CreateAnd(B, CI), I);
- return BinaryOperator::CreateOr(NewOp, A);
- }
- }
- }
- }
- }
- }
+ Instruction *Ret = FoldOrWithConstants(I, A, B, C);
+ if (Ret) return Ret;
}
// (B&-2)|((A|B)&1) -> (A&1) | B
if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) ||
match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) {
- if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
- if (CI->getValue() == 1) {
- Value *V1 = 0, *C2 = 0;
- if (match(Op0, m_And(m_Value(V1), m_Value(C2)))) {
- ConstantInt *CI2 = dyn_cast<ConstantInt>(C2);
-
- if (!CI2) {
- std::swap(V1, C2);
- CI2 = dyn_cast<ConstantInt>(C2);
- }
-
- if (CI2) {
- APInt NegTwo = -APInt(CI2->getValue().getBitWidth(), 2, true);
- if (CI2->getValue().eq(NegTwo)) {
- if (V1 == B) {
- Instruction *NewOp =
- InsertNewInstBefore(BinaryOperator::CreateAnd(A, CI), I);
- return BinaryOperator::CreateOr(NewOp, B);
- }
- if (V1 == A) {
- Instruction *NewOp =
- InsertNewInstBefore(BinaryOperator::CreateAnd(B, CI), I);
- return BinaryOperator::CreateOr(NewOp, A);
- }
- }
- }
- }
- }
- }
+ Instruction *Ret = FoldOrWithConstants(I, A, B, C);
+ if (Ret) return Ret;
}
if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1