blob: 7bfbf7d44cdfa159175a131d772c11607942cb23 [file] [log] [blame]
Chris Lattner5100dbb2003-06-29 18:17:07 +00001//===- GraphChecker.cpp - Assert that various graph properties hold -------===//
2//
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
6// it supports:
7//
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.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Analysis/DataStructure.h"
18#include "llvm/Analysis/DSGraph.h"
19#include "Support/CommandLine.h"
20#include "llvm/Value.h"
21#include <set>
22
23namespace {
24 enum DSPass { local, bu, td };
25 cl::opt<DSPass>
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));
31
32 cl::opt<bool>
33 AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
34 cl::desc("Abort if any collapsed nodes are found"));
35 cl::list<std::string>
36 AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
37 cl::desc("Abort if any of the named symbols is collapsed"));
38 cl::list<std::string>
39 AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
40 cl::desc("Abort if any of the named symbols are merged together"));
41
42 struct DSGC : public FunctionPass {
43 DSGC();
44 bool doFinalization(Module &M);
45 bool runOnFunction(Function &F);
46
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 switch (DSPass) {
49 case local: AU.addRequired<LocalDataStructures>(); break;
50 case bu: AU.addRequired<BUDataStructures>(); break;
51 case td: AU.addRequired<TDDataStructures>(); break;
52 }
53 AU.setPreservesAll();
54 }
55
56 private:
57 void verify(const DSGraph &G);
58 };
59
60 RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
61}
62
63DSGC::DSGC() {
64 if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
65 AbortIfMerged.empty()) {
66 std::cerr << "The -datastructure-gc is useless if you don't specify any"
67 " -dsgc-* options. See the -help-hidden output for a list.\n";
68 abort();
69 }
70}
71
72
73/// doFinalization - Verify that the globals graph is in good shape...
74///
75bool DSGC::doFinalization(Module &M) {
76 switch (DSPass) {
77 case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
78 case bu: verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
79 case td: verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
80 }
81 return false;
82}
83
84/// runOnFunction - Get the DSGraph for this function and verify that it is ok.
85///
86bool DSGC::runOnFunction(Function &F) {
87 switch (DSPass) {
88 case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
89 case bu: verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
90 case td: verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
91 }
92
93 return false;
94}
95
96/// verify - This is the function which checks to make sure that all of the
97/// invariants established on the command line are true.
98///
99void DSGC::verify(const DSGraph &G) {
100 // Loop over all of the nodes, checking to see if any are collapsed...
101 if (AbortIfAnyCollapsed) {
102 const std::vector<DSNode*> &Nodes = G.getNodes();
103 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
104 if (Nodes[i]->isNodeCompletelyFolded()) {
105 std::cerr << "Node is collapsed: ";
106 Nodes[i]->print(std::cerr, &G);
107 abort();
108 }
109 }
110
111 if (!AbortIfCollapsed.empty() || !AbortIfMerged.empty()) {
112 // Convert from a list to a set, because we don't have cl::set's yet. FIXME
113 std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
114 AbortIfCollapsed.end());
115 std::set<std::string> AbortIfMerged(AbortIfMerged.begin(),
116 AbortIfMerged.end());
117
118
119 // Now we loop over all of the scalars, checking to see if any are collapsed
120 // that are not supposed to be, or if any are merged together.
121 const DSGraph::ScalarMapTy &SM = G.getScalarMap();
122 std::map<DSNode*, std::string> AbortIfMergedNodes;
123
124 for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
125 I != E; ++I)
126 if (I->first->hasName() && I->second.getNode()) {
127 DSNode *N = I->second.getNode();
128
129 // Verify it is not collapsed if it is not supposed to be...
130 if (N->isNodeCompletelyFolded() &&
131 AbortIfCollapsedS.count(I->first->getName())) {
132 std::cerr << "Node for value '%" << I->first->getName()
133 << "' is collapsed: ";
134 N->print(std::cerr, &G);
135 abort();
136 }
137
138 // Verify that it is not merged if it is not supposed to be...
139 if (AbortIfMerged.count(I->first->getName())) {
140 if (AbortIfMergedNodes.count(N)) {
141 std::cerr << "Nodes for values '%" << I->first->getName()
142 << "' and '%" << AbortIfMergedNodes[N] << "' is merged: ";
143 N->print(std::cerr, &G);
144 abort();
145 }
146 AbortIfMergedNodes[N] = I->first->getName();
147 }
148 }
149 }
150}