LLVM 19.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"
21#include "llvm/CodeGen/Passes.h"
27
28#define DEBUG_TYPE "mir-debugify"
29
30using namespace llvm;
31
32namespace {
33bool 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;
68 DbgVariableRecord *EarliestDVR = nullptr;
70 DIExpression *Expr = nullptr;
71 if (DbgValF) {
72 for (const Use &U : DbgValF->uses()) {
73 auto *DVI = dyn_cast<DbgValueInst>(U.getUser());
74 if (!DVI || DVI->getFunction() != &F)
75 continue;
76 unsigned Line = DVI->getDebugLoc().getLine();
77 assert(Line != 0 && "debugify should not insert line 0 locations");
78 Line2Var[Line] = DVI->getVariable();
79 if (!EarliestDVI || Line < EarliestDVI->getDebugLoc().getLine())
80 EarliestDVI = DVI;
81 Expr = DVI->getExpression();
82 }
83 }
84 for (BasicBlock &BB : F) {
85 for (Instruction &I : BB) {
86 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
87 if (!DVR.isDbgValue())
88 continue;
89 unsigned Line = DVR.getDebugLoc().getLine();
90 assert(Line != 0 && "debugify should not insert line 0 locations");
91 Line2Var[Line] = DVR.getVariable();
92 if (!EarliestDVR || Line < EarliestDVR->getDebugLoc().getLine())
93 EarliestDVR = &DVR;
94 Expr = DVR.getExpression();
95 }
96 }
97 }
98 if (Line2Var.empty())
99 return true;
100
101 // Now, try to insert a DBG_VALUE instruction after each real instruction.
102 // Do this by introducing debug uses of each register definition. If that is
103 // not possible (e.g. we have a phi or a meta instruction), emit a constant.
104 uint64_t NextImm = 0;
106 const MCInstrDesc &DbgValDesc = TII.get(TargetOpcode::DBG_VALUE);
107 for (MachineBasicBlock &MBB : MF) {
109 for (auto I = MBB.begin(), E = MBB.end(); I != E;) {
110 MachineInstr &MI = *I;
111 ++I;
112
113 // `I` may point to a DBG_VALUE created in the previous loop iteration.
114 if (MI.isDebugInstr())
115 continue;
116
117 // It's not allowed to insert DBG_VALUEs after a terminator.
118 if (MI.isTerminator())
119 continue;
120
121 // Find a suitable insertion point for the DBG_VALUE.
122 auto InsertBeforeIt = MI.isPHI() ? FirstNonPHIIt : I;
123
124 // Find a suitable local variable for the DBG_VALUE.
125 unsigned Line = MI.getDebugLoc().getLine();
126 if (!Line2Var.count(Line))
127 Line = EarliestDVI ? EarliestDVI->getDebugLoc().getLine()
128 : EarliestDVR->getDebugLoc().getLine();
129 DILocalVariable *LocalVar = Line2Var[Line];
130 assert(LocalVar && "No variable for current line?");
131 VarSet.insert(LocalVar);
132
133 // Emit DBG_VALUEs for register definitions.
135 for (MachineOperand &MO : MI.all_defs())
136 if (MO.getReg())
137 RegDefs.push_back(&MO);
138 for (MachineOperand *MO : RegDefs)
139 BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
140 /*IsIndirect=*/false, *MO, LocalVar, Expr);
141
142 // OK, failing that, emit a constant DBG_VALUE.
143 if (RegDefs.empty()) {
144 auto ImmOp = MachineOperand::CreateImm(NextImm++);
145 BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
146 /*IsIndirect=*/false, ImmOp, LocalVar, Expr);
147 }
148 }
149 }
150
151 // Here we save the number of lines and variables into "llvm.mir.debugify".
152 // It is useful for mir-check-debugify.
153 NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
154 IntegerType *Int32Ty = Type::getInt32Ty(Ctx);
155 if (!NMD) {
156 NMD = M.getOrInsertNamedMetadata("llvm.mir.debugify");
157 auto addDebugifyOperand = [&](unsigned N) {
159 Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
160 };
161 // Add number of lines.
162 addDebugifyOperand(NextLine - 1);
163 // Add number of variables.
164 addDebugifyOperand(VarSet.size());
165 } else {
166 assert(NMD->getNumOperands() == 2 &&
167 "llvm.mir.debugify should have exactly 2 operands!");
168 auto setDebugifyOperand = [&](unsigned Idx, unsigned N) {
170 ConstantInt::get(Int32Ty, N))));
171 };
172 auto getDebugifyOperand = [&](unsigned Idx) {
173 return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
174 ->getZExtValue();
175 };
176 // Set number of lines.
177 setDebugifyOperand(0, NextLine - 1);
178 // Set number of variables.
179 auto OldNumVars = getDebugifyOperand(1);
180 setDebugifyOperand(1, OldNumVars + VarSet.size());
181 }
182
183 return true;
184}
185
186/// ModulePass for attaching synthetic debug info to everything, used with the
187/// legacy module pass manager.
188struct DebugifyMachineModule : public ModulePass {
189 bool runOnModule(Module &M) override {
190 // We will insert new debugify metadata, so erasing the old one.
191 assert(!M.getNamedMetadata("llvm.mir.debugify") &&
192 "llvm.mir.debugify metadata already exists! Strip it first");
193 MachineModuleInfo &MMI =
194 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
196 M, M.functions(),
197 "ModuleDebugify: ", [&](DIBuilder &DIB, Function &F) -> bool {
198 return applyDebugifyMetadataToMachineFunction(MMI, DIB, F);
199 });
200 }
201
202 DebugifyMachineModule() : ModulePass(ID) {}
203
204 void getAnalysisUsage(AnalysisUsage &AU) const override {
207 AU.setPreservesCFG();
208 }
209
210 static char ID; // Pass identification.
211};
212char DebugifyMachineModule::ID = 0;
213
214} // end anonymous namespace
215
216INITIALIZE_PASS_BEGIN(DebugifyMachineModule, DEBUG_TYPE,
217 "Machine Debugify Module", false, false)
218INITIALIZE_PASS_END(DebugifyMachineModule, DEBUG_TYPE,
219 "Machine Debugify Module", false, false)
220
222 return new DebugifyMachineModule();
223}
MachineBasicBlock & MBB
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:371
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define DEBUG_TYPE
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.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallSet class.
This file defines the SmallVector class.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:269
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
DWARF expression.
Subprogram description.
DebugLoc getDebugLoc() const
This represents the llvm.dbg.value instruction.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
unsigned getLine() const
Definition: DebugLoc.cpp:24
bool empty() const
Definition: DenseMap.h:98
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:151
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:454
Class to represent integer types.
Definition: DerivedTypes.h:40
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1428
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Representation of each machine instruction.
Definition: MachineInstr.h:69
This class contains meta information specific to a module.
MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
MachineOperand class - Representation of each machine instruction operand.
static MachineOperand CreateImm(int64_t Val)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1729
void setOperand(unsigned I, MDNode *New)
Definition: Metadata.cpp:1389
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1381
unsigned getNumOperands() const
Definition: Metadata.cpp:1377
void addOperand(MDNode *M)
Definition: Metadata.cpp:1387
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
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:179
size_type size() const
Definition: SmallSet.h:161
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
static IntegerType * getInt32Ty(LLVMContext &C)
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
static ConstantAsMetadata * getConstant(Value *C)
Definition: Metadata.h:472
iterator_range< use_iterator > uses()
Definition: Value.h:376
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool applyDebugifyMetadata(Module &M, iterator_range< Module::iterator > Functions, StringRef Banner, std::function< bool(DIBuilder &, Function &)> ApplyToMF)
Add synthesized debug information to a module.
ModulePass * createDebugifyMachineModulePass()
Creates MIR Debugify pass.
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
#define N