Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Scalar/LoopRotation.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LoopRotation.cpp - Loop Rotation 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 file implements Loop Rotation Pass.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Transforms/Scalar/LoopRotation.h"
14
#include "llvm/ADT/Statistic.h"
15
#include "llvm/Analysis/InstructionSimplify.h"
16
#include "llvm/Analysis/LoopPass.h"
17
#include "llvm/Analysis/MemorySSA.h"
18
#include "llvm/Analysis/MemorySSAUpdater.h"
19
#include "llvm/Analysis/ScalarEvolution.h"
20
#include "llvm/Analysis/TargetTransformInfo.h"
21
#include "llvm/Support/Debug.h"
22
#include "llvm/Transforms/Scalar.h"
23
#include "llvm/Transforms/Scalar/LoopPassManager.h"
24
#include "llvm/Transforms/Utils/LoopRotationUtils.h"
25
#include "llvm/Transforms/Utils/LoopUtils.h"
26
using namespace llvm;
27
28
#define DEBUG_TYPE "loop-rotate"
29
30
static cl::opt<unsigned> DefaultRotationThreshold(
31
    "rotation-max-header-size", cl::init(16), cl::Hidden,
32
    cl::desc("The default maximum header size for automatic loop rotation"));
33
34
LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
35
308
    : EnableHeaderDuplication(EnableHeaderDuplication) {}
36
37
PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
38
                                      LoopStandardAnalysisResults &AR,
39
121
                                      LPMUpdater &) {
40
121
  int Threshold = EnableHeaderDuplication ? 
DefaultRotationThreshold118
:
03
;
41
121
  const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
42
121
  const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
43
121
44
121
  Optional<MemorySSAUpdater> MSSAU;
45
121
  if (AR.MSSA)
46
2
    MSSAU = MemorySSAUpdater(AR.MSSA);
47
121
  bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
48
121
                              MSSAU.hasValue() ? 
MSSAU.getPointer()2
:
nullptr119
,
49
121
                              SQ, false, Threshold, false);
50
121
51
121
  if (!Changed)
52
89
    return PreservedAnalyses::all();
53
32
54
32
  if (AR.MSSA && 
VerifyMemorySSA1
)
55
1
    AR.MSSA->verifyMemorySSA();
56
32
57
32
  auto PA = getLoopPassPreservedAnalyses();
58
32
  if (EnableMSSALoopDependency)
59
1
    PA.preserve<MemorySSAAnalysis>();
60
32
  return PA;
61
32
}
62
63
namespace {
64
65
class LoopRotateLegacyPass : public LoopPass {
66
  unsigned MaxHeaderSize;
67
68
public:
69
  static char ID; // Pass ID, replacement for typeid
70
25.9k
  LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) {
71
25.9k
    initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
72
25.9k
    if (SpecifiedMaxHeaderSize == -1)
73
23.4k
      MaxHeaderSize = DefaultRotationThreshold;
74
2.52k
    else
75
2.52k
      MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
76
25.9k
  }
77
78
  // LCSSA form makes instruction renaming easier.
79
25.9k
  void getAnalysisUsage(AnalysisUsage &AU) const override {
80
25.9k
    AU.addRequired<AssumptionCacheTracker>();
81
25.9k
    AU.addRequired<TargetTransformInfoWrapperPass>();
82
25.9k
    if (EnableMSSALoopDependency) {
83
23
      AU.addRequired<MemorySSAWrapperPass>();
84
23
      AU.addPreserved<MemorySSAWrapperPass>();
85
23
    }
86
25.9k
    getLoopAnalysisUsage(AU);
87
25.9k
  }
88
89
402k
  bool runOnLoop(Loop *L, LPPassManager &LPM) override {
90
402k
    if (skipLoop(L))
91
34
      return false;
92
402k
    Function &F = *L->getHeader()->getParent();
93
402k
94
402k
    auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
95
402k
    const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
96
402k
    auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
97
402k
    auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
98
402k
    auto *DT = DTWP ? &DTWP->getDomTree() : 
nullptr0
;
99
402k
    auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
100
402k
    auto *SE = SEWP ? &SEWP->getSE() : 
nullptr0
;
101
402k
    const SimplifyQuery SQ = getBestSimplifyQuery(*this, F);
102
402k
    Optional<MemorySSAUpdater> MSSAU;
103
402k
    if (EnableMSSALoopDependency) {
104
57
      MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
105
57
      MSSAU = MemorySSAUpdater(MSSA);
106
57
    }
107
402k
    return LoopRotation(L, LI, TTI, AC, DT, SE,
108
402k
                        MSSAU.hasValue() ? 
MSSAU.getPointer()57
:
nullptr402k
, SQ,
109
402k
                        false, MaxHeaderSize, false);
110
402k
  }
111
};
112
}
113
114
char LoopRotateLegacyPass::ID = 0;
115
48.6k
INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
116
48.6k
                      false, false)
117
48.6k
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
118
48.6k
INITIALIZE_PASS_DEPENDENCY(LoopPass)
119
48.6k
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
120
48.6k
INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
121
48.6k
INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
122
                    false)
123
124
25.9k
Pass *llvm::createLoopRotatePass(int MaxHeaderSize) {
125
25.9k
  return new LoopRotateLegacyPass(MaxHeaderSize);
126
25.9k
}