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