VNInfo cleanup.
[oota-llvm.git] / include / llvm / CodeGen / LiveInterval.h
index 00b51a16f1ea381738f2af774cb404585a022576..8cd58671888a7d135d90932f1c5f5d8b52ed68d7 100644 (file)
 
 namespace llvm {
   class MachineInstr;
+  class MachineRegisterInfo;
   class TargetRegisterInfo;
   struct LiveInterval;
 
-  /// VNInfo - If the value number definition is undefined (e.g. phi
-  /// merge point), it contains ~0u,x. If the value number is not in use, it
-  /// contains ~1u,x to indicate that the value # is not used. 
-  ///   def   - Instruction # of the definition.
-  ///         - or reg # of the definition if it's a stack slot liveinterval.
-  ///   copy  - Copy iff val# is defined by a copy; zero otherwise.
-  ///   hasPHIKill - One or more of the kills are PHI nodes.
-  ///   redefByEC - Re-defined by early clobber somewhere during the live range.
-  ///   kills - Instruction # of the kills.
-  struct VNInfo {
+  /// VNInfo - Value Number Information.
+  /// This class holds information about a machine level values, including
+  /// definition and use points.
+  ///
+  /// Care must be taken in interpreting the def index of the value. The 
+  /// following rules apply:
+  ///
+  /// If the isDefAccurate() method returns false then the def index does not
+  /// actually point to the defining MachineInstr, or even (necessarily) a
+  /// valid MachineInstr at all. In general such a def index should not be
+  /// used as an index to obtain a MachineInstr. The exception is Values
+  /// defined by PHI instructions, after PHI elimination has occured. In this
+  /// case the def should point to the start of the block in which the PHI
+  /// existed. This fact can be used to insert code dealing with the PHI value
+  /// at the merge point (e.g. to spill or split it).
+
+  class VNInfo {
+  private:
+    static const uint8_t HAS_PHI_KILL = 1,                         
+                         REDEF_BY_EC  = 1 << 1,
+                         IS_PHI_DEF = 1 << 2,
+                         IS_UNUSED = 1 << 3,
+                         IS_DEF_ACCURATE = 1 << 4;
+
+    uint8_t flags;
+
+  public:
+    /// The ID number of this value.
     unsigned id;
+    
+    /// The index of the defining instruction (if isDefAccurate() returns true).
     unsigned def;
     MachineInstr *copy;
-    bool hasPHIKill : 1;
-    bool redefByEC : 1;
     SmallVector<unsigned, 4> kills;
+
     VNInfo()
-      : id(~1U), def(~1U), copy(0), hasPHIKill(false), redefByEC(false) {}
+      : flags(IS_UNUSED), id(~1U), def(0), copy(0) {}
+
+    /// VNInfo constructor.
+    /// d is presumed to point to the actual defining instr. If it doesn't
+    /// setIsDefAccurate(false) should be called after construction.
     VNInfo(unsigned i, unsigned d, MachineInstr *c)
-      : id(i), def(d), copy(c), hasPHIKill(false), redefByEC(false) {}
+      : flags(IS_DEF_ACCURATE), id(i), def(d), copy(c) {}
+
+    /// VNInfo construtor, copies values from orig, except for the value number.
+    VNInfo(unsigned i, const VNInfo &orig)
+      : flags(orig.flags), id(i), def(orig.def), copy(orig.copy),
+        kills(orig.kills) {}
+
+    /// Used for copying value number info.
+    unsigned getFlags() const { return flags; }
+    void setFlags(unsigned flags) { this->flags = flags; }
+
+    /// Returns true if one or more kills are PHI nodes.
+    bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
+    void setHasPHIKill(bool hasKill) {
+      if (hasKill)
+        flags |= HAS_PHI_KILL;
+      else
+        flags &= ~HAS_PHI_KILL;
+    }
+
+    /// Returns true if this value is re-defined by an early clobber somewhere
+    /// during the live range.
+    bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
+    void setHasRedefByEC(bool hasRedef) {
+      if (hasRedef)
+        flags |= REDEF_BY_EC;
+      else
+        flags &= ~REDEF_BY_EC;
+    }
+  
+    /// Returns true if this value is defined by a PHI instruction (or was,
+    /// PHI instrucions may have been eliminated).
+    bool isPHIDef() const { return flags & IS_PHI_DEF; }
+    void setIsPHIDef(bool phiDef) {
+      if (phiDef)
+        flags |= IS_PHI_DEF;
+      else
+        flags &= ~IS_PHI_DEF;
+    }
+
+    /// Returns true if this value is unused.
+    bool isUnused() const { return flags & IS_UNUSED; }
+    void setIsUnused(bool unused) {
+      if (unused)
+        flags |= IS_UNUSED;
+      else
+        flags &= ~IS_UNUSED;
+    }
+
+    /// Returns true if the def is accurate.
+    bool isDefAccurate() const { return flags & IS_DEF_ACCURATE; }
+    void setIsDefAccurate(bool defAccurate) {
+      if (defAccurate)
+        flags |= IS_DEF_ACCURATE;
+      else 
+        flags &= ~IS_DEF_ACCURATE;
+    }
+
   };
 
   /// LiveRange structure - This represents a simple register range in the
@@ -108,13 +189,32 @@ namespace llvm {
     unsigned reg;        // the register or stack slot of this interval
                          // if the top bits is set, it represents a stack slot.
     float weight;        // weight of this interval
-    unsigned short preference; // preferred register for this interval
     Ranges ranges;       // the ranges in which this register is live
     VNInfoList valnos;   // value#'s
 
   public:
+    
+    struct InstrSlots {
+      enum {
+        LOAD  = 0,
+        USE   = 1,
+        DEF   = 2,
+        STORE = 3,
+        NUM   = 4
+      };
+
+      static unsigned scale(unsigned slot, unsigned factor) {
+        unsigned index = slot / NUM,
+                 offset = slot % NUM;
+        assert(index <= ~0U / (factor * NUM) &&
+               "Rescaled interval would overflow");
+        return index * NUM * factor + offset;
+      }
+
+    };
+
     LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
-      : reg(Reg), weight(Weight), preference(0)  {
+      : reg(Reg), weight(Weight) {
       if (IsSS)
         reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
     }
@@ -190,15 +290,17 @@ namespace llvm {
     void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
       DstValNo->def = SrcValNo->def;
       DstValNo->copy = SrcValNo->copy;
-      DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
-      DstValNo->redefByEC = SrcValNo->redefByEC;
+      DstValNo->setFlags(SrcValNo->getFlags());
       DstValNo->kills = SrcValNo->kills;
     }
 
     /// getNextValue - Create a new value number and return it.  MIIdx specifies
     /// the instruction that defines the value number.
     VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
-                         BumpPtrAllocator &VNInfoAllocator) {
+                         bool isDefAccurate, BumpPtrAllocator &VNInfoAllocator) {
+
+      assert(MIIdx != ~0u && MIIdx != ~1u &&
+             "PHI def / unused flags should now be passed explicitly.");
 #ifdef __GNUC__
       unsigned Alignment = (unsigned)__alignof__(VNInfo);
 #else
@@ -209,13 +311,29 @@ namespace llvm {
         static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
                                                       Alignment));
       new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
+      VNI->setIsDefAccurate(isDefAccurate);
       valnos.push_back(VNI);
       return VNI;
     }
 
-    /// getUnknownValNo - Find a value# for unknown values, if there isn't one
-    /// create a new one.
-    VNInfo *getUnknownValNo(BumpPtrAllocator &VNInfoAllocator);
+    /// Create a copy of the given value. The new value will be identical except
+    /// for the Value number.
+    VNInfo *createValueCopy(const VNInfo *orig, BumpPtrAllocator &VNInfoAllocator) {
+
+#ifdef __GNUC__
+      unsigned Alignment = (unsigned)__alignof__(VNInfo);
+#else
+      // FIXME: ugly.
+      unsigned Alignment = 8;
+#endif
+      VNInfo *VNI =
+        static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
+                                                      Alignment));
+    
+      new (VNI) VNInfo((unsigned)valnos.size(), *orig);
+      valnos.push_back(VNI);
+      return VNI;
+    }
 
     /// addKill - Add a kill instruction index to the specified value
     /// number.
