Skip to content

Commit 184e9a3

Browse files
committed
optimized icfg's vertex and edge properties
1 parent 5ee184e commit 184e9a3

4 files changed

Lines changed: 73 additions & 27 deletions

File tree

include/phasar/PhasarLLVM/ControlFlow/LLVMBasedICFG.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ class LLVMBasedICFG
7373
// The VertexProperties for our call-graph.
7474
struct VertexProperties {
7575
const llvm::Function *F = nullptr;
76-
std::string FName;
7776
VertexProperties() = default;
7877
VertexProperties(const llvm::Function *F);
78+
std::string getFunctionName() const;
7979
};
8080

8181
// The EdgeProperties for our call-graph.
8282
struct EdgeProperties {
8383
const llvm::Instruction *CS = nullptr;
84-
std::string IR;
8584
size_t ID = 0;
8685
EdgeProperties() = default;
8786
EdgeProperties(const llvm::Instruction *I);
87+
std::string getCallSiteAsString() const;
8888
};
8989

9090
/// Specify the type of graph to be used.
@@ -160,6 +160,44 @@ class LLVMBasedICFG
160160
using LLVMBasedCFG::print; // tell the compiler we wish to have both prints
161161
void print(std::ostream &OS = std::cout) const override;
162162

163+
// provide a VertexPropertyWrite to tell boost how to write a vertex
164+
class CallGraphVertexWriter {
165+
public:
166+
CallGraphVertexWriter(const bidigraph_t &CGraph) : CGraph(CGraph) {}
167+
template <class VertexOrEdge>
168+
void operator()(std::ostream &out, const VertexOrEdge &v) const {
169+
out << "[label=\"" << CGraph[v].getFunctionName() << "\"]";
170+
}
171+
172+
private:
173+
const bidigraph_t &CGraph;
174+
};
175+
176+
// a function to conveniently create the vertex writer
177+
CallGraphVertexWriter
178+
makeCallGraphVertexWriter(const bidigraph_t &CGraph) const {
179+
return CallGraphVertexWriter(CGraph);
180+
}
181+
182+
// provide a EdgePropertyWrite to tell boost how to write an edge
183+
class CallGraphEdgeWriter {
184+
public:
185+
CallGraphEdgeWriter(const bidigraph_t &CGraph) : CGraph(CGraph) {}
186+
template <class VertexOrEdge>
187+
void operator()(std::ostream &out, const VertexOrEdge &v) const {
188+
out << "[label=\"" << CGraph[v].getCallSiteAsString() << "\"]";
189+
}
190+
191+
private:
192+
const bidigraph_t &CGraph;
193+
};
194+
195+
// a function to conveniently create the edge writer
196+
CallGraphEdgeWriter
197+
makeCallGraphEdgeWriter(const bidigraph_t &CGraph) const {
198+
return CallGraphEdgeWriter(CGraph);
199+
}
200+
163201
void printAsDot(std::ostream &OS = std::cout) const;
164202

165203
void printInternalPTGAsDot(const std::string &filename);

include/phasar/PhasarLLVM/TypeHierarchy/LLVMTypeHierarchy.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ class LLVMTypeHierarchy
8282
/// The type for edge representative objects.
8383
typedef boost::graph_traits<bidigraph_t>::edge_descriptor edge_t;
8484
// Let us have some further handy typedefs.
85-
typedef boost::graph_traits<bidigraph_t>::vertex_iterator vertex_iterator_t;
85+
typedef boost::graph_traits<bidigraph_t>::vertex_iterator vertex_iterator;
8686
typedef boost::graph_traits<bidigraph_t>::out_edge_iterator
87-
out_edge_iterator_t;
88-
typedef boost::graph_traits<bidigraph_t>::in_edge_iterator in_edge_iterator_t;
87+
out_edge_iterator;
88+
typedef boost::graph_traits<bidigraph_t>::in_edge_iterator in_edge_iterator;
8989

9090
private:
9191
bidigraph_t TypeGraph;

lib/PhasarLLVM/ControlFlow/LLVMBasedICFG.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ struct LLVMBasedICFG::dependency_visitor : boost::default_dfs_visitor {
6666
};
6767

6868
LLVMBasedICFG::VertexProperties::VertexProperties(const llvm::Function *F)
69-
: F(F), FName(F->getName().str()) {}
69+
: F(F) {}
70+
71+
std::string LLVMBasedICFG::VertexProperties::getFunctionName() const {
72+
return F->getName().str();
73+
}
7074

7175
LLVMBasedICFG::EdgeProperties::EdgeProperties(const llvm::Instruction *I)
72-
: CS(I),
73-
// WARNING: Huge cost
74-
//, ir_code(llvmIRToString(i)),
75-
IR(""), ID(stoull(getMetaDataID(I))) {}
76+
: CS(I), ID(stoull(getMetaDataID(I))) {}
77+
78+
std::string LLVMBasedICFG::EdgeProperties::getCallSiteAsString() const {
79+
return llvmIRToString(CS);
80+
}
7681

