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/IR/DebugLoc.h"
25 #include "llvm/IR/Metadata.h"
26 #include "llvm/IR/ValueHandle.h"
28 #include <unordered_map>
32 class MachineBasicBlock;
33 class MachineFunction;
35 //===----------------------------------------------------------------------===//
36 /// InsnRange - This is used to track range of instructions with identical
39 typedef std::pair<const MachineInstr *, const MachineInstr *> InsnRange;
41 //===----------------------------------------------------------------------===//
42 /// LexicalScope - This class is used to track scope information.
47 LexicalScope(LexicalScope *P, const MDNode *D, const MDNode *I, bool A)
48 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A),
49 LastInsn(nullptr), FirstInsn(nullptr), DFSIn(0), DFSOut(0) {
51 Parent->addChild(this);
55 LexicalScope *getParent() const { return Parent; }
56 const MDNode *getDesc() const { return Desc; }
57 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
58 const MDNode *getScopeNode() const { return Desc; }
59 bool isAbstractScope() const { return AbstractScope; }
60 SmallVectorImpl<LexicalScope *> &getChildren() { return Children; }
61 SmallVectorImpl<InsnRange> &getRanges() { return Ranges; }
63 /// addChild - Add a child scope.
64 void addChild(LexicalScope *S) { Children.push_back(S); }
66 /// openInsnRange - This scope covers instruction range starting from MI.
67 void openInsnRange(const MachineInstr *MI) {
72 Parent->openInsnRange(MI);
75 /// extendInsnRange - Extend the current instruction range covered by
77 void extendInsnRange(const MachineInstr *MI) {
78 assert(FirstInsn && "MI Range is not open!");
81 Parent->extendInsnRange(MI);
84 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
85 /// until now. This is used when a new scope is encountered while walking
86 /// machine instructions.
87 void closeInsnRange(LexicalScope *NewScope = nullptr) {
88 assert(LastInsn && "Last insn missing!");
89 Ranges.push_back(InsnRange(FirstInsn, LastInsn));
92 // If Parent dominates NewScope then do not close Parent's instruction
94 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
95 Parent->closeInsnRange(NewScope);
98 /// dominates - Return true if current scope dominates given lexical scope.
99 bool dominates(const LexicalScope *S) const {
102 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
107 // Depth First Search support to walk and manipulate LexicalScope hierarchy.
108 unsigned getDFSOut() const { return DFSOut; }
109 void setDFSOut(unsigned O) { DFSOut = O; }
110 unsigned getDFSIn() const { return DFSIn; }
111 void setDFSIn(unsigned I) { DFSIn = I; }
113 /// dump - print lexical scope.
114 void dump(unsigned Indent = 0) const;
117 LexicalScope *Parent; // Parent to this scope.
118 AssertingVH<const MDNode> Desc; // Debug info descriptor.
119 AssertingVH<const MDNode> InlinedAtLocation; // Location at which this
121 bool AbstractScope; // Abstract Scope
122 SmallVector<LexicalScope *, 4> Children; // Scopes defined in scope.
123 // Contents not owned.
124 SmallVector<InsnRange, 4> Ranges;
126 const MachineInstr *LastInsn; // Last instruction of this scope.
127 const MachineInstr *FirstInsn; // First instruction of this scope.
128 unsigned DFSIn, DFSOut; // In & Out Depth use to determine
132 //===----------------------------------------------------------------------===//
133 /// LexicalScopes - This class provides interface to collect and use lexical
134 /// scoping information from machine instruction.
136 class LexicalScopes {
138 LexicalScopes() : MF(nullptr), CurrentFnLexicalScope(nullptr) {}
140 /// initialize - Scan machine function and constuct lexical scope nest, resets
141 /// the instance if necessary.
142 void initialize(const MachineFunction &);
144 /// releaseMemory - release memory.
147 /// empty - Return true if there is any lexical scope information available.
148 bool empty() { return CurrentFnLexicalScope == nullptr; }
150 /// isCurrentFunctionScope - Return true if given lexical scope represents
151 /// current function.
152 bool isCurrentFunctionScope(const LexicalScope *LS) {
153 return LS == CurrentFnLexicalScope;
156 /// getCurrentFunctionScope - Return lexical scope for the current function.
157 LexicalScope *getCurrentFunctionScope() const {
158 return CurrentFnLexicalScope;
161 /// getMachineBasicBlocks - Populate given set using machine basic blocks
162 /// which have machine instructions that belong to lexical scope identified by
164 void getMachineBasicBlocks(DebugLoc DL,
165 SmallPtrSet<const MachineBasicBlock *, 4> &MBBs);
167 /// dominates - Return true if DebugLoc's lexical scope dominates at least one
168 /// machine instruction's lexical scope in a given machine basic block.
169 bool dominates(DebugLoc DL, MachineBasicBlock *MBB);
171 /// findLexicalScope - Find lexical scope, either regular or inlined, for the
172 /// given DebugLoc. Return NULL if not found.
173 LexicalScope *findLexicalScope(DebugLoc DL);
175 /// getAbstractScopesList - Return a reference to list of abstract scopes.
176 ArrayRef<LexicalScope *> getAbstractScopesList() const {
177 return AbstractScopesList;
180 /// findAbstractScope - Find an abstract scope or return null.
181 LexicalScope *findAbstractScope(const MDNode *N) {
182 auto I = AbstractScopeMap.find(N);
183 return I != AbstractScopeMap.end() ? &I->second : nullptr;
186 /// findInlinedScope - Find an inlined scope for the given DebugLoc or return
188 LexicalScope *findInlinedScope(DebugLoc DL) {
189 return InlinedLexicalScopeMap.lookup(DL);
192 /// findLexicalScope - Find regular lexical scope or return null.
193 LexicalScope *findLexicalScope(const MDNode *N) {
194 auto I = LexicalScopeMap.find(N);
195 return I != LexicalScopeMap.end() ? &I->second : nullptr;
198 /// dump - Print data structures to dbgs().
202 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
203 /// not available then create new lexical scope.
204 LexicalScope *getOrCreateLexicalScope(DebugLoc DL);
206 /// getOrCreateRegularScope - Find or create a regular lexical scope.
207 LexicalScope *getOrCreateRegularScope(MDNode *Scope);
209 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
210 LexicalScope *getOrCreateInlinedScope(MDNode *Scope, MDNode *InlinedAt);
212 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
213 LexicalScope *getOrCreateAbstractScope(const MDNode *N);
215 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
216 /// for the given machine function.
217 void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
218 DenseMap<const MachineInstr *, LexicalScope *> &M);
219 void constructScopeNest(LexicalScope *Scope);
221 assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
222 DenseMap<const MachineInstr *, LexicalScope *> &M);
225 const MachineFunction *MF;
227 /// LexicalScopeMap - Tracks the scopes in the current function.
228 // Use an unordered_map to ensure value pointer validity over insertion.
229 std::unordered_map<const MDNode *, LexicalScope> LexicalScopeMap;
231 /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
233 DenseMap<DebugLoc, LexicalScope *> InlinedLexicalScopeMap;
235 /// AbstractScopeMap - These scopes are not included LexicalScopeMap.
236 // Use an unordered_map to ensure value pointer validity over insertion.
237 std::unordered_map<const MDNode *, LexicalScope> AbstractScopeMap;
239 /// AbstractScopesList - Tracks abstract scopes constructed while processing
241 SmallVector<LexicalScope *, 4> AbstractScopesList;
243 /// CurrentFnLexicalScope - Top level scope for the current function.
245 LexicalScope *CurrentFnLexicalScope;
248 } // end llvm namespace