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