In debug output, clearly list new instructions without DebugLoc.
[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<MDNode *> DbgVariables;
55     std::set<Instruction *> MissingDebugLoc;
56   };
57 }
58
59 //===----------------------------------------------------------------------===//
60 // DebugInfoProbeImpl
61
62 /// initialize - Collect information before running an optimization pass.
63 void DebugInfoProbeImpl::initialize(StringRef PName, Function &F) {
64   if (!EnableDebugInfoProbe) return;
65   PassName = PName;
66
67   DbgVariables.clear();
68   TheFn = &F;
69
70   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
71     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); 
72          BI != BE; ++BI) {
73       if (BI->getDebugLoc().isUnknown())
74         MissingDebugLoc.insert(BI);
75       if (!isa<DbgInfoIntrinsic>(BI)) continue;
76       Value *Addr = NULL;
77       MDNode *Node = NULL;
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();
84       }
85       if (Addr)
86         DbgVariables.insert(Node);
87     }
88 }
89
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();
95     if (NumDbgLineLost)
96       *OutStream << NumDbgLineLost
97                  << "\t times line number info lost by "
98                  << PassName << "\n";
99     if (NumDbgValueLost)
100       *OutStream << NumDbgValueLost
101                  << "\t times variable info lost by    "
102                  << PassName << "\n";
103     delete OutStream;
104   }
105   NumDbgLineLost = 0;
106   NumDbgValueLost = 0;
107 }
108
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!");
114
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(); 
118          BI != BE; ++BI) {
119       if (BI->getDebugLoc().isUnknown() &&
120           MissingDebugLoc.count(BI) == 0) {
121         ++NumDbgLineLost;
122         DEBUG(dbgs() << "DebugInfoProbe (" << PassName << "): --- ");
123         DEBUG(BI->print(dbgs()));
124         DEBUG(dbgs() << "\n");
125       }
126       if (!isa<DbgInfoIntrinsic>(BI)) continue;
127       Value *Addr = NULL;
128       MDNode *Node = NULL;
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();
135       }
136       if (Addr)
137         DbgVariables2.insert(Node);
138     }
139
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) {
143       DEBUG(dbgs() 
144             << "DebugInfoProbe("
145             << PassName
146             << "): Losing dbg info for variable: ";
147             if (MDString *MDS = dyn_cast_or_null<MDString>(
148                 (*I)->getOperand(2)))
149               dbgs() << MDS->getString();
150             else
151               dbgs() << "...";
152             dbgs() << "\n");
153       ++NumDbgValueLost;
154     }
155   }
156 }
157
158 //===----------------------------------------------------------------------===//
159 // DebugInfoProbe
160
161 DebugInfoProbe::DebugInfoProbe() {
162   pImpl = new DebugInfoProbeImpl();
163 }
164
165 DebugInfoProbe::~DebugInfoProbe() {
166   delete pImpl;
167 }
168
169 /// initialize - Collect information before running an optimization pass.
170 void DebugInfoProbe::initialize(StringRef PName, Function &F) {
171   pImpl->initialize(PName, F);
172 }
173
174 /// finalize - Collect information after running an optimization pass. This
175 /// must be used after initialization.
176 void DebugInfoProbe::finalize(Function &F) {
177   pImpl->finalize(F);
178 }
179
180 /// report - Report findings. This should be invoked after finalize.
181 void DebugInfoProbe::report() {
182   pImpl->report();
183 }
184
185 //===----------------------------------------------------------------------===//
186 // DebugInfoProbeInfo
187
188 /// ~DebugInfoProbeInfo - Report data collected by all probes before deleting
189 /// them.
190 DebugInfoProbeInfo::~DebugInfoProbeInfo() {
191   if (!EnableDebugInfoProbe) return;
192     for (StringMap<DebugInfoProbe*>::iterator I = Probes.begin(),
193            E = Probes.end(); I != E; ++I) {
194       I->second->report();
195       delete I->second;
196     }
197   }
198
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())
203     return;
204
205   StringMapEntry<DebugInfoProbe *> &Entry =
206     Probes.GetOrCreateValue(P->getPassName());
207   DebugInfoProbe *&Probe = Entry.getValue();
208   if (!Probe)
209     Probe = new DebugInfoProbe();
210   Probe->initialize(P->getPassName(), F);
211 }
212
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())
218     return;
219   StringMapEntry<DebugInfoProbe *> &Entry =
220     Probes.GetOrCreateValue(P->getPassName());
221   DebugInfoProbe *&Probe = Entry.getValue();
222   assert (Probe && "DebugInfoProbe is not initialized!");
223   Probe->finalize(F);
224 }