summaryrefslogtreecommitdiff
path: root/lib/VMCore/DebugInfoProbe.cpp
blob: d1275ff58caaa6c00b71905bdc68a5fdfc2bcb12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//===-- DebugInfoProbe.cpp - DebugInfo Probe ------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements DebugInfoProbe. This probe can be used by a pass
// manager to analyze how optimizer is treating debugging information.
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "debuginfoprobe"
#include "llvm/DebugInfoProbe.h"
#include "llvm/Function.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Metadata.h"
#include "llvm/PassManager.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLoc.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/StringRef.h"
#include <set>
#include <string>

using namespace llvm;

static cl::opt<bool>
EnableDebugInfoProbe("enable-debug-info-probe", cl::Hidden,
                     cl::desc("Enable debug info probe"));

// CreateInfoOutputFile - Return a file stream to print our output on.
namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }

//===----------------------------------------------------------------------===//
// DebugInfoProbeImpl - This class implements a interface to monitor
// how an optimization pass is preserving debugging information.

namespace llvm {

  class DebugInfoProbeImpl {
  public:
    DebugInfoProbeImpl() : NumDbgLineLost(0),NumDbgValueLost(0) {}
    void initialize(StringRef PName, Function &F);
    void finalize(Function &F);
    void report();
  private:
    unsigned NumDbgLineLost, NumDbgValueLost;
    std::string PassName;
    Function *TheFn;
    std::set<MDNode *> DbgVariables;
    std::set<Instruction *> MissingDebugLoc;
  };
}

//===----------------------------------------------------------------------===//
// DebugInfoProbeImpl

/// initialize - Collect information before running an optimization pass.
void DebugInfoProbeImpl::initialize(StringRef PName, Function &F) {
  if (!EnableDebugInfoProbe) return;
  PassName = PName;

  DbgVariables.clear();
  MissingDebugLoc.clear();
  TheFn = &F;

  for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
    for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); 
         BI != BE; ++BI) {
      if (!isa<PHINode>(BI) && BI->getDebugLoc().isUnknown())
        MissingDebugLoc.insert(BI);
      if (!isa<DbgInfoIntrinsic>(BI)) continue;
      Value *Addr = NULL;
      MDNode *Node = NULL;
      if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) {
        Addr = DDI->getAddress();
        Node = DDI->getVariable();
      } else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
        Addr = DVI->getValue();
        Node = DVI->getVariable();
      }
      if (Addr)
        DbgVariables.insert(Node);
    }
}

/// report - Report findings. This should be invoked after finalize.
void DebugInfoProbeImpl::report() {
  if (!EnableDebugInfoProbe) return;
  if (NumDbgLineLost || NumDbgValueLost) {
    raw_ostream *OutStream = CreateInfoOutputFile();
    if (NumDbgLineLost)
      *OutStream << NumDbgLineLost
                 << "\t times line number info lost by "
                 << PassName << "\n";
    if (NumDbgValueLost)
      *OutStream << NumDbgValueLost
                 << "\t times variable info lost by    "
                 << PassName << "\n";
    delete OutStream;
  }
  NumDbgLineLost = 0;
  NumDbgValueLost = 0;
}

/// finalize - Collect information after running an optimization pass. This
/// must be used after initialization.
void DebugInfoProbeImpl::finalize(Function &F) {
  if (!EnableDebugInfoProbe) return;
  assert (TheFn == &F && "Invalid function to measure!");

  std::set<MDNode *>DbgVariables2;
  for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
    for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); 
         BI != BE; ++BI) {
      if (!isa<PHINode>(BI) && BI->getDebugLoc().isUnknown() &&
          MissingDebugLoc.count(BI) == 0) {
        ++NumDbgLineLost;
        DEBUG(dbgs() << "DebugInfoProbe (" << PassName << "): --- ");
        DEBUG(BI->print(dbgs()));
        DEBUG(dbgs() << "\n");
      }
      if (!isa<DbgInfoIntrinsic>(BI)) continue;
      Value *Addr = NULL;
      MDNode *Node = NULL;
      if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI)) {
        Addr = DDI->getAddress();
        Node = DDI->getVariable();
      } else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
        Addr = DVI->getValue();
        Node = DVI->getVariable();
      }
      if (Addr)
        DbgVariables2.insert(Node);
    }

  for (std::set<MDNode *>::iterator I = DbgVariables.begin(), 
         E = DbgVariables.end(); I != E; ++I) {
    if (DbgVariables2.count(*I) == 0 && (*I)->getNumOperands() >= 2) {
      DEBUG(dbgs() 
            << "DebugInfoProbe("
            << PassName
            << "): Losing dbg info for variable: ";
            if (MDString *MDS = dyn_cast_or_null<MDString>(
                (*I)->getOperand(2)))
              dbgs() << MDS->getString();
            else
              dbgs() << "...";
            dbgs() << "\n");
      ++NumDbgValueLost;
    }
  }
}

//===----------------------------------------------------------------------===//
// DebugInfoProbe

DebugInfoProbe::DebugInfoProbe() {
  pImpl = new DebugInfoProbeImpl();
}

DebugInfoProbe::~DebugInfoProbe() {
  delete pImpl;
}

/// initialize - Collect information before running an optimization pass.
void DebugInfoProbe::initialize(StringRef PName, Function &F) {
  pImpl->initialize(PName, F);
}

/// finalize - Collect information after running an optimization pass. This
/// must be used after initialization.
void DebugInfoProbe::finalize(Function &F) {
  pImpl->finalize(F);
}

/// report - Report findings. This should be invoked after finalize.
void DebugInfoProbe::report() {
  pImpl->report();
}

//===----------------------------------------------------------------------===//
// DebugInfoProbeInfo

/// ~DebugInfoProbeInfo - Report data collected by all probes before deleting
/// them.
DebugInfoProbeInfo::~DebugInfoProbeInfo() {
  if (!EnableDebugInfoProbe) return;
    for (StringMap<DebugInfoProbe*>::iterator I = Probes.begin(),
           E = Probes.end(); I != E; ++I) {
      I->second->report();
      delete I->second;
    }
  }

/// initialize - Collect information before running an optimization pass.
void DebugInfoProbeInfo::initialize(Pass *P, Function &F) {
  if (!EnableDebugInfoProbe) return;
  if (P->getAsPMDataManager())
    return;

  StringMapEntry<DebugInfoProbe *> &Entry =
    Probes.GetOrCreateValue(P->getPassName());
  DebugInfoProbe *&Probe = Entry.getValue();
  if (!Probe)
    Probe = new DebugInfoProbe();
  Probe->initialize(P->getPassName(), F);
}

/// finalize - Collect information after running an optimization pass. This
/// must be used after initialization.
void DebugInfoProbeInfo::finalize(Pass *P, Function &F) {
  if (!EnableDebugInfoProbe) return;
  if (P->getAsPMDataManager())
    return;
  StringMapEntry<DebugInfoProbe *> &Entry =
    Probes.GetOrCreateValue(P->getPassName());
  DebugInfoProbe *&Probe = Entry.getValue();
  assert (Probe && "DebugInfoProbe is not initialized!");
  Probe->finalize(F);
}