Create an equivalence class of global variables that DSA will never be able
authorChris Lattner <sabre@nondot.org>
Sat, 19 Mar 2005 22:23:45 +0000 (22:23 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 19 Mar 2005 22:23:45 +0000 (22:23 +0000)
to tell apart anyway, and only track the leader for of these equivalence
classes in our graphs.

This dramatically reduces the number of GlobalValue*'s that appear in scalar
maps, which A) reduces memory usage, by eliminating many many scalarmap entries
and B) reduces time for operations that need to execute an operation for each
global in the scalar map.

As an example, this reduces the memory used to analyze 176.gcc from 1GB to
511MB, which (while it's still way too much) is better because it doesn't hit
swap anymore.  On eon, this shrinks the local graphs from 14MB to 6.8MB,
shrinks the bu+td graphs of povray from 50M to 40M, shrinks the TD graphs of
130.li from 8.8M to 3.6M, etc.

This change also speeds up DSA on large programs where this makes a big
difference.  For example, 130.li goes from 1.17s -> 0.56s, 134.perl goes
from 2.14 -> 0.93s, povray goes from 15.63s->7.99s (!!!).

This also apparently either fixes the problem that caused DSA to crash on
perlbmk and gcc, or it hides it, because DSA now works on these.  These
both take entirely too much time in the TD pass (147s for perl, 538s for
gcc, vs 7.67/5.9s in the bu pass for either one), but this is a known
problem that I'll deal with later.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20696 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/DataStructure/BottomUpClosure.cpp
lib/Analysis/DataStructure/CompleteBottomUp.cpp
lib/Analysis/DataStructure/DataStructure.cpp
lib/Analysis/DataStructure/EquivClassGraphs.cpp
lib/Analysis/DataStructure/Local.cpp
lib/Analysis/DataStructure/Steensgaard.cpp
lib/Analysis/DataStructure/TopDownClosure.cpp

index 7ab7d8056435e931087545aebf7735ae9a21a45e..19030084a9e227b0cfd1fd5dcc9293a7e5dc3fd7 100644 (file)
@@ -37,7 +37,9 @@ using namespace DS;
 //
 bool BUDataStructures::runOnModule(Module &M) {
   LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
-  GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
+  GlobalECs = LocalDSA.getGlobalECs();
+
+  GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
   GlobalsGraph->setPrintAuxCalls();
 
   IndCallGraphMap = new std::map<std::vector<Function*>,
@@ -112,7 +114,8 @@ DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
   if (Graph) return *Graph;
 
   // Copy the local version into DSInfo...
-  Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
+  Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F),
+                      GlobalECs);
 
   Graph->setGlobalsGraph(GlobalsGraph);
   Graph->setPrintAuxCalls();
