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