LLVM  8.0.0svn
SIDebuggerInsertNops.cpp
Go to the documentation of this file.
1 //===--- SIDebuggerInsertNops.cpp - Inserts nops for debugger usage -------===//
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
11 /// Inserts one nop instruction for each high level source statement for
12 /// debugger usage.
13 ///
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.
19 //
20 //===----------------------------------------------------------------------===//
21 
22 #include "AMDGPUSubtarget.h"
23 #include "SIInstrInfo.h"
25 #include "llvm/ADT/DenseSet.h"
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "si-debugger-insert-nops"
33 #define PASS_NAME "SI Debugger Insert Nops"
34 
35 namespace {
36 
37 class SIDebuggerInsertNops : public MachineFunctionPass {
38 public:
39  static char ID;
40 
41  SIDebuggerInsertNops() : MachineFunctionPass(ID) { }
42  StringRef getPassName() const override { return PASS_NAME; }
43 
44  void getAnalysisUsage(AnalysisUsage &AU) const override {
45  AU.setPreservesCFG();
47  }
48 
49  bool runOnMachineFunction(MachineFunction &MF) override;
50 };
51 
52 } // anonymous namespace
53 
54 INITIALIZE_PASS(SIDebuggerInsertNops, DEBUG_TYPE, PASS_NAME, false, false)
55 
58 
60  return new SIDebuggerInsertNops();
61 }
62 
63 bool SIDebuggerInsertNops::runOnMachineFunction(MachineFunction &MF) {
64  // Skip this pass if "amdgpu-debugger-insert-nops" attribute was not
65  // specified.
67  if (!ST.debuggerInsertNops())
68  return false;
69 
70  // Skip machine functions without debug info.
71  if (!MF.getMMI().hasDebugInfo())
72  return false;
73 
74  // Target instruction info.
75  const SIInstrInfo *TII = ST.getInstrInfo();
76 
77  // Set containing line numbers that have nop inserted.
78  DenseSet<unsigned> NopInserted;
79 
80  for (auto &MBB : MF) {
81  for (auto MI = MBB.begin(); MI != MBB.end(); ++MI) {
82  // Skip debug instructions and instructions without location.
83  if (MI->isDebugInstr() || !MI->getDebugLoc())
84  continue;
85 
86  // Insert nop instruction if line number does not have nop inserted.
87  auto DL = MI->getDebugLoc();
88  if (NopInserted.find(DL.getLine()) == NopInserted.end()) {
89  BuildMI(MBB, *MI, DL, TII->get(AMDGPU::S_NOP))
90  .addImm(0);
91  NopInserted.insert(DL.getLine());
92  }
93  }
94  }
95 
96  return true;
97 }
#define DEBUG_TYPE
#define PASS_NAME
AMDGPU specific subclass of TargetSubtarget.
bool hasDebugInfo() const
Returns true if valid debug info is present.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
const SIInstrInfo * getInstrInfo() const override
MachineModuleInfo & getMMI() const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool debuggerInsertNops() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createSIDebuggerInsertNopsPass()
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:188
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:34
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:286
Provides AMDGPU specific target descriptions.
Interface definition for SIInstrInfo.
iterator find(const_arg_type_t< ValueT > V)
Definition: DenseSet.h:166
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
char & SIDebuggerInsertNopsID