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_TYPEINDEX" in 206def TypeIndex : Operand<i32>; 207 208} // OperandNamespace = "WebAssembly" 209 210// TODO: Find more places to use this. 211def bool_node : PatLeaf<(i32 I32:$cond), [{ 212 return CurDAG->computeKnownBits(SDValue(N, 0)).countMinLeadingZeros() == 31; 213}]>; 214 215//===----------------------------------------------------------------------===// 216// WebAssembly Register to Stack instruction mapping 217//===----------------------------------------------------------------------===// 218 219class StackRel; 220def getStackOpcode : InstrMapping { 221 let FilterClass = "StackRel"; 222 let RowFields = ["BaseName"]; 223 let ColFields = ["StackBased"]; 224 let KeyCol = ["false"]; 225 let ValueCols = [["true"]]; 226} 227 228//===----------------------------------------------------------------------===// 229// WebAssembly Stack to Register instruction mapping 230//===----------------------------------------------------------------------===// 231 232class RegisterRel; 233def getRegisterOpcode : InstrMapping { 234 let FilterClass = "RegisterRel"; 235 let RowFields = ["BaseName"]; 236 let ColFields = ["StackBased"]; 237 let KeyCol = ["true"]; 238 let ValueCols = [["false"]]; 239} 240 241//===----------------------------------------------------------------------===// 242// WebAssembly 32 to 64-bit instruction mapping 243//===----------------------------------------------------------------------===// 244 245class Wasm64Rel; 246def getWasm64Opcode : InstrMapping { 247 let FilterClass = "Wasm64Rel"; 248 let RowFields = ["Wasm32Name"]; 249 let ColFields = ["IsWasm64"]; 250 let KeyCol = ["false"]; 251 let ValueCols = [["true"]]; 252} 253 254//===----------------------------------------------------------------------===// 255// WebAssembly Instruction Format Definitions. 256//===----------------------------------------------------------------------===// 257 258include "WebAssemblyInstrFormats.td" 259 260//===----------------------------------------------------------------------===// 261// Additional instructions. 262//===----------------------------------------------------------------------===// 263 264multiclass ARGUMENT<WebAssemblyRegClass rc, ValueType vt> { 265 let hasSideEffects = 1, isCodeGenOnly = 1, Defs = []<Register>, 266 Uses = [ARGUMENTS] in 267 defm ARGUMENT_#vt : 268 I<(outs rc:$res), (ins i32imm:$argno), (outs), (ins i32imm:$argno), 269 [(set (vt rc:$res), (WebAssemblyargument timm:$argno))]>; 270} 271defm "": ARGUMENT<I32, i32>; 272defm "": ARGUMENT<I64, i64>; 273defm "": ARGUMENT<F32, f32>; 274defm "": ARGUMENT<F64, f64>; 275defm "": ARGUMENT<FUNCREF, funcref>; 276defm "": ARGUMENT<EXTERNREF, externref>; 277 278// local.get and local.set are not generated by instruction selection; they 279// are implied by virtual register uses and defs. 280multiclass LOCAL<WebAssemblyRegClass rc, Operand global_op> { 281 let hasSideEffects = 0 in { 282 // COPY is not an actual instruction in wasm, but since we allow local.get and 283 // local.set to be implicit during most of codegen, we can have a COPY which 284 // is actually a no-op because all the work is done in the implied local.get 285 // and local.set. COPYs are eliminated (and replaced with 286 // local.get/local.set) in the ExplicitLocals pass. 287 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in 288 defm COPY_#rc : I<(outs rc:$res), (ins rc:$src), (outs), (ins), [], 289 "local.copy\t$res, $src", "local.copy">; 290 291 // TEE is similar to COPY, but writes two copies of its result. Typically 292 // this would be used to stackify one result and write the other result to a 293 // local. 294 let isAsCheapAsAMove = 1, isCodeGenOnly = 1 in 295 defm TEE_#rc : I<(outs rc:$res, rc:$also), (ins rc:$src), (outs), (ins), [], 296 "local.tee\t$res, $also, $src", "local.tee">; 297 298 // This is the actual local.get instruction in wasm. These are made explicit 299 // by the ExplicitLocals pass. It has mayLoad because it reads from a wasm 300 // local, which is a side effect not otherwise modeled in LLVM. 301 let mayLoad = 1, isAsCheapAsAMove = 1 in 302 defm LOCAL_GET_#rc : I<(outs rc:$res), (ins local_op:$local), 303 (outs), (ins local_op:$local), [], 304 "local.get\t$res, $local", "local.get\t$local", 0x20>; 305 306 // This is the actual local.set instruction in wasm. These are made explicit 307 // by the ExplicitLocals pass. It has mayStore because it writes to a wasm 308 // local, which is a side effect not otherwise modeled in LLVM. 309 let mayStore = 1, isAsCheapAsAMove = 1 in 310 defm LOCAL_SET_#rc : I<(outs), (ins local_op:$local, rc:$src), 311 (outs), (ins local_op:$local), [], 312 "local.set\t$local, $src", "local.set\t$local", 0x21>; 313 314 // This is the actual local.tee instruction in wasm. TEEs are turned into 315 // LOCAL_TEEs by the ExplicitLocals pass. It has mayStore for the same reason 316 // as LOCAL_SET. 317 let mayStore = 1, isAsCheapAsAMove = 1 in 318 defm LOCAL_TEE_#rc : I<(outs rc:$res), (ins local_op:$local, rc:$src), 319 (outs), (ins local_op:$local), [], 320 "local.tee\t$res, $local, $src", "local.tee\t$local", 321 0x22>; 322 323 // Unused values must be dropped in some contexts. 324 defm DROP_#rc : I<(outs), (ins rc:$src), (outs), (ins), [], 325 "drop\t$src", "drop", 0x1a>; 326 327 let mayLoad = 1 in 328 defm GLOBAL_GET_#rc : I<(outs rc:$res), (ins global_op:$addr), 329 (outs), (ins global_op:$addr), [], 330 "global.get\t$res, $addr", "global.get\t$addr", 331 0x23>; 332 333 let mayStore = 1 in 334 defm GLOBAL_SET_#rc : I<(outs), (ins global_op:$addr, rc:$src), 335 (outs), (ins global_op:$addr), [], 336 "global.set\t$addr, $src", "global.set\t$addr", 337 0x24>; 338 339 } // hasSideEffects = 0 340 foreach vt = rc.RegTypes in { 341 def : Pat<(vt (WebAssemblyglobal_get 342 (WebAssemblyWrapper tglobaladdr:$addr))), 343 (!cast<NI>("GLOBAL_GET_" # rc) tglobaladdr:$addr)>; 344 def : Pat<(WebAssemblyglobal_set 345 vt:$src, (WebAssemblyWrapper tglobaladdr:$addr)), 346 (!cast<NI>("GLOBAL_SET_" # rc) tglobaladdr:$addr, vt:$src)>; 347 def : Pat<(vt (WebAssemblylocal_get (i32 timm:$local))), 348 (!cast<NI>("LOCAL_GET_" # rc) timm:$local)>; 349 def : Pat<(WebAssemblylocal_set timm:$local, vt:$src), 350 (!cast<NI>("LOCAL_SET_" # rc) timm:$local, vt:$src)>; 351 } 352} 353defm "" : LOCAL<I32, global_op32>; 354defm "" : LOCAL<I64, global_op64>; // 64-bit only needed for pointers. 355defm "" : LOCAL<F32, global_op32>; 356defm "" : LOCAL<F64, global_op32>; 357defm "" : LOCAL<V128, global_op32>, Requires<[HasSIMD128]>; 358defm "" : LOCAL<FUNCREF, global_op32>, Requires<[HasReferenceTypes]>; 359defm "" : LOCAL<EXTERNREF, global_op32>, Requires<[HasReferenceTypes]>; 360 361let isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 in { 362defm CONST_I32 : I<(outs I32:$res), (ins i32imm_op:$imm), 363 (outs), (ins i32imm_op:$imm), 364 [(set I32:$res, imm:$imm)], 365 "i32.const\t$res, $imm", "i32.const\t$imm", 0x41>; 366defm CONST_I64 : I<(outs I64:$res), (ins i64imm_op:$imm), 367 (outs), (ins i64imm_op:$imm), 368 [(set I64:$res, imm:$imm)], 369 "i64.const\t$res, $imm", "i64.const\t$imm", 0x42>; 370defm CONST_F32 : I<(outs F32:$res), (ins f32imm_op:$imm), 371 (outs), (ins f32imm_op:$imm), 372 [(set F32:$res, fpimm:$imm)], 373 "f32.const\t$res, $imm", "f32.const\t$imm", 0x43>; 374defm CONST_F64 : I<(outs F64:$res), (ins f64imm_op:$imm), 375 (outs), (ins f64imm_op:$imm), 376 [(set F64:$res, fpimm:$imm)], 377 "f64.const\t$res, $imm", "f64.const\t$imm", 0x44>; 378} // isMoveImm = 1, isAsCheapAsAMove = 1, isReMaterializable = 1 379 380def : Pat<(i32 (WebAssemblyWrapper tglobaladdr:$addr)), 381 (CONST_I32 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr32]>; 382def : Pat<(i64 (WebAssemblyWrapper tglobaladdr:$addr)), 383 (CONST_I64 tglobaladdr:$addr)>, Requires<[IsNotPIC, HasAddr64]>; 384 385def : Pat<(i32 (WebAssemblyWrapper tglobaladdr:$addr)), 386 (GLOBAL_GET_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>; 387def : Pat<(i64 (WebAssemblyWrapper tglobaladdr:$addr)), 388 (GLOBAL_GET_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>; 389 390def : Pat<(i32 (WebAssemblyWrapperREL tglobaladdr:$addr)), 391 (CONST_I32 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr32]>; 392def : Pat<(i64 (WebAssemblyWrapperREL tglobaladdr:$addr)), 393 (CONST_I64 tglobaladdr:$addr)>, Requires<[IsPIC, HasAddr64]>; 394 395def : Pat<(i32 (WebAssemblyWrapperREL tglobaltlsaddr:$addr)), 396 (CONST_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>; 397def : Pat<(i64 (WebAssemblyWrapperREL tglobaltlsaddr:$addr)), 398 (CONST_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>; 399 400def : Pat<(i32 (WebAssemblyWrapper tglobaltlsaddr:$addr)), 401 (GLOBAL_GET_I32 tglobaltlsaddr:$addr)>, Requires<[HasAddr32]>; 402def : Pat<(i64 (WebAssemblyWrapper tglobaltlsaddr:$addr)), 403 (GLOBAL_GET_I64 tglobaltlsaddr:$addr)>, Requires<[HasAddr64]>; 404 405def : Pat<(i32 (WebAssemblyWrapper texternalsym:$addr)), 406 (GLOBAL_GET_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>; 407def : Pat<(i64 (WebAssemblyWrapper texternalsym:$addr)), 408 (GLOBAL_GET_I64 texternalsym:$addr)>, Requires<[IsPIC, HasAddr64]>; 409 410def : Pat<(i32 (WebAssemblyWrapper texternalsym:$addr)), 411 (CONST_I32 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr32]>; 412def : Pat<(i64 (WebAssemblyWrapper texternalsym:$addr)), 413 (CONST_I64 texternalsym:$addr)>, Requires<[IsNotPIC, HasAddr64]>; 414 415def : Pat<(i32 (WebAssemblyWrapperREL texternalsym:$addr)), 416 (CONST_I32 texternalsym:$addr)>, Requires<[IsPIC, HasAddr32]>; 417def : Pat<(i64 (WebAssemblyWrapperREL texternalsym:$addr)), 418 (CONST_I64 texternalsym:$addr)>, Requires<[IsPIC, HasAddr64]>; 419 420//===----------------------------------------------------------------------===// 421// Additional sets of instructions. 422//===----------------------------------------------------------------------===// 423 424include "WebAssemblyInstrMemory.td" 425include "WebAssemblyInstrCall.td" 426include "WebAssemblyInstrControl.td" 427include "WebAssemblyInstrInteger.td" 428include "WebAssemblyInstrConv.td" 429include "WebAssemblyInstrFloat.td" 430include "WebAssemblyInstrAtomics.td" 431include "WebAssemblyInstrSIMD.td" 432include "WebAssemblyInstrRef.td" 433include "WebAssemblyInstrBulkMemory.td" 434include "WebAssemblyInstrTable.td" 435