1 //===- llvm/Transforms/Utils/LoopUtils.h - Loop utilities -*- C++ -*-=========//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines some loop transformation utilities.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
15 #define LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/IRBuilder.h"
24 class AliasSetTracker;
25 class AssumptionCache;
32 class PredIteratorCache;
33 class ScalarEvolution;
34 class TargetLibraryInfo;
36 /// \brief Captures loop safety information.
37 /// It keep information for loop & its header may throw exception.
38 struct LICMSafetyInfo {
39 bool MayThrow; // The current loop contains an instruction which
41 bool HeaderMayThrow; // Same as previous, but specific to loop header
42 LICMSafetyInfo() : MayThrow(false), HeaderMayThrow(false)
46 /// This POD struct holds information about a potential reduction operation.
47 class ReductionInstDesc {
50 // This enum represents the kind of minmax reduction.
51 enum MinMaxReductionKind {
60 ReductionInstDesc(bool IsRedux, Instruction *I)
61 : IsReduction(IsRedux), PatternLastInst(I), MinMaxKind(MRK_Invalid) {}
63 ReductionInstDesc(Instruction *I, MinMaxReductionKind K)
64 : IsReduction(true), PatternLastInst(I), MinMaxKind(K) {}
66 bool isReduction() { return IsReduction; }
68 MinMaxReductionKind getMinMaxKind() { return MinMaxKind; }
71 // Is this instruction a reduction candidate.
73 // The last instruction in a min/max pattern (select of the select(icmp())
74 // pattern), or the current reduction instruction otherwise.
75 Instruction *PatternLastInst;
76 // If this is a min/max pattern the comparison predicate.
77 MinMaxReductionKind MinMaxKind;
80 /// This struct holds information about reduction variables.
81 class ReductionDescriptor {
84 /// This enum represents the kinds of reductions that we support.
86 RK_NoReduction, ///< Not a reduction.
87 RK_IntegerAdd, ///< Sum of integers.
88 RK_IntegerMult, ///< Product of integers.
89 RK_IntegerOr, ///< Bitwise or logical OR of numbers.
90 RK_IntegerAnd, ///< Bitwise or logical AND of numbers.
91 RK_IntegerXor, ///< Bitwise or logical XOR of numbers.
92 RK_IntegerMinMax, ///< Min/max implemented in terms of select(cmp()).
93 RK_FloatAdd, ///< Sum of floats.
94 RK_FloatMult, ///< Product of floats.
95 RK_FloatMinMax ///< Min/max implemented in terms of select(cmp()).
99 : StartValue(nullptr), LoopExitInstr(nullptr), Kind(RK_NoReduction),
100 MinMaxKind(ReductionInstDesc::MRK_Invalid) {}
102 ReductionDescriptor(Value *Start, Instruction *Exit, ReductionKind K,
103 ReductionInstDesc::MinMaxReductionKind MK)
104 : StartValue(Start), LoopExitInstr(Exit), Kind(K), MinMaxKind(MK) {}
106 /// Returns a struct describing if the instruction 'I' can be a reduction
107 /// variable of type 'Kind'. If the reduction is a min/max pattern of
108 /// select(icmp()) this function advances the instruction pointer 'I' from the
109 /// compare instruction to the select instruction and stores this pointer in
110 /// 'PatternLastInst' member of the returned struct.
111 static ReductionInstDesc isReductionInstr(Instruction *I, ReductionKind Kind,
112 ReductionInstDesc &Prev,
113 bool HasFunNoNaNAttr);
115 /// Returns true if instuction I has multiple uses in Insts
116 static bool hasMultipleUsesOf(Instruction *I,
117 SmallPtrSetImpl<Instruction *> &Insts);
119 /// Returns true if all uses of the instruction I is within the Set.
120 static bool areAllUsesIn(Instruction *I, SmallPtrSetImpl<Instruction *> &Set);
122 /// Returns a struct describing if the instruction if the instruction is a
123 /// Select(ICmp(X, Y), X, Y) instruction pattern corresponding to a min(X, Y)
125 static ReductionInstDesc isMinMaxSelectCmpPattern(Instruction *I,
126 ReductionInstDesc &Prev);
128 /// Returns identity corresponding to the ReductionKind.
129 static Constant *getReductionIdentity(ReductionKind K, Type *Tp);
131 /// Returns the opcode of binary operation corresponding to the ReductionKind.
132 static unsigned getReductionBinOp(ReductionKind Kind);
134 /// Returns a Min/Max operation corresponding to MinMaxReductionKind.
135 static Value *createMinMaxOp(IRBuilder<> &Builder,
136 ReductionInstDesc::MinMaxReductionKind RK,
137 Value *Left, Value *Right);
139 /// Returns true if Phi is a reduction of type Kind and adds it to the
140 /// ReductionDescriptor.
141 static bool AddReductionVar(PHINode *Phi, ReductionKind Kind, Loop *TheLoop,
142 bool HasFunNoNaNAttr,
143 ReductionDescriptor &RedDes);
145 /// Returns true if Phi is a reduction in TheLoop. The ReductionDescriptor is
146 /// returned in RedDes.
147 static bool isReductionPHI(PHINode *Phi, Loop *TheLoop,
148 ReductionDescriptor &RedDes);
150 ReductionKind getReductionKind() { return Kind; }
152 ReductionInstDesc::MinMaxReductionKind getMinMaxReductionKind() {
156 TrackingVH<Value> getReductionStartValue() { return StartValue; }
158 Instruction *getLoopExitInstr() { return LoopExitInstr; }
161 // The starting value of the reduction.
162 // It does not have to be zero!
163 TrackingVH<Value> StartValue;
164 // The instruction who's value is used outside the loop.
165 Instruction *LoopExitInstr;
166 // The kind of the reduction.
168 // If this a min/max reduction the kind of reduction.
169 ReductionInstDesc::MinMaxReductionKind MinMaxKind;
172 BasicBlock *InsertPreheaderForLoop(Loop *L, Pass *P);
174 /// \brief Simplify each loop in a loop nest recursively.
176 /// This takes a potentially un-simplified loop L (and its children) and turns
177 /// it into a simplified loop nest with preheaders and single backedges. It
178 /// will optionally update \c AliasAnalysis and \c ScalarEvolution analyses if
180 bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, Pass *PP,
181 AliasAnalysis *AA = nullptr, ScalarEvolution *SE = nullptr,
182 AssumptionCache *AC = nullptr);
184 /// \brief Put loop into LCSSA form.
186 /// Looks at all instructions in the loop which have uses outside of the
187 /// current loop. For each, an LCSSA PHI node is inserted and the uses outside
188 /// the loop are rewritten to use this node.
190 /// LoopInfo and DominatorTree are required and preserved.
192 /// If ScalarEvolution is passed in, it will be preserved.
194 /// Returns true if any modifications are made to the loop.
195 bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI,
196 ScalarEvolution *SE = nullptr);
198 /// \brief Put a loop nest into LCSSA form.
200 /// This recursively forms LCSSA for a loop nest.
202 /// LoopInfo and DominatorTree are required and preserved.
204 /// If ScalarEvolution is passed in, it will be preserved.
206 /// Returns true if any modifications are made to the loop.
207 bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI,
208 ScalarEvolution *SE = nullptr);
210 /// \brief Walk the specified region of the CFG (defined by all blocks
211 /// dominated by the specified block, and that are in the current loop) in
212 /// reverse depth first order w.r.t the DominatorTree. This allows us to visit
213 /// uses before definitions, allowing us to sink a loop body in one pass without
214 /// iteration. Takes DomTreeNode, AliasAnalysis, LoopInfo, DominatorTree,
215 /// DataLayout, TargetLibraryInfo, Loop, AliasSet information for all
216 /// instructions of the loop and loop safety information as arguments.
217 /// It returns changed status.
218 bool sinkRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *,
219 TargetLibraryInfo *, Loop *, AliasSetTracker *,
222 /// \brief Walk the specified region of the CFG (defined by all blocks
223 /// dominated by the specified block, and that are in the current loop) in depth
224 /// first order w.r.t the DominatorTree. This allows us to visit definitions
225 /// before uses, allowing us to hoist a loop body in one pass without iteration.
226 /// Takes DomTreeNode, AliasAnalysis, LoopInfo, DominatorTree, DataLayout,
227 /// TargetLibraryInfo, Loop, AliasSet information for all instructions of the
228 /// loop and loop safety information as arguments. It returns changed status.
229 bool hoistRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *,
230 TargetLibraryInfo *, Loop *, AliasSetTracker *,
233 /// \brief Try to promote memory values to scalars by sinking stores out of
234 /// the loop and moving loads to before the loop. We do this by looping over
235 /// the stores in the loop, looking for stores to Must pointers which are
236 /// loop invariant. It takes AliasSet, Loop exit blocks vector, loop exit blocks
237 /// insertion point vector, PredIteratorCache, LoopInfo, DominatorTree, Loop,
238 /// AliasSet information for all instructions of the loop and loop safety
239 /// information as arguments. It returns changed status.
240 bool promoteLoopAccessesToScalars(AliasSet &, SmallVectorImpl<BasicBlock*> &,
241 SmallVectorImpl<Instruction*> &,
242 PredIteratorCache &, LoopInfo *,
243 DominatorTree *, Loop *, AliasSetTracker *,
246 /// \brief Computes safety information for a loop
247 /// checks loop body & header for the possiblity of may throw
248 /// exception, it takes LICMSafetyInfo and loop as argument.
249 /// Updates safety information in LICMSafetyInfo argument.
250 void computeLICMSafetyInfo(LICMSafetyInfo *, Loop *);
252 /// \brief Checks if the given PHINode in a loop header is an induction
253 /// variable. Returns true if this is an induction PHI along with the step
255 bool isInductionPHI(PHINode *, ScalarEvolution *, ConstantInt *&);