[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
Reid Spencer
reid at x10sys.com
Sat Mar 24 22:34:08 PDT 2007
Changes in directory llvm/lib/Transforms/Scalar:
InstructionCombining.cpp updated: 1.687 -> 1.688
---
Log message:
Refactor several ConstantExpr::getXXX calls with ConstantInt arguments
using the facilities of APInt. While this duplicates a tiny fraction of
the constant folding code, it also makes the code easier to read and
avoids large ConstantExpr overhead for simple, known computations.
---
Diffs of the changes: (+40 -27)
InstructionCombining.cpp | 67 ++++++++++++++++++++++++++++-------------------
1 files changed, 40 insertions(+), 27 deletions(-)
Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.687 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.688
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.687 Sun Mar 25 00:01:29 2007
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp Sun Mar 25 00:33:51 2007
@@ -540,8 +540,9 @@
if (I->getOpcode() == Instruction::Shl)
if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
// The multiplier is really 1 << CST.
- Constant *One = ConstantInt::get(V->getType(), 1);
- CST = cast<ConstantInt>(ConstantExpr::getShl(One, CST));
+ APInt Multiplier(V->getType()->getPrimitiveSizeInBits(), 0);
+ Multiplier.set(CST->getZExtValue()); // set bit is == 1 << CST
+ CST = ConstantInt::get(Multiplier);
return I->getOperand(0);
}
}
@@ -558,14 +559,31 @@
return false;
}
-// AddOne, SubOne - Add or subtract a constant one from an integer constant...
+/// AddOne - Add one to a ConstantInt
static ConstantInt *AddOne(ConstantInt *C) {
- return cast<ConstantInt>(ConstantExpr::getAdd(C,
- ConstantInt::get(C->getType(), 1)));
+ APInt One(C->getType()->getPrimitiveSizeInBits(),1);
+ return ConstantInt::get(C->getValue() + One);
}
+/// SubOne - Subtract one from a ConstantInt
static ConstantInt *SubOne(ConstantInt *C) {
- return cast<ConstantInt>(ConstantExpr::getSub(C,
- ConstantInt::get(C->getType(), 1)));
+ APInt One(C->getType()->getPrimitiveSizeInBits(),1);
+ return ConstantInt::get(C->getValue() - One);
+}
+/// Add - Add two ConstantInts together
+static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) {
+ return ConstantInt::get(C1->getValue() + C2->getValue());
+}
+/// And - Bitwise AND two ConstantInts together
+static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) {
+ return ConstantInt::get(C1->getValue() & C2->getValue());
+}
+/// Subtract - Subtract one ConstantInt from another
+static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) {
+ return ConstantInt::get(C1->getValue() - C2->getValue());
+}
+/// Multiply - Multiply two ConstantInts together
+static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) {
+ return ConstantInt::get(C1->getValue() * C2->getValue());
}
/// ComputeMaskedBits - Determine which of the bits specified in Mask are
@@ -1966,7 +1984,7 @@
// X*C1 + X*C2 --> X * (C1+C2)
ConstantInt *C1;
if (X == dyn_castFoldableMul(RHS, C1))
- return BinaryOperator::createMul(X, ConstantExpr::getAdd(C1, C2));
+ return BinaryOperator::createMul(X, Add(C1, C2));
}
// X + X*C --> X * (C+1)
@@ -1986,14 +2004,12 @@
if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
Value *X = 0;
- if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X
- Constant *C= ConstantExpr::getSub(CRHS, ConstantInt::get(I.getType(), 1));
- return BinaryOperator::createSub(C, X);
- }
+ if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
+ return BinaryOperator::createSub(SubOne(CRHS), X);
// (X & FF00) + xx00 -> (X+xx00) & FF00
if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
- Constant *Anded = ConstantExpr::getAnd(CRHS, C2);
+ Constant *Anded = And(CRHS, C2);
if (Anded == CRHS) {
// See if all bits from the first bit set in the Add RHS up are included
// in the mask. First, get the rightmost bit.
@@ -2075,8 +2091,8 @@
// C - ~X == X + (1+C)
Value *X = 0;
if (match(Op1, m_Not(m_Value(X))))
- return BinaryOperator::createAdd(X,
- ConstantExpr::getAdd(C, ConstantInt::get(I.getType(), 1)));
+ return BinaryOperator::createAdd(X, AddOne(C));
+
// -(X >>u 31) -> (X >>s 31)
// -(X >>s 31) -> (X >>u 31)
if (C->isNullValue()) {
@@ -2125,7 +2141,7 @@
else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
// C1-(X+C2) --> (C1-C2)-X
- return BinaryOperator::createSub(ConstantExpr::getSub(CI1, CI2),
+ return BinaryOperator::createSub(Subtract(CI1, CI2),
Op1I->getOperand(0));
}
}
@@ -2167,8 +2183,7 @@
// X - X*C --> X * (1-C)
ConstantInt *C2 = 0;
if (dyn_castFoldableMul(Op1I, C2) == Op0) {
- Constant *CP1 =
- ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2);
+ Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2);
return BinaryOperator::createMul(Op0, CP1);
}
}
@@ -2188,14 +2203,12 @@
ConstantInt *C1;
if (Value *X = dyn_castFoldableMul(Op0, C1)) {
- if (X == Op1) { // X*C - X --> X * (C-1)
- Constant *CP1 = ConstantExpr::getSub(C1, ConstantInt::get(I.getType(),1));
- return BinaryOperator::createMul(Op1, CP1);
- }
+ if (X == Op1) // X*C - X --> X * (C-1)
+ return BinaryOperator::createMul(Op1, SubOne(C1));
ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
if (X == dyn_castFoldableMul(Op1, C2))
- return BinaryOperator::createMul(Op1, ConstantExpr::getSub(C1, C2));
+ return BinaryOperator::createMul(Op1, Subtract(C1, C2));
}
return 0;
}
@@ -2411,7 +2424,7 @@
if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0),
- ConstantExpr::getMul(RHS, LHSRHS));
+ Multiply(RHS, LHSRHS));
}
if (!RHS->isZero()) { // avoid X udiv 0
@@ -2906,7 +2919,7 @@
Value *X = Op->getOperand(0);
Constant *Together = 0;
if (!Op->isShift())
- Together = ConstantExpr::getAnd(AndRHS, OpRHS);
+ Together = And(AndRHS, OpRHS);
switch (Op->getOpcode()) {
case Instruction::Xor:
@@ -3112,7 +3125,7 @@
switch (LHSI->getOpcode()) {
default: return 0;
case Instruction::And:
- if (ConstantExpr::getAnd(N, Mask) == Mask) {
+ if (And(N, Mask) == Mask) {
// If the AndRHS is a power of two minus one (0+1+), this is simple.
if ((Mask->getValue().countLeadingZeros() +
Mask->getValue().countPopulation()) ==
@@ -3137,7 +3150,7 @@
// If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
if ((Mask->getValue().countLeadingZeros() +
Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
- && ConstantExpr::getAnd(N, Mask)->isNullValue())
+ && And(N, Mask)->isNullValue())
break;
return 0;
}
More information about the llvm-commits
mailing list