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