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<unsigned> LineNos;
55 std::set<MDNode *> DbgVariables;
59 //===----------------------------------------------------------------------===//
62 static void collect(Function &F, std::set<unsigned> &Lines) {
63 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
64 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
66 const DebugLoc &DL = BI->getDebugLoc();
68 if (!DL.isUnknown()) {
69 if (MDNode *N = DL.getInlinedAt(F.getContext()))
70 LineNo = DebugLoc::getFromDILocation(N).getLine();
72 LineNo = DL.getLine();
79 /// initialize - Collect information before running an optimization pass.
80 void DebugInfoProbeImpl::initialize(StringRef PName, Function &F) {
81 if (!EnableDebugInfoProbe) return;
89 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
90 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
92 if (!isa<DbgInfoIntrinsic>(BI)) continue;
95 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) {
96 Addr = DDI->getAddress();
97 Node = DDI->getVariable();
98 } else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
99 Addr = DVI->getValue();
100 Node = DVI->getVariable();
103 DbgVariables.insert(Node);
107 /// report - Report findings. This should be invoked after finalize.
108 void DebugInfoProbeImpl::report() {
109 if (!EnableDebugInfoProbe) return;
110 if (NumDbgLineLost || NumDbgValueLost) {
111 raw_ostream *OutStream = CreateInfoOutputFile();
113 *OutStream << NumDbgLineLost
114 << "\t times line number info lost by "
117 *OutStream << NumDbgValueLost
118 << "\t times variable info lost by "
126 /// finalize - Collect information after running an optimization pass. This
127 /// must be used after initialization.
128 void DebugInfoProbeImpl::finalize(Function &F) {
129 if (!EnableDebugInfoProbe) return;
130 std::set<unsigned> LineNos2;
131 collect(F, LineNos2);
132 assert (TheFn == &F && "Invalid function to measure!");
134 for (std::set<unsigned>::iterator I = LineNos.begin(),
135 E = LineNos.end(); I != E; ++I) {
136 unsigned LineNo = *I;
137 if (LineNos2.count(LineNo) == 0) {
138 DEBUG(dbgs() << "DebugInfoProbe: Losing dbg info intrinsic at line " << LineNo << "\n");
143 std::set<MDNode *>DbgVariables2;
144 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
145 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
147 if (!isa<DbgInfoIntrinsic>(BI)) continue;
150 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) {
151 Addr = DDI->getAddress();
152 Node = DDI->getVariable();
153 } else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
154 Addr = DVI->getValue();
155 Node = DVI->getVariable();
158 DbgVariables2.insert(Node);
161 for (std::set<MDNode *>::iterator I = DbgVariables.begin(),
162 E = DbgVariables.end(); I != E; ++I) {
163 if (DbgVariables2.count(*I) == 0) {
164 DEBUG(dbgs() << "DebugInfoProbe: Losing dbg info for variable: ");
165 DEBUG((*I)->print(dbgs()));
171 //===----------------------------------------------------------------------===//
174 DebugInfoProbe::DebugInfoProbe() {
175 pImpl = new DebugInfoProbeImpl();
178 DebugInfoProbe::~DebugInfoProbe() {
182 /// initialize - Collect information before running an optimization pass.
183 void DebugInfoProbe::initialize(StringRef PName, Function &F) {
184 pImpl->initialize(PName, F);
187 /// finalize - Collect information after running an optimization pass. This
188 /// must be used after initialization.
189 void DebugInfoProbe::finalize(Function &F) {
193 /// report - Report findings. This should be invoked after finalize.
194 void DebugInfoProbe::report() {
198 //===----------------------------------------------------------------------===//
199 // DebugInfoProbeInfo
201 /// ~DebugInfoProbeInfo - Report data collected by all probes before deleting
203 DebugInfoProbeInfo::~DebugInfoProbeInfo() {
204 if (!EnableDebugInfoProbe) return;
205 for (StringMap<DebugInfoProbe*>::iterator I = Probes.begin(),
206 E = Probes.end(); I != E; ++I) {
212 /// initialize - Collect information before running an optimization pass.
213 void DebugInfoProbeInfo::initialize(Pass *P, Function &F) {
214 if (!EnableDebugInfoProbe) return;
215 if (P->getAsPMDataManager())
218 StringMapEntry<DebugInfoProbe *> &Entry =
219 Probes.GetOrCreateValue(P->getPassName());
220 DebugInfoProbe *&Probe = Entry.getValue();
222 Probe = new DebugInfoProbe();
223 Probe->initialize(P->getPassName(), F);
226 /// finalize - Collect information after running an optimization pass. This
227 /// must be used after initialization.
228 void DebugInfoProbeInfo::finalize(Pass *P, Function &F) {
229 if (!EnableDebugInfoProbe) return;
230 if (P->getAsPMDataManager())
232 StringMapEntry<DebugInfoProbe *> &Entry =
233 Probes.GetOrCreateValue(P->getPassName());
234 DebugInfoProbe *&Probe = Entry.getValue();
235 assert (Probe && "DebugInfoProbe is not initialized!");