ScalarEvolutionExpressions.h revision 2f199f9952b9dd62b5a0d0f4350b8fa780ebb9cc
1//===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- 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// This file defines the classes used to represent and build scalar expressions. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPRESSIONS_H 15#define LLVM_ANALYSIS_SCALAREVOLUTION_EXPRESSIONS_H 16 17#include "llvm/Analysis/ScalarEvolution.h" 18#include "llvm/Support/ErrorHandling.h" 19 20namespace llvm { 21 class ConstantInt; 22 class ConstantRange; 23 class DominatorTree; 24 25 enum SCEVTypes { 26 // These should be ordered in terms of increasing complexity to make the 27 // folders simpler. 28 scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr, 29 scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr, 30 scUnknown, scCouldNotCompute 31 }; 32 33 //===--------------------------------------------------------------------===// 34 /// SCEVConstant - This class represents a constant integer value. 35 /// 36 class SCEVConstant : public SCEV { 37 friend class ScalarEvolution; 38 39 ConstantInt *V; 40 SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) : 41 SCEV(ID, scConstant), V(v) {} 42 public: 43 ConstantInt *getValue() const { return V; } 44 45 virtual bool isLoopInvariant(const Loop *L) const { 46 return true; 47 } 48 49 virtual bool hasComputableLoopEvolution(const Loop *L) const { 50 return false; // Not loop variant 51 } 52 53 virtual const Type *getType() const; 54 55 virtual bool hasOperand(const SCEV *) const { 56 return false; 57 } 58 59 bool dominates(BasicBlock *BB, DominatorTree *DT) const { 60 return true; 61 } 62 63 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const { 64 return true; 65 } 66 67 virtual void print(raw_ostream &OS) const; 68 69 /// Methods for support type inquiry through isa, cast, and dyn_cast: 70 static inline bool classof(const SCEVConstant *S) { return true; } 71 static inline bool classof(const SCEV *S) { 72 return S->getSCEVType() == scConstant; 73 } 74 }; 75 76 //===--------------------------------------------------------------------===// 77 /// SCEVCastExpr - This is the base class for unary cast operator classes. 78 /// 79 class SCEVCastExpr : public SCEV { 80 protected: 81 const SCEV *Op; 82 const Type *Ty; 83 84 SCEVCastExpr(const FoldingSetNodeIDRef ID, 85 unsigned SCEVTy, const SCEV *op, const Type *ty); 86 87 public: 88 const SCEV *getOperand() const { return Op; } 89 virtual const Type *getType() const { return Ty; } 90 91 virtual bool isLoopInvariant(const Loop *L) const { 92 return Op->isLoopInvariant(L); 93 } 94 95 virtual bool hasComputableLoopEvolution(const Loop *L) const { 96 return Op->hasComputableLoopEvolution(L); 97 } 98 99 virtual bool hasOperand(const SCEV *O) const { 100 return Op == O || Op->hasOperand(O); 101 } 102 103 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const; 104 105 virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 106 107 /// Methods for support type inquiry through isa, cast, and dyn_cast: 108 static inline bool classof(const SCEVCastExpr *S) { return true; } 109 static inline bool classof(const SCEV *S) { 110 return S->getSCEVType() == scTruncate || 111 S->getSCEVType() == scZeroExtend || 112 S->getSCEVType() == scSignExtend; 113 } 114 }; 115 116 //===--------------------------------------------------------------------===// 117 /// SCEVTruncateExpr - This class represents a truncation of an integer value 118 /// to a smaller integer value. 119 /// 120 class SCEVTruncateExpr : public SCEVCastExpr { 121 friend class ScalarEvolution; 122 123 SCEVTruncateExpr(const FoldingSetNodeIDRef ID, 124 const SCEV *op, const Type *ty); 125 126 public: 127 virtual void print(raw_ostream &OS) const; 128 129 virtual bool hasComputableLoopEvolution(const Loop *QL) const { 130 // Not computable. A truncate of an addrec is always folded into 131 // the addrec. 132 return false; 133 } 134 135 /// Methods for support type inquiry through isa, cast, and dyn_cast: 136 static inline bool classof(const SCEVTruncateExpr *S) { return true; } 137 static inline bool classof(const SCEV *S) { 138 return S->getSCEVType() == scTruncate; 139 } 140 }; 141 142 //===--------------------------------------------------------------------===// 143 /// SCEVZeroExtendExpr - This class represents a zero extension of a small 144 /// integer value to a larger integer value. 145 /// 146 class SCEVZeroExtendExpr : public SCEVCastExpr { 147 friend class ScalarEvolution; 148 149 SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, 150 const SCEV *op, const Type *ty); 151 152 public: 153 virtual void print(raw_ostream &OS) const; 154 155 /// Methods for support type inquiry through isa, cast, and dyn_cast: 156 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; } 157 static inline bool classof(const SCEV *S) { 158 return S->getSCEVType() == scZeroExtend; 159 } 160 }; 161 162 //===--------------------------------------------------------------------===// 163 /// SCEVSignExtendExpr - This class represents a sign extension of a small 164 /// integer value to a larger integer value. 165 /// 166 class SCEVSignExtendExpr : public SCEVCastExpr { 167 friend class ScalarEvolution; 168 169 SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, 170 const SCEV *op, const Type *ty); 171 172 public: 173 virtual void print(raw_ostream &OS) const; 174 175 /// Methods for support type inquiry through isa, cast, and dyn_cast: 176 static inline bool classof(const SCEVSignExtendExpr *S) { return true; } 177 static inline bool classof(const SCEV *S) { 178 return S->getSCEVType() == scSignExtend; 179 } 180 }; 181 182 183 //===--------------------------------------------------------------------===// 184 /// SCEVNAryExpr - This node is a base class providing common 185 /// functionality for n'ary operators. 186 /// 187 class SCEVNAryExpr : public SCEV { 188 protected: 189 // Since SCEVs are immutable, ScalarEvolution allocates operand 190 // arrays with its SCEVAllocator, so this class just needs a simple 191 // pointer rather than a more elaborate vector-like data structure. 192 // This also avoids the need for a non-trivial destructor. 193 const SCEV *const *Operands; 194 size_t NumOperands; 195 196 SCEVNAryExpr(const FoldingSetNodeIDRef ID, 197 enum SCEVTypes T, const SCEV *const *O, size_t N) 198 : SCEV(ID, T), Operands(O), NumOperands(N) {} 199 200 public: 201 size_t getNumOperands() const { return NumOperands; } 202 const SCEV *getOperand(unsigned i) const { 203 assert(i < NumOperands && "Operand index out of range!"); 204 return Operands[i]; 205 } 206 207 typedef const SCEV *const *op_iterator; 208 op_iterator op_begin() const { return Operands; } 209 op_iterator op_end() const { return Operands + NumOperands; } 210 211 virtual bool isLoopInvariant(const Loop *L) const; 212 213 // hasComputableLoopEvolution - N-ary expressions have computable loop 214 // evolutions iff they have at least one operand that varies with the loop, 215 // but that all varying operands are computable. 216 virtual bool hasComputableLoopEvolution(const Loop *L) const; 217 218 virtual bool hasOperand(const SCEV *O) const; 219 220 bool dominates(BasicBlock *BB, DominatorTree *DT) const; 221 222 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 223 224 virtual const Type *getType() const { return getOperand(0)->getType(); } 225 226 bool hasNoUnsignedWrap() const { return SubclassData & (1 << 0); } 227 void setHasNoUnsignedWrap(bool B) { 228 SubclassData = (SubclassData & ~(1 << 0)) | (B << 0); 229 } 230 bool hasNoSignedWrap() const { return SubclassData & (1 << 1); } 231 void setHasNoSignedWrap(bool B) { 232 SubclassData = (SubclassData & ~(1 << 1)) | (B << 1); 233 } 234 235 /// Methods for support type inquiry through isa, cast, and dyn_cast: 236 static inline bool classof(const SCEVNAryExpr *S) { return true; } 237 static inline bool classof(const SCEV *S) { 238 return S->getSCEVType() == scAddExpr || 239 S->getSCEVType() == scMulExpr || 240 S->getSCEVType() == scSMaxExpr || 241 S->getSCEVType() == scUMaxExpr || 242 S->getSCEVType() == scAddRecExpr; 243 } 244 }; 245 246 //===--------------------------------------------------------------------===// 247 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative 248 /// operators. 249 /// 250 class SCEVCommutativeExpr : public SCEVNAryExpr { 251 protected: 252 SCEVCommutativeExpr(const FoldingSetNodeIDRef ID, 253 enum SCEVTypes T, const SCEV *const *O, size_t N) 254 : SCEVNAryExpr(ID, T, O, N) {} 255 256 public: 257 virtual const char *getOperationStr() const = 0; 258 259 virtual void print(raw_ostream &OS) const; 260 261 /// Methods for support type inquiry through isa, cast, and dyn_cast: 262 static inline bool classof(const SCEVCommutativeExpr *S) { return true; } 263 static inline bool classof(const SCEV *S) { 264 return S->getSCEVType() == scAddExpr || 265 S->getSCEVType() == scMulExpr || 266 S->getSCEVType() == scSMaxExpr || 267 S->getSCEVType() == scUMaxExpr; 268 } 269 }; 270 271 272 //===--------------------------------------------------------------------===// 273 /// SCEVAddExpr - This node represents an addition of some number of SCEVs. 274 /// 275 class SCEVAddExpr : public SCEVCommutativeExpr { 276 friend class ScalarEvolution; 277 278 SCEVAddExpr(const FoldingSetNodeIDRef ID, 279 const SCEV *const *O, size_t N) 280 : SCEVCommutativeExpr(ID, scAddExpr, O, N) { 281 } 282 283 public: 284 virtual bool hasComputableLoopEvolution(const Loop *QL) const { 285 // Not computable. An add of an addrec is always folded into the addrec 286 // if the other operands are loop-variant or loop-computable. 287 return false; 288 } 289 290 virtual const char *getOperationStr() const { return " + "; } 291 292 virtual const Type *getType() const { 293 // Use the type of the last operand, which is likely to be a pointer 294 // type, if there is one. This doesn't usually matter, but it can help 295 // reduce casts when the expressions are expanded. 296 return getOperand(getNumOperands() - 1)->getType(); 297 } 298 299 /// Methods for support type inquiry through isa, cast, and dyn_cast: 300 static inline bool classof(const SCEVAddExpr *S) { return true; } 301 static inline bool classof(const SCEV *S) { 302 return S->getSCEVType() == scAddExpr; 303 } 304 }; 305 306 //===--------------------------------------------------------------------===// 307 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs. 308 /// 309 class SCEVMulExpr : public SCEVCommutativeExpr { 310 friend class ScalarEvolution; 311 312 SCEVMulExpr(const FoldingSetNodeIDRef ID, 313 const SCEV *const *O, size_t N) 314 : SCEVCommutativeExpr(ID, scMulExpr, O, N) { 315 } 316 317 public: 318 virtual bool hasComputableLoopEvolution(const Loop *QL) const { 319 // Not computable. A mul of an addrec is always folded into the addrec 320 // if the other operands are loop-variant or loop-computable. 321 return false; 322 } 323 324 virtual const char *getOperationStr() const { return " * "; } 325 326 /// Methods for support type inquiry through isa, cast, and dyn_cast: 327 static inline bool classof(const SCEVMulExpr *S) { return true; } 328 static inline bool classof(const SCEV *S) { 329 return S->getSCEVType() == scMulExpr; 330 } 331 }; 332 333 334 //===--------------------------------------------------------------------===// 335 /// SCEVUDivExpr - This class represents a binary unsigned division operation. 336 /// 337 class SCEVUDivExpr : public SCEV { 338 friend class ScalarEvolution; 339 340 const SCEV *LHS; 341 const SCEV *RHS; 342 SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs) 343 : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {} 344 345 public: 346 const SCEV *getLHS() const { return LHS; } 347 const SCEV *getRHS() const { return RHS; } 348 349 virtual bool isLoopInvariant(const Loop *L) const { 350 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L); 351 } 352 353 virtual bool hasComputableLoopEvolution(const Loop *L) const { 354 return LHS->hasComputableLoopEvolution(L) && 355 RHS->hasComputableLoopEvolution(L); 356 } 357 358 virtual bool hasOperand(const SCEV *O) const { 359 return O == LHS || O == RHS || LHS->hasOperand(O) || RHS->hasOperand(O); 360 } 361 362 bool dominates(BasicBlock *BB, DominatorTree *DT) const; 363 364 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 365 366 virtual const Type *getType() const; 367 368 void print(raw_ostream &OS) const; 369 370 /// Methods for support type inquiry through isa, cast, and dyn_cast: 371 static inline bool classof(const SCEVUDivExpr *S) { return true; } 372 static inline bool classof(const SCEV *S) { 373 return S->getSCEVType() == scUDivExpr; 374 } 375 }; 376 377 378 //===--------------------------------------------------------------------===// 379 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip 380 /// count of the specified loop. This is the primary focus of the 381 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just 382 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be 383 /// created and analyzed. 384 /// 385 /// All operands of an AddRec are required to be loop invariant. 386 /// 387 class SCEVAddRecExpr : public SCEVNAryExpr { 388 friend class ScalarEvolution; 389 390 const Loop *L; 391 392 SCEVAddRecExpr(const FoldingSetNodeIDRef ID, 393 const SCEV *const *O, size_t N, const Loop *l) 394 : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) { 395 for (size_t i = 0, e = NumOperands; i != e; ++i) 396 assert(Operands[i]->isLoopInvariant(l) && 397 "Operands of AddRec must be loop-invariant!"); 398 } 399 400 public: 401 const SCEV *getStart() const { return Operands[0]; } 402 const Loop *getLoop() const { return L; } 403 404 /// getStepRecurrence - This method constructs and returns the recurrence 405 /// indicating how much this expression steps by. If this is a polynomial 406 /// of degree N, it returns a chrec of degree N-1. 407 const SCEV *getStepRecurrence(ScalarEvolution &SE) const { 408 if (isAffine()) return getOperand(1); 409 return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1, 410 op_end()), 411 getLoop()); 412 } 413 414 virtual bool hasComputableLoopEvolution(const Loop *QL) const { 415 return L == QL; 416 } 417 418 virtual bool isLoopInvariant(const Loop *QueryLoop) const; 419 420 bool dominates(BasicBlock *BB, DominatorTree *DT) const; 421 422 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 423 424 /// isAffine - Return true if this is an affine AddRec (i.e., it represents 425 /// an expressions A+B*x where A and B are loop invariant values. 426 bool isAffine() const { 427 // We know that the start value is invariant. This expression is thus 428 // affine iff the step is also invariant. 429 return getNumOperands() == 2; 430 } 431 432 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it 433 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop 434 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N} 435 bool isQuadratic() const { 436 return getNumOperands() == 3; 437 } 438 439 /// evaluateAtIteration - Return the value of this chain of recurrences at 440 /// the specified iteration number. 441 const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const; 442 443 /// getNumIterationsInRange - Return the number of iterations of this loop 444 /// that produce values in the specified constant range. Another way of 445 /// looking at this is that it returns the first iteration number where the 446 /// value is not in the condition, thus computing the exit count. If the 447 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is 448 /// returned. 449 const SCEV *getNumIterationsInRange(ConstantRange Range, 450 ScalarEvolution &SE) const; 451 452 /// getPostIncExpr - Return an expression representing the value of 453 /// this expression one iteration of the loop ahead. 454 const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const { 455 return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE))); 456 } 457 458 virtual void print(raw_ostream &OS) const; 459 460 /// Methods for support type inquiry through isa, cast, and dyn_cast: 461 static inline bool classof(const SCEVAddRecExpr *S) { return true; } 462 static inline bool classof(const SCEV *S) { 463 return S->getSCEVType() == scAddRecExpr; 464 } 465 }; 466 467 468 //===--------------------------------------------------------------------===// 469 /// SCEVSMaxExpr - This class represents a signed maximum selection. 470 /// 471 class SCEVSMaxExpr : public SCEVCommutativeExpr { 472 friend class ScalarEvolution; 473 474 SCEVSMaxExpr(const FoldingSetNodeIDRef ID, 475 const SCEV *const *O, size_t N) 476 : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) { 477 // Max never overflows. 478 setHasNoUnsignedWrap(true); 479 setHasNoSignedWrap(true); 480 } 481 482 public: 483 virtual const char *getOperationStr() const { return " smax "; } 484 485 /// Methods for support type inquiry through isa, cast, and dyn_cast: 486 static inline bool classof(const SCEVSMaxExpr *S) { return true; } 487 static inline bool classof(const SCEV *S) { 488 return S->getSCEVType() == scSMaxExpr; 489 } 490 }; 491 492 493 //===--------------------------------------------------------------------===// 494 /// SCEVUMaxExpr - This class represents an unsigned maximum selection. 495 /// 496 class SCEVUMaxExpr : public SCEVCommutativeExpr { 497 friend class ScalarEvolution; 498 499 SCEVUMaxExpr(const FoldingSetNodeIDRef ID, 500 const SCEV *const *O, size_t N) 501 : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) { 502 // Max never overflows. 503 setHasNoUnsignedWrap(true); 504 setHasNoSignedWrap(true); 505 } 506 507 public: 508 virtual const char *getOperationStr() const { return " umax "; } 509 510 /// Methods for support type inquiry through isa, cast, and dyn_cast: 511 static inline bool classof(const SCEVUMaxExpr *S) { return true; } 512 static inline bool classof(const SCEV *S) { 513 return S->getSCEVType() == scUMaxExpr; 514 } 515 }; 516 517 //===--------------------------------------------------------------------===// 518 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV 519 /// value, and only represent it as its LLVM Value. This is the "bottom" 520 /// value for the analysis. 521 /// 522 class SCEVUnknown : public SCEV, private CallbackVH { 523 friend class ScalarEvolution; 524 525 // Implement CallbackVH. 526 virtual void deleted(); 527 virtual void allUsesReplacedWith(Value *New); 528 529 /// SE - The parent ScalarEvolution value. This is used to update 530 /// the parent's maps when the value associated with a SCEVUnknown 531 /// is deleted or RAUW'd. 532 ScalarEvolution *SE; 533 534 /// Next - The next pointer in the linked list of all 535 /// SCEVUnknown instances owned by a ScalarEvolution. 536 SCEVUnknown *Next; 537 538 SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V, 539 ScalarEvolution *se, SCEVUnknown *next) : 540 SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {} 541 542 public: 543 Value *getValue() const { return getValPtr(); } 544 545 /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special 546 /// constant representing a type size, alignment, or field offset in 547 /// a target-independent manner, and hasn't happened to have been 548 /// folded with other operations into something unrecognizable. This 549 /// is mainly only useful for pretty-printing and other situations 550 /// where it isn't absolutely required for these to succeed. 551 bool isSizeOf(const Type *&AllocTy) const; 552 bool isAlignOf(const Type *&AllocTy) const; 553 bool isOffsetOf(const Type *&STy, Constant *&FieldNo) const; 554 555 virtual bool isLoopInvariant(const Loop *L) const; 556 virtual bool hasComputableLoopEvolution(const Loop *QL) const { 557 return false; // not computable 558 } 559 560 virtual bool hasOperand(const SCEV *) const { 561 return false; 562 } 563 564 bool dominates(BasicBlock *BB, DominatorTree *DT) const; 565 566 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 567 568 virtual const Type *getType() const; 569 570 virtual void print(raw_ostream &OS) const; 571 572 /// Methods for support type inquiry through isa, cast, and dyn_cast: 573 static inline bool classof(const SCEVUnknown *S) { return true; } 574 static inline bool classof(const SCEV *S) { 575 return S->getSCEVType() == scUnknown; 576 } 577 }; 578 579 /// SCEVVisitor - This class defines a simple visitor class that may be used 580 /// for various SCEV analysis purposes. 581 template<typename SC, typename RetVal=void> 582 struct SCEVVisitor { 583 RetVal visit(const SCEV *S) { 584 switch (S->getSCEVType()) { 585 case scConstant: 586 return ((SC*)this)->visitConstant((const SCEVConstant*)S); 587 case scTruncate: 588 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S); 589 case scZeroExtend: 590 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S); 591 case scSignExtend: 592 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S); 593 case scAddExpr: 594 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S); 595 case scMulExpr: 596 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S); 597 case scUDivExpr: 598 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S); 599 case scAddRecExpr: 600 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S); 601 case scSMaxExpr: 602 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S); 603 case scUMaxExpr: 604 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S); 605 case scUnknown: 606 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S); 607 case scCouldNotCompute: 608 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S); 609 default: 610 llvm_unreachable("Unknown SCEV type!"); 611 } 612 } 613 614 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) { 615 llvm_unreachable("Invalid use of SCEVCouldNotCompute!"); 616 return RetVal(); 617 } 618 }; 619} 620 621#endif 622