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