Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/WebAssembly/WebAssemblyOptimizeLiveIntervals.cpp
Line
Count
Source
1
//===--- WebAssemblyOptimizeLiveIntervals.cpp - LiveInterval processing ---===//
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
/// \file
10
/// Optimize LiveIntervals for use in a post-RA context.
11
//
12
/// LiveIntervals normally runs before register allocation when the code is
13
/// only recently lowered out of SSA form, so it's uncommon for registers to
14
/// have multiple defs, and when they do, the defs are usually closely related.
15
/// Later, after coalescing, tail duplication, and other optimizations, it's
16
/// more common to see registers with multiple unrelated defs. This pass
17
/// updates LiveIntervals to distribute the value numbers across separate
18
/// LiveIntervals.
19
///
20
//===----------------------------------------------------------------------===//
21
22
#include "WebAssembly.h"
23
#include "WebAssemblySubtarget.h"
24
#include "llvm/CodeGen/LiveIntervals.h"
25
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
26
#include "llvm/CodeGen/MachineRegisterInfo.h"
27
#include "llvm/CodeGen/Passes.h"
28
#include "llvm/Support/Debug.h"
29
#include "llvm/Support/raw_ostream.h"
30
using namespace llvm;
31
32
#define DEBUG_TYPE "wasm-optimize-live-intervals"
33
34
namespace {
35
class WebAssemblyOptimizeLiveIntervals final : public MachineFunctionPass {
36
4.72k
  StringRef getPassName() const override {
37
4.72k
    return "WebAssembly Optimize Live Intervals";
38
4.72k
  }
39
40
413
  void getAnalysisUsage(AnalysisUsage &AU) const override {
41
413
    AU.setPreservesCFG();
42
413
    AU.addRequired<LiveIntervals>();
43
413
    AU.addPreserved<MachineBlockFrequencyInfo>();
44
413
    AU.addPreserved<SlotIndexes>();
45
413
    AU.addPreserved<LiveIntervals>();
46
413
    AU.addPreservedID(LiveVariablesID);
47
413
    AU.addPreservedID(MachineDominatorsID);
48
413
    MachineFunctionPass::getAnalysisUsage(AU);
49
413
  }
50
51
  bool runOnMachineFunction(MachineFunction &MF) override;
52
53
public:
54
  static char ID; // Pass identification, replacement for typeid
55
413
  WebAssemblyOptimizeLiveIntervals() : MachineFunctionPass(ID) {}
56
};
57
} // end anonymous namespace
58
59
char WebAssemblyOptimizeLiveIntervals::ID = 0;
60
INITIALIZE_PASS(WebAssemblyOptimizeLiveIntervals, DEBUG_TYPE,
61
                "Optimize LiveIntervals for WebAssembly", false, false)
62
63
413
FunctionPass *llvm::createWebAssemblyOptimizeLiveIntervals() {
64
413
  return new WebAssemblyOptimizeLiveIntervals();
65
413
}
66
67
bool WebAssemblyOptimizeLiveIntervals::runOnMachineFunction(
68
4.31k
    MachineFunction &MF) {
69
4.31k
  LLVM_DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n"
70
4.31k
                       "********** Function: "
71
4.31k
                    << MF.getName() << '\n');
72
4.31k
73
4.31k
  MachineRegisterInfo &MRI = MF.getRegInfo();
74
4.31k
  auto &LIS = getAnalysis<LiveIntervals>();
75
4.31k
76
4.31k
  // We don't preserve SSA form.
77
4.31k
  MRI.leaveSSA();
78
4.31k
79
4.31k
  assert(MRI.tracksLiveness() && "OptimizeLiveIntervals expects liveness");
80
4.31k
81
4.31k
  // Split multiple-VN LiveIntervals into multiple LiveIntervals.
82
4.31k
  SmallVector<LiveInterval *, 4> SplitLIs;
83
39.1k
  for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; 
++I34.7k
) {
84
34.7k
    unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
85
34.7k
    if (MRI.reg_nodbg_empty(Reg))
86
704
      continue;
87
34.0k
88
34.0k
    LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs);
89
34.0k
    SplitLIs.clear();
90
34.0k
  }
91
4.31k
92
4.31k
  // In PrepareForLiveIntervals, we conservatively inserted IMPLICIT_DEF
93
4.31k
  // instructions to satisfy LiveIntervals' requirement that all uses be
94
4.31k
  // dominated by defs. Now that LiveIntervals has computed which of these
95
4.31k
  // defs are actually needed and which are dead, remove the dead ones.
96
69.4k
  for (auto MII = MF.begin()->begin(), MIE = MF.begin()->end(); MII != MIE;) {
97
65.1k
    MachineInstr *MI = &*MII++;
98
65.1k
    if (MI->isImplicitDef() && 
MI->getOperand(0).isDead()23.0k
) {
99
23.0k
      LiveInterval &LI = LIS.getInterval(MI->getOperand(0).getReg());
100
23.0k
      LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(*MI).getRegSlot());
101
23.0k
      LIS.RemoveMachineInstrFromMaps(*MI);
102
23.0k
      MI->eraseFromParent();
103
23.0k
    }
104
65.1k
  }
105
4.31k
106
4.31k
  return false;
107
4.31k
}