blob: 8883770efe14ffb95e134e7b962e4143577b6794 [file] [log] [blame]
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +00001//===--- SIDebuggerInsertNops.cpp - Inserts nops for debugger usage -------===//
Tom Stellardcc7067a62016-03-03 03:53:29 +00002//
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 Zhuravlyove3d322a2016-05-13 18:21:28 +000011/// \brief Inserts one nop instruction for each high level source statement for
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000012/// debugger usage.
Tom Stellardcc7067a62016-03-03 03:53:29 +000013///
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000014/// 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 Stellardcc7067a62016-03-03 03:53:29 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "SIInstrInfo.h"
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000023#include "llvm/ADT/DenseSet.h"
Tom Stellardcc7067a62016-03-03 03:53:29 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +000027#include "llvm/CodeGen/MachineModuleInfo.h"
Tom Stellardcc7067a62016-03-03 03:53:29 +000028using namespace llvm;
29
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000030#define DEBUG_TYPE "si-debugger-insert-nops"
31#define PASS_NAME "SI Debugger Insert Nops"
Tom Stellardcc7067a62016-03-03 03:53:29 +000032
33namespace {
34
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000035class SIDebuggerInsertNops : public MachineFunctionPass {
Tom Stellardcc7067a62016-03-03 03:53:29 +000036public:
37 static char ID;
38
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000039 SIDebuggerInsertNops() : MachineFunctionPass(ID) { }
Tom Stellardcc7067a62016-03-03 03:53:29 +000040 const char *getPassName() const override { return PASS_NAME; }
41
Matt Arsenaultd3e4c642016-06-02 00:04:22 +000042 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.setPreservesCFG();
44 MachineFunctionPass::getAnalysisUsage(AU);
45 }
46
Tom Stellardcc7067a62016-03-03 03:53:29 +000047 bool runOnMachineFunction(MachineFunction &MF) override;
48};
49
50} // anonymous namespace
51
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000052INITIALIZE_PASS(SIDebuggerInsertNops, DEBUG_TYPE, PASS_NAME, false, false)
Tom Stellardcc7067a62016-03-03 03:53:29 +000053
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000054char SIDebuggerInsertNops::ID = 0;
55char &llvm::SIDebuggerInsertNopsID = SIDebuggerInsertNops::ID;
Tom Stellardcc7067a62016-03-03 03:53:29 +000056
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000057FunctionPass *llvm::createSIDebuggerInsertNopsPass() {
58 return new SIDebuggerInsertNops();
Tom Stellardcc7067a62016-03-03 03:53:29 +000059}
60
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000061bool SIDebuggerInsertNops::runOnMachineFunction(MachineFunction &MF) {
62 // Skip this pass if "amdgpu-debugger-insert-nops" attribute was not
63 // specified.
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000064 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
65 if (!ST.debuggerInsertNops())
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +000066 return false;
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000067
68 // Skip machine functions without debug info.
69 if (!MF.getMMI().hasDebugInfo())
70 return false;
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +000071
72 // Target instruction info.
Tom Stellardcc7067a62016-03-03 03:53:29 +000073 const SIInstrInfo *TII =
74 static_cast<const SIInstrInfo*>(MF.getSubtarget().getInstrInfo());
75
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000076 // Set containing line numbers that have nop inserted.
77 DenseSet<unsigned> NopInserted;
78
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000079 for (auto &MBB : MF) {
80 for (auto MI = MBB.begin(); MI != MBB.end(); ++MI) {
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000081 // Skip DBG_VALUE instructions and instructions without location.
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000082 if (MI->isDebugValue() || !MI->getDebugLoc())
Tom Stellardcc7067a62016-03-03 03:53:29 +000083 continue;
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000084
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000085 // Insert nop instruction if line number does not have nop inserted.
Tom Stellardcc7067a62016-03-03 03:53:29 +000086 auto DL = MI->getDebugLoc();
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000087 if (NopInserted.find(DL.getLine()) == NopInserted.end()) {
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000088 BuildMI(MBB, *MI, DL, TII->get(AMDGPU::S_NOP))
Tom Stellardcc7067a62016-03-03 03:53:29 +000089 .addImm(0);
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000090 NopInserted.insert(DL.getLine());
Tom Stellardcc7067a62016-03-03 03:53:29 +000091 }
92 }
93 }
Tom Stellardcc7067a62016-03-03 03:53:29 +000094
95 return true;
96}