7782
LLVMBasedICFG::LLVMBasedICFG(ProjectIRDB &IRDB, CallGraphAnalysisType CGType,
7883
const std::set<std::string> &EntryPoints,
@@ -502,19 +507,22 @@ bool LLVMBasedICFG::isPrimitiveFunction(const string &name) {
502507
}
503508

504509
void LLVMBasedICFG::print(ostream &OS) const {
505-
cout << "Call Graph:\n";
506-
boost::print_graph(
507-
CallGraph, boost::get(&LLVMBasedICFG::VertexProperties::FName, CallGraph),
508-
OS);
510+
OS << "Call Graph:\n";
511+
vertex_iterator ui, ui_end;
512+
for (boost::tie(ui, ui_end) = boost::vertices(CallGraph); ui != ui_end;
513+
++ui) {
514+
OS << CallGraph[*ui].getFunctionName() << " --> ";
515+
out_edge_iterator ei, ei_end;
516+
for (boost::tie(ei, ei_end) = boost::out_edges(*ui, CallGraph);
517+
ei != ei_end; ++ei)
518+
OS << CallGraph[target(*ei, CallGraph)].getFunctionName() << " ";
519+
OS << '\n';
520+
}
509521
}
510522

511523
void LLVMBasedICFG::printAsDot(std::ostream &OS) const {
512-
boost::write_graphviz(
513-
OS, CallGraph,
514-
boost::make_label_writer(
515-
boost::get(&LLVMBasedICFG::VertexProperties::FName, CallGraph)),
516-
boost::make_label_writer(
517-
boost::get(&LLVMBasedICFG::EdgeProperties::IR, CallGraph)));
524+
boost::write_graphviz(OS, CallGraph, makeCallGraphVertexWriter(CallGraph),
525+
makeCallGraphEdgeWriter(CallGraph));
518526
}
519527

520528
void LLVMBasedICFG::printInternalPTGAsDot(const string &filename) {
@@ -534,12 +542,12 @@ nlohmann::json LLVMBasedICFG::getAsJson() const {
534542
// iterate all graph vertices
535543
for (boost::tie(vi_v, vi_v_end) = boost::vertices(CallGraph);
536544
vi_v != vi_v_end; ++vi_v) {
537-
J[PhasarConfig::JsonCallGraphID()][CallGraph[*vi_v].FName];
545+
J[PhasarConfig::JsonCallGraphID()][CallGraph[*vi_v].getFunctionName()];
538546
// iterate all out edges of vertex vi_v
539547
for (boost::tie(ei, ei_end) = boost::out_edges(*vi_v, CallGraph);
540548
ei != ei_end; ++ei) {
541-
J[PhasarConfig::JsonCallGraphID()][CallGraph[*vi_v].FName] +=
542-
CallGraph[boost::target(*ei, CallGraph)].FName;
549+
J[PhasarConfig::JsonCallGraphID()][CallGraph[*vi_v].getFunctionName()] +=
550+
CallGraph[boost::target(*ei, CallGraph)].getFunctionName();
543551
}
544552
}
545553
return J;

lib/PhasarLLVM/TypeHierarchy/LLVMTypeHierarchy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ bool LLVMTypeHierarchy::empty() const { return size() == 0; }
341341

342342
void LLVMTypeHierarchy::print(std::ostream &OS) const {
343343
OS << "Type Hierarchy:\n";
344-
vertex_iterator_t ui, ui_end;
344+
vertex_iterator ui, ui_end;
345345
for (boost::tie(ui, ui_end) = boost::vertices(TypeGraph); ui != ui_end;
346346
++ui) {
347347
OS << TypeGraph[*ui].getTypeName() << " --> ";
348-
out_edge_iterator_t ei, ei_end;
348+
out_edge_iterator ei, ei_end;
349349
for (boost::tie(ei, ei_end) = boost::out_edges(*ui, TypeGraph);
350350
ei != ei_end; ++ei)
351351
OS << TypeGraph[target(*ei, TypeGraph)].getTypeName() << " ";
@@ -362,8 +362,8 @@ void LLVMTypeHierarchy::print(std::ostream &OS) const {
362362

363363
nlohmann::json LLVMTypeHierarchy::getAsJson() const {
364364
nlohmann::json J;
365-
vertex_iterator_t vi_v, vi_v_end;
366-
out_edge_iterator_t ei, ei_end;
365+
vertex_iterator vi_v, vi_v_end;
366+
out_edge_iterator ei, ei_end;
367367
// iterate all graph vertices
368368
for (boost::tie(vi_v, vi_v_end) = boost::vertices(TypeGraph);
369369
vi_v != vi_v_end; ++vi_v) {

0 commit comments

Comments
 (0)