1 //===- GraphChecker.cpp - Assert that various graph properties hold -------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is used to test DSA with regression tests. It can be used to check
11 // that certain graph properties hold, such as two nodes being disjoint, whether
12 // or not a node is collapsed, etc. These are the command line arguments that
15 // --dsgc-dsapass={local,bu,td} - Specify what flavor of graph to check
16 // --dsgc-abort-if-any-collapsed - Abort if any collapsed nodes are found
17 // --dsgc-abort-if-collapsed=<list> - Abort if a node pointed to by an SSA
18 // value with name in <list> is collapsed
19 // --dsgc-check-flags=<list> - Abort if the specified nodes have flags
20 // that are not specified.
21 // --dsgc-abort-if-merged=<list> - Abort if any of the named SSA values
22 // point to the same node.
24 //===----------------------------------------------------------------------===//
26 #include "llvm/Analysis/DataStructure.h"
27 #include "llvm/Analysis/DSGraph.h"
28 #include "Support/CommandLine.h"
29 #include "llvm/Value.h"
35 enum DSPass { local, bu, td };
37 DSPass("dsgc-dspass", cl::Hidden,
38 cl::desc("Specify which DSA pass the -datastructure-gc pass should use"),
39 cl::values(clEnumVal(local, "Local pass"),
40 clEnumVal(bu, "Bottom-up pass"),
41 clEnumVal(td, "Top-down pass"), 0), cl::init(local));
44 AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
45 cl::desc("Abort if any collapsed nodes are found"));
47 AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
48 cl::desc("Abort if any of the named symbols is collapsed"));
50 CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
51 cl::desc("Check that flags are specified for nodes"));
53 AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
54 cl::desc("Abort if any of the named symbols are merged together"));
56 struct DSGC : public FunctionPass {
58 bool doFinalization(Module &M);
59 bool runOnFunction(Function &F);
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
63 case local: AU.addRequired<LocalDataStructures>(); break;
64 case bu: AU.addRequired<BUDataStructures>(); break;
65 case td: AU.addRequired<TDDataStructures>(); break;
69 void print(std::ostream &O, const Module *M) const {}
72 void verify(const DSGraph &G);
75 RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
79 if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
80 CheckFlags.empty() && AbortIfMerged.empty()) {
81 std::cerr << "The -datastructure-gc is useless if you don't specify any"
82 " -dsgc-* options. See the -help-hidden output for a list.\n";
88 /// doFinalization - Verify that the globals graph is in good shape...
90 bool DSGC::doFinalization(Module &M) {
92 case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
93 case bu: verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
94 case td: verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
99 /// runOnFunction - Get the DSGraph for this function and verify that it is ok.
101 bool DSGC::runOnFunction(Function &F) {
103 case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
104 case bu: verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
105 case td: verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
111 /// verify - This is the function which checks to make sure that all of the
112 /// invariants established on the command line are true.
114 void DSGC::verify(const DSGraph &G) {
115 // Loop over all of the nodes, checking to see if any are collapsed...
116 if (AbortIfAnyCollapsed) {
117 const std::vector<DSNode*> &Nodes = G.getNodes();
118 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
119 if (Nodes[i]->isNodeCompletelyFolded()) {
120 std::cerr << "Node is collapsed: ";
121 Nodes[i]->print(std::cerr, &G);
126 if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
127 !AbortIfMerged.empty()) {
128 // Convert from a list to a set, because we don't have cl::set's yet. FIXME
129 std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
130 AbortIfCollapsed.end());
131 std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
132 AbortIfMerged.end());
133 std::map<std::string, unsigned> CheckFlagsM;
135 for (cl::list<std::string>::iterator I = CheckFlags.begin(),
136 E = CheckFlags.end(); I != E; ++I) {
137 std::string::size_type ColonPos = I->rfind(':');
138 if (ColonPos == std::string::npos) {
139 std::cerr << "Error: '" << *I
140 << "' is an invalid value for the --dsgc-check-flags option!\n";
145 for (unsigned C = ColonPos+1; C != I->size(); ++C)
147 case 'S': Flags |= DSNode::AllocaNode; break;
148 case 'H': Flags |= DSNode::HeapNode; break;
149 case 'G': Flags |= DSNode::GlobalNode; break;
150 case 'U': Flags |= DSNode::UnknownNode; break;
151 case 'I': Flags |= DSNode::Incomplete; break;
152 case 'M': Flags |= DSNode::Modified; break;
153 case 'R': Flags |= DSNode::Read; break;
154 case 'A': Flags |= DSNode::Array; break;
155 default: std::cerr << "Invalid DSNode flag!\n"; abort();
157 CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
160 // Now we loop over all of the scalars, checking to see if any are collapsed
161 // that are not supposed to be, or if any are merged together.
162 const DSGraph::ScalarMapTy &SM = G.getScalarMap();
163 std::map<DSNode*, std::string> AbortIfMergedNodes;
165 for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
167 if (I->first->hasName() && I->second.getNode()) {
168 const std::string &Name = I->first->getName();
169 DSNode *N = I->second.getNode();
171 // Verify it is not collapsed if it is not supposed to be...
172 if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
173 std::cerr << "Node for value '%" << Name << "' is collapsed: ";
174 N->print(std::cerr, &G);
178 if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
179 std::cerr << "Node flags are not as expected for node: " << Name
181 N->print(std::cerr, &G);
185 // Verify that it is not merged if it is not supposed to be...
186 if (AbortIfMergedS.count(Name)) {
187 if (AbortIfMergedNodes.count(N)) {
188 std::cerr << "Nodes for values '%" << Name << "' and '%"
189 << AbortIfMergedNodes[N] << "' is merged: ";
190 N->print(std::cerr, &G);
193 AbortIfMergedNodes[N] = Name;
199 } // End llvm namespace