Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / lib / Analysis / DataStructure / GraphChecker.cpp
1 //===- GraphChecker.cpp - Assert that various graph properties hold -------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
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 //
15 //   --dsgc-dspass={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.
23 //
24 //===----------------------------------------------------------------------===//
25
26 #include "llvm/Analysis/DataStructure/DataStructure.h"
27 #include "llvm/Analysis/DataStructure/DSGraph.h"
28 #include "Support/CommandLine.h"
29 #include "llvm/Value.h"
30 #include <iostream>
31 #include <set>
32
33 using namespace llvm;
34
35 namespace {
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"),
42                     clEnumVal(td,    "Top-down pass"), 
43                     clEnumValEnd), cl::init(local));
44
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>
52   CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
53              cl::desc("Check that flags are specified for nodes"));
54   cl::list<std::string>
55   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     }
71     void print(std::ostream &O, const Module *M) const {}
72
73   private:
74     void verify(const DSGraph &G);
75   };
76
77   RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
78 }
79
80 DSGC::DSGC() {
81   if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
82       CheckFlags.empty() && AbortIfMerged.empty()) {
83     std::cerr << "The -datastructure-gc is useless if you don't specify any"
84                  " -dsgc-* options.  See the -help-hidden output for a list.\n";
85     abort();
86   }
87 }
88
89
90 /// doFinalization - Verify that the globals graph is in good shape...
91 ///
92 bool DSGC::doFinalization(Module &M) {
93   switch (DSPass) {
94   case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
95   case bu:   verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
96   case td:   verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
97   }
98   return false;
99 }
100
101 /// runOnFunction - Get the DSGraph for this function and verify that it is ok.
102 ///
103 bool DSGC::runOnFunction(Function &F) {
104   switch (DSPass) {
105   case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
106   case bu:    verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
107   case td:    verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
108   }
109
110   return false;
111 }
112
113 /// verify - This is the function which checks to make sure that all of the
114 /// invariants established on the command line are true.
115 ///
116 void DSGC::verify(const DSGraph &G) {
117   // Loop over all of the nodes, checking to see if any are collapsed...
118   if (AbortIfAnyCollapsed) {
119     for (DSGraph::node_iterator I = G.node_begin(), E = G.node_end(); I!=E; ++I)
120       if ((*I)->isNodeCompletelyFolded()) {
121         std::cerr << "Node is collapsed: ";
122         (*I)->print(std::cerr, &G);
123         abort();
124       }
125   }
126
127   if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
128       !AbortIfMerged.empty()) {
129     // Convert from a list to a set, because we don't have cl::set's yet.  FIXME
130     std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
131                                             AbortIfCollapsed.end());
132     std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
133                                          AbortIfMerged.end());
134     std::map<std::string, unsigned> CheckFlagsM;
135     
136     for (cl::list<std::string>::iterator I = CheckFlags.begin(),
137            E = CheckFlags.end(); I != E; ++I) {
138       std::string::size_type ColonPos = I->rfind(':');
139       if (ColonPos == std::string::npos) {
140         std::cerr << "Error: '" << *I
141                << "' is an invalid value for the --dsgc-check-flags option!\n";
142         abort();
143       }
144
145       unsigned Flags = 0;
146       for (unsigned C = ColonPos+1; C != I->size(); ++C)
147         switch ((*I)[C]) {
148         case 'S': Flags |= DSNode::AllocaNode;  break;
149         case 'H': Flags |= DSNode::HeapNode;    break;
150         case 'G': Flags |= DSNode::GlobalNode;  break;
151         case 'U': Flags |= DSNode::UnknownNode; break;
152         case 'I': Flags |= DSNode::Incomplete;  break;
153         case 'M': Flags |= DSNode::Modified;    break;
154         case 'R': Flags |= DSNode::Read;        break;
155         case 'A': Flags |= DSNode::Array;       break;
156         default: std::cerr << "Invalid DSNode flag!\n"; abort();
157         }
158       CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
159     }
160     
161     // Now we loop over all of the scalars, checking to see if any are collapsed
162     // that are not supposed to be, or if any are merged together.
163     const DSGraph::ScalarMapTy &SM = G.getScalarMap();
164     std::map<DSNode*, std::string> AbortIfMergedNodes;
165     
166     for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
167          I != E; ++I)
168       if (I->first->hasName() && I->second.getNode()) {
169         const std::string &Name = I->first->getName();
170         DSNode *N = I->second.getNode();
171         
172         // Verify it is not collapsed if it is not supposed to be...
173         if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
174           std::cerr << "Node for value '%" << Name << "' is collapsed: ";
175           N->print(std::cerr, &G);
176           abort();
177         }
178
179         if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
180           std::cerr << "Node flags are not as expected for node: " << Name
181                     << "\n";
182           N->print(std::cerr, &G);
183           abort();
184         }
185
186         // Verify that it is not merged if it is not supposed to be...
187         if (AbortIfMergedS.count(Name)) {
188           if (AbortIfMergedNodes.count(N)) {
189             std::cerr << "Nodes for values '%" << Name << "' and '%"
190                       << AbortIfMergedNodes[N] << "' is merged: ";
191             N->print(std::cerr, &G);
192             abort();
193           }
194           AbortIfMergedNodes[N] = Name;
195         }
196       }
197   }
198 }