LexicalScopes: use unique_ptr to own LexicalScope objects.
[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 #include <memory>
29 namespace llvm {
30
31 class MachineInstr;
32 class MachineBasicBlock;
33 class MachineFunction;
34 class LexicalScope;
35
36 //===----------------------------------------------------------------------===//
37 /// InsnRange - This is used to track range of instructions with identical
38 /// lexical scope.
39 ///
40 typedef std::pair<const MachineInstr *, const MachineInstr *> InsnRange;
41
42 //===----------------------------------------------------------------------===//
43 /// LexicalScopes -  This class provides interface to collect and use lexical
44 /// scoping information from machine instruction.
45 ///
46 class LexicalScopes {
47 public:
48   LexicalScopes() : MF(nullptr), CurrentFnLexicalScope(nullptr) {}
49   ~LexicalScopes();
50
51   /// initialize - Scan machine function and constuct lexical scope nest, resets
52   /// the instance if necessary.
53   void initialize(const MachineFunction &);
54
55   /// releaseMemory - release memory.
56   void reset();
57
58   /// empty - Return true if there is any lexical scope information available.
59   bool empty() { return CurrentFnLexicalScope == nullptr; }
60
61   /// isCurrentFunctionScope - Return true if given lexical scope represents
62   /// current function.
63   bool isCurrentFunctionScope(const LexicalScope *LS) {
64     return LS == CurrentFnLexicalScope;
65   }
66
67   /// getCurrentFunctionScope - Return lexical scope for the current function.
68   LexicalScope *getCurrentFunctionScope() const {
69     return CurrentFnLexicalScope;
70   }
71
72   /// getMachineBasicBlocks - Populate given set using machine basic blocks
73   /// which have machine instructions that belong to lexical scope identified by
74   /// DebugLoc.
75   void getMachineBasicBlocks(DebugLoc DL,
76                              SmallPtrSet<const MachineBasicBlock *, 4> &MBBs);
77
78   /// dominates - Return true if DebugLoc's lexical scope dominates at least one
79   /// machine instruction's lexical scope in a given machine basic block.
80   bool dominates(DebugLoc DL, MachineBasicBlock *MBB);
81
82   /// findLexicalScope - Find lexical scope, either regular or inlined, for the
83   /// given DebugLoc. Return NULL if not found.
84   LexicalScope *findLexicalScope(DebugLoc DL);
85
86   /// getAbstractScopesList - Return a reference to list of abstract scopes.
87   ArrayRef<LexicalScope *> getAbstractScopesList() const {
88     return AbstractScopesList;
89   }
90
91   /// findAbstractScope - Find an abstract scope or return NULL.
92   LexicalScope *findAbstractScope(const MDNode *N) {
93     return AbstractScopeMap.lookup(N);
94   }
95
96   /// findInlinedScope - Find an inlined scope for the given DebugLoc or return
97   /// NULL.
98   LexicalScope *findInlinedScope(DebugLoc DL) {
99     return InlinedLexicalScopeMap.lookup(DL);
100   }
101
102   /// findLexicalScope - Find regular lexical scope or return NULL.
103   LexicalScope *findLexicalScope(const MDNode *N) {
104     auto I = LexicalScopeMap.find(N);
105     return I != LexicalScopeMap.end() ? I->second.get() : nullptr;
106   }
107
108   /// dump - Print data structures to dbgs().
109   void dump();
110
111 private:
112   /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
113   /// not available then create new lexical scope.
114   LexicalScope *getOrCreateLexicalScope(DebugLoc DL);
115
116   /// getOrCreateRegularScope - Find or create a regular lexical scope.
117   LexicalScope *getOrCreateRegularScope(MDNode *Scope);
118
119   /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
120   LexicalScope *getOrCreateInlinedScope(MDNode *Scope, MDNode *InlinedAt);
121
122   /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
123   LexicalScope *getOrCreateAbstractScope(const MDNode *N);
124
125   /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
126   /// for the given machine function.
127   void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
128                             DenseMap<const MachineInstr *, LexicalScope *> &M);
129   void constructScopeNest(LexicalScope *Scope);
130   void
131   assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
132                           DenseMap<const MachineInstr *, LexicalScope *> &M);
133
134 private:
135   const MachineFunction *MF;
136
137   /// LexicalScopeMap - Tracks the scopes in the current function.  Owns the
138   /// contained LexicalScope*s.
139   DenseMap<const MDNode *, std::unique_ptr<LexicalScope>> LexicalScopeMap;
140
141   /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
142   /// function.
143   DenseMap<DebugLoc, LexicalScope *> InlinedLexicalScopeMap;
144
145   /// AbstractScopeMap - These scopes are  not included LexicalScopeMap.
146   /// AbstractScopes owns its LexicalScope*s.
147   DenseMap<const MDNode *, LexicalScope *> AbstractScopeMap;
148
149   /// AbstractScopesList - Tracks abstract scopes constructed while processing
150   /// a function.
151   SmallVector<LexicalScope *, 4> AbstractScopesList;
152
153   /// CurrentFnLexicalScope - Top level scope for the current function.
154   ///
155   LexicalScope *CurrentFnLexicalScope;
156 };
157
158 //===----------------------------------------------------------------------===//
159 /// LexicalScope - This class is used to track scope information.
160 ///
161 class LexicalScope {
162
163 public:
164   LexicalScope(LexicalScope *P, const MDNode *D, const MDNode *I, bool A)
165       : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A),
166         LastInsn(nullptr), FirstInsn(nullptr), DFSIn(0), DFSOut(0) {
167     if (Parent)
168       Parent->addChild(this);
169   }
170
171   // Accessors.
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; }
179
180   /// addChild - Add a child scope.
181   void addChild(LexicalScope *S) { Children.push_back(S); }
182
183   /// openInsnRange - This scope covers instruction range starting from MI.
184   void openInsnRange(const MachineInstr *MI) {
185     if (!FirstInsn)
186       FirstInsn = MI;
187
188     if (Parent)
189       Parent->openInsnRange(MI);
190   }
191
192   /// extendInsnRange - Extend the current instruction range covered by
193   /// this scope.
194   void extendInsnRange(const MachineInstr *MI) {
195     assert(FirstInsn && "MI Range is not open!");
196     LastInsn = MI;
197     if (Parent)
198       Parent->extendInsnRange(MI);
199   }
200
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 = nullptr) {
205     assert(LastInsn && "Last insn missing!");
206     Ranges.push_back(InsnRange(FirstInsn, LastInsn));
207     FirstInsn = nullptr;
208     LastInsn = nullptr;
209     // If Parent dominates NewScope then do not close Parent's instruction
210     // range.
211     if (Parent && (!NewScope || !Parent->dominates(NewScope)))
212       Parent->closeInsnRange(NewScope);
213   }
214
215   /// dominates - Return true if current scope dominates given lexical scope.
216   bool dominates(const LexicalScope *S) const {
217     if (S == this)
218       return true;
219     if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
220       return true;
221     return false;
222   }
223
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; }
229
230   /// dump - print lexical scope.
231   void dump(unsigned Indent = 0) const;
232
233 private:
234   LexicalScope *Parent;                        // Parent to this scope.
235   AssertingVH<const MDNode> Desc;              // Debug info descriptor.
236   AssertingVH<const MDNode> InlinedAtLocation; // Location at which this
237                                                // scope is inlined.
238   bool AbstractScope;                          // Abstract Scope
239   SmallVector<LexicalScope *, 4> Children;     // Scopes defined in scope.
240                                                // Contents not owned.
241   SmallVector<InsnRange, 4> Ranges;
242
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
246                                  // scope nesting.
247 };
248
249 } // end llvm namespace
250
251 #endif