[DebugInfo] Remove LexicalScopes::isCurrentFunctionScope and CSE a use of LexicalScop...
[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/ADT/STLExtras.h"
25 #include "llvm/IR/DebugLoc.h"
26 #include "llvm/IR/Metadata.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include <utility>
29 #include <unordered_map>
30 namespace llvm {
31
32 class MachineInstr;
33 class MachineBasicBlock;
34 class MachineFunction;
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 /// LexicalScope - This class is used to track scope information.
44 ///
45 class LexicalScope {
46
47 public:
48   LexicalScope(LexicalScope *P, const MDNode *D, const MDNode *I, bool A)
49       : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A),
50         LastInsn(nullptr), FirstInsn(nullptr), DFSIn(0), DFSOut(0) {
51     if (Parent)
52       Parent->addChild(this);
53   }
54
55   // Accessors.
56   LexicalScope *getParent() const { return Parent; }
57   const MDNode *getDesc() const { return Desc; }
58   const MDNode *getInlinedAt() const { return InlinedAtLocation; }
59   const MDNode *getScopeNode() const { return Desc; }
60   bool isAbstractScope() const { return AbstractScope; }
61   SmallVectorImpl<LexicalScope *> &getChildren() { return Children; }
62   SmallVectorImpl<InsnRange> &getRanges() { return Ranges; }
63
64   /// addChild - Add a child scope.
65   void addChild(LexicalScope *S) { Children.push_back(S); }
66
67   /// openInsnRange - This scope covers instruction range starting from MI.
68   void openInsnRange(const MachineInstr *MI) {
69     if (!FirstInsn)
70       FirstInsn = MI;
71
72     if (Parent)
73       Parent->openInsnRange(MI);
74   }
75
76   /// extendInsnRange - Extend the current instruction range covered by
77   /// this scope.
78   void extendInsnRange(const MachineInstr *MI) {
79     assert(FirstInsn && "MI Range is not open!");
80     LastInsn = MI;
81     if (Parent)
82       Parent->extendInsnRange(MI);
83   }
84
85   /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
86   /// until now. This is used when a new scope is encountered while walking
87   /// machine instructions.
88   void closeInsnRange(LexicalScope *NewScope = nullptr) {
89     assert(LastInsn && "Last insn missing!");
90     Ranges.push_back(InsnRange(FirstInsn, LastInsn));
91     FirstInsn = nullptr;
92     LastInsn = nullptr;
93     // If Parent dominates NewScope then do not close Parent's instruction
94     // range.
95     if (Parent && (!NewScope || !Parent->dominates(NewScope)))
96       Parent->closeInsnRange(NewScope);
97   }
98
99   /// dominates - Return true if current scope dominates given lexical scope.
100   bool dominates(const LexicalScope *S) const {
101     if (S == this)
102       return true;
103     if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
104       return true;
105     return false;
106   }
107
108   // Depth First Search support to walk and manipulate LexicalScope hierarchy.
109   unsigned getDFSOut() const { return DFSOut; }
110   void setDFSOut(unsigned O) { DFSOut = O; }
111   unsigned getDFSIn() const { return DFSIn; }
112   void setDFSIn(unsigned I) { DFSIn = I; }
113
114   /// dump - print lexical scope.
115   void dump(unsigned Indent = 0) const;
116
117 private:
118   LexicalScope *Parent;                        // Parent to this scope.
119   AssertingVH<const MDNode> Desc;              // Debug info descriptor.
120   AssertingVH<const MDNode> InlinedAtLocation; // Location at which this
121                                                // scope is inlined.
122   bool AbstractScope;                          // Abstract Scope
123   SmallVector<LexicalScope *, 4> Children;     // Scopes defined in scope.
124                                                // Contents not owned.
125   SmallVector<InsnRange, 4> Ranges;
126
127   const MachineInstr *LastInsn;  // Last instruction of this scope.
128   const MachineInstr *FirstInsn; // First instruction of this scope.
129   unsigned DFSIn, DFSOut;        // In & Out Depth use to determine
130                                  // scope nesting.
131 };
132
133 //===----------------------------------------------------------------------===//
134 /// LexicalScopes -  This class provides interface to collect and use lexical
135 /// scoping information from machine instruction.
136 ///
137 class LexicalScopes {
138 public:
139   LexicalScopes() : MF(nullptr), CurrentFnLexicalScope(nullptr) {}
140
141   /// initialize - Scan machine function and constuct lexical scope nest, resets
142   /// the instance if necessary.
143   void initialize(const MachineFunction &);
144
145   /// releaseMemory - release memory.
146   void reset();
147
148   /// empty - Return true if there is any lexical scope information available.
149   bool empty() { return CurrentFnLexicalScope == nullptr; }
150
151   /// getCurrentFunctionScope - Return lexical scope for the current function.
152   LexicalScope *getCurrentFunctionScope() const {
153     return CurrentFnLexicalScope;
154   }
155
156   /// getMachineBasicBlocks - Populate given set using machine basic blocks
157   /// which have machine instructions that belong to lexical scope identified by
158   /// DebugLoc.
159   void getMachineBasicBlocks(DebugLoc DL,
160                              SmallPtrSetImpl<const MachineBasicBlock *> &MBBs);
161
162   /// dominates - Return true if DebugLoc's lexical scope dominates at least one
163   /// machine instruction's lexical scope in a given machine basic block.
164   bool dominates(DebugLoc DL, MachineBasicBlock *MBB);
165
166   /// findLexicalScope - Find lexical scope, either regular or inlined, for the
167   /// given DebugLoc. Return NULL if not found.
168   LexicalScope *findLexicalScope(DebugLoc DL);
169
170   /// getAbstractScopesList - Return a reference to list of abstract scopes.
171   ArrayRef<LexicalScope *> getAbstractScopesList() const {
172     return AbstractScopesList;
173   }
174
175   /// findAbstractScope - Find an abstract scope or return null.
176   LexicalScope *findAbstractScope(const MDNode *N) {
177     auto I = AbstractScopeMap.find(N);
178     return I != AbstractScopeMap.end() ? &I->second : nullptr;
179   }
180
181   /// findInlinedScope - Find an inlined scope for the given DebugLoc or return
182   /// NULL.
183   LexicalScope *findInlinedScope(DebugLoc DL);
184
185   /// findLexicalScope - Find regular lexical scope or return null.
186   LexicalScope *findLexicalScope(const MDNode *N) {
187     auto I = LexicalScopeMap.find(N);
188     return I != LexicalScopeMap.end() ? &I->second : nullptr;
189   }
190
191   /// dump - Print data structures to dbgs().
192   void dump();
193
194   /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
195   LexicalScope *getOrCreateAbstractScope(const MDNode *N);
196
197 private:
198   /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
199   /// not available then create new lexical scope.
200   LexicalScope *getOrCreateLexicalScope(DebugLoc DL);
201
202   /// getOrCreateRegularScope - Find or create a regular lexical scope.
203   LexicalScope *getOrCreateRegularScope(MDNode *Scope);
204
205   /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
206   LexicalScope *getOrCreateInlinedScope(MDNode *Scope, MDNode *InlinedAt);
207
208   /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
209   /// for the given machine function.
210   void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
211                             DenseMap<const MachineInstr *, LexicalScope *> &M);
212   void constructScopeNest(LexicalScope *Scope);
213   void
214   assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
215                           DenseMap<const MachineInstr *, LexicalScope *> &M);
216
217 private:
218   const MachineFunction *MF;
219
220   /// LexicalScopeMap - Tracks the scopes in the current function.
221   // Use an unordered_map to ensure value pointer validity over insertion.
222   std::unordered_map<const MDNode *, LexicalScope> LexicalScopeMap;
223
224   /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
225   /// function.
226   std::unordered_map<std::pair<const MDNode *, const MDNode *>, LexicalScope,
227                      pair_hash<const MDNode *, const MDNode *>>
228   InlinedLexicalScopeMap;
229
230   /// AbstractScopeMap - These scopes are  not included LexicalScopeMap.
231   // Use an unordered_map to ensure value pointer validity over insertion.
232   std::unordered_map<const MDNode *, LexicalScope> AbstractScopeMap;
233
234   /// AbstractScopesList - Tracks abstract scopes constructed while processing
235   /// a function.
236   SmallVector<LexicalScope *, 4> AbstractScopesList;
237
238   /// CurrentFnLexicalScope - Top level scope for the current function.
239   ///
240   LexicalScope *CurrentFnLexicalScope;
241 };
242
243 } // end llvm namespace
244
245 #endif