diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h index ace33efd8713..ec2f270fcb3f 100644 --- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h +++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h @@ -569,6 +569,12 @@ public: return !empty() && back().isReturn(); } + /// Convenience function that returns true if the bock ends in a EH scope + /// return instruction. + bool isEHScopeReturnBlock() const { + return !empty() && back().isEHScopeReturn(); + } + /// Split the critical edge from this block to the given successor block, and /// return the newly created block, or null if splitting is not possible. /// diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h b/llvm/include/llvm/CodeGen/MachineInstr.h index 8a7d09aaf8bd..a1ebc22cd3b5 100644 --- a/llvm/include/llvm/CodeGen/MachineInstr.h +++ b/llvm/include/llvm/CodeGen/MachineInstr.h @@ -616,6 +616,12 @@ public: return hasProperty(MCID::Return, Type); } + /// Return true if this is an instruction that marks the end of an EH scope, + /// i.e., a catchpad or a cleanuppad instruction. + bool isEHScopeReturn(QueryType Type = AnyInBundle) const { + return hasProperty(MCID::EHScopeReturn, Type); + } + bool isCall(QueryType Type = AnyInBundle) const { return hasProperty(MCID::Call, Type); } diff --git a/llvm/include/llvm/MC/MCInstrDesc.h b/llvm/include/llvm/MC/MCInstrDesc.h index 3e000a2210e9..ba97aeb837be 100644 --- a/llvm/include/llvm/MC/MCInstrDesc.h +++ b/llvm/include/llvm/MC/MCInstrDesc.h @@ -120,6 +120,7 @@ enum Flag { HasOptionalDef, Pseudo, Return, + EHScopeReturn, Call, Barrier, Terminator, diff --git a/llvm/include/llvm/Target/Target.td b/llvm/include/llvm/Target/Target.td index b746505d2a45..538605a57ab4 100644 --- a/llvm/include/llvm/Target/Target.td +++ b/llvm/include/llvm/Target/Target.td @@ -439,6 +439,7 @@ class Instruction { // instruction. bit isReturn = 0; // Is this instruction a return instruction? bit isBranch = 0; // Is this instruction a branch instruction? + bit isEHScopeReturn = 0; // Does this instruction end an EH scope? bit isIndirectBranch = 0; // Is this instruction an indirect branch? bit isCompare = 0; // Is this instruction a comparison instruction? bit isMoveImm = 0; // Is this instruction a move immediate instruction? diff --git a/llvm/lib/CodeGen/Analysis.cpp b/llvm/lib/CodeGen/Analysis.cpp index 79f11def38f7..7c1fb6879a60 100644 --- a/llvm/lib/CodeGen/Analysis.cpp +++ b/llvm/lib/CodeGen/Analysis.cpp @@ -650,7 +650,7 @@ static void collectEHScopeMembers( // Returns are boundaries where scope transfer can occur, don't follow // successors. - if (Visiting->isReturnBlock()) + if (Visiting->isEHScopeReturnBlock()) continue; for (const MachineBasicBlock *Succ : Visiting->successors()) diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td index cd2336393cc6..a987906e0878 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td +++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrControl.td @@ -185,10 +185,8 @@ defm CATCH_ALL : NRI<(outs), (ins), [], "catch_all", 0x05>; } // Pseudo instructions: cleanupret / catchret -// They are not return instructions in wasm, but setting 'isReturn' to true as -// in X86 is necessary for computing EH scope membership. let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, - isCodeGenOnly = 1, isReturn = 1 in { + isCodeGenOnly = 1, isEHScopeReturn = 1 in { defm CLEANUPRET : NRI<(outs), (ins), [(cleanupret)], "", 0>; defm CATCHRET : NRI<(outs), (ins bb_op:$dst, bb_op:$from), [(catchret bb:$dst, bb:$from)], "", 0>; diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td index fb5e45731236..7c01714b6a03 100644 --- a/llvm/lib/Target/X86/X86InstrCompiler.td +++ b/llvm/lib/Target/X86/X86InstrCompiler.td @@ -178,7 +178,7 @@ def EH_RETURN64 : I<0xC3, RawFrm, (outs), (ins GR64:$addr), } let isTerminator = 1, hasSideEffects = 1, isBarrier = 1, hasCtrlDep = 1, - isCodeGenOnly = 1, isReturn = 1 in { + isCodeGenOnly = 1, isReturn = 1, isEHScopeReturn = 1 in { def CLEANUPRET : I<0, Pseudo, (outs), (ins), "# CLEANUPRET", [(cleanupret)]>; // CATCHRET needs a custom inserter for SEH. diff --git a/llvm/test/CodeGen/WebAssembly/exception.ll b/llvm/test/CodeGen/WebAssembly/exception.ll index 114311c360b6..c2ad8d7476ca 100644 --- a/llvm/test/CodeGen/WebAssembly/exception.ll +++ b/llvm/test/CodeGen/WebAssembly/exception.ll @@ -150,7 +150,71 @@ terminate10: ; preds = %ehcleanup7 unreachable } +; Tests prologues and epilogues are not generated within EH scopes. +; They should not be treated as funclets; BBs starting with a catch instruction +; should not have a prologue, and BBs ending with a catchret/cleanupret should +; not have an epilogue. This is separate from __stack_pointer restoring +; instructions after a catch instruction. + +; CHECK-LABEL: test_no_prolog_epilog_in_ehpad +; CHECK: try +; CHECK: call foo@FUNCTION +; CHECK: i32.catch +; CHECK-NOT: get_global $push{{.+}}=, __stack_pointer@GLOBAL +; CHECK: try +; CHECK: call foo@FUNCTION +; CHECK: catch_all +; TODO This should be removed too in a later patch +; CHECK-NO T: get_global $push{{.+}}=, __stack_pointer@GLOBAL +; CHECK: call __cxa_end_catch@FUNCTION +; CHECK-NOT: set_global __stack_pointer@GLOBAL, $pop{{.+}} +; CHECK: end_try +; CHECK-NOT: set_global __stack_pointer@GLOBAL, $pop{{.+}} +; CHECK: end_try +define void @test_no_prolog_epilog_in_ehpad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { +entry: + %stack_var = alloca i32, align 4 + call void @bar(i32* %stack_var) + invoke void @foo() + to label %try.cont unwind label %catch.dispatch + +catch.dispatch: ; preds = %entry + %0 = catchswitch within none [label %catch.start] unwind to caller + +catch.start: ; preds = %catch.dispatch + %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)] + %2 = call i8* @llvm.wasm.get.exception(token %1) + %3 = call i32 @llvm.wasm.get.ehselector(token %1) + %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) + %matches = icmp eq i32 %3, %4 + br i1 %matches, label %catch, label %rethrow + +catch: ; preds = %catch.start + %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] + %6 = bitcast i8* %5 to float* + %7 = load float, float* %6, align 4 + invoke void @foo() [ "funclet"(token %1) ] + to label %invoke.cont1 unwind label %ehcleanup + +invoke.cont1: ; preds = %catch + call void @__cxa_end_catch() [ "funclet"(token %1) ] + catchret from %1 to label %try.cont + +rethrow: ; preds = %catch.start + call void @__cxa_rethrow() [ "funclet"(token %1) ] + unreachable + +try.cont: ; preds = %entry, %invoke.cont1 + ret void + +ehcleanup: ; preds = %catch + %8 = cleanuppad within %1 [] + call void @__cxa_end_catch() [ "funclet"(token %8) ] + cleanupret from %8 unwind to caller +} + declare void @foo() +declare void @bar(i32*) declare i32 @__gxx_wasm_personality_v0(...) declare i8* @llvm.wasm.get.exception(token) declare i32 @llvm.wasm.get.ehselector(token) diff --git a/llvm/utils/TableGen/CodeGenInstruction.cpp b/llvm/utils/TableGen/CodeGenInstruction.cpp index eb35020d3d3a..a6ca400914b4 100644 --- a/llvm/utils/TableGen/CodeGenInstruction.cpp +++ b/llvm/utils/TableGen/CodeGenInstruction.cpp @@ -302,6 +302,7 @@ CodeGenInstruction::CodeGenInstruction(Record *R) AsmString = R->getValueAsString("AsmString"); isReturn = R->getValueAsBit("isReturn"); + isEHScopeReturn = R->getValueAsBit("isEHScopeReturn"); isBranch = R->getValueAsBit("isBranch"); isIndirectBranch = R->getValueAsBit("isIndirectBranch"); isCompare = R->getValueAsBit("isCompare"); diff --git a/llvm/utils/TableGen/CodeGenInstruction.h b/llvm/utils/TableGen/CodeGenInstruction.h index a50c3e60e6e7..6f79b4bcc5cd 100644 --- a/llvm/utils/TableGen/CodeGenInstruction.h +++ b/llvm/utils/TableGen/CodeGenInstruction.h @@ -222,6 +222,7 @@ template class ArrayRef; // Various boolean values we track for the instruction. bool isReturn : 1; + bool isEHScopeReturn : 1; bool isBranch : 1; bool isIndirectBranch : 1; bool isCompare : 1; diff --git a/llvm/utils/TableGen/InstrDocsEmitter.cpp b/llvm/utils/TableGen/InstrDocsEmitter.cpp index 65cb28cd17a3..41dc37a7ab35 100644 --- a/llvm/utils/TableGen/InstrDocsEmitter.cpp +++ b/llvm/utils/TableGen/InstrDocsEmitter.cpp @@ -100,6 +100,7 @@ void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS) { #define str(s) #s #define FLAG(f) if (II->f) { FlagStrings.push_back(str(f)); } FLAG(isReturn) + FLAG(isEHScopeReturn) FLAG(isBranch) FLAG(isIndirectBranch) FLAG(isCompare) diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp index 346801552093..ef8c849e25f4 100644 --- a/llvm/utils/TableGen/InstrInfoEmitter.cpp +++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp @@ -569,6 +569,7 @@ void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num, // Emit all of the target independent flags... if (Inst.isPseudo) OS << "|(1ULL<