Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Scalar/InstSimplifyPass.cpp
Line
Count
Source
1
//===- InstSimplifyPass.cpp -----------------------------------------------===//
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
#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
10
#include "llvm/ADT/DepthFirstIterator.h"
11
#include "llvm/ADT/SmallPtrSet.h"
12
#include "llvm/ADT/Statistic.h"
13
#include "llvm/Analysis/AssumptionCache.h"
14
#include "llvm/Analysis/InstructionSimplify.h"
15
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
16
#include "llvm/Analysis/TargetLibraryInfo.h"
17
#include "llvm/IR/DataLayout.h"
18
#include "llvm/IR/Dominators.h"
19
#include "llvm/IR/Function.h"
20
#include "llvm/IR/Type.h"
21
#include "llvm/Pass.h"
22
#include "llvm/Transforms/Utils.h"
23
#include "llvm/Transforms/Utils/Local.h"
24
using namespace llvm;
25
26
#define DEBUG_TYPE "instsimplify"
27
28
STATISTIC(NumSimplified, "Number of redundant instructions removed");
29
30
static bool runImpl(Function &F, const SimplifyQuery &SQ,
31
281k
                    OptimizationRemarkEmitter *ORE) {
32
281k
  SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
33
281k
  bool Changed = false;
34
281k
35
315k
  do {
36
3.54M
    for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
37
3.54M
      // Here be subtlety: the iterator must be incremented before the loop
38
3.54M
      // body (not sure why), so a range-for loop won't work here.
39
23.0M
      for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
40
19.4M
        Instruction *I = &*BI++;
41
19.4M
        // The first time through the loop ToSimplify is empty and we try to
42
19.4M
        // simplify all instructions.  On later iterations ToSimplify is not
43
19.4M
        // empty and we only bother simplifying instructions that are in it.
44
19.4M
        if (!ToSimplify->empty() && 
!ToSimplify->count(I)6.27M
)
45
6.12M
          continue;
46
13.3M
47
13.3M
        // Don't waste time simplifying unused instructions.
48
13.3M
        if (!I->use_empty()) {
49
8.98M
          if (Value *V = SimplifyInstruction(I, SQ, ORE)) {
50
115k
            // Mark all uses for resimplification next time round the loop.
51
115k
            for (User *U : I->users())
52
166k
              Next->insert(cast<Instruction>(U));
53
115k
            I->replaceAllUsesWith(V);
54
115k
            ++NumSimplified;
55
115k
            Changed = true;
56
115k
          }
57
8.98M
        }
58
13.3M
        if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
59
115k
          // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
60
115k
          // instruction, so simply incrementing the iterator does not work.
61
115k
          // When instructions get deleted re-iterate instead.
62
115k
          BI = BB->begin();
63
115k
          BE = BB->end();
64
115k
          Changed = true;
65
115k
        }
66
13.3M
      }
67
3.54M
    }
68
315k
69
315k
    // Place the list of instructions to simplify on the next loop iteration
70
315k
    // into ToSimplify.
71
315k
    std::swap(ToSimplify, Next);
72
315k
    Next->clear();
73
315k
  } while (!ToSimplify->empty());
74
281k
75
281k
  return Changed;
76
281k
}
77
78
namespace {
79
struct InstSimplifyLegacyPass : public FunctionPass {
80
  static char ID; // Pass identification, replacement for typeid
81
13.0k
  InstSimplifyLegacyPass() : FunctionPass(ID) {
82
13.0k
    initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
83
13.0k
  }
84
85
13.0k
  void getAnalysisUsage(AnalysisUsage &AU) const override {
86
13.0k
    AU.setPreservesCFG();
87
13.0k
    AU.addRequired<DominatorTreeWrapperPass>();
88
13.0k
    AU.addRequired<AssumptionCacheTracker>();
89
13.0k
    AU.addRequired<TargetLibraryInfoWrapperPass>();
90
13.0k
    AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
91
13.0k
  }
92
93
  /// runOnFunction - Remove instructions that simplify.
94
280k
  bool runOnFunction(Function &F) override {
95
280k
    if (skipFunction(F))
96
44
      return false;
97
280k
98
280k
    const DominatorTree *DT =
99
280k
        &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
100
280k
    const TargetLibraryInfo *TLI =
101
280k
        &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
102
280k
    AssumptionCache *AC =
103
280k
        &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
104
280k
    OptimizationRemarkEmitter *ORE =
105
280k
        &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
106
280k
    const DataLayout &DL = F.getParent()->getDataLayout();
107
280k
    const SimplifyQuery SQ(DL, TLI, DT, AC);
108
280k
    return runImpl(F, SQ, ORE);
109
280k
  }
110
};
111
} // namespace
112
113
char InstSimplifyLegacyPass::ID = 0;
114
48.6k
INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
115
48.6k
                      "Remove redundant instructions", false, false)
116
48.6k
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
117
48.6k
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
118
48.6k
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
119
48.6k
INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
120
48.6k
INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
121
                    "Remove redundant instructions", false, false)
122
123
// Public interface to the simplify instructions pass.
124
12.9k
FunctionPass *llvm::createInstSimplifyLegacyPass() {
125
12.9k
  return new InstSimplifyLegacyPass();
126
12.9k
}
127
128
PreservedAnalyses InstSimplifyPass::run(Function &F,
129
921
                                        FunctionAnalysisManager &AM) {
130
921
  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
131
921
  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
132
921
  auto &AC = AM.getResult<AssumptionAnalysis>(F);
133
921
  auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
134
921
  const DataLayout &DL = F.getParent()->getDataLayout();
135
921
  const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
136
921
  bool Changed = runImpl(F, SQ, &ORE);
137
921
  if (!Changed)
138
866
    return PreservedAnalyses::all();
139
55
140
55
  PreservedAnalyses PA;
141
55
  PA.preserveSet<CFGAnalyses>();
142
55
  return PA;
143
55
}