xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h (revision d409305fa3838fb39b38c26fc085fb729b8766d5)
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 {
45   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:
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 
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 
153     BasicBlock::iterator GetInsertPoint() const { return Point; }
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   /// Return the induction variable increment's IV operand.
251   Instruction *getIVIncOperand(Instruction *IncV, Instruction *InsertPos,
252                                bool allowScale);
253 
254   /// Utility for hoisting an IV increment.
255   bool hoistIVInc(Instruction *IncV, Instruction *InsertPos);
256 
257   /// replace congruent phis with their most canonical representative. Return
258   /// the number of phis eliminated.
259   unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
260                                SmallVectorImpl<WeakTrackingVH> &DeadInsts,
261                                const TargetTransformInfo *TTI = nullptr);
262 
263   /// Insert code to directly compute the specified SCEV expression into the
264   /// program.  The code is inserted into the specified block.
265   Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I) {
266     return expandCodeForImpl(SH, Ty, I, true);
267   }
268 
269   /// Insert code to directly compute the specified SCEV expression into the
270   /// program.  The code is inserted into the SCEVExpander's current
271   /// insertion point. If a type is specified, the result will be expanded to
272   /// have that type, with a cast if necessary.
273   Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr) {
274     return expandCodeForImpl(SH, Ty, true);
275   }
276 
277   /// Generates a code sequence that evaluates this predicate.  The inserted
278   /// instructions will be at position \p Loc.  The result will be of type i1
279   /// and will have a value of 0 when the predicate is false and 1 otherwise.
280   Value *expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc);
281 
282   /// A specialized variant of expandCodeForPredicate, handling the case when
283   /// we are expanding code for a SCEVEqualPredicate.
284   Value *expandEqualPredicate(const SCEVEqualPredicate *Pred, Instruction *Loc);
285 
286   /// Generates code that evaluates if the \p AR expression will overflow.
287   Value *generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc,
288                                bool Signed);
289 
290   /// A specialized variant of expandCodeForPredicate, handling the case when
291   /// we are expanding code for a SCEVWrapPredicate.
292   Value *expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc);
293 
294   /// A specialized variant of expandCodeForPredicate, handling the case when
295   /// we are expanding code for a SCEVUnionPredicate.
296   Value *expandUnionPredicate(const SCEVUnionPredicate *Pred, Instruction *Loc);
297 
298   /// Set the current IV increment loop and position.
299   void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
300     assert(!CanonicalMode &&
301            "IV increment positions are not supported in CanonicalMode");
302     IVIncInsertLoop = L;
303     IVIncInsertPos = Pos;
304   }
305 
306   /// Enable post-inc expansion for addrecs referring to the given
307   /// loops. Post-inc expansion is only supported in non-canonical mode.
308   void setPostInc(const PostIncLoopSet &L) {
309     assert(!CanonicalMode &&
310            "Post-inc expansion is not supported in CanonicalMode");
311     PostIncLoops = L;
312   }
313 
314   /// Disable all post-inc expansion.
315   void clearPostInc() {
316     PostIncLoops.clear();
317 
318     // When we change the post-inc loop set, cached expansions may no
319     // longer be valid.
320     InsertedPostIncValues.clear();
321   }
322 
323   /// Disable the behavior of expanding expressions in canonical form rather
324   /// than in a more literal form. Non-canonical mode is useful for late
325   /// optimization passes.
326   void disableCanonicalMode() { CanonicalMode = false; }
327 
328   void enableLSRMode() { LSRMode = true; }
329 
330   /// Set the current insertion point. This is useful if multiple calls to
331   /// expandCodeFor() are going to be made with the same insert point and the
332   /// insert point may be moved during one of the expansions (e.g. if the
333   /// insert point is not a block terminator).
334   void setInsertPoint(Instruction *IP) {
335     assert(IP);
336     Builder.SetInsertPoint(IP);
337   }
338 
339   /// Clear the current insertion point. This is useful if the instruction
340   /// that had been serving as the insertion point may have been deleted.
341   void clearInsertPoint() { Builder.ClearInsertionPoint(); }
342 
343   /// Set location information used by debugging information.
344   void SetCurrentDebugLocation(DebugLoc L) {
345     Builder.SetCurrentDebugLocation(std::move(L));
346   }
347 
348   /// Get location information used by debugging information.
349   DebugLoc getCurrentDebugLocation() const {
350     return Builder.getCurrentDebugLocation();
351   }
352 
353   /// Return true if the specified instruction was inserted by the code
354   /// rewriter.  If so, the client should not modify the instruction. Note that
355   /// this also includes instructions re-used during expansion.
356   bool isInsertedInstruction(Instruction *I) const {
357     return InsertedValues.count(I) || InsertedPostIncValues.count(I);
358   }
359 
360   void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); }
361 
362   /// Try to find the ValueOffsetPair for S. The function is mainly used to
363   /// check whether S can be expanded cheaply.  If this returns a non-None
364   /// value, we know we can codegen the `ValueOffsetPair` into a suitable
365   /// expansion identical with S so that S can be expanded cheaply.
366   ///
367   /// L is a hint which tells in which loop to look for the suitable value.
368   /// On success return value which is equivalent to the expanded S at point
369   /// At. Return nullptr if value was not found.
370   ///
371   /// Note that this function does not perform an exhaustive search. I.e if it
372   /// didn't find any value it does not mean that there is no such value.
373   ///
374   Optional<ScalarEvolution::ValueOffsetPair>
375   getRelatedExistingExpansion(const SCEV *S, const Instruction *At, Loop *L);
376 
377   /// Returns a suitable insert point after \p I, that dominates \p
378   /// MustDominate. Skips instructions inserted by the expander.
379   BasicBlock::iterator findInsertPointAfter(Instruction *I,
380                                             Instruction *MustDominate);
381 
382 private:
383   LLVMContext &getContext() const { return SE.getContext(); }
384 
385   /// Insert code to directly compute the specified SCEV expression into the
386   /// program. The code is inserted into the SCEVExpander's current
387   /// insertion point. If a type is specified, the result will be expanded to
388   /// have that type, with a cast if necessary. If \p Root is true, this
389   /// indicates that \p SH is the top-level expression to expand passed from
390   /// an external client call.
391   Value *expandCodeForImpl(const SCEV *SH, Type *Ty, bool Root);
392 
393   /// Insert code to directly compute the specified SCEV expression into the
394   /// program. The code is inserted into the specified block. If \p
395   /// Root is true, this indicates that \p SH is the top-level expression to
396   /// expand passed from an external client call.
397   Value *expandCodeForImpl(const SCEV *SH, Type *Ty, Instruction *I, bool Root);
398 
399   /// Recursive helper function for isHighCostExpansion.
400   bool isHighCostExpansionHelper(
401     const SCEVOperand &WorkItem, Loop *L, const Instruction &At,
402     int &BudgetRemaining, const TargetTransformInfo &TTI,
403     SmallPtrSetImpl<const SCEV *> &Processed,
404     SmallVectorImpl<SCEVOperand> &Worklist);
405 
406   /// Insert the specified binary operator, doing a small amount of work to
407   /// avoid inserting an obviously redundant operation, and hoisting to an
408   /// outer loop when the opportunity is there and it is safe.
409   Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS,
410                      SCEV::NoWrapFlags Flags, bool IsSafeToHoist);
411 
412   /// Arrange for there to be a cast of V to Ty at IP, reusing an existing
413   /// cast if a suitable one exists, moving an existing cast if a suitable one
414   /// exists but isn't in the right place, or creating a new one.
415   Value *ReuseOrCreateCast(Value *V, Type *Ty, Instruction::CastOps Op,
416                            BasicBlock::iterator IP);
417 
418   /// Insert a cast of V to the specified type, which must be possible with a
419   /// noop cast, doing what we can to share the casts.
420   Value *InsertNoopCastOfTo(Value *V, Type *Ty);
421 
422   /// Expand a SCEVAddExpr with a pointer type into a GEP instead of using
423   /// ptrtoint+arithmetic+inttoptr.
424   Value *expandAddToGEP(const SCEV *const *op_begin, const SCEV *const *op_end,
425                         PointerType *PTy, Type *Ty, Value *V);
426   Value *expandAddToGEP(const SCEV *Op, PointerType *PTy, Type *Ty, Value *V);
427 
428   /// Find a previous Value in ExprValueMap for expand.
429   ScalarEvolution::ValueOffsetPair
430   FindValueInExprValueMap(const SCEV *S, const Instruction *InsertPt);
431 
432   Value *expand(const SCEV *S);
433 
434   /// Determine the most "relevant" loop for the given SCEV.
435   const Loop *getRelevantLoop(const SCEV *);
436 
437   Value *visitConstant(const SCEVConstant *S) { return S->getValue(); }
438 
439   Value *visitPtrToIntExpr(const SCEVPtrToIntExpr *S);
440 
441   Value *visitTruncateExpr(const SCEVTruncateExpr *S);
442 
443   Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
444 
445   Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
446 
447   Value *visitAddExpr(const SCEVAddExpr *S);
448 
449   Value *visitMulExpr(const SCEVMulExpr *S);
450 
451   Value *visitUDivExpr(const SCEVUDivExpr *S);
452 
453   Value *visitAddRecExpr(const SCEVAddRecExpr *S);
454 
455   Value *visitSMaxExpr(const SCEVSMaxExpr *S);
456 
457   Value *visitUMaxExpr(const SCEVUMaxExpr *S);
458 
459   Value *visitSMinExpr(const SCEVSMinExpr *S);
460 
461   Value *visitUMinExpr(const SCEVUMinExpr *S);
462 
463   Value *visitUnknown(const SCEVUnknown *S) { return S->getValue(); }
464 
465   void rememberInstruction(Value *I);
466 
467   bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
468 
469   bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
470 
471   Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
472   PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
473                                      const Loop *L, Type *ExpandTy, Type *IntTy,
474                                      Type *&TruncTy, bool &InvertStep);
475   Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L, Type *ExpandTy,
476                      Type *IntTy, bool useSubtract);
477 
478   void hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist,
479                       Instruction *Pos, PHINode *LoopPhi);
480 
481   void fixupInsertPoints(Instruction *I);
482 
483   /// If required, create LCSSA PHIs for \p Users' operand \p OpIdx. If new
484   /// LCSSA PHIs have been created, return the LCSSA PHI available at \p User.
485   /// If no PHIs have been created, return the unchanged operand \p OpIdx.
486   Value *fixupLCSSAFormFor(Instruction *User, unsigned OpIdx);
487 };
488 
489 /// Helper to remove instructions inserted during SCEV expansion, unless they
490 /// are marked as used.
491 class SCEVExpanderCleaner {
492   SCEVExpander &Expander;
493 
494   DominatorTree &DT;
495 
496   /// Indicates whether the result of the expansion is used. If false, the
497   /// instructions added during expansion are removed.
498   bool ResultUsed;
499 
500 public:
501   SCEVExpanderCleaner(SCEVExpander &Expander, DominatorTree &DT)
502       : Expander(Expander), DT(DT), ResultUsed(false) {}
503 
504   ~SCEVExpanderCleaner();
505 
506   /// Indicate that the result of the expansion is used.
507   void markResultUsed() { ResultUsed = true; }
508 };
509 } // namespace llvm
510 
511 #endif
512