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/Metadata.h"
25 #include "llvm/Support/DebugLoc.h"
26 #include "llvm/Support/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(NULL), CurrentFnLexicalScope(NULL) {}
48 virtual ~LexicalScopes();
50 /// initialize - Scan machine function and constuct lexical scope nest.
51 virtual void initialize(const MachineFunction &);
53 /// releaseMemory - release memory.
54 virtual void releaseMemory();
56 /// empty - Return true if there is any lexical scope information available.
57 bool empty() { return CurrentFnLexicalScope == NULL; }
59 /// isCurrentFunctionScope - Return true if given lexical scope represents
61 bool isCurrentFunctionScope(const LexicalScope *LS) {
62 return LS == CurrentFnLexicalScope;
65 /// getCurrentFunctionScope - Return lexical scope for the current function.
66 LexicalScope *getCurrentFunctionScope() const {
67 return CurrentFnLexicalScope;
70 /// getMachineBasicBlocks - Populate given set using machine basic blocks
71 /// which have machine instructions that belong to lexical scope identified by
73 void getMachineBasicBlocks(DebugLoc DL,
74 SmallPtrSet<const MachineBasicBlock *, 4> &MBBs);
76 /// dominates - Return true if DebugLoc's lexical scope dominates at least one
77 /// machine instruction's lexical scope in a given machine basic block.
78 bool dominates(DebugLoc DL, MachineBasicBlock *MBB);
80 /// findLexicalScope - Find lexical scope, either regular or inlined, for the
81 /// given DebugLoc. Return NULL if not found.
82 LexicalScope *findLexicalScope(DebugLoc DL);
84 /// getAbstractScopesList - Return a reference to list of abstract scopes.
85 ArrayRef<LexicalScope *> getAbstractScopesList() const {
86 return AbstractScopesList;
89 /// findAbstractScope - Find an abstract scope or return NULL.
90 LexicalScope *findAbstractScope(const MDNode *N) {
91 return AbstractScopeMap.lookup(N);
94 /// findInlinedScope - Find an inlined scope for the given DebugLoc or return
96 LexicalScope *findInlinedScope(DebugLoc DL) {
97 return InlinedLexicalScopeMap.lookup(DL);
100 /// findLexicalScope - Find regular lexical scope or return NULL.
101 LexicalScope *findLexicalScope(const MDNode *N) {
102 return LexicalScopeMap.lookup(N);
105 /// dump - Print data structures to dbgs().
109 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
110 /// not available then create new lexical scope.
111 LexicalScope *getOrCreateLexicalScope(DebugLoc DL);
113 /// getOrCreateRegularScope - Find or create a regular lexical scope.
114 LexicalScope *getOrCreateRegularScope(MDNode *Scope);
116 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
117 LexicalScope *getOrCreateInlinedScope(MDNode *Scope, MDNode *InlinedAt);
119 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
120 LexicalScope *getOrCreateAbstractScope(const MDNode *N);
122 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
123 /// for the given machine function.
124 void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
125 DenseMap<const MachineInstr *, LexicalScope *> &M);
126 void constructScopeNest(LexicalScope *Scope);
128 assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
129 DenseMap<const MachineInstr *, LexicalScope *> &M);
132 const MachineFunction *MF;
134 /// LexicalScopeMap - Tracks the scopes in the current function. Owns the
135 /// contained LexicalScope*s.
136 DenseMap<const MDNode *, LexicalScope *> LexicalScopeMap;
138 /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
140 DenseMap<DebugLoc, LexicalScope *> InlinedLexicalScopeMap;
142 /// AbstractScopeMap - These scopes are not included LexicalScopeMap.
143 /// AbstractScopes owns its LexicalScope*s.
144 DenseMap<const MDNode *, LexicalScope *> AbstractScopeMap;
146 /// AbstractScopesList - Tracks abstract scopes constructed while processing
148 SmallVector<LexicalScope *, 4> AbstractScopesList;
150 /// CurrentFnLexicalScope - Top level scope for the current function.
152 LexicalScope *CurrentFnLexicalScope;
155 //===----------------------------------------------------------------------===//
156 /// LexicalScope - This class is used to track scope information.
159 virtual void anchor();
162 LexicalScope(LexicalScope *P, const MDNode *D, const MDNode *I, bool A)
163 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A), LastInsn(0),
164 FirstInsn(0), DFSIn(0), DFSOut(0) {
166 Parent->addChild(this);
169 virtual ~LexicalScope() {}
172 LexicalScope *getParent() const { return Parent; }
173 const MDNode *getDesc() const { return Desc; }
174 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
175 const MDNode *getScopeNode() const { return Desc; }
176 bool isAbstractScope() const { return AbstractScope; }
177 SmallVectorImpl<LexicalScope *> &getChildren() { return Children; }
178 SmallVectorImpl<InsnRange> &getRanges() { return Ranges; }
180 /// addChild - Add a child scope.
181 void addChild(LexicalScope *S) { Children.push_back(S); }
183 /// openInsnRange - This scope covers instruction range starting from MI.
184 void openInsnRange(const MachineInstr *MI) {
189 Parent->openInsnRange(MI);
192 /// extendInsnRange - Extend the current instruction range covered by
194 void extendInsnRange(const MachineInstr *MI) {
195 assert(FirstInsn && "MI Range is not open!");
198 Parent->extendInsnRange(MI);
201 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
202 /// until now. This is used when a new scope is encountered while walking
203 /// machine instructions.
204 void closeInsnRange(LexicalScope *NewScope = NULL) {
205 assert(LastInsn && "Last insn missing!");
206 Ranges.push_back(InsnRange(FirstInsn, LastInsn));
209 // If Parent dominates NewScope then do not close Parent's instruction
211 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
212 Parent->closeInsnRange(NewScope);
215 /// dominates - Return true if current scope dominates given lexical scope.
216 bool dominates(const LexicalScope *S) const {
219 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
224 // Depth First Search support to walk and manipulate LexicalScope hierarchy.
225 unsigned getDFSOut() const { return DFSOut; }
226 void setDFSOut(unsigned O) { DFSOut = O; }
227 unsigned getDFSIn() const { return DFSIn; }
228 void setDFSIn(unsigned I) { DFSIn = I; }
230 /// dump - print lexical scope.
231 void dump(unsigned Indent = 0) const;
234 LexicalScope *Parent; // Parent to this scope.
235 AssertingVH<const MDNode> Desc; // Debug info descriptor.
236 AssertingVH<const MDNode> InlinedAtLocation; // Location at which this
238 bool AbstractScope; // Abstract Scope
239 SmallVector<LexicalScope *, 4> Children; // Scopes defined in scope.
240 // Contents not owned.
241 SmallVector<InsnRange, 4> Ranges;
243 const MachineInstr *LastInsn; // Last instruction of this scope.
244 const MachineInstr *FirstInsn; // First instruction of this scope.
245 unsigned DFSIn, DFSOut; // In & Out Depth use to determine
249 } // end llvm namespace