diff --git a/llvm/lib/CodeGen/PatchableFunction.cpp b/llvm/lib/CodeGen/PatchableFunction.cpp --- a/llvm/lib/CodeGen/PatchableFunction.cpp +++ b/llvm/lib/CodeGen/PatchableFunction.cpp @@ -57,10 +57,15 @@ bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) { if (MF.getFunction().hasFnAttribute("patchable-function-entry")) { MachineBasicBlock &FirstMBB = *MF.begin(); - MachineInstr &FirstMI = *FirstMBB.begin(); const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); - BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(), - TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER)); + if (FirstMBB.empty()) { + BuildMI(&FirstMBB, DebugLoc(), + TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER)); + } else { + MachineInstr &FirstMI = *FirstMBB.begin(); + BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(), + TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER)); + } return true; } diff --git a/llvm/test/CodeGen/AArch64/patchable-function-entry-empty.mir b/llvm/test/CodeGen/AArch64/patchable-function-entry-empty.mir new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/patchable-function-entry-empty.mir @@ -0,0 +1,30 @@ +## Test we can handle empty entry MBB. +# RUN: llc -mtriple=aarch64 -run-pass=patchable-function %s -o - | FileCheck %s + +# CHECK: bb.0.entry +# CHECK: PATCHABLE_FUNCTION_ENTER +# CHECK: bb.1.here + +--- | + define internal fastcc void @empty_entry() "patchable-function-entry"="1" { + entry: + br label %here + here: + ret void + } +... +--- +name: empty_entry +alignment: 4 +tracksRegLiveness: true +frameInfo: + maxAlignment: 1 + maxCallFrameSize: 0 +machineFunctionInfo: {} +body: | + bb.0.entry: + liveins: $lr + bb.1.here: + liveins: $lr + RET undef $lr +...