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