LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GraphChecker.cpp

Go to the documentation of this file.
00001 //===- GraphChecker.cpp - Assert that various graph properties hold -------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass is used to test DSA with regression tests.  It can be used to check
00011 // that certain graph properties hold, such as two nodes being disjoint, whether
00012 // or not a node is collapsed, etc.  These are the command line arguments that
00013 // it supports:
00014 //
00015 //   --dsgc-dspass={local,bu,td}      - Specify what flavor of graph to check
00016 //   --dsgc-abort-if-any-collapsed    - Abort if any collapsed nodes are found
00017 //   --dsgc-abort-if-collapsed=<list> - Abort if a node pointed to by an SSA
00018 //                                      value with name in <list> is collapsed
00019 //   --dsgc-check-flags=<list>        - Abort if the specified nodes have flags
00020 //                                      that are not specified.
00021 //   --dsgc-abort-if-merged=<list>    - Abort if any of the named SSA values
00022 //                                      point to the same node.
00023 //
00024 //===----------------------------------------------------------------------===//
00025 
00026 #include "llvm/Analysis/DataStructure/DataStructure.h"
00027 #include "llvm/Analysis/DataStructure/DSGraph.h"
00028 #include "llvm/Support/CommandLine.h"
00029 #include "llvm/Value.h"
00030 #include <iostream>
00031 #include <set>
00032 
00033 using namespace llvm;
00034 
00035 namespace {
00036   enum DSPass { local, bu, td };
00037   cl::opt<DSPass>
00038   DSPass("dsgc-dspass", cl::Hidden,
00039        cl::desc("Specify which DSA pass the -datastructure-gc pass should use"),
00040          cl::values(clEnumVal(local, "Local pass"),
00041                     clEnumVal(bu,    "Bottom-up pass"),
00042                     clEnumVal(td,    "Top-down pass"), 
00043                     clEnumValEnd), cl::init(local));
00044 
00045   cl::opt<bool>
00046   AbortIfAnyCollapsed("dsgc-abort-if-any-collapsed", cl::Hidden,
00047                       cl::desc("Abort if any collapsed nodes are found"));
00048   cl::list<std::string>
00049   AbortIfCollapsed("dsgc-abort-if-collapsed", cl::Hidden, cl::CommaSeparated,
00050                    cl::desc("Abort if any of the named symbols is collapsed"));
00051   cl::list<std::string>
00052   CheckFlags("dsgc-check-flags", cl::Hidden, cl::CommaSeparated,
00053              cl::desc("Check that flags are specified for nodes"));
00054   cl::list<std::string>
00055   AbortIfMerged("dsgc-abort-if-merged", cl::Hidden, cl::CommaSeparated,
00056              cl::desc("Abort if any of the named symbols are merged together"));
00057 
00058   struct DSGC : public FunctionPass {
00059     DSGC();
00060     bool doFinalization(Module &M);
00061     bool runOnFunction(Function &F);
00062 
00063     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00064       switch (DSPass) {
00065       case local: AU.addRequired<LocalDataStructures>(); break;
00066       case bu:    AU.addRequired<BUDataStructures>(); break;
00067       case td:    AU.addRequired<TDDataStructures>(); break;
00068       }
00069       AU.setPreservesAll();
00070     }
00071     void print(std::ostream &O, const Module *M) const {}
00072 
00073   private:
00074     void verify(const DSGraph &G);
00075   };
00076 
00077   RegisterAnalysis<DSGC> X("datastructure-gc", "DSA Graph Checking Pass");
00078 }
00079 
00080 DSGC::DSGC() {
00081   if (!AbortIfAnyCollapsed && AbortIfCollapsed.empty() &&
00082       CheckFlags.empty() && AbortIfMerged.empty()) {
00083     std::cerr << "The -datastructure-gc is useless if you don't specify any"
00084                  " -dsgc-* options.  See the -help-hidden output for a list.\n";
00085     abort();
00086   }
00087 }
00088 
00089 
00090 /// doFinalization - Verify that the globals graph is in good shape...
00091 ///
00092 bool DSGC::doFinalization(Module &M) {
00093   switch (DSPass) {
00094   case local:verify(getAnalysis<LocalDataStructures>().getGlobalsGraph());break;
00095   case bu:   verify(getAnalysis<BUDataStructures>().getGlobalsGraph()); break;
00096   case td:   verify(getAnalysis<TDDataStructures>().getGlobalsGraph()); break;
00097   }
00098   return false;
00099 }
00100 
00101 /// runOnFunction - Get the DSGraph for this function and verify that it is ok.
00102 ///
00103 bool DSGC::runOnFunction(Function &F) {
00104   switch (DSPass) {
00105   case local: verify(getAnalysis<LocalDataStructures>().getDSGraph(F)); break;
00106   case bu:    verify(getAnalysis<BUDataStructures>().getDSGraph(F)); break;
00107   case td:    verify(getAnalysis<TDDataStructures>().getDSGraph(F)); break;
00108   }
00109 
00110   return false;
00111 }
00112 
00113 /// verify - This is the function which checks to make sure that all of the
00114 /// invariants established on the command line are true.
00115 ///
00116 void DSGC::verify(const DSGraph &G) {
00117   // Loop over all of the nodes, checking to see if any are collapsed...
00118   if (AbortIfAnyCollapsed) {
00119     for (DSGraph::node_iterator I = G.node_begin(), E = G.node_end(); I!=E; ++I)
00120       if ((*I)->isNodeCompletelyFolded()) {
00121         std::cerr << "Node is collapsed: ";
00122         (*I)->print(std::cerr, &G);
00123         abort();
00124       }
00125   }
00126 
00127   if (!AbortIfCollapsed.empty() || !CheckFlags.empty() ||
00128       !AbortIfMerged.empty()) {
00129     // Convert from a list to a set, because we don't have cl::set's yet.  FIXME
00130     std::set<std::string> AbortIfCollapsedS(AbortIfCollapsed.begin(),
00131                                             AbortIfCollapsed.end());
00132     std::set<std::string> AbortIfMergedS(AbortIfMerged.begin(),
00133                                          AbortIfMerged.end());
00134     std::map<std::string, unsigned> CheckFlagsM;
00135     
00136     for (cl::list<std::string>::iterator I = CheckFlags.begin(),
00137            E = CheckFlags.end(); I != E; ++I) {
00138       std::string::size_type ColonPos = I->rfind(':');
00139       if (ColonPos == std::string::npos) {
00140         std::cerr << "Error: '" << *I
00141                << "' is an invalid value for the --dsgc-check-flags option!\n";
00142         abort();
00143       }
00144 
00145       unsigned Flags = 0;
00146       for (unsigned C = ColonPos+1; C != I->size(); ++C)
00147         switch ((*I)[C]) {
00148         case 'S': Flags |= DSNode::AllocaNode;  break;
00149         case 'H': Flags |= DSNode::HeapNode;    break;
00150         case 'G': Flags |= DSNode::GlobalNode;  break;
00151         case 'U': Flags |= DSNode::UnknownNode; break;
00152         case 'I': Flags |= DSNode::Incomplete;  break;
00153         case 'M': Flags |= DSNode::Modified;    break;
00154         case 'R': Flags |= DSNode::Read;        break;
00155         case 'A': Flags |= DSNode::Array;       break;
00156         default: std::cerr << "Invalid DSNode flag!\n"; abort();
00157         }
00158       CheckFlagsM[std::string(I->begin(), I->begin()+ColonPos)] = Flags;
00159     }
00160     
00161     // Now we loop over all of the scalars, checking to see if any are collapsed
00162     // that are not supposed to be, or if any are merged together.
00163     const DSGraph::ScalarMapTy &SM = G.getScalarMap();
00164     std::map<DSNode*, std::string> AbortIfMergedNodes;
00165     
00166     for (DSGraph::ScalarMapTy::const_iterator I = SM.begin(), E = SM.end();
00167          I != E; ++I)
00168       if (I->first->hasName() && I->second.getNode()) {
00169         const std::string &Name = I->first->getName();
00170         DSNode *N = I->second.getNode();
00171         
00172         // Verify it is not collapsed if it is not supposed to be...
00173         if (N->isNodeCompletelyFolded() && AbortIfCollapsedS.count(Name)) {
00174           std::cerr << "Node for value '%" << Name << "' is collapsed: ";
00175           N->print(std::cerr, &G);
00176           abort();
00177         }
00178 
00179         if (CheckFlagsM.count(Name) && CheckFlagsM[Name] != N->getNodeFlags()) {
00180           std::cerr << "Node flags are not as expected for node: " << Name
00181                     << "\n";
00182           N->print(std::cerr, &G);
00183           abort();
00184         }
00185 
00186         // Verify that it is not merged if it is not supposed to be...
00187         if (AbortIfMergedS.count(Name)) {
00188           if (AbortIfMergedNodes.count(N)) {
00189             std::cerr << "Nodes for values '%" << Name << "' and '%"
00190                       << AbortIfMergedNodes[N] << "' is merged: ";
00191             N->print(std::cerr, &G);
00192             abort();
00193           }
00194           AbortIfMergedNodes[N] = Name;
00195         }
00196       }
00197   }
00198 }