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 #define DEBUG_TYPE "lexicalscopes"
18 #include "llvm/CodeGen/LexicalScopes.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/IR/DebugInfo.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/FormattedStream.h"
28 /// ~LexicalScopes - final cleanup after ourselves.
29 LexicalScopes::~LexicalScopes() { reset(); }
31 /// reset - Reset the instance so that it's prepared for another function.
32 void LexicalScopes::reset() {
34 CurrentFnLexicalScope = NULL;
35 DeleteContainerSeconds(LexicalScopeMap);
36 DeleteContainerSeconds(AbstractScopeMap);
37 InlinedLexicalScopeMap.clear();
38 AbstractScopesList.clear();
41 /// initialize - Scan machine function and constuct lexical scope nest.
42 void LexicalScopes::initialize(const MachineFunction &Fn) {
45 SmallVector<InsnRange, 4> MIRanges;
46 DenseMap<const MachineInstr *, LexicalScope *> MI2ScopeMap;
47 extractLexicalScopes(MIRanges, MI2ScopeMap);
48 if (CurrentFnLexicalScope) {
49 constructScopeNest(CurrentFnLexicalScope);
50 assignInstructionRanges(MIRanges, MI2ScopeMap);
54 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
55 /// for the given machine function.
56 void LexicalScopes::extractLexicalScopes(
57 SmallVectorImpl<InsnRange> &MIRanges,
58 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
60 // Scan each instruction and create scopes. First build working set of scopes.
61 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
63 const MachineInstr *RangeBeginMI = NULL;
64 const MachineInstr *PrevMI = NULL;
66 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
68 const MachineInstr *MInsn = II;
70 // Check if instruction has valid location information.
71 const DebugLoc MIDL = MInsn->getDebugLoc();
72 if (MIDL.isUnknown()) {
77 // If scope has not changed then skip this instruction.
83 // Ignore DBG_VALUE. It does not contribute to any instruction in output.
84 if (MInsn->isDebugValue())
88 // If we have already seen a beginning of an instruction range and
89 // current instruction scope does not match scope of first instruction
90 // in this range then create a new instruction range.
91 InsnRange R(RangeBeginMI, PrevMI);
92 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
93 MIRanges.push_back(R);
96 // This is a beginning of a new instruction range.
99 // Reset previous markers.
104 // Create last instruction range.
105 if (RangeBeginMI && PrevMI && !PrevDL.isUnknown()) {
106 InsnRange R(RangeBeginMI, PrevMI);
107 MIRanges.push_back(R);
108 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
113 /// findLexicalScope - Find lexical scope, either regular or inlined, for the
114 /// given DebugLoc. Return NULL if not found.
115 LexicalScope *LexicalScopes::findLexicalScope(DebugLoc DL) {
116 MDNode *Scope = NULL;
118 DL.getScopeAndInlinedAt(Scope, IA, MF->getFunction()->getContext());
122 // The scope that we were created with could have an extra file - which
123 // isn't what we care about in this case.
124 DIDescriptor D = DIDescriptor(Scope);
125 if (D.isLexicalBlockFile())
126 Scope = DILexicalBlockFile(Scope).getScope();
129 return InlinedLexicalScopeMap.lookup(DebugLoc::getFromDILocation(IA));
130 return LexicalScopeMap.lookup(Scope);
133 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
134 /// not available then create new lexical scope.
135 LexicalScope *LexicalScopes::getOrCreateLexicalScope(DebugLoc DL) {
136 MDNode *Scope = NULL;
137 MDNode *InlinedAt = NULL;
138 DL.getScopeAndInlinedAt(Scope, InlinedAt, MF->getFunction()->getContext());
141 // Create an abstract scope for inlined function.
142 getOrCreateAbstractScope(Scope);
143 // Create an inlined scope for inlined function.
144 return getOrCreateInlinedScope(Scope, InlinedAt);
147 return getOrCreateRegularScope(Scope);
150 /// getOrCreateRegularScope - Find or create a regular lexical scope.
151 LexicalScope *LexicalScopes::getOrCreateRegularScope(MDNode *Scope) {
152 DIDescriptor D = DIDescriptor(Scope);
153 if (D.isLexicalBlockFile()) {
154 Scope = DILexicalBlockFile(Scope).getScope();
155 D = DIDescriptor(Scope);
158 LexicalScope *WScope = LexicalScopeMap.lookup(Scope);
162 LexicalScope *Parent = NULL;
163 if (D.isLexicalBlock())
164 Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope));
165 WScope = new LexicalScope(Parent, DIDescriptor(Scope), NULL, false);
166 LexicalScopeMap.insert(std::make_pair(Scope, WScope));
167 if (!Parent && DIDescriptor(Scope).isSubprogram() &&
168 DISubprogram(Scope).describes(MF->getFunction()))
169 CurrentFnLexicalScope = WScope;
174 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
175 LexicalScope *LexicalScopes::getOrCreateInlinedScope(MDNode *Scope,
177 LexicalScope *InlinedScope = LexicalScopeMap.lookup(InlinedAt);
181 DebugLoc InlinedLoc = DebugLoc::getFromDILocation(InlinedAt);
182 InlinedScope = new LexicalScope(getOrCreateLexicalScope(InlinedLoc),
183 DIDescriptor(Scope), InlinedAt, false);
184 InlinedLexicalScopeMap[InlinedLoc] = InlinedScope;
185 LexicalScopeMap[InlinedAt] = InlinedScope;
189 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
190 LexicalScope *LexicalScopes::getOrCreateAbstractScope(const MDNode *N) {
191 assert(N && "Invalid Scope encoding!");
193 DIDescriptor Scope(N);
194 if (Scope.isLexicalBlockFile())
195 Scope = DILexicalBlockFile(Scope).getScope();
196 LexicalScope *AScope = AbstractScopeMap.lookup(N);
200 LexicalScope *Parent = NULL;
201 if (Scope.isLexicalBlock()) {
202 DILexicalBlock DB(N);
203 DIDescriptor ParentDesc = DB.getContext();
204 Parent = getOrCreateAbstractScope(ParentDesc);
206 AScope = new LexicalScope(Parent, DIDescriptor(N), NULL, true);
207 AbstractScopeMap[N] = AScope;
208 if (DIDescriptor(N).isSubprogram())
209 AbstractScopesList.push_back(AScope);
213 /// constructScopeNest
214 void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
215 assert(Scope && "Unable to calculate scope dominance graph!");
216 SmallVector<LexicalScope *, 4> WorkStack;
217 WorkStack.push_back(Scope);
218 unsigned Counter = 0;
219 while (!WorkStack.empty()) {
220 LexicalScope *WS = WorkStack.back();
221 const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
222 bool visitedChildren = false;
223 for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
226 LexicalScope *ChildScope = *SI;
227 if (!ChildScope->getDFSOut()) {
228 WorkStack.push_back(ChildScope);
229 visitedChildren = true;
230 ChildScope->setDFSIn(++Counter);
234 if (!visitedChildren) {
235 WorkStack.pop_back();
236 WS->setDFSOut(++Counter);
241 /// assignInstructionRanges - Find ranges of instructions covered by each
243 void LexicalScopes::assignInstructionRanges(
244 SmallVectorImpl<InsnRange> &MIRanges,
245 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
247 LexicalScope *PrevLexicalScope = NULL;
248 for (SmallVectorImpl<InsnRange>::const_iterator RI = MIRanges.begin(),
251 const InsnRange &R = *RI;
252 LexicalScope *S = MI2ScopeMap.lookup(R.first);
253 assert(S && "Lost LexicalScope for a machine instruction!");
254 if (PrevLexicalScope && !PrevLexicalScope->dominates(S))
255 PrevLexicalScope->closeInsnRange(S);
256 S->openInsnRange(R.first);
257 S->extendInsnRange(R.second);
258 PrevLexicalScope = S;
261 if (PrevLexicalScope)
262 PrevLexicalScope->closeInsnRange();
265 /// getMachineBasicBlocks - Populate given set using machine basic blocks which
266 /// have machine instructions that belong to lexical scope identified by
268 void LexicalScopes::getMachineBasicBlocks(
269 DebugLoc DL, SmallPtrSet<const MachineBasicBlock *, 4> &MBBs) {
271 LexicalScope *Scope = getOrCreateLexicalScope(DL);
275 if (Scope == CurrentFnLexicalScope) {
276 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
282 SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges();
283 for (SmallVectorImpl<InsnRange>::iterator I = InsnRanges.begin(),
284 E = InsnRanges.end();
287 MBBs.insert(R.first->getParent());
291 /// dominates - Return true if DebugLoc's lexical scope dominates at least one
292 /// machine instruction's lexical scope in a given machine basic block.
293 bool LexicalScopes::dominates(DebugLoc DL, MachineBasicBlock *MBB) {
294 LexicalScope *Scope = getOrCreateLexicalScope(DL);
298 // Current function scope covers all basic blocks in the function.
299 if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
303 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
305 DebugLoc IDL = I->getDebugLoc();
308 if (LexicalScope *IScope = getOrCreateLexicalScope(IDL))
309 if (Scope->dominates(IScope))
315 /// dump - Print data structures.
316 void LexicalScope::dump(unsigned Indent) const {
318 raw_ostream &err = dbgs();
320 err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n";
321 const MDNode *N = Desc;
325 err << std::string(Indent, ' ') << "Abstract Scope\n";
327 if (!Children.empty())
328 err << std::string(Indent + 2, ' ') << "Children ...\n";
329 for (unsigned i = 0, e = Children.size(); i != e; ++i)
330 if (Children[i] != this)
331 Children[i]->dump(Indent + 2);