Constness changes
[oota-llvm.git] / include / llvm / Analysis / DataStructure / DSSupport.h
index 06bcb5eaa9f4e59d7a1806997744916628fa86ef..1e886fe7c26da97cafa5e16dd2afabed4aea2490 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 #include <functional>
 #include <string>
+#include <assert.h>
 #include "Support/HashExtras.h"
 #include "Support/hash_set"
 
@@ -45,37 +46,54 @@ namespace DS { // FIXME: After the paper, this should get cleaned up
 /// DSNodeHandle (and friends) in one file complicates things.
 ///
 class DSNodeHandle {
-  DSNode *N;
-  unsigned Offset;
+  mutable DSNode *N;
+  mutable unsigned Offset;
+  void operator==(const DSNode *N);  // DISALLOW, use to promote N to nodehandle
 public:
   // Allow construction, destruction, and assignment...
   DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
     setNode(n);
   }
-  DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(H.Offset) { setNode(H.N); }
+  DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(0) {
+    setNode(H.getNode());
+    Offset = H.Offset;      // Must read offset AFTER the getNode()
+  }
   ~DSNodeHandle() { setNode((DSNode*)0); }
   DSNodeHandle &operator=(const DSNodeHandle &H) {
-    setNode(H.N); Offset = H.Offset;
+    Offset = 0; setNode(H.getNode()); Offset = H.Offset;
     return *this;
   }
 
   bool operator<(const DSNodeHandle &H) const {  // Allow sorting
-    return N < H.N || (N == H.N && Offset < H.Offset);
+    return getNode() < H.getNode() || (N == H.N && Offset < H.Offset);
   }
   bool operator>(const DSNodeHandle &H) const { return H < *this; }
   bool operator==(const DSNodeHandle &H) const { // Allow comparison
-    return N == H.N && Offset == H.Offset;
+    return getNode() == H.getNode() && Offset == H.Offset;
   }
   bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
 
-  inline void swap(DSNodeHandle &H);
+  inline void swap(DSNodeHandle &NH) {
+    std::swap(Offset, NH.Offset);
+    std::swap(N, NH.N);
+  }
+
+  /// isNull - Check to see if getNode() == 0, without going through the trouble
+  /// of checking to see if we are forwarding...
+  bool isNull() const { return N == 0; }
 
   // Allow explicit conversion to DSNode...
-  DSNode *getNode() const { return N; }
+  inline DSNode *getNode() const;  // Defined inline in DSNode.h
   unsigned getOffset() const { return Offset; }
 
-  inline void setNode(DSNode *N);  // Defined inline later...
-  void setOffset(unsigned O) { Offset = O; }
+  inline void setNode(DSNode *N);  // Defined inline in DSNode.h
+  void setOffset(unsigned O) {
+    //assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
+    //       !N->ForwardNH.isNull()) && "Node handle offset out of range!");
+    //assert((!N || O < N->Size || (N->Size == 0 && O == 0) ||
+    //       !N->ForwardNH.isNull()) && "Node handle offset out of range!");
+    Offset = O;
+  }
 
   void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
   void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
@@ -95,27 +113,25 @@ public:
   inline DSNodeHandle &getLink(unsigned Num);
 
   inline void setLink(unsigned Num, const DSNodeHandle &NH);
+private:
+  DSNode *HandleForwarding() const;
 };
 
-inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
+namespace std {
+  inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
+}
 
 //===----------------------------------------------------------------------===//
 /// DSCallSite - Representation of a call site via its call instruction,
 /// the DSNode handle for the callee function (or function pointer), and
 /// the DSNode handles for the function arguments.
