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