1 //===-- DebugInfoProbe.cpp - DebugInfo Probe ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements DebugInfoProbe. This probe can be used by a pass
11 // manager to analyze how optimizer is treating debugging information.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "debuginfoprobe"
16 #include "llvm/DebugInfoProbe.h"
17 #include "llvm/Function.h"
18 #include "llvm/IntrinsicInst.h"
19 #include "llvm/Metadata.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/DebugLoc.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/ADT/StringRef.h"
32 EnableDebugInfoProbe("enable-debug-info-probe", cl::Hidden
,
33 cl::desc("Enable debug info probe"));
35 // CreateInfoOutputFile - Return a file stream to print our output on.
36 namespace llvm
{ extern raw_ostream
*CreateInfoOutputFile(); }
38 //===----------------------------------------------------------------------===//
39 // DebugInfoProbeImpl - This class implements a interface to monitor
40 // how an optimization pass is preserving debugging information.
44 class DebugInfoProbeImpl
{
46 DebugInfoProbeImpl() : NumDbgLineLost(0),NumDbgValueLost(0) {}
47 void initialize(StringRef PName
, Function
&F
);
48 void finalize(Function
&F
);
51 unsigned NumDbgLineLost
, NumDbgValueLost
;
54 std::set
<MDNode
*> DbgVariables
;
55 std::set
<Instruction
*> MissingDebugLoc
;
59 //===----------------------------------------------------------------------===//
62 /// initialize - Collect information before running an optimization pass.
63 void DebugInfoProbeImpl::initialize(StringRef PName
, Function
&F
) {
64 if (!EnableDebugInfoProbe
) return;
70 for (Function::iterator FI
= F
.begin(), FE
= F
.end(); FI
!= FE
; ++FI
)
71 for (BasicBlock::iterator BI
= FI
->begin(), BE
= FI
->end();
73 if (BI
->getDebugLoc().isUnknown())
74 MissingDebugLoc
.insert(BI
);
75 if (!isa
<DbgInfoIntrinsic
>(BI
)) continue;
78 if (DbgDeclareInst
*DDI
= dyn_cast
<DbgDeclareInst
>(BI
)) {
79 Addr
= DDI
->getAddress();
80 Node
= DDI
->getVariable();
81 } else if (DbgValueInst
*DVI
= dyn_cast
<DbgValueInst
>(BI
)) {
82 Addr
= DVI
->getValue();
83 Node
= DVI
->getVariable();
86 DbgVariables
.insert(Node
);
90 /// report - Report findings. This should be invoked after finalize.
91 void DebugInfoProbeImpl::report() {
92 if (!EnableDebugInfoProbe
) return;
93 if (NumDbgLineLost
|| NumDbgValueLost
) {
94 raw_ostream
*OutStream
= CreateInfoOutputFile();
96 *OutStream
<< NumDbgLineLost
97 << "\t times line number info lost by "
100 *OutStream
<< NumDbgValueLost
101 << "\t times variable info lost by "
109 /// finalize - Collect information after running an optimization pass. This
110 /// must be used after initialization.
111 void DebugInfoProbeImpl::finalize(Function
&F
) {
112 if (!EnableDebugInfoProbe
) return;
113 assert (TheFn
== &F
&& "Invalid function to measure!");
115 std::set
<MDNode
*>DbgVariables2
;
116 for (Function::iterator FI
= F
.begin(), FE
= F
.end(); FI
!= FE
; ++FI
)
117 for (BasicBlock::iterator BI
= FI
->begin(), BE
= FI
->end();
119 if (BI
->getDebugLoc().isUnknown() &&
120 MissingDebugLoc
.count(BI
) == 0) {
122 DEBUG(dbgs() << "DebugInfoProbe (" << PassName
<< "): --- ");
123 DEBUG(BI
->print(dbgs()));
124 DEBUG(dbgs() << "\n");
126 if (!isa
<DbgInfoIntrinsic
>(BI
)) continue;
129 if (DbgDeclareInst
*DDI
= dyn_cast
<DbgDeclareInst
>(BI
)) {
130 Addr
= DDI
->getAddress();
131 Node
= DDI
->getVariable();
132 } else if (DbgValueInst
*DVI
= dyn_cast
<DbgValueInst
>(BI
)) {
133 Addr
= DVI
->getValue();
134 Node
= DVI
->getVariable();
137 DbgVariables2
.insert(Node
);
140 for (std::set
<MDNode
*>::iterator I
= DbgVariables
.begin(),
141 E
= DbgVariables
.end(); I
!= E
; ++I
) {
142 if (DbgVariables2
.count(*I
) == 0 && (*I
)->getNumOperands() >= 2) {
146 << "): Losing dbg info for variable: ";
147 if (MDString
*MDS
= dyn_cast_or_null
<MDString
>(
148 (*I
)->getOperand(2)))
149 dbgs() << MDS
->getString();
158 //===----------------------------------------------------------------------===//
161 DebugInfoProbe::DebugInfoProbe() {
162 pImpl
= new DebugInfoProbeImpl();
165 DebugInfoProbe::~DebugInfoProbe() {
169 /// initialize - Collect information before running an optimization pass.
170 void DebugInfoProbe::initialize(StringRef PName
, Function
&F
) {
171 pImpl
->initialize(PName
, F
);
174 /// finalize - Collect information after running an optimization pass. This
175 /// must be used after initialization.
176 void DebugInfoProbe::finalize(Function
&F
) {
180 /// report - Report findings. This should be invoked after finalize.
181 void DebugInfoProbe::report() {
185 //===----------------------------------------------------------------------===//
186 // DebugInfoProbeInfo
188 /// ~DebugInfoProbeInfo - Report data collected by all probes before deleting
190 DebugInfoProbeInfo::~DebugInfoProbeInfo() {
191 if (!EnableDebugInfoProbe
) return;
192 for (StringMap
<DebugInfoProbe
*>::iterator I
= Probes
.begin(),
193 E
= Probes
.end(); I
!= E
; ++I
) {
199 /// initialize - Collect information before running an optimization pass.
200 void DebugInfoProbeInfo::initialize(Pass
*P
, Function
&F
) {
201 if (!EnableDebugInfoProbe
) return;
202 if (P
->getAsPMDataManager())
205 StringMapEntry
<DebugInfoProbe
*> &Entry
=
206 Probes
.GetOrCreateValue(P
->getPassName());
207 DebugInfoProbe
*&Probe
= Entry
.getValue();
209 Probe
= new DebugInfoProbe();
210 Probe
->initialize(P
->getPassName(), F
);
213 /// finalize - Collect information after running an optimization pass. This
214 /// must be used after initialization.
215 void DebugInfoProbeInfo::finalize(Pass
*P
, Function
&F
) {
216 if (!EnableDebugInfoProbe
) return;
217 if (P
->getAsPMDataManager())
219 StringMapEntry
<DebugInfoProbe
*> &Entry
=
220 Probes
.GetOrCreateValue(P
->getPassName());
221 DebugInfoProbe
*&Probe
= Entry
.getValue();
222 assert (Probe
&& "DebugInfoProbe is not initialized!");