1//===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the classes used to generate code from scalar expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
15#define LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
16
17#include "llvm/Analysis/ScalarEvolutionExpressions.h"
18#include "llvm/Analysis/ScalarEvolutionNormalization.h"
19#include "llvm/Analysis/TargetFolder.h"
20#include "llvm/IR/IRBuilder.h"
21#include "llvm/IR/ValueHandle.h"
22#include <set>
23
24namespace llvm {
25  class TargetTransformInfo;
26
27  /// Return true if the given expression is safe to expand in the sense that
28  /// all materialized values are safe to speculate.
29  bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE);
30
31  /// This class uses information about analyze scalars to
32  /// rewrite expressions in canonical form.
33  ///
34  /// Clients should create an instance of this class when rewriting is needed,
35  /// and destroy it when finished to allow the release of the associated
36  /// memory.
37  class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
38    ScalarEvolution &SE;
39    const DataLayout &DL;
40
41    // New instructions receive a name to identifies them with the current pass.
42    const char* IVName;
43
44    // InsertedExpressions caches Values for reuse, so must track RAUW.
45    std::map<std::pair<const SCEV *, Instruction *>, TrackingVH<Value> >
46      InsertedExpressions;
47    // InsertedValues only flags inserted instructions so needs no RAUW.
48    std::set<AssertingVH<Value> > InsertedValues;
49    std::set<AssertingVH<Value> > InsertedPostIncValues;
50
51    /// A memoization of the "relevant" loop for a given SCEV.
52    DenseMap<const SCEV *, const Loop *> RelevantLoops;
53
54    /// \brief Addrecs referring to any of the given loops are expanded
55    /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode
56    /// returns the add instruction that adds one to the phi for {0,+,1}<L>,
57    /// as opposed to a new phi starting at 1. This is only supported in
58    /// non-canonical mode.
59    PostIncLoopSet PostIncLoops;
60
61    /// \brief When this is non-null, addrecs expanded in the loop it indicates
62    /// should be inserted with increments at IVIncInsertPos.
63    const Loop *IVIncInsertLoop;
64
65    /// \brief When expanding addrecs in the IVIncInsertLoop loop, insert the IV
66    /// increment at this position.
67    Instruction *IVIncInsertPos;
68
69    /// \brief Phis that complete an IV chain. Reuse
70    std::set<AssertingVH<PHINode> > ChainedPhis;
71
72    /// \brief When true, expressions are expanded in "canonical" form. In
73    /// particular, addrecs are expanded as arithmetic based on a canonical
74    /// induction variable. When false, expression are expanded in a more
75    /// literal form.
76    bool CanonicalMode;
77
78    /// \brief When invoked from LSR, the expander is in "strength reduction"
79    /// mode. The only difference is that phi's are only reused if they are
80    /// already in "expanded" form.
81    bool LSRMode;
82
83    typedef IRBuilder<TargetFolder> BuilderType;
84    BuilderType Builder;
85
86    // RAII object that stores the current insertion point and restores it when
87    // the object is destroyed. This includes the debug location.  Duplicated
88    // from InsertPointGuard to add SetInsertPoint() which is used to updated
89    // InsertPointGuards stack when insert points are moved during SCEV
90    // expansion.
91    class SCEVInsertPointGuard {
92      IRBuilderBase &Builder;
93      AssertingVH<BasicBlock> Block;
94      BasicBlock::iterator Point;
95      DebugLoc DbgLoc;
96      SCEVExpander *SE;
97
98      SCEVInsertPointGuard(const SCEVInsertPointGuard &) = delete;
99      SCEVInsertPointGuard &operator=(const SCEVInsertPointGuard &) = delete;
100
101    public:
102      SCEVInsertPointGuard(IRBuilderBase &B, SCEVExpander *SE)
103          : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
104            DbgLoc(B.getCurrentDebugLocation()), SE(SE) {
105        SE->InsertPointGuards.push_back(this);
106      }
107
108      ~SCEVInsertPointGuard() {
109        // These guards should always created/destroyed in FIFO order since they
110        // are used to guard lexically scoped blocks of code in
111        // ScalarEvolutionExpander.
112        assert(SE->InsertPointGuards.back() == this);
113        SE->InsertPointGuards.pop_back();
114        Builder.restoreIP(IRBuilderBase::InsertPoint(Block, Point));
115        Builder.SetCurrentDebugLocation(DbgLoc);
116      }
117
118      BasicBlock::iterator GetInsertPoint() const { return Point; }
119      void SetInsertPoint(BasicBlock::iterator I) { Point = I; }
120    };
121
122    /// Stack of pointers to saved insert points, used to keep insert points
123    /// consistent when instructions are moved.
124    SmallVector<SCEVInsertPointGuard *, 8> InsertPointGuards;
125
126#ifndef NDEBUG
127    const char *DebugType;
128#endif
129
130    friend struct SCEVVisitor<SCEVExpander, Value*>;
131
132  public:
133    /// \brief Construct a SCEVExpander in "canonical" mode.
134    explicit SCEVExpander(ScalarEvolution &se, const DataLayout &DL,
135                          const char *name)
136        : SE(se), DL(DL), IVName(name), IVIncInsertLoop(nullptr),
137          IVIncInsertPos(nullptr), CanonicalMode(true), LSRMode(false),
138          Builder(se.getContext(), TargetFolder(DL)) {
139#ifndef NDEBUG
140      DebugType = "";
141#endif
142    }
143
144    ~SCEVExpander() {
145      // Make sure the insert point guard stack is consistent.
146      assert(InsertPointGuards.empty());
147    }
148
149#ifndef NDEBUG
150    void setDebugType(const char* s) { DebugType = s; }
151#endif
152
153    /// \brief Erase the contents of the InsertedExpressions map so that users
154    /// trying to expand the same expression into multiple BasicBlocks or
155    /// different places within the same BasicBlock can do so.
156    void clear() {
157      InsertedExpressions.clear();
158      InsertedValues.clear();
159      InsertedPostIncValues.clear();
160      ChainedPhis.clear();
161    }
162
163    /// \brief Return true for expressions that may incur non-trivial cost to
164    /// evaluate at runtime.
165    ///
166    /// At is an optional parameter which specifies point in code where user is
167    /// going to expand this expression. Sometimes this knowledge can lead to a
168    /// more accurate cost estimation.
169    bool isHighCostExpansion(const SCEV *Expr, Loop *L,
170                             const Instruction *At = nullptr) {
171      SmallPtrSet<const SCEV *, 8> Processed;
172      return isHighCostExpansionHelper(Expr, L, At, Processed);
173    }
174
175    /// \brief This method returns the canonical induction variable of the
176    /// specified type for the specified loop (inserting one if there is none).
177    /// A canonical induction variable starts at zero and steps by one on each
178    /// iteration.
179    PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty);
180
181    /// \brief Return the induction variable increment's IV operand.
182    Instruction *getIVIncOperand(Instruction *IncV, Instruction *InsertPos,
183                                 bool allowScale);
184
185    /// \brief Utility for hoisting an IV increment.
186    bool hoistIVInc(Instruction *IncV, Instruction *InsertPos);
187
188    /// \brief replace congruent phis with their most canonical
189    /// representative. Return the number of phis eliminated.
190    unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
191                                 SmallVectorImpl<WeakVH> &DeadInsts,
192                                 const TargetTransformInfo *TTI = nullptr);
193
194    /// \brief Insert code to directly compute the specified SCEV expression
195    /// into the program.  The inserted code is inserted into the specified
196    /// block.
197    Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I);
198
199    /// \brief Generates a code sequence that evaluates this predicate.
200    /// The inserted instructions will be at position \p Loc.
201    /// The result will be of type i1 and will have a value of 0 when the
202    /// predicate is false and 1 otherwise.
203    Value *expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc);
204
205    /// \brief A specialized variant of expandCodeForPredicate, handling the
206    /// case when we are expanding code for a SCEVEqualPredicate.
207    Value *expandEqualPredicate(const SCEVEqualPredicate *Pred,
208                                Instruction *Loc);
209
210    /// \brief Generates code that evaluates if the \p AR expression will
211    /// overflow.
212    Value *generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc,
213                                 bool Signed);
214
215    /// \brief A specialized variant of expandCodeForPredicate, handling the
216    /// case when we are expanding code for a SCEVWrapPredicate.
217    Value *expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc);
218
219    /// \brief A specialized variant of expandCodeForPredicate, handling the
220    /// case when we are expanding code for a SCEVUnionPredicate.
221    Value *expandUnionPredicate(const SCEVUnionPredicate *Pred,
222                                Instruction *Loc);
223
224    /// \brief Set the current IV increment loop and position.
225    void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
226      assert(!CanonicalMode &&
227             "IV increment positions are not supported in CanonicalMode");
228      IVIncInsertLoop = L;
229      IVIncInsertPos = Pos;
230    }
231
232    /// \brief Enable post-inc expansion for addrecs referring to the given
233    /// loops. Post-inc expansion is only supported in non-canonical mode.
234    void setPostInc(const PostIncLoopSet &L) {
235      assert(!CanonicalMode &&
236             "Post-inc expansion is not supported in CanonicalMode");
237      PostIncLoops = L;
238    }
239
240    /// \brief Disable all post-inc expansion.
241    void clearPostInc() {
242      PostIncLoops.clear();
243
244      // When we change the post-inc loop set, cached expansions may no
245      // longer be valid.
246      InsertedPostIncValues.clear();
247    }
248
249    /// \brief Disable the behavior of expanding expressions in canonical form
250    /// rather than in a more literal form. Non-canonical mode is useful for
251    /// late optimization passes.
252    void disableCanonicalMode() { CanonicalMode = false; }
253
254    void enableLSRMode() { LSRMode = true; }
255
256    /// \brief Clear the current insertion point. This is useful if the
257    /// instruction that had been serving as the insertion point may have been
258    /// deleted.
259    void clearInsertPoint() {
260      Builder.ClearInsertionPoint();
261    }
262
263    /// \brief Return true if the specified instruction was inserted by the code
264    /// rewriter.  If so, the client should not modify the instruction.
265    bool isInsertedInstruction(Instruction *I) const {
266      return InsertedValues.count(I) || InsertedPostIncValues.count(I);
267    }
268
269    void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); }
270
271    /// \brief Try to find LLVM IR value for S available at the point At.
272    ///
273    /// L is a hint which tells in which loop to look for the suitable value.
274    /// On success return value which is equivalent to the expanded S at point
275    /// At. Return nullptr if value was not found.
276    ///
277    /// Note that this function does not perform an exhaustive search. I.e if it
278    /// didn't find any value it does not mean that there is no such value.
279    Value *findExistingExpansion(const SCEV *S, const Instruction *At, Loop *L);
280
281  private:
282    LLVMContext &getContext() const { return SE.getContext(); }
283
284    /// \brief Recursive helper function for isHighCostExpansion.
285    bool isHighCostExpansionHelper(const SCEV *S, Loop *L,
286                                   const Instruction *At,
287                                   SmallPtrSetImpl<const SCEV *> &Processed);
288
289    /// \brief Insert the specified binary operator, doing a small amount
290    /// of work to avoid inserting an obviously redundant operation.
291    Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
292
293    /// \brief Arrange for there to be a cast of V to Ty at IP, reusing an
294    /// existing cast if a suitable one exists, moving an existing cast if a
295    /// suitable one exists but isn't in the right place, or or creating a new
296    /// one.
297    Value *ReuseOrCreateCast(Value *V, Type *Ty,
298                             Instruction::CastOps Op,
299                             BasicBlock::iterator IP);
300
301    /// \brief Insert a cast of V to the specified type, which must be possible
302    /// with a noop cast, doing what we can to share the casts.
303    Value *InsertNoopCastOfTo(Value *V, Type *Ty);
304
305    /// \brief Expand a SCEVAddExpr with a pointer type into a GEP
306    /// instead of using ptrtoint+arithmetic+inttoptr.
307    Value *expandAddToGEP(const SCEV *const *op_begin,
308                          const SCEV *const *op_end,
309                          PointerType *PTy, Type *Ty, Value *V);
310
311    /// \brief Find a previous Value in ExprValueMap for expand.
312    Value *FindValueInExprValueMap(const SCEV *S, const Instruction *InsertPt);
313
314    Value *expand(const SCEV *S);
315
316    /// \brief Insert code to directly compute the specified SCEV expression
317    /// into the program.  The inserted code is inserted into the SCEVExpander's
318    /// current insertion point. If a type is specified, the result will be
319    /// expanded to have that type, with a cast if necessary.
320    Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr);
321
322    /// \brief Determine the most "relevant" loop for the given SCEV.
323    const Loop *getRelevantLoop(const SCEV *);
324
325    Value *visitConstant(const SCEVConstant *S) {
326      return S->getValue();
327    }
328
329    Value *visitTruncateExpr(const SCEVTruncateExpr *S);
330
331    Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
332
333    Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
334
335    Value *visitAddExpr(const SCEVAddExpr *S);
336
337    Value *visitMulExpr(const SCEVMulExpr *S);
338
339    Value *visitUDivExpr(const SCEVUDivExpr *S);
340
341    Value *visitAddRecExpr(const SCEVAddRecExpr *S);
342
343    Value *visitSMaxExpr(const SCEVSMaxExpr *S);
344
345    Value *visitUMaxExpr(const SCEVUMaxExpr *S);
346
347    Value *visitUnknown(const SCEVUnknown *S) {
348      return S->getValue();
349    }
350
351    void rememberInstruction(Value *I);
352
353    bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
354
355    bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
356
357    Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
358    PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
359                                       const Loop *L,
360                                       Type *ExpandTy,
361                                       Type *IntTy,
362                                       Type *&TruncTy,
363                                       bool &InvertStep);
364    Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
365                       Type *ExpandTy, Type *IntTy, bool useSubtract);
366
367    void hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist,
368                        Instruction *Pos, PHINode *LoopPhi);
369
370    void fixupInsertPoints(Instruction *I);
371  };
372}
373
374#endif
375