ScalarEvolutionExpander.h 21 KB

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