xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
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 "TargetInfo/WebAssemblyTargetInfo.h"
17 #include "Utils/WebAssemblyTypeUtilities.h"
18 #include "Utils/WebAssemblyUtilities.h"
19 #include "WebAssemblyAsmPrinter.h"
20 #include "WebAssemblyISelLowering.h"
21 #include "WebAssemblyMachineFunctionInfo.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCSymbolWasm.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 
33 using namespace llvm;
34 
35 // This disables the removal of registers when lowering into MC, as required
36 // by some current tests.
37 cl::opt<bool>
38     WasmKeepRegisters("wasm-keep-registers", cl::Hidden,
39                       cl::desc("WebAssembly: output stack registers in"
40                                " instruction output for test purposes only."),
41                       cl::init(false));
42 
43 extern cl::opt<bool> WasmEnableEmEH;
44 extern cl::opt<bool> WasmEnableEmSjLj;
45 
46 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
47 
48 MCSymbol *
49 WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
50   const GlobalValue *Global = MO.getGlobal();
51   if (!isa<Function>(Global)) {
52     auto *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
53     // If the symbol doesn't have an explicit WasmSymbolType yet and the
54     // GlobalValue is actually a WebAssembly global, then ensure the symbol is a
55     // WASM_SYMBOL_TYPE_GLOBAL.
56     if (WebAssembly::isWasmVarAddressSpace(Global->getAddressSpace()) &&
57         !WasmSym->getType()) {
58       const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
59       const TargetMachine &TM = MF.getTarget();
60       const Function &CurrentFunc = MF.getFunction();
61       Type *GlobalVT = Global->getValueType();
62       SmallVector<MVT, 1> VTs;
63       computeLegalValueVTs(CurrentFunc, TM, GlobalVT, VTs);
64 
65       // Tables are represented as Arrays in LLVM IR therefore
66       // they reach this point as aggregate Array types with an element type
67       // that is a reference type.
68       wasm::ValType Type;
69       if (GlobalVT->isArrayTy() &&
70           WebAssembly::isRefType(GlobalVT->getArrayElementType())) {
71         MVT VT;
72         switch (GlobalVT->getArrayElementType()->getPointerAddressSpace()) {
73         case WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF:
74           VT = MVT::funcref;
75           break;
76         case WebAssembly::WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF:
77           VT = MVT::externref;
78           break;
79         default:
80           report_fatal_error("unhandled address space type");
81         }
82         Type = WebAssembly::toValType(VT);
83       } else if (VTs.size() == 1) {
84         Type = WebAssembly::toValType(VTs[0]);
85       } else
86         report_fatal_error("Aggregate globals not yet implemented");
87 
88       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
89       WasmSym->setGlobalType(
90           wasm::WasmGlobalType{uint8_t(Type), /*Mutable=*/true});
91     }
92     return WasmSym;
93   }
94 
95   const auto *FuncTy = cast<FunctionType>(Global->getValueType());
96   const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
97   const TargetMachine &TM = MF.getTarget();
98   const Function &CurrentFunc = MF.getFunction();
99 
100   SmallVector<MVT, 1> ResultMVTs;
101   SmallVector<MVT, 4> ParamMVTs;
102   const auto *const F = dyn_cast<Function>(Global);
103   computeSignatureVTs(FuncTy, F, CurrentFunc, TM, ParamMVTs, ResultMVTs);
104   auto Signature = signatureFromMVTs(ResultMVTs, ParamMVTs);
105 
106   bool InvokeDetected = false;
107   auto *WasmSym = Printer.getMCSymbolForFunction(
108       F, WasmEnableEmEH || WasmEnableEmSjLj, Signature.get(), InvokeDetected);
109   WasmSym->setSignature(Signature.get());
110   Printer.addSignature(std::move(Signature));
111   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
112   return WasmSym;
113 }
114 
115 MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
116     const MachineOperand &MO) const {
117   return Printer.getOrCreateWasmSymbol(MO.getSymbolName());
118 }
119 
120 MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
121                                                      MCSymbol *Sym) const {
122   MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
123   unsigned TargetFlags = MO.getTargetFlags();
124 
125   switch (TargetFlags) {
126     case WebAssemblyII::MO_NO_FLAG:
127       break;
128     case WebAssemblyII::MO_GOT_TLS:
129       Kind = MCSymbolRefExpr::VK_WASM_GOT_TLS;
130       break;
131     case WebAssemblyII::MO_GOT:
132       Kind = MCSymbolRefExpr::VK_GOT;
133       break;
134     case WebAssemblyII::MO_MEMORY_BASE_REL:
135       Kind = MCSymbolRefExpr::VK_WASM_MBREL;
136       break;
137     case WebAssemblyII::MO_TLS_BASE_REL:
138       Kind = MCSymbolRefExpr::VK_WASM_TLSREL;
139       break;
140     case WebAssemblyII::MO_TABLE_BASE_REL:
141       Kind = MCSymbolRefExpr::VK_WASM_TBREL;
142       break;
143     default:
144       llvm_unreachable("Unknown target flag on GV operand");
145   }
146 
147   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx);
148 
149   if (MO.getOffset() != 0) {
150     const auto *WasmSym = cast<MCSymbolWasm>(Sym);
151     if (TargetFlags == WebAssemblyII::MO_GOT)
152       report_fatal_error("GOT symbol references do not support offsets");
153     if (WasmSym->isFunction())
154       report_fatal_error("Function addresses with offsets not supported");
155     if (WasmSym->isGlobal())
156       report_fatal_error("Global indexes with offsets not supported");
157     if (WasmSym->isTag())
158       report_fatal_error("Tag indexes with offsets not supported");
159     if (WasmSym->isTable())
160       report_fatal_error("Table indexes with offsets not supported");
161 
162     Expr = MCBinaryExpr::createAdd(
163         Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
164   }
165 
166   return MCOperand::createExpr(Expr);
167 }
168 
169 MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
170     SmallVector<wasm::ValType, 1> &&Returns,
171     SmallVector<wasm::ValType, 4> &&Params) const {
172   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
173                                                          std::move(Params));
174   MCSymbol *Sym = Printer.createTempSymbol("typeindex");
175   auto *WasmSym = cast<MCSymbolWasm>(Sym);
176   WasmSym->setSignature(Signature.get());
177   Printer.addSignature(std::move(Signature));
178   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
179   const MCExpr *Expr =
180       MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
181   return MCOperand::createExpr(Expr);
182 }
183 
184 // Return the WebAssembly type associated with the given register class.
185 static wasm::ValType getType(const TargetRegisterClass *RC) {
186   if (RC == &WebAssembly::I32RegClass)
187     return wasm::ValType::I32;
188   if (RC == &WebAssembly::I64RegClass)
189     return wasm::ValType::I64;
190   if (RC == &WebAssembly::F32RegClass)
191     return wasm::ValType::F32;
192   if (RC == &WebAssembly::F64RegClass)
193     return wasm::ValType::F64;
194   if (RC == &WebAssembly::V128RegClass)
195     return wasm::ValType::V128;
196   if (RC == &WebAssembly::EXTERNREFRegClass)
197     return wasm::ValType::EXTERNREF;
198   if (RC == &WebAssembly::FUNCREFRegClass)
199     return wasm::ValType::FUNCREF;
200   llvm_unreachable("Unexpected register class");
201 }
202 
203 static void getFunctionReturns(const MachineInstr *MI,
204                                SmallVectorImpl<wasm::ValType> &Returns) {
205   const Function &F = MI->getMF()->getFunction();
206   const TargetMachine &TM = MI->getMF()->getTarget();
207   Type *RetTy = F.getReturnType();
208   SmallVector<MVT, 4> CallerRetTys;
209   computeLegalValueVTs(F, TM, RetTy, CallerRetTys);
210   valTypesFromMVTs(CallerRetTys, Returns);
211 }
212 
213 void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
214                                    MCInst &OutMI) const {
215   OutMI.setOpcode(MI->getOpcode());
216 
217   const MCInstrDesc &Desc = MI->getDesc();
218   unsigned NumVariadicDefs = MI->getNumExplicitDefs() - Desc.getNumDefs();
219   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
220     const MachineOperand &MO = MI->getOperand(I);
221 
222     MCOperand MCOp;
223     switch (MO.getType()) {
224     default:
225       MI->print(errs());
226       llvm_unreachable("unknown operand type");
227     case MachineOperand::MO_MachineBasicBlock:
228       MI->print(errs());
229       llvm_unreachable("MachineBasicBlock operand should have been rewritten");
230     case MachineOperand::MO_Register: {
231       // Ignore all implicit register operands.
232       if (MO.isImplicit())
233         continue;
234       const WebAssemblyFunctionInfo &MFI =
235           *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
236       unsigned WAReg = MFI.getWAReg(MO.getReg());
237       MCOp = MCOperand::createReg(WAReg);
238       break;
239     }
240     case MachineOperand::MO_Immediate: {
241       unsigned DescIndex = I - NumVariadicDefs;
242       if (DescIndex < Desc.NumOperands) {
243         const MCOperandInfo &Info = Desc.OpInfo[DescIndex];
244         if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
245           SmallVector<wasm::ValType, 4> Returns;
246           SmallVector<wasm::ValType, 4> Params;
247 
248           const MachineRegisterInfo &MRI =
249               MI->getParent()->getParent()->getRegInfo();
250           for (const MachineOperand &MO : MI->defs())
251             Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
252           for (const MachineOperand &MO : MI->explicit_uses())
253             if (MO.isReg())
254               Params.push_back(getType(MRI.getRegClass(MO.getReg())));
255 
256           // call_indirect instructions have a callee operand at the end which
257           // doesn't count as a param.
258           if (WebAssembly::isCallIndirect(MI->getOpcode()))
259             Params.pop_back();
260 
261           // return_call_indirect instructions have the return type of the
262           // caller
263           if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT)
264             getFunctionReturns(MI, Returns);
265 
266           MCOp = lowerTypeIndexOperand(std::move(Returns), std::move(Params));
267           break;
268         } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
269           auto BT = static_cast<WebAssembly::BlockType>(MO.getImm());
270           assert(BT != WebAssembly::BlockType::Invalid);
271           if (BT == WebAssembly::BlockType::Multivalue) {
272             SmallVector<wasm::ValType, 1> Returns;
273             getFunctionReturns(MI, Returns);
274             MCOp = lowerTypeIndexOperand(std::move(Returns),
275                                          SmallVector<wasm::ValType, 4>());
276             break;
277           }
278         } else if (Info.OperandType == WebAssembly::OPERAND_HEAPTYPE) {
279           assert(static_cast<WebAssembly::HeapType>(MO.getImm()) !=
280                  WebAssembly::HeapType::Invalid);
281           // With typed function references, this will need a case for type
282           // index operands.  Otherwise, fall through.
283         }
284       }
285       MCOp = MCOperand::createImm(MO.getImm());
286       break;
287     }
288     case MachineOperand::MO_FPImmediate: {
289       const ConstantFP *Imm = MO.getFPImm();
290       const uint64_t BitPattern =
291           Imm->getValueAPF().bitcastToAPInt().getZExtValue();
292       if (Imm->getType()->isFloatTy())
293         MCOp = MCOperand::createSFPImm(static_cast<uint32_t>(BitPattern));
294       else if (Imm->getType()->isDoubleTy())
295         MCOp = MCOperand::createDFPImm(BitPattern);
296       else
297         llvm_unreachable("unknown floating point immediate type");
298       break;
299     }
300     case MachineOperand::MO_GlobalAddress:
301       MCOp = lowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
302       break;
303     case MachineOperand::MO_ExternalSymbol:
304       MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
305       break;
306     case MachineOperand::MO_MCSymbol:
307       assert(MO.getTargetFlags() == 0 &&
308              "WebAssembly does not use target flags on MCSymbol");
309       MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());
310       break;
311     }
312 
313     OutMI.addOperand(MCOp);
314   }
315 
316   if (!WasmKeepRegisters)
317     removeRegisterOperands(MI, OutMI);
318   else if (Desc.variadicOpsAreDefs())
319     OutMI.insert(OutMI.begin(), MCOperand::createImm(MI->getNumExplicitDefs()));
320 }
321 
322 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
323   // Remove all uses of stackified registers to bring the instruction format
324   // into its final stack form used thruout MC, and transition opcodes to
325   // their _S variant.
326   // We do this separate from the above code that still may need these
327   // registers for e.g. call_indirect signatures.
328   // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
329   // details.
330   // TODO: the code above creates new registers which are then removed here.
331   // That code could be slightly simplified by not doing that, though maybe
332   // it is simpler conceptually to keep the code above in "register mode"
333   // until this transition point.
334   // FIXME: we are not processing inline assembly, which contains register
335   // operands, because it is used by later target generic code.
336   if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
337     return;
338 
339   // Transform to _S instruction.
340   auto RegOpcode = OutMI.getOpcode();
341   auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
342   assert(StackOpcode != -1 && "Failed to stackify instruction");
343   OutMI.setOpcode(StackOpcode);
344 
345   // Remove register operands.
346   for (auto I = OutMI.getNumOperands(); I; --I) {
347     auto &MO = OutMI.getOperand(I - 1);
348     if (MO.isReg()) {
349       OutMI.erase(&MO);
350     }
351   }
352 }
353