Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/WebAssembly/WebAssemblyReplacePhysRegs.cpp
Line
Count
Source
1
//===-- WebAssemblyReplacePhysRegs.cpp - Replace phys regs with virt regs -===//
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
/// This file implements a pass that replaces physical registers with
11
/// virtual registers.
12
///
13
/// LLVM expects certain physical registers, such as a stack pointer. However,
14
/// WebAssembly doesn't actually have such physical registers. This pass is run
15
/// once LLVM no longer needs these registers, and replaces them with virtual
16
/// registers, so they can participate in register stackifying and coloring in
17
/// the normal way.
18
///
19
//===----------------------------------------------------------------------===//
20
21
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
22
#include "WebAssembly.h"
23
#include "WebAssemblyMachineFunctionInfo.h"
24
#include "WebAssemblySubtarget.h"
25
#include "llvm/CodeGen/MachineFunctionPass.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-replace-phys-regs"
33
34
namespace {
35
class WebAssemblyReplacePhysRegs final : public MachineFunctionPass {
36
public:
37
  static char ID; // Pass identification, replacement for typeid
38
426
  WebAssemblyReplacePhysRegs() : MachineFunctionPass(ID) {}
39
40
private:
41
4.87k
  StringRef getPassName() const override {
42
4.87k
    return "WebAssembly Replace Physical Registers";
43
4.87k
  }
44
45
426
  void getAnalysisUsage(AnalysisUsage &AU) const override {
46
426
    AU.setPreservesCFG();
47
426
    MachineFunctionPass::getAnalysisUsage(AU);
48
426
  }
49
50
  bool runOnMachineFunction(MachineFunction &MF) override;
51
};
52
} // end anonymous namespace
53
54
char WebAssemblyReplacePhysRegs::ID = 0;
55
INITIALIZE_PASS(WebAssemblyReplacePhysRegs, DEBUG_TYPE,
56
                "Replace physical registers with virtual registers", false,
57
                false)
58
59
426
FunctionPass *llvm::createWebAssemblyReplacePhysRegs() {
60
426
  return new WebAssemblyReplacePhysRegs();
61
426
}
62
63
4.45k
bool WebAssemblyReplacePhysRegs::runOnMachineFunction(MachineFunction &MF) {
64
4.45k
  LLVM_DEBUG({
65
4.45k
    dbgs() << "********** Replace Physical Registers **********\n"
66
4.45k
           << "********** Function: " << MF.getName() << '\n';
67
4.45k
  });
68
4.45k
69
4.45k
  MachineRegisterInfo &MRI = MF.getRegInfo();
70
4.45k
  const auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
71
4.45k
  bool Changed = false;
72
4.45k
73
4.45k
  assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
74
4.45k
         "LiveIntervals shouldn't be active yet!");
75
4.45k
  // We don't preserve SSA or liveness.
76
4.45k
  MRI.leaveSSA();
77
4.45k
  MRI.invalidateLiveness();
78
4.45k
79
4.45k
  for (unsigned PReg = WebAssembly::NoRegister + 1;
80
57.8k
       PReg < WebAssembly::NUM_TARGET_REGS; 
++PReg53.4k
) {
81
53.4k
    // Skip fake registers that are never used explicitly.
82
53.4k
    if (PReg == WebAssembly::VALUE_STACK || 
PReg == WebAssembly::ARGUMENTS48.9k
)
83
8.90k
      continue;
84
44.5k
85
44.5k
    // Replace explicit uses of the physical register with a virtual register.
86
44.5k
    const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(PReg);
87
44.5k
    unsigned VReg = WebAssembly::NoRegister;
88
47.5k
    for (auto I = MRI.reg_begin(PReg), E = MRI.reg_end(); I != E;) {
89
3.00k
      MachineOperand &MO = *I++;
90
3.00k
      if (!MO.isImplicit()) {
91
1.26k
        if (VReg == WebAssembly::NoRegister)
92
220
          VReg = MRI.createVirtualRegister(RC);
93
1.26k
        MO.setReg(VReg);
94
1.26k
        if (MO.getParent()->isDebugValue())
95
6
          MO.setIsDebug();
96
1.26k
        Changed = true;
97
1.26k
      }
98
3.00k
    }
99
44.5k
  }
100
4.45k
101
4.45k
  return Changed;
102
4.45k
}