blob: 1af92f02d8e02d8a15022fa02fada1e3df769b88 [file] [log] [blame]
Derek Schuff6f697832016-10-21 16:38:07 +00001//===-- WebAssemblyCallIndirectFixup.cpp - Fix call_indirects -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file converts pseudo call_indirect instructions into real
12/// call_indirects.
13///
14/// The order of arguments for a call_indirect is the arguments to the function
15/// call, followed by the function pointer. There's no natural way to express
16/// a machineinstr with varargs followed by one more arg, so we express it as
17/// the function pointer followed by varargs, then rewrite it here.
18///
19/// We need to rewrite the order of the arguments on the machineinstrs
20/// themselves so that register stackification knows the order they'll be
21/// executed in.
22///
23//===----------------------------------------------------------------------===//
24
Derek Schuff6f697832016-10-21 16:38:07 +000025#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
Chandler Carruth6bda14b2017-06-06 11:49:48 +000026#include "WebAssembly.h"
Derek Schuff6f697832016-10-21 16:38:07 +000027#include "WebAssemblyMachineFunctionInfo.h"
28#include "WebAssemblySubtarget.h"
29#include "llvm/Analysis/AliasAnalysis.h"
Matthias Braunf8422972017-12-13 02:51:04 +000030#include "llvm/CodeGen/LiveIntervals.h"
Derek Schuff6f697832016-10-21 16:38:07 +000031#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
32#include "llvm/CodeGen/MachineDominators.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
34#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/Passes.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/raw_ostream.h"
38using namespace llvm;
39
40#define DEBUG_TYPE "wasm-call-indirect-fixup"
41
42namespace {
43class WebAssemblyCallIndirectFixup final : public MachineFunctionPass {
44 StringRef getPassName() const override {
45 return "WebAssembly CallIndirect Fixup";
46 }
47
48 bool runOnMachineFunction(MachineFunction &MF) override;
49
50public:
51 static char ID; // Pass identification, replacement for typeid
52 WebAssemblyCallIndirectFixup() : MachineFunctionPass(ID) {}
53};
54} // end anonymous namespace
55
56char WebAssemblyCallIndirectFixup::ID = 0;
57FunctionPass *llvm::createWebAssemblyCallIndirectFixup() {
58 return new WebAssemblyCallIndirectFixup();
59}
60
61static unsigned GetNonPseudoCallIndirectOpcode(const MachineInstr &MI) {
62 switch (MI.getOpcode()) {
63 using namespace WebAssembly;
64 case PCALL_INDIRECT_VOID: return CALL_INDIRECT_VOID;
65 case PCALL_INDIRECT_I32: return CALL_INDIRECT_I32;
66 case PCALL_INDIRECT_I64: return CALL_INDIRECT_I64;
67 case PCALL_INDIRECT_F32: return CALL_INDIRECT_F32;
68 case PCALL_INDIRECT_F64: return CALL_INDIRECT_F64;
69 case PCALL_INDIRECT_v16i8: return CALL_INDIRECT_v16i8;
70 case PCALL_INDIRECT_v8i16: return CALL_INDIRECT_v8i16;
71 case PCALL_INDIRECT_v4i32: return CALL_INDIRECT_v4i32;
72 case PCALL_INDIRECT_v4f32: return CALL_INDIRECT_v4f32;
73 default: return INSTRUCTION_LIST_END;
74 }
75}
76
77static bool IsPseudoCallIndirect(const MachineInstr &MI) {
78 return GetNonPseudoCallIndirectOpcode(MI) !=
79 WebAssembly::INSTRUCTION_LIST_END;
80}
81
82bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) {
83 DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n"
84 << MF.getName() << '\n');
85
86 bool Changed = false;
87 const WebAssemblyInstrInfo *TII =
88 MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
89
90 for (MachineBasicBlock &MBB : MF) {
91 for (MachineInstr &MI : MBB) {
92 if (IsPseudoCallIndirect(MI)) {
93 DEBUG(dbgs() << "Found call_indirect: " << MI << '\n');
94
95 // Rewrite pseudo to non-pseudo
96 const MCInstrDesc &Desc = TII->get(GetNonPseudoCallIndirectOpcode(MI));
97 MI.setDesc(Desc);
98
99 // Rewrite argument order
Dan Gohmand934cb82017-02-24 23:18:00 +0000100 SmallVector<MachineOperand, 8> Ops;
101
102 // Set up a placeholder for the type signature immediate.
103 Ops.push_back(MachineOperand::CreateImm(0));
Dan Gohmanf50d9642016-10-25 16:55:52 +0000104
105 // Set up the flags immediate, which currently has no defined flags
106 // so it's always zero.
Dan Gohmand934cb82017-02-24 23:18:00 +0000107 Ops.push_back(MachineOperand::CreateImm(0));
Dan Gohmanf50d9642016-10-25 16:55:52 +0000108
Dan Gohmand934cb82017-02-24 23:18:00 +0000109 for (const MachineOperand &MO :
110 make_range(MI.operands_begin() +
111 MI.getDesc().getNumDefs() + 1,
112 MI.operands_begin() +
113 MI.getNumExplicitOperands()))
114 Ops.push_back(MO);
115 Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs()));
116
117 // Replace the instructions operands.
118 while (MI.getNumOperands() > MI.getDesc().getNumDefs())
119 MI.RemoveOperand(MI.getNumOperands() - 1);
120 for (const MachineOperand &MO : Ops)
121 MI.addOperand(MO);
Derek Schuff6f697832016-10-21 16:38:07 +0000122
123 DEBUG(dbgs() << " After transform: " << MI);
124 Changed = true;
125 }
126 }
127 }
128
129 DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n");
130
131 return Changed;
132}
133