Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/WebAssembly/WebAssemblyCallIndirectFixup.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- WebAssemblyCallIndirectFixup.cpp - Fix call_indirects -------------===//
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 converts pseudo call_indirect instructions into real
11
/// call_indirects.
12
///
13
/// The order of arguments for a call_indirect is the arguments to the function
14
/// call, followed by the function pointer. There's no natural way to express
15
/// a machineinstr with varargs followed by one more arg, so we express it as
16
/// the function pointer followed by varargs, then rewrite it here.
17
///
18
/// We need to rewrite the order of the arguments on the machineinstrs
19
/// themselves so that register stackification knows the order they'll be
20
/// executed in.
21
///
22
//===----------------------------------------------------------------------===//
23
24
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
25
#include "WebAssembly.h"
26
#include "WebAssemblyMachineFunctionInfo.h"
27
#include "WebAssemblySubtarget.h"
28
#include "llvm/Analysis/AliasAnalysis.h"
29
#include "llvm/CodeGen/LiveIntervals.h"
30
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
31
#include "llvm/CodeGen/MachineDominators.h"
32
#include "llvm/CodeGen/MachineInstrBuilder.h"
33
#include "llvm/CodeGen/MachineRegisterInfo.h"
34
#include "llvm/CodeGen/Passes.h"
35
#include "llvm/Support/Debug.h"
36
#include "llvm/Support/raw_ostream.h"
37
using namespace llvm;
38
39
#define DEBUG_TYPE "wasm-call-indirect-fixup"
40
41
namespace {
42
class WebAssemblyCallIndirectFixup final : public MachineFunctionPass {
43
4.87k
  StringRef getPassName() const override {
44
4.87k
    return "WebAssembly CallIndirect Fixup";
45
4.87k
  }
46
47
  bool runOnMachineFunction(MachineFunction &MF) override;
48
49
public:
50
  static char ID; // Pass identification, replacement for typeid
51
426
  WebAssemblyCallIndirectFixup() : MachineFunctionPass(ID) {}
52
};
53
} // end anonymous namespace
54
55
char WebAssemblyCallIndirectFixup::ID = 0;
56
INITIALIZE_PASS(WebAssemblyCallIndirectFixup, DEBUG_TYPE,
57
                "Rewrite call_indirect argument orderings", false, false)
58
59
426
FunctionPass *llvm::createWebAssemblyCallIndirectFixup() {
60
426
  return new WebAssemblyCallIndirectFixup();
61
426
}
62
63
45.3k
static unsigned getNonPseudoCallIndirectOpcode(const MachineInstr &MI) {
64
45.3k
  switch (MI.getOpcode()) {
65
45.3k
    using namespace WebAssembly;
66
26
  case PCALL_INDIRECT_VOID:
67
26
    return CALL_INDIRECT_VOID;
68
60
  case PCALL_INDIRECT_i32:
69
60
    return CALL_INDIRECT_i32;
70
16
  case PCALL_INDIRECT_i64:
71
16
    return CALL_INDIRECT_i64;
72
8
  case PCALL_INDIRECT_f32:
73
8
    return CALL_INDIRECT_f32;
74
8
  case PCALL_INDIRECT_f64:
75
8
    return CALL_INDIRECT_f64;
76
8
  case PCALL_INDIRECT_v16i8:
77
8
    return CALL_INDIRECT_v16i8;
78
0
  case PCALL_INDIRECT_v8i16:
79
0
    return CALL_INDIRECT_v8i16;
80
0
  case PCALL_INDIRECT_v4i32:
81
0
    return CALL_INDIRECT_v4i32;
82
0
  case PCALL_INDIRECT_v2i64:
83
0
    return CALL_INDIRECT_v2i64;
84
0
  case PCALL_INDIRECT_v4f32:
85
0
    return CALL_INDIRECT_v4f32;
86
0
  case PCALL_INDIRECT_v2f64:
87
0
    return CALL_INDIRECT_v2f64;
88
0
  case PCALL_INDIRECT_exnref:
89
0
    return CALL_INDIRECT_exnref;
90
14
  case PRET_CALL_INDIRECT:
91
14
    return RET_CALL_INDIRECT;
92
45.2k
  default:
93
45.2k
    return INSTRUCTION_LIST_END;
94
45.3k
  }
95
45.3k
}
96
97
45.3k
static bool isPseudoCallIndirect(const MachineInstr &MI) {
98
45.3k
  return getNonPseudoCallIndirectOpcode(MI) !=
99
45.3k
         WebAssembly::INSTRUCTION_LIST_END;
100
45.3k
}
101
102
4.45k
bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) {
103
4.45k
  LLVM_DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n"
104
4.45k
                    << "********** Function: " << MF.getName() << '\n');
105
4.45k
106
4.45k
  bool Changed = false;
107
4.45k
  const WebAssemblyInstrInfo *TII =
108
4.45k
      MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
109
4.45k
110
5.15k
  for (MachineBasicBlock &MBB : MF) {
111
45.3k
    for (MachineInstr &MI : MBB) {
112
45.3k
      if (isPseudoCallIndirect(MI)) {
113
70
        LLVM_DEBUG(dbgs() << "Found call_indirect: " << MI << '\n');
114
70
115
70
        // Rewrite pseudo to non-pseudo
116
70
        const MCInstrDesc &Desc = TII->get(getNonPseudoCallIndirectOpcode(MI));
117
70
        MI.setDesc(Desc);
118
70
119
70
        // Rewrite argument order
120
70
        SmallVector<MachineOperand, 8> Ops;
121
70
122
70
        // Set up a placeholder for the type signature immediate.
123
70
        Ops.push_back(MachineOperand::CreateImm(0));
124
70
125
70
        // Set up the flags immediate, which currently has no defined flags
126
70
        // so it's always zero.
127
70
        Ops.push_back(MachineOperand::CreateImm(0));
128
70
129
70
        for (const MachineOperand &MO :
130
70
             make_range(MI.operands_begin() + MI.getDesc().getNumDefs() + 1,
131
70
                        MI.operands_begin() + MI.getNumExplicitOperands()))
132
94
          Ops.push_back(MO);
133
70
        Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs()));
134
70
135
70
        // Replace the instructions operands.
136
401
        while (MI.getNumOperands() > MI.getDesc().getNumDefs())
137
331
          MI.RemoveOperand(MI.getNumOperands() - 1);
138
70
        for (const MachineOperand &MO : Ops)
139
304
          MI.addOperand(MO);
140
70
141
70
        LLVM_DEBUG(dbgs() << "  After transform: " << MI);
142
70
        Changed = true;
143
70
      }
144
45.3k
    }
145
5.15k
  }
146
4.45k
147
4.45k
  LLVM_DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n");
148
4.45k
149
4.45k
  return Changed;
150
4.45k
}