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