Deleted Added
sdiff udiff text old ( 11783:f94c14fd6561 ) new ( 11793:ef606668d247 )
full compact
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
37LocalBP::LocalBP(const LocalBPParams *params)
38 : BPredUnit(params),
39 localPredictorSize(params->localPredictorSize),
40 localCtrBits(params->localCtrBits)
41{
42 if (!isPowerOf2(localPredictorSize)) {
43 fatal("Invalid local predictor size!\n");
44 }
45
46 localPredictorSets = localPredictorSize / localCtrBits;
47
48 if (!isPowerOf2(localPredictorSets)) {
49 fatal("Invalid number of local predictor sets! Check localCtrBits.\n");
50 }
51
52 // Setup the index mask.
53 indexMask = localPredictorSets - 1;
54
55 DPRINTF(Fetch, "index mask: %#x\n", indexMask);
56
57 // Setup the array of counters for the local predictor.
58 localCtrs.resize(localPredictorSets);
59
60 for (unsigned i = 0; i < localPredictorSets; ++i)
61 localCtrs[i].setBits(localCtrBits);
62
63 DPRINTF(Fetch, "local predictor size: %i\n",
64 localPredictorSize);
65
66 DPRINTF(Fetch, "local counter bits: %i\n", localCtrBits);
67
68 DPRINTF(Fetch, "instruction shift amount: %i\n",
69 instShiftAmt);
70}
71
72void
73LocalBP::reset()
74{
75 for (unsigned i = 0; i < localPredictorSets; ++i) {
76 localCtrs[i].reset();
77 }
78}
79
80void
81LocalBP::btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history)
82{
83// Place holder for a function that is called to update predictor history when
84// a BTB entry is invalid or not found.
85}
86
87
88bool
89LocalBP::lookup(ThreadID tid, Addr branch_addr, void * &bp_history)
90{
91 bool taken;
92 uint8_t counter_val;
93 unsigned local_predictor_idx = getLocalIndex(branch_addr);
94
95 DPRINTF(Fetch, "Looking up index %#x\n",
96 local_predictor_idx);
97
98 counter_val = localCtrs[local_predictor_idx].read();
99
100 DPRINTF(Fetch, "prediction is %i.\n",
101 (int)counter_val);
102
103 taken = getPrediction(counter_val);
104
105#if 0
106 // Speculative update.
107 if (taken) {
108 DPRINTF(Fetch, "Branch updated as taken.\n");
109 localCtrs[local_predictor_idx].increment();
110 } else {
111 DPRINTF(Fetch, "Branch updated as not taken.\n");
112 localCtrs[local_predictor_idx].decrement();
113 }
114#endif
115
116 return taken;
117}
118
119void
120LocalBP::update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history,
121 bool squashed)
122{
123 assert(bp_history == NULL);
124 unsigned local_predictor_idx;
125
126 // No state to restore, and we do not update on the wrong
127 // path.
128 if (squashed) {
129 return;
130 }
131
132 // Update the local predictor.
133 local_predictor_idx = getLocalIndex(branch_addr);
134
135 DPRINTF(Fetch, "Looking up index %#x\n", local_predictor_idx);
136
137 if (taken) {
138 DPRINTF(Fetch, "Branch updated as taken.\n");
139 localCtrs[local_predictor_idx].increment();
140 } else {
141 DPRINTF(Fetch, "Branch updated as not taken.\n");
142 localCtrs[local_predictor_idx].decrement();
143 }
144}
145
146inline
147bool
148LocalBP::getPrediction(uint8_t &count)
149{
150 // Get the MSB of the count
151 return (count >> (localCtrBits - 1));
152}
153
154inline
155unsigned
156LocalBP::getLocalIndex(Addr &branch_addr)
157{
158 return (branch_addr >> instShiftAmt) & indexMask;
159}
160
161void
162LocalBP::uncondBranch(ThreadID tid, Addr pc, void *&bp_history)
163{
164}
165
166LocalBP*
167LocalBPParams::create()
168{
169 return new LocalBP(this);
170}