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