Skip to content

Commit 7fc9292

Browse files
Tweaks to naming, comments; removed method from bimap
1 parent 2f801fb commit 7fc9292

File tree

4 files changed

+40
-37
lines changed

4 files changed

+40
-37
lines changed

clang/include/clang/CConv/ConstraintsGraph.h

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,34 @@ class DataNode : public llvm::DGNode<DataNode<DataType, EdgeType>, EdgeType> {
3636

3737
DataType getData() const { return Data; }
3838

39-
void addPred(EdgeType &E){ PredEdges.insert(&E); }
40-
const llvm::SetVector<EdgeType *> &getPreds() { return PredEdges; }
39+
void connectTo(NodeType &Other) {
40+
auto *BLR = new EdgeType(Other);
41+
this->addEdge(*BLR);
42+
auto *BRL = new EdgeType(*this);
43+
Other.addPredecessor(*BRL);
44+
}
45+
46+
const llvm::SetVector<EdgeType *> &getPredecessors() {
47+
return PredecessorEdges;
48+
}
4149

50+
// Nodes are defined exactly by the data they contain, not by their
51+
// connections to other nodes.
4252
bool isEqualTo(const DataNode &N) const { return this->Data == N.Data; }
4353

4454
private:
55+
// Data element stored in each node. This is used by isEqualTo to discriminate
56+
// between nodes.
4557
DataType Data;
4658

47-
llvm::SetVector<EdgeType *> PredEdges;
59+
// While the constraint graph is directed, we want to efficiently traverse
60+
// edges in the opposite direction. This set contains an edge entry pointing
61+
// back to every node that has an edge to this node.
62+
llvm::SetVector<EdgeType *> PredecessorEdges;
63+
void addPredecessor(EdgeType &E){ PredecessorEdges.insert(&E); }
4864
};
4965

66+
// Boilerplate template specialization
5067
template<typename Data, typename EdgeType>
5168
struct llvm::GraphTraits<DataNode<Data, EdgeType> *> {
5269
using NodeRef = DataNode<Data, EdgeType> *;
@@ -110,13 +127,9 @@ class DataGraph :
110127
}
111128

112129
void addEdge(Data L, Data R) {
113-
NodeType *BL = this->addVertex(L);
114-
NodeType *BR = this->addVertex(R);
115-
116-
auto *BLR = new EdgeType(*BR);
117-
BL->addEdge(*BLR);
118-
auto *BRL = new EdgeType(*BL);
119-
BR->addPred(*BRL);
130+
NodeType *BL = this->findOrCreateNode(L);
131+
NodeType *BR = this->findOrCreateNode(R);
132+
BL->connectTo(*BR);
120133
}
121134

122135
bool getNeighbors(Data D, std::set<Data> &DataSet, bool Succ){
@@ -128,7 +141,7 @@ class DataGraph :
128141
if (Succ)
129142
Edges = (*N)->getEdges();
130143
else
131-
Edges = (*N)->getPreds();
144+
Edges = (*N)->getPredecessors();
132145
for (auto *E : Edges)
133146
DataSet.insert(E->getTargetNode().getData());
134147
return !DataSet.empty();
@@ -151,7 +164,10 @@ class DataGraph :
151164
}
152165

