909c47bd4c80332010cba8380c5264bb0e9cd360
[oota-llvm.git] / include / llvm / Analysis / LoopAccessAnalysis.h
1 //===- llvm/Analysis/LoopAccessAnalysis.h -----------------------*- 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 interface for the loop memory dependence framework that
11 // was originally developed for the Loop Vectorizer.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
16 #define LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
17
18 #include "llvm/ADT/EquivalenceClasses.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/AliasSetTracker.h"
22 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
23 #include "llvm/IR/ValueHandle.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 namespace llvm {
27
28 class Value;
29 class DataLayout;
30 class AliasAnalysis;
31 class ScalarEvolution;
32 class Loop;
33 class SCEV;
34
35 /// Optimization analysis message produced during vectorization. Messages inform
36 /// the user why vectorization did not occur.
37 class VectorizationReport {
38   std::string Message;
39   raw_string_ostream Out;
40   Instruction *Instr;
41
42 public:
43   VectorizationReport(Instruction *I = nullptr) : Out(Message), Instr(I) {
44     Out << "loop not vectorized: ";
45   }
46
47   template <typename A> VectorizationReport &operator<<(const A &Value) {
48     Out << Value;
49     return *this;
50   }
51
52   Instruction *getInstr() { return Instr; }
53
54   std::string &str() { return Out.str(); }
55   operator Twine() { return Out.str(); }
56
57   /// \brief Emit an analysis note with the debug location from the instruction
58   /// in \p Message if available.  Otherwise use the location of \p TheLoop.
59   static void emitAnalysis(VectorizationReport &Message,
60                            const Function *TheFunction,
61                            const Loop *TheLoop);
62 };
63
64 /// \brief Drive the analysis of memory accesses in the loop
65 ///
66 /// This class is responsible for analyzing the memory accesses of a loop.  It
67 /// collects the accesses and then its main helper the AccessAnalysis class
68 /// finds and categorizes the dependences in buildDependenceSets.
69 ///
70 /// For memory dependences that can be analyzed at compile time, it determines
71 /// whether the dependence is part of cycle inhibiting vectorization.  This work
72 /// is delegated to the MemoryDepChecker class.
73 ///
74 /// For memory dependences that cannot be determined at compile time, it
75 /// generates run-time checks to prove independence.  This is done by
76 /// AccessAnalysis::canCheckPtrAtRT and the checks are maintained by the
77 /// RuntimePointerCheck class.
78 class LoopAccessAnalysis {
79 public:
80   /// \brief Collection of parameters used from the vectorizer.
81   struct VectorizerParams {
82     /// \brief Maximum simd width.
83     unsigned MaxVectorWidth;
84
85     /// \brief VF as overridden by the user.
86     unsigned VectorizationFactor;
87     /// \brief Interleave factor as overridden by the user.
88     unsigned VectorizationInterleave;
89
90     /// \\brief When performing memory disambiguation checks at runtime do not
91     /// make more than this number of comparisons.
92     unsigned RuntimeMemoryCheckThreshold;
93
94     VectorizerParams(unsigned MaxVectorWidth,
95                      unsigned VectorizationFactor,
96                      unsigned VectorizationInterleave,
97                      unsigned RuntimeMemoryCheckThreshold) :
98         MaxVectorWidth(MaxVectorWidth),
99         VectorizationFactor(VectorizationFactor),
100         VectorizationInterleave(VectorizationInterleave),
101         RuntimeMemoryCheckThreshold(RuntimeMemoryCheckThreshold) {}
102   };
103
104   /// This struct holds information about the memory runtime legality check that
105   /// a group of pointers do not overlap.
106   struct RuntimePointerCheck {
107     RuntimePointerCheck() : Need(false) {}
108
109     /// Reset the state of the pointer runtime information.
110     void reset() {
111       Need = false;
112       Pointers.clear();
113       Starts.clear();
114       Ends.clear();
115       IsWritePtr.clear();
116       DependencySetId.clear();
117       AliasSetId.clear();
118     }
119
120     /// Insert a pointer and calculate the start and end SCEVs.
121     void insert(ScalarEvolution *SE, Loop *Lp, Value *Ptr, bool WritePtr,
122                 unsigned DepSetId, unsigned ASId, ValueToValueMap &Strides);
123
124     /// This flag indicates if we need to add the runtime check.
125     bool Need;
126     /// Holds the pointers that we need to check.
127     SmallVector<TrackingVH<Value>, 2> Pointers;
128     /// Holds the pointer value at the beginning of the loop.
129     SmallVector<const SCEV*, 2> Starts;
130     /// Holds the pointer value at the end of the loop.
131     SmallVector<const SCEV*, 2> Ends;
132     /// Holds the information if this pointer is used for writing to memory.
133     SmallVector<bool, 2> IsWritePtr;
134     /// Holds the id of the set of pointers that could be dependent because of a
135     /// shared underlying object.
136     SmallVector<unsigned, 2> DependencySetId;
137     /// Holds the id of the disjoint alias set to which this pointer belongs.
138     SmallVector<unsigned, 2> AliasSetId;
139   };
140
141   LoopAccessAnalysis(Function *F, Loop *L, ScalarEvolution *SE,
142                      const DataLayout *DL, const TargetLibraryInfo *TLI,
143                      AliasAnalysis *AA, DominatorTree *DT,
144                      const VectorizerParams &VectParams) :
145       TheFunction(F), TheLoop(L), SE(SE), DL(DL), TLI(TLI), AA(AA), DT(DT),
146       NumLoads(0), NumStores(0), MaxSafeDepDistBytes(-1U),
147       VectParams(VectParams) {}
148
149   /// Return true we can analyze the memory accesses in the loop and there are
150   /// no memory dependence cycles.  Replaces symbolic strides using Strides.
151   bool canVectorizeMemory(ValueToValueMap &Strides);
152
153   RuntimePointerCheck *getRuntimePointerCheck() { return &PtrRtCheck; }
154
155   /// Return true if the block BB needs to be predicated in order for the loop
156   /// to be vectorized.
157   bool blockNeedsPredication(BasicBlock *BB);
158
159   /// Returns true if the value V is uniform within the loop.
160   bool isUniform(Value *V);
161
162   unsigned getMaxSafeDepDistBytes() const { return MaxSafeDepDistBytes; }
163   unsigned getNumStores() const { return NumStores; }
164   unsigned getNumLoads() const { return NumLoads;}
165
166   /// \brief Add code that checks at runtime if the accessed arrays overlap.
167   ///
168   /// Returns a pair of instructions where the first element is the first
169   /// instruction generated in possibly a sequence of instructions and the
170   /// second value is the final comparator value or NULL if no check is needed.
171   std::pair<Instruction *, Instruction *> addRuntimeCheck(Instruction *Loc);
172
173 private:
174   void emitAnalysis(VectorizationReport &Message);
175
176   /// We need to check that all of the pointers in this list are disjoint
177   /// at runtime.
178   RuntimePointerCheck PtrRtCheck;
179   Function *TheFunction;
180   Loop *TheLoop;
181   ScalarEvolution *SE;
182   const DataLayout *DL;
183   const TargetLibraryInfo *TLI;
184   AliasAnalysis *AA;
185   DominatorTree *DT;
186
187   unsigned NumLoads;
188   unsigned NumStores;
189
190   unsigned MaxSafeDepDistBytes;
191
192   /// \brief Vectorizer parameters used by the analysis.
193   VectorizerParams VectParams;
194 };
195
196 Value *stripIntegerCast(Value *V);
197
198 ///\brief Return the SCEV corresponding to a pointer with the symbolic stride
199 ///replaced with constant one.
200 ///
201 /// If \p OrigPtr is not null, use it to look up the stride value instead of \p
202 /// Ptr.  \p PtrToStride provides the mapping between the pointer value and its
203 /// stride as collected by LoopVectorizationLegality::collectStridedAccess.
204 const SCEV *replaceSymbolicStrideSCEV(ScalarEvolution *SE,
205                                       ValueToValueMap &PtrToStride,
206                                       Value *Ptr, Value *OrigPtr = nullptr);
207
208 } // End llvm namespace
209
210 #endif