@@ -379,7 +382,7 @@ void BUDataStructures::calculateGraph(DSGraph &Graph) {
             E = CalledFuncs.end();
           
           // Start with a copy of the first graph.
-          GI = IndCallGraph.first = new DSGraph(getDSGraph(**I));
+          GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
           GI->setGlobalsGraph(Graph.getGlobalsGraph());
           std::vector<DSNodeHandle> &Args = IndCallGraph.second;
 
@@ -498,7 +501,7 @@ void BUDataStructures::copyValue(Value *From, Value *To) {
   if (Function *FromF = dyn_cast<Function>(From)) {
     Function *ToF = cast<Function>(To);
     assert(!DSInfo.count(ToF) && "New Function already exists!");
-    DSGraph *NG = new DSGraph(getDSGraph(*FromF));
+    DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
     DSInfo[ToF] = NG;
     assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
 
index a70080a8c66bc43e6c4359f38ee0ecd14f7ec6af..16ea4385dbe4af64931b62f79c6321789969be18 100644 (file)
@@ -34,7 +34,7 @@ namespace {
 //
 bool CompleteBUDataStructures::runOnModule(Module &M) {
   BUDataStructures &BU = getAnalysis<BUDataStructures>();
-  GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
+  GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
   GlobalsGraph->setPrintAuxCalls();
 
 #if 1   // REMOVE ME EVENTUALLY
@@ -121,7 +121,7 @@ DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) {
   if (Graph) return *Graph;
 
   // Copy the BU graph...
-  Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
+  Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
   Graph->setGlobalsGraph(GlobalsGraph);
   Graph->setPrintAuxCalls();
 
index 43170fdc24277883ec006bab03fea2000a8ead14..5f8b3bfb918a00ed1ba768e3dd30dc1ecefc95c3 100644 (file)
@@ -115,7 +115,7 @@ void DSNode::assertOK() const {
   assert(ParentGraph && "Node has no parent?");
   const DSScalarMap &SM = ParentGraph->getScalarMap();
   for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
-    assert(SM.count(Globals[i]));
+    assert(SM.global_count(Globals[i]));
     assert(SM.find(Globals[i])->second.getNode() == this);
   }
 }
@@ -143,6 +143,10 @@ void DSNode::forwardNode(DSNode *To, unsigned Offset) {
 // marks the node with the 'G' flag if it does not already have it.
 //
 void DSNode::addGlobal(GlobalValue *GV) {
+  // First, check to make sure this is the leader if the global is in an
+  // equivalence class.
+  GV = getParentGraph()->getScalarMap().getLeaderForGlobal(GV);
+
   // Keep the list sorted.
   std::vector<GlobalValue*>::iterator I =
     std::lower_bound(Globals.begin(), Globals.end(), GV);
@@ -1091,14 +1095,16 @@ std::string DSGraph::getFunctionNames() const {
 }
 
 
-DSGraph::DSGraph(const DSGraph &G) : GlobalsGraph(0), TD(G.TD) {
+DSGraph::DSGraph(const DSGraph &G, EquivalenceClasses<GlobalValue*> &ECs)
+  : GlobalsGraph(0), ScalarMap(ECs), TD(G.TD) {
   PrintAuxCalls = false;
   NodeMapTy NodeMap;
   cloneInto(G, ScalarMap, ReturnNodes, NodeMap);
 }
 
-DSGraph::DSGraph(const DSGraph &G, NodeMapTy &NodeMap)
-  : GlobalsGraph(0), TD(G.TD) {
+DSGraph::DSGraph(const DSGraph &G, NodeMapTy &NodeMap,
+                 EquivalenceClasses<GlobalValue*> &ECs)
+  : GlobalsGraph(0), ScalarMap(ECs), TD(G.TD) {
   PrintAuxCalls = false;
   cloneInto(G, ScalarMap, ReturnNodes, NodeMap);
 }
@@ -1865,8 +1871,9 @@ void DSGraph::removeDeadNodes(unsigned Flags) {
                               DSGraph::StripIncompleteBit);
 
   // Mark all nodes reachable by (non-global) scalar nodes as alive...
-  { TIME_REGION(Y, "removeDeadNodes:scalarscan");
-  for (DSScalarMap::iterator I = ScalarMap.begin(), E = ScalarMap.end(); I !=E;)
+{ TIME_REGION(Y, "removeDeadNodes:scalarscan");
+  for (DSScalarMap::iterator I = ScalarMap.begin(), E = ScalarMap.end();
+       I != E; ++I)
     if (isa<GlobalValue>(I->first)) {             // Keep track of global nodes
       assert(!I->second.isNull() && "Null global node?");
       assert(I->second.getNode()->isGlobalNode() && "Should be a global node!");
@@ -1881,29 +1888,10 @@ void DSGraph::removeDeadNodes(unsigned Flags) {
         else
           GGCloner.getClonedNH(I->second);
       }
-      ++I;
     } else {
-      DSNode *N = I->second.getNode();
-#if 0
-      // Check to see if this is a worthless node generated for non-pointer
-      // values, such as integers.  Consider an addition of long types: A+B.
-      // Assuming we can track all uses of the value in this context, and it is
-      // NOT used as a pointer, we can delete the node.  We will be able to
-      // detect this situation if the node pointed to ONLY has Unknown bit set
-      // in the node.  In this case, the node is not incomplete, does not point
-      // to any other nodes (no mod/ref bits set), and is therefore
-      // uninteresting for data structure analysis.  If we run across one of
-      // these, prune the scalar pointing to it.
-      //
-      if (N->getNodeFlags() == DSNode::UnknownNode && !isa<Argument>(I->first))
-        ScalarMap.erase(I++);
-      else {
-#endif
-        N->markReachableNodes(Alive);
-        ++I;
-      //}
+      I->second.getNode()->markReachableNodes(Alive);
     }
-  }
+}
 
   // The return values are alive as well.
   for (ReturnNodesTy::iterator I = ReturnNodes.begin(), E = ReturnNodes.end();
index 02b7fb7d824aa8c59c7471f598fc803ed5bce35c..447b36d7808117d15912582891263c4be0e7bdfc 100644 (file)
@@ -72,9 +72,10 @@ Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{
 //
 bool EquivClassGraphs::runOnModule(Module &M) {
   CBU = &getAnalysis<CompleteBUDataStructures>();
+  GlobalECs = CBU->getGlobalECs();
   DEBUG(CheckAllGraphs(&M, *CBU));
 
-  GlobalsGraph = new DSGraph(CBU->getGlobalsGraph());
+  GlobalsGraph = new DSGraph(CBU->getGlobalsGraph(), GlobalECs);
   GlobalsGraph->setPrintAuxCalls();
 
   ActualCallees = CBU->getActualCallees();
@@ -305,7 +306,7 @@ DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
   DSGraph &CBUGraph = CBU->getDSGraph(F);
 
   // Copy the CBU graph...
-  Graph = new DSGraph(CBUGraph);           // updates the map via reference
+  Graph = new DSGraph(CBUGraph, GlobalECs);   // updates the map via reference
   Graph->setGlobalsGraph(&getGlobalsGraph());
   Graph->setPrintAuxCalls();
 
index 9cb899cb374d014f347cc421ee9ced52c0b6abba..02b5b16a586e80d9126acf2a00b1c8ae28a7aeaa 100644 (file)
@@ -164,8 +164,9 @@ using namespace DS;
 //===----------------------------------------------------------------------===//
 // DSGraph constructor - Simply use the GraphBuilder to construct the local
 // graph.
-DSGraph::DSGraph(const TargetData &td, Function &F, DSGraph *GG)
-  : GlobalsGraph(GG), TD(td) {
+DSGraph::DSGraph(EquivalenceClasses<GlobalValue*> &ECs, const TargetData &td,
+                 Function &F, DSGraph *GG)
+  : GlobalsGraph(GG), ScalarMap(ECs), TD(td) {
   PrintAuxCalls = false;
 
   DEBUG(std::cerr << "  [Loc] Calculating graph for: " << F.getName() << "\n");
@@ -1071,21 +1072,49 @@ void GraphBuilder::mergeInGlobalInitializer(GlobalVariable *GV) {
 bool LocalDataStructures::runOnModule(Module &M) {
   const TargetData &TD = getAnalysis<TargetData>();
 
-  GlobalsGraph = new DSGraph(TD);
-
+  // First step, build the globals graph.
+  GlobalsGraph = new DSGraph(GlobalECs, TD);
   {
     GraphBuilder GGB(*GlobalsGraph);
     
-    // Add initializers for all of the globals to the globals graph...
-    for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
+    // Add initializers for all of the globals to the globals graph.
+    for (Module::global_iterator I = M.global_begin(), E = M.global_end();
+         I != E; ++I)
       if (!I->isExternal())
         GGB.mergeInGlobalInitializer(I);
   }
 
+  // Next step, iterate through the nodes in the globals graph, unioning
+  // together the globals into equivalence classes.
+  for (DSGraph::node_iterator I = GlobalsGraph->node_begin(),
+         E = GlobalsGraph->node_end(); I != E; ++I)
+    if (I->getGlobals().size() > 1) {
+      // First, build up the equivalence set for this block of globals.
+      const std::vector<GlobalValue*> &GVs = I->getGlobals();
+      GlobalValue *First = GVs[0];
+      for (unsigned i = 1, e = GVs.size(); i != e; ++i)
+        GlobalECs.unionSets(First, GVs[i]);
+
+      // Next, get the leader element.
+      First = GlobalECs.getLeaderValue(First);
+
+      // Next, remove all globals from the scalar map that are not the leader.
+      DSScalarMap &SM = GlobalsGraph->getScalarMap();
+      for (unsigned i = 0, e = GVs.size(); i != e; ++i)
+        if (GVs[i] != First)
+          SM.erase(SM.find(GVs[i]));
+
+      // Finally, change the global node to only contain the leader.
+      I->clearGlobals();
+      I->addGlobal(First);
+    }
+  DEBUG(GlobalsGraph->AssertGraphOK());
+
   // Calculate all of the graphs...
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
     if (!I->isExternal())
-      DSInfo.insert(std::make_pair(I, new DSGraph(TD, *I, GlobalsGraph)));
+      DSInfo.insert(std::make_pair(I, new DSGraph(GlobalECs, TD, *I,
+                                                  GlobalsGraph)));
 
   GlobalsGraph->removeTriviallyDeadNodes();
   GlobalsGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
index dd07ee6f8249aefc63816d430f8eb99a567c78d0..9ed65bb97502bbb09e8f7e0e55d666a3bb89a9a3 100644 (file)
@@ -26,6 +26,8 @@ namespace {
   class Steens : public ModulePass, public AliasAnalysis {
     DSGraph *ResultGraph;
     DSGraph *GlobalsGraph;  // FIXME: Eliminate globals graph stuff from DNE
+
+    EquivalenceClasses<GlobalValue*> GlobalECs;  // Always empty
   public:
     Steens() : ResultGraph(0), GlobalsGraph(0) {}
     ~Steens() {
@@ -112,8 +114,8 @@ bool Steens::runOnModule(Module &M) {
   LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
 
   // Create a new, empty, graph...
-  ResultGraph = new DSGraph(getTargetData());
-  GlobalsGraph = new DSGraph(getTargetData());
+  ResultGraph = new DSGraph(GlobalECs, getTargetData());
+  GlobalsGraph = new DSGraph(GlobalECs, getTargetData());
   ResultGraph->setGlobalsGraph(GlobalsGraph);
   ResultGraph->setPrintAuxCalls();
 
@@ -128,7 +130,7 @@ bool Steens::runOnModule(Module &M) {
   unsigned Count = 0;
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
     if (!I->isExternal()) {
-      DSGraph::ScalarMapTy ValMap;
+      DSGraph::ScalarMapTy ValMap(GlobalECs);
       {  // Scope to free NodeMap memory ASAP
         DSGraph::NodeMapTy NodeMap;
         const DSGraph &FDSG = LDS.getDSGraph(*I);
index 580f22884f5131c32bbda7e47570bb0e3b7eb447..9a661e24a57bae3cb347bc66de14c4c97e5bf6ea 100644 (file)
@@ -53,7 +53,8 @@ void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N,
 //
 bool TDDataStructures::runOnModule(Module &M) {
   BUDataStructures &BU = getAnalysis<BUDataStructures>();
-  GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
+  GlobalECs = BU.getGlobalECs();
+  GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
   GlobalsGraph->setPrintAuxCalls();
 
   // Figure out which functions must not mark their arguments complete because
@@ -115,7 +116,7 @@ bool TDDataStructures::runOnModule(Module &M) {
 DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
   DSGraph *&G = DSInfo[&F];
   if (G == 0) { // Not created yet?  Clone BU graph...
-    G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
+    G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
     G->getAuxFunctionCalls().clear();
     G->setPrintAuxCalls();
     G->setGlobalsGraph(GlobalsGraph);
@@ -324,7 +325,7 @@ void TDDataStructures::copyValue(Value *From, Value *To) {
   if (Function *FromF = dyn_cast<Function>(From)) {
     Function *ToF = cast<Function>(To);
     assert(!DSInfo.count(ToF) && "New Function already exists!");
-    DSGraph *NG = new DSGraph(getDSGraph(*FromF));
+    DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
     DSInfo[ToF] = NG;
     assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");