gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
gshare.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 REDS-HEIG-VD and ESL-EPFL
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30/* @file
31 * Implementation of a bi-mode branch predictor
32 */
33
34#include "cpu/pred/gshare.hh"
35
36#include "base/bitfield.hh"
37#include "base/intmath.hh"
38
39namespace gem5
40{
41
42namespace branch_prediction
43{
44
45GshareBP::GshareBP(const GshareBPParams &params)
48 globalHistoryBits(ceilLog2(params.global_predictor_size)),
49 globalPredictorSize(params.global_predictor_size),
50 globalCtrBits(params.global_counter_bits),
52{
53
55 fatal("Invalid global history predictor size.\n");
56 }
59 takenThreshold = (1ULL << (globalCtrBits - 1)) - 1;
60}
61
62/*
63 * For an unconditional branch we set its history such that
64 * everything is set to taken. I.e., its choice predictor
65 * chooses the taken array and the taken array predicts taken.
66 */
67void
68GshareBP::uncondBranch(ThreadID tid, Addr pc, void *&bp_history)
69{
70 BPHistory *history = new BPHistory;
71 history->globalHistoryReg = globalHistoryReg[tid];
72 history->finalPred = true;
73 bp_history = static_cast<void *>(history);
74}
75
76void
77GshareBP::updateHistories(ThreadID tid, Addr pc, bool uncond, bool taken,
78 Addr target, const StaticInstPtr &inst,
79 void *&bp_history)
80{
81 assert(uncond || bp_history);
82 if (uncond) {
83 uncondBranch(tid, pc, bp_history);
84 }
85 updateGlobalHistReg(tid, taken);
86}
87
88void
89GshareBP::squash(ThreadID tid, void *&bp_history)
90{
91 BPHistory *history = static_cast<BPHistory *>(bp_history);
92 globalHistoryReg[tid] = history->globalHistoryReg;
93
94 delete history;
95 bp_history = nullptr;
96}
97
98/*
99 * Here we lookup the actual branch prediction. A hash of
100 * the global history register and a branch's PC is used to
101 * index into the counter, which both present a prediction.
102 */
103bool
104GshareBP::lookup(ThreadID tid, Addr branchAddr, void *&bp_history)
105{
106 unsigned globalHistoryIdx =
107 (((branchAddr >> instShiftAmt) ^ globalHistoryReg[tid]) &
109
110 assert(globalHistoryIdx < globalPredictorSize);
111
112 bool final_prediction = globalCtrs[globalHistoryIdx] > takenThreshold;
113
114 BPHistory *history = new BPHistory;
115 history->globalHistoryReg = globalHistoryReg[tid];
116 history->finalPred = final_prediction;
117 bp_history = static_cast<void *>(history);
118
119 return final_prediction;
120}
121
122/* Updates the counter values based on the actual branch
123 * direction.
124 */
125void
126GshareBP::update(ThreadID tid, Addr branchAddr, bool taken, void *&bp_history,
127 bool squashed, const StaticInstPtr &inst, Addr target)
128{
129 assert(bp_history);
130
131 BPHistory *history = static_cast<BPHistory *>(bp_history);
132
133 // We do not update the counters speculatively on a squash.
134 // We just restore the global history register.
135 if (squashed) {
136 globalHistoryReg[tid] = (history->globalHistoryReg << 1) | taken;
137 return;
138 }
139
140 unsigned globalHistoryIdx =
141 (((branchAddr >> instShiftAmt) ^ history->globalHistoryReg) &
143
144 assert(globalHistoryIdx < globalPredictorSize);
145
146 if (taken) {
147 globalCtrs[globalHistoryIdx]++;
148 } else {
149 globalCtrs[globalHistoryIdx]--;
150 }
151 delete history;
152 bp_history = nullptr;
153}
154
155void
157{
158 globalHistoryReg[tid] = taken ? (globalHistoryReg[tid] << 1) | 1
159 : (globalHistoryReg[tid] << 1);
161}
162
163} // namespace branch_prediction
164} // namespace gem5
BPredUnit(const Params &p)
Branch Predictor Unit (BPU) interface functions.
Definition bpred_unit.cc:58
const unsigned numThreads
Number of the threads for which the branch history is maintained.
const unsigned instShiftAmt
Number of bits to shift instructions by for predictor addresses.
std::vector< unsigned > globalHistoryReg
Definition gshare.hh:77
void uncondBranch(ThreadID tid, Addr pc, void *&bp_history)
Definition gshare.cc:68
void updateHistories(ThreadID tid, Addr pc, bool uncond, bool taken, Addr target, const StaticInstPtr &inst, void *&bp_history)
Definition gshare.cc:77
GshareBP(const GshareBPParams &params)
Definition gshare.cc:45
bool lookup(ThreadID tid, Addr pc, void *&bp_history)
Definition gshare.cc:104
void update(ThreadID tid, Addr pc, bool taken, void *&bp_history, bool squashed, const StaticInstPtr &inst, Addr target)
Definition gshare.cc:126
void squash(ThreadID tid, void *&bp_history)
Definition gshare.cc:89
std::vector< SatCounter8 > globalCtrs
Definition gshare.hh:85
void updateGlobalHistReg(ThreadID tid, bool taken)
Definition gshare.cc:156
static constexpr int ceilLog2(const T &n)
Definition intmath.hh:84
static constexpr bool isPowerOf2(const T &n)
Definition intmath.hh:98
#define fatal(...)
This implements a cprintf based fatal() function.
Definition logging.hh:232
GenericSatCounter< uint8_t > SatCounter8
const Params & params() const
Bitfield< 3, 0 > mask
Definition pcstate.hh:63
Bitfield< 4 > pc
Copyright (c) 2024 Arm Limited 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
RefCountingPtr< StaticInst > StaticInstPtr

Generated on Mon Oct 27 2025 04:13:01 for gem5 by doxygen 1.14.0