| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 1 | //===--- SIDebuggerInsertNops.cpp - Inserts nops for debugger usage -------===// |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// Inserts one nop instruction for each high level source statement for |
| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 12 | /// debugger usage. |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 13 | /// |
| Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 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 |
| 18 | /// input. |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 22 | #include "AMDGPUSubtarget.h" |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 23 | #include "SIInstrInfo.h" |
| Tom Stellard | 44b30b4 | 2018-05-22 02:03:23 +0000 | [diff] [blame] | 24 | #include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
| Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFunction.h" |
| 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Konstantin Zhuravlyov | 8c273ad | 2016-04-18 16:28:23 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "si-debugger-insert-nops" |
| 33 | #define PASS_NAME "SI Debugger Insert Nops" |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 34 | |
| 35 | namespace { |
| 36 | |
| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 37 | class SIDebuggerInsertNops : public MachineFunctionPass { |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 38 | public: |
| 39 | static char ID; |
| 40 | |
| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 41 | SIDebuggerInsertNops() : MachineFunctionPass(ID) { } |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 42 | StringRef getPassName() const override { return PASS_NAME; } |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 43 | |
| Matt Arsenault | d3e4c64 | 2016-06-02 00:04:22 +0000 | [diff] [blame] | 44 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 45 | AU.setPreservesCFG(); |
| 46 | MachineFunctionPass::getAnalysisUsage(AU); |
| 47 | } |
| 48 | |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 49 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 50 | }; |
| 51 | |
| 52 | } // anonymous namespace |
| 53 | |
| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 54 | INITIALIZE_PASS(SIDebuggerInsertNops, DEBUG_TYPE, PASS_NAME, false, false) |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 55 | |
| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 56 | char SIDebuggerInsertNops::ID = 0; |
| 57 | char &llvm::SIDebuggerInsertNopsID = SIDebuggerInsertNops::ID; |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 58 | |
| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 59 | FunctionPass *llvm::createSIDebuggerInsertNopsPass() { |
| 60 | return new SIDebuggerInsertNops(); |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 63 | bool SIDebuggerInsertNops::runOnMachineFunction(MachineFunction &MF) { |
| 64 | // Skip this pass if "amdgpu-debugger-insert-nops" attribute was not |
| 65 | // specified. |
| Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 66 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
| Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 67 | if (!ST.debuggerInsertNops()) |
| Konstantin Zhuravlyov | 8c273ad | 2016-04-18 16:28:23 +0000 | [diff] [blame] | 68 | return false; |
| Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 69 | |
| 70 | // Skip machine functions without debug info. |
| 71 | if (!MF.getMMI().hasDebugInfo()) |
| 72 | return false; |
| Konstantin Zhuravlyov | 8c273ad | 2016-04-18 16:28:23 +0000 | [diff] [blame] | 73 | |
| 74 | // Target instruction info. |
| Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 75 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 76 | |
| Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 77 | // Set containing line numbers that have nop inserted. |
| 78 | DenseSet<unsigned> NopInserted; |
| 79 | |
| Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 80 | for (auto &MBB : MF) { |
| 81 | for (auto MI = MBB.begin(); MI != MBB.end(); ++MI) { |
| Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 82 | // Skip debug instructions and instructions without location. |
| 83 | if (MI->isDebugInstr() || !MI->getDebugLoc()) |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 84 | continue; |
| Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 85 | |
| Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 86 | // Insert nop instruction if line number does not have nop inserted. |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 87 | auto DL = MI->getDebugLoc(); |
| Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 88 | if (NopInserted.find(DL.getLine()) == NopInserted.end()) { |
| Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 89 | BuildMI(MBB, *MI, DL, TII->get(AMDGPU::S_NOP)) |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 90 | .addImm(0); |
| Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 91 | NopInserted.insert(DL.getLine()); |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 95 | |
| 96 | return true; |
| 97 | } |