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