static const Type *getPromotedType(const Type *Ty) {
if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
if (ITy->getBitWidth() < 32)
- return Type::Int32Ty;
+ return Type::getInt32Ty(Ty->getContext());
}
return Ty;
}
unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
DstTy,
- TD ? TD->getIntPtrType() : 0);
+ TD ? TD->getIntPtrType(CI->getContext()) : 0);
// We don't want to form an inttoptr or ptrtoint that converts to an integer
// type that differs from the pointer size.
- if ((Res == Instruction::IntToPtr && SrcTy != TD->getIntPtrType()) ||
- (Res == Instruction::PtrToInt && DstTy != TD->getIntPtrType()))
+ if ((Res == Instruction::IntToPtr &&
+ (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) ||
+ (Res == Instruction::PtrToInt &&
+ (!TD || DstTy != TD->getIntPtrType(CI->getContext()))))
Res = 0;
return Instruction::CastOps(Res);
// 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 ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
// all known
if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
- Constant *AndC = ConstantInt::get(*Context,
- ~RHSKnownOne & DemandedMask);
+ Constant *AndC = Constant::getIntegerValue(VTy,
+ ~RHSKnownOne & DemandedMask);
Instruction *And =
BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp");
return InsertNewInstBefore(And, *I);
// 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.
// If the client is only demanding bits that we know, return the known
// constant.
- if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
- Constant *C = ConstantInt::get(*Context, RHSKnownOne);
- if (isa<PointerType>(V->getType()))
- C = ConstantExpr::getIntToPtr(C, V->getType());
- return C;
- }
+ if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask)
+ return Constant::getIntegerValue(VTy, RHSKnownOne);
return false;
}
std::vector<Constant*> Elts;
for (unsigned i = 0; i < VWidth; ++i) {
if (UndefElts[i])
- Elts.push_back(UndefValue::get(Type::Int32Ty));
+ Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
else
- Elts.push_back(ConstantInt::get(Type::Int32Ty,
+ Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context),
Shuffle->getMaskValue(i)));
}
I->setOperand(2, ConstantVector::get(Elts));
Value *RHS = II->getOperand(2);
// Extract the element as scalars.
LHS = InsertNewInstBefore(ExtractElementInst::Create(LHS,
- ConstantInt::get(Type::Int32Ty, 0U, false), "tmp"), *II);
+ ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), "tmp"), *II);
RHS = InsertNewInstBefore(ExtractElementInst::Create(RHS,
- ConstantInt::get(Type::Int32Ty, 0U, false), "tmp"), *II);
+ ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), "tmp"), *II);
switch (II->getIntrinsicID()) {
default: llvm_unreachable("Case stmts out of sync!");
Instruction *New =
InsertElementInst::Create(
UndefValue::get(II->getType()), TmpV,
- ConstantInt::get(Type::Int32Ty, 0U, false), II->getName());
+ ConstantInt::get(Type::getInt32Ty(*Context), 0U, false), II->getName());
InsertNewInstBefore(New, *II);
AddSoonDeadInstToWorklist(*II, 0);
return New;
/// '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 {
static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
InstCombiner *IC) {
- LLVMContext *Context = IC->getContext();
-
if (CastInst *CI = dyn_cast<CastInst>(&I)) {
return IC->InsertCastBefore(CI->getOpcode(), SO, I.getType(), I);
}
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op");
else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
- New = CmpInst::Create(*Context, CI->getOpcode(), CI->getPredicate(),
+ New = CmpInst::Create(CI->getOpcode(), CI->getPredicate(),
Op0, Op1, SO->getName()+".cmp");
else {
llvm_unreachable("Unknown binary instruction type!");
if (isa<Constant>(TV) || isa<Constant>(FV)) {
// Bool selects with constant operands can be folded to logical ops.
- if (SI->getType() == Type::Int1Ty) return 0;
+ if (SI->getType() == Type::getInt1Ty(*IC->getContext())) return 0;
Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC);
Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC);
PN->getIncomingValue(i), C, "phitmp",
NonConstBB->getTerminator());
else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
- InV = CmpInst::Create(*Context, CI->getOpcode(),
+ InV = CmpInst::Create(CI->getOpcode(),
CI->getPredicate(),
PN->getIncomingValue(i), C, "phitmp",
NonConstBB->getTerminator());
// 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);
+ if (ZI->getSrcTy() == Type::getInt1Ty(*Context))
+ 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();
const Type *MiddleType = 0;
switch (Size) {
default: break;
- case 32: MiddleType = Type::Int32Ty; break;
- case 16: MiddleType = Type::Int16Ty; break;
- case 8: MiddleType = Type::Int8Ty; break;
+ case 32: MiddleType = Type::getInt32Ty(*Context); break;
+ case 16: MiddleType = Type::getInt16Ty(*Context); break;
+ case 8: MiddleType = Type::getInt8Ty(*Context); break;
}
if (MiddleType) {
Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext");
}
}
- if (I.getType() == Type::Int1Ty)
+ if (I.getType() == Type::getInt1Ty(*Context))
return BinaryOperator::CreateXor(LHS, RHS);
// 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);
+ if (ZI->getSrcTy() == Type::getInt1Ty(*Context))
+ return SelectInst::Create(ZI->getOperand(0), SubOne(C), C);
}
- if (I.getType() == Type::Int1Ty)
+ if (I.getType() == Type::getInt1Ty(*Context))
return BinaryOperator::CreateXor(Op0, Op1);
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);
}
}
- if (I.getType() == Type::Int1Ty)
+ if (I.getType() == Type::getInt1Ty(*Context))
return BinaryOperator::CreateAnd(Op0, I.getOperand(1));
// If one of the operands of the multiply is a cast from a boolean value, then
// formed.
CastInst *BoolCast = 0;
if (ZExtInst *CI = dyn_cast<ZExtInst>(Op0))
- if (CI->getOperand(0)->getType() == Type::Int1Ty)
+ if (CI->getOperand(0)->getType() == Type::getInt1Ty(*Context))
BoolCast = CI;
if (!BoolCast)
if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1)))
- if (CI->getOperand(0)->getType() == Type::Int1Ty)
+ if (CI->getOperand(0)->getType() == Type::getInt1Ty(*Context))
BoolCast = CI;
if (BoolCast) {
if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) {
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),
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
// It can't be division by zero, hence it must be division by one.
- if (I.getType() == Type::Int1Ty)
+ if (I.getType() == Type::getInt1Ty(*Context))
return ReplaceInstUsesWith(I, Op0);
if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
// X udiv C, where C >= signbit
if (C->getValue().isNegative()) {
- Value *IC = InsertNewInstBefore(new ICmpInst(*Context,
- ICmpInst::ICMP_ULT, Op0, C),
+ Value *IC = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_ULT, Op0, C),
I);
return SelectInst::Create(IC, Constant::getNullValue(I.getType()),
ConstantInt::get(I.getType(), 1));
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)
+ if (cast<SDivOperator>(&I)->isExact() &&
+ RHS->getValue().isNonNegative() &&
+ RHS->getValue().isPowerOf2()) {
+ Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
+ 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->hasNoSignedWrap())
+ 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())) {
case 0: return ConstantInt::getFalse(*Context);
case 1:
if (sign)
- return new ICmpInst(*Context, ICmpInst::ICMP_SGT, LHS, RHS);
+ return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS);
else
- return new ICmpInst(*Context, ICmpInst::ICMP_UGT, LHS, RHS);
- case 2: return new ICmpInst(*Context, ICmpInst::ICMP_EQ, LHS, RHS);
+ return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS);
+ case 2: return new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS);
case 3:
if (sign)
- return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHS, RHS);
+ return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS);
else
- return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHS, RHS);
+ return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS);
case 4:
if (sign)
- return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHS, RHS);
+ return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS);
else
- return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHS, RHS);
- case 5: return new ICmpInst(*Context, ICmpInst::ICMP_NE, LHS, RHS);
+ return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS);
+ case 5: return new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS);
case 6:
if (sign)
- return new ICmpInst(*Context, ICmpInst::ICMP_SLE, LHS, RHS);
+ return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS);
else
- return new ICmpInst(*Context, ICmpInst::ICMP_ULE, LHS, RHS);
+ return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS);
case 7: return ConstantInt::getTrue(*Context);
}
}
default: llvm_unreachable("Illegal FCmp code!");
case 0:
if (isordered)
- return new FCmpInst(*Context, FCmpInst::FCMP_ORD, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS);
else
- return new FCmpInst(*Context, FCmpInst::FCMP_UNO, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS);
case 1:
if (isordered)
- return new FCmpInst(*Context, FCmpInst::FCMP_OGT, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS);
else
- return new FCmpInst(*Context, FCmpInst::FCMP_UGT, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS);
case 2:
if (isordered)
- return new FCmpInst(*Context, FCmpInst::FCMP_OEQ, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS);
else
- return new FCmpInst(*Context, FCmpInst::FCMP_UEQ, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS);
case 3:
if (isordered)
- return new FCmpInst(*Context, FCmpInst::FCMP_OGE, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS);
else
- return new FCmpInst(*Context, FCmpInst::FCMP_UGE, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS);
case 4:
if (isordered)
- return new FCmpInst(*Context, FCmpInst::FCMP_OLT, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS);
else
- return new FCmpInst(*Context, FCmpInst::FCMP_ULT, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS);
case 5:
if (isordered)
- return new FCmpInst(*Context, FCmpInst::FCMP_ONE, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS);
else
- return new FCmpInst(*Context, FCmpInst::FCMP_UNE, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS);
case 6:
if (isordered)
- return new FCmpInst(*Context, FCmpInst::FCMP_OLE, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS);
else
- return new FCmpInst(*Context, FCmpInst::FCMP_ULE, LHS, RHS);
+ return new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS);
case 7: return ConstantInt::getTrue(*Context);
}
}
if (Inside) {
if (Lo == Hi) // Trivially false.
- return new ICmpInst(*Context, ICmpInst::ICMP_NE, V, V);
+ return new ICmpInst(ICmpInst::ICMP_NE, V, V);
// V >= Min && V < Hi --> V < Hi
if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
ICmpInst::Predicate pred = (isSigned ?
ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT);
- return new ICmpInst(*Context, pred, V, Hi);
+ return new ICmpInst(pred, V, Hi);
}
// Emit V-Lo <u Hi-Lo
Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off");
InsertNewInstBefore(Add, IB);
Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
- return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, UpperBound);
+ return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound);
}
if (Lo == Hi) // Trivially true.
- return new ICmpInst(*Context, ICmpInst::ICMP_EQ, V, V);
+ return new ICmpInst(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);
- return new ICmpInst(*Context, pred, V, Hi);
+ return new ICmpInst(pred, V, Hi);
}
// Emit V-Lo >u Hi-1-Lo
Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off");
InsertNewInstBefore(Add, IB);
Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
- return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add, LowerBound);
+ return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound);
}
// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
// 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)
LHSCst->getValue().isPowerOf2()) {
Instruction *NewOr = BinaryOperator::CreateOr(Val, Val2);
InsertNewInstBefore(NewOr, I);
- return new ICmpInst(*Context, LHSCC, NewOr, LHSCst);
+ return new ICmpInst(LHSCC, NewOr, LHSCst);
}
// From here on, we only handle:
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
- return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Val, LHSCst);
+ if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
+ return new ICmpInst(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
- return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Val, LHSCst);
+ if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
+ return new ICmpInst(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_UGT: // (X != 13 & X u> 15) -> X u> 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");
InsertNewInstBefore(Add, I);
- return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add,
+ return new ICmpInst(ICmpInst::ICMP_UGT, Add,
ConstantInt::get(Add->getType(), 1));
}
break; // (X != 13 & X != 15) -> no change
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
- return new ICmpInst(*Context, LHSCC, Val, RHSCst);
+ if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
+ return new ICmpInst(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
- return new ICmpInst(*Context, LHSCC, Val, RHSCst);
+ if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
+ return new ICmpInst(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;
// false.
if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
- return new FCmpInst(*Context, FCmpInst::FCMP_ORD,
+ return new FCmpInst(FCmpInst::FCMP_ORD,
LHS->getOperand(0), RHS->getOperand(0));
}
// "fcmp ord x,x" is "fcmp ord x, 0".
if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
isa<ConstantAggregateZero>(RHS->getOperand(1)))
- return new FCmpInst(*Context, FCmpInst::FCMP_ORD,
+ return new FCmpInst(FCmpInst::FCMP_ORD,
LHS->getOperand(0), RHS->getOperand(0));
return 0;
}
if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
// Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
if (Op0CC == Op1CC)
- return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
+ return new FCmpInst((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
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);
}
// (1 << x) & 1 --> zext(x == 0)
// (1 >> x) & 1 --> zext(x == 0)
if (AndRHSMask == 1 && Op0LHS == AndRHS) {
- Instruction *NewICmp = new ICmpInst(*Context, ICmpInst::ICMP_EQ,
+ Instruction *NewICmp = new ICmpInst(ICmpInst::ICMP_EQ,
Op0RHS, Constant::getNullValue(I.getType()));
InsertNewInstBefore(NewICmp, I);
return new ZExtInst(NewICmp, I.getType());
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);
- return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, AddCST);
+ AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
+ return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST);
}
break; // (X == 13 | X == 15) -> no change
case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> 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;
// Otherwise, no need to compare the two constants, compare the
// rest.
- return new FCmpInst(*Context, FCmpInst::FCMP_UNO,
+ return new FCmpInst(FCmpInst::FCMP_UNO,
LHS->getOperand(0), RHS->getOperand(0));
}
// "fcmp uno x,x" is "fcmp uno x, 0".
if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
isa<ConstantAggregateZero>(RHS->getOperand(1)))
- return new FCmpInst(*Context, FCmpInst::FCMP_UNO,
+ return new FCmpInst(FCmpInst::FCMP_UNO,
LHS->getOperand(0), RHS->getOperand(0));
return 0;
if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
// Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
if (Op0CC == Op1CC)
- return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC,
+ return new FCmpInst((FCmpInst::Predicate)Op0CC,
Op0LHS, Op0RHS);
if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context));
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)
if (RHS == ConstantInt::getTrue(*Context) && Op0->hasOneUse()) {
// xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0))
- return new ICmpInst(*Context, ICI->getInversePredicate(),
+ return new ICmpInst(ICI->getInversePredicate(),
ICI->getOperand(0), ICI->getOperand(1));
if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0))
- return new FCmpInst(*Context, FCI->getInversePredicate(),
+ return new FCmpInst(FCI->getInversePredicate(),
FCI->getOperand(0), FCI->getOperand(1));
}
ConstantInt::getTrue(*Context),
Op0C->getDestTy())) {
Instruction *NewCI = InsertNewInstBefore(CmpInst::Create(
- *Context,
CI->getOpcode(), CI->getInversePredicate(),
CI->getOperand(0), CI->getOperand(1)), I);
NewCI->takeName(CI);
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))
if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
- Constant *Idx = ConstantInt::get(Type::Int32Ty, i);
+ Constant *Idx = ConstantInt::get(Type::getInt32Ty(*Context), i);
if (HasAddOverflow(ExtractElement(Result, Idx, Context),
ExtractElement(In1, Idx, Context),
ExtractElement(In2, Idx, Context),
if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
- Constant *Idx = ConstantInt::get(Type::Int32Ty, i);
+ Constant *Idx = ConstantInt::get(Type::getInt32Ty(*Context), i);
if (HasSubOverflow(ExtractElement(Result, Idx, Context),
ExtractElement(In1, Idx, Context),
ExtractElement(In2, Idx, Context),
static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) {
TargetData &TD = *IC.getTargetData();
gep_type_iterator GTI = gep_type_begin(GEP);
- const Type *IntPtrTy = TD.getIntPtrType();
+ const Type *IntPtrTy = TD.getIntPtrType(I.getContext());
LLVMContext *Context = IC.getContext();
Value *Result = Constant::getNullValue(IntPtrTy);
// we don't need to bother extending: the extension won't affect where the
// computation crosses zero.
if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth)
- VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(),
+ VariableIdx = new TruncInst(VariableIdx,
+ TD.getIntPtrType(VariableIdx->getContext()),
VariableIdx->getName(), &I);
return VariableIdx;
}
return 0;
// Okay, we can do this evaluation. Start by converting the index to intptr.
- const Type *IntPtrTy = TD.getIntPtrType();
+ const Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext());
if (VariableIdx->getType() != IntPtrTy)
VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy,
true /*SExt*/,
// If not, synthesize the offset the hard way.
if (Offset == 0)
Offset = EmitGEPOffset(GEPLHS, I, *this);
- return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), Offset,
+ return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
Constant::getNullValue(Offset->getType()));
} else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
// If the base pointers are different, but the indices are the same, just
// If all indices are the same, just compare the base pointers.
if (IndicesTheSame)
- return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond),
+ return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
GEPLHS->getOperand(0), GEPRHS->getOperand(0));
// Otherwise, the base pointers are different and the indices are
if (NumDifferences == 0) // SAME GEP?
return ReplaceInstUsesWith(I, // No comparison is needed here.
- ConstantInt::get(Type::Int1Ty,
+ ConstantInt::get(Type::getInt1Ty(*Context),
ICmpInst::isTrueWhenEqual(Cond)));
else if (NumDifferences == 1) {
Value *LHSV = GEPLHS->getOperand(DiffOperand);
Value *RHSV = GEPRHS->getOperand(DiffOperand);
// Make sure we do a signed comparison here.
- return new ICmpInst(*Context,
- ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
+ return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
}
}
// ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
Value *L = EmitGEPOffset(GEPLHS, I, *this);
Value *R = EmitGEPOffset(GEPRHS, I, *this);
- return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), L, R);
+ return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
}
}
return 0;
// Lower this FP comparison into an appropriate integer version of the
// comparison.
- return new ICmpInst(*Context, Pred, LHSI->getOperand(0), RHSInt);
+ return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
}
Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
}
if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef
- return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
+ return ReplaceInstUsesWith(I, UndefValue::get(Type::getInt1Ty(*Context)));
// Handle fcmp with constant RHS
if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
// Fold the known value into the constant operand.
Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
// Insert a new FCmp of the other select operand.
- Op2 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(),
+ Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(),
LHSI->getOperand(2), RHSC,
I.getName()), I);
} else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
// Fold the known value into the constant operand.
Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC);
// Insert a new FCmp of the other select operand.
- Op1 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(),
+ Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(),
LHSI->getOperand(1), RHSC,
I.getName()), I);
}
// icmp X, X
if (Op0 == Op1)
- return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, ConstantInt::get(Type::getInt1Ty(*Context),
I.isTrueWhenEqual()));
if (isa<UndefValue>(Op1)) // X icmp undef -> undef
- return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
+ return ReplaceInstUsesWith(I, UndefValue::get(Type::getInt1Ty(*Context)));
// icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
// addresses never equal each other! We already know that Op0 != Op1.
isa<ConstantPointerNull>(Op0)) &&
(isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) ||
isa<ConstantPointerNull>(Op1)))
- return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, ConstantInt::get(Type::getInt1Ty(*Context),
!I.isTrueWhenEqual()));
// icmp's with boolean values can always be turned into bitwise operations
- if (Ty == Type::Int1Ty) {
+ if (Ty == Type::getInt1Ty(*Context)) {
switch (I.getPredicate()) {
default: llvm_unreachable("Invalid icmp instruction!");
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);
+ return new ICmpInst(I.getPredicate(), A, B);
}
// If we have an icmp le or icmp ge instruction, turn it into the
case ICmpInst::ICMP_ULE:
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));
+ return new ICmpInst(ICmpInst::ICMP_ULT, Op0,
+ 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));
+ return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
+ 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));
+ return new ICmpInst(ICmpInst::ICMP_UGT, Op0,
+ 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));
+ return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
+ SubOne(CI));
}
// If this comparison is a normal comparison, it demands all
// figured out that the LHS is a constant. Just constant fold this now so
// that code below can assume that Min != Max.
if (!isa<Constant>(Op0) && Op0Min == Op0Max)
- return new ICmpInst(*Context, I.getPredicate(),
+ return new ICmpInst(I.getPredicate(),
ConstantInt::get(*Context, Op0Min), Op1);
if (!isa<Constant>(Op1) && Op1Min == Op1Max)
- return new ICmpInst(*Context, I.getPredicate(), Op0,
+ return new ICmpInst(I.getPredicate(), Op0,
ConstantInt::get(*Context, Op1Min));
// Based on the range information we know about the LHS, see if we can
if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
- return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
+ return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
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));
+ return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
+ SubOne(CI));
// (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
if (CI->isMinValue(true))
- return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0,
+ return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
Constant::getAllOnesValue(Op0->getType()));
}
break;
return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
- return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
+ return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
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));
+ return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
+ AddOne(CI));
// (x >u 2147483647) -> (x <s 0) -> true if sign bit set
if (CI->isMaxValue(true))
- return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0,
+ return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
Constant::getNullValue(Op0->getType()));
}
break;
if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
- return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
+ return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
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));
+ return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
+ SubOne(CI));
}
break;
case ICmpInst::ICMP_SGT:
return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context));
if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
- return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1);
+ return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
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));
+ return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
+ AddOne(CI));
}
break;
case ICmpInst::ICMP_SGE:
if (I.isSignedPredicate() &&
((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
(Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
- return new ICmpInst(*Context, I.getUnsignedPredicate(), Op0, Op1);
+ return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
}
// Test if the ICmpInst instruction is used exclusively by a select as
break;
}
if (isAllZeros)
- return new ICmpInst(*Context, I.getPredicate(), LHSI->getOperand(0),
+ return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
Constant::getNullValue(LHSI->getOperand(0)->getType()));
}
break;
// Fold the known value into the constant operand.
Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
// Insert a new ICmp of the other select operand.
- Op2 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(),
+ Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(),
LHSI->getOperand(2), RHSC,
I.getName()), I);
} else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
// Fold the known value into the constant operand.
Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
// Insert a new ICmp of the other select operand.
- Op1 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(),
+ Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(),
LHSI->getOperand(1), RHSC,
I.getName()), I);
}
// can assume it is successful and remove the malloc.
if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) {
AddToWorkList(LHSI);
- return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
+ return ReplaceInstUsesWith(I, ConstantInt::get(Type::getInt1Ty(*Context),
!I.isTrueWhenEqual()));
}
break;
Op1 = InsertBitCastBefore(Op1, Op0->getType(), I);
}
}
- return new ICmpInst(*Context, I.getPredicate(), Op0, Op1);
+ return new ICmpInst(I.getPredicate(), Op0, Op1);
}
}
case Instruction::Sub:
case Instruction::Xor:
if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
- return new ICmpInst(*Context, I.getPredicate(), Op0I->getOperand(0),
+ return new ICmpInst(I.getPredicate(), Op0I->getOperand(0),
Op1I->getOperand(0));
// icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
ICmpInst::Predicate Pred = I.isSignedPredicate()
? I.getUnsignedPredicate()
: I.getSignedPredicate();
- return new ICmpInst(*Context, Pred, Op0I->getOperand(0),
+ return new ICmpInst(Pred, Op0I->getOperand(0),
Op1I->getOperand(0));
}
? I.getUnsignedPredicate()
: I.getSignedPredicate();
Pred = I.getSwappedPredicate(Pred);
- return new ICmpInst(*Context, Pred, Op0I->getOperand(0),
+ return new ICmpInst(Pred, Op0I->getOperand(0),
Op1I->getOperand(0));
}
}
Mask);
InsertNewInstBefore(And1, I);
InsertNewInstBefore(And2, I);
- return new ICmpInst(*Context, I.getPredicate(), And1, And2);
+ return new ICmpInst(I.getPredicate(), And1, And2);
}
}
break;
// ~x < ~y --> y < x
{ Value *A, *B;
- if (match(Op0, m_Not(m_Value(A)), *Context) &&
- match(Op1, m_Not(m_Value(B)), *Context))
- return new ICmpInst(*Context, I.getPredicate(), B, A);
+ if (match(Op0, m_Not(m_Value(A))) &&
+ match(Op1, m_Not(m_Value(B))))
+ return new ICmpInst(I.getPredicate(), B, A);
}
if (I.isEquality()) {
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))
- return new ICmpInst(*Context, I.getPredicate(), A, B);
+ if (match(Op0, m_Neg(m_Value(A))) &&
+ match(Op1, m_Neg(m_Value(B))))
+ return new ICmpInst(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,
+ return new ICmpInst(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");
- return new ICmpInst(*Context, I.getPredicate(), A,
+ return new ICmpInst(I.getPredicate(), A,
InsertNewInstBefore(Xor, I));
}
// A^B == A^D -> B == D
- if (A == C) return new ICmpInst(*Context, I.getPredicate(), B, D);
- if (A == D) return new ICmpInst(*Context, I.getPredicate(), B, C);
- if (B == C) return new ICmpInst(*Context, I.getPredicate(), A, D);
- if (B == D) return new ICmpInst(*Context, I.getPredicate(), A, C);
+ if (A == C) return new ICmpInst(I.getPredicate(), B, D);
+ if (A == D) return new ICmpInst(I.getPredicate(), B, C);
+ if (B == C) return new ICmpInst(I.getPredicate(), A, D);
+ if (B == D) return new ICmpInst(I.getPredicate(), A, C);
}
}
- 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;
- return new ICmpInst(*Context, I.getPredicate(), OtherVal,
+ return new ICmpInst(I.getPredicate(), OtherVal,
Constant::getNullValue(A->getType()));
}
// (A-B) == A -> B == 0
- if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)), *Context))
- return new ICmpInst(*Context, I.getPredicate(), B,
+ if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B))))
+ return new ICmpInst(I.getPredicate(), B,
Constant::getNullValue(B->getType()));
// A == (A-B) -> B == 0
- if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)), *Context))
- return new ICmpInst(*Context, I.getPredicate(), B,
+ if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B))))
+ return new ICmpInst(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 (LoOverflow && HiOverflow)
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
else if (HiOverflow)
- return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE :
+ return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
ICmpInst::ICMP_UGE, X, LoBound);
else if (LoOverflow)
- return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT :
+ return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
ICmpInst::ICMP_ULT, X, HiBound);
else
return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI);
if (LoOverflow && HiOverflow)
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
else if (HiOverflow)
- return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT :
+ return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
ICmpInst::ICMP_ULT, X, LoBound);
else if (LoOverflow)
- return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE :
+ return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
ICmpInst::ICMP_UGE, X, HiBound);
else
return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI);
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
if (LoOverflow == -1) // Low bound is less than input range.
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
- return new ICmpInst(*Context, Pred, X, LoBound);
+ return new ICmpInst(Pred, X, LoBound);
case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_SGT:
if (HiOverflow == +1) // High bound greater than input range.
else if (HiOverflow == -1) // High bound less than input range.
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
if (Pred == ICmpInst::ICMP_UGT)
- return new ICmpInst(*Context, ICmpInst::ICMP_UGE, X, HiBound);
+ return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
else
- return new ICmpInst(*Context, ICmpInst::ICMP_SGE, X, HiBound);
+ return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
}
}
APInt NewRHS(RHS->getValue());
NewRHS.zext(SrcBits);
NewRHS |= KnownOne;
- return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0),
+ return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
ConstantInt::get(*Context, NewRHS));
}
}
isTrueIfPositive ^= true;
if (isTrueIfPositive)
- return new ICmpInst(*Context, ICmpInst::ICMP_SGT, CompareVal,
- SubOne(RHS, Context));
+ return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal,
+ SubOne(RHS));
else
- return new ICmpInst(*Context, ICmpInst::ICMP_SLT, CompareVal,
- AddOne(RHS, Context));
+ return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal,
+ AddOne(RHS));
}
if (LHSI->hasOneUse()) {
ICmpInst::Predicate Pred = ICI.isSignedPredicate()
? ICI.getUnsignedPredicate()
: ICI.getSignedPredicate();
- return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
+ return new ICmpInst(Pred, LHSI->getOperand(0),
ConstantInt::get(*Context, RHSV ^ SignBit));
}
? ICI.getUnsignedPredicate()
: ICI.getSignedPredicate();
Pred = ICI.getSwappedPredicate(Pred);
- return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
+ return new ICmpInst(Pred, LHSI->getOperand(0),
ConstantInt::get(*Context, RHSV ^ NotSignBit));
}
}
BinaryOperator::CreateAnd(Cast->getOperand(0),
ConstantInt::get(*Context, NewCST), LHSI->getName());
InsertNewInstBefore(NewAnd, ICI);
- return new ICmpInst(*Context, ICI.getPredicate(), NewAnd,
+ return new ICmpInst(ICI.getPredicate(), NewAnd,
ConstantInt::get(*Context, NewCI));
}
}
ShAmt);
if (Comp != RHS) {// Comparing against a bit that we know is zero.
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
- Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
+ Constant *Cst = ConstantInt::get(Type::getInt1Ty(*Context), IsICMP_NE);
return ReplaceInstUsesWith(ICI, Cst);
}
BinaryOperator::CreateAnd(LHSI->getOperand(0),
Mask, LHSI->getName()+".mask");
Value *And = InsertNewInstBefore(AndI, ICI);
- return new ICmpInst(*Context, ICI.getPredicate(), And,
+ return new ICmpInst(ICI.getPredicate(), And,
ConstantInt::get(*Context, RHSV.lshr(ShAmtVal)));
}
}
Mask, LHSI->getName()+".mask");
Value *And = InsertNewInstBefore(AndI, ICI);
- return new ICmpInst(*Context,
- TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
+ return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
And, Constant::getNullValue(And->getType()));
}
break;
if (Comp != RHSV) { // Comparing against a bit that we know is zero.
bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
- Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
+ Constant *Cst = ConstantInt::get(Type::getInt1Ty(*Context), IsICMP_NE);
return ReplaceInstUsesWith(ICI, Cst);
}
if (LHSI->hasOneUse() &&
MaskedValueIsZero(LHSI->getOperand(0),
APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) {
- return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0),
+ return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
ConstantExpr::getShl(RHS, ShAmt));
}
BinaryOperator::CreateAnd(LHSI->getOperand(0),
Mask, LHSI->getName()+".mask");
Value *And = InsertNewInstBefore(AndI, ICI);
- return new ICmpInst(*Context, ICI.getPredicate(), And,
+ return new ICmpInst(ICI.getPredicate(), And,
ConstantExpr::getShl(RHS, ShAmt));
}
break;
if (ICI.isSignedPredicate()) {
if (CR.getLower().isSignBit()) {
- return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHSI->getOperand(0),
+ return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
ConstantInt::get(*Context, CR.getUpper()));
} else if (CR.getUpper().isSignBit()) {
- return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHSI->getOperand(0),
+ return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
ConstantInt::get(*Context, CR.getLower()));
}
} else {
if (CR.getLower().isMinValue()) {
- return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHSI->getOperand(0),
+ return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
ConstantInt::get(*Context, CR.getUpper()));
} else if (CR.getUpper().isMinValue()) {
- return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHSI->getOperand(0),
+ return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
ConstantInt::get(*Context, CR.getLower()));
}
}
BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1),
BO->getName());
InsertNewInstBefore(NewRem, ICI);
- return new ICmpInst(*Context, ICI.getPredicate(), NewRem,
+ return new ICmpInst(ICI.getPredicate(), NewRem,
Constant::getNullValue(BO->getType()));
}
}
// Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
if (BO->hasOneUse())
- return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0),
+ return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
ConstantExpr::getSub(RHS, BOp1C));
} else if (RHSV == 0) {
// Replace ((add A, B) != 0) with (A != -B) if A or B is
// 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))
- return new ICmpInst(*Context, ICI.getPredicate(), BOp0, NegVal);
- else if (Value *NegVal = dyn_castNegVal(BOp0, Context))
- return new ICmpInst(*Context, ICI.getPredicate(), NegVal, BOp1);
+ if (Value *NegVal = dyn_castNegVal(BOp1))
+ return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
+ else if (Value *NegVal = dyn_castNegVal(BOp0))
+ return new ICmpInst(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);
+ return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
}
}
break;
// For the xor case, we can xor two constants together, eliminating
// the explicit xor.
if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
- return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0),
+ return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
ConstantExpr::getXor(RHS, BOC));
// FALLTHROUGH
case Instruction::Sub:
// Replace (([sub|xor] A, B) != 0) with (A != B)
if (RHSV == 0)
- return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0),
+ return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
BO->getOperand(1));
break;
Constant *NotCI = ConstantExpr::getNot(RHS);
if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
return ReplaceInstUsesWith(ICI,
- ConstantInt::get(Type::Int1Ty,
+ ConstantInt::get(Type::getInt1Ty(*Context),
isICMP_NE));
}
break;
// comparison can never succeed!
if ((RHSV & ~BOC->getValue()) != 0)
return ReplaceInstUsesWith(ICI,
- ConstantInt::get(Type::Int1Ty,
+ ConstantInt::get(Type::getInt1Ty(*Context),
isICMP_NE));
// If we have ((X & C) == C), turn it into ((X & C) != 0).
if (RHS == BOC && RHSV.isPowerOf2())
- return new ICmpInst(*Context, isICMP_NE ? ICmpInst::ICMP_EQ :
+ return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
ICmpInst::ICMP_NE, LHSI,
Constant::getNullValue(RHS->getType()));
Constant *Zero = Constant::getNullValue(X->getType());
ICmpInst::Predicate pred = isICMP_NE ?
ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
- return new ICmpInst(*Context, pred, X, Zero);
+ return new ICmpInst(pred, X, Zero);
}
// ((X & ~7) == 0) --> X < 8
Constant *NegX = ConstantExpr::getNeg(BOC);
ICmpInst::Predicate pred = isICMP_NE ?
ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
- return new ICmpInst(*Context, pred, X, NegX);
+ return new ICmpInst(pred, X, NegX);
}
}
default: break;
}
if (RHSOp)
- return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSOp);
+ return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
}
// The code below only handles extension cast instructions, so far.
// Deal with equality cases early.
if (ICI.isEquality())
- return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSCIOp);
+ return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
// A signed comparison of sign extended values simplifies into a
// signed comparison.
if (isSignedCmp && isSignedExt)
- return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSCIOp);
+ return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
// The other three cases all fold into an unsigned comparison.
- return new ICmpInst(*Context, ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
+ return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
}
// If we aren't dealing with a constant on the RHS, exit early
// However, we allow this when the compare is EQ/NE, because they are
// signless.
if (isSignedExt == isSignedCmp || ICI.isEquality())
- return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, Res1);
+ return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
return 0;
}
// We're performing an unsigned comp with a sign extended value.
// This is true if the input is >= 0. [aka >s -1]
Constant *NegOne = Constant::getAllOnesValue(SrcTy);
- Result = InsertNewInstBefore(new ICmpInst(*Context, ICmpInst::ICMP_SGT,
+ Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT,
LHSCIOp, NegOne, ICI.getName()), ICI);
} else {
// Unsigned extend & unsigned compare -> always true.
"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(
case 32 :
case 64 :
case 128:
- SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1);
+ SExtType = IntegerType::get(*Context, Ty->getBitWidth() - ShiftAmt1);
break;
default: break;
}
///
static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
int &Offset, LLVMContext *Context) {
- assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!");
+ assert(Val->getType() == Type::getInt32Ty(*Context) && "Unexpected allocation size type!");
if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Offset = CI->getZExtValue();
Scale = 0;
- return ConstantInt::get(Type::Int32Ty, 0);
+ return ConstantInt::get(Type::getInt32Ty(*Context), 0);
} else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
if (I->getOpcode() == Instruction::Shl) {
++UI; // If this instruction uses AI more than once, don't break UI.
++NumDeadInst;
- DOUT << "IC: DCE: " << *User;
+ DEBUG(errs() << "IC: DCE: " << *User << '\n');
EraseInstFromFunction(*User);
}
}
Amt = NumElements;
} else {
// If the allocation size is constant, form a constant mul expression
- Amt = ConstantInt::get(Type::Int32Ty, Scale);
+ Amt = ConstantInt::get(Type::getInt32Ty(*Context), Scale);
if (isa<ConstantInt>(NumElements))
Amt = ConstantExpr::getMul(cast<ConstantInt>(NumElements),
cast<ConstantInt>(Amt));
}
if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
- Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true);
+ Value *Off = ConstantInt::get(Type::getInt32Ty(*Context), Offset, true);
Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp");
Amt = InsertNewInstBefore(Tmp, AI);
}
// Start with the index over the outer type. Note that the type size
// might be zero (even if the offset isn't zero) if the indexed type
// is something like [0 x {int, int}]
- const Type *IntPtrTy = TD->getIntPtrType();
+ const Type *IntPtrTy = TD->getIntPtrType(*Context);
int64_t FirstIdx = 0;
if (int64_t TySize = TD->getTypeAllocSize(Ty)) {
FirstIdx = Offset/TySize;
"Offset must stay within the indexed type");
unsigned Elt = SL->getElementContainingOffset(Offset);
- NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt));
+ NewIndices.push_back(ConstantInt::get(Type::getInt32Ty(*Context), Elt));
Offset -= SL->getElementOffset(Elt);
Ty = STy->getElementType(Elt);
}
if (DoXForm) {
- DOUT << "ICE: EvaluateInDifferentType converting expression type to avoid"
- << " cast: " << CI;
+ DEBUG(errs() << "ICE: EvaluateInDifferentType converting expression type"
+ " to avoid cast: " << CI);
Value *Res = EvaluateInDifferentType(SrcI, DestTy,
CI.getOpcode() == Instruction::SExt);
if (JustReplace)
Constant *One = ConstantInt::get(Src->getType(), 1);
Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI);
Value *Zero = Constant::getNullValue(Src->getType());
- return new ICmpInst(*Context, ICmpInst::ICMP_NE, Src, Zero);
+ return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
}
// Optimize trunc(lshr(), c) to pull the shift through the truncate.
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.
if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
// (X&4) == 2 --> false
// (X&4) != 2 --> true
- Constant *Res = ConstantInt::get(Type::Int1Ty, isNE);
+ Constant *Res = ConstantInt::get(Type::getInt1Ty(*Context), isNE);
Res = ConstantExpr::getZExt(Res, CI.getType());
return ReplaceInstUsesWith(CI, Res);
}
Value *Src = CI.getOperand(0);
// Canonicalize sign-extend from i1 to a select.
- if (Src->getType() == Type::Int1Ty)
+ if (Src->getType() == Type::getInt1Ty(*Context))
return SelectInst::Create(Src,
Constant::getAllOnesValue(CI.getType()),
Constant::getNullValue(CI.getType()));
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()) {
// that can accurately represent it. This allows us to turn
// (float)((double)X+2.0) into x+2.0f.
if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
- if (CFP->getType() == Type::PPC_FP128Ty)
+ if (CFP->getType() == Type::getPPC_FP128Ty(*Context))
return V; // No constant folding of this.
// See if the value can be truncated to float and then reextended.
if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle, Context))
return V;
- if (CFP->getType() == Type::DoubleTy)
+ if (CFP->getType() == Type::getDoubleTy(*Context))
return V; // Won't shrink.
if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble, Context))
return V;
if (TD &&
CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) {
Value *P = InsertNewInstBefore(new PtrToIntInst(CI.getOperand(0),
- TD->getIntPtrType(),
+ TD->getIntPtrType(CI.getContext()),
"tmp"), CI);
return new TruncInst(P, CI.getType());
}
CI.getOperand(0)->getType()->getScalarSizeInBits() >
TD->getPointerSizeInBits()) {
Value *P = InsertNewInstBefore(new TruncInst(CI.getOperand(0),
- TD->getIntPtrType(),
+ TD->getIntPtrType(CI.getContext()),
"tmp"), CI);
return new IntToPtrInst(P, CI.getType());
}
// If the source and destination are pointers, and this cast is equivalent
// to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
// This can enhance SROA and other transforms that want type-safe pointers.
- Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty);
+ Constant *ZeroUInt = Constant::getNullValue(Type::getInt32Ty(*Context));
unsigned NumZeros = 0;
while (SrcElTy != DstElTy &&
isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) &&
Value *Elem = InsertCastBefore(Instruction::BitCast, Src,
DestVTy->getElementType(), CI);
return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
- Constant::getNullValue(Type::Int32Ty));
+ Constant::getNullValue(Type::getInt32Ty(*Context)));
}
// FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
}
if (SrcVTy->getNumElements() == 1) {
if (!isa<VectorType>(DestTy)) {
Instruction *Elem =
- ExtractElementInst::Create(Src, Constant::getNullValue(Type::Int32Ty));
+ ExtractElementInst::Create(Src, Constant::getNullValue(Type::getInt32Ty(*Context)));
InsertNewInstBefore(Elem, CI);
return CastInst::Create(Instruction::BitCast, Elem, DestTy);
}
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);
return ReplaceInstUsesWith(SI, FalseVal);
}
- if (SI.getType() == Type::Int1Ty) {
+ if (SI.getType() == Type::getInt1Ty(*Context)) {
if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
if (C->getZExtValue()) {
// Change: A = select B, true, C --> A = or B, C
} 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);
}
// Use an integer load+store unless we can find something better.
Type *NewPtrTy =
- PointerType::getUnqual(IntegerType::get(Size<<3));
+ PointerType::getUnqual(IntegerType::get(*Context, Size<<3));
// Memcpy forces the use of i8* for the source and destination. That means
// that if you're using memcpy to move one double around, you'll get a cast
// Extract the length and alignment and fill if they are constant.
ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
- if (!LenC || !FillC || FillC->getType() != Type::Int8Ty)
+ if (!LenC || !FillC || FillC->getType() != Type::getInt8Ty(*Context))
return 0;
uint64_t Len = LenC->getZExtValue();
Alignment = MI->getAlignment();
// memset(s,c,n) -> store s, c (for n=1,2,4,8)
if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
- const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8.
+ const Type *ITy = IntegerType::get(*Context, Len*8); // n=1 -> i8.
Value *Dest = MI->getDest();
Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI);
if (ExtractedElts[Idx] == 0) {
Instruction *Elt =
ExtractElementInst::Create(Idx < 16 ? Op0 : Op1,
- ConstantInt::get(Type::Int32Ty, Idx&15, false), "tmp");
+ ConstantInt::get(Type::getInt32Ty(*Context), Idx&15, false), "tmp");
InsertNewInstBefore(Elt, CI);
ExtractedElts[Idx] = Elt;
}
// Insert this value into the result vector.
Result = InsertElementInst::Create(Result, ExtractedElts[Idx],
- ConstantInt::get(Type::Int32Ty, i, false),
+ ConstantInt::get(Type::getInt32Ty(*Context), i, false),
"tmp");
InsertNewInstBefore(cast<Instruction>(Result), CI);
}
// If the call and callee calling conventions don't match, this call must
// be unreachable, as the call is undefined.
new StoreInst(ConstantInt::getTrue(*Context),
- UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
+ UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))),
OldCall);
if (!OldCall->use_empty())
OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
// undef so that we know that this code is not reachable, despite the fact
// that we can't modify the CFG here.
new StoreInst(ConstantInt::getTrue(*Context),
- UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
+ UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))),
CS.getInstruction());
if (!CS.getInstruction()->use_empty())
// Conversion is ok if changing from one pointer type to another or from
// a pointer to an integer of the same size.
!((isa<PointerType>(OldRetTy) || !TD ||
- OldRetTy == TD->getIntPtrType()) &&
+ OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
(isa<PointerType>(NewRetTy) || !TD ||
- NewRetTy == TD->getIntPtrType())))
+ NewRetTy == TD->getIntPtrType(Caller->getContext()))))
return false; // Cannot transform this return value.
if (!Caller->use_empty() &&
// void -> non-void is handled specially
- NewRetTy != Type::VoidTy && !CastInst::isCastable(NewRetTy, OldRetTy))
+ NewRetTy != Type::getVoidTy(*Context) && !CastInst::isCastable(NewRetTy, OldRetTy))
return false; // Cannot transform this return value.
if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
// Converting from one pointer type to another or between a pointer and an
// integer of the same size is safe even if we do not have a body.
bool isConvertible = ActTy == ParamTy ||
- (TD && ((isa<PointerType>(ParamTy) || ParamTy == TD->getIntPtrType()) &&
- (isa<PointerType>(ActTy) || ActTy == TD->getIntPtrType())));
+ (TD && ((isa<PointerType>(ParamTy) ||
+ ParamTy == TD->getIntPtrType(Caller->getContext())) &&
+ (isa<PointerType>(ActTy) ||
+ ActTy == TD->getIntPtrType(Caller->getContext()))));
if (Callee->isDeclaration() && !isConvertible) return false;
}
if (Attributes FnAttrs = CallerPAL.getFnAttributes())
attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
- if (NewRetTy == Type::VoidTy)
+ if (NewRetTy == Type::getVoidTy(*Context))
Caller->setName(""); // Void type should not have a name.
const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
// Insert a cast of the return type as necessary.
Value *NV = NC;
if (OldRetTy != NV->getType() && !Caller->use_empty()) {
- if (NV->getType() != Type::VoidTy) {
+ if (NV->getType() != Type::getVoidTy(*Context)) {
Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
OldRetTy, false);
NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp");
}
}
- if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
+ if (Caller->getType() != Type::getVoidTy(*Context) && !Caller->use_empty())
Caller->replaceAllUsesWith(NV);
Caller->eraseFromParent();
RemoveFromWorkList(Caller);
setCallingConv(cast<CallInst>(Caller)->getCallingConv());
cast<CallInst>(NewCaller)->setAttributes(NewPAL);
}
- if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
+ if (Caller->getType() != Type::getVoidTy(*Context) && !Caller->use_empty())
Caller->replaceAllUsesWith(NewCaller);
Caller->eraseFromParent();
RemoveFromWorkList(Caller);
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
CmpInst *CIOp = cast<CmpInst>(FirstInst);
- return CmpInst::Create(*Context, CIOp->getOpcode(), CIOp->getPredicate(),
+ return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
LHSVal, RHSVal);
}
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst))
return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst))
- return CmpInst::Create(*Context, CIOp->getOpcode(), CIOp->getPredicate(),
+ return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
PhiVal, ConstantOp);
assert(isa<LoadInst>(FirstInst) && "Unknown operation");
Value *Op = *i;
if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) {
if (Constant *C = dyn_cast<Constant>(Op)) {
- *i = ConstantExpr::getTrunc(C, TD->getIntPtrType());
+ *i = ConstantExpr::getTrunc(C, TD->getIntPtrType(GEP.getContext()));
MadeChange = true;
} else {
- Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(),
+ Op = InsertCastBefore(Instruction::Trunc, Op,
+ TD->getIntPtrType(GEP.getContext()),
GEP);
*i = Op;
MadeChange = true;
} else if (TD->getTypeSizeInBits(Op->getType())
< TD->getPointerSizeInBits()) {
if (Constant *C = dyn_cast<Constant>(Op)) {
- *i = ConstantExpr::getSExt(C, TD->getIntPtrType());
+ *i = ConstantExpr::getSExt(C, TD->getIntPtrType(GEP.getContext()));
MadeChange = true;
} else {
- Op = InsertCastBefore(Instruction::SExt, Op, TD->getIntPtrType(),
- GEP);
+ Op = InsertCastBefore(Instruction::SExt, Op,
+ TD->getIntPtrType(GEP.getContext()), GEP);
*i = Op;
MadeChange = true;
}
// Convert SO1 to GO1's type.
SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this);
} else {
- const Type *PT = TD->getIntPtrType();
+ const Type *PT = TD->getIntPtrType(GEP.getContext());
SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this);
GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this);
}
TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
TD->getTypeAllocSize(ResElTy)) {
Value *Idx[2];
- Idx[0] = Constant::getNullValue(Type::Int32Ty);
+ Idx[0] = Constant::getNullValue(Type::getInt32Ty(*Context));
Idx[1] = GEP.getOperand(1);
GetElementPtrInst *NewGEP =
GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
// (where tmp = 8*tmp2) into:
// getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
- if (TD && isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
+ if (TD && isa<ArrayType>(SrcElTy) && ResElTy == Type::getInt8Ty(*Context)) {
uint64_t ArrayEltSize =
TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType());
// Insert the new GEP instruction.
Value *Idx[2];
- Idx[0] = Constant::getNullValue(Type::Int32Ty);
+ Idx[0] = Constant::getNullValue(Type::getInt32Ty(*Context));
Idx[1] = NewIdx;
Instruction *NewGEP =
GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
// Now that I is pointing to the first non-allocation-inst in the block,
// insert our getelementptr instruction...
//
- Value *NullIdx = Constant::getNullValue(Type::Int32Ty);
+ Value *NullIdx = Constant::getNullValue(Type::getInt32Ty(*Context));
Value *Idx[2];
Idx[0] = NullIdx;
Idx[1] = NullIdx;
if (isa<UndefValue>(Op)) {
// Insert a new store to null because we cannot modify the CFG here.
new StoreInst(ConstantInt::getTrue(*Context),
- UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI);
+ UndefValue::get(PointerType::getUnqual(Type::getInt1Ty(*Context))), &FI);
return EraseInstFromFunction(FI);
}
if (Constant *CSrc = dyn_cast<Constant>(CastOp))
if (ASrcTy->getNumElements() != 0) {
Value *Idxs[2];
- Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty);
+ Idxs[0] = Idxs[1] = Constant::getNullValue(Type::getInt32Ty(*Context));
CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
SrcTy = cast<PointerType>(CastOp->getType());
SrcPTy = SrcTy->getElementType();
// constants.
if (isa<ArrayType>(SrcPTy) || isa<StructType>(SrcPTy)) {
// Index through pointer.
- Constant *Zero = Constant::getNullValue(Type::Int32Ty);
+ Constant *Zero = Constant::getNullValue(Type::getInt32Ty(*IC.getContext()));
NewGEPIndices.push_back(Zero);
while (1) {
if (A == B) return true;
// Test if the values come form identical arithmetic instructions.
+ // This uses isIdenticalToWhenDefined instead of isIdenticalTo because
+ // its only used to compare two uses within the same basic block, which
+ // means that they'll always either have the same value or one of them
+ // will have an undefined value.
if (isa<BinaryOperator>(A) ||
isa<CastInst>(A) ||
isa<PHINode>(A) ||
isa<GetElementPtrInst>(A))
if (Instruction *BI = dyn_cast<Instruction>(B))
- if (cast<Instruction>(A)->isIdenticalTo(BI))
+ if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
return true;
// Otherwise they may not be equivalent.
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()) {
unsigned AS =
cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace();
Value *Ptr = InsertBitCastBefore(I->getOperand(0),
- PointerType::get(EI.getType(), AS),EI);
+ PointerType::get(EI.getType(), AS),*I);
GetElementPtrInst *GEP =
GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep");
cast<GEPOperator>(GEP)->setIsInBounds(true);
- InsertNewInstBefore(GEP, EI);
- return new LoadInst(GEP);
+ InsertNewInstBefore(GEP, *I);
+ LoadInst* Load = new LoadInst(GEP, "tmp");
+ InsertNewInstBefore(Load, *I);
+ return ReplaceInstUsesWith(EI, Load);
}
}
if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
}
return ExtractElementInst::Create(Src,
- ConstantInt::get(Type::Int32Ty, SrcIdx, false));
+ ConstantInt::get(Type::getInt32Ty(*Context), SrcIdx, false));
}
}
// FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
if (isa<UndefValue>(V)) {
- Mask.assign(NumElts, UndefValue::get(Type::Int32Ty));
+ Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(*Context)));
return true;
} else if (V == LHS) {
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
+ Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i));
return true;
} else if (V == RHS) {
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts));
+ Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i+NumElts));
return true;
} else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
// If this is an insert of an extract from some other vector, include it.
// transitively ok.
if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) {
// If so, update the mask to reflect the inserted undef.
- Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty);
+ Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(*Context));
return true;
}
} else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
// If so, update the mask to reflect the inserted value.
if (EI->getOperand(0) == LHS) {
Mask[InsertedIdx % NumElts] =
- ConstantInt::get(Type::Int32Ty, ExtractedIdx);
+ ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx);
} else {
assert(EI->getOperand(0) == RHS);
Mask[InsertedIdx % NumElts] =
- ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts);
+ ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx+NumElts);
}
return true;
unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
if (isa<UndefValue>(V)) {
- Mask.assign(NumElts, UndefValue::get(Type::Int32Ty));
+ Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(*Context)));
return V;
} else if (isa<ConstantAggregateZero>(V)) {
- Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0));
+ Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(*Context), 0));
return V;
} else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
// If this is an insert of an extract from some other vector, include it.
RHS = EI->getOperand(0);
Value *V = CollectShuffleElements(VecOp, Mask, RHS, Context);
Mask[InsertedIdx % NumElts] =
- ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx);
+ ConstantInt::get(Type::getInt32Ty(*Context), NumElts+ExtractedIdx);
return V;
}
// Everything but the extracted element is replaced with the RHS.
for (unsigned i = 0; i != NumElts; ++i) {
if (i != InsertedIdx)
- Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i);
+ Mask[i] = ConstantInt::get(Type::getInt32Ty(*Context), NumElts+i);
}
return V;
}
// Otherwise, can't do anything fancy. Return an identity vector.
for (unsigned i = 0; i != NumElts; ++i)
- Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
+ Mask.push_back(ConstantInt::get(Type::getInt32Ty(*Context), i));
return V;
}
// Build a new shuffle mask.
std::vector<Constant*> Mask;
if (isa<UndefValue>(VecOp))
- Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty));
+ Mask.assign(NumVectorElts, UndefValue::get(Type::getInt32Ty(*Context)));
else {
assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing");
- Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty,
+ Mask.assign(NumVectorElts, ConstantInt::get(Type::getInt32Ty(*Context),
NumVectorElts));
}
Mask[InsertedIdx] =
- ConstantInt::get(Type::Int32Ty, ExtractedIdx);
+ ConstantInt::get(Type::getInt32Ty(*Context), ExtractedIdx);
return new ShuffleVectorInst(EI->getOperand(0), VecOp,
ConstantVector::get(Mask));
}
std::vector<Constant*> Elts;
for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
if (Mask[i] >= 2*e)
- Elts.push_back(UndefValue::get(Type::Int32Ty));
+ Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
else {
if ((Mask[i] >= e && isa<UndefValue>(RHS)) ||
(Mask[i] < e && isa<UndefValue>(LHS))) {
Mask[i] = 2*e; // Turn into undef.
- Elts.push_back(UndefValue::get(Type::Int32Ty));
+ Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
} else {
Mask[i] = Mask[i] % e; // Force to LHS.
- Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i]));
+ Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context), Mask[i]));
}
}
}
std::vector<Constant*> Elts;
for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
if (NewMask[i] >= LHSInNElts*2) {
- Elts.push_back(UndefValue::get(Type::Int32Ty));
+ Elts.push_back(UndefValue::get(Type::getInt32Ty(*Context)));
} else {
- Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i]));
+ Elts.push_back(ConstantInt::get(Type::getInt32Ty(*Context), NewMask[i]));
}
}
return new ShuffleVectorInst(LHSSVI->getOperand(0),
// DCE instruction if trivially dead.
if (isInstructionTriviallyDead(Inst)) {
++NumDeadInst;
- DOUT << "IC: DCE: " << *Inst;
+ DEBUG(errs() << "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;
+ DEBUG(errs() << "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;
+ DEBUG(errs() << "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;
+ DEBUG(errs() << "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;
+ DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');
// Add operands to the worklist.
AddUsesToWorkList(*I);
#ifndef NDEBUG
std::string OrigI;
#endif
- DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str(););
+ DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str(););
if (Instruction *Result = visit(*I)) {
++NumCombined;
// Should we replace the old instruction with a new one?
if (Result != I) {
- DOUT << "IC: Old = " << *I
- << " New = " << *Result;
+ DEBUG(errs() << "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;
+ DEBUG(errs() << "IC: Mod = " << OrigI << '\n'
+ << " New = " << *I << '\n');
#endif
// If the instruction was modified, it's possible that it is now dead.