LLVM  9.0.0svn
SIDebuggerInsertNops.cpp
Go to the documentation of this file.
1 //===--- SIDebuggerInsertNops.cpp - Inserts nops for debugger usage -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Inserts one nop instruction for each high level source statement for
11 /// debugger usage.
12 ///
13 /// Tools, such as a debugger, need to pause execution based on user input (i.e.
14 /// breakpoint). In order to do this, one nop instruction is inserted before the
15 /// first isa instruction of each high level source statement. Further, the
16 /// debugger may replace nop instructions with trap instructions based on user
17 /// input.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #include "AMDGPUSubtarget.h"
22 #include "SIInstrInfo.h"
24 #include "llvm/ADT/DenseSet.h"
29 using namespace llvm;
30 
31 #define DEBUG_TYPE "si-debugger-insert-nops"
32 #define PASS_NAME "SI Debugger Insert Nops"
33 
34 namespace {
35 
36 class SIDebuggerInsertNops : public MachineFunctionPass {
37 public:
38  static char ID;
39 
40  SIDebuggerInsertNops() : MachineFunctionPass(ID) { }
41  StringRef getPassName() const override { return PASS_NAME; }
42 
43  void getAnalysisUsage(AnalysisUsage &AU) const override {
44  AU.setPreservesCFG();
46  }
47 
48  bool runOnMachineFunction(MachineFunction &MF) override;
49 };
50 
51 } // anonymous namespace
52 
53 INITIALIZE_PASS(SIDebuggerInsertNops, DEBUG_TYPE, PASS_NAME, false, false)
54 
57 
59  return new SIDebuggerInsertNops();
60 }
61 
62 bool SIDebuggerInsertNops::runOnMachineFunction(MachineFunction &MF) {
63  // Skip this pass if "amdgpu-debugger-insert-nops" attribute was not
64  // specified.
66  if (!ST.debuggerInsertNops())
67  return false;
68 
69  // Skip machine functions without debug info.
70  if (!MF.getMMI().hasDebugInfo())
71  return false;
72 
73  // Target instruction info.
74  const SIInstrInfo *TII = ST.getInstrInfo();
75 
76  // Set containing line numbers that have nop inserted.
77  DenseSet<unsigned> NopInserted;
78 
79  for (auto &MBB : MF) {
80  for (auto MI = MBB.begin(); MI != MBB.end(); ++MI) {
81  // Skip debug instructions and instructions without location.
82  if (MI->isDebugInstr() || !MI->getDebugLoc())
83  continue;
84 
85  // Insert nop instruction if line number does not have nop inserted.
86  auto DL = MI->getDebugLoc();
87  if (NopInserted.find(DL.getLine()) == NopInserted.end()) {
88  BuildMI(MBB, *MI, DL, TII->get(AMDGPU::S_NOP))
89  .addImm(0);
90  NopInserted.insert(DL.getLine());
91  }
92  }
93  }
94 
95  return true;
96 }
#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:23
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:187
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:33
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:285
Provides AMDGPU specific target descriptions.
Interface definition for SIInstrInfo.
iterator find(const_arg_type_t< ValueT > V)
Definition: DenseSet.h:165
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
char & SIDebuggerInsertNopsID