| //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// |
| // |
| // The LLVM Compiler Infrastructure |
| // |
| // This file is distributed under the University of Illinois Open Source |
| // License. See LICENSE.TXT for details. |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // This file contains the implementation of the scalar evolution analysis |
| // engine, which is used primarily to analyze expressions involving induction |
| // variables in loops. |
| // |
| // There are several aspects to this library. First is the representation of |
| // scalar expressions, which are represented as subclasses of the SCEV class. |
| // These classes are used to represent certain types of subexpressions that we |
| // can handle. We only create one SCEV of a particular shape, so |
| // pointer-comparisons for equality are legal. |
| // |
| // One important aspect of the SCEV objects is that they are never cyclic, even |
| // if there is a cycle in the dataflow for an expression (ie, a PHI node). If |
| // the PHI node is one of the idioms that we can represent (e.g., a polynomial |
| // recurrence) then we represent it directly as a recurrence node, otherwise we |
| // represent it as a SCEVUnknown node. |
| // |
| // In addition to being able to represent expressions of various types, we also |
| // have folders that are used to build the *canonical* representation for a |
| // particular expression. These folders are capable of using a variety of |
| // rewrite rules to simplify the expressions. |
| // |
| // Once the folders are defined, we can implement the more interesting |
| // higher-level code, such as the code that recognizes PHI nodes of various |
| // types, computes the execution count of a loop, etc. |
| // |
| // TODO: We should use these routines and value representations to implement |
| // dependence analysis! |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // There are several good references for the techniques used in this analysis. |
| // |
| // Chains of recurrences -- a method to expedite the evaluation |
| // of closed-form functions |
| // Olaf Bachmann, Paul S. Wang, Eugene V. Zima |
| // |
| // On computational properties of chains of recurrences |
| // Eugene V. Zima |
| // |
| // Symbolic Evaluation of Chains of Recurrences for Loop Optimization |
| // Robert A. van Engelen |
| // |
| // Efficient Symbolic Analysis for Optimizing Compilers |
| // Robert A. van Engelen |
| // |
| // Using the chains of recurrences algebra for data dependence testing and |
| // induction variable substitution |
| // MS Thesis, Johnie Birch |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #define DEBUG_TYPE "scalar-evolution" |
| #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| #include "llvm/Constants.h" |
| #include "llvm/DerivedTypes.h" |
| #include "llvm/GlobalVariable.h" |
| #include "llvm/Instructions.h" |
| #include "llvm/LLVMContext.h" |
| #include "llvm/Operator.h" |
| #include "llvm/Analysis/ConstantFolding.h" |
| #include "llvm/Analysis/Dominators.h" |
| #include "llvm/Analysis/LoopInfo.h" |
| #include "llvm/Analysis/ValueTracking.h" |
| #include "llvm/Assembly/Writer.h" |
| #include "llvm/Target/TargetData.h" |
| #include "llvm/Support/CommandLine.h" |
| #include "llvm/Support/Compiler.h" |
| #include "llvm/Support/ConstantRange.h" |
| #include "llvm/Support/ErrorHandling.h" |
| #include "llvm/Support/GetElementPtrTypeIterator.h" |
| #include "llvm/Support/InstIterator.h" |
| #include "llvm/Support/MathExtras.h" |
| #include "llvm/Support/raw_ostream.h" |
| #include "llvm/ADT/Statistic.h" |
| #include "llvm/ADT/STLExtras.h" |
| #include "llvm/ADT/SmallPtrSet.h" |
| #include <algorithm> |
| using namespace llvm; |
| |
| STATISTIC(NumArrayLenItCounts, |
| "Number of trip counts computed with array length"); |
| STATISTIC(NumTripCountsComputed, |
| "Number of loops with predictable loop counts"); |
| STATISTIC(NumTripCountsNotComputed, |
| "Number of loops without predictable loop counts"); |
| STATISTIC(NumBruteForceTripCountsComputed, |
| "Number of loops with trip counts computed by force"); |
| |
| static cl::opt<unsigned> |
| MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| cl::desc("Maximum number of iterations SCEV will " |
| "symbolically execute a constant " |
| "derived loop"), |
| cl::init(100)); |
| |
| static RegisterPass<ScalarEvolution> |
| R("scalar-evolution", "Scalar Evolution Analysis", false, true); |
| char ScalarEvolution::ID = 0; |
| |
| //===----------------------------------------------------------------------===// |
| // SCEV class definitions |
| //===----------------------------------------------------------------------===// |
| |
| //===----------------------------------------------------------------------===// |
| // Implementation of the SCEV class. |
| // |
| |
| SCEV::~SCEV() {} |
| |
| void SCEV::dump() const { |
| print(errs()); |
| errs() << '\n'; |
| } |
| |
| void SCEV::print(std::ostream &o) const { |
| raw_os_ostream OS(o); |
| print(OS); |
| } |
| |
| bool SCEV::isZero() const { |
| if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| return SC->getValue()->isZero(); |
| return false; |
| } |
| |
| bool SCEV::isOne() const { |
| if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| return SC->getValue()->isOne(); |
| return false; |
| } |
| |
| bool SCEV::isAllOnesValue() const { |
| if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| return SC->getValue()->isAllOnesValue(); |
| return false; |
| } |
| |
| SCEVCouldNotCompute::SCEVCouldNotCompute() : |
| SCEV(FoldingSetNodeID(), scCouldNotCompute) {} |
| |
| bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { |
| llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| return false; |
| } |
| |
| const Type *SCEVCouldNotCompute::getType() const { |
| llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| return 0; |
| } |
| |
| bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { |
| llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| return false; |
| } |
| |
| bool SCEVCouldNotCompute::hasOperand(const SCEV *) const { |
| llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| return false; |
| } |
| |
| void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
| OS << "***COULDNOTCOMPUTE***"; |
| } |
| |
| bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| return S->getSCEVType() == scCouldNotCompute; |
| } |
| |
| const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
| FoldingSetNodeID ID; |
| ID.AddInteger(scConstant); |
| ID.AddPointer(V); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); |
| new (S) SCEVConstant(ID, V); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| const SCEV *ScalarEvolution::getConstant(const APInt& Val) { |
| return getConstant(ConstantInt::get(getContext(), Val)); |
| } |
| |
| const SCEV * |
| ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
| return getConstant( |
| ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); |
| } |
| |
| const Type *SCEVConstant::getType() const { return V->getType(); } |
| |
| void SCEVConstant::print(raw_ostream &OS) const { |
| WriteAsOperand(OS, V, false); |
| } |
| |
| SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID, |
| unsigned SCEVTy, const SCEV *op, const Type *ty) |
| : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} |
| |
| bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| return Op->dominates(BB, DT); |
| } |
| |
| SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID, |
| const SCEV *op, const Type *ty) |
| : SCEVCastExpr(ID, scTruncate, op, ty) { |
| assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| (Ty->isInteger() || isa<PointerType>(Ty)) && |
| "Cannot truncate non-integer value!"); |
| } |
| |
| void SCEVTruncateExpr::print(raw_ostream &OS) const { |
| OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
| } |
| |
| SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID, |
| const SCEV *op, const Type *ty) |
| : SCEVCastExpr(ID, scZeroExtend, op, ty) { |
| assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| (Ty->isInteger() || isa<PointerType>(Ty)) && |
| "Cannot zero extend non-integer value!"); |
| } |
| |
| void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
| OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
| } |
| |
| SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID, |
| const SCEV *op, const Type *ty) |
| : SCEVCastExpr(ID, scSignExtend, op, ty) { |
| assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| (Ty->isInteger() || isa<PointerType>(Ty)) && |
| "Cannot sign extend non-integer value!"); |
| } |
| |
| void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
| OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
| } |
| |
| void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
| assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); |
| const char *OpStr = getOperationStr(); |
| OS << "(" << *Operands[0]; |
| for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| OS << OpStr << *Operands[i]; |
| OS << ")"; |
| } |
| |
| bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| if (!getOperand(i)->dominates(BB, DT)) |
| return false; |
| } |
| return true; |
| } |
| |
| bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); |
| } |
| |
| void SCEVUDivExpr::print(raw_ostream &OS) const { |
| OS << "(" << *LHS << " /u " << *RHS << ")"; |
| } |
| |
| const Type *SCEVUDivExpr::getType() const { |
| // In most cases the types of LHS and RHS will be the same, but in some |
| // crazy cases one or the other may be a pointer. ScalarEvolution doesn't |
| // depend on the type for correctness, but handling types carefully can |
| // avoid extra casts in the SCEVExpander. The LHS is more likely to be |
| // a pointer type than the RHS, so use the RHS' type here. |
| return RHS->getType(); |
| } |
| |
| bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { |
| // Add recurrences are never invariant in the function-body (null loop). |
| if (!QueryLoop) |
| return false; |
| |
| // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. |
| if (QueryLoop->contains(L->getHeader())) |
| return false; |
| |
| // This recurrence is variant w.r.t. QueryLoop if any of its operands |
| // are variant. |
| for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| if (!getOperand(i)->isLoopInvariant(QueryLoop)) |
| return false; |
| |
| // Otherwise it's loop-invariant. |
| return true; |
| } |
| |
| void SCEVAddRecExpr::print(raw_ostream &OS) const { |
| OS << "{" << *Operands[0]; |
| for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| OS << ",+," << *Operands[i]; |
| OS << "}<" << L->getHeader()->getName() + ">"; |
| } |
| |
| void SCEVFieldOffsetExpr::print(raw_ostream &OS) const { |
| // LLVM struct fields don't have names, so just print the field number. |
| OS << "offsetof(" << *STy << ", " << FieldNo << ")"; |
| } |
| |
| void SCEVAllocSizeExpr::print(raw_ostream &OS) const { |
| OS << "sizeof(" << *AllocTy << ")"; |
| } |
| |
| bool SCEVUnknown::isLoopInvariant(const Loop *L) const { |
| // All non-instruction values are loop invariant. All instructions are loop |
| // invariant if they are not contained in the specified loop. |
| // Instructions are never considered invariant in the function body |
| // (null loop) because they are defined within the "loop". |
| if (Instruction *I = dyn_cast<Instruction>(V)) |
| return L && !L->contains(I->getParent()); |
| return true; |
| } |
| |
| bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| return DT->dominates(I->getParent(), BB); |
| return true; |
| } |
| |
| const Type *SCEVUnknown::getType() const { |
| return V->getType(); |
| } |
| |
| void SCEVUnknown::print(raw_ostream &OS) const { |
| WriteAsOperand(OS, V, false); |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // SCEV Utilities |
| //===----------------------------------------------------------------------===// |
| |
| static bool CompareTypes(const Type *A, const Type *B) { |
| if (A->getTypeID() != B->getTypeID()) |
| return A->getTypeID() < B->getTypeID(); |
| if (const IntegerType *AI = dyn_cast<IntegerType>(A)) { |
| const IntegerType *BI = cast<IntegerType>(B); |
| return AI->getBitWidth() < BI->getBitWidth(); |
| } |
| if (const PointerType *AI = dyn_cast<PointerType>(A)) { |
| const PointerType *BI = cast<PointerType>(B); |
| return CompareTypes(AI->getElementType(), BI->getElementType()); |
| } |
| if (const ArrayType *AI = dyn_cast<ArrayType>(A)) { |
| const ArrayType *BI = cast<ArrayType>(B); |
| if (AI->getNumElements() != BI->getNumElements()) |
| return AI->getNumElements() < BI->getNumElements(); |
| return CompareTypes(AI->getElementType(), BI->getElementType()); |
| } |
| if (const VectorType *AI = dyn_cast<VectorType>(A)) { |
| const VectorType *BI = cast<VectorType>(B); |
| if (AI->getNumElements() != BI->getNumElements()) |
| return AI->getNumElements() < BI->getNumElements(); |
| return CompareTypes(AI->getElementType(), BI->getElementType()); |
| } |
| if (const StructType *AI = dyn_cast<StructType>(A)) { |
| const StructType *BI = cast<StructType>(B); |
| if (AI->getNumElements() != BI->getNumElements()) |
| return AI->getNumElements() < BI->getNumElements(); |
| for (unsigned i = 0, e = AI->getNumElements(); i != e; ++i) |
| if (CompareTypes(AI->getElementType(i), BI->getElementType(i)) || |
| CompareTypes(BI->getElementType(i), AI->getElementType(i))) |
| return CompareTypes(AI->getElementType(i), BI->getElementType(i)); |
| } |
| return false; |
| } |
| |
| namespace { |
| /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| /// than the complexity of the RHS. This comparator is used to canonicalize |
| /// expressions. |
| class VISIBILITY_HIDDEN SCEVComplexityCompare { |
| LoopInfo *LI; |
| public: |
| explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} |
| |
| bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
| // Primarily, sort the SCEVs by their getSCEVType(). |
| if (LHS->getSCEVType() != RHS->getSCEVType()) |
| return LHS->getSCEVType() < RHS->getSCEVType(); |
| |
| // Aside from the getSCEVType() ordering, the particular ordering |
| // isn't very important except that it's beneficial to be consistent, |
| // so that (a + b) and (b + a) don't end up as different expressions. |
| |
| // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| // not as complete as it could be. |
| if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { |
| const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
| |
| // Order pointer values after integer values. This helps SCEVExpander |
| // form GEPs. |
| if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) |
| return false; |
| if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) |
| return true; |
| |
| // Compare getValueID values. |
| if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) |
| return LU->getValue()->getValueID() < RU->getValue()->getValueID(); |
| |
| // Sort arguments by their position. |
| if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { |
| const Argument *RA = cast<Argument>(RU->getValue()); |
| return LA->getArgNo() < RA->getArgNo(); |
| } |
| |
| // For instructions, compare their loop depth, and their opcode. |
| // This is pretty loose. |
| if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { |
| Instruction *RV = cast<Instruction>(RU->getValue()); |
| |
| // Compare loop depths. |
| if (LI->getLoopDepth(LV->getParent()) != |
| LI->getLoopDepth(RV->getParent())) |
| return LI->getLoopDepth(LV->getParent()) < |
| LI->getLoopDepth(RV->getParent()); |
| |
| // Compare opcodes. |
| if (LV->getOpcode() != RV->getOpcode()) |
| return LV->getOpcode() < RV->getOpcode(); |
| |
| // Compare the number of operands. |
| if (LV->getNumOperands() != RV->getNumOperands()) |
| return LV->getNumOperands() < RV->getNumOperands(); |
| } |
| |
| return false; |
| } |
| |
| // Compare constant values. |
| if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { |
| const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
| if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth()) |
| return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth(); |
| return LC->getValue()->getValue().ult(RC->getValue()->getValue()); |
| } |
| |
| // Compare addrec loop depths. |
| if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
| if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) |
| return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); |
| } |
| |
| // Lexicographically compare n-ary expressions. |
| if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { |
| const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
| for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { |
| if (i >= RC->getNumOperands()) |
| return false; |
| if (operator()(LC->getOperand(i), RC->getOperand(i))) |
| return true; |
| if (operator()(RC->getOperand(i), LC->getOperand(i))) |
| return false; |
| } |
| return LC->getNumOperands() < RC->getNumOperands(); |
| } |
| |
| // Lexicographically compare udiv expressions. |
| if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { |
| const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
| if (operator()(LC->getLHS(), RC->getLHS())) |
| return true; |
| if (operator()(RC->getLHS(), LC->getLHS())) |
| return false; |
| if (operator()(LC->getRHS(), RC->getRHS())) |
| return true; |
| if (operator()(RC->getRHS(), LC->getRHS())) |
| return false; |
| return false; |
| } |
| |
| // Compare cast expressions by operand. |
| if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { |
| const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
| return operator()(LC->getOperand(), RC->getOperand()); |
| } |
| |
| // Compare offsetof expressions. |
| if (const SCEVFieldOffsetExpr *LA = dyn_cast<SCEVFieldOffsetExpr>(LHS)) { |
| const SCEVFieldOffsetExpr *RA = cast<SCEVFieldOffsetExpr>(RHS); |
| if (CompareTypes(LA->getStructType(), RA->getStructType()) || |
| CompareTypes(RA->getStructType(), LA->getStructType())) |
| return CompareTypes(LA->getStructType(), RA->getStructType()); |
| return LA->getFieldNo() < RA->getFieldNo(); |
| } |
| |
| // Compare sizeof expressions by the allocation type. |
| if (const SCEVAllocSizeExpr *LA = dyn_cast<SCEVAllocSizeExpr>(LHS)) { |
| const SCEVAllocSizeExpr *RA = cast<SCEVAllocSizeExpr>(RHS); |
| return CompareTypes(LA->getAllocType(), RA->getAllocType()); |
| } |
| |
| llvm_unreachable("Unknown SCEV kind!"); |
| return false; |
| } |
| }; |
| } |
| |
| /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| /// complexity, and group objects of the same complexity together by value. |
| /// When this routine is finished, we know that any duplicates in the vector are |
| /// consecutive and that complexity is monotonically increasing. |
| /// |
| /// Note that we go take special precautions to ensure that we get determinstic |
| /// results from this routine. In other words, we don't want the results of |
| /// this to depend on where the addresses of various SCEV objects happened to |
| /// land in memory. |
| /// |
| static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, |
| LoopInfo *LI) { |
| if (Ops.size() < 2) return; // Noop |
| if (Ops.size() == 2) { |
| // This is the common case, which also happens to be trivially simple. |
| // Special case it. |
| if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) |
| std::swap(Ops[0], Ops[1]); |
| return; |
| } |
| |
| // Do the rough sort by complexity. |
| std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
| |
| // Now that we are sorted by complexity, group elements of the same |
| // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| // be extremely short in practice. Note that we take this approach because we |
| // do not want to depend on the addresses of the objects we are grouping. |
| for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
| const SCEV *S = Ops[i]; |
| unsigned Complexity = S->getSCEVType(); |
| |
| // If there are any objects of the same complexity and same value as this |
| // one, group them. |
| for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| if (Ops[j] == S) { // Found a duplicate. |
| // Move it to immediately after i'th element. |
| std::swap(Ops[i+1], Ops[j]); |
| ++i; // no need to rescan it. |
| if (i == e-2) return; // Done! |
| } |
| } |
| } |
| } |
| |
| |
| |
| //===----------------------------------------------------------------------===// |
| // Simple SCEV method implementations |
| //===----------------------------------------------------------------------===// |
| |
| /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
| /// Assume, K > 0. |
| static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
| ScalarEvolution &SE, |
| const Type* ResultTy) { |
| // Handle the simplest case efficiently. |
| if (K == 1) |
| return SE.getTruncateOrZeroExtend(It, ResultTy); |
| |
| // We are using the following formula for BC(It, K): |
| // |
| // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| // |
| // Suppose, W is the bitwidth of the return value. We must be prepared for |
| // overflow. Hence, we must assure that the result of our computation is |
| // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| // safe in modular arithmetic. |
| // |
| // However, this code doesn't use exactly that formula; the formula it uses |
| // is something like the following, where T is the number of factors of 2 in |
| // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| // exponentiation: |
| // |
| // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) |
| // |
| // This formula is trivially equivalent to the previous formula. However, |
| // this formula can be implemented much more efficiently. The trick is that |
| // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| // arithmetic. To do exact division in modular arithmetic, all we have |
| // to do is multiply by the inverse. Therefore, this step can be done at |
| // width W. |
| // |
| // The next issue is how to safely do the division by 2^T. The way this |
| // is done is by doing the multiplication step at a width of at least W + T |
| // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| // when we perform the division by 2^T (which is equivalent to a right shift |
| // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| // truncated out after the division by 2^T. |
| // |
| // In comparison to just directly using the first formula, this technique |
| // is much more efficient; using the first formula requires W * K bits, |
| // but this formula less than W + K bits. Also, the first formula requires |
| // a division step, whereas this formula only requires multiplies and shifts. |
| // |
| // It doesn't matter whether the subtraction step is done in the calculation |
| // width or the input iteration count's width; if the subtraction overflows, |
| // the result must be zero anyway. We prefer here to do it in the width of |
| // the induction variable because it helps a lot for certain cases; CodeGen |
| // isn't smart enough to ignore the overflow, which leads to much less |
| // efficient code if the width of the subtraction is wider than the native |
| // register width. |
| // |
| // (It's possible to not widen at all by pulling out factors of 2 before |
| // the multiplication; for example, K=2 can be calculated as |
| // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| // extra arithmetic, so it's not an obvious win, and it gets |
| // much more complicated for K > 3.) |
| |
| // Protection from insane SCEVs; this bound is conservative, |
| // but it probably doesn't matter. |
| if (K > 1000) |
| return SE.getCouldNotCompute(); |
| |
| 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. |
| // Other overflow doesn't matter because we only care about the bottom |
| // W bits of the result. |
| APInt OddFactorial(W, 1); |
| unsigned T = 1; |
| for (unsigned i = 3; i <= K; ++i) { |
| APInt Mult(W, i); |
| unsigned TwoFactors = Mult.countTrailingZeros(); |
| T += TwoFactors; |
| Mult = Mult.lshr(TwoFactors); |
| OddFactorial *= Mult; |
| } |
| |
| // We need at least W + T bits for the multiplication step |
| unsigned CalculationBits = W + T; |
| |
| // Calcuate 2^T, at width T+W. |
| APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| |
| // Calculate the multiplicative inverse of K! / 2^T; |
| // this multiplication factor will perform the exact division by |
| // K! / 2^T. |
| APInt Mod = APInt::getSignedMinValue(W+1); |
| APInt MultiplyFactor = OddFactorial.zext(W+1); |
| MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| MultiplyFactor = MultiplyFactor.trunc(W); |
| |
| // Calculate the product, at width T+W |
| const IntegerType *CalculationTy = IntegerType::get(SE.getContext(), |
| CalculationBits); |
| const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
| for (unsigned i = 1; i != K; ++i) { |
| const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); |
| Dividend = SE.getMulExpr(Dividend, |
| SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| } |
| |
| // Divide by 2^T |
| const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
| |
| // Truncate the result, and divide by K! / 2^T. |
| |
| return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
| } |
| |
| /// evaluateAtIteration - Return the value of this chain of recurrences at |
| /// the specified iteration number. We can evaluate this recurrence by |
| /// multiplying each element in the chain by the binomial coefficient |
| /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| /// |
| /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) |
| /// |
| /// where BC(It, k) stands for binomial coefficient. |
| /// |
| const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
| ScalarEvolution &SE) const { |
| const SCEV *Result = getStart(); |
| for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| // The computation is correct in the face of overflow provided that the |
| // multiplication is performed _after_ the evaluation of the binomial |
| // coefficient. |
| const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); |
| if (isa<SCEVCouldNotCompute>(Coeff)) |
| return Coeff; |
| |
| Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
| } |
| return Result; |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // SCEV Expression folder implementations |
| //===----------------------------------------------------------------------===// |
| |
| const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, |
| const Type *Ty) { |
| assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
| "This is not a truncating conversion!"); |
| assert(isSCEVable(Ty) && |
| "This is not a conversion to a SCEVable type!"); |
| Ty = getEffectiveSCEVType(Ty); |
| |
| FoldingSetNodeID ID; |
| ID.AddInteger(scTruncate); |
| ID.AddPointer(Op); |
| ID.AddPointer(Ty); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| |
| // Fold if the operand is constant. |
| if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| return getConstant( |
| cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
| |
| // trunc(trunc(x)) --> trunc(x) |
| if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
| return getTruncateExpr(ST->getOperand(), Ty); |
| |
| // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
| if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
| return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| |
| // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
| if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
| return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| |
| // If the input value is a chrec scev, truncate the chrec's operands. |
| if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
| SmallVector<const SCEV *, 4> Operands; |
| for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
| Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| return getAddRecExpr(Operands, AddRec->getLoop()); |
| } |
| |
| // The cast wasn't folded; create an explicit cast node. |
| // Recompute the insert position, as it may have been invalidated. |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); |
| new (S) SCEVTruncateExpr(ID, Op, Ty); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, |
| const Type *Ty) { |
| assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| "This is not an extending conversion!"); |
| assert(isSCEVable(Ty) && |
| "This is not a conversion to a SCEVable type!"); |
| Ty = getEffectiveSCEVType(Ty); |
| |
| // Fold if the operand is constant. |
| if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
| const Type *IntTy = getEffectiveSCEVType(Ty); |
| Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); |
| if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
| return getConstant(cast<ConstantInt>(C)); |
| } |
| |
| // zext(zext(x)) --> zext(x) |
| if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
| return getZeroExtendExpr(SZ->getOperand(), Ty); |
| |
| // Before doing any expensive analysis, check to see if we've already |
| // computed a SCEV for this Op and Ty. |
| FoldingSetNodeID ID; |
| ID.AddInteger(scZeroExtend); |
| ID.AddPointer(Op); |
| ID.AddPointer(Ty); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| |
| // 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 |
| // operands (often constants). This allows analysis of something like |
| // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
| if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
| if (AR->isAffine()) { |
| const SCEV *Start = AR->getStart(); |
| const SCEV *Step = AR->getStepRecurrence(*this); |
| unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| const Loop *L = AR->getLoop(); |
| |
| // If we have special knowledge that this addrec won't overflow, |
| // we don't need to do any further analysis. |
| if (AR->hasNoUnsignedWrap()) |
| return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| getZeroExtendExpr(Step, Ty), |
| L); |
| |
| // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| // Note that this serves two purposes: It filters out loops that are |
| // simply not analyzable, and it covers the case where this code is |
| // being called from within backedge-taken count analysis, such that |
| // attempting to ask for the backedge-taken count would likely result |
| // in infinite recursion. In the later case, the analysis code will |
| // cope with a conservative value, and it will take care to purge |
| // that value once it has finished. |
| const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
| if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
| // Manually compute the final value for AR, checking for |
| // overflow. |
| |
| // Check whether the backedge-taken count can be losslessly casted to |
| // the addrec's type. The count is always unsigned. |
| const SCEV *CastedMaxBECount = |
| getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
| const SCEV *RecastedMaxBECount = |
| getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| if (MaxBECount == RecastedMaxBECount) { |
| const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
| // Check whether Start+Step*MaxBECount has no unsigned overflow. |
| const SCEV *ZMul = |
| getMulExpr(CastedMaxBECount, |
| getTruncateOrZeroExtend(Step, Start->getType())); |
| const SCEV *Add = getAddExpr(Start, ZMul); |
| const SCEV *OperandExtendedAdd = |
| getAddExpr(getZeroExtendExpr(Start, WideTy), |
| getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| getZeroExtendExpr(Step, WideTy))); |
| if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
| // Return the expression with the addrec on the outside. |
| return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| getZeroExtendExpr(Step, Ty), |
| L); |
| |
| // Similar to above, only this time treat the step value as signed. |
| // This covers loops that count down. |
| const SCEV *SMul = |
| getMulExpr(CastedMaxBECount, |
| getTruncateOrSignExtend(Step, Start->getType())); |
| Add = getAddExpr(Start, SMul); |
| OperandExtendedAdd = |
| getAddExpr(getZeroExtendExpr(Start, WideTy), |
| getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| getSignExtendExpr(Step, WideTy))); |
| if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
| // Return the expression with the addrec on the outside. |
| return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| getSignExtendExpr(Step, Ty), |
| L); |
| } |
| |
| // If the backedge is guarded by a comparison with the pre-inc value |
| // the addrec is safe. Also, if the entry is guarded by a comparison |
| // with the start value and the backedge is guarded by a comparison |
| // with the post-inc value, the addrec is safe. |
| if (isKnownPositive(Step)) { |
| const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
| getUnsignedRange(Step).getUnsignedMax()); |
| if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
| (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && |
| isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, |
| AR->getPostIncExpr(*this), N))) |
| // Return the expression with the addrec on the outside. |
| return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| getZeroExtendExpr(Step, Ty), |
| L); |
| } else if (isKnownNegative(Step)) { |
| const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
| getSignedRange(Step).getSignedMin()); |
| if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) && |
| (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) || |
| isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, |
| AR->getPostIncExpr(*this), N))) |
| // Return the expression with the addrec on the outside. |
| return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| getSignExtendExpr(Step, Ty), |
| L); |
| } |
| } |
| } |
| |
| // The cast wasn't folded; create an explicit cast node. |
| // Recompute the insert position, as it may have been invalidated. |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); |
| new (S) SCEVZeroExtendExpr(ID, Op, Ty); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, |
| const Type *Ty) { |
| assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| "This is not an extending conversion!"); |
| assert(isSCEVable(Ty) && |
| "This is not a conversion to a SCEVable type!"); |
| Ty = getEffectiveSCEVType(Ty); |
| |
| // Fold if the operand is constant. |
| if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
| const Type *IntTy = getEffectiveSCEVType(Ty); |
| Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); |
| if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
| return getConstant(cast<ConstantInt>(C)); |
| } |
| |
| // sext(sext(x)) --> sext(x) |
| if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
| return getSignExtendExpr(SS->getOperand(), Ty); |
| |
| // Before doing any expensive analysis, check to see if we've already |
| // computed a SCEV for this Op and Ty. |
| FoldingSetNodeID ID; |
| ID.AddInteger(scSignExtend); |
| ID.AddPointer(Op); |
| ID.AddPointer(Ty); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| |
| // If the input value is a chrec scev, and we can prove that the value |
| // did not overflow the old, smaller, value, we can sign extend all of the |
| // operands (often constants). This allows analysis of something like |
| // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
| if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
| if (AR->isAffine()) { |
| const SCEV *Start = AR->getStart(); |
| const SCEV *Step = AR->getStepRecurrence(*this); |
| unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| const Loop *L = AR->getLoop(); |
| |
| // If we have special knowledge that this addrec won't overflow, |
| // we don't need to do any further analysis. |
| if (AR->hasNoSignedWrap()) |
| return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| getSignExtendExpr(Step, Ty), |
| L); |
| |
| // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| // Note that this serves two purposes: It filters out loops that are |
| // simply not analyzable, and it covers the case where this code is |
| // being called from within backedge-taken count analysis, such that |
| // attempting to ask for the backedge-taken count would likely result |
| // in infinite recursion. In the later case, the analysis code will |
| // cope with a conservative value, and it will take care to purge |
| // that value once it has finished. |
| const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
| if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
| // Manually compute the final value for AR, checking for |
| // overflow. |
| |
| // Check whether the backedge-taken count can be losslessly casted to |
| // the addrec's type. The count is always unsigned. |
| const SCEV *CastedMaxBECount = |
| getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
| const SCEV *RecastedMaxBECount = |
| getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| if (MaxBECount == RecastedMaxBECount) { |
| const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
| // Check whether Start+Step*MaxBECount has no signed overflow. |
| const SCEV *SMul = |
| getMulExpr(CastedMaxBECount, |
| getTruncateOrSignExtend(Step, Start->getType())); |
| const SCEV *Add = getAddExpr(Start, SMul); |
| const SCEV *OperandExtendedAdd = |
| getAddExpr(getSignExtendExpr(Start, WideTy), |
| getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| getSignExtendExpr(Step, WideTy))); |
| if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
| // Return the expression with the addrec on the outside. |
| return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| getSignExtendExpr(Step, Ty), |
| L); |
| |
| // Similar to above, only this time treat the step value as unsigned. |
| // This covers loops that count up with an unsigned step. |
| const SCEV *UMul = |
| getMulExpr(CastedMaxBECount, |
| getTruncateOrZeroExtend(Step, Start->getType())); |
| Add = getAddExpr(Start, UMul); |
| OperandExtendedAdd = |
| getAddExpr(getSignExtendExpr(Start, WideTy), |
| getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| getZeroExtendExpr(Step, WideTy))); |
| if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
| // Return the expression with the addrec on the outside. |
| return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| getZeroExtendExpr(Step, Ty), |
| L); |
| } |
| |
| // If the backedge is guarded by a comparison with the pre-inc value |
| // the addrec is safe. Also, if the entry is guarded by a comparison |
| // with the start value and the backedge is guarded by a comparison |
| // with the post-inc value, the addrec is safe. |
| if (isKnownPositive(Step)) { |
| const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) - |
| getSignedRange(Step).getSignedMax()); |
| if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) || |
| (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) && |
| isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, |
| AR->getPostIncExpr(*this), N))) |
| // Return the expression with the addrec on the outside. |
| return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| getSignExtendExpr(Step, Ty), |
| L); |
| } else if (isKnownNegative(Step)) { |
| const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) - |
| getSignedRange(Step).getSignedMin()); |
| if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) || |
| (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) && |
| isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, |
| AR->getPostIncExpr(*this), N))) |
| // Return the expression with the addrec on the outside. |
| return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| getSignExtendExpr(Step, Ty), |
| L); |
| } |
| } |
| } |
| |
| // The cast wasn't folded; create an explicit cast node. |
| // Recompute the insert position, as it may have been invalidated. |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); |
| new (S) SCEVSignExtendExpr(ID, Op, Ty); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| /// unspecified bits out to the given type. |
| /// |
| const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
| const Type *Ty) { |
| assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| "This is not an extending conversion!"); |
| assert(isSCEVable(Ty) && |
| "This is not a conversion to a SCEVable type!"); |
| Ty = getEffectiveSCEVType(Ty); |
| |
| // Sign-extend negative constants. |
| if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| if (SC->getValue()->getValue().isNegative()) |
| return getSignExtendExpr(Op, Ty); |
| |
| // Peel off a truncate cast. |
| if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
| const SCEV *NewOp = T->getOperand(); |
| if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| return getAnyExtendExpr(NewOp, Ty); |
| return getTruncateOrNoop(NewOp, Ty); |
| } |
| |
| // Next try a zext cast. If the cast is folded, use it. |
| const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
| if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| return ZExt; |
| |
| // Next try a sext cast. If the cast is folded, use it. |
| const SCEV *SExt = getSignExtendExpr(Op, Ty); |
| if (!isa<SCEVSignExtendExpr>(SExt)) |
| return SExt; |
| |
| // If the expression is obviously signed, use the sext cast value. |
| if (isa<SCEVSMaxExpr>(Op)) |
| return SExt; |
| |
| // Absent any other information, use the zext cast value. |
| return ZExt; |
| } |
| |
| /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| /// a list of operands to be added under the given scale, update the given |
| /// map. This is a helper function for getAddRecExpr. As an example of |
| /// what it does, given a sequence of operands that would form an add |
| /// expression like this: |
| /// |
| /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| /// |
| /// where A and B are constants, update the map with these values: |
| /// |
| /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| /// |
| /// and add 13 + A*B*29 to AccumulatedConstant. |
| /// This will allow getAddRecExpr to produce this: |
| /// |
| /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| /// |
| /// This form often exposes folding opportunities that are hidden in |
| /// the original operand list. |
| /// |
| /// Return true iff it appears that any interesting folding opportunities |
| /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| /// the common case where no interesting opportunities are present, and |
| /// is also used as a check to avoid infinite recursion. |
| /// |
| static bool |
| CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, |
| SmallVector<const SCEV *, 8> &NewOps, |
| APInt &AccumulatedConstant, |
| const SmallVectorImpl<const SCEV *> &Ops, |
| const APInt &Scale, |
| ScalarEvolution &SE) { |
| bool Interesting = false; |
| |
| // Iterate over the add operands. |
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| APInt NewScale = |
| Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| // A multiplication of a constant with another add; recurse. |
| Interesting |= |
| CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| cast<SCEVAddExpr>(Mul->getOperand(1)) |
| ->getOperands(), |
| NewScale, SE); |
| } else { |
| // A multiplication of a constant with some other value. Update |
| // the map. |
| SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| const SCEV *Key = SE.getMulExpr(MulOps); |
| std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
| M.insert(std::make_pair(Key, NewScale)); |
| if (Pair.second) { |
| NewOps.push_back(Pair.first->first); |
| } else { |
| Pair.first->second += NewScale; |
| // The map already had an entry for this value, which may indicate |
| // a folding opportunity. |
| Interesting = true; |
| } |
| } |
| } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| // Pull a buried constant out to the outside. |
| if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) |
| Interesting = true; |
| AccumulatedConstant += Scale * C->getValue()->getValue(); |
| } else { |
| // An ordinary operand. Update the map. |
| std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
| M.insert(std::make_pair(Ops[i], Scale)); |
| if (Pair.second) { |
| NewOps.push_back(Pair.first->first); |
| } else { |
| Pair.first->second += Scale; |
| // The map already had an entry for this value, which may indicate |
| // a folding opportunity. |
| Interesting = true; |
| } |
| } |
| } |
| |
| return Interesting; |
| } |
| |
| namespace { |
| struct APIntCompare { |
| bool operator()(const APInt &LHS, const APInt &RHS) const { |
| return LHS.ult(RHS); |
| } |
| }; |
| } |
| |
| /// getAddExpr - Get a canonical add expression, or something simpler if |
| /// possible. |
| const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops) { |
| assert(!Ops.empty() && "Cannot get empty add!"); |
| if (Ops.size() == 1) return Ops[0]; |
| #ifndef NDEBUG |
| for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| getEffectiveSCEVType(Ops[0]->getType()) && |
| "SCEVAddExpr operand types don't match!"); |
| #endif |
| |
| // Sort by complexity, this groups all similar expression types together. |
| GroupByComplexity(Ops, LI); |
| |
| // If there are any constants, fold them together. |
| unsigned Idx = 0; |
| if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
| ++Idx; |
| assert(Idx < Ops.size()); |
| while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
| // We found two constants, fold them together! |
| Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| RHSC->getValue()->getValue()); |
| if (Ops.size() == 2) return Ops[0]; |
| Ops.erase(Ops.begin()+1); // Erase the folded element |
| LHSC = cast<SCEVConstant>(Ops[0]); |
| } |
| |
| // If we are left with a constant zero being added, strip it off. |
| if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| Ops.erase(Ops.begin()); |
| --Idx; |
| } |
| } |
| |
| if (Ops.size() == 1) return Ops[0]; |
| |
| // Okay, check to see if the same value occurs in the operand list twice. If |
| // so, merge them together into an multiply expression. Since we sorted the |
| // list, these values are required to be adjacent. |
| const Type *Ty = Ops[0]->getType(); |
| for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
| // Found a match, merge the two values into a multiply, and add any |
| // remaining values to the result. |
| const SCEV *Two = getIntegerSCEV(2, Ty); |
| const SCEV *Mul = getMulExpr(Ops[i], Two); |
| if (Ops.size() == 2) |
| return Mul; |
| Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| Ops.push_back(Mul); |
| return getAddExpr(Ops); |
| } |
| |
| // Check for truncates. If all the operands are truncated from the same |
| // type, see if factoring out the truncate would permit the result to be |
| // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| // if the contents of the resulting outer trunc fold to something simple. |
| for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| const Type *DstType = Trunc->getType(); |
| const Type *SrcType = Trunc->getOperand()->getType(); |
| SmallVector<const SCEV *, 8> LargeOps; |
| bool Ok = true; |
| // Check all the operands to see if they can be represented in the |
| // source type of the truncate. |
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| if (T->getOperand()->getType() != SrcType) { |
| Ok = false; |
| break; |
| } |
| LargeOps.push_back(T->getOperand()); |
| } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| // This could be either sign or zero extension, but sign extension |
| // is much more likely to be foldable here. |
| LargeOps.push_back(getSignExtendExpr(C, SrcType)); |
| } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
| SmallVector<const SCEV *, 8> LargeMulOps; |
| for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| if (const SCEVTruncateExpr *T = |
| dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| if (T->getOperand()->getType() != SrcType) { |
| Ok = false; |
| break; |
| } |
| LargeMulOps.push_back(T->getOperand()); |
| } else if (const SCEVConstant *C = |
| dyn_cast<SCEVConstant>(M->getOperand(j))) { |
| // This could be either sign or zero extension, but sign extension |
| // is much more likely to be foldable here. |
| LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); |
| } else { |
| Ok = false; |
| break; |
| } |
| } |
| if (Ok) |
| LargeOps.push_back(getMulExpr(LargeMulOps)); |
| } else { |
| Ok = false; |
| break; |
| } |
| } |
| if (Ok) { |
| // Evaluate the expression in the larger type. |
| const SCEV *Fold = getAddExpr(LargeOps); |
| // If it folds to something simple, use it. Otherwise, don't. |
| if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| return getTruncateExpr(Fold, DstType); |
| } |
| } |
| |
| // Skip past any other cast SCEVs. |
| while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| ++Idx; |
| |
| // If there are add operands they would be next. |
| if (Idx < Ops.size()) { |
| bool DeletedAdd = false; |
| while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
| // If we have an add, expand the add operands onto the end of the operands |
| // list. |
| Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| Ops.erase(Ops.begin()+Idx); |
| DeletedAdd = true; |
| } |
| |
| // If we deleted at least one add, we added operands to the end of the list, |
| // and they are not necessarily sorted. Recurse to resort and resimplify |
| // any operands we just aquired. |
| if (DeletedAdd) |
| return getAddExpr(Ops); |
| } |
| |
| // Skip over the add expression until we get to a multiply. |
| while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| ++Idx; |
| |
| // Check to see if there are any folding opportunities present with |
| // operands multiplied by constant values. |
| if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| uint64_t BitWidth = getTypeSizeInBits(Ty); |
| DenseMap<const SCEV *, APInt> M; |
| SmallVector<const SCEV *, 8> NewOps; |
| APInt AccumulatedConstant(BitWidth, 0); |
| if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| Ops, APInt(BitWidth, 1), *this)) { |
| // Some interesting folding opportunity is present, so its worthwhile to |
| // re-generate the operands list. Group the operands by constant scale, |
| // to avoid multiplying by the same constant scale multiple times. |
| std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; |
| for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(), |
| E = NewOps.end(); I != E; ++I) |
| MulOpLists[M.find(*I)->second].push_back(*I); |
| // Re-generate the operands list. |
| Ops.clear(); |
| if (AccumulatedConstant != 0) |
| Ops.push_back(getConstant(AccumulatedConstant)); |
| for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
| if (I->first != 0) |
| Ops.push_back(getMulExpr(getConstant(I->first), |
| getAddExpr(I->second))); |
| if (Ops.empty()) |
| return getIntegerSCEV(0, Ty); |
| if (Ops.size() == 1) |
| return Ops[0]; |
| return getAddExpr(Ops); |
| } |
| } |
| |
| // If we are adding something to a multiply expression, make sure the |
| // something is not already an operand of the multiply. If so, merge it into |
| // the multiply. |
| for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
| const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
| for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
| const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
| for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
| if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { |
| // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
| const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
| if (Mul->getNumOperands() != 2) { |
| // If the multiply has more than two operands, we must get the |
| // Y*Z term. |
| SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end()); |
| MulOps.erase(MulOps.begin()+MulOp); |
| InnerMul = getMulExpr(MulOps); |
| } |
| const SCEV *One = getIntegerSCEV(1, Ty); |
| const SCEV *AddOne = getAddExpr(InnerMul, One); |
| const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]); |
| if (Ops.size() == 2) return OuterMul; |
| if (AddOp < Idx) { |
| Ops.erase(Ops.begin()+AddOp); |
| Ops.erase(Ops.begin()+Idx-1); |
| } else { |
| Ops.erase(Ops.begin()+Idx); |
| Ops.erase(Ops.begin()+AddOp-1); |
| } |
| Ops.push_back(OuterMul); |
| return getAddExpr(Ops); |
| } |
| |
| // Check this multiply against other multiplies being added together. |
| for (unsigned OtherMulIdx = Idx+1; |
| OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| ++OtherMulIdx) { |
| const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
| // If MulOp occurs in OtherMul, we can fold the two multiplies |
| // together. |
| for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| OMulOp != e; ++OMulOp) |
| if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
| const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
| if (Mul->getNumOperands() != 2) { |
| SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| Mul->op_end()); |
| MulOps.erase(MulOps.begin()+MulOp); |
| InnerMul1 = getMulExpr(MulOps); |
| } |
| const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
| if (OtherMul->getNumOperands() != 2) { |
| SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
| OtherMul->op_end()); |
| MulOps.erase(MulOps.begin()+OMulOp); |
| InnerMul2 = getMulExpr(MulOps); |
| } |
| const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
| if (Ops.size() == 2) return OuterMul; |
| Ops.erase(Ops.begin()+Idx); |
| Ops.erase(Ops.begin()+OtherMulIdx-1); |
| Ops.push_back(OuterMul); |
| return getAddExpr(Ops); |
| } |
| } |
| } |
| } |
| |
| // If there are any add recurrences in the operands list, see if any other |
| // added values are loop invariant. If so, we can fold them into the |
| // recurrence. |
| while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| ++Idx; |
| |
| // Scan over all recurrences, trying to fold loop invariants into them. |
| for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| // Scan all of the other operands to this add and add them to the vector if |
| // they are loop invariant w.r.t. the recurrence. |
| SmallVector<const SCEV *, 8> LIOps; |
| const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| LIOps.push_back(Ops[i]); |
| Ops.erase(Ops.begin()+i); |
| --i; --e; |
| } |
| |
| // If we found some loop invariants, fold them into the recurrence. |
| if (!LIOps.empty()) { |
| // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
| LIOps.push_back(AddRec->getStart()); |
| |
| SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
| AddRec->op_end()); |
| AddRecOps[0] = getAddExpr(LIOps); |
| |
| const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); |
| // If all of the other operands were loop invariant, we are done. |
| if (Ops.size() == 1) return NewRec; |
| |
| // Otherwise, add the folded AddRec by the non-liv parts. |
| for (unsigned i = 0;; ++i) |
| if (Ops[i] == AddRec) { |
| Ops[i] = NewRec; |
| break; |
| } |
| return getAddExpr(Ops); |
| } |
| |
| // Okay, if there weren't any loop invariants to be folded, check to see if |
| // there are multiple AddRec's with the same loop induction variable being |
| // added together. If so, we can fold them. |
| for (unsigned OtherIdx = Idx+1; |
| OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| if (OtherIdx != Idx) { |
| const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
| if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
| SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), |
| AddRec->op_end()); |
| for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { |
| if (i >= NewOps.size()) { |
| NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, |
| OtherAddRec->op_end()); |
| break; |
| } |
| NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); |
| } |
| const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
| |
| if (Ops.size() == 2) return NewAddRec; |
| |
| Ops.erase(Ops.begin()+Idx); |
| Ops.erase(Ops.begin()+OtherIdx-1); |
| Ops.push_back(NewAddRec); |
| return getAddExpr(Ops); |
| } |
| } |
| |
| // Otherwise couldn't fold anything into this recurrence. Move onto the |
| // next one. |
| } |
| |
| // Okay, it looks like we really DO need an add expr. Check to see if we |
| // already have one, otherwise create a new one. |
| FoldingSetNodeID ID; |
| ID.AddInteger(scAddExpr); |
| ID.AddInteger(Ops.size()); |
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| ID.AddPointer(Ops[i]); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>(); |
| new (S) SCEVAddExpr(ID, Ops); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| |
| /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| /// possible. |
| const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops) { |
| assert(!Ops.empty() && "Cannot get empty mul!"); |
| #ifndef NDEBUG |
| for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| getEffectiveSCEVType(Ops[0]->getType()) && |
| "SCEVMulExpr operand types don't match!"); |
| #endif |
| |
| // Sort by complexity, this groups all similar expression types together. |
| GroupByComplexity(Ops, LI); |
| |
| // If there are any constants, fold them together. |
| unsigned Idx = 0; |
| if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
| |
| // C1*(C2+V) -> C1*C2 + C1*V |
| if (Ops.size() == 2) |
| if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
| if (Add->getNumOperands() == 2 && |
| isa<SCEVConstant>(Add->getOperand(0))) |
| return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| getMulExpr(LHSC, Add->getOperand(1))); |
| |
| |
| ++Idx; |
| while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
| // We found two constants, fold them together! |
| ConstantInt *Fold = ConstantInt::get(getContext(), |
| LHSC->getValue()->getValue() * |
| RHSC->getValue()->getValue()); |
| Ops[0] = getConstant(Fold); |
| Ops.erase(Ops.begin()+1); // Erase the folded element |
| if (Ops.size() == 1) return Ops[0]; |
| LHSC = cast<SCEVConstant>(Ops[0]); |
| } |
| |
| // If we are left with a constant one being multiplied, strip it off. |
| if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| Ops.erase(Ops.begin()); |
| --Idx; |
| } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| // If we have a multiply of zero, it will always be zero. |
| return Ops[0]; |
| } |
| } |
| |
| // Skip over the add expression until we get to a multiply. |
| while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| ++Idx; |
| |
| if (Ops.size() == 1) |
| return Ops[0]; |
| |
| // If there are mul operands inline them all into this expression. |
| if (Idx < Ops.size()) { |
| bool DeletedMul = false; |
| while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
| // If we have an mul, expand the mul operands onto the end of the operands |
| // list. |
| Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); |
| Ops.erase(Ops.begin()+Idx); |
| DeletedMul = true; |
| } |
| |
| // If we deleted at least one mul, we added operands to the end of the list, |
| // and they are not necessarily sorted. Recurse to resort and resimplify |
| // any operands we just aquired. |
| if (DeletedMul) |
| return getMulExpr(Ops); |
| } |
| |
| // If there are any add recurrences in the operands list, see if any other |
| // added values are loop invariant. If so, we can fold them into the |
| // recurrence. |
| while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| ++Idx; |
| |
| // Scan over all recurrences, trying to fold loop invariants into them. |
| for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| // Scan all of the other operands to this mul and add them to the vector if |
| // they are loop invariant w.r.t. the recurrence. |
| SmallVector<const SCEV *, 8> LIOps; |
| const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| LIOps.push_back(Ops[i]); |
| Ops.erase(Ops.begin()+i); |
| --i; --e; |
| } |
| |
| // If we found some loop invariants, fold them into the recurrence. |
| if (!LIOps.empty()) { |
| // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
| SmallVector<const SCEV *, 4> NewOps; |
| NewOps.reserve(AddRec->getNumOperands()); |
| if (LIOps.size() == 1) { |
| const SCEV *Scale = LIOps[0]; |
| for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
| NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
| } else { |
| for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
| SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end()); |
| MulOps.push_back(AddRec->getOperand(i)); |
| NewOps.push_back(getMulExpr(MulOps)); |
| } |
| } |
| |
| const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
| |
| // If all of the other operands were loop invariant, we are done. |
| if (Ops.size() == 1) return NewRec; |
| |
| // Otherwise, multiply the folded AddRec by the non-liv parts. |
| for (unsigned i = 0;; ++i) |
| if (Ops[i] == AddRec) { |
| Ops[i] = NewRec; |
| break; |
| } |
| return getMulExpr(Ops); |
| } |
| |
| // Okay, if there weren't any loop invariants to be folded, check to see if |
| // there are multiple AddRec's with the same loop induction variable being |
| // multiplied together. If so, we can fold them. |
| for (unsigned OtherIdx = Idx+1; |
| OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| if (OtherIdx != Idx) { |
| const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
| if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} |
| const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
| const SCEV *NewStart = getMulExpr(F->getStart(), |
| G->getStart()); |
| const SCEV *B = F->getStepRecurrence(*this); |
| const SCEV *D = G->getStepRecurrence(*this); |
| const SCEV *NewStep = getAddExpr(getMulExpr(F, D), |
| getMulExpr(G, B), |
| getMulExpr(B, D)); |
| const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, |
| F->getLoop()); |
| if (Ops.size() == 2) return NewAddRec; |
| |
| Ops.erase(Ops.begin()+Idx); |
| Ops.erase(Ops.begin()+OtherIdx-1); |
| Ops.push_back(NewAddRec); |
| return getMulExpr(Ops); |
| } |
| } |
| |
| // Otherwise couldn't fold anything into this recurrence. Move onto the |
| // next one. |
| } |
| |
| // Okay, it looks like we really DO need an mul expr. Check to see if we |
| // already have one, otherwise create a new one. |
| FoldingSetNodeID ID; |
| ID.AddInteger(scMulExpr); |
| ID.AddInteger(Ops.size()); |
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| ID.AddPointer(Ops[i]); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>(); |
| new (S) SCEVMulExpr(ID, Ops); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| /// getUDivExpr - Get a canonical unsigned division expression, or something |
| /// simpler if possible. |
| const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| const SCEV *RHS) { |
| assert(getEffectiveSCEVType(LHS->getType()) == |
| getEffectiveSCEVType(RHS->getType()) && |
| "SCEVUDivExpr operand types don't match!"); |
| |
| if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
| if (RHSC->getValue()->equalsInt(1)) |
| return LHS; // X udiv 1 --> x |
| if (RHSC->isZero()) |
| return getIntegerSCEV(0, LHS->getType()); // value is undefined |
| |
| // Determine if the division can be folded into the operands of |
| // its operands. |
| // TODO: Generalize this to non-constants by using known-bits information. |
| const Type *Ty = LHS->getType(); |
| unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
| unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; |
| // For non-power-of-two values, effectively round the value up to the |
| // nearest power of two. |
| if (!RHSC->getValue()->getValue().isPowerOf2()) |
| ++MaxShiftAmt; |
| const IntegerType *ExtTy = |
| IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); |
| // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| if (const SCEVConstant *Step = |
| dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| if (!Step->getValue()->getValue() |
| .urem(RHSC->getValue()->getValue()) && |
| getZeroExtendExpr(AR, ExtTy) == |
| getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| getZeroExtendExpr(Step, ExtTy), |
| AR->getLoop())) { |
| SmallVector<const SCEV *, 4> Operands; |
| for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| return getAddRecExpr(Operands, AR->getLoop()); |
| } |
| // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
| if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
| SmallVector<const SCEV *, 4> Operands; |
| for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
| // Find an operand that's safely divisible. |
| for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
| const SCEV *Op = M->getOperand(i); |
| const SCEV *Div = getUDivExpr(Op, RHSC); |
| if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
| const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands(); |
| Operands = SmallVector<const SCEV *, 4>(MOperands.begin(), |
| MOperands.end()); |
| Operands[i] = Div; |
| return getMulExpr(Operands); |
| } |
| } |
| } |
| // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
| if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| SmallVector<const SCEV *, 4> Operands; |
| for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| Operands.clear(); |
| for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
| const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
| if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) |
| break; |
| Operands.push_back(Op); |
| } |
| if (Operands.size() == A->getNumOperands()) |
| return getAddExpr(Operands); |
| } |
| } |
| |
| // Fold if both operands are constant. |
| if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
| Constant *LHSCV = LHSC->getValue(); |
| Constant *RHSCV = RHSC->getValue(); |
| return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
| RHSCV))); |
| } |
| } |
| |
| FoldingSetNodeID ID; |
| ID.AddInteger(scUDivExpr); |
| ID.AddPointer(LHS); |
| ID.AddPointer(RHS); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); |
| new (S) SCEVUDivExpr(ID, LHS, RHS); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| |
| /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| /// Simplify the expression as much as possible. |
| const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, |
| const SCEV *Step, const Loop *L) { |
| SmallVector<const SCEV *, 4> Operands; |
| Operands.push_back(Start); |
| if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
| if (StepChrec->getLoop() == L) { |
| Operands.insert(Operands.end(), StepChrec->op_begin(), |
| StepChrec->op_end()); |
| return getAddRecExpr(Operands, L); |
| } |
| |
| Operands.push_back(Step); |
| return getAddRecExpr(Operands, L); |
| } |
| |
| /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| /// Simplify the expression as much as possible. |
| const SCEV * |
| ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, |
| const Loop *L) { |
| if (Operands.size() == 1) return Operands[0]; |
| #ifndef NDEBUG |
| for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| assert(getEffectiveSCEVType(Operands[i]->getType()) == |
| getEffectiveSCEVType(Operands[0]->getType()) && |
| "SCEVAddRecExpr operand types don't match!"); |
| #endif |
| |
| if (Operands.back()->isZero()) { |
| Operands.pop_back(); |
| return getAddRecExpr(Operands, L); // {X,+,0} --> X |
| } |
| |
| // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
| if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
| const Loop* NestedLoop = NestedAR->getLoop(); |
| if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { |
| SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), |
| NestedAR->op_end()); |
| Operands[0] = NestedAR->getStart(); |
| // AddRecs require their operands be loop-invariant with respect to their |
| // loops. Don't perform this transformation if it would break this |
| // requirement. |
| bool AllInvariant = true; |
| for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| if (!Operands[i]->isLoopInvariant(L)) { |
| AllInvariant = false; |
| break; |
| } |
| if (AllInvariant) { |
| NestedOperands[0] = getAddRecExpr(Operands, L); |
| AllInvariant = true; |
| for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
| if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { |
| AllInvariant = false; |
| break; |
| } |
| if (AllInvariant) |
| // Ok, both add recurrences are valid after the transformation. |
| return getAddRecExpr(NestedOperands, NestedLoop); |
| } |
| // Reset Operands to its original state. |
| Operands[0] = NestedAR; |
| } |
| } |
| |
| FoldingSetNodeID ID; |
| ID.AddInteger(scAddRecExpr); |
| ID.AddInteger(Operands.size()); |
| for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| ID.AddPointer(Operands[i]); |
| ID.AddPointer(L); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); |
| new (S) SCEVAddRecExpr(ID, Operands, L); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| const SCEV *RHS) { |
| SmallVector<const SCEV *, 2> Ops; |
| Ops.push_back(LHS); |
| Ops.push_back(RHS); |
| return getSMaxExpr(Ops); |
| } |
| |
| const SCEV * |
| ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
| assert(!Ops.empty() && "Cannot get empty smax!"); |
| if (Ops.size() == 1) return Ops[0]; |
| #ifndef NDEBUG |
| for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| getEffectiveSCEVType(Ops[0]->getType()) && |
| "SCEVSMaxExpr operand types don't match!"); |
| #endif |
| |
| // Sort by complexity, this groups all similar expression types together. |
| GroupByComplexity(Ops, LI); |
| |
| // If there are any constants, fold them together. |
| unsigned Idx = 0; |
| if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
| ++Idx; |
| assert(Idx < Ops.size()); |
| while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
| // We found two constants, fold them together! |
| ConstantInt *Fold = ConstantInt::get(getContext(), |
| APIntOps::smax(LHSC->getValue()->getValue(), |
| RHSC->getValue()->getValue())); |
| Ops[0] = getConstant(Fold); |
| Ops.erase(Ops.begin()+1); // Erase the folded element |
| if (Ops.size() == 1) return Ops[0]; |
| LHSC = cast<SCEVConstant>(Ops[0]); |
| } |
| |
| // If we are left with a constant minimum-int, strip it off. |
| if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| Ops.erase(Ops.begin()); |
| --Idx; |
| } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| // If we have an smax with a constant maximum-int, it will always be |
| // maximum-int. |
| return Ops[0]; |
| } |
| } |
| |
| if (Ops.size() == 1) return Ops[0]; |
| |
| // Find the first SMax |
| while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| ++Idx; |
| |
| // Check to see if one of the operands is an SMax. If so, expand its operands |
| // onto our operand list, and recurse to simplify. |
| if (Idx < Ops.size()) { |
| bool DeletedSMax = false; |
| while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
| Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); |
| Ops.erase(Ops.begin()+Idx); |
| DeletedSMax = true; |
| } |
| |
| if (DeletedSMax) |
| return getSMaxExpr(Ops); |
| } |
| |
| // Okay, check to see if the same value occurs in the operand list twice. If |
| // so, delete one. Since we sorted the list, these values are required to |
| // be adjacent. |
| for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y |
| Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| --i; --e; |
| } |
| |
| if (Ops.size() == 1) return Ops[0]; |
| |
| assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| |
| // Okay, it looks like we really DO need an smax expr. Check to see if we |
| // already have one, otherwise create a new one. |
| FoldingSetNodeID ID; |
| ID.AddInteger(scSMaxExpr); |
| ID.AddInteger(Ops.size()); |
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| ID.AddPointer(Ops[i]); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); |
| new (S) SCEVSMaxExpr(ID, Ops); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| const SCEV *RHS) { |
| SmallVector<const SCEV *, 2> Ops; |
| Ops.push_back(LHS); |
| Ops.push_back(RHS); |
| return getUMaxExpr(Ops); |
| } |
| |
| const SCEV * |
| ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
| assert(!Ops.empty() && "Cannot get empty umax!"); |
| if (Ops.size() == 1) return Ops[0]; |
| #ifndef NDEBUG |
| for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| getEffectiveSCEVType(Ops[0]->getType()) && |
| "SCEVUMaxExpr operand types don't match!"); |
| #endif |
| |
| // Sort by complexity, this groups all similar expression types together. |
| GroupByComplexity(Ops, LI); |
| |
| // If there are any constants, fold them together. |
| unsigned Idx = 0; |
| if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
| ++Idx; |
| assert(Idx < Ops.size()); |
| while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
| // We found two constants, fold them together! |
| ConstantInt *Fold = ConstantInt::get(getContext(), |
| APIntOps::umax(LHSC->getValue()->getValue(), |
| RHSC->getValue()->getValue())); |
| Ops[0] = getConstant(Fold); |
| Ops.erase(Ops.begin()+1); // Erase the folded element |
| if (Ops.size() == 1) return Ops[0]; |
| LHSC = cast<SCEVConstant>(Ops[0]); |
| } |
| |
| // If we are left with a constant minimum-int, strip it off. |
| if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| Ops.erase(Ops.begin()); |
| --Idx; |
| } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| // If we have an umax with a constant maximum-int, it will always be |
| // maximum-int. |
| return Ops[0]; |
| } |
| } |
| |
| if (Ops.size() == 1) return Ops[0]; |
| |
| // Find the first UMax |
| while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| ++Idx; |
| |
| // Check to see if one of the operands is a UMax. If so, expand its operands |
| // onto our operand list, and recurse to simplify. |
| if (Idx < Ops.size()) { |
| bool DeletedUMax = false; |
| while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
| Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); |
| Ops.erase(Ops.begin()+Idx); |
| DeletedUMax = true; |
| } |
| |
| if (DeletedUMax) |
| return getUMaxExpr(Ops); |
| } |
| |
| // Okay, check to see if the same value occurs in the operand list twice. If |
| // so, delete one. Since we sorted the list, these values are required to |
| // be adjacent. |
| for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y |
| Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| --i; --e; |
| } |
| |
| if (Ops.size() == 1) return Ops[0]; |
| |
| assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| |
| // Okay, it looks like we really DO need a umax expr. Check to see if we |
| // already have one, otherwise create a new one. |
| FoldingSetNodeID ID; |
| ID.AddInteger(scUMaxExpr); |
| ID.AddInteger(Ops.size()); |
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| ID.AddPointer(Ops[i]); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); |
| new (S) SCEVUMaxExpr(ID, Ops); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| const SCEV *RHS) { |
| // ~smax(~x, ~y) == smin(x, y). |
| return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| } |
| |
| const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| const SCEV *RHS) { |
| // ~umax(~x, ~y) == umin(x, y) |
| return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| } |
| |
| const SCEV *ScalarEvolution::getFieldOffsetExpr(const StructType *STy, |
| unsigned FieldNo) { |
| // If we have TargetData we can determine the constant offset. |
| if (TD) { |
| const Type *IntPtrTy = TD->getIntPtrType(getContext()); |
| const StructLayout &SL = *TD->getStructLayout(STy); |
| uint64_t Offset = SL.getElementOffset(FieldNo); |
| return getIntegerSCEV(Offset, IntPtrTy); |
| } |
| |
| // Field 0 is always at offset 0. |
| if (FieldNo == 0) { |
| const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
| return getIntegerSCEV(0, Ty); |
| } |
| |
| // Okay, it looks like we really DO need an offsetof expr. Check to see if we |
| // already have one, otherwise create a new one. |
| FoldingSetNodeID ID; |
| ID.AddInteger(scFieldOffset); |
| ID.AddPointer(STy); |
| ID.AddInteger(FieldNo); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVFieldOffsetExpr>(); |
| const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
| new (S) SCEVFieldOffsetExpr(ID, Ty, STy, FieldNo); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| const SCEV *ScalarEvolution::getAllocSizeExpr(const Type *AllocTy) { |
| // If we have TargetData we can determine the constant size. |
| if (TD && AllocTy->isSized()) { |
| const Type *IntPtrTy = TD->getIntPtrType(getContext()); |
| return getIntegerSCEV(TD->getTypeAllocSize(AllocTy), IntPtrTy); |
| } |
| |
| // Expand an array size into the element size times the number |
| // of elements. |
| if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy)) { |
| const SCEV *E = getAllocSizeExpr(ATy->getElementType()); |
| return getMulExpr( |
| E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()), |
| ATy->getNumElements()))); |
| } |
| |
| // Expand a vector size into the element size times the number |
| // of elements. |
| if (const VectorType *VTy = dyn_cast<VectorType>(AllocTy)) { |
| const SCEV *E = getAllocSizeExpr(VTy->getElementType()); |
| return getMulExpr( |
| E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()), |
| VTy->getNumElements()))); |
| } |
| |
| // Okay, it looks like we really DO need a sizeof expr. Check to see if we |
| // already have one, otherwise create a new one. |
| FoldingSetNodeID ID; |
| ID.AddInteger(scAllocSize); |
| ID.AddPointer(AllocTy); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVAllocSizeExpr>(); |
| const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| new (S) SCEVAllocSizeExpr(ID, Ty, AllocTy); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| const SCEV *ScalarEvolution::getUnknown(Value *V) { |
| // Don't attempt to do anything other than create a SCEVUnknown object |
| // here. createSCEV only calls getUnknown after checking for all other |
| // interesting possibilities, and any other code that calls getUnknown |
| // is doing so in order to hide a value from SCEV canonicalization. |
| |
| FoldingSetNodeID ID; |
| ID.AddInteger(scUnknown); |
| ID.AddPointer(V); |
| void *IP = 0; |
| if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); |
| new (S) SCEVUnknown(ID, V); |
| UniqueSCEVs.InsertNode(S, IP); |
| return S; |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // Basic SCEV Analysis and PHI Idiom Recognition Code |
| // |
| |
| /// 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 ScalarEvolution::isSCEVable(const Type *Ty) const { |
| // Integers and pointers are always SCEVable. |
| return Ty->isInteger() || isa<PointerType>(Ty); |
| } |
| |
| /// getTypeSizeInBits - Return the size in bits of the specified type, |
| /// for which isSCEVable must return true. |
| uint64_t ScalarEvolution::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); |
| |
| // Integer types have fixed sizes. |
| if (Ty->isInteger()) |
| return Ty->getPrimitiveSizeInBits(); |
| |
| // The only other support type is pointer. Without TargetData, conservatively |
| // assume pointers are 64-bit. |
| assert(isa<PointerType>(Ty) && "isSCEVable permitted a non-SCEVable type!"); |
| return 64; |
| } |
| |
| /// 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 *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
| assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| |
| if (Ty->isInteger()) |
| return Ty; |
| |
| // The only other support type is pointer. |
| assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); |
| if (TD) return TD->getIntPtrType(getContext()); |
| |
| // Without TargetData, conservatively assume pointers are 64-bit. |
| return Type::getInt64Ty(getContext()); |
| } |
| |
| const SCEV *ScalarEvolution::getCouldNotCompute() { |
| return &CouldNotCompute; |
| } |
| |
| /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| /// expression and create a new one. |
| const SCEV *ScalarEvolution::getSCEV(Value *V) { |
| assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
| |
| std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V); |
| if (I != Scalars.end()) return I->second; |
| const SCEV *S = createSCEV(V); |
| Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
| return S; |
| } |
| |
| /// getIntegerSCEV - Given a SCEVable type, create a constant for the |
| /// specified signed integer value and return a SCEV for the constant. |
| const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { |
| const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
| return getConstant(ConstantInt::get(ITy, Val)); |
| } |
| |
| /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| /// |
| const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { |
| if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
| return getConstant( |
| cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); |
| |
| const Type *Ty = V->getType(); |
| Ty = getEffectiveSCEVType(Ty); |
| return getMulExpr(V, |
| getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)))); |
| } |
| |
| /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
| const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
| if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
| return getConstant( |
| cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
| |
| const Type *Ty = V->getType(); |
| Ty = getEffectiveSCEVType(Ty); |
| const SCEV *AllOnes = |
| getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))); |
| return getMinusSCEV(AllOnes, V); |
| } |
| |
| /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| /// |
| const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| const SCEV *RHS) { |
| // X - Y --> X + -Y |
| return getAddExpr(LHS, getNegativeSCEV(RHS)); |
| } |
| |
| /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| /// input value to the specified type. If the type must be extended, it is zero |
| /// extended. |
| const SCEV * |
| ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, |
| const Type *Ty) { |
| const Type *SrcTy = V->getType(); |
| assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| (Ty->isInteger() || isa<PointerType>(Ty)) && |
| "Cannot truncate or zero extend with non-integer arguments!"); |
| if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| return V; // No conversion |
| if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
| return getTruncateExpr(V, Ty); |
| return getZeroExtendExpr(V, Ty); |
| } |
| |
| /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| /// input value to the specified type. If the type must be extended, it is sign |
| /// extended. |
| const SCEV * |
| ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, |
| const Type *Ty) { |
| const Type *SrcTy = V->getType(); |
| assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| (Ty->isInteger() || isa<PointerType>(Ty)) && |
| "Cannot truncate or zero extend with non-integer arguments!"); |
| if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| return V; // No conversion |
| if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
| return getTruncateExpr(V, Ty); |
| return getSignExtendExpr(V, Ty); |
| } |
| |
| /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| /// input value to the specified type. If the type must be extended, it is zero |
| /// extended. The conversion must not be narrowing. |
| const SCEV * |
| ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { |
| const Type *SrcTy = V->getType(); |
| assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| (Ty->isInteger() || isa<PointerType>(Ty)) && |
| "Cannot noop or zero extend with non-integer arguments!"); |
| assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| "getNoopOrZeroExtend cannot truncate!"); |
| if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| return V; // No conversion |
| return getZeroExtendExpr(V, Ty); |
| } |
| |
| /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| /// input value to the specified type. If the type must be extended, it is sign |
| /// extended. The conversion must not be narrowing. |
| const SCEV * |
| ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { |
| const Type *SrcTy = V->getType(); |
| assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| (Ty->isInteger() || isa<PointerType>(Ty)) && |
| "Cannot noop or sign extend with non-integer arguments!"); |
| assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| "getNoopOrSignExtend cannot truncate!"); |
| if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| return V; // No conversion |
| return getSignExtendExpr(V, Ty); |
| } |
| |
| /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| /// the input value to the specified type. If the type must be extended, |
| /// it is extended with unspecified bits. The conversion must not be |
| /// narrowing. |
| const SCEV * |
| ScalarEvolution::getNoopOrAnyExtend |