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