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"
31 class MachineBasicBlock;
32 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 /// LexicalScopes - This class provides interface to collect and use lexical
43 /// scoping information from machine instruction.
47 LexicalScopes() : MF(nullptr), CurrentFnLexicalScope(nullptr) {}
50 /// initialize - Scan machine function and constuct lexical scope nest, resets
51 /// the instance if necessary.
52 void initialize(const MachineFunction &);
54 /// releaseMemory - release memory.
57 /// empty - Return true if there is any lexical scope information available.
58 bool empty() { return CurrentFnLexicalScope == nullptr; }
60 /// isCurrentFunctionScope - Return true if given lexical scope represents
62 bool isCurrentFunctionScope(const LexicalScope *LS) {
63 return LS == CurrentFnLexicalScope;
66 /// getCurrentFunctionScope - Return lexical scope for the current function.
67 LexicalScope *getCurrentFunctionScope() const {
68 return CurrentFnLexicalScope;
71 /// getMachineBasicBlocks - Populate given set using machine basic blocks
72 /// which have machine instructions that belong to lexical scope identified by
74 void getMachineBasicBlocks(DebugLoc DL,
75 SmallPtrSet<const MachineBasicBlock *, 4> &MBBs);
77 /// dominates - Return true if DebugLoc's lexical scope dominates at least one
78 /// machine instruction's lexical scope in a given machine basic block.
79 bool dominates(DebugLoc DL, MachineBasicBlock *MBB);
81 /// findLexicalScope - Find lexical scope, either regular or inlined, for the
82 /// given DebugLoc. Return NULL if not found.
83 LexicalScope *findLexicalScope(DebugLoc DL);
85 /// getAbstractScopesList - Return a reference to list of abstract scopes.
86 ArrayRef<LexicalScope *> getAbstractScopesList() const {
87 return AbstractScopesList;
90 /// findAbstractScope - Find an abstract scope or return NULL.
91 LexicalScope *findAbstractScope(const MDNode *N) {
92 return AbstractScopeMap.lookup(N);
95 /// findInlinedScope - Find an inlined scope for the given DebugLoc or return
97 LexicalScope *findInlinedScope(DebugLoc DL) {
98 return InlinedLexicalScopeMap.lookup(DL);
101 /// findLexicalScope - Find regular lexical scope or return NULL.
102 LexicalScope *findLexicalScope(const MDNode *N) {
103 return LexicalScopeMap.lookup(N);
106 /// dump - Print data structures to dbgs().
110 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
111 /// not available then create new lexical scope.
112 LexicalScope *getOrCreateLexicalScope(DebugLoc DL);
114 /// getOrCreateRegularScope - Find or create a regular lexical scope.
115 LexicalScope *getOrCreateRegularScope(MDNode *Scope);
117 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
118 LexicalScope *getOrCreateInlinedScope(MDNode *Scope, MDNode *InlinedAt);
120 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
121 LexicalScope *getOrCreateAbstractScope(const MDNode *N);
123 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
124 /// for the given machine function.
125 void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
126 DenseMap<const MachineInstr *, LexicalScope *> &M);
127 void constructScopeNest(LexicalScope *Scope);
129 assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
130 DenseMap<const MachineInstr *, LexicalScope *> &M);
133 const MachineFunction *MF;
135 /// LexicalScopeMap - Tracks the scopes in the current function. Owns the
136 /// contained LexicalScope*s.
137 DenseMap<const MDNode *, LexicalScope *> LexicalScopeMap;
139 /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
141 DenseMap<DebugLoc, LexicalScope *> InlinedLexicalScopeMap;
143 /// AbstractScopeMap - These scopes are not included LexicalScopeMap.
144 /// AbstractScopes owns its LexicalScope*s.
145 DenseMap<const MDNode *, LexicalScope *> AbstractScopeMap;
147 /// AbstractScopesList - Tracks abstract scopes constructed while processing
149 SmallVector<LexicalScope *, 4> AbstractScopesList;
151 /// CurrentFnLexicalScope - Top level scope for the current function.
153 LexicalScope *CurrentFnLexicalScope;
156 //===----------------------------------------------------------------------===//
157 /// LexicalScope - This class is used to track scope information.
162 LexicalScope(LexicalScope *P, const MDNode *D, const MDNode *I, bool A)
163 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A),
164 LastInsn(nullptr), FirstInsn(nullptr), DFSIn(0), DFSOut(0) {
166 Parent->addChild(this);
170 LexicalScope *getParent() const { return Parent; }
171 const MDNode *getDesc() const { return Desc; }
172 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
173 const MDNode *getScopeNode() const { return Desc; }
174 bool isAbstractScope() const { return AbstractScope; }
175 SmallVectorImpl<LexicalScope *> &getChildren() { return Children; }
176 SmallVectorImpl<InsnRange> &getRanges() { return Ranges; }
178 /// addChild - Add a child scope.
179 void addChild(LexicalScope *S) { Children.push_back(S); }
181 /// openInsnRange - This scope covers instruction range starting from MI.
182 void openInsnRange(const MachineInstr *MI) {
187 Parent->openInsnRange(MI);
190 /// extendInsnRange - Extend the current instruction range covered by
192 void extendInsnRange(const MachineInstr *MI) {
193 assert(FirstInsn && "MI Range is not open!");
196 Parent->extendInsnRange(MI);
199 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
200 /// until now. This is used when a new scope is encountered while walking
201 /// machine instructions.
202 void closeInsnRange(LexicalScope *NewScope = nullptr) {
203 assert(LastInsn && "Last insn missing!");
204 Ranges.push_back(InsnRange(FirstInsn, LastInsn));
207 // If Parent dominates NewScope then do not close Parent's instruction
209 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
210 Parent->closeInsnRange(NewScope);
213 /// dominates - Return true if current scope dominates given lexical scope.
214 bool dominates(const LexicalScope *S) const {
217 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
222 // Depth First Search support to walk and manipulate LexicalScope hierarchy.
223 unsigned getDFSOut() const { return DFSOut; }
224 void setDFSOut(unsigned O) { DFSOut = O; }
225 unsigned getDFSIn() const { return DFSIn; }
226 void setDFSIn(unsigned I) { DFSIn = I; }
228 /// dump - print lexical scope.
229 void dump(unsigned Indent = 0) const;
232 LexicalScope *Parent; // Parent to this scope.
233 AssertingVH<const MDNode> Desc; // Debug info descriptor.
234 AssertingVH<const MDNode> InlinedAtLocation; // Location at which this
236 bool AbstractScope; // Abstract Scope
237 SmallVector<LexicalScope *, 4> Children; // Scopes defined in scope.
238 // Contents not owned.
239 SmallVector<InsnRange, 4> Ranges;
241 const MachineInstr *LastInsn; // Last instruction of this scope.
242 const MachineInstr *FirstInsn; // First instruction of this scope.
243 unsigned DFSIn, DFSOut; // In & Out Depth use to determine
247 } // end llvm namespace