153166
protected:
154-
virtual NodeType *addVertex(Data D) {
167+
// Finds the node containing the Data if it exists, otherwise a new Node
168+
// is allocated. Node equality is defined only by the data stored in a node,
169+
// so if any node already contains the data, this node will be found.
170+
virtual NodeType *findOrCreateNode(Data D) {
155171
auto *OldN = this->findNode(NodeType(D));
156172
if (OldN != this->end())
157173
return *OldN;
@@ -182,7 +198,7 @@ class ConstraintsGraph : public DataGraph<Atom *> {
182198
protected:
183199
// Add vertex is overridden to save const atoms as they are added to the graph
184200
// so that getAllConstAtoms can efficiently retrieve them.
185-
NodeType *addVertex(Atom *A) override;
201+
NodeType *findOrCreateNode(Atom *A) override;
186202
private:
187203
std::set<ConstAtom*> AllConstAtoms;
188204
};

clang/include/clang/CConv/Utils.h

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,26 @@ typedef std::map<PersistentSourceLoc,
3333
typedef std::map<clang::Decl *, clang::DeclStmt *> VariableDecltoStmtMap;
3434

3535
// Replacement for boost:bimap. A wrapper class around two std::maps to enable
36-
// map lookup in from key to value or from value to key.
36+
// map lookup from key to value or from value to key.
3737
template <typename KeyT, typename ValueT>
3838
class BiMap {
3939
public:
40-
BiMap() { }
41-
~BiMap() { KtoVal.clear(); ValToK.clear();}
40+
BiMap() = default;
41+
~BiMap() { clear(); }
4242

4343
void insert(KeyT KO, ValueT VO) {
4444
KtoVal[KO] = VO;
4545
ValToK[VO] = KO;
4646
}
4747

48-
typename std::map<KeyT, ValueT>::const_iterator find(const KeyT &K) {
49-
return KtoVal.find(K);
50-
}
51-
52-
typename std::map<KeyT, ValueT>::const_iterator end() {
53-
return KtoVal.end();
54-
}
55-
56-
const std::map<KeyT, ValueT> &left() {
57-
return KtoVal;
58-
}
59-
60-
const std::map<ValueT, KeyT> &right() {
61-
return ValToK;
62-
}
63-
6448
void clear() {
6549
KtoVal.clear();
6650
ValToK.clear();
6751
}
6852

53+
const std::map<KeyT, ValueT> &left() { return KtoVal; }
54+
const std::map<ValueT, KeyT> &right() { return ValToK; }
55+
6956
private:
7057
std::map<KeyT, ValueT> KtoVal;
7158
std::map<ValueT, KeyT> ValToK;

clang/lib/CConv/AVarBoundsInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ ProgramVar *AVarBoundsInfo::getProgramVar(BoundsKey VK) {
277277
}
278278

279279
bool AVarBoundsInfo::hasVarKey(PersistentSourceLoc &PSL) {
280-
return DeclVarMap.find(PSL) != DeclVarMap.end();
280+
return DeclVarMap.left().find(PSL) != DeclVarMap.left().end();
281281
}
282282

283283
BoundsKey AVarBoundsInfo::getVarKey(PersistentSourceLoc &PSL) {

clang/lib/CConv/ConstraintsGraph.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
#include "clang/CConv/ConstraintsGraph.h"
1313
#include <iostream>
1414

15-
ConstraintsGraph::NodeType *ConstraintsGraph::addVertex(Atom *A) {
15+
ConstraintsGraph::NodeType *ConstraintsGraph::findOrCreateNode(Atom *A) {
1616
// Save all the const atoms.
1717
if (auto *CA = clang::dyn_cast<ConstAtom>(A))
1818
AllConstAtoms.insert(CA);
19-
return DataGraph::addVertex(A);
19+
return DataGraph::findOrCreateNode(A);
2020
}
2121

2222
std::set<ConstAtom*> &ConstraintsGraph::getAllConstAtoms() {
@@ -76,10 +76,10 @@ std::string llvm::DOTGraphTraits<GraphVizOutputGraph>::getEdgeAttributes
7676
void GraphVizOutputGraph::mergeConstraintGraph(const ConstraintsGraph &Graph,
7777
GraphVizEdge::EdgeKind EK) {
7878
for (auto *N : Graph.Nodes) {
79-
auto *S = addVertex(N->getData());
79+
auto *S = findOrCreateNode(N->getData());
8080
for (auto *E : N->getEdges()) {
8181
Atom *TargetData = E->getTargetNode().getData();
82-
auto *D = addVertex(TargetData);
82+
auto *D = findOrCreateNode(TargetData);
8383
if (D->hasEdgeTo(*S)) {
8484
llvm::SmallVector<GraphVizEdge*, 2> Edges;
8585
D->findEdgesTo(*S, Edges);

0 commit comments

Comments
 (0)