Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //- WebAssemblyISelDAGToDAG.cpp - A dag to dag inst selector for WebAssembly -// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file defines an instruction selector for the WebAssembly target. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 14 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "WebAssembly.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 16 | #include "WebAssemblyTargetMachine.h" |
| 17 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DiagnosticInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Function.h" // To access function attributes. |
| 20 | #include "llvm/Support/Debug.h" |
Craig Topper | 053cf4d | 2017-04-28 08:15:33 +0000 | [diff] [blame] | 21 | #include "llvm/Support/KnownBits.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | #define DEBUG_TYPE "wasm-isel" |
| 27 | |
| 28 | //===--------------------------------------------------------------------===// |
| 29 | /// WebAssembly-specific code to select WebAssembly machine instructions for |
| 30 | /// SelectionDAG operations. |
| 31 | /// |
| 32 | namespace { |
| 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; |
| 37 | |
| 38 | bool ForCodeSize; |
| 39 | |
| 40 | public: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 41 | WebAssemblyDAGToDAGISel(WebAssemblyTargetMachine &TM, |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 42 | CodeGenOpt::Level OptLevel) |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 43 | : SelectionDAGISel(TM, OptLevel), Subtarget(nullptr), ForCodeSize(false) { |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 46 | StringRef getPassName() const override { |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 47 | return "WebAssembly Instruction Selection"; |
| 48 | } |
| 49 | |
| 50 | bool runOnMachineFunction(MachineFunction &MF) override { |
Heejin Ahn | 569f090 | 2019-01-09 23:05:21 +0000 | [diff] [blame] | 51 | LLVM_DEBUG(dbgs() << "********** ISelDAGToDAG **********\n" |
| 52 | "********** Function: " |
| 53 | << MF.getName() << '\n'); |
| 54 | |
Heejin Ahn | 5f3a045 | 2019-04-13 16:54:39 +0000 | [diff] [blame] | 55 | ForCodeSize = MF.getFunction().hasOptSize(); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 56 | Subtarget = &MF.getSubtarget<WebAssemblySubtarget>(); |
Thomas Lively | 5b74c39 | 2019-10-02 17:34:44 +0000 | [diff] [blame] | 57 | |
| 58 | // Wasm64 is not fully supported right now (and is not specified) |
| 59 | if (Subtarget->hasAddr64()) |
| 60 | report_fatal_error( |
| 61 | "64-bit WebAssembly (wasm64) is not currently supported"); |
| 62 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 63 | return SelectionDAGISel::runOnMachineFunction(MF); |
| 64 | } |
| 65 | |
Justin Bogner | c6afd4b | 2016-05-13 22:44:57 +0000 | [diff] [blame] | 66 | void Select(SDNode *Node) override; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 67 | |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 68 | bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, |
| 69 | std::vector<SDValue> &OutOps) override; |
| 70 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 71 | // Include the pieces autogenerated from the target description. |
| 72 | #include "WebAssemblyGenDAGISel.inc" |
| 73 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 74 | private: |
| 75 | // add select functions here... |
| 76 | }; |
| 77 | } // end anonymous namespace |
| 78 | |
Justin Bogner | c6afd4b | 2016-05-13 22:44:57 +0000 | [diff] [blame] | 79 | void WebAssemblyDAGToDAGISel::Select(SDNode *Node) { |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 80 | // If we have a custom node, we already have selected! |
| 81 | if (Node->isMachineOpcode()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 82 | LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 83 | Node->setNodeId(-1); |
Justin Bogner | c6afd4b | 2016-05-13 22:44:57 +0000 | [diff] [blame] | 84 | return; |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Heejin Ahn | 5514658 | 2019-05-28 22:09:12 +0000 | [diff] [blame] | 87 | // Few custom selection stuff. |
| 88 | SDLoc DL(Node); |
| 89 | MachineFunction &MF = CurDAG->getMachineFunction(); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 90 | switch (Node->getOpcode()) { |
Heejin Ahn | 5514658 | 2019-05-28 22:09:12 +0000 | [diff] [blame] | 91 | case ISD::ATOMIC_FENCE: { |
| 92 | if (!MF.getSubtarget<WebAssemblySubtarget>().hasAtomics()) |
| 93 | break; |
| 94 | |
| 95 | uint64_t SyncScopeID = |
| 96 | cast<ConstantSDNode>(Node->getOperand(2).getNode())->getZExtValue(); |
Heejin Ahn | d85fd5a | 2019-08-28 23:13:43 +0000 | [diff] [blame] | 97 | MachineSDNode *Fence = nullptr; |
Heejin Ahn | 5514658 | 2019-05-28 22:09:12 +0000 | [diff] [blame] | 98 | switch (SyncScopeID) { |
Heejin Ahn | d85fd5a | 2019-08-28 23:13:43 +0000 | [diff] [blame] | 99 | case SyncScope::SingleThread: |
Heejin Ahn | 5514658 | 2019-05-28 22:09:12 +0000 | [diff] [blame] | 100 | // We lower a single-thread fence to a pseudo compiler barrier instruction |
| 101 | // preventing instruction reordering. This will not be emitted in final |
| 102 | // binary. |
Heejin Ahn | d85fd5a | 2019-08-28 23:13:43 +0000 | [diff] [blame] | 103 | Fence = CurDAG->getMachineNode(WebAssembly::COMPILER_FENCE, |
| 104 | DL, // debug loc |
| 105 | MVT::Other, // outchain type |
| 106 | Node->getOperand(0) // inchain |
| 107 | ); |
| 108 | break; |
| 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, |
| 114 | DL, // debug loc |
| 115 | MVT::Other, // outchain type |
| 116 | CurDAG->getTargetConstant(0, DL, MVT::i32), // order |
| 117 | Node->getOperand(0) // inchain |
| 118 | ); |
| 119 | break; |
Heejin Ahn | 5514658 | 2019-05-28 22:09:12 +0000 | [diff] [blame] | 120 | default: |
| 121 | llvm_unreachable("Unknown scope!"); |
| 122 | } |
Heejin Ahn | d85fd5a | 2019-08-28 23:13:43 +0000 | [diff] [blame] | 123 | |
| 124 | ReplaceNode(Node, Fence); |
| 125 | CurDAG->RemoveDeadNode(Node); |
| 126 | return; |
Heejin Ahn | 5514658 | 2019-05-28 22:09:12 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 129 | case ISD::GlobalTLSAddress: { |
| 130 | const auto *GA = cast<GlobalAddressSDNode>(Node); |
| 131 | |
| 132 | if (!MF.getSubtarget<WebAssemblySubtarget>().hasBulkMemory()) |
| 133 | report_fatal_error("cannot use thread-local storage without bulk memory", |
| 134 | false); |
| 135 | |
Guanzhong Chen | 0a8d4df | 2019-07-16 22:22:08 +0000 | [diff] [blame] | 136 | // Currently Emscripten does not support dynamic linking with threads. |
| 137 | // Therefore, if we have thread-local storage, only the local-exec model |
| 138 | // is possible. |
| 139 | // TODO: remove this and implement proper TLS models once Emscripten |
| 140 | // supports dynamic linking with threads. |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 141 | if (GA->getGlobal()->getThreadLocalMode() != |
Guanzhong Chen | 0a8d4df | 2019-07-16 22:22:08 +0000 | [diff] [blame] | 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(), |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 147 | false); |
| 148 | } |
| 149 | |
| 150 | MVT PtrVT = TLI->getPointerTy(CurDAG->getDataLayout()); |
| 151 | assert(PtrVT == MVT::i32 && "only wasm32 is supported for now"); |
| 152 | |
| 153 | SDValue TLSBaseSym = CurDAG->getTargetExternalSymbol("__tls_base", PtrVT); |
| 154 | SDValue TLSOffsetSym = CurDAG->getTargetGlobalAddress( |
| 155 | GA->getGlobal(), DL, PtrVT, GA->getOffset(), 0); |
| 156 | |
| 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); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | case ISD::INTRINSIC_WO_CHAIN: { |
| 169 | unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue(); |
| 170 | switch (IntNo) { |
| 171 | case Intrinsic::wasm_tls_size: { |
| 172 | MVT PtrVT = TLI->getPointerTy(CurDAG->getDataLayout()); |
| 173 | assert(PtrVT == MVT::i32 && "only wasm32 is supported for now"); |
| 174 | |
| 175 | MachineSDNode *TLSSize = CurDAG->getMachineNode( |
| 176 | WebAssembly::GLOBAL_GET_I32, DL, PtrVT, |
| 177 | CurDAG->getTargetExternalSymbol("__tls_size", MVT::i32)); |
| 178 | ReplaceNode(Node, TLSSize); |
| 179 | return; |
| 180 | } |
Guanzhong Chen | 5204f76 | 2019-07-19 23:34:16 +0000 | [diff] [blame] | 181 | case Intrinsic::wasm_tls_align: { |
| 182 | MVT PtrVT = TLI->getPointerTy(CurDAG->getDataLayout()); |
| 183 | assert(PtrVT == MVT::i32 && "only wasm32 is supported for now"); |
| 184 | |
| 185 | MachineSDNode *TLSAlign = CurDAG->getMachineNode( |
| 186 | WebAssembly::GLOBAL_GET_I32, DL, PtrVT, |
| 187 | CurDAG->getTargetExternalSymbol("__tls_align", MVT::i32)); |
| 188 | ReplaceNode(Node, TLSAlign); |
| 189 | return; |
| 190 | } |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 191 | } |
| 192 | break; |
| 193 | } |
Guanzhong Chen | 801fa8e | 2019-07-18 17:53:22 +0000 | [diff] [blame] | 194 | case ISD::INTRINSIC_W_CHAIN: { |
| 195 | unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); |
| 196 | switch (IntNo) { |
| 197 | case Intrinsic::wasm_tls_base: { |
| 198 | MVT PtrVT = TLI->getPointerTy(CurDAG->getDataLayout()); |
| 199 | assert(PtrVT == MVT::i32 && "only wasm32 is supported for now"); |
| 200 | |
| 201 | MachineSDNode *TLSBase = CurDAG->getMachineNode( |
Guanzhong Chen | df44792 | 2019-07-18 21:17:52 +0000 | [diff] [blame] | 202 | WebAssembly::GLOBAL_GET_I32, DL, MVT::i32, MVT::Other, |
Guanzhong Chen | 801fa8e | 2019-07-18 17:53:22 +0000 | [diff] [blame] | 203 | CurDAG->getTargetExternalSymbol("__tls_base", PtrVT), |
| 204 | Node->getOperand(0)); |
| 205 | ReplaceNode(Node, TLSBase); |
| 206 | return; |
| 207 | } |
| 208 | } |
| 209 | break; |
| 210 | } |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame] | 211 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 212 | default: |
| 213 | break; |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Select the default instruction. |
Justin Bogner | c6afd4b | 2016-05-13 22:44:57 +0000 | [diff] [blame] | 217 | SelectCode(Node); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 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); |
| 228 | return false; |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | return true; |
| 234 | } |
| 235 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 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); |
| 241 | } |