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 // +--------------------+ +----------------------+ 52 // |Orig Loop Exit Block| |Cloned Loop Exit Block| 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" 95 static const char* LICMVersioningMetaData = 96 "llvm.loop.licm_versioning.disable"; 97 98 using namespace llvm; 99 100 /// Threshold minimum allowed percentage for possible 101 /// invariant instructions in a loop. 102 static cl::opt<float> 103 LVInvarThreshold("licm-versioning-invariant-threshold", 104 cl::desc("LoopVersioningLICM's minimum allowed percentage" 105 "of possible invariant instructions per loop"), 106 cl::init(25), cl::Hidden); 107 108 /// Threshold for maximum allowed loop nest/depth 109 static cl::opt<unsigned> LVLoopDepthThreshold( 110 "licm-versioning-max-depth-threshold", 111 cl::desc( 112 "LoopVersioningLICM's threshold for maximum allowed loop nest/depth"), 113 cl::init(2), cl::Hidden); 114 115 /// \brief Create MDNode for input string. 116 static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) { 117 LLVMContext &Context = TheLoop->getHeader()->getContext(); 118 Metadata *MDs[] = { 119 MDString::get(Context, Name), 120 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))}; 121 return MDNode::get(Context, MDs); 122 } 123 124 /// \brief Check string metadata in loop, if it exist return true, 125 /// else return false. 126 bool llvm::checkStringMetadataIntoLoop(Loop *TheLoop, StringRef Name) { 127 MDNode *LoopID = TheLoop->getLoopID(); 128 // Return false if LoopID is false. 129 if (!LoopID) 130 return false; 131 // Iterate over LoopID operands and look for MDString Metadata 132 for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) { 133 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); 134 if (!MD) 135 continue; 136 MDString *S = dyn_cast<MDString>(MD->getOperand(0)); 137 if (!S) 138 continue; 139 // Return true if MDString holds expected MetaData. 140 if (Name.equals(S->getString())) 141 return true; 142 } 143 return false; 144 } 145 146 /// \brief Set input string into loop metadata by keeping other values intact. 147 void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *MDString, 148 unsigned V) { 149 SmallVector<Metadata *, 4> MDs(1); 150 // If the loop already has metadata, retain it. 151 MDNode *LoopID = TheLoop->getLoopID(); 152 if (LoopID) { 153 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { 154 MDNode *Node = cast<MDNode>(LoopID->getOperand(i)); 155 MDs.push_back(Node); 156 } 157 } 158 // Add new metadata. 159 MDs.push_back(createStringMetadata(TheLoop, MDString, V)); 160 // Replace current metadata node with new one. 161 LLVMContext &Context = TheLoop->getHeader()->getContext(); 162 MDNode *NewLoopID = MDNode::get(Context, MDs); 163 // Set operand 0 to refer to the loop id itself. 164 NewLoopID->replaceOperandWith(0, NewLoopID); 165 TheLoop->setLoopID(NewLoopID); 166 } 167 168 namespace { 169 struct LoopVersioningLICM : public LoopPass { 170 static char ID; 171 172 bool runOnLoop(Loop *L, LPPassManager &LPM) override; 173 174 void getAnalysisUsage(AnalysisUsage &AU) const override { 175 AU.setPreservesCFG(); 176 AU.addRequired<AAResultsWrapperPass>(); 177 AU.addRequired<DominatorTreeWrapperPass>(); 178 AU.addRequiredID(LCSSAID); 179 AU.addRequired<LoopAccessAnalysis>(); 180 AU.addRequired<LoopInfoWrapperPass>(); 181 AU.addRequiredID(LoopSimplifyID); 182 AU.addRequired<ScalarEvolutionWrapperPass>(); 183 AU.addRequired<TargetLibraryInfoWrapperPass>(); 184 AU.addPreserved<AAResultsWrapperPass>(); 185 AU.addPreserved<GlobalsAAWrapperPass>(); 186 } 187 188 using llvm::Pass::doFinalization; 189 190 bool doFinalization() override { return false; } 191 192 LoopVersioningLICM() 193 : LoopPass(ID), AA(nullptr), SE(nullptr), LI(nullptr), DT(nullptr), 194 TLI(nullptr), LAA(nullptr), LAI(nullptr), Changed(false), 195 Preheader(nullptr), CurLoop(nullptr), CurAST(nullptr), 196 LoopDepthThreshold(LVLoopDepthThreshold), 197 InvariantThreshold(LVInvarThreshold), LoadAndStoreCounter(0), 198 InvariantCounter(0), IsReadOnlyLoop(true) { 199 initializeLoopVersioningLICMPass(*PassRegistry::getPassRegistry()); 200 } 201 202 AliasAnalysis *AA; // Current AliasAnalysis information 203 ScalarEvolution *SE; // Current ScalarEvolution 204 LoopInfo *LI; // Current LoopInfo 205 DominatorTree *DT; // Dominator Tree for the current Loop. 206 TargetLibraryInfo *TLI; // TargetLibraryInfo for constant folding. 207 LoopAccessAnalysis *LAA; // Current LoopAccessAnalysis 208 const LoopAccessInfo *LAI; // Current Loop's LoopAccessInfo 209 210 bool Changed; // Set to true when we change anything. 211 BasicBlock *Preheader; // The preheader block of the current loop. 212 Loop *CurLoop; // The current loop we are working on. 213 AliasSetTracker *CurAST; // AliasSet information for the current loop. 214 ValueToValueMap Strides; 215 216 unsigned LoopDepthThreshold; // Maximum loop nest threshold 217 float InvariantThreshold; // Minimum invariant threshold 218 unsigned LoadAndStoreCounter; // Counter to track num of load & store 219 unsigned InvariantCounter; // Counter to track num of invariant 220 bool IsReadOnlyLoop; // Read only loop marker. 221 222 bool isLegalForVersioning(); 223 bool legalLoopStructure(); 224 bool legalLoopInstructions(); 225 bool legalLoopMemoryAccesses(); 226 void collectStridedAccess(Value *LoadOrStoreInst); 227 bool isLoopAlreadyVisited(); 228 void setNoAliasToLoop(Loop *); 229 bool instructionSafeForVersioning(Instruction *); 230 const char *getPassName() const override { return "Loop Versioning"; } 231 }; 232 } 233 234 /// \brief Collects stride access from a given value. 235 void LoopVersioningLICM::collectStridedAccess(Value *MemAccess) { 236 Value *Ptr = nullptr; 237 if (LoadInst *LI = dyn_cast<LoadInst>(MemAccess)) 238 Ptr = LI->getPointerOperand(); 239 else if (StoreInst *SI = dyn_cast<StoreInst>(MemAccess)) 240 Ptr = SI->getPointerOperand(); 241 else 242 return; 243 244 Value *Stride = getStrideFromPointer(Ptr, SE, CurLoop); 245 if (!Stride) 246 return; 247 248 DEBUG(dbgs() << "Found a strided access that we can version"); 249 DEBUG(dbgs() << " Ptr: " << *Ptr << " Stride: " << *Stride << "\n"); 250 Strides[Ptr] = Stride; 251 } 252 253 /// \brief Check loop structure and confirms it's good for LoopVersioningLICM. 254 bool LoopVersioningLICM::legalLoopStructure() { 255 // Loop must have a preheader, if not return false. 256 if (!CurLoop->getLoopPreheader()) { 257 DEBUG(dbgs() << " loop preheader is missing\n"); 258 return false; 259 } 260 // Loop should be innermost loop, if not return false. 261 if (CurLoop->getSubLoops().size()) { 262 DEBUG(dbgs() << " loop is not innermost\n"); 263 return false; 264 } 265 // Loop should have a single backedge, if not return false. 266 if (CurLoop->getNumBackEdges() != 1) { 267 DEBUG(dbgs() << " loop has multiple backedges\n"); 268 return false; 269 } 270 // Loop must have a single exiting block, if not return false. 271 if (!CurLoop->getExitingBlock()) { 272 DEBUG(dbgs() << " loop has multiple exiting block\n"); 273 return false; 274 } 275 // We only handle bottom-tested loop, i.e. loop in which the condition is 276 // checked at the end of each iteration. With that we can assume that all 277 // instructions in the loop are executed the same number of times. 278 if (CurLoop->getExitingBlock() != CurLoop->getLoopLatch()) { 279 DEBUG(dbgs() << " loop is not bottom tested\n"); 280 return false; 281 } 282 // Parallel loops must not have aliasing loop-invariant memory accesses. 283 // Hence we don't need to version anything in this case. 284 if (CurLoop->isAnnotatedParallel()) { 285 DEBUG(dbgs() << " Parallel loop is not worth versioning\n"); 286 return false; 287 } 288 // Loop depth more then LoopDepthThreshold are not allowed 289 if (CurLoop->getLoopDepth() > LoopDepthThreshold) { 290 DEBUG(dbgs() << " loop depth is more then threshold\n"); 291 return false; 292 } 293 // Loop should have a dedicated exit block, if not return false. 294 if (!CurLoop->hasDedicatedExits()) { 295 DEBUG(dbgs() << " loop does not has dedicated exit blocks\n"); 296 return false; 297 } 298 // We need to be able to compute the loop trip count in order 299 // to generate the bound checks. 300 const SCEV *ExitCount = SE->getBackedgeTakenCount(CurLoop); 301 if (ExitCount == SE->getCouldNotCompute()) { 302 DEBUG(dbgs() << " loop does not has trip count\n"); 303 return false; 304 } 305 return true; 306 } 307 308 /// \brief Check memory accesses in loop and confirms it's good for 309 /// LoopVersioningLICM. 310 bool LoopVersioningLICM::legalLoopMemoryAccesses() { 311 bool HasMayAlias = false; 312 bool TypeSafety = false; 313 bool HasMod = false; 314 // Memory check: 315 // Transform phase will generate a versioned loop and also a runtime check to 316 // ensure the pointers are independent and they don’t alias. 317 // In version variant of loop, alias meta data asserts that all access are 318 // mutually independent. 319 // 320 // Pointers aliasing in alias domain are avoided because with multiple 321 // aliasing domains we may not be able to hoist potential loop invariant 322 // access out of the loop. 323 // 324 // Iterate over alias tracker sets, and confirm AliasSets doesn't have any 325 // must alias set. 326 for (const auto &I : *CurAST) { 327 const AliasSet &AS = I; 328 // Skip Forward Alias Sets, as this should be ignored as part of 329 // the AliasSetTracker object. 330 if (AS.isForwardingAliasSet()) 331 continue; 332 // With MustAlias its not worth adding runtime bound check. 333 if (AS.isMustAlias()) 334 return false; 335 Value *SomePtr = AS.begin()->getValue(); 336 bool TypeCheck = true; 337 // Check for Mod & MayAlias 338 HasMayAlias |= AS.isMayAlias(); 339 HasMod |= AS.isMod(); 340 for (const auto &A : AS) { 341 Value *Ptr = A.getValue(); 342 // Alias tracker should have pointers of same data type. 343 TypeCheck = (TypeCheck && (SomePtr->getType() == Ptr->getType())); 344 } 345 // At least one alias tracker should have pointers of same data type. 346 TypeSafety |= TypeCheck; 347 } 348 // Ensure types should be of same type. 349 if (!TypeSafety) { 350 DEBUG(dbgs() << " Alias tracker type safety failed!\n"); 351 return false; 352 } 353 // Ensure loop body shouldn't be read only. 354 if (!HasMod) { 355 DEBUG(dbgs() << " No memory modified in loop body\n"); 356 return false; 357 } 358 // Make sure alias set has may alias case. 359 // If there no alias memory ambiguity, return false. 360 if (!HasMayAlias) { 361 DEBUG(dbgs() << " No ambiguity in memory access.\n"); 362 return false; 363 } 364 return true; 365 } 366 367 /// \brief Check loop instructions safe for Loop versioning. 368 /// It returns true if it's safe else returns false. 369 /// Consider following: 370 /// 1) Check all load store in loop body are non atomic & non volatile. 371 /// 2) Check function call safety, by ensuring its not accessing memory. 372 /// 3) Loop body shouldn't have any may throw instruction. 373 bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *I) { 374 assert(I != nullptr && "Null instruction found!"); 375 // Check function call safety 376 if (isa<CallInst>(I) && !AA->doesNotAccessMemory(CallSite(I))) { 377 DEBUG(dbgs() << " Unsafe call site found.\n"); 378 return false; 379 } 380 // Avoid loops with possiblity of throw 381 if (I->mayThrow()) { 382 DEBUG(dbgs() << " May throw instruction found in loop body\n"); 383 return false; 384 } 385 // If current instruction is load instructions 386 // make sure it's a simple load (non atomic & non volatile) 387 if (I->mayReadFromMemory()) { 388 LoadInst *Ld = dyn_cast<LoadInst>(I); 389 if (!Ld || !Ld->isSimple()) { 390 DEBUG(dbgs() << " Found a non-simple load.\n"); 391 return false; 392 } 393 LoadAndStoreCounter++; 394 collectStridedAccess(Ld); 395 Value *Ptr = Ld->getPointerOperand(); 396 // Check loop invariant. 397 if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop)) 398 InvariantCounter++; 399 } 400 // If current instruction is store instruction 401 // make sure it's a simple store (non atomic & non volatile) 402 else if (I->mayWriteToMemory()) { 403 StoreInst *St = dyn_cast<StoreInst>(I); 404 if (!St || !St->isSimple()) { 405 DEBUG(dbgs() << " Found a non-simple store.\n"); 406 return false; 407 } 408 LoadAndStoreCounter++; 409 collectStridedAccess(St); 410 Value *Ptr = St->getPointerOperand(); 411 // Check loop invariant. 412 if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop)) 413 InvariantCounter++; 414 415 IsReadOnlyLoop = false; 416 } 417 return true; 418 } 419 420 /// \brief Check loop instructions and confirms it's good for 421 /// LoopVersioningLICM. 422 bool LoopVersioningLICM::legalLoopInstructions() { 423 // Resetting counters. 424 LoadAndStoreCounter = 0; 425 InvariantCounter = 0; 426 IsReadOnlyLoop = true; 427 // Iterate over loop blocks and instructions of each block and check 428 // instruction safety. 429 for (auto *Block : CurLoop->getBlocks()) 430 for (auto &Inst : *Block) { 431 // If instruction is unsafe just return false. 432 if (!instructionSafeForVersioning(&Inst)) 433 return false; 434 } 435 // Get LoopAccessInfo from current loop. 436 LAI = &LAA->getInfo(CurLoop, Strides); 437 // Check LoopAccessInfo for need of runtime check. 438 if (LAI->getRuntimePointerChecking()->getChecks().empty()) { 439 DEBUG(dbgs() << " LAA: Runtime check not found !!\n"); 440 return false; 441 } 442 // Number of runtime-checks should be less then RuntimeMemoryCheckThreshold 443 if (LAI->getNumRuntimePointerChecks() > 444 VectorizerParams::RuntimeMemoryCheckThreshold) { 445 DEBUG(dbgs() << " LAA: Runtime checks are more than threshold !!\n"); 446 return false; 447 } 448 // Loop should have at least one invariant load or store instruction. 449 if (!InvariantCounter) { 450 DEBUG(dbgs() << " Invariant not found !!\n"); 451 return false; 452 } 453 // Read only loop not allowed. 454 if (IsReadOnlyLoop) { 455 DEBUG(dbgs() << " Found a read-only loop!\n"); 456 return false; 457 } 458 // Profitablity check: 459 // Check invariant threshold, should be in limit. 460 if (InvariantCounter * 100 < InvariantThreshold * LoadAndStoreCounter) { 461 DEBUG(dbgs() 462 << " Invariant load & store are less then defined threshold\n"); 463 DEBUG(dbgs() << " Invariant loads & stores: " 464 << ((InvariantCounter * 100) / LoadAndStoreCounter) << "%\n"); 465 DEBUG(dbgs() << " Invariant loads & store threshold: " 466 << InvariantThreshold << "%\n"); 467 return false; 468 } 469 return true; 470 } 471 472 /// \brief It checks loop is already visited or not. 473 /// check loop meta data, if loop revisited return true 474 /// else false. 475 bool LoopVersioningLICM::isLoopAlreadyVisited() { 476 // Check LoopVersioningLICM metadata into loop 477 if (checkStringMetadataIntoLoop(CurLoop, LICMVersioningMetaData)) { 478 return true; 479 } 480 return false; 481 } 482 483 /// \brief Checks legality for LoopVersioningLICM by considering following: 484 /// a) loop structure legality b) loop instruction legality 485 /// c) loop memory access legality. 486 /// Return true if legal else returns false. 487 bool LoopVersioningLICM::isLegalForVersioning() { 488 DEBUG(dbgs() << "Loop: " << *CurLoop); 489 // Make sure not re-visiting same loop again. 490 if (isLoopAlreadyVisited()) { 491 DEBUG( 492 dbgs() << " Revisiting loop in LoopVersioningLICM not allowed.\n\n"); 493 return false; 494 } 495 // Check loop structure leagality. 496 if (!legalLoopStructure()) { 497 DEBUG( 498 dbgs() << " Loop structure not suitable for LoopVersioningLICM\n\n"); 499 return false; 500 } 501 // Check loop instruction leagality. 502 if (!legalLoopInstructions()) { 503 DEBUG(dbgs() 504 << " Loop instructions not suitable for LoopVersioningLICM\n\n"); 505 return false; 506 } 507 // Check loop memory access leagality. 508 if (!legalLoopMemoryAccesses()) { 509 DEBUG(dbgs() 510 << " Loop memory access not suitable for LoopVersioningLICM\n\n"); 511 return false; 512 } 513 // Loop versioning is feasible, return true. 514 DEBUG(dbgs() << " Loop Versioning found to be beneficial\n\n"); 515 return true; 516 } 517 518 /// \brief Update loop with aggressive aliasing assumptions. 519 /// It marks no-alias to any pairs of memory operations by assuming 520 /// loop should not have any must-alias memory accesses pairs. 521 /// During LoopVersioningLICM legality we ignore loops having must 522 /// aliasing memory accesses. 523 void LoopVersioningLICM::setNoAliasToLoop(Loop *VerLoop) { 524 // Get latch terminator instruction. 525 Instruction *I = VerLoop->getLoopLatch()->getTerminator(); 526 // Create alias scope domain. 527 MDBuilder MDB(I->getContext()); 528 MDNode *NewDomain = MDB.createAnonymousAliasScopeDomain("LVDomain"); 529 StringRef Name = "LVAliasScope"; 530 SmallVector<Metadata *, 4> Scopes, NoAliases; 531 MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name); 532 // Iterate over each instruction of loop. 533 // set no-alias for all load & store instructions. 534 for (auto *Block : CurLoop->getBlocks()) { 535 for (auto &Inst : *Block) { 536 // Only interested in instruction that may modify or read memory. 537 if (!Inst.mayReadFromMemory() && !Inst.mayWriteToMemory()) 538 continue; 539 Scopes.push_back(NewScope); 540 NoAliases.push_back(NewScope); 541 // Set no-alias for current instruction. 542 Inst.setMetadata( 543 LLVMContext::MD_noalias, 544 MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_noalias), 545 MDNode::get(Inst.getContext(), NoAliases))); 546 // set alias-scope for current instruction. 547 Inst.setMetadata( 548 LLVMContext::MD_alias_scope, 549 MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_alias_scope), 550 MDNode::get(Inst.getContext(), Scopes))); 551 } 552 } 553 } 554 555 bool LoopVersioningLICM::runOnLoop(Loop *L, LPPassManager &LPM) { 556 if (skipOptnoneFunction(L)) 557 return false; 558 Changed = false; 559 // Get Analysis information. 560 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 561 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); 562 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 563 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 564 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 565 LAA = &getAnalysis<LoopAccessAnalysis>(); 566 LAI = nullptr; 567 // Set Current Loop 568 CurLoop = L; 569 // Get the preheader block. 570 Preheader = L->getLoopPreheader(); 571 // Initial allocation 572 CurAST = new AliasSetTracker(*AA); 573 574 // Loop over the body of this loop, construct AST. 575 for (auto *Block : L->getBlocks()) { 576 if (LI->getLoopFor(Block) == L) // Ignore blocks in subloop. 577 CurAST->add(*Block); // Incorporate the specified basic block 578 } 579 // Check feasiblity of LoopVersioningLICM. 580 // If versioning found to be feasible and beneficial then proceed 581 // else simply return, by cleaning up memory. 582 if (isLegalForVersioning()) { 583 // Do loop versioning. 584 // Create memcheck for memory accessed inside loop. 585 // Clone original loop, and set blocks properly. 586 LoopVersioning LVer(*LAI, CurLoop, LI, DT, SE, true); 587 LVer.versionLoop(); 588 // Set Loop Versioning metaData for original loop. 589 addStringMetadataToLoop(LVer.getNonVersionedLoop(), LICMVersioningMetaData); 590 // Set Loop Versioning metaData for version loop. 591 addStringMetadataToLoop(LVer.getVersionedLoop(), LICMVersioningMetaData); 592 // Set "llvm.mem.parallel_loop_access" metaData to versioned loop. 593 addStringMetadataToLoop(LVer.getVersionedLoop(), 594 "llvm.mem.parallel_loop_access"); 595 // Update version loop with aggressive aliasing assumption. 596 setNoAliasToLoop(LVer.getVersionedLoop()); 597 Changed = true; 598 } 599 // Delete allocated memory. 600 delete CurAST; 601 return Changed; 602 } 603 604 char LoopVersioningLICM::ID = 0; 605 INITIALIZE_PASS_BEGIN(LoopVersioningLICM, "loop-versioning-licm", 606 "Loop Versioning For LICM", false, false) 607 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 608 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 609 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) 610 INITIALIZE_PASS_DEPENDENCY(LCSSA) 611 INITIALIZE_PASS_DEPENDENCY(LoopAccessAnalysis) 612 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 613 INITIALIZE_PASS_DEPENDENCY(LoopSimplify) 614 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 615 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 616 INITIALIZE_PASS_END(LoopVersioningLICM, "loop-versioning-licm", 617 "Loop Versioning For LICM", false, false) 618 619 Pass *llvm::createLoopVersioningLICMPass() { return new LoopVersioningLICM(); } 620