blob: 2537e6042b1e38e12351e01f40dce0acd91e538c [file] [log] [blame]
Derek Schuff6f697832016-10-21 16:38:07 +00001//===-- WebAssemblyCallIndirectFixup.cpp - Fix call_indirects -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Derek Schuff6f697832016-10-21 16:38:07 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file converts pseudo call_indirect instructions into real
Derek Schuff6f697832016-10-21 16:38:07 +000011/// 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
Derek Schuff6f697832016-10-21 16:38:07 +000024#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "WebAssembly.h"
Derek Schuff6f697832016-10-21 16:38:07 +000026#include "WebAssemblyMachineFunctionInfo.h"
27#include "WebAssemblySubtarget.h"
28#include "llvm/Analysis/AliasAnalysis.h"
Matthias Braunf8422972017-12-13 02:51:04 +000029#include "llvm/CodeGen/LiveIntervals.h"
Derek Schuff6f697832016-10-21 16:38:07 +000030#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"
37using namespace llvm;
38
39#define DEBUG_TYPE "wasm-call-indirect-fixup"
40
41namespace {
42class WebAssemblyCallIndirectFixup final : public MachineFunctionPass {
43 StringRef getPassName() const override {
44 return "WebAssembly CallIndirect Fixup";
45 }
46
47 bool runOnMachineFunction(MachineFunction &MF) override;
48
49public:
50 static char ID; // Pass identification, replacement for typeid
51 WebAssemblyCallIndirectFixup() : MachineFunctionPass(ID) {}
52};
53} // end anonymous namespace
54
55char WebAssemblyCallIndirectFixup::ID = 0;
Jacob Gravelle40926452018-03-30 20:36:58 +000056INITIALIZE_PASS(WebAssemblyCallIndirectFixup, DEBUG_TYPE,
57 "Rewrite call_indirect argument orderings", false, false)
58
Derek Schuff6f697832016-10-21 16:38:07 +000059FunctionPass *llvm::createWebAssemblyCallIndirectFixup() {
60 return new WebAssemblyCallIndirectFixup();
61}
62
Heejin Ahn18c56a02019-02-04 19:13:39 +000063static unsigned getNonPseudoCallIndirectOpcode(const MachineInstr &MI) {
Derek Schuff6f697832016-10-21 16:38:07 +000064 switch (MI.getOpcode()) {
65 using namespace WebAssembly;
Heejin Ahnf208f632018-09-05 01:27:38 +000066 case PCALL_INDIRECT_VOID:
67 return CALL_INDIRECT_VOID;
Thomas Livelya1d97a92019-06-26 16:17:15 +000068 case PCALL_INDIRECT_i32:
69 return CALL_INDIRECT_i32;
70 case PCALL_INDIRECT_i64:
71 return CALL_INDIRECT_i64;
72 case PCALL_INDIRECT_f32:
73 return CALL_INDIRECT_f32;
74 case PCALL_INDIRECT_f64:
75 return CALL_INDIRECT_f64;
Heejin Ahnf208f632018-09-05 01:27:38 +000076 case PCALL_INDIRECT_v16i8:
77 return CALL_INDIRECT_v16i8;
78 case PCALL_INDIRECT_v8i16:
79 return CALL_INDIRECT_v8i16;
80 case PCALL_INDIRECT_v4i32:
81 return CALL_INDIRECT_v4i32;
82 case PCALL_INDIRECT_v2i64:
83 return CALL_INDIRECT_v2i64;
84 case PCALL_INDIRECT_v4f32:
85 return CALL_INDIRECT_v4f32;
86 case PCALL_INDIRECT_v2f64:
87 return CALL_INDIRECT_v2f64;
Heejin Ahn9f96a582019-07-15 22:49:25 +000088 case PCALL_INDIRECT_exnref:
89 return CALL_INDIRECT_exnref;
Thomas Livelya1d97a92019-06-26 16:17:15 +000090 case PRET_CALL_INDIRECT:
91 return RET_CALL_INDIRECT;
Heejin Ahnf208f632018-09-05 01:27:38 +000092 default:
93 return INSTRUCTION_LIST_END;
Derek Schuff6f697832016-10-21 16:38:07 +000094 }
95}
96
Heejin Ahn18c56a02019-02-04 19:13:39 +000097static bool isPseudoCallIndirect(const MachineInstr &MI) {
98 return getNonPseudoCallIndirectOpcode(MI) !=
Derek Schuff6f697832016-10-21 16:38:07 +000099 WebAssembly::INSTRUCTION_LIST_END;
100}
101
102bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000103 LLVM_DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n"
Heejin Ahn569f0902019-01-09 23:05:21 +0000104 << "********** Function: " << MF.getName() << '\n');
Derek Schuff6f697832016-10-21 16:38:07 +0000105
106 bool Changed = false;
107 const WebAssemblyInstrInfo *TII =
108 MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
109
110 for (MachineBasicBlock &MBB : MF) {
111 for (MachineInstr &MI : MBB) {
Heejin Ahn18c56a02019-02-04 19:13:39 +0000112 if (isPseudoCallIndirect(MI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000113 LLVM_DEBUG(dbgs() << "Found call_indirect: " << MI << '\n');
Derek Schuff6f697832016-10-21 16:38:07 +0000114
115 // Rewrite pseudo to non-pseudo
Heejin Ahn18c56a02019-02-04 19:13:39 +0000116 const MCInstrDesc &Desc = TII->get(getNonPseudoCallIndirectOpcode(MI));
Derek Schuff6f697832016-10-21 16:38:07 +0000117 MI.setDesc(Desc);
118
119 // Rewrite argument order
Dan Gohmand934cb82017-02-24 23:18:00 +0000120 SmallVector<MachineOperand, 8> Ops;
121
122 // Set up a placeholder for the type signature immediate.
123 Ops.push_back(MachineOperand::CreateImm(0));
Dan Gohmanf50d9642016-10-25 16:55:52 +0000124
125 // Set up the flags immediate, which currently has no defined flags
126 // so it's always zero.
Dan Gohmand934cb82017-02-24 23:18:00 +0000127 Ops.push_back(MachineOperand::CreateImm(0));
Dan Gohmanf50d9642016-10-25 16:55:52 +0000128
Dan Gohmand934cb82017-02-24 23:18:00 +0000129 for (const MachineOperand &MO :
Heejin Ahnf208f632018-09-05 01:27:38 +0000130 make_range(MI.operands_begin() + MI.getDesc().getNumDefs() + 1,
131 MI.operands_begin() + MI.getNumExplicitOperands()))
Dan Gohmand934cb82017-02-24 23:18:00 +0000132 Ops.push_back(MO);
133 Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs()));
134
135 // Replace the instructions operands.
136 while (MI.getNumOperands() > MI.getDesc().getNumDefs())
137 MI.RemoveOperand(MI.getNumOperands() - 1);
138 for (const MachineOperand &MO : Ops)
139 MI.addOperand(MO);
Derek Schuff6f697832016-10-21 16:38:07 +0000140
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000141 LLVM_DEBUG(dbgs() << " After transform: " << MI);
Derek Schuff6f697832016-10-21 16:38:07 +0000142 Changed = true;
143 }
144 }
145 }
146
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000147 LLVM_DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n");
Derek Schuff6f697832016-10-21 16:38:07 +0000148
149 return Changed;
150}