00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
00022 #define LLVM_ANALYSIS_SCALAREVOLUTION_H
00023
00024 #include "llvm/Pass.h"
00025 #include "llvm/Instructions.h"
00026 #include "llvm/Function.h"
00027 #include "llvm/System/DataTypes.h"
00028 #include "llvm/Support/ValueHandle.h"
00029 #include "llvm/Support/Allocator.h"
00030 #include "llvm/Support/ConstantRange.h"
00031 #include "llvm/ADT/FoldingSet.h"
00032 #include "llvm/ADT/DenseMap.h"
00033 #include <map>
00034
00035 namespace llvm {
00036 class APInt;
00037 class Constant;
00038 class ConstantInt;
00039 class DominatorTree;
00040 class Type;
00041 class ScalarEvolution;
00042 class TargetData;
00043 class LLVMContext;
00044 class Loop;
00045 class LoopInfo;
00046 class Operator;
00047
00052 class SCEV : public FoldingSetNode {
00055 FoldingSetNodeIDRef FastID;
00056
00057
00058 const unsigned short SCEVType;
00059
00060 protected:
00063 unsigned short SubclassData;
00064
00065 private:
00066 SCEV(const SCEV &);
00067 void operator=(const SCEV &);
00068 protected:
00069 virtual ~SCEV();
00070 public:
00071 explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) :
00072 FastID(ID), SCEVType(SCEVTy), SubclassData(0) {}
00073
00074 unsigned getSCEVType() const { return SCEVType; }
00075
00077 void Profile(FoldingSetNodeID& ID) { ID = FastID; }
00078
00081 virtual bool isLoopInvariant(const Loop *L) const = 0;
00082
00087 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
00088
00091 virtual const Type *getType() const = 0;
00092
00095 bool isZero() const;
00096
00099 bool isOne() const;
00100
00104 bool isAllOnesValue() const;
00105
00108 virtual bool hasOperand(const SCEV *Op) const = 0;
00109
00112 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0;
00113
00116 virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const = 0;
00117
00121 virtual void print(raw_ostream &OS) const = 0;
00122
00125 void dump() const;
00126 };
00127
00128 inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
00129 S.print(OS);
00130 return OS;
00131 }
00132
00138 struct SCEVCouldNotCompute : public SCEV {
00139 SCEVCouldNotCompute();
00140
00141
00142 virtual bool isLoopInvariant(const Loop *L) const;
00143 virtual const Type *getType() const;
00144 virtual bool hasComputableLoopEvolution(const Loop *L) const;
00145 virtual void print(raw_ostream &OS) const;
00146 virtual bool hasOperand(const SCEV *Op) const;
00147
00148 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
00149 return true;
00150 }
00151
00152 virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
00153 return true;
00154 }
00155
00157 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
00158 static bool classof(const SCEV *S);
00159 };
00160
00165 class ScalarEvolution : public FunctionPass {
00168 class SCEVCallbackVH : public CallbackVH {
00169 ScalarEvolution *SE;
00170 virtual void deleted();
00171 virtual void allUsesReplacedWith(Value *New);
00172 public:
00173 SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0);
00174 };
00175
00176 friend class SCEVCallbackVH;
00177 friend class SCEVExpander;
00178
00181 Function *F;
00182
00185 LoopInfo *LI;
00186
00189 TargetData *TD;
00190
00193 DominatorTree *DT;
00194
00197 SCEVCouldNotCompute CouldNotCompute;
00198
00201 std::map<SCEVCallbackVH, const SCEV *> Scalars;
00202
00206 struct BackedgeTakenInfo {
00209 const SCEV *Exact;
00210
00213 const SCEV *Max;
00214
00215 BackedgeTakenInfo(const SCEV *exact) :
00216 Exact(exact), Max(exact) {}
00217
00218 BackedgeTakenInfo(const SCEV *exact, const SCEV *max) :
00219 Exact(exact), Max(max) {}
00220
00224 bool hasAnyInfo() const {
00225 return !isa<SCEVCouldNotCompute>(Exact) ||
00226 !isa<SCEVCouldNotCompute>(Max);
00227 }
00228 };
00229
00232 std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
00233
00239 std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
00240
00244 std::map<const SCEV *,
00245 std::map<const Loop *, const SCEV *> > ValuesAtScopes;
00246
00249 const SCEV *createSCEV(Value *V);
00250
00253 const SCEV *createNodeForPHI(PHINode *PN);
00254
00257 const SCEV *createNodeForGEP(GEPOperator *GEP);
00258
00262 const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
00263
00268 void ForgetSymbolicName(Instruction *I, const SCEV *SymName);
00269
00273 const SCEV *getBECount(const SCEV *Start,
00274 const SCEV *End,
00275 const SCEV *Step,
00276 bool NoWrap);
00277
00281 const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
00282
00285 BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L);
00286
00290 BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L,
00291 BasicBlock *ExitingBlock);
00292
00296 BackedgeTakenInfo
00297 ComputeBackedgeTakenCountFromExitCond(const Loop *L,
00298 Value *ExitCond,
00299 BasicBlock *TBB,
00300 BasicBlock *FBB);
00301
00306 BackedgeTakenInfo
00307 ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
00308 ICmpInst *ExitCond,
00309 BasicBlock *TBB,
00310 BasicBlock *FBB);
00311
00315 BackedgeTakenInfo
00316 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
00317 Constant *RHS,
00318 const Loop *L,
00319 ICmpInst::Predicate p);
00320
00326 const SCEV *ComputeBackedgeTakenCountExhaustively(const Loop *L,
00327 Value *Cond,
00328 bool ExitWhen);
00329
00333 BackedgeTakenInfo HowFarToZero(const SCEV *V, const Loop *L);
00334
00338 BackedgeTakenInfo HowFarToNonZero(const SCEV *V, const Loop *L);
00339
00343 BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
00344 const Loop *L, bool isSigned);
00345
00348 BasicBlock *getLoopPredecessor(const Loop *L);
00349
00354 BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
00355
00358 bool isImpliedCond(Value *Cond, ICmpInst::Predicate Pred,
00359 const SCEV *LHS, const SCEV *RHS,
00360 bool Inverse);
00361
00365 bool isImpliedCondOperands(ICmpInst::Predicate Pred,
00366 const SCEV *LHS, const SCEV *RHS,
00367 const SCEV *FoundLHS, const SCEV *FoundRHS);
00368
00372 bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
00373 const SCEV *LHS, const SCEV *RHS,
00374 const SCEV *FoundLHS, const SCEV *FoundRHS);
00375
00380 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
00381 const Loop *L);
00382
00383 public:
00384 static char ID;
00385 ScalarEvolution();
00386
00387 LLVMContext &getContext() const { return F->getContext(); }
00388
00393 bool isSCEVable(const Type *Ty) const;
00394
00397 uint64_t getTypeSizeInBits(const Type *Ty) const;
00398
00403 const Type *getEffectiveSCEVType(const Type *Ty) const;
00404
00407 const SCEV *getSCEV(Value *V);
00408
00409 const SCEV *getConstant(ConstantInt *V);
00410 const SCEV *getConstant(const APInt& Val);
00411 const SCEV *getConstant(const Type *Ty, uint64_t V, bool isSigned = false);
00412 const SCEV *getTruncateExpr(const SCEV *Op, const Type *Ty);
00413 const SCEV *getZeroExtendExpr(const SCEV *Op, const Type *Ty);
00414 const SCEV *getSignExtendExpr(const SCEV *Op, const Type *Ty);
00415 const SCEV *getAnyExtendExpr(const SCEV *Op, const Type *Ty);
00416 const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
00417 bool HasNUW = false, bool HasNSW = false);
00418 const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
00419 bool HasNUW = false, bool HasNSW = false) {
00420 SmallVector<const SCEV *, 2> Ops;
00421 Ops.push_back(LHS);
00422 Ops.push_back(RHS);
00423 return getAddExpr(Ops, HasNUW, HasNSW);
00424 }
00425 const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1,
00426 const SCEV *Op2,
00427 bool HasNUW = false, bool HasNSW = false) {
00428 SmallVector<const SCEV *, 3> Ops;
00429 Ops.push_back(Op0);
00430 Ops.push_back(Op1);
00431 Ops.push_back(Op2);
00432 return getAddExpr(Ops, HasNUW, HasNSW);
00433 }
00434 const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
00435 bool HasNUW = false, bool HasNSW = false);
00436 const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
00437 bool HasNUW = false, bool HasNSW = false) {
00438 SmallVector<const SCEV *, 2> Ops;
00439 Ops.push_back(LHS);
00440 Ops.push_back(RHS);
00441 return getMulExpr(Ops, HasNUW, HasNSW);
00442 }
00443 const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
00444 const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
00445 const Loop *L,
00446 bool HasNUW = false, bool HasNSW = false);
00447 const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
00448 const Loop *L,
00449 bool HasNUW = false, bool HasNSW = false);
00450 const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
00451 const Loop *L,
00452 bool HasNUW = false, bool HasNSW = false) {
00453 SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
00454 return getAddRecExpr(NewOp, L, HasNUW, HasNSW);
00455 }
00456 const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
00457 const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
00458 const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
00459 const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
00460 const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
00461 const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
00462 const SCEV *getUnknown(Value *V);
00463 const SCEV *getCouldNotCompute();
00464
00467 const SCEV *getSizeOfExpr(const Type *AllocTy);
00468
00471 const SCEV *getAlignOfExpr(const Type *AllocTy);
00472
00475 const SCEV *getOffsetOfExpr(const StructType *STy, unsigned FieldNo);
00476
00479 const SCEV *getOffsetOfExpr(const Type *CTy, Constant *FieldNo);
00480
00483 const SCEV *getNegativeSCEV(const SCEV *V);
00484
00487 const SCEV *getNotSCEV(const SCEV *V);
00488
00491 const SCEV *getMinusSCEV(const SCEV *LHS,
00492 const SCEV *RHS);
00493
00497 const SCEV *getTruncateOrZeroExtend(const SCEV *V, const Type *Ty);
00498
00502 const SCEV *getTruncateOrSignExtend(const SCEV *V, const Type *Ty);
00503
00507 const SCEV *getNoopOrZeroExtend(const SCEV *V, const Type *Ty);
00508
00512 const SCEV *getNoopOrSignExtend(const SCEV *V, const Type *Ty);
00513
00518 const SCEV *getNoopOrAnyExtend(const SCEV *V, const Type *Ty);
00519
00523 const SCEV *getTruncateOrNoop(const SCEV *V, const Type *Ty);
00524
00527 const SCEV *getIntegerSCEV(int64_t Val, const Type *Ty);
00528
00532 const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS,
00533 const SCEV *RHS);
00534
00538 const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS,
00539 const SCEV *RHS);
00540
00551 const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
00552
00555 const SCEV *getSCEVAtScope(Value *V, const Loop *L);
00556
00560 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
00561 const SCEV *LHS, const SCEV *RHS);
00562
00566 bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
00567 const SCEV *LHS, const SCEV *RHS);
00568
00580 const SCEV *getBackedgeTakenCount(const Loop *L);
00581
00585 const SCEV *getMaxBackedgeTakenCount(const Loop *L);
00586
00589 bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
00590
00594 void forgetLoop(const Loop *L);
00595
00599 void forgetValue(Value *V);
00600
00606 uint32_t GetMinTrailingZeros(const SCEV *S);
00607
00610 ConstantRange getUnsignedRange(const SCEV *S);
00611
00614 ConstantRange getSignedRange(const SCEV *S);
00615
00618 bool isKnownNegative(const SCEV *S);
00619
00622 bool isKnownPositive(const SCEV *S);
00623
00627 bool isKnownNonNegative(const SCEV *S);
00628
00632 bool isKnownNonPositive(const SCEV *S);
00633
00637 bool isKnownNonZero(const SCEV *S);
00638
00642 bool isKnownPredicate(ICmpInst::Predicate Pred,
00643 const SCEV *LHS, const SCEV *RHS);
00644
00645 virtual bool runOnFunction(Function &F);
00646 virtual void releaseMemory();
00647 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
00648 virtual void print(raw_ostream &OS, const Module* = 0) const;
00649
00650 private:
00651 FoldingSet<SCEV> UniqueSCEVs;
00652 BumpPtrAllocator SCEVAllocator;
00653 };
00654 }
00655
00656 #endif