1// WebAssemblyInstrInfo.td-Describe the WebAssembly Instructions-*- tablegen -*- 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/// WebAssembly Instruction definitions. 11/// 12//===----------------------------------------------------------------------===// 13 14//===----------------------------------------------------------------------===// 15// WebAssembly Instruction Predicate Definitions. 16//===----------------------------------------------------------------------===// 17 18def IsPIC : Predicate<"TM.isPositionIndependent()">; 19def IsNotPIC : Predicate<"!TM.isPositionIndependent()">; 20 21def HasAddr32 : Predicate<"!Subtarget->hasAddr64()">; 22 23def HasAddr64 : Predicate<"Subtarget->hasAddr64()">; 24 25def HasAtomics : 26 Predicate<"Subtarget->hasAtomics()">, 27 AssemblerPredicate<(all_of FeatureAtomics), "atomics">; 28 29def HasBulkMemory : 30 Predicate<"Subtarget->hasBulkMemory()">, 31 AssemblerPredicate<(all_of FeatureBulkMemory), "bulk-memory">; 32 33def HasExceptionHandling : 34 Predicate<"Subtarget->hasExceptionHandling()">, 35 AssemblerPredicate<(all_of FeatureExceptionHandling), "exception-handling">; 36 37def HasExtendedConst : 38 Predicate<"Subtarget->hasExtendedConst()">, 39 AssemblerPredicate<(all_of FeatureExtendedConst), "extended-const">; 40 41def HasHalfPrecision : 42 Predicate<"Subtarget->hasHalfPrecision()">, 43 AssemblerPredicate<(all_of FeatureHalfPrecision), "half-precision">; 44 45def HasMultiMemory : 46 Predicate<"Subtarget->hasMultiMemory()">, 47 AssemblerPredicate<(all_of FeatureMultiMemory), "multimemory">; 48 49def HasMultivalue : 50 Predicate<"Subtarget->hasMultivalue()">, 51 AssemblerPredicate<(all_of FeatureMultivalue), "multivalue">; 52 53def HasMutableGlobals: 54 Predicate<"Subtarget->hasMutableGlobals()">, 55 AssemblerPredicate<(all_of FeatureMutableGlobals), "mutable-globals">; 56 57def HasNontrappingFPToInt : 58 Predicate<"Subtarget->hasNontrappingFPToInt()">, 59 AssemblerPredicate<(all_of FeatureNontrappingFPToInt), 60 "nontrapping-fptoint">; 61 62def NotHasNontrappingFPToInt : 63 Predicate<"!Subtarget->hasNontrappingFPToInt()">, 64 AssemblerPredicate<(all_of (not FeatureNontrappingFPToInt)), 65 "nontrapping-fptoint">; 66 67def HasReferenceTypes : 68 Predicate<"Subtarget->hasReferenceTypes()">, 69 AssemblerPredicate<(all_of FeatureReferenceTypes), "reference-types">; 70 71def HasRelaxedSIMD : 72 Predicate<"Subtarget->hasRelaxedSIMD()">, 73 AssemblerPredicate<(all_of FeatureRelaxedSIMD), "relaxed-simd">; 74 75def HasSignExt : 76 Predicate<"Subtarget->hasSignExt()">, 77 AssemblerPredicate<(all_of FeatureSignExt), "sign-ext">; 78 79def HasSIMD128 : 80 Predicate<"Subtarget->hasSIMD128()">, 81 AssemblerPredicate<(all_of FeatureSIMD128), "simd128">; 82 83def HasTailCall : 84 Predicate<"Subtarget->hasTailCall()">, 85 AssemblerPredicate<(all_of FeatureTailCall), "tail-call">; 86 87//===----------------------------------------------------------------------===// 88// WebAssembly-specific DAG Node Types. 89//===----------------------------------------------------------------------===// 90 91def SDT_WebAssemblyCallSeqStart : SDCallSeqStart<[SDTCisVT<0, iPTR>, 92 SDTCisVT<1, iPTR>]>; 93def SDT_WebAssemblyCallSeqEnd : 94 SDCallSeqEnd<[SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>; 95def SDT_WebAssemblyBrTable : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>; 96def SDT_WebAssemblyArgument : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>; 97def SDT_WebAssemblyLocalGet : SDTypeProfile<1, 1, [SDTCisVT<1, i32>]>; 98def SDT_WebAssemblyLocalSet : SDTypeProfile<0, 2, [SDTCisVT<0, i32>]>; 99def SDT_WebAssemblyReturn : SDTypeProfile<0, -1, []>; 100def SDT_WebAssemblyWrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>, 101 SDTCisPtrTy<0>]>; 102def SDT_WebAssemblyGlobalGet : SDTypeProfile<1, 1, [SDTCisPtrTy<1>]>; 103def SDT_WebAssemblyGlobalSet : SDTypeProfile<0, 2, [SDTCisPtrTy<1>]>; 104 105//===----------------------------------------------------------------------===// 106// WebAssembly-specific DAG Nodes. 107//===----------------------------------------------------------------------===// 108 109def WebAssemblycallseq_start : 110 SDNode<"ISD::CALLSEQ_START", SDT_WebAssemblyCallSeqStart, 111 [SDNPHasChain, SDNPOutGlue]>; 112def WebAssemblycallseq_end : 113 SDNode<"ISD::CALLSEQ_END", SDT_WebAssemblyCallSeqEnd, 114 [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>; 115def WebAssemblybr_table : SDNode<"WebAssemblyISD::BR_TABLE", 116 SDT_WebAssemblyBrTable, 117 [SDNPHasChain, SDNPVariadic]>; 118def WebAssemblyargument : SDNode<"WebAssemblyISD::ARGUMENT", 119 SDT_WebAssemblyArgument>; 120def WebAssemblyreturn : SDNode<"WebAssemblyISD::RETURN", 121 SDT_WebAssemblyReturn, 122 [SDNPHasChain, SDNPVariadic]>; 123def WebAssemblyWrapper : SDNode<"WebAssemblyISD::Wrapper", 124 SDT_WebAssemblyWrapper>; 125def WebAssemblyWrapperREL : SDNode<"WebAssemblyISD::WrapperREL", 126 SDT_WebAssemblyWrapper>; 127def WebAssemblyglobal_get : 128 SDNode<"WebAssemblyISD::GLOBAL_GET", SDT_WebAssemblyGlobalGet, 129 [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>; 130def WebAssemblyglobal_set : 131 SDNode<"WebAssemblyISD::GLOBAL_SET", SDT_WebAssemblyGlobalSet, 132 [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>; 133def WebAssemblylocal_get : 134 SDNode<"WebAssemblyISD::LOCAL_GET", SDT_WebAssemblyLocalGet, 135 [SDNPHasChain, SDNPMayLoad]>; 136def WebAssemblylocal_set : 137 SDNode<"WebAssemblyISD::LOCAL_SET", SDT_WebAssemblyLocalSet, 138 [SDNPHasChain, SDNPMayStore]>; 139 140//===----------------------------------------------------------------------===// 141// WebAssembly-specific Operands. 142//===----------------------------------------------------------------------===// 143 144// Default Operand has AsmOperandClass "Imm" which is for integers (and 145// symbols), so specialize one for floats: 146class FPImmAsmOperand<ValueType ty> : AsmOperandClass { 147 let Name = "FPImm" # ty; 148 let PredicateMethod = "isFPImm"; 149} 150 151class FPOperand<ValueType ty> : Operand<ty> { 152 AsmOperandClass ParserMatchClass = FPImmAsmOperand<ty>; 153} 154 155let OperandNamespace = "WebAssembly" in { 156 157let OperandType = "OPERAND_BASIC_BLOCK" in 158def bb_op : Operand<OtherVT>; 159 160let OperandType = "OPERAND_LOCAL" in 161def local_op : Operand<i32>; 162 163let OperandType = "OPERAND_GLOBAL" in { 164 // The operand to global instructions is always a 32-bit index. 165 def global_op32 : Operand<i32>; 166 // In PIC mode however, we temporarily represent this index as an external 167 // symbol, which to LLVM is a pointer, so in wasm64 mode it is easiest to 168 // pretend we use a 64-bit index for it. 169 def global_op64 : Operand<i64>; 170} 171 172let OperandType = "OPERAND_I32IMM" in 173def i32imm_op : Operand<i32>; 174 175let OperandType = "OPERAND_I64IMM" in 176def i64imm_op : Operand<i64>; 177 178let OperandType = "OPERAND_F32IMM" in 179def f32imm_op : FPOperand<f32>; 180 181let OperandType = "OPERAND_F64IMM" in 182def f64imm_op : FPOperand<f64>; 183 184let OperandType = "OPERAND_VEC_I8IMM" in 185def vec_i8imm_op : Operand<i32>; 186 187let OperandType = "OPERAND_VEC_I16IMM" in 188def vec_i16imm_op : Operand<i32>; 189 190let OperandType = "OPERAND_VEC_I32IMM" in 191def vec_i32imm_op : Operand<i32>; 192 193let OperandType = "OPERAND_VEC_I64IMM" in 194def vec_i64imm_op : Operand<i64>; 195 196let OperandType = "OPERAND_FUNCTION32" in 197def function32_op : Operand<i32>; 198 199let OperandType = "OPERAND_TABLE" in 200def table32_op : Operand<i32>; 201 202let OperandType = "OPERAND_OFFSET32" in 203def offset32_op : Operand<i32>; 204 205let OperandType = "OPERAND_OFFSET64" in 206def offset64_op : Operand<i64>; 207 208let OperandType = "OPERAND_P2ALIGN" in { 209def P2Align : Operand<i32> { 210 let PrintMethod = "printWebAssemblyP2AlignOperand"; 211} 212 213let OperandType = "OPERAND_TAG" in 214def tag_op : Operand<i32>; 215 216} // OperandType = "OPERAND_P2ALIGN" 217 218let OperandType = "OPERAND_SIGNATURE" in 219def Signature : Operand<i32> { 220 let PrintMethod = "printWebAssemblySignatureOperand"; 221} 222 223let OperandType = "OPERAND_TYPEINDEX" in 224def TypeIndex : Operand<i32>; 225 226} // OperandNamespace = "WebAssembly" 227 228// TODO: Find more places to use this. 229def bool_node : PatLeaf<(i32 I32:$cond), [{ 230 return CurDAG->computeKnownBits(SDValue(N, 0)).countMinLeadingZeros() == 31; 231}]>; 232 233//===----------------------------------------------------------------------===// 234// WebAssembly Register to Stack instruction mapping 235//===----------------------------------------------------------------------===// 236 237class StackRel; 238def getStackOpcode : InstrMapping { 239 let FilterClass = "StackRel"; 240 let RowFields = ["BaseName"]; 241 let ColFields = ["StackBased"]; 242 let KeyCol = ["0"]; 243 let ValueCols = [["1"]]; 244} 245 246//===----------------------------------------------------------------------===// 247// WebAssembly Stack to Register instruction mapping 248//===----------------------------------------------------------------------===// 249 250class RegisterRel; 251def getRegisterOpcode : InstrMapping { 252 let FilterClass = "RegisterRel"; 253 let RowFields = ["BaseName"]; 254 let ColFields = ["StackBased"]; 255 let KeyCol = ["1"]; 256 let ValueCols = [["0"]]; 257} 258 259//===----------------------------------------------------------------------===// 260// WebAssembly 32 to 64-bit instruction mapping 261//===----------------------------------------------------------------------===// 262 263class Wasm64Rel; 264def getWasm64Opcode : InstrMapping { 265 let FilterClass = "Wasm64Rel"; 266 let RowFields = ["Wasm32Name"]; 267 let ColFields = ["IsWasm64"]; 268 let KeyCol = ["0"]; 269 let ValueCols = [["1"]]; 270} 271 272//===----------------------------------------------------------------------===// 273// WebAssembly Instruction Format Definitions. 274//===----------------------------------------------------------------------===// 275 276include "WebAssemblyInstrFormats.td" 277 278//===----------------------------------------------------------------------===// 279// Additional instructions. 280//===----------------------------------------------------------------------===// 281 282multiclass ARGUMENT<WebAssemblyRegClass rc, ValueType vt> { 283 let hasSideEffects = 1, isCodeGenOnly = 1, Defs = []<Register>, 284 Uses = [ARGUMENTS] in 285 defm ARGUMENT_#vt : 286 I<(outs rc:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno), 287 [(set (vt rc:$res), (WebAssemblyargument timm:$argno))]>; 288} 289defm "": ARGUMENT<I32, i32>; 290defm "": ARGUMENT<I64, i64>; 291defm "": ARGUMENT<F32, f32>; 292defm "": ARGUMENT<F64, f64>; 293defm "": ARGUMENT<FUNCREF, funcref>; 294defm "": ARGUMENT<EXTERNREF, externref>; 295defm "": ARGUMENT<EXNREF, exnref>; 296 297// local.get and local.set are not generated by instruction selection; they 298// are implied by virtual register uses and defs. 299multiclass LOCAL<WebAssemblyRegClass rc, Operand global_op> { 300 let hasSideEffects = 0 in { 301 // COPY is not an actual instruction in wasm, but since we allow local.get and 302 // local.set to be implicit during most of codegen, we can have a COPY which 303 // is actually a no-op because all the work is done in the implied local.get 304 // and local.set. COPYs are eliminated (and replaced with 305 // local.get/local.set) in the ExplicitLocals pass. 306 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in 307 defm COPY_#rc : I<(outs rc:$res), (ins rc:$src), (outs), (ins), [], 308 "local.copy\t$res, $src", "local.copy">; 309 310 // TEE is similar to COPY, but writes two copies of its result. Typically 311 // this would be used to stackify one result and write the other result to a 312 // local. 313 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in 314 defm TEE_#rc : I<(outs rc:$res, rc:$also), (ins rc:$src), (outs), (ins), [], 315 "local.tee\t$res, $also, $src", "local.tee">; 316 317 // This is the actual local.get instruction in wasm. These are made explicit 318 // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm 319 // local, which is a side effect not otherwise modeled in LLVM. 320 let mayLoad = 1, isAsCheapAsAMove = 1 in 321 defm LOCAL_GET_#rc : I<(outs rc:$res), (ins local_op:$local), 322 (outs), (ins local_op:$local), [], 323 "local.get\t$res, $local", "local.get\t$local", 0x20>; 324 325 // This is the actual local.set instruction in wasm. These are made explicit 326 // by the ExplicitLocals pass. It has mayStore because it writes to a wasm 327 // local, which is a side effect not otherwise modeled in LLVM. 328 let mayStore = 1, isAsCheapAsAMove = 1 in 329 defm LOCAL_SET_#rc : I<(outs), (ins local_op:$local, rc:$src), 330 (outs), (ins local_op:$local), [], 331 "local.set\t$local, $src", "local.set\t$local", 0x21>; 332 333 // This is the actual local.tee instruction in wasm. TEEs are turned into 334 // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason 335 // as LOCAL_SET. 336 let mayStore = 1, isAsCheapAsAMove = 1 in 337 defm LOCAL_TEE_#rc : I<(outs rc:$res), (ins local_op:$local, rc:$src), 338 (outs), (ins local_op:$local), [], 339 "local.tee\t$res, $local, $src", "local.tee\t$local", 340 0x22>; 341 342 // Unused values must be dropped in some contexts. 343 defm DROP_#rc : I<(outs), (ins rc:$src), (outs), (ins), [], 344 "drop\t$src", "drop", 0x1a>; 345 346 let mayLoad = 1 in 347 defm GLOBAL_GET_#rc : I<(outs rc:$res), (ins global_op:$addr), 348 (outs), (ins global_op:$addr), [], 349 "global.get\t$res, $addr", "global.get\t$addr", 350 0x23>; 351 352 let mayStore = 1 in 353 defm GLOBAL_SET_#rc : I<(outs), (ins global_op:$addr, rc:$src), 354 (outs), (ins global_op:$addr), [], 355 "global.set\t$addr, $src", "global.set\t$addr", 356 0x24>; 357 358 } // hasSideEffects = 0 359 foreach vt = rc.RegTypes in { 360 def : Pat<(vt (WebAssemblyglobal_get 361 (WebAssemblyWrapper tglobaladdr:$addr))), 362 (!cast<NI>("GLOBAL_GET_" # rc) tglobaladdr:$addr)>; 363 def : Pat<(WebAssemblyglobal_set 364 vt:$src, (WebAssemblyWrapper tglobaladdr:$addr)), 365 (!cast<NI>("GLOBAL_SET_" # rc) tglobaladdr:$addr, vt:$src)>; 366 def : Pat<(vt (WebAssemblylocal_get (i32 timm:$local))), 367 (!cast<NI>("LOCAL_GET_" # rc) timm:$local)>; 368 def : Pat<(WebAssemblylocal_set timm:$local, vt:$src), 369 (!cast<NI>("LOCAL_SET_" # rc) timm:$local, vt:$src)>; 370 } 371} 372defm "" : LOCAL<I32, global_op32>; 373defm "" : LOCAL<I64, global_op64>; // 64-bit only needed for pointers. 374defm "" : LOCAL<F32, global_op32>; 375defm "" : LOCAL<F64, global_op32>; 376defm "" : LOCAL<V128, global_op32>, Requires<[HasSIMD128]>; 377defm "" : LOCAL<FUNCREF, global_op32>, Requires<[HasReferenceTypes]>; 378defm "" : LOCAL<EXTERNREF, global_op32>, Requires<[HasReferenceTypes]>; 379defm "" : LOCAL<EXNREF, global_op32>, 380 Requires<[HasReferenceTypes, HasExceptionHandling]>; 381 382let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in { 383defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm), 384 (outs), (ins i32imm_op:$imm), 385 [(set I32:$res, imm:$imm)], 386 "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>; 387defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm), 388 (outs), (ins i64imm_op:$imm), 389 [(set I64:$res, imm:$imm)], 390 "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>; 391defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm), 392 (outs), (ins f32imm_op:$imm), 393 [(set F32:$res, fpimm:$imm)], 394 "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>; 395defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm), 396 (outs), (ins f64imm_op:$imm), 397 [(set F64:$res, fpimm:$imm)], 398 "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>; 399} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 400 401def : Pat<(i32 (WebAssemblyWrapper tglobaladdr:$addr)), 402 (CONST_I32 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr32]>; 403def : Pat<(i64 (WebAssemblyWrapper tglobaladdr:$addr)), 404 (CONST_I64 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr64]>; 405 406def : Pat<(i32 (WebAssemblyWrapper tglobaladdr:$addr)), 407 (GLOBAL_GET_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>; 408def : Pat<(i64 (WebAssemblyWrapper tglobaladdr:$addr)), 409 (GLOBAL_GET_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>; 410 411def : Pat<(i32 (WebAssemblyWrapperREL tglobaladdr:$addr)), 412 (CONST_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>; 413def : Pat<(i64 (WebAssemblyWrapperREL tglobaladdr:$addr)), 414 (CONST_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>; 415 416def : Pat<(i32 (WebAssemblyWrapperREL tglobaltlsaddr:$addr)), 417 (CONST_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>; 418def : Pat<(i64 (WebAssemblyWrapperREL tglobaltlsaddr:$addr)), 419 (CONST_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>; 420 421def : Pat<(i32 (WebAssemblyWrapper tglobaltlsaddr:$addr)), 422 (GLOBAL_GET_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>; 423def : Pat<(i64 (WebAssemblyWrapper tglobaltlsaddr:$addr)), 424 (GLOBAL_GET_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>; 425 426def : Pat<(i32 (WebAssemblyWrapper texternalsym:$addr)), 427 (GLOBAL_GET_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>; 428def : Pat<(i64 (WebAssemblyWrapper texternalsym:$addr)), 429 (GLOBAL_GET_I64 texternalsym:$addr)>, Requires<[IsPIC, HasAddr64]>; 430 431def : Pat<(i32 (WebAssemblyWrapper texternalsym:$addr)), 432 (CONST_I32 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr32]>; 433def : Pat<(i64 (WebAssemblyWrapper texternalsym:$addr)), 434 (CONST_I64 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr64]>; 435 436def : Pat<(i32 (WebAssemblyWrapperREL texternalsym:$addr)), 437 (CONST_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>; 438def : Pat<(i64 (WebAssemblyWrapperREL texternalsym:$addr)), 439 (CONST_I64 texternalsym:$addr)>, Requires<[IsPIC, HasAddr64]>; 440 441//===----------------------------------------------------------------------===// 442// Additional sets of instructions. 443//===----------------------------------------------------------------------===// 444 445include "WebAssemblyInstrMemory.td" 446include "WebAssemblyInstrCall.td" 447include "WebAssemblyInstrControl.td" 448include "WebAssemblyInstrInteger.td" 449include "WebAssemblyInstrConv.td" 450include "WebAssemblyInstrFloat.td" 451include "WebAssemblyInstrAtomics.td" 452include "WebAssemblyInstrSIMD.td" 453include "WebAssemblyInstrRef.td" 454include "WebAssemblyInstrBulkMemory.td" 455include "WebAssemblyInstrTable.td" 456