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/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/IR/DebugLoc.h"
26 #include "llvm/IR/Metadata.h"
27 #include "llvm/IR/ValueHandle.h"
29 #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 MDNode *D, const MDNode *I, bool A)
49 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A),
50 LastInsn(nullptr), FirstInsn(nullptr), DFSIn(0), DFSOut(0) {
52 Parent->addChild(this);
56 LexicalScope *getParent() const { return Parent; }
57 const MDNode *getDesc() const { return Desc; }
58 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
59 const MDNode *getScopeNode() const { return Desc; }
60 bool isAbstractScope() const { return AbstractScope; }
61 SmallVectorImpl<LexicalScope *> &getChildren() { return Children; }
62 SmallVectorImpl<InsnRange> &getRanges() { return Ranges; }
64 /// addChild - Add a child scope.
65 void addChild(LexicalScope *S) { Children.push_back(S); }
67 /// openInsnRange - This scope covers instruction range starting from MI.
68 void openInsnRange(const MachineInstr *MI) {
73 Parent->openInsnRange(MI);
76 /// extendInsnRange - Extend the current instruction range covered by
78 void extendInsnRange(const MachineInstr *MI) {
79 assert(FirstInsn && "MI Range is not open!");
82 Parent->extendInsnRange(MI);
85 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
86 /// until now. This is used when a new scope is encountered while walking
87 /// machine instructions.
88 void closeInsnRange(LexicalScope *NewScope = nullptr) {
89 assert(LastInsn && "Last insn missing!");
90 Ranges.push_back(InsnRange(FirstInsn, LastInsn));
93 // If Parent dominates NewScope then do not close Parent's instruction
95 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
96 Parent->closeInsnRange(NewScope);
99 /// dominates - Return true if current scope dominates given lexical scope.
100 bool dominates(const LexicalScope *S) const {
103 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
108 // Depth First Search support to walk and manipulate LexicalScope hierarchy.
109 unsigned getDFSOut() const { return DFSOut; }
110 void setDFSOut(unsigned O) { DFSOut = O; }
111 unsigned getDFSIn() const { return DFSIn; }
112 void setDFSIn(unsigned I) { DFSIn = I; }
114 /// dump - print lexical scope.
115 void dump(unsigned Indent = 0) const;
118 LexicalScope *Parent; // Parent to this scope.
119 AssertingVH<const MDNode> Desc; // Debug info descriptor.
120 AssertingVH<const MDNode> InlinedAtLocation; // Location at which this
122 bool AbstractScope; // Abstract Scope
123 SmallVector<LexicalScope *, 4> Children; // Scopes defined in scope.
124 // Contents not owned.
125 SmallVector<InsnRange, 4> Ranges;
127 const MachineInstr *LastInsn; // Last instruction of this scope.
128 const MachineInstr *FirstInsn; // First instruction of this scope.
129 unsigned DFSIn, DFSOut; // In & Out Depth use to determine
133 //===----------------------------------------------------------------------===//
134 /// LexicalScopes - This class provides interface to collect and use lexical
135 /// scoping information from machine instruction.
137 class LexicalScopes {
139 LexicalScopes() : MF(nullptr), CurrentFnLexicalScope(nullptr) {}
141 /// initialize - Scan machine function and constuct lexical scope nest, resets
142 /// the instance if necessary.
143 void initialize(const MachineFunction &);
145 /// releaseMemory - release memory.
148 /// empty - Return true if there is any lexical scope information available.
149 bool empty() { return CurrentFnLexicalScope == nullptr; }
151 /// getCurrentFunctionScope - Return lexical scope for the current function.
152 LexicalScope *getCurrentFunctionScope() const {
153 return CurrentFnLexicalScope;
156 /// getMachineBasicBlocks - Populate given set using machine basic blocks
157 /// which have machine instructions that belong to lexical scope identified by
159 void getMachineBasicBlocks(DebugLoc DL,
160 SmallPtrSetImpl<const MachineBasicBlock *> &MBBs);
162 /// dominates - Return true if DebugLoc's lexical scope dominates at least one
163 /// machine instruction's lexical scope in a given machine basic block.
164 bool dominates(DebugLoc DL, MachineBasicBlock *MBB);
166 /// findLexicalScope - Find lexical scope, either regular or inlined, for the
167 /// given DebugLoc. Return NULL if not found.
168 LexicalScope *findLexicalScope(DebugLoc DL);
170 /// getAbstractScopesList - Return a reference to list of abstract scopes.
171 ArrayRef<LexicalScope *> getAbstractScopesList() const {
172 return AbstractScopesList;
175 /// findAbstractScope - Find an abstract scope or return null.
176 LexicalScope *findAbstractScope(const MDNode *N) {
177 auto I = AbstractScopeMap.find(N);
178 return I != AbstractScopeMap.end() ? &I->second : nullptr;
181 /// findInlinedScope - Find an inlined scope for the given DebugLoc or return
183 LexicalScope *findInlinedScope(DebugLoc DL);
185 /// findLexicalScope - Find regular lexical scope or return null.
186 LexicalScope *findLexicalScope(const MDNode *N) {
187 auto I = LexicalScopeMap.find(N);
188 return I != LexicalScopeMap.end() ? &I->second : nullptr;
191 /// dump - Print data structures to dbgs().
194 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
195 LexicalScope *getOrCreateAbstractScope(const MDNode *N);
198 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
199 /// not available then create new lexical scope.
200 LexicalScope *getOrCreateLexicalScope(DebugLoc DL);
202 /// getOrCreateRegularScope - Find or create a regular lexical scope.
203 LexicalScope *getOrCreateRegularScope(MDNode *Scope);
205 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
206 LexicalScope *getOrCreateInlinedScope(MDNode *Scope, MDNode *InlinedAt);
208 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
209 /// for the given machine function.
210 void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
211 DenseMap<const MachineInstr *, LexicalScope *> &M);
212 void constructScopeNest(LexicalScope *Scope);
214 assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
215 DenseMap<const MachineInstr *, LexicalScope *> &M);
218 const MachineFunction *MF;
220 /// LexicalScopeMap - Tracks the scopes in the current function.
221 // Use an unordered_map to ensure value pointer validity over insertion.
222 std::unordered_map<const MDNode *, LexicalScope> LexicalScopeMap;
224 /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
226 std::unordered_map<std::pair<const MDNode *, const MDNode *>, LexicalScope,
227 pair_hash<const MDNode *, const MDNode *>>
228 InlinedLexicalScopeMap;
230 /// AbstractScopeMap - These scopes are not included LexicalScopeMap.
231 // Use an unordered_map to ensure value pointer validity over insertion.
232 std::unordered_map<const MDNode *, LexicalScope> AbstractScopeMap;
234 /// AbstractScopesList - Tracks abstract scopes constructed while processing
236 SmallVector<LexicalScope *, 4> AbstractScopesList;
238 /// CurrentFnLexicalScope - Top level scope for the current function.
240 LexicalScope *CurrentFnLexicalScope;
243 } // end llvm namespace