1 //===- LexicalScopes.cpp - Collecting lexical scope info -*- C++ -*--------===//
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 #ifndef LLVM_CODEGEN_LEXICALSCOPES_H
18 #define LLVM_CODEGEN_LEXICALSCOPES_H
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/IR/DebugLoc.h"
26 #include "llvm/IR/DebugInfoMetadata.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include <unordered_map>
33 class MachineBasicBlock;
34 class MachineFunction;
36 //===----------------------------------------------------------------------===//
37 /// InsnRange - This is used to track range of instructions with identical
40 typedef std::pair<const MachineInstr *, const MachineInstr *> InsnRange;
42 //===----------------------------------------------------------------------===//
43 /// LexicalScope - This class is used to track scope information.
48 LexicalScope(LexicalScope *P, const DILocalScope *D, const DILocation *I,
50 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A),
51 LastInsn(nullptr), FirstInsn(nullptr), DFSIn(0), DFSOut(0) {
52 assert((!D || D->isResolved()) && "Expected resolved node");
53 assert((!I || I->isResolved()) && "Expected resolved node");
55 Parent->addChild(this);
59 LexicalScope *getParent() const { return Parent; }
60 const MDNode *getDesc() const { return Desc; }
61 const DILocation *getInlinedAt() const { return InlinedAtLocation; }
62 const DILocalScope *getScopeNode() const { return Desc; }
63 bool isAbstractScope() const { return AbstractScope; }
64 SmallVectorImpl<LexicalScope *> &getChildren() { return Children; }
65 SmallVectorImpl<InsnRange> &getRanges() { return Ranges; }
67 /// addChild - Add a child scope.
68 void addChild(LexicalScope *S) { Children.push_back(S); }
70 /// openInsnRange - This scope covers instruction range starting from MI.
71 void openInsnRange(const MachineInstr *MI) {
76 Parent->openInsnRange(MI);
79 /// extendInsnRange - Extend the current instruction range covered by
81 void extendInsnRange(const MachineInstr *MI) {
82 assert(FirstInsn && "MI Range is not open!");
85 Parent->extendInsnRange(MI);
88 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
89 /// until now. This is used when a new scope is encountered while walking
90 /// machine instructions.
91 void closeInsnRange(LexicalScope *NewScope = nullptr) {
92 assert(LastInsn && "Last insn missing!");
93 Ranges.push_back(InsnRange(FirstInsn, LastInsn));
96 // If Parent dominates NewScope then do not close Parent's instruction
98 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
99 Parent->closeInsnRange(NewScope);
102 /// dominates - Return true if current scope dominates given lexical scope.
103 bool dominates(const LexicalScope *S) const {
106 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
111 // Depth First Search support to walk and manipulate LexicalScope hierarchy.
112 unsigned getDFSOut() const { return DFSOut; }
113 void setDFSOut(unsigned O) { DFSOut = O; }
114 unsigned getDFSIn() const { return DFSIn; }
115 void setDFSIn(unsigned I) { DFSIn = I; }
117 /// dump - print lexical scope.
118 void dump(unsigned Indent = 0) const;
121 LexicalScope *Parent; // Parent to this scope.
122 const DILocalScope *Desc; // Debug info descriptor.
123 const DILocation *InlinedAtLocation; // Location at which this
125 bool AbstractScope; // Abstract Scope
126 SmallVector<LexicalScope *, 4> Children; // Scopes defined in scope.
127 // Contents not owned.
128 SmallVector<InsnRange, 4> Ranges;
130 const MachineInstr *LastInsn; // Last instruction of this scope.
131 const MachineInstr *FirstInsn; // First instruction of this scope.
132 unsigned DFSIn, DFSOut; // In & Out Depth use to determine
136 //===----------------------------------------------------------------------===//
137 /// LexicalScopes - This class provides interface to collect and use lexical
138 /// scoping information from machine instruction.
140 class LexicalScopes {
142 LexicalScopes() : MF(nullptr), CurrentFnLexicalScope(nullptr) {}
144 /// initialize - Scan machine function and constuct lexical scope nest, resets
145 /// the instance if necessary.
146 void initialize(const MachineFunction &);
148 /// releaseMemory - release memory.
151 /// empty - Return true if there is any lexical scope information available.
152 bool empty() { return CurrentFnLexicalScope == nullptr; }
154 /// getCurrentFunctionScope - Return lexical scope for the current function.
155 LexicalScope *getCurrentFunctionScope() const {
156 return CurrentFnLexicalScope;
159 /// getMachineBasicBlocks - Populate given set using machine basic blocks
160 /// which have machine instructions that belong to lexical scope identified by
162 void getMachineBasicBlocks(const DILocation *DL,
163 SmallPtrSetImpl<const MachineBasicBlock *> &MBBs);
165 /// dominates - Return true if DebugLoc's lexical scope dominates at least one
166 /// machine instruction's lexical scope in a given machine basic block.
167 bool dominates(const DILocation *DL, MachineBasicBlock *MBB);
169 /// findLexicalScope - Find lexical scope, either regular or inlined, for the
170 /// given DebugLoc. Return NULL if not found.
171 LexicalScope *findLexicalScope(const DILocation *DL);
173 /// getAbstractScopesList - Return a reference to list of abstract scopes.
174 ArrayRef<LexicalScope *> getAbstractScopesList() const {
175 return AbstractScopesList;
178 /// findAbstractScope - Find an abstract scope or return null.
179 LexicalScope *findAbstractScope(const DILocalScope *N) {
180 auto I = AbstractScopeMap.find(N);
181 return I != AbstractScopeMap.end() ? &I->second : nullptr;
184 /// findInlinedScope - Find an inlined scope for the given scope/inlined-at.
185 LexicalScope *findInlinedScope(const DILocalScope *N, const DILocation *IA) {
186 auto I = InlinedLexicalScopeMap.find(std::make_pair(N, IA));
187 return I != InlinedLexicalScopeMap.end() ? &I->second : nullptr;
190 /// findLexicalScope - Find regular lexical scope or return null.
191 LexicalScope *findLexicalScope(const DILocalScope *N) {
192 auto I = LexicalScopeMap.find(N);
193 return I != LexicalScopeMap.end() ? &I->second : nullptr;
196 /// dump - Print data structures to dbgs().
199 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
200 LexicalScope *getOrCreateAbstractScope(const DILocalScope *Scope);
203 /// getOrCreateLexicalScope - Find lexical scope for the given Scope/IA. If
204 /// not available then create new lexical scope.
205 LexicalScope *getOrCreateLexicalScope(const DILocalScope *Scope,
206 const DILocation *IA = nullptr);
207 LexicalScope *getOrCreateLexicalScope(const DILocation *DL) {
208 return DL ? getOrCreateLexicalScope(DL->getScope(), DL->getInlinedAt())
212 /// getOrCreateRegularScope - Find or create a regular lexical scope.
213 LexicalScope *getOrCreateRegularScope(const DILocalScope *Scope);
215 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
216 LexicalScope *getOrCreateInlinedScope(const DILocalScope *Scope,
217 const DILocation *InlinedAt);
219 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
220 /// for the given machine function.
221 void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
222 DenseMap<const MachineInstr *, LexicalScope *> &M);
223 void constructScopeNest(LexicalScope *Scope);
225 assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
226 DenseMap<const MachineInstr *, LexicalScope *> &M);
229 const MachineFunction *MF;
231 /// LexicalScopeMap - Tracks the scopes in the current function.
232 // Use an unordered_map to ensure value pointer validity over insertion.
233 std::unordered_map<const DILocalScope *, LexicalScope> LexicalScopeMap;
235 /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
237 std::unordered_map<std::pair<const DILocalScope *, const DILocation *>,
239 pair_hash<const DILocalScope *, const DILocation *>>
240 InlinedLexicalScopeMap;
242 /// AbstractScopeMap - These scopes are not included LexicalScopeMap.
243 // Use an unordered_map to ensure value pointer validity over insertion.
244 std::unordered_map<const DILocalScope *, LexicalScope> AbstractScopeMap;
246 /// AbstractScopesList - Tracks abstract scopes constructed while processing
248 SmallVector<LexicalScope *, 4> AbstractScopesList;
250 /// CurrentFnLexicalScope - Top level scope for the current function.
252 LexicalScope *CurrentFnLexicalScope;
255 } // end llvm namespace