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