1 //- WebAssemblyISelDAGToDAG.cpp - A dag to dag inst selector for WebAssembly -//
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
7 //===----------------------------------------------------------------------===//
10 /// This file defines an instruction selector for the WebAssembly target.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
15 #include "WebAssembly.h"
16 #include "WebAssemblyTargetMachine.h"
17 #include "llvm/CodeGen/SelectionDAGISel.h"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/Function.h" // To access function attributes.
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/KnownBits.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "wasm-isel"
28 //===--------------------------------------------------------------------===//
29 /// WebAssembly-specific code to select WebAssembly machine instructions for
30 /// SelectionDAG operations.
33 class WebAssemblyDAGToDAGISel final
: public SelectionDAGISel
{
34 /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
35 /// right decision when generating code for different targets.
36 const WebAssemblySubtarget
*Subtarget
;
41 WebAssemblyDAGToDAGISel(WebAssemblyTargetMachine
&TM
,
42 CodeGenOpt::Level OptLevel
)
43 : SelectionDAGISel(TM
, OptLevel
), Subtarget(nullptr), ForCodeSize(false) {
46 StringRef
getPassName() const override
{
47 return "WebAssembly Instruction Selection";
50 bool runOnMachineFunction(MachineFunction
&MF
) override
{
51 LLVM_DEBUG(dbgs() << "********** ISelDAGToDAG **********\n"
52 "********** Function: "
53 << MF
.getName() << '\n');
55 ForCodeSize
= MF
.getFunction().hasOptSize();
56 Subtarget
= &MF
.getSubtarget
<WebAssemblySubtarget
>();
58 // Wasm64 is not fully supported right now (and is not specified)
59 if (Subtarget
->hasAddr64())
61 "64-bit WebAssembly (wasm64) is not currently supported");
63 return SelectionDAGISel::runOnMachineFunction(MF
);
66 void Select(SDNode
*Node
) override
;
68 bool SelectInlineAsmMemoryOperand(const SDValue
&Op
, unsigned ConstraintID
,
69 std::vector
<SDValue
> &OutOps
) override
;
71 // Include the pieces autogenerated from the target description.
72 #include "WebAssemblyGenDAGISel.inc"
75 // add select functions here...
77 } // end anonymous namespace
79 void WebAssemblyDAGToDAGISel::Select(SDNode
*Node
) {
80 // If we have a custom node, we already have selected!
81 if (Node
->isMachineOpcode()) {
82 LLVM_DEBUG(errs() << "== "; Node
->dump(CurDAG
); errs() << "\n");
87 // Few custom selection stuff.
89 MachineFunction
&MF
= CurDAG
->getMachineFunction();
90 switch (Node
->getOpcode()) {
91 case ISD::ATOMIC_FENCE
: {
92 if (!MF
.getSubtarget
<WebAssemblySubtarget
>().hasAtomics())
95 uint64_t SyncScopeID
=
96 cast
<ConstantSDNode
>(Node
->getOperand(2).getNode())->getZExtValue();
97 MachineSDNode
*Fence
= nullptr;
98 switch (SyncScopeID
) {
99 case SyncScope::SingleThread
:
100 // We lower a single-thread fence to a pseudo compiler barrier instruction
101 // preventing instruction reordering. This will not be emitted in final
103 Fence
= CurDAG
->getMachineNode(WebAssembly::COMPILER_FENCE
,
105 MVT::Other
, // outchain type
106 Node
->getOperand(0) // inchain
109 case SyncScope::System
:
110 // Currently wasm only supports sequentially consistent atomics, so we
111 // always set the order to 0 (sequentially consistent).
112 Fence
= CurDAG
->getMachineNode(
113 WebAssembly::ATOMIC_FENCE
,
115 MVT::Other
, // outchain type
116 CurDAG
->getTargetConstant(0, DL
, MVT::i32
), // order
117 Node
->getOperand(0) // inchain
121 llvm_unreachable("Unknown scope!");
124 ReplaceNode(Node
, Fence
);
125 CurDAG
->RemoveDeadNode(Node
);
129 case ISD::GlobalTLSAddress
: {
130 const auto *GA
= cast
<GlobalAddressSDNode
>(Node
);
132 if (!MF
.getSubtarget
<WebAssemblySubtarget
>().hasBulkMemory())
133 report_fatal_error("cannot use thread-local storage without bulk memory",
136 // Currently Emscripten does not support dynamic linking with threads.
137 // Therefore, if we have thread-local storage, only the local-exec model
139 // TODO: remove this and implement proper TLS models once Emscripten
140 // supports dynamic linking with threads.
141 if (GA
->getGlobal()->getThreadLocalMode() !=
142 GlobalValue::LocalExecTLSModel
&&
143 !Subtarget
->getTargetTriple().isOSEmscripten()) {
144 report_fatal_error("only -ftls-model=local-exec is supported for now on "
145 "non-Emscripten OSes: variable " +
146 GA
->getGlobal()->getName(),
150 MVT PtrVT
= TLI
->getPointerTy(CurDAG
->getDataLayout());
151 assert(PtrVT
== MVT::i32
&& "only wasm32 is supported for now");
153 SDValue TLSBaseSym
= CurDAG
->getTargetExternalSymbol("__tls_base", PtrVT
);
154 SDValue TLSOffsetSym
= CurDAG
->getTargetGlobalAddress(
155 GA
->getGlobal(), DL
, PtrVT
, GA
->getOffset(), 0);
157 MachineSDNode
*TLSBase
= CurDAG
->getMachineNode(WebAssembly::GLOBAL_GET_I32
,
158 DL
, MVT::i32
, TLSBaseSym
);
159 MachineSDNode
*TLSOffset
= CurDAG
->getMachineNode(
160 WebAssembly::CONST_I32
, DL
, MVT::i32
, TLSOffsetSym
);
161 MachineSDNode
*TLSAddress
=
162 CurDAG
->getMachineNode(WebAssembly::ADD_I32
, DL
, MVT::i32
,
163 SDValue(TLSBase
, 0), SDValue(TLSOffset
, 0));
164 ReplaceNode(Node
, TLSAddress
);
168 case ISD::INTRINSIC_WO_CHAIN
: {
169 unsigned IntNo
= cast
<ConstantSDNode
>(Node
->getOperand(0))->getZExtValue();
171 case Intrinsic::wasm_tls_size
: {
172 MVT PtrVT
= TLI
->getPointerTy(CurDAG
->getDataLayout());
173 assert(PtrVT
== MVT::i32
&& "only wasm32 is supported for now");
175 MachineSDNode
*TLSSize
= CurDAG
->getMachineNode(
176 WebAssembly::GLOBAL_GET_I32
, DL
, PtrVT
,
177 CurDAG
->getTargetExternalSymbol("__tls_size", MVT::i32
));
178 ReplaceNode(Node
, TLSSize
);
181 case Intrinsic::wasm_tls_align
: {
182 MVT PtrVT
= TLI
->getPointerTy(CurDAG
->getDataLayout());
183 assert(PtrVT
== MVT::i32
&& "only wasm32 is supported for now");
185 MachineSDNode
*TLSAlign
= CurDAG
->getMachineNode(
186 WebAssembly::GLOBAL_GET_I32
, DL
, PtrVT
,
187 CurDAG
->getTargetExternalSymbol("__tls_align", MVT::i32
));
188 ReplaceNode(Node
, TLSAlign
);
194 case ISD::INTRINSIC_W_CHAIN
: {
195 unsigned IntNo
= cast
<ConstantSDNode
>(Node
->getOperand(1))->getZExtValue();
197 case Intrinsic::wasm_tls_base
: {
198 MVT PtrVT
= TLI
->getPointerTy(CurDAG
->getDataLayout());
199 assert(PtrVT
== MVT::i32
&& "only wasm32 is supported for now");
201 MachineSDNode
*TLSBase
= CurDAG
->getMachineNode(
202 WebAssembly::GLOBAL_GET_I32
, DL
, MVT::i32
, MVT::Other
,
203 CurDAG
->getTargetExternalSymbol("__tls_base", PtrVT
),
204 Node
->getOperand(0));
205 ReplaceNode(Node
, TLSBase
);
216 // Select the default instruction.
220 bool WebAssemblyDAGToDAGISel::SelectInlineAsmMemoryOperand(
221 const SDValue
&Op
, unsigned ConstraintID
, std::vector
<SDValue
> &OutOps
) {
222 switch (ConstraintID
) {
223 case InlineAsm::Constraint_i
:
224 case InlineAsm::Constraint_m
:
225 // We just support simple memory operands that just have a single address
226 // operand and need no special handling.
227 OutOps
.push_back(Op
);
236 /// This pass converts a legalized DAG into a WebAssembly-specific DAG, ready
237 /// for instruction scheduling.
238 FunctionPass
*llvm::createWebAssemblyISelDag(WebAssemblyTargetMachine
&TM
,
239 CodeGenOpt::Level OptLevel
) {
240 return new WebAssemblyDAGToDAGISel(TM
, OptLevel
);