-///
-/// One unusual aspect of this callsite record is the ResolvingCaller member.
-/// If this is non-null, then it indicates the function that allowed a call-site
-/// to finally be resolved.  Because of indirect calls, this function may not
-/// actually be the function that contains the Call instruction itself.  This is
-/// used by the BU and TD passes to communicate.
 /// 
 class DSCallSite {
-  CallInst    *Inst;                    // Actual call site
-  DSNodeHandle RetVal;                  // Returned value
-  DSNodeHandle Callee;                  // The function node called
-  std::vector<DSNodeHandle> CallArgs;   // The pointer arguments
-  Function    *ResolvingCaller;         // See comments above
+  CallInst    *Inst;                 // Actual call site
+  Function    *CalleeF;              // The function called (direct call)
+  DSNodeHandle CalleeN;              // The function node called (indirect call)
+  DSNodeHandle RetVal;               // Returned value
+  std::vector<DSNodeHandle> CallArgs;// The pointer arguments
 
   static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
                      const hash_map<const DSNode*, DSNode*> &NodeMap) {
@@ -144,16 +160,22 @@ public:
   /// Constructor.  Note - This ctor destroys the argument vector passed in.  On
   /// exit, the argument vector is empty.
   ///
-  DSCallSite(CallInst &inst, const DSNodeHandle &rv, const DSNodeHandle &callee,
+  DSCallSite(CallInst &inst, const DSNodeHandle &rv, DSNode *Callee,
              std::vector<DSNodeHandle> &Args)
-    : Inst(&inst), RetVal(rv), Callee(callee), ResolvingCaller(0) {
+    : Inst(&inst), CalleeF(0), CalleeN(Callee), RetVal(rv) {
+    assert(Callee && "Null callee node specified for call site!");
+    Args.swap(CallArgs);
+  }
+  DSCallSite(CallInst &inst, const DSNodeHandle &rv, Function *Callee,
+             std::vector<DSNodeHandle> &Args)
+    : Inst(&inst), CalleeF(Callee), RetVal(rv) {
+    assert(Callee && "Null callee function specified for call site!");
     Args.swap(CallArgs);
   }
 
   DSCallSite(const DSCallSite &DSCS)   // Simple copy ctor
-    : Inst(DSCS.Inst), RetVal(DSCS.RetVal),
-      Callee(DSCS.Callee), CallArgs(DSCS.CallArgs),
-      ResolvingCaller(DSCS.ResolvingCaller) {}
+    : Inst(DSCS.Inst), CalleeF(DSCS.CalleeF), CalleeN(DSCS.CalleeN),
+      RetVal(DSCS.RetVal), CallArgs(DSCS.CallArgs) {}
 
   /// Mapping copy constructor - This constructor takes a preexisting call site
   /// to copy plus a map that specifies how the links should be transformed.
@@ -163,27 +185,36 @@ public:
   DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
     Inst = FromCall.Inst;
     InitNH(RetVal, FromCall.RetVal, NodeMap);
-    InitNH(Callee, FromCall.Callee, NodeMap);
+    InitNH(CalleeN, FromCall.CalleeN, NodeMap);
+    CalleeF = FromCall.CalleeF;
 
     CallArgs.resize(FromCall.CallArgs.size());
     for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
       InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
-    ResolvingCaller = FromCall.ResolvingCaller;
   }
 
+  /// isDirectCall - Return true if this call site is a direct call of the
+  /// function specified by getCalleeFunc.  If not, it is an indirect call to
+  /// the node specified by getCalleeNode.
+  ///
+  bool isDirectCall() const { return CalleeF != 0; }
+  bool isIndirectCall() const { return !isDirectCall(); }
+
+
   // Accessor functions...
   Function           &getCaller()     const;
   CallInst           &getCallInst()   const { return *Inst; }
         DSNodeHandle &getRetVal()           { return RetVal; }
-        DSNodeHandle &getCallee()           { return Callee; }
   const DSNodeHandle &getRetVal()     const { return RetVal; }
-  const DSNodeHandle &getCallee()     const { return Callee; }
-  void setCallee(const DSNodeHandle &H) { Callee = H; }
 
-  unsigned            getNumPtrArgs() const { return CallArgs.size(); }
+  DSNode *getCalleeNode() const {
+    assert(!CalleeF && CalleeN.getNode()); return CalleeN.getNode();
+  }
+  Function *getCalleeFunc() const {
+    assert(!CalleeN.getNode() && CalleeF); return CalleeF;
+  }
 
-  Function           *getResolvingCaller() const { return ResolvingCaller; }
-  void setResolvingCaller(Function *F) { ResolvingCaller = F; }
+  unsigned            getNumPtrArgs() const { return CallArgs.size(); }
 
   DSNodeHandle &getPtrArg(unsigned i) {
     assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
@@ -198,9 +229,9 @@ public:
     if (this != &CS) {
       std::swap(Inst, CS.Inst);
       std::swap(RetVal, CS.RetVal);
-      std::swap(Callee, CS.Callee);
+      std::swap(CalleeN, CS.CalleeN);
+      std::swap(CalleeF, CS.CalleeF);
       std::swap(CallArgs, CS.CallArgs);
-      std::swap(ResolvingCaller, CS.ResolvingCaller);
     }
   }
 
@@ -222,19 +253,27 @@ public:
   void markReachableNodes(hash_set<DSNode*> &Nodes);
 
   bool operator<(const DSCallSite &CS) const {
-    if (Callee < CS.Callee) return true;   // This must sort by callee first!
-    if (Callee > CS.Callee) return false;
+    if (isDirectCall()) {      // This must sort by callee first!
+      if (CS.isIndirectCall()) return true;
+      if (CalleeF < CS.CalleeF) return true;
+      if (CalleeF > CS.CalleeF) return false;
+    } else {
+      if (CS.isDirectCall()) return false;
+      if (CalleeN < CS.CalleeN) return true;
+      if (CalleeN > CS.CalleeN) return false;
+    }
     if (RetVal < CS.RetVal) return true;
     if (RetVal > CS.RetVal) return false;
     return CallArgs < CS.CallArgs;
   }
 
   bool operator==(const DSCallSite &CS) const {
-    return RetVal == CS.RetVal && Callee == CS.Callee &&
-           CallArgs == CS.CallArgs;
+    return RetVal == CS.RetVal && CalleeN == CS.CalleeN &&
+           CalleeF == CS.CalleeF && CallArgs == CS.CallArgs;
   }
 };
 
-inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }
-
+namespace std {
+  inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }
+}
 #endif