[Modules] Remove potential ODR violations by sinking the DEBUG_TYPE
[oota-llvm.git] / lib / CodeGen / LexicalScopes.cpp
1 //===- LexicalScopes.cpp - Collecting lexical scope info ------------------===//
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 LexicalScopes analysis.
11 //
12 // This pass collects lexical scope information and maps machine instructions
13 // to respective lexical scopes.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/CodeGen/LexicalScopes.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FormattedStream.h"
25 using namespace llvm;
26
27 #define DEBUG_TYPE "lexicalscopes"
28
29 /// ~LexicalScopes - final cleanup after ourselves.
30 LexicalScopes::~LexicalScopes() { reset(); }
31
32 /// reset - Reset the instance so that it's prepared for another function.
33 void LexicalScopes::reset() {
34   MF = nullptr;
35   CurrentFnLexicalScope = nullptr;
36   DeleteContainerSeconds(LexicalScopeMap);
37   DeleteContainerSeconds(AbstractScopeMap);
38   InlinedLexicalScopeMap.clear();
39   AbstractScopesList.clear();
40 }
41
42 /// initialize - Scan machine function and constuct lexical scope nest.
43 void LexicalScopes::initialize(const MachineFunction &Fn) {
44   reset();
45   MF = &Fn;
46   SmallVector<InsnRange, 4> MIRanges;
47   DenseMap<const MachineInstr *, LexicalScope *> MI2ScopeMap;
48   extractLexicalScopes(MIRanges, MI2ScopeMap);
49   if (CurrentFnLexicalScope) {
50     constructScopeNest(CurrentFnLexicalScope);
51     assignInstructionRanges(MIRanges, MI2ScopeMap);
52   }
53 }
54
55 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
56 /// for the given machine function.
57 void LexicalScopes::extractLexicalScopes(
58     SmallVectorImpl<InsnRange> &MIRanges,
59     DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
60
61   // Scan each instruction and create scopes. First build working set of scopes.
62   for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
63        ++I) {
64     const MachineInstr *RangeBeginMI = nullptr;
65     const MachineInstr *PrevMI = nullptr;
66     DebugLoc PrevDL;
67     for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
68          II != IE; ++II) {
69       const MachineInstr *MInsn = II;
70
71       // Check if instruction has valid location information.
72       const DebugLoc MIDL = MInsn->getDebugLoc();
73       if (MIDL.isUnknown()) {
74         PrevMI = MInsn;
75         continue;
76       }
77
78       // If scope has not changed then skip this instruction.
79       if (MIDL == PrevDL) {
80         PrevMI = MInsn;
81         continue;
82       }
83
84       // Ignore DBG_VALUE. It does not contribute to any instruction in output.
85       if (MInsn->isDebugValue())
86         continue;
87
88       if (RangeBeginMI) {
89         // If we have already seen a beginning of an instruction range and
90         // current instruction scope does not match scope of first instruction
91         // in this range then create a new instruction range.
92         InsnRange R(RangeBeginMI, PrevMI);
93         MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
94         MIRanges.push_back(R);
95       }
96
97       // This is a beginning of a new instruction range.
98       RangeBeginMI = MInsn;
99
100       // Reset previous markers.
101       PrevMI = MInsn;
102       PrevDL = MIDL;
103     }
104
105     // Create last instruction range.
106     if (RangeBeginMI && PrevMI && !PrevDL.isUnknown()) {
107       InsnRange R(RangeBeginMI, PrevMI);
108       MIRanges.push_back(R);
109       MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
110     }
111   }
112 }
113
114 /// findLexicalScope - Find lexical scope, either regular or inlined, for the
115 /// given DebugLoc. Return NULL if not found.
116 LexicalScope *LexicalScopes::findLexicalScope(DebugLoc DL) {
117   MDNode *Scope = nullptr;
118   MDNode *IA = nullptr;
119   DL.getScopeAndInlinedAt(Scope, IA, MF->getFunction()->getContext());
120   if (!Scope)
121     return nullptr;
122
123   // The scope that we were created with could have an extra file - which
124   // isn't what we care about in this case.
125   DIDescriptor D = DIDescriptor(Scope);
126   if (D.isLexicalBlockFile())
127     Scope = DILexicalBlockFile(Scope).getScope();
128
129   if (IA)
130     return InlinedLexicalScopeMap.lookup(DebugLoc::getFromDILocation(IA));
131   return LexicalScopeMap.lookup(Scope);
132 }
133
134 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
135 /// not available then create new lexical scope.
136 LexicalScope *LexicalScopes::getOrCreateLexicalScope(DebugLoc DL) {
137   MDNode *Scope = nullptr;
138   MDNode *InlinedAt = nullptr;
139   DL.getScopeAndInlinedAt(Scope, InlinedAt, MF->getFunction()->getContext());
140
141   if (InlinedAt) {
142     // Create an abstract scope for inlined function.
143     getOrCreateAbstractScope(Scope);
144     // Create an inlined scope for inlined function.
145     return getOrCreateInlinedScope(Scope, InlinedAt);
146   }
147
148   return getOrCreateRegularScope(Scope);
149 }
150
151 /// getOrCreateRegularScope - Find or create a regular lexical scope.
152 LexicalScope *LexicalScopes::getOrCreateRegularScope(MDNode *Scope) {
153   DIDescriptor D = DIDescriptor(Scope);
154   if (D.isLexicalBlockFile()) {
155     Scope = DILexicalBlockFile(Scope).getScope();
156     D = DIDescriptor(Scope);
157   }
158
159   LexicalScope *WScope = LexicalScopeMap.lookup(Scope);
160   if (WScope)
161     return WScope;
162
163   LexicalScope *Parent = nullptr;
164   if (D.isLexicalBlock())
165     Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope));
166   WScope = new LexicalScope(Parent, DIDescriptor(Scope), nullptr, false);
167   LexicalScopeMap.insert(std::make_pair(Scope, WScope));
168   if (!Parent && DIDescriptor(Scope).isSubprogram() &&
169       DISubprogram(Scope).describes(MF->getFunction()))
170     CurrentFnLexicalScope = WScope;
171
172   return WScope;
173 }
174
175 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
176 LexicalScope *LexicalScopes::getOrCreateInlinedScope(MDNode *Scope,
177                                                      MDNode *InlinedAt) {
178   LexicalScope *InlinedScope = LexicalScopeMap.lookup(InlinedAt);
179   if (InlinedScope)
180     return InlinedScope;
181
182   DebugLoc InlinedLoc = DebugLoc::getFromDILocation(InlinedAt);
183   InlinedScope = new LexicalScope(getOrCreateLexicalScope(InlinedLoc),
184                                   DIDescriptor(Scope), InlinedAt, false);
185   InlinedLexicalScopeMap[InlinedLoc] = InlinedScope;
186   LexicalScopeMap[InlinedAt] = InlinedScope;
187   return InlinedScope;
188 }
189
190 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
191 LexicalScope *LexicalScopes::getOrCreateAbstractScope(const MDNode *N) {
192   assert(N && "Invalid Scope encoding!");
193
194   DIDescriptor Scope(N);
195   if (Scope.isLexicalBlockFile())
196     Scope = DILexicalBlockFile(Scope).getScope();
197   LexicalScope *AScope = AbstractScopeMap.lookup(N);
198   if (AScope)
199     return AScope;
200
201   LexicalScope *Parent = nullptr;
202   if (Scope.isLexicalBlock()) {
203     DILexicalBlock DB(N);
204     DIDescriptor ParentDesc = DB.getContext();
205     Parent = getOrCreateAbstractScope(ParentDesc);
206   }
207   AScope = new LexicalScope(Parent, DIDescriptor(N), nullptr, true);
208   AbstractScopeMap[N] = AScope;
209   if (DIDescriptor(N).isSubprogram())
210     AbstractScopesList.push_back(AScope);
211   return AScope;
212 }
213
214 /// constructScopeNest
215 void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
216   assert(Scope && "Unable to calculate scope dominance graph!");
217   SmallVector<LexicalScope *, 4> WorkStack;
218   WorkStack.push_back(Scope);
219   unsigned Counter = 0;
220   while (!WorkStack.empty()) {
221     LexicalScope *WS = WorkStack.back();
222     const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
223     bool visitedChildren = false;
224     for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
225                                                          SE = Children.end();
226          SI != SE; ++SI) {
227       LexicalScope *ChildScope = *SI;
228       if (!ChildScope->getDFSOut()) {
229         WorkStack.push_back(ChildScope);
230         visitedChildren = true;
231         ChildScope->setDFSIn(++Counter);
232         break;
233       }
234     }
235     if (!visitedChildren) {
236       WorkStack.pop_back();
237       WS->setDFSOut(++Counter);
238     }
239   }
240 }
241
242 /// assignInstructionRanges - Find ranges of instructions covered by each
243 /// lexical scope.
244 void LexicalScopes::assignInstructionRanges(
245     SmallVectorImpl<InsnRange> &MIRanges,
246     DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
247
248   LexicalScope *PrevLexicalScope = nullptr;
249   for (SmallVectorImpl<InsnRange>::const_iterator RI = MIRanges.begin(),
250                                                   RE = MIRanges.end();
251        RI != RE; ++RI) {
252     const InsnRange &R = *RI;
253     LexicalScope *S = MI2ScopeMap.lookup(R.first);
254     assert(S && "Lost LexicalScope for a machine instruction!");
255     if (PrevLexicalScope && !PrevLexicalScope->dominates(S))
256       PrevLexicalScope->closeInsnRange(S);
257     S->openInsnRange(R.first);
258     S->extendInsnRange(R.second);
259     PrevLexicalScope = S;
260   }
261
262   if (PrevLexicalScope)
263     PrevLexicalScope->closeInsnRange();
264 }
265
266 /// getMachineBasicBlocks - Populate given set using machine basic blocks which
267 /// have machine instructions that belong to lexical scope identified by
268 /// DebugLoc.
269 void LexicalScopes::getMachineBasicBlocks(
270     DebugLoc DL, SmallPtrSet<const MachineBasicBlock *, 4> &MBBs) {
271   MBBs.clear();
272   LexicalScope *Scope = getOrCreateLexicalScope(DL);
273   if (!Scope)
274     return;
275
276   if (Scope == CurrentFnLexicalScope) {
277     for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
278          ++I)
279       MBBs.insert(I);
280     return;
281   }
282
283   SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges();
284   for (SmallVectorImpl<InsnRange>::iterator I = InsnRanges.begin(),
285                                             E = InsnRanges.end();
286        I != E; ++I) {
287     InsnRange &R = *I;
288     MBBs.insert(R.first->getParent());
289   }
290 }
291
292 /// dominates - Return true if DebugLoc's lexical scope dominates at least one
293 /// machine instruction's lexical scope in a given machine basic block.
294 bool LexicalScopes::dominates(DebugLoc DL, MachineBasicBlock *MBB) {
295   LexicalScope *Scope = getOrCreateLexicalScope(DL);
296   if (!Scope)
297     return false;
298
299   // Current function scope covers all basic blocks in the function.
300   if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
301     return true;
302
303   bool Result = false;
304   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
305        ++I) {
306     DebugLoc IDL = I->getDebugLoc();
307     if (IDL.isUnknown())
308       continue;
309     if (LexicalScope *IScope = getOrCreateLexicalScope(IDL))
310       if (Scope->dominates(IScope))
311         return true;
312   }
313   return Result;
314 }
315
316 /// dump - Print data structures.
317 void LexicalScope::dump(unsigned Indent) const {
318 #ifndef NDEBUG
319   raw_ostream &err = dbgs();
320   err.indent(Indent);
321   err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n";
322   const MDNode *N = Desc;
323   err.indent(Indent);
324   N->dump();
325   if (AbstractScope)
326     err << std::string(Indent, ' ') << "Abstract Scope\n";
327
328   if (!Children.empty())
329     err << std::string(Indent + 2, ' ') << "Children ...\n";
330   for (unsigned i = 0, e = Children.size(); i != e; ++i)
331     if (Children[i] != this)
332       Children[i]->dump(Indent + 2);
333 #endif
334 }