1 //===- GraphChecker.cpp - Assert that various graph properties hold -------===//
3 // This pass is used to test DSA with regression tests. It can be used to check
4 // that certain graph properties hold, such as two nodes being disjoint, whether
5 // or not a node is collapsed, etc. These are the command line arguments that
8 // --dsgc-dsapass={local,bu,td} - Specify what flavor of graph to check
9 // --dsgc-abort-if-any-collapsed - Abort if any collapsed nodes are found
10 // --dsgc-abort-if-collapsed=<list> - Abort if a node pointed to by an SSA
11 // value with name in <list> is collapsed
12 // --dsgc-abort-if-merged=<list> - Abort if any of the named SSA values
13 // point to the same node.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/DataStructure.h"
18 #include "llvm/Analysis/DSGraph.h"
19 #include "Support/CommandLine.h"
20 #include "llvm/Value.h"
24 enum DSPass { local, bu, td };
26 DSPass("dsgc-dspass", cl::Hidden,
27 cl::desc("Specify which DSA pass the -datastructure-gc pass should use"),
28 cl::values(clEnumVal(local, "Local pass"),
29 clEnumVal(bu, "Bottom-up pass"),
30 clEnumVal(td, "Top-down pass"), 0), cl::init(local));
33 AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
34 cl::desc("Abort if any collapsed nodes are found"));
36 AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
37 cl::desc("Abort if any of the named symbols is collapsed"));
39 AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
40 cl::desc("Abort if any of the named symbols are merged together"));
42 struct DSGC : public FunctionPass {
44 bool doFinalization(Module &M);
45 bool runOnFunction(Function &F);
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 case local: AU.addRequired<LocalDataStructures>(); break;
50 case bu: AU.addRequired<BUDataStructures>(); break;
51 case td: AU.addRequired<TDDataStructures>(); break;
55 void print(std::ostream &O, const Module *M) const {}
58 void verify(const DSGraph &G);
61 RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
65 if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
66 AbortIfMerged.empty()) {
67 std::cerr << "The -datastructure-gc is useless if you don't specify any"
68 " -dsgc-* options. See the -help-hidden output for a list.\n";
74 /// doFinalization - Verify that the globals graph is in good shape...
76 bool DSGC::doFinalization(Module &M) {
78 case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
79 case bu: verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
80 case td: verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
85 /// runOnFunction - Get the DSGraph for this function and verify that it is ok.
87 bool DSGC::runOnFunction(Function &F) {
89 case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
90 case bu: verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
91 case td: verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
97 /// verify - This is the function which checks to make sure that all of the
98 /// invariants established on the command line are true.
100 void DSGC::verify(const DSGraph &G) {
101 // Loop over all of the nodes, checking to see if any are collapsed...
102 if (AbortIfAnyCollapsed) {
103 const std::vector<DSNode*> &Nodes = G.getNodes();
104 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
105 if (Nodes[i]->isNodeCompletelyFolded()) {
106 std::cerr << "Node is collapsed: ";
107 Nodes[i]->print(std::cerr, &G);
112 if (!AbortIfCollapsed.empty() || !AbortIfMerged.empty()) {
113 // Convert from a list to a set, because we don't have cl::set's yet. FIXME
114 std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
115 AbortIfCollapsed.end());
116 std::set<std::string> AbortIfMerged(AbortIfMerged.begin(),
117 AbortIfMerged.end());
120 // Now we loop over all of the scalars, checking to see if any are collapsed
121 // that are not supposed to be, or if any are merged together.
122 const DSGraph::ScalarMapTy &SM = G.getScalarMap();
123 std::map<DSNode*, std::string> AbortIfMergedNodes;
125 for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
127 if (I->first->hasName() && I->second.getNode()) {
128 DSNode *N = I->second.getNode();
130 // Verify it is not collapsed if it is not supposed to be...
131 if (N->isNodeCompletelyFolded() &&
132 AbortIfCollapsedS.count(I->first->getName())) {
133 std::cerr << "Node for value '%" << I->first->getName()
134 << "' is collapsed: ";
135 N->print(std::cerr, &G);
139 // Verify that it is not merged if it is not supposed to be...
140 if (AbortIfMerged.count(I->first->getName())) {
141 if (AbortIfMergedNodes.count(N)) {
142 std::cerr << "Nodes for values '%" << I->first->getName()
143 << "' and '%" << AbortIfMergedNodes[N] << "' is merged: ";
144 N->print(std::cerr, &G);
147 AbortIfMergedNodes[N] = I->first->getName();