Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Scalar/BDCE.cpp
Line
Count
Source
1
//===---- BDCE.cpp - Bit-tracking dead code elimination -------------------===//
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 the Bit-Tracking Dead Code Elimination pass. Some
10
// instructions (shifts, some ands, ors, etc.) kill some of their input bits.
11
// We track these dead bits and remove instructions that compute only these
12
// dead bits.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "llvm/Transforms/Scalar/BDCE.h"
17
#include "llvm/ADT/SmallPtrSet.h"
18
#include "llvm/ADT/SmallVector.h"
19
#include "llvm/ADT/Statistic.h"
20
#include "llvm/Analysis/DemandedBits.h"
21
#include "llvm/Analysis/GlobalsModRef.h"
22
#include "llvm/Transforms/Utils/Local.h"
23
#include "llvm/IR/InstIterator.h"
24
#include "llvm/IR/Instructions.h"
25
#include "llvm/Pass.h"
26
#include "llvm/Support/Debug.h"
27
#include "llvm/Support/raw_ostream.h"
28
#include "llvm/Transforms/Scalar.h"
29
using namespace llvm;
30
31
#define DEBUG_TYPE "bdce"
32
33
STATISTIC(NumRemoved, "Number of instructions removed (unused)");
34
STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
35
36
/// If an instruction is trivialized (dead), then the chain of users of that
37
/// instruction may need to be cleared of assumptions that can no longer be
38
/// guaranteed correct.
39
63
static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
40
63
  assert(I->getType()->isIntOrIntVectorTy() &&
41
63
         "Trivializing a non-integer value?");
42
63
43
63
  // Initialize the worklist with eligible direct users.
44
63
  SmallPtrSet<Instruction *, 16> Visited;
45
63
  SmallVector<Instruction *, 16> WorkList;
46
66
  for (User *JU : I->users()) {
47
66
    // If all bits of a user are demanded, then we know that nothing below that
48
66
    // in the def-use chain needs to be changed.
49
66
    auto *J = dyn_cast<Instruction>(JU);
50
66
    if (J && J->getType()->isIntOrIntVectorTy() &&
51
66
        !DB.getDemandedBits(J).isAllOnesValue()) {
52
66
      Visited.insert(J);
53
66
      WorkList.push_back(J);
54
66
    }
55
66
56
66
    // Note that we need to check for non-int types above before asking for
57
66
    // demanded bits. Normally, the only way to reach an instruction with an
58
66
    // non-int type is via an instruction that has side effects (or otherwise
59
66
    // will demand its input bits). However, if we have a readnone function
60
66
    // that returns an unsized type (e.g., void), we must avoid asking for the
61
66
    // demanded bits of the function call's return value. A void-returning
62
66
    // readnone function is always dead (and so we can stop walking the use/def
63
66
    // chain here), but the check is necessary to avoid asserting.
64
66
  }
65
63
66
63
  // DFS through subsequent users while tracking visits to avoid cycles.
67
395
  while (!WorkList.empty()) {
68
332
    Instruction *J = WorkList.pop_back_val();
69
332
70
332
    // NSW, NUW, and exact are based on operands that might have changed.
71
332
    J->dropPoisonGeneratingFlags();
72
332
73
332
    // We do not have to worry about llvm.assume or range metadata:
74
332
    // 1. llvm.assume demands its operand, so trivializing can't change it.
75
332
    // 2. range metadata only applies to memory accesses which demand all bits.
76
332
77
347
    for (User *KU : J->users()) {
78
347
      // If all bits of a user are demanded, then we know that nothing below
79
347
      // that in the def-use chain needs to be changed.
80
347
      auto *K = dyn_cast<Instruction>(KU);
81
347
      if (K && Visited.insert(K).second && 
K->getType()->isIntOrIntVectorTy()335
&&
82
347
          
!DB.getDemandedBits(K).isAllOnesValue()335
)
83
266
        WorkList.push_back(K);
84
347
    }
85
332
  }
86
63
}
87
88
466k
static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
89
466k
  SmallVector<Instruction*, 128> Worklist;
