ScalarEvolution.h revision 1f6efa3996dd1929fbc129203ce5009b620e6969
1//===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// The ScalarEvolution class is an LLVM pass which can be used to analyze and 11// categorize scalar expressions in loops. It specializes in recognizing 12// general induction variables, representing them with the abstract and opaque 13// SCEV class. Given this analysis, trip counts of loops and other important 14// properties can be obtained. 15// 16// This analysis is primarily useful for induction variable substitution and 17// strength reduction. 18// 19//===----------------------------------------------------------------------===// 20 21#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H 22#define LLVM_ANALYSIS_SCALAREVOLUTION_H 23 24#include "llvm/Pass.h" 25#include "llvm/Instructions.h" 26#include "llvm/Function.h" 27#include "llvm/Support/DataTypes.h" 28#include "llvm/Support/ValueHandle.h" 29#include "llvm/Support/Allocator.h" 30#include "llvm/Support/ConstantRange.h" 31#include "llvm/ADT/FoldingSet.h" 32#include "llvm/ADT/DenseMap.h" 33#include <map> 34 35namespace llvm { 36 class APInt; 37 class Constant; 38 class ConstantInt; 39 class DominatorTree; 40 class Type; 41 class ScalarEvolution; 42 class TargetData; 43 class LLVMContext; 44 class Loop; 45 class LoopInfo; 46 class Operator; 47 class SCEVUnknown; 48 class SCEV; 49 template<> struct FoldingSetTrait<SCEV>; 50 51 /// SCEV - This class represents an analyzed expression in the program. These 52 /// are opaque objects that the client is not allowed to do much with 53 /// directly. 54 /// 55 class SCEV : public FoldingSetNode { 56 friend struct FoldingSetTrait<SCEV>; 57 58 /// FastID - A reference to an Interned FoldingSetNodeID for this node. 59 /// The ScalarEvolution's BumpPtrAllocator holds the data. 60 FoldingSetNodeIDRef FastID; 61 62 // The SCEV baseclass this node corresponds to 63 const unsigned short SCEVType; 64 65 protected: 66 /// SubclassData - This field is initialized to zero and may be used in 67 /// subclasses to store miscellaneous information. 68 unsigned short SubclassData; 69 70 private: 71 SCEV(const SCEV &); // DO NOT IMPLEMENT 72 void operator=(const SCEV &); // DO NOT IMPLEMENT 73 74 public: 75 explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) : 76 FastID(ID), SCEVType(SCEVTy), SubclassData(0) {} 77 78 unsigned getSCEVType() const { return SCEVType; } 79 80 /// getType - Return the LLVM type of this SCEV expression. 81 /// 82 const Type *getType() const; 83 84 /// isZero - Return true if the expression is a constant zero. 85 /// 86 bool isZero() const; 87 88 /// isOne - Return true if the expression is a constant one. 89 /// 90 bool isOne() const; 91 92 /// isAllOnesValue - Return true if the expression is a constant 93 /// all-ones value. 94 /// 95 bool isAllOnesValue() const; 96 97 /// print - Print out the internal representation of this scalar to the 98 /// specified stream. This should really only be used for debugging 99 /// purposes. 100 void print(raw_ostream &OS) const; 101 102 /// dump - This method is used for debugging. 103 /// 104 void dump() const; 105 }; 106 107 // Specialize FoldingSetTrait for SCEV to avoid needing to compute 108 // temporary FoldingSetNodeID values. 109 template<> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> { 110 static void Profile(const SCEV &X, FoldingSetNodeID& ID) { 111 ID = X.FastID; 112 } 113 static bool Equals(const SCEV &X, const FoldingSetNodeID &ID, 114 FoldingSetNodeID &TempID) { 115 return ID == X.FastID; 116 } 117 static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) { 118 return X.FastID.ComputeHash(); 119 } 120 }; 121 122 inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) { 123 S.print(OS); 124 return OS; 125 } 126 127 /// SCEVCouldNotCompute - An object of this class is returned by queries that 128 /// could not be answered. For example, if you ask for the number of 129 /// iterations of a linked-list traversal loop, you will get one of these. 130 /// None of the standard SCEV operations are valid on this class, it is just a 131 /// marker. 132 struct SCEVCouldNotCompute : public SCEV { 133 SCEVCouldNotCompute(); 134 135 /// Methods for support type inquiry through isa, cast, and dyn_cast: 136 static inline bool classof(const SCEVCouldNotCompute *S) { return true; } 137 static bool classof(const SCEV *S); 138 }; 139 140 /// ScalarEvolution - This class is the main scalar evolution driver. Because 141 /// client code (intentionally) can't do much with the SCEV objects directly, 142 /// they must ask this class for services. 143 /// 144 class ScalarEvolution : public FunctionPass { 145 public: 146 /// LoopDisposition - An enum describing the relationship between a 147 /// SCEV and a loop. 148 enum LoopDisposition { 149 LoopVariant, ///< The SCEV is loop-variant (unknown). 150 LoopInvariant, ///< The SCEV is loop-invariant. 151 LoopComputable ///< The SCEV varies predictably with the loop. 152 }; 153 154 /// BlockDisposition - An enum describing the relationship between a 155 /// SCEV and a basic block. 156 enum BlockDisposition { 157 DoesNotDominateBlock, ///< The SCEV does not dominate the block. 158 DominatesBlock, ///< The SCEV dominates the block. 159 ProperlyDominatesBlock ///< The SCEV properly dominates the block. 160 }; 161 162 private: 163 /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be 164 /// notified whenever a Value is deleted. 165 class SCEVCallbackVH : public CallbackVH { 166 ScalarEvolution *SE; 167 virtual void deleted(); 168 virtual void allUsesReplacedWith(Value *New); 169 public: 170 SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0); 171 }; 172 173 friend class SCEVCallbackVH; 174 friend class SCEVExpander; 175 friend class SCEVUnknown; 176 177 /// F - The function we are analyzing. 178 /// 179 Function *F; 180 181 /// LI - The loop information for the function we are currently analyzing. 182 /// 183 LoopInfo *LI; 184 185 /// TD - The target data information for the target we are targeting. 186 /// 187 TargetData *TD; 188 189 /// DT - The dominator tree. 190 /// 191 DominatorTree *DT; 192 193 /// CouldNotCompute - This SCEV is used to represent unknown trip 194 /// counts and things. 195 SCEVCouldNotCompute CouldNotCompute; 196 197 /// ValueExprMapType - The typedef for ValueExprMap. 198 /// 199 typedef DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *> > 200 ValueExprMapType; 201 202 /// ValueExprMap - This is a cache of the values we have analyzed so far. 203 /// 204 ValueExprMapType ValueExprMap; 205 206 /// BackedgeTakenInfo - Information about the backedge-taken count 207 /// of a loop. This currently includes an exact count and a maximum count. 208 /// 209 struct BackedgeTakenInfo { 210 /// Exact - An expression indicating the exact backedge-taken count of 211 /// the loop if it is known, or a SCEVCouldNotCompute otherwise. 212 const SCEV *Exact; 213 214 /// Max - An expression indicating the least maximum backedge-taken 215 /// count of the loop that is known, or a SCEVCouldNotCompute. 216 const SCEV *Max; 217 218 /*implicit*/ BackedgeTakenInfo(const SCEV *exact) : 219 Exact(exact), Max(exact) {} 220 221 BackedgeTakenInfo(const SCEV *exact, const SCEV *max) : 222 Exact(exact), Max(max) {} 223 224 /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any 225 /// computed information, or whether it's all SCEVCouldNotCompute 226 /// values. 227 bool hasAnyInfo() const { 228 return !isa<SCEVCouldNotCompute>(Exact) || 229 !isa<SCEVCouldNotCompute>(Max); 230 } 231 }; 232 233 /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for 234 /// this function as they are computed. 235 std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts; 236 237 /// ConstantEvolutionLoopExitValue - This map contains entries for all of 238 /// the PHI instructions that we attempt to compute constant evolutions for. 239 /// This allows us to avoid potentially expensive recomputation of these 240 /// properties. An instruction maps to null if we are unable to compute its 241 /// exit value. 242 std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue; 243 244 /// ValuesAtScopes - This map contains entries for all the expressions 245 /// that we attempt to compute getSCEVAtScope information for, which can 246 /// be expensive in extreme cases. 247 std::map<const SCEV *, 248 std::map<const Loop *, const SCEV *> > ValuesAtScopes; 249 250 /// LoopDispositions - Memoized computeLoopDisposition results. 251 std::map<const SCEV *, 252 std::map<const Loop *, LoopDisposition> > LoopDispositions; 253 254 /// computeLoopDisposition - Compute a LoopDisposition value. 255 LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L); 256 257 /// BlockDispositions - Memoized computeBlockDisposition results. 258 std::map<const SCEV *, 259 std::map<const BasicBlock *, BlockDisposition> > BlockDispositions; 260 261 /// computeBlockDisposition - Compute a BlockDisposition value. 262 BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB); 263 264 /// UnsignedRanges - Memoized results from getUnsignedRange 265 DenseMap<const SCEV *, ConstantRange> UnsignedRanges; 266 267 /// SignedRanges - Memoized results from getSignedRange 268 DenseMap<const SCEV *, ConstantRange> SignedRanges; 269 270 /// setUnsignedRange - Set the memoized unsigned range for the given SCEV. 271 const ConstantRange &setUnsignedRange(const SCEV *S, 272 const ConstantRange &CR) { 273 std::pair<DenseMap<const SCEV *, ConstantRange>::iterator, bool> Pair = 274 UnsignedRanges.insert(std::make_pair(S, CR)); 275 if (!Pair.second) 276 Pair.first->second = CR; 277 return Pair.first->second; 278 } 279 280 /// setUnsignedRange - Set the memoized signed range for the given SCEV. 281 const ConstantRange &setSignedRange(const SCEV *S, 282 const ConstantRange &CR) { 283 std::pair<DenseMap<const SCEV *, ConstantRange>::iterator, bool> Pair = 284 SignedRanges.insert(std::make_pair(S, CR)); 285 if (!Pair.second) 286 Pair.first->second = CR; 287 return Pair.first->second; 288 } 289 290 /// createSCEV - We know that there is no SCEV for the specified value. 291 /// Analyze the expression. 292 const SCEV *createSCEV(Value *V); 293 294 /// createNodeForPHI - Provide the special handling we need to analyze PHI 295 /// SCEVs. 296 const SCEV *createNodeForPHI(PHINode *PN); 297 298 /// createNodeForGEP - Provide the special handling we need to analyze GEP 299 /// SCEVs. 300 const SCEV *createNodeForGEP(GEPOperator *GEP); 301 302 /// computeSCEVAtScope - Implementation code for getSCEVAtScope; called 303 /// at most once for each SCEV+Loop pair. 304 /// 305 const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L); 306 307 /// ForgetSymbolicValue - This looks up computed SCEV values for all 308 /// instructions that depend on the given instruction and removes them from 309 /// the ValueExprMap map if they reference SymName. This is used during PHI 310 /// resolution. 311 void ForgetSymbolicName(Instruction *I, const SCEV *SymName); 312 313 /// getBECount - Subtract the end and start values and divide by the step, 314 /// rounding up, to get the number of times the backedge is executed. Return 315 /// CouldNotCompute if an intermediate computation overflows. 316 const SCEV *getBECount(const SCEV *Start, 317 const SCEV *End, 318 const SCEV *Step, 319 bool NoWrap); 320 321 /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given 322 /// loop, lazily computing new values if the loop hasn't been analyzed 323 /// yet. 324 const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L); 325 326 /// ComputeBackedgeTakenCount - Compute the number of times the specified 327 /// loop will iterate. 328 BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L); 329 330 /// ComputeBackedgeTakenCountFromExit - Compute the number of times the 331 /// backedge of the specified loop will execute if it exits via the 332 /// specified block. 333 BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L, 334 BasicBlock *ExitingBlock); 335 336 /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the 337 /// backedge of the specified loop will execute if its exit condition 338 /// were a conditional branch of ExitCond, TBB, and FBB. 339 BackedgeTakenInfo 340 ComputeBackedgeTakenCountFromExitCond(const Loop *L, 341 Value *ExitCond, 342 BasicBlock *TBB, 343 BasicBlock *FBB); 344 345 /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of 346 /// times the backedge of the specified loop will execute if its exit 347 /// condition were a conditional branch of the ICmpInst ExitCond, TBB, 348 /// and FBB. 349 BackedgeTakenInfo 350 ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, 351 ICmpInst *ExitCond, 352 BasicBlock *TBB, 353 BasicBlock *FBB); 354 355 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition 356 /// of 'icmp op load X, cst', try to see if we can compute the 357 /// backedge-taken count. 358 BackedgeTakenInfo 359 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, 360 Constant *RHS, 361 const Loop *L, 362 ICmpInst::Predicate p); 363 364 /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute 365 /// a constant number of times (the condition evolves only from constants), 366 /// try to evaluate a few iterations of the loop until we get the exit 367 /// condition gets a value of ExitWhen (true or false). If we cannot 368 /// evaluate the backedge-taken count of the loop, return CouldNotCompute. 369 const SCEV *ComputeBackedgeTakenCountExhaustively(const Loop *L, 370 Value *Cond, 371 bool ExitWhen); 372 373 /// HowFarToZero - Return the number of times a backedge comparing the 374 /// specified value to zero will execute. If not computable, return 375 /// CouldNotCompute. 376 BackedgeTakenInfo HowFarToZero(const SCEV *V, const Loop *L); 377 378 /// HowFarToNonZero - Return the number of times a backedge checking the 379 /// specified value for nonzero will execute. If not computable, return 380 /// CouldNotCompute. 381 BackedgeTakenInfo HowFarToNonZero(const SCEV *V, const Loop *L); 382 383 /// HowManyLessThans - Return the number of times a backedge containing the 384 /// specified less-than comparison will execute. If not computable, return 385 /// CouldNotCompute. isSigned specifies whether the less-than is signed. 386 BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS, 387 const Loop *L, bool isSigned); 388 389 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB 390 /// (which may not be an immediate predecessor) which has exactly one 391 /// successor from which BB is reachable, or null if no such block is 392 /// found. 393 std::pair<BasicBlock *, BasicBlock *> 394 getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB); 395 396 /// isImpliedCond - Test whether the condition described by Pred, LHS, and 397 /// RHS is true whenever the given FoundCondValue value evaluates to true. 398 bool isImpliedCond(ICmpInst::Predicate Pred, 399 const SCEV *LHS, const SCEV *RHS, 400 Value *FoundCondValue, 401 bool Inverse); 402 403 /// isImpliedCondOperands - Test whether the condition described by Pred, 404 /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS, 405 /// and FoundRHS is true. 406 bool isImpliedCondOperands(ICmpInst::Predicate Pred, 407 const SCEV *LHS, const SCEV *RHS, 408 const SCEV *FoundLHS, const SCEV *FoundRHS); 409 410 /// isImpliedCondOperandsHelper - Test whether the condition described by 411 /// Pred, LHS, and RHS is true whenever the condition described by Pred, 412 /// FoundLHS, and FoundRHS is true. 413 bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, 414 const SCEV *LHS, const SCEV *RHS, 415 const SCEV *FoundLHS, const SCEV *FoundRHS); 416 417 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is 418 /// in the header of its containing loop, we know the loop executes a 419 /// constant number of times, and the PHI node is just a recurrence 420 /// involving constants, fold it. 421 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, 422 const Loop *L); 423 424 /// isKnownPredicateWithRanges - Test if the given expression is known to 425 /// satisfy the condition described by Pred and the known constant ranges 426 /// of LHS and RHS. 427 /// 428 bool isKnownPredicateWithRanges(ICmpInst::Predicate Pred, 429 const SCEV *LHS, const SCEV *RHS); 430 431 /// forgetMemoizedResults - Drop memoized information computed for S. 432 void forgetMemoizedResults(const SCEV *S); 433 434 public: 435 static char ID; // Pass identification, replacement for typeid 436 ScalarEvolution(); 437 438 LLVMContext &getContext() const { return F->getContext(); } 439 440 /// isSCEVable - Test if values of the given type are analyzable within 441 /// the SCEV framework. This primarily includes integer types, and it 442 /// can optionally include pointer types if the ScalarEvolution class 443 /// has access to target-specific information. 444 bool isSCEVable(const Type *Ty) const; 445 446 /// getTypeSizeInBits - Return the size in bits of the specified type, 447 /// for which isSCEVable must return true. 448 uint64_t getTypeSizeInBits(const Type *Ty) const; 449 450 /// getEffectiveSCEVType - Return a type with the same bitwidth as 451 /// the given type and which represents how SCEV will treat the given 452 /// type, for which isSCEVable must return true. For pointer types, 453 /// this is the pointer-sized integer type. 454 const Type *getEffectiveSCEVType(const Type *Ty) const; 455 456 /// getSCEV - Return a SCEV expression for the full generality of the 457 /// specified expression. 458 const SCEV *getSCEV(Value *V); 459 460 const SCEV *getConstant(ConstantInt *V); 461 const SCEV *getConstant(const APInt& Val); 462 const SCEV *getConstant(const Type *Ty, uint64_t V, bool isSigned = false); 463 const SCEV *getTruncateExpr(const SCEV *Op, const Type *Ty); 464 const SCEV *getZeroExtendExpr(const SCEV *Op, const Type *Ty); 465 const SCEV *getSignExtendExpr(const SCEV *Op, const Type *Ty); 466 const SCEV *getAnyExtendExpr(const SCEV *Op, const Type *Ty); 467 const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops, 468 bool HasNUW = false, bool HasNSW = false); 469 const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS, 470 bool HasNUW = false, bool HasNSW = false) { 471 SmallVector<const SCEV *, 2> Ops; 472 Ops.push_back(LHS); 473 Ops.push_back(RHS); 474 return getAddExpr(Ops, HasNUW, HasNSW); 475 } 476 const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, 477 const SCEV *Op2, 478 bool HasNUW = false, bool HasNSW = false) { 479 SmallVector<const SCEV *, 3> Ops; 480 Ops.push_back(Op0); 481 Ops.push_back(Op1); 482 Ops.push_back(Op2); 483 return getAddExpr(Ops, HasNUW, HasNSW); 484 } 485 const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops, 486 bool HasNUW = false, bool HasNSW = false); 487 const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS, 488 bool HasNUW = false, bool HasNSW = false) { 489 SmallVector<const SCEV *, 2> Ops; 490 Ops.push_back(LHS); 491 Ops.push_back(RHS); 492 return getMulExpr(Ops, HasNUW, HasNSW); 493 } 494 const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS); 495 const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step, 496 const Loop *L, 497 bool HasNUW = false, bool HasNSW = false); 498 const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, 499 const Loop *L, 500 bool HasNUW = false, bool HasNSW = false); 501 const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands, 502 const Loop *L, 503 bool HasNUW = false, bool HasNSW = false) { 504 SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end()); 505 return getAddRecExpr(NewOp, L, HasNUW, HasNSW); 506 } 507 const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS); 508 const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands); 509 const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS); 510 const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands); 511 const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS); 512 const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS); 513 const SCEV *getUnknown(Value *V); 514 const SCEV *getCouldNotCompute(); 515 516 /// getSizeOfExpr - Return an expression for sizeof on the given type. 517 /// 518 const SCEV *getSizeOfExpr(const Type *AllocTy); 519 520 /// getAlignOfExpr - Return an expression for alignof on the given type. 521 /// 522 const SCEV *getAlignOfExpr(const Type *AllocTy); 523 524 /// getOffsetOfExpr - Return an expression for offsetof on the given field. 525 /// 526 const SCEV *getOffsetOfExpr(const StructType *STy, unsigned FieldNo); 527 528 /// getOffsetOfExpr - Return an expression for offsetof on the given field. 529 /// 530 const SCEV *getOffsetOfExpr(const Type *CTy, Constant *FieldNo); 531 532 /// getNegativeSCEV - Return the SCEV object corresponding to -V. 533 /// 534 const SCEV *getNegativeSCEV(const SCEV *V); 535 536 /// getNotSCEV - Return the SCEV object corresponding to ~V. 537 /// 538 const SCEV *getNotSCEV(const SCEV *V); 539 540 /// getMinusSCEV - Return LHS-RHS. 541 /// 542 const SCEV *getMinusSCEV(const SCEV *LHS, 543 const SCEV *RHS); 544 545 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion 546 /// of the input value to the specified type. If the type must be 547 /// extended, it is zero extended. 548 const SCEV *getTruncateOrZeroExtend(const SCEV *V, const Type *Ty); 549 550 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion 551 /// of the input value to the specified type. If the type must be 552 /// extended, it is sign extended. 553 const SCEV *getTruncateOrSignExtend(const SCEV *V, const Type *Ty); 554 555 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of 556 /// the input value to the specified type. If the type must be extended, 557 /// it is zero extended. The conversion must not be narrowing. 558 const SCEV *getNoopOrZeroExtend(const SCEV *V, const Type *Ty); 559 560 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of 561 /// the input value to the specified type. If the type must be extended, 562 /// it is sign extended. The conversion must not be narrowing. 563 const SCEV *getNoopOrSignExtend(const SCEV *V, const Type *Ty); 564 565 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of 566 /// the input value to the specified type. If the type must be extended, 567 /// it is extended with unspecified bits. The conversion must not be 568 /// narrowing. 569 const SCEV *getNoopOrAnyExtend(const SCEV *V, const Type *Ty); 570 571 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the 572 /// input value to the specified type. The conversion must not be 573 /// widening. 574 const SCEV *getTruncateOrNoop(const SCEV *V, const Type *Ty); 575 576 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of 577 /// the types using zero-extension, and then perform a umax operation 578 /// with them. 579 const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS, 580 const SCEV *RHS); 581 582 /// getUMinFromMismatchedTypes - Promote the operands to the wider of 583 /// the types using zero-extension, and then perform a umin operation 584 /// with them. 585 const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS, 586 const SCEV *RHS); 587 588 /// getSCEVAtScope - Return a SCEV expression for the specified value 589 /// at the specified scope in the program. The L value specifies a loop 590 /// nest to evaluate the expression at, where null is the top-level or a 591 /// specified loop is immediately inside of the loop. 592 /// 593 /// This method can be used to compute the exit value for a variable defined 594 /// in a loop by querying what the value will hold in the parent loop. 595 /// 596 /// In the case that a relevant loop exit value cannot be computed, the 597 /// original value V is returned. 598 const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L); 599 600 /// getSCEVAtScope - This is a convenience function which does 601 /// getSCEVAtScope(getSCEV(V), L). 602 const SCEV *getSCEVAtScope(Value *V, const Loop *L); 603 604 /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected 605 /// by a conditional between LHS and RHS. This is used to help avoid max 606 /// expressions in loop trip counts, and to eliminate casts. 607 bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 608 const SCEV *LHS, const SCEV *RHS); 609 610 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is 611 /// protected by a conditional between LHS and RHS. This is used to 612 /// to eliminate casts. 613 bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 614 const SCEV *LHS, const SCEV *RHS); 615 616 /// getBackedgeTakenCount - If the specified loop has a predictable 617 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute 618 /// object. The backedge-taken count is the number of times the loop header 619 /// will be branched to from within the loop. This is one less than the 620 /// trip count of the loop, since it doesn't count the first iteration, 621 /// when the header is branched to from outside the loop. 622 /// 623 /// Note that it is not valid to call this method on a loop without a 624 /// loop-invariant backedge-taken count (see 625 /// hasLoopInvariantBackedgeTakenCount). 626 /// 627 const SCEV *getBackedgeTakenCount(const Loop *L); 628 629 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except 630 /// return the least SCEV value that is known never to be less than the 631 /// actual backedge taken count. 632 const SCEV *getMaxBackedgeTakenCount(const Loop *L); 633 634 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop 635 /// has an analyzable loop-invariant backedge-taken count. 636 bool hasLoopInvariantBackedgeTakenCount(const Loop *L); 637 638 /// forgetLoop - This method should be called by the client when it has 639 /// changed a loop in a way that may effect ScalarEvolution's ability to 640 /// compute a trip count, or if the loop is deleted. 641 void forgetLoop(const Loop *L); 642 643 /// forgetValue - This method should be called by the client when it has 644 /// changed a value in a way that may effect its value, or which may 645 /// disconnect it from a def-use chain linking it to a loop. 646 void forgetValue(Value *V); 647 648 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S 649 /// is guaranteed to end in (at every loop iteration). It is, at the same 650 /// time, the minimum number of times S is divisible by 2. For example, 651 /// given {4,+,8} it returns 2. If S is guaranteed to be 0, it returns the 652 /// bitwidth of S. 653 uint32_t GetMinTrailingZeros(const SCEV *S); 654 655 /// getUnsignedRange - Determine the unsigned range for a particular SCEV. 656 /// 657 ConstantRange getUnsignedRange(const SCEV *S); 658 659 /// getSignedRange - Determine the signed range for a particular SCEV. 660 /// 661 ConstantRange getSignedRange(const SCEV *S); 662 663 /// isKnownNegative - Test if the given expression is known to be negative. 664 /// 665 bool isKnownNegative(const SCEV *S); 666 667 /// isKnownPositive - Test if the given expression is known to be positive. 668 /// 669 bool isKnownPositive(const SCEV *S); 670 671 /// isKnownNonNegative - Test if the given expression is known to be 672 /// non-negative. 673 /// 674 bool isKnownNonNegative(const SCEV *S); 675 676 /// isKnownNonPositive - Test if the given expression is known to be 677 /// non-positive. 678 /// 679 bool isKnownNonPositive(const SCEV *S); 680 681 /// isKnownNonZero - Test if the given expression is known to be 682 /// non-zero. 683 /// 684 bool isKnownNonZero(const SCEV *S); 685 686 /// isKnownPredicate - Test if the given expression is known to satisfy 687 /// the condition described by Pred, LHS, and RHS. 688 /// 689 bool isKnownPredicate(ICmpInst::Predicate Pred, 690 const SCEV *LHS, const SCEV *RHS); 691 692 /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with 693 /// predicate Pred. Return true iff any changes were made. If the 694 /// operands are provably equal or inequal, LHS and RHS are set to 695 /// the same value and Pred is set to either ICMP_EQ or ICMP_NE. 696 /// 697 bool SimplifyICmpOperands(ICmpInst::Predicate &Pred, 698 const SCEV *&LHS, 699 const SCEV *&RHS); 700 701 /// getLoopDisposition - Return the "disposition" of the given SCEV with 702 /// respect to the given loop. 703 LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L); 704 705 /// isLoopInvariant - Return true if the value of the given SCEV is 706 /// unchanging in the specified loop. 707 bool isLoopInvariant(const SCEV *S, const Loop *L); 708 709 /// hasComputableLoopEvolution - Return true if the given SCEV changes value 710 /// in a known way in the specified loop. This property being true implies 711 /// that the value is variant in the loop AND that we can emit an expression 712 /// to compute the value of the expression at any particular loop iteration. 713 bool hasComputableLoopEvolution(const SCEV *S, const Loop *L); 714 715 /// getLoopDisposition - Return the "disposition" of the given SCEV with 716 /// respect to the given block. 717 BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB); 718 719 /// dominates - Return true if elements that makes up the given SCEV 720 /// dominate the specified basic block. 721 bool dominates(const SCEV *S, const BasicBlock *BB); 722 723 /// properlyDominates - Return true if elements that makes up the given SCEV 724 /// properly dominate the specified basic block. 725 bool properlyDominates(const SCEV *S, const BasicBlock *BB); 726 727 /// hasOperand - Test whether the given SCEV has Op as a direct or 728 /// indirect operand. 729 bool hasOperand(const SCEV *S, const SCEV *Op) const; 730 731 virtual bool runOnFunction(Function &F); 732 virtual void releaseMemory(); 733 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 734 virtual void print(raw_ostream &OS, const Module* = 0) const; 735 736 private: 737 FoldingSet<SCEV> UniqueSCEVs; 738 BumpPtrAllocator SCEVAllocator; 739 740 /// FirstUnknown - The head of a linked list of all SCEVUnknown 741 /// values that have been allocated. This is used by releaseMemory 742 /// to locate them all and call their destructors. 743 SCEVUnknown *FirstUnknown; 744 }; 745} 746 747#endif 748