WebAssemblyISelLowering.cpp 56 KB

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