blob: 65ceff3930acefa194f54f02abe8de8b7411fe49 [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"
Matt Arsenault43e92fe2016-06-24 06:30:11 +000023#include "AMDGPUSubtarget.h"
Konstantin Zhuravlyove3d322a2016-05-13 18:21:28 +000024#include "llvm/ADT/DenseSet.h"
Tom Stellardcc7067a62016-03-03 03:53:29 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +000028#include "llvm/CodeGen/MachineModuleInfo.h"
Tom Stellardcc7067a62016-03-03 03:53:29 +000029using namespace llvm;
30
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000031#define DEBUG_TYPE "si-debugger-insert-nops"
32#define PASS_NAME "SI Debugger Insert Nops"
Tom Stellardcc7067a62016-03-03 03:53:29 +000033
34namespace {
35
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000036class SIDebuggerInsertNops : public MachineFunctionPass {
Tom Stellardcc7067a62016-03-03 03:53:29 +000037public:
38 static char ID;
39
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000040 SIDebuggerInsertNops() : MachineFunctionPass(ID) { }
Tom Stellardcc7067a62016-03-03 03:53:29 +000041 const char *getPassName() const override { return PASS_NAME; }
42
Matt Arsenaultd3e4c642016-06-02 00:04:22 +000043 void getAnalysisUsage(AnalysisUsage &AU) const override {
44 AU.setPreservesCFG();
45 MachineFunctionPass::getAnalysisUsage(AU);
46 }
47
Tom Stellardcc7067a62016-03-03 03:53:29 +000048 bool runOnMachineFunction(MachineFunction &MF) override;
49};
50
51} // anonymous namespace
52
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000053INITIALIZE_PASS(SIDebuggerInsertNops, DEBUG_TYPE, PASS_NAME, false, false)
Tom Stellardcc7067a62016-03-03 03:53:29 +000054
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000055char SIDebuggerInsertNops::ID = 0;
56char &llvm::SIDebuggerInsertNopsID = SIDebuggerInsertNops::ID;
Tom Stellardcc7067a62016-03-03 03:53:29 +000057
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000058FunctionPass *llvm::createSIDebuggerInsertNopsPass() {
59 return new SIDebuggerInsertNops();
Tom Stellardcc7067a62016-03-03 03:53:29 +000060}
61
Konstantin Zhuravlyova7919322016-05-10 18:33:41 +000062bool SIDebuggerInsertNops::runOnMachineFunction(MachineFunction &MF) {
63 // Skip this pass if "amdgpu-debugger-insert-nops" attribute was not
64 // specified.
Matt Arsenault43e92fe2016-06-24 06:30:11 +000065 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000066 if (!ST.debuggerInsertNops())
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +000067 return false;
Konstantin Zhuravlyova40d8352016-04-22 17:04:51 +000068
69 // Skip machine functions without debug info.
70 if (!MF.getMMI().hasDebugInfo())
71 return false;
Konstantin Zhuravlyov8c273ad2016-04-18 16:28:23 +000072
73 // Target instruction info.
Matt Arsenault43e92fe2016-06-24 06:30:11 +000074 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellardcc7067a62016-03-03 03:53:29 +000075
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}