Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1 | //===- LoopAccessAnalysis.cpp - Loop Access Analysis Implementation --------==// |
| 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 | // The implementation for the loop memory dependence that was originally |
| 11 | // developed for the loop vectorizer. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
| 16 | #include "llvm/Analysis/LoopInfo.h" |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ValueTracking.h" |
| 20 | #include "llvm/IR/DiagnosticInfo.h" |
| 21 | #include "llvm/IR/Dominators.h" |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 22 | #include "llvm/IR/IRBuilder.h" |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | b447ac6 | 2015-06-26 18:02:52 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/VectorUtils.h" |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "loop-accesses" |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 29 | |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 30 | static cl::opt<unsigned, true> |
| 31 | VectorizationFactor("force-vector-width", cl::Hidden, |
| 32 | cl::desc("Sets the SIMD width. Zero is autoselect."), |
| 33 | cl::location(VectorizerParams::VectorizationFactor)); |
Adam Nemet | 1d862af | 2015-02-26 04:39:09 +0000 | [diff] [blame] | 34 | unsigned VectorizerParams::VectorizationFactor; |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 35 | |
| 36 | static cl::opt<unsigned, true> |
| 37 | VectorizationInterleave("force-vector-interleave", cl::Hidden, |
| 38 | cl::desc("Sets the vectorization interleave count. " |
| 39 | "Zero is autoselect."), |
| 40 | cl::location( |
| 41 | VectorizerParams::VectorizationInterleave)); |
Adam Nemet | 1d862af | 2015-02-26 04:39:09 +0000 | [diff] [blame] | 42 | unsigned VectorizerParams::VectorizationInterleave; |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 43 | |
Adam Nemet | 1d862af | 2015-02-26 04:39:09 +0000 | [diff] [blame] | 44 | static cl::opt<unsigned, true> RuntimeMemoryCheckThreshold( |
| 45 | "runtime-memory-check-threshold", cl::Hidden, |
| 46 | cl::desc("When performing memory disambiguation checks at runtime do not " |
| 47 | "generate more than this number of comparisons (default = 8)."), |
| 48 | cl::location(VectorizerParams::RuntimeMemoryCheckThreshold), cl::init(8)); |
| 49 | unsigned VectorizerParams::RuntimeMemoryCheckThreshold; |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 50 | |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 51 | /// \brief The maximum iterations used to merge memory checks |
| 52 | static cl::opt<unsigned> MemoryCheckMergeThreshold( |
| 53 | "memory-check-merge-threshold", cl::Hidden, |
| 54 | cl::desc("Maximum number of comparisons done when trying to merge " |
| 55 | "runtime memory checks. (default = 100)"), |
| 56 | cl::init(100)); |
| 57 | |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 58 | /// Maximum SIMD width. |
| 59 | const unsigned VectorizerParams::MaxVectorWidth = 64; |
| 60 | |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 61 | /// \brief We collect interesting dependences up to this threshold. |
| 62 | static cl::opt<unsigned> MaxInterestingDependence( |
| 63 | "max-interesting-dependences", cl::Hidden, |
| 64 | cl::desc("Maximum number of interesting dependences collected by " |
| 65 | "loop-access analysis (default = 100)"), |
| 66 | cl::init(100)); |
| 67 | |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 68 | bool VectorizerParams::isInterleaveForced() { |
| 69 | return ::VectorizationInterleave.getNumOccurrences() > 0; |
| 70 | } |
| 71 | |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 72 | void LoopAccessReport::emitAnalysis(const LoopAccessReport &Message, |
| 73 | const Function *TheFunction, |
| 74 | const Loop *TheLoop, |
| 75 | const char *PassName) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 76 | DebugLoc DL = TheLoop->getStartLoc(); |
Adam Nemet | 3e87634 | 2015-02-19 19:15:13 +0000 | [diff] [blame] | 77 | if (const Instruction *I = Message.getInstr()) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 78 | DL = I->getDebugLoc(); |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 79 | emitOptimizationRemarkAnalysis(TheFunction->getContext(), PassName, |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 80 | *TheFunction, DL, Message.str()); |
| 81 | } |
| 82 | |
| 83 | Value *llvm::stripIntegerCast(Value *V) { |
| 84 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 85 | if (CI->getOperand(0)->getType()->isIntegerTy()) |
| 86 | return CI->getOperand(0); |
| 87 | return V; |
| 88 | } |
| 89 | |
| 90 | const SCEV *llvm::replaceSymbolicStrideSCEV(ScalarEvolution *SE, |
Adam Nemet | 8bc61df | 2015-02-24 00:41:59 +0000 | [diff] [blame] | 91 | const ValueToValueMap &PtrToStride, |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 92 | Value *Ptr, Value *OrigPtr) { |
| 93 | |
| 94 | const SCEV *OrigSCEV = SE->getSCEV(Ptr); |
| 95 | |
| 96 | // If there is an entry in the map return the SCEV of the pointer with the |
| 97 | // symbolic stride replaced by one. |
Adam Nemet | 8bc61df | 2015-02-24 00:41:59 +0000 | [diff] [blame] | 98 | ValueToValueMap::const_iterator SI = |
| 99 | PtrToStride.find(OrigPtr ? OrigPtr : Ptr); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 100 | if (SI != PtrToStride.end()) { |
| 101 | Value *StrideVal = SI->second; |
| 102 | |
| 103 | // Strip casts. |
| 104 | StrideVal = stripIntegerCast(StrideVal); |
| 105 | |
| 106 | // Replace symbolic stride by one. |
| 107 | Value *One = ConstantInt::get(StrideVal->getType(), 1); |
| 108 | ValueToValueMap RewriteMap; |
| 109 | RewriteMap[StrideVal] = One; |
| 110 | |
| 111 | const SCEV *ByOne = |
| 112 | SCEVParameterRewriter::rewrite(OrigSCEV, *SE, RewriteMap, true); |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 113 | DEBUG(dbgs() << "LAA: Replacing SCEV: " << *OrigSCEV << " by: " << *ByOne |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 114 | << "\n"); |
| 115 | return ByOne; |
| 116 | } |
| 117 | |
| 118 | // Otherwise, just return the SCEV of the original pointer. |
| 119 | return SE->getSCEV(Ptr); |
| 120 | } |
| 121 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 122 | void RuntimePointerChecking::insert(Loop *Lp, Value *Ptr, bool WritePtr, |
| 123 | unsigned DepSetId, unsigned ASId, |
| 124 | const ValueToValueMap &Strides) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 125 | // Get the stride replaced scev. |
| 126 | const SCEV *Sc = replaceSymbolicStrideSCEV(SE, Strides, Ptr); |
| 127 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Sc); |
| 128 | assert(AR && "Invalid addrec expression"); |
| 129 | const SCEV *Ex = SE->getBackedgeTakenCount(Lp); |
Silviu Baranga | 0e5804a | 2015-07-16 14:02:58 +0000 | [diff] [blame] | 130 | |
| 131 | const SCEV *ScStart = AR->getStart(); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 132 | const SCEV *ScEnd = AR->evaluateAtIteration(Ex, *SE); |
Silviu Baranga | 0e5804a | 2015-07-16 14:02:58 +0000 | [diff] [blame] | 133 | const SCEV *Step = AR->getStepRecurrence(*SE); |
| 134 | |
| 135 | // For expressions with negative step, the upper bound is ScStart and the |
| 136 | // lower bound is ScEnd. |
| 137 | if (const SCEVConstant *CStep = dyn_cast<const SCEVConstant>(Step)) { |
| 138 | if (CStep->getValue()->isNegative()) |
| 139 | std::swap(ScStart, ScEnd); |
| 140 | } else { |
| 141 | // Fallback case: the step is not constant, but the we can still |
| 142 | // get the upper and lower bounds of the interval by using min/max |
| 143 | // expressions. |
| 144 | ScStart = SE->getUMinExpr(ScStart, ScEnd); |
| 145 | ScEnd = SE->getUMaxExpr(AR->getStart(), ScEnd); |
| 146 | } |
| 147 | |
| 148 | Pointers.emplace_back(Ptr, ScStart, ScEnd, WritePtr, DepSetId, ASId, Sc); |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Adam Nemet | bbe1f1d | 2015-07-27 19:38:48 +0000 | [diff] [blame] | 151 | SmallVector<RuntimePointerChecking::PointerCheck, 4> |
| 152 | RuntimePointerChecking::generateChecks( |
| 153 | const SmallVectorImpl<int> *PtrPartition) const { |
| 154 | SmallVector<PointerCheck, 4> Checks; |
| 155 | |
Adam Nemet | 7c52e05 | 2015-07-27 19:38:50 +0000 | [diff] [blame^] | 156 | for (unsigned I = 0; I < CheckingGroups.size(); ++I) { |
| 157 | for (unsigned J = I + 1; J < CheckingGroups.size(); ++J) { |
| 158 | const RuntimePointerChecking::CheckingPtrGroup &CGI = CheckingGroups[I]; |
| 159 | const RuntimePointerChecking::CheckingPtrGroup &CGJ = CheckingGroups[J]; |
Adam Nemet | bbe1f1d | 2015-07-27 19:38:48 +0000 | [diff] [blame] | 160 | |
| 161 | if (needsChecking(CGI, CGJ, PtrPartition)) |
| 162 | Checks.push_back(std::make_pair(&CGI, &CGJ)); |
| 163 | } |
| 164 | } |
| 165 | return Checks; |
| 166 | } |
| 167 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 168 | bool RuntimePointerChecking::needsChecking( |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 169 | const CheckingPtrGroup &M, const CheckingPtrGroup &N, |
| 170 | const SmallVectorImpl<int> *PtrPartition) const { |
| 171 | for (unsigned I = 0, EI = M.Members.size(); EI != I; ++I) |
| 172 | for (unsigned J = 0, EJ = N.Members.size(); EJ != J; ++J) |
| 173 | if (needsChecking(M.Members[I], N.Members[J], PtrPartition)) |
| 174 | return true; |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | /// Compare \p I and \p J and return the minimum. |
| 179 | /// Return nullptr in case we couldn't find an answer. |
| 180 | static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J, |
| 181 | ScalarEvolution *SE) { |
| 182 | const SCEV *Diff = SE->getMinusSCEV(J, I); |
| 183 | const SCEVConstant *C = dyn_cast<const SCEVConstant>(Diff); |
| 184 | |
| 185 | if (!C) |
| 186 | return nullptr; |
| 187 | if (C->getValue()->isNegative()) |
| 188 | return J; |
| 189 | return I; |
| 190 | } |
| 191 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 192 | bool RuntimePointerChecking::CheckingPtrGroup::addPointer(unsigned Index) { |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 193 | const SCEV *Start = RtCheck.Pointers[Index].Start; |
| 194 | const SCEV *End = RtCheck.Pointers[Index].End; |
| 195 | |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 196 | // Compare the starts and ends with the known minimum and maximum |
| 197 | // of this set. We need to know how we compare against the min/max |
| 198 | // of the set in order to be able to emit memchecks. |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 199 | const SCEV *Min0 = getMinFromExprs(Start, Low, RtCheck.SE); |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 200 | if (!Min0) |
| 201 | return false; |
| 202 | |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 203 | const SCEV *Min1 = getMinFromExprs(End, High, RtCheck.SE); |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 204 | if (!Min1) |
| 205 | return false; |
| 206 | |
| 207 | // Update the low bound expression if we've found a new min value. |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 208 | if (Min0 == Start) |
| 209 | Low = Start; |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 210 | |
| 211 | // Update the high bound expression if we've found a new max value. |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 212 | if (Min1 != End) |
| 213 | High = End; |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 214 | |
| 215 | Members.push_back(Index); |
| 216 | return true; |
| 217 | } |
| 218 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 219 | void RuntimePointerChecking::groupChecks( |
| 220 | MemoryDepChecker::DepCandidates &DepCands, bool UseDependencies) { |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 221 | // We build the groups from dependency candidates equivalence classes |
| 222 | // because: |
| 223 | // - We know that pointers in the same equivalence class share |
| 224 | // the same underlying object and therefore there is a chance |
| 225 | // that we can compare pointers |
| 226 | // - We wouldn't be able to merge two pointers for which we need |
| 227 | // to emit a memcheck. The classes in DepCands are already |
| 228 | // conveniently built such that no two pointers in the same |
| 229 | // class need checking against each other. |
| 230 | |
| 231 | // We use the following (greedy) algorithm to construct the groups |
| 232 | // For every pointer in the equivalence class: |
| 233 | // For each existing group: |
| 234 | // - if the difference between this pointer and the min/max bounds |
| 235 | // of the group is a constant, then make the pointer part of the |
| 236 | // group and update the min/max bounds of that group as required. |
| 237 | |
| 238 | CheckingGroups.clear(); |
| 239 | |
| 240 | // If we don't have the dependency partitions, construct a new |
| 241 | // checking pointer group for each pointer. |
| 242 | if (!UseDependencies) { |
| 243 | for (unsigned I = 0; I < Pointers.size(); ++I) |
| 244 | CheckingGroups.push_back(CheckingPtrGroup(I, *this)); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | unsigned TotalComparisons = 0; |
| 249 | |
| 250 | DenseMap<Value *, unsigned> PositionMap; |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 251 | for (unsigned Index = 0; Index < Pointers.size(); ++Index) |
| 252 | PositionMap[Pointers[Index].PointerValue] = Index; |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 253 | |
Silviu Baranga | ce3877f | 2015-07-09 15:18:25 +0000 | [diff] [blame] | 254 | // We need to keep track of what pointers we've already seen so we |
| 255 | // don't process them twice. |
| 256 | SmallSet<unsigned, 2> Seen; |
| 257 | |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 258 | // Go through all equivalence classes, get the the "pointer check groups" |
Silviu Baranga | ce3877f | 2015-07-09 15:18:25 +0000 | [diff] [blame] | 259 | // and add them to the overall solution. We use the order in which accesses |
| 260 | // appear in 'Pointers' to enforce determinism. |
| 261 | for (unsigned I = 0; I < Pointers.size(); ++I) { |
| 262 | // We've seen this pointer before, and therefore already processed |
| 263 | // its equivalence class. |
| 264 | if (Seen.count(I)) |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 265 | continue; |
| 266 | |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 267 | MemoryDepChecker::MemAccessInfo Access(Pointers[I].PointerValue, |
| 268 | Pointers[I].IsWritePtr); |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 269 | |
Silviu Baranga | ce3877f | 2015-07-09 15:18:25 +0000 | [diff] [blame] | 270 | SmallVector<CheckingPtrGroup, 2> Groups; |
| 271 | auto LeaderI = DepCands.findValue(DepCands.getLeaderValue(Access)); |
| 272 | |
Silviu Baranga | a647c30 | 2015-07-13 14:48:24 +0000 | [diff] [blame] | 273 | // Because DepCands is constructed by visiting accesses in the order in |
| 274 | // which they appear in alias sets (which is deterministic) and the |
| 275 | // iteration order within an equivalence class member is only dependent on |
| 276 | // the order in which unions and insertions are performed on the |
| 277 | // equivalence class, the iteration order is deterministic. |
Silviu Baranga | ce3877f | 2015-07-09 15:18:25 +0000 | [diff] [blame] | 278 | for (auto MI = DepCands.member_begin(LeaderI), ME = DepCands.member_end(); |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 279 | MI != ME; ++MI) { |
| 280 | unsigned Pointer = PositionMap[MI->getPointer()]; |
| 281 | bool Merged = false; |
Silviu Baranga | ce3877f | 2015-07-09 15:18:25 +0000 | [diff] [blame] | 282 | // Mark this pointer as seen. |
| 283 | Seen.insert(Pointer); |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 284 | |
| 285 | // Go through all the existing sets and see if we can find one |
| 286 | // which can include this pointer. |
| 287 | for (CheckingPtrGroup &Group : Groups) { |
| 288 | // Don't perform more than a certain amount of comparisons. |
| 289 | // This should limit the cost of grouping the pointers to something |
| 290 | // reasonable. If we do end up hitting this threshold, the algorithm |
| 291 | // will create separate groups for all remaining pointers. |
| 292 | if (TotalComparisons > MemoryCheckMergeThreshold) |
| 293 | break; |
| 294 | |
| 295 | TotalComparisons++; |
| 296 | |
| 297 | if (Group.addPointer(Pointer)) { |
| 298 | Merged = true; |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (!Merged) |
| 304 | // We couldn't add this pointer to any existing set or the threshold |
| 305 | // for the number of comparisons has been reached. Create a new group |
| 306 | // to hold the current pointer. |
| 307 | Groups.push_back(CheckingPtrGroup(Pointer, *this)); |
| 308 | } |
| 309 | |
| 310 | // We've computed the grouped checks for this partition. |
| 311 | // Save the results and continue with the next one. |
| 312 | std::copy(Groups.begin(), Groups.end(), std::back_inserter(CheckingGroups)); |
| 313 | } |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Adam Nemet | 041e6de | 2015-07-16 02:48:05 +0000 | [diff] [blame] | 316 | bool RuntimePointerChecking::arePointersInSamePartition( |
| 317 | const SmallVectorImpl<int> &PtrToPartition, unsigned PtrIdx1, |
| 318 | unsigned PtrIdx2) { |
| 319 | return (PtrToPartition[PtrIdx1] != -1 && |
| 320 | PtrToPartition[PtrIdx1] == PtrToPartition[PtrIdx2]); |
| 321 | } |
| 322 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 323 | bool RuntimePointerChecking::needsChecking( |
Adam Nemet | ec1e2bb | 2015-03-10 18:54:26 +0000 | [diff] [blame] | 324 | unsigned I, unsigned J, const SmallVectorImpl<int> *PtrPartition) const { |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 325 | const PointerInfo &PointerI = Pointers[I]; |
| 326 | const PointerInfo &PointerJ = Pointers[J]; |
| 327 | |
Adam Nemet | a8945b7 | 2015-02-18 03:43:58 +0000 | [diff] [blame] | 328 | // No need to check if two readonly pointers intersect. |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 329 | if (!PointerI.IsWritePtr && !PointerJ.IsWritePtr) |
Adam Nemet | a8945b7 | 2015-02-18 03:43:58 +0000 | [diff] [blame] | 330 | return false; |
| 331 | |
| 332 | // Only need to check pointers between two different dependency sets. |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 333 | if (PointerI.DependencySetId == PointerJ.DependencySetId) |
Adam Nemet | a8945b7 | 2015-02-18 03:43:58 +0000 | [diff] [blame] | 334 | return false; |
| 335 | |
| 336 | // Only need to check pointers in the same alias set. |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 337 | if (PointerI.AliasSetId != PointerJ.AliasSetId) |
Adam Nemet | a8945b7 | 2015-02-18 03:43:58 +0000 | [diff] [blame] | 338 | return false; |
| 339 | |
Adam Nemet | ec1e2bb | 2015-03-10 18:54:26 +0000 | [diff] [blame] | 340 | // If PtrPartition is set omit checks between pointers of the same partition. |
Adam Nemet | 041e6de | 2015-07-16 02:48:05 +0000 | [diff] [blame] | 341 | if (PtrPartition && arePointersInSamePartition(*PtrPartition, I, J)) |
Adam Nemet | ec1e2bb | 2015-03-10 18:54:26 +0000 | [diff] [blame] | 342 | return false; |
| 343 | |
Adam Nemet | a8945b7 | 2015-02-18 03:43:58 +0000 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 347 | void RuntimePointerChecking::print( |
Adam Nemet | ec1e2bb | 2015-03-10 18:54:26 +0000 | [diff] [blame] | 348 | raw_ostream &OS, unsigned Depth, |
| 349 | const SmallVectorImpl<int> *PtrPartition) const { |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 350 | |
| 351 | OS.indent(Depth) << "Run-time memory checks:\n"; |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 352 | |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 353 | unsigned N = 0; |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 354 | for (unsigned I = 0; I < CheckingGroups.size(); ++I) |
| 355 | for (unsigned J = I + 1; J < CheckingGroups.size(); ++J) |
| 356 | if (needsChecking(CheckingGroups[I], CheckingGroups[J], PtrPartition)) { |
| 357 | OS.indent(Depth) << "Check " << N++ << ":\n"; |
| 358 | OS.indent(Depth + 2) << "Comparing group " << I << ":\n"; |
| 359 | |
| 360 | for (unsigned K = 0; K < CheckingGroups[I].Members.size(); ++K) { |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 361 | OS.indent(Depth + 2) |
| 362 | << *Pointers[CheckingGroups[I].Members[K]].PointerValue << "\n"; |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 363 | if (PtrPartition) |
| 364 | OS << " (Partition: " |
| 365 | << (*PtrPartition)[CheckingGroups[I].Members[K]] << ")" |
| 366 | << "\n"; |
| 367 | } |
| 368 | |
| 369 | OS.indent(Depth + 2) << "Against group " << J << ":\n"; |
| 370 | |
| 371 | for (unsigned K = 0; K < CheckingGroups[J].Members.size(); ++K) { |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 372 | OS.indent(Depth + 2) |
| 373 | << *Pointers[CheckingGroups[J].Members[K]].PointerValue << "\n"; |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 374 | if (PtrPartition) |
| 375 | OS << " (Partition: " |
| 376 | << (*PtrPartition)[CheckingGroups[J].Members[K]] << ")" |
| 377 | << "\n"; |
| 378 | } |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 379 | } |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 380 | |
| 381 | OS.indent(Depth) << "Grouped accesses:\n"; |
| 382 | for (unsigned I = 0; I < CheckingGroups.size(); ++I) { |
| 383 | OS.indent(Depth + 2) << "Group " << I << ":\n"; |
| 384 | OS.indent(Depth + 4) << "(Low: " << *CheckingGroups[I].Low |
| 385 | << " High: " << *CheckingGroups[I].High << ")\n"; |
| 386 | for (unsigned J = 0; J < CheckingGroups[I].Members.size(); ++J) { |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 387 | OS.indent(Depth + 6) << "Member: " |
| 388 | << *Pointers[CheckingGroups[I].Members[J]].Expr |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 389 | << "\n"; |
| 390 | } |
| 391 | } |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 394 | unsigned RuntimePointerChecking::getNumberOfChecks( |
Adam Nemet | 51870d1 | 2015-04-07 03:35:26 +0000 | [diff] [blame] | 395 | const SmallVectorImpl<int> *PtrPartition) const { |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 396 | |
| 397 | unsigned NumPartitions = CheckingGroups.size(); |
Silviu Baranga | 98a1371 | 2015-06-08 10:27:06 +0000 | [diff] [blame] | 398 | unsigned CheckCount = 0; |
Adam Nemet | 51870d1 | 2015-04-07 03:35:26 +0000 | [diff] [blame] | 399 | |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 400 | for (unsigned I = 0; I < NumPartitions; ++I) |
| 401 | for (unsigned J = I + 1; J < NumPartitions; ++J) |
| 402 | if (needsChecking(CheckingGroups[I], CheckingGroups[J], PtrPartition)) |
Silviu Baranga | 98a1371 | 2015-06-08 10:27:06 +0000 | [diff] [blame] | 403 | CheckCount++; |
| 404 | return CheckCount; |
| 405 | } |
| 406 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 407 | bool RuntimePointerChecking::needsAnyChecking( |
Silviu Baranga | 98a1371 | 2015-06-08 10:27:06 +0000 | [diff] [blame] | 408 | const SmallVectorImpl<int> *PtrPartition) const { |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 409 | unsigned NumPointers = Pointers.size(); |
| 410 | |
| 411 | for (unsigned I = 0; I < NumPointers; ++I) |
| 412 | for (unsigned J = I + 1; J < NumPointers; ++J) |
| 413 | if (needsChecking(I, J, PtrPartition)) |
| 414 | return true; |
| 415 | return false; |
Adam Nemet | 51870d1 | 2015-04-07 03:35:26 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 418 | namespace { |
| 419 | /// \brief Analyses memory accesses in a loop. |
| 420 | /// |
| 421 | /// Checks whether run time pointer checks are needed and builds sets for data |
| 422 | /// dependence checking. |
| 423 | class AccessAnalysis { |
| 424 | public: |
| 425 | /// \brief Read or write access location. |
| 426 | typedef PointerIntPair<Value *, 1, bool> MemAccessInfo; |
| 427 | typedef SmallPtrSet<MemAccessInfo, 8> MemAccessInfoSet; |
| 428 | |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 429 | AccessAnalysis(const DataLayout &Dl, AliasAnalysis *AA, LoopInfo *LI, |
Adam Nemet | dee666b | 2015-03-10 17:40:34 +0000 | [diff] [blame] | 430 | MemoryDepChecker::DepCandidates &DA) |
Adam Nemet | 5dc3b2c | 2015-07-09 06:47:18 +0000 | [diff] [blame] | 431 | : DL(Dl), AST(*AA), LI(LI), DepCands(DA), |
| 432 | IsRTCheckAnalysisNeeded(false) {} |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 433 | |
| 434 | /// \brief Register a load and whether it is only read from. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 435 | void addLoad(MemoryLocation &Loc, bool IsReadOnly) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 436 | Value *Ptr = const_cast<Value*>(Loc.Ptr); |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 437 | AST.add(Ptr, MemoryLocation::UnknownSize, Loc.AATags); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 438 | Accesses.insert(MemAccessInfo(Ptr, false)); |
| 439 | if (IsReadOnly) |
| 440 | ReadOnlyPtr.insert(Ptr); |
| 441 | } |
| 442 | |
| 443 | /// \brief Register a store. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 444 | void addStore(MemoryLocation &Loc) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 445 | Value *Ptr = const_cast<Value*>(Loc.Ptr); |
Chandler Carruth | ecbd168 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 446 | AST.add(Ptr, MemoryLocation::UnknownSize, Loc.AATags); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 447 | Accesses.insert(MemAccessInfo(Ptr, true)); |
| 448 | } |
| 449 | |
| 450 | /// \brief Check whether we can check the pointers at runtime for |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 451 | /// non-intersection. |
| 452 | /// |
| 453 | /// Returns true if we need no check or if we do and we can generate them |
| 454 | /// (i.e. the pointers have computable bounds). |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 455 | bool canCheckPtrAtRT(RuntimePointerChecking &RtCheck, ScalarEvolution *SE, |
| 456 | Loop *TheLoop, const ValueToValueMap &Strides, |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 457 | bool ShouldCheckStride = false); |
| 458 | |
| 459 | /// \brief Goes over all memory accesses, checks whether a RT check is needed |
| 460 | /// and builds sets of dependent accesses. |
| 461 | void buildDependenceSets() { |
| 462 | processMemAccesses(); |
| 463 | } |
| 464 | |
Adam Nemet | 5dc3b2c | 2015-07-09 06:47:18 +0000 | [diff] [blame] | 465 | /// \brief Initial processing of memory accesses determined that we need to |
| 466 | /// perform dependency checking. |
| 467 | /// |
| 468 | /// Note that this can later be cleared if we retry memcheck analysis without |
| 469 | /// dependency checking (i.e. ShouldRetryWithRuntimeCheck). |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 470 | bool isDependencyCheckNeeded() { return !CheckDeps.empty(); } |
Adam Nemet | df3dc5b | 2015-05-18 15:37:03 +0000 | [diff] [blame] | 471 | |
| 472 | /// We decided that no dependence analysis would be used. Reset the state. |
| 473 | void resetDepChecks(MemoryDepChecker &DepChecker) { |
| 474 | CheckDeps.clear(); |
| 475 | DepChecker.clearInterestingDependences(); |
| 476 | } |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 477 | |
| 478 | MemAccessInfoSet &getDependenciesToCheck() { return CheckDeps; } |
| 479 | |
| 480 | private: |
| 481 | typedef SetVector<MemAccessInfo> PtrAccessSet; |
| 482 | |
| 483 | /// \brief Go over all memory access and check whether runtime pointer checks |
Adam Nemet | b41d2d3 | 2015-07-09 06:47:21 +0000 | [diff] [blame] | 484 | /// are needed and build sets of dependency check candidates. |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 485 | void processMemAccesses(); |
| 486 | |
| 487 | /// Set of all accesses. |
| 488 | PtrAccessSet Accesses; |
| 489 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 490 | const DataLayout &DL; |
| 491 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 492 | /// Set of accesses that need a further dependence check. |
| 493 | MemAccessInfoSet CheckDeps; |
| 494 | |
| 495 | /// Set of pointers that are read only. |
| 496 | SmallPtrSet<Value*, 16> ReadOnlyPtr; |
| 497 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 498 | /// An alias set tracker to partition the access set by underlying object and |
| 499 | //intrinsic property (such as TBAA metadata). |
| 500 | AliasSetTracker AST; |
| 501 | |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 502 | LoopInfo *LI; |
| 503 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 504 | /// Sets of potentially dependent accesses - members of one set share an |
| 505 | /// underlying pointer. The set "CheckDeps" identfies which sets really need a |
| 506 | /// dependence check. |
Adam Nemet | dee666b | 2015-03-10 17:40:34 +0000 | [diff] [blame] | 507 | MemoryDepChecker::DepCandidates &DepCands; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 508 | |
Adam Nemet | 5dc3b2c | 2015-07-09 06:47:18 +0000 | [diff] [blame] | 509 | /// \brief Initial processing of memory accesses determined that we may need |
| 510 | /// to add memchecks. Perform the analysis to determine the necessary checks. |
| 511 | /// |
| 512 | /// Note that, this is different from isDependencyCheckNeeded. When we retry |
| 513 | /// memcheck analysis without dependency checking |
| 514 | /// (i.e. ShouldRetryWithRuntimeCheck), isDependencyCheckNeeded is cleared |
| 515 | /// while this remains set if we have potentially dependent accesses. |
| 516 | bool IsRTCheckAnalysisNeeded; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | } // end anonymous namespace |
| 520 | |
| 521 | /// \brief Check whether a pointer can participate in a runtime bounds check. |
Adam Nemet | 8bc61df | 2015-02-24 00:41:59 +0000 | [diff] [blame] | 522 | static bool hasComputableBounds(ScalarEvolution *SE, |
| 523 | const ValueToValueMap &Strides, Value *Ptr) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 524 | const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, Strides, Ptr); |
| 525 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev); |
| 526 | if (!AR) |
| 527 | return false; |
| 528 | |
| 529 | return AR->isAffine(); |
| 530 | } |
| 531 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 532 | bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck, |
| 533 | ScalarEvolution *SE, Loop *TheLoop, |
| 534 | const ValueToValueMap &StridesMap, |
| 535 | bool ShouldCheckStride) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 536 | // Find pointers with computable bounds. We are going to use this information |
| 537 | // to place a runtime bound check. |
| 538 | bool CanDoRT = true; |
| 539 | |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 540 | bool NeedRTCheck = false; |
Adam Nemet | 5dc3b2c | 2015-07-09 06:47:18 +0000 | [diff] [blame] | 541 | if (!IsRTCheckAnalysisNeeded) return true; |
Silviu Baranga | 98a1371 | 2015-06-08 10:27:06 +0000 | [diff] [blame] | 542 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 543 | bool IsDepCheckNeeded = isDependencyCheckNeeded(); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 544 | |
| 545 | // We assign a consecutive id to access from different alias sets. |
| 546 | // Accesses between different groups doesn't need to be checked. |
| 547 | unsigned ASId = 1; |
| 548 | for (auto &AS : AST) { |
Adam Nemet | 424edc6 | 2015-07-08 22:58:48 +0000 | [diff] [blame] | 549 | int NumReadPtrChecks = 0; |
| 550 | int NumWritePtrChecks = 0; |
| 551 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 552 | // We assign consecutive id to access from different dependence sets. |
| 553 | // Accesses within the same set don't need a runtime check. |
| 554 | unsigned RunningDepId = 1; |
| 555 | DenseMap<Value *, unsigned> DepSetId; |
| 556 | |
| 557 | for (auto A : AS) { |
| 558 | Value *Ptr = A.getValue(); |
| 559 | bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true)); |
| 560 | MemAccessInfo Access(Ptr, IsWrite); |
| 561 | |
Adam Nemet | 424edc6 | 2015-07-08 22:58:48 +0000 | [diff] [blame] | 562 | if (IsWrite) |
| 563 | ++NumWritePtrChecks; |
| 564 | else |
| 565 | ++NumReadPtrChecks; |
| 566 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 567 | if (hasComputableBounds(SE, StridesMap, Ptr) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 568 | // When we run after a failing dependency check we have to make sure |
| 569 | // we don't have wrapping pointers. |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 570 | (!ShouldCheckStride || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 571 | isStridedPtr(SE, Ptr, TheLoop, StridesMap) == 1)) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 572 | // The id of the dependence set. |
| 573 | unsigned DepId; |
| 574 | |
| 575 | if (IsDepCheckNeeded) { |
| 576 | Value *Leader = DepCands.getLeaderValue(Access).getPointer(); |
| 577 | unsigned &LeaderId = DepSetId[Leader]; |
| 578 | if (!LeaderId) |
| 579 | LeaderId = RunningDepId++; |
| 580 | DepId = LeaderId; |
| 581 | } else |
| 582 | // Each access has its own dependence set. |
| 583 | DepId = RunningDepId++; |
| 584 | |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 585 | RtCheck.insert(TheLoop, Ptr, IsWrite, DepId, ASId, StridesMap); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 586 | |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 587 | DEBUG(dbgs() << "LAA: Found a runtime check ptr:" << *Ptr << '\n'); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 588 | } else { |
Adam Nemet | f10ca27 | 2015-05-18 15:36:52 +0000 | [diff] [blame] | 589 | DEBUG(dbgs() << "LAA: Can't find bounds for ptr:" << *Ptr << '\n'); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 590 | CanDoRT = false; |
| 591 | } |
| 592 | } |
| 593 | |
Adam Nemet | 424edc6 | 2015-07-08 22:58:48 +0000 | [diff] [blame] | 594 | // If we have at least two writes or one write and a read then we need to |
| 595 | // check them. But there is no need to checks if there is only one |
| 596 | // dependence set for this alias set. |
| 597 | // |
| 598 | // Note that this function computes CanDoRT and NeedRTCheck independently. |
| 599 | // For example CanDoRT=false, NeedRTCheck=false means that we have a pointer |
| 600 | // for which we couldn't find the bounds but we don't actually need to emit |
| 601 | // any checks so it does not matter. |
| 602 | if (!(IsDepCheckNeeded && CanDoRT && RunningDepId == 2)) |
| 603 | NeedRTCheck |= (NumWritePtrChecks >= 2 || (NumReadPtrChecks >= 1 && |
| 604 | NumWritePtrChecks >= 1)); |
| 605 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 606 | ++ASId; |
| 607 | } |
| 608 | |
| 609 | // If the pointers that we would use for the bounds comparison have different |
| 610 | // address spaces, assume the values aren't directly comparable, so we can't |
| 611 | // use them for the runtime check. We also have to assume they could |
| 612 | // overlap. In the future there should be metadata for whether address spaces |
| 613 | // are disjoint. |
| 614 | unsigned NumPointers = RtCheck.Pointers.size(); |
| 615 | for (unsigned i = 0; i < NumPointers; ++i) { |
| 616 | for (unsigned j = i + 1; j < NumPointers; ++j) { |
| 617 | // Only need to check pointers between two different dependency sets. |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 618 | if (RtCheck.Pointers[i].DependencySetId == |
| 619 | RtCheck.Pointers[j].DependencySetId) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 620 | continue; |
| 621 | // Only need to check pointers in the same alias set. |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 622 | if (RtCheck.Pointers[i].AliasSetId != RtCheck.Pointers[j].AliasSetId) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 623 | continue; |
| 624 | |
Adam Nemet | 9f7dedc | 2015-07-14 22:32:50 +0000 | [diff] [blame] | 625 | Value *PtrI = RtCheck.Pointers[i].PointerValue; |
| 626 | Value *PtrJ = RtCheck.Pointers[j].PointerValue; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 627 | |
| 628 | unsigned ASi = PtrI->getType()->getPointerAddressSpace(); |
| 629 | unsigned ASj = PtrJ->getType()->getPointerAddressSpace(); |
| 630 | if (ASi != ASj) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 631 | DEBUG(dbgs() << "LAA: Runtime check would require comparison between" |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 632 | " different address spaces\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 633 | return false; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 638 | if (NeedRTCheck && CanDoRT) |
| 639 | RtCheck.groupChecks(DepCands, IsDepCheckNeeded); |
| 640 | |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 641 | DEBUG(dbgs() << "LAA: We need to do " << RtCheck.getNumberOfChecks(nullptr) |
| 642 | << " pointer comparisons.\n"); |
| 643 | |
| 644 | RtCheck.Need = NeedRTCheck; |
| 645 | |
| 646 | bool CanDoRTIfNeeded = !NeedRTCheck || CanDoRT; |
| 647 | if (!CanDoRTIfNeeded) |
| 648 | RtCheck.reset(); |
| 649 | return CanDoRTIfNeeded; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | void AccessAnalysis::processMemAccesses() { |
| 653 | // We process the set twice: first we process read-write pointers, last we |
| 654 | // process read-only pointers. This allows us to skip dependence tests for |
| 655 | // read-only pointers. |
| 656 | |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 657 | DEBUG(dbgs() << "LAA: Processing memory accesses...\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 658 | DEBUG(dbgs() << " AST: "; AST.dump()); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 659 | DEBUG(dbgs() << "LAA: Accesses(" << Accesses.size() << "):\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 660 | DEBUG({ |
| 661 | for (auto A : Accesses) |
| 662 | dbgs() << "\t" << *A.getPointer() << " (" << |
| 663 | (A.getInt() ? "write" : (ReadOnlyPtr.count(A.getPointer()) ? |
| 664 | "read-only" : "read")) << ")\n"; |
| 665 | }); |
| 666 | |
| 667 | // The AliasSetTracker has nicely partitioned our pointers by metadata |
| 668 | // compatibility and potential for underlying-object overlap. As a result, we |
| 669 | // only need to check for potential pointer dependencies within each alias |
| 670 | // set. |
| 671 | for (auto &AS : AST) { |
| 672 | // Note that both the alias-set tracker and the alias sets themselves used |
| 673 | // linked lists internally and so the iteration order here is deterministic |
| 674 | // (matching the original instruction order within each set). |
| 675 | |
| 676 | bool SetHasWrite = false; |
| 677 | |
| 678 | // Map of pointers to last access encountered. |
| 679 | typedef DenseMap<Value*, MemAccessInfo> UnderlyingObjToAccessMap; |
| 680 | UnderlyingObjToAccessMap ObjToLastAccess; |
| 681 | |
| 682 | // Set of access to check after all writes have been processed. |
| 683 | PtrAccessSet DeferredAccesses; |
| 684 | |
| 685 | // Iterate over each alias set twice, once to process read/write pointers, |
| 686 | // and then to process read-only pointers. |
| 687 | for (int SetIteration = 0; SetIteration < 2; ++SetIteration) { |
| 688 | bool UseDeferred = SetIteration > 0; |
| 689 | PtrAccessSet &S = UseDeferred ? DeferredAccesses : Accesses; |
| 690 | |
| 691 | for (auto AV : AS) { |
| 692 | Value *Ptr = AV.getValue(); |
| 693 | |
| 694 | // For a single memory access in AliasSetTracker, Accesses may contain |
| 695 | // both read and write, and they both need to be handled for CheckDeps. |
| 696 | for (auto AC : S) { |
| 697 | if (AC.getPointer() != Ptr) |
| 698 | continue; |
| 699 | |
| 700 | bool IsWrite = AC.getInt(); |
| 701 | |
| 702 | // If we're using the deferred access set, then it contains only |
| 703 | // reads. |
| 704 | bool IsReadOnlyPtr = ReadOnlyPtr.count(Ptr) && !IsWrite; |
| 705 | if (UseDeferred && !IsReadOnlyPtr) |
| 706 | continue; |
| 707 | // Otherwise, the pointer must be in the PtrAccessSet, either as a |
| 708 | // read or a write. |
| 709 | assert(((IsReadOnlyPtr && UseDeferred) || IsWrite || |
| 710 | S.count(MemAccessInfo(Ptr, false))) && |
| 711 | "Alias-set pointer not in the access set?"); |
| 712 | |
| 713 | MemAccessInfo Access(Ptr, IsWrite); |
| 714 | DepCands.insert(Access); |
| 715 | |
| 716 | // Memorize read-only pointers for later processing and skip them in |
| 717 | // the first round (they need to be checked after we have seen all |
| 718 | // write pointers). Note: we also mark pointer that are not |
| 719 | // consecutive as "read-only" pointers (so that we check |
| 720 | // "a[b[i]] +="). Hence, we need the second check for "!IsWrite". |
| 721 | if (!UseDeferred && IsReadOnlyPtr) { |
| 722 | DeferredAccesses.insert(Access); |
| 723 | continue; |
| 724 | } |
| 725 | |
| 726 | // If this is a write - check other reads and writes for conflicts. If |
| 727 | // this is a read only check other writes for conflicts (but only if |
| 728 | // there is no other write to the ptr - this is an optimization to |
| 729 | // catch "a[i] = a[i] + " without having to do a dependence check). |
| 730 | if ((IsWrite || IsReadOnlyPtr) && SetHasWrite) { |
| 731 | CheckDeps.insert(Access); |
Adam Nemet | 5dc3b2c | 2015-07-09 06:47:18 +0000 | [diff] [blame] | 732 | IsRTCheckAnalysisNeeded = true; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (IsWrite) |
| 736 | SetHasWrite = true; |
| 737 | |
| 738 | // Create sets of pointers connected by a shared alias set and |
| 739 | // underlying object. |
| 740 | typedef SmallVector<Value *, 16> ValueVector; |
| 741 | ValueVector TempObjects; |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 742 | |
| 743 | GetUnderlyingObjects(Ptr, TempObjects, DL, LI); |
| 744 | DEBUG(dbgs() << "Underlying objects for pointer " << *Ptr << "\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 745 | for (Value *UnderlyingObj : TempObjects) { |
| 746 | UnderlyingObjToAccessMap::iterator Prev = |
| 747 | ObjToLastAccess.find(UnderlyingObj); |
| 748 | if (Prev != ObjToLastAccess.end()) |
| 749 | DepCands.unionSets(Access, Prev->second); |
| 750 | |
| 751 | ObjToLastAccess[UnderlyingObj] = Access; |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 752 | DEBUG(dbgs() << " " << *UnderlyingObj << "\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 760 | static bool isInBoundsGep(Value *Ptr) { |
| 761 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) |
| 762 | return GEP->isInBounds(); |
| 763 | return false; |
| 764 | } |
| 765 | |
Adam Nemet | c4866d2 | 2015-06-26 17:25:43 +0000 | [diff] [blame] | 766 | /// \brief Return true if an AddRec pointer \p Ptr is unsigned non-wrapping, |
| 767 | /// i.e. monotonically increasing/decreasing. |
| 768 | static bool isNoWrapAddRec(Value *Ptr, const SCEVAddRecExpr *AR, |
| 769 | ScalarEvolution *SE, const Loop *L) { |
| 770 | // FIXME: This should probably only return true for NUW. |
| 771 | if (AR->getNoWrapFlags(SCEV::NoWrapMask)) |
| 772 | return true; |
| 773 | |
| 774 | // Scalar evolution does not propagate the non-wrapping flags to values that |
| 775 | // are derived from a non-wrapping induction variable because non-wrapping |
| 776 | // could be flow-sensitive. |
| 777 | // |
| 778 | // Look through the potentially overflowing instruction to try to prove |
| 779 | // non-wrapping for the *specific* value of Ptr. |
| 780 | |
| 781 | // The arithmetic implied by an inbounds GEP can't overflow. |
| 782 | auto *GEP = dyn_cast<GetElementPtrInst>(Ptr); |
| 783 | if (!GEP || !GEP->isInBounds()) |
| 784 | return false; |
| 785 | |
| 786 | // Make sure there is only one non-const index and analyze that. |
| 787 | Value *NonConstIndex = nullptr; |
| 788 | for (auto Index = GEP->idx_begin(); Index != GEP->idx_end(); ++Index) |
| 789 | if (!isa<ConstantInt>(*Index)) { |
| 790 | if (NonConstIndex) |
| 791 | return false; |
| 792 | NonConstIndex = *Index; |
| 793 | } |
| 794 | if (!NonConstIndex) |
| 795 | // The recurrence is on the pointer, ignore for now. |
| 796 | return false; |
| 797 | |
| 798 | // The index in GEP is signed. It is non-wrapping if it's derived from a NSW |
| 799 | // AddRec using a NSW operation. |
| 800 | if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(NonConstIndex)) |
| 801 | if (OBO->hasNoSignedWrap() && |
| 802 | // Assume constant for other the operand so that the AddRec can be |
| 803 | // easily found. |
| 804 | isa<ConstantInt>(OBO->getOperand(1))) { |
| 805 | auto *OpScev = SE->getSCEV(OBO->getOperand(0)); |
| 806 | |
| 807 | if (auto *OpAR = dyn_cast<SCEVAddRecExpr>(OpScev)) |
| 808 | return OpAR->getLoop() == L && OpAR->getNoWrapFlags(SCEV::FlagNSW); |
| 809 | } |
| 810 | |
| 811 | return false; |
| 812 | } |
| 813 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 814 | /// \brief Check whether the access through \p Ptr has a constant stride. |
Hao Liu | 32c0539 | 2015-06-08 06:39:56 +0000 | [diff] [blame] | 815 | int llvm::isStridedPtr(ScalarEvolution *SE, Value *Ptr, const Loop *Lp, |
| 816 | const ValueToValueMap &StridesMap) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 817 | const Type *Ty = Ptr->getType(); |
| 818 | assert(Ty->isPointerTy() && "Unexpected non-ptr"); |
| 819 | |
| 820 | // Make sure that the pointer does not point to aggregate types. |
| 821 | const PointerType *PtrTy = cast<PointerType>(Ty); |
| 822 | if (PtrTy->getElementType()->isAggregateType()) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 823 | DEBUG(dbgs() << "LAA: Bad stride - Not a pointer to a scalar type" |
| 824 | << *Ptr << "\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, StridesMap, Ptr); |
| 829 | |
| 830 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev); |
| 831 | if (!AR) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 832 | DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer " |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 833 | << *Ptr << " SCEV: " << *PtrScev << "\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | // The accesss function must stride over the innermost loop. |
| 838 | if (Lp != AR->getLoop()) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 839 | DEBUG(dbgs() << "LAA: Bad stride - Not striding over innermost loop " << |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 840 | *Ptr << " SCEV: " << *PtrScev << "\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | // The address calculation must not wrap. Otherwise, a dependence could be |
| 844 | // inverted. |
| 845 | // An inbounds getelementptr that is a AddRec with a unit stride |
| 846 | // cannot wrap per definition. The unit stride requirement is checked later. |
| 847 | // An getelementptr without an inbounds attribute and unit stride would have |
| 848 | // to access the pointer value "0" which is undefined behavior in address |
| 849 | // space 0, therefore we can also vectorize this case. |
| 850 | bool IsInBoundsGEP = isInBoundsGep(Ptr); |
Adam Nemet | c4866d2 | 2015-06-26 17:25:43 +0000 | [diff] [blame] | 851 | bool IsNoWrapAddRec = isNoWrapAddRec(Ptr, AR, SE, Lp); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 852 | bool IsInAddressSpaceZero = PtrTy->getAddressSpace() == 0; |
| 853 | if (!IsNoWrapAddRec && !IsInBoundsGEP && !IsInAddressSpaceZero) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 854 | DEBUG(dbgs() << "LAA: Bad stride - Pointer may wrap in the address space " |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 855 | << *Ptr << " SCEV: " << *PtrScev << "\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | // Check the step is constant. |
| 860 | const SCEV *Step = AR->getStepRecurrence(*SE); |
| 861 | |
Adam Nemet | 943befe | 2015-07-09 00:03:22 +0000 | [diff] [blame] | 862 | // Calculate the pointer stride and check if it is constant. |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 863 | const SCEVConstant *C = dyn_cast<SCEVConstant>(Step); |
| 864 | if (!C) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 865 | DEBUG(dbgs() << "LAA: Bad stride - Not a constant strided " << *Ptr << |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 866 | " SCEV: " << *PtrScev << "\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 867 | return 0; |
| 868 | } |
| 869 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 870 | auto &DL = Lp->getHeader()->getModule()->getDataLayout(); |
| 871 | int64_t Size = DL.getTypeAllocSize(PtrTy->getElementType()); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 872 | const APInt &APStepVal = C->getValue()->getValue(); |
| 873 | |
| 874 | // Huge step value - give up. |
| 875 | if (APStepVal.getBitWidth() > 64) |
| 876 | return 0; |
| 877 | |
| 878 | int64_t StepVal = APStepVal.getSExtValue(); |
| 879 | |
| 880 | // Strided access. |
| 881 | int64_t Stride = StepVal / Size; |
| 882 | int64_t Rem = StepVal % Size; |
| 883 | if (Rem) |
| 884 | return 0; |
| 885 | |
| 886 | // If the SCEV could wrap but we have an inbounds gep with a unit stride we |
| 887 | // know we can't "wrap around the address space". In case of address space |
| 888 | // zero we know that this won't happen without triggering undefined behavior. |
| 889 | if (!IsNoWrapAddRec && (IsInBoundsGEP || IsInAddressSpaceZero) && |
| 890 | Stride != 1 && Stride != -1) |
| 891 | return 0; |
| 892 | |
| 893 | return Stride; |
| 894 | } |
| 895 | |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 896 | bool MemoryDepChecker::Dependence::isSafeForVectorization(DepType Type) { |
| 897 | switch (Type) { |
| 898 | case NoDep: |
| 899 | case Forward: |
| 900 | case BackwardVectorizable: |
| 901 | return true; |
| 902 | |
| 903 | case Unknown: |
| 904 | case ForwardButPreventsForwarding: |
| 905 | case Backward: |
| 906 | case BackwardVectorizableButPreventsForwarding: |
| 907 | return false; |
| 908 | } |
David Majnemer | d388e93 | 2015-03-10 20:23:29 +0000 | [diff] [blame] | 909 | llvm_unreachable("unexpected DepType!"); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | bool MemoryDepChecker::Dependence::isInterestingDependence(DepType Type) { |
| 913 | switch (Type) { |
| 914 | case NoDep: |
| 915 | case Forward: |
| 916 | return false; |
| 917 | |
| 918 | case BackwardVectorizable: |
| 919 | case Unknown: |
| 920 | case ForwardButPreventsForwarding: |
| 921 | case Backward: |
| 922 | case BackwardVectorizableButPreventsForwarding: |
| 923 | return true; |
| 924 | } |
David Majnemer | d388e93 | 2015-03-10 20:23:29 +0000 | [diff] [blame] | 925 | llvm_unreachable("unexpected DepType!"); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | bool MemoryDepChecker::Dependence::isPossiblyBackward() const { |
| 929 | switch (Type) { |
| 930 | case NoDep: |
| 931 | case Forward: |
| 932 | case ForwardButPreventsForwarding: |
| 933 | return false; |
| 934 | |
| 935 | case Unknown: |
| 936 | case BackwardVectorizable: |
| 937 | case Backward: |
| 938 | case BackwardVectorizableButPreventsForwarding: |
| 939 | return true; |
| 940 | } |
David Majnemer | d388e93 | 2015-03-10 20:23:29 +0000 | [diff] [blame] | 941 | llvm_unreachable("unexpected DepType!"); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 944 | bool MemoryDepChecker::couldPreventStoreLoadForward(unsigned Distance, |
| 945 | unsigned TypeByteSize) { |
| 946 | // If loads occur at a distance that is not a multiple of a feasible vector |
| 947 | // factor store-load forwarding does not take place. |
| 948 | // Positive dependences might cause troubles because vectorizing them might |
| 949 | // prevent store-load forwarding making vectorized code run a lot slower. |
| 950 | // a[i] = a[i-3] ^ a[i-8]; |
| 951 | // The stores to a[i:i+1] don't align with the stores to a[i-3:i-2] and |
| 952 | // hence on your typical architecture store-load forwarding does not take |
| 953 | // place. Vectorizing in such cases does not make sense. |
| 954 | // Store-load forwarding distance. |
| 955 | const unsigned NumCyclesForStoreLoadThroughMemory = 8*TypeByteSize; |
| 956 | // Maximum vector factor. |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 957 | unsigned MaxVFWithoutSLForwardIssues = |
| 958 | VectorizerParams::MaxVectorWidth * TypeByteSize; |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 959 | if(MaxSafeDepDistBytes < MaxVFWithoutSLForwardIssues) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 960 | MaxVFWithoutSLForwardIssues = MaxSafeDepDistBytes; |
| 961 | |
| 962 | for (unsigned vf = 2*TypeByteSize; vf <= MaxVFWithoutSLForwardIssues; |
| 963 | vf *= 2) { |
| 964 | if (Distance % vf && Distance / vf < NumCyclesForStoreLoadThroughMemory) { |
| 965 | MaxVFWithoutSLForwardIssues = (vf >>=1); |
| 966 | break; |
| 967 | } |
| 968 | } |
| 969 | |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 970 | if (MaxVFWithoutSLForwardIssues< 2*TypeByteSize) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 971 | DEBUG(dbgs() << "LAA: Distance " << Distance << |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 972 | " that could cause a store-load forwarding conflict\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
| 976 | if (MaxVFWithoutSLForwardIssues < MaxSafeDepDistBytes && |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 977 | MaxVFWithoutSLForwardIssues != |
| 978 | VectorizerParams::MaxVectorWidth * TypeByteSize) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 979 | MaxSafeDepDistBytes = MaxVFWithoutSLForwardIssues; |
| 980 | return false; |
| 981 | } |
| 982 | |
Hao Liu | 751004a | 2015-06-08 04:48:37 +0000 | [diff] [blame] | 983 | /// \brief Check the dependence for two accesses with the same stride \p Stride. |
| 984 | /// \p Distance is the positive distance and \p TypeByteSize is type size in |
| 985 | /// bytes. |
| 986 | /// |
| 987 | /// \returns true if they are independent. |
| 988 | static bool areStridedAccessesIndependent(unsigned Distance, unsigned Stride, |
| 989 | unsigned TypeByteSize) { |
| 990 | assert(Stride > 1 && "The stride must be greater than 1"); |
| 991 | assert(TypeByteSize > 0 && "The type size in byte must be non-zero"); |
| 992 | assert(Distance > 0 && "The distance must be non-zero"); |
| 993 | |
| 994 | // Skip if the distance is not multiple of type byte size. |
| 995 | if (Distance % TypeByteSize) |
| 996 | return false; |
| 997 | |
| 998 | unsigned ScaledDist = Distance / TypeByteSize; |
| 999 | |
| 1000 | // No dependence if the scaled distance is not multiple of the stride. |
| 1001 | // E.g. |
| 1002 | // for (i = 0; i < 1024 ; i += 4) |
| 1003 | // A[i+2] = A[i] + 1; |
| 1004 | // |
| 1005 | // Two accesses in memory (scaled distance is 2, stride is 4): |
| 1006 | // | A[0] | | | | A[4] | | | | |
| 1007 | // | | | A[2] | | | | A[6] | | |
| 1008 | // |
| 1009 | // E.g. |
| 1010 | // for (i = 0; i < 1024 ; i += 3) |
| 1011 | // A[i+4] = A[i] + 1; |
| 1012 | // |
| 1013 | // Two accesses in memory (scaled distance is 4, stride is 3): |
| 1014 | // | A[0] | | | A[3] | | | A[6] | | | |
| 1015 | // | | | | | A[4] | | | A[7] | | |
| 1016 | return ScaledDist % Stride; |
| 1017 | } |
| 1018 | |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1019 | MemoryDepChecker::Dependence::DepType |
| 1020 | MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx, |
| 1021 | const MemAccessInfo &B, unsigned BIdx, |
| 1022 | const ValueToValueMap &Strides) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1023 | assert (AIdx < BIdx && "Must pass arguments in program order"); |
| 1024 | |
| 1025 | Value *APtr = A.getPointer(); |
| 1026 | Value *BPtr = B.getPointer(); |
| 1027 | bool AIsWrite = A.getInt(); |
| 1028 | bool BIsWrite = B.getInt(); |
| 1029 | |
| 1030 | // Two reads are independent. |
| 1031 | if (!AIsWrite && !BIsWrite) |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1032 | return Dependence::NoDep; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1033 | |
| 1034 | // We cannot check pointers in different address spaces. |
| 1035 | if (APtr->getType()->getPointerAddressSpace() != |
| 1036 | BPtr->getType()->getPointerAddressSpace()) |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1037 | return Dependence::Unknown; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1038 | |
| 1039 | const SCEV *AScev = replaceSymbolicStrideSCEV(SE, Strides, APtr); |
| 1040 | const SCEV *BScev = replaceSymbolicStrideSCEV(SE, Strides, BPtr); |
| 1041 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1042 | int StrideAPtr = isStridedPtr(SE, APtr, InnermostLoop, Strides); |
| 1043 | int StrideBPtr = isStridedPtr(SE, BPtr, InnermostLoop, Strides); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1044 | |
| 1045 | const SCEV *Src = AScev; |
| 1046 | const SCEV *Sink = BScev; |
| 1047 | |
| 1048 | // If the induction step is negative we have to invert source and sink of the |
| 1049 | // dependence. |
| 1050 | if (StrideAPtr < 0) { |
| 1051 | //Src = BScev; |
| 1052 | //Sink = AScev; |
| 1053 | std::swap(APtr, BPtr); |
| 1054 | std::swap(Src, Sink); |
| 1055 | std::swap(AIsWrite, BIsWrite); |
| 1056 | std::swap(AIdx, BIdx); |
| 1057 | std::swap(StrideAPtr, StrideBPtr); |
| 1058 | } |
| 1059 | |
| 1060 | const SCEV *Dist = SE->getMinusSCEV(Sink, Src); |
| 1061 | |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1062 | DEBUG(dbgs() << "LAA: Src Scev: " << *Src << "Sink Scev: " << *Sink |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 1063 | << "(Induction step: " << StrideAPtr << ")\n"); |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1064 | DEBUG(dbgs() << "LAA: Distance for " << *InstMap[AIdx] << " to " |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 1065 | << *InstMap[BIdx] << ": " << *Dist << "\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1066 | |
Adam Nemet | 943befe | 2015-07-09 00:03:22 +0000 | [diff] [blame] | 1067 | // Need accesses with constant stride. We don't want to vectorize |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1068 | // "A[B[i]] += ..." and similar code or pointer arithmetic that could wrap in |
| 1069 | // the address space. |
| 1070 | if (!StrideAPtr || !StrideBPtr || StrideAPtr != StrideBPtr){ |
Adam Nemet | 943befe | 2015-07-09 00:03:22 +0000 | [diff] [blame] | 1071 | DEBUG(dbgs() << "Pointer access with non-constant stride\n"); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1072 | return Dependence::Unknown; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | const SCEVConstant *C = dyn_cast<SCEVConstant>(Dist); |
| 1076 | if (!C) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1077 | DEBUG(dbgs() << "LAA: Dependence because of non-constant distance\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1078 | ShouldRetryWithRuntimeCheck = true; |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1079 | return Dependence::Unknown; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | Type *ATy = APtr->getType()->getPointerElementType(); |
| 1083 | Type *BTy = BPtr->getType()->getPointerElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1084 | auto &DL = InnermostLoop->getHeader()->getModule()->getDataLayout(); |
| 1085 | unsigned TypeByteSize = DL.getTypeAllocSize(ATy); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1086 | |
| 1087 | // Negative distances are not plausible dependencies. |
| 1088 | const APInt &Val = C->getValue()->getValue(); |
| 1089 | if (Val.isNegative()) { |
| 1090 | bool IsTrueDataDependence = (AIsWrite && !BIsWrite); |
| 1091 | if (IsTrueDataDependence && |
| 1092 | (couldPreventStoreLoadForward(Val.abs().getZExtValue(), TypeByteSize) || |
| 1093 | ATy != BTy)) |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1094 | return Dependence::ForwardButPreventsForwarding; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1095 | |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1096 | DEBUG(dbgs() << "LAA: Dependence is negative: NoDep\n"); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1097 | return Dependence::Forward; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | // Write to the same location with the same size. |
| 1101 | // Could be improved to assert type sizes are the same (i32 == float, etc). |
| 1102 | if (Val == 0) { |
| 1103 | if (ATy == BTy) |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1104 | return Dependence::NoDep; |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1105 | DEBUG(dbgs() << "LAA: Zero dependence difference but different types\n"); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1106 | return Dependence::Unknown; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | assert(Val.isStrictlyPositive() && "Expect a positive value"); |
| 1110 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1111 | if (ATy != BTy) { |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 1112 | DEBUG(dbgs() << |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1113 | "LAA: ReadWrite-Write positive dependency with different types\n"); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1114 | return Dependence::Unknown; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | unsigned Distance = (unsigned) Val.getZExtValue(); |
| 1118 | |
Hao Liu | 751004a | 2015-06-08 04:48:37 +0000 | [diff] [blame] | 1119 | unsigned Stride = std::abs(StrideAPtr); |
| 1120 | if (Stride > 1 && |
Adam Nemet | 0131a56 | 2015-07-08 18:47:38 +0000 | [diff] [blame] | 1121 | areStridedAccessesIndependent(Distance, Stride, TypeByteSize)) { |
| 1122 | DEBUG(dbgs() << "LAA: Strided accesses are independent\n"); |
Hao Liu | 751004a | 2015-06-08 04:48:37 +0000 | [diff] [blame] | 1123 | return Dependence::NoDep; |
Adam Nemet | 0131a56 | 2015-07-08 18:47:38 +0000 | [diff] [blame] | 1124 | } |
Hao Liu | 751004a | 2015-06-08 04:48:37 +0000 | [diff] [blame] | 1125 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1126 | // Bail out early if passed-in parameters make vectorization not feasible. |
Adam Nemet | f219c64 | 2015-02-19 19:14:52 +0000 | [diff] [blame] | 1127 | unsigned ForcedFactor = (VectorizerParams::VectorizationFactor ? |
| 1128 | VectorizerParams::VectorizationFactor : 1); |
| 1129 | unsigned ForcedUnroll = (VectorizerParams::VectorizationInterleave ? |
| 1130 | VectorizerParams::VectorizationInterleave : 1); |
Hao Liu | 751004a | 2015-06-08 04:48:37 +0000 | [diff] [blame] | 1131 | // The minimum number of iterations for a vectorized/unrolled version. |
| 1132 | unsigned MinNumIter = std::max(ForcedFactor * ForcedUnroll, 2U); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1133 | |
Hao Liu | 751004a | 2015-06-08 04:48:37 +0000 | [diff] [blame] | 1134 | // It's not vectorizable if the distance is smaller than the minimum distance |
| 1135 | // needed for a vectroized/unrolled version. Vectorizing one iteration in |
| 1136 | // front needs TypeByteSize * Stride. Vectorizing the last iteration needs |
| 1137 | // TypeByteSize (No need to plus the last gap distance). |
| 1138 | // |
| 1139 | // E.g. Assume one char is 1 byte in memory and one int is 4 bytes. |
| 1140 | // foo(int *A) { |
| 1141 | // int *B = (int *)((char *)A + 14); |
| 1142 | // for (i = 0 ; i < 1024 ; i += 2) |
| 1143 | // B[i] = A[i] + 1; |
| 1144 | // } |
| 1145 | // |
| 1146 | // Two accesses in memory (stride is 2): |
| 1147 | // | A[0] | | A[2] | | A[4] | | A[6] | | |
| 1148 | // | B[0] | | B[2] | | B[4] | |
| 1149 | // |
| 1150 | // Distance needs for vectorizing iterations except the last iteration: |
| 1151 | // 4 * 2 * (MinNumIter - 1). Distance needs for the last iteration: 4. |
| 1152 | // So the minimum distance needed is: 4 * 2 * (MinNumIter - 1) + 4. |
| 1153 | // |
| 1154 | // If MinNumIter is 2, it is vectorizable as the minimum distance needed is |
| 1155 | // 12, which is less than distance. |
| 1156 | // |
| 1157 | // If MinNumIter is 4 (Say if a user forces the vectorization factor to be 4), |
| 1158 | // the minimum distance needed is 28, which is greater than distance. It is |
| 1159 | // not safe to do vectorization. |
| 1160 | unsigned MinDistanceNeeded = |
| 1161 | TypeByteSize * Stride * (MinNumIter - 1) + TypeByteSize; |
| 1162 | if (MinDistanceNeeded > Distance) { |
| 1163 | DEBUG(dbgs() << "LAA: Failure because of positive distance " << Distance |
| 1164 | << '\n'); |
| 1165 | return Dependence::Backward; |
| 1166 | } |
| 1167 | |
| 1168 | // Unsafe if the minimum distance needed is greater than max safe distance. |
| 1169 | if (MinDistanceNeeded > MaxSafeDepDistBytes) { |
| 1170 | DEBUG(dbgs() << "LAA: Failure because it needs at least " |
| 1171 | << MinDistanceNeeded << " size in bytes"); |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1172 | return Dependence::Backward; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Adam Nemet | 9cc0c39 | 2015-02-26 17:58:48 +0000 | [diff] [blame] | 1175 | // Positive distance bigger than max vectorization factor. |
Hao Liu | 751004a | 2015-06-08 04:48:37 +0000 | [diff] [blame] | 1176 | // FIXME: Should use max factor instead of max distance in bytes, which could |
| 1177 | // not handle different types. |
| 1178 | // E.g. Assume one char is 1 byte in memory and one int is 4 bytes. |
| 1179 | // void foo (int *A, char *B) { |
| 1180 | // for (unsigned i = 0; i < 1024; i++) { |
| 1181 | // A[i+2] = A[i] + 1; |
| 1182 | // B[i+2] = B[i] + 1; |
| 1183 | // } |
| 1184 | // } |
| 1185 | // |
| 1186 | // This case is currently unsafe according to the max safe distance. If we |
| 1187 | // analyze the two accesses on array B, the max safe dependence distance |
| 1188 | // is 2. Then we analyze the accesses on array A, the minimum distance needed |
| 1189 | // is 8, which is less than 2 and forbidden vectorization, But actually |
| 1190 | // both A and B could be vectorized by 2 iterations. |
| 1191 | MaxSafeDepDistBytes = |
| 1192 | Distance < MaxSafeDepDistBytes ? Distance : MaxSafeDepDistBytes; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1193 | |
| 1194 | bool IsTrueDataDependence = (!AIsWrite && BIsWrite); |
| 1195 | if (IsTrueDataDependence && |
| 1196 | couldPreventStoreLoadForward(Distance, TypeByteSize)) |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1197 | return Dependence::BackwardVectorizableButPreventsForwarding; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1198 | |
Hao Liu | 751004a | 2015-06-08 04:48:37 +0000 | [diff] [blame] | 1199 | DEBUG(dbgs() << "LAA: Positive distance " << Val.getSExtValue() |
| 1200 | << " with max VF = " |
| 1201 | << MaxSafeDepDistBytes / (TypeByteSize * Stride) << '\n'); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1202 | |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1203 | return Dependence::BackwardVectorizable; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Adam Nemet | dee666b | 2015-03-10 17:40:34 +0000 | [diff] [blame] | 1206 | bool MemoryDepChecker::areDepsSafe(DepCandidates &AccessSets, |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1207 | MemAccessInfoSet &CheckDeps, |
Adam Nemet | 8bc61df | 2015-02-24 00:41:59 +0000 | [diff] [blame] | 1208 | const ValueToValueMap &Strides) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1209 | |
| 1210 | MaxSafeDepDistBytes = -1U; |
| 1211 | while (!CheckDeps.empty()) { |
| 1212 | MemAccessInfo CurAccess = *CheckDeps.begin(); |
| 1213 | |
| 1214 | // Get the relevant memory access set. |
| 1215 | EquivalenceClasses<MemAccessInfo>::iterator I = |
| 1216 | AccessSets.findValue(AccessSets.getLeaderValue(CurAccess)); |
| 1217 | |
| 1218 | // Check accesses within this set. |
| 1219 | EquivalenceClasses<MemAccessInfo>::member_iterator AI, AE; |
| 1220 | AI = AccessSets.member_begin(I), AE = AccessSets.member_end(); |
| 1221 | |
| 1222 | // Check every access pair. |
| 1223 | while (AI != AE) { |
| 1224 | CheckDeps.erase(*AI); |
| 1225 | EquivalenceClasses<MemAccessInfo>::member_iterator OI = std::next(AI); |
| 1226 | while (OI != AE) { |
| 1227 | // Check every accessing instruction pair in program order. |
| 1228 | for (std::vector<unsigned>::iterator I1 = Accesses[*AI].begin(), |
| 1229 | I1E = Accesses[*AI].end(); I1 != I1E; ++I1) |
| 1230 | for (std::vector<unsigned>::iterator I2 = Accesses[*OI].begin(), |
| 1231 | I2E = Accesses[*OI].end(); I2 != I2E; ++I2) { |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1232 | auto A = std::make_pair(&*AI, *I1); |
| 1233 | auto B = std::make_pair(&*OI, *I2); |
| 1234 | |
| 1235 | assert(*I1 != *I2); |
| 1236 | if (*I1 > *I2) |
| 1237 | std::swap(A, B); |
| 1238 | |
| 1239 | Dependence::DepType Type = |
| 1240 | isDependent(*A.first, A.second, *B.first, B.second, Strides); |
| 1241 | SafeForVectorization &= Dependence::isSafeForVectorization(Type); |
| 1242 | |
| 1243 | // Gather dependences unless we accumulated MaxInterestingDependence |
| 1244 | // dependences. In that case return as soon as we find the first |
| 1245 | // unsafe dependence. This puts a limit on this quadratic |
| 1246 | // algorithm. |
| 1247 | if (RecordInterestingDependences) { |
| 1248 | if (Dependence::isInterestingDependence(Type)) |
| 1249 | InterestingDependences.push_back( |
| 1250 | Dependence(A.second, B.second, Type)); |
| 1251 | |
| 1252 | if (InterestingDependences.size() >= MaxInterestingDependence) { |
| 1253 | RecordInterestingDependences = false; |
| 1254 | InterestingDependences.clear(); |
| 1255 | DEBUG(dbgs() << "Too many dependences, stopped recording\n"); |
| 1256 | } |
| 1257 | } |
| 1258 | if (!RecordInterestingDependences && !SafeForVectorization) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1259 | return false; |
| 1260 | } |
| 1261 | ++OI; |
| 1262 | } |
| 1263 | AI++; |
| 1264 | } |
| 1265 | } |
Adam Nemet | 9c92657 | 2015-03-10 17:40:37 +0000 | [diff] [blame] | 1266 | |
| 1267 | DEBUG(dbgs() << "Total Interesting Dependences: " |
| 1268 | << InterestingDependences.size() << "\n"); |
| 1269 | return SafeForVectorization; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Adam Nemet | ec1e2bb | 2015-03-10 18:54:26 +0000 | [diff] [blame] | 1272 | SmallVector<Instruction *, 4> |
| 1273 | MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool isWrite) const { |
| 1274 | MemAccessInfo Access(Ptr, isWrite); |
| 1275 | auto &IndexVector = Accesses.find(Access)->second; |
| 1276 | |
| 1277 | SmallVector<Instruction *, 4> Insts; |
| 1278 | std::transform(IndexVector.begin(), IndexVector.end(), |
| 1279 | std::back_inserter(Insts), |
| 1280 | [&](unsigned Idx) { return this->InstMap[Idx]; }); |
| 1281 | return Insts; |
| 1282 | } |
| 1283 | |
Adam Nemet | 58913d6 | 2015-03-10 17:40:43 +0000 | [diff] [blame] | 1284 | const char *MemoryDepChecker::Dependence::DepName[] = { |
| 1285 | "NoDep", "Unknown", "Forward", "ForwardButPreventsForwarding", "Backward", |
| 1286 | "BackwardVectorizable", "BackwardVectorizableButPreventsForwarding"}; |
| 1287 | |
| 1288 | void MemoryDepChecker::Dependence::print( |
| 1289 | raw_ostream &OS, unsigned Depth, |
| 1290 | const SmallVectorImpl<Instruction *> &Instrs) const { |
| 1291 | OS.indent(Depth) << DepName[Type] << ":\n"; |
| 1292 | OS.indent(Depth + 2) << *Instrs[Source] << " -> \n"; |
| 1293 | OS.indent(Depth + 2) << *Instrs[Destination] << "\n"; |
| 1294 | } |
| 1295 | |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1296 | bool LoopAccessInfo::canAnalyzeLoop() { |
Adam Nemet | 8dcb3b6 | 2015-04-17 22:43:10 +0000 | [diff] [blame] | 1297 | // We need to have a loop header. |
| 1298 | DEBUG(dbgs() << "LAA: Found a loop: " << |
| 1299 | TheLoop->getHeader()->getName() << '\n'); |
| 1300 | |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1301 | // We can only analyze innermost loops. |
| 1302 | if (!TheLoop->empty()) { |
Adam Nemet | 8dcb3b6 | 2015-04-17 22:43:10 +0000 | [diff] [blame] | 1303 | DEBUG(dbgs() << "LAA: loop is not the innermost loop\n"); |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1304 | emitAnalysis(LoopAccessReport() << "loop is not the innermost loop"); |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1305 | return false; |
| 1306 | } |
| 1307 | |
| 1308 | // We must have a single backedge. |
| 1309 | if (TheLoop->getNumBackEdges() != 1) { |
Adam Nemet | 8dcb3b6 | 2015-04-17 22:43:10 +0000 | [diff] [blame] | 1310 | DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n"); |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1311 | emitAnalysis( |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1312 | LoopAccessReport() << |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1313 | "loop control flow is not understood by analyzer"); |
| 1314 | return false; |
| 1315 | } |
| 1316 | |
| 1317 | // We must have a single exiting block. |
| 1318 | if (!TheLoop->getExitingBlock()) { |
Adam Nemet | 8dcb3b6 | 2015-04-17 22:43:10 +0000 | [diff] [blame] | 1319 | DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n"); |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1320 | emitAnalysis( |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1321 | LoopAccessReport() << |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1322 | "loop control flow is not understood by analyzer"); |
| 1323 | return false; |
| 1324 | } |
| 1325 | |
| 1326 | // We only handle bottom-tested loops, i.e. loop in which the condition is |
| 1327 | // checked at the end of each iteration. With that we can assume that all |
| 1328 | // instructions in the loop are executed the same number of times. |
| 1329 | if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch()) { |
Adam Nemet | 8dcb3b6 | 2015-04-17 22:43:10 +0000 | [diff] [blame] | 1330 | DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n"); |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1331 | emitAnalysis( |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1332 | LoopAccessReport() << |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1333 | "loop control flow is not understood by analyzer"); |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1337 | // ScalarEvolution needs to be able to find the exit count. |
| 1338 | const SCEV *ExitCount = SE->getBackedgeTakenCount(TheLoop); |
| 1339 | if (ExitCount == SE->getCouldNotCompute()) { |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1340 | emitAnalysis(LoopAccessReport() << |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1341 | "could not determine number of loop iterations"); |
| 1342 | DEBUG(dbgs() << "LAA: SCEV could not compute the loop exit count.\n"); |
| 1343 | return false; |
| 1344 | } |
| 1345 | |
| 1346 | return true; |
| 1347 | } |
| 1348 | |
Adam Nemet | 8bc61df | 2015-02-24 00:41:59 +0000 | [diff] [blame] | 1349 | void LoopAccessInfo::analyzeLoop(const ValueToValueMap &Strides) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1350 | |
| 1351 | typedef SmallVector<Value*, 16> ValueVector; |
| 1352 | typedef SmallPtrSet<Value*, 16> ValueSet; |
| 1353 | |
| 1354 | // Holds the Load and Store *instructions*. |
| 1355 | ValueVector Loads; |
| 1356 | ValueVector Stores; |
| 1357 | |
| 1358 | // Holds all the different accesses in the loop. |
| 1359 | unsigned NumReads = 0; |
| 1360 | unsigned NumReadWrites = 0; |
| 1361 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 1362 | PtrRtChecking.Pointers.clear(); |
| 1363 | PtrRtChecking.Need = false; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1364 | |
| 1365 | const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel(); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1366 | |
| 1367 | // For each block. |
| 1368 | for (Loop::block_iterator bb = TheLoop->block_begin(), |
| 1369 | be = TheLoop->block_end(); bb != be; ++bb) { |
| 1370 | |
| 1371 | // Scan the BB and collect legal loads and stores. |
| 1372 | for (BasicBlock::iterator it = (*bb)->begin(), e = (*bb)->end(); it != e; |
| 1373 | ++it) { |
| 1374 | |
| 1375 | // If this is a load, save it. If this instruction can read from memory |
| 1376 | // but is not a load, then we quit. Notice that we don't handle function |
| 1377 | // calls that read or write. |
| 1378 | if (it->mayReadFromMemory()) { |
| 1379 | // Many math library functions read the rounding mode. We will only |
| 1380 | // vectorize a loop if it contains known function calls that don't set |
| 1381 | // the flag. Therefore, it is safe to ignore this read from memory. |
| 1382 | CallInst *Call = dyn_cast<CallInst>(it); |
| 1383 | if (Call && getIntrinsicIDForCall(Call, TLI)) |
| 1384 | continue; |
| 1385 | |
Michael Zolotukhin | 9b3cf60 | 2015-03-17 19:46:50 +0000 | [diff] [blame] | 1386 | // If the function has an explicit vectorized counterpart, we can safely |
| 1387 | // assume that it can be vectorized. |
| 1388 | if (Call && !Call->isNoBuiltin() && Call->getCalledFunction() && |
| 1389 | TLI->isFunctionVectorizable(Call->getCalledFunction()->getName())) |
| 1390 | continue; |
| 1391 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1392 | LoadInst *Ld = dyn_cast<LoadInst>(it); |
| 1393 | if (!Ld || (!Ld->isSimple() && !IsAnnotatedParallel)) { |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1394 | emitAnalysis(LoopAccessReport(Ld) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1395 | << "read with atomic ordering or volatile read"); |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1396 | DEBUG(dbgs() << "LAA: Found a non-simple load.\n"); |
Adam Nemet | 436018c | 2015-02-19 19:15:00 +0000 | [diff] [blame] | 1397 | CanVecMem = false; |
| 1398 | return; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1399 | } |
| 1400 | NumLoads++; |
| 1401 | Loads.push_back(Ld); |
| 1402 | DepChecker.addAccess(Ld); |
| 1403 | continue; |
| 1404 | } |
| 1405 | |
| 1406 | // Save 'store' instructions. Abort if other instructions write to memory. |
| 1407 | if (it->mayWriteToMemory()) { |
| 1408 | StoreInst *St = dyn_cast<StoreInst>(it); |
| 1409 | if (!St) { |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1410 | emitAnalysis(LoopAccessReport(it) << |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 1411 | "instruction cannot be vectorized"); |
Adam Nemet | 436018c | 2015-02-19 19:15:00 +0000 | [diff] [blame] | 1412 | CanVecMem = false; |
| 1413 | return; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1414 | } |
| 1415 | if (!St->isSimple() && !IsAnnotatedParallel) { |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1416 | emitAnalysis(LoopAccessReport(St) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1417 | << "write with atomic ordering or volatile write"); |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1418 | DEBUG(dbgs() << "LAA: Found a non-simple store.\n"); |
Adam Nemet | 436018c | 2015-02-19 19:15:00 +0000 | [diff] [blame] | 1419 | CanVecMem = false; |
| 1420 | return; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1421 | } |
| 1422 | NumStores++; |
| 1423 | Stores.push_back(St); |
| 1424 | DepChecker.addAccess(St); |
| 1425 | } |
| 1426 | } // Next instr. |
| 1427 | } // Next block. |
| 1428 | |
| 1429 | // Now we have two lists that hold the loads and the stores. |
| 1430 | // Next, we find the pointers that they use. |
| 1431 | |
| 1432 | // Check if we see any stores. If there are no stores, then we don't |
| 1433 | // care if the pointers are *restrict*. |
| 1434 | if (!Stores.size()) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1435 | DEBUG(dbgs() << "LAA: Found a read-only loop!\n"); |
Adam Nemet | 436018c | 2015-02-19 19:15:00 +0000 | [diff] [blame] | 1436 | CanVecMem = true; |
| 1437 | return; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Adam Nemet | dee666b | 2015-03-10 17:40:34 +0000 | [diff] [blame] | 1440 | MemoryDepChecker::DepCandidates DependentAccesses; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1441 | AccessAnalysis Accesses(TheLoop->getHeader()->getModule()->getDataLayout(), |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 1442 | AA, LI, DependentAccesses); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1443 | |
| 1444 | // Holds the analyzed pointers. We don't want to call GetUnderlyingObjects |
| 1445 | // multiple times on the same object. If the ptr is accessed twice, once |
| 1446 | // for read and once for write, it will only appear once (on the write |
| 1447 | // list). This is okay, since we are going to check for conflicts between |
| 1448 | // writes and between reads and writes, but not between reads and reads. |
| 1449 | ValueSet Seen; |
| 1450 | |
| 1451 | ValueVector::iterator I, IE; |
| 1452 | for (I = Stores.begin(), IE = Stores.end(); I != IE; ++I) { |
| 1453 | StoreInst *ST = cast<StoreInst>(*I); |
| 1454 | Value* Ptr = ST->getPointerOperand(); |
Adam Nemet | ce48250 | 2015-04-08 17:48:40 +0000 | [diff] [blame] | 1455 | // Check for store to loop invariant address. |
| 1456 | StoreToLoopInvariantAddress |= isUniform(Ptr); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1457 | // If we did *not* see this pointer before, insert it to the read-write |
| 1458 | // list. At this phase it is only a 'write' list. |
| 1459 | if (Seen.insert(Ptr).second) { |
| 1460 | ++NumReadWrites; |
| 1461 | |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1462 | MemoryLocation Loc = MemoryLocation::get(ST); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1463 | // The TBAA metadata could have a control dependency on the predication |
| 1464 | // condition, so we cannot rely on it when determining whether or not we |
| 1465 | // need runtime pointer checks. |
Adam Nemet | 01abb2c | 2015-02-18 03:43:19 +0000 | [diff] [blame] | 1466 | if (blockNeedsPredication(ST->getParent(), TheLoop, DT)) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1467 | Loc.AATags.TBAA = nullptr; |
| 1468 | |
| 1469 | Accesses.addStore(Loc); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | if (IsAnnotatedParallel) { |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 1474 | DEBUG(dbgs() |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1475 | << "LAA: A loop annotated parallel, ignore memory dependency " |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 1476 | << "checks.\n"); |
Adam Nemet | 436018c | 2015-02-19 19:15:00 +0000 | [diff] [blame] | 1477 | CanVecMem = true; |
| 1478 | return; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | for (I = Loads.begin(), IE = Loads.end(); I != IE; ++I) { |
| 1482 | LoadInst *LD = cast<LoadInst>(*I); |
| 1483 | Value* Ptr = LD->getPointerOperand(); |
| 1484 | // If we did *not* see this pointer before, insert it to the |
| 1485 | // read list. If we *did* see it before, then it is already in |
| 1486 | // the read-write list. This allows us to vectorize expressions |
| 1487 | // such as A[i] += x; Because the address of A[i] is a read-write |
| 1488 | // pointer. This only works if the index of A[i] is consecutive. |
| 1489 | // If the address of i is unknown (for example A[B[i]]) then we may |
| 1490 | // read a few words, modify, and write a few words, and some of the |
| 1491 | // words may be written to the same address. |
| 1492 | bool IsReadOnlyPtr = false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1493 | if (Seen.insert(Ptr).second || !isStridedPtr(SE, Ptr, TheLoop, Strides)) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1494 | ++NumReads; |
| 1495 | IsReadOnlyPtr = true; |
| 1496 | } |
| 1497 | |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1498 | MemoryLocation Loc = MemoryLocation::get(LD); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1499 | // The TBAA metadata could have a control dependency on the predication |
| 1500 | // condition, so we cannot rely on it when determining whether or not we |
| 1501 | // need runtime pointer checks. |
Adam Nemet | 01abb2c | 2015-02-18 03:43:19 +0000 | [diff] [blame] | 1502 | if (blockNeedsPredication(LD->getParent(), TheLoop, DT)) |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1503 | Loc.AATags.TBAA = nullptr; |
| 1504 | |
| 1505 | Accesses.addLoad(Loc, IsReadOnlyPtr); |
| 1506 | } |
| 1507 | |
| 1508 | // If we write (or read-write) to a single destination and there are no |
| 1509 | // other reads in this loop then is it safe to vectorize. |
| 1510 | if (NumReadWrites == 1 && NumReads == 0) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1511 | DEBUG(dbgs() << "LAA: Found a write-only loop!\n"); |
Adam Nemet | 436018c | 2015-02-19 19:15:00 +0000 | [diff] [blame] | 1512 | CanVecMem = true; |
| 1513 | return; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | // Build dependence sets and check whether we need a runtime pointer bounds |
| 1517 | // check. |
| 1518 | Accesses.buildDependenceSets(); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1519 | |
| 1520 | // Find pointers with computable bounds. We are going to use this information |
| 1521 | // to place a runtime bound check. |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 1522 | bool CanDoRTIfNeeded = |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 1523 | Accesses.canCheckPtrAtRT(PtrRtChecking, SE, TheLoop, Strides); |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 1524 | if (!CanDoRTIfNeeded) { |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1525 | emitAnalysis(LoopAccessReport() << "cannot identify array bounds"); |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 1526 | DEBUG(dbgs() << "LAA: We can't vectorize because we can't find " |
| 1527 | << "the array bounds.\n"); |
Adam Nemet | 436018c | 2015-02-19 19:15:00 +0000 | [diff] [blame] | 1528 | CanVecMem = false; |
| 1529 | return; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 1532 | DEBUG(dbgs() << "LAA: We can perform a memory runtime check if needed.\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1533 | |
Adam Nemet | 436018c | 2015-02-19 19:15:00 +0000 | [diff] [blame] | 1534 | CanVecMem = true; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1535 | if (Accesses.isDependencyCheckNeeded()) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1536 | DEBUG(dbgs() << "LAA: Checking memory dependencies\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1537 | CanVecMem = DepChecker.areDepsSafe( |
| 1538 | DependentAccesses, Accesses.getDependenciesToCheck(), Strides); |
| 1539 | MaxSafeDepDistBytes = DepChecker.getMaxSafeDepDistBytes(); |
| 1540 | |
| 1541 | if (!CanVecMem && DepChecker.shouldRetryWithRuntimeCheck()) { |
Adam Nemet | 339f42b | 2015-02-19 19:15:07 +0000 | [diff] [blame] | 1542 | DEBUG(dbgs() << "LAA: Retrying with memory checks\n"); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1543 | |
| 1544 | // Clear the dependency checks. We assume they are not needed. |
Adam Nemet | df3dc5b | 2015-05-18 15:37:03 +0000 | [diff] [blame] | 1545 | Accesses.resetDepChecks(DepChecker); |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1546 | |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 1547 | PtrRtChecking.reset(); |
| 1548 | PtrRtChecking.Need = true; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1549 | |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 1550 | CanDoRTIfNeeded = |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 1551 | Accesses.canCheckPtrAtRT(PtrRtChecking, SE, TheLoop, Strides, true); |
Silviu Baranga | 98a1371 | 2015-06-08 10:27:06 +0000 | [diff] [blame] | 1552 | |
Adam Nemet | 949e91a | 2015-03-10 19:12:41 +0000 | [diff] [blame] | 1553 | // Check that we found the bounds for the pointer. |
Adam Nemet | ee61474 | 2015-07-09 22:17:38 +0000 | [diff] [blame] | 1554 | if (!CanDoRTIfNeeded) { |
Adam Nemet | b6dc76f | 2015-03-10 18:54:19 +0000 | [diff] [blame] | 1555 | emitAnalysis(LoopAccessReport() |
| 1556 | << "cannot check memory dependencies at runtime"); |
| 1557 | DEBUG(dbgs() << "LAA: Can't vectorize with memory checks\n"); |
Adam Nemet | b6dc76f | 2015-03-10 18:54:19 +0000 | [diff] [blame] | 1558 | CanVecMem = false; |
| 1559 | return; |
| 1560 | } |
| 1561 | |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1562 | CanVecMem = true; |
| 1563 | } |
| 1564 | } |
| 1565 | |
Adam Nemet | 4bb90a7 | 2015-03-10 21:47:39 +0000 | [diff] [blame] | 1566 | if (CanVecMem) |
| 1567 | DEBUG(dbgs() << "LAA: No unsafe dependent memory operations in loop. We" |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 1568 | << (PtrRtChecking.Need ? "" : " don't") |
Adam Nemet | 0f67c6c | 2015-07-09 22:17:41 +0000 | [diff] [blame] | 1569 | << " need runtime memory checks.\n"); |
Adam Nemet | 4bb90a7 | 2015-03-10 21:47:39 +0000 | [diff] [blame] | 1570 | else { |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1571 | emitAnalysis(LoopAccessReport() << |
Adam Nemet | 04d4163 | 2015-02-19 19:14:34 +0000 | [diff] [blame] | 1572 | "unsafe dependent memory operations in loop"); |
Adam Nemet | 4bb90a7 | 2015-03-10 21:47:39 +0000 | [diff] [blame] | 1573 | DEBUG(dbgs() << "LAA: unsafe dependent memory operations in loop\n"); |
| 1574 | } |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Adam Nemet | 01abb2c | 2015-02-18 03:43:19 +0000 | [diff] [blame] | 1577 | bool LoopAccessInfo::blockNeedsPredication(BasicBlock *BB, Loop *TheLoop, |
| 1578 | DominatorTree *DT) { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1579 | assert(TheLoop->contains(BB) && "Unknown block used"); |
| 1580 | |
| 1581 | // Blocks that do not dominate the latch need predication. |
| 1582 | BasicBlock* Latch = TheLoop->getLoopLatch(); |
| 1583 | return !DT->dominates(BB, Latch); |
| 1584 | } |
| 1585 | |
Adam Nemet | 2bd6e98 | 2015-02-19 19:15:15 +0000 | [diff] [blame] | 1586 | void LoopAccessInfo::emitAnalysis(LoopAccessReport &Message) { |
Adam Nemet | c922853 | 2015-02-19 19:14:56 +0000 | [diff] [blame] | 1587 | assert(!Report && "Multiple reports generated"); |
| 1588 | Report = Message; |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Adam Nemet | 57ac766 | 2015-02-19 19:15:21 +0000 | [diff] [blame] | 1591 | bool LoopAccessInfo::isUniform(Value *V) const { |
Adam Nemet | 0456327 | 2015-02-01 16:56:15 +0000 | [diff] [blame] | 1592 | return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop)); |
| 1593 | } |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1594 | |
| 1595 | // FIXME: this function is currently a duplicate of the one in |
| 1596 | // LoopVectorize.cpp. |
| 1597 | static Instruction *getFirstInst(Instruction *FirstInst, Value *V, |
| 1598 | Instruction *Loc) { |
| 1599 | if (FirstInst) |
| 1600 | return FirstInst; |
| 1601 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 1602 | return I->getParent() == Loc->getParent() ? I : nullptr; |
| 1603 | return nullptr; |
| 1604 | } |
| 1605 | |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1606 | /// \brief IR Values for the lower and upper bounds of a pointer evolution. |
| 1607 | struct PointerBounds { |
| 1608 | Value *Start; |
| 1609 | Value *End; |
| 1610 | }; |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1611 | |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1612 | /// \brief Expand code for the lower and upper bound of the pointer group \p CG |
| 1613 | /// in \p TheLoop. \return the values for the bounds. |
| 1614 | static PointerBounds |
| 1615 | expandBounds(const RuntimePointerChecking::CheckingPtrGroup *CG, Loop *TheLoop, |
| 1616 | Instruction *Loc, SCEVExpander &Exp, ScalarEvolution *SE, |
| 1617 | const RuntimePointerChecking &PtrRtChecking) { |
| 1618 | Value *Ptr = PtrRtChecking.Pointers[CG->Members[0]].PointerValue; |
| 1619 | const SCEV *Sc = SE->getSCEV(Ptr); |
| 1620 | |
| 1621 | if (SE->isLoopInvariant(Sc, TheLoop)) { |
| 1622 | DEBUG(dbgs() << "LAA: Adding RT check for a loop invariant ptr:" << *Ptr |
| 1623 | << "\n"); |
| 1624 | return {Ptr, Ptr}; |
| 1625 | } else { |
| 1626 | unsigned AS = Ptr->getType()->getPointerAddressSpace(); |
| 1627 | LLVMContext &Ctx = Loc->getContext(); |
| 1628 | |
| 1629 | // Use this type for pointer arithmetic. |
| 1630 | Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS); |
| 1631 | Value *Start = nullptr, *End = nullptr; |
| 1632 | |
| 1633 | DEBUG(dbgs() << "LAA: Adding RT check for range:\n"); |
| 1634 | Start = Exp.expandCodeFor(CG->Low, PtrArithTy, Loc); |
| 1635 | End = Exp.expandCodeFor(CG->High, PtrArithTy, Loc); |
| 1636 | DEBUG(dbgs() << "Start: " << *CG->Low << " End: " << *CG->High << "\n"); |
| 1637 | return {Start, End}; |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | /// \brief Turns a collection of checks into a collection of expanded upper and |
| 1642 | /// lower bounds for both pointers in the check. |
| 1643 | static SmallVector<std::pair<PointerBounds, PointerBounds>, 4> expandBounds( |
| 1644 | const SmallVectorImpl<RuntimePointerChecking::PointerCheck> &PointerChecks, |
| 1645 | Loop *L, Instruction *Loc, ScalarEvolution *SE, SCEVExpander &Exp, |
| 1646 | const RuntimePointerChecking &PtrRtChecking) { |
| 1647 | SmallVector<std::pair<PointerBounds, PointerBounds>, 4> ChecksWithBounds; |
| 1648 | |
| 1649 | // Here we're relying on the SCEV Expander's cache to only emit code for the |
| 1650 | // same bounds once. |
| 1651 | std::transform( |
| 1652 | PointerChecks.begin(), PointerChecks.end(), |
| 1653 | std::back_inserter(ChecksWithBounds), |
| 1654 | [&](const RuntimePointerChecking::PointerCheck &Check) { |
NAKAMURA Takumi | 94abbbd | 2015-07-27 01:35:30 +0000 | [diff] [blame] | 1655 | PointerBounds |
| 1656 | First = expandBounds(Check.first, L, Loc, Exp, SE, PtrRtChecking), |
| 1657 | Second = expandBounds(Check.second, L, Loc, Exp, SE, PtrRtChecking); |
| 1658 | return std::make_pair(First, Second); |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1659 | }); |
| 1660 | |
| 1661 | return ChecksWithBounds; |
| 1662 | } |
| 1663 | |
| 1664 | std::pair<Instruction *, Instruction *> LoopAccessInfo::addRuntimeCheck( |
| 1665 | Instruction *Loc, |
| 1666 | const SmallVectorImpl<RuntimePointerChecking::PointerCheck> &PointerChecks) |
| 1667 | const { |
| 1668 | |
| 1669 | SCEVExpander Exp(*SE, DL, "induction"); |
| 1670 | auto ExpandedChecks = |
| 1671 | expandBounds(PointerChecks, TheLoop, Loc, SE, Exp, PtrRtChecking); |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1672 | |
| 1673 | LLVMContext &Ctx = Loc->getContext(); |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1674 | Instruction *FirstInst = nullptr; |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1675 | IRBuilder<> ChkBuilder(Loc); |
| 1676 | // Our instructions might fold to a constant. |
| 1677 | Value *MemoryRuntimeCheck = nullptr; |
Silviu Baranga | 1b6b50a | 2015-07-08 09:16:33 +0000 | [diff] [blame] | 1678 | |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1679 | for (const auto &Check : ExpandedChecks) { |
| 1680 | const PointerBounds &A = Check.first, &B = Check.second; |
| 1681 | unsigned AS0 = A.Start->getType()->getPointerAddressSpace(); |
| 1682 | unsigned AS1 = B.Start->getType()->getPointerAddressSpace(); |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1683 | |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1684 | assert((AS0 == B.End->getType()->getPointerAddressSpace()) && |
| 1685 | (AS1 == A.End->getType()->getPointerAddressSpace()) && |
| 1686 | "Trying to bounds check pointers with different address spaces"); |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1687 | |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1688 | Type *PtrArithTy0 = Type::getInt8PtrTy(Ctx, AS0); |
| 1689 | Type *PtrArithTy1 = Type::getInt8PtrTy(Ctx, AS1); |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1690 | |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1691 | Value *Start0 = ChkBuilder.CreateBitCast(A.Start, PtrArithTy0, "bc"); |
| 1692 | Value *Start1 = ChkBuilder.CreateBitCast(B.Start, PtrArithTy1, "bc"); |
| 1693 | Value *End0 = ChkBuilder.CreateBitCast(A.End, PtrArithTy1, "bc"); |
| 1694 | Value *End1 = ChkBuilder.CreateBitCast(B.End, PtrArithTy0, "bc"); |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1695 | |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1696 | Value *Cmp0 = ChkBuilder.CreateICmpULE(Start0, End1, "bound0"); |
| 1697 | FirstInst = getFirstInst(FirstInst, Cmp0, Loc); |
| 1698 | Value *Cmp1 = ChkBuilder.CreateICmpULE(Start1, End0, "bound1"); |
| 1699 | FirstInst = getFirstInst(FirstInst, Cmp1, Loc); |
| 1700 | Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1, "found.conflict"); |
| 1701 | FirstInst = getFirstInst(FirstInst, IsConflict, Loc); |
| 1702 | if (MemoryRuntimeCheck) { |
| 1703 | IsConflict = |
| 1704 | ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict, "conflict.rdx"); |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1705 | FirstInst = getFirstInst(FirstInst, IsConflict, Loc); |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1706 | } |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1707 | MemoryRuntimeCheck = IsConflict; |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Adam Nemet | 90fec84 | 2015-04-02 17:51:57 +0000 | [diff] [blame] | 1710 | if (!MemoryRuntimeCheck) |
| 1711 | return std::make_pair(nullptr, nullptr); |
| 1712 | |
Adam Nemet | 7206d7a | 2015-02-06 18:31:04 +0000 | [diff] [blame] | 1713 | // We have to do this trickery because the IRBuilder might fold the check to a |
| 1714 | // constant expression in which case there is no Instruction anchored in a |
| 1715 | // the block. |
| 1716 | Instruction *Check = BinaryOperator::CreateAnd(MemoryRuntimeCheck, |
| 1717 | ConstantInt::getTrue(Ctx)); |
| 1718 | ChkBuilder.Insert(Check, "memcheck.conflict"); |
| 1719 | FirstInst = getFirstInst(FirstInst, Check, Loc); |
| 1720 | return std::make_pair(FirstInst, Check); |
| 1721 | } |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1722 | |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1723 | std::pair<Instruction *, Instruction *> LoopAccessInfo::addRuntimeCheck( |
| 1724 | Instruction *Loc, const SmallVectorImpl<int> *PtrPartition) const { |
| 1725 | if (!PtrRtChecking.Need) |
| 1726 | return std::make_pair(nullptr, nullptr); |
| 1727 | |
Adam Nemet | bbe1f1d | 2015-07-27 19:38:48 +0000 | [diff] [blame] | 1728 | return addRuntimeCheck(Loc, PtrRtChecking.generateChecks(PtrPartition)); |
Adam Nemet | 1da7df3 | 2015-07-26 05:32:14 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1731 | LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1732 | const DataLayout &DL, |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1733 | const TargetLibraryInfo *TLI, AliasAnalysis *AA, |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 1734 | DominatorTree *DT, LoopInfo *LI, |
Adam Nemet | 8bc61df | 2015-02-24 00:41:59 +0000 | [diff] [blame] | 1735 | const ValueToValueMap &Strides) |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 1736 | : PtrRtChecking(SE), DepChecker(SE, L), TheLoop(L), SE(SE), DL(DL), |
| 1737 | TLI(TLI), AA(AA), DT(DT), LI(LI), NumLoads(0), NumStores(0), |
Adam Nemet | ce48250 | 2015-04-08 17:48:40 +0000 | [diff] [blame] | 1738 | MaxSafeDepDistBytes(-1U), CanVecMem(false), |
| 1739 | StoreToLoopInvariantAddress(false) { |
Adam Nemet | 929c38e | 2015-02-19 19:15:10 +0000 | [diff] [blame] | 1740 | if (canAnalyzeLoop()) |
| 1741 | analyzeLoop(Strides); |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1744 | void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const { |
| 1745 | if (CanVecMem) { |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 1746 | if (PtrRtChecking.Need) |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1747 | OS.indent(Depth) << "Memory dependences are safe with run-time checks\n"; |
Adam Nemet | 26da8e9 | 2015-04-14 01:12:55 +0000 | [diff] [blame] | 1748 | else |
| 1749 | OS.indent(Depth) << "Memory dependences are safe\n"; |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | if (Report) |
| 1753 | OS.indent(Depth) << "Report: " << Report->str() << "\n"; |
| 1754 | |
Adam Nemet | 58913d6 | 2015-03-10 17:40:43 +0000 | [diff] [blame] | 1755 | if (auto *InterestingDependences = DepChecker.getInterestingDependences()) { |
| 1756 | OS.indent(Depth) << "Interesting Dependences:\n"; |
| 1757 | for (auto &Dep : *InterestingDependences) { |
| 1758 | Dep.print(OS, Depth + 2, DepChecker.getMemoryInstructions()); |
| 1759 | OS << "\n"; |
| 1760 | } |
| 1761 | } else |
| 1762 | OS.indent(Depth) << "Too many interesting dependences, not recorded\n"; |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1763 | |
| 1764 | // List the pair of accesses need run-time checks to prove independence. |
Adam Nemet | 7cdebac | 2015-07-14 22:32:44 +0000 | [diff] [blame] | 1765 | PtrRtChecking.print(OS, Depth); |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1766 | OS << "\n"; |
Adam Nemet | c338432 | 2015-05-18 15:36:57 +0000 | [diff] [blame] | 1767 | |
| 1768 | OS.indent(Depth) << "Store to invariant address was " |
| 1769 | << (StoreToLoopInvariantAddress ? "" : "not ") |
| 1770 | << "found in loop.\n"; |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Adam Nemet | 8bc61df | 2015-02-24 00:41:59 +0000 | [diff] [blame] | 1773 | const LoopAccessInfo & |
| 1774 | LoopAccessAnalysis::getInfo(Loop *L, const ValueToValueMap &Strides) { |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1775 | auto &LAI = LoopAccessInfoMap[L]; |
| 1776 | |
| 1777 | #ifndef NDEBUG |
| 1778 | assert((!LAI || LAI->NumSymbolicStrides == Strides.size()) && |
| 1779 | "Symbolic strides changed for loop"); |
| 1780 | #endif |
| 1781 | |
| 1782 | if (!LAI) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1783 | const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 1784 | LAI = llvm::make_unique<LoopAccessInfo>(L, SE, DL, TLI, AA, DT, LI, |
| 1785 | Strides); |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1786 | #ifndef NDEBUG |
| 1787 | LAI->NumSymbolicStrides = Strides.size(); |
| 1788 | #endif |
| 1789 | } |
| 1790 | return *LAI.get(); |
| 1791 | } |
| 1792 | |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1793 | void LoopAccessAnalysis::print(raw_ostream &OS, const Module *M) const { |
| 1794 | LoopAccessAnalysis &LAA = *const_cast<LoopAccessAnalysis *>(this); |
| 1795 | |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1796 | ValueToValueMap NoSymbolicStrides; |
| 1797 | |
| 1798 | for (Loop *TopLevelLoop : *LI) |
| 1799 | for (Loop *L : depth_first(TopLevelLoop)) { |
| 1800 | OS.indent(2) << L->getHeader()->getName() << ":\n"; |
| 1801 | auto &LAI = LAA.getInfo(L, NoSymbolicStrides); |
| 1802 | LAI.print(OS, 4); |
| 1803 | } |
| 1804 | } |
| 1805 | |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1806 | bool LoopAccessAnalysis::runOnFunction(Function &F) { |
| 1807 | SE = &getAnalysis<ScalarEvolution>(); |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1808 | auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); |
| 1809 | TLI = TLIP ? &TLIP->getTLI() : nullptr; |
| 1810 | AA = &getAnalysis<AliasAnalysis>(); |
| 1811 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 1812 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1813 | |
| 1814 | return false; |
| 1815 | } |
| 1816 | |
| 1817 | void LoopAccessAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1818 | AU.addRequired<ScalarEvolution>(); |
| 1819 | AU.addRequired<AliasAnalysis>(); |
| 1820 | AU.addRequired<DominatorTreeWrapperPass>(); |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1821 | AU.addRequired<LoopInfoWrapperPass>(); |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1822 | |
| 1823 | AU.setPreservesAll(); |
| 1824 | } |
| 1825 | |
| 1826 | char LoopAccessAnalysis::ID = 0; |
| 1827 | static const char laa_name[] = "Loop Access Analysis"; |
| 1828 | #define LAA_NAME "loop-accesses" |
| 1829 | |
| 1830 | INITIALIZE_PASS_BEGIN(LoopAccessAnalysis, LAA_NAME, laa_name, false, true) |
| 1831 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 1832 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 1833 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Adam Nemet | e91cc6e | 2015-02-19 19:15:19 +0000 | [diff] [blame] | 1834 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Adam Nemet | 3bfd93d | 2015-02-19 19:15:04 +0000 | [diff] [blame] | 1835 | INITIALIZE_PASS_END(LoopAccessAnalysis, LAA_NAME, laa_name, false, true) |
| 1836 | |
| 1837 | namespace llvm { |
| 1838 | Pass *createLAAPass() { |
| 1839 | return new LoopAccessAnalysis(); |
| 1840 | } |
| 1841 | } |