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