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-check-flags=<list> - Abort if the specified nodes have flags
13 // that are not specified.
14 // --dsgc-abort-if-merged=<list> - Abort if any of the named SSA values
15 // point to the same node.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/DataStructure.h"
20 #include "llvm/Analysis/DSGraph.h"
21 #include "Support/CommandLine.h"
22 #include "llvm/Value.h"
26 enum DSPass { local, bu, td };
28 DSPass("dsgc-dspass", cl::Hidden,
29 cl::desc("Specify which DSA pass the -datastructure-gc pass should use"),
30 cl::values(clEnumVal(local, "Local pass"),
31 clEnumVal(bu, "Bottom-up pass"),
32 clEnumVal(td, "Top-down pass"), 0), cl::init(local));
35 AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
36 cl::desc("Abort if any collapsed nodes are found"));
38 AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
39 cl::desc("Abort if any of the named symbols is collapsed"));
41 CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
42 cl::desc("Check that flags are specified for nodes"));
44 AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
45 cl::desc("Abort if any of the named symbols are merged together"));
47 struct DSGC : public FunctionPass {
49 bool doFinalization(Module &M);
50 bool runOnFunction(Function &F);
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 case local: AU.addRequired<LocalDataStructures>(); break;
55 case bu: AU.addRequired<BUDataStructures>(); break;
56 case td: AU.addRequired<TDDataStructures>(); break;
60 void print(std::ostream &O, const Module *M) const {}
63 void verify(const DSGraph &G);
66 RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
70 if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
71 CheckFlags.empty() && AbortIfMerged.empty()) {
72 std::cerr << "The -datastructure-gc is useless if you don't specify any"
73 " -dsgc-* options. See the -help-hidden output for a list.\n";
79 /// doFinalization - Verify that the globals graph is in good shape...
81 bool DSGC::doFinalization(Module &M) {
83 case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
84 case bu: verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
85 case td: verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
90 /// runOnFunction - Get the DSGraph for this function and verify that it is ok.
92 bool DSGC::runOnFunction(Function &F) {
94 case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
95 case bu: verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
96 case td: verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
102 /// verify - This is the function which checks to make sure that all of the
103 /// invariants established on the command line are true.
105 void DSGC::verify(const DSGraph &G) {
106 // Loop over all of the nodes, checking to see if any are collapsed...
107 if (AbortIfAnyCollapsed) {
108 const std::vector<DSNode*> &Nodes = G.getNodes();
109 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
110 if (Nodes[i]->isNodeCompletelyFolded()) {
111 std::cerr << "Node is collapsed: ";
112 Nodes[i]->print(std::cerr, &G);
117 if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
118 !AbortIfMerged.empty()) {
119 // Convert from a list to a set, because we don't have cl::set's yet. FIXME
120 std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
121 AbortIfCollapsed.end());
122 std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
123 AbortIfMerged.end());
124 std::map<std::string, unsigned> CheckFlagsM;
126 for (cl::list<std::string>::iterator I = CheckFlags.begin(),
127 E = CheckFlags.end(); I != E; ++I) {
128 std::string::size_type ColonPos = I->rfind(':');
129 if (ColonPos == std::string::npos) {
130 std::cerr << "Error: '" << *I
131 << "' is an invalid value for the --dsgc-check-flags option!\n";
136 for (unsigned C = ColonPos+1; C != I->size(); ++C)
138 case 'S': Flags |= DSNode::AllocaNode; break;
139 case 'H': Flags |= DSNode::HeapNode; break;
140 case 'G': Flags |= DSNode::GlobalNode; break;
141 case 'U': Flags |= DSNode::UnknownNode; break;
142 case 'I': Flags |= DSNode::Incomplete; break;
143 case 'M': Flags |= DSNode::Modified; break;
144 case 'R': Flags |= DSNode::Read; break;
145 case 'A': Flags |= DSNode::Array; break;
146 default: std::cerr << "Invalid DSNode flag!\n"; abort();
148 CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
151 // Now we loop over all of the scalars, checking to see if any are collapsed
152 // that are not supposed to be, or if any are merged together.
153 const DSGraph::ScalarMapTy &SM = G.getScalarMap();
154 std::map<DSNode*, std::string> AbortIfMergedNodes;
156 for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
158 if (I->first->hasName() && I->second.getNode()) {
159 const std::string &Name = I->first->getName();
160 DSNode *N = I->second.getNode();
162 // Verify it is not collapsed if it is not supposed to be...
163 if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
164 std::cerr << "Node for value '%" << Name << "' is collapsed: ";
165 N->print(std::cerr, &G);
169 if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
170 std::cerr << "Node flags are not as expected for node: " << Name
172 N->print(std::cerr, &G);
176 // Verify that it is not merged if it is not supposed to be...
177 if (AbortIfMergedS.count(Name)) {
178 if (AbortIfMergedNodes.count(N)) {
179 std::cerr << "Nodes for values '%" << Name << "' and '%"
180 << AbortIfMergedNodes[N] << "' is merged: ";
181 N->print(std::cerr, &G);
184 AbortIfMergedNodes[N] = Name;