Don't apply type information to load instructions if it will cause collapsing
[oota-llvm.git] / lib / Analysis / DataStructure / Local.cpp
index 169ccbf9f3370404d8cedd6fba6a00fa01060cbd..005580155d63ddafe3c2babcdb9c25b470422f50 100644 (file)
 #include "llvm/Support/InstVisitor.h"
 #include "llvm/Target/TargetData.h"
 #include "Support/Statistic.h"
+#include "Support/Timer.h"
+#include "Support/CommandLine.h"
 
 // FIXME: This should eventually be a FunctionPass that is automatically
 // aggregated into a Pass.
 //
 #include "llvm/Module.h"
 
-using std::map;
-using std::vector;
-
 static RegisterAnalysis<LocalDataStructures>
 X("datastructure", "Local Data Structure Analysis");
 
@@ -47,6 +46,14 @@ using namespace DS;
 
 
 namespace {
+  cl::opt<bool>
+  DisableDirectCallOpt("disable-direct-call-dsopt", cl::Hidden,
+                       cl::desc("Disable direct call optimization in "
+                                "DSGraph construction"));
+  cl::opt<bool>
+  DisableFieldSensitivity("disable-ds-field-sensitivity", cl::Hidden,
+                          cl::desc("Disable field sensitivity in DSGraphs"));
+
   //===--------------------------------------------------------------------===//
   //  GraphBuilder Class
   //===--------------------------------------------------------------------===//
@@ -56,15 +63,15 @@ namespace {
   ///
   class GraphBuilder : InstVisitor<GraphBuilder> {
     DSGraph &G;
-    vector<DSNode*> &Nodes;
+    std::vector<DSNode*> &Nodes;
     DSNodeHandle &RetNode;               // Node that gets returned...
-    map<Value*, DSNodeHandle> &ScalarMap;
-    vector<DSCallSite> &FunctionCalls;
+    hash_map<Value*, DSNodeHandle> &ScalarMap;
+    std::vector<DSCallSite> &FunctionCalls;
 
   public:
-    GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
-                 map<Value*, DSNodeHandle> &SM,
-                 vector<DSCallSite> &fc)
+    GraphBuilder(DSGraph &g, std::vector<DSNode*> &nodes, DSNodeHandle &retNode,
+                 hash_map<Value*, DSNodeHandle> &SM,
+                 std::vector<DSCallSite> &fc)
       : G(g), Nodes(nodes), RetNode(retNode), ScalarMap(SM), FunctionCalls(fc) {
 
       // Create scalar nodes for all pointer arguments...
@@ -91,9 +98,9 @@ namespace {
     void visitStoreInst(StoreInst &SI);
     void visitCallInst(CallInst &CI);
     void visitSetCondInst(SetCondInst &SCI) {}  // SetEQ & friends are ignored
-    void visitFreeInst(FreeInst &FI) {}         // Ignore free instructions
+    void visitFreeInst(FreeInst &FI);
     void visitCastInst(CastInst &CI);
-    void visitInstruction(Instruction &I) {}
+    void visitInstruction(Instruction &I);
 
   private:
     // Helper functions used to implement the visitation functions...
@@ -102,8 +109,9 @@ namespace {
     /// the graph.
     ///
     DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty = 0) {
-      DSNode *N = new DSNode(NodeType, Ty);   // Create the node
-      Nodes.push_back(N);                     // Add node to nodes list
+      DSNode *N = new DSNode(NodeType, Ty, &G);   // Create the node
+      if (DisableFieldSensitivity)
+        N->foldNodeCompletely();
       return N;
     }
 
@@ -132,10 +140,23 @@ DSGraph::DSGraph(Function &F, DSGraph *GG) : Func(&F), GlobalsGraph(GG) {
   PrintAuxCalls = false;
   // Use the graph builder to construct the local version of the graph
   GraphBuilder B(*this, Nodes, RetNode, ScalarMap, FunctionCalls);
-  markIncompleteNodes();
+#ifndef NDEBUG
+  Timer::addPeakMemoryMeasurement();
+#endif
+
+  // Remove all integral constants from the scalarmap!
+  for (hash_map<Value*, DSNodeHandle>::iterator I = ScalarMap.begin();
+       I != ScalarMap.end();)
+    if (isa<ConstantIntegral>(I->first)) {
+      hash_map<Value*, DSNodeHandle>::iterator J = I++;
+      ScalarMap.erase(J);
+    } else
+      ++I;
+
+  markIncompleteNodes(DSGraph::MarkFormalArgs);
 
   // Remove any nodes made dead due to merging...
-  removeDeadNodes();
+  removeDeadNodes(DSGraph::KeepUnreachableGlobals);
 }
 
 
@@ -143,7 +164,6 @@ DSGraph::DSGraph(Function &F, DSGraph *GG) : Func(&F), GlobalsGraph(GG) {
 // Helper method implementations...
 //
 
-
 /// getValueDest - Return the DSNode that the actual value points to.
 ///
 DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
@@ -151,34 +171,41 @@ DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
   if (V == Constant::getNullValue(V->getType()))
     return 0;  // Null doesn't point to anything, don't add to ScalarMap!
 
+  DSNodeHandle &NH = ScalarMap[V];
+  if (NH.getNode())
+    return NH;     // Already have a node?  Just return it...
+
+  // Otherwise we need to create a new node to point to.
+  // Check first for constant expressions that must be traversed to
+  // extract the actual value.
   if (Constant *C = dyn_cast<Constant>(V))
     if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
-      return getValueDest(*CPR->getValue());
+      return NH = getValueDest(*CPR->getValue());
     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
       if (CE->getOpcode() == Instruction::Cast)
-        return getValueDest(*CE->getOperand(0));
-      if (CE->getOpcode() == Instruction::GetElementPtr) {
+        NH = getValueDest(*CE->getOperand(0));
+      else if (CE->getOpcode() == Instruction::GetElementPtr) {
         visitGetElementPtrInst(*CE);
-        std::map<Value*, DSNodeHandle>::iterator I = ScalarMap.find(CE);
+        hash_map<Value*, DSNodeHandle>::iterator I = ScalarMap.find(CE);
         assert(I != ScalarMap.end() && "GEP didn't get processed right?");
-        DSNodeHandle NH = I->second;
-        ScalarMap.erase(I);           // Remove constant from scalarmap
-        return NH;
+        NH = I->second;
+      } else {
+        // This returns a conservative unknown node for any unhandled ConstExpr
+        return NH = createNode(DSNode::UnknownNode);
       }
+      if (NH.getNode() == 0) {  // (getelementptr null, X) returns null
+        ScalarMap.erase(V);
+        return 0;
+      }
+      return NH;
 
-      // This returns a conservative unknown node for any unhandled ConstExpr
-      return createNode(DSNode::UnknownNode);
     } else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(C)) {
       // Random constants are unknown mem
-      return createNode(DSNode::UnknownNode);
+      return NH = createNode(DSNode::UnknownNode);
     } else {
       assert(0 && "Unknown constant type!");
     }
 
-  DSNodeHandle &NH = ScalarMap[V];
-  if (NH.getNode())
-    return NH;     // Already have a node?  Just return it...
-
   // Otherwise we need to create a new node to point to...
   DSNode *N;
   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
@@ -341,7 +368,7 @@ void GraphBuilder::visitLoadInst(LoadInst &LI) {
   Ptr.getNode()->NodeType |= DSNode::Read;
 
   // Ensure a typerecord exists...
-  Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset());
+  Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset(), false);
 
   if (isPointerType(LI.getType()))
     setDestTo(LI, getLink(Ptr));
@@ -352,7 +379,7 @@ void GraphBuilder::visitStoreInst(StoreInst &SI) {
   DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
   if (Dest.getNode() == 0) return;
 
-  // Make that the node is written to...
+  // Mark that the node is written to...
   Dest.getNode()->NodeType |= DSNode::Modified;
 
   // Ensure a typerecord exists...
@@ -374,7 +401,9 @@ void GraphBuilder::visitCallInst(CallInst &CI) {
   if (isPointerType(CI.getType()))
     RetVal = getValueDest(CI);
 
-  DSNodeHandle Callee = getValueDest(*CI.getOperand(0));
+  DSNode *Callee = 0;
+  if (DisableDirectCallOpt || !isa<Function>(CI.getOperand(0)))
+    Callee = getValueDest(*CI.getOperand(0)).getNode();
 
   std::vector<DSNodeHandle> Args;
   Args.reserve(CI.getNumOperands()-1);
@@ -385,7 +414,17 @@ void GraphBuilder::visitCallInst(CallInst &CI) {
       Args.push_back(getValueDest(*CI.getOperand(i)));
 
   // Add a new function call entry...
-  FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
+  if (Callee)
+    FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
+  else
+    FunctionCalls.push_back(DSCallSite(CI, RetVal,
+                                       cast<Function>(CI.getOperand(0)), Args));
+}
+
+void GraphBuilder::visitFreeInst(FreeInst &FI) {
+  // Mark that the node is written to...
+  getValueDest(*FI.getOperand(0)).getNode()->NodeType
+    |= DSNode::Modified | DSNode::HeapNode;
 }
 
 /// Handle casts...
@@ -404,6 +443,21 @@ void GraphBuilder::visitCastInst(CastInst &CI) {
 }
 
 
+// visitInstruction - For all other instruction types, if we have any arguments
+// that are of pointer type, make them have unknown composition bits, and merge
+// the nodes together.
+void GraphBuilder::visitInstruction(Instruction &Inst) {
+  DSNodeHandle CurNode;
+  if (isPointerType(Inst.getType()))
+    CurNode = getValueDest(Inst);
+  for (User::op_iterator I = Inst.op_begin(), E = Inst.op_end(); I != E; ++I)
+    if (isPointerType((*I)->getType()))
+      CurNode.mergeWith(getValueDest(**I));
+
+  if (CurNode.getNode())
+    CurNode.getNode()->NodeType |= DSNode::UnknownNode;
+}
+
 
 
 //===----------------------------------------------------------------------===//
@@ -424,7 +478,7 @@ bool LocalDataStructures::run(Module &M) {
 // our memory... here...
 //
 void LocalDataStructures::releaseMemory() {
-  for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
+  for (hash_map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
          E = DSInfo.end(); I != E; ++I)
     delete I->second;