X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FScalar%2FInstructionCombining.cpp;h=b875420e5beb521ef015ecfc51f3fa07f7db0552;hb=f1f2dc24577f4bb48563e4515324883027275736;hp=99f315fcda10bccc32a76fc4721afedfc11062ad;hpb=7e25d45cfb3572a98aac364ee74dbc4d108ebeb7;p=oota-llvm.git diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 99f315fcda1..b875420e5be 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -40,6 +40,7 @@ #include "llvm/Pass.h" #include "llvm/DerivedTypes.h" #include "llvm/GlobalVariable.h" +#include "llvm/Operator.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/Target/TargetData.h" @@ -54,6 +55,7 @@ #include "llvm/Support/MathExtras.h" #include "llvm/Support/PatternMatch.h" #include "llvm/Support/Compiler.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallPtrSet.h" @@ -84,7 +86,8 @@ namespace { static char ID; // Pass identification, replacement for typeid InstCombiner() : FunctionPass(&ID) {} - LLVMContext *getContext() { return Context; } + LLVMContext *Context; + LLVMContext *getContext() const { return Context; } /// AddToWorkList - Add the specified instruction to the worklist if it /// isn't already in it. @@ -144,7 +147,7 @@ namespace { if (Instruction *Op = dyn_cast(*i)) { AddToWorkList(Op); // Set the operand to undef to drop the use. - *i = Context->getUndef(Op->getType()); + *i = UndefValue::get(Op->getType()); } return R; @@ -156,12 +159,11 @@ namespace { bool DoOneIteration(Function &F, unsigned ItNum); virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(); AU.addPreservedID(LCSSAID); AU.setPreservesCFG(); } - TargetData &getTargetData() const { return *TD; } + TargetData *getTargetData() const { return TD; } // Visitation implementation - Implement instruction combining for different // instruction types. The semantics are as follows: @@ -188,8 +190,10 @@ namespace { Instruction *visitSDiv(BinaryOperator &I); Instruction *visitFDiv(BinaryOperator &I); Instruction *FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS); + Instruction *FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS); Instruction *visitAnd(BinaryOperator &I); Instruction *FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS); + Instruction *FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS); Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A, Value *B, Value *C); Instruction *visitOr (BinaryOperator &I); @@ -209,7 +213,7 @@ namespace { Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, ConstantInt *DivRHS); - Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, + Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, Instruction &I); Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, BinaryOperator &I); @@ -282,7 +286,7 @@ namespace { if (V->getType() == Ty) return V; if (Constant *CV = dyn_cast(V)) - return Context->getConstantExprCast(opc, CV, Ty); + return ConstantExpr::getCast(opc, CV, Ty); Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos); AddToWorkList(C); @@ -308,7 +312,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(Context->getUndef(I.getType())); + I.replaceAllUsesWith(UndefValue::get(I.getType())); return &I; } } @@ -410,8 +414,8 @@ X("instcombine", "Combine redundant instructions"); // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst static unsigned getComplexity(LLVMContext *Context, Value *V) { if (isa(V)) { - if (BinaryOperator::isNeg(*Context, V) || - BinaryOperator::isFNeg(*Context, V) || + if (BinaryOperator::isNeg(V) || + BinaryOperator::isFNeg(V) || BinaryOperator::isNot(V)) return 3; return 4; @@ -440,29 +444,12 @@ static const Type *getPromotedType(const Type *Ty) { /// expression bitcast, or a GetElementPtrInst with all zero indices, return the /// operand value, otherwise return null. static Value *getBitCastOperand(Value *V) { - if (BitCastInst *I = dyn_cast(V)) - // BitCastInst? - return I->getOperand(0); - else if (GetElementPtrInst *GEP = dyn_cast(V)) { - // GetElementPtrInst? - if (GEP->hasAllZeroIndices()) - return GEP->getOperand(0); - } else if (ConstantExpr *CE = dyn_cast(V)) { - if (CE->getOpcode() == Instruction::BitCast) - // BitCast ConstantExp? - return CE->getOperand(0); - else if (CE->getOpcode() == Instruction::GetElementPtr) { - // GetElementPtr ConstantExp? - for (User::op_iterator I = CE->op_begin() + 1, E = CE->op_end(); - I != E; ++I) { - ConstantInt *CI = dyn_cast(I); - if (!CI || !CI->isZero()) - // Any non-zero indices? Not cast-like. - return 0; - } - // All-zero indices? This is just like casting. - return CE->getOperand(0); - } + if (Operator *O = dyn_cast(V)) { + if (O->getOpcode() == Instruction::BitCast) + return O->getOperand(0); + if (GEPOperator *GEP = dyn_cast(V)) + if (GEP->hasAllZeroIndices()) + return GEP->getPointerOperand(); } return 0; } @@ -476,7 +463,7 @@ isEliminableCastPair( const Type *DstTy, ///< The target type for the second cast instruction TargetData *TD ///< The target data for pointer size ) { - + const Type *SrcTy = CI->getOperand(0)->getType(); // A from above const Type *MidTy = CI->getType(); // B from above @@ -485,7 +472,8 @@ isEliminableCastPair( Instruction::CastOps secondOp = Instruction::CastOps(opcode); unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, - DstTy, TD->getIntPtrType()); + DstTy, + TD ? TD->getIntPtrType() : 0); // We don't want to form an inttoptr or ptrtoint that converts to an integer // type that differs from the pointer size. @@ -505,7 +493,7 @@ static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, // If this is another cast that can be eliminated, it isn't codegen either. if (const CastInst *CI = dyn_cast(V)) - if (isEliminableCastPair(CI, opcode, Ty, TD)) + if (isEliminableCastPair(CI, opcode, Ty, TD)) return false; return true; } @@ -531,7 +519,7 @@ bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { if (BinaryOperator *Op = dyn_cast(I.getOperand(0))) if (Op->getOpcode() == Opcode && isa(Op->getOperand(1))) { if (isa(I.getOperand(1))) { - Constant *Folded = Context->getConstantExpr(I.getOpcode(), + Constant *Folded = ConstantExpr::get(I.getOpcode(), cast(I.getOperand(1)), cast(Op->getOperand(1))); I.setOperand(0, Op->getOperand(0)); @@ -544,7 +532,7 @@ bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { Constant *C2 = cast(Op1->getOperand(1)); // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) - Constant *Folded = Context->getConstantExpr(I.getOpcode(), C1, C2); + Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0), Op1->getOperand(0), Op1->getName(), &I); @@ -572,17 +560,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, LLVMContext *Context) { - if (BinaryOperator::isNeg(*Context, V)) +static inline Value *dyn_castNegVal(Value *V) { + 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(V)) - return Context->getConstantExprNeg(C); + return ConstantExpr::getNeg(C); if (ConstantVector *C = dyn_cast(V)) if (C->getType()->getElementType()->isInteger()) - return Context->getConstantExprNeg(C); + return ConstantExpr::getNeg(C); return 0; } @@ -591,28 +579,28 @@ static inline Value *dyn_castNegVal(Value *V, LLVMContext *Context) { // instruction if the LHS is a constant negative zero (which is the 'negate' // form). // -static inline Value *dyn_castFNegVal(Value *V, LLVMContext *Context) { - if (BinaryOperator::isFNeg(*Context, V)) +static inline Value *dyn_castFNegVal(Value *V) { + 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(V)) - return Context->getConstantExprFNeg(C); + return ConstantExpr::getFNeg(C); if (ConstantVector *C = dyn_cast(V)) if (C->getType()->getElementType()->isFloatingPoint()) - return Context->getConstantExprFNeg(C); + return ConstantExpr::getFNeg(C); 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(V)) - return Context->getConstantInt(~C->getValue()); + return ConstantInt::get(C->getType(), ~C->getValue()); return 0; } @@ -621,8 +609,7 @@ static inline Value *dyn_castNotVal(Value *V, LLVMContext *Context) { // 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(V)) { if (I->getOpcode() == Instruction::Mul) @@ -633,48 +620,27 @@ static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST, // The multiplier is really 1 << CST. uint32_t BitWidth = cast(V->getType())->getBitWidth(); uint32_t CSTVal = CST->getLimitedValue(BitWidth); - CST = Context->getConstantInt(APInt(BitWidth, 1).shl(CSTVal)); + CST = ConstantInt::get(V->getType()->getContext(), + APInt(BitWidth, 1).shl(CSTVal)); return I->getOperand(0); } } return 0; } -/// dyn_castGetElementPtr - If this is a getelementptr instruction or constant -/// expression, return it. -static User *dyn_castGetElementPtr(Value *V) { - if (isa(V)) return cast(V); - if (ConstantExpr *CE = dyn_cast(V)) - if (CE->getOpcode() == Instruction::GetElementPtr) - return cast(V); - return false; -} - -/// getOpcode - If this is an Instruction or a ConstantExpr, return the -/// opcode value. Otherwise return UserOp1. -static unsigned getOpcode(const Value *V) { - if (const Instruction *I = dyn_cast(V)) - return I->getOpcode(); - if (const ConstantExpr *CE = dyn_cast(V)) - return CE->getOpcode(); - // Use UserOp1 to mean there's no opcode. - return Instruction::UserOp1; -} - /// AddOne - Add one to a ConstantInt -static Constant *AddOne(Constant *C, LLVMContext *Context) { - return Context->getConstantExprAdd(C, - Context->getConstantInt(C->getType(), 1)); +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) { - return Context->getConstantExprSub(C, - Context->getConstantInt(C->getType(), 1)); +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) { @@ -701,7 +667,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, LLVMContext *Context) { + APInt Demanded) { assert(I && "No instruction?"); assert(OpNo < I->getNumOperands() && "Operand index too large"); @@ -716,7 +682,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, Context->getConstantInt(Demanded)); + I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded)); return true; } @@ -848,7 +814,7 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, if (DemandedMask == 0) { // Not demanding any bits from V. if (isa(V)) return 0; - return Context->getUndef(VTy); + return UndefValue::get(VTy); } if (Depth == 6) // Limit search depth. @@ -890,7 +856,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 Context->getNullValue(VTy); + return Constant::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 @@ -959,10 +925,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 Context->getNullValue(VTy); + 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. @@ -999,7 +965,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, Context)) + if (ShrinkDemandedConstant(I, 1, DemandedMask)) return I; // Output known-0 bits are only known if clear in both the LHS & RHS. @@ -1047,7 +1013,8 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { // all known if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { - Constant *AndC = Context->getConstantInt(~RHSKnownOne & DemandedMask); + Constant *AndC = Constant::getIntegerValue(VTy, + ~RHSKnownOne & DemandedMask); Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp"); return InsertNewInstBefore(And, *I); @@ -1056,7 +1023,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, Context)) + if (ShrinkDemandedConstant(I, 1, DemandedMask)) return I; RHSKnownZero = KnownZeroOut; @@ -1073,8 +1040,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, 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. @@ -1198,7 +1165,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, Context)) + if (ShrinkDemandedConstant(I, 1, InDemandedBits)) return I; // Avoid excess work. @@ -1419,10 +1386,10 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, Instruction *NewVal; if (InputBit > ResultBit) NewVal = BinaryOperator::CreateLShr(I->getOperand(1), - Context->getConstantInt(I->getType(), InputBit-ResultBit)); + ConstantInt::get(I->getType(), InputBit-ResultBit)); else NewVal = BinaryOperator::CreateShl(I->getOperand(1), - Context->getConstantInt(I->getType(), ResultBit-InputBit)); + ConstantInt::get(I->getType(), ResultBit-InputBit)); NewVal->takeName(I); return InsertNewInstBefore(NewVal, *I); } @@ -1438,12 +1405,8 @@ 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 = Context->getConstantInt(RHSKnownOne); - if (isa(V->getType())) - C = Context->getConstantExprIntToPtr(C, V->getType()); - return C; - } + if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) + return Constant::getIntegerValue(VTy, RHSKnownOne); return false; } @@ -1469,13 +1432,13 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, return 0; } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. UndefElts = EltMask; - return Context->getUndef(V->getType()); + return UndefValue::get(V->getType()); } UndefElts = 0; if (ConstantVector *CP = dyn_cast(V)) { const Type *EltTy = cast(V->getType())->getElementType(); - Constant *Undef = Context->getUndef(EltTy); + Constant *Undef = UndefValue::get(EltTy); std::vector Elts; for (unsigned i = 0; i != VWidth; ++i) @@ -1490,7 +1453,7 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, } // If we changed the constant, return it. - Constant *NewCP = Context->getConstantVector(Elts); + Constant *NewCP = ConstantVector::get(Elts); return NewCP != CP ? NewCP : 0; } else if (isa(V)) { // Simplify the CAZ to a ConstantVector where the non-demanded elements are @@ -1502,15 +1465,15 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, return 0; const Type *EltTy = cast(V->getType())->getElementType(); - Constant *Zero = Context->getNullValue(EltTy); - Constant *Undef = Context->getUndef(EltTy); + Constant *Zero = Constant::getNullValue(EltTy); + Constant *Undef = UndefValue::get(EltTy); std::vector Elts; for (unsigned i = 0; i != VWidth; ++i) { Constant *Elt = DemandedElts[i] ? Zero : Undef; Elts.push_back(Elt); } UndefElts = DemandedElts ^ EltMask; - return Context->getConstantVector(Elts); + return ConstantVector::get(Elts); } // Limit search depth. @@ -1624,12 +1587,12 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, std::vector Elts; for (unsigned i = 0; i < VWidth; ++i) { if (UndefElts[i]) - Elts.push_back(Context->getUndef(Type::Int32Ty)); + Elts.push_back(UndefValue::get(Type::Int32Ty)); else - Elts.push_back(Context->getConstantInt(Type::Int32Ty, + Elts.push_back(ConstantInt::get(Type::Int32Ty, Shuffle->getMaskValue(i))); } - I->setOperand(2, Context->getConstantVector(Elts)); + I->setOperand(2, ConstantVector::get(Elts)); MadeChange = true; } break; @@ -1682,7 +1645,7 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, UndefElts = UndefElts2; if (VWidth > InVWidth) { - LLVM_UNREACHABLE("Unimp"); + llvm_unreachable("Unimp"); // If there are more elements in the result than there are in the source, // then an output element is undef if the corresponding input element is // undef. @@ -1690,7 +1653,7 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, if (UndefElts2[OutIdx/Ratio]) UndefElts.set(OutIdx); } else if (VWidth < InVWidth) { - LLVM_UNREACHABLE("Unimp"); + llvm_unreachable("Unimp"); // If there are more elements in the source than there are in the result, // then a result element is undef if all of the corresponding input // elements are undef. @@ -1756,11 +1719,13 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, Value *LHS = II->getOperand(1); Value *RHS = II->getOperand(2); // Extract the element as scalars. - LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); - RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); + LHS = InsertNewInstBefore(ExtractElementInst::Create(LHS, + ConstantInt::get(Type::Int32Ty, 0U, false), "tmp"), *II); + RHS = InsertNewInstBefore(ExtractElementInst::Create(RHS, + ConstantInt::get(Type::Int32Ty, 0U, false), "tmp"), *II); switch (II->getIntrinsicID()) { - default: LLVM_UNREACHABLE("Case stmts out of sync!"); + default: llvm_unreachable("Case stmts out of sync!"); case Intrinsic::x86_sse_sub_ss: case Intrinsic::x86_sse2_sub_sd: TmpV = InsertNewInstBefore(BinaryOperator::CreateFSub(LHS, RHS, @@ -1775,7 +1740,8 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, Instruction *New = InsertElementInst::Create( - Context->getUndef(II->getType()), TmpV, 0U, II->getName()); + UndefValue::get(II->getType()), TmpV, + ConstantInt::get(Type::Int32Ty, 0U, false), II->getName()); InsertNewInstBefore(New, *II); AddSoonDeadInstToWorklist(*II, 0); return New; @@ -1803,8 +1769,7 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, /// 'shouldApply' and 'apply' methods. /// template -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); @@ -1837,7 +1802,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(Context->getNullValue(TmpLHSI->getType())); + Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); return 0; } Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI @@ -1876,12 +1841,11 @@ namespace { // 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), - Context->getConstantInt(Add.getType(), 1)); + ConstantInt::get(Add.getType(), 1)); } }; @@ -1889,12 +1853,11 @@ struct AddRHS { // 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) && - Context->getConstantExprAnd(C1, C2)->isNullValue(); + return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && + ConstantExpr::getAnd(C1, C2)->isNullValue(); } Instruction *apply(BinaryOperator &Add) const { return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1)); @@ -1917,8 +1880,8 @@ static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, if (Constant *SOC = dyn_cast(SO)) { if (ConstIsRHS) - return Context->getConstantExpr(I.getOpcode(), SOC, ConstOperand); - return Context->getConstantExpr(I.getOpcode(), ConstOperand, SOC); + return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); + return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); } Value *Op0 = SO, *Op1 = ConstOperand; @@ -1931,7 +1894,7 @@ static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, New = CmpInst::Create(*Context, CI->getOpcode(), CI->getPredicate(), Op0, Op1, SO->getName()+".cmp"); else { - LLVM_UNREACHABLE("Unknown binary instruction type!"); + llvm_unreachable("Unknown binary instruction type!"); } return IC->InsertNewInstBefore(New, I); } @@ -2007,9 +1970,9 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { Value *InV = 0; if (Constant *InC = dyn_cast(PN->getIncomingValue(i))) { if (CmpInst *CI = dyn_cast(&I)) - InV = Context->getConstantExprCompare(CI->getPredicate(), InC, C); + InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); else - InV = Context->getConstantExpr(I.getOpcode(), InC, C); + InV = ConstantExpr::get(I.getOpcode(), InC, C); } else { assert(PN->getIncomingBlock(i) == NonConstBB); if (BinaryOperator *BO = dyn_cast(&I)) @@ -2022,7 +1985,7 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { PN->getIncomingValue(i), C, "phitmp", NonConstBB->getTerminator()); else - LLVM_UNREACHABLE("Unknown binop!"); + llvm_unreachable("Unknown binop!"); AddToWorkList(cast(InV)); } @@ -2034,7 +1997,7 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { for (unsigned i = 0; i != NumPHIValues; ++i) { Value *InV; if (Constant *InC = dyn_cast(PN->getIncomingValue(i))) { - InV = Context->getConstantExprCast(CI->getOpcode(), InC, RetTy); + InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); } else { assert(PN->getIncomingBlock(i) == NonConstBB); InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), @@ -2101,13 +2064,10 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { if (SimplifyDemandedInstructionBits(I)) return &I; - // zext(i1) - 1 -> select i1, 0, -1 + // zext(bool) + C -> bool ? C + 1 : C if (ZExtInst *ZI = dyn_cast(LHS)) - if (CI->isAllOnesValue() && - ZI->getOperand(0)->getType() == Type::Int1Ty) - return SelectInst::Create(ZI->getOperand(0), - Context->getNullValue(I.getType()), - Context->getConstantIntAllOnesValue(I.getType())); + if (ZI->getSrcTy() == Type::Int1Ty) + return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI); } if (isa(LHS)) @@ -2117,7 +2077,7 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { ConstantInt *XorRHS = 0; Value *XorLHS = 0; if (isa(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(RHSC)->getValue(); @@ -2166,7 +2126,7 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // 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(RHS)) { @@ -2183,12 +2143,12 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // -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); } } @@ -2197,34 +2157,34 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // A + -B --> A - B if (!isa(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)) - return BinaryOperator::CreateMul(X, Context->getConstantExprAdd(C1, C2)); + 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) - return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); + 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. @@ -2247,8 +2207,8 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // 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); @@ -2270,13 +2230,13 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { if (ConstantInt *CRHS = dyn_cast(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)) { - Constant *Anded = Context->getConstantExprAnd(CRHS, C2); + 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 // in the mask. First, get the rightmost bit. @@ -2303,28 +2263,6 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { return R; } - // add (cast *A to intptrtype) B -> - // cast (GEP (cast *A to i8*) B) --> intptrtype - { - CastInst *CI = dyn_cast(LHS); - Value *Other = RHS; - if (!CI) { - CI = dyn_cast(RHS); - Other = LHS; - } - if (CI && CI->getType()->isSized() && - (CI->getType()->getScalarSizeInBits() == - TD->getIntPtrType()->getPrimitiveSizeInBits()) - && isa(CI->getOperand(0)->getType())) { - unsigned AS = - cast(CI->getOperand(0)->getType())->getAddressSpace(); - Value *I2 = InsertBitCastBefore(CI->getOperand(0), - Context->getPointerType(Type::Int8Ty, AS), I); - I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I); - return new PtrToIntInst(I2, CI->getType()); - } - } - // add (select X 0 (sub n A)) A --> select X A n { SelectInst *SI = dyn_cast(LHS); @@ -2340,12 +2278,12 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // 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); } @@ -2357,9 +2295,9 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // (add (sext x), cst) --> (sext (add x, cst')) if (ConstantInt *RHSC = dyn_cast(RHS)) { Constant *CI = - Context->getConstantExprTrunc(RHSC, LHSConv->getOperand(0)->getType()); + ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); if (LHSConv->hasOneUse() && - Context->getConstantExprSExt(CI, I.getType()) == RHSC && + ConstantExpr::getSExt(CI, I.getType()) == RHSC && WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { // Insert the new, smaller add. Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), @@ -2398,7 +2336,7 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { if (Constant *RHSC = dyn_cast(RHS)) { // X + 0 --> X if (ConstantFP *CFP = dyn_cast(RHSC)) { - if (CFP->isExactlyValue(Context->getConstantFPNegativeZero + if (CFP->isExactlyValue(ConstantFP::getNegativeZero (I.getType())->getValueAPF())) return ReplaceInstUsesWith(I, LHS); } @@ -2410,12 +2348,12 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { // -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(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. @@ -2433,9 +2371,9 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { // instcombined. if (ConstantFP *CFP = dyn_cast(RHS)) { Constant *CI = - Context->getConstantExprFPToSI(CFP, LHSConv->getOperand(0)->getType()); + ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); if (LHSConv->hasOneUse() && - Context->getConstantExprSIToFP(CI, I.getType()) == CFP && + ConstantExpr::getSIToFP(CI, I.getType()) == CFP && WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { // Insert the new integer add. Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), @@ -2471,10 +2409,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, Context->getNullValue(I.getType())); + 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(Op0)) @@ -2489,8 +2427,8 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { // 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) @@ -2525,6 +2463,11 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { if (SelectInst *SI = dyn_cast(Op1)) if (Instruction *R = FoldOpIntoSelect(I, SI, this)) return R; + + // C - zext(bool) -> bool ? C - 1 : C + if (ZExtInst *ZI = dyn_cast(Op1)) + if (ZI->getSrcTy() == Type::Int1Ty) + return SelectInst::Create(ZI->getOperand(0), SubOne(C), C); } if (I.getType() == Type::Int1Ty) @@ -2533,16 +2476,16 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { if (BinaryOperator *Op1I = dyn_cast(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(I.getOperand(0))) { if (ConstantInt *CI2 = dyn_cast(Op1I->getOperand(1))) // C1-(X+C2) --> (C1-C2)-X return BinaryOperator::CreateSub( - Context->getConstantExprSub(CI1, CI2), Op1I->getOperand(0)); + ConstantExpr::getSub(CI1, CI2), Op1I->getOperand(0)); } } @@ -2577,13 +2520,13 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { if (CSI->isZero()) if (Constant *DivRHS = dyn_cast(Op1I->getOperand(1))) return BinaryOperator::CreateSDiv(Op1I->getOperand(0), - Context->getConstantExprNeg(DivRHS)); + ConstantExpr::getNeg(DivRHS)); // X - X*C --> X * (1-C) ConstantInt *C2 = 0; - if (dyn_castFoldableMul(Op1I, C2, Context) == Op0) { + if (dyn_castFoldableMul(Op1I, C2) == Op0) { Constant *CP1 = - Context->getConstantExprSub(Context->getConstantInt(I.getType(), 1), + ConstantExpr::getSub(ConstantInt::get(I.getType(), 1), C2); return BinaryOperator::CreateMul(Op0, CP1); } @@ -2598,19 +2541,19 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { 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)) - return BinaryOperator::CreateMul(X, Context->getConstantExprSub(C1, C2)); + if (X == dyn_castFoldableMul(Op1, C2)) + return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2)); } return 0; } @@ -2619,16 +2562,16 @@ 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, Context)) + if (Value *V = dyn_castFNegVal(Op1)) return BinaryOperator::CreateFAdd(Op0, V); if (BinaryOperator *Op1I = dyn_cast(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()); } } @@ -2670,10 +2613,8 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { bool Changed = SimplifyCommutative(I); Value *Op0 = I.getOperand(0); - // TODO: If Op1 is undef and Op0 is finite, return zero. - if (!I.getType()->isFPOrFPVector() && - isa(I.getOperand(1))) // undef * X -> 0 - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + if (isa(I.getOperand(1))) // undef * X -> 0 + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); // Simplify mul instructions with a constant RHS... if (Constant *Op1 = dyn_cast(I.getOperand(1))) { @@ -2684,26 +2625,27 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { if (SI->getOpcode() == Instruction::Shl) if (Constant *ShOp = dyn_cast(SI->getOperand(1))) return BinaryOperator::CreateMul(SI->getOperand(0), - Context->getConstantExprShl(CI, ShOp)); + ConstantExpr::getShl(CI, ShOp)); if (CI->isZero()) return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 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(CI)->getValue(); if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C return BinaryOperator::CreateShl(Op0, - Context->getConstantInt(Op0->getType(), Val.logBase2())); + ConstantInt::get(Op0->getType(), Val.logBase2())); } } else if (isa(Op1->getType())) { - // TODO: If Op1 is all zeros and Op0 is all finite, return all zeros. + if (Op1->isNullValue()) + return ReplaceInstUsesWith(I, Op1); if (ConstantVector *Op1V = dyn_cast(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()) { @@ -2721,7 +2663,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0), Op1, "tmp"); InsertNewInstBefore(Add, I); - Value *C1C2 = Context->getConstantExprMul(Op1, + Value *C1C2 = ConstantExpr::getMul(Op1, cast(Op0I->getOperand(1))); return BinaryOperator::CreateAdd(Add, C1C2); @@ -2737,8 +2679,8 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { 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) @@ -2752,13 +2694,22 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { Op1 = Op0; BO = dyn_cast(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(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); @@ -2802,7 +2753,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { isSignBitCheck(SCI->getPredicate(), cast(SCIOp1), TIS) && TIS) { // Shift the X value right to turn it into "all signbits". - Constant *Amt = Context->getConstantInt(SCIOp0->getType(), + Constant *Amt = ConstantInt::get(SCIOp0->getType(), SCOpTy->getPrimitiveSizeInBits()-1); Value *V = InsertNewInstBefore( @@ -2862,8 +2813,8 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) { 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; @@ -2920,8 +2871,8 @@ bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { *I = SI->getOperand(NonNullOperand); AddToWorkList(BBI); } else if (*I == SelectCond) { - *I = NonNullOperand == 1 ? Context->getConstantIntTrue() : - Context->getConstantIntFalse(); + *I = NonNullOperand == 1 ? ConstantInt::getTrue(*Context) : + ConstantInt::getFalse(*Context); AddToWorkList(BBI); } } @@ -2953,7 +2904,7 @@ Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { if (isa(Op0)) { if (Op0->getType()->isFPOrFPVector()) return ReplaceInstUsesWith(I, Op0); - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); } // X / undef -> undef @@ -2973,12 +2924,12 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { // (sdiv X, X) --> 1 (udiv X, X) --> 1 if (Op0 == Op1) { if (const VectorType *Ty = dyn_cast(I.getType())) { - Constant *CI = Context->getConstantInt(Ty->getElementType(), 1); + Constant *CI = ConstantInt::get(Ty->getElementType(), 1); std::vector Elts(Ty->getNumElements(), CI); - return ReplaceInstUsesWith(I, Context->getConstantVector(Elts)); + return ReplaceInstUsesWith(I, ConstantVector::get(Elts)); } - Constant *CI = Context->getConstantInt(I.getType(), 1); + Constant *CI = ConstantInt::get(I.getType(), 1); return ReplaceInstUsesWith(I, CI); } @@ -3000,11 +2951,11 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) if (ConstantInt *LHSRHS = dyn_cast(LHS->getOperand(1))) { if (MultiplyOverflows(RHS, LHSRHS, - I.getOpcode()==Instruction::SDiv, Context)) - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + I.getOpcode()==Instruction::SDiv)) + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); else return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), - Context->getConstantExprMul(RHS, LHSRHS)); + ConstantExpr::getMul(RHS, LHSRHS)); } if (!RHS->isZero()) { // avoid X udiv 0 @@ -3020,7 +2971,7 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { // 0 / X == 0, we don't need to preserve faults! if (ConstantInt *LHS = dyn_cast(Op0)) if (LHS->equalsInt(0)) - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + 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) @@ -3049,15 +3000,15 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { // if so, convert to a right shift. if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 return BinaryOperator::CreateLShr(Op0, - Context->getConstantInt(Op0->getType(), C->getValue().logBase2())); + ConstantInt::get(Op0->getType(), C->getValue().logBase2())); // X udiv C, where C >= signbit if (C->getValue().isNegative()) { Value *IC = InsertNewInstBefore(new ICmpInst(*Context, ICmpInst::ICMP_ULT, Op0, C), I); - return SelectInst::Create(IC, Context->getNullValue(I.getType()), - Context->getConstantInt(I.getType(), 1)); + return SelectInst::Create(IC, Constant::getNullValue(I.getType()), + ConstantInt::get(I.getType(), 1)); } } @@ -3070,7 +3021,7 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { Value *N = RHSI->getOperand(1); const Type *NTy = N->getType(); if (uint32_t C2 = C1.logBase2()) { - Constant *C2V = Context->getConstantInt(NTy, C2); + Constant *C2V = ConstantInt::get(NTy, C2); N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I); } return BinaryOperator::CreateLShr(Op0, N); @@ -3088,13 +3039,13 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { // Compute the shift amounts uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); // Construct the "on true" case of the select - Constant *TC = Context->getConstantInt(Op0->getType(), TSA); + Constant *TC = ConstantInt::get(Op0->getType(), TSA); Instruction *TSI = BinaryOperator::CreateLShr( Op0, TC, SI->getName()+".t"); TSI = InsertNewInstBefore(TSI, I); // Construct the "on false" case of the select - Constant *FC = Context->getConstantInt(Op0->getType(), FSA); + Constant *FC = ConstantInt::get(Op0->getType(), FSA); Instruction *FSI = BinaryOperator::CreateLShr( Op0, FC, SI->getName()+".f"); FSI = InsertNewInstBefore(FSI, I); @@ -3116,18 +3067,46 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { if (ConstantInt *RHS = dyn_cast(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(&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(Op0)) + if (isa(Sub->getOperand(0)) && + cast(Sub->getOperand(0))->isNullValue() && + Sub->hasNoSignedOverflow()) + return BinaryOperator::CreateSDiv(Sub->getOperand(1), + ConstantExpr::getNeg(RHS)); } // If the sign bits of both operands are zero (i.e. we can prove they are // unsigned inputs), turn this into a udiv. if (I.getType()->isInteger()) { APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); - if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { - // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set - return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); + if (MaskedValueIsZero(Op0, Mask)) { + if (MaskedValueIsZero(Op1, Mask)) { + // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set + return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); + } + ConstantInt *ShiftedInt; + 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 + // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have + // the sign bit set. + return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); + } } - } + } return 0; } @@ -3146,7 +3125,7 @@ Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { if (isa(Op0)) { // undef % X -> 0 if (I.getType()->isFPOrFPVector()) return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); } if (isa(Op1)) return ReplaceInstUsesWith(I, Op1); // X % undef -> undef @@ -3171,15 +3150,15 @@ Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { // 0 % X == 0 for integer, we don't need to preserve faults! if (Constant *LHS = dyn_cast(Op0)) if (LHS->isNullValue()) - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); if (ConstantInt *RHS = dyn_cast(Op1)) { // X % 0 == undef, we don't need to preserve faults! if (RHS->equalsInt(0)) - return ReplaceInstUsesWith(I, Context->getUndef(I.getType())); + return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); if (RHS->equalsInt(1)) // X % 1 == 0 - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); if (Instruction *Op0I = dyn_cast(Op0)) { if (SelectInst *SI = dyn_cast(Op0I)) { @@ -3211,7 +3190,7 @@ Instruction *InstCombiner::visitURem(BinaryOperator &I) { // if so, convert to a bitwise and. if (ConstantInt *C = dyn_cast(RHS)) if (C->getValue().isPowerOf2()) - return BinaryOperator::CreateAnd(Op0, SubOne(C, Context)); + return BinaryOperator::CreateAnd(Op0, SubOne(C)); } if (Instruction *RHSI = dyn_cast(I.getOperand(1))) { @@ -3219,7 +3198,7 @@ Instruction *InstCombiner::visitURem(BinaryOperator &I) { if (RHSI->getOpcode() == Instruction::Shl && isa(RHSI->getOperand(0))) { if (cast(RHSI->getOperand(0))->getValue().isPowerOf2()) { - Constant *N1 = Context->getConstantIntAllOnesValue(I.getType()); + Constant *N1 = Constant::getAllOnesValue(I.getType()); Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1, "tmp"), I); return BinaryOperator::CreateAnd(Op0, Add); @@ -3236,10 +3215,10 @@ Instruction *InstCombiner::visitURem(BinaryOperator &I) { 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); } @@ -3256,7 +3235,7 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) { if (Instruction *common = commonIRemTransforms(I)) return common; - if (Value *RHSNeg = dyn_castNegVal(Op1, Context)) + if (Value *RHSNeg = dyn_castNegVal(Op1)) if (!isa(RHSNeg) || (isa(RHSNeg) && cast(RHSNeg)->getValue().isStrictlyPositive())) { @@ -3291,13 +3270,13 @@ Instruction *InstCombiner::visitSRem(BinaryOperator &I) { for (unsigned i = 0; i != VWidth; ++i) { if (ConstantInt *RHS = dyn_cast(RHSV->getOperand(i))) { if (RHS->getValue().isNegative()) - Elts[i] = cast(Context->getConstantExprNeg(RHS)); + Elts[i] = cast(ConstantExpr::getNeg(RHS)); else Elts[i] = RHS; } } - Constant *NewRHSV = Context->getConstantVector(Elts); + Constant *NewRHSV = ConstantVector::get(Elts); if (NewRHSV != RHSV) { AddUsesToWorkList(I); I.setOperand(1, NewRHSV); @@ -3363,7 +3342,7 @@ static unsigned getICmpCode(const ICmpInst *ICI) { case ICmpInst::ICMP_SLE: return 6; // 110 // True -> 7 default: - LLVM_UNREACHABLE("Invalid ICmp predicate!"); + llvm_unreachable("Invalid ICmp predicate!"); return 0; } } @@ -3391,7 +3370,7 @@ static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) { // True -> 7 default: // Not expecting FCMP_FALSE and FCMP_TRUE; - LLVM_UNREACHABLE("Unexpected FCmp predicate!"); + llvm_unreachable("Unexpected FCmp predicate!"); return 0; } } @@ -3403,8 +3382,8 @@ static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) { static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS, LLVMContext *Context) { switch (code) { - default: LLVM_UNREACHABLE("Illegal ICmp code!"); - case 0: return Context->getConstantIntFalse(); + default: llvm_unreachable("Illegal ICmp code!"); + case 0: return ConstantInt::getFalse(*Context); case 1: if (sign) return new ICmpInst(*Context, ICmpInst::ICMP_SGT, LHS, RHS); @@ -3427,7 +3406,7 @@ static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS, return new ICmpInst(*Context, ICmpInst::ICMP_SLE, LHS, RHS); else return new ICmpInst(*Context, ICmpInst::ICMP_ULE, LHS, RHS); - case 7: return Context->getConstantIntTrue(); + case 7: return ConstantInt::getTrue(*Context); } } @@ -3437,7 +3416,7 @@ static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS, static Value *getFCmpValue(bool isordered, unsigned code, Value *LHS, Value *RHS, LLVMContext *Context) { switch (code) { - default: LLVM_UNREACHABLE("Illegal FCmp code!"); + default: llvm_unreachable("Illegal FCmp code!"); case 0: if (isordered) return new FCmpInst(*Context, FCmpInst::FCMP_ORD, LHS, RHS); @@ -3473,7 +3452,7 @@ static Value *getFCmpValue(bool isordered, unsigned code, return new FCmpInst(*Context, FCmpInst::FCMP_OLE, LHS, RHS); else return new FCmpInst(*Context, FCmpInst::FCMP_ULE, LHS, RHS); - case 7: return Context->getConstantIntTrue(); + case 7: return ConstantInt::getTrue(*Context); } } @@ -3516,7 +3495,7 @@ struct FoldICmpLogical { case Instruction::And: Code = LHSCode & RHSCode; break; case Instruction::Or: Code = LHSCode | RHSCode; break; case Instruction::Xor: Code = LHSCode ^ RHSCode; break; - default: LLVM_UNREACHABLE("Illegal logical opcode!"); return 0; + default: llvm_unreachable("Illegal logical opcode!"); return 0; } bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || @@ -3541,7 +3520,7 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op, Value *X = Op->getOperand(0); Constant *Together = 0; if (!Op->isShift()) - Together = Context->getConstantExprAnd(AndRHS, OpRHS); + Together = ConstantExpr::getAnd(AndRHS, OpRHS); switch (Op->getOpcode()) { case Instruction::Xor: @@ -3607,7 +3586,7 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op, uint32_t BitWidth = AndRHS->getType()->getBitWidth(); uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); - ConstantInt *CI = Context->getConstantInt(AndRHS->getValue() & ShlMask); + ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShlMask); if (CI->getValue() == ShlMask) { // Masking out bits that the shift already masks @@ -3627,7 +3606,7 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op, uint32_t BitWidth = AndRHS->getType()->getBitWidth(); uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); - ConstantInt *CI = Context->getConstantInt(AndRHS->getValue() & ShrMask); + ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask); if (CI->getValue() == ShrMask) { // Masking out bits that the shift already masks. @@ -3646,7 +3625,7 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op, uint32_t BitWidth = AndRHS->getType()->getBitWidth(); uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); - Constant *C = Context->getConstantInt(AndRHS->getValue() & ShrMask); + Constant *C = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask); if (C == AndRHS) { // Masking out bits shifted in. // (Val ashr C1) & C2 -> (Val lshr C1) & C2 // Make the argument unsigned. @@ -3671,7 +3650,7 @@ Instruction *InstCombiner::OptAndOp(Instruction *Op, Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned, bool Inside, Instruction &IB) { - assert(cast(Context->getConstantExprICmp((isSigned ? + assert(cast(ConstantExpr::getICmp((isSigned ? ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && "Lo is not <= Hi in range emission code!"); @@ -3687,10 +3666,10 @@ Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, } // Emit V-Lo getConstantExprNeg(Lo); + Constant *NegLo = ConstantExpr::getNeg(Lo); Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); InsertNewInstBefore(Add, IB); - Constant *UpperBound = Context->getConstantExprAdd(NegLo, Hi); + Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, UpperBound); } @@ -3698,7 +3677,7 @@ Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, return new ICmpInst(*Context, ICmpInst::ICMP_EQ, V, V); // V < Min || V >= Hi -> V > Hi-1 - Hi = SubOne(cast(Hi), Context); + Hi = SubOne(cast(Hi)); if (cast(Lo)->isMinValue(isSigned)) { ICmpInst::Predicate pred = (isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); @@ -3707,10 +3686,10 @@ Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, // Emit V-Lo >u Hi-1-Lo // Note that Hi has already had one subtracted from it, above. - ConstantInt *NegLo = cast(Context->getConstantExprNeg(Lo)); + ConstantInt *NegLo = cast(ConstantExpr::getNeg(Lo)); Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); InsertNewInstBefore(Add, IB); - Constant *LowerBound = Context->getConstantExprAdd(NegLo, Hi); + Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add, LowerBound); } @@ -3752,7 +3731,7 @@ Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, switch (LHSI->getOpcode()) { default: return 0; case Instruction::And: - if (Context->getConstantExprAnd(N, Mask) == Mask) { + if (ConstantExpr::getAnd(N, Mask) == Mask) { // If the AndRHS is a power of two minus one (0+1+), this is simple. if ((Mask->getValue().countLeadingZeros() + Mask->getValue().countPopulation()) == @@ -3776,7 +3755,7 @@ Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 if ((Mask->getValue().countLeadingZeros() + Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() - && Context->getConstantExprAnd(N, Mask)->isNullValue()) + && ConstantExpr::getAnd(N, Mask)->isNullValue()) break; return 0; } @@ -3798,9 +3777,9 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, // 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) @@ -3851,14 +3830,14 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, assert(LHSCst != RHSCst && "Compares not folded above?"); switch (LHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 @@ -3866,13 +3845,13 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, } case ICmpInst::ICMP_NE: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_ULT: - if (LHSCst == SubOne(RHSCst, Context)) // (X != 13 & X u< 14) -> X < 13 + if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Val, LHSCst); break; // (X != 13 & X u< 15) -> no change case ICmpInst::ICMP_SLT: - if (LHSCst == SubOne(RHSCst, Context)) // (X != 13 & X s< 14) -> X < 13 + if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Val, LHSCst); break; // (X != 13 & X s< 15) -> no change case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 @@ -3880,23 +3859,23 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, 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 - Constant *AddCST = Context->getConstantExprNeg(LHSCst); + 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, - Context->getConstantInt(Add->getType(), 1)); + ConstantInt::get(Add->getType(), 1)); } break; // (X != 13 & X != 15) -> no change } break; case ICmpInst::ICMP_ULT: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change break; case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 @@ -3908,10 +3887,10 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, break; case ICmpInst::ICMP_SLT: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change break; case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 @@ -3923,18 +3902,18 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, break; case ICmpInst::ICMP_UGT: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15 case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 return ReplaceInstUsesWith(I, RHS); case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change break; case ICmpInst::ICMP_NE: - if (RHSCst == AddOne(LHSCst, Context)) // (X u> 13 & X != 14) -> X u> 14 + if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 return new ICmpInst(*Context, LHSCC, Val, RHSCst); break; // (X u> 13 & X != 15) -> no change case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) 13 & X s< 15) -> no change break; @@ -3942,18 +3921,18 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, break; case ICmpInst::ICMP_SGT: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 return ReplaceInstUsesWith(I, RHS); case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change break; case ICmpInst::ICMP_NE: - if (RHSCst == AddOne(LHSCst, Context)) // (X s> 13 & X != 14) -> X s> 14 + if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 return new ICmpInst(*Context, LHSCC, Val, RHSCst); break; // (X s> 13 & X != 15) -> no change case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1 - return InsertRangeTest(Val, AddOne(LHSCst, Context), + return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true, I); case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change break; @@ -3964,13 +3943,89 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, return 0; } +Instruction *InstCombiner::FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, + FCmpInst *RHS) { + + if (LHS->getPredicate() == FCmpInst::FCMP_ORD && + RHS->getPredicate() == FCmpInst::FCMP_ORD) { + // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) + if (ConstantFP *LHSC = dyn_cast(LHS->getOperand(1))) + if (ConstantFP *RHSC = dyn_cast(RHS->getOperand(1))) { + // If either of the constants are nans, then the whole thing returns + // false. + if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); + return new FCmpInst(*Context, FCmpInst::FCMP_ORD, + LHS->getOperand(0), RHS->getOperand(0)); + } + + // Handle vector zeros. This occurs because the canonical form of + // "fcmp ord x,x" is "fcmp ord x, 0". + if (isa(LHS->getOperand(1)) && + isa(RHS->getOperand(1))) + return new FCmpInst(*Context, FCmpInst::FCMP_ORD, + LHS->getOperand(0), RHS->getOperand(0)); + return 0; + } + + Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); + Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); + FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); + + + if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { + // Swap RHS operands to match LHS. + Op1CC = FCmpInst::getSwappedPredicate(Op1CC); + std::swap(Op1LHS, Op1RHS); + } + + 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); + + if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE) + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); + if (Op0CC == FCmpInst::FCMP_TRUE) + return ReplaceInstUsesWith(I, RHS); + if (Op1CC == FCmpInst::FCMP_TRUE) + return ReplaceInstUsesWith(I, LHS); + + bool Op0Ordered; + bool Op1Ordered; + unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); + unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); + if (Op1Pred == 0) { + std::swap(LHS, RHS); + std::swap(Op0Pred, Op1Pred); + std::swap(Op0Ordered, Op1Ordered); + } + if (Op0Pred == 0) { + // uno && ueq -> uno && (uno || eq) -> ueq + // ord && olt -> ord && (ord && lt) -> olt + if (Op0Ordered == Op1Ordered) + return ReplaceInstUsesWith(I, RHS); + + // uno && oeq -> uno && (ord && eq) -> false + // uno && ord -> false + if (!Op0Ordered) + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); + // ord && ueq -> ord && (uno || eq) -> oeq + return cast(getFCmpValue(true, Op1Pred, + Op0LHS, Op0RHS, Context)); + } + } + + return 0; +} + Instruction *InstCombiner::visitAnd(BinaryOperator &I) { bool Changed = SimplifyCommutative(I); Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); if (isa(Op1)) // X & undef -> 0 - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); // and X, X = X if (Op0 == Op1) @@ -4050,7 +4105,7 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { ConstantInt *A = dyn_cast(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); } @@ -4063,7 +4118,7 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { // (1 >> x) & 1 --> zext(x == 0) if (AndRHSMask == 1 && Op0LHS == AndRHS) { Instruction *NewICmp = new ICmpInst(*Context, ICmpInst::ICMP_EQ, - Op0RHS, Context->getNullValue(I.getType())); + Op0RHS, Constant::getNullValue(I.getType())); InsertNewInstBefore(NewICmp, I); return new ZExtInst(NewICmp, I.getType()); } @@ -4092,15 +4147,15 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { NewCast = InsertNewInstBefore(NewCast, I); // trunc_or_bitcast(C1)&C2 Constant *C3 = - Context->getConstantExprTruncOrBitCast(AndCI,I.getType()); - C3 = Context->getConstantExprAnd(C3, AndRHS); + ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); + C3 = ConstantExpr::getAnd(C3, AndRHS); return BinaryOperator::CreateAnd(NewCast, C3); } else if (CastOp->getOpcode() == Instruction::Or) { // Change: and (cast (or X, C1) to T), C2 // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 Constant *C3 = - Context->getConstantExprTruncOrBitCast(AndCI,I.getType()); - if (Context->getConstantExprAnd(C3, AndRHS) == AndRHS) + ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); + if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS) // trunc(C1)&C2 return ReplaceInstUsesWith(I, AndRHS); } @@ -4117,11 +4172,11 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { 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, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); // (~A & ~B) == (~(A | B)) - De Morgan's Law if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { @@ -4133,30 +4188,30 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { { 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); @@ -4168,7 +4223,7 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { } 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(Op1)->swapOperands(); std::swap(A, B); @@ -4181,17 +4236,17 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { } // (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(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(Op0)) @@ -4204,7 +4259,8 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { if (CastInst *Op1C = dyn_cast(Op1)) if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? const Type *SrcTy = Op0C->getOperand(0)->getType(); - if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && + if (SrcTy == Op1C->getOperand(0)->getType() && + SrcTy->isIntOrIntVector() && // Only do this if the casts both really cause code to be generated. ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), I.getType(), TD) && @@ -4235,69 +4291,9 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { // If and'ing two fcmp, try combine them into one. if (FCmpInst *LHS = dyn_cast(I.getOperand(0))) { - if (FCmpInst *RHS = dyn_cast(I.getOperand(1))) { - if (LHS->getPredicate() == FCmpInst::FCMP_ORD && - RHS->getPredicate() == FCmpInst::FCMP_ORD) { - // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) - if (ConstantFP *LHSC = dyn_cast(LHS->getOperand(1))) - if (ConstantFP *RHSC = dyn_cast(RHS->getOperand(1))) { - // If either of the constants are nans, then the whole thing returns - // false. - if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); - return new FCmpInst(*Context, FCmpInst::FCMP_ORD, - LHS->getOperand(0), RHS->getOperand(0)); - } - } else { - Value *Op0LHS, *Op0RHS, *Op1LHS, *Op1RHS; - FCmpInst::Predicate Op0CC, Op1CC; - if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS), - m_Value(Op0RHS)), *Context) && - match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), - m_Value(Op1RHS)), *Context)) { - if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { - // Swap RHS operands to match LHS. - Op1CC = FCmpInst::getSwappedPredicate(Op1CC); - std::swap(Op1LHS, Op1RHS); - } - 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); - else if (Op0CC == FCmpInst::FCMP_FALSE || - Op1CC == FCmpInst::FCMP_FALSE) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); - else if (Op0CC == FCmpInst::FCMP_TRUE) - return ReplaceInstUsesWith(I, Op1); - else if (Op1CC == FCmpInst::FCMP_TRUE) - return ReplaceInstUsesWith(I, Op0); - bool Op0Ordered; - bool Op1Ordered; - unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); - unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); - if (Op1Pred == 0) { - std::swap(Op0, Op1); - std::swap(Op0Pred, Op1Pred); - std::swap(Op0Ordered, Op1Ordered); - } - if (Op0Pred == 0) { - // uno && ueq -> uno && (uno || eq) -> ueq - // ord && olt -> ord && (ord && lt) -> olt - if (Op0Ordered == Op1Ordered) - return ReplaceInstUsesWith(I, Op1); - // uno && oeq -> uno && (ord && eq) -> false - // uno && ord -> false - if (!Op0Ordered) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); - // ord && ueq -> ord && (uno || eq) -> oeq - return cast(getFCmpValue(true, Op1Pred, - Op0LHS, Op0RHS, Context)); - } - } - } - } - } + if (FCmpInst *RHS = dyn_cast(I.getOperand(1))) + if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS)) + return Res; } return Changed ? &I : 0; @@ -4471,18 +4467,18 @@ static Instruction *MatchSelectFromAndOr(Value *A, Value *B, 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; } @@ -4496,9 +4492,9 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, // 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: @@ -4540,18 +4536,18 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, assert(LHSCst != RHSCst && "Compares not folded above?"); switch (LHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + 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 getConstantExprNeg(LHSCst); + Constant *AddCST = ConstantExpr::getNeg(LHSCst); Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST, Val->getName()+".off"); InsertNewInstBefore(Add, I); - AddCST = Context->getConstantExprSub(AddOne(RHSCst, Context), LHSCst); + AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst); return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, AddCST); } break; // (X == 13 | X == 15) -> no change @@ -4566,7 +4562,7 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, break; case ICmpInst::ICMP_NE: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 @@ -4574,12 +4570,12 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); } break; case ICmpInst::ICMP_ULT: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change break; case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2 @@ -4587,7 +4583,7 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, // 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; @@ -4600,7 +4596,7 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, break; case ICmpInst::ICMP_SLT: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change break; case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2 @@ -4608,7 +4604,7 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, // 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; @@ -4621,7 +4617,7 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, break; case ICmpInst::ICMP_UGT: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 return ReplaceInstUsesWith(I, LHS); @@ -4629,14 +4625,14 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, break; case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change break; } break; case ICmpInst::ICMP_SGT: switch (RHSCC) { - default: LLVM_UNREACHABLE("Unknown integer condition code!"); + default: llvm_unreachable("Unknown integer condition code!"); case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 return ReplaceInstUsesWith(I, LHS); @@ -4644,7 +4640,7 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, break; case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change break; } @@ -4653,6 +4649,72 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, return 0; } +Instruction *InstCombiner::FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, + FCmpInst *RHS) { + if (LHS->getPredicate() == FCmpInst::FCMP_UNO && + RHS->getPredicate() == FCmpInst::FCMP_UNO && + LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) { + if (ConstantFP *LHSC = dyn_cast(LHS->getOperand(1))) + if (ConstantFP *RHSC = dyn_cast(RHS->getOperand(1))) { + // If either of the constants are nans, then the whole thing returns + // true. + if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); + + // Otherwise, no need to compare the two constants, compare the + // rest. + return new FCmpInst(*Context, FCmpInst::FCMP_UNO, + LHS->getOperand(0), RHS->getOperand(0)); + } + + // Handle vector zeros. This occurs because the canonical form of + // "fcmp uno x,x" is "fcmp uno x, 0". + if (isa(LHS->getOperand(1)) && + isa(RHS->getOperand(1))) + return new FCmpInst(*Context, FCmpInst::FCMP_UNO, + LHS->getOperand(0), RHS->getOperand(0)); + + return 0; + } + + Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); + Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); + FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); + + if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { + // Swap RHS operands to match LHS. + Op1CC = FCmpInst::getSwappedPredicate(Op1CC); + std::swap(Op1LHS, Op1RHS); + } + 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); + if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE) + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); + if (Op0CC == FCmpInst::FCMP_FALSE) + return ReplaceInstUsesWith(I, RHS); + if (Op1CC == FCmpInst::FCMP_FALSE) + return ReplaceInstUsesWith(I, LHS); + bool Op0Ordered; + bool Op1Ordered; + unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); + unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); + if (Op0Ordered == Op1Ordered) { + // If both are ordered or unordered, return a new fcmp with + // or'ed predicates. + Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, + Op0LHS, Op0RHS, Context); + if (Instruction *I = dyn_cast(RV)) + return I; + // Otherwise, it's a constant boolean value... + return ReplaceInstUsesWith(I, RV); + } + } + return 0; +} + /// FoldOrWithConstants - This helper function folds: /// /// ((A | B) & C1) | (B & C2) @@ -4669,7 +4731,7 @@ Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op, 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; @@ -4688,7 +4750,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); if (isa(Op1)) // X | undef -> -1 - return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); // or X, X = X if (Op0 == Op1) @@ -4711,23 +4773,23 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { if (ConstantInt *RHS = dyn_cast(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); Or->takeName(Op0); return BinaryOperator::CreateAnd(Or, - Context->getConstantInt(RHS->getValue() | C1->getValue())); + ConstantInt::get(*Context, RHS->getValue() | C1->getValue())); } // (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); Or->takeName(Op0); return BinaryOperator::CreateXor(Or, - Context->getConstantInt(C1->getValue() & ~RHS->getValue())); + ConstantInt::get(*Context, C1->getValue() & ~RHS->getValue())); } // Try to fold constant and into select arguments. @@ -4742,26 +4804,26 @@ Instruction *InstCombiner::visitOr(BinaryOperator &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); @@ -4771,7 +4833,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &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); @@ -4781,8 +4843,8 @@ Instruction *InstCombiner::visitOr(BinaryOperator &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(C); C2 = dyn_cast(D); @@ -4792,7 +4854,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { // 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); @@ -4801,7 +4863,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { } // 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); @@ -4842,20 +4904,20 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { 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); } @@ -4875,28 +4937,28 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { } // ((A|B)&1)|(B&-2) -> (A&1) | B - if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C)), *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, Context->getAllOnesValue(I.getType())); + 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, Context->getAllOnesValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); // (~A | ~B) == (~(A & B)) - De Morgan's Law if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { @@ -4908,7 +4970,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) if (ICmpInst *RHS = dyn_cast(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(I.getOperand(0))) @@ -4923,7 +4985,8 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { if (!isa(Op0C->getOperand(0)) || !isa(Op1C->getOperand(0))) { const Type *SrcTy = Op0C->getOperand(0)->getType(); - if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && + if (SrcTy == Op1C->getOperand(0)->getType() && + SrcTy->isIntOrIntVector() && // Only do this if the casts both really cause code to be // generated. ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), @@ -4943,64 +5006,9 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) if (FCmpInst *LHS = dyn_cast(I.getOperand(0))) { - if (FCmpInst *RHS = dyn_cast(I.getOperand(1))) { - if (LHS->getPredicate() == FCmpInst::FCMP_UNO && - RHS->getPredicate() == FCmpInst::FCMP_UNO && - LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) { - if (ConstantFP *LHSC = dyn_cast(LHS->getOperand(1))) - if (ConstantFP *RHSC = dyn_cast(RHS->getOperand(1))) { - // If either of the constants are nans, then the whole thing returns - // true. - if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); - - // Otherwise, no need to compare the two constants, compare the - // rest. - return new FCmpInst(*Context, FCmpInst::FCMP_UNO, - LHS->getOperand(0), RHS->getOperand(0)); - } - } else { - Value *Op0LHS, *Op0RHS, *Op1LHS, *Op1RHS; - FCmpInst::Predicate Op0CC, Op1CC; - if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS), - m_Value(Op0RHS)), *Context) && - match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), - m_Value(Op1RHS)), *Context)) { - if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { - // Swap RHS operands to match LHS. - Op1CC = FCmpInst::getSwappedPredicate(Op1CC); - std::swap(Op1LHS, Op1RHS); - } - 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); - else if (Op0CC == FCmpInst::FCMP_TRUE || - Op1CC == FCmpInst::FCMP_TRUE) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); - else if (Op0CC == FCmpInst::FCMP_FALSE) - return ReplaceInstUsesWith(I, Op1); - else if (Op1CC == FCmpInst::FCMP_FALSE) - return ReplaceInstUsesWith(I, Op0); - bool Op0Ordered; - bool Op1Ordered; - unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); - unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); - if (Op0Ordered == Op1Ordered) { - // If both are ordered or unordered, return a new fcmp with - // or'ed predicates. - Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, - Op0LHS, Op0RHS, Context); - if (Instruction *I = dyn_cast(RV)) - return I; - // Otherwise, it's a constant boolean value... - return ReplaceInstUsesWith(I, RV); - } - } - } - } - } + if (FCmpInst *RHS = dyn_cast(I.getOperand(1))) + if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS)) + return Res; } return Changed ? &I : 0; @@ -5028,14 +5036,14 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { if (isa(Op0)) // Handle undef ^ undef -> 0 special case. This is a common // idiom (misuse). - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef } // 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, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); } // See if we can simplify any instructions used by the instruction whose sole @@ -5047,14 +5055,14 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { 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(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(Op0I->getOperand(1), Op0I->getOperand(1)->getName()+".not"); @@ -5070,7 +5078,7 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { if (ConstantInt *RHS = dyn_cast(Op1)) { - if (RHS == Context->getConstantIntTrue() && Op0->hasOneUse()) { + if (RHS == ConstantInt::getTrue(*Context) && Op0->hasOneUse()) { // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B if (ICmpInst *ICI = dyn_cast(Op0)) return new ICmpInst(*Context, ICI->getInversePredicate(), @@ -5087,8 +5095,8 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { if (CI->hasOneUse() && Op0C->hasOneUse()) { Instruction::CastOps Opcode = Op0C->getOpcode(); if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) { - if (RHS == Context->getConstantExprCast(Opcode, - Context->getConstantIntTrue(), + if (RHS == ConstantExpr::getCast(Opcode, + ConstantInt::getTrue(*Context), Op0C->getDestTy())) { Instruction *NewCI = InsertNewInstBefore(CmpInst::Create( *Context, @@ -5106,9 +5114,9 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { // ~(c-X) == X-c-1 == X+(-c-1) if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) if (Constant *Op0I0C = dyn_cast(Op0I->getOperand(0))) { - Constant *NegOp0I0C = Context->getConstantExprNeg(Op0I0C); - Constant *ConstantRHS = Context->getConstantExprSub(NegOp0I0C, - Context->getConstantInt(I.getType(), 1)); + Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); + Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, + ConstantInt::get(I.getType(), 1)); return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS); } @@ -5116,27 +5124,27 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { if (Op0I->getOpcode() == Instruction::Add) { // ~(X-c) --> (-c-1)-X if (RHS->isAllOnesValue()) { - Constant *NegOp0CI = Context->getConstantExprNeg(Op0CI); + Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); return BinaryOperator::CreateSub( - Context->getConstantExprSub(NegOp0CI, - Context->getConstantInt(I.getType(), 1)), + ConstantExpr::getSub(NegOp0CI, + ConstantInt::get(I.getType(), 1)), Op0I->getOperand(0)); } else if (RHS->getValue().isSignBit()) { // (X + C) ^ signbit -> (X + C + signbit) - Constant *C = - Context->getConstantInt(RHS->getValue() + Op0CI->getValue()); + Constant *C = ConstantInt::get(*Context, + RHS->getValue() + Op0CI->getValue()); return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); } } else if (Op0I->getOpcode() == Instruction::Or) { // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { - Constant *NewRHS = Context->getConstantExprOr(Op0CI, RHS); + Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); // Anything in both C1 and C2 is known to be zero, remove it from // NewRHS. - Constant *CommonBits = Context->getConstantExprAnd(Op0CI, RHS); - NewRHS = Context->getConstantExprAnd(NewRHS, - Context->getConstantExprNot(CommonBits)); + Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS); + NewRHS = ConstantExpr::getAnd(NewRHS, + ConstantExpr::getNot(CommonBits)); AddToWorkList(Op0I); I.setOperand(0, Op0I->getOperand(0)); I.setOperand(1, NewRHS); @@ -5155,19 +5163,19 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { 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, Context->getAllOnesValue(I.getType())); + 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, Context->getAllOnesValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType())); BinaryOperator *Op1I = dyn_cast(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(); @@ -5176,11 +5184,11 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { 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(); @@ -5196,7 +5204,7 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { BinaryOperator *Op0I = dyn_cast(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); @@ -5205,11 +5213,11 @@ Instruction *InstCombiner::visitXor(BinaryOperator &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); @@ -5238,22 +5246,22 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { 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) @@ -5275,7 +5283,7 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) if (ICmpInst *RHS = dyn_cast(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)) @@ -5303,7 +5311,7 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { static ConstantInt *ExtractElement(Constant *V, Constant *Idx, LLVMContext *Context) { - return cast(Context->getConstantExprExtractElement(V, Idx)); + return cast(ConstantExpr::getExtractElement(V, Idx)); } static bool HasAddOverflow(ConstantInt *Result, @@ -5323,11 +5331,11 @@ static bool HasAddOverflow(ConstantInt *Result, static bool AddWithOverflow(Constant *&Result, Constant *In1, Constant *In2, LLVMContext *Context, bool IsSigned = false) { - Result = Context->getConstantExprAdd(In1, In2); + Result = ConstantExpr::getAdd(In1, In2); if (const VectorType *VTy = dyn_cast(In1->getType())) { for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - Constant *Idx = Context->getConstantInt(Type::Int32Ty, i); + Constant *Idx = ConstantInt::get(Type::Int32Ty, i); if (HasAddOverflow(ExtractElement(Result, Idx, Context), ExtractElement(In1, Idx, Context), ExtractElement(In2, Idx, Context), @@ -5359,11 +5367,11 @@ static bool HasSubOverflow(ConstantInt *Result, static bool SubWithOverflow(Constant *&Result, Constant *In1, Constant *In2, LLVMContext *Context, bool IsSigned = false) { - Result = Context->getConstantExprSub(In1, In2); + Result = ConstantExpr::getSub(In1, In2); if (const VectorType *VTy = dyn_cast(In1->getType())) { for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - Constant *Idx = Context->getConstantInt(Type::Int32Ty, i); + Constant *Idx = ConstantInt::get(Type::Int32Ty, i); if (HasSubOverflow(ExtractElement(Result, Idx, Context), ExtractElement(In1, Idx, Context), ExtractElement(In2, Idx, Context), @@ -5382,11 +5390,11 @@ static bool SubWithOverflow(Constant *&Result, Constant *In1, /// code necessary to compute the offset from the base pointer (without adding /// in the base pointer). Return the result as a signed integer of intptr size. static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { - TargetData &TD = IC.getTargetData(); + TargetData &TD = *IC.getTargetData(); gep_type_iterator GTI = gep_type_begin(GEP); const Type *IntPtrTy = TD.getIntPtrType(); LLVMContext *Context = IC.getContext(); - Value *Result = Context->getNullValue(IntPtrTy); + Value *Result = Constant::getNullValue(IntPtrTy); // Build a mask for high order bits. unsigned IntPtrWidth = TD.getPointerSizeInBits(); @@ -5405,21 +5413,22 @@ static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { if (ConstantInt *RC = dyn_cast(Result)) Result = - Context->getConstantInt(RC->getValue() + APInt(IntPtrWidth, Size)); + ConstantInt::get(*Context, + RC->getValue() + APInt(IntPtrWidth, Size)); else Result = IC.InsertNewInstBefore( BinaryOperator::CreateAdd(Result, - Context->getConstantInt(IntPtrTy, Size), + ConstantInt::get(IntPtrTy, Size), GEP->getName()+".offs"), I); continue; } - Constant *Scale = Context->getConstantInt(IntPtrTy, Size); + Constant *Scale = ConstantInt::get(IntPtrTy, Size); Constant *OC = - Context->getConstantExprIntegerCast(OpC, IntPtrTy, true /*SExt*/); - Scale = Context->getConstantExprMul(OC, Scale); + ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); + Scale = ConstantExpr::getMul(OC, Scale); if (Constant *RC = dyn_cast(Result)) - Result = Context->getConstantExprAdd(RC, Scale); + Result = ConstantExpr::getAdd(RC, Scale); else { // Emit an add instruction. Result = IC.InsertNewInstBefore( @@ -5431,16 +5440,16 @@ static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { // Convert to correct type. if (Op->getType() != IntPtrTy) { if (Constant *OpC = dyn_cast(Op)) - Op = Context->getConstantExprIntegerCast(OpC, IntPtrTy, true); + Op = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true); else Op = IC.InsertNewInstBefore(CastInst::CreateIntegerCast(Op, IntPtrTy, true, Op->getName()+".c"), I); } if (Size != 1) { - Constant *Scale = Context->getConstantInt(IntPtrTy, Size); + Constant *Scale = ConstantInt::get(IntPtrTy, Size); if (Constant *OpC = dyn_cast(Op)) - Op = Context->getConstantExprMul(OpC, Scale); + Op = ConstantExpr::getMul(OpC, Scale); else // We'll let instcombine(mul) convert this to a shl if possible. Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale, GEP->getName()+".idx"), I); @@ -5448,7 +5457,7 @@ static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { // Emit an add instruction. if (isa(Op) && isa(Result)) - Result = Context->getConstantExprAdd(cast(Op), + Result = ConstantExpr::getAdd(cast(Op), cast(Result)); else Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result, @@ -5458,19 +5467,19 @@ static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { } -/// EvaluateGEPOffsetExpression - Return an value that can be used to compare of -/// the *offset* implied by GEP to zero. For example, if we have &A[i], we want -/// to return 'i' for "icmp ne i, 0". Note that, in general, indices can be -/// complex, and scales are involved. The above expression would also be legal -/// to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This -/// later form is less amenable to optimization though, and we are allowed to -/// generate the first by knowing that pointer arithmetic doesn't overflow. +/// EvaluateGEPOffsetExpression - Return a value that can be used to compare +/// the *offset* implied by a GEP to zero. For example, if we have &A[i], we +/// want to return 'i' for "icmp ne i, 0". Note that, in general, indices can +/// be complex, and scales are involved. The above expression would also be +/// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). +/// This later form is less amenable to optimization though, and we are allowed +/// to generate the first by knowing that pointer arithmetic doesn't overflow. /// /// If we can't emit an optimized form for this expression, this returns null. /// static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, InstCombiner &IC) { - TargetData &TD = IC.getTargetData(); + TargetData &TD = *IC.getTargetData(); gep_type_iterator GTI = gep_type_begin(GEP); // Check to see if this gep only has a single variable index. If so, and if @@ -5534,7 +5543,7 @@ static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, // computation crosses zero. if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(), - VariableIdx->getNameStart(), &I); + VariableIdx->getName(), &I); return VariableIdx; } @@ -5558,36 +5567,35 @@ static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, if (VariableIdx->getType() != IntPtrTy) VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy, true /*SExt*/, - VariableIdx->getNameStart(), &I); - Constant *OffsetVal = IC.getContext()->getConstantInt(IntPtrTy, NewOffs); + VariableIdx->getName(), &I); + Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I); } /// FoldGEPICmp - Fold comparisons between a GEP instruction and something /// else. At this point we know that the GEP is on the LHS of the comparison. -Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, +Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, Instruction &I) { - assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); - // Look through bitcasts. if (BitCastInst *BCI = dyn_cast(RHS)) RHS = BCI->getOperand(0); Value *PtrBase = GEPLHS->getOperand(0); - if (PtrBase == RHS) { + if (TD && PtrBase == RHS && GEPLHS->isInBounds()) { // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). // This transformation (ignoring the base and scales) is valid because we - // know pointers can't overflow. See if we can output an optimized form. + // know pointers can't overflow since the gep is inbounds. See if we can + // output an optimized form. Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this); // If not, synthesize the offset the hard way. if (Offset == 0) Offset = EmitGEPOffset(GEPLHS, I, *this); return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), Offset, - Context->getNullValue(Offset->getType())); - } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { + Constant::getNullValue(Offset->getType())); + } else if (GEPOperator *GEPRHS = dyn_cast(RHS)) { // If the base pointers are different, but the indices are the same, just // compare the base pointer. if (PtrBase != GEPRHS->getOperand(0)) { @@ -5653,7 +5661,7 @@ Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, if (NumDifferences == 0) // SAME GEP? return ReplaceInstUsesWith(I, // No comparison is needed here. - Context->getConstantInt(Type::Int1Ty, + ConstantInt::get(Type::Int1Ty, ICmpInst::isTrueWhenEqual(Cond))); else if (NumDifferences == 1) { @@ -5667,7 +5675,8 @@ Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, // Only lower this if the icmp is the only user of the GEP or if we expect // the result to fold to a constant! - if ((isa(GEPLHS) || GEPLHS->hasOneUse()) && + if (TD && + (isa(GEPLHS) || GEPLHS->hasOneUse()) && (isa(GEPRHS) || GEPRHS->hasOneUse())) { // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) Value *L = EmitGEPOffset(GEPLHS, I, *this); @@ -5712,7 +5721,7 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, ICmpInst::Predicate Pred; switch (I.getPredicate()) { - default: LLVM_UNREACHABLE("Unexpected predicate!"); + default: llvm_unreachable("Unexpected predicate!"); case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_OEQ: Pred = ICmpInst::ICMP_EQ; @@ -5738,9 +5747,9 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, Pred = ICmpInst::ICMP_NE; break; case FCmpInst::FCMP_ORD: - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); case FCmpInst::FCMP_UNO: - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); } const IntegerType *IntTy = cast(LHSI->getOperand(0)->getType()); @@ -5760,8 +5769,8 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); } } else { // If the RHS value is > UnsignedMax, fold the comparison. This handles @@ -5772,8 +5781,8 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); } } @@ -5785,8 +5794,8 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); } } @@ -5795,27 +5804,27 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, // casting the FP value to the integer value and back, checking for equality. // Don't do this for zero, because -0.0 is not fractional. Constant *RHSInt = LHSUnsigned - ? Context->getConstantExprFPToUI(RHSC, IntTy) - : Context->getConstantExprFPToSI(RHSC, IntTy); + ? ConstantExpr::getFPToUI(RHSC, IntTy) + : ConstantExpr::getFPToSI(RHSC, IntTy); if (!RHS.isZero()) { bool Equal = LHSUnsigned - ? Context->getConstantExprUIToFP(RHSInt, RHSC->getType()) == RHSC - : Context->getConstantExprSIToFP(RHSInt, RHSC->getType()) == RHSC; + ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC + : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC; if (!Equal) { // If we had a comparison against a fractional value, we have to adjust // the compare predicate and sometimes the value. RHSC is rounded towards // zero at this point. switch (Pred) { - default: LLVM_UNREACHABLE("Unexpected integer comparison!"); + default: llvm_unreachable("Unexpected integer comparison!"); case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); case ICmpInst::ICMP_ULE: // (float)int <= 4.4 --> int <= 4 // (float)int <= -4.4 --> false if (RHS.isNegative()) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); break; case ICmpInst::ICMP_SLE: // (float)int <= 4.4 --> int <= 4 @@ -5827,7 +5836,7 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, // (float)int < -4.4 --> false // (float)int < 4.4 --> int <= 4 if (RHS.isNegative()) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); Pred = ICmpInst::ICMP_ULE; break; case ICmpInst::ICMP_SLT: @@ -5840,7 +5849,7 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, // (float)int > 4.4 --> int > 4 // (float)int > -4.4 --> true if (RHS.isNegative()) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); break; case ICmpInst::ICMP_SGT: // (float)int > 4.4 --> int > 4 @@ -5852,7 +5861,7 @@ Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, // (float)int >= -4.4 --> true // (float)int >= 4.4 --> int > 4 if (!RHS.isNegative()) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); Pred = ICmpInst::ICMP_UGT; break; case ICmpInst::ICMP_SGE: @@ -5876,22 +5885,22 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { // Fold trivial predicates. if (I.getPredicate() == FCmpInst::FCMP_FALSE) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); if (I.getPredicate() == FCmpInst::FCMP_TRUE) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); // Simplify 'fcmp pred X, X' if (Op0 == Op1) { switch (I.getPredicate()) { - default: LLVM_UNREACHABLE("Unknown predicate!"); + default: llvm_unreachable("Unknown predicate!"); case FCmpInst::FCMP_UEQ: // True if unordered or equal case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); case FCmpInst::FCMP_OGT: // True if ordered and greater than case FCmpInst::FCMP_OLT: // True if ordered and less than case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) case FCmpInst::FCMP_ULT: // True if unordered or less than @@ -5899,7 +5908,7 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { case FCmpInst::FCMP_UNE: // True if unordered or not equal // Canonicalize these to be 'fcmp uno %X, 0.0'. I.setPredicate(FCmpInst::FCMP_UNO); - I.setOperand(1, Context->getNullValue(Op0->getType())); + I.setOperand(1, Constant::getNullValue(Op0->getType())); return &I; case FCmpInst::FCMP_ORD: // True if ordered (no nans) @@ -5908,13 +5917,13 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { case FCmpInst::FCMP_OLE: // True if ordered and less than or equal // Canonicalize these to be 'fcmp ord %X, 0.0'. I.setPredicate(FCmpInst::FCMP_ORD); - I.setOperand(1, Context->getNullValue(Op0->getType())); + I.setOperand(1, Constant::getNullValue(Op0->getType())); return &I; } } if (isa(Op1)) // fcmp pred X, undef -> undef - return ReplaceInstUsesWith(I, Context->getUndef(Type::Int1Ty)); + return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); // Handle fcmp with constant RHS if (Constant *RHSC = dyn_cast(Op1)) { @@ -5922,11 +5931,11 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { if (ConstantFP *CFP = dyn_cast(RHSC)) { if (CFP->getValueAPF().isNaN()) { if (FCmpInst::isOrdered(I.getPredicate())) // True if ordered and... - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); assert(FCmpInst::isUnordered(I.getPredicate()) && "Comparison must be either ordered or unordered!"); // True if unordered. - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); } } @@ -5953,14 +5962,14 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { if (LHSI->hasOneUse()) { if (Constant *C = dyn_cast(LHSI->getOperand(1))) { // Fold the known value into the constant operand. - Op1 = Context->getConstantExprCompare(I.getPredicate(), C, RHSC); + Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); // Insert a new FCmp of the other select operand. Op2 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(), LHSI->getOperand(2), RHSC, I.getName()), I); } else if (Constant *C = dyn_cast(LHSI->getOperand(2))) { // Fold the known value into the constant operand. - Op2 = Context->getConstantExprCompare(I.getPredicate(), C, RHSC); + Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); // Insert a new FCmp of the other select operand. Op1 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(), LHSI->getOperand(1), RHSC, @@ -5984,11 +5993,11 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // icmp X, X if (Op0 == Op1) - return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty, + return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, I.isTrueWhenEqual())); if (isa(Op1)) // X icmp undef -> undef - return ReplaceInstUsesWith(I, Context->getUndef(Type::Int1Ty)); + return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); // icmp , - Global/Stack value // addresses never equal each other! We already know that Op0 != Op1. @@ -5996,13 +6005,13 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { isa(Op0)) && (isa(Op1) || isa(Op1) || isa(Op1))) - return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty, + return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, !I.isTrueWhenEqual())); // icmp's with boolean values can always be turned into bitwise operations if (Ty == Type::Int1Ty) { switch (I.getPredicate()) { - default: LLVM_UNREACHABLE("Invalid icmp instruction!"); + 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); @@ -6060,7 +6069,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // (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); } @@ -6072,24 +6081,24 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { default: break; case ICmpInst::ICMP_ULE: if (CI->isMaxValue(false)) // A <=u MAX -> TRUE - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Op0, - AddOne(CI, Context)); + AddOne(CI)); case ICmpInst::ICMP_SLE: if (CI->isMaxValue(true)) // A <=s MAX -> TRUE - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0, - AddOne(CI, Context)); + AddOne(CI)); case ICmpInst::ICMP_UGE: if (CI->isMinValue(false)) // A >=u MIN -> TRUE - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Op0, - SubOne(CI, Context)); + SubOne(CI)); case ICmpInst::ICMP_SGE: if (CI->isMinValue(true)) // A >=s MIN -> TRUE - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0, - SubOne(CI, Context)); + SubOne(CI)); } // If this comparison is a normal comparison, it demands all @@ -6136,114 +6145,114 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // that code below can assume that Min != Max. if (!isa(Op0) && Op0Min == Op0Max) return new ICmpInst(*Context, I.getPredicate(), - Context->getConstantInt(Op0Min), Op1); + ConstantInt::get(*Context, Op0Min), Op1); if (!isa(Op1) && Op1Min == Op1Max) return new ICmpInst(*Context, I.getPredicate(), Op0, - Context->getConstantInt(Op1Min)); + ConstantInt::get(*Context, Op1Min)); // Based on the range information we know about the LHS, see if we can // simplify this comparison. For example, (x&4) < 8 is always true. switch (I.getPredicate()) { - default: LLVM_UNREACHABLE("Unknown icmp opcode!"); + default: llvm_unreachable("Unknown icmp opcode!"); case ICmpInst::ICMP_EQ: if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); break; case ICmpInst::ICMP_NE: if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); break; case ICmpInst::ICMP_ULT: if (Op0Max.ult(Op1Min)) // A true if max(A) < min(B) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); if (Op0Min.uge(Op1Max)) // A false if min(A) >= max(B) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); if (Op1Min == Op0Max) // A A != B if max(A) == min(B) return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1); if (ConstantInt *CI = dyn_cast(Op1)) { if (Op1Max == Op0Min+1) // A A == C-1 if min(A)+1 == C return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0, - SubOne(CI, Context)); + SubOne(CI)); // (x (x >s -1) -> true if sign bit clear if (CI->isMinValue(true)) return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0, - Context->getConstantIntAllOnesValue(Op0->getType())); + Constant::getAllOnesValue(Op0->getType())); } break; case ICmpInst::ICMP_UGT: if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + 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); if (ConstantInt *CI = dyn_cast(Op1)) { if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0, - AddOne(CI, Context)); + AddOne(CI)); // (x >u 2147483647) -> (x true if sign bit set if (CI->isMaxValue(true)) return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0, - Context->getNullValue(Op0->getType())); + Constant::getNullValue(Op0->getType())); } break; case ICmpInst::ICMP_SLT: if (Op0Max.slt(Op1Min)) // A true if max(A) < min(C) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); if (Op0Min.sge(Op1Max)) // A false if min(A) >= max(C) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); if (Op1Min == Op0Max) // A A != B if max(A) == min(B) return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1); if (ConstantInt *CI = dyn_cast(Op1)) { if (Op1Max == Op0Min+1) // A A == C-1 if min(A)+1 == C return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0, - SubOne(CI, Context)); + SubOne(CI)); } break; case ICmpInst::ICMP_SGT: if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + 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); if (ConstantInt *CI = dyn_cast(Op1)) { if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0, - AddOne(CI, Context)); + AddOne(CI)); } break; case ICmpInst::ICMP_SGE: assert(!isa(Op1) && "ICMP_SGE with ConstantInt not folded!"); if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); break; case ICmpInst::ICMP_SLE: assert(!isa(Op1) && "ICMP_SLE with ConstantInt not folded!"); if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); break; case ICmpInst::ICMP_UGE: assert(!isa(Op1) && "ICMP_UGE with ConstantInt not folded!"); if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); break; case ICmpInst::ICMP_ULE: assert(!isa(Op1) && "ICMP_ULE with ConstantInt not folded!"); if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B) - return ReplaceInstUsesWith(I, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(I, ConstantInt::getTrue(*Context)); if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B) - return ReplaceInstUsesWith(I, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(I, ConstantInt::getFalse(*Context)); break; } @@ -6295,7 +6304,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { } if (isAllZeros) return new ICmpInst(*Context, I.getPredicate(), LHSI->getOperand(0), - Context->getNullValue(LHSI->getOperand(0)->getType())); + Constant::getNullValue(LHSI->getOperand(0)->getType())); } break; @@ -6315,14 +6324,14 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { if (LHSI->hasOneUse()) { if (Constant *C = dyn_cast(LHSI->getOperand(1))) { // Fold the known value into the constant operand. - Op1 = Context->getConstantExprICmp(I.getPredicate(), C, RHSC); + Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); // Insert a new ICmp of the other select operand. Op2 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(), LHSI->getOperand(2), RHSC, I.getName()), I); } else if (Constant *C = dyn_cast(LHSI->getOperand(2))) { // Fold the known value into the constant operand. - Op2 = Context->getConstantExprICmp(I.getPredicate(), C, RHSC); + Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); // Insert a new ICmp of the other select operand. Op1 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(), LHSI->getOperand(1), RHSC, @@ -6339,7 +6348,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // can assume it is successful and remove the malloc. if (LHSI->hasOneUse() && isa(RHSC)) { AddToWorkList(LHSI); - return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty, + return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, !I.isTrueWhenEqual())); } break; @@ -6347,10 +6356,10 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { } // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. - if (User *GEP = dyn_castGetElementPtr(Op0)) + if (GEPOperator *GEP = dyn_cast(Op0)) if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) return NI; - if (User *GEP = dyn_castGetElementPtr(Op1)) + if (GEPOperator *GEP = dyn_cast(Op1)) if (Instruction *NI = FoldGEPICmp(GEP, Op0, ICmpInst::getSwappedPredicate(I.getPredicate()), I)) return NI; @@ -6373,7 +6382,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // If Op1 is a constant, we can fold the cast into the constant. if (Op0->getType() != Op1->getType()) { if (Constant *Op1C = dyn_cast(Op1)) { - Op1 = Context->getConstantExprBitCast(Op1C, Op0->getType()); + Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); } else { // Otherwise, cast the RHS right before the icmp Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); @@ -6437,7 +6446,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // Mask = -1 >> count-trailing-zeros(Cst). if (!CI->isZero() && !CI->isOne()) { const APInt &AP = CI->getValue(); - ConstantInt *Mask = Context->getConstantInt( + ConstantInt *Mask = ConstantInt::get(*Context, APInt::getLowBitsSet(AP.getBitWidth(), AP.getBitWidth() - AP.countTrailingZeros())); @@ -6458,8 +6467,8 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // ~x < ~y --> y < x { Value *A, *B; - if (match(Op0, m_Not(m_Value(A)), *Context) && - match(Op1, m_Not(m_Value(B)), *Context)) + if (match(Op0, m_Not(m_Value(A))) && + match(Op1, m_Not(m_Value(B)))) return new ICmpInst(*Context, I.getPredicate(), B, A); } @@ -6467,24 +6476,24 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { Value *A, *B, *C, *D; // -x == -y --> x == y - if (match(Op0, m_Neg(m_Value(A)), *Context) && - match(Op1, m_Neg(m_Value(B)), *Context)) + if (match(Op0, m_Neg(m_Value(A))) && + match(Op1, m_Neg(m_Value(B)))) return new ICmpInst(*Context, I.getPredicate(), A, B); - if (match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) { + if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 Value *OtherVal = A == Op1 ? B : A; return new ICmpInst(*Context, I.getPredicate(), OtherVal, - Context->getNullValue(A->getType())); + 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 = - Context->getConstantInt(C1->getValue() ^ C2->getValue()); + ConstantInt::get(*Context, C1->getValue() ^ C2->getValue()); Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp"); return new ICmpInst(*Context, I.getPredicate(), A, InsertNewInstBefore(Xor, I)); @@ -6498,28 +6507,28 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { } } - 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, - Context->getNullValue(A->getType())); + Constant::getNullValue(A->getType())); } // (A-B) == A -> B == 0 - if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)), *Context)) + if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)))) return new ICmpInst(*Context, I.getPredicate(), B, - Context->getNullValue(B->getType())); + Constant::getNullValue(B->getType())); // A == (A-B) -> B == 0 - if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)), *Context)) + if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)))) return new ICmpInst(*Context, I.getPredicate(), B, - Context->getNullValue(B->getType())); + 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) { @@ -6536,7 +6545,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I); Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I); I.setOperand(0, Op1); - I.setOperand(1, Context->getNullValue(Op1->getType())); + I.setOperand(1, Constant::getNullValue(Op1->getType())); return &I; } } @@ -6575,13 +6584,13 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and // C2 (CI). By solving for X we can turn this into a range check // instead of computing a divide. - Constant *Prod = Context->getConstantExprMul(CmpRHS, DivRHS); + Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS); // Determine if the product overflows by seeing if the product is // not equal to the divide. Make sure we do the same kind of divide // as in the LHS instruction that we're folding. - bool ProdOV = (DivIsSigned ? Context->getConstantExprSDiv(Prod, DivRHS) : - Context->getConstantExprUDiv(Prod, DivRHS)) != CmpRHS; + bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : + ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; // Get the ICmp opcode ICmpInst::Predicate Pred = ICI.getPredicate(); @@ -6605,8 +6614,7 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, } 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(Context->getConstantExprNeg(SubOne(DivRHS, - Context))); + LoBound = cast(ConstantExpr::getNeg(SubOne(DivRHS))); HiBound = DivRHS; } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) @@ -6615,11 +6623,11 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, 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 = - cast(Context->getConstantExprNeg(DivRHS)); + cast(ConstantExpr::getNeg(DivRHS)); LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, Context, true) ? -1 : 0; } @@ -6627,15 +6635,15 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, } 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); - HiBound = cast(Context->getConstantExprNeg(DivRHS)); + LoBound = AddOne(DivRHS); + HiBound = cast(ConstantExpr::getNeg(DivRHS)); if (HiBound == DivRHS) { // -INTMIN = INTMIN HiOverflow = 1; // [INTMIN+1, overflow) HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN } } 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, @@ -6653,10 +6661,10 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, Value *X = DivI->getOperand(0); switch (Pred) { - default: LLVM_UNREACHABLE("Unhandled icmp opcode!"); + default: llvm_unreachable("Unhandled icmp opcode!"); case ICmpInst::ICMP_EQ: if (LoOverflow && HiOverflow) - return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context)); else if (HiOverflow) return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, X, LoBound); @@ -6667,7 +6675,7 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); case ICmpInst::ICMP_NE: if (LoOverflow && HiOverflow) - return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context)); else if (HiOverflow) return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, X, LoBound); @@ -6679,16 +6687,16 @@ Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_SLT: if (LoOverflow == +1) // Low bound is greater than input range. - return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context)); if (LoOverflow == -1) // Low bound is less than input range. - return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context)); return new ICmpInst(*Context, Pred, X, LoBound); case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_SGT: if (HiOverflow == +1) // High bound greater than input range. - return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context)); else if (HiOverflow == -1) // High bound less than input range. - return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context)); if (Pred == ICmpInst::ICMP_UGT) return new ICmpInst(*Context, ICmpInst::ICMP_UGE, X, HiBound); else @@ -6722,7 +6730,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, NewRHS.zext(SrcBits); NewRHS |= KnownOne; return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0), - Context->getConstantInt(NewRHS)); + ConstantInt::get(*Context, NewRHS)); } } break; @@ -6751,10 +6759,10 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, if (isTrueIfPositive) return new ICmpInst(*Context, ICmpInst::ICMP_SGT, CompareVal, - SubOne(RHS, Context)); + SubOne(RHS)); else return new ICmpInst(*Context, ICmpInst::ICMP_SLT, CompareVal, - AddOne(RHS, Context)); + AddOne(RHS)); } if (LHSI->hasOneUse()) { @@ -6765,7 +6773,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, ? ICI.getUnsignedPredicate() : ICI.getSignedPredicate(); return new ICmpInst(*Context, Pred, LHSI->getOperand(0), - Context->getConstantInt(RHSV ^ SignBit)); + ConstantInt::get(*Context, RHSV ^ SignBit)); } // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A) @@ -6776,7 +6784,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, : ICI.getSignedPredicate(); Pred = ICI.getSwappedPredicate(Pred); return new ICmpInst(*Context, Pred, LHSI->getOperand(0), - Context->getConstantInt(RHSV ^ NotSignBit)); + ConstantInt::get(*Context, RHSV ^ NotSignBit)); } } } @@ -6805,10 +6813,10 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, NewCI.zext(BitWidth); Instruction *NewAnd = BinaryOperator::CreateAnd(Cast->getOperand(0), - Context->getConstantInt(NewCST),LHSI->getName()); + ConstantInt::get(*Context, NewCST), LHSI->getName()); InsertNewInstBefore(NewAnd, ICI); return new ICmpInst(*Context, ICI.getPredicate(), NewAnd, - Context->getConstantInt(NewCI)); + ConstantInt::get(*Context, NewCI)); } } @@ -6845,28 +6853,28 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, if (CanFold) { Constant *NewCst; if (Shift->getOpcode() == Instruction::Shl) - NewCst = Context->getConstantExprLShr(RHS, ShAmt); + NewCst = ConstantExpr::getLShr(RHS, ShAmt); else - NewCst = Context->getConstantExprShl(RHS, ShAmt); + NewCst = ConstantExpr::getShl(RHS, ShAmt); // Check to see if we are shifting out any of the bits being // compared. - if (Context->getConstantExpr(Shift->getOpcode(), + if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { // If we shifted bits out, the fold is not going to work out. // As a special case, check to see if this means that the // result is always true or false now. if (ICI.getPredicate() == ICmpInst::ICMP_EQ) - return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context)); if (ICI.getPredicate() == ICmpInst::ICMP_NE) - return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context)); } else { ICI.setOperand(1, NewCst); Constant *NewAndCST; if (Shift->getOpcode() == Instruction::Shl) - NewAndCST = Context->getConstantExprLShr(AndCST, ShAmt); + NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); else - NewAndCST = Context->getConstantExprShl(AndCST, ShAmt); + NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); LHSI->setOperand(1, NewAndCST); LHSI->setOperand(0, Shift->getOperand(0)); AddToWorkList(Shift); // Shift is dead. @@ -6921,11 +6929,11 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, // If we are comparing against bits always shifted out, the // comparison cannot succeed. Constant *Comp = - Context->getConstantExprShl(Context->getConstantExprLShr(RHS, ShAmt), + ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); if (Comp != RHS) {// Comparing against a bit that we know is zero. bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; - Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE); + Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); return ReplaceInstUsesWith(ICI, Cst); } @@ -6933,7 +6941,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, // Otherwise strength reduce the shift into an and. uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); Constant *Mask = - Context->getConstantInt(APInt::getLowBitsSet(TypeBits, + ConstantInt::get(*Context, APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); Instruction *AndI = @@ -6941,7 +6949,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Mask, LHSI->getName()+".mask"); Value *And = InsertNewInstBefore(AndI, ICI); return new ICmpInst(*Context, ICI.getPredicate(), And, - Context->getConstantInt(RHSV.lshr(ShAmtVal))); + ConstantInt::get(*Context, RHSV.lshr(ShAmtVal))); } } @@ -6950,7 +6958,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, if (LHSI->hasOneUse() && isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { // (X << 31) (X&1) != 0 - Constant *Mask = Context->getConstantInt(APInt(TypeBits, 1) << + Constant *Mask = ConstantInt::get(*Context, APInt(TypeBits, 1) << (TypeBits-ShAmt->getZExtValue()-1)); Instruction *AndI = BinaryOperator::CreateAnd(LHSI->getOperand(0), @@ -6959,7 +6967,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, return new ICmpInst(*Context, TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, - And, Context->getNullValue(And->getType())); + And, Constant::getNullValue(And->getType())); } break; } @@ -6989,7 +6997,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, if (Comp != RHSV) { // Comparing against a bit that we know is zero. bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; - Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE); + Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); return ReplaceInstUsesWith(ICI, Cst); } @@ -7000,20 +7008,20 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, MaskedValueIsZero(LHSI->getOperand(0), APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0), - Context->getConstantExprShl(RHS, ShAmt)); + ConstantExpr::getShl(RHS, ShAmt)); } if (LHSI->hasOneUse()) { // Otherwise strength reduce the shift into an and. APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); - Constant *Mask = Context->getConstantInt(Val); + Constant *Mask = ConstantInt::get(*Context, Val); Instruction *AndI = BinaryOperator::CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask"); Value *And = InsertNewInstBefore(AndI, ICI); return new ICmpInst(*Context, ICI.getPredicate(), And, - Context->getConstantExprShl(RHS, ShAmt)); + ConstantExpr::getShl(RHS, ShAmt)); } break; } @@ -7046,18 +7054,18 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, if (ICI.isSignedPredicate()) { if (CR.getLower().isSignBit()) { return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHSI->getOperand(0), - Context->getConstantInt(CR.getUpper())); + ConstantInt::get(*Context, CR.getUpper())); } else if (CR.getUpper().isSignBit()) { return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHSI->getOperand(0), - Context->getConstantInt(CR.getLower())); + ConstantInt::get(*Context, CR.getLower())); } } else { if (CR.getLower().isMinValue()) { return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHSI->getOperand(0), - Context->getConstantInt(CR.getUpper())); + ConstantInt::get(*Context, CR.getUpper())); } else if (CR.getUpper().isMinValue()) { return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHSI->getOperand(0), - Context->getConstantInt(CR.getLower())); + ConstantInt::get(*Context, CR.getLower())); } } } @@ -7082,7 +7090,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, BO->getName()); InsertNewInstBefore(NewRem, ICI); return new ICmpInst(*Context, ICI.getPredicate(), NewRem, - Context->getNullValue(BO->getType())); + Constant::getNullValue(BO->getType())); } } break; @@ -7091,18 +7099,18 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, if (ConstantInt *BOp1C = dyn_cast(BO->getOperand(1))) { if (BO->hasOneUse()) return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0), - Context->getConstantExprSub(RHS, BOp1C)); + 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)) + if (Value *NegVal = dyn_castNegVal(BOp1)) return new ICmpInst(*Context, ICI.getPredicate(), BOp0, NegVal); - else if (Value *NegVal = dyn_castNegVal(BOp0, Context)) + else if (Value *NegVal = dyn_castNegVal(BOp0)) return new ICmpInst(*Context, ICI.getPredicate(), NegVal, BOp1); else if (BO->hasOneUse()) { - Instruction *Neg = BinaryOperator::CreateNeg(*Context, BOp1); + Instruction *Neg = BinaryOperator::CreateNeg(BOp1); InsertNewInstBefore(Neg, ICI); Neg->takeName(BO); return new ICmpInst(*Context, ICI.getPredicate(), BOp0, Neg); @@ -7114,7 +7122,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, // the explicit xor. if (Constant *BOC = dyn_cast(BO->getOperand(1))) return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0), - Context->getConstantExprXor(RHS, BOC)); + ConstantExpr::getXor(RHS, BOC)); // FALLTHROUGH case Instruction::Sub: @@ -7128,10 +7136,10 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, // If bits are being or'd in that are not present in the constant we // are comparing against, then the comparison could never succeed! if (Constant *BOC = dyn_cast(BO->getOperand(1))) { - Constant *NotCI = Context->getConstantExprNot(RHS); - if (!Context->getConstantExprAnd(BOC, NotCI)->isNullValue()) + Constant *NotCI = ConstantExpr::getNot(RHS); + if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) return ReplaceInstUsesWith(ICI, - Context->getConstantInt(Type::Int1Ty, + ConstantInt::get(Type::Int1Ty, isICMP_NE)); } break; @@ -7142,19 +7150,19 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, // comparison can never succeed! if ((RHSV & ~BOC->getValue()) != 0) return ReplaceInstUsesWith(ICI, - Context->getConstantInt(Type::Int1Ty, + ConstantInt::get(Type::Int1Ty, 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 : ICmpInst::ICMP_NE, LHSI, - Context->getNullValue(RHS->getType())); + Constant::getNullValue(RHS->getType())); // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 if (BOC->getValue().isSignBit()) { Value *X = BO->getOperand(0); - Constant *Zero = Context->getNullValue(X->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); @@ -7163,7 +7171,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, // ((X & ~7) == 0) --> X < 8 if (RHSV == 0 && isHighOnes(BOC)) { Value *X = BO->getOperand(0); - Constant *NegX = Context->getConstantExprNeg(BOC); + Constant *NegX = ConstantExpr::getNeg(BOC); ICmpInst::Predicate pred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; return new ICmpInst(*Context, pred, X, NegX); @@ -7176,7 +7184,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, if (II->getIntrinsicID() == Intrinsic::bswap) { AddToWorkList(II); ICI.setOperand(0, II->getOperand(1)); - ICI.setOperand(1, Context->getConstantInt(RHSV.byteSwap())); + ICI.setOperand(1, ConstantInt::get(*Context, RHSV.byteSwap())); return &ICI; } } @@ -7196,12 +7204,12 @@ Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the // integer type is the same size as the pointer type. - if (LHSCI->getOpcode() == Instruction::PtrToInt && - getTargetData().getPointerSizeInBits() == + if (TD && LHSCI->getOpcode() == Instruction::PtrToInt && + TD->getPointerSizeInBits() == cast(DestTy)->getBitWidth()) { Value *RHSOp = 0; if (Constant *RHSC = dyn_cast(ICI.getOperand(1))) { - RHSOp = Context->getConstantExprIntToPtr(RHSC, SrcTy); + RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); } else if (PtrToIntInst *RHSC = dyn_cast(ICI.getOperand(1))) { RHSOp = RHSC->getOperand(0); // If the pointer types don't match, insert a bitcast. @@ -7253,8 +7261,8 @@ Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { // Compute the constant that would happen if we truncated to SrcTy then // reextended to DestTy. - Constant *Res1 = Context->getConstantExprTrunc(CI, SrcTy); - Constant *Res2 = Context->getConstantExprCast(LHSCI->getOpcode(), + Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); + Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); // If the re-extended constant didn't change... @@ -7280,9 +7288,9 @@ Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { // First, handle some easy cases. We know the result cannot be equal at this // point so handle the ICI.isEquality() cases if (ICI.getPredicate() == ICmpInst::ICMP_EQ) - return ReplaceInstUsesWith(ICI, Context->getConstantIntFalse()); + return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context)); if (ICI.getPredicate() == ICmpInst::ICMP_NE) - return ReplaceInstUsesWith(ICI, Context->getConstantIntTrue()); + return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context)); // Evaluate the comparison for LT (we invert for GT below). LE and GE cases // should have been folded away previously and not enter in here. @@ -7290,20 +7298,20 @@ Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { if (isSignedCmp) { // We're performing a signed comparison. if (cast(CI)->getValue().isNegative()) - Result = Context->getConstantIntFalse(); // X < (small) --> false + Result = ConstantInt::getFalse(*Context); // X < (small) --> false else - Result = Context->getConstantIntTrue(); // X < (large) --> true + Result = ConstantInt::getTrue(*Context); // X < (large) --> true } else { // We're performing an unsigned comparison. if (isSignedExt) { // We're performing an unsigned comp with a sign extended value. // This is true if the input is >= 0. [aka >s -1] - Constant *NegOne = Context->getConstantIntAllOnesValue(SrcTy); + Constant *NegOne = Constant::getAllOnesValue(SrcTy); Result = InsertNewInstBefore(new ICmpInst(*Context, ICmpInst::ICMP_SGT, LHSCIOp, NegOne, ICI.getName()), ICI); } else { // Unsigned extend & unsigned compare -> always true. - Result = Context->getConstantIntTrue(); + Result = ConstantInt::getTrue(*Context); } } @@ -7316,7 +7324,7 @@ Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { ICI.getPredicate()==ICmpInst::ICMP_SGT) && "ICmp should be folded!"); if (Constant *CI = dyn_cast(Result)) - return ReplaceInstUsesWith(ICI, Context->getConstantExprNot(CI)); + return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); return BinaryOperator::CreateNot(Result); } @@ -7358,21 +7366,21 @@ Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { // shl X, 0 == X and shr X, 0 == X // shl 0, X == 0 and shr 0, X == 0 - if (Op1 == Context->getNullValue(Op1->getType()) || - Op0 == Context->getNullValue(Op0->getType())) + if (Op1 == Constant::getNullValue(Op1->getType()) || + Op0 == Constant::getNullValue(Op0->getType())) return ReplaceInstUsesWith(I, Op0); if (isa(Op0)) { if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef return ReplaceInstUsesWith(I, Op0); else // undef << X -> 0, undef >>u X -> 0 - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); } if (isa(Op1)) { if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X return ReplaceInstUsesWith(I, Op0); else // X << undef, X >>u undef -> 0 - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); } // See if we can fold away this shift. @@ -7404,9 +7412,9 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, // if (Op1->uge(TypeBits)) { if (I.getOpcode() != Instruction::AShr) - return ReplaceInstUsesWith(I, Context->getNullValue(Op0->getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); else { - I.setOperand(1, Context->getConstantInt(I.getType(), TypeBits-1)); + I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); return &I; } } @@ -7416,7 +7424,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, if (BO->getOpcode() == Instruction::Mul && isLeftShift) if (Constant *BOOp = dyn_cast(BO->getOperand(1))) return BinaryOperator::CreateMul(BO->getOperand(0), - Context->getConstantExprShl(BOOp, Op1)); + ConstantExpr::getShl(BOOp, Op1)); // Try to fold constant and into select arguments. if (SelectInst *SI = dyn_cast(Op0)) @@ -7437,7 +7445,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, if (TrOp && I.isLogicalShift() && TrOp->isShift() && isa(TrOp->getOperand(1))) { // Okay, we'll do this xform. Make the shift of shift. - Constant *ShAmt = Context->getConstantExprZExt(Op1, TrOp->getType()); + Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType()); Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt, I.getName()); InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) @@ -7462,7 +7470,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, } Instruction *And = - BinaryOperator::CreateAnd(NSh, Context->getConstantInt(MaskV), + BinaryOperator::CreateAnd(NSh, ConstantInt::get(*Context, MaskV), TI->getName()); InsertNewInstBefore(And, I); // shift1 & 0x00FF @@ -7486,7 +7494,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, // 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()); @@ -7496,7 +7504,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, Op0BO->getOperand(1)->getName()); InsertNewInstBefore(X, I); // (X + (Y << C)) uint32_t Op1Val = Op1->getLimitedValue(TypeBits); - return BinaryOperator::CreateAnd(X, Context->getConstantInt( + return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); } @@ -7505,7 +7513,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, if (isLeftShift && Op0BOOp1->hasOneUse() && match(Op0BOOp1, m_And(m_Shr(m_Value(V1), m_Specific(Op1)), - m_ConstantInt(CC)), *Context) && + m_ConstantInt(CC))) && cast(Op0BOOp1)->getOperand(0)->hasOneUse()) { Instruction *YS = BinaryOperator::CreateShl( Op0BO->getOperand(0), Op1, @@ -7513,7 +7521,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, InsertNewInstBefore(YS, I); // (Y << C) Instruction *XM = BinaryOperator::CreateAnd(V1, - Context->getConstantExprShl(CC, Op1), + ConstantExpr::getShl(CC, Op1), V1->getName()+".mask"); InsertNewInstBefore(XM, I); // X & (CC << C) @@ -7526,7 +7534,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *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()); @@ -7536,7 +7544,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, Op0BO->getOperand(0)->getName()); InsertNewInstBefore(X, I); // (X + (Y << C)) uint32_t Op1Val = Op1->getLimitedValue(TypeBits); - return BinaryOperator::CreateAnd(X, Context->getConstantInt( + return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); } @@ -7544,7 +7552,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, 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(Op0BO->getOperand(0)) ->getOperand(0)->hasOneUse()) { Instruction *YS = BinaryOperator::CreateShl( @@ -7553,7 +7561,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, InsertNewInstBefore(YS, I); // (Y << C) Instruction *XM = BinaryOperator::CreateAnd(V1, - Context->getConstantExprShl(CC, Op1), + ConstantExpr::getShl(CC, Op1), V1->getName()+".mask"); InsertNewInstBefore(XM, I); // X & (CC << C) @@ -7595,7 +7603,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, isValid = Op0C->getValue()[TypeBits-1] == highBitSet; if (isValid) { - Constant *NewRHS = Context->getConstantExpr(I.getOpcode(), Op0C, Op1); + Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); Instruction *NewShift = BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1); @@ -7632,19 +7640,19 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, // saturates. if (AmtSum >= TypeBits) { if (I.getOpcode() != Instruction::AShr) - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr. } return BinaryOperator::Create(I.getOpcode(), X, - Context->getConstantInt(Ty, AmtSum)); + ConstantInt::get(Ty, AmtSum)); } else if (ShiftOp->getOpcode() == Instruction::LShr && I.getOpcode() == Instruction::AShr) { if (AmtSum >= TypeBits) - return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); + return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. - return BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, AmtSum)); + return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum)); } else if (ShiftOp->getOpcode() == Instruction::AShr && I.getOpcode() == Instruction::LShr) { // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. @@ -7652,11 +7660,11 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, AmtSum = TypeBits-1; Instruction *Shift = - BinaryOperator::CreateAShr(X, Context->getConstantInt(Ty, AmtSum)); + BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum)); InsertNewInstBefore(Shift, I); APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); - return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); + return BinaryOperator::CreateAnd(Shift, ConstantInt::get(*Context, Mask)); } // Okay, if we get here, one shift must be left, and the other shift must be @@ -7665,12 +7673,12 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, // If we have ((X >>? C) << C), turn this into X & (-1 << C). if (I.getOpcode() == Instruction::Shl) { APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); - return BinaryOperator::CreateAnd(X, Context->getConstantInt(Mask)); + return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask)); } // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). if (I.getOpcode() == Instruction::LShr) { APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); - return BinaryOperator::CreateAnd(X, Context->getConstantInt(Mask)); + return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask)); } // We can simplify ((X << C) >>s C) into a trunc + sext. // NOTE: we could do this for any C, but that would make 'unusual' integer @@ -7684,7 +7692,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, case 32 : case 64 : case 128: - SExtType = Context->getIntegerType(Ty->getBitWidth() - ShiftAmt1); + SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); break; default: break; } @@ -7702,22 +7710,24 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, assert(ShiftOp->getOpcode() == Instruction::LShr || ShiftOp->getOpcode() == Instruction::AShr); Instruction *Shift = - BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff)); + BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); InsertNewInstBefore(Shift, I); APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); - return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); + return BinaryOperator::CreateAnd(Shift, + ConstantInt::get(*Context, Mask)); } // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) if (I.getOpcode() == Instruction::LShr) { assert(ShiftOp->getOpcode() == Instruction::Shl); Instruction *Shift = - BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, ShiftDiff)); + BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff)); InsertNewInstBefore(Shift, I); APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); - return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); + return BinaryOperator::CreateAnd(Shift, + ConstantInt::get(*Context, Mask)); } // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. @@ -7731,22 +7741,24 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, ShiftOp->getOpcode() == Instruction::AShr); Instruction *Shift = BinaryOperator::Create(ShiftOp->getOpcode(), X, - Context->getConstantInt(Ty, ShiftDiff)); + ConstantInt::get(Ty, ShiftDiff)); InsertNewInstBefore(Shift, I); APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); - return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); + return BinaryOperator::CreateAnd(Shift, + ConstantInt::get(*Context, Mask)); } // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) if (I.getOpcode() == Instruction::LShr) { assert(ShiftOp->getOpcode() == Instruction::Shl); Instruction *Shift = - BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff)); + BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff)); InsertNewInstBefore(Shift, I); APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); - return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); + return BinaryOperator::CreateAnd(Shift, + ConstantInt::get(*Context, Mask)); } // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in. @@ -7766,7 +7778,7 @@ static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, if (ConstantInt *CI = dyn_cast(Val)) { Offset = CI->getZExtValue(); Scale = 0; - return Context->getConstantInt(Type::Int32Ty, 0); + return ConstantInt::get(Type::Int32Ty, 0); } else if (BinaryOperator *I = dyn_cast(Val)) { if (ConstantInt *RHS = dyn_cast(I->getOperand(1))) { if (I->getOpcode() == Instruction::Shl) { @@ -7816,11 +7828,14 @@ Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, ++UI; // If this instruction uses AI more than once, don't break UI. ++NumDeadInst; - DOUT << "IC: DCE: " << *User; + DOUT << "IC: DCE: " << *User << '\n'; EraseInstFromFunction(*User); } } - + + // This requires TargetData to get the alloca alignment and size information. + if (!TD) return 0; + // Get the type really allocated and the type casted to. const Type *AllocElTy = AI.getAllocatedType(); const Type *CastElTy = PTy->getElementType(); @@ -7860,9 +7875,9 @@ Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, Amt = NumElements; } else { // If the allocation size is constant, form a constant mul expression - Amt = Context->getConstantInt(Type::Int32Ty, Scale); + Amt = ConstantInt::get(Type::Int32Ty, Scale); if (isa(NumElements)) - Amt = Context->getConstantExprMul(cast(NumElements), + Amt = ConstantExpr::getMul(cast(NumElements), cast(Amt)); // otherwise multiply the amount and the number of elements else { @@ -7872,7 +7887,7 @@ Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, } if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { - Value *Off = Context->getConstantInt(Type::Int32Ty, Offset, true); + Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp"); Amt = InsertNewInstBefore(Tmp, AI); } @@ -7966,6 +7981,23 @@ bool InstCombiner::CanEvaluateInDifferentType(Value *V, const Type *Ty, CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, NumCastsRemoved); + case Instruction::UDiv: + case Instruction::URem: { + // UDiv and URem can be truncated if all the truncated bits are zero. + uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); + uint32_t BitWidth = Ty->getScalarSizeInBits(); + if (BitWidth < OrigBitWidth) { + APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth); + if (MaskedValueIsZero(I->getOperand(0), Mask) && + MaskedValueIsZero(I->getOperand(1), Mask)) { + return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, + NumCastsRemoved) && + CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, + NumCastsRemoved); + } + } + break; + } case Instruction::Shl: // If we are truncating the result of this SHL, and if it's a shift of a // constant amount, we can always perform a SHL in a smaller type. @@ -8036,7 +8068,7 @@ bool InstCombiner::CanEvaluateInDifferentType(Value *V, const Type *Ty, Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned) { if (Constant *C = dyn_cast(V)) - return Context->getConstantExprIntegerCast(C, Ty, + return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); // Otherwise, it must be an instruction. @@ -8052,7 +8084,9 @@ Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, case Instruction::Xor: case Instruction::AShr: case Instruction::LShr: - case Instruction::Shl: { + case Instruction::Shl: + case Instruction::UDiv: + case Instruction::URem: { Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); @@ -8089,7 +8123,7 @@ Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, } default: // TODO: Can handle more cases here. - LLVM_UNREACHABLE("Unreachable!"); + llvm_unreachable("Unreachable!"); break; } @@ -8133,6 +8167,7 @@ static const Type *FindElementAtOffset(const Type *Ty, int64_t Offset, SmallVectorImpl &NewIndices, const TargetData *TD, LLVMContext *Context) { + if (!TD) return 0; if (!Ty->isSized()) return 0; // Start with the index over the outer type. Note that the type size @@ -8153,7 +8188,7 @@ static const Type *FindElementAtOffset(const Type *Ty, int64_t Offset, assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset"); } - NewIndices.push_back(Context->getConstantInt(IntPtrTy, FirstIdx)); + NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx)); // Index into the types. If we fail, set OrigBase to null. while (Offset) { @@ -8167,14 +8202,14 @@ static const Type *FindElementAtOffset(const Type *Ty, int64_t Offset, "Offset must stay within the indexed type"); unsigned Elt = SL->getElementContainingOffset(Offset); - NewIndices.push_back(Context->getConstantInt(Type::Int32Ty, Elt)); + NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); Offset -= SL->getElementOffset(Elt); Ty = STy->getElementType(Elt); } else if (const ArrayType *AT = dyn_cast(Ty)) { uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType()); assert(EltSize && "Cannot index into a zero-sized array"); - NewIndices.push_back(Context->getConstantInt(IntPtrTy,Offset/EltSize)); + NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); Offset %= EltSize; Ty = AT->getElementType(); } else { @@ -8206,7 +8241,7 @@ Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { // GEP computes a constant offset, see if we can convert these three // instructions into fewer. This typically happens with unions and other // non-type-safe code. - if (GEP->hasOneUse() && isa(GEP->getOperand(0))) { + if (TD && GEP->hasOneUse() && isa(GEP->getOperand(0))) { if (GEP->hasAllConstantIndices()) { // We are guaranteed to get a constant from EmitGEPOffset. ConstantInt *OffsetV = @@ -8227,6 +8262,8 @@ Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { NewIndices.end(), ""); InsertNewInstBefore(NGEP, CI); NGEP->takeName(GEP); + if (cast(GEP)->isInBounds()) + cast(NGEP)->setIsInBounds(true); if (isa(CI)) return new BitCastInst(NGEP, CI.getType()); @@ -8257,9 +8294,8 @@ static bool isSafeIntegerType(const Type *Ty) { } } -/// Only the TRUNC, ZEXT, SEXT. This function implements the common transforms -/// for all those cases. -/// @brief Implement the transforms common to CastInst with integer operands +/// commonIntCastTransforms - This function implements the common transforms +/// for trunc, zext, and sext. Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { if (Instruction *Result = commonCastTransforms(CI)) return Result; @@ -8283,11 +8319,10 @@ Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { // Attempt to propagate the cast into the instruction for int->int casts. int NumCastsRemoved = 0; - if (!isa(CI) && - // Only do this if the dest type is a simple type, don't convert the - // expression tree to something weird like i93 unless the source is also - // strange. - (isSafeIntegerType(DestTy->getScalarType()) || + // Only do this if the dest type is a simple type, don't convert the + // expression tree to something weird like i93 unless the source is also + // strange. + if ((isSafeIntegerType(DestTy->getScalarType()) || !isSafeIntegerType(SrcI->getType()->getScalarType())) && CanEvaluateInDifferentType(SrcI, DestTy, CI.getOpcode(), NumCastsRemoved)) { @@ -8303,7 +8338,7 @@ Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { default: // All the others use floating point so we shouldn't actually // get here because of the check above. - LLVM_UNREACHABLE("Unknown cast type"); + llvm_unreachable("Unknown cast type"); case Instruction::Trunc: DoXForm = true; break; @@ -8359,9 +8394,8 @@ Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { assert(Res->getType() == DestTy); switch (CI.getOpcode()) { - default: LLVM_UNREACHABLE("Unknown cast type!"); + default: llvm_unreachable("Unknown cast type!"); case Instruction::Trunc: - case Instruction::BitCast: // Just replace this cast with the result. return ReplaceInstUsesWith(CI, Res); case Instruction::ZExt: { @@ -8374,8 +8408,8 @@ Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { return ReplaceInstUsesWith(CI, Res); // We need to emit an AND to clear the high bits. - Constant *C = Context->getConstantInt(APInt::getLowBitsSet(DestBitSize, - SrcBitSize)); + Constant *C = ConstantInt::get(*Context, + APInt::getLowBitsSet(DestBitSize, SrcBitSize)); return BinaryOperator::CreateAnd(Res, C); } case Instruction::SExt: { @@ -8404,16 +8438,12 @@ Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { case Instruction::Or: case Instruction::Xor: // If we are discarding information, rewrite. - if (DestBitSize <= SrcBitSize && DestBitSize != 1) { - // Don't insert two casts if they cannot be eliminated. We allow - // two casts to be inserted if the sizes are the same. This could - // only be converting signedness, which is a noop. - if (DestBitSize == SrcBitSize || - !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || + if (DestBitSize < SrcBitSize && DestBitSize != 1) { + // Don't insert two casts unless at least one can be eliminated. + if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { - Instruction::CastOps opcode = CI.getOpcode(); - Value *Op0c = InsertCastBefore(opcode, Op0, DestTy, *SrcI); - Value *Op1c = InsertCastBefore(opcode, Op1, DestTy, *SrcI); + Value *Op0c = InsertCastBefore(Instruction::Trunc, Op0, DestTy, *SrcI); + Value *Op1c = InsertCastBefore(Instruction::Trunc, Op1, DestTy, *SrcI); return BinaryOperator::Create( cast(SrcI)->getOpcode(), Op0c, Op1c); } @@ -8422,62 +8452,25 @@ Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 if (isa(CI) && SrcBitSize == 1 && SrcI->getOpcode() == Instruction::Xor && - Op1 == Context->getConstantIntTrue() && + Op1 == ConstantInt::getTrue(*Context) && (!Op0->hasOneUse() || !isa(Op0))) { Value *New = InsertCastBefore(Instruction::ZExt, Op0, DestTy, CI); return BinaryOperator::CreateXor(New, - Context->getConstantInt(CI.getType(), 1)); - } - break; - case Instruction::SDiv: - case Instruction::UDiv: - case Instruction::SRem: - case Instruction::URem: - // If we are just changing the sign, rewrite. - if (DestBitSize == SrcBitSize) { - // Don't insert two casts if they cannot be eliminated. We allow - // two casts to be inserted if the sizes are the same. This could - // only be converting signedness, which is a noop. - if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || - !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { - Value *Op0c = InsertCastBefore(Instruction::BitCast, - Op0, DestTy, *SrcI); - Value *Op1c = InsertCastBefore(Instruction::BitCast, - Op1, DestTy, *SrcI); - return BinaryOperator::Create( - cast(SrcI)->getOpcode(), Op0c, Op1c); - } + ConstantInt::get(CI.getType(), 1)); } break; - case Instruction::Shl: - // Allow changing the sign of the source operand. Do not allow - // changing the size of the shift, UNLESS the shift amount is a - // constant. We must not change variable sized shifts to a smaller - // size, because it is undefined to shift more bits out than exist - // in the value. - if (DestBitSize == SrcBitSize || - (DestBitSize < SrcBitSize && isa(Op1))) { - Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? - Instruction::BitCast : Instruction::Trunc); - Value *Op0c = InsertCastBefore(opcode, Op0, DestTy, *SrcI); - Value *Op1c = InsertCastBefore(opcode, Op1, DestTy, *SrcI); + case Instruction::Shl: { + // Canonicalize trunc inside shl, if we can. + ConstantInt *CI = dyn_cast(Op1); + if (CI && DestBitSize < SrcBitSize && + CI->getLimitedValue(DestBitSize) < DestBitSize) { + Value *Op0c = InsertCastBefore(Instruction::Trunc, Op0, DestTy, *SrcI); + Value *Op1c = InsertCastBefore(Instruction::Trunc, Op1, DestTy, *SrcI); return BinaryOperator::CreateShl(Op0c, Op1c); } break; - case Instruction::AShr: - // If this is a signed shr, and if all bits shifted in are about to be - // truncated off, turn it into an unsigned shr to allow greater - // simplifications. - if (DestBitSize < SrcBitSize && - isa(Op1)) { - uint32_t ShiftAmt = cast(Op1)->getLimitedValue(SrcBitSize); - if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { - // Insert the new logical shift right. - return BinaryOperator::CreateLShr(Op0, Op1); - } - } - break; + } } return 0; } @@ -8492,11 +8485,10 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) { uint32_t SrcBitWidth = Src->getType()->getScalarSizeInBits(); // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0) - if (DestBitWidth == 1 && - isa(Ty) == isa(Src->getType())) { - Constant *One = Context->getConstantInt(Src->getType(), 1); + if (DestBitWidth == 1) { + Constant *One = ConstantInt::get(Src->getType(), 1); Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI); - Value *Zero = Context->getNullValue(Src->getType()); + Value *Zero = Constant::getNullValue(Src->getType()); return new ICmpInst(*Context, ICmpInst::ICMP_NE, Src, Zero); } @@ -8504,19 +8496,19 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) { 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. APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); if (MaskedValueIsZero(ShiftOp, Mask)) { if (ShAmt >= DestBitWidth) // All zeros. - return ReplaceInstUsesWith(CI, Context->getNullValue(Ty)); + return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); // Okay, we can shrink this. Truncate the input, then return a new // shift. Value *V1 = InsertCastBefore(Instruction::Trunc, ShiftOp, Ty, CI); - Value *V2 = Context->getConstantExprTrunc(ShAmtV, Ty); + Value *V2 = ConstantExpr::getTrunc(ShAmtV, Ty); return BinaryOperator::CreateLShr(V1, V2); } } @@ -8541,7 +8533,7 @@ Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, if (!DoXform) return ICI; Value *In = ICI->getOperand(0); - Value *Sh = Context->getConstantInt(In->getType(), + Value *Sh = ConstantInt::get(In->getType(), In->getType()->getScalarSizeInBits()-1); In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh, In->getName()+".lobit"), @@ -8551,7 +8543,7 @@ Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, false/*ZExt*/, "tmp", &CI); if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { - Constant *One = Context->getConstantInt(In->getType(), 1); + Constant *One = ConstantInt::get(In->getType(), 1); In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One, In->getName()+".not"), CI); @@ -8587,8 +8579,8 @@ Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { // (X&4) == 2 --> false // (X&4) != 2 --> true - Constant *Res = Context->getConstantInt(Type::Int1Ty, isNE); - Res = Context->getConstantExprZExt(Res, CI.getType()); + Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); + Res = ConstantExpr::getZExt(Res, CI.getType()); return ReplaceInstUsesWith(CI, Res); } @@ -8598,12 +8590,12 @@ Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, // Perform a logical shr by shiftamt. // Insert the shift to put the result in the low bit. In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, - Context->getConstantInt(In->getType(), ShiftAmt), + ConstantInt::get(In->getType(), ShiftAmt), In->getName()+".lobit"), CI); } if ((Op1CV != 0) == isNE) { // Toggle the low bit. - Constant *One = Context->getConstantInt(In->getType(), 1); + Constant *One = ConstantInt::get(In->getType(), 1); In = BinaryOperator::CreateXor(In, One, "tmp"); InsertNewInstBefore(cast(In), CI); } @@ -8642,21 +8634,21 @@ Instruction *InstCombiner::visitZExt(ZExtInst &CI) { // SrcSize > DstSize: trunc(a) & mask if (SrcSize < DstSize) { APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); - Constant *AndConst = Context->getConstantInt(A->getType(), AndValue); + Constant *AndConst = ConstantInt::get(A->getType(), AndValue); Instruction *And = BinaryOperator::CreateAnd(A, AndConst, CSrc->getName()+".mask"); InsertNewInstBefore(And, CI); return new ZExtInst(And, CI.getType()); } else if (SrcSize == DstSize) { APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); - return BinaryOperator::CreateAnd(A, Context->getConstantInt(A->getType(), + return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(), AndValue)); } else if (SrcSize > DstSize) { Instruction *Trunc = new TruncInst(A, CI.getType(), "tmp"); InsertNewInstBefore(Trunc, CI); APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); return BinaryOperator::CreateAnd(Trunc, - Context->getConstantInt(Trunc->getType(), + ConstantInt::get(Trunc->getType(), AndValue)); } } @@ -8687,7 +8679,7 @@ Instruction *InstCombiner::visitZExt(ZExtInst &CI) { if (TI0->getType() == CI.getType()) return BinaryOperator::CreateAnd(TI0, - Context->getConstantExprZExt(C, CI.getType())); + ConstantExpr::getZExt(C, CI.getType())); } // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)). @@ -8699,7 +8691,7 @@ Instruction *InstCombiner::visitZExt(ZExtInst &CI) { if (TruncInst *TI = dyn_cast(And->getOperand(0))) { Value *TI0 = TI->getOperand(0); if (TI0->getType() == CI.getType()) { - Constant *ZC = Context->getConstantExprZExt(C, CI.getType()); + Constant *ZC = ConstantExpr::getZExt(C, CI.getType()); Instruction *NewAnd = BinaryOperator::CreateAnd(TI0, ZC, "tmp"); InsertNewInstBefore(NewAnd, *And); return BinaryOperator::CreateXor(NewAnd, ZC); @@ -8718,12 +8710,12 @@ Instruction *InstCombiner::visitSExt(SExtInst &CI) { // Canonicalize sign-extend from i1 to a select. if (Src->getType() == Type::Int1Ty) return SelectInst::Create(Src, - Context->getConstantIntAllOnesValue(CI.getType()), - Context->getNullValue(CI.getType())); + Constant::getAllOnesValue(CI.getType()), + Constant::getNullValue(CI.getType())); // See if the value being truncated is already sign extended. If so, just // eliminate the trunc/sext pair. - if (getOpcode(Src) == Instruction::Trunc) { + if (Operator::getOpcode(Src) == Instruction::Trunc) { Value *Op = cast(Src)->getOperand(0); unsigned OpBits = Op->getType()->getScalarSizeInBits(); unsigned MidBits = Src->getType()->getScalarSizeInBits(); @@ -8764,14 +8756,14 @@ Instruction *InstCombiner::visitSExt(SExtInst &CI) { 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(A)) { Value *I = cast(A)->getOperand(0); if (I->getType() == CI.getType()) { unsigned MidSize = Src->getType()->getScalarSizeInBits(); unsigned SrcDstSize = CI.getType()->getScalarSizeInBits(); unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize; - Constant *ShAmtV = Context->getConstantInt(CI.getType(), ShAmt); + Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt); I = InsertNewInstBefore(BinaryOperator::CreateShl(I, ShAmtV, CI.getName()), CI); return BinaryOperator::CreateAShr(I, ShAmtV); @@ -8789,7 +8781,7 @@ static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem, APFloat F = CFP->getValueAPF(); (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo); if (!losesInfo) - return Context->getConstantFP(F); + return ConstantFP::get(*Context, F); return 0; } @@ -8917,7 +8909,8 @@ Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) { // trunc to be exposed to other transforms. Don't do this for extending // ptrtoint's, because we don't know if the target sign or zero extends its // pointers. - if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) { + if (TD && + CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) { Value *P = InsertNewInstBefore(new PtrToIntInst(CI.getOperand(0), TD->getIntPtrType(), "tmp"), CI); @@ -8933,7 +8926,8 @@ Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { // allows the trunc to be exposed to other transforms. Don't do this for // extending inttoptr's, because we don't know if the target sign or zero // extends to pointers. - if (CI.getOperand(0)->getType()->getScalarSizeInBits() > + if (TD && + CI.getOperand(0)->getType()->getScalarSizeInBits() > TD->getPointerSizeInBits()) { Value *P = InsertNewInstBefore(new TruncInst(CI.getOperand(0), TD->getIntPtrType(), @@ -8943,56 +8937,7 @@ Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { if (Instruction *I = commonCastTransforms(CI)) return I; - - const Type *DestPointee = cast(CI.getType())->getElementType(); - if (!DestPointee->isSized()) return 0; - - // If this is inttoptr(add (ptrtoint x), cst), try to turn this into a GEP. - ConstantInt *Cst; - Value *X; - if (match(CI.getOperand(0), m_Add(m_Cast(m_Value(X)), - m_ConstantInt(Cst)), *Context)) { - // If the source and destination operands have the same type, see if this - // is a single-index GEP. - if (X->getType() == CI.getType()) { - // Get the size of the pointee type. - uint64_t Size = TD->getTypeAllocSize(DestPointee); - - // Convert the constant to intptr type. - APInt Offset = Cst->getValue(); - Offset.sextOrTrunc(TD->getPointerSizeInBits()); - - // If Offset is evenly divisible by Size, we can do this xform. - if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ - Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); - return GetElementPtrInst::Create(X, Context->getConstantInt(Offset)); - } - } - // TODO: Could handle other cases, e.g. where add is indexing into field of - // struct etc. - } else if (CI.getOperand(0)->hasOneUse() && - match(CI.getOperand(0), m_Add(m_Value(X), - m_ConstantInt(Cst)), *Context)) { - // Otherwise, if this is inttoptr(add x, cst), try to turn this into an - // "inttoptr+GEP" instead of "add+intptr". - - // Get the size of the pointee type. - uint64_t Size = TD->getTypeAllocSize(DestPointee); - - // Convert the constant to intptr type. - APInt Offset = Cst->getValue(); - Offset.sextOrTrunc(TD->getPointerSizeInBits()); - - // If Offset is evenly divisible by Size, we can do this xform. - if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){ - Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size)); - - Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(), - "tmp"), CI); - return GetElementPtrInst::Create(P, - Context->getConstantInt(Offset), "tmp"); - } - } + return 0; } @@ -9036,7 +8981,7 @@ Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { // 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 = Context->getNullValue(Type::Int32Ty); + Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); unsigned NumZeros = 0; while (SrcElTy != DstElTy && isa(SrcElTy) && !isa(SrcElTy) && @@ -9048,8 +8993,34 @@ Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { // If we found a path from the src to dest, create the getelementptr now. if (SrcElTy == DstElTy) { SmallVector Idxs(NumZeros+1, ZeroUInt); - return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", - ((Instruction*) NULL)); + Instruction *GEP = GetElementPtrInst::Create(Src, + Idxs.begin(), Idxs.end(), "", + ((Instruction*) NULL)); + cast(GEP)->setIsInBounds(true); + return GEP; + } + } + + if (const VectorType *DestVTy = dyn_cast(DestTy)) { + if (DestVTy->getNumElements() == 1) { + if (!isa(SrcTy)) { + Value *Elem = InsertCastBefore(Instruction::BitCast, Src, + DestVTy->getElementType(), CI); + return InsertElementInst::Create(UndefValue::get(DestTy), Elem, + Constant::getNullValue(Type::Int32Ty)); + } + // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast) + } + } + + if (const VectorType *SrcVTy = dyn_cast(SrcTy)) { + if (SrcVTy->getNumElements() == 1) { + if (!isa(DestTy)) { + Instruction *Elem = + ExtractElementInst::Create(Src, Constant::getNullValue(Type::Int32Ty)); + InsertNewInstBefore(Elem, CI); + return CastInst::Create(Instruction::BitCast, Elem, DestTy); + } } } @@ -9118,7 +9089,7 @@ static unsigned GetSelectFoldableOperands(Instruction *I) { static Constant *GetSelectFoldableConstant(Instruction *I, LLVMContext *Context) { switch (I->getOpcode()) { - default: LLVM_UNREACHABLE("This cannot happen!"); + default: llvm_unreachable("This cannot happen!"); case Instruction::Add: case Instruction::Sub: case Instruction::Or: @@ -9126,11 +9097,11 @@ static Constant *GetSelectFoldableConstant(Instruction *I, case Instruction::Shl: case Instruction::LShr: case Instruction::AShr: - return Context->getNullValue(I->getType()); + return Constant::getNullValue(I->getType()); case Instruction::And: - return Context->getAllOnesValue(I->getType()); + return Constant::getAllOnesValue(I->getType()); case Instruction::Mul: - return Context->getConstantInt(I->getType(), 1); + return ConstantInt::get(I->getType(), 1); } } @@ -9150,7 +9121,7 @@ Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, // Fold this by inserting a select from the input values. SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), - FI->getOperand(0), SI.getName()+".v"); + FI->getOperand(0), SI.getName()+".v"); InsertNewInstBefore(NewSI, SI); return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, TI->getType()); @@ -9200,7 +9171,7 @@ Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, else return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp); } - LLVM_UNREACHABLE("Shouldn't get here"); + llvm_unreachable("Shouldn't get here"); return 0; } @@ -9242,7 +9213,7 @@ Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal, NewSel->takeName(TVI); if (BinaryOperator *BO = dyn_cast(TVI)) return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel); - LLVM_UNREACHABLE("Unknown instruction!!"); + llvm_unreachable("Unknown instruction!!"); } } } @@ -9271,7 +9242,7 @@ Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal, NewSel->takeName(FVI); if (BinaryOperator *BO = dyn_cast(FVI)) return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel); - LLVM_UNREACHABLE("Unknown instruction!!"); + llvm_unreachable("Unknown instruction!!"); } } } @@ -9306,7 +9277,7 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, 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); @@ -9326,7 +9297,7 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, 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); @@ -9345,11 +9316,11 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, // (x 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) { @@ -9363,10 +9334,10 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) || (Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) { Value *In = ICI->getOperand(0); - Value *Sh = Context->getConstantInt(In->getType(), + Value *Sh = ConstantInt::get(In->getType(), In->getType()->getScalarSizeInBits()-1); In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh, - In->getName()+".lobit"), + In->getName()+".lobit"), *ICI); if (In->getType() != SI.getType()) In = CastInst::CreateIntegerCast(In, SI.getType(), @@ -9588,10 +9559,10 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { // select C, (add X, Y), (sub X, Z) Value *NegVal; // Compute -Z if (Constant *C = dyn_cast(SubOp->getOperand(1))) { - NegVal = Context->getConstantExprNeg(C); + NegVal = ConstantExpr::getNeg(C); } else { NegVal = InsertNewInstBefore( - BinaryOperator::CreateNeg(*Context, SubOp->getOperand(1), + BinaryOperator::CreateNeg(SubOp->getOperand(1), "tmp"), SI); } @@ -9638,7 +9609,7 @@ static unsigned EnforceKnownAlignment(Value *V, User *U = dyn_cast(V); if (!U) return Align; - switch (getOpcode(U)) { + switch (Operator::getOpcode(U)) { default: break; case Instruction::BitCast: return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); @@ -9715,7 +9686,7 @@ Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { unsigned CopyAlign = MI->getAlignment(); if (CopyAlign < MinAlign) { - MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(), + MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), MinAlign, false)); return MI; } @@ -9737,7 +9708,7 @@ Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { // Use an integer load+store unless we can find something better. Type *NewPtrTy = - Context->getPointerTypeUnqual(Context->getIntegerType(Size<<3)); + PointerType::getUnqual(IntegerType::get(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 @@ -9747,7 +9718,7 @@ Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { // integer datatype. if (Value *Op = getBitCastOperand(MI->getOperand(1))) { const Type *SrcETy = cast(Op->getType())->getElementType(); - if (SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { + if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { // The SrcETy might be something like {{{double}}} or [1 x double]. Rip // down through these levels if so. while (!SrcETy->isSingleValueType()) { @@ -9766,7 +9737,7 @@ Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { } if (SrcETy->isSingleValueType()) - NewPtrTy = Context->getPointerTypeUnqual(SrcETy); + NewPtrTy = PointerType::getUnqual(SrcETy); } } @@ -9783,14 +9754,14 @@ Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); // Set the size of the copy to 0, it will be deleted on the next iteration. - MI->setOperand(3, Context->getNullValue(MemOpLength->getType())); + MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); return MI; } Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); if (MI->getAlignment() < Alignment) { - MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(), + MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), Alignment, false)); return MI; } @@ -9808,21 +9779,21 @@ Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { // memset(s,c,n) -> store s, c (for n=1,2,4,8) if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { - const Type *ITy = Context->getIntegerType(Len*8); // n=1 -> i8. + const Type *ITy = IntegerType::get(Len*8); // n=1 -> i8. Value *Dest = MI->getDest(); - Dest = InsertBitCastBefore(Dest, Context->getPointerTypeUnqual(ITy), *MI); + Dest = InsertBitCastBefore(Dest, PointerType::getUnqual(ITy), *MI); // Alignment 0 is identity for alignment 1 for memset, but not store. if (Alignment == 0) Alignment = 1; // Extract the fill value and store. uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; - InsertNewInstBefore(new StoreInst(Context->getConstantInt(ITy, Fill), + InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), Dest, false, Alignment), *MI); // Set the size of the copy to 0, it will be deleted on the next iteration. - MI->setLength(Context->getNullValue(LenC->getType())); + MI->setLength(Constant::getNullValue(LenC->getType())); return MI; } @@ -9915,7 +9886,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { // Turn X86 loadups -> load if the pointer is known aligned. if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { Value *Ptr = InsertBitCastBefore(II->getOperand(1), - Context->getPointerTypeUnqual(II->getType()), + PointerType::getUnqual(II->getType()), CI); return new LoadInst(Ptr); } @@ -9925,7 +9896,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { // Turn stvx -> store if the pointer is known aligned. if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { const Type *OpPtrTy = - Context->getPointerTypeUnqual(II->getOperand(1)->getType()); + PointerType::getUnqual(II->getOperand(1)->getType()); Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); return new StoreInst(II->getOperand(1), Ptr); } @@ -9936,7 +9907,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { // Turn X86 storeu -> store if the pointer is known aligned. if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { const Type *OpPtrTy = - Context->getPointerTypeUnqual(II->getOperand(2)->getType()); + PointerType::getUnqual(II->getOperand(2)->getType()); Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); return new StoreInst(II->getOperand(2), Ptr); } @@ -9976,7 +9947,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { // Cast the input vectors to byte vectors. Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); - Value *Result = Context->getUndef(Op0->getType()); + Value *Result = UndefValue::get(Op0->getType()); // Only extract each element once. Value *ExtractedElts[32]; @@ -9990,14 +9961,16 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { if (ExtractedElts[Idx] == 0) { Instruction *Elt = - new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); + ExtractElementInst::Create(Idx < 16 ? Op0 : Op1, + ConstantInt::get(Type::Int32Ty, Idx&15, false), "tmp"); InsertNewInstBefore(Elt, CI); ExtractedElts[Idx] = Elt; } // Insert this value into the result vector. Result = InsertElementInst::Create(Result, ExtractedElts[Idx], - i, "tmp"); + ConstantInt::get(Type::Int32Ty, i, false), + "tmp"); InsertNewInstBefore(cast(Result), CI); } return CastInst::Create(Instruction::BitCast, Result, CI.getType()); @@ -10078,7 +10051,7 @@ static bool isSafeToEliminateVarargsCast(const CallSite CS, const Type* DstTy = cast(CI->getType())->getElementType(); if (!SrcTy->isSized() || !DstTy->isSized()) return false; - if (TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy)) + if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy)) return false; return true; } @@ -10099,11 +10072,11 @@ Instruction *InstCombiner::visitCallSite(CallSite CS) { Instruction *OldCall = CS.getInstruction(); // If the call and callee calling conventions don't match, this call must // be unreachable, as the call is undefined. - new StoreInst(Context->getConstantIntTrue(), - Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)), + new StoreInst(ConstantInt::getTrue(*Context), + UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), OldCall); if (!OldCall->use_empty()) - OldCall->replaceAllUsesWith(Context->getUndef(OldCall->getType())); + OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); if (isa(OldCall)) // Not worth removing an invoke here. return EraseInstFromFunction(*OldCall); return 0; @@ -10113,18 +10086,18 @@ Instruction *InstCombiner::visitCallSite(CallSite CS) { // This instruction is not reachable, just remove it. We insert a store to // undef so that we know that this code is not reachable, despite the fact // that we can't modify the CFG here. - new StoreInst(Context->getConstantIntTrue(), - Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)), + new StoreInst(ConstantInt::getTrue(*Context), + UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), CS.getInstruction()); if (!CS.getInstruction()->use_empty()) CS.getInstruction()-> - replaceAllUsesWith(Context->getUndef(CS.getInstruction()->getType())); + replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); if (InvokeInst *II = dyn_cast(CS.getInstruction())) { // Don't break the CFG, insert a dummy cond branch. BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), - Context->getConstantIntTrue(), II); + ConstantInt::getTrue(*Context), II); } return EraseInstFromFunction(*CS.getInstruction()); } @@ -10188,8 +10161,10 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { if (Callee->isDeclaration() && // Conversion is ok if changing from one pointer type to another or from // a pointer to an integer of the same size. - !((isa(OldRetTy) || OldRetTy == TD->getIntPtrType()) && - (isa(NewRetTy) || NewRetTy == TD->getIntPtrType()))) + !((isa(OldRetTy) || !TD || + OldRetTy == TD->getIntPtrType()) && + (isa(NewRetTy) || !TD || + NewRetTy == TD->getIntPtrType()))) return false; // Cannot transform this return value. if (!Caller->use_empty() && @@ -10235,8 +10210,8 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { // 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 || - ((isa(ParamTy) || ParamTy == TD->getIntPtrType()) && - (isa(ActTy) || ActTy == TD->getIntPtrType())); + (TD && ((isa(ParamTy) || ParamTy == TD->getIntPtrType()) && + (isa(ActTy) || ActTy == TD->getIntPtrType()))); if (Callee->isDeclaration() && !isConvertible) return false; } @@ -10295,13 +10270,13 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { // If the function takes more arguments than the call was taking, add them // now... for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) - Args.push_back(Context->getNullValue(FT->getParamType(i))); + Args.push_back(Constant::getNullValue(FT->getParamType(i))); // If we are removing arguments to the function, emit an obnoxious warning... if (FT->getNumParams() < NumActualArgs) { if (!FT->isVarArg()) { - cerr << "WARNING: While resolving call to function '" - << Callee->getName() << "' arguments were dropped!\n"; + errs() << "WARNING: While resolving call to function '" + << Callee->getName() << "' arguments were dropped!\n"; } else { // Add all of the arguments in their promoted form to the arg list... for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { @@ -10330,7 +10305,8 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { if (NewRetTy == Type::VoidTy) Caller->setName(""); // Void type should not have a name. - const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),attrVec.end()); + const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(), + attrVec.end()); Instruction *NC; if (InvokeInst *II = dyn_cast(Caller)) { @@ -10368,7 +10344,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { } AddUsersToWorkList(*Caller); } else { - NV = Context->getUndef(Caller->getType()); + NV = UndefValue::get(Caller->getType()); } } @@ -10492,14 +10468,14 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { // Replace the trampoline call with a direct call. Let the generic // code sort out any function type mismatches. - FunctionType *NewFTy = - Context->getFunctionType(FTy->getReturnType(), NewTypes, + FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg()); Constant *NewCallee = - NestF->getType() == Context->getPointerTypeUnqual(NewFTy) ? - NestF : Context->getConstantExprBitCast(NestF, - Context->getPointerTypeUnqual(NewFTy)); - const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),NewAttrs.end()); + NestF->getType() == PointerType::getUnqual(NewFTy) ? + NestF : ConstantExpr::getBitCast(NestF, + PointerType::getUnqual(NewFTy)); + const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(), + NewAttrs.end()); Instruction *NewCaller; if (InvokeInst *II = dyn_cast(Caller)) { @@ -10531,7 +10507,7 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { // code sort out any function type mismatches. Constant *NewCallee = NestF->getType() == PTy ? NestF : - Context->getConstantExprBitCast(NestF, PTy); + ConstantExpr::getBitCast(NestF, PTy); CS.setCalledFunction(NewCallee); return CS.getInstruction(); } @@ -10701,8 +10677,12 @@ Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) { } Value *Base = FixedOperands[0]; - return GetElementPtrInst::Create(Base, FixedOperands.begin()+1, - FixedOperands.end()); + GetElementPtrInst *GEP = + GetElementPtrInst::Create(Base, FixedOperands.begin()+1, + FixedOperands.end()); + if (cast(FirstInst)->isInBounds()) + cast(GEP)->setIsInBounds(true); + return GEP; } @@ -10952,7 +10932,7 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) { SmallPtrSet PotentiallyDeadPHIs; PotentiallyDeadPHIs.insert(&PN); if (DeadPHICycle(PU, PotentiallyDeadPHIs)) - return ReplaceInstUsesWith(PN, Context->getUndef(PN.getType())); + return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); } // If this phi has a single use, and if that use just computes a value for @@ -10964,7 +10944,7 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) { if (PHIUser->hasOneUse() && (isa(PHIUser) || isa(PHIUser)) && PHIUser->use_back() == &PN) { - return ReplaceInstUsesWith(PN, Context->getUndef(PN.getType())); + return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); } } @@ -11028,7 +11008,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { return ReplaceInstUsesWith(GEP, PtrOp); if (isa(GEP.getOperand(0))) - return ReplaceInstUsesWith(GEP, Context->getUndef(GEP.getType())); + return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); bool HasZeroPointerIndex = false; if (Constant *C = dyn_cast(GEP.getOperand(1))) @@ -11043,7 +11023,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { gep_type_iterator GTI = gep_type_begin(GEP); for (User::op_iterator i = GEP.op_begin() + 1, e = GEP.op_end(); i != e; ++i, ++GTI) { - if (isa(*GTI)) { + if (TD && isa(*GTI)) { if (CastInst *CI = dyn_cast(*i)) { if (CI->getOpcode() == Instruction::ZExt || CI->getOpcode() == Instruction::SExt) { @@ -11064,7 +11044,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { Value *Op = *i; if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { if (Constant *C = dyn_cast(Op)) { - *i = Context->getConstantExprTrunc(C, TD->getIntPtrType()); + *i = ConstantExpr::getTrunc(C, TD->getIntPtrType()); MadeChange = true; } else { Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), @@ -11072,9 +11052,10 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { *i = Op; MadeChange = true; } - } else if (TD->getTypeSizeInBits(Op->getType()) < TD->getPointerSizeInBits()) { + } else if (TD->getTypeSizeInBits(Op->getType()) + < TD->getPointerSizeInBits()) { if (Constant *C = dyn_cast(Op)) { - *i = Context->getConstantExprSExt(C, TD->getIntPtrType()); + *i = ConstantExpr::getSExt(C, TD->getIntPtrType()); MadeChange = true; } else { Op = InsertCastBefore(Instruction::SExt, Op, TD->getIntPtrType(), @@ -11092,8 +11073,12 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // getelementptr instructions into a single instruction. // SmallVector SrcGEPOperands; - if (User *Src = dyn_castGetElementPtr(PtrOp)) + bool BothInBounds = cast(&GEP)->isInBounds(); + if (GEPOperator *Src = dyn_cast(PtrOp)) { SrcGEPOperands.append(Src->op_begin(), Src->op_end()); + if (!Src->isInBounds()) + BothInBounds = false; + } if (!SrcGEPOperands.empty()) { // Note that if our source is a gep chain itself that we wait for that @@ -11118,9 +11103,9 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // With: T = long A+B; gep %P, T, ... // Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); - if (SO1 == Context->getNullValue(SO1->getType())) { + if (SO1 == Constant::getNullValue(SO1->getType())) { Sum = GO1; - } else if (GO1 == Context->getNullValue(GO1->getType())) { + } else if (GO1 == Constant::getNullValue(GO1->getType())) { Sum = SO1; } else { // If they aren't the same type, convert both to an integer of the @@ -11128,11 +11113,11 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { if (SO1->getType() != GO1->getType()) { if (Constant *SO1C = dyn_cast(SO1)) { SO1 = - Context->getConstantExprIntegerCast(SO1C, GO1->getType(), true); + ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); } else if (Constant *GO1C = dyn_cast(GO1)) { GO1 = - Context->getConstantExprIntegerCast(GO1C, SO1->getType(), true); - } else { + ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); + } else if (TD) { unsigned PS = TD->getPointerSizeInBits(); if (TD->getTypeSizeInBits(SO1->getType()) == PS) { // Convert GO1 to SO1's type. @@ -11149,7 +11134,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { } } if (isa(SO1) && isa(GO1)) - Sum = Context->getConstantExprAdd(cast(SO1), + Sum = ConstantExpr::getAdd(cast(SO1), cast(GO1)); else { Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); @@ -11177,9 +11162,15 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); } - if (!Indices.empty()) - return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), - Indices.end(), GEP.getName()); + if (!Indices.empty()) { + GetElementPtrInst *NewGEP = GetElementPtrInst::Create(SrcGEPOperands[0], + Indices.begin(), + Indices.end(), + GEP.getName()); + if (BothInBounds) + cast(NewGEP)->setIsInBounds(true); + return NewGEP; + } } else if (GlobalValue *GV = dyn_cast(PtrOp)) { // GEP of global variable. If all of the indices for this GEP are @@ -11192,7 +11183,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { Indices.push_back(cast(*I)); if (I == E) { // If they are all constants... - Constant *CE = Context->getConstantExprGetElementPtr(GV, + Constant *CE = ConstantExpr::getGetElementPtr(GV, &Indices[0],Indices.size()); // Replace all uses of the GEP with the new constexpr... @@ -11217,8 +11208,12 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { if (CATy->getElementType() == XTy->getElementType()) { // -> GEP i8* X, ... SmallVector Indices(GEP.idx_begin()+1, GEP.idx_end()); - return GetElementPtrInst::Create(X, Indices.begin(), Indices.end(), - GEP.getName()); + GetElementPtrInst *NewGEP = + GetElementPtrInst::Create(X, Indices.begin(), Indices.end(), + GEP.getName()); + if (cast(&GEP)->isInBounds()) + cast(NewGEP)->setIsInBounds(true); + return NewGEP; } else if (const ArrayType *XATy = dyn_cast(XTy->getElementType())) { // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ? @@ -11239,14 +11234,17 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast const Type *SrcElTy = cast(X->getType())->getElementType(); const Type *ResElTy=cast(PtrOp->getType())->getElementType(); - if (isa(SrcElTy) && + if (TD && isa(SrcElTy) && TD->getTypeAllocSize(cast(SrcElTy)->getElementType()) == TD->getTypeAllocSize(ResElTy)) { Value *Idx[2]; - Idx[0] = Context->getNullValue(Type::Int32Ty); + Idx[0] = Constant::getNullValue(Type::Int32Ty); Idx[1] = GEP.getOperand(1); - Value *V = InsertNewInstBefore( - GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); + GetElementPtrInst *NewGEP = + GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); + if (cast(&GEP)->isInBounds()) + cast(NewGEP)->setIsInBounds(true); + Value *V = InsertNewInstBefore(NewGEP, GEP); // V and GEP are both pointer types --> BitCast return new BitCastInst(V, GEP.getType()); } @@ -11256,7 +11254,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // (where tmp = 8*tmp2) into: // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast - if (isa(SrcElTy) && ResElTy == Type::Int8Ty) { + if (TD && isa(SrcElTy) && ResElTy == Type::Int8Ty) { uint64_t ArrayEltSize = TD->getTypeAllocSize(cast(SrcElTy)->getElementType()); @@ -11267,16 +11265,16 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { if (ArrayEltSize == 1) { NewIdx = GEP.getOperand(1); Scale = - Context->getConstantInt(cast(NewIdx->getType()), 1); + ConstantInt::get(cast(NewIdx->getType()), 1); } else if (ConstantInt *CI = dyn_cast(GEP.getOperand(1))) { - NewIdx = Context->getConstantInt(CI->getType(), 1); + NewIdx = ConstantInt::get(CI->getType(), 1); Scale = CI; } else if (Instruction *Inst =dyn_cast(GEP.getOperand(1))){ if (Inst->getOpcode() == Instruction::Shl && isa(Inst->getOperand(1))) { ConstantInt *ShAmt = cast(Inst->getOperand(1)); uint32_t ShAmtVal = ShAmt->getLimitedValue(64); - Scale = Context->getConstantInt(cast(Inst->getType()), + Scale = ConstantInt::get(cast(Inst->getType()), 1ULL << ShAmtVal); NewIdx = Inst->getOperand(0); } else if (Inst->getOpcode() == Instruction::Mul && @@ -11292,11 +11290,11 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // operation after making sure Scale doesn't have the sign bit set. if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL && Scale->getZExtValue() % ArrayEltSize == 0) { - Scale = Context->getConstantInt(Scale->getType(), + Scale = ConstantInt::get(Scale->getType(), Scale->getZExtValue() / ArrayEltSize); if (Scale->getZExtValue() != 1) { Constant *C = - Context->getConstantExprIntegerCast(Scale, NewIdx->getType(), + ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), false /*ZExt*/); Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale"); NewIdx = InsertNewInstBefore(Sc, GEP); @@ -11304,10 +11302,12 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // Insert the new GEP instruction. Value *Idx[2]; - Idx[0] = Context->getNullValue(Type::Int32Ty); + Idx[0] = Constant::getNullValue(Type::Int32Ty); Idx[1] = NewIdx; Instruction *NewGEP = GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); + if (cast(&GEP)->isInBounds()) + cast(NewGEP)->setIsInBounds(true); NewGEP = InsertNewInstBefore(NewGEP, GEP); // The NewGEP must be pointer typed, so must the old one -> BitCast return new BitCastInst(NewGEP, GEP.getType()); @@ -11322,7 +11322,8 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { /// into a gep of the original struct. This is important for SROA and alias /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged. if (BitCastInst *BCI = dyn_cast(PtrOp)) { - if (!isa(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) { + if (TD && + !isa(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) { // Determine how much the GEP moves the pointer. We are guaranteed to get // a constant back from EmitGEPOffset. ConstantInt *OffsetV = @@ -11359,6 +11360,8 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { GetElementPtrInst::Create(BCI->getOperand(0), NewIndices.begin(), NewIndices.end()); if (NGEP->getType() == GEP.getType()) return NGEP; + if (cast(&GEP)->isInBounds()) + cast(NGEP)->setIsInBounds(true); InsertNewInstBefore(NGEP, GEP); NGEP->takeName(&GEP); return new BitCastInst(NGEP, GEP.getType()); @@ -11374,7 +11377,7 @@ Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { if (AI.isArrayAllocation()) { // Check C != 1 if (const ConstantInt *C = dyn_cast(AI.getArraySize())) { const Type *NewTy = - Context->getArrayType(AI.getAllocatedType(), C->getZExtValue()); + ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); AllocationInst *New = 0; // Create and insert the replacement instruction... @@ -11396,27 +11399,28 @@ Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { // Now that I is pointing to the first non-allocation-inst in the block, // insert our getelementptr instruction... // - Value *NullIdx = Context->getNullValue(Type::Int32Ty); + Value *NullIdx = Constant::getNullValue(Type::Int32Ty); Value *Idx[2]; Idx[0] = NullIdx; Idx[1] = NullIdx; Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, New->getName()+".sub", It); + cast(V)->setIsInBounds(true); // Now make everything use the getelementptr instead of the original // allocation. return ReplaceInstUsesWith(AI, V); } else if (isa(AI.getArraySize())) { - return ReplaceInstUsesWith(AI, Context->getNullValue(AI.getType())); + return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); } } - if (isa(AI) && AI.getAllocatedType()->isSized()) { + if (TD && isa(AI) && AI.getAllocatedType()->isSized()) { // If alloca'ing a zero byte object, replace the alloca with a null pointer. // Note that we only do this for alloca's, because malloc should allocate // and return a unique pointer, even for a zero byte allocation. if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0) - return ReplaceInstUsesWith(AI, Context->getNullValue(AI.getType())); + return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); // If the alignment is 0 (unspecified), assign it the preferred alignment. if (AI.getAlignment() == 0) @@ -11432,8 +11436,8 @@ Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { // free undef -> unreachable. if (isa(Op)) { // Insert a new store to null because we cannot modify the CFG here. - new StoreInst(Context->getConstantIntTrue(), - Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)), &FI); + new StoreInst(ConstantInt::getTrue(*Context), + UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI); return EraseInstFromFunction(FI); } @@ -11502,7 +11506,7 @@ static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, SingleChar = 0; StrVal = (StrVal << 8) | SingleChar; } - Value *NL = Context->getConstantInt(StrVal); + Value *NL = ConstantInt::get(*Context, StrVal); return IC.ReplaceInstUsesWith(LI, NL); } } @@ -11528,19 +11532,20 @@ static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, if (Constant *CSrc = dyn_cast(CastOp)) if (ASrcTy->getNumElements() != 0) { Value *Idxs[2]; - Idxs[0] = Idxs[1] = Context->getNullValue(Type::Int32Ty); - CastOp = Context->getConstantExprGetElementPtr(CSrc, Idxs, 2); + Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); + CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); SrcTy = cast(CastOp->getType()); SrcPTy = SrcTy->getElementType(); } - if ((SrcPTy->isInteger() || isa(SrcPTy) || + if (IC.getTargetData() && + (SrcPTy->isInteger() || isa(SrcPTy) || isa(SrcPTy)) && // Do not allow turning this into a load of an integer, which is then // casted to a pointer, this pessimizes pointer analysis a lot. (isa(SrcPTy) == isa(LI.getType())) && - IC.getTargetData().getTypeSizeInBits(SrcPTy) == - IC.getTargetData().getTypeSizeInBits(DestPTy)) { + IC.getTargetData()->getTypeSizeInBits(SrcPTy) == + IC.getTargetData()->getTypeSizeInBits(DestPTy)) { // Okay, we are casting from one integer or pointer type to another of // the same size. Instead of casting the pointer before the load, cast @@ -11560,12 +11565,14 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { Value *Op = LI.getOperand(0); // Attempt to improve the alignment. - unsigned KnownAlign = - GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType())); - if (KnownAlign > - (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : - LI.getAlignment())) - LI.setAlignment(KnownAlign); + if (TD) { + unsigned KnownAlign = + GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType())); + if (KnownAlign > + (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : + LI.getAlignment())) + LI.setAlignment(KnownAlign); + } // load (cast X) --> cast (load X) iff safe if (isa(Op)) @@ -11591,9 +11598,9 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { // that this code is not reachable. We do this instead of inserting // an unreachable instruction directly because we cannot modify the // CFG. - new StoreInst(Context->getUndef(LI.getType()), - Context->getNullValue(Op->getType()), &LI); - return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType())); + new StoreInst(UndefValue::get(LI.getType()), + Constant::getNullValue(Op->getType()), &LI); + return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); } } @@ -11605,9 +11612,9 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { // Insert a new store to null instruction before the load to indicate that // this code is not reachable. We do this instead of inserting an // unreachable instruction directly because we cannot modify the CFG. - new StoreInst(Context->getUndef(LI.getType()), - Context->getNullValue(Op->getType()), &LI); - return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType())); + new StoreInst(UndefValue::get(LI.getType()), + Constant::getNullValue(Op->getType()), &LI); + return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); } // Instcombine load (constant global) into the value loaded. @@ -11622,16 +11629,16 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { if (GV->isConstant() && GV->hasDefinitiveInitializer()) if (Constant *V = ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE, - Context)) + *Context)) return ReplaceInstUsesWith(LI, V); if (CE->getOperand(0)->isNullValue()) { // Insert a new store to null instruction before the load to indicate // that this code is not reachable. We do this instead of inserting // an unreachable instruction directly because we cannot modify the // CFG. - new StoreInst(Context->getUndef(LI.getType()), - Context->getNullValue(Op->getType()), &LI); - return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType())); + new StoreInst(UndefValue::get(LI.getType()), + Constant::getNullValue(Op->getType()), &LI); + return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); } } else if (CE->isCast()) { @@ -11646,9 +11653,9 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { if (GlobalVariable *GV = dyn_cast(Op->getUnderlyingObject())){ if (GV->isConstant() && GV->hasDefinitiveInitializer()) { if (GV->getInitializer()->isNullValue()) - return ReplaceInstUsesWith(LI, Context->getNullValue(LI.getType())); + return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); else if (isa(GV->getInitializer())) - return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType())); + return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); } } @@ -11698,7 +11705,6 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { User *CI = cast(SI.getOperand(1)); Value *CastOp = CI->getOperand(0); - LLVMContext *Context = IC.getContext(); const Type *DestPTy = cast(CI->getType())->getElementType(); const PointerType *SrcTy = dyn_cast(CastOp->getType()); @@ -11720,7 +11726,7 @@ static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { // constants. if (isa(SrcPTy) || isa(SrcPTy)) { // Index through pointer. - Constant *Zero = Context->getNullValue(Type::Int32Ty); + Constant *Zero = Constant::getNullValue(Type::Int32Ty); NewGEPIndices.push_back(Zero); while (1) { @@ -11737,7 +11743,7 @@ static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { } } - SrcTy = Context->getPointerType(SrcPTy, SrcTy->getAddressSpace()); + SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace()); } if (!SrcPTy->isInteger() && !isa(SrcPTy)) @@ -11745,10 +11751,11 @@ static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { // If the pointers point into different address spaces or if they point to // values with different sizes, we can't do the transformation. - if (SrcTy->getAddressSpace() != + if (!IC.getTargetData() || + SrcTy->getAddressSpace() != cast(CI->getType())->getAddressSpace() || - IC.getTargetData().getTypeSizeInBits(SrcPTy) != - IC.getTargetData().getTypeSizeInBits(DestPTy)) + IC.getTargetData()->getTypeSizeInBits(SrcPTy) != + IC.getTargetData()->getTypeSizeInBits(DestPTy)) return 0; // Okay, we are casting from one integer or pointer type to another of @@ -11771,16 +11778,17 @@ static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { // emit a GEP to index into its first field. if (!NewGEPIndices.empty()) { if (Constant *C = dyn_cast(CastOp)) - CastOp = Context->getConstantExprGetElementPtr(C, &NewGEPIndices[0], + CastOp = ConstantExpr::getGetElementPtr(C, &NewGEPIndices[0], NewGEPIndices.size()); else CastOp = IC.InsertNewInstBefore( GetElementPtrInst::Create(CastOp, NewGEPIndices.begin(), NewGEPIndices.end()), SI); + cast(CastOp)->setIsInBounds(true); } if (Constant *C = dyn_cast(SIOp0)) - NewCast = Context->getConstantExprCast(opcode, C, CastDstTy); + NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); else NewCast = IC.InsertNewInstBefore( CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), @@ -11878,12 +11886,14 @@ Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { } // Attempt to improve the alignment. - unsigned KnownAlign = - GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType())); - if (KnownAlign > - (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : - SI.getAlignment())) - SI.setAlignment(KnownAlign); + if (TD) { + unsigned KnownAlign = + GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType())); + if (KnownAlign > + (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : + SI.getAlignment())) + SI.setAlignment(KnownAlign); + } // Do really simple DSE, to catch cases where there are several consecutive // stores to the same location, separated by a few arithmetic operations. This @@ -11941,7 +11951,7 @@ Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { if (isa(Ptr) && cast(Ptr->getType())->getAddressSpace() == 0) { if (!isa(Val)) { - SI.setOperand(0, Context->getUndef(Val->getType())); + SI.setOperand(0, UndefValue::get(Val->getType())); if (Instruction *U = dyn_cast(Val)) AddToWorkList(U); // Dropped a use. ++NumCombined; @@ -12108,7 +12118,7 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { 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(X)) { // Swap Destinations and condition... BI.setCondition(X); @@ -12120,7 +12130,7 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { // 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(BI.getCondition()); @@ -12140,7 +12150,7 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { // 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()) { @@ -12169,7 +12179,7 @@ Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { // change 'switch (X+4) case 1:' into 'switch (X) case -3' for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) SI.setOperand(i, - Context->getConstantExprSub(cast(SI.getOperand(i)), + ConstantExpr::getSub(cast(SI.getOperand(i)), AddRHS)); SI.setOperand(0, I->getOperand(0)); AddToWorkList(I); @@ -12187,10 +12197,10 @@ Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) { if (Constant *C = dyn_cast(Agg)) { if (isa(C)) - return ReplaceInstUsesWith(EV, Context->getUndef(EV.getType())); + return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType())); if (isa(C)) - return ReplaceInstUsesWith(EV, Context->getNullValue(EV.getType())); + return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType())); if (isa(C) || isa(C)) { // Extract the element indexed by the first index out of the constant @@ -12332,12 +12342,12 @@ static Value *FindScalarElement(Value *V, unsigned EltNo, const VectorType *PTy = cast(V->getType()); unsigned Width = PTy->getNumElements(); if (EltNo >= Width) // Out of range access. - return Context->getUndef(PTy->getElementType()); + return UndefValue::get(PTy->getElementType()); if (isa(V)) - return Context->getUndef(PTy->getElementType()); + return UndefValue::get(PTy->getElementType()); else if (isa(V)) - return Context->getNullValue(PTy->getElementType()); + return Constant::getNullValue(PTy->getElementType()); else if (ConstantVector *CP = dyn_cast(V)) return CP->getOperand(EltNo); else if (InsertElementInst *III = dyn_cast(V)) { @@ -12363,7 +12373,7 @@ static Value *FindScalarElement(Value *V, unsigned EltNo, else if (InEl < LHSWidth*2) return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth, Context); else - return Context->getUndef(PTy->getElementType()); + return UndefValue::get(PTy->getElementType()); } // Otherwise, we don't know. @@ -12373,11 +12383,11 @@ static Value *FindScalarElement(Value *V, unsigned EltNo, Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { // If vector val is undef, replace extract with scalar undef. if (isa(EI.getOperand(0))) - return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType())); + return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); // If vector val is constant 0, replace extract with scalar 0. if (isa(EI.getOperand(0))) - return ReplaceInstUsesWith(EI, Context->getNullValue(EI.getType())); + return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); if (ConstantVector *C = dyn_cast(EI.getOperand(0))) { // If vector val is constant with all elements the same, replace EI with @@ -12403,7 +12413,7 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { // If this is extracting an invalid index, turn this into undef, to avoid // crashing the code below. if (IndexVal >= VectorWidth) - return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType())); + return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); // This instruction only demands the single element from the input vector. // If the input vector has a single use, simplify it based on this use @@ -12442,10 +12452,10 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { bool isConstantElt = isa(EI.getOperand(1)); if (CheapToScalarize(BO, isConstantElt)) { ExtractElementInst *newEI0 = - new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), + ExtractElementInst::Create(BO->getOperand(0), EI.getOperand(1), EI.getName()+".lhs"); ExtractElementInst *newEI1 = - new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), + ExtractElementInst::Create(BO->getOperand(1), EI.getOperand(1), EI.getName()+".rhs"); InsertNewInstBefore(newEI0, EI); InsertNewInstBefore(newEI1, EI); @@ -12455,9 +12465,10 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { unsigned AS = cast(I->getOperand(0)->getType())->getAddressSpace(); Value *Ptr = InsertBitCastBefore(I->getOperand(0), - Context->getPointerType(EI.getType(), AS),EI); + PointerType::get(EI.getType(), AS),EI); GetElementPtrInst *GEP = GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep"); + cast(GEP)->setIsInBounds(true); InsertNewInstBefore(GEP, EI); return new LoadInst(GEP); } @@ -12489,11 +12500,13 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { SrcIdx -= LHSWidth; Src = SVI->getOperand(1); } else { - return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType())); + return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); } - return new ExtractElementInst(Src, SrcIdx); + return ExtractElementInst::Create(Src, + ConstantInt::get(Type::Int32Ty, SrcIdx, false)); } } + // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement) } return 0; } @@ -12509,15 +12522,15 @@ static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, unsigned NumElts = cast(V->getType())->getNumElements(); if (isa(V)) { - Mask.assign(NumElts, Context->getUndef(Type::Int32Ty)); + Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); return true; } else if (V == LHS) { for (unsigned i = 0; i != NumElts; ++i) - Mask.push_back(Context->getConstantInt(Type::Int32Ty, i)); + Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); return true; } else if (V == RHS) { for (unsigned i = 0; i != NumElts; ++i) - Mask.push_back(Context->getConstantInt(Type::Int32Ty, i+NumElts)); + Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); return true; } else if (InsertElementInst *IEI = dyn_cast(V)) { // If this is an insert of an extract from some other vector, include it. @@ -12534,7 +12547,7 @@ static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, // transitively ok. if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) { // If so, update the mask to reflect the inserted undef. - Mask[InsertedIdx] = Context->getUndef(Type::Int32Ty); + Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); return true; } } else if (ExtractElementInst *EI = dyn_cast(ScalarOp)){ @@ -12551,11 +12564,11 @@ static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, // If so, update the mask to reflect the inserted value. if (EI->getOperand(0) == LHS) { Mask[InsertedIdx % NumElts] = - Context->getConstantInt(Type::Int32Ty, ExtractedIdx); + ConstantInt::get(Type::Int32Ty, ExtractedIdx); } else { assert(EI->getOperand(0) == RHS); Mask[InsertedIdx % NumElts] = - Context->getConstantInt(Type::Int32Ty, ExtractedIdx+NumElts); + ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); } return true; @@ -12580,10 +12593,10 @@ static Value *CollectShuffleElements(Value *V, std::vector &Mask, unsigned NumElts = cast(V->getType())->getNumElements(); if (isa(V)) { - Mask.assign(NumElts, Context->getUndef(Type::Int32Ty)); + Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); return V; } else if (isa(V)) { - Mask.assign(NumElts, Context->getConstantInt(Type::Int32Ty, 0)); + Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); return V; } else if (InsertElementInst *IEI = dyn_cast(V)) { // If this is an insert of an extract from some other vector, include it. @@ -12604,7 +12617,7 @@ static Value *CollectShuffleElements(Value *V, std::vector &Mask, RHS = EI->getOperand(0); Value *V = CollectShuffleElements(VecOp, Mask, RHS, Context); Mask[InsertedIdx % NumElts] = - Context->getConstantInt(Type::Int32Ty, NumElts+ExtractedIdx); + ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); return V; } @@ -12614,7 +12627,7 @@ static Value *CollectShuffleElements(Value *V, std::vector &Mask, // Everything but the extracted element is replaced with the RHS. for (unsigned i = 0; i != NumElts; ++i) { if (i != InsertedIdx) - Mask[i] = Context->getConstantInt(Type::Int32Ty, NumElts+i); + Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); } return V; } @@ -12632,7 +12645,7 @@ static Value *CollectShuffleElements(Value *V, std::vector &Mask, // Otherwise, can't do anything fancy. Return an identity vector. for (unsigned i = 0; i != NumElts; ++i) - Mask.push_back(Context->getConstantInt(Type::Int32Ty, i)); + Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); return V; } @@ -12659,7 +12672,7 @@ Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { return ReplaceInstUsesWith(IE, VecOp); if (InsertedIdx >= NumVectorElts) // Out of range insert. - return ReplaceInstUsesWith(IE, Context->getUndef(IE.getType())); + return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); // If we are extracting a value from a vector, then inserting it right // back into the same place, just use the input vector. @@ -12676,16 +12689,16 @@ Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { // Build a new shuffle mask. std::vector Mask; if (isa(VecOp)) - Mask.assign(NumVectorElts, Context->getUndef(Type::Int32Ty)); + Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); else { assert(isa(VecOp) && "Unknown thing"); - Mask.assign(NumVectorElts, Context->getConstantInt(Type::Int32Ty, + Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, NumVectorElts)); } Mask[InsertedIdx] = - Context->getConstantInt(Type::Int32Ty, ExtractedIdx); + ConstantInt::get(Type::Int32Ty, ExtractedIdx); return new ShuffleVectorInst(EI->getOperand(0), VecOp, - Context->getConstantVector(Mask)); + ConstantVector::get(Mask)); } // If this insertelement isn't used by some other insertelement, turn it @@ -12694,10 +12707,10 @@ Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { std::vector Mask; Value *RHS = 0; Value *LHS = CollectShuffleElements(&IE, Mask, RHS, Context); - if (RHS == 0) RHS = Context->getUndef(LHS->getType()); + if (RHS == 0) RHS = UndefValue::get(LHS->getType()); // We now have a shuffle of LHS, RHS, Mask. return new ShuffleVectorInst(LHS, RHS, - Context->getConstantVector(Mask)); + ConstantVector::get(Mask)); } } } @@ -12721,7 +12734,7 @@ Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { // Undefined shuffle mask -> undefined value. if (isa(SVI.getOperand(2))) - return ReplaceInstUsesWith(SVI, Context->getUndef(SVI.getType())); + return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); unsigned VWidth = cast(SVI.getType())->getNumElements(); @@ -12748,21 +12761,21 @@ Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { std::vector Elts; for (unsigned i = 0, e = Mask.size(); i != e; ++i) { if (Mask[i] >= 2*e) - Elts.push_back(Context->getUndef(Type::Int32Ty)); + Elts.push_back(UndefValue::get(Type::Int32Ty)); else { if ((Mask[i] >= e && isa(RHS)) || (Mask[i] < e && isa(LHS))) { Mask[i] = 2*e; // Turn into undef. - Elts.push_back(Context->getUndef(Type::Int32Ty)); + Elts.push_back(UndefValue::get(Type::Int32Ty)); } else { Mask[i] = Mask[i] % e; // Force to LHS. - Elts.push_back(Context->getConstantInt(Type::Int32Ty, Mask[i])); + Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); } } } SVI.setOperand(0, SVI.getOperand(1)); - SVI.setOperand(1, Context->getUndef(RHS->getType())); - SVI.setOperand(2, Context->getConstantVector(Elts)); + SVI.setOperand(1, UndefValue::get(RHS->getType())); + SVI.setOperand(2, ConstantVector::get(Elts)); LHS = SVI.getOperand(0); RHS = SVI.getOperand(1); MadeChange = true; @@ -12812,14 +12825,14 @@ Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { std::vector Elts; for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { if (NewMask[i] >= LHSInNElts*2) { - Elts.push_back(Context->getUndef(Type::Int32Ty)); + Elts.push_back(UndefValue::get(Type::Int32Ty)); } else { - Elts.push_back(Context->getConstantInt(Type::Int32Ty, NewMask[i])); + Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); } } return new ShuffleVectorInst(LHSSVI->getOperand(0), LHSSVI->getOperand(1), - Context->getConstantVector(Elts)); + ConstantVector::get(Elts)); } } } @@ -12894,14 +12907,14 @@ static void AddReachableCodeToWorklist(BasicBlock *BB, // DCE instruction if trivially dead. if (isInstructionTriviallyDead(Inst)) { ++NumDeadInst; - DOUT << "IC: DCE: " << *Inst; + DOUT << "IC: DCE: " << *Inst << '\n'; Inst->eraseFromParent(); continue; } // ConstantProp instruction if trivially constant. if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) { - DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; + DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst << '\n'; Inst->replaceAllUsesWith(C); ++NumConstProp; Inst->eraseFromParent(); @@ -12960,10 +12973,10 @@ static void AddReachableCodeToWorklist(BasicBlock *BB, bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { bool Changed = false; - TD = &getAnalysis(); + TD = getAnalysisIfAvailable(); - DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " - << F.getNameStr() << "\n"); + DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " + << F.getNameStr() << "\n"); { // Do a depth-first traversal of the function, populate the worklist with @@ -12981,7 +12994,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { while (Term != BB->begin()) { // Remove instrs bottom-up BasicBlock::iterator I = Term; --I; - DOUT << "IC: DCE: " << *I; + DOUT << "IC: DCE: " << *I << '\n'; // A debug intrinsic shouldn't force another iteration if we weren't // going to do one without it. if (!isa(I)) { @@ -12989,7 +13002,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { Changed = true; } if (!I->use_empty()) - I->replaceAllUsesWith(Context->getUndef(I->getType())); + I->replaceAllUsesWith(UndefValue::get(I->getType())); I->eraseFromParent(); } } @@ -13006,7 +13019,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { AddUsesToWorkList(*I); ++NumDeadInst; - DOUT << "IC: DCE: " << *I; + DOUT << "IC: DCE: " << *I << '\n'; I->eraseFromParent(); RemoveFromWorkList(I); @@ -13016,7 +13029,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { // Instruction isn't dead, see if we can constant propagate it. if (Constant *C = ConstantFoldInstruction(I, F.getContext(), TD)) { - DOUT << "IC: ConstFold to: " << *C << " from: " << *I; + DOUT << "IC: ConstFold to: " << *C << " from: " << *I << '\n'; // Add operands to the worklist. AddUsesToWorkList(*I); @@ -13029,9 +13042,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { continue; } - if (TD && - (I->getType()->getTypeID() == Type::VoidTyID || - I->isTrapping())) { + if (TD) { // See if we can constant fold its operands. for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) if (ConstantExpr *CE = dyn_cast(i)) @@ -13075,8 +13086,8 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { ++NumCombined; // Should we replace the old instruction with a new one? if (Result != I) { - DOUT << "IC: Old = " << *I - << " New = " << *Result; + DOUT << "IC: Old = " << *I << '\n' + << " New = " << *Result << '\n'; // Everything uses the new instruction now. I->replaceAllUsesWith(Result); @@ -13110,8 +13121,8 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { InstParent->getInstList().erase(I); } else { #ifndef NDEBUG - DOUT << "IC: Mod = " << OrigI - << " New = " << *I; + DOUT << "IC: Mod = " << OrigI << '\n' + << " New = " << *I << '\n'; #endif // If the instruction was modified, it's possible that it is now dead. @@ -13144,6 +13155,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { bool InstCombiner::runOnFunction(Function &F) { MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); + Context = &F.getContext(); bool EverMadeChange = false;