blob: 40ee7ea785f0b84ecd36b3a4d48033b48ad289e6 [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
Eugene Zelenko5db84df2017-02-17 21:43:25 +000017#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/SmallVector.h"
Devang Patele1649c32011-08-10 19:04:06 +000019#include "llvm/CodeGen/LexicalScopes.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Devang Patele1649c32011-08-10 19:04:06 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000023#include "llvm/IR/DebugInfoMetadata.h"
24#include "llvm/IR/Metadata.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/Compiler.h"
Devang Patele1649c32011-08-10 19:04:06 +000027#include "llvm/Support/Debug.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000028#include "llvm/Support/raw_ostream.h"
29#include <cassert>
30#include <string>
31#include <tuple>
32#include <utility>
33
Devang Patele1649c32011-08-10 19:04:06 +000034using namespace llvm;
35
Chandler Carruth1b9dde02014-04-22 02:02:50 +000036#define DEBUG_TYPE "lexicalscopes"
37
Eric Christopherb7dee8a2013-11-20 00:54:28 +000038/// reset - Reset the instance so that it's prepared for another function.
39void LexicalScopes::reset() {
Craig Topperc0196b12014-04-14 00:51:57 +000040 MF = nullptr;
41 CurrentFnLexicalScope = nullptr;
David Blaikie2f143e02014-05-08 22:24:51 +000042 LexicalScopeMap.clear();
43 AbstractScopeMap.clear();
Devang Patele1649c32011-08-10 19:04:06 +000044 InlinedLexicalScopeMap.clear();
45 AbstractScopesList.clear();
46}
47
48/// initialize - Scan machine function and constuct lexical scope nest.
49void LexicalScopes::initialize(const MachineFunction &Fn) {
Teresa Johnson76b5b742017-02-17 00:21:19 +000050 // Don't attempt any lexical scope creation for a NoDebug compile unit.
51 if (Fn.getFunction()->getSubprogram()->getUnit()->getEmissionKind() ==
52 DICompileUnit::NoDebug)
53 return;
Eric Christopherb7dee8a2013-11-20 00:54:28 +000054 reset();
Devang Patele1649c32011-08-10 19:04:06 +000055 MF = &Fn;
56 SmallVector<InsnRange, 4> MIRanges;
57 DenseMap<const MachineInstr *, LexicalScope *> MI2ScopeMap;
58 extractLexicalScopes(MIRanges, MI2ScopeMap);
59 if (CurrentFnLexicalScope) {
60 constructScopeNest(CurrentFnLexicalScope);
61 assignInstructionRanges(MIRanges, MI2ScopeMap);
62 }
63}
64
65/// extractLexicalScopes - Extract instruction ranges for each lexical scopes
66/// for the given machine function.
Eric Christopher6211e4b2013-11-20 00:54:19 +000067void LexicalScopes::extractLexicalScopes(
68 SmallVectorImpl<InsnRange> &MIRanges,
69 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
Devang Patele1649c32011-08-10 19:04:06 +000070 // Scan each instruction and create scopes. First build working set of scopes.
Alexey Samsonov41b977d2014-04-30 18:29:51 +000071 for (const auto &MBB : *MF) {
Craig Topperc0196b12014-04-14 00:51:57 +000072 const MachineInstr *RangeBeginMI = nullptr;
73 const MachineInstr *PrevMI = nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000074 const DILocation *PrevDL = nullptr;
Alexey Samsonovf74bde62014-04-30 22:17:38 +000075 for (const auto &MInsn : MBB) {
Devang Patele1649c32011-08-10 19:04:06 +000076 // Check if instruction has valid location information.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000077 const DILocation *MIDL = MInsn.getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +000078 if (!MIDL) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +000079 PrevMI = &MInsn;
Devang Patele1649c32011-08-10 19:04:06 +000080 continue;
81 }
82
83 // If scope has not changed then skip this instruction.
84 if (MIDL == PrevDL) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +000085 PrevMI = &MInsn;
Devang Patele1649c32011-08-10 19:04:06 +000086 continue;
87 }
88
Adrian Prantlfb31da12017-05-22 20:47:09 +000089 // Ignore DBG_VALUE and similar instruction that do not contribute to any
90 // instruction in the output.
91 if (MInsn.isMetaInstruction())
Devang Patele1649c32011-08-10 19:04:06 +000092 continue;
93
94 if (RangeBeginMI) {
95 // If we have already seen a beginning of an instruction range and
96 // current instruction scope does not match scope of first instruction
97 // in this range then create a new instruction range.
98 InsnRange R(RangeBeginMI, PrevMI);
99 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
100 MIRanges.push_back(R);
101 }
102
103 // This is a beginning of a new instruction range.
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000104 RangeBeginMI = &MInsn;
Devang Patele1649c32011-08-10 19:04:06 +0000105
106 // Reset previous markers.
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000107 PrevMI = &MInsn;
Devang Patele1649c32011-08-10 19:04:06 +0000108 PrevDL = MIDL;
109 }
110
111 // Create last instruction range.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000112 if (RangeBeginMI && PrevMI && PrevDL) {
Devang Patele1649c32011-08-10 19:04:06 +0000113 InsnRange R(RangeBeginMI, PrevMI);
114 MIRanges.push_back(R);
115 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
116 }
117 }
118}
119
120/// findLexicalScope - Find lexical scope, either regular or inlined, for the
121/// given DebugLoc. Return NULL if not found.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000122LexicalScope *LexicalScopes::findLexicalScope(const DILocation *DL) {
123 DILocalScope *Scope = DL->getScope();
Eric Christopher6211e4b2013-11-20 00:54:19 +0000124 if (!Scope)
Craig Topperc0196b12014-04-14 00:51:57 +0000125 return nullptr;
Eric Christopher6647b832011-10-11 22:59:11 +0000126
127 // The scope that we were created with could have an extra file - which
128 // isn't what we care about in this case.
Amjad Abouda5ba9912016-04-21 16:58:49 +0000129 Scope = Scope->getNonLexicalBlockFileScope();
Eric Christopher6211e4b2013-11-20 00:54:19 +0000130
Duncan P. N. Exon Smith5a227ff2015-03-30 21:54:46 +0000131 if (auto *IA = DL->getInlinedAt()) {
David Blaikie9b8c8cd2014-05-14 01:08:28 +0000132 auto I = InlinedLexicalScopeMap.find(std::make_pair(Scope, IA));
133 return I != InlinedLexicalScopeMap.end() ? &I->second : nullptr;
134 }
David Blaikie2f143e02014-05-08 22:24:51 +0000135 return findLexicalScope(Scope);
Devang Patele1649c32011-08-10 19:04:06 +0000136}
137
138/// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
139/// not available then create new lexical scope.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000140LexicalScope *LexicalScopes::getOrCreateLexicalScope(const DILocalScope *Scope,
141 const DILocation *IA) {
Duncan P. N. Exon Smith82eba742015-03-30 23:47:26 +0000142 if (IA) {
Teresa Johnson76b5b742017-02-17 00:21:19 +0000143 // Skip scopes inlined from a NoDebug compile unit.
144 if (Scope->getSubprogram()->getUnit()->getEmissionKind() ==
145 DICompileUnit::NoDebug)
146 return getOrCreateLexicalScope(IA);
Devang Patele1649c32011-08-10 19:04:06 +0000147 // Create an abstract scope for inlined function.
148 getOrCreateAbstractScope(Scope);
149 // Create an inlined scope for inlined function.
Duncan P. N. Exon Smith82eba742015-03-30 23:47:26 +0000150 return getOrCreateInlinedScope(Scope, IA);
Devang Patele1649c32011-08-10 19:04:06 +0000151 }
Eric Christopher6211e4b2013-11-20 00:54:19 +0000152
Devang Patele1649c32011-08-10 19:04:06 +0000153 return getOrCreateRegularScope(Scope);
154}
155
156/// getOrCreateRegularScope - Find or create a regular lexical scope.
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000157LexicalScope *
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000158LexicalScopes::getOrCreateRegularScope(const DILocalScope *Scope) {
Amjad Abouda5ba9912016-04-21 16:58:49 +0000159 assert(Scope && "Invalid Scope encoding!");
160 Scope = Scope->getNonLexicalBlockFileScope();
Eric Christopher6211e4b2013-11-20 00:54:19 +0000161
David Blaikie2f143e02014-05-08 22:24:51 +0000162 auto I = LexicalScopeMap.find(Scope);
163 if (I != LexicalScopeMap.end())
164 return &I->second;
Devang Patele1649c32011-08-10 19:04:06 +0000165
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000166 // FIXME: Should the following dyn_cast be DILexicalBlock?
Craig Topperc0196b12014-04-14 00:51:57 +0000167 LexicalScope *Parent = nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000168 if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
Duncan P. N. Exon Smith82eba742015-03-30 23:47:26 +0000169 Parent = getOrCreateLexicalScope(Block->getScope());
Aaron Ballmanf17583e2015-02-16 18:21:19 +0000170 I = LexicalScopeMap.emplace(std::piecewise_construct,
171 std::forward_as_tuple(Scope),
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000172 std::forward_as_tuple(Parent, Scope, nullptr,
173 false)).first;
David Blaikie2f143e02014-05-08 22:24:51 +0000174
David Blaikie3dfe4782014-10-14 18:22:52 +0000175 if (!Parent) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000176 assert(cast<DISubprogram>(Scope)->describes(MF->getFunction()));
David Blaikie3dfe4782014-10-14 18:22:52 +0000177 assert(!CurrentFnLexicalScope);
David Blaikie2f143e02014-05-08 22:24:51 +0000178 CurrentFnLexicalScope = &I->second;
David Blaikie3dfe4782014-10-14 18:22:52 +0000179 }
Eric Christopher6211e4b2013-11-20 00:54:19 +0000180
David Blaikie2f143e02014-05-08 22:24:51 +0000181 return &I->second;
Devang Patele1649c32011-08-10 19:04:06 +0000182}
183
184/// getOrCreateInlinedScope - Find or create an inlined lexical scope.
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000185LexicalScope *
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000186LexicalScopes::getOrCreateInlinedScope(const DILocalScope *Scope,
187 const DILocation *InlinedAt) {
Amjad Abouda5ba9912016-04-21 16:58:49 +0000188 assert(Scope && "Invalid Scope encoding!");
189 Scope = Scope->getNonLexicalBlockFileScope();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000190 std::pair<const DILocalScope *, const DILocation *> P(Scope, InlinedAt);
David Blaikie9b8c8cd2014-05-14 01:08:28 +0000191 auto I = InlinedLexicalScopeMap.find(P);
192 if (I != InlinedLexicalScopeMap.end())
David Blaikie2f143e02014-05-08 22:24:51 +0000193 return &I->second;
Devang Patele1649c32011-08-10 19:04:06 +0000194
David Blaikie9b8c8cd2014-05-14 01:08:28 +0000195 LexicalScope *Parent;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000196 if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000197 Parent = getOrCreateInlinedScope(Block->getScope(), InlinedAt);
David Blaikie9b8c8cd2014-05-14 01:08:28 +0000198 else
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000199 Parent = getOrCreateLexicalScope(InlinedAt);
David Blaikie9b8c8cd2014-05-14 01:08:28 +0000200
Eric Christopher14303d12017-02-14 19:43:50 +0000201 I = InlinedLexicalScopeMap
202 .emplace(std::piecewise_construct, std::forward_as_tuple(P),
203 std::forward_as_tuple(Parent, Scope, InlinedAt, false))
Aaron Ballmanf17583e2015-02-16 18:21:19 +0000204 .first;
David Blaikie2f143e02014-05-08 22:24:51 +0000205 return &I->second;
Devang Patele1649c32011-08-10 19:04:06 +0000206}
207
208/// getOrCreateAbstractScope - Find or create an abstract lexical scope.
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000209LexicalScope *
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000210LexicalScopes::getOrCreateAbstractScope(const DILocalScope *Scope) {
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000211 assert(Scope && "Invalid Scope encoding!");
Amjad Abouda5ba9912016-04-21 16:58:49 +0000212 Scope = Scope->getNonLexicalBlockFileScope();
David Blaikieea862262014-05-25 18:11:35 +0000213 auto I = AbstractScopeMap.find(Scope);
David Blaikie2f143e02014-05-08 22:24:51 +0000214 if (I != AbstractScopeMap.end())
215 return &I->second;
Devang Patele1649c32011-08-10 19:04:06 +0000216
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000217 // FIXME: Should the following isa be DILexicalBlock?
Craig Topperc0196b12014-04-14 00:51:57 +0000218 LexicalScope *Parent = nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000219 if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000220 Parent = getOrCreateAbstractScope(Block->getScope());
221
David Blaikie2f143e02014-05-08 22:24:51 +0000222 I = AbstractScopeMap.emplace(std::piecewise_construct,
David Blaikieea862262014-05-25 18:11:35 +0000223 std::forward_as_tuple(Scope),
224 std::forward_as_tuple(Parent, Scope,
David Blaikie2f143e02014-05-08 22:24:51 +0000225 nullptr, true)).first;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000226 if (isa<DISubprogram>(Scope))
David Blaikie2f143e02014-05-08 22:24:51 +0000227 AbstractScopesList.push_back(&I->second);
228 return &I->second;
Devang Patele1649c32011-08-10 19:04:06 +0000229}
230
231/// constructScopeNest
232void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
Eric Christopher6211e4b2013-11-20 00:54:19 +0000233 assert(Scope && "Unable to calculate scope dominance graph!");
Devang Patele1649c32011-08-10 19:04:06 +0000234 SmallVector<LexicalScope *, 4> WorkStack;
235 WorkStack.push_back(Scope);
236 unsigned Counter = 0;
237 while (!WorkStack.empty()) {
238 LexicalScope *WS = WorkStack.back();
Craig Topper80170e52013-07-03 04:30:58 +0000239 const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
Devang Patele1649c32011-08-10 19:04:06 +0000240 bool visitedChildren = false;
Adrian Prantl16b2ace2016-09-28 17:31:17 +0000241 for (auto &ChildScope : Children)
Devang Patele1649c32011-08-10 19:04:06 +0000242 if (!ChildScope->getDFSOut()) {
243 WorkStack.push_back(ChildScope);
244 visitedChildren = true;
245 ChildScope->setDFSIn(++Counter);
246 break;
247 }
Devang Patele1649c32011-08-10 19:04:06 +0000248 if (!visitedChildren) {
249 WorkStack.pop_back();
250 WS->setDFSOut(++Counter);
251 }
252 }
253}
254
Devang Patel784077e2011-08-10 23:58:09 +0000255/// assignInstructionRanges - Find ranges of instructions covered by each
256/// lexical scope.
Eric Christopher6211e4b2013-11-20 00:54:19 +0000257void LexicalScopes::assignInstructionRanges(
258 SmallVectorImpl<InsnRange> &MIRanges,
259 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
Craig Topperc0196b12014-04-14 00:51:57 +0000260 LexicalScope *PrevLexicalScope = nullptr;
Adrian Prantl16b2ace2016-09-28 17:31:17 +0000261 for (const auto &R : MIRanges) {
Devang Patele1649c32011-08-10 19:04:06 +0000262 LexicalScope *S = MI2ScopeMap.lookup(R.first);
Eric Christopher6211e4b2013-11-20 00:54:19 +0000263 assert(S && "Lost LexicalScope for a machine instruction!");
Devang Patele1649c32011-08-10 19:04:06 +0000264 if (PrevLexicalScope && !PrevLexicalScope->dominates(S))
265 PrevLexicalScope->closeInsnRange(S);
266 S->openInsnRange(R.first);
267 S->extendInsnRange(R.second);
268 PrevLexicalScope = S;
269 }
270
271 if (PrevLexicalScope)
272 PrevLexicalScope->closeInsnRange();
273}
274
275/// getMachineBasicBlocks - Populate given set using machine basic blocks which
Eric Christopher6211e4b2013-11-20 00:54:19 +0000276/// have machine instructions that belong to lexical scope identified by
Devang Patele1649c32011-08-10 19:04:06 +0000277/// DebugLoc.
Eric Christopher6211e4b2013-11-20 00:54:19 +0000278void LexicalScopes::getMachineBasicBlocks(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000279 const DILocation *DL, SmallPtrSetImpl<const MachineBasicBlock *> &MBBs) {
Devang Patele1649c32011-08-10 19:04:06 +0000280 MBBs.clear();
281 LexicalScope *Scope = getOrCreateLexicalScope(DL);
282 if (!Scope)
283 return;
Eric Christopher6211e4b2013-11-20 00:54:19 +0000284
Devang Pateldb4374a2011-08-12 18:01:34 +0000285 if (Scope == CurrentFnLexicalScope) {
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000286 for (const auto &MBB : *MF)
287 MBBs.insert(&MBB);
Devang Pateldb4374a2011-08-12 18:01:34 +0000288 return;
289 }
290
Craig Topper80170e52013-07-03 04:30:58 +0000291 SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges();
Adrian Prantl16b2ace2016-09-28 17:31:17 +0000292 for (auto &R : InsnRanges)
Devang Patele1649c32011-08-10 19:04:06 +0000293 MBBs.insert(R.first->getParent());
Devang Patele1649c32011-08-10 19:04:06 +0000294}
295
296/// dominates - Return true if DebugLoc's lexical scope dominates at least one
297/// machine instruction's lexical scope in a given machine basic block.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000298bool LexicalScopes::dominates(const DILocation *DL, MachineBasicBlock *MBB) {
Devang Patele1649c32011-08-10 19:04:06 +0000299 LexicalScope *Scope = getOrCreateLexicalScope(DL);
300 if (!Scope)
301 return false;
Devang Pateldb4374a2011-08-12 18:01:34 +0000302
303 // Current function scope covers all basic blocks in the function.
304 if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
305 return true;
306
Devang Patele1649c32011-08-10 19:04:06 +0000307 bool Result = false;
Adrian Prantl16b2ace2016-09-28 17:31:17 +0000308 for (auto &I : *MBB) {
309 if (const DILocation *IDL = I.getDebugLoc())
Duncan P. N. Exon Smith3386e0e2015-03-30 23:58:59 +0000310 if (LexicalScope *IScope = getOrCreateLexicalScope(IDL))
311 if (Scope->dominates(IScope))
312 return true;
Devang Patele1649c32011-08-10 19:04:06 +0000313 }
314 return Result;
315}
316
Matthias Braun8c209aa2017-01-28 02:02:38 +0000317#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
318LLVM_DUMP_METHOD void LexicalScope::dump(unsigned Indent) const {
Devang Patele1649c32011-08-10 19:04:06 +0000319 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}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000334#endif