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