return false; // Function is too small.
}
- auto &FirstMBB = *MF.begin();
+ // We look for the first non-empty MachineBasicBlock, so that we can insert
+ // the function instrumentation in the appropriate place.
+ auto MBI =
+ find_if(MF, [&](const MachineBasicBlock &MBB) { return !MBB.empty(); });
+ if (MBI == MF.end())
+ return false; // The function is empty.
+
+ auto *TII = MF.getSubtarget().getInstrInfo();
+ auto &FirstMBB = *MBI;
auto &FirstMI = *FirstMBB.begin();
if (!MF.getSubtarget().isXRaySupported()) {
// First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the
// MachineFunction.
- auto *TII = MF.getSubtarget().getInstrInfo();
BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
--- /dev/null
+# RUN: llc -run-pass xray-instrumentation -mtriple=x86_64-unknown-linux-gnu -o - %s | FileCheck %s
+#
+# Make sure we can handle empty first basic blocks.
+
+--- |
+
+ define i32 @foo() noinline uwtable "xray-instruction-threshold"="1" {
+ entry:
+ unreachable
+ }
+
+...
+
+---
+name: foo
+tracksRegLiveness: true
+liveins:
+ - { reg: '%edi'}
+body: |
+ bb.0.entry:
+ liveins: %edi
+ ; CHECK-NOT: PATCHABLE_FUNCTION_ENTER
+...
--- /dev/null
+# RUN: llc -run-pass xray-instrumentation -mtriple=x86_64-unknown-linux-gnu -o - %s | FileCheck %s
+#
+# Make sure we can handle empty functions.
+---
+name: empty
+tracksRegLiveness: true
+liveins:
+ - { reg: '%edi'}
+body: |
+ bb.0:
+ ; CHECK-NOT: PATCHABLE_FUNCTION_ENTER
+...
+