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;
68 MissingDebugLoc
.clear();
71 for (Function::iterator FI
= F
.begin(), FE
= F
.end(); FI
!= FE
; ++FI
)
72 for (BasicBlock::iterator BI
= FI
->begin(), BE
= FI
->end();
74 if (!isa
<PHINode
>(BI
) && BI
->getDebugLoc().isUnknown())
75 MissingDebugLoc
.insert(BI
);
76 if (!isa
<DbgInfoIntrinsic
>(BI
)) continue;
79 if (DbgDeclareInst
*DDI
= dyn_cast
<DbgDeclareInst
>(BI
)) {
80 Addr
= DDI
->getAddress();
81 Node
= DDI
->getVariable();
82 } else if (DbgValueInst
*DVI
= dyn_cast
<DbgValueInst
>(BI
)) {
83 Addr
= DVI
->getValue();
84 Node
= DVI
->getVariable();
87 DbgVariables
.insert(Node
);
91 /// report - Report findings. This should be invoked after finalize.
92 void DebugInfoProbeImpl::report() {
93 if (!EnableDebugInfoProbe
) return;
94 if (NumDbgLineLost
|| NumDbgValueLost
) {
95 raw_ostream
*OutStream
= CreateInfoOutputFile();
97 *OutStream
<< NumDbgLineLost
98 << "\t times line number info lost by "
101 *OutStream
<< NumDbgValueLost
102 << "\t times variable info lost by "
110 /// finalize - Collect information after running an optimization pass. This
111 /// must be used after initialization.
112 void DebugInfoProbeImpl::finalize(Function
&F
) {
113 if (!EnableDebugInfoProbe
) return;
114 assert (TheFn
== &F
&& "Invalid function to measure!");
116 std::set
<MDNode
*>DbgVariables2
;
117 for (Function::iterator FI
= F
.begin(), FE
= F
.end(); FI
!= FE
; ++FI
)
118 for (BasicBlock::iterator BI
= FI
->begin(), BE
= FI
->end();
120 if (!isa
<PHINode
>(BI
) && BI
->getDebugLoc().isUnknown() &&
121 MissingDebugLoc
.count(BI
) == 0) {
123 DEBUG(dbgs() << "DebugInfoProbe (" << PassName
<< "): --- ");
124 DEBUG(BI
->print(dbgs()));
125 DEBUG(dbgs() << "\n");
127 if (!isa
<DbgInfoIntrinsic
>(BI
)) continue;
130 if (DbgDeclareInst
*DDI
= dyn_cast
<DbgDeclareInst
>(BI
)) {
131 Addr
= DDI
->getAddress();
132 Node
= DDI
->getVariable();
133 } else if (DbgValueInst
*DVI
= dyn_cast
<DbgValueInst
>(BI
)) {
134 Addr
= DVI
->getValue();
135 Node
= DVI
->getVariable();
138 DbgVariables2
.insert(Node
);
141 for (std::set
<MDNode
*>::iterator I
= DbgVariables
.begin(),
142 E
= DbgVariables
.end(); I
!= E
; ++I
) {
143 if (DbgVariables2
.count(*I
) == 0 && (*I
)->getNumOperands() >= 2) {
147 << "): Losing dbg info for variable: ";
148 if (MDString
*MDS
= dyn_cast_or_null
<MDString
>(
149 (*I
)->getOperand(2)))
150 dbgs() << MDS
->getString();
159 //===----------------------------------------------------------------------===//
162 DebugInfoProbe::DebugInfoProbe() {
163 pImpl
= new DebugInfoProbeImpl();
166 DebugInfoProbe::~DebugInfoProbe() {
170 /// initialize - Collect information before running an optimization pass.
171 void DebugInfoProbe::initialize(StringRef PName
, Function
&F
) {
172 pImpl
->initialize(PName
, F
);
175 /// finalize - Collect information after running an optimization pass. This
176 /// must be used after initialization.
177 void DebugInfoProbe::finalize(Function
&F
) {
181 /// report - Report findings. This should be invoked after finalize.
182 void DebugInfoProbe::report() {
186 //===----------------------------------------------------------------------===//
187 // DebugInfoProbeInfo
189 /// ~DebugInfoProbeInfo - Report data collected by all probes before deleting
191 DebugInfoProbeInfo::~DebugInfoProbeInfo() {
192 if (!EnableDebugInfoProbe
) return;
193 for (StringMap
<DebugInfoProbe
*>::iterator I
= Probes
.begin(),
194 E
= Probes
.end(); I
!= E
; ++I
) {
200 /// initialize - Collect information before running an optimization pass.
201 void DebugInfoProbeInfo::initialize(Pass
*P
, Function
&F
) {
202 if (!EnableDebugInfoProbe
) return;
203 if (P
->getAsPMDataManager())
206 StringMapEntry
<DebugInfoProbe
*> &Entry
=
207 Probes
.GetOrCreateValue(P
->getPassName());
208 DebugInfoProbe
*&Probe
= Entry
.getValue();
210 Probe
= new DebugInfoProbe();
211 Probe
->initialize(P
->getPassName(), F
);
214 /// finalize - Collect information after running an optimization pass. This
215 /// must be used after initialization.
216 void DebugInfoProbeInfo::finalize(Pass
*P
, Function
&F
) {
217 if (!EnableDebugInfoProbe
) return;
218 if (P
->getAsPMDataManager())
220 StringMapEntry
<DebugInfoProbe
*> &Entry
=
221 Probes
.GetOrCreateValue(P
->getPassName());
222 DebugInfoProbe
*&Probe
= Entry
.getValue();
223 assert (Probe
&& "DebugInfoProbe is not initialized!");