diff --git a/llvm/test/tools/llvm-reduce/mir/remove-frame-destroy.mir b/llvm/test/tools/llvm-reduce/mir/remove-frame-destroy.mir new file mode 100644 index 000000000000..7a7edcbf75e3 --- /dev/null +++ b/llvm/test/tools/llvm-reduce/mir/remove-frame-destroy.mir @@ -0,0 +1,24 @@ +# REQUIRES: amdgpu-registered-target +# RUN: llvm-reduce --delta-passes=instructions -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log +# RUN: FileCheck --check-prefix=RESULT %s < %t + +# CHECK-INTERESTINGNESS: S_NOP 0 + +# RESULT: ADJCALLSTACKUP +# RESULT-NEXT: ADJCALLSTACKDOWN +# RESULT-NEXT: S_ENDPGM 0 + +--- +name: frame_setup_destroy +tracksRegLiveness: true +machineFunctionInfo: + stackPtrOffsetReg: '$sgpr32' +body: | + bb.0: + S_NOP 0 + ADJCALLSTACKUP 0, 0, implicit-def dead $scc, implicit-def $sgpr32, implicit $sgpr32 + S_NOP 0 + ADJCALLSTACKDOWN 0, 0, implicit-def dead $scc, implicit-def $sgpr32, implicit $sgpr32 + S_ENDPGM 0 + +... diff --git a/llvm/tools/llvm-reduce/deltas/ReduceInstructionsMIR.cpp b/llvm/tools/llvm-reduce/deltas/ReduceInstructionsMIR.cpp index 3be2eae01df0..065de7da5a1a 100644 --- a/llvm/tools/llvm-reduce/deltas/ReduceInstructionsMIR.cpp +++ b/llvm/tools/llvm-reduce/deltas/ReduceInstructionsMIR.cpp @@ -45,6 +45,21 @@ static Register getPrevDefOfRCInMBB(MachineBasicBlock &MBB, return 0; } +static bool shouldNotRemoveInstruction(const TargetInstrInfo &TII, + const MachineInstr &MI) { + if (MI.isTerminator()) + return true; + + // The MIR is almost certainly going to be invalid if frame instructions are + // deleted individually since they need to come in balanced pairs, so don't + // try to delete them. + if (MI.getOpcode() == TII.getCallFrameSetupOpcode() || + MI.getOpcode() == TII.getCallFrameDestroyOpcode()) + return true; + + return false; +} + static void extractInstrFromFunction(Oracle &O, MachineFunction &MF) { MachineDominatorTree MDT; MDT.runOnMachineFunction(MF); @@ -52,12 +67,14 @@ static void extractInstrFromFunction(Oracle &O, MachineFunction &MF) { auto MRI = &MF.getRegInfo(); SetVector ToDelete; + const TargetSubtargetInfo &STI = MF.getSubtarget(); + const TargetInstrInfo *TII = STI.getInstrInfo(); MachineInstr *TopMI = nullptr; // Mark MIs for deletion according to some criteria. for (auto &MBB : MF) { for (auto &MI : MBB) { - if (MI.isTerminator()) + if (shouldNotRemoveInstruction(*TII, MI)) continue; if (MBB.isEntryBlock() && !TopMI) { TopMI = &MI;