1 //===-- 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 /// 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
25 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
26 #include "WebAssembly.h"
27 #include "WebAssemblyMachineFunctionInfo.h"
28 #include "WebAssemblySubtarget.h"
29 #include "llvm/Analysis/AliasAnalysis.h"
30 #include "llvm/CodeGen/LiveIntervals.h"
31 #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"
38 using namespace llvm;
39
40 #define DEBUG_TYPE "wasm-call-indirect-fixup"
41
42 namespace {
43 class WebAssemblyCallIndirectFixup final : public MachineFunctionPass {
getPassName() const44 StringRef getPassName() const override {
45 return "WebAssembly CallIndirect Fixup";
46 }
47
48 bool runOnMachineFunction(MachineFunction &MF) override;
49
50 public:
51 static char ID; // Pass identification, replacement for typeid
WebAssemblyCallIndirectFixup()52 WebAssemblyCallIndirectFixup() : MachineFunctionPass(ID) {}
53 };
54 } // end anonymous namespace
55
56 char WebAssemblyCallIndirectFixup::ID = 0;
57 INITIALIZE_PASS(WebAssemblyCallIndirectFixup, DEBUG_TYPE,
58 "Rewrite call_indirect argument orderings", false, false)
59
createWebAssemblyCallIndirectFixup()60 FunctionPass *llvm::createWebAssemblyCallIndirectFixup() {
61 return new WebAssemblyCallIndirectFixup();
62 }
63
GetNonPseudoCallIndirectOpcode(const MachineInstr & MI)64 static unsigned GetNonPseudoCallIndirectOpcode(const MachineInstr &MI) {
65 switch (MI.getOpcode()) {
66 using namespace WebAssembly;
67 case PCALL_INDIRECT_VOID: return CALL_INDIRECT_VOID;
68 case PCALL_INDIRECT_I32: return CALL_INDIRECT_I32;
69 case PCALL_INDIRECT_I64: return CALL_INDIRECT_I64;
70 case PCALL_INDIRECT_F32: return CALL_INDIRECT_F32;
71 case PCALL_INDIRECT_F64: return CALL_INDIRECT_F64;
72 case PCALL_INDIRECT_v16i8: return CALL_INDIRECT_v16i8;
73 case PCALL_INDIRECT_v8i16: return CALL_INDIRECT_v8i16;
74 case PCALL_INDIRECT_v4i32: return CALL_INDIRECT_v4i32;
75 case PCALL_INDIRECT_v4f32: return CALL_INDIRECT_v4f32;
76 default: return INSTRUCTION_LIST_END;
77 }
78 }
79
IsPseudoCallIndirect(const MachineInstr & MI)80 static bool IsPseudoCallIndirect(const MachineInstr &MI) {
81 return GetNonPseudoCallIndirectOpcode(MI) !=
82 WebAssembly::INSTRUCTION_LIST_END;
83 }
84
runOnMachineFunction(MachineFunction & MF)85 bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) {
86 LLVM_DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n"
87 << MF.getName() << '\n');
88
89 bool Changed = false;
90 const WebAssemblyInstrInfo *TII =
91 MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
92
93 for (MachineBasicBlock &MBB : MF) {
94 for (MachineInstr &MI : MBB) {
95 if (IsPseudoCallIndirect(MI)) {
96 LLVM_DEBUG(dbgs() << "Found call_indirect: " << MI << '\n');
97
98 // Rewrite pseudo to non-pseudo
99 const MCInstrDesc &Desc = TII->get(GetNonPseudoCallIndirectOpcode(MI));
100 MI.setDesc(Desc);
101
102 // Rewrite argument order
103 SmallVector<MachineOperand, 8> Ops;
104
105 // Set up a placeholder for the type signature immediate.
106 Ops.push_back(MachineOperand::CreateImm(0));
107
108 // Set up the flags immediate, which currently has no defined flags
109 // so it's always zero.
110 Ops.push_back(MachineOperand::CreateImm(0));
111
112 for (const MachineOperand &MO :
113 make_range(MI.operands_begin() +
114 MI.getDesc().getNumDefs() + 1,
115 MI.operands_begin() +
116 MI.getNumExplicitOperands()))
117 Ops.push_back(MO);
118 Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs()));
119
120 // Replace the instructions operands.
121 while (MI.getNumOperands() > MI.getDesc().getNumDefs())
122 MI.RemoveOperand(MI.getNumOperands() - 1);
123 for (const MachineOperand &MO : Ops)
124 MI.addOperand(MO);
125
126 LLVM_DEBUG(dbgs() << " After transform: " << MI);
127 Changed = true;
128 }
129 }
130 }
131
132 LLVM_DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n");
133
134 return Changed;
135 }
136
137