gem5  v20.1.0.0
btb.cc
Go to the documentation of this file.
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 "cpu/pred/btb.hh"
30 
31 #include "base/intmath.hh"
32 #include "base/trace.hh"
33 #include "debug/Fetch.hh"
34 
35 DefaultBTB::DefaultBTB(unsigned _numEntries,
36  unsigned _tagBits,
37  unsigned _instShiftAmt,
38  unsigned _num_threads)
39  : numEntries(_numEntries),
40  tagBits(_tagBits),
41  instShiftAmt(_instShiftAmt),
42  log2NumThreads(floorLog2(_num_threads))
43 {
44  DPRINTF(Fetch, "BTB: Creating BTB object.\n");
45 
46  if (!isPowerOf2(numEntries)) {
47  fatal("BTB entries is not a power of 2!");
48  }
49 
50  btb.resize(numEntries);
51 
52  for (unsigned i = 0; i < numEntries; ++i) {
53  btb[i].valid = false;
54  }
55 
56  idxMask = numEntries - 1;
57 
58  tagMask = (1 << tagBits) - 1;
59 
61 }
62 
63 void
65 {
66  for (unsigned i = 0; i < numEntries; ++i) {
67  btb[i].valid = false;
68  }
69 }
70 
71 inline
72 unsigned
74 {
75  // Need to shift PC over by the word offset.
76  return ((instPC >> instShiftAmt)
77  ^ (tid << (tagShiftAmt - instShiftAmt - log2NumThreads)))
78  & idxMask;
79 }
80 
81 inline
82 Addr
84 {
85  return (instPC >> tagShiftAmt) & tagMask;
86 }
87 
88 bool
90 {
91  unsigned btb_idx = getIndex(instPC, tid);
92 
93  Addr inst_tag = getTag(instPC);
94 
95  assert(btb_idx < numEntries);
96 
97  if (btb[btb_idx].valid
98  && inst_tag == btb[btb_idx].tag
99  && btb[btb_idx].tid == tid) {
100  return true;
101  } else {
102  return false;
103  }
104 }
105 
106 // @todo Create some sort of return struct that has both whether or not the
107 // address is valid, and also the address. For now will just use addr = 0 to
108 // represent invalid entry.
111 {
112  unsigned btb_idx = getIndex(instPC, tid);
113 
114  Addr inst_tag = getTag(instPC);
115 
116  assert(btb_idx < numEntries);
117 
118  if (btb[btb_idx].valid
119  && inst_tag == btb[btb_idx].tag
120  && btb[btb_idx].tid == tid) {
121  return btb[btb_idx].target;
122  } else {
123  return 0;
124  }
125 }
126 
127 void
128 DefaultBTB::update(Addr instPC, const TheISA::PCState &target, ThreadID tid)
129 {
130  unsigned btb_idx = getIndex(instPC, tid);
131 
132  assert(btb_idx < numEntries);
133 
134  btb[btb_idx].tid = tid;
135  btb[btb_idx].valid = true;
136  btb[btb_idx].target = target;
137  btb[btb_idx].tag = getTag(instPC);
138 }
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:183
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
ThreadID
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:227
btb.hh
DefaultBTB::log2NumThreads
unsigned log2NumThreads
Log2 NumThreads used for hashing threadid.
Definition: btb.hh:128
DefaultBTB::tagBits
unsigned tagBits
The number of tag bits per entry.
Definition: btb.hh:116
floorLog2
std::enable_if< std::is_integral< T >::value, int >::type floorLog2(T x)
Definition: intmath.hh:63
DefaultBTB::instShiftAmt
unsigned instShiftAmt
Number of bits to shift PC when calculating index.
Definition: btb.hh:122
DefaultBTB::btb
std::vector< BTBEntry > btb
The actual BTB.
Definition: btb.hh:107
DefaultBTB::update
void update(Addr instPC, const TheISA::PCState &targetPC, ThreadID tid)
Updates the BTB with the target of a branch.
Definition: btb.cc:128
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
DefaultBTB::valid
bool valid(Addr instPC, ThreadID tid)
Checks if a branch is in the BTB.
Definition: btb.cc:89
DefaultBTB::DefaultBTB
DefaultBTB(unsigned numEntries, unsigned tagBits, unsigned instShiftAmt, unsigned numThreads)
Creates a BTB with the given number of entries, number of bits per tag, and instruction offset amount...
Definition: btb.cc:35
DefaultBTB::numEntries
unsigned numEntries
The number of entries in the BTB.
Definition: btb.hh:110
DefaultBTB::tagShiftAmt
unsigned tagShiftAmt
Number of bits to shift PC when calculating tag.
Definition: btb.hh:125
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
DefaultBTB::tagMask
unsigned tagMask
The tag mask.
Definition: btb.hh:119
DefaultBTB::getTag
Addr getTag(Addr instPC)
Returns the tag bits of a given address.
Definition: btb.cc:83
DefaultBTB::getIndex
unsigned getIndex(Addr instPC, ThreadID tid)
Returns the index into the BTB, based on the branch's PC.
Definition: btb.cc:73
DefaultBTB::reset
void reset()
Definition: btb.cc:64
MipsISA::PCState
GenericISA::DelaySlotPCState< MachInst > PCState
Definition: types.hh:41
trace.hh
DefaultBTB::lookup
TheISA::PCState lookup(Addr instPC, ThreadID tid)
Looks up an address in the BTB.
Definition: btb.cc:110
DefaultBTB::idxMask
unsigned idxMask
The index mask.
Definition: btb.hh:113
intmath.hh
isPowerOf2
bool isPowerOf2(const T &n)
Definition: intmath.hh:102

Generated on Wed Sep 30 2020 14:02:09 for gem5 by doxygen 1.8.17