LLVM: lib/CodeGen/MachineDebugify.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
26
27#define DEBUG_TYPE "mir-debugify"
28
29using namespace llvm;
30
31namespace {
32bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
35 if (!MaybeMF)
36 return false;
39
41 assert(SP && "IR Debugify just created it?");
42
43 Module &M = *F.getParent();
45
46 unsigned NextLine = SP->getLine();
49
50
51
52
54 }
55 }
56
57
58
59
60
61
62
63
64
71 if (!DVR.isDbgValue())
72 continue;
73 unsigned Line = DVR.getDebugLoc().getLine();
74 assert(Line != 0 && "debugify should not insert line 0 locations");
75 Line2Var[Line] = DVR.getVariable();
76 if (!EarliestDVR || Line < EarliestDVR->getDebugLoc().getLine())
77 EarliestDVR = &DVR;
78 Expr = DVR.getExpression();
79 }
80 }
81 }
82 if (Line2Var.empty())
83 return true;
84
85
86
87
90 const MCInstrDesc &DbgValDesc = TII.get(TargetOpcode::DBG_VALUE);
93 for (auto I = MBB.begin(), E = MBB.end(); I != E;) {
95 ++I;
96
97
98 if (MI.isDebugInstr())
99 continue;
100
101
102 if (MI.isTerminator())
103 continue;
104
105
106 auto InsertBeforeIt = MI.isPHI() ? FirstNonPHIIt : I;
107
108
109 unsigned Line = MI.getDebugLoc().getLine();
110 auto It = Line2Var.find(Line);
111 if (It == Line2Var.end()) {
113 It = Line2Var.find(Line);
115 }
117 assert(LocalVar && "No variable for current line?");
118 VarSet.insert(LocalVar);
119
120
123 if (MO.getReg())
126 BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
127 false, *MO, LocalVar, Expr);
128
129
130 if (RegDefs.empty()) {
132 BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
133 false, ImmOp, LocalVar, Expr);
134 }
135 }
136 }
137
138
139
140 NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
142 if (!NMD) {
143 NMD = M.getOrInsertNamedMetadata("llvm.mir.debugify");
144 auto addDebugifyOperand = [&](unsigned N) {
147 };
148
149 addDebugifyOperand(NextLine - 1);
150
151 addDebugifyOperand(VarSet.size());
152 } else {
154 "llvm.mir.debugify should have exactly 2 operands!");
155 auto setDebugifyOperand = [&](unsigned Idx, unsigned N) {
157 ConstantInt::get(Int32Ty, N))));
158 };
159 auto getDebugifyOperand = [&](unsigned Idx) {
161 ->getZExtValue();
162 };
163
164 setDebugifyOperand(0, NextLine - 1);
165
166 auto OldNumVars = getDebugifyOperand(1);
167 setDebugifyOperand(1, OldNumVars + VarSet.size());
168 }
169
170 return true;
171}
172
173
174
175struct DebugifyMachineModule : public ModulePass {
176 bool runOnModule(Module &M) override {
177
178 assert(!M.getNamedMetadata("llvm.mir.debugify") &&
179 "llvm.mir.debugify metadata already exists! Strip it first");
181 getAnalysis().getMMI();
183 M, M.functions(),
185 return applyDebugifyMetadataToMachineFunction(MMI, DIB, F);
186 });
187 }
188
190
191 void getAnalysisUsage(AnalysisUsage &AU) const override {
195 }
196
197 static char ID;
198};
199char DebugifyMachineModule::ID = 0;
200
201}
202
204 "Machine Debugify Module", false, false)
207
209 return new DebugifyMachineModule();
210}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const TargetInstrInfo & TII
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first DebugLoc that has line number information, given a range of instructions.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
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.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
LLVM Basic Block Representation.
Subprogram description. Uses SubclassData1.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI unsigned getLine() const
iterator find(const_arg_type_t< KeyT > Val)
Class to represent integer types.
This is an important class for using LLVM in a threaded context.
Describe properties that are true of each instruction in the target description file.
const MDOperand & getOperand(unsigned I) const
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
MachineInstrBundleIterator< MachineInstr > iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Representation of each machine instruction.
This class contains meta information specific to a module.
LLVM_ABI 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...
A Module instance is used to store all the information related to an LLVM module.
LLVM_ABI void setOperand(unsigned I, MDNode *New)
LLVM_ABI MDNode * getOperand(unsigned i) const
LLVM_ABI unsigned getNumOperands() const
LLVM_ABI void addOperand(MDNode *M)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
LLVM_ABI 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_ABI ModulePass * createDebugifyMachineModulePass()
Creates MIR Debugify pass.
Definition MachineDebugify.cpp:208
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.