gem5 v24.0.0.0
Loading...
Searching...
No Matches
2bit_local.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022-2023 The University of Edinburgh
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
42
43#include "base/intmath.hh"
44#include "base/logging.hh"
45#include "base/trace.hh"
46#include "debug/Fetch.hh"
47
48namespace gem5
49{
50
51namespace branch_prediction
52{
53
54LocalBP::LocalBP(const LocalBPParams &params)
55 : BPredUnit(params),
56 localPredictorSize(params.localPredictorSize),
57 localCtrBits(params.localCtrBits),
58 localPredictorSets(localPredictorSize / localCtrBits),
59 localCtrs(localPredictorSets, SatCounter8(localCtrBits)),
60 indexMask(localPredictorSets - 1)
61{
63 fatal("Invalid local predictor size!\n");
64 }
65
67 fatal("Invalid number of local predictor sets! Check localCtrBits.\n");
68 }
69
70 DPRINTF(Fetch, "index mask: %#x\n", indexMask);
71
72 DPRINTF(Fetch, "local predictor size: %i\n",
74
75 DPRINTF(Fetch, "local counter bits: %i\n", localCtrBits);
76
77 DPRINTF(Fetch, "instruction shift amount: %i\n",
79}
80
81void
83 bool taken, Addr target, void * &bp_history)
84{
85// Place holder for a function that is called to update predictor history
86}
87
88
89bool
90LocalBP::lookup(ThreadID tid, Addr branch_addr, void * &bp_history)
91{
92 bool taken;
93 unsigned local_predictor_idx = getLocalIndex(branch_addr);
94
95 DPRINTF(Fetch, "Looking up index %#x\n",
96 local_predictor_idx);
97
98 uint8_t counter_val = localCtrs[local_predictor_idx];
99
100 DPRINTF(Fetch, "prediction is %i.\n",
101 (int)counter_val);
102
103 taken = getPrediction(counter_val);
104
105 return taken;
106}
107
108void
109LocalBP::update(ThreadID tid, Addr branch_addr, bool taken, void *&bp_history,
110 bool squashed, const StaticInstPtr & inst, Addr target)
111{
112 assert(bp_history == NULL);
113 unsigned local_predictor_idx;
114
115 // No state to restore, and we do not update on the wrong
116 // path.
117 if (squashed) {
118 return;
119 }
120
121 // Update the local predictor.
122 local_predictor_idx = getLocalIndex(branch_addr);
123
124 DPRINTF(Fetch, "Looking up index %#x\n", local_predictor_idx);
125
126 if (taken) {
127 DPRINTF(Fetch, "Branch updated as taken.\n");
128 localCtrs[local_predictor_idx]++;
129 } else {
130 DPRINTF(Fetch, "Branch updated as not taken.\n");
131 localCtrs[local_predictor_idx]--;
132 }
133}
134
135inline
136bool
138{
139 // Get the MSB of the count
140 return (count >> (localCtrBits - 1));
141}
142
143inline
144unsigned
146{
147 return (branch_addr >> instShiftAmt) & indexMask;
148}
149
150
151} // namespace branch_prediction
152} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
Basically a wrapper class to hold both the branch predictor and the BTB.
Definition bpred_unit.hh:71
const unsigned instShiftAmt
Number of bits to shift instructions by for predictor addresses.
const unsigned localPredictorSize
Size of the local predictor.
Definition 2bit_local.hh:99
bool getPrediction(uint8_t &count)
Returns the taken/not taken prediction given the value of the counter.
const unsigned localCtrBits
Number of bits of the local predictor's counters.
const unsigned localPredictorSets
Number of sets.
void update(ThreadID tid, Addr pc, bool taken, void *&bp_history, bool squashed, const StaticInstPtr &inst, Addr target) override
Updates the BP with taken/not taken information.
LocalBP(const LocalBPParams &params)
Default branch predictor constructor.
Definition 2bit_local.cc:54
void updateHistories(ThreadID tid, Addr pc, bool uncond, bool taken, Addr target, void *&bp_history) override
Ones done with the prediction this function updates the path and global history.
Definition 2bit_local.cc:82
bool lookup(ThreadID tid, Addr pc, void *&bp_history) override
Looks up a given conditional branch PC of in the BP to see if it is taken or not taken.
Definition 2bit_local.cc:90
const unsigned indexMask
Mask to get index bits.
std::vector< SatCounter8 > localCtrs
Array of counters that make up the local predictor.
unsigned getLocalIndex(Addr &PC)
Calculates the local index based on the PC.
static constexpr bool isPowerOf2(const T &n)
Definition intmath.hh:98
#define fatal(...)
This implements a cprintf based fatal() function.
Definition logging.hh:200
Bitfield< 4 > pc
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
int16_t ThreadID
Thread index/ID type.
Definition types.hh:235
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147

Generated on Tue Jun 18 2024 16:24:02 for gem5 by doxygen 1.11.0