// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
// if the LHS is a constant zero (which is the 'negate' form).
//
-static inline Value *dyn_castNegVal(Value *V, LLVMContext *Context) {
+static inline Value *dyn_castNegVal(Value *V) {
if (BinaryOperator::isNeg(V))
return BinaryOperator::getNegArgument(V);
// instruction if the LHS is a constant negative zero (which is the 'negate'
// form).
//
-static inline Value *dyn_castFNegVal(Value *V, LLVMContext *Context) {
+static inline Value *dyn_castFNegVal(Value *V) {
if (BinaryOperator::isFNeg(V))
return BinaryOperator::getFNegArgument(V);
return 0;
}
-static inline Value *dyn_castNotVal(Value *V, LLVMContext *Context) {
+static inline Value *dyn_castNotVal(Value *V) {
if (BinaryOperator::isNot(V))
return BinaryOperator::getNotArgument(V);
// Constants can be considered to be not'ed values...
if (ConstantInt *C = dyn_cast<ConstantInt>(V))
- return ConstantInt::get(*Context, ~C->getValue());
+ return ConstantInt::get(C->getType(), ~C->getValue());
return 0;
}
// non-constant operand of the multiply, and set CST to point to the multiplier.
// Otherwise, return null.
//
-static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST,
- LLVMContext *Context) {
+static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
if (V->hasOneUse() && V->getType()->isInteger())
if (Instruction *I = dyn_cast<Instruction>(V)) {
if (I->getOpcode() == Instruction::Mul)
// The multiplier is really 1 << CST.
uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
uint32_t CSTVal = CST->getLimitedValue(BitWidth);
- CST = ConstantInt::get(*Context, APInt(BitWidth, 1).shl(CSTVal));
+ CST = ConstantInt::get(V->getType()->getContext(),
+ APInt(BitWidth, 1).shl(CSTVal));
return I->getOperand(0);
}
}
}
/// AddOne - Add one to a ConstantInt
-static Constant *AddOne(Constant *C, LLVMContext *Context) {
+static Constant *AddOne(Constant *C) {
return ConstantExpr::getAdd(C,
ConstantInt::get(C->getType(), 1));
}
/// SubOne - Subtract one from a ConstantInt
-static Constant *SubOne(ConstantInt *C, LLVMContext *Context) {
+static Constant *SubOne(ConstantInt *C) {
return ConstantExpr::getSub(C,
ConstantInt::get(C->getType(), 1));
}
/// MultiplyOverflows - True if the multiply can not be expressed in an int
/// this size.
-static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign,
- LLVMContext *Context) {
+static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
uint32_t W = C1->getBitWidth();
APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
if (sign) {
/// are any bits set in the constant that are not demanded. If so, shrink the
/// constant and return true.
static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo,
- APInt Demanded, LLVMContext *Context) {
+ APInt Demanded) {
assert(I && "No instruction?");
assert(OpNo < I->getNumOperands() && "Operand index too large");
// This instruction is producing bits that are not demanded. Shrink the RHS.
Demanded &= OpC->getValue();
- I->setOperand(OpNo, ConstantInt::get(*Context, Demanded));
+ I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded));
return true;
}
return Constant::getNullValue(VTy);
// If the RHS is a constant, see if we can simplify it.
- if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero, Context))
+ if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
return I;
// Output known-1 bits are only known if set in both the LHS & RHS.
return I->getOperand(1);
// If the RHS is a constant, see if we can simplify it.
- if (ShrinkDemandedConstant(I, 1, DemandedMask, Context))
+ if (ShrinkDemandedConstant(I, 1, DemandedMask))
return I;
// Output known-0 bits are only known if clear in both the LHS & RHS.
// If the RHS is a constant, see if we can simplify it.
// FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
- if (ShrinkDemandedConstant(I, 1, DemandedMask, Context))
+ if (ShrinkDemandedConstant(I, 1, DemandedMask))
return I;
RHSKnownZero = KnownZeroOut;
assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
// If the operands are constants, see if we can simplify them.
- if (ShrinkDemandedConstant(I, 1, DemandedMask, Context) ||
- ShrinkDemandedConstant(I, 2, DemandedMask, Context))
+ if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
+ ShrinkDemandedConstant(I, 2, DemandedMask))
return I;
// Only known if known in both the LHS and RHS.
// If the RHS of the add has bits set that can't affect the input, reduce
// the constant.
- if (ShrinkDemandedConstant(I, 1, InDemandedBits, Context))
+ if (ShrinkDemandedConstant(I, 1, InDemandedBits))
return I;
// Avoid excess work.
/// 'shouldApply' and 'apply' methods.
///
template<typename Functor>
-static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F,
- LLVMContext *Context) {
+static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
unsigned Opcode = Root.getOpcode();
Value *LHS = Root.getOperand(0);
// AddRHS - Implements: X + X --> X << 1
struct AddRHS {
Value *RHS;
- LLVMContext *Context;
- AddRHS(Value *rhs, LLVMContext *C) : RHS(rhs), Context(C) {}
+ explicit AddRHS(Value *rhs) : RHS(rhs) {}
bool shouldApply(Value *LHS) const { return LHS == RHS; }
Instruction *apply(BinaryOperator &Add) const {
return BinaryOperator::CreateShl(Add.getOperand(0),
// iff C1&C2 == 0
struct AddMaskingAnd {
Constant *C2;
- LLVMContext *Context;
- AddMaskingAnd(Constant *c, LLVMContext *C) : C2(c), Context(C) {}
+ explicit AddMaskingAnd(Constant *c) : C2(c) {}
bool shouldApply(Value *LHS) const {
ConstantInt *C1;
- return match(LHS, m_And(m_Value(), m_ConstantInt(C1)), *Context) &&
+ return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) &&
ConstantExpr::getAnd(C1, C2)->isNullValue();
}
Instruction *apply(BinaryOperator &Add) const {
// zext(bool) + C -> bool ? C + 1 : C
if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
if (ZI->getSrcTy() == Type::Int1Ty)
- return SelectInst::Create(ZI->getOperand(0), AddOne(CI, Context), CI);
+ return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI);
}
if (isa<PHINode>(LHS))
ConstantInt *XorRHS = 0;
Value *XorLHS = 0;
if (isa<ConstantInt>(RHSC) &&
- match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)), *Context)) {
+ match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue();
// X + X --> X << 1
if (I.getType()->isInteger()) {
- if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS, Context), Context))
+ if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS)))
return Result;
if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) {
// -A + B --> B - A
// -A + -B --> -(A + B)
- if (Value *LHSV = dyn_castNegVal(LHS, Context)) {
+ if (Value *LHSV = dyn_castNegVal(LHS)) {
if (LHS->getType()->isIntOrIntVector()) {
- if (Value *RHSV = dyn_castNegVal(RHS, Context)) {
+ if (Value *RHSV = dyn_castNegVal(RHS)) {
Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum");
InsertNewInstBefore(NewAdd, I);
- return BinaryOperator::CreateNeg(*Context, NewAdd);
+ return BinaryOperator::CreateNeg(NewAdd);
}
}
// A + -B --> A - B
if (!isa<Constant>(RHS))
- if (Value *V = dyn_castNegVal(RHS, Context))
+ if (Value *V = dyn_castNegVal(RHS))
return BinaryOperator::CreateSub(LHS, V);
ConstantInt *C2;
- if (Value *X = dyn_castFoldableMul(LHS, C2, Context)) {
+ if (Value *X = dyn_castFoldableMul(LHS, C2)) {
if (X == RHS) // X*C + X --> X * (C+1)
- return BinaryOperator::CreateMul(RHS, AddOne(C2, Context));
+ return BinaryOperator::CreateMul(RHS, AddOne(C2));
// X*C1 + X*C2 --> X * (C1+C2)
ConstantInt *C1;
- if (X == dyn_castFoldableMul(RHS, C1, Context))
+ if (X == dyn_castFoldableMul(RHS, C1))
return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2));
}
// X + X*C --> X * (C+1)
- if (dyn_castFoldableMul(RHS, C2, Context) == LHS)
- return BinaryOperator::CreateMul(LHS, AddOne(C2, Context));
+ if (dyn_castFoldableMul(RHS, C2) == LHS)
+ return BinaryOperator::CreateMul(LHS, AddOne(C2));
// X + ~X --> -1 since ~X = -X-1
- if (dyn_castNotVal(LHS, Context) == RHS ||
- dyn_castNotVal(RHS, Context) == LHS)
+ if (dyn_castNotVal(LHS) == RHS ||
+ dyn_castNotVal(RHS) == LHS)
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
// (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
- if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)), *Context))
- if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2, Context), Context))
+ if (match(RHS, m_And(m_Value(), m_ConstantInt(C2))))
+ if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2)))
return R;
// A+B --> A|B iff A and B have no bits set in common.
// W*X + Y*Z --> W * (X+Z) iff W == Y
if (I.getType()->isIntOrIntVector()) {
Value *W, *X, *Y, *Z;
- if (match(LHS, m_Mul(m_Value(W), m_Value(X)), *Context) &&
- match(RHS, m_Mul(m_Value(Y), m_Value(Z)), *Context)) {
+ if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
+ match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
if (W != Y) {
if (W == Z) {
std::swap(Y, Z);
if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
Value *X = 0;
- if (match(LHS, m_Not(m_Value(X)), *Context)) // ~X + C --> (C-1) - X
- return BinaryOperator::CreateSub(SubOne(CRHS, Context), 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)), *Context)) {
+ match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
Constant *Anded = ConstantExpr::getAnd(CRHS, C2);
if (Anded == CRHS) {
// See if all bits from the first bit set in the Add RHS up are included
// Can we fold the add into the argument of the select?
// We check both true and false select arguments for a matching subtract.
- if (match(FV, m_Zero(), *Context) &&
- match(TV, m_Sub(m_Value(N), m_Specific(A)), *Context))
+ if (match(FV, m_Zero()) &&
+ match(TV, m_Sub(m_Value(N), m_Specific(A))))
// Fold the add into the true select value.
return SelectInst::Create(SI->getCondition(), N, A);
- if (match(TV, m_Zero(), *Context) &&
- match(FV, m_Sub(m_Value(N), m_Specific(A)), *Context))
+ if (match(TV, m_Zero()) &&
+ match(FV, m_Sub(m_Value(N), m_Specific(A))))
// Fold the add into the false select value.
return SelectInst::Create(SI->getCondition(), A, N);
}
// -A + B --> B - A
// -A + -B --> -(A + B)
- if (Value *LHSV = dyn_castFNegVal(LHS, Context))
+ if (Value *LHSV = dyn_castFNegVal(LHS))
return BinaryOperator::CreateFSub(RHS, LHSV);
// A + -B --> A - B
if (!isa<Constant>(RHS))
- if (Value *V = dyn_castFNegVal(RHS, Context))
+ if (Value *V = dyn_castFNegVal(RHS))
return BinaryOperator::CreateFSub(LHS, V);
// Check for X+0.0. Simplify it to X if we know X is not -0.0.
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
// If this is a 'B = x-(-A)', change to B = x+A...
- if (Value *V = dyn_castNegVal(Op1, Context))
+ if (Value *V = dyn_castNegVal(Op1))
return BinaryOperator::CreateAdd(Op0, V);
if (isa<UndefValue>(Op0))
if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
// Replace (-1 - A) with (~A)...
if (C->isAllOnesValue())
- return BinaryOperator::CreateNot(*Context, Op1);
+ return BinaryOperator::CreateNot(Op1);
// C - ~X == X + (1+C)
Value *X = 0;
- if (match(Op1, m_Not(m_Value(X)), *Context))
- return BinaryOperator::CreateAdd(X, AddOne(C, Context));
+ if (match(Op1, m_Not(m_Value(X))))
+ return BinaryOperator::CreateAdd(X, AddOne(C));
// -(X >>u 31) -> (X >>s 31)
// -(X >>s 31) -> (X >>u 31)
// 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);
+ return SelectInst::Create(ZI->getOperand(0), SubOne(C), C);
}
if (I.getType() == Type::Int1Ty)
if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
if (Op1I->getOpcode() == Instruction::Add) {
if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
- return BinaryOperator::CreateNeg(*Context, Op1I->getOperand(1),
+ return BinaryOperator::CreateNeg(Op1I->getOperand(1),
I.getName());
else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
- return BinaryOperator::CreateNeg(*Context, Op1I->getOperand(0),
+ return BinaryOperator::CreateNeg(Op1I->getOperand(0),
I.getName());
else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
Value *NewNot =
- InsertNewInstBefore(BinaryOperator::CreateNot(*Context,
- OtherOp, "B.not"), I);
+ InsertNewInstBefore(BinaryOperator::CreateNot(OtherOp, "B.not"), I);
return BinaryOperator::CreateAnd(Op0, NewNot);
}
// X - X*C --> X * (1-C)
ConstantInt *C2 = 0;
- if (dyn_castFoldableMul(Op1I, C2, Context) == Op0) {
+ if (dyn_castFoldableMul(Op1I, C2) == Op0) {
Constant *CP1 =
ConstantExpr::getSub(ConstantInt::get(I.getType(), 1),
C2);
return ReplaceInstUsesWith(I, Op0I->getOperand(0));
} else if (Op0I->getOpcode() == Instruction::Sub) {
if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y
- return BinaryOperator::CreateNeg(*Context, Op0I->getOperand(1),
+ return BinaryOperator::CreateNeg(Op0I->getOperand(1),
I.getName());
}
}
ConstantInt *C1;
- if (Value *X = dyn_castFoldableMul(Op0, C1, Context)) {
+ if (Value *X = dyn_castFoldableMul(Op0, C1)) {
if (X == Op1) // X*C - X --> X * (C-1)
- return BinaryOperator::CreateMul(Op1, SubOne(C1, Context));
+ return BinaryOperator::CreateMul(Op1, SubOne(C1));
ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
- if (X == dyn_castFoldableMul(Op1, C2, Context))
+ if (X == dyn_castFoldableMul(Op1, C2))
return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2));
}
return 0;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
// If this is a 'B = x-(-A)', change to B = x+A...
- if (Value *V = dyn_castFNegVal(Op1, Context))
+ if (Value *V = dyn_castFNegVal(Op1))
return BinaryOperator::CreateFAdd(Op0, V);
if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
if (Op1I->getOpcode() == Instruction::FAdd) {
if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
- return BinaryOperator::CreateFNeg(*Context, Op1I->getOperand(1),
+ return BinaryOperator::CreateFNeg(Op1I->getOperand(1),
I.getName());
else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
- return BinaryOperator::CreateFNeg(*Context, Op1I->getOperand(0),
+ return BinaryOperator::CreateFNeg(Op1I->getOperand(0),
I.getName());
}
}
if (CI->equalsInt(1)) // X * 1 == X
return ReplaceInstUsesWith(I, Op0);
if (CI->isAllOnesValue()) // X * -1 == 0 - X
- return BinaryOperator::CreateNeg(*Context, Op0, I.getName());
+ return BinaryOperator::CreateNeg(Op0, I.getName());
const APInt& Val = cast<ConstantInt>(CI)->getValue();
if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C
if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
if (Op1V->isAllOnesValue()) // X * -1 == 0 - X
- return BinaryOperator::CreateNeg(*Context, Op0, I.getName());
+ return BinaryOperator::CreateNeg(Op0, I.getName());
// As above, vector X*splat(1.0) -> X in all defined cases.
if (Constant *Splat = Op1V->getSplatValue()) {
return NV;
}
- if (Value *Op0v = dyn_castNegVal(Op0, Context)) // -X * -Y = X*Y
- if (Value *Op1v = dyn_castNegVal(I.getOperand(1), Context))
+ if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
+ if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
return BinaryOperator::CreateMul(Op0v, Op1v);
// (X / Y) * Y = X - (X % Y)
Op1 = Op0;
BO = dyn_cast<BinaryOperator>(I.getOperand(1));
}
- Value *Neg = dyn_castNegVal(Op1, Context);
+ Value *Neg = dyn_castNegVal(Op1);
if (BO && BO->hasOneUse() &&
(BO->getOperand(1) == Op1 || BO->getOperand(1) == Neg) &&
(BO->getOpcode() == Instruction::UDiv ||
BO->getOpcode() == Instruction::SDiv)) {
Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
+ // If the division is exact, X % Y is zero.
+ if (SDivOperator *SDiv = dyn_cast<SDivOperator>(BO))
+ if (SDiv->isExact()) {
+ if (Op1BO == Op1)
+ return ReplaceInstUsesWith(I, Op0BO);
+ else
+ return BinaryOperator::CreateNeg(Op0BO);
+ }
+
Instruction *Rem;
if (BO->getOpcode() == Instruction::UDiv)
Rem = BinaryOperator::CreateURem(Op0BO, Op1BO);
return NV;
}
- if (Value *Op0v = dyn_castFNegVal(Op0, Context)) // -X * -Y = X*Y
- if (Value *Op1v = dyn_castFNegVal(I.getOperand(1), Context))
+ if (Value *Op0v = dyn_castFNegVal(Op0)) // -X * -Y = X*Y
+ if (Value *Op1v = dyn_castFNegVal(I.getOperand(1)))
return BinaryOperator::CreateFMul(Op0v, Op1v);
return Changed ? &I : 0;
if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
if (MultiplyOverflows(RHS, LHSRHS,
- I.getOpcode()==Instruction::SDiv, Context))
+ I.getOpcode()==Instruction::SDiv))
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
else
return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
// sdiv X, -1 == -X
if (RHS->isAllOnesValue())
- return BinaryOperator::CreateNeg(*Context, Op0);
+ return BinaryOperator::CreateNeg(Op0);
- // sdiv X, C --> ashr X, log2(C)
+ // sdiv X, C --> ashr X, log2(C)
if (cast<SDivOperator>(&I)->isExact() &&
RHS->getValue().isNonNegative() &&
RHS->getValue().isPowerOf2()) {
RHS->getValue().exactLogBase2());
return BinaryOperator::CreateAShr(Op0, ShAmt, I.getName());
}
+
+ // -X/C --> X/-C provided the negation doesn't overflow.
+ if (SubOperator *Sub = dyn_cast<SubOperator>(Op0))
+ if (isa<Constant>(Sub->getOperand(0)) &&
+ cast<Constant>(Sub->getOperand(0))->isNullValue() &&
+ Sub->hasNoSignedOverflow())
+ return BinaryOperator::CreateSDiv(Sub->getOperand(1),
+ ConstantExpr::getNeg(RHS));
}
// If the sign bits of both operands are zero (i.e. we can prove they are
return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
}
ConstantInt *ShiftedInt;
- if (match(Op1, m_Shl(m_ConstantInt(ShiftedInt), m_Value()), *Context) &&
+ if (match(Op1, m_Shl(m_ConstantInt(ShiftedInt), m_Value())) &&
ShiftedInt->getValue().isPowerOf2()) {
// X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
// Safe because the only negative value (1 << Y) can take on is
// if so, convert to a bitwise and.
if (ConstantInt *C = dyn_cast<ConstantInt>(RHS))
if (C->getValue().isPowerOf2())
- return BinaryOperator::CreateAnd(Op0, SubOne(C, Context));
+ return BinaryOperator::CreateAnd(Op0, SubOne(C));
}
if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) {
if ((STO->getValue().isPowerOf2()) &&
(SFO->getValue().isPowerOf2())) {
Value *TrueAnd = InsertNewInstBefore(
- BinaryOperator::CreateAnd(Op0, SubOne(STO, Context),
+ BinaryOperator::CreateAnd(Op0, SubOne(STO),
SI->getName()+".t"), I);
Value *FalseAnd = InsertNewInstBefore(
- BinaryOperator::CreateAnd(Op0, SubOne(SFO, Context),
+ BinaryOperator::CreateAnd(Op0, SubOne(SFO),
SI->getName()+".f"), I);
return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd);
}
if (Instruction *common = commonIRemTransforms(I))
return common;
- if (Value *RHSNeg = dyn_castNegVal(Op1, Context))
+ if (Value *RHSNeg = dyn_castNegVal(Op1))
if (!isa<Constant>(RHSNeg) ||
(isa<ConstantInt>(RHSNeg) &&
cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
return new ICmpInst(*Context, ICmpInst::ICMP_EQ, V, V);
// V < Min || V >= Hi -> V > Hi-1
- Hi = SubOne(cast<ConstantInt>(Hi), Context);
+ Hi = SubOne(cast<ConstantInt>(Hi));
if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
ICmpInst::Predicate pred = (isSigned ?
ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT);
// This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
- m_ConstantInt(LHSCst)), *Context) ||
+ m_ConstantInt(LHSCst))) ||
!match(RHS, m_ICmp(RHSCC, m_Value(Val2),
- m_ConstantInt(RHSCst)), *Context))
+ m_ConstantInt(RHSCst))))
return 0;
// (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
switch (RHSCC) {
default: llvm_unreachable("Unknown integer condition code!");
case ICmpInst::ICMP_ULT:
- if (LHSCst == SubOne(RHSCst, Context)) // (X != 13 & X u< 14) -> X < 13
+ if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Val, LHSCst);
break; // (X != 13 & X u< 15) -> no change
case ICmpInst::ICMP_SLT:
- if (LHSCst == SubOne(RHSCst, Context)) // (X != 13 & X s< 14) -> X < 13
+ if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Val, LHSCst);
break; // (X != 13 & X s< 15) -> no change
case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
return ReplaceInstUsesWith(I, RHS);
case ICmpInst::ICMP_NE:
- if (LHSCst == SubOne(RHSCst, Context)){// (X != 13 & X != 14) -> X-13 >u 1
+ if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
Constant *AddCST = ConstantExpr::getNeg(LHSCst);
Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST,
Val->getName()+".off");
case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
break;
case ICmpInst::ICMP_NE:
- if (RHSCst == AddOne(LHSCst, Context)) // (X u> 13 & X != 14) -> X u> 14
+ if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
return new ICmpInst(*Context, LHSCC, Val, RHSCst);
break; // (X u> 13 & X != 15) -> no change
case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1
- return InsertRangeTest(Val, AddOne(LHSCst, Context),
+ return InsertRangeTest(Val, AddOne(LHSCst),
RHSCst, false, true, I);
case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
break;
case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
break;
case ICmpInst::ICMP_NE:
- if (RHSCst == AddOne(LHSCst, Context)) // (X s> 13 & X != 14) -> X s> 14
+ if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
return new ICmpInst(*Context, LHSCC, Val, RHSCst);
break; // (X s> 13 & X != 15) -> no change
case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
- return InsertRangeTest(Val, AddOne(LHSCst, Context),
+ return InsertRangeTest(Val, AddOne(LHSCst),
RHSCst, true, true, I);
case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
break;
ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
if (!(A && A->isZero()) && // avoid infinite recursion.
MaskedValueIsZero(Op0LHS, Mask)) {
- Instruction *NewNeg = BinaryOperator::CreateNeg(*Context, Op0RHS);
+ Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS);
InsertNewInstBefore(NewNeg, I);
return BinaryOperator::CreateAnd(NewNeg, AndRHS);
}
return NV;
}
- Value *Op0NotVal = dyn_castNotVal(Op0, Context);
- Value *Op1NotVal = dyn_castNotVal(Op1, Context);
+ Value *Op0NotVal = dyn_castNotVal(Op0);
+ Value *Op1NotVal = dyn_castNotVal(Op1);
if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Instruction *Or = BinaryOperator::CreateOr(Op0NotVal, Op1NotVal,
I.getName()+".demorgan");
InsertNewInstBefore(Or, I);
- return BinaryOperator::CreateNot(*Context, Or);
+ return BinaryOperator::CreateNot(Or);
}
{
Value *A = 0, *B = 0, *C = 0, *D = 0;
- if (match(Op0, m_Or(m_Value(A), m_Value(B)), *Context)) {
+ if (match(Op0, m_Or(m_Value(A), m_Value(B)))) {
if (A == Op1 || B == Op1) // (A | ?) & A --> A
return ReplaceInstUsesWith(I, Op1);
// (A|B) & ~(A&B) -> A^B
- if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))), *Context)) {
+ if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
}
- if (match(Op1, m_Or(m_Value(A), m_Value(B)), *Context)) {
+ if (match(Op1, m_Or(m_Value(A), m_Value(B)))) {
if (A == Op0 || B == Op0) // A & (A | ?) --> A
return ReplaceInstUsesWith(I, Op0);
// ~(A&B) & (A|B) -> A^B
- if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))), *Context)) {
+ if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
}
if (Op0->hasOneUse() &&
- match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) {
+ match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
if (A == Op1) { // (A^B)&A -> A&(A^B)
I.swapOperands(); // Simplify below
std::swap(Op0, Op1);
}
if (Op1->hasOneUse() &&
- match(Op1, m_Xor(m_Value(A), m_Value(B)), *Context)) {
+ match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
if (B == Op0) { // B&(A^B) -> B&(B^A)
cast<BinaryOperator>(Op1)->swapOperands();
std::swap(A, B);
}
if (A == Op0) { // A&(A^B) -> A & ~B
- Instruction *NotB = BinaryOperator::CreateNot(*Context, B, "tmp");
+ Instruction *NotB = BinaryOperator::CreateNot(B, "tmp");
InsertNewInstBefore(NotB, I);
return BinaryOperator::CreateAnd(A, NotB);
}
}
// (A&((~A)|B)) -> A&B
- if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A)), *Context) ||
- match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1))), *Context))
+ if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
+ match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
return BinaryOperator::CreateAnd(A, Op1);
- if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A)), *Context) ||
- match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0))), *Context))
+ if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
+ match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
return BinaryOperator::CreateAnd(A, Op0);
}
if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) {
// (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
- if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context))
+ if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
return R;
if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
LLVMContext *Context) {
// If A is not a select of -1/0, this cannot match.
Value *Cond = 0;
- if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond)), *Context))
+ if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond))))
return 0;
// ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B.
- if (match(D, m_SelectCst<0, -1>(m_Specific(Cond)), *Context))
+ if (match(D, m_SelectCst<0, -1>(m_Specific(Cond))))
return SelectInst::Create(Cond, C, B);
- if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))), *Context))
+ if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
return SelectInst::Create(Cond, C, B);
// ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D.
- if (match(B, m_SelectCst<0, -1>(m_Specific(Cond)), *Context))
+ if (match(B, m_SelectCst<0, -1>(m_Specific(Cond))))
return SelectInst::Create(Cond, C, D);
- if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))), *Context))
+ if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond)))))
return SelectInst::Create(Cond, C, D);
return 0;
}
// This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
if (!match(LHS, m_ICmp(LHSCC, m_Value(Val),
- m_ConstantInt(LHSCst)), *Context) ||
+ m_ConstantInt(LHSCst))) ||
!match(RHS, m_ICmp(RHSCC, m_Value(Val2),
- m_ConstantInt(RHSCst)), *Context))
+ m_ConstantInt(RHSCst))))
return 0;
// From here on, we only handle:
switch (RHSCC) {
default: llvm_unreachable("Unknown integer condition code!");
case ICmpInst::ICMP_EQ:
- if (LHSCst == SubOne(RHSCst, Context)) {
+ if (LHSCst == SubOne(RHSCst)) {
// (X == 13 | X == 14) -> X-13 <u 2
Constant *AddCST = ConstantExpr::getNeg(LHSCst);
Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST,
Val->getName()+".off");
InsertNewInstBefore(Add, I);
- AddCST = ConstantExpr::getSub(AddOne(RHSCst, Context), LHSCst);
+ AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, AddCST);
}
break; // (X == 13 | X == 15) -> no change
// this can cause overflow.
if (RHSCst->isMaxValue(false))
return ReplaceInstUsesWith(I, LHS);
- return InsertRangeTest(Val, LHSCst, AddOne(RHSCst, Context),
+ return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
false, false, I);
case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
break;
// this can cause overflow.
if (RHSCst->isMaxValue(true))
return ReplaceInstUsesWith(I, LHS);
- return InsertRangeTest(Val, LHSCst, AddOne(RHSCst, Context),
+ return InsertRangeTest(Val, LHSCst, AddOne(RHSCst),
true, false, I);
case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
break;
Value *V1 = 0;
ConstantInt *CI2 = 0;
- if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)), *Context)) return 0;
+ if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0;
APInt Xor = CI1->getValue() ^ CI2->getValue();
if (!Xor.isAllOnesValue()) return 0;
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
ConstantInt *C1 = 0; Value *X = 0;
// (X & C1) | C2 --> (X | C2) & (C1|C2)
- if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1)), *Context) &&
+ if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
isOnlyUse(Op0)) {
Instruction *Or = BinaryOperator::CreateOr(X, RHS);
InsertNewInstBefore(Or, I);
}
// (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
- if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1)), *Context) &&
+ if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
isOnlyUse(Op0)) {
Instruction *Or = BinaryOperator::CreateOr(X, RHS);
InsertNewInstBefore(Or, I);
Value *A = 0, *B = 0;
ConstantInt *C1 = 0, *C2 = 0;
- if (match(Op0, m_And(m_Value(A), m_Value(B)), *Context))
+ if (match(Op0, m_And(m_Value(A), m_Value(B))))
if (A == Op1 || B == Op1) // (A & ?) | A --> A
return ReplaceInstUsesWith(I, Op1);
- if (match(Op1, m_And(m_Value(A), m_Value(B)), *Context))
+ if (match(Op1, m_And(m_Value(A), m_Value(B))))
if (A == Op0 || B == Op0) // A | (A & ?) --> A
return ReplaceInstUsesWith(I, Op0);
// (A | B) | C and A | (B | C) -> bswap if possible.
// (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
- if (match(Op0, m_Or(m_Value(), m_Value()), *Context) ||
- match(Op1, m_Or(m_Value(), m_Value()), *Context) ||
- (match(Op0, m_Shift(m_Value(), m_Value()), *Context) &&
- match(Op1, m_Shift(m_Value(), m_Value()), *Context))) {
+ if (match(Op0, m_Or(m_Value(), m_Value())) ||
+ match(Op1, m_Or(m_Value(), m_Value())) ||
+ (match(Op0, m_Shift(m_Value(), m_Value())) &&
+ match(Op1, m_Shift(m_Value(), m_Value())))) {
if (Instruction *BSwap = MatchBSwap(I))
return BSwap;
}
// (X^C)|Y -> (X|Y)^C iff Y&C == 0
if (Op0->hasOneUse() &&
- match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1)), *Context) &&
+ match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
MaskedValueIsZero(Op1, C1->getValue())) {
Instruction *NOr = BinaryOperator::CreateOr(A, Op1);
InsertNewInstBefore(NOr, I);
// Y|(X^C) -> (X|Y)^C iff Y&C == 0
if (Op1->hasOneUse() &&
- match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1)), *Context) &&
+ match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
MaskedValueIsZero(Op0, C1->getValue())) {
Instruction *NOr = BinaryOperator::CreateOr(A, Op0);
InsertNewInstBefore(NOr, I);
// (A & C)|(B & D)
Value *C = 0, *D = 0;
- if (match(Op0, m_And(m_Value(A), m_Value(C)), *Context) &&
- match(Op1, m_And(m_Value(B), m_Value(D)), *Context)) {
+ if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
+ match(Op1, m_And(m_Value(B), m_Value(D)))) {
Value *V1 = 0, *V2 = 0, *V3 = 0;
C1 = dyn_cast<ConstantInt>(C);
C2 = dyn_cast<ConstantInt>(D);
// replace with V+N.
if (C1->getValue() == ~C2->getValue()) {
if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+
- match(A, m_Add(m_Value(V1), m_Value(V2)), *Context)) {
+ match(A, m_Add(m_Value(V1), m_Value(V2)))) {
// Add commutes, try both ways.
if (V1 == B && MaskedValueIsZero(V2, C2->getValue()))
return ReplaceInstUsesWith(I, A);
}
// Or commutes, try both ways.
if ((C1->getValue() & (C1->getValue()+1)) == 0 &&
- match(B, m_Add(m_Value(V1), m_Value(V2)), *Context)) {
+ match(B, m_Add(m_Value(V1), m_Value(V2)))) {
// Add commutes, try both ways.
if (V1 == A && MaskedValueIsZero(V2, C1->getValue()))
return ReplaceInstUsesWith(I, B);
return Match;
// ((A&~B)|(~A&B)) -> A^B
- if ((match(C, m_Not(m_Specific(D)), *Context) &&
- match(B, m_Not(m_Specific(A)), *Context)))
+ if ((match(C, m_Not(m_Specific(D))) &&
+ match(B, m_Not(m_Specific(A)))))
return BinaryOperator::CreateXor(A, D);
// ((~B&A)|(~A&B)) -> A^B
- if ((match(A, m_Not(m_Specific(D)), *Context) &&
- match(B, m_Not(m_Specific(C)), *Context)))
+ if ((match(A, m_Not(m_Specific(D))) &&
+ match(B, m_Not(m_Specific(C)))))
return BinaryOperator::CreateXor(C, D);
// ((A&~B)|(B&~A)) -> A^B
- if ((match(C, m_Not(m_Specific(B)), *Context) &&
- match(D, m_Not(m_Specific(A)), *Context)))
+ if ((match(C, m_Not(m_Specific(B))) &&
+ match(D, m_Not(m_Specific(A)))))
return BinaryOperator::CreateXor(A, B);
// ((~B&A)|(B&~A)) -> A^B
- if ((match(A, m_Not(m_Specific(B)), *Context) &&
- match(D, m_Not(m_Specific(C)), *Context)))
+ if ((match(A, m_Not(m_Specific(B))) &&
+ match(D, m_Not(m_Specific(C)))))
return BinaryOperator::CreateXor(C, B);
}
}
// ((A|B)&1)|(B&-2) -> (A&1) | B
- if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C)), *Context) ||
- match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))), *Context)) {
+ 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))))) {
Instruction *Ret = FoldOrWithConstants(I, Op1, 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)), *Context) ||
- match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))), *Context)) {
+ 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))))) {
Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C);
if (Ret) return Ret;
}
- if (match(Op0, m_Not(m_Value(A)), *Context)) { // ~A | Op1
+ if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1
if (A == Op1) // ~A | A == -1
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
} else {
A = 0;
}
// Note, A is still live here!
- if (match(Op1, m_Not(m_Value(B)), *Context)) { // Op0 | ~B
+ if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B
if (Op0 == B)
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
Value *And = InsertNewInstBefore(BinaryOperator::CreateAnd(A, B,
I.getName()+".demorgan"), I);
- return BinaryOperator::CreateNot(*Context, And);
+ return BinaryOperator::CreateNot(And);
}
}
// (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) {
- if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context))
+ if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
return R;
if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
}
// xor X, X = 0, even if X is nested in a sequence of Xor's.
- if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1), Context)) {
+ if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) {
assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result;
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
}
return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X
// Is this a ~ operation?
- if (Value *NotOp = dyn_castNotVal(&I, Context)) {
+ if (Value *NotOp = dyn_castNotVal(&I)) {
// ~(~X & Y) --> (X | ~Y) - De Morgan's Law
// ~(~X | Y) === (X & ~Y) - De Morgan's Law
if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
if (Op0I->getOpcode() == Instruction::And ||
Op0I->getOpcode() == Instruction::Or) {
- if (dyn_castNotVal(Op0I->getOperand(1), Context)) Op0I->swapOperands();
- if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0), Context)) {
+ if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
+ if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
Instruction *NotY =
- BinaryOperator::CreateNot(*Context, Op0I->getOperand(1),
+ BinaryOperator::CreateNot(Op0I->getOperand(1),
Op0I->getOperand(1)->getName()+".not");
InsertNewInstBefore(NotY, I);
if (Op0I->getOpcode() == Instruction::And)
return NV;
}
- if (Value *X = dyn_castNotVal(Op0, Context)) // ~A ^ A == -1
+ if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1
if (X == Op1)
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
- if (Value *X = dyn_castNotVal(Op1, Context)) // A ^ ~A == -1
+ if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1
if (X == Op0)
return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
if (Op1I) {
Value *A, *B;
- if (match(Op1I, m_Or(m_Value(A), m_Value(B)), *Context)) {
+ if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
if (A == Op0) { // B^(B|A) == (A|B)^B
Op1I->swapOperands();
I.swapOperands();
I.swapOperands(); // Simplified below.
std::swap(Op0, Op1);
}
- } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)), *Context)) {
+ } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) {
return ReplaceInstUsesWith(I, B); // A^(A^B) == B
- } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)), *Context)) {
+ } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) {
return ReplaceInstUsesWith(I, A); // A^(B^A) == B
- } else if (match(Op1I, m_And(m_Value(A), m_Value(B)), *Context) &&
+ } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
Op1I->hasOneUse()){
if (A == Op0) { // A^(A&B) -> A^(B&A)
Op1I->swapOperands();
BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
if (Op0I) {
Value *A, *B;
- if (match(Op0I, m_Or(m_Value(A), m_Value(B)), *Context) &&
+ if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
Op0I->hasOneUse()) {
if (A == Op1) // (B|A)^B == (A|B)^B
std::swap(A, B);
if (B == Op1) { // (A|B)^B == A & ~B
Instruction *NotB =
- InsertNewInstBefore(BinaryOperator::CreateNot(*Context,
- Op1, "tmp"), I);
+ InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I);
return BinaryOperator::CreateAnd(A, NotB);
}
- } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)), *Context)) {
+ } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) {
return ReplaceInstUsesWith(I, B); // (A^B)^A == B
- } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)), *Context)) {
+ } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) {
return ReplaceInstUsesWith(I, A); // (B^A)^A == B
- } else if (match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) &&
+ } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
Op0I->hasOneUse()){
if (A == Op1) // (A&B)^A -> (B&A)^A
std::swap(A, B);
if (B == Op1 && // (B&A)^A == ~B & A
!isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
Instruction *N =
- InsertNewInstBefore(BinaryOperator::CreateNot(*Context, A, "tmp"), I);
+ InsertNewInstBefore(BinaryOperator::CreateNot(A, "tmp"), I);
return BinaryOperator::CreateAnd(N, Op1);
}
}
if (Op0I && Op1I) {
Value *A, *B, *C, *D;
// (A & B)^(A | B) -> A ^ B
- if (match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) &&
- match(Op1I, m_Or(m_Value(C), m_Value(D)), *Context)) {
+ if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
+ match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
// (A | B)^(A & B) -> A ^ B
- if (match(Op0I, m_Or(m_Value(A), m_Value(B)), *Context) &&
- match(Op1I, m_And(m_Value(C), m_Value(D)), *Context)) {
+ if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
+ match(Op1I, m_And(m_Value(C), m_Value(D)))) {
if ((A == C && B == D) || (A == D && B == C))
return BinaryOperator::CreateXor(A, B);
}
// (A & B)^(C & D)
if ((Op0I->hasOneUse() || Op1I->hasOneUse()) &&
- match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) &&
- match(Op1I, m_And(m_Value(C), m_Value(D)), *Context)) {
+ match(Op0I, m_And(m_Value(A), m_Value(B))) &&
+ match(Op1I, m_And(m_Value(C), m_Value(D)))) {
// (X & Y)^(X & Y) -> (Y^Z) & X
Value *X = 0, *Y = 0, *Z = 0;
if (A == C)
// (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
- if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context))
+ if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS)))
return R;
// fold (xor (cast A), (cast B)) -> (cast (xor A, B))
case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B)
Instruction *Xor = BinaryOperator::CreateXor(Op0, Op1, I.getName()+"tmp");
InsertNewInstBefore(Xor, I);
- return BinaryOperator::CreateNot(*Context, Xor);
+ return BinaryOperator::CreateNot(Xor);
}
case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B
return BinaryOperator::CreateXor(Op0, Op1);
std::swap(Op0, Op1); // Change icmp ugt -> icmp ult
// FALL THROUGH
case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B
- Instruction *Not = BinaryOperator::CreateNot(*Context,
- Op0, I.getName()+"tmp");
+ Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp");
InsertNewInstBefore(Not, I);
return BinaryOperator::CreateAnd(Not, Op1);
}
std::swap(Op0, Op1); // Change icmp sgt -> icmp slt
// FALL THROUGH
case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B
- Instruction *Not = BinaryOperator::CreateNot(*Context,
- Op1, I.getName()+"tmp");
+ Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp");
InsertNewInstBefore(Not, I);
return BinaryOperator::CreateAnd(Not, Op0);
}
std::swap(Op0, Op1); // Change icmp uge -> icmp ule
// FALL THROUGH
case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B
- Instruction *Not = BinaryOperator::CreateNot(*Context,
- Op0, I.getName()+"tmp");
+ Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp");
InsertNewInstBefore(Not, I);
return BinaryOperator::CreateOr(Not, Op1);
}
std::swap(Op0, Op1); // Change icmp sge -> icmp sle
// FALL THROUGH
case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B
- Instruction *Not = BinaryOperator::CreateNot(*Context,
- Op1, I.getName()+"tmp");
+ Instruction *Not = BinaryOperator::CreateNot(Op1, I.getName()+"tmp");
InsertNewInstBefore(Not, I);
return BinaryOperator::CreateOr(Not, Op0);
}
// (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
if (I.isEquality() && CI->isNullValue() &&
- match(Op0, m_Sub(m_Value(A), m_Value(B)), *Context)) {
+ match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
// (icmp cond A B) if cond is equality
return new ICmpInst(*Context, I.getPredicate(), A, B);
}
if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Op0,
- AddOne(CI, Context));
+ AddOne(CI));
case ICmpInst::ICMP_SLE:
if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0,
- AddOne(CI, Context));
+ AddOne(CI));
case ICmpInst::ICMP_UGE:
if (CI->isMinValue(false)) // A >=u MIN -> TRUE
return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Op0,
- SubOne(CI, Context));
+ SubOne(CI));
case ICmpInst::ICMP_SGE:
if (CI->isMinValue(true)) // A >=s MIN -> TRUE
return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0,
- SubOne(CI, Context));
+ SubOne(CI));
}
// If this comparison is a normal comparison, it demands all
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C
return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
- SubOne(CI, Context));
+ SubOne(CI));
// (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
if (CI->isMinValue(true))
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C
return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
- AddOne(CI, Context));
+ AddOne(CI));
// (x >u 2147483647) -> (x <s 0) -> true if sign bit set
if (CI->isMaxValue(true))
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C
return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
- SubOne(CI, Context));
+ SubOne(CI));
}
break;
case ICmpInst::ICMP_SGT:
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C
return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0,
- AddOne(CI, Context));
+ AddOne(CI));
}
break;
case ICmpInst::ICMP_SGE:
// ~x < ~y --> y < x
{ Value *A, *B;
- if (match(Op0, m_Not(m_Value(A)), *Context) &&
- match(Op1, m_Not(m_Value(B)), *Context))
+ if (match(Op0, m_Not(m_Value(A))) &&
+ match(Op1, m_Not(m_Value(B))))
return new ICmpInst(*Context, I.getPredicate(), B, A);
}
Value *A, *B, *C, *D;
// -x == -y --> x == y
- if (match(Op0, m_Neg(m_Value(A)), *Context) &&
- match(Op1, m_Neg(m_Value(B)), *Context))
+ if (match(Op0, m_Neg(m_Value(A))) &&
+ match(Op1, m_Neg(m_Value(B))))
return new ICmpInst(*Context, I.getPredicate(), A, B);
- if (match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) {
+ if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
Value *OtherVal = A == Op1 ? B : A;
return new ICmpInst(*Context, I.getPredicate(), OtherVal,
Constant::getNullValue(A->getType()));
}
- if (match(Op1, m_Xor(m_Value(C), m_Value(D)), *Context)) {
+ if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
// A^c1 == C^c2 --> A == C^(c1^c2)
ConstantInt *C1, *C2;
- if (match(B, m_ConstantInt(C1), *Context) &&
- match(D, m_ConstantInt(C2), *Context) && Op1->hasOneUse()) {
+ if (match(B, m_ConstantInt(C1)) &&
+ match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
Constant *NC =
ConstantInt::get(*Context, C1->getValue() ^ C2->getValue());
Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp");
}
}
- if (match(Op1, m_Xor(m_Value(A), m_Value(B)), *Context) &&
+ if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
(A == Op0 || B == Op0)) {
// A == (A^B) -> B == 0
Value *OtherVal = A == Op0 ? B : A;
}
// (A-B) == A -> B == 0
- if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)), *Context))
+ if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B))))
return new ICmpInst(*Context, I.getPredicate(), B,
Constant::getNullValue(B->getType()));
// A == (A-B) -> B == 0
- if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)), *Context))
+ if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B))))
return new ICmpInst(*Context, I.getPredicate(), B,
Constant::getNullValue(B->getType()));
// (X&Z) == (Y&Z) -> (X^Y) & Z == 0
if (Op0->hasOneUse() && Op1->hasOneUse() &&
- match(Op0, m_And(m_Value(A), m_Value(B)), *Context) &&
- match(Op1, m_And(m_Value(C), m_Value(D)), *Context)) {
+ match(Op0, m_And(m_Value(A), m_Value(B))) &&
+ match(Op1, m_And(m_Value(C), m_Value(D)))) {
Value *X = 0, *Y = 0, *Z = 0;
if (A == C) {
} else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
if (CmpRHSV == 0) { // (X / pos) op 0
// Can't overflow. e.g. X/2 op 0 --> [-1, 2)
- LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS,
- Context)));
+ LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS)));
HiBound = DivRHS;
} else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos
LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, Context, true);
} else { // (X / pos) op neg
// e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
- HiBound = AddOne(Prod, Context);
+ HiBound = AddOne(Prod);
LoOverflow = HiOverflow = ProdOV ? -1 : 0;
if (!LoOverflow) {
ConstantInt* DivNeg =
} else if (DivRHS->getValue().isNegative()) { // Divisor is < 0.
if (CmpRHSV == 0) { // (X / neg) op 0
// e.g. X/-5 op 0 --> [-4, 5)
- LoBound = AddOne(DivRHS, Context);
+ LoBound = AddOne(DivRHS);
HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS));
if (HiBound == DivRHS) { // -INTMIN = INTMIN
HiOverflow = 1; // [INTMIN+1, overflow)
}
} else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos
// e.g. X/-5 op 3 --> [-19, -14)
- HiBound = AddOne(Prod, Context);
+ HiBound = AddOne(Prod);
HiOverflow = LoOverflow = ProdOV ? -1 : 0;
if (!LoOverflow)
LoOverflow = AddWithOverflow(LoBound, HiBound,
if (isTrueIfPositive)
return new ICmpInst(*Context, ICmpInst::ICMP_SGT, CompareVal,
- SubOne(RHS, Context));
+ SubOne(RHS));
else
return new ICmpInst(*Context, ICmpInst::ICMP_SLT, CompareVal,
- AddOne(RHS, Context));
+ AddOne(RHS));
}
if (LHSI->hasOneUse()) {
// efficiently invertible, or if the add has just this one use.
Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
- if (Value *NegVal = dyn_castNegVal(BOp1, Context))
+ if (Value *NegVal = dyn_castNegVal(BOp1))
return new ICmpInst(*Context, ICI.getPredicate(), BOp0, NegVal);
- else if (Value *NegVal = dyn_castNegVal(BOp0, Context))
+ else if (Value *NegVal = dyn_castNegVal(BOp0))
return new ICmpInst(*Context, ICI.getPredicate(), NegVal, BOp1);
else if (BO->hasOneUse()) {
- Instruction *Neg = BinaryOperator::CreateNeg(*Context, BOp1);
+ Instruction *Neg = BinaryOperator::CreateNeg(BOp1);
InsertNewInstBefore(Neg, ICI);
Neg->takeName(BO);
return new ICmpInst(*Context, ICI.getPredicate(), BOp0, Neg);
"ICmp should be folded!");
if (Constant *CI = dyn_cast<Constant>(Result))
return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI));
- return BinaryOperator::CreateNot(*Context, Result);
+ return BinaryOperator::CreateNot(Result);
}
Instruction *InstCombiner::visitShl(BinaryOperator &I) {
// Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
- m_Specific(Op1)), *Context)){
+ m_Specific(Op1)))){
Instruction *YS = BinaryOperator::CreateShl(
Op0BO->getOperand(0), Op1,
Op0BO->getName());
if (isLeftShift && Op0BOOp1->hasOneUse() &&
match(Op0BOOp1,
m_And(m_Shr(m_Value(V1), m_Specific(Op1)),
- m_ConstantInt(CC)), *Context) &&
+ m_ConstantInt(CC))) &&
cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) {
Instruction *YS = BinaryOperator::CreateShl(
Op0BO->getOperand(0), Op1,
// Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
- m_Specific(Op1)), *Context)){
+ m_Specific(Op1)))) {
Instruction *YS = BinaryOperator::CreateShl(
Op0BO->getOperand(1), Op1,
Op0BO->getName());
if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
match(Op0BO->getOperand(0),
m_And(m_Shr(m_Value(V1), m_Value(V2)),
- m_ConstantInt(CC)), *Context) && V2 == Op1 &&
+ m_ConstantInt(CC))) && V2 == Op1 &&
cast<BinaryOperator>(Op0BO->getOperand(0))
->getOperand(0)->hasOneUse()) {
Instruction *YS = BinaryOperator::CreateShl(
++UI; // If this instruction uses AI more than once, don't break UI.
++NumDeadInst;
- DOUT << "IC: DCE: " << *User;
+ DOUT << "IC: DCE: " << *User << '\n';
EraseInstFromFunction(*User);
}
}
ConstantInt *ShAmtV = 0;
Value *ShiftOp = 0;
if (Src->hasOneUse() &&
- match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)), *Context)) {
+ match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)))) {
uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth);
// Get a mask for the bits shifting in.
Value *A = 0;
ConstantInt *BA = 0, *CA = 0;
if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)),
- m_ConstantInt(CA)), *Context) &&
+ m_ConstantInt(CA))) &&
BA == CA && isa<TruncInst>(A)) {
Value *I = cast<TruncInst>(A)->getOperand(0);
if (I->getType() == CI.getType()) {
if (CI->isMinValue(Pred == ICmpInst::ICMP_SLT))
return ReplaceInstUsesWith(SI, FalseVal);
// X < C ? X : C-1 --> X > C-1 ? C-1 : X
- Constant *AdjustedRHS = SubOne(CI, Context);
+ Constant *AdjustedRHS = SubOne(CI);
if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
(CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
Pred = ICmpInst::getSwappedPredicate(Pred);
if (CI->isMaxValue(Pred == ICmpInst::ICMP_SGT))
return ReplaceInstUsesWith(SI, FalseVal);
// X > C ? X : C+1 --> X < C+1 ? C+1 : X
- Constant *AdjustedRHS = AddOne(CI, Context);
+ Constant *AdjustedRHS = AddOne(CI);
if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
(CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
Pred = ICmpInst::getSwappedPredicate(Pred);
// (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed
// (x >s -1) ? -1 : 0 -> ashr x, 31 -> all ones if not signed
CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
- if (match(TrueVal, m_ConstantInt<-1>(), *Context) &&
- match(FalseVal, m_ConstantInt<0>(), *Context))
+ if (match(TrueVal, m_ConstantInt<-1>()) &&
+ match(FalseVal, m_ConstantInt<0>()))
Pred = ICI->getPredicate();
- else if (match(TrueVal, m_ConstantInt<0>(), *Context) &&
- match(FalseVal, m_ConstantInt<-1>(), *Context))
+ else if (match(TrueVal, m_ConstantInt<0>()) &&
+ match(FalseVal, m_ConstantInt<-1>()))
Pred = CmpInst::getInversePredicate(ICI->getPredicate());
if (Pred != CmpInst::BAD_ICMP_PREDICATE) {
true/*SExt*/, "tmp", ICI);
if (Pred == ICmpInst::ICMP_SGT)
- In = InsertNewInstBefore(BinaryOperator::CreateNot(*Context, In,
+ In = InsertNewInstBefore(BinaryOperator::CreateNot(In,
In->getName()+".not"), *ICI);
return ReplaceInstUsesWith(SI, In);
} else {
// Change: A = select B, false, C --> A = and !B, C
Value *NotCond =
- InsertNewInstBefore(BinaryOperator::CreateNot(*Context, CondVal,
+ InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
"not."+CondVal->getName()), SI);
return BinaryOperator::CreateAnd(NotCond, FalseVal);
}
} else {
// Change: A = select B, C, true --> A = or !B, C
Value *NotCond =
- InsertNewInstBefore(BinaryOperator::CreateNot(*Context, CondVal,
+ InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
"not."+CondVal->getName()), SI);
return BinaryOperator::CreateOr(NotCond, TrueVal);
}
} else if (TrueValC->isZero() && FalseValC->getValue() == 1) {
// select C, 0, 1 -> zext !C to int
Value *NotCond =
- InsertNewInstBefore(BinaryOperator::CreateNot(*Context, CondVal,
+ InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
"not."+CondVal->getName()), SI);
return CastInst::Create(Instruction::ZExt, NotCond, SI.getType());
}
NegVal = ConstantExpr::getNeg(C);
} else {
NegVal = InsertNewInstBefore(
- BinaryOperator::CreateNeg(*Context, SubOp->getOperand(1),
+ BinaryOperator::CreateNeg(SubOp->getOperand(1),
"tmp"), SI);
}
Value *X = 0;
BasicBlock *TrueDest;
BasicBlock *FalseDest;
- if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest), *Context) &&
+ if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
!isa<Constant>(X)) {
// Swap Destinations and condition...
BI.setCondition(X);
// Cannonicalize fcmp_one -> fcmp_oeq
FCmpInst::Predicate FPred; Value *Y;
if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
- TrueDest, FalseDest), *Context))
+ TrueDest, FalseDest)))
if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) {
FCmpInst *I = cast<FCmpInst>(BI.getCondition());
// Cannonicalize icmp_ne -> icmp_eq
ICmpInst::Predicate IPred;
if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
- TrueDest, FalseDest), *Context))
+ TrueDest, FalseDest)))
if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE ||
IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) {
// DCE instruction if trivially dead.
if (isInstructionTriviallyDead(Inst)) {
++NumDeadInst;
- DOUT << "IC: DCE: " << *Inst;
+ DOUT << "IC: DCE: " << *Inst << '\n';
Inst->eraseFromParent();
continue;
}
// ConstantProp instruction if trivially constant.
if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) {
- DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst;
+ DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst << '\n';
Inst->replaceAllUsesWith(C);
++NumConstProp;
Inst->eraseFromParent();
while (Term != BB->begin()) { // Remove instrs bottom-up
BasicBlock::iterator I = Term; --I;
- DOUT << "IC: DCE: " << *I;
+ DOUT << "IC: DCE: " << *I << '\n';
// A debug intrinsic shouldn't force another iteration if we weren't
// going to do one without it.
if (!isa<DbgInfoIntrinsic>(I)) {
AddUsesToWorkList(*I);
++NumDeadInst;
- DOUT << "IC: DCE: " << *I;
+ DOUT << "IC: DCE: " << *I << '\n';
I->eraseFromParent();
RemoveFromWorkList(I);
// Instruction isn't dead, see if we can constant propagate it.
if (Constant *C = ConstantFoldInstruction(I, F.getContext(), TD)) {
- DOUT << "IC: ConstFold to: " << *C << " from: " << *I;
+ DOUT << "IC: ConstFold to: " << *C << " from: " << *I << '\n';
// Add operands to the worklist.
AddUsesToWorkList(*I);
++NumCombined;
// Should we replace the old instruction with a new one?
if (Result != I) {
- DOUT << "IC: Old = " << *I
- << " New = " << *Result;
+ DOUT << "IC: Old = " << *I << '\n'
+ << " New = " << *Result << '\n';
// Everything uses the new instruction now.
I->replaceAllUsesWith(Result);
InstParent->getInstList().erase(I);
} else {
#ifndef NDEBUG
- DOUT << "IC: Mod = " << OrigI
- << " New = " << *I;
+ DOUT << "IC: Mod = " << OrigI << '\n'
+ << " New = " << *I << '\n';
#endif
// If the instruction was modified, it's possible that it is now dead.