blob: f42ea7eb08e834eab9fa8e92f9039e98f8b27e4e [file] [log] [blame]
Chris Lattner5100dbb2003-06-29 18:17:07 +00001//===- GraphChecker.cpp - Assert that various graph properties hold -------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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
Chris Lattner4dabb2c2004-07-07 06:32:21 +000026#include "llvm/Analysis/DataStructure/DataStructure.h"
27#include "llvm/Analysis/DataStructure/DSGraph.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/CommandLine.h"
Chris Lattner5100dbb2003-06-29 18:17:07 +000029#include "llvm/Value.h"
Reid Spencer954da372004-07-04 12:19:56 +000030#include <iostream>
Chris Lattner5100dbb2003-06-29 18:17:07 +000031#include <set>
32
Chris Lattner9a927292003-11-12 23:11:14 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner5100dbb2003-06-29 18:17:07 +000035namespace {
36 enum DSPass { local, bu, td };
37 cl::opt<DSPass>
38 DSPass("dsgc-dspass", cl::Hidden,
39 cl::desc("Specify which DSA pass the -datastructure-gc pass should use"),
40 cl::values(clEnumVal(local, "Local pass"),
41 clEnumVal(bu, "Bottom-up pass"),
Misha Brukman2b37d7c2005-04-21 21:13:18 +000042 clEnumVal(td, "Top-down pass"),
Chris Lattnerf99947c2004-07-16 00:04:13 +000043 clEnumValEnd), cl::init(local));
Chris Lattner5100dbb2003-06-29 18:17:07 +000044
45 cl::opt<bool>
46 AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
47 cl::desc("Abort if any collapsed nodes are found"));
48 cl::list<std::string>
49 AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
50 cl::desc("Abort if any of the named symbols is collapsed"));
51 cl::list<std::string>
Chris Lattnerf2d01342003-07-25 20:45:40 +000052 CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
53 cl::desc("Check that flags are specified for nodes"));
Chris Lattner919ffbf2003-06-29 22:36:15 +000054 cl::list<std::string>
Chris Lattner5100dbb2003-06-29 18:17:07 +000055 AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
56 cl::desc("Abort if any of the named symbols are merged together"));
57
58 struct DSGC : public FunctionPass {
59 DSGC();
60 bool doFinalization(Module &M);
61 bool runOnFunction(Function &F);
62
63 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 switch (DSPass) {
65 case local: AU.addRequired<LocalDataStructures>(); break;
66 case bu: AU.addRequired<BUDataStructures>(); break;
67 case td: AU.addRequired<TDDataStructures>(); break;
68 }
69 AU.setPreservesAll();
70 }
Chris Lattner889fb9d2003-06-29 20:27:16 +000071 void print(std::ostream &O, const Module *M) const {}
Chris Lattner5100dbb2003-06-29 18:17:07 +000072
73 private:
74 void verify(const DSGraph &G);
75 };
76
77 RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
78}
79
Chris Lattner6d796232005-10-24 00:38:38 +000080FunctionPass *llvm::createDataStructureGraphCheckerPass() {
81 return new DSGC();
82}
83
84
Chris Lattner5100dbb2003-06-29 18:17:07 +000085DSGC::DSGC() {
86 if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
Chris Lattnerf2d01342003-07-25 20:45:40 +000087 CheckFlags.empty() && AbortIfMerged.empty()) {
Chris Lattner5100dbb2003-06-29 18:17:07 +000088 std::cerr << "The -datastructure-gc is useless if you don't specify any"
89 " -dsgc-* options. See the -help-hidden output for a list.\n";
90 abort();
91 }
92}
93
94
95/// doFinalization - Verify that the globals graph is in good shape...
96///
97bool DSGC::doFinalization(Module &M) {
98 switch (DSPass) {
99 case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
100 case bu: verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
101 case td: verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
102 }
103 return false;
104}
105
106/// runOnFunction - Get the DSGraph for this function and verify that it is ok.
107///
108bool DSGC::runOnFunction(Function &F) {
109 switch (DSPass) {
110 case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
111 case bu: verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
112 case td: verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
113 }
114
115 return false;
116}
117
118/// verify - This is the function which checks to make sure that all of the
119/// invariants established on the command line are true.
120///
121void DSGC::verify(const DSGraph &G) {
122 // Loop over all of the nodes, checking to see if any are collapsed...
123 if (AbortIfAnyCollapsed) {
Chris Lattner84b80a22005-03-16 22:42:19 +0000124 for (DSGraph::node_const_iterator I = G.node_begin(), E = G.node_end();
125 I != E; ++I)
126 if (I->isNodeCompletelyFolded()) {
Chris Lattner5100dbb2003-06-29 18:17:07 +0000127 std::cerr << "Node is collapsed: ";
Chris Lattner84b80a22005-03-16 22:42:19 +0000128 I->print(std::cerr, &G);
Chris Lattner5100dbb2003-06-29 18:17:07 +0000129 abort();
130 }
131 }
132
Chris Lattnerf2d01342003-07-25 20:45:40 +0000133 if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
Chris Lattner919ffbf2003-06-29 22:36:15 +0000134 !AbortIfMerged.empty()) {
Chris Lattner5100dbb2003-06-29 18:17:07 +0000135 // Convert from a list to a set, because we don't have cl::set's yet. FIXME
136 std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
137 AbortIfCollapsed.end());
Chris Lattner919ffbf2003-06-29 22:36:15 +0000138 std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
139 AbortIfMerged.end());
Chris Lattnerf2d01342003-07-25 20:45:40 +0000140 std::map<std::string, unsigned> CheckFlagsM;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000141
Chris Lattnerf2d01342003-07-25 20:45:40 +0000142 for (cl::list<std::string>::iterator I = CheckFlags.begin(),
143 E = CheckFlags.end(); I != E; ++I) {
Alkis Evlogimenosb5c1af42003-10-01 22:49:22 +0000144 std::string::size_type ColonPos = I->rfind(':');
Chris Lattnerf2d01342003-07-25 20:45:40 +0000145 if (ColonPos == std::string::npos) {
146 std::cerr << "Error: '" << *I
147 << "' is an invalid value for the --dsgc-check-flags option!\n";
148 abort();
149 }
150
151 unsigned Flags = 0;
Chris Lattner1f2bbaa2003-07-26 23:00:05 +0000152 for (unsigned C = ColonPos+1; C != I->size(); ++C)
153 switch ((*I)[C]) {
Chris Lattnerf2d01342003-07-25 20:45:40 +0000154 case 'S': Flags |= DSNode::AllocaNode; break;
155 case 'H': Flags |= DSNode::HeapNode; break;
156 case 'G': Flags |= DSNode::GlobalNode; break;
157 case 'U': Flags |= DSNode::UnknownNode; break;
158 case 'I': Flags |= DSNode::Incomplete; break;
159 case 'M': Flags |= DSNode::Modified; break;
160 case 'R': Flags |= DSNode::Read; break;
161 case 'A': Flags |= DSNode::Array; break;
Chris Lattnere7e221a2003-07-25 20:49:29 +0000162 default: std::cerr << "Invalid DSNode flag!\n"; abort();
Chris Lattnerf2d01342003-07-25 20:45:40 +0000163 }
164 CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
165 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000166
Chris Lattner5100dbb2003-06-29 18:17:07 +0000167 // Now we loop over all of the scalars, checking to see if any are collapsed
168 // that are not supposed to be, or if any are merged together.
169 const DSGraph::ScalarMapTy &SM = G.getScalarMap();
170 std::map<DSNode*, std::string> AbortIfMergedNodes;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000171
Chris Lattner5100dbb2003-06-29 18:17:07 +0000172 for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
173 I != E; ++I)
174 if (I->first->hasName() && I->second.getNode()) {
Chris Lattnerf2d01342003-07-25 20:45:40 +0000175 const std::string &Name = I->first->getName();
Chris Lattner5100dbb2003-06-29 18:17:07 +0000176 DSNode *N = I->second.getNode();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000177
Chris Lattner5100dbb2003-06-29 18:17:07 +0000178 // Verify it is not collapsed if it is not supposed to be...
Chris Lattner919ffbf2003-06-29 22:36:15 +0000179 if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
180 std::cerr << "Node for value '%" << Name << "' is collapsed: ";
181 N->print(std::cerr, &G);
182 abort();
183 }
184
Chris Lattnerf2d01342003-07-25 20:45:40 +0000185 if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
Andrew Lenharth94a8d772006-04-25 19:33:41 +0000186 std::cerr << "Node flags are not as expected for node: " << Name
187 << " (" << CheckFlagsM[Name] << ":" <<N->getNodeFlags()
188 << ")\n";
Chris Lattner5100dbb2003-06-29 18:17:07 +0000189 N->print(std::cerr, &G);
190 abort();
191 }
192
193 // Verify that it is not merged if it is not supposed to be...
Chris Lattner919ffbf2003-06-29 22:36:15 +0000194 if (AbortIfMergedS.count(Name)) {
Chris Lattner5100dbb2003-06-29 18:17:07 +0000195 if (AbortIfMergedNodes.count(N)) {
Chris Lattner919ffbf2003-06-29 22:36:15 +0000196 std::cerr << "Nodes for values '%" << Name << "' and '%"
197 << AbortIfMergedNodes[N] << "' is merged: ";
Chris Lattner5100dbb2003-06-29 18:17:07 +0000198 N->print(std::cerr, &G);
199 abort();
200 }
Chris Lattner919ffbf2003-06-29 22:36:15 +0000201 AbortIfMergedNodes[N] = Name;
Chris Lattner5100dbb2003-06-29 18:17:07 +0000202 }
203 }
204 }
205}