1 //===--- SIDebuggerInsertNops.cpp - Inserts nops for debugger usage -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Inserts one nop instruction for each high level source statement for
14 /// Tools, such as a debugger, need to pause execution based on user input (i.e.
15 /// breakpoint). In order to do this, one nop instruction is inserted before the
16 /// first isa instruction of each high level source statement. Further, the
17 /// debugger may replace nop instructions with trap instructions based on user
20 //===----------------------------------------------------------------------===//
22 #include "AMDGPUSubtarget.h"
23 #include "SIInstrInfo.h"
24 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineModuleInfo.h"
32 #define DEBUG_TYPE "si-debugger-insert-nops"
33 #define PASS_NAME "SI Debugger Insert Nops"
37 class SIDebuggerInsertNops : public MachineFunctionPass {
41 SIDebuggerInsertNops() : MachineFunctionPass(ID) { }
42 StringRef getPassName() const override { return PASS_NAME; }
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
46 MachineFunctionPass::getAnalysisUsage(AU);
49 bool runOnMachineFunction(MachineFunction &MF) override;
52 } // anonymous namespace
54 INITIALIZE_PASS(SIDebuggerInsertNops, DEBUG_TYPE, PASS_NAME, false, false)
56 char SIDebuggerInsertNops::ID = 0;
57 char &llvm::SIDebuggerInsertNopsID = SIDebuggerInsertNops::ID;
59 FunctionPass *llvm::createSIDebuggerInsertNopsPass() {
60 return new SIDebuggerInsertNops();
63 bool SIDebuggerInsertNops::runOnMachineFunction(MachineFunction &MF) {
64 // Skip this pass if "amdgpu-debugger-insert-nops" attribute was not
66 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
67 if (!ST.debuggerInsertNops())
70 // Skip machine functions without debug info.
71 if (!MF.getMMI().hasDebugInfo())
74 // Target instruction info.
75 const SIInstrInfo *TII = ST.getInstrInfo();
77 // Set containing line numbers that have nop inserted.
78 DenseSet<unsigned> NopInserted;
80 for (auto &MBB : MF) {
81 for (auto MI = MBB.begin(); MI != MBB.end(); ++MI) {
82 // Skip debug instructions and instructions without location.
83 if (MI->isDebugInstr() || !MI->getDebugLoc())
86 // Insert nop instruction if line number does not have nop inserted.
87 auto DL = MI->getDebugLoc();
88 if (NopInserted.find(DL.getLine()) == NopInserted.end()) {
89 BuildMI(MBB, *MI, DL, TII->get(AMDGPU::S_NOP))
91 NopInserted.insert(DL.getLine());