Introduce DebugInfoProbe. This is used to monitor how llvm optimizer is treating...
[oota-llvm.git] / lib / VMCore / DebugInfoProbe.cpp
1 //===-- DebugInfoProbe.cpp - DebugInfo Probe ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements DebugInfoProbe. This probe can be used by a pass
11 // manager to analyze how optimizer is treating debugging information.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
26 #include <set>
27 #include <string>
28
29 using namespace llvm;
30
31 static cl::opt<bool>
32 EnableDebugInfoProbe("enable-debug-info-probe", cl::Hidden,
33                      cl::desc("Enable debug info probe"));
34
35 // CreateInfoOutputFile - Return a file stream to print our output on.
36 namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
37
38 //===----------------------------------------------------------------------===//
39 // DebugInfoProbeImpl - This class implements a interface to monitor
40 // how an optimization pass is preserving debugging information.
41
42 namespace llvm {
43
44   class DebugInfoProbeImpl {
45   public:
46     DebugInfoProbeImpl() : NumDbgLineLost(0),NumDbgValueLost(0) {}
47     void initialize(StringRef PName, Function &F);
48     void finalize(Function &F);
49     void report();
50   private:
51     unsigned NumDbgLineLost, NumDbgValueLost;
52     std::string PassName;
53     Function *TheFn;
54     std::set<unsigned> LineNos;
55     std::set<MDNode *> DbgVariables;
56   };
57 }
58
59 //===----------------------------------------------------------------------===//
60 // DebugInfoProbeImpl
61
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(); 
65          BI != BE; ++BI) {
66       const DebugLoc &DL = BI->getDebugLoc();
67       unsigned LineNo = 0;
68       if (!DL.isUnknown()) {
69         if (MDNode *N = DL.getInlinedAt(F.getContext()))
70           LineNo = DebugLoc::getFromDILocation(N).getLine();
71         else
72           LineNo = DL.getLine();
73
74         Lines.insert(LineNo);
75       }
76     }
77 }
78
79 /// initialize - Collect information before running an optimization pass.
80 void DebugInfoProbeImpl::initialize(StringRef PName, Function &F) {
81   if (!EnableDebugInfoProbe) return;
82   PassName = PName;
83   NumDbgLineLost = 0;
84   NumDbgValueLost = 0;
85
86   LineNos.clear();
87   DbgVariables.clear();
88   TheFn = &F;
89   collect(F, LineNos);
90
91   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
92     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); 
93          BI != BE; ++BI) {
94       if (!isa<DbgInfoIntrinsic>(BI)) continue;
95       Value *Addr = NULL;
96       MDNode *Node = NULL;
97       if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) {
98         Addr = DDI->getAddress();
99         Node = DDI->getVariable();
100       } else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
101         Addr = DVI->getValue();
102         Node = DVI->getVariable();
103       }
104       if (Addr) continue;
105       DbgVariables.insert(Node);
106     }
107 }
108
109 /// report - Report findings. This should be invoked after finalize.
110 void DebugInfoProbeImpl::report() {
111   if (!EnableDebugInfoProbe) return;
112   if (NumDbgLineLost || NumDbgValueLost) {
113     raw_ostream *OutStream = CreateInfoOutputFile();
114     if (NumDbgLineLost)
115       *OutStream << NumDbgLineLost
116                  << "\t times line number info lost by "
117                  << PassName << "\n";
118     if (NumDbgValueLost)
119       *OutStream << NumDbgValueLost
120                  << "\t times variable info lost by    "
121                  << PassName << "\n";
122     delete OutStream;
123   }
124 }
125
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!");
133
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() << "Losing dbg info intrinsic at line " << LineNo << " ");
139       ++NumDbgLineLost;
140     }
141   }
142
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(); 
146          BI != BE; ++BI) {
147       if (!isa<DbgInfoIntrinsic>(BI)) continue;
148       Value *Addr = NULL;
149       MDNode *Node = NULL;
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();
156       }
157       if (Addr) continue;
158       DbgVariables2.insert(Node);
159     }
160
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() << "Losing dbg info for variable: ");
165       DEBUG((*I)->print(dbgs()));
166       ++NumDbgValueLost;
167     }
168   }
169 }
170
171 //===----------------------------------------------------------------------===//
172 // DebugInfoProbe
173
174 DebugInfoProbe::DebugInfoProbe() {
175   pImpl = new DebugInfoProbeImpl();
176 }
177
178 DebugInfoProbe::~DebugInfoProbe() {
179   delete pImpl;
180 }
181
182 /// initialize - Collect information before running an optimization pass.
183 void DebugInfoProbe::initialize(StringRef PName, Function &F) {
184   pImpl->initialize(PName, F);
185 }
186
187 /// finalize - Collect information after running an optimization pass. This
188 /// must be used after initialization.
189 void DebugInfoProbe::finalize(Function &F) {
190   pImpl->finalize(F);
191 }
192
193 /// report - Report findings. This should be invoked after finalize.
194 void DebugInfoProbe::report() {
195   pImpl->report();
196 }
197
198 //===----------------------------------------------------------------------===//
199 // DebugInfoProbeInfo
200
201 /// ~DebugInfoProbeInfo - Report data collected by all probes before deleting
202 /// them.
203 DebugInfoProbeInfo::~DebugInfoProbeInfo() {
204   if (!EnableDebugInfoProbe) return;
205     for (StringMap<DebugInfoProbe*>::iterator I = Probes.begin(),
206            E = Probes.end(); I != E; ++I) {
207       I->second->report();
208       delete I->second;
209     }
210   }
211
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())
216     return;
217
218   StringMapEntry<DebugInfoProbe *> &Entry =
219     Probes.GetOrCreateValue(P->getPassName());
220   DebugInfoProbe *&Probe = Entry.getValue();
221   if (!Probe)
222     Probe = new DebugInfoProbe();
223   Probe->initialize(P->getPassName(), F);
224 }
225
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())
231     return;
232   StringMapEntry<DebugInfoProbe *> &Entry =
233     Probes.GetOrCreateValue(P->getPassName());
234   DebugInfoProbe *&Probe = Entry.getValue();
235   assert (Probe && "DebugInfoProbe is not initialized!");
236   Probe->finalize(F);
237 }