diff options
author | Owen Anderson <resistor@mac.com> | 2009-07-03 00:17:18 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2009-07-03 00:17:18 +0000 |
commit | d672ecb0178c6247a5eaa5b0fb0c3b23cd25bd7c (patch) | |
tree | c31a0d8e0a4c3f954438945d2b70e61050ba099b /lib/Transforms/Scalar/InstructionCombining.cpp | |
parent | 1a55180238dbcf11113f610aea010447e51f595b (diff) |
Convert the first batch of passes to use LLVMContext.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74748 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar/InstructionCombining.cpp')
-rw-r--r-- | lib/Transforms/Scalar/InstructionCombining.cpp | 1307 |
1 files changed, 695 insertions, 612 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 16e5ce07c3..59fbd396a3 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -36,6 +36,7 @@ #define DEBUG_TYPE "instcombine" #include "llvm/Transforms/Scalar.h" #include "llvm/IntrinsicInst.h" +#include "llvm/LLVMContext.h" #include "llvm/Pass.h" #include "llvm/DerivedTypes.h" #include "llvm/GlobalVariable.h" @@ -82,6 +83,8 @@ namespace { static char ID; // Pass identification, replacement for typeid InstCombiner() : FunctionPass(&ID) {} + LLVMContext* getContext() { return Context; } + /// AddToWorkList - Add the specified instruction to the worklist if it /// isn't already in it. void AddToWorkList(Instruction *I) { @@ -140,7 +143,7 @@ namespace { if (Instruction *Op = dyn_cast<Instruction>(*i)) { AddToWorkList(Op); // Set the operand to undef to drop the use. - *i = UndefValue::get(Op->getType()); + *i = Context->getUndef(Op->getType()); } return R; @@ -278,7 +281,7 @@ namespace { if (V->getType() == Ty) return V; if (Constant *CV = dyn_cast<Constant>(V)) - return ConstantExpr::getCast(opc, CV, Ty); + return Context->getConstantExprCast(opc, CV, Ty); Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos); AddToWorkList(C); @@ -304,7 +307,7 @@ namespace { } else { // If we are replacing the instruction with itself, this must be in a // segment of unreachable code, so just clobber the instruction. - I.replaceAllUsesWith(UndefValue::get(I.getType())); + I.replaceAllUsesWith(Context->getUndef(I.getType())); return &I; } } @@ -525,7 +528,7 @@ bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { if (isa<Constant>(I.getOperand(1))) { - Constant *Folded = ConstantExpr::get(I.getOpcode(), + Constant *Folded = Context->getConstantExpr(I.getOpcode(), cast<Constant>(I.getOperand(1)), cast<Constant>(Op->getOperand(1))); I.setOperand(0, Op->getOperand(0)); @@ -538,7 +541,7 @@ bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { Constant *C2 = cast<Constant>(Op1->getOperand(1)); // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) - Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); + Constant *Folded = Context->getConstantExpr(I.getOpcode(), C1, C2); Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0), Op1->getOperand(0), Op1->getName(), &I); @@ -565,17 +568,17 @@ bool InstCombiner::SimplifyCompare(CmpInst &I) { // 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) { +static inline Value *dyn_castNegVal(Value *V, LLVMContext* Context) { if (BinaryOperator::isNeg(V)) return BinaryOperator::getNegArgument(V); // Constants can be considered to be negated values if they can be folded. if (ConstantInt *C = dyn_cast<ConstantInt>(V)) - return ConstantExpr::getNeg(C); + return Context->getConstantExprNeg(C); if (ConstantVector *C = dyn_cast<ConstantVector>(V)) if (C->getType()->getElementType()->isInteger()) - return ConstantExpr::getNeg(C); + return Context->getConstantExprNeg(C); return 0; } @@ -584,28 +587,28 @@ static inline Value *dyn_castNegVal(Value *V) { // instruction if the LHS is a constant negative zero (which is the 'negate' // form). // -static inline Value *dyn_castFNegVal(Value *V) { +static inline Value *dyn_castFNegVal(Value *V, LLVMContext* Context) { if (BinaryOperator::isFNeg(V)) return BinaryOperator::getFNegArgument(V); // Constants can be considered to be negated values if they can be folded. if (ConstantFP *C = dyn_cast<ConstantFP>(V)) - return ConstantExpr::getFNeg(C); + return Context->getConstantExprFNeg(C); if (ConstantVector *C = dyn_cast<ConstantVector>(V)) if (C->getType()->getElementType()->isFloatingPoint()) - return ConstantExpr::getFNeg(C); + return Context->getConstantExprFNeg(C); return 0; } -static inline Value *dyn_castNotVal(Value *V) { +static inline Value *dyn_castNotVal(Value *V, LLVMContext* Context) { 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(~C->getValue()); + return Context->getConstantInt(~C->getValue()); return 0; } @@ -614,7 +617,8 @@ static inline Value *dyn_castNotVal(Value *V) { // 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) { +static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST, + LLVMContext* Context) { if (V->hasOneUse() && V->getType()->isInteger()) if (Instruction *I = dyn_cast<Instruction>(V)) { if (I->getOpcode() == Instruction::Mul) @@ -625,7 +629,7 @@ static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { // The multiplier is really 1 << CST. uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); uint32_t CSTVal = CST->getLimitedValue(BitWidth); - CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); + CST = Context->getConstantInt(APInt(BitWidth, 1).shl(CSTVal)); return I->getOperand(0); } } @@ -654,16 +658,19 @@ static unsigned getOpcode(const Value *V) { } /// AddOne - Add one to a ConstantInt -static Constant *AddOne(Constant *C) { - return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); +static Constant *AddOne(Constant *C, LLVMContext* Context) { + return Context->getConstantExprAdd(C, + Context->getConstantInt(C->getType(), 1)); } /// SubOne - Subtract one from a ConstantInt -static Constant *SubOne(ConstantInt *C) { - return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1)); +static Constant *SubOne(ConstantInt *C, LLVMContext* Context) { + return Context->getConstantExprSub(C, + Context->getConstantInt(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) { +static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign, + LLVMContext* Context) { uint32_t W = C1->getBitWidth(); APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); if (sign) { @@ -690,7 +697,7 @@ static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool 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) { + APInt Demanded, LLVMContext* Context) { assert(I && "No instruction?"); assert(OpNo < I->getNumOperands() && "Operand index too large"); @@ -705,7 +712,7 @@ static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, // This instruction is producing bits that are not demanded. Shrink the RHS. Demanded &= OpC->getValue(); - I->setOperand(OpNo, ConstantInt::get(Demanded)); + I->setOperand(OpNo, Context->getConstantInt(Demanded)); return true; } @@ -837,7 +844,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, if (DemandedMask == 0) { // Not demanding any bits from V. if (isa<UndefValue>(V)) return 0; - return UndefValue::get(VTy); + return Context->getUndef(VTy); } if (Depth == 6) // Limit search depth. @@ -879,7 +886,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, // If all of the demanded bits in the inputs are known zeros, return zero. if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) - return Constant::getNullValue(VTy); + return Context->getNullValue(VTy); } else if (I->getOpcode() == Instruction::Or) { // We can simplify (X|Y) -> X or Y in the user's context if we know that @@ -948,10 +955,10 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, // If all of the demanded bits in the inputs are known zeros, return zero. if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) - return Constant::getNullValue(VTy); + return Context->getNullValue(VTy); // If the RHS is a constant, see if we can simplify it. - if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) + if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero, Context)) return I; // Output known-1 bits are only known if set in both the LHS & RHS. @@ -988,7 +995,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, return I->getOperand(1); // If the RHS is a constant, see if we can simplify it. - if (ShrinkDemandedConstant(I, 1, DemandedMask)) + if (ShrinkDemandedConstant(I, 1, DemandedMask, Context)) return I; // Output known-0 bits are only known if clear in both the LHS & RHS. @@ -1036,7 +1043,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { // all known if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { - Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); + Constant *AndC = Context->getConstantInt(~RHSKnownOne & DemandedMask); Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp"); return InsertNewInstBefore(And, *I); @@ -1045,7 +1052,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, // 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)) + if (ShrinkDemandedConstant(I, 1, DemandedMask, Context)) return I; RHSKnownZero = KnownZeroOut; @@ -1062,8 +1069,8 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, 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) || - ShrinkDemandedConstant(I, 2, DemandedMask)) + if (ShrinkDemandedConstant(I, 1, DemandedMask, Context) || + ShrinkDemandedConstant(I, 2, DemandedMask, Context)) return I; // Only known if known in both the LHS and RHS. @@ -1187,7 +1194,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, // If the RHS of the add has bits set that can't affect the input, reduce // the constant. - if (ShrinkDemandedConstant(I, 1, InDemandedBits)) + if (ShrinkDemandedConstant(I, 1, InDemandedBits, Context)) return I; // Avoid excess work. @@ -1408,10 +1415,10 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, Instruction *NewVal; if (InputBit > ResultBit) NewVal = BinaryOperator::CreateLShr(I->getOperand(1), - ConstantInt::get(I->getType(), InputBit-ResultBit)); + Context->getConstantInt(I->getType(), InputBit-ResultBit)); else NewVal = BinaryOperator::CreateShl(I->getOperand(1), - ConstantInt::get(I->getType(), ResultBit-InputBit)); + Context->getConstantInt(I->getType(), ResultBit-InputBit)); NewVal->takeName(I); return InsertNewInstBefore(NewVal, *I); } @@ -1428,9 +1435,9 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, // If the client is only demanding bits that we know, return the known // constant. if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { - Constant *C = ConstantInt::get(RHSKnownOne); + Constant *C = Context->getConstantInt(RHSKnownOne); if (isa<PointerType>(V->getType())) - C = ConstantExpr::getIntToPtr(C, V->getType()); + C = Context->getConstantExprIntToPtr(C, V->getType()); return C; } return false; @@ -1458,13 +1465,13 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, return 0; } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. UndefElts = EltMask; - return UndefValue::get(V->getType()); + return Context->getUndef(V->getType()); } UndefElts = 0; if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); - Constant *Undef = UndefValue::get(EltTy); + Constant *Undef = Context->getUndef(EltTy); std::vector<Constant*> Elts; for (unsigned i = 0; i != VWidth; ++i) @@ -1479,7 +1486,7 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, } // If we changed the constant, return it. - Constant *NewCP = ConstantVector::get(Elts); + Constant *NewCP = Context->getConstantVector(Elts); return NewCP != CP ? NewCP : 0; } else if (isa<ConstantAggregateZero>(V)) { // Simplify the CAZ to a ConstantVector where the non-demanded elements are @@ -1491,15 +1498,15 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, return 0; const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); - Constant *Zero = Constant::getNullValue(EltTy); - Constant *Undef = UndefValue::get(EltTy); + Constant *Zero = Context->getNullValue(EltTy); + Constant *Undef = Context->getUndef(EltTy); std::vector<Constant*> Elts; for (unsigned i = 0; i != VWidth; ++i) { Constant *Elt = DemandedElts[i] ? Zero : Undef; Elts.push_back(Elt); } UndefElts = DemandedElts ^ EltMask; - return ConstantVector::get(Elts); + return Context->getConstantVector(Elts); } // Limit search depth. @@ -1613,12 +1620,12 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, std::vector<Constant*> Elts; for (unsigned i = 0; i < VWidth; ++i) { if (UndefElts[i]) - Elts.push_back(UndefValue::get(Type::Int32Ty)); + Elts.push_back(Context->getUndef(Type::Int32Ty)); else - Elts.push_back(ConstantInt::get(Type::Int32Ty, + Elts.push_back(Context->getConstantInt(Type::Int32Ty, Shuffle->getMaskValue(i))); } - I->setOperand(2, ConstantVector::get(Elts)); + I->setOperand(2, Context->getConstantVector(Elts)); MadeChange = true; } break; @@ -1763,8 +1770,8 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, } Instruction *New = - InsertElementInst::Create(UndefValue::get(II->getType()), TmpV, 0U, - II->getName()); + InsertElementInst::Create( + Context->getUndef(II->getType()), TmpV, 0U, II->getName()); InsertNewInstBefore(New, *II); AddSoonDeadInstToWorklist(*II, 0); return New; @@ -1792,7 +1799,8 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, /// 'shouldApply' and 'apply' methods. /// template<typename Functor> -static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { +static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F, + LLVMContext* Context) { unsigned Opcode = Root.getOpcode(); Value *LHS = Root.getOperand(0); @@ -1825,7 +1833,7 @@ static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { // Make what used to be the LHS of the root be the user of the root... Value *ExtraOperand = TmpLHSI->getOperand(1); if (&Root == TmpLHSI) { - Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); + Root.replaceAllUsesWith(Context->getNullValue(TmpLHSI->getType())); return 0; } Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI @@ -1864,11 +1872,12 @@ namespace { // AddRHS - Implements: X + X --> X << 1 struct AddRHS { Value *RHS; - AddRHS(Value *rhs) : RHS(rhs) {} + LLVMContext* Context; + AddRHS(Value *rhs, LLVMContext* C) : RHS(rhs), Context(C) {} bool shouldApply(Value *LHS) const { return LHS == RHS; } Instruction *apply(BinaryOperator &Add) const { return BinaryOperator::CreateShl(Add.getOperand(0), - ConstantInt::get(Add.getType(), 1)); + Context->getConstantInt(Add.getType(), 1)); } }; @@ -1876,11 +1885,12 @@ struct AddRHS { // iff C1&C2 == 0 struct AddMaskingAnd { Constant *C2; - AddMaskingAnd(Constant *c) : C2(c) {} + LLVMContext* Context; + AddMaskingAnd(Constant *c, LLVMContext* C) : C2(c), Context(C) {} bool shouldApply(Value *LHS) const { ConstantInt *C1; return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && - ConstantExpr::getAnd(C1, C2)->isNullValue(); + Context->getConstantExprAnd(C1, C2)->isNullValue(); } Instruction *apply(BinaryOperator &Add) const { return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1)); @@ -1891,6 +1901,8 @@ struct AddMaskingAnd { 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); } @@ -1901,8 +1913,8 @@ static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, if (Constant *SOC = dyn_cast<Constant>(SO)) { if (ConstIsRHS) - return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); - return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); + return Context->getConstantExpr(I.getOpcode(), SOC, ConstOperand); + return Context->getConstantExpr(I.getOpcode(), ConstOperand, SOC); } Value *Op0 = SO, *Op1 = ConstOperand; @@ -1992,9 +2004,9 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { Value *InV = 0; if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { if (CmpInst *CI = dyn_cast<CmpInst>(&I)) - InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); + InV = Context->getConstantExprCompare(CI->getPredicate(), InC, C); else - InV = ConstantExpr::get(I.getOpcode(), InC, C); + InV = Context->getConstantExpr(I.getOpcode(), InC, C); } else { assert(PN->getIncomingBlock(i) == NonConstBB); if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) @@ -2019,7 +2031,7 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { for (unsigned i = 0; i != NumPHIValues; ++i) { Value *InV; if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { - InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); + InV = Context->getConstantExprCast(CI->getOpcode(), InC, RetTy); } else { assert(PN->getIncomingBlock(i) == NonConstBB); InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), @@ -2091,8 +2103,8 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { if (CI->isAllOnesValue() && ZI->getOperand(0)->getType() == Type::Int1Ty) return SelectInst::Create(ZI->getOperand(0), - Constant::getNullValue(I.getType()), - ConstantInt::getAllOnesValue(I.getType())); + Context->getNullValue(I.getType()), + Context->getConstantIntAllOnesValue(I.getType())); } if (isa<PHINode>(LHS)) @@ -2151,7 +2163,8 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // X + X --> X << 1 if (I.getType()->isInteger()) { - if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; + if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS, Context), Context)) + return Result; if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { if (RHSI->getOpcode() == Instruction::Sub) @@ -2167,9 +2180,9 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // -A + B --> B - A // -A + -B --> -(A + B) - if (Value *LHSV = dyn_castNegVal(LHS)) { + if (Value *LHSV = dyn_castNegVal(LHS, Context)) { if (LHS->getType()->isIntOrIntVector()) { - if (Value *RHSV = dyn_castNegVal(RHS)) { + if (Value *RHSV = dyn_castNegVal(RHS, Context)) { Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum"); InsertNewInstBefore(NewAdd, I); return BinaryOperator::CreateNeg(NewAdd); @@ -2181,33 +2194,34 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // A + -B --> A - B if (!isa<Constant>(RHS)) - if (Value *V = dyn_castNegVal(RHS)) + if (Value *V = dyn_castNegVal(RHS, Context)) return BinaryOperator::CreateSub(LHS, V); ConstantInt *C2; - if (Value *X = dyn_castFoldableMul(LHS, C2)) { + if (Value *X = dyn_castFoldableMul(LHS, C2, Context)) { if (X == RHS) // X*C + X --> X * (C+1) - return BinaryOperator::CreateMul(RHS, AddOne(C2)); + return BinaryOperator::CreateMul(RHS, AddOne(C2, Context)); // X*C1 + X*C2 --> X * (C1+C2) ConstantInt *C1; - if (X == dyn_castFoldableMul(RHS, C1)) - return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2)); + if (X == dyn_castFoldableMul(RHS, C1, Context)) + return BinaryOperator::CreateMul(X, Context->getConstantExprAdd(C1, C2)); } // X + X*C --> X * (C+1) - if (dyn_castFoldableMul(RHS, C2) == LHS) - return BinaryOperator::CreateMul(LHS, AddOne(C2)); + if (dyn_castFoldableMul(RHS, C2, Context) == LHS) + return BinaryOperator::CreateMul(LHS, AddOne(C2, Context)); // X + ~X --> -1 since ~X = -X-1 - if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS) - return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); + if (dyn_castNotVal(LHS, Context) == RHS || + dyn_castNotVal(RHS, Context) == LHS) + return ReplaceInstUsesWith(I, Context->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)))) - if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) + if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2, Context), Context)) return R; // A+B --> A|B iff A and B have no bits set in common. @@ -2254,11 +2268,11 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { Value *X = 0; if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X - return BinaryOperator::CreateSub(SubOne(CRHS), X); + return BinaryOperator::CreateSub(SubOne(CRHS, Context), X); // (X & FF00) + xx00 -> (X+xx00) & FF00 if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { - Constant *Anded = ConstantExpr::getAnd(CRHS, C2); + Constant *Anded = Context->getConstantExprAnd(CRHS, C2); if (Anded == CRHS) { // See if all bits from the first bit set in the Add RHS up are included // in the mask. First, get the rightmost bit. @@ -2301,7 +2315,7 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { unsigned AS = cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace(); Value *I2 = InsertBitCastBefore(CI->getOperand(0), - PointerType::get(Type::Int8Ty, AS), I); + Context->getPointerType(Type::Int8Ty, AS), I); I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); return new PtrToIntInst(I2, CI->getType()); } @@ -2337,9 +2351,9 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // (add (sext x), cst) --> (sext (add x, cst')) if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { Constant *CI = - ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); + Context->getConstantExprTrunc(RHSC, LHSConv->getOperand(0)->getType()); if (LHSConv->hasOneUse() && - ConstantExpr::getSExt(CI, I.getType()) == RHSC && + Context->getConstantExprSExt(CI, I.getType()) == RHSC && WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { // Insert the new, smaller add. Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), @@ -2378,7 +2392,7 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { if (Constant *RHSC = dyn_cast<Constant>(RHS)) { // X + 0 --> X if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { - if (CFP->isExactlyValue(ConstantFP::getNegativeZero + if (CFP->isExactlyValue(Context->getConstantFPNegativeZero (I.getType())->getValueAPF())) return ReplaceInstUsesWith(I, LHS); } @@ -2390,12 +2404,12 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { // -A + B --> B - A // -A + -B --> -(A + B) - if (Value *LHSV = dyn_castFNegVal(LHS)) + if (Value *LHSV = dyn_castFNegVal(LHS, Context)) return BinaryOperator::CreateFSub(RHS, LHSV); // A + -B --> A - B if (!isa<Constant>(RHS)) - if (Value *V = dyn_castFNegVal(RHS)) + if (Value *V = dyn_castFNegVal(RHS, Context)) return BinaryOperator::CreateFSub(LHS, V); // Check for X+0.0. Simplify it to X if we know X is not -0.0. @@ -2413,9 +2427,9 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { // instcombined. if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { Constant *CI = - ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); + Context->getConstantExprFPToSI(CFP, LHSConv->getOperand(0)->getType()); if (LHSConv->hasOneUse() && - ConstantExpr::getSIToFP(CI, I.getType()) == CFP && + Context->getConstantExprSIToFP(CI, I.getType()) == CFP && WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { // Insert the new integer add. Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), @@ -2451,10 +2465,10 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); if (Op0 == Op1) // sub X, X -> 0 - return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); // If this is a 'B = x-(-A)', change to B = x+A... - if (Value *V = dyn_castNegVal(Op1)) + if (Value *V = dyn_castNegVal(Op1, Context)) return BinaryOperator::CreateAdd(Op0, V); if (isa<UndefValue>(Op0)) @@ -2470,7 +2484,7 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { // C - ~X == X + (1+C) Value *X = 0; if (match(Op1, m_Not(m_Value(X)))) - return BinaryOperator::CreateAdd(X, AddOne(C)); + return BinaryOperator::CreateAdd(X, AddOne(C, Context)); // -(X >>u 31) -> (X >>s 31) // -(X >>s 31) -> (X >>u 31) @@ -2519,8 +2533,8 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) // C1-(X+C2) --> (C1-C2)-X - return BinaryOperator::CreateSub(ConstantExpr::getSub(CI1, CI2), - Op1I->getOperand(0)); + return BinaryOperator::CreateSub( + Context->getConstantExprSub(CI1, CI2), Op1I->getOperand(0)); } } @@ -2555,12 +2569,13 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { if (CSI->isZero()) if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) return BinaryOperator::CreateSDiv(Op1I->getOperand(0), - ConstantExpr::getNeg(DivRHS)); + Context->getConstantExprNeg(DivRHS)); // X - X*C --> X * (1-C) ConstantInt *C2 = 0; - if (dyn_castFoldableMul(Op1I, C2) == Op0) { - Constant *CP1 = ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), + if (dyn_castFoldableMul(Op1I, C2, Context) == Op0) { + Constant *CP1 = + Context->getConstantExprSub(Context->getConstantInt(I.getType(), 1), C2); return BinaryOperator::CreateMul(Op0, CP1); } @@ -2580,13 +2595,13 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { } ConstantInt *C1; - if (Value *X = dyn_castFoldableMul(Op0, C1)) { + if (Value *X = dyn_castFoldableMul(Op0, C1, Context)) { if (X == Op1) // X*C - X --> X * (C-1) - return BinaryOperator::CreateMul(Op1, SubOne(C1)); + return BinaryOperator::CreateMul(Op1, SubOne(C1, Context)); ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) - if (X == dyn_castFoldableMul(Op1, C2)) - return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2)); + if (X == dyn_castFoldableMul(Op1, C2, Context)) + return BinaryOperator::CreateMul(X, Context->getConstantExprSub(C1, C2)); } return 0; } @@ -2595,7 +2610,7 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) { 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)) + if (Value *V = dyn_castFNegVal(Op1, Context)) return BinaryOperator::CreateFAdd(Op0, V); if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { @@ -2647,7 +2662,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { // TODO: If Op1 is undef and Op0 is finite, return zero. if (!I.getType()->isFPOrFPVector() && isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 - return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); // Simplify mul instructions with a constant RHS... if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { @@ -2658,7 +2673,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { if (SI->getOpcode() == Instruction::Shl) if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) return BinaryOperator::CreateMul(SI->getOperand(0), - ConstantExpr::getShl(CI, ShOp)); + Context->getConstantExprShl(CI, ShOp)); if (CI->isZero()) return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 @@ -2670,7 +2685,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { const APInt& Val = cast<ConstantInt>(CI)->getValue(); if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C return BinaryOperator::CreateShl(Op0, - ConstantInt::get(Op0->getType(), Val.logBase2())); + Context->getConstantInt(Op0->getType(), Val.logBase2())); } } else if (isa<VectorType>(Op1->getType())) { // TODO: If Op1 is all zeros and Op0 is all finite, return all zeros. @@ -2695,7 +2710,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0), Op1, "tmp"); InsertNewInstBefore(Add, I); - Value *C1C2 = ConstantExpr::getMul(Op1, + Value *C1C2 = Context->getConstantExprMul(Op1, cast<Constant>(Op0I->getOperand(1))); return BinaryOperator::CreateAdd(Add, C1C2); @@ -2711,8 +2726,8 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { return NV; } - if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y - if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) + if (Value *Op0v = dyn_castNegVal(Op0, Context)) // -X * -Y = X*Y + if (Value *Op1v = dyn_castNegVal(I.getOperand(1), Context)) return BinaryOperator::CreateMul(Op0v, Op1v); // (X / Y) * Y = X - (X % Y) @@ -2726,7 +2741,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { Op1 = Op0; BO = dyn_cast<BinaryOperator>(I.getOperand(1)); } - Value *Neg = dyn_castNegVal(Op1); + Value *Neg = dyn_castNegVal(Op1, Context); if (BO && BO->hasOneUse() && (BO->getOperand(1) == Op1 || BO->getOperand(1) == Neg) && (BO->getOpcode() == Instruction::UDiv || @@ -2776,7 +2791,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && TIS) { // Shift the X value right to turn it into "all signbits". - Constant *Amt = ConstantInt::get(SCIOp0->getType(), + Constant *Amt = Context->getConstantInt(SCIOp0->getType(), SCOpTy->getPrimitiveSizeInBits()-1); Value *V = InsertNewInstBefore( @@ -2836,8 +2851,8 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) { return NV; } - if (Value *Op0v = dyn_castFNegVal(Op0)) // -X * -Y = X*Y - if (Value *Op1v = dyn_castFNegVal(I.getOperand(1))) + if (Value *Op0v = dyn_castFNegVal(Op0, Context)) // -X * -Y = X*Y + if (Value *Op1v = dyn_castFNegVal(I.getOperand(1), Context)) return BinaryOperator::CreateFMul(Op0v, Op1v); return Changed ? &I : 0; @@ -2894,8 +2909,8 @@ bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { *I = SI->getOperand(NonNullOperand); AddToWorkList(BBI); } else if (*I == SelectCond) { - *I = NonNullOperand == 1 ? ConstantInt::getTrue() : - ConstantInt::getFalse(); + *I = NonNullOperand == 1 ? Context->getConstantIntTrue() : + Context->getConstantIntFalse(); AddToWorkList(BBI); } } @@ -2927,7 +2942,7 @@ Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { if (isa<UndefValue>(Op0)) { if (Op0->getType()->isFPOrFPVector()) return ReplaceInstUsesWith(I, Op0); - return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); } // X / undef -> undef @@ -2947,12 +2962,12 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { // (sdiv X, X) --> 1 (udiv X, X) --> 1 if (Op0 == Op1) { if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) { - Constant *CI = ConstantInt::get(Ty->getElementType(), 1); + Constant *CI = Context->getConstantInt(Ty->getElementType(), 1); std::vector<Constant*> Elts(Ty->getNumElements(), CI); - return ReplaceInstUsesWith(I, ConstantVector::get(Elts)); + return ReplaceInstUsesWith(I, Context->getConstantVector(Elts)); } - Constant *CI = ConstantInt::get(I.getType(), 1); + Constant *CI = Context->getConstantInt(I.getType(), 1); return ReplaceInstUsesWith(I, CI); } @@ -2973,11 +2988,12 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { if (Instruction *LHS = dyn_cast<Instruction>(Op0)) if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { - if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv)) - return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); + if (MultiplyOverflows(RHS, LHSRHS, + I.getOpcode()==Instruction::SDiv, Context)) + return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); else return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), - ConstantExpr::getMul(RHS, LHSRHS)); + Context->getConstantExprMul(RHS, LHSRHS)); } if (!RHS->isZero()) { // avoid X udiv 0 @@ -2993,7 +3009,7 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |