2bit_local.cc (6227:a17798f2a52c) 2bit_local.cc (8232:b28d06a175be)
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"
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"
35
36LocalBP::LocalBP(unsigned _localPredictorSize,
37 unsigned _localCtrBits,
38 unsigned _instShiftAmt)
39 : localPredictorSize(_localPredictorSize),
40 localCtrBits(_localCtrBits),
41 instShiftAmt(_instShiftAmt)
42{
43 if (!isPowerOf2(localPredictorSize)) {
44 fatal("Invalid local predictor size!\n");
45 }
46
47 localPredictorSets = localPredictorSize / localCtrBits;
48
49 if (!isPowerOf2(localPredictorSets)) {
50 fatal("Invalid number of local predictor sets! Check localCtrBits.\n");
51 }
52
53 // Setup the index mask.
54 indexMask = localPredictorSets - 1;
55
56 DPRINTF(Fetch, "Branch predictor: index mask: %#x\n", indexMask);
57
58 // Setup the array of counters for the local predictor.
59 localCtrs.resize(localPredictorSets);
60
61 for (unsigned i = 0; i < localPredictorSets; ++i)
62 localCtrs[i].setBits(_localCtrBits);
63
64 DPRINTF(Fetch, "Branch predictor: local predictor size: %i\n",
65 localPredictorSize);
66
67 DPRINTF(Fetch, "Branch predictor: local counter bits: %i\n", localCtrBits);
68
69 DPRINTF(Fetch, "Branch predictor: instruction shift amount: %i\n",
70 instShiftAmt);
71}
72
73void
74LocalBP::reset()
75{
76 for (unsigned i = 0; i < localPredictorSets; ++i) {
77 localCtrs[i].reset();
78 }
79}
80
81bool
82LocalBP::lookup(Addr &branch_addr, void * &bp_history)
83{
84 bool taken;
85 uint8_t counter_val;
86 unsigned local_predictor_idx = getLocalIndex(branch_addr);
87
88 DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n",
89 local_predictor_idx);
90
91 counter_val = localCtrs[local_predictor_idx].read();
92
93 DPRINTF(Fetch, "Branch predictor: prediction is %i.\n",
94 (int)counter_val);
95
96 taken = getPrediction(counter_val);
97
98#if 0
99 // Speculative update.
100 if (taken) {
101 DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n");
102 localCtrs[local_predictor_idx].increment();
103 } else {
104 DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n");
105 localCtrs[local_predictor_idx].decrement();
106 }
107#endif
108
109 return taken;
110}
111
112void
113LocalBP::update(Addr &branch_addr, bool taken, void *bp_history)
114{
115 assert(bp_history == NULL);
116 unsigned local_predictor_idx;
117
118 // Update the local predictor.
119 local_predictor_idx = getLocalIndex(branch_addr);
120
121 DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n",
122 local_predictor_idx);
123
124 if (taken) {
125 DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n");
126 localCtrs[local_predictor_idx].increment();
127 } else {
128 DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n");
129 localCtrs[local_predictor_idx].decrement();
130 }
131}
132
133inline
134bool
135LocalBP::getPrediction(uint8_t &count)
136{
137 // Get the MSB of the count
138 return (count >> (localCtrBits - 1));
139}
140
141inline
142unsigned
143LocalBP::getLocalIndex(Addr &branch_addr)
144{
145 return (branch_addr >> instShiftAmt) & indexMask;
146}
36
37LocalBP::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
74void
75LocalBP::reset()
76{
77 for (unsigned i = 0; i < localPredictorSets; ++i) {
78 localCtrs[i].reset();
79 }
80}
81
82bool
83LocalBP::lookup(Addr &branch_addr, void * &bp_history)
84{
85 bool taken;
86 uint8_t counter_val;
87 unsigned local_predictor_idx = getLocalIndex(branch_addr);
88
89 DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n",
90 local_predictor_idx);
91
92 counter_val = localCtrs[local_predictor_idx].read();
93
94 DPRINTF(Fetch, "Branch predictor: prediction is %i.\n",
95 (int)counter_val);
96
97 taken = getPrediction(counter_val);
98
99#if 0
100 // Speculative update.
101 if (taken) {
102 DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n");
103 localCtrs[local_predictor_idx].increment();
104 } else {
105 DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n");
106 localCtrs[local_predictor_idx].decrement();
107 }
108#endif
109
110 return taken;
111}
112
113void
114LocalBP::update(Addr &branch_addr, bool taken, void *bp_history)
115{
116 assert(bp_history == NULL);
117 unsigned local_predictor_idx;
118
119 // Update the local predictor.
120 local_predictor_idx = getLocalIndex(branch_addr);
121
122 DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n",
123 local_predictor_idx);
124
125 if (taken) {
126 DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n");
127 localCtrs[local_predictor_idx].increment();
128 } else {
129 DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n");
130 localCtrs[local_predictor_idx].decrement();
131 }
132}
133
134inline
135bool
136LocalBP::getPrediction(uint8_t &count)
137{
138 // Get the MSB of the count
139 return (count >> (localCtrBits - 1));
140}
141
142inline
143unsigned
144LocalBP::getLocalIndex(Addr &branch_addr)
145{
146 return (branch_addr >> instShiftAmt) & indexMask;
147}