10b57cec5SDimitry Andric//===- WebAssemblyInstrControl.td-WebAssembly control-flow ------*- tablegen -*- 20b57cec5SDimitry Andric// 30b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric// 70b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric/// 90b57cec5SDimitry Andric/// \file 100b57cec5SDimitry Andric/// WebAssembly control-flow code-gen constructs. 110b57cec5SDimitry Andric/// 120b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andriclet isBranch = 1, isTerminator = 1, hasCtrlDep = 1 in { 150b57cec5SDimitry Andric// The condition operand is a boolean value which WebAssembly represents as i32. 160b57cec5SDimitry Andricdefm BR_IF : I<(outs), (ins bb_op:$dst, I32:$cond), 170b57cec5SDimitry Andric (outs), (ins bb_op:$dst), 180b57cec5SDimitry Andric [(brcond I32:$cond, bb:$dst)], 190b57cec5SDimitry Andric "br_if \t$dst, $cond", "br_if \t$dst", 0x0d>; 200b57cec5SDimitry Andriclet isCodeGenOnly = 1 in 210b57cec5SDimitry Andricdefm BR_UNLESS : I<(outs), (ins bb_op:$dst, I32:$cond), 220b57cec5SDimitry Andric (outs), (ins bb_op:$dst), []>; 230b57cec5SDimitry Andriclet isBarrier = 1 in 240b57cec5SDimitry Andricdefm BR : NRI<(outs), (ins bb_op:$dst), 250b57cec5SDimitry Andric [(br bb:$dst)], 260b57cec5SDimitry Andric "br \t$dst", 0x0c>; 270b57cec5SDimitry Andric} // isBranch = 1, isTerminator = 1, hasCtrlDep = 1 280b57cec5SDimitry Andric 290b57cec5SDimitry Andricdef : Pat<(brcond (i32 (setne I32:$cond, 0)), bb:$dst), 300b57cec5SDimitry Andric (BR_IF bb_op:$dst, I32:$cond)>; 310b57cec5SDimitry Andricdef : Pat<(brcond (i32 (seteq I32:$cond, 0)), bb:$dst), 320b57cec5SDimitry Andric (BR_UNLESS bb_op:$dst, I32:$cond)>; 33fe6060f1SDimitry Andricdef : Pat<(brcond (i32 (xor bool_node:$cond, (i32 1))), bb:$dst), 34fe6060f1SDimitry Andric (BR_UNLESS bb_op:$dst, I32:$cond)>; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric// A list of branch targets enclosed in {} and separated by comma. 370b57cec5SDimitry Andric// Used by br_table only. 380b57cec5SDimitry Andricdef BrListAsmOperand : AsmOperandClass { let Name = "BrList"; } 390b57cec5SDimitry Andriclet OperandNamespace = "WebAssembly", OperandType = "OPERAND_BRLIST" in 400b57cec5SDimitry Andricdef brlist : Operand<i32> { 410b57cec5SDimitry Andric let ParserMatchClass = BrListAsmOperand; 420b57cec5SDimitry Andric let PrintMethod = "printBrList"; 430b57cec5SDimitry Andric} 440b57cec5SDimitry Andric 455ffd83dbSDimitry Andric// Duplicating a BR_TABLE is almost never a good idea. In particular, it can 465ffd83dbSDimitry Andric// lead to some nasty irreducibility due to tail merging when the br_table is in 475ffd83dbSDimitry Andric// a loop. 485ffd83dbSDimitry Andriclet isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1 in { 495ffd83dbSDimitry Andric 500b57cec5SDimitry Andricdefm BR_TABLE_I32 : I<(outs), (ins I32:$index, variable_ops), 510b57cec5SDimitry Andric (outs), (ins brlist:$brl), 520b57cec5SDimitry Andric [(WebAssemblybr_table I32:$index)], 530b57cec5SDimitry Andric "br_table \t$index", "br_table \t$brl", 540b57cec5SDimitry Andric 0x0e>; 555ffd83dbSDimitry Andric// TODO: SelectionDAG's lowering insists on using a pointer as the index for 565ffd83dbSDimitry Andric// jump tables, so in practice we don't ever use BR_TABLE_I64 in wasm32 mode 575ffd83dbSDimitry Andric// currently. 580b57cec5SDimitry Andricdefm BR_TABLE_I64 : I<(outs), (ins I64:$index, variable_ops), 590b57cec5SDimitry Andric (outs), (ins brlist:$brl), 600b57cec5SDimitry Andric [(WebAssemblybr_table I64:$index)], 610b57cec5SDimitry Andric "br_table \t$index", "br_table \t$brl", 620b57cec5SDimitry Andric 0x0e>; 635ffd83dbSDimitry Andric} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1, isNotDuplicable = 1 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric// This is technically a control-flow instruction, since all it affects is the 660b57cec5SDimitry Andric// IP. 670b57cec5SDimitry Andricdefm NOP : NRI<(outs), (ins), [], "nop", 0x01>; 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric// Placemarkers to indicate the start or end of a block or loop scope. 700b57cec5SDimitry Andric// These use/clobber VALUE_STACK to prevent them from being moved into the 710b57cec5SDimitry Andric// middle of an expression tree. 720b57cec5SDimitry Andriclet Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { 730b57cec5SDimitry Andricdefm BLOCK : NRI<(outs), (ins Signature:$sig), [], "block \t$sig", 0x02>; 740b57cec5SDimitry Andricdefm LOOP : NRI<(outs), (ins Signature:$sig), [], "loop \t$sig", 0x03>; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andricdefm IF : I<(outs), (ins Signature:$sig, I32:$cond), 770b57cec5SDimitry Andric (outs), (ins Signature:$sig), 780b57cec5SDimitry Andric [], "if \t$sig, $cond", "if \t$sig", 0x04>; 790b57cec5SDimitry Andricdefm ELSE : NRI<(outs), (ins), [], "else", 0x05>; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric// END_BLOCK, END_LOOP, END_IF and END_FUNCTION are represented with the same 820b57cec5SDimitry Andric// opcode in wasm. 830b57cec5SDimitry Andricdefm END_BLOCK : NRI<(outs), (ins), [], "end_block", 0x0b>; 840b57cec5SDimitry Andricdefm END_LOOP : NRI<(outs), (ins), [], "end_loop", 0x0b>; 850b57cec5SDimitry Andricdefm END_IF : NRI<(outs), (ins), [], "end_if", 0x0b>; 860b57cec5SDimitry Andric// Generic instruction, for disassembler. 870b57cec5SDimitry Andriclet IsCanonical = 1 in 880b57cec5SDimitry Andricdefm END : NRI<(outs), (ins), [], "end", 0x0b>; 890b57cec5SDimitry Andriclet isTerminator = 1, isBarrier = 1 in 900b57cec5SDimitry Andricdefm END_FUNCTION : NRI<(outs), (ins), [], "end_function", 0x0b>; 910b57cec5SDimitry Andric} // Uses = [VALUE_STACK], Defs = [VALUE_STACK] 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric 945ffd83dbSDimitry Andriclet hasCtrlDep = 1, isBarrier = 1 in { 955ffd83dbSDimitry Andriclet isTerminator = 1 in { 960b57cec5SDimitry Andriclet isReturn = 1 in { 970b57cec5SDimitry Andric 988bcb0991SDimitry Andricdefm RETURN : I<(outs), (ins variable_ops), (outs), (ins), 998bcb0991SDimitry Andric [(WebAssemblyreturn)], 1008bcb0991SDimitry Andric "return", "return", 0x0f>; 1018bcb0991SDimitry Andric// Equivalent to RETURN, for use at the end of a function when wasm 1028bcb0991SDimitry Andric// semantics return by falling off the end of the block. 1030b57cec5SDimitry Andriclet isCodeGenOnly = 1 in 1048bcb0991SDimitry Andricdefm FALLTHROUGH_RETURN : I<(outs), (ins variable_ops), (outs), (ins), []>; 1058bcb0991SDimitry Andric 1060b57cec5SDimitry Andric} // isReturn = 1 1070b57cec5SDimitry Andric 108e8d8bef9SDimitry Andriclet IsCanonical = 1, isTrap = 1 in 1090b57cec5SDimitry Andricdefm UNREACHABLE : NRI<(outs), (ins), [(trap)], "unreachable", 0x00>; 1105ffd83dbSDimitry Andric 1115ffd83dbSDimitry Andric} // isTerminator = 1 1125ffd83dbSDimitry Andric 1135ffd83dbSDimitry Andric// debugtrap explicitly returns despite trapping because it is supposed to just 1145ffd83dbSDimitry Andric// get the attention of the debugger. Unfortunately, because UNREACHABLE is a 1155ffd83dbSDimitry Andric// terminator, lowering debugtrap to UNREACHABLE can create an invalid 1165ffd83dbSDimitry Andric// MachineBasicBlock when there is additional code after it. Lower it to this 1175ffd83dbSDimitry Andric// non-terminator version instead. 1185ffd83dbSDimitry Andric// TODO: Actually execute the debugger statement when running on the Web 1195ffd83dbSDimitry Andriclet isTrap = 1 in 1205ffd83dbSDimitry Andricdefm DEBUG_UNREACHABLE : NRI<(outs), (ins), [(debugtrap)], "unreachable", 0x00>; 1215ffd83dbSDimitry Andric 1225ffd83dbSDimitry Andric} // hasCtrlDep = 1, isBarrier = 1 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 1250b57cec5SDimitry Andric// Exception handling instructions 1260b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andriclet Predicates = [HasExceptionHandling] in { 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric// Throwing an exception: throw / rethrow 1310b57cec5SDimitry Andriclet isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in { 132fe6060f1SDimitry Andricdefm THROW : I<(outs), (ins tag_op:$tag, variable_ops), 133349cc55cSDimitry Andric (outs), (ins tag_op:$tag), [], 1340b57cec5SDimitry Andric "throw \t$tag", "throw \t$tag", 0x08>; 135*415efcecSDimitry Andric// $ehpad is the EH pad where the exception to rethrow has been caught. 136*415efcecSDimitry Andricdefm RETHROW : NRI<(outs), (ins bb_op:$ehpad), [], "rethrow \t$ehpad", 0x09>; 1370b57cec5SDimitry Andric} // isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric// Region within which an exception is caught: try / end_try 1400b57cec5SDimitry Andriclet Uses = [VALUE_STACK], Defs = [VALUE_STACK] in { 1410b57cec5SDimitry Andricdefm TRY : NRI<(outs), (ins Signature:$sig), [], "try \t$sig", 0x06>; 1420b57cec5SDimitry Andricdefm END_TRY : NRI<(outs), (ins), [], "end_try", 0x0b>; 1430b57cec5SDimitry Andric} // Uses = [VALUE_STACK], Defs = [VALUE_STACK] 1440b57cec5SDimitry Andric 145e8d8bef9SDimitry Andric// Catching an exception: catch / catch_all 146e8d8bef9SDimitry Andriclet hasCtrlDep = 1, hasSideEffects = 1 in { 147349cc55cSDimitry Andriclet variadicOpsAreDefs = 1 in 148349cc55cSDimitry Andricdefm CATCH : I<(outs), (ins tag_op:$tag, variable_ops), 149349cc55cSDimitry Andric (outs), (ins tag_op:$tag), [], 150349cc55cSDimitry Andric "catch", "catch \t$tag", 0x07>; 151fe6060f1SDimitry Andricdefm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x19>; 152e8d8bef9SDimitry Andric} 1530b57cec5SDimitry Andric 154fe6060f1SDimitry Andric// Delegating an exception: delegate 155fe6060f1SDimitry Andriclet isTerminator = 1, hasCtrlDep = 1, hasSideEffects = 1 in 156fe6060f1SDimitry Andricdefm DELEGATE : NRI<(outs), (ins bb_op:$dst), [], "delegate \t $dst", 0x18>; 157fe6060f1SDimitry Andric 1580b57cec5SDimitry Andric// Pseudo instructions: cleanupret / catchret 1590b57cec5SDimitry Andriclet isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 1600b57cec5SDimitry Andric isPseudo = 1, isEHScopeReturn = 1 in { 161*415efcecSDimitry Andric defm CLEANUPRET : NRI<(outs), (ins bb_op:$ehpad), [(cleanupret bb:$ehpad)], 162*415efcecSDimitry Andric "cleanupret", 0>; 1630b57cec5SDimitry Andric defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from), 1640b57cec5SDimitry Andric [(catchret bb:$dst, bb:$from)], "catchret", 0>; 1650b57cec5SDimitry Andric} // isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, 1660b57cec5SDimitry Andric // isPseudo = 1, isEHScopeReturn = 1 1670b57cec5SDimitry Andric} // Predicates = [HasExceptionHandling] 168