ScalarEvolutionExpander.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the classes used to generate code from scalar expressions.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
  18. #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/DenseSet.h"
  21. #include "llvm/ADT/Optional.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  24. #include "llvm/Analysis/ScalarEvolutionNormalization.h"
  25. #include "llvm/Analysis/TargetFolder.h"
  26. #include "llvm/Analysis/TargetTransformInfo.h"
  27. #include "llvm/IR/IRBuilder.h"
  28. #include "llvm/IR/ValueHandle.h"
  29. #include "llvm/Support/CommandLine.h"
  30. namespace llvm {
  31. extern cl::opt<unsigned> SCEVCheapExpansionBudget;
  32. /// Return true if the given expression is safe to expand in the sense that
  33. /// all materialized values are safe to speculate anywhere their operands are
  34. /// defined.
  35. bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE);
  36. /// Return true if the given expression is safe to expand in the sense that
  37. /// all materialized values are defined and safe to speculate at the specified
  38. /// location and their operands are defined at this location.
  39. bool isSafeToExpandAt(const SCEV *S, const Instruction *InsertionPoint,
  40. ScalarEvolution &SE);
  41. /// struct for holding enough information to help calculate the cost of the
  42. /// given SCEV when expanded into IR.
  43. struct SCEVOperand {
  44. explicit SCEVOperand(unsigned Opc, int Idx, const SCEV *S) :
  45. ParentOpcode(Opc), OperandIdx(Idx), S(S) { }
  46. /// LLVM instruction opcode that uses the operand.
  47. unsigned ParentOpcode;
  48. /// The use index of an expanded instruction.
  49. int OperandIdx;
  50. /// The SCEV operand to be costed.
  51. const SCEV* S;
  52. };
  53. /// This class uses information about analyze scalars to rewrite expressions
  54. /// in canonical form.
  55. ///
  56. /// Clients should create an instance of this class when rewriting is needed,
  57. /// and destroy it when finished to allow the release of the associated
  58. /// memory.
  59. class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
  60. ScalarEvolution &SE;
  61. const DataLayout &DL;
  62. // New instructions receive a name to identify them with the current pass.
  63. const char *IVName;
  64. /// Indicates whether LCSSA phis should be created for inserted values.
  65. bool PreserveLCSSA;
  66. // InsertedExpressions caches Values for reuse, so must track RAUW.
  67. DenseMap<std::pair<const SCEV *, Instruction *>, TrackingVH<Value>>
  68. InsertedExpressions;
  69. // InsertedValues only flags inserted instructions so needs no RAUW.
  70. DenseSet<AssertingVH<Value>> InsertedValues;
  71. DenseSet<AssertingVH<Value>> InsertedPostIncValues;
  72. /// Keep track of the existing IR values re-used during expansion.
  73. /// FIXME: Ideally re-used instructions would not be added to
  74. /// InsertedValues/InsertedPostIncValues.
  75. SmallPtrSet<Value *, 16> ReusedValues;
  76. /// A memoization of the "relevant" loop for a given SCEV.
  77. DenseMap<const SCEV *, const Loop *> RelevantLoops;
  78. /// Addrecs referring to any of the given loops are expanded in post-inc
  79. /// mode. For example, expanding {1,+,1}<L> in post-inc mode returns the add
  80. /// instruction that adds one to the phi for {0,+,1}<L>, as opposed to a new
  81. /// phi starting at 1. This is only supported in non-canonical mode.
  82. PostIncLoopSet PostIncLoops;
  83. /// When this is non-null, addrecs expanded in the loop it indicates should
  84. /// be inserted with increments at IVIncInsertPos.
  85. const Loop *IVIncInsertLoop;
  86. /// When expanding addrecs in the IVIncInsertLoop loop, insert the IV
  87. /// increment at this position.
  88. Instruction *IVIncInsertPos;
  89. /// Phis that complete an IV chain. Reuse
  90. DenseSet<AssertingVH<PHINode>> ChainedPhis;
  91. /// When true, SCEVExpander tries to expand expressions in "canonical" form.
  92. /// When false, expressions are expanded in a more literal form.
  93. ///
  94. /// In "canonical" form addrecs are expanded as arithmetic based on a
  95. /// canonical induction variable. Note that CanonicalMode doesn't guarantee
  96. /// that all expressions are expanded in "canonical" form. For some
  97. /// expressions literal mode can be preferred.
  98. bool CanonicalMode;
  99. /// When invoked from LSR, the expander is in "strength reduction" mode. The
  100. /// only difference is that phi's are only reused if they are already in
  101. /// "expanded" form.
  102. bool LSRMode;
  103. typedef IRBuilder<TargetFolder, IRBuilderCallbackInserter> BuilderType;
  104. BuilderType Builder;
  105. // RAII object that stores the current insertion point and restores it when
  106. // the object is destroyed. This includes the debug location. Duplicated
  107. // from InsertPointGuard to add SetInsertPoint() which is used to updated
  108. // InsertPointGuards stack when insert points are moved during SCEV
  109. // expansion.
  110. class SCEVInsertPointGuard {
  111. IRBuilderBase &Builder;
  112. AssertingVH<BasicBlock> Block;
  113. BasicBlock::iterator Point;
  114. DebugLoc DbgLoc;
  115. SCEVExpander *SE;
  116. SCEVInsertPointGuard(const SCEVInsertPointGuard &) = delete;
  117. SCEVInsertPointGuard &operator=(const SCEVInsertPointGuard &) = delete;
  118. public:
  119. SCEVInsertPointGuard(IRBuilderBase &B, SCEVExpander *SE)
  120. : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
  121. DbgLoc(B.getCurrentDebugLocation()), SE(SE) {
  122. SE->InsertPointGuards.push_back(this);
  123. }
  124. ~SCEVInsertPointGuard() {
  125. // These guards should always created/destroyed in FIFO order since they
  126. // are used to guard lexically scoped blocks of code in
  127. // ScalarEvolutionExpander.
  128. assert(SE->InsertPointGuards.back() == this);
  129. SE->InsertPointGuards.pop_back();
  130. Builder.restoreIP(IRBuilderBase::InsertPoint(Block, Point));
  131. Builder.SetCurrentDebugLocation(DbgLoc);
  132. }
  133. BasicBlock::iterator GetInsertPoint() const { return Point; }
  134. void SetInsertPoint(BasicBlock::iterator I) { Point = I; }
  135. };
  136. /// Stack of pointers to saved insert points, used to keep insert points
  137. /// consistent when instructions are moved.
  138. SmallVector<SCEVInsertPointGuard *, 8> InsertPointGuards;
  139. #ifndef NDEBUG
  140. const char *DebugType;
  141. #endif
  142. friend struct SCEVVisitor<SCEVExpander, Value *>;
  143. public:
  144. /// Construct a SCEVExpander in "canonical" mode.
  145. explicit SCEVExpander(ScalarEvolution &se, const DataLayout &DL,
  146. const char *name, bool PreserveLCSSA = true)
  147. : SE(se), DL(DL), IVName(name), PreserveLCSSA(PreserveLCSSA),
  148. IVIncInsertLoop(nullptr), IVIncInsertPos(nullptr), CanonicalMode(true),
  149. LSRMode(false),
  150. Builder(se.getContext(), TargetFolder(DL),
  151. IRBuilderCallbackInserter(
  152. [this](Instruction *I) { rememberInstruction(I); })) {
  153. #ifndef NDEBUG
  154. DebugType = "";
  155. #endif
  156. }
  157. ~SCEVExpander() {
  158. // Make sure the insert point guard stack is consistent.
  159. assert(InsertPointGuards.empty());
  160. }
  161. #ifndef NDEBUG
  162. void setDebugType(const char *s) { DebugType = s; }
  163. #endif
  164. /// Erase the contents of the InsertedExpressions map so that users trying
  165. /// to expand the same expression into multiple BasicBlocks or different
  166. /// places within the same BasicBlock can do so.
  167. void clear() {
  168. InsertedExpressions.clear();
  169. InsertedValues.clear();
  170. InsertedPostIncValues.clear();
  171. ReusedValues.clear();
  172. ChainedPhis.clear();
  173. }
  174. /// Return a vector containing all instructions inserted during expansion.
  175. SmallVector<Instruction *, 32> getAllInsertedInstructions() const {
  176. SmallVector<Instruction *, 32> Result;
  177. for (auto &VH : InsertedValues) {
  178. Value *V = VH;
  179. if (ReusedValues.contains(V))
  180. continue;
  181. if (auto *Inst = dyn_cast<Instruction>(V))
  182. Result.push_back(Inst);
  183. }
  184. for (auto &VH : InsertedPostIncValues) {
  185. Value *V = VH;
  186. if (ReusedValues.contains(V))
  187. continue;
  188. if (auto *Inst = dyn_cast<Instruction>(V))
  189. Result.push_back(Inst);
  190. }
  191. return Result;
  192. }
  193. /// Return true for expressions that can't be evaluated at runtime
  194. /// within given \b Budget.
  195. ///
  196. /// At is a parameter which specifies point in code where user is going to
  197. /// expand this expression. Sometimes this knowledge can lead to
  198. /// a less pessimistic cost estimation.
  199. bool isHighCostExpansion(const SCEV *Expr, Loop *L, unsigned Budget,
  200. const TargetTransformInfo *TTI,
  201. const Instruction *At) {
  202. assert(TTI && "This function requires TTI to be provided.");
  203. assert(At && "This function requires At instruction to be provided.");
  204. if (!TTI) // In assert-less builds, avoid crashing
  205. return true; // by always claiming to be high-cost.
  206. SmallVector<SCEVOperand, 8> Worklist;
  207. SmallPtrSet<const SCEV *, 8> Processed;
  208. int BudgetRemaining = Budget * TargetTransformInfo::TCC_Basic;
  209. Worklist.emplace_back(-1, -1, Expr);
  210. while (!Worklist.empty()) {
  211. const SCEVOperand WorkItem = Worklist.pop_back_val();
  212. if (isHighCostExpansionHelper(WorkItem, L, *At, BudgetRemaining,
  213. *TTI, Processed, Worklist))
  214. return true;
  215. }
  216. assert(BudgetRemaining >= 0 && "Should have returned from inner loop.");
  217. return false;
  218. }
  219. /// Return the induction variable increment's IV operand.
  220. Instruction *getIVIncOperand(Instruction *IncV, Instruction *InsertPos,
  221. bool allowScale);
  222. /// Utility for hoisting an IV increment.
  223. bool hoistIVInc(Instruction *IncV, Instruction *InsertPos);
  224. /// replace congruent phis with their most canonical representative. Return
  225. /// the number of phis eliminated.
  226. unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
  227. SmallVectorImpl<WeakTrackingVH> &DeadInsts,
  228. const TargetTransformInfo *TTI = nullptr);
  229. /// Insert code to directly compute the specified SCEV expression into the
  230. /// program. The code is inserted into the specified block.
  231. Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I) {
  232. return expandCodeForImpl(SH, Ty, I, true);
  233. }
  234. /// Insert code to directly compute the specified SCEV expression into the
  235. /// program. The code is inserted into the SCEVExpander's current
  236. /// insertion point. If a type is specified, the result will be expanded to
  237. /// have that type, with a cast if necessary.
  238. Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr) {
  239. return expandCodeForImpl(SH, Ty, true);
  240. }
  241. /// Generates a code sequence that evaluates this predicate. The inserted
  242. /// instructions will be at position \p Loc. The result will be of type i1
  243. /// and will have a value of 0 when the predicate is false and 1 otherwise.
  244. Value *expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc);
  245. /// A specialized variant of expandCodeForPredicate, handling the case when
  246. /// we are expanding code for a SCEVEqualPredicate.
  247. Value *expandEqualPredicate(const SCEVEqualPredicate *Pred, Instruction *Loc);
  248. /// Generates code that evaluates if the \p AR expression will overflow.
  249. Value *generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc,
  250. bool Signed);
  251. /// A specialized variant of expandCodeForPredicate, handling the case when
  252. /// we are expanding code for a SCEVWrapPredicate.
  253. Value *expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc);
  254. /// A specialized variant of expandCodeForPredicate, handling the case when
  255. /// we are expanding code for a SCEVUnionPredicate.
  256. Value *expandUnionPredicate(const SCEVUnionPredicate *Pred, Instruction *Loc);
  257. /// Set the current IV increment loop and position.
  258. void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
  259. assert(!CanonicalMode &&
  260. "IV increment positions are not supported in CanonicalMode");
  261. IVIncInsertLoop = L;
  262. IVIncInsertPos = Pos;
  263. }
  264. /// Enable post-inc expansion for addrecs referring to the given
  265. /// loops. Post-inc expansion is only supported in non-canonical mode.
  266. void setPostInc(const PostIncLoopSet &L) {
  267. assert(!CanonicalMode &&
  268. "Post-inc expansion is not supported in CanonicalMode");
  269. PostIncLoops = L;
  270. }
  271. /// Disable all post-inc expansion.
  272. void clearPostInc() {
  273. PostIncLoops.clear();
  274. // When we change the post-inc loop set, cached expansions may no
  275. // longer be valid.
  276. InsertedPostIncValues.clear();
  277. }
  278. /// Disable the behavior of expanding expressions in canonical form rather
  279. /// than in a more literal form. Non-canonical mode is useful for late
  280. /// optimization passes.
  281. void disableCanonicalMode() { CanonicalMode = false; }
  282. void enableLSRMode() { LSRMode = true; }
  283. /// Set the current insertion point. This is useful if multiple calls to
  284. /// expandCodeFor() are going to be made with the same insert point and the
  285. /// insert point may be moved during one of the expansions (e.g. if the
  286. /// insert point is not a block terminator).
  287. void setInsertPoint(Instruction *IP) {
  288. assert(IP);
  289. Builder.SetInsertPoint(IP);
  290. }
  291. /// Clear the current insertion point. This is useful if the instruction
  292. /// that had been serving as the insertion point may have been deleted.
  293. void clearInsertPoint() { Builder.ClearInsertionPoint(); }
  294. /// Set location information used by debugging information.
  295. void SetCurrentDebugLocation(DebugLoc L) {
  296. Builder.SetCurrentDebugLocation(std::move(L));
  297. }
  298. /// Get location information used by debugging information.
  299. DebugLoc getCurrentDebugLocation() const {
  300. return Builder.getCurrentDebugLocation();
  301. }
  302. /// Return true if the specified instruction was inserted by the code
  303. /// rewriter. If so, the client should not modify the instruction. Note that
  304. /// this also includes instructions re-used during expansion.
  305. bool isInsertedInstruction(Instruction *I) const {
  306. return InsertedValues.count(I) || InsertedPostIncValues.count(I);
  307. }
  308. void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); }
  309. /// Try to find the ValueOffsetPair for S. The function is mainly used to
  310. /// check whether S can be expanded cheaply. If this returns a non-None
  311. /// value, we know we can codegen the `ValueOffsetPair` into a suitable
  312. /// expansion identical with S so that S can be expanded cheaply.
  313. ///
  314. /// L is a hint which tells in which loop to look for the suitable value.
  315. /// On success return value which is equivalent to the expanded S at point
  316. /// At. Return nullptr if value was not found.
  317. ///
  318. /// Note that this function does not perform an exhaustive search. I.e if it
  319. /// didn't find any value it does not mean that there is no such value.
  320. ///
  321. Optional<ScalarEvolution::ValueOffsetPair>
  322. getRelatedExistingExpansion(const SCEV *S, const Instruction *At, Loop *L);
  323. /// Returns a suitable insert point after \p I, that dominates \p
  324. /// MustDominate. Skips instructions inserted by the expander.
  325. BasicBlock::iterator findInsertPointAfter(Instruction *I,
  326. Instruction *MustDominate);
  327. private:
  328. LLVMContext &getContext() const { return SE.getContext(); }
  329. /// Insert code to directly compute the specified SCEV expression into the
  330. /// program. The code is inserted into the SCEVExpander's current
  331. /// insertion point. If a type is specified, the result will be expanded to
  332. /// have that type, with a cast if necessary. If \p Root is true, this
  333. /// indicates that \p SH is the top-level expression to expand passed from
  334. /// an external client call.
  335. Value *expandCodeForImpl(const SCEV *SH, Type *Ty, bool Root);
  336. /// Insert code to directly compute the specified SCEV expression into the
  337. /// program. The code is inserted into the specified block. If \p
  338. /// Root is true, this indicates that \p SH is the top-level expression to
  339. /// expand passed from an external client call.
  340. Value *expandCodeForImpl(const SCEV *SH, Type *Ty, Instruction *I, bool Root);
  341. /// Recursive helper function for isHighCostExpansion.
  342. bool isHighCostExpansionHelper(
  343. const SCEVOperand &WorkItem, Loop *L, const Instruction &At,
  344. int &BudgetRemaining, const TargetTransformInfo &TTI,
  345. SmallPtrSetImpl<const SCEV *> &Processed,
  346. SmallVectorImpl<SCEVOperand> &Worklist);
  347. /// Insert the specified binary operator, doing a small amount of work to
  348. /// avoid inserting an obviously redundant operation, and hoisting to an
  349. /// outer loop when the opportunity is there and it is safe.
  350. Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS,
  351. SCEV::NoWrapFlags Flags, bool IsSafeToHoist);
  352. /// Arrange for there to be a cast of V to Ty at IP, reusing an existing
  353. /// cast if a suitable one exists, moving an existing cast if a suitable one
  354. /// exists but isn't in the right place, or creating a new one.
  355. Value *ReuseOrCreateCast(Value *V, Type *Ty, Instruction::CastOps Op,
  356. BasicBlock::iterator IP);
  357. /// Insert a cast of V to the specified type, which must be possible with a
  358. /// noop cast, doing what we can to share the casts.
  359. Value *InsertNoopCastOfTo(Value *V, Type *Ty);
  360. /// Expand a SCEVAddExpr with a pointer type into a GEP instead of using
  361. /// ptrtoint+arithmetic+inttoptr.
  362. Value *expandAddToGEP(const SCEV *const *op_begin, const SCEV *const *op_end,
  363. PointerType *PTy, Type *Ty, Value *V);
  364. Value *expandAddToGEP(const SCEV *Op, PointerType *PTy, Type *Ty, Value *V);
  365. /// Find a previous Value in ExprValueMap for expand.
  366. ScalarEvolution::ValueOffsetPair
  367. FindValueInExprValueMap(const SCEV *S, const Instruction *InsertPt);
  368. Value *expand(const SCEV *S);
  369. /// Determine the most "relevant" loop for the given SCEV.
  370. const Loop *getRelevantLoop(const SCEV *);
  371. Value *visitConstant(const SCEVConstant *S) { return S->getValue(); }
  372. Value *visitPtrToIntExpr(const SCEVPtrToIntExpr *S);
  373. Value *visitTruncateExpr(const SCEVTruncateExpr *S);
  374. Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
  375. Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
  376. Value *visitAddExpr(const SCEVAddExpr *S);
  377. Value *visitMulExpr(const SCEVMulExpr *S);
  378. Value *visitUDivExpr(const SCEVUDivExpr *S);
  379. Value *visitAddRecExpr(const SCEVAddRecExpr *S);
  380. Value *visitSMaxExpr(const SCEVSMaxExpr *S);
  381. Value *visitUMaxExpr(const SCEVUMaxExpr *S);
  382. Value *visitSMinExpr(const SCEVSMinExpr *S);
  383. Value *visitUMinExpr(const SCEVUMinExpr *S);
  384. Value *visitUnknown(const SCEVUnknown *S) { return S->getValue(); }
  385. void rememberInstruction(Value *I);
  386. bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
  387. bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
  388. Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
  389. PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
  390. const Loop *L, Type *ExpandTy, Type *IntTy,
  391. Type *&TruncTy, bool &InvertStep);
  392. Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L, Type *ExpandTy,
  393. Type *IntTy, bool useSubtract);
  394. void hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist,
  395. Instruction *Pos, PHINode *LoopPhi);
  396. void fixupInsertPoints(Instruction *I);
  397. /// If required, create LCSSA PHIs for \p Users' operand \p OpIdx. If new
  398. /// LCSSA PHIs have been created, return the LCSSA PHI available at \p User.
  399. /// If no PHIs have been created, return the unchanged operand \p OpIdx.
  400. Value *fixupLCSSAFormFor(Instruction *User, unsigned OpIdx);
  401. };
  402. /// Helper to remove instructions inserted during SCEV expansion, unless they
  403. /// are marked as used.
  404. class SCEVExpanderCleaner {
  405. SCEVExpander &Expander;
  406. DominatorTree &DT;
  407. /// Indicates whether the result of the expansion is used. If false, the
  408. /// instructions added during expansion are removed.
  409. bool ResultUsed;
  410. public:
  411. SCEVExpanderCleaner(SCEVExpander &Expander, DominatorTree &DT)
  412. : Expander(Expander), DT(DT), ResultUsed(false) {}
  413. ~SCEVExpanderCleaner();
  414. /// Indicate that the result of the expansion is used.
  415. void markResultUsed() { ResultUsed = true; }
  416. };
  417. } // namespace llvm
  418. #endif
  419. #ifdef __GNUC__
  420. #pragma GCC diagnostic pop
  421. #endif