Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //=- WebAssemblyISelLowering.cpp - WebAssembly DAG Lowering Implementation -==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file implements the WebAssemblyTargetLowering class. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "WebAssemblyISelLowering.h" |
| 16 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 17 | #include "WebAssemblyMachineFunctionInfo.h" |
| 18 | #include "WebAssemblySubtarget.h" |
| 19 | #include "WebAssemblyTargetMachine.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/Analysis.h" |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/CallingConvLower.h" |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 26 | #include "llvm/CodeGen/SelectionDAG.h" |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/WasmEHFuncInfo.h" |
Oliver Stannard | 02fa1c8 | 2016-01-28 13:19:47 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DiagnosticInfo.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DiagnosticPrinter.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Function.h" |
| 31 | #include "llvm/IR/Intrinsics.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
| 35 | #include "llvm/Target/TargetOptions.h" |
| 36 | using namespace llvm; |
| 37 | |
| 38 | #define DEBUG_TYPE "wasm-lower" |
| 39 | |
Heejin Ahn | 5831e9c | 2018-08-09 23:58:51 +0000 | [diff] [blame] | 40 | // Emit proposed instructions that may not have been implemented in engines |
| 41 | cl::opt<bool> EnableUnimplementedWasmSIMDInstrs( |
| 42 | "wasm-enable-unimplemented-simd", |
| 43 | cl::desc("Emit potentially-unimplemented WebAssembly SIMD instructions"), |
| 44 | cl::init(false)); |
| 45 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 46 | WebAssemblyTargetLowering::WebAssemblyTargetLowering( |
| 47 | const TargetMachine &TM, const WebAssemblySubtarget &STI) |
Dan Gohman | bfaf7e1 | 2015-07-02 21:36:25 +0000 | [diff] [blame] | 48 | : TargetLowering(TM), Subtarget(&STI) { |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 49 | auto MVTPtr = Subtarget->hasAddr64() ? MVT::i64 : MVT::i32; |
| 50 | |
JF Bastien | 71d29ac | 2015-08-12 17:53:29 +0000 | [diff] [blame] | 51 | // Booleans always contain 0 or 1. |
| 52 | setBooleanContents(ZeroOrOneBooleanContent); |
Thomas Lively | 5ea17d4 | 2018-10-20 01:35:23 +0000 | [diff] [blame] | 53 | // Except in SIMD vectors |
| 54 | setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); |
Dan Gohman | bfaf7e1 | 2015-07-02 21:36:25 +0000 | [diff] [blame] | 55 | // WebAssembly does not produce floating-point exceptions on normal floating |
| 56 | // point operations. |
| 57 | setHasFloatingPointExceptions(false); |
Dan Gohman | 489abd7 | 2015-07-07 22:38:06 +0000 | [diff] [blame] | 58 | // We don't know the microarchitecture here, so just reduce register pressure. |
| 59 | setSchedulingPreference(Sched::RegPressure); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 60 | // Tell ISel that we have a stack pointer. |
| 61 | setStackPointerRegisterToSaveRestore( |
| 62 | Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32); |
| 63 | // Set up the register classes. |
Dan Gohman | d0bf981 | 2015-09-26 01:09:44 +0000 | [diff] [blame] | 64 | addRegisterClass(MVT::i32, &WebAssembly::I32RegClass); |
| 65 | addRegisterClass(MVT::i64, &WebAssembly::I64RegClass); |
| 66 | addRegisterClass(MVT::f32, &WebAssembly::F32RegClass); |
| 67 | addRegisterClass(MVT::f64, &WebAssembly::F64RegClass); |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 68 | if (Subtarget->hasSIMD128()) { |
| 69 | addRegisterClass(MVT::v16i8, &WebAssembly::V128RegClass); |
| 70 | addRegisterClass(MVT::v8i16, &WebAssembly::V128RegClass); |
| 71 | addRegisterClass(MVT::v4i32, &WebAssembly::V128RegClass); |
| 72 | addRegisterClass(MVT::v4f32, &WebAssembly::V128RegClass); |
Heejin Ahn | 5831e9c | 2018-08-09 23:58:51 +0000 | [diff] [blame] | 73 | if (EnableUnimplementedWasmSIMDInstrs) { |
| 74 | addRegisterClass(MVT::v2i64, &WebAssembly::V128RegClass); |
| 75 | addRegisterClass(MVT::v2f64, &WebAssembly::V128RegClass); |
| 76 | } |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 77 | } |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 78 | // Compute derived properties from the register classes. |
| 79 | computeRegisterProperties(Subtarget->getRegisterInfo()); |
| 80 | |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 81 | setOperationAction(ISD::GlobalAddress, MVTPtr, Custom); |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 82 | setOperationAction(ISD::ExternalSymbol, MVTPtr, Custom); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 83 | setOperationAction(ISD::JumpTable, MVTPtr, Custom); |
Derek Schuff | 51699a8 | 2016-02-12 22:56:03 +0000 | [diff] [blame] | 84 | setOperationAction(ISD::BlockAddress, MVTPtr, Custom); |
| 85 | setOperationAction(ISD::BRIND, MVT::Other, Custom); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 86 | |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 87 | // Take the default expansion for va_arg, va_copy, and va_end. There is no |
| 88 | // default action for va_start, so we do that custom. |
| 89 | setOperationAction(ISD::VASTART, MVT::Other, Custom); |
| 90 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
| 91 | setOperationAction(ISD::VACOPY, MVT::Other, Expand); |
| 92 | setOperationAction(ISD::VAEND, MVT::Other, Expand); |
| 93 | |
Thomas Lively | ebd4c90 | 2018-09-12 17:56:00 +0000 | [diff] [blame] | 94 | for (auto T : {MVT::f32, MVT::f64, MVT::v4f32, MVT::v2f64}) { |
JF Bastien | da06bce | 2015-08-11 21:02:46 +0000 | [diff] [blame] | 95 | // Don't expand the floating-point types to constant pools. |
| 96 | setOperationAction(ISD::ConstantFP, T, Legal); |
| 97 | // Expand floating-point comparisons. |
| 98 | for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE, |
| 99 | ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE}) |
| 100 | setCondCodeAction(CC, T, Expand); |
Dan Gohman | 32907a6 | 2015-08-20 22:57:13 +0000 | [diff] [blame] | 101 | // Expand floating-point library function operators. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 102 | for (auto Op : |
| 103 | {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM, ISD::FMA}) |
Dan Gohman | 32907a6 | 2015-08-20 22:57:13 +0000 | [diff] [blame] | 104 | setOperationAction(Op, T, Expand); |
Dan Gohman | 896e53f | 2015-08-24 18:23:13 +0000 | [diff] [blame] | 105 | // Note supported floating-point library function operators that otherwise |
| 106 | // default to expand. |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 107 | for (auto Op : |
| 108 | {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT, ISD::FRINT}) |
Dan Gohman | 896e53f | 2015-08-24 18:23:13 +0000 | [diff] [blame] | 109 | setOperationAction(Op, T, Legal); |
Thomas Lively | 30f1d69 | 2018-10-24 22:49:55 +0000 | [diff] [blame] | 110 | // Support minimum and maximum, which otherwise default to expand. |
| 111 | setOperationAction(ISD::FMINIMUM, T, Legal); |
| 112 | setOperationAction(ISD::FMAXIMUM, T, Legal); |
Dan Gohman | a63e8eb | 2017-02-22 16:28:00 +0000 | [diff] [blame] | 113 | // WebAssembly currently has no builtin f16 support. |
| 114 | setOperationAction(ISD::FP16_TO_FP, T, Expand); |
| 115 | setOperationAction(ISD::FP_TO_FP16, T, Expand); |
| 116 | setLoadExtAction(ISD::EXTLOAD, T, MVT::f16, Expand); |
| 117 | setTruncStoreAction(T, MVT::f16, Expand); |
JF Bastien | da06bce | 2015-08-11 21:02:46 +0000 | [diff] [blame] | 118 | } |
Dan Gohman | 32907a6 | 2015-08-20 22:57:13 +0000 | [diff] [blame] | 119 | |
Thomas Lively | 0aad98f | 2018-10-25 19:06:13 +0000 | [diff] [blame] | 120 | // Support saturating add for i8x16 and i16x8 |
| 121 | if (Subtarget->hasSIMD128()) |
| 122 | for (auto T : {MVT::v16i8, MVT::v8i16}) |
| 123 | for (auto Op : {ISD::SADDSAT, ISD::UADDSAT}) |
| 124 | setOperationAction(Op, T, Legal); |
| 125 | |
Thomas Lively | 66ea30c | 2018-11-29 22:01:01 +0000 | [diff] [blame] | 126 | // Expand unavailable integer operations. |
| 127 | for (auto Op : |
| 128 | {ISD::BSWAP, ISD::SMUL_LOHI, ISD::UMUL_LOHI, ISD::MULHS, ISD::MULHU, |
| 129 | ISD::SDIVREM, ISD::UDIVREM, ISD::SHL_PARTS, ISD::SRA_PARTS, |
| 130 | ISD::SRL_PARTS, ISD::ADDC, ISD::ADDE, ISD::SUBC, ISD::SUBE}) { |
| 131 | for (auto T : {MVT::i32, MVT::i64}) { |
Dan Gohman | 32907a6 | 2015-08-20 22:57:13 +0000 | [diff] [blame] | 132 | setOperationAction(Op, T, Expand); |
| 133 | } |
Thomas Lively | 66ea30c | 2018-11-29 22:01:01 +0000 | [diff] [blame] | 134 | if (Subtarget->hasSIMD128()) { |
| 135 | for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32}) { |
| 136 | setOperationAction(Op, T, Expand); |
| 137 | } |
| 138 | if (EnableUnimplementedWasmSIMDInstrs) { |
| 139 | setOperationAction(Op, MVT::v2i64, Expand); |
| 140 | } |
| 141 | } |
Dan Gohman | 32907a6 | 2015-08-20 22:57:13 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Thomas Lively | 2ee686d | 2018-08-22 23:06:27 +0000 | [diff] [blame] | 144 | // There is no i64x2.mul instruction |
| 145 | setOperationAction(ISD::MUL, MVT::v2i64, Expand); |
| 146 | |
Thomas Lively | a0d2581 | 2018-09-07 21:54:46 +0000 | [diff] [blame] | 147 | // We have custom shuffle lowering to expose the shuffle mask |
| 148 | if (Subtarget->hasSIMD128()) { |
| 149 | for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) { |
| 150 | setOperationAction(ISD::VECTOR_SHUFFLE, T, Custom); |
| 151 | } |
| 152 | if (EnableUnimplementedWasmSIMDInstrs) { |
| 153 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); |
| 154 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); |
| 155 | } |
| 156 | } |
| 157 | |
Thomas Lively | b2382c8 | 2018-11-02 00:39:57 +0000 | [diff] [blame] | 158 | // Custom lowering since wasm shifts must have a scalar shift amount |
| 159 | if (Subtarget->hasSIMD128()) { |
| 160 | for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32}) |
| 161 | for (auto Op : {ISD::SHL, ISD::SRA, ISD::SRL}) |
| 162 | setOperationAction(Op, T, Custom); |
| 163 | if (EnableUnimplementedWasmSIMDInstrs) |
| 164 | for (auto Op : {ISD::SHL, ISD::SRA, ISD::SRL}) |
| 165 | setOperationAction(Op, MVT::v2i64, Custom); |
| 166 | } |
Thomas Lively | 55735d5 | 2018-10-20 01:31:18 +0000 | [diff] [blame] | 167 | |
Thomas Lively | 38c902b | 2018-11-09 01:38:44 +0000 | [diff] [blame] | 168 | // There are no select instructions for vectors |
| 169 | if (Subtarget->hasSIMD128()) |
| 170 | for (auto Op : {ISD::VSELECT, ISD::SELECT_CC, ISD::SELECT}) { |
| 171 | for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) |
| 172 | setOperationAction(Op, T, Expand); |
| 173 | if (EnableUnimplementedWasmSIMDInstrs) |
| 174 | for (auto T : {MVT::v2i64, MVT::v2f64}) |
| 175 | setOperationAction(Op, T, Expand); |
| 176 | } |
Thomas Lively | d4891a1 | 2018-11-01 00:01:02 +0000 | [diff] [blame] | 177 | |
Dan Gohman | 32907a6 | 2015-08-20 22:57:13 +0000 | [diff] [blame] | 178 | // As a special case, these operators use the type to mean the type to |
| 179 | // sign-extend from. |
Derek Schuff | a519fe5 | 2017-09-13 00:29:06 +0000 | [diff] [blame] | 180 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
Dan Gohman | 5d2b935 | 2018-01-19 17:16:24 +0000 | [diff] [blame] | 181 | if (!Subtarget->hasSignExt()) { |
Derek Schuff | a519fe5 | 2017-09-13 00:29:06 +0000 | [diff] [blame] | 182 | for (auto T : {MVT::i8, MVT::i16, MVT::i32}) |
| 183 | setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand); |
| 184 | } |
Thomas Lively | 5ea17d4 | 2018-10-20 01:35:23 +0000 | [diff] [blame] | 185 | for (auto T : MVT::integer_vector_valuetypes()) |
| 186 | setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand); |
Dan Gohman | 32907a6 | 2015-08-20 22:57:13 +0000 | [diff] [blame] | 187 | |
| 188 | // Dynamic stack allocation: use the default expansion. |
| 189 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 190 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
Dan Gohman | 2683a55 | 2015-08-24 22:31:52 +0000 | [diff] [blame] | 191 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVTPtr, Expand); |
JF Bastien | 73ff6af | 2015-08-31 22:24:11 +0000 | [diff] [blame] | 192 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 193 | setOperationAction(ISD::FrameIndex, MVT::i32, Custom); |
Derek Schuff | aadc89c | 2016-02-16 18:18:36 +0000 | [diff] [blame] | 194 | setOperationAction(ISD::CopyToReg, MVT::Other, Custom); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 195 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 196 | // Expand these forms; we pattern-match the forms that we can handle in isel. |
| 197 | for (auto T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64}) |
| 198 | for (auto Op : {ISD::BR_CC, ISD::SELECT_CC}) |
| 199 | setOperationAction(Op, T, Expand); |
| 200 | |
| 201 | // We have custom switch handling. |
| 202 | setOperationAction(ISD::BR_JT, MVT::Other, Custom); |
| 203 | |
JF Bastien | 73ff6af | 2015-08-31 22:24:11 +0000 | [diff] [blame] | 204 | // WebAssembly doesn't have: |
| 205 | // - Floating-point extending loads. |
| 206 | // - Floating-point truncating stores. |
| 207 | // - i1 extending loads. |
Thomas Lively | 325c9c5 | 2018-10-25 01:46:07 +0000 | [diff] [blame] | 208 | // - extending/truncating SIMD loads/stores |
Dan Gohman | 60bddf1 | 2015-12-10 02:07:53 +0000 | [diff] [blame] | 209 | setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); |
JF Bastien | 73ff6af | 2015-08-31 22:24:11 +0000 | [diff] [blame] | 210 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 211 | for (auto T : MVT::integer_valuetypes()) |
| 212 | for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD}) |
| 213 | setLoadExtAction(Ext, T, MVT::i1, Promote); |
Thomas Lively | 325c9c5 | 2018-10-25 01:46:07 +0000 | [diff] [blame] | 214 | if (Subtarget->hasSIMD128()) { |
| 215 | for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, MVT::v4f32, |
| 216 | MVT::v2f64}) { |
| 217 | for (auto MemT : MVT::vector_valuetypes()) { |
| 218 | if (MVT(T) != MemT) { |
| 219 | setTruncStoreAction(T, MemT, Expand); |
| 220 | for (auto Ext : {ISD::EXTLOAD, ISD::ZEXTLOAD, ISD::SEXTLOAD}) |
| 221 | setLoadExtAction(Ext, T, MemT, Expand); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 226 | |
Thomas Lively | 8dbf29af | 2018-12-20 02:10:22 +0000 | [diff] [blame] | 227 | // Expand additional SIMD ops that V8 hasn't implemented yet |
| 228 | if (Subtarget->hasSIMD128() && !EnableUnimplementedWasmSIMDInstrs) { |
| 229 | setOperationAction(ISD::FSQRT, MVT::v4f32, Expand); |
| 230 | setOperationAction(ISD::FDIV, MVT::v4f32, Expand); |
| 231 | } |
| 232 | |
Thomas Lively | fb84fd7 | 2018-11-02 00:06:56 +0000 | [diff] [blame] | 233 | // Custom lower lane accesses to expand out variable indices |
| 234 | if (Subtarget->hasSIMD128()) { |
| 235 | for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32}) { |
| 236 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, T, Custom); |
| 237 | setOperationAction(ISD::INSERT_VECTOR_ELT, T, Custom); |
| 238 | } |
| 239 | if (EnableUnimplementedWasmSIMDInstrs) { |
| 240 | for (auto T : {MVT::v2i64, MVT::v2f64}) { |
| 241 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, T, Custom); |
| 242 | setOperationAction(ISD::INSERT_VECTOR_ELT, T, Custom); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 247 | // Trap lowers to wasm unreachable |
| 248 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
Derek Schuff | 18ba192 | 2017-08-30 18:07:45 +0000 | [diff] [blame] | 249 | |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 250 | // Exception handling intrinsics |
| 251 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 252 | setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 253 | |
Derek Schuff | 18ba192 | 2017-08-30 18:07:45 +0000 | [diff] [blame] | 254 | setMaxAtomicSizeInBitsSupported(64); |
Dan Gohman | bfaf7e1 | 2015-07-02 21:36:25 +0000 | [diff] [blame] | 255 | } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 256 | |
Heejin Ahn | e8653bb | 2018-08-07 00:22:22 +0000 | [diff] [blame] | 257 | TargetLowering::AtomicExpansionKind |
| 258 | WebAssemblyTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { |
| 259 | // We have wasm instructions for these |
| 260 | switch (AI->getOperation()) { |
| 261 | case AtomicRMWInst::Add: |
| 262 | case AtomicRMWInst::Sub: |
| 263 | case AtomicRMWInst::And: |
| 264 | case AtomicRMWInst::Or: |
| 265 | case AtomicRMWInst::Xor: |
| 266 | case AtomicRMWInst::Xchg: |
| 267 | return AtomicExpansionKind::None; |
| 268 | default: |
| 269 | break; |
| 270 | } |
| 271 | return AtomicExpansionKind::CmpXChg; |
| 272 | } |
| 273 | |
Dan Gohman | 7b63484 | 2015-08-24 18:44:37 +0000 | [diff] [blame] | 274 | FastISel *WebAssemblyTargetLowering::createFastISel( |
| 275 | FunctionLoweringInfo &FuncInfo, const TargetLibraryInfo *LibInfo) const { |
| 276 | return WebAssembly::createFastISel(FuncInfo, LibInfo); |
| 277 | } |
| 278 | |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 279 | bool WebAssemblyTargetLowering::isOffsetFoldingLegal( |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 280 | const GlobalAddressSDNode * /*GA*/) const { |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 281 | // All offsets can be folded. |
| 282 | return true; |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 285 | MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout & /*DL*/, |
JF Bastien | fda5337 | 2015-08-03 00:00:11 +0000 | [diff] [blame] | 286 | EVT VT) const { |
Dan Gohman | a848375 | 2015-12-10 00:26:26 +0000 | [diff] [blame] | 287 | unsigned BitWidth = NextPowerOf2(VT.getSizeInBits() - 1); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 288 | if (BitWidth > 1 && BitWidth < 8) |
| 289 | BitWidth = 8; |
Dan Gohman | 4172953 | 2015-12-16 23:25:51 +0000 | [diff] [blame] | 290 | |
| 291 | if (BitWidth > 64) { |
Dan Gohman | a01e8bd | 2016-05-14 02:15:47 +0000 | [diff] [blame] | 292 | // The shift will be lowered to a libcall, and compiler-rt libcalls expect |
| 293 | // the count to be an i32. |
| 294 | BitWidth = 32; |
Dan Gohman | 4172953 | 2015-12-16 23:25:51 +0000 | [diff] [blame] | 295 | assert(BitWidth >= Log2_32_Ceil(VT.getSizeInBits()) && |
Dan Gohman | a01e8bd | 2016-05-14 02:15:47 +0000 | [diff] [blame] | 296 | "32-bit shift counts ought to be enough for anyone"); |
Dan Gohman | 4172953 | 2015-12-16 23:25:51 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Dan Gohman | a848375 | 2015-12-10 00:26:26 +0000 | [diff] [blame] | 299 | MVT Result = MVT::getIntegerVT(BitWidth); |
| 300 | assert(Result != MVT::INVALID_SIMPLE_VALUE_TYPE && |
| 301 | "Unable to represent scalar shift amount type"); |
| 302 | return Result; |
JF Bastien | fda5337 | 2015-08-03 00:00:11 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 305 | // Lower an fp-to-int conversion operator from the LLVM opcode, which has an |
| 306 | // undefined result on invalid/overflow, to the WebAssembly opcode, which |
| 307 | // traps on invalid/overflow. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 308 | static MachineBasicBlock *LowerFPToInt(MachineInstr &MI, DebugLoc DL, |
| 309 | MachineBasicBlock *BB, |
| 310 | const TargetInstrInfo &TII, |
| 311 | bool IsUnsigned, bool Int64, |
| 312 | bool Float64, unsigned LoweredOpcode) { |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 313 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
| 314 | |
| 315 | unsigned OutReg = MI.getOperand(0).getReg(); |
| 316 | unsigned InReg = MI.getOperand(1).getReg(); |
| 317 | |
| 318 | unsigned Abs = Float64 ? WebAssembly::ABS_F64 : WebAssembly::ABS_F32; |
| 319 | unsigned FConst = Float64 ? WebAssembly::CONST_F64 : WebAssembly::CONST_F32; |
| 320 | unsigned LT = Float64 ? WebAssembly::LT_F64 : WebAssembly::LT_F32; |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 321 | unsigned GE = Float64 ? WebAssembly::GE_F64 : WebAssembly::GE_F32; |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 322 | unsigned IConst = Int64 ? WebAssembly::CONST_I64 : WebAssembly::CONST_I32; |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 323 | unsigned Eqz = WebAssembly::EQZ_I32; |
| 324 | unsigned And = WebAssembly::AND_I32; |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 325 | int64_t Limit = Int64 ? INT64_MIN : INT32_MIN; |
| 326 | int64_t Substitute = IsUnsigned ? 0 : Limit; |
| 327 | double CmpVal = IsUnsigned ? -(double)Limit * 2.0 : -(double)Limit; |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 328 | auto &Context = BB->getParent()->getFunction().getContext(); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 329 | Type *Ty = Float64 ? Type::getDoubleTy(Context) : Type::getFloatTy(Context); |
| 330 | |
| 331 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 332 | MachineFunction *F = BB->getParent(); |
| 333 | MachineBasicBlock *TrueMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 334 | MachineBasicBlock *FalseMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 335 | MachineBasicBlock *DoneMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 336 | |
| 337 | MachineFunction::iterator It = ++BB->getIterator(); |
| 338 | F->insert(It, FalseMBB); |
| 339 | F->insert(It, TrueMBB); |
| 340 | F->insert(It, DoneMBB); |
| 341 | |
| 342 | // Transfer the remainder of BB and its successor edges to DoneMBB. |
| 343 | DoneMBB->splice(DoneMBB->begin(), BB, |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 344 | std::next(MachineBasicBlock::iterator(MI)), BB->end()); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 345 | DoneMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 346 | |
| 347 | BB->addSuccessor(TrueMBB); |
| 348 | BB->addSuccessor(FalseMBB); |
| 349 | TrueMBB->addSuccessor(DoneMBB); |
| 350 | FalseMBB->addSuccessor(DoneMBB); |
| 351 | |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 352 | unsigned Tmp0, Tmp1, CmpReg, EqzReg, FalseReg, TrueReg; |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 353 | Tmp0 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); |
| 354 | Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 355 | CmpReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); |
| 356 | EqzReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); |
| 357 | FalseReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg)); |
| 358 | TrueReg = MRI.createVirtualRegister(MRI.getRegClass(OutReg)); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 359 | |
| 360 | MI.eraseFromParent(); |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 361 | // For signed numbers, we can do a single comparison to determine whether |
| 362 | // fabs(x) is within range. |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 363 | if (IsUnsigned) { |
| 364 | Tmp0 = InReg; |
| 365 | } else { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 366 | BuildMI(BB, DL, TII.get(Abs), Tmp0).addReg(InReg); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 367 | } |
| 368 | BuildMI(BB, DL, TII.get(FConst), Tmp1) |
| 369 | .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, CmpVal))); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 370 | BuildMI(BB, DL, TII.get(LT), CmpReg).addReg(Tmp0).addReg(Tmp1); |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 371 | |
| 372 | // For unsigned numbers, we have to do a separate comparison with zero. |
| 373 | if (IsUnsigned) { |
| 374 | Tmp1 = MRI.createVirtualRegister(MRI.getRegClass(InReg)); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 375 | unsigned SecondCmpReg = |
| 376 | MRI.createVirtualRegister(&WebAssembly::I32RegClass); |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 377 | unsigned AndReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); |
| 378 | BuildMI(BB, DL, TII.get(FConst), Tmp1) |
| 379 | .addFPImm(cast<ConstantFP>(ConstantFP::get(Ty, 0.0))); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 380 | BuildMI(BB, DL, TII.get(GE), SecondCmpReg).addReg(Tmp0).addReg(Tmp1); |
| 381 | BuildMI(BB, DL, TII.get(And), AndReg).addReg(CmpReg).addReg(SecondCmpReg); |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 382 | CmpReg = AndReg; |
| 383 | } |
| 384 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 385 | BuildMI(BB, DL, TII.get(Eqz), EqzReg).addReg(CmpReg); |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 386 | |
| 387 | // Create the CFG diamond to select between doing the conversion or using |
| 388 | // the substitute value. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 389 | BuildMI(BB, DL, TII.get(WebAssembly::BR_IF)).addMBB(TrueMBB).addReg(EqzReg); |
| 390 | BuildMI(FalseMBB, DL, TII.get(LoweredOpcode), FalseReg).addReg(InReg); |
| 391 | BuildMI(FalseMBB, DL, TII.get(WebAssembly::BR)).addMBB(DoneMBB); |
| 392 | BuildMI(TrueMBB, DL, TII.get(IConst), TrueReg).addImm(Substitute); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 393 | BuildMI(*DoneMBB, DoneMBB->begin(), DL, TII.get(TargetOpcode::PHI), OutReg) |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 394 | .addReg(FalseReg) |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 395 | .addMBB(FalseMBB) |
Dan Gohman | 580c102 | 2017-11-29 20:20:11 +0000 | [diff] [blame] | 396 | .addReg(TrueReg) |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 397 | .addMBB(TrueMBB); |
| 398 | |
| 399 | return DoneMBB; |
| 400 | } |
| 401 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 402 | MachineBasicBlock *WebAssemblyTargetLowering::EmitInstrWithCustomInserter( |
| 403 | MachineInstr &MI, MachineBasicBlock *BB) const { |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 404 | const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); |
| 405 | DebugLoc DL = MI.getDebugLoc(); |
| 406 | |
| 407 | switch (MI.getOpcode()) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 408 | default: |
| 409 | llvm_unreachable("Unexpected instr type to insert"); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 410 | case WebAssembly::FP_TO_SINT_I32_F32: |
| 411 | return LowerFPToInt(MI, DL, BB, TII, false, false, false, |
| 412 | WebAssembly::I32_TRUNC_S_F32); |
| 413 | case WebAssembly::FP_TO_UINT_I32_F32: |
| 414 | return LowerFPToInt(MI, DL, BB, TII, true, false, false, |
| 415 | WebAssembly::I32_TRUNC_U_F32); |
| 416 | case WebAssembly::FP_TO_SINT_I64_F32: |
| 417 | return LowerFPToInt(MI, DL, BB, TII, false, true, false, |
| 418 | WebAssembly::I64_TRUNC_S_F32); |
| 419 | case WebAssembly::FP_TO_UINT_I64_F32: |
| 420 | return LowerFPToInt(MI, DL, BB, TII, true, true, false, |
| 421 | WebAssembly::I64_TRUNC_U_F32); |
| 422 | case WebAssembly::FP_TO_SINT_I32_F64: |
| 423 | return LowerFPToInt(MI, DL, BB, TII, false, false, true, |
| 424 | WebAssembly::I32_TRUNC_S_F64); |
| 425 | case WebAssembly::FP_TO_UINT_I32_F64: |
| 426 | return LowerFPToInt(MI, DL, BB, TII, true, false, true, |
| 427 | WebAssembly::I32_TRUNC_U_F64); |
| 428 | case WebAssembly::FP_TO_SINT_I64_F64: |
| 429 | return LowerFPToInt(MI, DL, BB, TII, false, true, true, |
| 430 | WebAssembly::I64_TRUNC_S_F64); |
| 431 | case WebAssembly::FP_TO_UINT_I64_F64: |
| 432 | return LowerFPToInt(MI, DL, BB, TII, true, true, true, |
| 433 | WebAssembly::I64_TRUNC_U_F64); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 434 | llvm_unreachable("Unexpected instruction to emit with custom inserter"); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 438 | const char * |
| 439 | WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const { |
JF Bastien | 480c840 | 2015-08-11 20:13:18 +0000 | [diff] [blame] | 440 | switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 441 | case WebAssemblyISD::FIRST_NUMBER: |
| 442 | break; |
| 443 | #define HANDLE_NODETYPE(NODE) \ |
| 444 | case WebAssemblyISD::NODE: \ |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 445 | return "WebAssemblyISD::" #NODE; |
| 446 | #include "WebAssemblyISD.def" |
| 447 | #undef HANDLE_NODETYPE |
JF Bastien | 480c840 | 2015-08-11 20:13:18 +0000 | [diff] [blame] | 448 | } |
| 449 | return nullptr; |
| 450 | } |
| 451 | |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 452 | std::pair<unsigned, const TargetRegisterClass *> |
| 453 | WebAssemblyTargetLowering::getRegForInlineAsmConstraint( |
| 454 | const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { |
| 455 | // First, see if this is a constraint that directly corresponds to a |
| 456 | // WebAssembly register class. |
| 457 | if (Constraint.size() == 1) { |
| 458 | switch (Constraint[0]) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 459 | case 'r': |
| 460 | assert(VT != MVT::iPTR && "Pointer MVT not expected here"); |
| 461 | if (Subtarget->hasSIMD128() && VT.isVector()) { |
| 462 | if (VT.getSizeInBits() == 128) |
| 463 | return std::make_pair(0U, &WebAssembly::V128RegClass); |
| 464 | } |
| 465 | if (VT.isInteger() && !VT.isVector()) { |
| 466 | if (VT.getSizeInBits() <= 32) |
| 467 | return std::make_pair(0U, &WebAssembly::I32RegClass); |
| 468 | if (VT.getSizeInBits() <= 64) |
| 469 | return std::make_pair(0U, &WebAssembly::I64RegClass); |
| 470 | } |
| 471 | break; |
| 472 | default: |
| 473 | break; |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); |
| 478 | } |
| 479 | |
Dan Gohman | 3192ddf | 2015-11-19 23:04:59 +0000 | [diff] [blame] | 480 | bool WebAssemblyTargetLowering::isCheapToSpeculateCttz() const { |
| 481 | // Assume ctz is a relatively cheap operation. |
| 482 | return true; |
| 483 | } |
| 484 | |
| 485 | bool WebAssemblyTargetLowering::isCheapToSpeculateCtlz() const { |
| 486 | // Assume clz is a relatively cheap operation. |
| 487 | return true; |
| 488 | } |
| 489 | |
Dan Gohman | 4b9d791 | 2015-12-15 22:01:29 +0000 | [diff] [blame] | 490 | bool WebAssemblyTargetLowering::isLegalAddressingMode(const DataLayout &DL, |
| 491 | const AddrMode &AM, |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 492 | Type *Ty, unsigned AS, |
Jonas Paulsson | 024e319 | 2017-07-21 11:59:37 +0000 | [diff] [blame] | 493 | Instruction *I) const { |
Dan Gohman | 4b9d791 | 2015-12-15 22:01:29 +0000 | [diff] [blame] | 494 | // WebAssembly offsets are added as unsigned without wrapping. The |
| 495 | // isLegalAddressingMode gives us no way to determine if wrapping could be |
| 496 | // happening, so we approximate this by accepting only non-negative offsets. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 497 | if (AM.BaseOffs < 0) |
| 498 | return false; |
Dan Gohman | 4b9d791 | 2015-12-15 22:01:29 +0000 | [diff] [blame] | 499 | |
| 500 | // WebAssembly has no scale register operands. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 501 | if (AM.Scale != 0) |
| 502 | return false; |
Dan Gohman | 4b9d791 | 2015-12-15 22:01:29 +0000 | [diff] [blame] | 503 | |
| 504 | // Everything else is legal. |
| 505 | return true; |
| 506 | } |
| 507 | |
Dan Gohman | bb37224 | 2016-01-26 03:39:31 +0000 | [diff] [blame] | 508 | bool WebAssemblyTargetLowering::allowsMisalignedMemoryAccesses( |
Derek Schuff | 3f06329 | 2016-02-11 20:57:09 +0000 | [diff] [blame] | 509 | EVT /*VT*/, unsigned /*AddrSpace*/, unsigned /*Align*/, bool *Fast) const { |
Dan Gohman | bb37224 | 2016-01-26 03:39:31 +0000 | [diff] [blame] | 510 | // WebAssembly supports unaligned accesses, though it should be declared |
| 511 | // with the p2align attribute on loads and stores which do so, and there |
| 512 | // may be a performance impact. We tell LLVM they're "fast" because |
Dan Gohman | fb619e9 | 2016-01-26 14:55:17 +0000 | [diff] [blame] | 513 | // for the kinds of things that LLVM uses this for (merging adjacent stores |
Dan Gohman | bb37224 | 2016-01-26 03:39:31 +0000 | [diff] [blame] | 514 | // of constants, etc.), WebAssembly implementations will either want the |
| 515 | // unaligned access or they'll split anyway. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 516 | if (Fast) |
| 517 | *Fast = true; |
Dan Gohman | bb37224 | 2016-01-26 03:39:31 +0000 | [diff] [blame] | 518 | return true; |
| 519 | } |
| 520 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 521 | bool WebAssemblyTargetLowering::isIntDivCheap(EVT VT, |
| 522 | AttributeList Attr) const { |
Dan Gohman | b4c3c38 | 2016-05-18 14:29:42 +0000 | [diff] [blame] | 523 | // The current thinking is that wasm engines will perform this optimization, |
| 524 | // so we can save on code size. |
| 525 | return true; |
| 526 | } |
| 527 | |
Simon Pilgrim | 99f7016 | 2018-06-28 17:27:09 +0000 | [diff] [blame] | 528 | EVT WebAssemblyTargetLowering::getSetCCResultType(const DataLayout &DL, |
| 529 | LLVMContext &C, |
| 530 | EVT VT) const { |
| 531 | if (VT.isVector()) |
| 532 | return VT.changeVectorElementTypeToInteger(); |
| 533 | |
| 534 | return TargetLowering::getSetCCResultType(DL, C, VT); |
| 535 | } |
| 536 | |
Heejin Ahn | 4128cb0 | 2018-08-02 21:44:24 +0000 | [diff] [blame] | 537 | bool WebAssemblyTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, |
| 538 | const CallInst &I, |
| 539 | MachineFunction &MF, |
| 540 | unsigned Intrinsic) const { |
| 541 | switch (Intrinsic) { |
| 542 | case Intrinsic::wasm_atomic_notify: |
| 543 | Info.opc = ISD::INTRINSIC_W_CHAIN; |
| 544 | Info.memVT = MVT::i32; |
| 545 | Info.ptrVal = I.getArgOperand(0); |
| 546 | Info.offset = 0; |
| 547 | Info.align = 4; |
| 548 | // atomic.notify instruction does not really load the memory specified with |
| 549 | // this argument, but MachineMemOperand should either be load or store, so |
| 550 | // we set this to a load. |
| 551 | // FIXME Volatile isn't really correct, but currently all LLVM atomic |
| 552 | // instructions are treated as volatiles in the backend, so we should be |
| 553 | // consistent. The same applies for wasm_atomic_wait intrinsics too. |
| 554 | Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; |
| 555 | return true; |
| 556 | case Intrinsic::wasm_atomic_wait_i32: |
| 557 | Info.opc = ISD::INTRINSIC_W_CHAIN; |
| 558 | Info.memVT = MVT::i32; |
| 559 | Info.ptrVal = I.getArgOperand(0); |
| 560 | Info.offset = 0; |
| 561 | Info.align = 4; |
| 562 | Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; |
| 563 | return true; |
| 564 | case Intrinsic::wasm_atomic_wait_i64: |
| 565 | Info.opc = ISD::INTRINSIC_W_CHAIN; |
| 566 | Info.memVT = MVT::i64; |
| 567 | Info.ptrVal = I.getArgOperand(0); |
| 568 | Info.offset = 0; |
| 569 | Info.align = 8; |
| 570 | Info.flags = MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad; |
| 571 | return true; |
| 572 | default: |
| 573 | return false; |
| 574 | } |
| 575 | } |
| 576 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 577 | //===----------------------------------------------------------------------===// |
| 578 | // WebAssembly Lowering private implementation. |
| 579 | //===----------------------------------------------------------------------===// |
| 580 | |
| 581 | //===----------------------------------------------------------------------===// |
| 582 | // Lowering Code |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 585 | static void fail(const SDLoc &DL, SelectionDAG &DAG, const char *msg) { |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 586 | MachineFunction &MF = DAG.getMachineFunction(); |
| 587 | DAG.getContext()->diagnose( |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 588 | DiagnosticInfoUnsupported(MF.getFunction(), msg, DL.getDebugLoc())); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Dan Gohman | 85dbdda | 2015-12-04 17:16:07 +0000 | [diff] [blame] | 591 | // Test whether the given calling convention is supported. |
Dan Gohman | a3f5ce5 | 2015-12-04 17:18:32 +0000 | [diff] [blame] | 592 | static bool CallingConvSupported(CallingConv::ID CallConv) { |
Dan Gohman | 85dbdda | 2015-12-04 17:16:07 +0000 | [diff] [blame] | 593 | // We currently support the language-independent target-independent |
Dan Gohman | 1ce2b1a | 2015-12-04 18:27:03 +0000 | [diff] [blame] | 594 | // conventions. We don't yet have a way to annotate calls with properties like |
| 595 | // "cold", and we don't have any call-clobbered registers, so these are mostly |
| 596 | // all handled the same. |
Dan Gohman | a3f5ce5 | 2015-12-04 17:18:32 +0000 | [diff] [blame] | 597 | return CallConv == CallingConv::C || CallConv == CallingConv::Fast || |
Dan Gohman | 1ce2b1a | 2015-12-04 18:27:03 +0000 | [diff] [blame] | 598 | CallConv == CallingConv::Cold || |
| 599 | CallConv == CallingConv::PreserveMost || |
| 600 | CallConv == CallingConv::PreserveAll || |
| 601 | CallConv == CallingConv::CXX_FAST_TLS; |
Dan Gohman | 85dbdda | 2015-12-04 17:16:07 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 604 | SDValue |
| 605 | WebAssemblyTargetLowering::LowerCall(CallLoweringInfo &CLI, |
| 606 | SmallVectorImpl<SDValue> &InVals) const { |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 607 | SelectionDAG &DAG = CLI.DAG; |
| 608 | SDLoc DL = CLI.DL; |
| 609 | SDValue Chain = CLI.Chain; |
| 610 | SDValue Callee = CLI.Callee; |
| 611 | MachineFunction &MF = DAG.getMachineFunction(); |
Derek Schuff | 992d83f | 2016-02-10 20:14:15 +0000 | [diff] [blame] | 612 | auto Layout = MF.getDataLayout(); |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 613 | |
| 614 | CallingConv::ID CallConv = CLI.CallConv; |
Dan Gohman | 85dbdda | 2015-12-04 17:16:07 +0000 | [diff] [blame] | 615 | if (!CallingConvSupported(CallConv)) |
Dan Gohman | 9cc692b | 2015-10-02 20:54:23 +0000 | [diff] [blame] | 616 | fail(DL, DAG, |
| 617 | "WebAssembly doesn't support language-specific or target-specific " |
| 618 | "calling conventions yet"); |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 619 | if (CLI.IsPatchPoint) |
| 620 | fail(DL, DAG, "WebAssembly doesn't support patch point yet"); |
| 621 | |
Dan Gohman | 9cc692b | 2015-10-02 20:54:23 +0000 | [diff] [blame] | 622 | // WebAssembly doesn't currently support explicit tail calls. If they are |
| 623 | // required, fail. Otherwise, just disable them. |
| 624 | if ((CallConv == CallingConv::Fast && CLI.IsTailCall && |
| 625 | MF.getTarget().Options.GuaranteedTailCallOpt) || |
Peter Collingbourne | 081ffe2 | 2017-07-26 19:15:29 +0000 | [diff] [blame] | 626 | (CLI.CS && CLI.CS.isMustTailCall())) |
Dan Gohman | 9cc692b | 2015-10-02 20:54:23 +0000 | [diff] [blame] | 627 | fail(DL, DAG, "WebAssembly doesn't support tail call yet"); |
| 628 | CLI.IsTailCall = false; |
| 629 | |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 630 | SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; |
Dan Gohman | e590b33 | 2015-09-09 01:52:45 +0000 | [diff] [blame] | 631 | if (Ins.size() > 1) |
| 632 | fail(DL, DAG, "WebAssembly doesn't support more than 1 returned value yet"); |
| 633 | |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 634 | SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; |
Derek Schuff | 4dd6778 | 2016-01-27 21:17:39 +0000 | [diff] [blame] | 635 | SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; |
Dan Gohman | 910ba33 | 2018-06-26 03:18:38 +0000 | [diff] [blame] | 636 | unsigned NumFixedArgs = 0; |
Derek Schuff | 4dd6778 | 2016-01-27 21:17:39 +0000 | [diff] [blame] | 637 | for (unsigned i = 0; i < Outs.size(); ++i) { |
| 638 | const ISD::OutputArg &Out = Outs[i]; |
| 639 | SDValue &OutVal = OutVals[i]; |
Dan Gohman | 7935fa3 | 2015-12-10 00:22:40 +0000 | [diff] [blame] | 640 | if (Out.Flags.isNest()) |
| 641 | fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 642 | if (Out.Flags.isInAlloca()) |
Dan Gohman | 7935fa3 | 2015-12-10 00:22:40 +0000 | [diff] [blame] | 643 | fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 644 | if (Out.Flags.isInConsecutiveRegs()) |
Dan Gohman | 7935fa3 | 2015-12-10 00:22:40 +0000 | [diff] [blame] | 645 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 646 | if (Out.Flags.isInConsecutiveRegsLast()) |
Dan Gohman | 7935fa3 | 2015-12-10 00:22:40 +0000 | [diff] [blame] | 647 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); |
Dan Gohman | a6771b3 | 2016-02-12 21:30:18 +0000 | [diff] [blame] | 648 | if (Out.Flags.isByVal() && Out.Flags.getByValSize() != 0) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 649 | auto &MFI = MF.getFrameInfo(); |
| 650 | int FI = MFI.CreateStackObject(Out.Flags.getByValSize(), |
| 651 | Out.Flags.getByValAlign(), |
| 652 | /*isSS=*/false); |
Derek Schuff | 4dd6778 | 2016-01-27 21:17:39 +0000 | [diff] [blame] | 653 | SDValue SizeNode = |
| 654 | DAG.getConstant(Out.Flags.getByValSize(), DL, MVT::i32); |
Derek Schuff | 992d83f | 2016-02-10 20:14:15 +0000 | [diff] [blame] | 655 | SDValue FINode = DAG.getFrameIndex(FI, getPointerTy(Layout)); |
Derek Schuff | 4dd6778 | 2016-01-27 21:17:39 +0000 | [diff] [blame] | 656 | Chain = DAG.getMemcpy( |
| 657 | Chain, DL, FINode, OutVal, SizeNode, Out.Flags.getByValAlign(), |
Dan Gohman | 476ffce | 2016-02-17 01:43:37 +0000 | [diff] [blame] | 658 | /*isVolatile*/ false, /*AlwaysInline=*/false, |
Derek Schuff | 4dd6778 | 2016-01-27 21:17:39 +0000 | [diff] [blame] | 659 | /*isTailCall*/ false, MachinePointerInfo(), MachinePointerInfo()); |
| 660 | OutVal = FINode; |
| 661 | } |
Dan Gohman | 910ba33 | 2018-06-26 03:18:38 +0000 | [diff] [blame] | 662 | // Count the number of fixed args *after* legalization. |
| 663 | NumFixedArgs += Out.IsFixed; |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 664 | } |
| 665 | |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 666 | bool IsVarArg = CLI.IsVarArg; |
Derek Schuff | 992d83f | 2016-02-10 20:14:15 +0000 | [diff] [blame] | 667 | auto PtrVT = getPointerTy(Layout); |
Dan Gohman | e590b33 | 2015-09-09 01:52:45 +0000 | [diff] [blame] | 668 | |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 669 | // Analyze operands of the call, assigning locations to each operand. |
| 670 | SmallVector<CCValAssign, 16> ArgLocs; |
| 671 | CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 672 | |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 673 | if (IsVarArg) { |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 674 | // Outgoing non-fixed arguments are placed in a buffer. First |
| 675 | // compute their offsets and the total amount of buffer space needed. |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 676 | for (SDValue Arg : |
| 677 | make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) { |
| 678 | EVT VT = Arg.getValueType(); |
| 679 | assert(VT != MVT::iPTR && "Legalized args should be concrete"); |
| 680 | Type *Ty = VT.getTypeForEVT(*DAG.getContext()); |
Derek Schuff | 992d83f | 2016-02-10 20:14:15 +0000 | [diff] [blame] | 681 | unsigned Offset = CCInfo.AllocateStack(Layout.getTypeAllocSize(Ty), |
| 682 | Layout.getABITypeAlignment(Ty)); |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 683 | CCInfo.addLoc(CCValAssign::getMem(ArgLocs.size(), VT.getSimpleVT(), |
| 684 | Offset, VT.getSimpleVT(), |
| 685 | CCValAssign::Full)); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | unsigned NumBytes = CCInfo.getAlignedCallFrameSize(); |
| 690 | |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 691 | SDValue FINode; |
| 692 | if (IsVarArg && NumBytes) { |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 693 | // For non-fixed arguments, next emit stores to store the argument values |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 694 | // to the stack buffer at the offsets computed above. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 695 | int FI = MF.getFrameInfo().CreateStackObject(NumBytes, |
| 696 | Layout.getStackAlignment(), |
| 697 | /*isSS=*/false); |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 698 | unsigned ValNo = 0; |
| 699 | SmallVector<SDValue, 8> Chains; |
| 700 | for (SDValue Arg : |
| 701 | make_range(OutVals.begin() + NumFixedArgs, OutVals.end())) { |
| 702 | assert(ArgLocs[ValNo].getValNo() == ValNo && |
| 703 | "ArgLocs should remain in order and only hold varargs args"); |
| 704 | unsigned Offset = ArgLocs[ValNo++].getLocMemOffset(); |
Derek Schuff | 992d83f | 2016-02-10 20:14:15 +0000 | [diff] [blame] | 705 | FINode = DAG.getFrameIndex(FI, getPointerTy(Layout)); |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 706 | SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, FINode, |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 707 | DAG.getConstant(Offset, DL, PtrVT)); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 708 | Chains.push_back( |
| 709 | DAG.getStore(Chain, DL, Arg, Add, |
| 710 | MachinePointerInfo::getFixedStack(MF, FI, Offset), 0)); |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 711 | } |
| 712 | if (!Chains.empty()) |
| 713 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 714 | } else if (IsVarArg) { |
| 715 | FINode = DAG.getIntPtrConstant(0, DL); |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | // Compute the operands for the CALLn node. |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 719 | SmallVector<SDValue, 16> Ops; |
| 720 | Ops.push_back(Chain); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 721 | Ops.push_back(Callee); |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 722 | |
| 723 | // Add all fixed arguments. Note that for non-varargs calls, NumFixedArgs |
| 724 | // isn't reliable. |
| 725 | Ops.append(OutVals.begin(), |
| 726 | IsVarArg ? OutVals.begin() + NumFixedArgs : OutVals.end()); |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 727 | // Add a pointer to the vararg buffer. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 728 | if (IsVarArg) |
| 729 | Ops.push_back(FINode); |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 730 | |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 731 | SmallVector<EVT, 8> InTys; |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 732 | for (const auto &In : Ins) { |
Dan Gohman | 7935fa3 | 2015-12-10 00:22:40 +0000 | [diff] [blame] | 733 | assert(!In.Flags.isByVal() && "byval is not valid for return values"); |
| 734 | assert(!In.Flags.isNest() && "nest is not valid for return values"); |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 735 | if (In.Flags.isInAlloca()) |
Dan Gohman | 7935fa3 | 2015-12-10 00:22:40 +0000 | [diff] [blame] | 736 | fail(DL, DAG, "WebAssembly hasn't implemented inalloca return values"); |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 737 | if (In.Flags.isInConsecutiveRegs()) |
Dan Gohman | 7935fa3 | 2015-12-10 00:22:40 +0000 | [diff] [blame] | 738 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs return values"); |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 739 | if (In.Flags.isInConsecutiveRegsLast()) |
Dan Gohman | 4b9d791 | 2015-12-15 22:01:29 +0000 | [diff] [blame] | 740 | fail(DL, DAG, |
| 741 | "WebAssembly hasn't implemented cons regs last return values"); |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 742 | // Ignore In.getOrigAlign() because all our arguments are passed in |
| 743 | // registers. |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 744 | InTys.push_back(In.VT); |
Dan Gohman | 2d822e7 | 2015-12-04 17:12:52 +0000 | [diff] [blame] | 745 | } |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 746 | InTys.push_back(MVT::Other); |
| 747 | SDVTList InTyList = DAG.getVTList(InTys); |
Dan Gohman | f71abef | 2015-09-09 16:13:47 +0000 | [diff] [blame] | 748 | SDValue Res = |
| 749 | DAG.getNode(Ins.empty() ? WebAssemblyISD::CALL0 : WebAssemblyISD::CALL1, |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 750 | DL, InTyList, Ops); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 751 | if (Ins.empty()) { |
| 752 | Chain = Res; |
| 753 | } else { |
| 754 | InVals.push_back(Res); |
| 755 | Chain = Res.getValue(1); |
| 756 | } |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 757 | |
JF Bastien | d8a9d66 | 2015-08-24 21:59:51 +0000 | [diff] [blame] | 758 | return Chain; |
| 759 | } |
| 760 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 761 | bool WebAssemblyTargetLowering::CanLowerReturn( |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 762 | CallingConv::ID /*CallConv*/, MachineFunction & /*MF*/, bool /*IsVarArg*/, |
| 763 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 764 | LLVMContext & /*Context*/) const { |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 765 | // WebAssembly can't currently handle returning tuples. |
| 766 | return Outs.size() <= 1; |
| 767 | } |
| 768 | |
| 769 | SDValue WebAssemblyTargetLowering::LowerReturn( |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 770 | SDValue Chain, CallingConv::ID CallConv, bool /*IsVarArg*/, |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 771 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 772 | const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL, |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 773 | SelectionDAG &DAG) const { |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 774 | assert(Outs.size() <= 1 && "WebAssembly can only return up to one value"); |
Dan Gohman | 85dbdda | 2015-12-04 17:16:07 +0000 | [diff] [blame] | 775 | if (!CallingConvSupported(CallConv)) |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 776 | fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); |
| 777 | |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 778 | SmallVector<SDValue, 4> RetOps(1, Chain); |
| 779 | RetOps.append(OutVals.begin(), OutVals.end()); |
JF Bastien | 4a2d560 | 2015-07-31 21:04:18 +0000 | [diff] [blame] | 780 | Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 781 | |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 782 | // Record the number and types of the return values. |
| 783 | for (const ISD::OutputArg &Out : Outs) { |
Dan Gohman | ac132e9 | 2015-12-02 23:40:03 +0000 | [diff] [blame] | 784 | assert(!Out.Flags.isByVal() && "byval is not valid for return values"); |
| 785 | assert(!Out.Flags.isNest() && "nest is not valid for return values"); |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 786 | assert(Out.IsFixed && "non-fixed return value is not valid"); |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 787 | if (Out.Flags.isInAlloca()) |
| 788 | fail(DL, DAG, "WebAssembly hasn't implemented inalloca results"); |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 789 | if (Out.Flags.isInConsecutiveRegs()) |
| 790 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs results"); |
| 791 | if (Out.Flags.isInConsecutiveRegsLast()) |
| 792 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs last results"); |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 793 | } |
| 794 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 795 | return Chain; |
| 796 | } |
| 797 | |
| 798 | SDValue WebAssemblyTargetLowering::LowerFormalArguments( |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 799 | SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 800 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, |
| 801 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { |
Dan Gohman | 85dbdda | 2015-12-04 17:16:07 +0000 | [diff] [blame] | 802 | if (!CallingConvSupported(CallConv)) |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 803 | fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 804 | |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 805 | MachineFunction &MF = DAG.getMachineFunction(); |
| 806 | auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>(); |
| 807 | |
Dan Gohman | fb3e059 | 2015-11-25 19:36:19 +0000 | [diff] [blame] | 808 | // Set up the incoming ARGUMENTS value, which serves to represent the liveness |
| 809 | // of the incoming values before they're represented by virtual registers. |
| 810 | MF.getRegInfo().addLiveIn(WebAssembly::ARGUMENTS); |
| 811 | |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 812 | for (const ISD::InputArg &In : Ins) { |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 813 | if (In.Flags.isInAlloca()) |
| 814 | fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); |
| 815 | if (In.Flags.isNest()) |
| 816 | fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 817 | if (In.Flags.isInConsecutiveRegs()) |
| 818 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); |
| 819 | if (In.Flags.isInConsecutiveRegsLast()) |
| 820 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); |
Dan Gohman | 9c54d3b | 2015-11-25 18:13:18 +0000 | [diff] [blame] | 821 | // Ignore In.getOrigAlign() because all our arguments are passed in |
| 822 | // registers. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 823 | InVals.push_back(In.Used ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT, |
| 824 | DAG.getTargetConstant(InVals.size(), |
| 825 | DL, MVT::i32)) |
| 826 | : DAG.getUNDEF(In.VT)); |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 827 | |
| 828 | // Record the number and types of arguments. |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 829 | MFI->addParam(In.VT); |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 830 | } |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 831 | |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 832 | // Varargs are copied into a buffer allocated by the caller, and a pointer to |
| 833 | // the buffer is passed as an argument. |
| 834 | if (IsVarArg) { |
| 835 | MVT PtrVT = getPointerTy(MF.getDataLayout()); |
| 836 | unsigned VarargVreg = |
| 837 | MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrVT)); |
| 838 | MFI->setVarargBufferVreg(VarargVreg); |
| 839 | Chain = DAG.getCopyToReg( |
| 840 | Chain, DL, VarargVreg, |
| 841 | DAG.getNode(WebAssemblyISD::ARGUMENT, DL, PtrVT, |
| 842 | DAG.getTargetConstant(Ins.size(), DL, MVT::i32))); |
| 843 | MFI->addParam(PtrVT); |
| 844 | } |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 845 | |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 846 | // Record the number and types of arguments and results. |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 847 | SmallVector<MVT, 4> Params; |
| 848 | SmallVector<MVT, 4> Results; |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 849 | ComputeSignatureVTs(MF.getFunction().getFunctionType(), MF.getFunction(), |
| 850 | DAG.getTarget(), Params, Results); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 851 | for (MVT VT : Results) |
| 852 | MFI->addResult(VT); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 853 | // TODO: Use signatures in WebAssemblyMachineFunctionInfo too and unify |
| 854 | // the param logic here with ComputeSignatureVTs |
| 855 | assert(MFI->getParams().size() == Params.size() && |
| 856 | std::equal(MFI->getParams().begin(), MFI->getParams().end(), |
| 857 | Params.begin())); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 858 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 859 | return Chain; |
| 860 | } |
| 861 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 862 | //===----------------------------------------------------------------------===// |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 863 | // Custom lowering hooks. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 864 | //===----------------------------------------------------------------------===// |
| 865 | |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 866 | SDValue WebAssemblyTargetLowering::LowerOperation(SDValue Op, |
| 867 | SelectionDAG &DAG) const { |
Derek Schuff | 51699a8 | 2016-02-12 22:56:03 +0000 | [diff] [blame] | 868 | SDLoc DL(Op); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 869 | switch (Op.getOpcode()) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 870 | default: |
| 871 | llvm_unreachable("unimplemented operation lowering"); |
| 872 | return SDValue(); |
| 873 | case ISD::FrameIndex: |
| 874 | return LowerFrameIndex(Op, DAG); |
| 875 | case ISD::GlobalAddress: |
| 876 | return LowerGlobalAddress(Op, DAG); |
| 877 | case ISD::ExternalSymbol: |
| 878 | return LowerExternalSymbol(Op, DAG); |
| 879 | case ISD::JumpTable: |
| 880 | return LowerJumpTable(Op, DAG); |
| 881 | case ISD::BR_JT: |
| 882 | return LowerBR_JT(Op, DAG); |
| 883 | case ISD::VASTART: |
| 884 | return LowerVASTART(Op, DAG); |
| 885 | case ISD::BlockAddress: |
| 886 | case ISD::BRIND: |
| 887 | fail(DL, DAG, "WebAssembly hasn't implemented computed gotos"); |
| 888 | return SDValue(); |
| 889 | case ISD::RETURNADDR: // Probably nothing meaningful can be returned here. |
| 890 | fail(DL, DAG, "WebAssembly hasn't implemented __builtin_return_address"); |
| 891 | return SDValue(); |
| 892 | case ISD::FRAMEADDR: |
| 893 | return LowerFRAMEADDR(Op, DAG); |
| 894 | case ISD::CopyToReg: |
| 895 | return LowerCopyToReg(Op, DAG); |
| 896 | case ISD::INTRINSIC_WO_CHAIN: |
| 897 | return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
Thomas Lively | fb84fd7 | 2018-11-02 00:06:56 +0000 | [diff] [blame] | 898 | case ISD::EXTRACT_VECTOR_ELT: |
| 899 | case ISD::INSERT_VECTOR_ELT: |
| 900 | return LowerAccessVectorElement(Op, DAG); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 901 | case ISD::INTRINSIC_VOID: |
| 902 | return LowerINTRINSIC_VOID(Op, DAG); |
Thomas Lively | a0d2581 | 2018-09-07 21:54:46 +0000 | [diff] [blame] | 903 | case ISD::VECTOR_SHUFFLE: |
| 904 | return LowerVECTOR_SHUFFLE(Op, DAG); |
Thomas Lively | 55735d5 | 2018-10-20 01:31:18 +0000 | [diff] [blame] | 905 | case ISD::SHL: |
| 906 | case ISD::SRA: |
| 907 | case ISD::SRL: |
| 908 | return LowerShift(Op, DAG); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Derek Schuff | aadc89c | 2016-02-16 18:18:36 +0000 | [diff] [blame] | 912 | SDValue WebAssemblyTargetLowering::LowerCopyToReg(SDValue Op, |
| 913 | SelectionDAG &DAG) const { |
| 914 | SDValue Src = Op.getOperand(2); |
| 915 | if (isa<FrameIndexSDNode>(Src.getNode())) { |
| 916 | // CopyToReg nodes don't support FrameIndex operands. Other targets select |
| 917 | // the FI to some LEA-like instruction, but since we don't have that, we |
| 918 | // need to insert some kind of instruction that can take an FI operand and |
| 919 | // produces a value usable by CopyToReg (i.e. in a vreg). So insert a dummy |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame^] | 920 | // local.copy between Op and its FI operand. |
Dan Gohman | 02c0871 | 2016-02-20 23:09:44 +0000 | [diff] [blame] | 921 | SDValue Chain = Op.getOperand(0); |
Derek Schuff | aadc89c | 2016-02-16 18:18:36 +0000 | [diff] [blame] | 922 | SDLoc DL(Op); |
Dan Gohman | 02c0871 | 2016-02-20 23:09:44 +0000 | [diff] [blame] | 923 | unsigned Reg = cast<RegisterSDNode>(Op.getOperand(1))->getReg(); |
Derek Schuff | aadc89c | 2016-02-16 18:18:36 +0000 | [diff] [blame] | 924 | EVT VT = Src.getValueType(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 925 | SDValue Copy(DAG.getMachineNode(VT == MVT::i32 ? WebAssembly::COPY_I32 |
| 926 | : WebAssembly::COPY_I64, |
| 927 | DL, VT, Src), |
| 928 | 0); |
Dan Gohman | 02c0871 | 2016-02-20 23:09:44 +0000 | [diff] [blame] | 929 | return Op.getNode()->getNumValues() == 1 |
| 930 | ? DAG.getCopyToReg(Chain, DL, Reg, Copy) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 931 | : DAG.getCopyToReg(Chain, DL, Reg, Copy, |
| 932 | Op.getNumOperands() == 4 ? Op.getOperand(3) |
| 933 | : SDValue()); |
Derek Schuff | aadc89c | 2016-02-16 18:18:36 +0000 | [diff] [blame] | 934 | } |
| 935 | return SDValue(); |
| 936 | } |
| 937 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 938 | SDValue WebAssemblyTargetLowering::LowerFrameIndex(SDValue Op, |
| 939 | SelectionDAG &DAG) const { |
| 940 | int FI = cast<FrameIndexSDNode>(Op)->getIndex(); |
| 941 | return DAG.getTargetFrameIndex(FI, Op.getValueType()); |
| 942 | } |
| 943 | |
Dan Gohman | 94c6566 | 2016-02-16 23:48:04 +0000 | [diff] [blame] | 944 | SDValue WebAssemblyTargetLowering::LowerFRAMEADDR(SDValue Op, |
| 945 | SelectionDAG &DAG) const { |
| 946 | // Non-zero depths are not supported by WebAssembly currently. Use the |
| 947 | // legalizer's default expansion, which is to return 0 (what this function is |
| 948 | // documented to do). |
Dan Gohman | 1d547bf | 2016-02-17 00:14:03 +0000 | [diff] [blame] | 949 | if (Op.getConstantOperandVal(0) > 0) |
Dan Gohman | 94c6566 | 2016-02-16 23:48:04 +0000 | [diff] [blame] | 950 | return SDValue(); |
| 951 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 952 | DAG.getMachineFunction().getFrameInfo().setFrameAddressIsTaken(true); |
Dan Gohman | 94c6566 | 2016-02-16 23:48:04 +0000 | [diff] [blame] | 953 | EVT VT = Op.getValueType(); |
| 954 | unsigned FP = |
| 955 | Subtarget->getRegisterInfo()->getFrameRegister(DAG.getMachineFunction()); |
| 956 | return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), FP, VT); |
| 957 | } |
| 958 | |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 959 | SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op, |
| 960 | SelectionDAG &DAG) const { |
| 961 | SDLoc DL(Op); |
| 962 | const auto *GA = cast<GlobalAddressSDNode>(Op); |
| 963 | EVT VT = Op.getValueType(); |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 964 | assert(GA->getTargetFlags() == 0 && |
| 965 | "Unexpected target flags on generic GlobalAddressSDNode"); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 966 | if (GA->getAddressSpace() != 0) |
| 967 | fail(DL, DAG, "WebAssembly only expects the 0 address space"); |
Dan Gohman | 4b9d791 | 2015-12-15 22:01:29 +0000 | [diff] [blame] | 968 | return DAG.getNode( |
| 969 | WebAssemblyISD::Wrapper, DL, VT, |
| 970 | DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, GA->getOffset())); |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 973 | SDValue |
| 974 | WebAssemblyTargetLowering::LowerExternalSymbol(SDValue Op, |
| 975 | SelectionDAG &DAG) const { |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 976 | SDLoc DL(Op); |
| 977 | const auto *ES = cast<ExternalSymbolSDNode>(Op); |
| 978 | EVT VT = Op.getValueType(); |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 979 | assert(ES->getTargetFlags() == 0 && |
| 980 | "Unexpected target flags on generic ExternalSymbolSDNode"); |
| 981 | // Set the TargetFlags to 0x1 which indicates that this is a "function" |
| 982 | // symbol rather than a data symbol. We do this unconditionally even though |
| 983 | // we don't know anything about the symbol other than its name, because all |
| 984 | // external symbols used in target-independent SelectionDAG code are for |
| 985 | // functions. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 986 | return DAG.getNode( |
| 987 | WebAssemblyISD::Wrapper, DL, VT, |
| 988 | DAG.getTargetExternalSymbol(ES->getSymbol(), VT, |
| 989 | WebAssemblyII::MO_SYMBOL_FUNCTION)); |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 992 | SDValue WebAssemblyTargetLowering::LowerJumpTable(SDValue Op, |
| 993 | SelectionDAG &DAG) const { |
| 994 | // There's no need for a Wrapper node because we always incorporate a jump |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 995 | // table operand into a BR_TABLE instruction, rather than ever |
Dan Gohman | bb7ce8e | 2015-11-20 03:02:49 +0000 | [diff] [blame] | 996 | // materializing it in a register. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 997 | const JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| 998 | return DAG.getTargetJumpTable(JT->getIndex(), Op.getValueType(), |
| 999 | JT->getTargetFlags()); |
| 1000 | } |
| 1001 | |
| 1002 | SDValue WebAssemblyTargetLowering::LowerBR_JT(SDValue Op, |
| 1003 | SelectionDAG &DAG) const { |
| 1004 | SDLoc DL(Op); |
| 1005 | SDValue Chain = Op.getOperand(0); |
| 1006 | const auto *JT = cast<JumpTableSDNode>(Op.getOperand(1)); |
| 1007 | SDValue Index = Op.getOperand(2); |
| 1008 | assert(JT->getTargetFlags() == 0 && "WebAssembly doesn't set target flags"); |
| 1009 | |
| 1010 | SmallVector<SDValue, 8> Ops; |
| 1011 | Ops.push_back(Chain); |
| 1012 | Ops.push_back(Index); |
| 1013 | |
| 1014 | MachineJumpTableInfo *MJTI = DAG.getMachineFunction().getJumpTableInfo(); |
| 1015 | const auto &MBBs = MJTI->getJumpTables()[JT->getIndex()].MBBs; |
| 1016 | |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 1017 | // Add an operand for each case. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 1018 | for (auto MBB : MBBs) |
| 1019 | Ops.push_back(DAG.getBasicBlock(MBB)); |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 1020 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 1021 | // TODO: For now, we just pick something arbitrary for a default case for now. |
| 1022 | // We really want to sniff out the guard and put in the real default case (and |
| 1023 | // delete the guard). |
| 1024 | Ops.push_back(DAG.getBasicBlock(MBBs[0])); |
| 1025 | |
Dan Gohman | 1402606 | 2016-03-08 03:18:12 +0000 | [diff] [blame] | 1026 | return DAG.getNode(WebAssemblyISD::BR_TABLE, DL, MVT::Other, Ops); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 1029 | SDValue WebAssemblyTargetLowering::LowerVASTART(SDValue Op, |
| 1030 | SelectionDAG &DAG) const { |
| 1031 | SDLoc DL(Op); |
| 1032 | EVT PtrVT = getPointerTy(DAG.getMachineFunction().getDataLayout()); |
| 1033 | |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 1034 | auto *MFI = DAG.getMachineFunction().getInfo<WebAssemblyFunctionInfo>(); |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 1035 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 1036 | |
| 1037 | SDValue ArgN = DAG.getCopyFromReg(DAG.getEntryNode(), DL, |
| 1038 | MFI->getVarargBufferVreg(), PtrVT); |
| 1039 | return DAG.getStore(Op.getOperand(0), DL, ArgN, Op.getOperand(1), |
Derek Schuff | 1a946e4 | 2016-07-15 19:35:43 +0000 | [diff] [blame] | 1040 | MachinePointerInfo(SV), 0); |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 1043 | SDValue |
| 1044 | WebAssemblyTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, |
| 1045 | SelectionDAG &DAG) const { |
| 1046 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 1047 | SDLoc DL(Op); |
| 1048 | switch (IntNo) { |
| 1049 | default: |
| 1050 | return {}; // Don't custom lower most intrinsics. |
Thomas Lively | 5d461c9 | 2018-10-03 23:02:23 +0000 | [diff] [blame] | 1051 | |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 1052 | case Intrinsic::wasm_lsda: { |
| 1053 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1054 | EVT VT = Op.getValueType(); |
| 1055 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 1056 | MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); |
| 1057 | auto &Context = MF.getMMI().getContext(); |
| 1058 | MCSymbol *S = Context.getOrCreateSymbol(Twine("GCC_except_table") + |
| 1059 | Twine(MF.getFunctionNumber())); |
| 1060 | return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, |
| 1061 | DAG.getMCSymbol(S, PtrVT)); |
| 1062 | } |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
Thomas Lively | a0d2581 | 2018-09-07 21:54:46 +0000 | [diff] [blame] | 1066 | SDValue |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 1067 | WebAssemblyTargetLowering::LowerINTRINSIC_VOID(SDValue Op, |
| 1068 | SelectionDAG &DAG) const { |
| 1069 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1070 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 1071 | SDLoc DL(Op); |
| 1072 | |
| 1073 | switch (IntNo) { |
| 1074 | default: |
| 1075 | return {}; // Don't custom lower most intrinsics. |
| 1076 | |
| 1077 | case Intrinsic::wasm_throw: { |
| 1078 | int Tag = cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue(); |
| 1079 | switch (Tag) { |
| 1080 | case CPP_EXCEPTION: { |
| 1081 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 1082 | MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); |
| 1083 | const char *SymName = MF.createExternalSymbolName("__cpp_exception"); |
| 1084 | SDValue SymNode = |
| 1085 | DAG.getNode(WebAssemblyISD::Wrapper, DL, PtrVT, |
| 1086 | DAG.getTargetExternalSymbol( |
| 1087 | SymName, PtrVT, WebAssemblyII::MO_SYMBOL_EVENT)); |
| 1088 | return DAG.getNode(WebAssemblyISD::THROW, DL, |
| 1089 | MVT::Other, // outchain type |
| 1090 | { |
| 1091 | Op.getOperand(0), // inchain |
| 1092 | SymNode, // exception symbol |
| 1093 | Op.getOperand(3) // thrown value |
| 1094 | }); |
| 1095 | } |
| 1096 | default: |
| 1097 | llvm_unreachable("Invalid tag!"); |
| 1098 | } |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | SDValue |
Thomas Lively | a0d2581 | 2018-09-07 21:54:46 +0000 | [diff] [blame] | 1105 | WebAssemblyTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, |
| 1106 | SelectionDAG &DAG) const { |
| 1107 | SDLoc DL(Op); |
| 1108 | ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Op.getNode())->getMask(); |
| 1109 | MVT VecType = Op.getOperand(0).getSimpleValueType(); |
| 1110 | assert(VecType.is128BitVector() && "Unexpected shuffle vector type"); |
| 1111 | size_t LaneBytes = VecType.getVectorElementType().getSizeInBits() / 8; |
| 1112 | |
| 1113 | // Space for two vector args and sixteen mask indices |
| 1114 | SDValue Ops[18]; |
| 1115 | size_t OpIdx = 0; |
| 1116 | Ops[OpIdx++] = Op.getOperand(0); |
| 1117 | Ops[OpIdx++] = Op.getOperand(1); |
| 1118 | |
| 1119 | // Expand mask indices to byte indices and materialize them as operands |
| 1120 | for (size_t I = 0, Lanes = Mask.size(); I < Lanes; ++I) { |
| 1121 | for (size_t J = 0; J < LaneBytes; ++J) { |
Thomas Lively | 11a332d0 | 2018-10-19 19:08:06 +0000 | [diff] [blame] | 1122 | // Lower undefs (represented by -1 in mask) to zero |
| 1123 | uint64_t ByteIndex = |
| 1124 | Mask[I] == -1 ? 0 : (uint64_t)Mask[I] * LaneBytes + J; |
| 1125 | Ops[OpIdx++] = DAG.getConstant(ByteIndex, DL, MVT::i32); |
Thomas Lively | a0d2581 | 2018-09-07 21:54:46 +0000 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
Thomas Lively | ed95134 | 2018-10-24 23:27:40 +0000 | [diff] [blame] | 1129 | return DAG.getNode(WebAssemblyISD::SHUFFLE, DL, Op.getValueType(), Ops); |
Thomas Lively | a0d2581 | 2018-09-07 21:54:46 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Thomas Lively | fb84fd7 | 2018-11-02 00:06:56 +0000 | [diff] [blame] | 1132 | SDValue |
| 1133 | WebAssemblyTargetLowering::LowerAccessVectorElement(SDValue Op, |
| 1134 | SelectionDAG &DAG) const { |
| 1135 | // Allow constant lane indices, expand variable lane indices |
| 1136 | SDNode *IdxNode = Op.getOperand(Op.getNumOperands() - 1).getNode(); |
| 1137 | if (isa<ConstantSDNode>(IdxNode) || IdxNode->isUndef()) |
| 1138 | return Op; |
| 1139 | else |
| 1140 | // Perform default expansion |
| 1141 | return SDValue(); |
| 1142 | } |
| 1143 | |
Thomas Lively | 55735d5 | 2018-10-20 01:31:18 +0000 | [diff] [blame] | 1144 | SDValue WebAssemblyTargetLowering::LowerShift(SDValue Op, |
| 1145 | SelectionDAG &DAG) const { |
| 1146 | SDLoc DL(Op); |
Thomas Lively | b2382c8 | 2018-11-02 00:39:57 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Only manually lower vector shifts |
| 1149 | assert(Op.getSimpleValueType().isVector()); |
| 1150 | |
| 1151 | // Unroll non-splat vector shifts |
| 1152 | BuildVectorSDNode *ShiftVec; |
| 1153 | SDValue SplatVal; |
| 1154 | if (!(ShiftVec = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) || |
| 1155 | !(SplatVal = ShiftVec->getSplatValue())) |
| 1156 | return DAG.UnrollVectorOp(Op.getNode()); |
| 1157 | |
| 1158 | // All splats except i64x2 const splats are handled by patterns |
| 1159 | ConstantSDNode *SplatConst = dyn_cast<ConstantSDNode>(SplatVal); |
| 1160 | if (!SplatConst || Op.getSimpleValueType() != MVT::v2i64) |
Thomas Lively | 55735d5 | 2018-10-20 01:31:18 +0000 | [diff] [blame] | 1161 | return Op; |
Thomas Lively | b2382c8 | 2018-11-02 00:39:57 +0000 | [diff] [blame] | 1162 | |
| 1163 | // i64x2 const splats are custom lowered to avoid unnecessary wraps |
Thomas Lively | 55735d5 | 2018-10-20 01:31:18 +0000 | [diff] [blame] | 1164 | unsigned Opcode; |
| 1165 | switch (Op.getOpcode()) { |
| 1166 | case ISD::SHL: |
| 1167 | Opcode = WebAssemblyISD::VEC_SHL; |
| 1168 | break; |
| 1169 | case ISD::SRA: |
| 1170 | Opcode = WebAssemblyISD::VEC_SHR_S; |
| 1171 | break; |
| 1172 | case ISD::SRL: |
| 1173 | Opcode = WebAssemblyISD::VEC_SHR_U; |
| 1174 | break; |
| 1175 | default: |
| 1176 | llvm_unreachable("unexpected opcode"); |
Thomas Lively | 55735d5 | 2018-10-20 01:31:18 +0000 | [diff] [blame] | 1177 | } |
Thomas Lively | b2382c8 | 2018-11-02 00:39:57 +0000 | [diff] [blame] | 1178 | APInt Shift = SplatConst->getAPIntValue().zextOrTrunc(32); |
Thomas Lively | 55735d5 | 2018-10-20 01:31:18 +0000 | [diff] [blame] | 1179 | return DAG.getNode(Opcode, DL, Op.getValueType(), Op.getOperand(0), |
Thomas Lively | b2382c8 | 2018-11-02 00:39:57 +0000 | [diff] [blame] | 1180 | DAG.getConstant(Shift, DL, MVT::i32)); |
Thomas Lively | 55735d5 | 2018-10-20 01:31:18 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1183 | //===----------------------------------------------------------------------===// |
| 1184 | // WebAssembly Optimization Hooks |
| 1185 | //===----------------------------------------------------------------------===// |