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 |
Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 11 | /// \brief 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 | |
| 22 | #include "SIInstrInfo.h" |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 23 | #include "AMDGPUSubtarget.h" |
Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseSet.h" |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunction.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Konstantin Zhuravlyov | 8c273ad | 2016-04-18 16:28:23 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "si-debugger-insert-nops" |
| 32 | #define PASS_NAME "SI Debugger Insert Nops" |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 36 | class SIDebuggerInsertNops : public MachineFunctionPass { |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 37 | public: |
| 38 | static char ID; |
| 39 | |
Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 40 | SIDebuggerInsertNops() : MachineFunctionPass(ID) { } |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 41 | const char *getPassName() const override { return PASS_NAME; } |
| 42 | |
Matt Arsenault | d3e4c64 | 2016-06-02 00:04:22 +0000 | [diff] [blame] | 43 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 44 | AU.setPreservesCFG(); |
| 45 | MachineFunctionPass::getAnalysisUsage(AU); |
| 46 | } |
| 47 | |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 48 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 49 | }; |
| 50 | |
| 51 | } // anonymous namespace |
| 52 | |
Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 53 | INITIALIZE_PASS(SIDebuggerInsertNops, DEBUG_TYPE, PASS_NAME, false, false) |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 54 | |
Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 55 | char SIDebuggerInsertNops::ID = 0; |
| 56 | char &llvm::SIDebuggerInsertNopsID = SIDebuggerInsertNops::ID; |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 57 | |
Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 58 | FunctionPass *llvm::createSIDebuggerInsertNopsPass() { |
| 59 | return new SIDebuggerInsertNops(); |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Konstantin Zhuravlyov | a791932 | 2016-05-10 18:33:41 +0000 | [diff] [blame] | 62 | bool SIDebuggerInsertNops::runOnMachineFunction(MachineFunction &MF) { |
| 63 | // Skip this pass if "amdgpu-debugger-insert-nops" attribute was not |
| 64 | // specified. |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 65 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 66 | if (!ST.debuggerInsertNops()) |
Konstantin Zhuravlyov | 8c273ad | 2016-04-18 16:28:23 +0000 | [diff] [blame] | 67 | return false; |
Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 68 | |
| 69 | // Skip machine functions without debug info. |
| 70 | if (!MF.getMMI().hasDebugInfo()) |
| 71 | return false; |
Konstantin Zhuravlyov | 8c273ad | 2016-04-18 16:28:23 +0000 | [diff] [blame] | 72 | |
| 73 | // Target instruction info. |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 74 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 75 | |
Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 76 | // Set containing line numbers that have nop inserted. |
| 77 | DenseSet<unsigned> NopInserted; |
| 78 | |
Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 79 | for (auto &MBB : MF) { |
| 80 | for (auto MI = MBB.begin(); MI != MBB.end(); ++MI) { |
Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 81 | // Skip DBG_VALUE instructions and instructions without location. |
Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 82 | if (MI->isDebugValue() || !MI->getDebugLoc()) |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 83 | continue; |
Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 84 | |
Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 85 | // Insert nop instruction if line number does not have nop inserted. |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 86 | auto DL = MI->getDebugLoc(); |
Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 87 | if (NopInserted.find(DL.getLine()) == NopInserted.end()) { |
Konstantin Zhuravlyov | a40d835 | 2016-04-22 17:04:51 +0000 | [diff] [blame] | 88 | BuildMI(MBB, *MI, DL, TII->get(AMDGPU::S_NOP)) |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 89 | .addImm(0); |
Konstantin Zhuravlyov | e3d322a | 2016-05-13 18:21:28 +0000 | [diff] [blame] | 90 | NopInserted.insert(DL.getLine()); |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
Tom Stellard | cc7067a6 | 2016-03-03 03:53:29 +0000 | [diff] [blame] | 94 | |
| 95 | return true; |
| 96 | } |