diff options
author | Dan Gohman <gohman@apple.com> | 2009-04-21 01:07:12 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2009-04-21 01:07:12 +0000 |
commit | af79fb5f47b0088c6a8973a7fdbaea96973a429d (patch) | |
tree | be262820979564b6c6e19bf3ad4c50dc4a895b16 | |
parent | fb17fd2cdf35f8ad0b9e0e7e1b06a186fce442f8 (diff) |
Introduce encapsulation for ScalarEvolution's TargetData object, and refactor
the code to minimize dependencies on TargetData.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69644 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/Analysis/ScalarEvolution.h | 19 | ||||
-rw-r--r-- | include/llvm/Analysis/ScalarEvolutionExpander.h | 12 | ||||
-rw-r--r-- | lib/Analysis/ScalarEvolution.cpp | 190 | ||||
-rw-r--r-- | lib/Analysis/ScalarEvolutionExpander.cpp | 99 | ||||
-rw-r--r-- | lib/Transforms/Scalar/IndVarSimplify.cpp | 40 | ||||
-rw-r--r-- | lib/Transforms/Scalar/LoopStrengthReduce.cpp | 72 |
6 files changed, 244 insertions, 188 deletions
diff --git a/include/llvm/Analysis/ScalarEvolution.h b/include/llvm/Analysis/ScalarEvolution.h index 9e0965397a..56d3aa3ee7 100644 --- a/include/llvm/Analysis/ScalarEvolution.h +++ b/include/llvm/Analysis/ScalarEvolution.h @@ -32,7 +32,6 @@ namespace llvm { class Type; class SCEVHandle; class ScalarEvolution; - class TargetData; /// SCEV - This class represent an analyzed expression in the program. These /// are reference counted opaque objects that the client is not allowed to @@ -201,9 +200,21 @@ namespace llvm { static char ID; // Pass identification, replacement for typeid ScalarEvolution() : FunctionPass(&ID), Impl(0) {} - // getTargetData - Return the TargetData object contained in this - // ScalarEvolution. - const TargetData &getTargetData() const; + /// isSCEVable - Test if values of the given type are analyzable within + /// the SCEV framework. This primarily includes integer types, and it + /// can optionally include pointer types if the ScalarEvolution class + /// has access to target-specific information. + bool isSCEVable(const Type *Ty) const; + + /// getTypeSizeInBits - Return the size in bits of the specified type, + /// for which isSCEVable must return true. + uint64_t getTypeSizeInBits(const Type *Ty) const; + + /// getEffectiveSCEVType - Return a type with the same bitwidth as + /// the given type and which represents how SCEV will treat the given + /// type, for which isSCEVable must return true. For pointer types, + /// this is the pointer-sized integer type. + const Type *getEffectiveSCEVType(const Type *Ty) const; /// getSCEV - Return a SCEV expression handle for the full generality of the /// specified expression. diff --git a/include/llvm/Analysis/ScalarEvolutionExpander.h b/include/llvm/Analysis/ScalarEvolutionExpander.h index b5072da4ad..16db5f39c4 100644 --- a/include/llvm/Analysis/ScalarEvolutionExpander.h +++ b/include/llvm/Analysis/ScalarEvolutionExpander.h @@ -20,8 +20,6 @@ #include "llvm/Analysis/ScalarEvolutionExpressions.h" namespace llvm { - class TargetData; - /// SCEVExpander - This class uses information about analyze scalars to /// rewrite expressions in canonical form. /// @@ -31,7 +29,6 @@ namespace llvm { struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> { ScalarEvolution &SE; LoopInfo &LI; - const TargetData &TD; std::map<SCEVHandle, Value*> InsertedExpressions; std::set<Instruction*> InsertedInstructions; @@ -39,8 +36,8 @@ namespace llvm { friend struct SCEVVisitor<SCEVExpander, Value*>; public: - SCEVExpander(ScalarEvolution &se, LoopInfo &li, const TargetData &td) - : SE(se), LI(li), TD(td) {} + SCEVExpander(ScalarEvolution &se, LoopInfo &li) + : SE(se), LI(li) {} LoopInfo &getLoopInfo() const { return LI; } @@ -85,6 +82,11 @@ namespace llvm { /// we can to share the casts. Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, const Type *Ty); + + /// InsertNoopCastOfTo - Insert a cast of V to the specified type, + /// which must be possible with a noop cast. + Value *InsertNoopCastOfTo(Value *V, const Type *Ty); + /// InsertBinop - Insert the specified binary operator, doing a small amount /// of work to avoid inserting an obviously redundant operation. static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 2a6cc50c0b..e429697b1c 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -570,7 +570,7 @@ static SCEVHandle BinomialCoefficient(SCEVHandle It, unsigned K, if (K > 1000) return SE.getCouldNotCompute(); - unsigned W = SE.getTargetData().getTypeSizeInBits(ResultTy); + unsigned W = SE.getTypeSizeInBits(ResultTy); // Calculate K! / 2^T and T; we divide out the factors of two before // multiplying for calculating K! / 2^T to avoid overflow. @@ -648,8 +648,7 @@ SCEVHandle SCEVAddRecExpr::evaluateAtIteration(SCEVHandle It, //===----------------------------------------------------------------------===// SCEVHandle ScalarEvolution::getTruncateExpr(const SCEVHandle &Op, const Type *Ty) { - assert(getTargetData().getTypeSizeInBits(Op->getType()) > - getTargetData().getTypeSizeInBits(Ty) && + assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && "This is not a truncating conversion!"); if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) @@ -677,13 +676,11 @@ SCEVHandle ScalarEvolution::getTruncateExpr(const SCEVHandle &Op, const Type *Ty SCEVHandle ScalarEvolution::getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty) { - assert(getTargetData().getTypeSizeInBits(Op->getType()) < - getTargetData().getTypeSizeInBits(Ty) && + assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && "This is not an extending conversion!"); if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { - const Type *IntTy = Ty; - if (isa<PointerType>(IntTy)) IntTy = getTargetData().getIntPtrType(); + const Type *IntTy = getEffectiveSCEVType(Ty); Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); return getUnknown(C); @@ -700,13 +697,11 @@ SCEVHandle ScalarEvolution::getZeroExtendExpr(const SCEVHandle &Op, } SCEVHandle ScalarEvolution::getSignExtendExpr(const SCEVHandle &Op, const Type *Ty) { - assert(getTargetData().getTypeSizeInBits(Op->getType()) < - getTargetData().getTypeSizeInBits(Ty) && + assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && "This is not an extending conversion!"); if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { - const Type *IntTy = Ty; - if (isa<PointerType>(IntTy)) IntTy = getTargetData().getIntPtrType(); + const Type *IntTy = getEffectiveSCEVType(Ty); Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); return getUnknown(C); @@ -1366,7 +1361,7 @@ namespace { /// TD - The target data information for the target we are targetting. /// - TargetData &TD; + TargetData *TD; /// UnknownValue - This SCEV is used to represent unknown trip counts and /// things. @@ -1389,9 +1384,25 @@ namespace { public: ScalarEvolutionsImpl(ScalarEvolution &se, Function &f, LoopInfo &li, - TargetData &td) + TargetData *td) : SE(se), F(f), LI(li), TD(td), UnknownValue(new SCEVCouldNotCompute()) {} + /// isSCEVable - Test if values of the given type are analyzable within + /// the SCEV framework. This primarily includes integer types, and it + /// can optionally include pointer types if the ScalarEvolution class + /// has access to target-specific information. + bool isSCEVable(const Type *Ty) const; + + /// getTypeSizeInBits - Return the size in bits of the specified type, + /// for which isSCEVable must return true. + uint64_t getTypeSizeInBits(const Type *Ty) const; + + /// getEffectiveSCEVType - Return a type with the same bitwidth as + /// the given type and which represents how SCEV will treat the given + /// type, for which isSCEVable must return true. For pointer types, + /// this is the pointer-sized integer type. + const Type *getEffectiveSCEVType(const Type *Ty) const; + SCEVHandle getCouldNotCompute(); /// getIntegerSCEV - Given an integer or FP type, create a constant for the @@ -1478,9 +1489,6 @@ namespace { /// that no dangling references are left around. void deleteValueFromRecords(Value *V); - /// getTargetData - Return the TargetData. - const TargetData &getTargetData() const; - private: /// createSCEV - We know that there is no SCEV for the specified value. /// Analyze the expression. @@ -1581,8 +1589,50 @@ void ScalarEvolutionsImpl::deleteValueFromRecords(Value *V) { } } -const TargetData &ScalarEvolutionsImpl::getTargetData() const { - return TD; +/// isSCEVable - Test if values of the given type are analyzable within +/// the SCEV framework. This primarily includes integer types, and it +/// can optionally include pointer types if the ScalarEvolution class +/// has access to target-specific information. +bool ScalarEvolutionsImpl::isSCEVable(const Type *Ty) const { + // Integers are always SCEVable. + if (Ty->isInteger()) + return true; + + // Pointers are SCEVable if TargetData information is available + // to provide pointer size information. + if (isa<PointerType>(Ty)) + return TD != NULL; + + // Otherwise it's not SCEVable. + return false; +} + +/// getTypeSizeInBits - Return the size in bits of the specified type, +/// for which isSCEVable must return true. +uint64_t ScalarEvolutionsImpl::getTypeSizeInBits(const Type *Ty) const { + assert(isSCEVable(Ty) && "Type is not SCEVable!"); + + // If we have a TargetData, use it! + if (TD) + return TD->getTypeSizeInBits(Ty); + + // Otherwise, we support only integer types. + assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!"); + return Ty->getPrimitiveSizeInBits(); +} + +/// getEffectiveSCEVType - Return a type with the same bitwidth as +/// the given type and which represents how SCEV will treat the given +/// type, for which isSCEVable must return true. For pointer types, +/// this is the pointer-sized integer type. +const Type *ScalarEvolutionsImpl::getEffectiveSCEVType(const Type *Ty) const { + assert(isSCEVable(Ty) && "Type is not SCEVable!"); + + if (Ty->isInteger()) + return Ty; + + assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); + return TD->getIntPtrType(); } SCEVHandle ScalarEvolutionsImpl::getCouldNotCompute() { @@ -1592,7 +1642,7 @@ SCEVHandle ScalarEvolutionsImpl::getCouldNotCompute() { /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the /// expression and create a new one. SCEVHandle ScalarEvolutionsImpl::getSCEV(Value *V) { - assert(V->getType() != Type::VoidTy && "Can't analyze void expressions!"); + assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); std::map<Value*, SCEVHandle>::iterator I = Scalars.find(V); if (I != Scalars.end()) return I->second; @@ -1604,8 +1654,7 @@ SCEVHandle ScalarEvolutionsImpl::getSCEV(Value *V) { /// getIntegerSCEV - Given an integer or FP type, create a constant for the /// specified signed integer value and return a SCEV for the constant. SCEVHandle ScalarEvolutionsImpl::getIntegerSCEV(int Val, const Type *Ty) { - if (isa<PointerType>(Ty)) - Ty = TD.getIntPtrType(); + Ty = SE.getEffectiveSCEVType(Ty); Constant *C; if (Val == 0) C = Constant::getNullValue(Ty); @@ -1624,8 +1673,7 @@ SCEVHandle ScalarEvolutionsImpl::getNegativeSCEV(const SCEVHandle &V) { return SE.getUnknown(ConstantExpr::getNeg(VC->getValue())); const Type *Ty = V->getType(); - if (isa<PointerType>(Ty)) - Ty = TD.getIntPtrType(); + Ty = SE.getEffectiveSCEVType(Ty); return SE.getMulExpr(V, SE.getConstant(ConstantInt::getAllOnesValue(Ty))); } @@ -1635,8 +1683,7 @@ SCEVHandle ScalarEvolutionsImpl::getNotSCEV(const SCEVHandle &V) { return SE.getUnknown(ConstantExpr::getNot(VC->getValue())); const Type *Ty = V->getType(); - if (isa<PointerType>(Ty)) - Ty = TD.getIntPtrType(); + Ty = SE.getEffectiveSCEVType(Ty); SCEVHandle AllOnes = SE.getConstant(ConstantInt::getAllOnesValue(Ty)); return getMinusSCEV(AllOnes, V); } @@ -1656,12 +1703,12 @@ SCEVHandle ScalarEvolutionsImpl::getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty) { const Type *SrcTy = V->getType(); - assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && - (Ty->isInteger() || isa<PointerType>(Ty)) && + assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && + (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && "Cannot truncate or zero extend with non-integer arguments!"); - if (TD.getTypeSizeInBits(SrcTy) == TD.getTypeSizeInBits(Ty)) + if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) return V; // No conversion - if (TD.getTypeSizeInBits(SrcTy) > TD.getTypeSizeInBits(Ty)) + if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) return SE.getTruncateExpr(V, Ty); return SE.getZeroExtendExpr(V, Ty); } @@ -1673,12 +1720,12 @@ SCEVHandle ScalarEvolutionsImpl::getTruncateOrSignExtend(const SCEVHandle &V, const Type *Ty) { const Type *SrcTy = V->getType(); - assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && - (Ty->isInteger() || isa<PointerType>(Ty)) && + assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && + (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && "Cannot truncate or zero extend with non-integer arguments!"); - if (TD.getTypeSizeInBits(SrcTy) == TD.getTypeSizeInBits(Ty)) + if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) return V; // No conversion - if (TD.getTypeSizeInBits(SrcTy) > TD.getTypeSizeInBits(Ty)) + if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) return SE.getTruncateExpr(V, Ty); return SE.getSignExtendExpr(V, Ty); } @@ -1806,66 +1853,66 @@ SCEVHandle ScalarEvolutionsImpl::createNodeForPHI(PHINode *PN) { /// guaranteed to end in (at every loop iteration). It is, at the same time, /// the minimum number of times S is divisible by 2. For example, given {4,+,8} /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S. -static uint32_t GetMinTrailingZeros(SCEVHandle S, const TargetData &TD) { +static uint32_t GetMinTrailingZeros(SCEVHandle S, const ScalarEvolution &SE) { if (SCEVConstant *C = dyn_cast<SCEVConstant>(S)) return C->getValue()->getValue().countTrailingZeros(); if (SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) - return std::min(GetMinTrailingZeros(T->getOperand(), TD), - (uint32_t)TD.getTypeSizeInBits(T->getType())); + return std::min(GetMinTrailingZeros(T->getOperand(), SE), + (uint32_t)SE.getTypeSizeInBits(T->getType())); if (SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { - uint32_t OpRes = GetMinTrailingZeros(E->getOperand(), TD); - return OpRes == TD.getTypeSizeInBits(E->getOperand()->getType()) ? - TD.getTypeSizeInBits(E->getOperand()->getType()) : OpRes; + uint32_t OpRes = GetMinTrailingZeros(E->getOperand(), SE); + return OpRes == SE.getTypeSizeInBits(E->getOperand()->getType()) ? + SE.getTypeSizeInBits(E->getOperand()->getType()) : OpRes; } if (SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { - uint32_t OpRes = GetMinTrailingZeros(E->getOperand(), TD); - return OpRes == TD.getTypeSizeInBits(E->getOperand()->getType()) ? - TD.getTypeSizeInBits(E->getOperand()->getType()) : OpRes; + uint32_t OpRes = GetMinTrailingZeros(E->getOperand(), SE); + return OpRes == SE.getTypeSizeInBits(E->getOperand()->getType()) ? + SE.getTypeSizeInBits(E->getOperand()->getType()) : OpRes; } if (SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { // The result is the min of all operands results. - uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0), TD); + uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0), SE); for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) - MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i), TD)); + MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i), SE)); return MinOpRes; } if (SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { // The result is the sum of all operands results. - uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0), TD); - uint32_t BitWidth = TD.getTypeSizeInBits(M->getType()); + uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0), SE); + uint32_t BitWidth = SE.getTypeSizeInBits(M->getType()); for (unsigned i = 1, e = M->getNumOperands(); SumOpRes != BitWidth && i != e; ++i) - SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i), TD), + SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i), SE), BitWidth); return SumOpRes; } if (SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { // The result is the min of all operands results. - uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0), TD); + uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0), SE); for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) - MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i), TD)); + MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i), SE)); return MinOpRes; } if (SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { // The result is the min of all operands results. - uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0), TD); + uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0), SE); for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) - MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i), TD)); + MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i), SE)); return MinOpRes; } if (SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { // The result is the min of all operands results. - uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0), TD); + uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0), SE); for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) - MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i), TD)); + MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i), SE)); return MinOpRes; } @@ -1877,8 +1924,7 @@ static uint32_t GetMinTrailingZeros(SCEVHandle S, const TargetData &TD) { /// Analyze the expression. /// SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) { - if (!isa<IntegerType>(V->getType()) && - !isa<PointerType>(V->getType())) + if (!isSCEVable(V->getType())) return SE.getUnknown(V); unsigned Opcode = Instruction::UserOp1; @@ -1913,7 +1959,7 @@ SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) { if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { SCEVHandle LHS = getSCEV(U->getOperand(0)); const APInt &CIVal = CI->getValue(); - if (GetMinTrailingZeros(LHS, TD) >= + if (GetMinTrailingZeros(LHS, SE) >= (CIVal.getBitWidth() - CIVal.countLeadingZeros())) return SE.getAddExpr(LHS, getSCEV(U->getOperand(1))); } @@ -1963,23 +2009,23 @@ SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) { case Instruction::BitCast: // BitCasts are no-op casts so we just eliminate the cast. - if ((U->getType()->isInteger() || - isa<PointerType>(U->getType())) && - (U->getOperand(0)->getType()->isInteger() || - isa<PointerType>(U->getOperand(0)->getType()))) + if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) return getSCEV(U->getOperand(0)); break; case Instruction::IntToPtr: + if (!TD) break; // Without TD we can't analyze pointers. return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), - TD.getIntPtrType()); + TD->getIntPtrType()); case Instruction::PtrToInt: + if (!TD) break; // Without TD we can't analyze pointers. return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), U->getType()); case Instruction::GetElementPtr: { - const Type *IntPtrTy = TD.getIntPtrType(); + if (!TD) break; // Without TD we can't analyze pointers. + const Type *IntPtrTy = TD->getIntPtrType(); Value *Base = U->getOperand(0); SCEVHandle TotalOffset = SE.getIntegerSCEV(0, IntPtrTy); gep_type_iterator GTI = gep_type_begin(U); @@ -1990,7 +2036,7 @@ SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) { // Compute the (potentially symbolic) offset in bytes for this index. if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { // For a struct, add the member offset. - const StructLayout &SL = *TD.getStructLayout(STy); + const StructLayout &SL = *TD->getStructLayout(STy); unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); uint64_t Offset = SL.getElementOffset(FieldNo); TotalOffset = SE.getAddExpr(TotalOffset, @@ -2004,7 +2050,7 @@ SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) { IntPtrTy); LocalOffset = SE.getMulExpr(LocalOffset, - SE.getIntegerSCEV(TD.getTypePaddedSize(*GTI), + SE.getIntegerSCEV(TD->getTypePaddedSize(*GTI), IntPtrTy)); TotalOffset = SE.getAddExpr(TotalOffset, LocalOffset); } @@ -3132,7 +3178,7 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, // First check to see if the range contains zero. If not, the first // iteration exits. - unsigned BitWidth = SE.getTargetData().getTypeSizeInBits(getType()); + unsigned BitWidth = SE.getTypeSizeInBits(getType()); if (!Range.contains(APInt(BitWidth, 0))) return SE.getConstant(ConstantInt::get(getType(),0)); @@ -3226,7 +3272,7 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, bool ScalarEvolution::runOnFunction(Function &F) { Impl = new ScalarEvolutionsImpl(*this, F, getAnalysis<LoopInfo>(), - getAnalysis<TargetData>()); + &getAnalysis<TargetData>()); return false; } @@ -3241,8 +3287,16 @@ void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequiredTransitive<TargetData>(); } -const TargetData &ScalarEvolution::getTargetData() const { - return ((ScalarEvolutionsImpl*)Impl)->getTargetData(); +bool ScalarEvolution::isSCEVable(const Type *Ty) const { + return ((ScalarEvolutionsImpl*)Impl)->isSCEVable(Ty); +} + +uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { + return ((ScalarEvolutionsImpl*)Impl)->getTypeSizeInBits(Ty); +} + +const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { + return ((ScalarEvolutionsImpl*)Impl)->getEffectiveSCEVType(Ty); } SCEVHandle ScalarEvolution::getCouldNotCompute() { diff --git a/lib/Analysis/ScalarEvolutionExpander.cpp b/lib/Analysis/ScalarEvolutionExpander.cpp index 560441f0c5..e3103fa84b 100644 --- a/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/lib/Analysis/ScalarEvolutionExpander.cpp @@ -15,7 +15,6 @@ #include "llvm/Analysis/ScalarEvolutionExpander.h" #include "llvm/Analysis/LoopInfo.h" -#include "llvm/Target/TargetData.h" using namespace llvm; /// InsertCastOfTo - Insert a cast of V to the specified type, doing what @@ -27,12 +26,14 @@ Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, return V; // Short-circuit unnecessary inttoptr<->ptrtoint casts. - if (opcode == Instruction::PtrToInt && Ty == TD.getIntPtrType()) - if (IntToPtrInst *ITP = dyn_cast<IntToPtrInst>(V)) - return ITP->getOperand(0); - if (opcode == Instruction::IntToPtr && V->getType() == TD.getIntPtrType()) - if (PtrToIntInst *PTI = dyn_cast<PtrToIntInst>(V)) - return PTI->getOperand(0); + if ((opcode == Instruction::PtrToInt || opcode == Instruction::IntToPtr) && + SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) + if (CastInst *CI = dyn_cast<CastInst>(V)) + if ((CI->getOpcode() == Instruction::PtrToInt || + CI->getOpcode() == Instruction::IntToPtr) && + SE.getTypeSizeInBits(CI->getType()) == + SE.getTypeSizeInBits(CI->getOperand(0)->getType())) + return CI->getOperand(0); // FIXME: keep track of the cast instruction. if (Constant *C = dyn_cast<Constant>(V)) @@ -83,6 +84,19 @@ Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, return CastInst::Create(opcode, V, Ty, V->getName(), IP); } +/// InsertNoopCastOfTo - Insert a cast of V to the specified type, +/// which must be possible with a noop cast. +Value *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) { + Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false); + assert((Op == Instruction::BitCast || + Op == Instruction::Instruction::PtrToInt || + Op == Instruction::Instruction::IntToPtr) && + "InsertNoopCastOfTo cannot perform non-noop casts!"); + assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) && + "InsertNoopCastOfTo cannot change sizes!"); + return InsertCastOfTo(Op, V, Ty); +} + /// InsertBinop - Insert the specified binary operator, doing a small amount /// of work to avoid inserting an obviously redundant operation. Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, @@ -113,23 +127,21 @@ Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, } Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { - const Type *Ty = S->getType(); - if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); Value *V = expand(S->getOperand(S->getNumOperands()-1)); - V = InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty); + V = InsertNoopCastOfTo(V, Ty); // Emit a bunch of add instructions for (int i = S->getNumOperands()-2; i >= 0; --i) { Value *W = expand(S->getOperand(i)); - W = InsertCastOfTo(CastInst::getCastOpcode(W, false, Ty, false), W, Ty); + W = InsertNoopCastOfTo(W, Ty); V = InsertBinop(Instruction::Add, V, W, InsertPt); } return V; } Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { - const Type *Ty = S->getType(); - if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); int FirstOp = 0; // Set if we should emit a subtract. if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) if (SC->getValue()->isAllOnesValue()) @@ -137,12 +149,12 @@ Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { int i = S->getNumOperands()-2; Value *V = expand(S->getOperand(i+1)); - V = InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty); + V = InsertNoopCastOfTo(V, Ty); // Emit a bunch of multiply instructions for (; i >= FirstOp; --i) { Value *W = expand(S->getOperand(i)); - W = InsertCastOfTo(CastInst::getCastOpcode(W, false, Ty, false), W, Ty); + W = InsertNoopCastOfTo(W, Ty); V = InsertBinop(Instruction::Mul, V, W, InsertPt); } @@ -153,11 +165,10 @@ Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { } Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { - const Type *Ty = S->getType(); - if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); Value *LHS = expand(S->getLHS()); - LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); + LHS = InsertNoopCastOfTo(LHS, Ty); if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { const APInt &RHS = SC->getValue()->getValue(); if (RHS.isPowerOf2()) @@ -167,27 +178,22 @@ Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { } Value *RHS = expand(S->getRHS()); - RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false), RHS, Ty); + RHS = InsertNoopCastOfTo(RHS, Ty); return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt); } Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { - const Type *Ty = S->getType(); + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); const Loop *L = S->getLoop(); - // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F} - assert((Ty->isInteger() || isa<PointerType>(Ty)) && - "Cannot expand fp recurrences yet!"); // {X,+,F} --> X + {0,+,F} if (!S->getStart()->isZero()) { Value *Start = expand(S->getStart()); - if (isa<PointerType>(Start->getType())) - Start = InsertCastOfTo(Instruction::PtrToInt, Start, TD.getIntPtrType()); + Start = InsertNoopCastOfTo(Start, Ty); std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end()); NewOps[0] = SE.getIntegerSCEV(0, Ty); Value *Rest = expand(SE.getAddRecExpr(NewOps, L)); - if (isa<PointerType>(Rest->getType())) - Rest = InsertCastOfTo(Instruction::PtrToInt, Rest, TD.getIntPtrType()); + Rest = InsertNoopCastOfTo(Rest, Ty); // FIXME: look for an existing add to use. return InsertBinop(Instruction::Add, Rest, Start, InsertPt); @@ -227,8 +233,7 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { // If this is a simple linear addrec, emit it now as a special case. if (S->isAffine()) { // {0,+,F} --> i*F Value *F = expand(S->getOperand(1)); - if (isa<PointerType>(F->getType())) - F = InsertCastOfTo(Instruction::PtrToInt, F, TD.getIntPtrType()); + F = InsertNoopCastOfTo(F, Ty); // IF the step is by one, just return the inserted IV. if (ConstantInt *CI = dyn_cast<ConstantInt>(F)) @@ -276,38 +281,33 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { } Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); Value *V = expand(S->getOperand()); - if (isa<PointerType>(V->getType())) - V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); - return CastInst::CreateTruncOrBitCast(V, S->getType(), "tmp.", InsertPt); + V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); + return CastInst::CreateTruncOrBitCast(V, Ty, "tmp.", InsertPt); } Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { - const Type *Ty = S->getType(); - if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); Value *V = expand(S->getOperand()); - if (isa<PointerType>(V->getType())) - V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); + V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); return CastInst::CreateZExtOrBitCast(V, Ty, "tmp.", InsertPt); } Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { - const Type *Ty = S->getType(); - if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType(); + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); Value *V = expand(S->getOperand()); - if (isa<PointerType>(V->getType())) - V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType()); + V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); return CastInst::CreateSExtOrBitCast(V, Ty, "tmp.", InsertPt); } Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { - const Type *Ty = S->getType(); + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); Value *LHS = expand(S->getOperand(0)); - LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); + LHS = InsertNoopCastOfTo(LHS, Ty); for (unsigned i = 1; i < S->getNumOperands(); ++i) { Value *RHS = expand(S->getOperand(i)); - RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false), - RHS, Ty); + RHS = InsertNoopCastOfTo(RHS, Ty); Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt); LHS = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt); } @@ -315,13 +315,12 @@ Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { } Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { - const Type *Ty = S->getType(); + const Type *Ty = SE.getEffectiveSCEVType(S->getType()); Value *LHS = expand(S->getOperand(0)); - LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty); + LHS = InsertNoopCastOfTo(LHS, Ty); for (unsigned i = 1; i < S->getNumOperands(); ++i) { Value *RHS = expand(S->getOperand(i)); - RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false), - RHS, Ty); + RHS = InsertNoopCastOfTo(RHS, Ty); Value *ICmp = new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt); LHS = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt); } @@ -331,11 +330,11 @@ Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty, Instruction *IP) { // Expand the code for this SCEV. - assert(TD.getTypeSizeInBits(Ty) == TD.getTypeSizeInBits(SH->getType()) && + assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && "non-trivial casts should be done with the SCEVs directly!"); this->InsertPt = IP; Value *V = expand(SH); - return InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty); + return InsertNoopCastOfTo(V, Ty); } Value *SCEVExpander::expand(const SCEV *S) { diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index 42fb9b9147..3e944d3857 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -50,7 +50,6 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/Debug.h" #include "llvm/Support/GetElementPtrTypeIterator.h" -#include "llvm/Target/TargetData.h" #include "llvm/Transforms/Utils/Local.h" #include "llvm/Support/CommandLine.h" #include "llvm/ADT/SmallVector.h" @@ -67,7 +66,6 @@ STATISTIC(NumLFTR , "Number of loop exit tests replaced"); namespace { class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass { LoopInfo *LI; - TargetData *TD; ScalarEvolution *SE; bool Changed; public: @@ -82,7 +80,6 @@ namespace { AU.addRequiredID(LCSSAID); AU.addRequiredID(LoopSimplifyID); AU.addRequired<LoopInfo>(); - AU.addRequired<TargetData>(); AU.addPreserved<ScalarEvolution>(); AU.addPreservedID(LoopSimplifyID); AU.addPreservedID(LCSSAID); @@ -217,7 +214,7 @@ void IndVarSimplify::RewriteLoopExitValues(Loop *L, // Scan all of the instructions in the loop, looking at those that have // extra-loop users and which are recurrences. - SCEVExpander Rewriter(*SE, *LI, *TD); + SCEVExpander Rewriter(*SE, *LI); // We insert the code into the preheader of the loop if the loop contains // multiple exit blocks, or in the exit block if there is exactly one. @@ -350,7 +347,7 @@ void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { /// induction-variable PHINode Phi is cast to. /// static const Type *getEffectiveIndvarType(const PHINode *Phi, - const TargetData *TD) { + const ScalarEvolution *SE) { const Type *Ty = Phi->getType(); for (Value::use_const_iterator UI = Phi->use_begin(), UE = Phi->use_end(); @@ -360,8 +357,13 @@ static const Type *getEffectiveIndvarType(const PHINode *Phi, CandidateType = ZI->getDestTy(); else if (const SExtInst *SI = dyn_cast<SExtInst>(UI)) CandidateType = SI->getDestTy(); + else if (const IntToPtrInst *IP = dyn_cast<IntToPtrInst>(UI)) + CandidateType = IP->getDestTy(); + else if (const PtrToIntInst *PI = dyn_cas |