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/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"
32 class ScalarEvolution;
36 /// Optimization analysis message produced during vectorization. Messages inform
37 /// the user why vectorization did not occur.
38 class VectorizationReport {
43 VectorizationReport(Instruction *I = nullptr)
44 : Message("loop not vectorized: "), Instr(I) {}
46 template <typename A> VectorizationReport &operator<<(const A &Value) {
47 raw_string_ostream Out(Message);
52 Instruction *getInstr() { return Instr; }
54 std::string &str() { return Message; }
55 operator Twine() { return Message; }
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 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;
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;
75 /// \\brief When performing memory disambiguation checks at runtime do not
76 /// make more than this number of comparisons.
77 static const unsigned RuntimeMemoryCheckThreshold;
80 /// \brief Drive the analysis of memory accesses in the loop
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.
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.
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 {
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) {}
101 /// Reset the state of the pointer runtime information.
108 DependencySetId.clear();
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);
116 /// This flag indicates if we need to add the runtime check.
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;
133 LoopAccessInfo(Loop *L, ScalarEvolution *SE, const DataLayout *DL,
134 const TargetLibraryInfo *TLI, AliasAnalysis *AA,
136 TheLoop(L), SE(SE), DL(DL), TLI(TLI), AA(AA), DT(DT), NumLoads(0),
137 NumStores(0), MaxSafeDepDistBytes(-1U), CanVecMem(false) {}
139 /// \brief Analyze the loop. Replaces symbolic strides using Strides.
140 void analyzeLoop(ValueToValueMap &Strides);
142 /// Return true we can analyze the memory accesses in the loop and there are
143 /// no memory dependence cycles.
144 bool canVectorizeMemory() { return CanVecMem; }
146 RuntimePointerCheck *getRuntimePointerCheck() { return &PtrRtCheck; }
148 /// Return true if the block BB needs to be predicated in order for the loop
149 /// to be vectorized.
150 static bool blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
153 /// Returns true if the value V is uniform within the loop.
154 bool isUniform(Value *V);
156 unsigned getMaxSafeDepDistBytes() const { return MaxSafeDepDistBytes; }
157 unsigned getNumStores() const { return NumStores; }
158 unsigned getNumLoads() const { return NumLoads;}
160 /// \brief Add code that checks at runtime if the accessed arrays overlap.
162 /// Returns a pair of instructions where the first element is the first
163 /// instruction generated in possibly a sequence of instructions and the
164 /// second value is the final comparator value or NULL if no check is needed.
165 std::pair<Instruction *, Instruction *> addRuntimeCheck(Instruction *Loc);
167 /// \brief The diagnostics report generated for the analysis. E.g. why we
168 /// couldn't analyze the loop.
169 Optional<VectorizationReport> &getReport() { return Report; }
172 void emitAnalysis(VectorizationReport &Message);
174 /// We need to check that all of the pointers in this list are disjoint
176 RuntimePointerCheck PtrRtCheck;
179 const DataLayout *DL;
180 const TargetLibraryInfo *TLI;
187 unsigned MaxSafeDepDistBytes;
189 /// \brief Cache the result of analyzeLoop.
192 /// \brief The diagnostics report generated for the analysis. E.g. why we
193 /// couldn't analyze the loop.
194 Optional<VectorizationReport> Report;
197 Value *stripIntegerCast(Value *V);
199 ///\brief Return the SCEV corresponding to a pointer with the symbolic stride
200 ///replaced with constant one.
202 /// If \p OrigPtr is not null, use it to look up the stride value instead of \p
203 /// Ptr. \p PtrToStride provides the mapping between the pointer value and its
204 /// stride as collected by LoopVectorizationLegality::collectStridedAccess.
205 const SCEV *replaceSymbolicStrideSCEV(ScalarEvolution *SE,
206 ValueToValueMap &PtrToStride,
207 Value *Ptr, Value *OrigPtr = nullptr);
209 } // End llvm namespace