Merge with head.
[gem5.git] / src / cpu / o3 / btb.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 * Authors: Kevin Lim
29 */
30
31 #include "base/intmath.hh"
32 #include "base/trace.hh"
33 #include "cpu/o3/btb.hh"
34
35 DefaultBTB::DefaultBTB(unsigned _numEntries,
36 unsigned _tagBits,
37 unsigned _instShiftAmt)
38 : numEntries(_numEntries),
39 tagBits(_tagBits),
40 instShiftAmt(_instShiftAmt)
41 {
42 DPRINTF(Fetch, "BTB: Creating BTB object.\n");
43
44 if (!isPowerOf2(numEntries)) {
45 fatal("BTB entries is not a power of 2!");
46 }
47
48 btb.resize(numEntries);
49
50 for (int i = 0; i < numEntries; ++i) {
51 btb[i].valid = false;
52 }
53
54 idxMask = numEntries - 1;
55
56 tagMask = (1 << tagBits) - 1;
57
58 tagShiftAmt = instShiftAmt + floorLog2(numEntries);
59 }
60
61 void
62 DefaultBTB::reset()
63 {
64 for (int i = 0; i < numEntries; ++i) {
65 btb[i].valid = false;
66 }
67 }
68
69 inline
70 unsigned
71 DefaultBTB::getIndex(const Addr &inst_PC)
72 {
73 // Need to shift PC over by the word offset.
74 return (inst_PC >> instShiftAmt) & idxMask;
75 }
76
77 inline
78 Addr
79 DefaultBTB::getTag(const Addr &inst_PC)
80 {
81 return (inst_PC >> tagShiftAmt) & tagMask;
82 }
83
84 bool
85 DefaultBTB::valid(const Addr &inst_PC, unsigned tid)
86 {
87 unsigned btb_idx = getIndex(inst_PC);
88
89 Addr inst_tag = getTag(inst_PC);
90
91 assert(btb_idx < numEntries);
92
93 if (btb[btb_idx].valid
94 && inst_tag == btb[btb_idx].tag
95 && btb[btb_idx].tid == tid) {
96 return true;
97 } else {
98 return false;
99 }
100 }
101
102 // @todo Create some sort of return struct that has both whether or not the
103 // address is valid, and also the address. For now will just use addr = 0 to
104 // represent invalid entry.
105 Addr
106 DefaultBTB::lookup(const Addr &inst_PC, unsigned tid)
107 {
108 unsigned btb_idx = getIndex(inst_PC);
109
110 Addr inst_tag = getTag(inst_PC);
111
112 assert(btb_idx < numEntries);
113
114 if (btb[btb_idx].valid
115 && inst_tag == btb[btb_idx].tag
116 && btb[btb_idx].tid == tid) {
117 return btb[btb_idx].target;
118 } else {
119 return 0;
120 }
121 }
122
123 void
124 DefaultBTB::update(const Addr &inst_PC, const Addr &target, unsigned tid)
125 {
126 unsigned btb_idx = getIndex(inst_PC);
127
128 assert(btb_idx < numEntries);
129
130 btb[btb_idx].tid = tid;
131 btb[btb_idx].valid = true;
132 btb[btb_idx].target = target;
133 btb[btb_idx].tag = getTag(inst_PC);
134 }