1 //===- LexicalScopes.cpp - Collecting lexical scope info ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LexicalScopes analysis.
12 // This pass collects lexical scope information and maps machine instructions
13 // to respective lexical scopes.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/LexicalScopes.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FormattedStream.h"
27 #define DEBUG_TYPE "lexicalscopes"
29 /// ~LexicalScopes - final cleanup after ourselves.
30 LexicalScopes::~LexicalScopes() { reset(); }
32 /// reset - Reset the instance so that it's prepared for another function.
33 void LexicalScopes::reset() {
35 CurrentFnLexicalScope = nullptr;
36 DeleteContainerSeconds(LexicalScopeMap);
37 DeleteContainerSeconds(AbstractScopeMap);
38 InlinedLexicalScopeMap.clear();
39 AbstractScopesList.clear();
42 /// initialize - Scan machine function and constuct lexical scope nest.
43 void LexicalScopes::initialize(const MachineFunction &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);
55 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
56 /// for the given machine function.
57 void LexicalScopes::extractLexicalScopes(
58 SmallVectorImpl<InsnRange> &MIRanges,
59 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
61 // Scan each instruction and create scopes. First build working set of scopes.
62 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
64 const MachineInstr *RangeBeginMI = nullptr;
65 const MachineInstr *PrevMI = nullptr;
67 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
69 const MachineInstr *MInsn = II;
71 // Check if instruction has valid location information.
72 const DebugLoc MIDL = MInsn->getDebugLoc();
73 if (MIDL.isUnknown()) {
78 // If scope has not changed then skip this instruction.
84 // Ignore DBG_VALUE. It does not contribute to any instruction in output.
85 if (MInsn->isDebugValue())
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);
97 // This is a beginning of a new instruction range.
100 // Reset previous markers.
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);
114 /// findLexicalScope - Find lexical scope, either regular or inlined, for the
115 /// given DebugLoc. Return NULL if not found.
116 LexicalScope *LexicalScopes::findLexicalScope(DebugLoc DL) {
117 MDNode *Scope = nullptr;
118 MDNode *IA = nullptr;
119 DL.getScopeAndInlinedAt(Scope, IA, MF->getFunction()->getContext());
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();
130 return InlinedLexicalScopeMap.lookup(DebugLoc::getFromDILocation(IA));
131 return LexicalScopeMap.lookup(Scope);
134 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
135 /// not available then create new lexical scope.
136 LexicalScope *LexicalScopes::getOrCreateLexicalScope(DebugLoc DL) {
137 MDNode *Scope = nullptr;
138 MDNode *InlinedAt = nullptr;
139 DL.getScopeAndInlinedAt(Scope, InlinedAt, MF->getFunction()->getContext());
142 // Create an abstract scope for inlined function.
143 getOrCreateAbstractScope(Scope);
144 // Create an inlined scope for inlined function.
145 return getOrCreateInlinedScope(Scope, InlinedAt);
148 return getOrCreateRegularScope(Scope);
151 /// getOrCreateRegularScope - Find or create a regular lexical scope.
152 LexicalScope *LexicalScopes::getOrCreateRegularScope(MDNode *Scope) {
153 DIDescriptor D = DIDescriptor(Scope);
154 if (D.isLexicalBlockFile()) {
155 Scope = DILexicalBlockFile(Scope).getScope();
156 D = DIDescriptor(Scope);
159 LexicalScope *WScope = LexicalScopeMap.lookup(Scope);
163 LexicalScope *Parent = nullptr;
164 if (D.isLexicalBlock())
165 Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope));
166 WScope = new LexicalScope(Parent, DIDescriptor(Scope), nullptr, false);
167 LexicalScopeMap.insert(std::make_pair(Scope, WScope));
168 if (!Parent && DIDescriptor(Scope).isSubprogram() &&
169 DISubprogram(Scope).describes(MF->getFunction()))
170 CurrentFnLexicalScope = WScope;
175 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
176 LexicalScope *LexicalScopes::getOrCreateInlinedScope(MDNode *Scope,
178 LexicalScope *InlinedScope = LexicalScopeMap.lookup(InlinedAt);
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;
190 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
191 LexicalScope *LexicalScopes::getOrCreateAbstractScope(const MDNode *N) {
192 assert(N && "Invalid Scope encoding!");
194 DIDescriptor Scope(N);
195 if (Scope.isLexicalBlockFile())
196 Scope = DILexicalBlockFile(Scope).getScope();
197 LexicalScope *AScope = AbstractScopeMap.lookup(N);
201 LexicalScope *Parent = nullptr;
202 if (Scope.isLexicalBlock()) {
203 DILexicalBlock DB(N);
204 DIDescriptor ParentDesc = DB.getContext();
205 Parent = getOrCreateAbstractScope(ParentDesc);
207 AScope = new LexicalScope(Parent, DIDescriptor(N), nullptr, true);
208 AbstractScopeMap[N] = AScope;
209 if (DIDescriptor(N).isSubprogram())
210 AbstractScopesList.push_back(AScope);
214 /// constructScopeNest
215 void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
216 assert(Scope && "Unable to calculate scope dominance graph!");
217 SmallVector<LexicalScope *, 4> WorkStack;
218 WorkStack.push_back(Scope);
219 unsigned Counter = 0;
220 while (!WorkStack.empty()) {
221 LexicalScope *WS = WorkStack.back();
222 const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
223 bool visitedChildren = false;
224 for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
227 LexicalScope *ChildScope = *SI;
228 if (!ChildScope->getDFSOut()) {
229 WorkStack.push_back(ChildScope);
230 visitedChildren = true;
231 ChildScope->setDFSIn(++Counter);
235 if (!visitedChildren) {
236 WorkStack.pop_back();
237 WS->setDFSOut(++Counter);
242 /// assignInstructionRanges - Find ranges of instructions covered by each
244 void LexicalScopes::assignInstructionRanges(
245 SmallVectorImpl<InsnRange> &MIRanges,
246 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
248 LexicalScope *PrevLexicalScope = nullptr;
249 for (SmallVectorImpl<InsnRange>::const_iterator RI = MIRanges.begin(),
252 const InsnRange &R = *RI;
253 LexicalScope *S = MI2ScopeMap.lookup(R.first);
254 assert(S && "Lost LexicalScope for a machine instruction!");
255 if (PrevLexicalScope && !PrevLexicalScope->dominates(S))
256 PrevLexicalScope->closeInsnRange(S);
257 S->openInsnRange(R.first);
258 S->extendInsnRange(R.second);
259 PrevLexicalScope = S;
262 if (PrevLexicalScope)
263 PrevLexicalScope->closeInsnRange();
266 /// getMachineBasicBlocks - Populate given set using machine basic blocks which
267 /// have machine instructions that belong to lexical scope identified by
269 void LexicalScopes::getMachineBasicBlocks(
270 DebugLoc DL, SmallPtrSet<const MachineBasicBlock *, 4> &MBBs) {
272 LexicalScope *Scope = getOrCreateLexicalScope(DL);
276 if (Scope == CurrentFnLexicalScope) {
277 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
283 SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges();
284 for (SmallVectorImpl<InsnRange>::iterator I = InsnRanges.begin(),
285 E = InsnRanges.end();
288 MBBs.insert(R.first->getParent());
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.
294 bool LexicalScopes::dominates(DebugLoc DL, MachineBasicBlock *MBB) {
295 LexicalScope *Scope = getOrCreateLexicalScope(DL);
299 // Current function scope covers all basic blocks in the function.
300 if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
304 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
306 DebugLoc IDL = I->getDebugLoc();
309 if (LexicalScope *IScope = getOrCreateLexicalScope(IDL))
310 if (Scope->dominates(IScope))
316 /// dump - Print data structures.
317 void LexicalScope::dump(unsigned Indent) const {
319 raw_ostream &err = dbgs();
321 err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n";
322 const MDNode *N = Desc;
326 err << std::string(Indent, ' ') << "Abstract Scope\n";
328 if (!Children.empty())
329 err << std::string(Indent + 2, ' ') << "Children ...\n";
330 for (unsigned i = 0, e = Children.size(); i != e; ++i)
331 if (Children[i] != this)
332 Children[i]->dump(Indent + 2);