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