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