ScalarEvolutionExpressions.h 26 KB

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