Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 1 | //===----------- LoopVersioningLICM.cpp - LICM Loop Versioning ------------===// |
| 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 | // When alias analysis is uncertain about the aliasing between any two accesses, |
| 11 | // it will return MayAlias. This uncertainty from alias analysis restricts LICM |
| 12 | // from proceeding further. In cases where alias analysis is uncertain we might |
| 13 | // use loop versioning as an alternative. |
| 14 | // |
| 15 | // Loop Versioning will create a version of the loop with aggressive aliasing |
| 16 | // assumptions in addition to the original with conservative (default) aliasing |
| 17 | // assumptions. The version of the loop making aggressive aliasing assumptions |
| 18 | // will have all the memory accesses marked as no-alias. These two versions of |
| 19 | // loop will be preceded by a memory runtime check. This runtime check consists |
| 20 | // of bound checks for all unique memory accessed in loop, and it ensures the |
| 21 | // lack of memory aliasing. The result of the runtime check determines which of |
| 22 | // the loop versions is executed: If the runtime check detects any memory |
| 23 | // aliasing, then the original loop is executed. Otherwise, the version with |
| 24 | // aggressive aliasing assumptions is used. |
| 25 | // |
| 26 | // Following are the top level steps: |
| 27 | // |
| 28 | // a) Perform LoopVersioningLICM's feasibility check. |
| 29 | // b) If loop is a candidate for versioning then create a memory bound check, |
| 30 | // by considering all the memory accesses in loop body. |
| 31 | // c) Clone original loop and set all memory accesses as no-alias in new loop. |
| 32 | // d) Set original loop & versioned loop as a branch target of the runtime check |
| 33 | // result. |
| 34 | // |
| 35 | // It transforms loop as shown below: |
| 36 | // |
| 37 | // +----------------+ |
| 38 | // |Runtime Memcheck| |
| 39 | // +----------------+ |
| 40 | // | |
| 41 | // +----------+----------------+----------+ |
| 42 | // | | |
| 43 | // +---------+----------+ +-----------+----------+ |
| 44 | // |Orig Loop Preheader | |Cloned Loop Preheader | |
| 45 | // +--------------------+ +----------------------+ |
| 46 | // | | |
| 47 | // +--------------------+ +----------------------+ |
| 48 | // |Orig Loop Body | |Cloned Loop Body | |
| 49 | // +--------------------+ +----------------------+ |
| 50 | // | | |
| 51 | // +--------------------+ +----------------------+ |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 52 | // |Orig Loop Exit Block| |Cloned Loop Exit Block| |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 53 | // +--------------------+ +-----------+----------+ |
| 54 | // | | |
| 55 | // +----------+--------------+-----------+ |
| 56 | // | |
| 57 | // +-----+----+ |
| 58 | // |Join Block| |
| 59 | // +----------+ |
| 60 | // |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
| 63 | #include "llvm/ADT/MapVector.h" |
| 64 | #include "llvm/ADT/SmallPtrSet.h" |
| 65 | #include "llvm/ADT/Statistic.h" |
| 66 | #include "llvm/ADT/StringExtras.h" |
| 67 | #include "llvm/Analysis/AliasAnalysis.h" |
| 68 | #include "llvm/Analysis/AliasSetTracker.h" |
| 69 | #include "llvm/Analysis/ConstantFolding.h" |
| 70 | #include "llvm/Analysis/GlobalsModRef.h" |
| 71 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
| 72 | #include "llvm/Analysis/LoopInfo.h" |
| 73 | #include "llvm/Analysis/LoopPass.h" |
| 74 | #include "llvm/Analysis/ScalarEvolution.h" |
| 75 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 76 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 77 | #include "llvm/Analysis/ValueTracking.h" |
| 78 | #include "llvm/Analysis/VectorUtils.h" |
| 79 | #include "llvm/IR/Dominators.h" |
| 80 | #include "llvm/IR/IntrinsicInst.h" |
| 81 | #include "llvm/IR/MDBuilder.h" |
| 82 | #include "llvm/IR/PatternMatch.h" |
| 83 | #include "llvm/IR/PredIteratorCache.h" |
| 84 | #include "llvm/IR/Type.h" |
| 85 | #include "llvm/Support/Debug.h" |
| 86 | #include "llvm/Support/raw_ostream.h" |
| 87 | #include "llvm/Transforms/Scalar.h" |
| 88 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 89 | #include "llvm/Transforms/Utils/Cloning.h" |
| 90 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 91 | #include "llvm/Transforms/Utils/LoopVersioning.h" |
| 92 | #include "llvm/Transforms/Utils/ValueMapper.h" |
| 93 | |
| 94 | #define DEBUG_TYPE "loop-versioning-licm" |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 95 | static const char *LICMVersioningMetaData = "llvm.loop.licm_versioning.disable"; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 96 | |
| 97 | using namespace llvm; |
| 98 | |
| 99 | /// Threshold minimum allowed percentage for possible |
| 100 | /// invariant instructions in a loop. |
| 101 | static cl::opt<float> |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 102 | LVInvarThreshold("licm-versioning-invariant-threshold", |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 103 | cl::desc("LoopVersioningLICM's minimum allowed percentage" |
| 104 | "of possible invariant instructions per loop"), |
| 105 | cl::init(25), cl::Hidden); |
| 106 | |
| 107 | /// Threshold for maximum allowed loop nest/depth |
| 108 | static cl::opt<unsigned> LVLoopDepthThreshold( |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 109 | "licm-versioning-max-depth-threshold", |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 110 | cl::desc( |
| 111 | "LoopVersioningLICM's threshold for maximum allowed loop nest/depth"), |
| 112 | cl::init(2), cl::Hidden); |
| 113 | |
| 114 | /// \brief Create MDNode for input string. |
| 115 | static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) { |
| 116 | LLVMContext &Context = TheLoop->getHeader()->getContext(); |
| 117 | Metadata *MDs[] = { |
| 118 | MDString::get(Context, Name), |
| 119 | ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))}; |
| 120 | return MDNode::get(Context, MDs); |
| 121 | } |
| 122 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 123 | /// \brief Set input string into loop metadata by keeping other values intact. |
| 124 | void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *MDString, |
| 125 | unsigned V) { |
| 126 | SmallVector<Metadata *, 4> MDs(1); |
| 127 | // If the loop already has metadata, retain it. |
| 128 | MDNode *LoopID = TheLoop->getLoopID(); |
| 129 | if (LoopID) { |
| 130 | for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { |
| 131 | MDNode *Node = cast<MDNode>(LoopID->getOperand(i)); |
| 132 | MDs.push_back(Node); |
| 133 | } |
| 134 | } |
| 135 | // Add new metadata. |
| 136 | MDs.push_back(createStringMetadata(TheLoop, MDString, V)); |
| 137 | // Replace current metadata node with new one. |
| 138 | LLVMContext &Context = TheLoop->getHeader()->getContext(); |
| 139 | MDNode *NewLoopID = MDNode::get(Context, MDs); |
| 140 | // Set operand 0 to refer to the loop id itself. |
| 141 | NewLoopID->replaceOperandWith(0, NewLoopID); |
| 142 | TheLoop->setLoopID(NewLoopID); |
| 143 | } |
| 144 | |
| 145 | namespace { |
| 146 | struct LoopVersioningLICM : public LoopPass { |
| 147 | static char ID; |
| 148 | |
| 149 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
| 150 | |
| 151 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 152 | AU.setPreservesCFG(); |
| 153 | AU.addRequired<AAResultsWrapperPass>(); |
| 154 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 155 | AU.addRequiredID(LCSSAID); |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 156 | AU.addRequired<LoopAccessLegacyAnalysis>(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 157 | AU.addRequired<LoopInfoWrapperPass>(); |
| 158 | AU.addRequiredID(LoopSimplifyID); |
| 159 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 160 | AU.addPreserved<AAResultsWrapperPass>(); |
| 161 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 162 | } |
| 163 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 164 | LoopVersioningLICM() |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 165 | : LoopPass(ID), AA(nullptr), SE(nullptr), LAA(nullptr), LAI(nullptr), |
| 166 | CurLoop(nullptr), LoopDepthThreshold(LVLoopDepthThreshold), |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 167 | InvariantThreshold(LVInvarThreshold), LoadAndStoreCounter(0), |
| 168 | InvariantCounter(0), IsReadOnlyLoop(true) { |
| 169 | initializeLoopVersioningLICMPass(*PassRegistry::getPassRegistry()); |
| 170 | } |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 171 | StringRef getPassName() const override { return "Loop Versioning for LICM"; } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 172 | |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 173 | void reset() { |
| 174 | AA = nullptr; |
| 175 | SE = nullptr; |
| 176 | LAA = nullptr; |
| 177 | CurLoop = nullptr; |
| 178 | LoadAndStoreCounter = 0; |
| 179 | InvariantCounter = 0; |
| 180 | IsReadOnlyLoop = true; |
| 181 | CurAST.reset(); |
| 182 | } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 183 | |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 184 | class AutoResetter { |
| 185 | public: |
| 186 | AutoResetter(LoopVersioningLICM &LVLICM) : LVLICM(LVLICM) {} |
| 187 | ~AutoResetter() { LVLICM.reset(); } |
| 188 | |
| 189 | private: |
| 190 | LoopVersioningLICM &LVLICM; |
| 191 | }; |
| 192 | |
| 193 | private: |
| 194 | AliasAnalysis *AA; // Current AliasAnalysis information |
| 195 | ScalarEvolution *SE; // Current ScalarEvolution |
| 196 | LoopAccessLegacyAnalysis *LAA; // Current LoopAccessAnalysis |
| 197 | const LoopAccessInfo *LAI; // Current Loop's LoopAccessInfo |
| 198 | |
| 199 | Loop *CurLoop; // The current loop we are working on. |
| 200 | std::unique_ptr<AliasSetTracker> |
| 201 | CurAST; // AliasSet information for the current loop. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 202 | |
| 203 | unsigned LoopDepthThreshold; // Maximum loop nest threshold |
| 204 | float InvariantThreshold; // Minimum invariant threshold |
| 205 | unsigned LoadAndStoreCounter; // Counter to track num of load & store |
| 206 | unsigned InvariantCounter; // Counter to track num of invariant |
| 207 | bool IsReadOnlyLoop; // Read only loop marker. |
| 208 | |
| 209 | bool isLegalForVersioning(); |
| 210 | bool legalLoopStructure(); |
| 211 | bool legalLoopInstructions(); |
| 212 | bool legalLoopMemoryAccesses(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 213 | bool isLoopAlreadyVisited(); |
| 214 | void setNoAliasToLoop(Loop *); |
| 215 | bool instructionSafeForVersioning(Instruction *); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 216 | }; |
| 217 | } |
| 218 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 219 | /// \brief Check loop structure and confirms it's good for LoopVersioningLICM. |
| 220 | bool LoopVersioningLICM::legalLoopStructure() { |
Florian Hahn | 2e03213 | 2016-12-19 17:13:37 +0000 | [diff] [blame] | 221 | // Loop must be in loop simplify form. |
| 222 | if (!CurLoop->isLoopSimplifyForm()) { |
| 223 | DEBUG( |
| 224 | dbgs() << " loop is not in loop-simplify form.\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | // Loop should be innermost loop, if not return false. |
| 228 | if (CurLoop->getSubLoops().size()) { |
| 229 | DEBUG(dbgs() << " loop is not innermost\n"); |
| 230 | return false; |
| 231 | } |
| 232 | // Loop should have a single backedge, if not return false. |
| 233 | if (CurLoop->getNumBackEdges() != 1) { |
| 234 | DEBUG(dbgs() << " loop has multiple backedges\n"); |
| 235 | return false; |
| 236 | } |
| 237 | // Loop must have a single exiting block, if not return false. |
| 238 | if (!CurLoop->getExitingBlock()) { |
| 239 | DEBUG(dbgs() << " loop has multiple exiting block\n"); |
| 240 | return false; |
| 241 | } |
| 242 | // We only handle bottom-tested loop, i.e. loop in which the condition is |
| 243 | // checked at the end of each iteration. With that we can assume that all |
| 244 | // instructions in the loop are executed the same number of times. |
| 245 | if (CurLoop->getExitingBlock() != CurLoop->getLoopLatch()) { |
| 246 | DEBUG(dbgs() << " loop is not bottom tested\n"); |
| 247 | return false; |
| 248 | } |
| 249 | // Parallel loops must not have aliasing loop-invariant memory accesses. |
| 250 | // Hence we don't need to version anything in this case. |
| 251 | if (CurLoop->isAnnotatedParallel()) { |
| 252 | DEBUG(dbgs() << " Parallel loop is not worth versioning\n"); |
| 253 | return false; |
| 254 | } |
| 255 | // Loop depth more then LoopDepthThreshold are not allowed |
| 256 | if (CurLoop->getLoopDepth() > LoopDepthThreshold) { |
| 257 | DEBUG(dbgs() << " loop depth is more then threshold\n"); |
| 258 | return false; |
| 259 | } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 260 | // We need to be able to compute the loop trip count in order |
| 261 | // to generate the bound checks. |
| 262 | const SCEV *ExitCount = SE->getBackedgeTakenCount(CurLoop); |
| 263 | if (ExitCount == SE->getCouldNotCompute()) { |
| 264 | DEBUG(dbgs() << " loop does not has trip count\n"); |
| 265 | return false; |
| 266 | } |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | /// \brief Check memory accesses in loop and confirms it's good for |
| 271 | /// LoopVersioningLICM. |
| 272 | bool LoopVersioningLICM::legalLoopMemoryAccesses() { |
| 273 | bool HasMayAlias = false; |
| 274 | bool TypeSafety = false; |
| 275 | bool HasMod = false; |
| 276 | // Memory check: |
| 277 | // Transform phase will generate a versioned loop and also a runtime check to |
| 278 | // ensure the pointers are independent and they don’t alias. |
| 279 | // In version variant of loop, alias meta data asserts that all access are |
| 280 | // mutually independent. |
| 281 | // |
| 282 | // Pointers aliasing in alias domain are avoided because with multiple |
| 283 | // aliasing domains we may not be able to hoist potential loop invariant |
| 284 | // access out of the loop. |
| 285 | // |
| 286 | // Iterate over alias tracker sets, and confirm AliasSets doesn't have any |
| 287 | // must alias set. |
| 288 | for (const auto &I : *CurAST) { |
| 289 | const AliasSet &AS = I; |
| 290 | // Skip Forward Alias Sets, as this should be ignored as part of |
| 291 | // the AliasSetTracker object. |
| 292 | if (AS.isForwardingAliasSet()) |
| 293 | continue; |
| 294 | // With MustAlias its not worth adding runtime bound check. |
| 295 | if (AS.isMustAlias()) |
| 296 | return false; |
| 297 | Value *SomePtr = AS.begin()->getValue(); |
| 298 | bool TypeCheck = true; |
| 299 | // Check for Mod & MayAlias |
| 300 | HasMayAlias |= AS.isMayAlias(); |
| 301 | HasMod |= AS.isMod(); |
| 302 | for (const auto &A : AS) { |
| 303 | Value *Ptr = A.getValue(); |
| 304 | // Alias tracker should have pointers of same data type. |
| 305 | TypeCheck = (TypeCheck && (SomePtr->getType() == Ptr->getType())); |
| 306 | } |
| 307 | // At least one alias tracker should have pointers of same data type. |
| 308 | TypeSafety |= TypeCheck; |
| 309 | } |
| 310 | // Ensure types should be of same type. |
| 311 | if (!TypeSafety) { |
| 312 | DEBUG(dbgs() << " Alias tracker type safety failed!\n"); |
| 313 | return false; |
| 314 | } |
| 315 | // Ensure loop body shouldn't be read only. |
| 316 | if (!HasMod) { |
| 317 | DEBUG(dbgs() << " No memory modified in loop body\n"); |
| 318 | return false; |
| 319 | } |
| 320 | // Make sure alias set has may alias case. |
| 321 | // If there no alias memory ambiguity, return false. |
| 322 | if (!HasMayAlias) { |
| 323 | DEBUG(dbgs() << " No ambiguity in memory access.\n"); |
| 324 | return false; |
| 325 | } |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | /// \brief Check loop instructions safe for Loop versioning. |
| 330 | /// It returns true if it's safe else returns false. |
| 331 | /// Consider following: |
| 332 | /// 1) Check all load store in loop body are non atomic & non volatile. |
| 333 | /// 2) Check function call safety, by ensuring its not accessing memory. |
| 334 | /// 3) Loop body shouldn't have any may throw instruction. |
| 335 | bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *I) { |
| 336 | assert(I != nullptr && "Null instruction found!"); |
| 337 | // Check function call safety |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 338 | if (isa<CallInst>(I) && !AA->doesNotAccessMemory(CallSite(I))) { |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 339 | DEBUG(dbgs() << " Unsafe call site found.\n"); |
| 340 | return false; |
| 341 | } |
| 342 | // Avoid loops with possiblity of throw |
| 343 | if (I->mayThrow()) { |
| 344 | DEBUG(dbgs() << " May throw instruction found in loop body\n"); |
| 345 | return false; |
| 346 | } |
| 347 | // If current instruction is load instructions |
| 348 | // make sure it's a simple load (non atomic & non volatile) |
| 349 | if (I->mayReadFromMemory()) { |
| 350 | LoadInst *Ld = dyn_cast<LoadInst>(I); |
| 351 | if (!Ld || !Ld->isSimple()) { |
| 352 | DEBUG(dbgs() << " Found a non-simple load.\n"); |
| 353 | return false; |
| 354 | } |
| 355 | LoadAndStoreCounter++; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 356 | Value *Ptr = Ld->getPointerOperand(); |
| 357 | // Check loop invariant. |
| 358 | if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop)) |
| 359 | InvariantCounter++; |
| 360 | } |
| 361 | // If current instruction is store instruction |
| 362 | // make sure it's a simple store (non atomic & non volatile) |
| 363 | else if (I->mayWriteToMemory()) { |
| 364 | StoreInst *St = dyn_cast<StoreInst>(I); |
| 365 | if (!St || !St->isSimple()) { |
| 366 | DEBUG(dbgs() << " Found a non-simple store.\n"); |
| 367 | return false; |
| 368 | } |
| 369 | LoadAndStoreCounter++; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 370 | Value *Ptr = St->getPointerOperand(); |
| 371 | // Check loop invariant. |
| 372 | if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop)) |
| 373 | InvariantCounter++; |
| 374 | |
| 375 | IsReadOnlyLoop = false; |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | /// \brief Check loop instructions and confirms it's good for |
| 381 | /// LoopVersioningLICM. |
| 382 | bool LoopVersioningLICM::legalLoopInstructions() { |
| 383 | // Resetting counters. |
| 384 | LoadAndStoreCounter = 0; |
| 385 | InvariantCounter = 0; |
| 386 | IsReadOnlyLoop = true; |
| 387 | // Iterate over loop blocks and instructions of each block and check |
| 388 | // instruction safety. |
| 389 | for (auto *Block : CurLoop->getBlocks()) |
| 390 | for (auto &Inst : *Block) { |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 391 | // If instruction is unsafe just return false. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 392 | if (!instructionSafeForVersioning(&Inst)) |
| 393 | return false; |
| 394 | } |
| 395 | // Get LoopAccessInfo from current loop. |
Adam Nemet | a9f09c6 | 2016-06-17 22:35:41 +0000 | [diff] [blame] | 396 | LAI = &LAA->getInfo(CurLoop); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 397 | // Check LoopAccessInfo for need of runtime check. |
| 398 | if (LAI->getRuntimePointerChecking()->getChecks().empty()) { |
| 399 | DEBUG(dbgs() << " LAA: Runtime check not found !!\n"); |
| 400 | return false; |
| 401 | } |
| 402 | // Number of runtime-checks should be less then RuntimeMemoryCheckThreshold |
| 403 | if (LAI->getNumRuntimePointerChecks() > |
| 404 | VectorizerParams::RuntimeMemoryCheckThreshold) { |
| 405 | DEBUG(dbgs() << " LAA: Runtime checks are more than threshold !!\n"); |
| 406 | return false; |
| 407 | } |
| 408 | // Loop should have at least one invariant load or store instruction. |
| 409 | if (!InvariantCounter) { |
| 410 | DEBUG(dbgs() << " Invariant not found !!\n"); |
| 411 | return false; |
| 412 | } |
| 413 | // Read only loop not allowed. |
| 414 | if (IsReadOnlyLoop) { |
| 415 | DEBUG(dbgs() << " Found a read-only loop!\n"); |
| 416 | return false; |
| 417 | } |
| 418 | // Profitablity check: |
| 419 | // Check invariant threshold, should be in limit. |
| 420 | if (InvariantCounter * 100 < InvariantThreshold * LoadAndStoreCounter) { |
| 421 | DEBUG(dbgs() |
| 422 | << " Invariant load & store are less then defined threshold\n"); |
| 423 | DEBUG(dbgs() << " Invariant loads & stores: " |
| 424 | << ((InvariantCounter * 100) / LoadAndStoreCounter) << "%\n"); |
| 425 | DEBUG(dbgs() << " Invariant loads & store threshold: " |
| 426 | << InvariantThreshold << "%\n"); |
| 427 | return false; |
| 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | /// \brief It checks loop is already visited or not. |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 433 | /// check loop meta data, if loop revisited return true |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 434 | /// else false. |
| 435 | bool LoopVersioningLICM::isLoopAlreadyVisited() { |
| 436 | // Check LoopVersioningLICM metadata into loop |
Adam Nemet | f787826 | 2016-04-21 17:33:12 +0000 | [diff] [blame] | 437 | if (findStringMetadataForLoop(CurLoop, LICMVersioningMetaData)) { |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 438 | return true; |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | /// \brief Checks legality for LoopVersioningLICM by considering following: |
| 444 | /// a) loop structure legality b) loop instruction legality |
| 445 | /// c) loop memory access legality. |
| 446 | /// Return true if legal else returns false. |
| 447 | bool LoopVersioningLICM::isLegalForVersioning() { |
| 448 | DEBUG(dbgs() << "Loop: " << *CurLoop); |
| 449 | // Make sure not re-visiting same loop again. |
| 450 | if (isLoopAlreadyVisited()) { |
| 451 | DEBUG( |
| 452 | dbgs() << " Revisiting loop in LoopVersioningLICM not allowed.\n\n"); |
| 453 | return false; |
| 454 | } |
| 455 | // Check loop structure leagality. |
| 456 | if (!legalLoopStructure()) { |
| 457 | DEBUG( |
| 458 | dbgs() << " Loop structure not suitable for LoopVersioningLICM\n\n"); |
| 459 | return false; |
| 460 | } |
| 461 | // Check loop instruction leagality. |
| 462 | if (!legalLoopInstructions()) { |
| 463 | DEBUG(dbgs() |
| 464 | << " Loop instructions not suitable for LoopVersioningLICM\n\n"); |
| 465 | return false; |
| 466 | } |
| 467 | // Check loop memory access leagality. |
| 468 | if (!legalLoopMemoryAccesses()) { |
| 469 | DEBUG(dbgs() |
| 470 | << " Loop memory access not suitable for LoopVersioningLICM\n\n"); |
| 471 | return false; |
| 472 | } |
| 473 | // Loop versioning is feasible, return true. |
| 474 | DEBUG(dbgs() << " Loop Versioning found to be beneficial\n\n"); |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | /// \brief Update loop with aggressive aliasing assumptions. |
| 479 | /// It marks no-alias to any pairs of memory operations by assuming |
| 480 | /// loop should not have any must-alias memory accesses pairs. |
| 481 | /// During LoopVersioningLICM legality we ignore loops having must |
| 482 | /// aliasing memory accesses. |
| 483 | void LoopVersioningLICM::setNoAliasToLoop(Loop *VerLoop) { |
| 484 | // Get latch terminator instruction. |
| 485 | Instruction *I = VerLoop->getLoopLatch()->getTerminator(); |
| 486 | // Create alias scope domain. |
| 487 | MDBuilder MDB(I->getContext()); |
| 488 | MDNode *NewDomain = MDB.createAnonymousAliasScopeDomain("LVDomain"); |
| 489 | StringRef Name = "LVAliasScope"; |
| 490 | SmallVector<Metadata *, 4> Scopes, NoAliases; |
| 491 | MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name); |
| 492 | // Iterate over each instruction of loop. |
| 493 | // set no-alias for all load & store instructions. |
| 494 | for (auto *Block : CurLoop->getBlocks()) { |
| 495 | for (auto &Inst : *Block) { |
| 496 | // Only interested in instruction that may modify or read memory. |
| 497 | if (!Inst.mayReadFromMemory() && !Inst.mayWriteToMemory()) |
| 498 | continue; |
| 499 | Scopes.push_back(NewScope); |
| 500 | NoAliases.push_back(NewScope); |
| 501 | // Set no-alias for current instruction. |
| 502 | Inst.setMetadata( |
| 503 | LLVMContext::MD_noalias, |
| 504 | MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_noalias), |
| 505 | MDNode::get(Inst.getContext(), NoAliases))); |
| 506 | // set alias-scope for current instruction. |
| 507 | Inst.setMetadata( |
| 508 | LLVMContext::MD_alias_scope, |
| 509 | MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_alias_scope), |
| 510 | MDNode::get(Inst.getContext(), Scopes))); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | bool LoopVersioningLICM::runOnLoop(Loop *L, LPPassManager &LPM) { |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 516 | // This will automatically release all resources hold by the current |
| 517 | // LoopVersioningLICM object. |
| 518 | AutoResetter Resetter(*this); |
| 519 | |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 520 | if (skipLoop(L)) |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 521 | return false; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 522 | // Get Analysis information. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 523 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 524 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 525 | LAA = &getAnalysis<LoopAccessLegacyAnalysis>(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 526 | LAI = nullptr; |
| 527 | // Set Current Loop |
| 528 | CurLoop = L; |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 529 | CurAST.reset(new AliasSetTracker(*AA)); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 530 | |
| 531 | // Loop over the body of this loop, construct AST. |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 532 | LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 533 | for (auto *Block : L->getBlocks()) { |
| 534 | if (LI->getLoopFor(Block) == L) // Ignore blocks in subloop. |
| 535 | CurAST->add(*Block); // Incorporate the specified basic block |
| 536 | } |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 537 | |
| 538 | bool Changed = false; |
| 539 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 540 | // Check feasiblity of LoopVersioningLICM. |
| 541 | // If versioning found to be feasible and beneficial then proceed |
| 542 | // else simply return, by cleaning up memory. |
| 543 | if (isLegalForVersioning()) { |
| 544 | // Do loop versioning. |
| 545 | // Create memcheck for memory accessed inside loop. |
| 546 | // Clone original loop, and set blocks properly. |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 547 | DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 548 | LoopVersioning LVer(*LAI, CurLoop, LI, DT, SE, true); |
| 549 | LVer.versionLoop(); |
| 550 | // Set Loop Versioning metaData for original loop. |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 551 | addStringMetadataToLoop(LVer.getNonVersionedLoop(), LICMVersioningMetaData); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 552 | // Set Loop Versioning metaData for version loop. |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 553 | addStringMetadataToLoop(LVer.getVersionedLoop(), LICMVersioningMetaData); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 554 | // Set "llvm.mem.parallel_loop_access" metaData to versioned loop. |
| 555 | addStringMetadataToLoop(LVer.getVersionedLoop(), |
| 556 | "llvm.mem.parallel_loop_access"); |
| 557 | // Update version loop with aggressive aliasing assumption. |
| 558 | setNoAliasToLoop(LVer.getVersionedLoop()); |
| 559 | Changed = true; |
| 560 | } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 561 | return Changed; |
| 562 | } |
| 563 | |
| 564 | char LoopVersioningLICM::ID = 0; |
| 565 | INITIALIZE_PASS_BEGIN(LoopVersioningLICM, "loop-versioning-licm", |
| 566 | "Loop Versioning For LICM", false, false) |
| 567 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 568 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 569 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
Easwaran Raman | e12c487 | 2016-06-09 19:44:46 +0000 | [diff] [blame] | 570 | INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 571 | INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis) |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 572 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 573 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 574 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 575 | INITIALIZE_PASS_END(LoopVersioningLICM, "loop-versioning-licm", |
| 576 | "Loop Versioning For LICM", false, false) |
| 577 | |
| 578 | Pass *llvm::createLoopVersioningLICMPass() { return new LoopVersioningLICM(); } |