BP: Fix several Branch Predictor issues.
[gem5.git] / src / cpu / pred / 2bit_local.cc
1 /*
2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
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 * Authors: Kevin Lim
29 */
30
31 #include "base/intmath.hh"
32 #include "base/misc.hh"
33 #include "base/trace.hh"
34 #include "cpu/pred/2bit_local.hh"
35 #include "debug/Fetch.hh"
36
37 LocalBP::LocalBP(unsigned _localPredictorSize,
38 unsigned _localCtrBits,
39 unsigned _instShiftAmt)
40 : localPredictorSize(_localPredictorSize),
41 localCtrBits(_localCtrBits),
42 instShiftAmt(_instShiftAmt)
43 {
44 if (!isPowerOf2(localPredictorSize)) {
45 fatal("Invalid local predictor size!\n");
46 }
47
48 localPredictorSets = localPredictorSize / localCtrBits;
49
50 if (!isPowerOf2(localPredictorSets)) {
51 fatal("Invalid number of local predictor sets! Check localCtrBits.\n");
52 }
53
54 // Setup the index mask.
55 indexMask = localPredictorSets - 1;
56
57 DPRINTF(Fetch, "Branch predictor: index mask: %#x\n", indexMask);
58
59 // Setup the array of counters for the local predictor.
60 localCtrs.resize(localPredictorSets);
61
62 for (unsigned i = 0; i < localPredictorSets; ++i)
63 localCtrs[i].setBits(_localCtrBits);
64
65 DPRINTF(Fetch, "Branch predictor: local predictor size: %i\n",
66 localPredictorSize);
67
68 DPRINTF(Fetch, "Branch predictor: local counter bits: %i\n", localCtrBits);
69
70 DPRINTF(Fetch, "Branch predictor: instruction shift amount: %i\n",
71 instShiftAmt);
72 }
73
74 void
75 LocalBP::reset()
76 {
77 for (unsigned i = 0; i < localPredictorSets; ++i) {
78 localCtrs[i].reset();
79 }
80 }
81
82 void
83 LocalBP::BTBUpdate(Addr &branch_addr, void * &bp_history)
84 {
85 // Place holder for a function that is called to update predictor history when
86 // a BTB entry is invalid or not found.
87 }
88
89
90 bool
91 LocalBP::lookup(Addr &branch_addr, void * &bp_history)
92 {
93 bool taken;
94 uint8_t counter_val;
95 unsigned local_predictor_idx = getLocalIndex(branch_addr);
96
97 DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n",
98 local_predictor_idx);
99
100 counter_val = localCtrs[local_predictor_idx].read();
101
102 DPRINTF(Fetch, "Branch predictor: prediction is %i.\n",
103 (int)counter_val);
104
105 taken = getPrediction(counter_val);
106
107 #if 0
108 // Speculative update.
109 if (taken) {
110 DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n");
111 localCtrs[local_predictor_idx].increment();
112 } else {
113 DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n");
114 localCtrs[local_predictor_idx].decrement();
115 }
116 #endif
117
118 return taken;
119 }
120
121 void
122 LocalBP::update(Addr &branch_addr, bool taken, void *bp_history)
123 {
124 assert(bp_history == NULL);
125 unsigned local_predictor_idx;
126
127 // Update the local predictor.
128 local_predictor_idx = getLocalIndex(branch_addr);
129
130 DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n",
131 local_predictor_idx);
132
133 if (taken) {
134 DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n");
135 localCtrs[local_predictor_idx].increment();
136 } else {
137 DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n");
138 localCtrs[local_predictor_idx].decrement();
139 }
140 }
141
142 inline
143 bool
144 LocalBP::getPrediction(uint8_t &count)
145 {
146 // Get the MSB of the count
147 return (count >> (localCtrBits - 1));
148 }
149
150 inline
151 unsigned
152 LocalBP::getLocalIndex(Addr &branch_addr)
153 {
154 return (branch_addr >> instShiftAmt) & indexMask;
155 }