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