blob: f01dec28e52db34564ef7afb2468280ee4373375 [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
David Blaikie9dabbf62014-05-06 21:07:17 +000029/// ~LexicalScopes - final cleanup after ourselves.
30LexicalScopes::~LexicalScopes() { reset(); }
31
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;
David Blaikie9dabbf62014-05-06 21:07:17 +000036 DeleteContainerSeconds(LexicalScopeMap);
37 DeleteContainerSeconds(AbstractScopeMap);
Devang Patele1649c32011-08-10 19:04:06 +000038 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.
Alexey Samsonov41b977d2014-04-30 18:29:51 +000062 for (const auto &MBB : *MF) {
Craig Topperc0196b12014-04-14 00:51:57 +000063 const MachineInstr *RangeBeginMI = nullptr;
64 const MachineInstr *PrevMI = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +000065 DebugLoc PrevDL;
Alexey Samsonovf74bde62014-04-30 22:17:38 +000066 for (const auto &MInsn : MBB) {
Devang Patele1649c32011-08-10 19:04:06 +000067 // Check if instruction has valid location information.
Alexey Samsonovf74bde62014-04-30 22:17:38 +000068 const DebugLoc MIDL = MInsn.getDebugLoc();
Devang Patele1649c32011-08-10 19:04:06 +000069 if (MIDL.isUnknown()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +000070 PrevMI = &MInsn;
Devang Patele1649c32011-08-10 19:04:06 +000071 continue;
72 }
73
74 // If scope has not changed then skip this instruction.
75 if (MIDL == PrevDL) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +000076 PrevMI = &MInsn;
Devang Patele1649c32011-08-10 19:04:06 +000077 continue;
78 }
79
80 // Ignore DBG_VALUE. It does not contribute to any instruction in output.
Alexey Samsonovf74bde62014-04-30 22:17:38 +000081 if (MInsn.isDebugValue())
Devang Patele1649c32011-08-10 19:04:06 +000082 continue;
83
84 if (RangeBeginMI) {
85 // If we have already seen a beginning of an instruction range and
86 // current instruction scope does not match scope of first instruction
87 // in this range then create a new instruction range.
88 InsnRange R(RangeBeginMI, PrevMI);
89 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
90 MIRanges.push_back(R);
91 }
92
93 // This is a beginning of a new instruction range.
Alexey Samsonovf74bde62014-04-30 22:17:38 +000094 RangeBeginMI = &MInsn;
Devang Patele1649c32011-08-10 19:04:06 +000095
96 // Reset previous markers.
Alexey Samsonovf74bde62014-04-30 22:17:38 +000097 PrevMI = &MInsn;
Devang Patele1649c32011-08-10 19:04:06 +000098 PrevDL = MIDL;
99 }
100
101 // Create last instruction range.
102 if (RangeBeginMI && PrevMI && !PrevDL.isUnknown()) {
103 InsnRange R(RangeBeginMI, PrevMI);
104 MIRanges.push_back(R);
105 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
106 }
107 }
108}
109
110/// findLexicalScope - Find lexical scope, either regular or inlined, for the
111/// given DebugLoc. Return NULL if not found.
112LexicalScope *LexicalScopes::findLexicalScope(DebugLoc DL) {
Craig Topperc0196b12014-04-14 00:51:57 +0000113 MDNode *Scope = nullptr;
114 MDNode *IA = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +0000115 DL.getScopeAndInlinedAt(Scope, IA, MF->getFunction()->getContext());
Eric Christopher6211e4b2013-11-20 00:54:19 +0000116 if (!Scope)
Craig Topperc0196b12014-04-14 00:51:57 +0000117 return nullptr;
Eric Christopher6647b832011-10-11 22:59:11 +0000118
119 // The scope that we were created with could have an extra file - which
120 // isn't what we care about in this case.
121 DIDescriptor D = DIDescriptor(Scope);
122 if (D.isLexicalBlockFile())
123 Scope = DILexicalBlockFile(Scope).getScope();
Eric Christopher6211e4b2013-11-20 00:54:19 +0000124
Devang Patele1649c32011-08-10 19:04:06 +0000125 if (IA)
126 return InlinedLexicalScopeMap.lookup(DebugLoc::getFromDILocation(IA));
David Blaikie9dabbf62014-05-06 21:07:17 +0000127 return LexicalScopeMap.lookup(Scope);
Devang Patele1649c32011-08-10 19:04:06 +0000128}
129
130/// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
131/// not available then create new lexical scope.
132LexicalScope *LexicalScopes::getOrCreateLexicalScope(DebugLoc DL) {
Craig Topperc0196b12014-04-14 00:51:57 +0000133 MDNode *Scope = nullptr;
134 MDNode *InlinedAt = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +0000135 DL.getScopeAndInlinedAt(Scope, InlinedAt, MF->getFunction()->getContext());
Eric Christopher6647b832011-10-11 22:59:11 +0000136
Devang Patele1649c32011-08-10 19:04:06 +0000137 if (InlinedAt) {
138 // Create an abstract scope for inlined function.
139 getOrCreateAbstractScope(Scope);
140 // Create an inlined scope for inlined function.
141 return getOrCreateInlinedScope(Scope, InlinedAt);
142 }
Eric Christopher6211e4b2013-11-20 00:54:19 +0000143
Devang Patele1649c32011-08-10 19:04:06 +0000144 return getOrCreateRegularScope(Scope);
145}
146
147/// getOrCreateRegularScope - Find or create a regular lexical scope.
148LexicalScope *LexicalScopes::getOrCreateRegularScope(MDNode *Scope) {
Eric Christopher6647b832011-10-11 22:59:11 +0000149 DIDescriptor D = DIDescriptor(Scope);
Eric Christopher76933f42011-10-13 21:43:44 +0000150 if (D.isLexicalBlockFile()) {
Eric Christopher6647b832011-10-11 22:59:11 +0000151 Scope = DILexicalBlockFile(Scope).getScope();
Eric Christopher76933f42011-10-13 21:43:44 +0000152 D = DIDescriptor(Scope);
153 }
Eric Christopher6211e4b2013-11-20 00:54:19 +0000154
David Blaikie9dabbf62014-05-06 21:07:17 +0000155 LexicalScope *WScope = LexicalScopeMap.lookup(Scope);
156 if (WScope)
157 return WScope;
Devang Patele1649c32011-08-10 19:04:06 +0000158
Craig Topperc0196b12014-04-14 00:51:57 +0000159 LexicalScope *Parent = nullptr;
Eric Christopher6647b832011-10-11 22:59:11 +0000160 if (D.isLexicalBlock())
Devang Patele1649c32011-08-10 19:04:06 +0000161 Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope));
David Blaikie9dabbf62014-05-06 21:07:17 +0000162 WScope = new LexicalScope(Parent, DIDescriptor(Scope), nullptr, false);
163 LexicalScopeMap.insert(std::make_pair(Scope, WScope));
Eric Christopher6211e4b2013-11-20 00:54:19 +0000164 if (!Parent && DIDescriptor(Scope).isSubprogram() &&
165 DISubprogram(Scope).describes(MF->getFunction()))
David Blaikie9dabbf62014-05-06 21:07:17 +0000166 CurrentFnLexicalScope = WScope;
Eric Christopher6211e4b2013-11-20 00:54:19 +0000167
David Blaikie9dabbf62014-05-06 21:07:17 +0000168 return WScope;
Devang Patele1649c32011-08-10 19:04:06 +0000169}
170
171/// getOrCreateInlinedScope - Find or create an inlined lexical scope.
Eric Christopher6211e4b2013-11-20 00:54:19 +0000172LexicalScope *LexicalScopes::getOrCreateInlinedScope(MDNode *Scope,
Devang Patele1649c32011-08-10 19:04:06 +0000173 MDNode *InlinedAt) {
David Blaikie9dabbf62014-05-06 21:07:17 +0000174 LexicalScope *InlinedScope = LexicalScopeMap.lookup(InlinedAt);
175 if (InlinedScope)
176 return InlinedScope;
Devang Patele1649c32011-08-10 19:04:06 +0000177
178 DebugLoc InlinedLoc = DebugLoc::getFromDILocation(InlinedAt);
David Blaikie9dabbf62014-05-06 21:07:17 +0000179 InlinedScope = new LexicalScope(getOrCreateLexicalScope(InlinedLoc),
180 DIDescriptor(Scope), InlinedAt, false);
181 InlinedLexicalScopeMap[InlinedLoc] = InlinedScope;
182 LexicalScopeMap[InlinedAt] = InlinedScope;
183 return InlinedScope;
Devang Patele1649c32011-08-10 19:04:06 +0000184}
185
186/// getOrCreateAbstractScope - Find or create an abstract lexical scope.
187LexicalScope *LexicalScopes::getOrCreateAbstractScope(const MDNode *N) {
188 assert(N && "Invalid Scope encoding!");
189
Eric Christopher6647b832011-10-11 22:59:11 +0000190 DIDescriptor Scope(N);
191 if (Scope.isLexicalBlockFile())
192 Scope = DILexicalBlockFile(Scope).getScope();
David Blaikie9dabbf62014-05-06 21:07:17 +0000193 LexicalScope *AScope = AbstractScopeMap.lookup(N);
194 if (AScope)
195 return AScope;
Devang Patele1649c32011-08-10 19:04:06 +0000196
Craig Topperc0196b12014-04-14 00:51:57 +0000197 LexicalScope *Parent = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +0000198 if (Scope.isLexicalBlock()) {
199 DILexicalBlock DB(N);
200 DIDescriptor ParentDesc = DB.getContext();
201 Parent = getOrCreateAbstractScope(ParentDesc);
202 }
David Blaikie9dabbf62014-05-06 21:07:17 +0000203 AScope = new LexicalScope(Parent, DIDescriptor(N), nullptr, true);
204 AbstractScopeMap[N] = AScope;
Devang Patele1649c32011-08-10 19:04:06 +0000205 if (DIDescriptor(N).isSubprogram())
David Blaikie9dabbf62014-05-06 21:07:17 +0000206 AbstractScopesList.push_back(AScope);
207 return AScope;
Devang Patele1649c32011-08-10 19:04:06 +0000208}
209
210/// constructScopeNest
211void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
Eric Christopher6211e4b2013-11-20 00:54:19 +0000212 assert(Scope && "Unable to calculate scope dominance graph!");
Devang Patele1649c32011-08-10 19:04:06 +0000213 SmallVector<LexicalScope *, 4> WorkStack;
214 WorkStack.push_back(Scope);
215 unsigned Counter = 0;
216 while (!WorkStack.empty()) {
217 LexicalScope *WS = WorkStack.back();
Craig Topper80170e52013-07-03 04:30:58 +0000218 const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
Devang Patele1649c32011-08-10 19:04:06 +0000219 bool visitedChildren = false;
Craig Topper24fd7ee2013-07-03 04:42:33 +0000220 for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
Eric Christopher6211e4b2013-11-20 00:54:19 +0000221 SE = Children.end();
222 SI != SE; ++SI) {
Devang Patele1649c32011-08-10 19:04:06 +0000223 LexicalScope *ChildScope = *SI;
224 if (!ChildScope->getDFSOut()) {
225 WorkStack.push_back(ChildScope);
226 visitedChildren = true;
227 ChildScope->setDFSIn(++Counter);
228 break;
229 }
230 }
231 if (!visitedChildren) {
232 WorkStack.pop_back();
233 WS->setDFSOut(++Counter);
234 }
235 }
236}
237
Devang Patel784077e2011-08-10 23:58:09 +0000238/// assignInstructionRanges - Find ranges of instructions covered by each
239/// lexical scope.
Eric Christopher6211e4b2013-11-20 00:54:19 +0000240void LexicalScopes::assignInstructionRanges(
241 SmallVectorImpl<InsnRange> &MIRanges,
242 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
243
Craig Topperc0196b12014-04-14 00:51:57 +0000244 LexicalScope *PrevLexicalScope = nullptr;
Devang Patele1649c32011-08-10 19:04:06 +0000245 for (SmallVectorImpl<InsnRange>::const_iterator RI = MIRanges.begin(),
Eric Christopher6211e4b2013-11-20 00:54:19 +0000246 RE = MIRanges.end();
247 RI != RE; ++RI) {
Devang Patele1649c32011-08-10 19:04:06 +0000248 const InsnRange &R = *RI;
249 LexicalScope *S = MI2ScopeMap.lookup(R.first);
Eric Christopher6211e4b2013-11-20 00:54:19 +0000250 assert(S && "Lost LexicalScope for a machine instruction!");
Devang Patele1649c32011-08-10 19:04:06 +0000251 if (PrevLexicalScope && !PrevLexicalScope->dominates(S))
252 PrevLexicalScope->closeInsnRange(S);
253 S->openInsnRange(R.first);
254 S->extendInsnRange(R.second);
255 PrevLexicalScope = S;
256 }
257
258 if (PrevLexicalScope)
259 PrevLexicalScope->closeInsnRange();
260}
261
262/// getMachineBasicBlocks - Populate given set using machine basic blocks which
Eric Christopher6211e4b2013-11-20 00:54:19 +0000263/// have machine instructions that belong to lexical scope identified by
Devang Patele1649c32011-08-10 19:04:06 +0000264/// DebugLoc.
Eric Christopher6211e4b2013-11-20 00:54:19 +0000265void LexicalScopes::getMachineBasicBlocks(
266 DebugLoc DL, SmallPtrSet<const MachineBasicBlock *, 4> &MBBs) {
Devang Patele1649c32011-08-10 19:04:06 +0000267 MBBs.clear();
268 LexicalScope *Scope = getOrCreateLexicalScope(DL);
269 if (!Scope)
270 return;
Eric Christopher6211e4b2013-11-20 00:54:19 +0000271
Devang Pateldb4374a2011-08-12 18:01:34 +0000272 if (Scope == CurrentFnLexicalScope) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000273 for (const auto &MBB : *MF)
274 MBBs.insert(&MBB);
Devang Pateldb4374a2011-08-12 18:01:34 +0000275 return;
276 }
277
Craig Topper80170e52013-07-03 04:30:58 +0000278 SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges();
Craig Topper24fd7ee2013-07-03 04:42:33 +0000279 for (SmallVectorImpl<InsnRange>::iterator I = InsnRanges.begin(),
Eric Christopher6211e4b2013-11-20 00:54:19 +0000280 E = InsnRanges.end();
281 I != E; ++I) {
Devang Patele1649c32011-08-10 19:04:06 +0000282 InsnRange &R = *I;
283 MBBs.insert(R.first->getParent());
284 }
285}
286
287/// dominates - Return true if DebugLoc's lexical scope dominates at least one
288/// machine instruction's lexical scope in a given machine basic block.
289bool LexicalScopes::dominates(DebugLoc DL, MachineBasicBlock *MBB) {
290 LexicalScope *Scope = getOrCreateLexicalScope(DL);
291 if (!Scope)
292 return false;
Devang Pateldb4374a2011-08-12 18:01:34 +0000293
294 // Current function scope covers all basic blocks in the function.
295 if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
296 return true;
297
Devang Patele1649c32011-08-10 19:04:06 +0000298 bool Result = false;
Eric Christopher6211e4b2013-11-20 00:54:19 +0000299 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
300 ++I) {
Devang Patele1649c32011-08-10 19:04:06 +0000301 DebugLoc IDL = I->getDebugLoc();
302 if (IDL.isUnknown())
303 continue;
304 if (LexicalScope *IScope = getOrCreateLexicalScope(IDL))
305 if (Scope->dominates(IScope))
306 return true;
307 }
308 return Result;
309}
310
Devang Patele1649c32011-08-10 19:04:06 +0000311/// dump - Print data structures.
Manman Rene498b252013-02-02 00:02:03 +0000312void LexicalScope::dump(unsigned Indent) const {
Devang Patele1649c32011-08-10 19:04:06 +0000313#ifndef NDEBUG
314 raw_ostream &err = dbgs();
Manman Rene498b252013-02-02 00:02:03 +0000315 err.indent(Indent);
Devang Patele1649c32011-08-10 19:04:06 +0000316 err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n";
317 const MDNode *N = Desc;
Manman Rene498b252013-02-02 00:02:03 +0000318 err.indent(Indent);
Devang Patele1649c32011-08-10 19:04:06 +0000319 N->dump();
320 if (AbstractScope)
Manman Rene498b252013-02-02 00:02:03 +0000321 err << std::string(Indent, ' ') << "Abstract Scope\n";
Devang Patele1649c32011-08-10 19:04:06 +0000322
Devang Patele1649c32011-08-10 19:04:06 +0000323 if (!Children.empty())
Manman Rene498b252013-02-02 00:02:03 +0000324 err << std::string(Indent + 2, ' ') << "Children ...\n";
Devang Patele1649c32011-08-10 19:04:06 +0000325 for (unsigned i = 0, e = Children.size(); i != e; ++i)
326 if (Children[i] != this)
Manman Rene498b252013-02-02 00:02:03 +0000327 Children[i]->dump(Indent + 2);
Devang Patele1649c32011-08-10 19:04:06 +0000328#endif
329}