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 
292   TransformationMode EnableMode = hasUnrollAndJamTransformation(L);
293   if (EnableMode & TM_Disable)
294     return LoopUnrollResult::Unmodified;
295   if (EnableMode & TM_ForcedByUser)
296     UP.UnrollAndJam = true;
297 
298   if (AllowUnrollAndJam.getNumOccurrences() > 0)
299     UP.UnrollAndJam = AllowUnrollAndJam;
300   if (UnrollAndJamThreshold.getNumOccurrences() > 0)
301     UP.UnrollAndJamInnerLoopThreshold = UnrollAndJamThreshold;
302   // Exit early if unrolling is disabled.
303   if (!UP.UnrollAndJam || UP.UnrollAndJamInnerLoopThreshold == 0)
304     return LoopUnrollResult::Unmodified;
305 
306   LLVM_DEBUG(dbgs() << "Loop Unroll and Jam: F["
307                     << L->getHeader()->getParent()->getName() << "] Loop %"
308                     << L->getHeader()->getName() << "\n");
309 
310   // A loop with any unroll pragma (enabling/disabling/count/etc) is left for
311   // the unroller, so long as it does not explicitly have unroll_and_jam
312   // metadata. This means #pragma nounroll will disable unroll and jam as well
313   // as unrolling
314   if (hasAnyUnrollPragma(L, "llvm.loop.unroll.") &&
315       !hasAnyUnrollPragma(L, "llvm.loop.unroll_and_jam.")) {
316     LLVM_DEBUG(dbgs() << "  Disabled due to pragma.\n");
317     return LoopUnrollResult::Unmodified;
318   }
319 
320   if (!isSafeToUnrollAndJam(L, SE, DT, DI, *LI)) {
321     LLVM_DEBUG(dbgs() << "  Disabled due to not being safe.\n");
322     return LoopUnrollResult::Unmodified;
323   }
324 
325   // Approximate the loop size and collect useful info
326   unsigned NumInlineCandidates;
327   bool NotDuplicatable;
328   bool Convergent;
329   SmallPtrSet<const Value *, 32> EphValues;
330   CodeMetrics::collectEphemeralValues(L, &AC, EphValues);
331   Loop *SubLoop = L->getSubLoops()[0];
332   unsigned InnerLoopSize =
333       ApproximateLoopSize(SubLoop, NumInlineCandidates, NotDuplicatable,
334                           Convergent, TTI, EphValues, UP.BEInsns);
335   unsigned OuterLoopSize =
336       ApproximateLoopSize(L, NumInlineCandidates, NotDuplicatable, Convergent,
337                           TTI, EphValues, UP.BEInsns);
338   LLVM_DEBUG(dbgs() << "  Outer Loop Size: " << OuterLoopSize << "\n");
339   LLVM_DEBUG(dbgs() << "  Inner Loop Size: " << InnerLoopSize << "\n");
340   if (NotDuplicatable) {
341     LLVM_DEBUG(dbgs() << "  Not unrolling loop which contains non-duplicatable "
342                          "instructions.\n");
343     return LoopUnrollResult::Unmodified;
344   }
345   if (NumInlineCandidates != 0) {
346     LLVM_DEBUG(dbgs() << "  Not unrolling loop with inlinable calls.\n");
347     return LoopUnrollResult::Unmodified;
348   }
349   if (Convergent) {
350     LLVM_DEBUG(
351         dbgs() << "  Not unrolling loop with convergent instructions.\n");
352     return LoopUnrollResult::Unmodified;
353   }
354 
355   // Save original loop IDs for after the transformation.
356   MDNode *OrigOuterLoopID = L->getLoopID();
357   MDNode *OrigSubLoopID = SubLoop->getLoopID();
358 
359   // To assign the loop id of the epilogue, assign it before unrolling it so it
360   // is applied to every inner loop of the epilogue. We later apply the loop ID
361   // for the jammed inner loop.
362   Optional<MDNode *> NewInnerEpilogueLoopID = makeFollowupLoopID(
363       OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll,
364                         LLVMLoopUnrollAndJamFollowupRemainderInner});
365   if (NewInnerEpilogueLoopID.hasValue())
366     SubLoop->setLoopID(NewInnerEpilogueLoopID.getValue());
367 
368   // Find trip count and trip multiple
369   BasicBlock *Latch = L->getLoopLatch();
370   BasicBlock *SubLoopLatch = SubLoop->getLoopLatch();
371   unsigned OuterTripCount = SE.getSmallConstantTripCount(L, Latch);
372   unsigned OuterTripMultiple = SE.getSmallConstantTripMultiple(L, Latch);
373   unsigned InnerTripCount = SE.getSmallConstantTripCount(SubLoop, SubLoopLatch);
374 
375   // Decide if, and by how much, to unroll
376   bool IsCountSetExplicitly = computeUnrollAndJamCount(
377       L, SubLoop, TTI, DT, LI, SE, EphValues, &ORE, OuterTripCount,
378       OuterTripMultiple, OuterLoopSize, InnerTripCount, InnerLoopSize, UP, PP);
379   if (UP.Count <= 1)
380     return LoopUnrollResult::Unmodified;
381   // Unroll factor (Count) must be less or equal to TripCount.
382   if (OuterTripCount && UP.Count > OuterTripCount)
383     UP.Count = OuterTripCount;
384 
385   Loop *EpilogueOuterLoop = nullptr;
386   LoopUnrollResult UnrollResult = UnrollAndJamLoop(
387       L, UP.Count, OuterTripCount, OuterTripMultiple, UP.UnrollRemainder, LI,
388       &SE, &DT, &AC, &TTI, &ORE, &EpilogueOuterLoop);
389 
390   // Assign new loop attributes.
391   if (EpilogueOuterLoop) {
392     Optional<MDNode *> NewOuterEpilogueLoopID = makeFollowupLoopID(
393         OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll,
394                           LLVMLoopUnrollAndJamFollowupRemainderOuter});
395     if (NewOuterEpilogueLoopID.hasValue())
396       EpilogueOuterLoop->setLoopID(NewOuterEpilogueLoopID.getValue());
397   }
398 
399   Optional<MDNode *> NewInnerLoopID =
400       makeFollowupLoopID(OrigOuterLoopID, {LLVMLoopUnrollAndJamFollowupAll,
401                                            LLVMLoopUnrollAndJamFollowupInner});
402   if (NewInnerLoopID.hasValue())
403     SubLoop->setLoopID(NewInnerLoopID.getValue());
404   else
405     SubLoop->setLoopID(OrigSubLoopID);
406 
407   if (UnrollResult == LoopUnrollResult::PartiallyUnrolled) {
408     Optional<MDNode *> NewOuterLoopID = makeFollowupLoopID(
409         OrigOuterLoopID,
410         {LLVMLoopUnrollAndJamFollowupAll, LLVMLoopUnrollAndJamFollowupOuter});
411     if (NewOuterLoopID.hasValue()) {
412       L->setLoopID(NewOuterLoopID.getValue());
413 
414       // Do not setLoopAlreadyUnrolled if a followup was given.
415       return UnrollResult;
416     }
417   }
418 
419   // If loop has an unroll count pragma or unrolled by explicitly set count
420   // mark loop as unrolled to prevent unrolling beyond that requested.
421   if (UnrollResult != LoopUnrollResult::FullyUnrolled && IsCountSetExplicitly)
422     L->setLoopAlreadyUnrolled();
423 
424   return UnrollResult;
425 }
426 
427 static bool tryToUnrollAndJamLoop(Function &F, DominatorTree &DT, LoopInfo &LI,
428                                   ScalarEvolution &SE,
429                                   const TargetTransformInfo &TTI,
430                                   AssumptionCache &AC, DependenceInfo &DI,
431                                   OptimizationRemarkEmitter &ORE,
432                                   int OptLevel) {
433   bool DidSomething = false;
434 
435   // The loop unroll and jam pass requires loops to be in simplified form, and
436   // also needs LCSSA. Since simplification may add new inner loops, it has to
437   // run before the legality and profitability checks. This means running the
438   // loop unroll and jam pass will simplify all loops, regardless of whether
439   // anything end up being unroll and jammed.
440   for (auto &L : LI) {
441     DidSomething |=
442         simplifyLoop(L, &DT, &LI, &SE, &AC, nullptr, false /* PreserveLCSSA */);
443     DidSomething |= formLCSSARecursively(*L, DT, &LI, &SE);
444   }
445 
446   // Add the loop nests in the reverse order of LoopInfo. See method
447   // declaration.
448   SmallPriorityWorklist<Loop *, 4> Worklist;
449   appendLoopsToWorklist(LI, Worklist);
450   while (!Worklist.empty()) {
451     Loop *L = Worklist.pop_back_val();
452     LoopUnrollResult Result =
453         tryToUnrollAndJamLoop(L, DT, &LI, SE, TTI, AC, DI, ORE, OptLevel);
454     if (Result != LoopUnrollResult::Unmodified)
455       DidSomething = true;
456   }
457 
458   return DidSomething;
459 }
460 
461 namespace {
462 
463 class LoopUnrollAndJam : public FunctionPass {
464 public:
465   static char ID; // Pass ID, replacement for typeid
466   unsigned OptLevel;
467 
468   LoopUnrollAndJam(int OptLevel = 2) : FunctionPass(ID), OptLevel(OptLevel) {
469     initializeLoopUnrollAndJamPass(*PassRegistry::getPassRegistry());
470   }
471 
472   bool runOnFunction(Function &F) override {
473     if (skipFunction(F))
474       return false;
475 
476     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
477     LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
478     ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
479     const TargetTransformInfo &TTI =
480         getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
481     auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
482     auto &DI = getAnalysis<DependenceAnalysisWrapperPass>().getDI();
483     auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
484 
485     return tryToUnrollAndJamLoop(F, DT, LI, SE, TTI, AC, DI, ORE, OptLevel);
486   }
487 
488   /// This transformation requires natural loop information & requires that
489   /// loop preheaders be inserted into the CFG...
490   void getAnalysisUsage(AnalysisUsage &AU) const override {
491     AU.addRequired<DominatorTreeWrapperPass>();
492     AU.addRequired<LoopInfoWrapperPass>();
493     AU.addRequired<ScalarEvolutionWrapperPass>();
494     AU.addRequired<TargetTransformInfoWrapperPass>();
495     AU.addRequired<AssumptionCacheTracker>();
496     AU.addRequired<DependenceAnalysisWrapperPass>();
497     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
498   }
499 };
500 
501 } // end anonymous namespace
502 
503 char LoopUnrollAndJam::ID = 0;
504 
505 INITIALIZE_PASS_BEGIN(LoopUnrollAndJam, "loop-unroll-and-jam",
506                       "Unroll and Jam loops", false, false)
507 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
508 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
509 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
510 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
511 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
512 INITIALIZE_PASS_DEPENDENCY(DependenceAnalysisWrapperPass)
513 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
514 INITIALIZE_PASS_END(LoopUnrollAndJam, "loop-unroll-and-jam",
515                     "Unroll and Jam loops", false, false)
516 
517 Pass *llvm::createLoopUnrollAndJamPass(int OptLevel) {
518   return new LoopUnrollAndJam(OptLevel);
519 }
520 
521 PreservedAnalyses LoopUnrollAndJamPass::run(Function &F,
522                                             FunctionAnalysisManager &AM) {
523   ScalarEvolution &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
524   LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
525   TargetTransformInfo &TTI = AM.getResult<TargetIRAnalysis>(F);
526   AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
527   DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
528   DependenceInfo &DI = AM.getResult<DependenceAnalysis>(F);
529   OptimizationRemarkEmitter &ORE =
530       AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
531 
532   if (!tryToUnrollAndJamLoop(F, DT, LI, SE, TTI, AC, DI, ORE, OptLevel))
533     return PreservedAnalyses::all();
534 
535   return getLoopPassPreservedAnalyses();
536 }
537