blob: ce29484e2f00d12d3138c2963552a0bfd16a55b0 [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
42 bool runOnMachineFunction(MachineFunction &MF) override;
43};
44
45} // anonymous namespace
46
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000047INITIALIZE_PASS(SIDebuggerInsertNops, DEBUG_TYPE, PASS_NAME, false, false)
Tom Stellardcc7067a62016-03-03 03:53:29 +000048
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000049char SIDebuggerInsertNops::ID = 0;
50char &llvm::SIDebuggerInsertNopsID = SIDebuggerInsertNops::ID;
Tom Stellardcc7067a62016-03-03 03:53:29 +000051
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000052FunctionPass *llvm::createSIDebuggerInsertNopsPass() {
53 return new SIDebuggerInsertNops();
Tom Stellardcc7067a62016-03-03 03:53:29 +000054}
55
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000056bool SIDebuggerInsertNops::runOnMachineFunction(MachineFunction &MF) {
57 // Skip this pass if "amdgpu-debugger-insert-nops" attribute was not
58 // specified.
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000059 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
60 if (!ST.debuggerInsertNops())
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +000061 return false;
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000062
63 // Skip machine functions without debug info.
64 if (!MF.getMMI().hasDebugInfo())
65 return false;
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +000066
67 // Target instruction info.
Tom Stellardcc7067a62016-03-03 03:53:29 +000068 const SIInstrInfo *TII =
69 static_cast<const SIInstrInfo*>(MF.getSubtarget().getInstrInfo());
70
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000071 // Set containing line numbers that have nop inserted.
72 DenseSet<unsigned> NopInserted;
73
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000074 for (auto &MBB : MF) {
75 for (auto MI = MBB.begin(); MI != MBB.end(); ++MI) {
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000076 // Skip DBG_VALUE instructions and instructions without location.
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000077 if (MI->isDebugValue() || !MI->getDebugLoc())
Tom Stellardcc7067a62016-03-03 03:53:29 +000078 continue;
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000079
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000080 // Insert nop instruction if line number does not have nop inserted.
Tom Stellardcc7067a62016-03-03 03:53:29 +000081 auto DL = MI->getDebugLoc();
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000082 if (NopInserted.find(DL.getLine()) == NopInserted.end()) {
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000083 BuildMI(MBB, *MI, DL, TII->get(AMDGPU::S_NOP))
Tom Stellardcc7067a62016-03-03 03:53:29 +000084 .addImm(0);
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000085 NopInserted.insert(DL.getLine());
Tom Stellardcc7067a62016-03-03 03:53:29 +000086 }
87 }
88 }
Tom Stellardcc7067a62016-03-03 03:53:29 +000089
90 return true;
91}