Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

698 lines
24 KiB

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