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