diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index f142976d330..b4db582ea63 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -567,15 +567,7 @@ public: const Type *Ty ///< The type to which cast should be made ); - // This method uses the CastInst::getCastOpcode method to infer the - // cast opcode to use. - // @brief Get a ConstantExpr Conversion operator that casts C to Ty - static Constant *getInferredCast(Constant *C, bool SrcIsSigned, - const Type *Ty, bool DestIsSigned); - - static Constant *getCast(Constant *C, const Type *Ty) { - return getInferredCast(C, C->getType()->isSigned(), Ty, Ty->isSigned()); - } + static Constant *getCast(Constant *C, const Type *Ty); /// @brief Return true if this is a convert constant expression bool isCast() const; diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 6f75fa0f0e7..4c1fa10cc8f 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -179,7 +179,7 @@ SCEVHandle SCEVConstant::get(ConstantInt *V) { if (V->getType()->isSigned()) { const Type *NewTy = V->getType()->getUnsignedVersion(); V = cast( - ConstantExpr::getInferredCast(V, false, NewTy, false)); + ConstantExpr::getBitCast(V, NewTy)); } SCEVConstant *&R = (*SCEVConstants)[V]; @@ -466,7 +466,7 @@ SCEVHandle SCEVUnknown::getIntegerSCEV(int Val, const Type *Ty) { C = ConstantInt::get(Ty, Val); else { C = ConstantInt::get(Ty->getSignedVersion(), Val); - C = ConstantExpr::getInferredCast(C, true, Ty, false); + C = ConstantExpr::getBitCast(C, Ty); } return SCEVUnknown::get(C); } @@ -513,7 +513,7 @@ static SCEVHandle PartialFact(SCEVHandle V, unsigned NumSteps) { Result *= Val-(NumSteps-1); Constant *Res = ConstantInt::get(Type::ULongTy, Result); return SCEVUnknown::get( - ConstantExpr::getInferredCast(Res, false, V->getType(), true)); + ConstantExpr::getTruncOrBitCast(Res, V->getType())); } const Type *Ty = V->getType(); @@ -559,7 +559,8 @@ SCEVHandle SCEVAddRecExpr::evaluateAtIteration(SCEVHandle It) const { SCEVHandle SCEVTruncateExpr::get(const SCEVHandle &Op, const Type *Ty) { if (SCEVConstant *SC = dyn_cast(Op)) - return SCEVUnknown::get(ConstantExpr::getCast(SC->getValue(), Ty)); + return SCEVUnknown::get( + ConstantExpr::getTruncOrBitCast(SC->getValue(), Ty)); // If the input value is a chrec scev made out of constants, truncate // all of the constants. @@ -582,7 +583,8 @@ SCEVHandle SCEVTruncateExpr::get(const SCEVHandle &Op, const Type *Ty) { SCEVHandle SCEVZeroExtendExpr::get(const SCEVHandle &Op, const Type *Ty) { if (SCEVConstant *SC = dyn_cast(Op)) - return SCEVUnknown::get(ConstantExpr::getCast(SC->getValue(), Ty)); + return SCEVUnknown::get( + ConstantExpr::getZExtOrBitCast(SC->getValue(), Ty)); // FIXME: If the input value is a chrec scev, and we can prove that the value // did not overflow the old, smaller, value, we can zero extend all of the @@ -998,11 +1000,10 @@ SCEVHandle SCEVSDivExpr::get(const SCEVHandle &LHS, const SCEVHandle &RHS) { Constant *LHSCV = LHSC->getValue(); Constant *RHSCV = RHSC->getValue(); if (LHSCV->getType()->isUnsigned()) - LHSCV = ConstantExpr::getInferredCast( - LHSCV, false, LHSCV->getType()->getSignedVersion(), true); + LHSCV = ConstantExpr::getBitCast(LHSCV, + LHSCV->getType()->getSignedVersion()); if (RHSCV->getType()->isUnsigned()) - RHSCV = ConstantExpr::getInferredCast( - RHSCV, false, LHSCV->getType(), true); + RHSCV = ConstantExpr::getBitCast(RHSCV, LHSCV->getType()); return SCEVUnknown::get(ConstantExpr::getSDiv(LHSCV, RHSCV)); } } diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index dc3d9935d69..f52504298e1 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -1500,14 +1500,7 @@ static inline Constant *getFoldedCast( ExprMapKeyType Key(opc, argVec); return ExprConstants->getOrCreate(Ty, Key); } - -Constant *ConstantExpr::getInferredCast(Constant *C, bool SrcIsSigned, - const Type *Ty, bool DestIsSigned) { - // Note: we can't inline this because it requires the Instructions.h header - return getCast( - CastInst::getCastOpcode(C, SrcIsSigned, Ty, DestIsSigned), C, Ty); -} - + Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { Instruction::CastOps opc = Instruction::CastOps(oc); assert(Instruction::isCast(opc) && "opcode out of range"); @@ -1532,8 +1525,15 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) { case Instruction::BitCast: return getBitCast(C, Ty); } return 0; +} + +Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) { + // Note: we can't inline this because it requires the Instructions.h header + return getCast(CastInst::getCastOpcode( + C, C->getType()->isSigned(), Ty, Ty->isSigned()), C, Ty); } + Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) { if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) return getCast(Instruction::BitCast, C, Ty);