@@ -121,13 +121,13 @@ class DataGraph
121
121
}
122
122
123
123
void removeEdge (Data Src, Data Dst) {
124
- auto *NSrc = this ->findNode (NodeType ( Src) );
125
- auto *NDst = this ->findNode (NodeType ( Dst) );
126
- assert (NSrc != this -> end () && NDst != this -> end () );
124
+ NodeType *NSrc = this ->findNode (Src);
125
+ NodeType *NDst = this ->findNode (Dst);
126
+ assert (NSrc && NDst);
127
127
llvm::SmallVector<EdgeType *, 10 > Edges;
128
- (* NDst) ->findEdgesTo (* *NSrc, Edges);
128
+ NDst->findEdgesTo (*NSrc, Edges);
129
129
for (EdgeType *E : Edges) {
130
- (* NDst) ->removeEdge (*E);
130
+ NDst->removeEdge (*E);
131
131
delete E;
132
132
}
133
133
invalidateBFSCache ();
@@ -151,15 +151,15 @@ class DataGraph
151
151
}
152
152
153
153
bool getNeighbors (Data D, std::set<Data> &DataSet, bool Succ) {
154
- auto *N = this ->findNode (NodeType (D) );
155
- if (N == this -> end () )
154
+ NodeType *N = this ->findNode (D );
155
+ if (N == nullptr )
156
156
return false ;
157
157
DataSet.clear ();
158
158
llvm::SetVector<EdgeType *> Edges;
159
159
if (Succ)
160
- Edges = (*N) ->getEdges ();
160
+ Edges = N ->getEdges ();
161
161
else
162
- Edges = (*N) ->getPredecessors ();
162
+ Edges = N ->getPredecessors ();
163
163
for (auto *E : Edges)
164
164
DataSet.insert (E->getTargetNode ().getData ());
165
165
return !DataSet.empty ();
@@ -173,14 +173,20 @@ class DataGraph
173
173
return getNeighbors (D, DataSet, false );
174
174
}
175
175
176
+ NodeType *findNode (Data D) {
177
+ if (NodeSet.find (D) != NodeSet.end ())
178
+ return NodeSet[D];
179
+ return nullptr ;
180
+ }
181
+
176
182
void visitBreadthFirst (Data Start, llvm::function_ref<void (Data)> Fn) {
177
- auto *N = this ->findNode (NodeType ( Start) );
178
- if (N == this -> end () )
183
+ NodeType *N = this ->findNode (Start);
184
+ if (N == nullptr )
179
185
return ;
180
186
// Insert into BFS cache.
181
187
if (BFSCache.find (Start) == BFSCache.end ()) {
182
188
std::set<Data> ReachableNodes;
183
- for (auto TNode : llvm::breadth_first (* N)) {
189
+ for (auto TNode : llvm::breadth_first (N)) {
184
190
ReachableNodes.insert (TNode->getData ());
185
191
}
186
192
BFSCache[Start] = ReachableNodes;
@@ -194,19 +200,20 @@ class DataGraph
194
200
// is allocated. Node equality is defined only by the data stored in a node,
195
201
// so if any node already contains the data, this node will be found.
196
202
virtual NodeType *findOrCreateNode (Data D) {
197
- auto *OldN = this ->findNode (NodeType (D));
198
- if (OldN != this ->end ())
199
- return *OldN;
203
+ if (NodeSet.find (D) != NodeSet.end ())
204
+ return NodeSet[D];
200
205
201
206
auto *NewN = new NodeType (D);
202
- this ->addNode (*NewN);
207
+ this ->Nodes .push_back (NewN);
208
+ NodeSet[D] = NewN;
203
209
return NewN;
204
210
}
205
211
206
212
private:
207
213
template <typename G> friend struct llvm ::GraphTraits;
208
214
friend class GraphVizOutputGraph ;
209
215
std::map<Data, std::set<Data>> BFSCache;
216
+ std::map<Data, NodeType*> NodeSet;
210
217
211
218
void invalidateBFSCache () { BFSCache.clear (); }
212
219
};
0 commit comments