[PBQP] Fix transposed worst row/column check in handleAdd/RemoveNode in the PBQP
[oota-llvm.git] / include / llvm / CodeGen / RegAllocPBQP.h
index 0d379ea8b226586a7ce059a250895533fc203db3..5c0e9859915f9a740a904e302b79392a86d44df6 100644 (file)
@@ -17,9 +17,9 @@
 #define LLVM_CODEGEN_REGALLOCPBQP_H
 
 #include "llvm/CodeGen/MachineFunctionPass.h"
-#include "llvm/CodeGen/PBQPRAConstraint.h"
 #include "llvm/CodeGen/PBQP/CostAllocator.h"
 #include "llvm/CodeGen/PBQP/ReductionRules.h"
+#include "llvm/CodeGen/PBQPRAConstraint.h"
 #include "llvm/Support/ErrorHandling.h"
 
 namespace llvm {
@@ -73,39 +73,171 @@ private:
   std::unique_ptr<bool[]> UnsafeCols;
 };
 
+/// \brief Holds a vector of the allowed physical regs for a vreg.
+class AllowedRegVector {
+  friend hash_code hash_value(const AllowedRegVector &);
+public:
+
+  AllowedRegVector() : NumOpts(0), Opts(nullptr) {}
+
+  AllowedRegVector(const std::vector<unsigned> &OptVec)
+    : NumOpts(OptVec.size()), Opts(new unsigned[NumOpts]) {
+    std::copy(OptVec.begin(), OptVec.end(), Opts.get());
+  }
+
+  AllowedRegVector(const AllowedRegVector &Other)
+    : NumOpts(Other.NumOpts), Opts(new unsigned[NumOpts]) {
+    std::copy(Other.Opts.get(), Other.Opts.get() + NumOpts, Opts.get());
+  }
+
+  AllowedRegVector(AllowedRegVector &&Other)
+    : NumOpts(std::move(Other.NumOpts)), Opts(std::move(Other.Opts)) {}
+
+  AllowedRegVector& operator=(const AllowedRegVector &Other) {
+    NumOpts = Other.NumOpts;
+    Opts.reset(new unsigned[NumOpts]);
+    std::copy(Other.Opts.get(), Other.Opts.get() + NumOpts, Opts.get());
+    return *this;
+  }
+
+  AllowedRegVector& operator=(AllowedRegVector &&Other) {
+    NumOpts = std::move(Other.NumOpts);
+    Opts = std::move(Other.Opts);
+    return *this;
+  }
+
+  unsigned size() const { return NumOpts; }
+  unsigned operator[](size_t I) const { return Opts[I]; }
+
+  bool operator==(const AllowedRegVector &Other) const {
+    if (NumOpts != Other.NumOpts)
+      return false;
+    return std::equal(Opts.get(), Opts.get() + NumOpts, Other.Opts.get());
+  }
+
+  bool operator!=(const AllowedRegVector &Other) const {
+    return !(*this == Other);
+  }
+
+private:
+  unsigned NumOpts;
+  std::unique_ptr<unsigned[]> Opts;
+};
+
+inline hash_code hash_value(const AllowedRegVector &OptRegs) {
+  unsigned *OStart = OptRegs.Opts.get();
+  unsigned *OEnd = OptRegs.Opts.get() + OptRegs.NumOpts;
+  return hash_combine(OptRegs.NumOpts,
+                      hash_combine_range(OStart, OEnd));
+}
+
+/// \brief Holds graph-level metadata relevent to PBQP RA problems.
+class GraphMetadata {
+private:
+  typedef ValuePool<AllowedRegVector> AllowedRegVecPool;
+public:
+
+  typedef AllowedRegVecPool::PoolRef AllowedRegVecRef;
+
+  GraphMetadata(MachineFunction &MF,
+                LiveIntervals &LIS,
+                MachineBlockFrequencyInfo &MBFI)
+    : MF(MF), LIS(LIS), MBFI(MBFI) {}
+
+  MachineFunction &MF;
+  LiveIntervals &LIS;
+  MachineBlockFrequencyInfo &MBFI;
+
+  void setNodeIdForVReg(unsigned VReg, GraphBase::NodeId NId) {
+    VRegToNodeId[VReg] = NId;
+  }
+
+  GraphBase::NodeId getNodeIdForVReg(unsigned VReg) const {
+    auto VRegItr = VRegToNodeId.find(VReg);
+    if (VRegItr == VRegToNodeId.end())
+      return GraphBase::invalidNodeId();
+    return VRegItr->second;
+  }
+
+  void eraseNodeIdForVReg(unsigned VReg) {
+    VRegToNodeId.erase(VReg);
+  }
+
+  AllowedRegVecRef getAllowedRegs(AllowedRegVector Allowed) {
+    return AllowedRegVecs.getValue(std::move(Allowed));
+  }
+
+private:
+  DenseMap<unsigned, GraphBase::NodeId> VRegToNodeId;
+  AllowedRegVecPool AllowedRegVecs;
+};
+
+/// \brief Holds solver state and other metadata relevant to each PBQP RA node.
 class NodeMetadata {
 public:
-  typedef std::vector<unsigned> OptionToRegMap;
+  typedef RegAlloc::AllowedRegVector AllowedRegVector;
 
   typedef enum { Unprocessed,
                  OptimallyReducible,
                  ConservativelyAllocatable,
                  NotProvablyAllocatable } ReductionState;
 
-  NodeMetadata() : RS(Unprocessed), DeniedOpts(0), OptUnsafeEdges(nullptr){}
+  NodeMetadata()
+    : RS(Unprocessed), NumOpts(0), DeniedOpts(0), OptUnsafeEdges(nullptr),
+      VReg(0) {}
+
+  // FIXME: Re-implementing default behavior to work around MSVC. Remove once
+  // MSVC synthesizes move constructors properly.
+  NodeMetadata(const NodeMetadata &Other)
+    : RS(Other.RS), NumOpts(Other.NumOpts), DeniedOpts(Other.DeniedOpts),
+      OptUnsafeEdges(new unsigned[NumOpts]), VReg(Other.VReg),
+      AllowedRegs(Other.AllowedRegs) {
+    if (NumOpts > 0) {
+      std::copy(&Other.OptUnsafeEdges[0], &Other.OptUnsafeEdges[NumOpts],
+                &OptUnsafeEdges[0]);
+    }
+  }
 
+  // FIXME: Re-implementing default behavior to work around MSVC. Remove once
+  // MSVC synthesizes move constructors properly.
   NodeMetadata(NodeMetadata &&Other)
     : RS(Other.RS), NumOpts(Other.NumOpts), DeniedOpts(Other.DeniedOpts),
       OptUnsafeEdges(std::move(Other.OptUnsafeEdges)), VReg(Other.VReg),
-      OptionRegs(std::move(Other.OptionRegs)) {}
+      AllowedRegs(std::move(Other.AllowedRegs)) {}
 
+  // FIXME: Re-implementing default behavior to work around MSVC. Remove once
+  // MSVC synthesizes move constructors properly.
+  NodeMetadata& operator=(const NodeMetadata &Other) {
+    RS = Other.RS;
+    NumOpts = Other.NumOpts;
+    DeniedOpts = Other.DeniedOpts;
+    OptUnsafeEdges.reset(new unsigned[NumOpts]);
+    std::copy(Other.OptUnsafeEdges.get(), Other.OptUnsafeEdges.get() + NumOpts,
+              OptUnsafeEdges.get());
+    VReg = Other.VReg;
+    AllowedRegs = Other.AllowedRegs;
+    return *this;
+  }
+
+  // FIXME: Re-implementing default behavior to work around MSVC. Remove once
+  // MSVC synthesizes move constructors properly.
   NodeMetadata& operator=(NodeMetadata &&Other) {
     RS = Other.RS;
     NumOpts = Other.NumOpts;
     DeniedOpts = Other.DeniedOpts;
     OptUnsafeEdges = std::move(Other.OptUnsafeEdges);
     VReg = Other.VReg;
-    OptionRegs = std::move(Other.OptionRegs);
+    AllowedRegs = std::move(Other.AllowedRegs);
     return *this;
   }
 
   void setVReg(unsigned VReg) { this->VReg = VReg; }
   unsigned getVReg() const { return VReg; }
 
-  void setOptionRegs(OptionToRegMap OptionRegs) {
-    this->OptionRegs = std::move(OptionRegs);
+  void setAllowedRegs(GraphMetadata::AllowedRegVecRef AllowedRegs) {
+    this->AllowedRegs = std::move(AllowedRegs);
   }
-  const OptionToRegMap& getOptionRegs() const { return OptionRegs; }
+  const AllowedRegVector& getAllowedRegs() const { return *AllowedRegs; }
 
   void setup(const Vector& Costs) {
     NumOpts = Costs.getLength() - 1;
@@ -116,7 +248,7 @@ public:
   void setReductionState(ReductionState RS) { this->RS = RS; }
 
   void handleAddEdge(const MatrixMetadata& MD, bool Transpose) {
-    DeniedOpts += Transpose ? MD.getWorstCol() : MD.getWorstRow();
+    DeniedOpts += Transpose ? MD.getWorstRow() : MD.getWorstCol();
     const bool* UnsafeOpts =
       Transpose ? MD.getUnsafeCols() : MD.getUnsafeRows();
     for (unsigned i = 0; i < NumOpts; ++i)
@@ -124,7 +256,7 @@ public:
   }
 
   void handleRemoveEdge(const MatrixMetadata& MD, bool Transpose) {
-    DeniedOpts -= Transpose ? MD.getWorstCol() : MD.getWorstRow();
+    DeniedOpts -= Transpose ? MD.getWorstRow() : MD.getWorstCol();
     const bool* UnsafeOpts =
       Transpose ? MD.getUnsafeCols() : MD.getUnsafeRows();
     for (unsigned i = 0; i < NumOpts; ++i)
@@ -138,15 +270,12 @@ public:
   }
 
 private:
-  NodeMetadata(const NodeMetadata&) LLVM_DELETED_FUNCTION;
-  void operator=(const NodeMetadata&) LLVM_DELETED_FUNCTION;
-
   ReductionState RS;
   unsigned NumOpts;
   unsigned DeniedOpts;
   std::unique_ptr<unsigned[]> OptUnsafeEdges;
   unsigned VReg;
-  OptionToRegMap OptionRegs;
+  GraphMetadata::AllowedRegVecRef AllowedRegs;
 };
 
 class RegAllocSolverImpl {
@@ -163,38 +292,8 @@ public:
   typedef GraphBase::EdgeId EdgeId;
 
   typedef RegAlloc::NodeMetadata NodeMetadata;
-
   struct EdgeMetadata { };
-
-  class GraphMetadata {
-  public:
-    GraphMetadata(MachineFunction &MF,
-                  LiveIntervals &LIS,
-                  MachineBlockFrequencyInfo &MBFI)
-      : MF(MF), LIS(LIS), MBFI(MBFI) {}
-
-    MachineFunction &MF;
-    LiveIntervals &LIS;
-    MachineBlockFrequencyInfo &MBFI;
-
-    void setNodeIdForVReg(unsigned VReg, GraphBase::NodeId NId) {
-      VRegToNodeId[VReg] = NId;
-    }
-
-    GraphBase::NodeId getNodeIdForVReg(unsigned VReg) const {
-      auto VRegItr = VRegToNodeId.find(VReg);
-      if (VRegItr == VRegToNodeId.end())
-        return GraphBase::invalidNodeId();
-      return VRegItr->second;
-    }
-
-    void eraseNodeIdForVReg(unsigned VReg) {
-      VRegToNodeId.erase(VReg);
-    }
-
-  private:
-    DenseMap<unsigned, NodeId> VRegToNodeId;
-  };
+  typedef RegAlloc::GraphMetadata GraphMetadata;
 
   typedef PBQP::Graph<RegAllocSolverImpl> Graph;