mem-cache: Use SatCounter for RRPV

Use SatCounter in RRIP's RRPV. As such, move validation functionality
to a proper variable.

Change-Id: I142db2b7f6cd518ac3a2b68c9ed48005402b3464
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/20452
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/src/mem/cache/replacement_policies/ReplacementPolicies.py b/src/mem/cache/replacement_policies/ReplacementPolicies.py
index f40d145..8a67dea 100644
--- a/src/mem/cache/replacement_policies/ReplacementPolicies.py
+++ b/src/mem/cache/replacement_policies/ReplacementPolicies.py
@@ -78,7 +78,7 @@
     type = 'BRRIPRP'
     cxx_class = 'BRRIPRP'
     cxx_header = "mem/cache/replacement_policies/brrip_rp.hh"
-    max_RRPV = Param.Int(3, "Maximum RRPV possible")
+    num_bits = Param.Int(2, "Number of bits per RRPV")
     hit_priority = Param.Bool(False,
         "Prioritize evicting blocks that havent had a hit recently")
     btp = Param.Percent(3,
@@ -89,7 +89,7 @@
 
 class NRURP(BRRIPRP):
     btp = 100
-    max_RRPV = 1
+    num_bits = 1
 
 class TreePLRURP(BaseReplacementPolicy):
     type = 'TreePLRURP'
diff --git a/src/mem/cache/replacement_policies/brrip_rp.cc b/src/mem/cache/replacement_policies/brrip_rp.cc
index dc41d8b..1e85367 100644
--- a/src/mem/cache/replacement_policies/brrip_rp.cc
+++ b/src/mem/cache/replacement_policies/brrip_rp.cc
@@ -39,9 +39,9 @@
 
 BRRIPRP::BRRIPRP(const Params *p)
     : BaseReplacementPolicy(p),
-      maxRRPV(p->max_RRPV), hitPriority(p->hit_priority), btp(p->btp)
+      numRRPVBits(p->num_bits), hitPriority(p->hit_priority), btp(p->btp)
 {
-    fatal_if(maxRRPV <= 0, "max_RRPV should be greater than zero.\n");
+    fatal_if(numRRPVBits <= 0, "There should be at least one bit per RRPV.\n");
 }
 
 void
@@ -51,8 +51,8 @@
     std::shared_ptr<BRRIPReplData> casted_replacement_data =
         std::static_pointer_cast<BRRIPReplData>(replacement_data);
 
-    // Set RRPV to an invalid distance
-    casted_replacement_data->rrpv = maxRRPV + 1;
+    // Invalidate entry
+    casted_replacement_data->valid = false;
 }
 
 void
@@ -65,8 +65,8 @@
     // Every hit in HP mode makes the entry the last to be evicted, while
     // in FP mode a hit makes the entry less likely to be evicted
     if (hitPriority) {
-        casted_replacement_data->rrpv = 0;
-    } else if (casted_replacement_data->rrpv > 0) {
+        casted_replacement_data->rrpv.reset();
+    } else {
         casted_replacement_data->rrpv--;
     }
 }
@@ -80,11 +80,13 @@
     // Reset RRPV
     // Replacement data is inserted as "long re-reference" if lower than btp,
     // "distant re-reference" otherwise
+    casted_replacement_data->rrpv.saturate();
     if (random_mt.random<unsigned>(1, 100) <= btp) {
-        casted_replacement_data->rrpv = maxRRPV-1;
-    } else {
-        casted_replacement_data->rrpv = maxRRPV;
+        casted_replacement_data->rrpv--;
     }
+
+    // Mark entry as ready to be used
+    casted_replacement_data->valid = true;
 }
 
 ReplaceableEntry*
@@ -102,15 +104,18 @@
 
     // Visit all candidates to find victim
     for (const auto& candidate : candidates) {
-        // Get candidate's rrpv
-        int candidate_RRPV = std::static_pointer_cast<BRRIPReplData>(
-                                    candidate->replacementData)->rrpv;
+        std::shared_ptr<BRRIPReplData> candidate_repl_data =
+            std::static_pointer_cast<BRRIPReplData>(
+                candidate->replacementData);
 
         // Stop searching for victims if an invalid entry is found
-        if (candidate_RRPV == maxRRPV + 1) {
+        if (!candidate_repl_data->valid) {
             return candidate;
+        }
+
         // Update victim entry if necessary
-        } else if (candidate_RRPV > victim_RRPV) {
+        int candidate_RRPV = candidate_repl_data->rrpv;
+        if (candidate_RRPV > victim_RRPV) {
             victim = candidate;
             victim_RRPV = candidate_RRPV;
         }
@@ -118,7 +123,8 @@
 
     // Get difference of victim's RRPV to the highest possible RRPV in
     // order to update the RRPV of all the other entries accordingly
-    int diff = maxRRPV - victim_RRPV;
+    int diff = std::static_pointer_cast<BRRIPReplData>(
+        victim->replacementData)->rrpv.saturate();
 
     // No need to update RRPV if there is no difference
     if (diff > 0){
@@ -135,7 +141,7 @@
 std::shared_ptr<ReplacementData>
 BRRIPRP::instantiateEntry()
 {
-    return std::shared_ptr<ReplacementData>(new BRRIPReplData(maxRRPV));
+    return std::shared_ptr<ReplacementData>(new BRRIPReplData(numRRPVBits));
 }
 
 BRRIPRP*
diff --git a/src/mem/cache/replacement_policies/brrip_rp.hh b/src/mem/cache/replacement_policies/brrip_rp.hh
index d374664..1c19ddc 100644
--- a/src/mem/cache/replacement_policies/brrip_rp.hh
+++ b/src/mem/cache/replacement_policies/brrip_rp.hh
@@ -54,6 +54,7 @@
 #ifndef __MEM_CACHE_REPLACEMENT_POLICIES_BRRIP_RP_HH__
 #define __MEM_CACHE_REPLACEMENT_POLICIES_BRRIP_RP_HH__
 
+#include "base/sat_counter.hh"
 #include "mem/cache/replacement_policies/base.hh"
 
 struct BRRIPRPParams;
@@ -70,24 +71,28 @@
          * 0 -> near-immediate re-rereference interval
          * max_RRPV-1 -> long re-rereference interval
          * max_RRPV -> distant re-rereference interval
-         * A value equal to max_RRPV + 1 indicates an invalid entry.
          */
-        int rrpv;
+        SatCounter rrpv;
+
+        /** Whether the entry is valid. */
+        bool valid;
 
         /**
          * Default constructor. Invalidate data.
          */
-        BRRIPReplData(const int max_RRPV) : rrpv(max_RRPV + 1) {}
+        BRRIPReplData(const int num_bits)
+            : rrpv(num_bits), valid(false)
+        {
+        }
     };
 
     /**
-     * Maximum Re-Reference Prediction Value possible. An entry with this
-     * value as the rrpv has the longest possible re-reference interval,
-     * that is, it is likely not to be used in the near future, and is
-     * among the best eviction candidates.
-     * A maxRRPV of 1 implies in a NRU.
+     * Number of RRPV bits. An entry that saturates its RRPV has the longest
+     * possible re-reference interval, that is, it is likely not to be used
+     * in the near future, and is among the best eviction candidates.
+     * A maximum RRPV of 1 implies in a NRU.
      */
-    const int maxRRPV;
+    const unsigned numRRPVBits;
 
     /**
      * The hit priority (HP) policy replaces entries that do not receive cache