blob: 02ffb01665e2eca83fade1bf0164d3e6fe803304 [file] [log] [blame]
Devang Patele1649c32011-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
Devang Patele1649c32011-08-10 19:04:06 +000017#include "llvm/CodeGen/LexicalScopes.h"
Devang Patele1649c32011-08-10 19:04:06 +000018#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000020#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Function.h"
Devang Patele1649c32011-08-10 19:04:06 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/FormattedStream.h"
25using namespace llvm;
26
Chandler Carruth1b9dde02014-04-22 02:02:50 +000027#define DEBUG_TYPE "lexicalscopes"
28
Eric Christopherb7dee8a2013-11-20 00:54:28 +000029/// ~LexicalScopes - final cleanup after ourselves.
30LexicalScopes::~LexicalScopes() { reset(); }
Devang Patele1649c32011-08-10 19:04:06 +000031
Eric Christopherb7dee8a2013-11-20 00:54:28 +000032/// reset - Reset the instance so that it's prepared for another function.
33void LexicalScopes::reset() {
Craig Topperc0196b12014-04-14 00:51:57 +000034 MF = nullptr;
35 CurrentFnLexicalScope = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +000036 DeleteContainerSeconds(LexicalScopeMap);
37 DeleteContainerSeconds(AbstractScopeMap);
38 InlinedLexicalScopeMap.clear();
39 AbstractScopesList.clear();
40}
41
42/// initialize - Scan machine function and constuct lexical scope nest.
43void LexicalScopes::initialize(const MachineFunction &Fn) {
Eric Christopherb7dee8a2013-11-20 00:54:28 +000044 reset();
Devang Patele1649c32011-08-10 19:04:06 +000045 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.
Eric Christopher6211e4b2013-11-20 00:54:19 +000057void LexicalScopes::extractLexicalScopes(
58 SmallVectorImpl<InsnRange> &MIRanges,
59 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
Devang Patele1649c32011-08-10 19:04:06 +000060
61 // Scan each instruction and create scopes. First build working set of scopes.
Eric Christopher6211e4b2013-11-20 00:54:19 +000062 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
63 ++I) {
Craig Topperc0196b12014-04-14 00:51:57 +000064 const MachineInstr *RangeBeginMI = nullptr;
65 const MachineInstr *PrevMI = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +000066 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.
116LexicalScope *LexicalScopes::findLexicalScope(DebugLoc DL) {
Craig Topperc0196b12014-04-14 00:51:57 +0000117 MDNode *Scope = nullptr;
118 MDNode *IA = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +0000119 DL.getScopeAndInlinedAt(Scope, IA, MF->getFunction()->getContext());
Eric Christopher6211e4b2013-11-20 00:54:19 +0000120 if (!Scope)
Craig Topperc0196b12014-04-14 00:51:57 +0000121 return nullptr;
Eric Christopher6647b832011-10-11 22:59:11 +0000122
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();
Eric Christopher6211e4b2013-11-20 00:54:19 +0000128
Devang Patele1649c32011-08-10 19:04:06 +0000129 if (IA)
130 return InlinedLexicalScopeMap.lookup(DebugLoc::getFromDILocation(IA));
Eric Christopher6647b832011-10-11 22:59:11 +0000131 return LexicalScopeMap.lookup(Scope);
Devang Patele1649c32011-08-10 19:04:06 +0000132}
133
134/// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
135/// not available then create new lexical scope.
136LexicalScope *LexicalScopes::getOrCreateLexicalScope(DebugLoc DL) {
Craig Topperc0196b12014-04-14 00:51:57 +0000137 MDNode *Scope = nullptr;
138 MDNode *InlinedAt = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +0000139 DL.getScopeAndInlinedAt(Scope, InlinedAt, MF->getFunction()->getContext());
Eric Christopher6647b832011-10-11 22:59:11 +0000140
Devang Patele1649c32011-08-10 19:04:06 +0000141 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 }
Eric Christopher6211e4b2013-11-20 00:54:19 +0000147
Devang Patele1649c32011-08-10 19:04:06 +0000148 return getOrCreateRegularScope(Scope);
149}
150
151/// getOrCreateRegularScope - Find or create a regular lexical scope.
152LexicalScope *LexicalScopes::getOrCreateRegularScope(MDNode *Scope) {
Eric Christopher6647b832011-10-11 22:59:11 +0000153 DIDescriptor D = DIDescriptor(Scope);
Eric Christopher76933f42011-10-13 21:43:44 +0000154 if (D.isLexicalBlockFile()) {
Eric Christopher6647b832011-10-11 22:59:11 +0000155 Scope = DILexicalBlockFile(Scope).getScope();
Eric Christopher76933f42011-10-13 21:43:44 +0000156 D = DIDescriptor(Scope);
157 }
Eric Christopher6211e4b2013-11-20 00:54:19 +0000158
Devang Patele1649c32011-08-10 19:04:06 +0000159 LexicalScope *WScope = LexicalScopeMap.lookup(Scope);
160 if (WScope)
161 return WScope;
162
Craig Topperc0196b12014-04-14 00:51:57 +0000163 LexicalScope *Parent = nullptr;
Eric Christopher6647b832011-10-11 22:59:11 +0000164 if (D.isLexicalBlock())
Devang Patele1649c32011-08-10 19:04:06 +0000165 Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope));
Craig Topperc0196b12014-04-14 00:51:57 +0000166 WScope = new LexicalScope(Parent, DIDescriptor(Scope), nullptr, false);
Devang Patele1649c32011-08-10 19:04:06 +0000167 LexicalScopeMap.insert(std::make_pair(Scope, WScope));
Eric Christopher6211e4b2013-11-20 00:54:19 +0000168 if (!Parent && DIDescriptor(Scope).isSubprogram() &&
169 DISubprogram(Scope).describes(MF->getFunction()))
Devang Patele1649c32011-08-10 19:04:06 +0000170 CurrentFnLexicalScope = WScope;
Eric Christopher6211e4b2013-11-20 00:54:19 +0000171
Devang Patele1649c32011-08-10 19:04:06 +0000172 return WScope;
173}
174
175/// getOrCreateInlinedScope - Find or create an inlined lexical scope.
Eric Christopher6211e4b2013-11-20 00:54:19 +0000176LexicalScope *LexicalScopes::getOrCreateInlinedScope(MDNode *Scope,
Devang Patele1649c32011-08-10 19:04:06 +0000177 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.
191LexicalScope *LexicalScopes::getOrCreateAbstractScope(const MDNode *N) {
192 assert(N && "Invalid Scope encoding!");
193
Eric Christopher6647b832011-10-11 22:59:11 +0000194 DIDescriptor Scope(N);
195 if (Scope.isLexicalBlockFile())
196 Scope = DILexicalBlockFile(Scope).getScope();
Devang Patele1649c32011-08-10 19:04:06 +0000197 LexicalScope *AScope = AbstractScopeMap.lookup(N);
198 if (AScope)
199 return AScope;
200
Craig Topperc0196b12014-04-14 00:51:57 +0000201 LexicalScope *Parent = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +0000202 if (Scope.isLexicalBlock()) {
203 DILexicalBlock DB(N);
204 DIDescriptor ParentDesc = DB.getContext();
205 Parent = getOrCreateAbstractScope(ParentDesc);
206 }
Craig Topperc0196b12014-04-14 00:51:57 +0000207 AScope = new LexicalScope(Parent, DIDescriptor(N), nullptr, true);
Devang Patele1649c32011-08-10 19:04:06 +0000208 AbstractScopeMap[N] = AScope;
209 if (DIDescriptor(N).isSubprogram())
210 AbstractScopesList.push_back(AScope);
211 return AScope;
212}
213
214/// constructScopeNest
215void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
Eric Christopher6211e4b2013-11-20 00:54:19 +0000216 assert(Scope && "Unable to calculate scope dominance graph!");
Devang Patele1649c32011-08-10 19:04:06 +0000217 SmallVector<LexicalScope *, 4> WorkStack;
218 WorkStack.push_back(Scope);
219 unsigned Counter = 0;
220 while (!WorkStack.empty()) {
221 LexicalScope *WS = WorkStack.back();
Craig Topper80170e52013-07-03 04:30:58 +0000222 const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
Devang Patele1649c32011-08-10 19:04:06 +0000223 bool visitedChildren = false;
Craig Topper24fd7ee2013-07-03 04:42:33 +0000224 for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
Eric Christopher6211e4b2013-11-20 00:54:19 +0000225 SE = Children.end();
226 SI != SE; ++SI) {
Devang Patele1649c32011-08-10 19:04:06 +0000227 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
Devang Patel784077e2011-08-10 23:58:09 +0000242/// assignInstructionRanges - Find ranges of instructions covered by each
243/// lexical scope.
Eric Christopher6211e4b2013-11-20 00:54:19 +0000244void LexicalScopes::assignInstructionRanges(
245 SmallVectorImpl<InsnRange> &MIRanges,
246 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
247
Craig Topperc0196b12014-04-14 00:51:57 +0000248 LexicalScope *PrevLexicalScope = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +0000249 for (SmallVectorImpl<InsnRange>::const_iterator RI = MIRanges.begin(),
Eric Christopher6211e4b2013-11-20 00:54:19 +0000250 RE = MIRanges.end();
251 RI != RE; ++RI) {
Devang Patele1649c32011-08-10 19:04:06 +0000252 const InsnRange &R = *RI;
253 LexicalScope *S = MI2ScopeMap.lookup(R.first);
Eric Christopher6211e4b2013-11-20 00:54:19 +0000254 assert(S && "Lost LexicalScope for a machine instruction!");
Devang Patele1649c32011-08-10 19:04:06 +0000255 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
Eric Christopher6211e4b2013-11-20 00:54:19 +0000267/// have machine instructions that belong to lexical scope identified by
Devang Patele1649c32011-08-10 19:04:06 +0000268/// DebugLoc.
Eric Christopher6211e4b2013-11-20 00:54:19 +0000269void LexicalScopes::getMachineBasicBlocks(
270 DebugLoc DL, SmallPtrSet<const MachineBasicBlock *, 4> &MBBs) {
Devang Patele1649c32011-08-10 19:04:06 +0000271 MBBs.clear();
272 LexicalScope *Scope = getOrCreateLexicalScope(DL);
273 if (!Scope)
274 return;
Eric Christopher6211e4b2013-11-20 00:54:19 +0000275
Devang Pateldb4374a2011-08-12 18:01:34 +0000276 if (Scope == CurrentFnLexicalScope) {
Eric Christopher6211e4b2013-11-20 00:54:19 +0000277 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
278 ++I)
Devang Pateldb4374a2011-08-12 18:01:34 +0000279 MBBs.insert(I);
280 return;
281 }
282
Craig Topper80170e52013-07-03 04:30:58 +0000283 SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges();
Craig Topper24fd7ee2013-07-03 04:42:33 +0000284 for (SmallVectorImpl<InsnRange>::iterator I = InsnRanges.begin(),
Eric Christopher6211e4b2013-11-20 00:54:19 +0000285 E = InsnRanges.end();
286 I != E; ++I) {
Devang Patele1649c32011-08-10 19:04:06 +0000287 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.
294bool LexicalScopes::dominates(DebugLoc DL, MachineBasicBlock *MBB) {
295 LexicalScope *Scope = getOrCreateLexicalScope(DL);
296 if (!Scope)
297 return false;
Devang Pateldb4374a2011-08-12 18:01:34 +0000298
299 // Current function scope covers all basic blocks in the function.
300 if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
301 return true;
302
Devang Patele1649c32011-08-10 19:04:06 +0000303 bool Result = false;
Eric Christopher6211e4b2013-11-20 00:54:19 +0000304 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
305 ++I) {
Devang Patele1649c32011-08-10 19:04:06 +0000306 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
Devang Patele1649c32011-08-10 19:04:06 +0000316/// dump - Print data structures.
Manman Rene498b252013-02-02 00:02:03 +0000317void LexicalScope::dump(unsigned Indent) const {
Devang Patele1649c32011-08-10 19:04:06 +0000318#ifndef NDEBUG
319 raw_ostream &err = dbgs();
Manman Rene498b252013-02-02 00:02:03 +0000320 err.indent(Indent);
Devang Patele1649c32011-08-10 19:04:06 +0000321 err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n";
322 const MDNode *N = Desc;
Manman Rene498b252013-02-02 00:02:03 +0000323 err.indent(Indent);
Devang Patele1649c32011-08-10 19:04:06 +0000324 N->dump();
325 if (AbstractScope)
Manman Rene498b252013-02-02 00:02:03 +0000326 err << std::string(Indent, ' ') << "Abstract Scope\n";
Devang Patele1649c32011-08-10 19:04:06 +0000327
Devang Patele1649c32011-08-10 19:04:06 +0000328 if (!Children.empty())
Manman Rene498b252013-02-02 00:02:03 +0000329 err << std::string(Indent + 2, ' ') << "Children ...\n";
Devang Patele1649c32011-08-10 19:04:06 +0000330 for (unsigned i = 0, e = Children.size(); i != e; ++i)
331 if (Children[i] != this)
Manman Rene498b252013-02-02 00:02:03 +0000332 Children[i]->dump(Indent + 2);
Devang Patele1649c32011-08-10 19:04:06 +0000333#endif
334}