blob: af8731eb05c01934e7c166891be26d97d6ff6257 [file] [log] [blame]
Chris Lattner5100dbb2003-06-29 18:17:07 +00001//===- GraphChecker.cpp - Assert that various graph properties hold -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner5100dbb2003-06-29 18:17:07 +00009//
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
13// it supports:
14//
Chris Lattner4e46e322004-02-20 23:27:09 +000015// --dsgc-dspass={local,bu,td} - Specify what flavor of graph to check
Chris Lattner5100dbb2003-06-29 18:17:07 +000016// --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
Chris Lattnerf2d01342003-07-25 20:45:40 +000019// --dsgc-check-flags=<list> - Abort if the specified nodes have flags
20// that are not specified.
Chris Lattner5100dbb2003-06-29 18:17:07 +000021// --dsgc-abort-if-merged=<list> - Abort if any of the named SSA values
22// point to the same node.
23//
24//===----------------------------------------------------------------------===//
25
26#include "llvm/Analysis/DataStructure.h"
27#include "llvm/Analysis/DSGraph.h"
28#include "Support/CommandLine.h"
29#include "llvm/Value.h"
30#include <set>
31
Chris Lattner9a927292003-11-12 23:11:14 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner5100dbb2003-06-29 18:17:07 +000034namespace {
35 enum DSPass { local, bu, td };
36 cl::opt<DSPass>
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));
42
43 cl::opt<bool>
44 AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
45 cl::desc("Abort if any collapsed nodes are found"));
46 cl::list<std::string>
47 AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
48 cl::desc("Abort if any of the named symbols is collapsed"));
49 cl::list<std::string>
Chris Lattnerf2d01342003-07-25 20:45:40 +000050 CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
51 cl::desc("Check that flags are specified for nodes"));
Chris Lattner919ffbf2003-06-29 22:36:15 +000052 cl::list<std::string>
Chris Lattner5100dbb2003-06-29 18:17:07 +000053 AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
54 cl::desc("Abort if any of the named symbols are merged together"));
55
56 struct DSGC : public FunctionPass {
57 DSGC();
58 bool doFinalization(Module &M);
59 bool runOnFunction(Function &F);
60
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62 switch (DSPass) {
63 case local: AU.addRequired<LocalDataStructures>(); break;
64 case bu: AU.addRequired<BUDataStructures>(); break;
65 case td: AU.addRequired<TDDataStructures>(); break;
66 }
67 AU.setPreservesAll();
68 }
Chris Lattner889fb9d2003-06-29 20:27:16 +000069 void print(std::ostream &O, const Module *M) const {}
Chris Lattner5100dbb2003-06-29 18:17:07 +000070
71 private:
72 void verify(const DSGraph &G);
73 };
74
75 RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
76}
77
78DSGC::DSGC() {
79 if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
Chris Lattnerf2d01342003-07-25 20:45:40 +000080 CheckFlags.empty() && AbortIfMerged.empty()) {
Chris Lattner5100dbb2003-06-29 18:17:07 +000081 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";
83 abort();
84 }
85}
86
87
88/// doFinalization - Verify that the globals graph is in good shape...
89///
90bool DSGC::doFinalization(Module &M) {
91 switch (DSPass) {
92 case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
93 case bu: verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
94 case td: verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
95 }
96 return false;
97}
98
99/// runOnFunction - Get the DSGraph for this function and verify that it is ok.
100///
101bool DSGC::runOnFunction(Function &F) {
102 switch (DSPass) {
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;
106 }
107
108 return false;
109}
110
111/// verify - This is the function which checks to make sure that all of the
112/// invariants established on the command line are true.
113///
114void DSGC::verify(const DSGraph &G) {
115 // Loop over all of the nodes, checking to see if any are collapsed...
116 if (AbortIfAnyCollapsed) {
Chris Lattnere92e7642004-02-07 23:58:05 +0000117 for (DSGraph::node_iterator I = G.node_begin(), E = G.node_end(); I!=E; ++I)
118 if ((*I)->isNodeCompletelyFolded()) {
Chris Lattner5100dbb2003-06-29 18:17:07 +0000119 std::cerr << "Node is collapsed: ";
Chris Lattnere92e7642004-02-07 23:58:05 +0000120 (*I)->print(std::cerr, &G);
Chris Lattner5100dbb2003-06-29 18:17:07 +0000121 abort();
122 }
123 }
124
Chris Lattnerf2d01342003-07-25 20:45:40 +0000125 if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
Chris Lattner919ffbf2003-06-29 22:36:15 +0000126 !AbortIfMerged.empty()) {
Chris Lattner5100dbb2003-06-29 18:17:07 +0000127 // Convert from a list to a set, because we don't have cl::set's yet. FIXME
128 std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
129 AbortIfCollapsed.end());
Chris Lattner919ffbf2003-06-29 22:36:15 +0000130 std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
131 AbortIfMerged.end());
Chris Lattnerf2d01342003-07-25 20:45:40 +0000132 std::map<std::string, unsigned> CheckFlagsM;
Chris Lattner5100dbb2003-06-29 18:17:07 +0000133
Chris Lattnerf2d01342003-07-25 20:45:40 +0000134 for (cl::list<std::string>::iterator I = CheckFlags.begin(),
135 E = CheckFlags.end(); I != E; ++I) {
Alkis Evlogimenosb5c1af42003-10-01 22:49:22 +0000136 std::string::size_type ColonPos = I->rfind(':');
Chris Lattnerf2d01342003-07-25 20:45:40 +0000137 if (ColonPos == std::string::npos) {
138 std::cerr << "Error: '" << *I
139 << "' is an invalid value for the --dsgc-check-flags option!\n";
140 abort();
141 }
142
143 unsigned Flags = 0;
Chris Lattner1f2bbaa2003-07-26 23:00:05 +0000144 for (unsigned C = ColonPos+1; C != I->size(); ++C)
145 switch ((*I)[C]) {
Chris Lattnerf2d01342003-07-25 20:45:40 +0000146 case 'S': Flags |= DSNode::AllocaNode; break;
147 case 'H': Flags |= DSNode::HeapNode; break;
148 case 'G': Flags |= DSNode::GlobalNode; break;
149 case 'U': Flags |= DSNode::UnknownNode; break;
150 case 'I': Flags |= DSNode::Incomplete; break;
151 case 'M': Flags |= DSNode::Modified; break;
152 case 'R': Flags |= DSNode::Read; break;
153 case 'A': Flags |= DSNode::Array; break;
Chris Lattnere7e221a2003-07-25 20:49:29 +0000154 default: std::cerr << "Invalid DSNode flag!\n"; abort();
Chris Lattnerf2d01342003-07-25 20:45:40 +0000155 }
156 CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
157 }
Chris Lattner5100dbb2003-06-29 18:17:07 +0000158
159 // Now we loop over all of the scalars, checking to see if any are collapsed
160 // that are not supposed to be, or if any are merged together.
161 const DSGraph::ScalarMapTy &SM = G.getScalarMap();
162 std::map<DSNode*, std::string> AbortIfMergedNodes;
163
164 for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
165 I != E; ++I)
166 if (I->first->hasName() && I->second.getNode()) {
Chris Lattnerf2d01342003-07-25 20:45:40 +0000167 const std::string &Name = I->first->getName();
Chris Lattner5100dbb2003-06-29 18:17:07 +0000168 DSNode *N = I->second.getNode();
169
170 // Verify it is not collapsed if it is not supposed to be...
Chris Lattner919ffbf2003-06-29 22:36:15 +0000171 if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
172 std::cerr << "Node for value '%" << Name << "' is collapsed: ";
173 N->print(std::cerr, &G);
174 abort();
175 }
176
Chris Lattnerf2d01342003-07-25 20:45:40 +0000177 if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
178 std::cerr << "Node flags are not as expected for node: " << Name
179 << "\n";
Chris Lattner5100dbb2003-06-29 18:17:07 +0000180 N->print(std::cerr, &G);
181 abort();
182 }
183
184 // Verify that it is not merged if it is not supposed to be...
Chris Lattner919ffbf2003-06-29 22:36:15 +0000185 if (AbortIfMergedS.count(Name)) {
Chris Lattner5100dbb2003-06-29 18:17:07 +0000186 if (AbortIfMergedNodes.count(N)) {
Chris Lattner919ffbf2003-06-29 22:36:15 +0000187 std::cerr << "Nodes for values '%" << Name << "' and '%"
188 << AbortIfMergedNodes[N] << "' is merged: ";
Chris Lattner5100dbb2003-06-29 18:17:07 +0000189 N->print(std::cerr, &G);
190 abort();
191 }
Chris Lattner919ffbf2003-06-29 22:36:15 +0000192 AbortIfMergedNodes[N] = Name;
Chris Lattner5100dbb2003-06-29 18:17:07 +0000193 }
194 }
195 }
196}