blob: 59c10243c545ec618215cc09d3b90540762b3f2d [file] [log] [blame]
Dan Gohmane9361d52015-11-05 19:28:16 +00001// WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst //
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
Dan Gohmane9361d52015-11-05 19:28:16 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file contains code to lower WebAssembly MachineInstrs to their
Dan Gohmane9361d52015-11-05 19:28:16 +000011/// corresponding MCInst records.
12///
13//===----------------------------------------------------------------------===//
14
15#include "WebAssemblyMCInstLower.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000016#include "WebAssemblyAsmPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000017#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000018#include "WebAssemblyRuntimeLibcallSignatures.h"
Wouter van Oortmerssend8ddf832019-07-12 22:08:25 +000019#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000020#include "llvm/CodeGen/AsmPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000022#include "llvm/IR/Constants.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000023#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCInst.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000027#include "llvm/MC/MCSymbolWasm.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000028#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30using namespace llvm;
31
Thomas Livelyc63b5fc2018-10-22 21:55:26 +000032// Defines llvm::WebAssembly::getStackOpcode to convert register instructions to
33// stack instructions
34#define GET_INSTRMAP_INFO 1
35#include "WebAssemblyGenInstrInfo.inc"
36
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000037// This disables the removal of registers when lowering into MC, as required
38// by some current tests.
Heejin Ahnd6f48782019-01-30 03:21:57 +000039cl::opt<bool>
Heejin Ahnf208f632018-09-05 01:27:38 +000040 WasmKeepRegisters("wasm-keep-registers", cl::Hidden,
41 cl::desc("WebAssembly: output stack registers in"
42 " instruction output for test purposes only."),
43 cl::init(false));
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000044
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000045static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
46
Dan Gohmane9361d52015-11-05 19:28:16 +000047MCSymbol *
48WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000049 const GlobalValue *Global = MO.getGlobal();
Heejin Ahn18c56a02019-02-04 19:13:39 +000050 auto *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
Dan Gohmand934cb82017-02-24 23:18:00 +000051
52 if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
53 const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
54 const TargetMachine &TM = MF.getTarget();
David Blaikie21109242017-12-15 23:52:06 +000055 const Function &CurrentFunc = MF.getFunction();
Dan Gohmand934cb82017-02-24 23:18:00 +000056
Derek Schuff77a7a382018-10-03 22:22:48 +000057 SmallVector<MVT, 1> ResultMVTs;
58 SmallVector<MVT, 4> ParamMVTs;
Heejin Ahn18c56a02019-02-04 19:13:39 +000059 computeSignatureVTs(FuncTy, CurrentFunc, TM, ParamMVTs, ResultMVTs);
Dan Gohmand934cb82017-02-24 23:18:00 +000060
Heejin Ahn18c56a02019-02-04 19:13:39 +000061 auto Signature = signatureFromMVTs(ResultMVTs, ParamMVTs);
Derek Schuff77a7a382018-10-03 22:22:48 +000062 WasmSym->setSignature(Signature.get());
63 Printer.addSignature(std::move(Signature));
Sam Clegg6c899ba2018-02-23 05:08:34 +000064 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +000065 }
66
67 return WasmSym;
Dan Gohmane9361d52015-11-05 19:28:16 +000068}
69
Dan Gohman7a6b9822015-11-29 22:32:02 +000070MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
71 const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000072 const char *Name = MO.getSymbolName();
Heejin Ahn18c56a02019-02-04 19:13:39 +000073 auto *WasmSym = cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
Dan Gohmand934cb82017-02-24 23:18:00 +000074 const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
75
Sam Clegg492f7522019-03-26 19:46:15 +000076 // Except for certain known symbols, all symbols used by CodeGen are
77 // functions. It's OK to hardcode knowledge of specific symbols here; this
78 // method is precisely there for fetching the signatures of known
79 // Clang-provided symbols.
Guanzhong Chen42bba4b2019-07-16 22:00:45 +000080 if (strcmp(Name, "__stack_pointer") == 0 || strcmp(Name, "__tls_base") == 0 ||
81 strcmp(Name, "__memory_base") == 0 || strcmp(Name, "__table_base") == 0 ||
Guanzhong Chen5204f762019-07-19 23:34:16 +000082 strcmp(Name, "__tls_size") == 0 || strcmp(Name, "__tls_align") == 0) {
Guanzhong Chen42bba4b2019-07-16 22:00:45 +000083 bool Mutable =
84 strcmp(Name, "__stack_pointer") == 0 || strcmp(Name, "__tls_base") == 0;
Sam Clegg6c899ba2018-02-23 05:08:34 +000085 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
Sam Clegg03e101f2018-03-01 18:06:21 +000086 WasmSym->setGlobalType(wasm::WasmGlobalType{
Sam Clegg503fdea2018-03-01 18:48:08 +000087 uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
88 : wasm::WASM_TYPE_I32),
Sam Clegg492f7522019-03-26 19:46:15 +000089 Mutable});
Dan Gohmand934cb82017-02-24 23:18:00 +000090 return WasmSym;
Sam Clegg6c899ba2018-02-23 05:08:34 +000091 }
Dan Gohmand934cb82017-02-24 23:18:00 +000092
Derek Schuffe2688c42017-03-14 20:23:22 +000093 SmallVector<wasm::ValType, 4> Returns;
94 SmallVector<wasm::ValType, 4> Params;
Heejin Ahnda419bd2018-11-14 02:46:21 +000095 if (strcmp(Name, "__cpp_exception") == 0) {
96 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT);
97 // We can't confirm its signature index for now because there can be
98 // imported exceptions. Set it to be 0 for now.
99 WasmSym->setEventType(
100 {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
101 // We may have multiple C++ compilation units to be linked together, each of
102 // which defines the exception symbol. To resolve them, we declare them as
103 // weak.
104 WasmSym->setWeak(true);
105 WasmSym->setExternal(true);
106
107 // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
108 // (for wasm64) param type and void return type. The reaon is, all C++
109 // exception values are pointers, and to share the type section with
110 // functions, exceptions are assumed to have void return type.
111 Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
112 : wasm::ValType::I32);
113 } else { // Function symbols
114 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Heejin Ahn18c56a02019-02-04 19:13:39 +0000115 getLibcallSignature(Subtarget, Name, Returns, Params);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000116 }
Derek Schuff77a7a382018-10-03 22:22:48 +0000117 auto Signature =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000118 std::make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params));
Derek Schuff77a7a382018-10-03 22:22:48 +0000119 WasmSym->setSignature(Signature.get());
120 Printer.addSignature(std::move(Signature));
Dan Gohmand934cb82017-02-24 23:18:00 +0000121
122 return WasmSym;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000123}
124
Sam Cleggef4c66c2019-04-03 00:17:29 +0000125MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
126 MCSymbol *Sym) const {
Sam Clegg2a7cac92019-04-04 17:43:50 +0000127 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
128 unsigned TargetFlags = MO.getTargetFlags();
129
130 switch (TargetFlags) {
131 case WebAssemblyII::MO_NO_FLAG:
132 break;
133 case WebAssemblyII::MO_GOT:
134 Kind = MCSymbolRefExpr::VK_GOT;
135 break;
136 case WebAssemblyII::MO_MEMORY_BASE_REL:
137 Kind = MCSymbolRefExpr::VK_WASM_MBREL;
138 break;
139 case WebAssemblyII::MO_TABLE_BASE_REL:
140 Kind = MCSymbolRefExpr::VK_WASM_TBREL;
141 break;
142 default:
143 llvm_unreachable("Unknown target flag on GV operand");
144 }
145
Sam Clegg492f7522019-03-26 19:46:15 +0000146 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx);
Dan Gohmane9361d52015-11-05 19:28:16 +0000147
Sam Cleggef4c66c2019-04-03 00:17:29 +0000148 if (MO.getOffset() != 0) {
149 const auto *WasmSym = cast<MCSymbolWasm>(Sym);
Sam Clegg2a7cac92019-04-04 17:43:50 +0000150 if (TargetFlags == WebAssemblyII::MO_GOT)
Sam Clegg492f7522019-03-26 19:46:15 +0000151 report_fatal_error("GOT symbol references do not support offsets");
Sam Cleggef4c66c2019-04-03 00:17:29 +0000152 if (WasmSym->isFunction())
Dan Gohman26c67652016-01-11 23:38:05 +0000153 report_fatal_error("Function addresses with offsets not supported");
Sam Cleggef4c66c2019-04-03 00:17:29 +0000154 if (WasmSym->isGlobal())
Nicholas Wilsone408a892018-08-03 14:33:37 +0000155 report_fatal_error("Global indexes with offsets not supported");
Sam Cleggef4c66c2019-04-03 00:17:29 +0000156 if (WasmSym->isEvent())
Heejin Ahnda419bd2018-11-14 02:46:21 +0000157 report_fatal_error("Event indexes with offsets not supported");
Sam Cleggef4c66c2019-04-03 00:17:29 +0000158
159 Expr = MCBinaryExpr::createAdd(
160 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
Dan Gohmana4b710a2015-12-06 19:33:32 +0000161 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000162
163 return MCOperand::createExpr(Expr);
164}
165
Thomas Lively2cb27072019-10-15 18:28:22 +0000166MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
167 SmallVector<wasm::ValType, 1> &&Returns,
168 SmallVector<wasm::ValType, 4> &&Params) const {
169 auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
170 std::move(Params));
171 MCSymbol *Sym = Printer.createTempSymbol("typeindex");
172 auto *WasmSym = cast<MCSymbolWasm>(Sym);
173 WasmSym->setSignature(Signature.get());
174 Printer.addSignature(std::move(Signature));
175 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
176 const MCExpr *Expr =
177 MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
178 return MCOperand::createExpr(Expr);
179}
180
Dan Gohmand934cb82017-02-24 23:18:00 +0000181// Return the WebAssembly type associated with the given register class.
Derek Schuffe2688c42017-03-14 20:23:22 +0000182static wasm::ValType getType(const TargetRegisterClass *RC) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000183 if (RC == &WebAssembly::I32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000184 return wasm::ValType::I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000185 if (RC == &WebAssembly::I64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000186 return wasm::ValType::I64;
Dan Gohmand934cb82017-02-24 23:18:00 +0000187 if (RC == &WebAssembly::F32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000188 return wasm::ValType::F32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000189 if (RC == &WebAssembly::F64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000190 return wasm::ValType::F64;
Thomas Lively6f21a132018-09-20 22:04:44 +0000191 if (RC == &WebAssembly::V128RegClass)
192 return wasm::ValType::V128;
Dan Gohmand934cb82017-02-24 23:18:00 +0000193 llvm_unreachable("Unexpected register class");
194}
195
Thomas Lively2cb27072019-10-15 18:28:22 +0000196static void getFunctionReturns(const MachineInstr *MI,
197 SmallVectorImpl<wasm::ValType> &Returns) {
198 const Function &F = MI->getMF()->getFunction();
199 const TargetMachine &TM = MI->getMF()->getTarget();
200 Type *RetTy = F.getReturnType();
201 SmallVector<MVT, 4> CallerRetTys;
202 computeLegalValueVTs(F, TM, RetTy, CallerRetTys);
203 valTypesFromMVTs(CallerRetTys, Returns);
204}
205
Heejin Ahn18c56a02019-02-04 19:13:39 +0000206void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
Dan Gohmane9361d52015-11-05 19:28:16 +0000207 MCInst &OutMI) const {
208 OutMI.setOpcode(MI->getOpcode());
209
Dan Gohmand934cb82017-02-24 23:18:00 +0000210 const MCInstrDesc &Desc = MI->getDesc();
Heejin Ahn18c56a02019-02-04 19:13:39 +0000211 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
212 const MachineOperand &MO = MI->getOperand(I);
Dan Gohmane9361d52015-11-05 19:28:16 +0000213
214 MCOperand MCOp;
215 switch (MO.getType()) {
216 default:
Richard Trieu3de487b2017-01-28 03:23:49 +0000217 MI->print(errs());
Dan Gohmane9361d52015-11-05 19:28:16 +0000218 llvm_unreachable("unknown operand type");
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000219 case MachineOperand::MO_MachineBasicBlock:
Richard Trieu3de487b2017-01-28 03:23:49 +0000220 MI->print(errs());
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000221 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
Dan Gohmancf4748f2015-11-12 17:04:33 +0000222 case MachineOperand::MO_Register: {
Dan Gohmane9361d52015-11-05 19:28:16 +0000223 // Ignore all implicit register operands.
224 if (MO.isImplicit())
225 continue;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000226 const WebAssemblyFunctionInfo &MFI =
227 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
228 unsigned WAReg = MFI.getWAReg(MO.getReg());
229 MCOp = MCOperand::createReg(WAReg);
Dan Gohmane9361d52015-11-05 19:28:16 +0000230 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000231 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000232 case MachineOperand::MO_Immediate:
Heejin Ahn18c56a02019-02-04 19:13:39 +0000233 if (I < Desc.NumOperands) {
234 const MCOperandInfo &Info = Desc.OpInfo[I];
Dan Gohmand934cb82017-02-24 23:18:00 +0000235 if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000236 SmallVector<wasm::ValType, 4> Returns;
237 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +0000238
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000239 const MachineRegisterInfo &MRI =
240 MI->getParent()->getParent()->getRegInfo();
241 for (const MachineOperand &MO : MI->defs())
242 Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
243 for (const MachineOperand &MO : MI->explicit_uses())
244 if (MO.isReg())
245 Params.push_back(getType(MRI.getRegClass(MO.getReg())));
Dan Gohmand934cb82017-02-24 23:18:00 +0000246
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000247 // call_indirect instructions have a callee operand at the end which
248 // doesn't count as a param.
Wouter van Oortmerssend8ddf832019-07-12 22:08:25 +0000249 if (WebAssembly::isCallIndirect(MI->getOpcode()))
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000250 Params.pop_back();
Dan Gohmand934cb82017-02-24 23:18:00 +0000251
Thomas Livelye0a9dce2019-07-30 18:08:39 +0000252 // return_call_indirect instructions have the return type of the
253 // caller
Thomas Lively2cb27072019-10-15 18:28:22 +0000254 if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT)
255 getFunctionReturns(MI, Returns);
Thomas Livelye0a9dce2019-07-30 18:08:39 +0000256
Thomas Lively2cb27072019-10-15 18:28:22 +0000257 MCOp = lowerTypeIndexOperand(std::move(Returns), std::move(Params));
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000258 break;
Thomas Lively2cb27072019-10-15 18:28:22 +0000259 } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
260 auto BT = static_cast<WebAssembly::BlockType>(MO.getImm());
261 assert(BT != WebAssembly::BlockType::Invalid);
262 if (BT == WebAssembly::BlockType::Multivalue) {
263 SmallVector<wasm::ValType, 1> Returns;
264 getFunctionReturns(MI, Returns);
265 MCOp = lowerTypeIndexOperand(std::move(Returns),
266 SmallVector<wasm::ValType, 4>());
267 break;
268 }
Dan Gohmand934cb82017-02-24 23:18:00 +0000269 }
270 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000271 MCOp = MCOperand::createImm(MO.getImm());
272 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000273 case MachineOperand::MO_FPImmediate: {
274 // TODO: MC converts all floating point immediate operands to double.
275 // This is fine for numeric values, but may cause NaNs to change bits.
276 const ConstantFP *Imm = MO.getFPImm();
277 if (Imm->getType()->isFloatTy())
278 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
279 else if (Imm->getType()->isDoubleTy())
280 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
281 else
282 llvm_unreachable("unknown floating point immediate type");
Dan Gohmane9361d52015-11-05 19:28:16 +0000283 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000284 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000285 case MachineOperand::MO_GlobalAddress:
Sam Cleggef4c66c2019-04-03 00:17:29 +0000286 MCOp = lowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
Dan Gohmane9361d52015-11-05 19:28:16 +0000287 break;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000288 case MachineOperand::MO_ExternalSymbol:
Dan Gohman26c67652016-01-11 23:38:05 +0000289 // The target flag indicates whether this is a symbol for a
290 // variable or a function.
Sam Cleggef4c66c2019-04-03 00:17:29 +0000291 assert(MO.getTargetFlags() == 0 &&
Nicholas Wilsone408a892018-08-03 14:33:37 +0000292 "WebAssembly uses only symbol flags on ExternalSymbols");
Sam Cleggef4c66c2019-04-03 00:17:29 +0000293 MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000294 break;
Heejin Ahn24faf852018-10-25 23:55:10 +0000295 case MachineOperand::MO_MCSymbol:
296 // This is currently used only for LSDA symbols (GCC_except_table),
297 // because global addresses or other external symbols are handled above.
298 assert(MO.getTargetFlags() == 0 &&
299 "WebAssembly does not use target flags on MCSymbol");
Sam Cleggef4c66c2019-04-03 00:17:29 +0000300 MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());
Heejin Ahn24faf852018-10-25 23:55:10 +0000301 break;
Dan Gohmane9361d52015-11-05 19:28:16 +0000302 }
303
304 OutMI.addOperand(MCOp);
305 }
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000306
307 if (!WasmKeepRegisters)
308 removeRegisterOperands(MI, OutMI);
309}
310
311static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
312 // Remove all uses of stackified registers to bring the instruction format
313 // into its final stack form used thruout MC, and transition opcodes to
314 // their _S variant.
315 // We do this seperate from the above code that still may need these
316 // registers for e.g. call_indirect signatures.
317 // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
318 // details.
319 // TODO: the code above creates new registers which are then removed here.
320 // That code could be slightly simplified by not doing that, though maybe
321 // it is simpler conceptually to keep the code above in "register mode"
322 // until this transition point.
323 // FIXME: we are not processing inline assembly, which contains register
324 // operands, because it is used by later target generic code.
325 if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
326 return;
327
328 // Transform to _S instruction.
329 auto RegOpcode = OutMI.getOpcode();
Thomas Livelyc63b5fc2018-10-22 21:55:26 +0000330 auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
331 assert(StackOpcode != -1 && "Failed to stackify instruction");
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000332 OutMI.setOpcode(StackOpcode);
333
334 // Remove register operands.
335 for (auto I = OutMI.getNumOperands(); I; --I) {
336 auto &MO = OutMI.getOperand(I - 1);
337 if (MO.isReg()) {
338 OutMI.erase(&MO);
339 }
340 }
341}