blob: c22ab11f7a10451e74bf752534c36b3962df342b [file] [log] [blame]
Devang Patel103b8e62011-08-10 19:04:06 +00001//===- 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#define DEBUG_TYPE "lexicalscopes"
18#include "llvm/CodeGen/LexicalScopes.h"
Devang Patel103b8e62011-08-10 19:04:06 +000019#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstr.h"
Stephen Hines36b56882014-04-23 16:57:46 -070021#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/Function.h"
Devang Patel103b8e62011-08-10 19:04:06 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/FormattedStream.h"
26using namespace llvm;
27
Stephen Hines36b56882014-04-23 16:57:46 -070028/// ~LexicalScopes - final cleanup after ourselves.
29LexicalScopes::~LexicalScopes() { reset(); }
Devang Patel103b8e62011-08-10 19:04:06 +000030
Stephen Hines36b56882014-04-23 16:57:46 -070031/// reset - Reset the instance so that it's prepared for another function.
32void LexicalScopes::reset() {
Devang Patel103b8e62011-08-10 19:04:06 +000033 MF = NULL;
34 CurrentFnLexicalScope = NULL;
35 DeleteContainerSeconds(LexicalScopeMap);
36 DeleteContainerSeconds(AbstractScopeMap);
37 InlinedLexicalScopeMap.clear();
38 AbstractScopesList.clear();
39}
40
41/// initialize - Scan machine function and constuct lexical scope nest.
42void LexicalScopes::initialize(const MachineFunction &Fn) {
Stephen Hines36b56882014-04-23 16:57:46 -070043 reset();
Devang Patel103b8e62011-08-10 19:04:06 +000044 MF = &Fn;
45 SmallVector<InsnRange, 4> MIRanges;
46 DenseMap<const MachineInstr *, LexicalScope *> MI2ScopeMap;
47 extractLexicalScopes(MIRanges, MI2ScopeMap);
48 if (CurrentFnLexicalScope) {
49 constructScopeNest(CurrentFnLexicalScope);
50 assignInstructionRanges(MIRanges, MI2ScopeMap);
51 }
52}
53
54/// extractLexicalScopes - Extract instruction ranges for each lexical scopes
55/// for the given machine function.
Stephen Hines36b56882014-04-23 16:57:46 -070056void LexicalScopes::extractLexicalScopes(
57 SmallVectorImpl<InsnRange> &MIRanges,
58 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
Devang Patel103b8e62011-08-10 19:04:06 +000059
60 // Scan each instruction and create scopes. First build working set of scopes.
Stephen Hines36b56882014-04-23 16:57:46 -070061 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
62 ++I) {
Devang Patel103b8e62011-08-10 19:04:06 +000063 const MachineInstr *RangeBeginMI = NULL;
64 const MachineInstr *PrevMI = NULL;
65 DebugLoc PrevDL;
66 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
67 II != IE; ++II) {
68 const MachineInstr *MInsn = II;
69
70 // Check if instruction has valid location information.
71 const DebugLoc MIDL = MInsn->getDebugLoc();
72 if (MIDL.isUnknown()) {
73 PrevMI = MInsn;
74 continue;
75 }
76
77 // If scope has not changed then skip this instruction.
78 if (MIDL == PrevDL) {
79 PrevMI = MInsn;
80 continue;
81 }
82
83 // Ignore DBG_VALUE. It does not contribute to any instruction in output.
84 if (MInsn->isDebugValue())
85 continue;
86
87 if (RangeBeginMI) {
88 // If we have already seen a beginning of an instruction range and
89 // current instruction scope does not match scope of first instruction
90 // in this range then create a new instruction range.
91 InsnRange R(RangeBeginMI, PrevMI);
92 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
93 MIRanges.push_back(R);
94 }
95
96 // This is a beginning of a new instruction range.
97 RangeBeginMI = MInsn;
98
99 // Reset previous markers.
100 PrevMI = MInsn;
101 PrevDL = MIDL;
102 }
103
104 // Create last instruction range.
105 if (RangeBeginMI && PrevMI && !PrevDL.isUnknown()) {
106 InsnRange R(RangeBeginMI, PrevMI);
107 MIRanges.push_back(R);
108 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
109 }
110 }
111}
112
113/// findLexicalScope - Find lexical scope, either regular or inlined, for the
114/// given DebugLoc. Return NULL if not found.
115LexicalScope *LexicalScopes::findLexicalScope(DebugLoc DL) {
116 MDNode *Scope = NULL;
117 MDNode *IA = NULL;
118 DL.getScopeAndInlinedAt(Scope, IA, MF->getFunction()->getContext());
Stephen Hines36b56882014-04-23 16:57:46 -0700119 if (!Scope)
120 return NULL;
Eric Christopher6618a242011-10-11 22:59:11 +0000121
122 // The scope that we were created with could have an extra file - which
123 // isn't what we care about in this case.
124 DIDescriptor D = DIDescriptor(Scope);
125 if (D.isLexicalBlockFile())
126 Scope = DILexicalBlockFile(Scope).getScope();
Stephen Hines36b56882014-04-23 16:57:46 -0700127
Devang Patel103b8e62011-08-10 19:04:06 +0000128 if (IA)
129 return InlinedLexicalScopeMap.lookup(DebugLoc::getFromDILocation(IA));
Eric Christopher6618a242011-10-11 22:59:11 +0000130 return LexicalScopeMap.lookup(Scope);
Devang Patel103b8e62011-08-10 19:04:06 +0000131}
132
133/// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
134/// not available then create new lexical scope.
135LexicalScope *LexicalScopes::getOrCreateLexicalScope(DebugLoc DL) {
136 MDNode *Scope = NULL;
137 MDNode *InlinedAt = NULL;
138 DL.getScopeAndInlinedAt(Scope, InlinedAt, MF->getFunction()->getContext());
Eric Christopher6618a242011-10-11 22:59:11 +0000139
Devang Patel103b8e62011-08-10 19:04:06 +0000140 if (InlinedAt) {
141 // Create an abstract scope for inlined function.
142 getOrCreateAbstractScope(Scope);
143 // Create an inlined scope for inlined function.
144 return getOrCreateInlinedScope(Scope, InlinedAt);
145 }
Stephen Hines36b56882014-04-23 16:57:46 -0700146
Devang Patel103b8e62011-08-10 19:04:06 +0000147 return getOrCreateRegularScope(Scope);
148}
149
150/// getOrCreateRegularScope - Find or create a regular lexical scope.
151LexicalScope *LexicalScopes::getOrCreateRegularScope(MDNode *Scope) {
Eric Christopher6618a242011-10-11 22:59:11 +0000152 DIDescriptor D = DIDescriptor(Scope);
Eric Christopherfe28ef42011-10-13 21:43:44 +0000153 if (D.isLexicalBlockFile()) {
Eric Christopher6618a242011-10-11 22:59:11 +0000154 Scope = DILexicalBlockFile(Scope).getScope();
Eric Christopherfe28ef42011-10-13 21:43:44 +0000155 D = DIDescriptor(Scope);
156 }
Stephen Hines36b56882014-04-23 16:57:46 -0700157
Devang Patel103b8e62011-08-10 19:04:06 +0000158 LexicalScope *WScope = LexicalScopeMap.lookup(Scope);
159 if (WScope)
160 return WScope;
161
162 LexicalScope *Parent = NULL;
Eric Christopher6618a242011-10-11 22:59:11 +0000163 if (D.isLexicalBlock())
Devang Patel103b8e62011-08-10 19:04:06 +0000164 Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope));
165 WScope = new LexicalScope(Parent, DIDescriptor(Scope), NULL, false);
166 LexicalScopeMap.insert(std::make_pair(Scope, WScope));
Stephen Hines36b56882014-04-23 16:57:46 -0700167 if (!Parent && DIDescriptor(Scope).isSubprogram() &&
168 DISubprogram(Scope).describes(MF->getFunction()))
Devang Patel103b8e62011-08-10 19:04:06 +0000169 CurrentFnLexicalScope = WScope;
Stephen Hines36b56882014-04-23 16:57:46 -0700170
Devang Patel103b8e62011-08-10 19:04:06 +0000171 return WScope;
172}
173
174/// getOrCreateInlinedScope - Find or create an inlined lexical scope.
Stephen Hines36b56882014-04-23 16:57:46 -0700175LexicalScope *LexicalScopes::getOrCreateInlinedScope(MDNode *Scope,
Devang Patel103b8e62011-08-10 19:04:06 +0000176 MDNode *InlinedAt) {
177 LexicalScope *InlinedScope = LexicalScopeMap.lookup(InlinedAt);
178 if (InlinedScope)
179 return InlinedScope;
180
181 DebugLoc InlinedLoc = DebugLoc::getFromDILocation(InlinedAt);
182 InlinedScope = new LexicalScope(getOrCreateLexicalScope(InlinedLoc),
183 DIDescriptor(Scope), InlinedAt, false);
184 InlinedLexicalScopeMap[InlinedLoc] = InlinedScope;
185 LexicalScopeMap[InlinedAt] = InlinedScope;
186 return InlinedScope;
187}
188
189/// getOrCreateAbstractScope - Find or create an abstract lexical scope.
190LexicalScope *LexicalScopes::getOrCreateAbstractScope(const MDNode *N) {
191 assert(N && "Invalid Scope encoding!");
192
Eric Christopher6618a242011-10-11 22:59:11 +0000193 DIDescriptor Scope(N);
194 if (Scope.isLexicalBlockFile())
195 Scope = DILexicalBlockFile(Scope).getScope();
Devang Patel103b8e62011-08-10 19:04:06 +0000196 LexicalScope *AScope = AbstractScopeMap.lookup(N);
197 if (AScope)
198 return AScope;
199
200 LexicalScope *Parent = NULL;
Devang Patel103b8e62011-08-10 19:04:06 +0000201 if (Scope.isLexicalBlock()) {
202 DILexicalBlock DB(N);
203 DIDescriptor ParentDesc = DB.getContext();
204 Parent = getOrCreateAbstractScope(ParentDesc);
205 }
206 AScope = new LexicalScope(Parent, DIDescriptor(N), NULL, true);
207 AbstractScopeMap[N] = AScope;
208 if (DIDescriptor(N).isSubprogram())
209 AbstractScopesList.push_back(AScope);
210 return AScope;
211}
212
213/// constructScopeNest
214void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
Stephen Hines36b56882014-04-23 16:57:46 -0700215 assert(Scope && "Unable to calculate scope dominance graph!");
Devang Patel103b8e62011-08-10 19:04:06 +0000216 SmallVector<LexicalScope *, 4> WorkStack;
217 WorkStack.push_back(Scope);
218 unsigned Counter = 0;
219 while (!WorkStack.empty()) {
220 LexicalScope *WS = WorkStack.back();
Craig Topper9f4c3792013-07-03 04:30:58 +0000221 const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
Devang Patel103b8e62011-08-10 19:04:06 +0000222 bool visitedChildren = false;
Craig Topper40c744e2013-07-03 04:42:33 +0000223 for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
Stephen Hines36b56882014-04-23 16:57:46 -0700224 SE = Children.end();
225 SI != SE; ++SI) {
Devang Patel103b8e62011-08-10 19:04:06 +0000226 LexicalScope *ChildScope = *SI;
227 if (!ChildScope->getDFSOut()) {
228 WorkStack.push_back(ChildScope);
229 visitedChildren = true;
230 ChildScope->setDFSIn(++Counter);
231 break;
232 }
233 }
234 if (!visitedChildren) {
235 WorkStack.pop_back();
236 WS->setDFSOut(++Counter);
237 }
238 }
239}
240
Devang Patel5bc942c2011-08-10 23:58:09 +0000241/// assignInstructionRanges - Find ranges of instructions covered by each
242/// lexical scope.
Stephen Hines36b56882014-04-23 16:57:46 -0700243void LexicalScopes::assignInstructionRanges(
244 SmallVectorImpl<InsnRange> &MIRanges,
245 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
246
Devang Patel103b8e62011-08-10 19:04:06 +0000247 LexicalScope *PrevLexicalScope = NULL;
248 for (SmallVectorImpl<InsnRange>::const_iterator RI = MIRanges.begin(),
Stephen Hines36b56882014-04-23 16:57:46 -0700249 RE = MIRanges.end();
250 RI != RE; ++RI) {
Devang Patel103b8e62011-08-10 19:04:06 +0000251 const InsnRange &R = *RI;
252 LexicalScope *S = MI2ScopeMap.lookup(R.first);
Stephen Hines36b56882014-04-23 16:57:46 -0700253 assert(S && "Lost LexicalScope for a machine instruction!");
Devang Patel103b8e62011-08-10 19:04:06 +0000254 if (PrevLexicalScope && !PrevLexicalScope->dominates(S))
255 PrevLexicalScope->closeInsnRange(S);
256 S->openInsnRange(R.first);
257 S->extendInsnRange(R.second);
258 PrevLexicalScope = S;
259 }
260
261 if (PrevLexicalScope)
262 PrevLexicalScope->closeInsnRange();
263}
264
265/// getMachineBasicBlocks - Populate given set using machine basic blocks which
Stephen Hines36b56882014-04-23 16:57:46 -0700266/// have machine instructions that belong to lexical scope identified by
Devang Patel103b8e62011-08-10 19:04:06 +0000267/// DebugLoc.
Stephen Hines36b56882014-04-23 16:57:46 -0700268void LexicalScopes::getMachineBasicBlocks(
269 DebugLoc DL, SmallPtrSet<const MachineBasicBlock *, 4> &MBBs) {
Devang Patel103b8e62011-08-10 19:04:06 +0000270 MBBs.clear();
271 LexicalScope *Scope = getOrCreateLexicalScope(DL);
272 if (!Scope)
273 return;
Stephen Hines36b56882014-04-23 16:57:46 -0700274
Devang Patel2e85b1b2011-08-12 18:01:34 +0000275 if (Scope == CurrentFnLexicalScope) {
Stephen Hines36b56882014-04-23 16:57:46 -0700276 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
277 ++I)
Devang Patel2e85b1b2011-08-12 18:01:34 +0000278 MBBs.insert(I);
279 return;
280 }
281
Craig Topper9f4c3792013-07-03 04:30:58 +0000282 SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges();
Craig Topper40c744e2013-07-03 04:42:33 +0000283 for (SmallVectorImpl<InsnRange>::iterator I = InsnRanges.begin(),
Stephen Hines36b56882014-04-23 16:57:46 -0700284 E = InsnRanges.end();
285 I != E; ++I) {
Devang Patel103b8e62011-08-10 19:04:06 +0000286 InsnRange &R = *I;
287 MBBs.insert(R.first->getParent());
288 }
289}
290
291/// dominates - Return true if DebugLoc's lexical scope dominates at least one
292/// machine instruction's lexical scope in a given machine basic block.
293bool LexicalScopes::dominates(DebugLoc DL, MachineBasicBlock *MBB) {
294 LexicalScope *Scope = getOrCreateLexicalScope(DL);
295 if (!Scope)
296 return false;
Devang Patel2e85b1b2011-08-12 18:01:34 +0000297
298 // Current function scope covers all basic blocks in the function.
299 if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
300 return true;
301
Devang Patel103b8e62011-08-10 19:04:06 +0000302 bool Result = false;
Stephen Hines36b56882014-04-23 16:57:46 -0700303 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
304 ++I) {
Devang Patel103b8e62011-08-10 19:04:06 +0000305 DebugLoc IDL = I->getDebugLoc();
306 if (IDL.isUnknown())
307 continue;
308 if (LexicalScope *IScope = getOrCreateLexicalScope(IDL))
309 if (Scope->dominates(IScope))
310 return true;
311 }
312 return Result;
313}
314
Devang Patel103b8e62011-08-10 19:04:06 +0000315/// dump - Print data structures.
Manman Ren7650d9b2013-02-02 00:02:03 +0000316void LexicalScope::dump(unsigned Indent) const {
Devang Patel103b8e62011-08-10 19:04:06 +0000317#ifndef NDEBUG
318 raw_ostream &err = dbgs();
Manman Ren7650d9b2013-02-02 00:02:03 +0000319 err.indent(Indent);
Devang Patel103b8e62011-08-10 19:04:06 +0000320 err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n";
321 const MDNode *N = Desc;
Manman Ren7650d9b2013-02-02 00:02:03 +0000322 err.indent(Indent);
Devang Patel103b8e62011-08-10 19:04:06 +0000323 N->dump();
324 if (AbstractScope)
Manman Ren7650d9b2013-02-02 00:02:03 +0000325 err << std::string(Indent, ' ') << "Abstract Scope\n";
Devang Patel103b8e62011-08-10 19:04:06 +0000326
Devang Patel103b8e62011-08-10 19:04:06 +0000327 if (!Children.empty())
Manman Ren7650d9b2013-02-02 00:02:03 +0000328 err << std::string(Indent + 2, ' ') << "Children ...\n";
Devang Patel103b8e62011-08-10 19:04:06 +0000329 for (unsigned i = 0, e = Children.size(); i != e; ++i)
330 if (Children[i] != this)
Manman Ren7650d9b2013-02-02 00:02:03 +0000331 Children[i]->dump(Indent + 2);
Devang Patel103b8e62011-08-10 19:04:06 +0000332#endif
333}