1// WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst //
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 contains code to lower WebAssembly MachineInstrs to their
11/// corresponding MCInst records.
12///
13//===----------------------------------------------------------------------===//
14
15#include "WebAssemblyMCInstLower.h"
16#include "MCTargetDesc/WebAssemblyMCAsmInfo.h"
17#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18#include "TargetInfo/WebAssemblyTargetInfo.h"
19#include "Utils/WebAssemblyTypeUtilities.h"
20#include "WebAssemblyAsmPrinter.h"
21#include "WebAssemblyMachineFunctionInfo.h"
22#include "WebAssemblyUtilities.h"
23#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCInst.h"
30#include "llvm/MC/MCSymbolWasm.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33
34using namespace llvm;
35
36// This disables the removal of registers when lowering into MC, as required
37// by some current tests.
38static cl::opt<bool>
39 WasmKeepRegisters("wasm-keep-registers", cl::Hidden,
40 cl::desc("WebAssembly: output stack registers in"
41 " instruction output for test purposes only."),
42 cl::init(Val: false));
43
44static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
45
46MCSymbol *
47WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
48 const GlobalValue *Global = MO.getGlobal();
49 if (!isa<Function>(Val: Global)) {
50 auto *WasmSym = cast<MCSymbolWasm>(Val: Printer.getSymbol(GV: Global));
51 // If the symbol doesn't have an explicit WasmSymbolType yet and the
52 // GlobalValue is actually a WebAssembly global, then ensure the symbol is a
53 // WASM_SYMBOL_TYPE_GLOBAL.
54 if (WebAssembly::isWasmVarAddressSpace(AS: Global->getAddressSpace()) &&
55 !WasmSym->getType()) {
56 const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
57 const TargetMachine &TM = MF.getTarget();
58 const Function &CurrentFunc = MF.getFunction();
59 Type *GlobalVT = Global->getValueType();
60 SmallVector<MVT, 1> VTs;
61 computeLegalValueVTs(F: CurrentFunc, TM, Ty: GlobalVT, ValueVTs&: VTs);
62
63 WebAssembly::wasmSymbolSetType(Sym: WasmSym, GlobalVT, VTs);
64 }
65 return WasmSym;
66 }
67
68 const auto *FuncTy = cast<FunctionType>(Val: Global->getValueType());
69 const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
70 const TargetMachine &TM = MF.getTarget();
71 const Function &CurrentFunc = MF.getFunction();
72
73 SmallVector<MVT, 1> ResultMVTs;
74 SmallVector<MVT, 4> ParamMVTs;
75 const auto *const F = dyn_cast<Function>(Val: Global);
76 computeSignatureVTs(Ty: FuncTy, TargetFunc: F, ContextFunc: CurrentFunc, TM, Params&: ParamMVTs, Results&: ResultMVTs);
77 auto Signature = signatureFromMVTs(Ctx, Results: ResultMVTs, Params: ParamMVTs);
78
79 bool InvokeDetected = false;
80 auto *WasmSym = Printer.getMCSymbolForFunction(
81 F, EnableEmEH: WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj,
82 Sig: Signature, InvokeDetected);
83 WasmSym->setSignature(Signature);
84 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
85 return WasmSym;
86}
87
88MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
89 const MachineOperand &MO) const {
90 return Printer.getOrCreateWasmSymbol(Name: MO.getSymbolName());
91}
92
93MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
94 MCSymbol *Sym) const {
95 auto Spec = WebAssembly::S_None;
96 unsigned TargetFlags = MO.getTargetFlags();
97
98 switch (TargetFlags) {
99 case WebAssemblyII::MO_NO_FLAG:
100 break;
101 case WebAssemblyII::MO_GOT_TLS:
102 Spec = WebAssembly::S_GOT_TLS;
103 break;
104 case WebAssemblyII::MO_GOT:
105 Spec = WebAssembly::S_GOT;
106 break;
107 case WebAssemblyII::MO_MEMORY_BASE_REL:
108 Spec = WebAssembly::S_MBREL;
109 break;
110 case WebAssemblyII::MO_TLS_BASE_REL:
111 Spec = WebAssembly::S_TLSREL;
112 break;
113 case WebAssemblyII::MO_TABLE_BASE_REL:
114 Spec = WebAssembly::S_TBREL;
115 break;
116 default:
117 llvm_unreachable("Unknown target flag on GV operand");
118 }
119
120 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol: Sym, specifier: Spec, Ctx);
121
122 if (MO.getOffset() != 0) {
123 const auto *WasmSym = cast<MCSymbolWasm>(Val: Sym);
124 if (TargetFlags == WebAssemblyII::MO_GOT)
125 report_fatal_error(reason: "GOT symbol references do not support offsets");
126 if (WasmSym->isFunction())
127 report_fatal_error(reason: "Function addresses with offsets not supported");
128 if (WasmSym->isGlobal())
129 report_fatal_error(reason: "Global indexes with offsets not supported");
130 if (WasmSym->isTag())
131 report_fatal_error(reason: "Tag indexes with offsets not supported");
132 if (WasmSym->isTable())
133 report_fatal_error(reason: "Table indexes with offsets not supported");
134
135 Expr = MCBinaryExpr::createAdd(
136 LHS: Expr, RHS: MCConstantExpr::create(Value: MO.getOffset(), Ctx), Ctx);
137 }
138
139 return MCOperand::createExpr(Val: Expr);
140}
141
142MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
143 SmallVectorImpl<wasm::ValType> &&Returns,
144 SmallVectorImpl<wasm::ValType> &&Params) const {
145 auto Signature = Ctx.createWasmSignature();
146 Signature->Returns = std::move(Returns);
147 Signature->Params = std::move(Params);
148 MCSymbol *Sym = Printer.createTempSymbol(Name: "typeindex");
149 auto *WasmSym = cast<MCSymbolWasm>(Val: Sym);
150 WasmSym->setSignature(Signature);
151 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
152 const MCExpr *Expr =
153 MCSymbolRefExpr::create(Symbol: WasmSym, specifier: WebAssembly::S_TYPEINDEX, Ctx);
154 return MCOperand::createExpr(Val: Expr);
155}
156
157static void getFunctionReturns(const MachineInstr *MI,
158 SmallVectorImpl<wasm::ValType> &Returns) {
159 const Function &F = MI->getMF()->getFunction();
160 const TargetMachine &TM = MI->getMF()->getTarget();
161 Type *RetTy = F.getReturnType();
162 SmallVector<MVT, 4> CallerRetTys;
163 computeLegalValueVTs(F, TM, Ty: RetTy, ValueVTs&: CallerRetTys);
164 valTypesFromMVTs(In: CallerRetTys, Out&: Returns);
165}
166
167void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
168 MCInst &OutMI) const {
169 OutMI.setOpcode(MI->getOpcode());
170
171 const MCInstrDesc &Desc = MI->getDesc();
172 unsigned NumVariadicDefs = MI->getNumExplicitDefs() - Desc.getNumDefs();
173 const MachineFunction *MF = MI->getMF();
174 const auto &TLI =
175 *MF->getSubtarget<WebAssemblySubtarget>().getTargetLowering();
176 wasm::ValType PtrTy = TLI.getPointerTy(DL: MF->getDataLayout()) == MVT::i32
177 ? wasm::ValType::I32
178 : wasm::ValType::I64;
179
180 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
181 const MachineOperand &MO = MI->getOperand(i: I);
182
183 MCOperand MCOp;
184 switch (MO.getType()) {
185 default:
186 MI->print(OS&: errs());
187 llvm_unreachable("unknown operand type");
188 case MachineOperand::MO_MachineBasicBlock:
189 MI->print(OS&: errs());
190 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
191 case MachineOperand::MO_Register: {
192 // Ignore all implicit register operands.
193 if (MO.isImplicit())
194 continue;
195 const WebAssemblyFunctionInfo &MFI =
196 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
197 unsigned WAReg = MFI.getWAReg(VReg: MO.getReg());
198 MCOp = MCOperand::createReg(Reg: WAReg);
199 break;
200 }
201 case MachineOperand::MO_Immediate: {
202 unsigned DescIndex = I - NumVariadicDefs;
203 if (DescIndex < Desc.NumOperands) {
204 const MCOperandInfo &Info = Desc.operands()[DescIndex];
205 if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
206 SmallVector<wasm::ValType, 4> Returns;
207 SmallVector<wasm::ValType, 4> Params;
208
209 const MachineRegisterInfo &MRI =
210 MI->getParent()->getParent()->getRegInfo();
211 for (const MachineOperand &MO : MI->defs())
212 Returns.push_back(Elt: WebAssembly::regClassToValType(
213 RC: MRI.getRegClass(Reg: MO.getReg())->getID()));
214 for (const MachineOperand &MO : MI->explicit_uses())
215 if (MO.isReg())
216 Params.push_back(Elt: WebAssembly::regClassToValType(
217 RC: MRI.getRegClass(Reg: MO.getReg())->getID()));
218
219 // call_indirect instructions have a callee operand at the end which
220 // doesn't count as a param.
221 if (WebAssembly::isCallIndirect(Opc: MI->getOpcode()))
222 Params.pop_back();
223
224 // return_call_indirect instructions have the return type of the
225 // caller
226 if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT)
227 getFunctionReturns(MI, Returns);
228
229 MCOp = lowerTypeIndexOperand(Returns: std::move(Returns), Params: std::move(Params));
230 break;
231 }
232 if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
233 auto BT = static_cast<WebAssembly::BlockType>(MO.getImm());
234 assert(BT != WebAssembly::BlockType::Invalid);
235 if (BT == WebAssembly::BlockType::Multivalue) {
236 SmallVector<wasm::ValType, 2> Returns;
237 // Multivalue blocks are emitted in two cases:
238 // 1. When the blocks will never be exited and are at the ends of
239 // functions (see
240 // WebAssemblyCFGStackify::fixEndsAtEndOfFunction). In this case
241 // the exact multivalue signature can always be inferred from the
242 // return type of the parent function.
243 // 2. (catch_ref ...) clause in try_table instruction. Currently all
244 // tags we support (cpp_exception and c_longjmp) throws a single
245 // pointer, so the multivalue signature for this case will be
246 // (ptr, exnref). Having MO_CATCH_BLOCK_SIG target flags means
247 // this is a destination of a catch_ref.
248 if (MO.getTargetFlags() == WebAssemblyII::MO_CATCH_BLOCK_SIG) {
249 Returns = {PtrTy, wasm::ValType::EXNREF};
250 } else
251 getFunctionReturns(MI, Returns);
252 MCOp = lowerTypeIndexOperand(Returns: std::move(Returns),
253 Params: SmallVector<wasm::ValType, 4>());
254 break;
255 }
256 }
257 }
258 MCOp = MCOperand::createImm(Val: MO.getImm());
259 break;
260 }
261 case MachineOperand::MO_FPImmediate: {
262 const ConstantFP *Imm = MO.getFPImm();
263 const uint64_t BitPattern =
264 Imm->getValueAPF().bitcastToAPInt().getZExtValue();
265 if (Imm->getType()->isFloatTy())
266 MCOp = MCOperand::createSFPImm(Val: static_cast<uint32_t>(BitPattern));
267 else if (Imm->getType()->isDoubleTy())
268 MCOp = MCOperand::createDFPImm(Val: BitPattern);
269 else
270 llvm_unreachable("unknown floating point immediate type");
271 break;
272 }
273 case MachineOperand::MO_GlobalAddress:
274 MCOp = lowerSymbolOperand(MO, Sym: GetGlobalAddressSymbol(MO));
275 break;
276 case MachineOperand::MO_ExternalSymbol:
277 MCOp = lowerSymbolOperand(MO, Sym: GetExternalSymbolSymbol(MO));
278 break;
279 case MachineOperand::MO_MCSymbol:
280 assert(MO.getTargetFlags() == 0 &&
281 "WebAssembly does not use target flags on MCSymbol");
282 MCOp = lowerSymbolOperand(MO, Sym: MO.getMCSymbol());
283 break;
284 }
285
286 OutMI.addOperand(Op: MCOp);
287 }
288
289 if (!WasmKeepRegisters)
290 removeRegisterOperands(MI, OutMI);
291 else if (Desc.variadicOpsAreDefs())
292 OutMI.insert(I: OutMI.begin(), Op: MCOperand::createImm(Val: MI->getNumExplicitDefs()));
293}
294
295static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
296 // Remove all uses of stackified registers to bring the instruction format
297 // into its final stack form used thruout MC, and transition opcodes to
298 // their _S variant.
299 // We do this separate from the above code that still may need these
300 // registers for e.g. call_indirect signatures.
301 // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
302 // details.
303 // TODO: the code above creates new registers which are then removed here.
304 // That code could be slightly simplified by not doing that, though maybe
305 // it is simpler conceptually to keep the code above in "register mode"
306 // until this transition point.
307 // FIXME: we are not processing inline assembly, which contains register
308 // operands, because it is used by later target generic code.
309 if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
310 return;
311
312 // Transform to _S instruction.
313 auto RegOpcode = OutMI.getOpcode();
314 auto StackOpcode = WebAssembly::getStackOpcode(Opcode: RegOpcode);
315 assert(StackOpcode != -1 && "Failed to stackify instruction");
316 OutMI.setOpcode(StackOpcode);
317
318 // Remove register operands.
319 for (auto I = OutMI.getNumOperands(); I; --I) {
320 auto &MO = OutMI.getOperand(i: I - 1);
321 if (MO.isReg()) {
322 OutMI.erase(I: &MO);
323 }
324 }
325}
326