Added LLVM project notice to the top of every C++ source file.
[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-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.
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
32 namespace {
33   enum DSPass { local, bu, td };
34   cl::opt<DSPass>
35   DSPass("dsgc-dspass", cl::Hidden,
36        cl::desc("Specify which DSA pass the -datastructure-gc pass should use"),
37          cl::values(clEnumVal(local, "Local pass"),
38                     clEnumVal(bu,    "Bottom-up pass"),
39                     clEnumVal(td,    "Top-down pass"), 0), cl::init(local));
40
41   cl::opt<bool>
42   AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
43                       cl::desc("Abort if any collapsed nodes are found"));
44   cl::list<std::string>
45   AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
46                    cl::desc("Abort if any of the named symbols is collapsed"));
47   cl::list<std::string>
48   CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
49              cl::desc("Check that flags are specified for nodes"));
50   cl::list<std::string>
51   AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
52              cl::desc("Abort if any of the named symbols are merged together"));
53
54   struct DSGC : public FunctionPass {
55     DSGC();
56     bool doFinalization(Module &M);
57     bool runOnFunction(Function &F);
58
59     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60       switch (DSPass) {
61       case local: AU.addRequired<LocalDataStructures>(); break;
62       case bu:    AU.addRequired<BUDataStructures>(); break;
63       case td:    AU.addRequired<TDDataStructures>(); break;
64       }
65       AU.setPreservesAll();
66     }
67     void print(std::ostream &O, const Module *M) const {}
68
69   private:
70     void verify(const DSGraph &G);
71   };
72
73   RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
74 }
75
76 DSGC::DSGC() {
77   if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
78       CheckFlags.empty() && AbortIfMerged.empty()) {
79     std::cerr << "The -datastructure-gc is useless if you don't specify any"
80                  " -dsgc-* options.  See the -help-hidden output for a list.\n";
81     abort();
82   }
83 }
84
85
86 /// doFinalization - Verify that the globals graph is in good shape...
87 ///
88 bool DSGC::doFinalization(Module &M) {
89   switch (DSPass) {
90   case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
91   case bu:   verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
92   case td:   verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
93   }
94   return false;
95 }
96
97 /// runOnFunction - Get the DSGraph for this function and verify that it is ok.
98 ///
99 bool DSGC::runOnFunction(Function &F) {
100   switch (DSPass) {
101   case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
102   case bu:    verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
103   case td:    verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
104   }
105
106   return false;
107 }
108
109 /// verify - This is the function which checks to make sure that all of the
110 /// invariants established on the command line are true.
111 ///
112 void DSGC::verify(const DSGraph &G) {
113   // Loop over all of the nodes, checking to see if any are collapsed...
114   if (AbortIfAnyCollapsed) {
115     const std::vector<DSNode*> &Nodes = G.getNodes();
116     for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
117       if (Nodes[i]->isNodeCompletelyFolded()) {
118         std::cerr << "Node is collapsed: ";
119         Nodes[i]->print(std::cerr, &G);
120         abort();
121       }
122   }
123
124   if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
125       !AbortIfMerged.empty()) {
126     // Convert from a list to a set, because we don't have cl::set's yet.  FIXME
127     std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
128                                             AbortIfCollapsed.end());
129     std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
130                                          AbortIfMerged.end());
131     std::map<std::string, unsigned> CheckFlagsM;
132     
133     for (cl::list<std::string>::iterator I = CheckFlags.begin(),
134            E = CheckFlags.end(); I != E; ++I) {
135       std::string::size_type ColonPos = I->rfind(':');
136       if (ColonPos == std::string::npos) {
137         std::cerr << "Error: '" << *I
138                << "' is an invalid value for the --dsgc-check-flags option!\n";
139         abort();
140       }
141
142       unsigned Flags = 0;
143       for (unsigned C = ColonPos+1; C != I->size(); ++C)
144         switch ((*I)[C]) {
145         case 'S': Flags |= DSNode::AllocaNode;  break;
146         case 'H': Flags |= DSNode::HeapNode;    break;
147         case 'G': Flags |= DSNode::GlobalNode;  break;
148         case 'U': Flags |= DSNode::UnknownNode; break;
149         case 'I': Flags |= DSNode::Incomplete;  break;
150         case 'M': Flags |= DSNode::Modified;    break;
151         case 'R': Flags |= DSNode::Read;        break;
152         case 'A': Flags |= DSNode::Array;       break;
153         default: std::cerr << "Invalid DSNode flag!\n"; abort();
154         }
155       CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
156     }
157     
158     // Now we loop over all of the scalars, checking to see if any are collapsed
159     // that are not supposed to be, or if any are merged together.
160     const DSGraph::ScalarMapTy &SM = G.getScalarMap();
161     std::map<DSNode*, std::string> AbortIfMergedNodes;
162     
163     for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
164          I != E; ++I)
165       if (I->first->hasName() && I->second.getNode()) {
166         const std::string &Name = I->first->getName();
167         DSNode *N = I->second.getNode();
168         
169         // Verify it is not collapsed if it is not supposed to be...
170         if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
171           std::cerr << "Node for value '%" << Name << "' is collapsed: ";
172           N->print(std::cerr, &G);
173           abort();
174         }
175
176         if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
177           std::cerr << "Node flags are not as expected for node: " << Name
178                     << "\n";
179           N->print(std::cerr, &G);
180           abort();
181         }
182
183         // Verify that it is not merged if it is not supposed to be...
184         if (AbortIfMergedS.count(Name)) {
185           if (AbortIfMergedNodes.count(N)) {
186             std::cerr << "Nodes for values '%" << Name << "' and '%"
187                       << AbortIfMergedNodes[N] << "' is merged: ";
188             N->print(std::cerr, &G);
189             abort();
190           }
191           AbortIfMergedNodes[N] = Name;
192         }
193       }
194   }
195 }