@@ -323,7 +441,8 @@ namespace llvm {
 
     /// Copy - Copy the specified live interval. This copies all the fields
     /// except for the register of the interval.
-    void Copy(const LiveInterval &RHS, BumpPtrAllocator &VNInfoAllocator);
+    void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
+              BumpPtrAllocator &VNInfoAllocator);
     
     bool empty() const { return ranges.empty(); }
 
@@ -400,7 +519,8 @@ namespace llvm {
     /// the intervals are not joinable, this aborts.
     void join(LiveInterval &Other, const int *ValNoAssignments,
               const int *RHSValNoAssignments,
-              SmallVector<VNInfo*, 16> &NewVNInfo);
+              SmallVector<VNInfo*, 16> &NewVNInfo,
+              MachineRegisterInfo *MRI);
 
     /// isInOneLiveRange - Return true if the range specified is entirely in the
     /// a single LiveRange of the live interval.
@@ -418,6 +538,10 @@ namespace llvm {
     /// Also remove the value# from value# list.
     void removeValNo(VNInfo *ValNo);
 
+    /// scaleNumbering - Renumber VNI and ranges to provide gaps for new
+    /// instructions.
+    void scaleNumbering(unsigned factor);
+
     /// getSize - Returns the sum of sizes of all the LiveRange's.
     ///
     unsigned getSize() const;