LLVM  16.0.0git
MachineDebugify.cpp
Go to the documentation of this file.
1 //===- MachineDebugify.cpp - Attach synthetic debug info to everything ----===//
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 This pass attaches synthetic debug info to everything. It can be used
10 /// to create targeted tests for debug info preservation, or test for CodeGen
11 /// differences with vs. without debug info.
12 ///
13 /// This isn't intended to have feature parity with Debugify.
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/InitializePasses.h"
27 
28 #define DEBUG_TYPE "mir-debugify"
29 
30 using namespace llvm;
31 
32 namespace {
33 bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
34  DIBuilder &DIB, Function &F) {
35  MachineFunction *MaybeMF = MMI.getMachineFunction(F);
36  if (!MaybeMF)
37  return false;
38  MachineFunction &MF = *MaybeMF;
40 
41  DISubprogram *SP = F.getSubprogram();
42  assert(SP && "IR Debugify just created it?");
43 
44  Module &M = *F.getParent();
45  LLVMContext &Ctx = M.getContext();
46 
47  unsigned NextLine = SP->getLine();
48  for (MachineBasicBlock &MBB : MF) {
49  for (MachineInstr &MI : MBB) {
50  // This will likely emit line numbers beyond the end of the imagined
51  // source function and into subsequent ones. We don't do anything about
52  // that as it doesn't really matter to the compiler where the line is in
53  // the imaginary source code.
54  MI.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
55  }
56  }
57 
58  // Find local variables defined by debugify. No attempt is made to match up
59  // MIR-level regs to the 'correct' IR-level variables: there isn't a simple
60  // way to do that, and it isn't necessary to find interesting CodeGen bugs.
61  // Instead, simply keep track of one variable per line. Later, we can insert
62  // DBG_VALUE insts that point to these local variables. Emitting DBG_VALUEs
63  // which cover a wide range of lines can help stress the debug info passes:
64  // if we can't do that, fall back to using the local variable which precedes
65  // all the others.
66  Function *DbgValF = M.getFunction("llvm.dbg.value");
67  DbgValueInst *EarliestDVI = nullptr;
69  DIExpression *Expr = nullptr;
70  if (DbgValF) {
71  for (const Use &U : DbgValF->uses()) {
72  auto *DVI = dyn_cast<DbgValueInst>(U.getUser());
73  if (!DVI || DVI->getFunction() != &F)
74  continue;
75  unsigned Line = DVI->getDebugLoc().getLine();
76  assert(Line != 0 && "debugify should not insert line 0 locations");
77  Line2Var[Line] = DVI->getVariable();
78  if (!EarliestDVI || Line < EarliestDVI->getDebugLoc().getLine())
79  EarliestDVI = DVI;
80  Expr = DVI->getExpression();
81  }
82  }
83  if (Line2Var.empty())
84  return true;
85 
86  // Now, try to insert a DBG_VALUE instruction after each real instruction.
87  // Do this by introducing debug uses of each register definition. If that is
88  // not possible (e.g. we have a phi or a meta instruction), emit a constant.
89  uint64_t NextImm = 0;
91  const MCInstrDesc &DbgValDesc = TII.get(TargetOpcode::DBG_VALUE);
92  for (MachineBasicBlock &MBB : MF) {
94  for (auto I = MBB.begin(), E = MBB.end(); I != E;) {
95  MachineInstr &MI = *I;
96  ++I;
97 
98  // `I` may point to a DBG_VALUE created in the previous loop iteration.
99  if (MI.isDebugInstr())
100  continue;
101 
102  // It's not allowed to insert DBG_VALUEs after a terminator.
103  if (MI.isTerminator())
104  continue;
105 
106  // Find a suitable insertion point for the DBG_VALUE.
107  auto InsertBeforeIt = MI.isPHI() ? FirstNonPHIIt : I;
108 
109  // Find a suitable local variable for the DBG_VALUE.
110  unsigned Line = MI.getDebugLoc().getLine();
111  if (!Line2Var.count(Line))
112  Line = EarliestDVI->getDebugLoc().getLine();
113  DILocalVariable *LocalVar = Line2Var[Line];
114  assert(LocalVar && "No variable for current line?");
115  VarSet.insert(LocalVar);
116 
117  // Emit DBG_VALUEs for register definitions.
119  for (MachineOperand &MO : MI.operands())
120  if (MO.isReg() && MO.isDef() && MO.getReg())
121  RegDefs.push_back(&MO);
122  for (MachineOperand *MO : RegDefs)
123  BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
124  /*IsIndirect=*/false, *MO, LocalVar, Expr);
125 
126  // OK, failing that, emit a constant DBG_VALUE.
127  if (RegDefs.empty()) {
128  auto ImmOp = MachineOperand::CreateImm(NextImm++);
129  BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
130  /*IsIndirect=*/false, ImmOp, LocalVar, Expr);
131  }
132  }
133  }
134 
135  // Here we save the number of lines and variables into "llvm.mir.debugify".
136  // It is useful for mir-check-debugify.
137  NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
139  if (!NMD) {
140  NMD = M.getOrInsertNamedMetadata("llvm.mir.debugify");
141  auto addDebugifyOperand = [&](unsigned N) {
142  NMD->addOperand(MDNode::get(
144  };
145  // Add number of lines.
146  addDebugifyOperand(NextLine - 1);
147  // Add number of variables.
148  addDebugifyOperand(VarSet.size());
149  } else {
150  assert(NMD->getNumOperands() == 2 &&
151  "llvm.mir.debugify should have exactly 2 operands!");
152  auto setDebugifyOperand = [&](unsigned Idx, unsigned N) {
155  };
156  auto getDebugifyOperand = [&](unsigned Idx) {
157  return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
158  ->getZExtValue();
159  };
160  // Set number of lines.
161  setDebugifyOperand(0, NextLine - 1);
162  // Set number of variables.
163  auto OldNumVars = getDebugifyOperand(1);
164  setDebugifyOperand(1, OldNumVars + VarSet.size());
165  }
166 
167  return true;
168 }
169 
170 /// ModulePass for attaching synthetic debug info to everything, used with the
171 /// legacy module pass manager.
172 struct DebugifyMachineModule : public ModulePass {
173  bool runOnModule(Module &M) override {
174  // We will insert new debugify metadata, so erasing the old one.
175  assert(!M.getNamedMetadata("llvm.mir.debugify") &&
176  "llvm.mir.debugify metadata already exists! Strip it first");
177  MachineModuleInfo &MMI =
178  getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
179  return applyDebugifyMetadata(
180  M, M.functions(),
181  "ModuleDebugify: ", [&](DIBuilder &DIB, Function &F) -> bool {
182  return applyDebugifyMetadataToMachineFunction(MMI, DIB, F);
183  });
184  }
185 
186  DebugifyMachineModule() : ModulePass(ID) {}
187 
188  void getAnalysisUsage(AnalysisUsage &AU) const override {
191  AU.setPreservesCFG();
192  }
193 
194  static char ID; // Pass identification.
195 };
197 
198 } // end anonymous namespace
199 
200 INITIALIZE_PASS_BEGIN(DebugifyMachineModule, DEBUG_TYPE,
201  "Machine Debugify Module", false, false)
202 INITIALIZE_PASS_END(DebugifyMachineModule, DEBUG_TYPE,
203  "Machine Debugify Module", false, false)
204 
206  return new DebugifyMachineModule();
207 }
Int32Ty
IntegerType * Int32Ty
Definition: NVVMIntrRange.cpp:67
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:108
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
DEBUG_TYPE
#define DEBUG_TYPE
Definition: MachineDebugify.cpp:28
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::NamedMDNode
A tuple of MDNodes.
Definition: Metadata.h:1588
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:818
llvm::NamedMDNode::getNumOperands
unsigned getNumOperands() const
Definition: Metadata.cpp:1212
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
IntrinsicInst.h
llvm::DIBuilder
Definition: DIBuilder.h:42
Debugify.h
llvm::Function
Definition: Function.h:60
llvm::TargetSubtargetInfo::getInstrInfo
virtual const TargetInstrInfo * getInstrInfo() const
Definition: TargetSubtargetInfo.h:95
llvm::lltok::LocalVar
@ LocalVar
Definition: LLToken.h:440
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::NamedMDNode::setOperand
void setOperand(unsigned I, MDNode *New)
Definition: Metadata.cpp:1224
DenseMap.h
TargetInstrInfo.h
llvm::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:136
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::count
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
llvm::applyDebugifyMetadata
bool applyDebugifyMetadata(Module &M, iterator_range< Module::iterator > Functions, StringRef Banner, std::function< bool(DIBuilder &, Function &)> ApplyToMF)
Add synthesized debug information to a module.
llvm::DIExpression
DWARF expression.
Definition: DebugInfoMetadata.h:2596
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1400
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::NamedMDNode::addOperand
void addOperand(MDNode *M)
Definition: Metadata.cpp:1222
llvm::TargetInstrInfo
TargetInstrInfo - Interface to description of machine instruction set.
Definition: TargetInstrInfo.h:98
llvm::DbgValueInst
This represents the llvm.dbg.value instruction.
Definition: IntrinsicInst.h:387
llvm::MachineOperand::CreateImm
static MachineOperand CreateImm(int64_t Val)
Definition: MachineOperand.h:782
llvm::DILocalVariable
Local variable.
Definition: DebugInfoMetadata.h:3096
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::Value::uses
iterator_range< use_iterator > uses()
Definition: Value.h:376
false
Definition: StackSlotColoring.cpp:141
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:125
llvm::MCInstrDesc
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:197
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:48
llvm::IntegerType
Class to represent integer types.
Definition: DerivedTypes.h:40
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition: MachineModuleInfo.h:74
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:879
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
Passes.h
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1292
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:657
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
uint64_t
llvm::MachineModuleInfoWrapperPass
Definition: MachineModuleInfo.h:214
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::DenseMap
Definition: DenseMap.h:714
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::MachineBasicBlock::getFirstNonPHI
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: MachineBasicBlock.cpp:197
llvm::NamedMDNode::getOperand
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1216
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineModuleInfo.h
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::empty
bool empty() const
Definition: DenseMap.h:98
llvm::MachineFunction
Definition: MachineFunction.h:257
getDebugLoc
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first found DebugLoc that has a DILocation, given a range of instructions.
Definition: MachineInstrBundle.cpp:110
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition: PassAnalysisSupport.h:98
TargetSubtargetInfo.h
llvm::ValueAsMetadata::getConstant
static ConstantAsMetadata * getConstant(Value *C)
Definition: Metadata.h:367
llvm::createDebugifyMachineModulePass
ModulePass * createDebugifyMachineModulePass()
Creates MIR Debugify pass.
Definition: MachineDebugify.cpp:205
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineModuleInfo::getMachineFunction
MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
Definition: MachineModuleInfo.cpp:103
llvm::SmallSet::insert
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:178
llvm::DebugLoc::getLine
unsigned getLine() const
Definition: DebugLoc.cpp:24
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:357
Machine
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:370
SmallVector.h
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:305
llvm::Instruction::getDebugLoc
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:354
MachineInstrBuilder.h
llvm::SmallSet::size
size_type size() const
Definition: SmallSet.h:160
N
#define N
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(DebugifyMachineModule, DEBUG_TYPE, "Machine Debugify Module", false, false) INITIALIZE_PASS_END(DebugifyMachineModule
llvm::DISubprogram
Subprogram description.
Definition: DebugInfoMetadata.h:1841
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::MachineInstrBundleIterator< MachineInstr >
InitializePasses.h
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:307
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
SmallSet.h