22
22
#include " llvm/Analysis/DependenceAnalysis.h"
23
23
#include " llvm/Analysis/LoopAnalysisManager.h"
24
24
#include " llvm/Analysis/LoopInfo.h"
25
+ #include " llvm/Analysis/LoopPass.h"
25
26
#include " llvm/Analysis/OptimizationRemarkEmitter.h"
26
27
#include " llvm/Analysis/ScalarEvolution.h"
27
28
#include " llvm/Analysis/TargetTransformInfo.h"
@@ -278,11 +279,10 @@ static bool computeUnrollAndJamCount(
278
279
return false ;
279
280
}
280
281
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) {
282
+ static LoopUnrollResult tryToUnrollAndJamLoop (
283
+ Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
284
+ const TargetTransformInfo &TTI, AssumptionCache &AC, DependenceInfo &DI,
285
+ OptimizationRemarkEmitter &ORE, int OptLevel, LPMUpdater *U) {
286
286
TargetTransformInfo::UnrollingPreferences UP =
287
287
gatherUnrollingPreferences (L, SE, TTI, nullptr , nullptr , OptLevel, None,
288
288
None, None, None, None, None);
@@ -385,7 +385,7 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
385
385
Loop *EpilogueOuterLoop = nullptr ;
386
386
LoopUnrollResult UnrollResult = UnrollAndJamLoop (
387
387
L, UP.Count , OuterTripCount, OuterTripMultiple, UP.UnrollRemainder , LI,
388
- &SE, &DT, &AC, &TTI, &ORE, &EpilogueOuterLoop);
388
+ &SE, &DT, &AC, &TTI, &ORE, U, &EpilogueOuterLoop);
389
389
390
390
// Assign new loop attributes.
391
391
if (EpilogueOuterLoop) {
@@ -424,33 +424,23 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
424
424
return UnrollResult;
425
425
}
426
426
427
- static bool tryToUnrollAndJamLoop (Function &F , DominatorTree &DT, LoopInfo &LI,
427
+ static bool tryToUnrollAndJamLoop (LoopNest &LN , DominatorTree &DT, LoopInfo &LI,
428
428
ScalarEvolution &SE,
429
429
const TargetTransformInfo &TTI,
430
430
AssumptionCache &AC, DependenceInfo &DI,
431
- OptimizationRemarkEmitter &ORE,
432
- int OptLevel ) {
431
+ OptimizationRemarkEmitter &ORE, int OptLevel,
432
+ LPMUpdater &U ) {
433
433
bool DidSomething = false ;
434
+ ArrayRef<Loop *> Loops = LN.getLoops ();
434
435
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
436
+ // Add the loop nests in the reverse order of LN. See method
447
437
// declaration.
448
438
SmallPriorityWorklist<Loop *, 4 > Worklist;
449
- appendLoopsToWorklist (LI , Worklist);
439
+ appendLoopsToWorklist (Loops , Worklist);
450
440
while (!Worklist.empty ()) {
451
441
Loop *L = Worklist.pop_back_val ();
452
442
LoopUnrollResult Result =
453
- tryToUnrollAndJamLoop (L, DT, &LI, SE, TTI, AC, DI, ORE, OptLevel);
443
+ tryToUnrollAndJamLoop (L, DT, &LI, SE, TTI, AC, DI, ORE, OptLevel, &U );
454
444
if (Result != LoopUnrollResult::Unmodified)
455
445
DidSomething = true ;
456
446
}
@@ -460,29 +450,35 @@ static bool tryToUnrollAndJamLoop(Function &F, DominatorTree &DT, LoopInfo &LI,
460
450
461
451
namespace {
462
452
463
- class LoopUnrollAndJam : public FunctionPass {
453
+ class LoopUnrollAndJam : public LoopPass {
464
454
public:
465
455
static char ID; // Pass ID, replacement for typeid
466
456
unsigned OptLevel;
467
457
468
- LoopUnrollAndJam (int OptLevel = 2 ) : FunctionPass (ID), OptLevel(OptLevel) {
458
+ LoopUnrollAndJam (int OptLevel = 2 ) : LoopPass (ID), OptLevel(OptLevel) {
469
459
initializeLoopUnrollAndJamPass (*PassRegistry::getPassRegistry ());
470
460
}
471
461
472
- bool runOnFunction (Function &F ) override {
473
- if (skipFunction (F ))
462
+ bool runOnLoop (Loop *L, LPPassManager &LPM ) override {
463
+ if (skipLoop (L ))
474
464
return false ;
475
465
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);
466
+ auto *F = L->getHeader ()->getParent ();
467
+ auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE ();
468
+ auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo ();
482
469
auto &DI = getAnalysis<DependenceAnalysisWrapperPass>().getDI ();
470
+ auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree ();
471
+ auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI (*F);
483
472
auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE ();
473
+ auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache (*F);
484
474
485
- return tryToUnrollAndJamLoop (F, DT, LI, SE, TTI, AC, DI, ORE, OptLevel);
475
+ LoopUnrollResult Result = tryToUnrollAndJamLoop (L, DT, LI, SE, TTI, AC, DI,
476
+ ORE, OptLevel, nullptr );
477
+
478
+ if (Result == LoopUnrollResult::FullyUnrolled)
479
+ LPM.markLoopAsDeleted (*L);
480
+
481
+ return Result != LoopUnrollResult::Unmodified;
486
482
}
487
483
488
484
// / This transformation requires natural loop information & requires that
@@ -505,7 +501,10 @@ char LoopUnrollAndJam::ID = 0;
505
501
INITIALIZE_PASS_BEGIN (LoopUnrollAndJam, " loop-unroll-and-jam" ,
506
502
" Unroll and Jam loops" , false , false )
507
503
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
504
+ INITIALIZE_PASS_DEPENDENCY(LoopPass)
508
505
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
506
+ INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
507
+ INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
509
508
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
510
509
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
511
510
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
@@ -518,19 +517,20 @@ Pass *llvm::createLoopUnrollAndJamPass(int OptLevel) {
518
517
return new LoopUnrollAndJam (OptLevel);
519
518
}
520
519
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))
520
+ PreservedAnalyses LoopUnrollAndJamPass::run (LoopNest &LN,
521
+ LoopAnalysisManager &AM,
522
+ LoopStandardAnalysisResults &AR,
523
+ LPMUpdater &U) {
524
+ Function &F = *LN.getParent ();
525
+
526
+ DependenceInfo DI (&F, &AR.AA , &AR.SE , &AR.LI );
527
+ OptimizationRemarkEmitter ORE (&F);
528
+
529
+ if (!tryToUnrollAndJamLoop (LN, AR.DT , AR.LI , AR.SE , AR.TTI , AR.AC , DI, ORE,
530
+ OptLevel, U))
533
531
return PreservedAnalyses::all ();
534
532
535
- return getLoopPassPreservedAnalyses ();
533
+ auto PA = getLoopPassPreservedAnalyses ();
534
+ PA.preserve <LoopNestAnalysis>();
535
+ return PA;
536
536
}
0 commit comments