1 //===- llvm/Analysis/LoopAccessAnalysis.h -----------------------*- 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 the interface for the loop memory dependence framework that
11 // was originally developed for the Loop Vectorizer.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
16 #define LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
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"
31 class ScalarEvolution;
35 /// Optimization analysis message produced during vectorization. Messages inform
36 /// the user why vectorization did not occur.
37 class VectorizationReport {
39 raw_string_ostream Out;
43 VectorizationReport(Instruction *I = nullptr) : Out(Message), Instr(I) {
44 Out << "loop not vectorized: ";
47 template <typename A> VectorizationReport &operator<<(const A &Value) {
52 Instruction *getInstr() { return Instr; }
54 std::string &str() { return Out.str(); }
55 operator Twine() { return Out.str(); }
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,
64 /// \brief Drive the analysis of memory accesses in the loop
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.
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.
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 {
80 /// \brief Collection of parameters used from the vectorizer.
81 struct VectorizerParams {
82 /// \brief Maximum simd width.
83 unsigned MaxVectorWidth;
85 /// \brief VF as overridden by the user.
86 unsigned VectorizationFactor;
87 /// \brief Interleave factor as overridden by the user.
88 unsigned VectorizationInterleave;
90 /// \\brief When performing memory disambiguation checks at runtime do not
91 /// make more than this number of comparisons.
92 unsigned RuntimeMemoryCheckThreshold;
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) {}
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) {}
109 /// Reset the state of the pointer runtime information.
116 DependencySetId.clear();
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);
124 /// This flag indicates if we need to add the runtime check.
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;
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) {}
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);
153 RuntimePointerCheck *getRuntimePointerCheck() { return &PtrRtCheck; }
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);
159 /// Returns true if the value V is uniform within the loop.
160 bool isUniform(Value *V);
162 unsigned getMaxSafeDepDistBytes() { return MaxSafeDepDistBytes; }
165 void emitAnalysis(VectorizationReport &Message);
167 /// We need to check that all of the pointers in this list are disjoint
169 RuntimePointerCheck PtrRtCheck;
170 Function *TheFunction;
173 const DataLayout *DL;
174 const TargetLibraryInfo *TLI;
181 unsigned MaxSafeDepDistBytes;
183 /// \brief Vectorizer parameters used by the analysis.
184 VectorizerParams VectParams;
187 Value *stripIntegerCast(Value *V);
189 ///\brief Return the SCEV corresponding to a pointer with the symbolic stride
190 ///replaced with constant one.
192 /// If \p OrigPtr is not null, use it to look up the stride value instead of \p
193 /// Ptr. \p PtrToStride provides the mapping between the pointer value and its
194 /// stride as collected by LoopVectorizationLegality::collectStridedAccess.
195 const SCEV *replaceSymbolicStrideSCEV(ScalarEvolution *SE,
196 ValueToValueMap &PtrToStride,
197 Value *Ptr, Value *OrigPtr = nullptr);
199 } // End llvm namespace