1 //===- LoopUnrollAndJam.cpp - Loop unroll and jam pass --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass implements an unroll and jam pass. Most of the work is done by 10 // Utils/UnrollLoopAndJam.cpp. 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h" 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/ADT/None.h" 16 #include "llvm/ADT/Optional.h" 17 #include "llvm/ADT/PriorityWorklist.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/Analysis/AssumptionCache.h" 21 #include "llvm/Analysis/CodeMetrics.h" 22 #include "llvm/Analysis/DependenceAnalysis.h" 23 #include "llvm/Analysis/LoopAnalysisManager.h" 24 #include "llvm/Analysis/LoopInfo.h" 25 #include "llvm/Analysis/LoopPass.h" 26 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 27 #include "llvm/Analysis/ScalarEvolution.h" 28 #include "llvm/Analysis/TargetTransformInfo.h" 29 #include "llvm/IR/BasicBlock.h" 30 #include "llvm/IR/Constants.h" 31 #include "llvm/IR/Dominators.h" 32 #include "llvm/IR/Function.h" 33 #include "llvm/IR/Instructions.h" 34 #include "llvm/IR/Metadata.h" 35 #include "llvm/IR/PassManager.h" 36 #include "llvm/InitializePasses.h" 37 #include "llvm/Pass.h" 38 #include "llvm/PassRegistry.h" 39 #include "llvm/Support/Casting.h" 40 #include "llvm/Support/CommandLine.h" 41 #include "llvm/Support/Compiler.h" 42 #include "llvm/Support/Debug.h" 43 #include "llvm/Support/raw_ostream.h" 44 #include "llvm/Transforms/Scalar.h" 45 #include "llvm/Transforms/Utils.h" 46 #include "llvm/Transforms/Utils/LCSSA.h" 47 #include "llvm/Transforms/Utils/LoopPeel.h" 48 #include "llvm/Transforms/Utils/LoopSimplify.h" 49 #include "llvm/Transforms/Utils/LoopUtils.h" 50 #include "llvm/Transforms/Utils/UnrollLoop.h" 51 #include <cassert> 52 #include <cstdint> 53 #include <vector> 54 55 namespace llvm { 56 class Instruction; 57 class Value; 58 } // namespace llvm 59 60 using namespace llvm; 61 62 #define DEBUG_TYPE "loop-unroll-and-jam" 63 64 /// @{ 65 /// Metadata attribute names 66 static const char *const LLVMLoopUnrollAndJamFollowupAll = 67 "llvm.loop.unroll_and_jam.followup_all"; 68 static const char *const LLVMLoopUnrollAndJamFollowupInner = 69 "llvm.loop.unroll_and_jam.followup_inner"; 70 static const char *const LLVMLoopUnrollAndJamFollowupOuter = 71 "llvm.loop.unroll_and_jam.followup_outer"; 72 static const char *const LLVMLoopUnrollAndJamFollowupRemainderInner = 73 "llvm.loop.unroll_and_jam.followup_remainder_inner"; 74 static const char *const LLVMLoopUnrollAndJamFollowupRemainderOuter = 75 "llvm.loop.unroll_and_jam.followup_remainder_outer"; 76 /// @} 77 78 static cl::opt<bool> 79 AllowUnrollAndJam("allow-unroll-and-jam", cl::Hidden, 80 cl::desc("Allows loops to be unroll-and-jammed.")); 81 82 static cl::opt<unsigned> UnrollAndJamCount( 83 "unroll-and-jam-count", cl::Hidden, 84 cl::desc("Use this unroll count for all loops including those with " 85 "unroll_and_jam_count pragma values, for testing purposes")); 86 87 static cl::opt<unsigned> UnrollAndJamThreshold( 88 "unroll-and-jam-threshold", cl::init(60), cl::Hidden, 89 cl::desc("Threshold to use for inner loop when doing unroll and jam.")); 90 91 static cl::opt<unsigned> PragmaUnrollAndJamThreshold( 92 "pragma-unroll-and-jam-threshold", cl::init(1024), cl::Hidden, 93 cl::desc("Unrolled size limit for loops with an unroll_and_jam(full) or " 94 "unroll_count pragma.")); 95 96 // Returns the loop hint metadata node with the given name (for example, 97 // "llvm.loop.unroll.count"). If no such metadata node exists, then nullptr is 98 // returned. 99 static MDNode *getUnrollMetadataForLoop(const Loop *L, StringRef Name) { 100 if (MDNode *LoopID = L->getLoopID()) 101 return GetUnrollMetadata(LoopID, Name); 102 return nullptr; 103 } 104 105 // Returns true if the loop has any metadata starting with Prefix. For example a 106 // Prefix of "llvm.loop.unroll." returns true if we have any unroll metadata. 107 static bool hasAnyUnrollPragma(const Loop *L, StringRef Prefix) { 108 if (MDNode *LoopID = L->getLoopID()) { 109 // First operand should refer to the loop id itself. 110 assert(LoopID->getNumOperands() > 0 && "requires at least one operand"); 111 assert(LoopID->getOperand(0) == LoopID && "invalid loop id"); 112 113 for (unsigned I = 1, E = LoopID->getNumOperands(); I < E; ++I) { 114 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(I)); 115 if (!MD) 116 continue; 117 118 MDString *S = dyn_cast<MDString>(MD->getOperand(0)); 119 if (!S) 120 continue; 121 122 if (S->getString().startswith(Prefix)) 123 return true; 124 } 125 } 126 return false; 127 } 128 129 // Returns true if the loop has an unroll_and_jam(enable) pragma. 130 static bool hasUnrollAndJamEnablePragma(const Loop *L) { 131 return getUnrollMetadataForLoop(L, "llvm.loop.unroll_and_jam.enable"); 132 } 133 134 // If loop has an unroll_and_jam_count pragma return the (necessarily 135 // positive) value from the pragma. Otherwise return 0. 136 static unsigned unrollAndJamCountPragmaValue(const Loop *L) { 137 MDNode *MD = getUnrollMetadataForLoop(L, "llvm.loop.unroll_and_jam.count"); 138 if (MD) { 139 assert(MD->getNumOperands() == 2 && 140 "Unroll count hint metadata should have two operands."); 141 unsigned Count = 142 mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue(); 143 assert(Count >= 1 && "Unroll count must be positive."); 144 return Count; 145 } 146 return 0; 147 } 148 149 // Returns loop size estimation for unrolled loop. 150 static uint64_t 151 getUnrollAndJammedLoopSize(unsigned LoopSize, 152 TargetTransformInfo::UnrollingPreferences &UP) { 153 assert(LoopSize >= UP.BEInsns && "LoopSize should not be less than BEInsns!"); 154 return static_cast<uint64_t>(LoopSize - UP.BEInsns) * UP.Count + UP.BEInsns; 155 } 156 157 // Calculates unroll and jam count and writes it to UP.Count. Returns true if 158 // unroll count was set explicitly. 159 static bool computeUnrollAndJamCount( 160 Loop *L, Loop *SubLoop, const TargetTransformInfo &TTI, DominatorTree &DT, 161 LoopInfo *LI, ScalarEvolution &SE, 162 const SmallPtrSetImpl<const Value *> &EphValues, 163 OptimizationRemarkEmitter *ORE, unsigned OuterTripCount, 164 unsigned OuterTripMultiple, unsigned OuterLoopSize, unsigned InnerTripCount, 165 unsigned InnerLoopSize, TargetTransformInfo::UnrollingPreferences &UP, 166 TargetTransformInfo::PeelingPreferences &PP) { 167 // First up use computeUnrollCount from the loop unroller to get a count 168 // for unrolling the outer loop, plus any loops requiring explicit 169 // unrolling we leave to the unroller. This uses UP.Threshold / 170 // UP.PartialThreshold / UP.MaxCount to come up with sensible loop values. 171 // We have already checked that the loop has no unroll.* pragmas. 172 unsigned MaxTripCount = 0; 173 bool UseUpperBound = false; 174 bool ExplicitUnroll = computeUnrollCount( 175 L, TTI, DT, LI, SE, EphValues, ORE, OuterTripCount, MaxTripCount, 176 /*MaxOrZero*/ false, OuterTripMultiple, OuterLoopSize, UP, PP, 177 UseUpperBound); 178 if (ExplicitUnroll || UseUpperBound) { 179 // If the user explicitly set the loop as unrolled, dont UnJ it. Leave it 180 // for the unroller instead. 181 LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; explicit count set by " 182 "computeUnrollCount\n"); 183 UP.Count = 0; 184 return false; 185 } 186 187 // Override with any explicit Count from the "unroll-and-jam-count" option. 188 bool UserUnrollCount = UnrollAndJamCount.getNumOccurrences() > 0; 189 if (UserUnrollCount) { 190 UP.Count = UnrollAndJamCount; 191 UP.Force = true; 192 if (UP.AllowRemainder && 193 getUnrollAndJammedLoopSize(OuterLoopSize, UP) < UP.Threshold && 194 getUnrollAndJammedLoopSize(InnerLoopSize, UP) < 195 UP.UnrollAndJamInnerLoopThreshold) 196 return true; 197 } 198 199 // Check for unroll_and_jam pragmas 200 unsigned PragmaCount = unrollAndJamCountPragmaValue(L); 201 if (PragmaCount > 0) { 202 UP.Count = PragmaCount; 203 UP.Runtime = true; 204 UP.Force = true; 205 if ((UP.AllowRemainder || (OuterTripMultiple % PragmaCount == 0)) && 206 getUnrollAndJammedLoopSize(OuterLoopSize, UP) < UP.Threshold && 207 getUnrollAndJammedLoopSize(InnerLoopSize, UP) < 208 UP.UnrollAndJamInnerLoopThreshold) 209 return true; 210 } 211 212 bool PragmaEnableUnroll = hasUnrollAndJamEnablePragma(L); 213 bool ExplicitUnrollAndJamCount = PragmaCount > 0 || UserUnrollCount; 214 bool ExplicitUnrollAndJam = PragmaEnableUnroll || ExplicitUnrollAndJamCount; 215 216 // If the loop has an unrolling pragma, we want to be more aggressive with 217 // unrolling limits. 218 if (ExplicitUnrollAndJam) 219 UP.UnrollAndJamInnerLoopThreshold = PragmaUnrollAndJamThreshold; 220 221 if (!UP.AllowRemainder && getUnrollAndJammedLoopSize(InnerLoopSize, UP) >= 222 UP.UnrollAndJamInnerLoopThreshold) { 223 LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; can't create remainder and " 224 "inner loop too large\n"); 225 UP.Count = 0; 226 return false; 227 } 228 229 // We have a sensible limit for the outer loop, now adjust it for the inner 230 // loop and UP.UnrollAndJamInnerLoopThreshold. If the outer limit was set 231 // explicitly, we want to stick to it. 232 if (!ExplicitUnrollAndJamCount && UP.AllowRemainder) { 233 while (UP.Count != 0 && getUnrollAndJammedLoopSize(InnerLoopSize, UP) >= 234 UP.UnrollAndJamInnerLoopThreshold) 235 UP.Count--; 236 } 237 238 // If we are explicitly unroll and jamming, we are done. Otherwise there are a 239 // number of extra performance heuristics to check. 240 if (ExplicitUnrollAndJam) 241 return true; 242 243 // If the inner loop count is known and small, leave the entire loop nest to 244 // be the unroller 245 if (InnerTripCount && InnerLoopSize * InnerTripCount < UP.Threshold) { 246 LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; small inner loop count is " 247 "being left for the unroller\n"); 248 UP.Count = 0; 249 return false; 250 } 251 252 // Check for situations where UnJ is likely to be unprofitable. Including 253 // subloops with more than 1 block. 254 if (SubLoop->getBlocks().size() != 1) { 255 LLVM_DEBUG( 256 dbgs() << "Won't unroll-and-jam; More than one inner loop block\n"); 257 UP.Count = 0; 258 return false; 259 } 260 261 // Limit to loops where there is something to gain from unrolling and 262 // jamming the loop. In this case, look for loads that are invariant in the 263 // outer loop and can become shared. 264 unsigned NumInvariant = 0; 265 for (BasicBlock *BB : SubLoop->getBlocks()) { 266 for (Instruction &I : *BB) { 267 if (auto *Ld = dyn_cast<LoadInst>(&I)) { 268 Value *V = Ld->getPointerOperand(); 269 const SCEV *LSCEV = SE.getSCEVAtScope(V, L); 270 if (SE.isLoopInvariant(LSCEV, L)) 271 NumInvariant++; 272 } 273 } 274 } 275 if (NumInvariant == 0) { 276 LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; No loop invariant loads\n"); 277 UP.Count = 0; 278 return false; 279 } 280 281 return false; 282 } 283 284 static LoopUnrollResult 285 tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, 286 ScalarEvolution &SE, const TargetTransformInfo &TTI, 287 AssumptionCache &AC, DependenceInfo &DI, 288 OptimizationRemarkEmitter &ORE, int OptLevel) { 289 TargetTransformInfo::UnrollingPreferences UP = 290 gatherUnrollingPreferences(L, SE, TTI, nullptr, nullptr, OptLevel, None, 291 None, None, None, None, None); 292 TargetTransformInfo::PeelingPreferences PP = 293 gatherPeelingPreferences(L, SE, TTI, None, None); 294 295 TransformationMode EnableMode = hasUnrollAndJamTransformation(L); 296 if (EnableMode & TM_Disable) 297 return LoopUnrollResult::Unmodified; 298 if (EnableMode & TM_ForcedByUser) 299 UP.UnrollAndJam = true; 300 301 if (AllowUnrollAndJam.getNumOccurrences() > 0) 302 UP.UnrollAndJam = AllowUnrollAndJam; 303 if (UnrollAndJamThreshold.getNumOccurrences() > 0) 304 UP.UnrollAndJamInnerLoopThreshold = UnrollAndJamThreshold; 305 // Exit early if unrolling is disabled. 306 if (!UP.UnrollAndJam || UP.UnrollAndJamInnerLoopThreshold == 0) 307 return LoopUnrollResult::Unmodified; 308 309 LLVM_DEBUG(dbgs() << "Loop Unroll and Jam: F[" 310 << L->getHeader()->getParent()->getName() << "] Loop %" 311 << L->getHeader()->getName() << "\n"); 312 313 // A loop with any unroll pragma (enabling/disabling/count/etc) is left for 314 // the unroller, so long as it does not explicitly have unroll_and_jam 315 // metadata. This means #pragma nounroll will disable unroll and jam as well 316 // as unrolling 317 if (hasAnyUnrollPragma(L, "llvm.loop.unroll.") && 318 !hasAnyUnrollPragma(L, "llvm.loop.unroll_and_jam.")) { 319 LLVM_DEBUG(dbgs() << " Disabled due to pragma.\n"); 320 return LoopUnrollResult::Unmodified; 321 } 322 323 if (!isSafeToUnrollAndJam(L, SE, DT, DI, *LI)) { 324 LLVM_DEBUG(dbgs() << " Disabled due to not being safe.\n"); 325 return LoopUnrollResult::Unmodified; 326 } 327 328 // Approximate the loop size and collect useful info 329 unsigned NumInlineCandidates; 330 bool NotDuplicatable; 331 bool Convergent; 332 SmallPtrSet<const Value *, 32> EphValues; 333 CodeMetrics::collectEphemeralValues(L, &AC, EphValues); 334 Loop *SubLoop = L->getSubLoops()[0]; 335 unsigned InnerLoopSize = 336 ApproximateLoopSize(SubLoop, NumInlineCandidates, NotDuplicatable, 337 Convergent, TTI, EphValues, UP.BEInsns); 338 unsigned OuterLoopSize = 339 ApproximateLoopSize(L, NumInlineCandidates, NotDuplicatable, Convergent, 340 TTI, EphValues, UP.BEInsns); 341 LLVM_DEBUG(dbgs() << " Outer Loop Size: " << OuterLoopSize << "\n"); 342 LLVM_DEBUG(dbgs() << " Inner Loop Size: " << InnerLoopSize << "\n"); 343 if (NotDuplicatable) { 344 LLVM_DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable " 345 "instructions.\n"); 346 return LoopUnrollResult::Unmodified; 347 } 348 if (NumInlineCandidates != 0) { 349 LLVM_DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n"); 350 return LoopUnrollResult::Unmodified; 351 } 352 if (Convergent) { 353 LLVM_DEBUG( 354 dbgs() << " Not unrolling loop with convergent instructions.\n"); 355 return LoopUnrollResult::Unmodified; 356 } 357 358 // Save original loop IDs for after the transformation. 359 MDNode *OrigOuterLoopID = L->getLoopID(); 360 MDNode *OrigSubLoopID = SubLoop->getLoopID(); 361 362 // To assign the loop id of the epilogue, assign it before unrolling it so it 363 // is applied to every inner loop of the epilogue. We later apply the loop ID 364 // for the jammed inner loop. 365 Optional<MDNode *> NewInnerEpilogueLoopID = makeFollowupLoopID( 366 OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll, 367 LLVMLoopUnrollAndJamFollowupRemainderInner}); 368 if (NewInnerEpilogueLoopID.hasValue()) 369 SubLoop->setLoopID(NewInnerEpilogueLoopID.getValue()); 370 371 // Find trip count and trip multiple 372 BasicBlock *Latch = L->getLoopLatch(); 373 BasicBlock *SubLoopLatch = SubLoop->getLoopLatch(); 374 unsigned OuterTripCount = SE.getSmallConstantTripCount(L, Latch); 375 unsigned OuterTripMultiple = SE.getSmallConstantTripMultiple(L, Latch); 376 unsigned InnerTripCount = SE.getSmallConstantTripCount(SubLoop, SubLoopLatch); 377 378 // Decide if, and by how much, to unroll 379 bool IsCountSetExplicitly = computeUnrollAndJamCount( 380 L, SubLoop, TTI, DT, LI, SE, EphValues, &ORE, OuterTripCount, 381 OuterTripMultiple, OuterLoopSize, InnerTripCount, InnerLoopSize, UP, PP); 382 if (UP.Count <= 1) 383 return LoopUnrollResult::Unmodified; 384 // Unroll factor (Count) must be less or equal to TripCount. 385 if (OuterTripCount && UP.Count > OuterTripCount) 386 UP.Count = OuterTripCount; 387 388 Loop *EpilogueOuterLoop = nullptr; 389 LoopUnrollResult UnrollResult = UnrollAndJamLoop( 390 L, UP.Count, OuterTripCount, OuterTripMultiple, UP.UnrollRemainder, LI, 391 &SE, &DT, &AC, &TTI, &ORE, &EpilogueOuterLoop); 392 393 // Assign new loop attributes. 394 if (EpilogueOuterLoop) { 395 Optional<MDNode *> NewOuterEpilogueLoopID = makeFollowupLoopID( 396 OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll, 397 LLVMLoopUnrollAndJamFollowupRemainderOuter}); 398 if (NewOuterEpilogueLoopID.hasValue()) 399 EpilogueOuterLoop->setLoopID(NewOuterEpilogueLoopID.getValue()); 400 } 401 402 Optional<MDNode *> NewInnerLoopID = 403 makeFollowupLoopID(OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll, 404 LLVMLoopUnrollAndJamFollowupInner}); 405 if (NewInnerLoopID.hasValue()) 406 SubLoop->setLoopID(NewInnerLoopID.getValue()); 407 else 408 SubLoop->setLoopID(OrigSubLoopID); 409 410 if (UnrollResult == LoopUnrollResult::PartiallyUnrolled) { 411 Optional<MDNode *> NewOuterLoopID = makeFollowupLoopID( 412 OrigOuterLoopID, 413 {LLVMLoopUnrollAndJamFollowupAll, LLVMLoopUnrollAndJamFollowupOuter}); 414 if (NewOuterLoopID.hasValue()) { 415 L->setLoopID(NewOuterLoopID.getValue()); 416 417 // Do not setLoopAlreadyUnrolled if a followup was given. 418 return UnrollResult; 419 } 420 } 421 422 // If loop has an unroll count pragma or unrolled by explicitly set count 423 // mark loop as unrolled to prevent unrolling beyond that requested. 424 if (UnrollResult != LoopUnrollResult::FullyUnrolled && IsCountSetExplicitly) 425 L->setLoopAlreadyUnrolled(); 426 427 return UnrollResult; 428 } 429 430 static bool tryToUnrollAndJamLoop(LoopNest &LN, DominatorTree &DT, LoopInfo &LI, 431 ScalarEvolution &SE, 432 const TargetTransformInfo &TTI, 433 AssumptionCache &AC, DependenceInfo &DI, 434 OptimizationRemarkEmitter &ORE, int OptLevel, 435 LPMUpdater &U) { 436 bool DidSomething = false; 437 ArrayRef<Loop *> Loops = LN.getLoops(); 438 Loop *OutmostLoop = &LN.getOutermostLoop(); 439 440 // Add the loop nests in the reverse order of LN. See method 441 // declaration. 442 SmallPriorityWorklist<Loop *, 4> Worklist; 443 appendLoopsToWorklist(Loops, Worklist); 444 while (!Worklist.empty()) { 445 Loop *L = Worklist.pop_back_val(); 446 std::string LoopName = std::string(L->getName()); 447 LoopUnrollResult Result = 448 tryToUnrollAndJamLoop(L, DT, &LI, SE, TTI, AC, DI, ORE, OptLevel); 449 if (Result != LoopUnrollResult::Unmodified) 450 DidSomething = true; 451 if (L == OutmostLoop && Result == LoopUnrollResult::FullyUnrolled) 452 U.markLoopAsDeleted(*L, LoopName); 453 } 454 455 return DidSomething; 456 } 457 458 namespace { 459 460 class LoopUnrollAndJam : public LoopPass { 461 public: 462 static char ID; // Pass ID, replacement for typeid 463 unsigned OptLevel; 464 465 LoopUnrollAndJam(int OptLevel = 2) : LoopPass(ID), OptLevel(OptLevel) { 466 initializeLoopUnrollAndJamPass(*PassRegistry::getPassRegistry()); 467 } 468 469 bool runOnLoop(Loop *L, LPPassManager &LPM) override { 470 if (skipLoop(L)) 471 return false; 472 473 auto *F = L->getHeader()->getParent(); 474 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 475 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 476 auto &DI = getAnalysis<DependenceAnalysisWrapperPass>().getDI(); 477 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 478 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*F); 479 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); 480 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F); 481 482 LoopUnrollResult Result = 483 tryToUnrollAndJamLoop(L, DT, LI, SE, TTI, AC, DI, ORE, OptLevel); 484 485 if (Result == LoopUnrollResult::FullyUnrolled) 486 LPM.markLoopAsDeleted(*L); 487 488 return Result != LoopUnrollResult::Unmodified; 489 } 490 491 /// This transformation requires natural loop information & requires that 492 /// loop preheaders be inserted into the CFG... 493 void getAnalysisUsage(AnalysisUsage &AU) const override { 494 AU.addRequired<DominatorTreeWrapperPass>(); 495 AU.addRequired<LoopInfoWrapperPass>(); 496 AU.addRequired<ScalarEvolutionWrapperPass>(); 497 AU.addRequired<TargetTransformInfoWrapperPass>(); 498 AU.addRequired<AssumptionCacheTracker>(); 499 AU.addRequired<DependenceAnalysisWrapperPass>(); 500 AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); 501 getLoopAnalysisUsage(AU); 502 } 503 }; 504 505 } // end anonymous namespace 506 507 char LoopUnrollAndJam::ID = 0; 508 509 INITIALIZE_PASS_BEGIN(LoopUnrollAndJam, "loop-unroll-and-jam", 510 "Unroll and Jam loops", false, false) 511 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 512 INITIALIZE_PASS_DEPENDENCY(LoopPass) 513 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 514 INITIALIZE_PASS_DEPENDENCY(LoopSimplify) 515 INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) 516 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 517 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 518 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 519 INITIALIZE_PASS_DEPENDENCY(DependenceAnalysisWrapperPass) 520 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) 521 INITIALIZE_PASS_END(LoopUnrollAndJam, "loop-unroll-and-jam", 522 "Unroll and Jam loops", false, false) 523 524 Pass *llvm::createLoopUnrollAndJamPass(int OptLevel) { 525 return new LoopUnrollAndJam(OptLevel); 526 } 527 528 PreservedAnalyses LoopUnrollAndJamPass::run(LoopNest &LN, 529 LoopAnalysisManager &AM, 530 LoopStandardAnalysisResults &AR, 531 LPMUpdater &U) { 532 Function &F = *LN.getParent(); 533 534 DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI); 535 OptimizationRemarkEmitter ORE(&F); 536 537 if (!tryToUnrollAndJamLoop(LN, AR.DT, AR.LI, AR.SE, AR.TTI, AR.AC, DI, ORE, 538 OptLevel, U)) 539 return PreservedAnalyses::all(); 540 541 auto PA = getLoopPassPreservedAnalyses(); 542 PA.preserve<LoopNestAnalysis>(); 543 return PA; 544 } 545