[Layering] Move DebugLoc.h into the IR library. The implementation
[oota-llvm.git] / include / llvm / CodeGen / LexicalScopes.h
1 //===- LexicalScopes.cpp - Collecting lexical scope info -*- C++ -*--------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements LexicalScopes analysis.
11 //
12 // This pass collects lexical scope information and maps machine instructions
13 // to respective lexical scopes.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_LEXICALSCOPES_H
18 #define LLVM_CODEGEN_LEXICALSCOPES_H
19
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"
27 #include <utility>
28 namespace llvm {
29
30 class MachineInstr;
31 class MachineBasicBlock;
32 class MachineFunction;
33 class LexicalScope;
34
35 //===----------------------------------------------------------------------===//
36 /// InsnRange - This is used to track range of instructions with identical
37 /// lexical scope.
38 ///
39 typedef std::pair<const MachineInstr *, const MachineInstr *> InsnRange;
40
41 //===----------------------------------------------------------------------===//
42 /// LexicalScopes -  This class provides interface to collect and use lexical
43 /// scoping information from machine instruction.
44 ///
45 class LexicalScopes {
46 public:
47   LexicalScopes() : MF(NULL), CurrentFnLexicalScope(NULL) {}
48   ~LexicalScopes();
49
50   /// initialize - Scan machine function and constuct lexical scope nest, resets
51   /// the instance if necessary.
52   void initialize(const MachineFunction &);
53
54   /// releaseMemory - release memory.
55   void reset();
56
57   /// empty - Return true if there is any lexical scope information available.
58   bool empty() { return CurrentFnLexicalScope == NULL; }
59
60   /// isCurrentFunctionScope - Return true if given lexical scope represents
61   /// current function.
62   bool isCurrentFunctionScope(const LexicalScope *LS) {
63     return LS == CurrentFnLexicalScope;
64   }
65
66   /// getCurrentFunctionScope - Return lexical scope for the current function.
67   LexicalScope *getCurrentFunctionScope() const {
68     return CurrentFnLexicalScope;
69   }
70
71   /// getMachineBasicBlocks - Populate given set using machine basic blocks
72   /// which have machine instructions that belong to lexical scope identified by
73   /// DebugLoc.
74   void getMachineBasicBlocks(DebugLoc DL,
75                              SmallPtrSet<const MachineBasicBlock *, 4> &MBBs);
76
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);
80
81   /// findLexicalScope - Find lexical scope, either regular or inlined, for the
82   /// given DebugLoc. Return NULL if not found.
83   LexicalScope *findLexicalScope(DebugLoc DL);
84
85   /// getAbstractScopesList - Return a reference to list of abstract scopes.
86   ArrayRef<LexicalScope *> getAbstractScopesList() const {
87     return AbstractScopesList;
88   }
89
90   /// findAbstractScope - Find an abstract scope or return NULL.
91   LexicalScope *findAbstractScope(const MDNode *N) {
92     return AbstractScopeMap.lookup(N);
93   }
94
95   /// findInlinedScope - Find an inlined scope for the given DebugLoc or return
96   /// NULL.
97   LexicalScope *findInlinedScope(DebugLoc DL) {
98     return InlinedLexicalScopeMap.lookup(DL);
99   }
100
101   /// findLexicalScope - Find regular lexical scope or return NULL.
102   LexicalScope *findLexicalScope(const MDNode *N) {
103     return LexicalScopeMap.lookup(N);
104   }
105
106   /// dump - Print data structures to dbgs().
107   void dump();
108
109 private:
110   /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
111   /// not available then create new lexical scope.
112   LexicalScope *getOrCreateLexicalScope(DebugLoc DL);
113
114   /// getOrCreateRegularScope - Find or create a regular lexical scope.
115   LexicalScope *getOrCreateRegularScope(MDNode *Scope);
116
117   /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
118   LexicalScope *getOrCreateInlinedScope(MDNode *Scope, MDNode *InlinedAt);
119
120   /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
121   LexicalScope *getOrCreateAbstractScope(const MDNode *N);
122
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);
128   void
129   assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
130                           DenseMap<const MachineInstr *, LexicalScope *> &M);
131
132 private:
133   const MachineFunction *MF;
134
135   /// LexicalScopeMap - Tracks the scopes in the current function.  Owns the
136   /// contained LexicalScope*s.
137   DenseMap<const MDNode *, LexicalScope *> LexicalScopeMap;
138
139   /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
140   /// function.
141   DenseMap<DebugLoc, LexicalScope *> InlinedLexicalScopeMap;
142
143   /// AbstractScopeMap - These scopes are  not included LexicalScopeMap.
144   /// AbstractScopes owns its LexicalScope*s.
145   DenseMap<const MDNode *, LexicalScope *> AbstractScopeMap;
146
147   /// AbstractScopesList - Tracks abstract scopes constructed while processing
148   /// a function.
149   SmallVector<LexicalScope *, 4> AbstractScopesList;
150
151   /// CurrentFnLexicalScope - Top level scope for the current function.
152   ///
153   LexicalScope *CurrentFnLexicalScope;
154 };
155
156 //===----------------------------------------------------------------------===//
157 /// LexicalScope - This class is used to track scope information.
158 ///
159 class LexicalScope {
160
161 public:
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) {
165     if (Parent)
166       Parent->addChild(this);
167   }
168
169   // Accessors.
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; }
177
178   /// addChild - Add a child scope.
179   void addChild(LexicalScope *S) { Children.push_back(S); }
180
181   /// openInsnRange - This scope covers instruction range starting from MI.
182   void openInsnRange(const MachineInstr *MI) {
183     if (!FirstInsn)
184       FirstInsn = MI;
185
186     if (Parent)
187       Parent->openInsnRange(MI);
188   }
189
190   /// extendInsnRange - Extend the current instruction range covered by
191   /// this scope.
192   void extendInsnRange(const MachineInstr *MI) {
193     assert(FirstInsn && "MI Range is not open!");
194     LastInsn = MI;
195     if (Parent)
196       Parent->extendInsnRange(MI);
197   }
198
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 = NULL) {
203     assert(LastInsn && "Last insn missing!");
204     Ranges.push_back(InsnRange(FirstInsn, LastInsn));
205     FirstInsn = NULL;
206     LastInsn = NULL;
207     // If Parent dominates NewScope then do not close Parent's instruction
208     // range.
209     if (Parent && (!NewScope || !Parent->dominates(NewScope)))
210       Parent->closeInsnRange(NewScope);
211   }
212
213   /// dominates - Return true if current scope dominates given lexical scope.
214   bool dominates(const LexicalScope *S) const {
215     if (S == this)
216       return true;
217     if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
218       return true;
219     return false;
220   }
221
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; }
227
228   /// dump - print lexical scope.
229   void dump(unsigned Indent = 0) const;
230
231 private:
232   LexicalScope *Parent;                        // Parent to this scope.
233   AssertingVH<const MDNode> Desc;              // Debug info descriptor.
234   AssertingVH<const MDNode> InlinedAtLocation; // Location at which this
235                                                // scope is inlined.
236   bool AbstractScope;                          // Abstract Scope
237   SmallVector<LexicalScope *, 4> Children;     // Scopes defined in scope.
238                                                // Contents not owned.
239   SmallVector<InsnRange, 4> Ranges;
240
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
244                                  // scope nesting.
245 };
246
247 } // end llvm namespace
248
249 #endif