90
466k
  bool Changed = false;
91
15.0M
  for (Instruction &I : instructions(F)) {
92
15.0M
    // If the instruction has side effects and no non-dbg uses,
93
15.0M
    // skip it. This way we avoid computing known bits on an instruction
94
15.0M
    // that will not help us.
95
15.0M
    if (I.mayHaveSideEffects() && 
I.use_empty()3.14M
)
96
2.31M
      continue;
97
12.7M
98
12.7M
    // Remove instructions that are dead, either because they were not reached
99
12.7M
    // during analysis or have no demanded bits.
100
12.7M
    if (DB.isInstructionDead(&I) ||
101
12.7M
        
(12.6M
I.getType()->isIntOrIntVectorTy()12.6M
&&
102
12.6M
         
DB.getDemandedBits(&I).isNullValue()4.98M
&&
103
12.6M
         
wouldInstructionBeTriviallyDead(&I)109
)) {
104
59.5k
      salvageDebugInfo(I);
105
59.5k
      Worklist.push_back(&I);
106
59.5k
      I.dropAllReferences();
107
59.5k
      Changed = true;
108
59.5k
      continue;
109
59.5k
    }
110
12.6M
111
26.7M
    
for (Use &U : I.operands())12.6M
{
112
26.7M
      // DemandedBits only detects dead integer uses.
113
26.7M
      if (!U->getType()->isIntOrIntVectorTy())
114
12.8M
        continue;
115
13.9M
116
13.9M
      if (!isa<Instruction>(U) && 
!isa<Argument>(U)7.91M
)
117
7.54M
        continue;
118
6.39M
119
6.39M
      if (!DB.isUseDead(&U))
120
6.39M
        continue;
121
65
122
65
      LLVM_DEBUG(dbgs() << "BDCE: Trivializing: " << U << " (all bits dead)\n");
123
65
124
65
      clearAssumptionsOfUsers(&I, DB);
125
65
126
65
      // FIXME: In theory we could substitute undef here instead of zero.
127
65
      // This should be reconsidered once we settle on the semantics of
128
65
      // undef, poison, etc.
129
65
      U.set(ConstantInt::get(U->getType(), 0));
130
65
      ++NumSimplified;
131
65
      Changed = true;
132
65
    }
133
12.6M
  }
134
466k
135
466k
  for (Instruction *&I : Worklist) {
136
59.5k
    ++NumRemoved;
137
59.5k
    I->eraseFromParent();
138
59.5k
  }
139
466k
140
466k
  return Changed;
141
466k
}
142
143
1.17k
PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
144
1.17k
  auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
145
1.17k
  if (!bitTrackingDCE(F, DB))
146
1.16k
    return PreservedAnalyses::all();
147
15
148
15
  PreservedAnalyses PA;
149
15
  PA.preserveSet<CFGAnalyses>();
150
15
  PA.preserve<GlobalsAA>();
151
15
  return PA;
152
15
}
153
154
namespace {
155
struct BDCELegacyPass : public FunctionPass {
156
  static char ID; // Pass identification, replacement for typeid
157
13.4k
  BDCELegacyPass() : FunctionPass(ID) {
158
13.4k
    initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
159
13.4k
  }
160
161
465k
  bool runOnFunction(Function &F) override {
162
465k
    if (skipFunction(F))
163
44
      return false;
164
465k
    auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
165
465k
    return bitTrackingDCE(F, DB);
166
465k
  }
167
168
13.4k
  void getAnalysisUsage(AnalysisUsage &AU) const override {
169
13.4k
    AU.setPreservesCFG();
170
13.4k
    AU.addRequired<DemandedBitsWrapperPass>();
171
13.4k
    AU.addPreserved<GlobalsAAWrapperPass>();
172
13.4k
  }
173
};
174
}
175
176
char BDCELegacyPass::ID = 0;
177
48.9k
INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
178
48.9k
                      "Bit-Tracking Dead Code Elimination", false, false)
179
48.9k
INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
180
48.9k
INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
181
                    "Bit-Tracking Dead Code Elimination", false, false)
182
183
13.4k
FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }