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