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