Fix SCons version check.
[gem5.git] / src / cpu / o3 / 2bit_local_pred.cc
1 /*
2 * Copyright (c) 2004-2005 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
29 #include "base/trace.hh"
30 #include "cpu/o3/2bit_local_pred.hh"
31
32 DefaultBP::DefaultBP(unsigned _localPredictorSize,
33 unsigned _localCtrBits,
34 unsigned _instShiftAmt)
35 : localPredictorSize(_localPredictorSize),
36 localCtrBits(_localCtrBits),
37 instShiftAmt(_instShiftAmt)
38 {
39 // Should do checks here to make sure sizes are correct (powers of 2).
40
41 // Setup the index mask.
42 indexMask = localPredictorSize - 1;
43
44 DPRINTF(Fetch, "Branch predictor: index mask: %#x\n", indexMask);
45
46 // Setup the array of counters for the local predictor.
47 localCtrs = new SatCounter[localPredictorSize];
48
49 for (int i = 0; i < localPredictorSize; ++i)
50 localCtrs[i].setBits(_localCtrBits);
51
52 DPRINTF(Fetch, "Branch predictor: local predictor size: %i\n",
53 localPredictorSize);
54
55 DPRINTF(Fetch, "Branch predictor: local counter bits: %i\n", localCtrBits);
56
57 DPRINTF(Fetch, "Branch predictor: instruction shift amount: %i\n",
58 instShiftAmt);
59 }
60
61 bool
62 DefaultBP::lookup(Addr &branch_addr)
63 {
64 bool taken;
65 uint8_t local_prediction;
66 unsigned local_predictor_idx = getLocalIndex(branch_addr);
67
68 DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n",
69 local_predictor_idx);
70
71 assert(local_predictor_idx < localPredictorSize);
72
73 local_prediction = localCtrs[local_predictor_idx].read();
74
75 DPRINTF(Fetch, "Branch predictor: prediction is %i.\n",
76 (int)local_prediction);
77
78 taken = getPrediction(local_prediction);
79
80 #if 0
81 // Speculative update.
82 if (taken) {
83 DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n");
84 localCtrs[local_predictor_idx].increment();
85 } else {
86 DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n");
87 localCtrs[local_predictor_idx].decrement();
88 }
89 #endif
90
91 return taken;
92 }
93
94 void
95 DefaultBP::update(Addr &branch_addr, bool taken)
96 {
97 unsigned local_predictor_idx;
98
99 // Update the local predictor.
100 local_predictor_idx = getLocalIndex(branch_addr);
101
102 DPRINTF(Fetch, "Branch predictor: Looking up index %#x\n",
103 local_predictor_idx);
104
105 assert(local_predictor_idx < localPredictorSize);
106
107 if (taken) {
108 DPRINTF(Fetch, "Branch predictor: Branch updated as taken.\n");
109 localCtrs[local_predictor_idx].increment();
110 } else {
111 DPRINTF(Fetch, "Branch predictor: Branch updated as not taken.\n");
112 localCtrs[local_predictor_idx].decrement();
113 }
114 }
115
116 inline
117 bool
118 DefaultBP::getPrediction(uint8_t &count)
119 {
120 // Get the MSB of the count
121 return (count >> (localCtrBits - 1));
122 }
123
124 inline
125 unsigned
126 DefaultBP::getLocalIndex(Addr &branch_addr)
127 {
128 return (branch_addr >> instShiftAmt) & indexMask;
129 }