gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
indirect.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 ARM Limited
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: Mitch Hayenga
29  */
30 
31 #include "cpu/pred/indirect.hh"
32 
33 #include "base/intmath.hh"
34 #include "debug/Indirect.hh"
35 
36 IndirectPredictor::IndirectPredictor(bool hash_ghr, bool hash_targets,
37  unsigned num_sets, unsigned num_ways,
38  unsigned tag_bits, unsigned path_len, unsigned inst_shift,
39  unsigned num_threads)
40  : hashGHR(hash_ghr), hashTargets(hash_targets),
41  numSets(num_sets), numWays(num_ways), tagBits(tag_bits),
42  pathLength(path_len), instShift(inst_shift)
43 {
44  if (!isPowerOf2(numSets)) {
45  panic("Indirect predictor requires power of 2 number of sets");
46  }
47 
48  threadInfo.resize(num_threads);
49 
50  targetCache.resize(numSets);
51  for (unsigned i = 0; i < numSets; i++) {
52  targetCache[i].resize(numWays);
53  }
54 }
55 
56 bool
57 IndirectPredictor::lookup(Addr br_addr, unsigned ghr, TheISA::PCState& target,
58  ThreadID tid)
59 {
60  Addr set_index = getSetIndex(br_addr, ghr, tid);
61  Addr tag = getTag(br_addr);
62 
63  assert(set_index < numSets);
64 
65  DPRINTF(Indirect, "Looking up %x (set:%d)\n", br_addr, set_index);
66  const auto &iset = targetCache[set_index];
67  for (auto way = iset.begin(); way != iset.end(); ++way) {
68  if (way->tag == tag) {
69  DPRINTF(Indirect, "Hit %x (target:%s)\n", br_addr, way->target);
70  target = way->target;
71  return true;
72  }
73  }
74  DPRINTF(Indirect, "Miss %x\n", br_addr);
75  return false;
76 }
77 
78 void
80  InstSeqNum seq_num, ThreadID tid)
81 {
82  DPRINTF(Indirect, "Recording %x seq:%d\n", br_addr, seq_num);
83  HistoryEntry entry(br_addr, tgt_addr, seq_num);
84  threadInfo[tid].pathHist.push_back(entry);
85 }
86 
87 void
89 {
90  DPRINTF(Indirect, "Committing seq:%d\n", seq_num);
91  ThreadInfo &t_info = threadInfo[tid];
92 
93  if (t_info.pathHist.empty()) return;
94 
95  if (t_info.headHistEntry < t_info.pathHist.size() &&
96  t_info.pathHist[t_info.headHistEntry].seqNum <= seq_num) {
97  if (t_info.headHistEntry >= pathLength) {
98  t_info.pathHist.pop_front();
99  } else {
100  ++t_info.headHistEntry;
101  }
102  }
103 }
104 
105 void
107 {
108  DPRINTF(Indirect, "Squashing seq:%d\n", seq_num);
109  ThreadInfo &t_info = threadInfo[tid];
110  auto squash_itr = t_info.pathHist.begin();
111  while (squash_itr != t_info.pathHist.end()) {
112  if (squash_itr->seqNum > seq_num) {
113  break;
114  }
115  ++squash_itr;
116  }
117  if (squash_itr != t_info.pathHist.end()) {
118  DPRINTF(Indirect, "Squashing series starting with sn:%d\n",
119  squash_itr->seqNum);
120  }
121  t_info.pathHist.erase(squash_itr, t_info.pathHist.end());
122 }
123 
124 
125 void
127  const TheISA::PCState& target, ThreadID tid)
128 {
129  ThreadInfo &t_info = threadInfo[tid];
130 
131  // Should have just squashed so this branch should be the oldest
132  auto hist_entry = *(t_info.pathHist.rbegin());
133  // Temporarily pop it off the history so we can calculate the set
134  t_info.pathHist.pop_back();
135  Addr set_index = getSetIndex(hist_entry.pcAddr, ghr, tid);
136  Addr tag = getTag(hist_entry.pcAddr);
137  hist_entry.targetAddr = target.instAddr();
138  t_info.pathHist.push_back(hist_entry);
139 
140  assert(set_index < numSets);
141 
142  auto &iset = targetCache[set_index];
143  for (auto way = iset.begin(); way != iset.end(); ++way) {
144  if (way->tag == tag) {
145  DPRINTF(Indirect, "Updating Target (seq: %d br:%x set:%d target:"
146  "%s)\n", seq_num, hist_entry.pcAddr, set_index, target);
147  way->target = target;
148  return;
149  }
150  }
151 
152  DPRINTF(Indirect, "Allocating Target (seq: %d br:%x set:%d target:%s)\n",
153  seq_num, hist_entry.pcAddr, set_index, target);
154  // Did not find entry, random replacement
155  auto &way = iset[rand() % numWays];
156  way.tag = tag;
157  way.target = target;
158 }
159 
160 
161 inline Addr
162 IndirectPredictor::getSetIndex(Addr br_addr, unsigned ghr, ThreadID tid)
163 {
164  ThreadInfo &t_info = threadInfo[tid];
165 
166  Addr hash = br_addr >> instShift;
167  if (hashGHR) {
168  hash ^= ghr;
169  }
170  if (hashTargets) {
171  unsigned hash_shift = floorLog2(numSets) / pathLength;
172  for (int i = t_info.pathHist.size()-1, p = 0;
173  i >= 0 && p < pathLength; i--, p++) {
174  hash ^= (t_info.pathHist[i].targetAddr >>
175  (instShift + p*hash_shift));
176  }
177  }
178  return hash & (numSets-1);
179 }
180 
181 inline Addr
183 {
184  return (br_addr >> instShift) & ((0x1<<tagBits)-1);
185 }
#define DPRINTF(x,...)
Definition: trace.hh:212
std::vector< ThreadInfo > threadInfo
Definition: indirect.hh:94
Bitfield< 7 > i
Definition: miscregs.hh:1378
#define panic(...)
Definition: misc.hh:153
const unsigned pathLength
Definition: indirect.hh:62
std::vector< std::vector< IPredEntry > > targetCache
Definition: indirect.hh:72
const unsigned numWays
Definition: indirect.hh:60
Addr getTag(Addr br_addr)
Definition: indirect.cc:182
Addr getSetIndex(Addr br_addr, unsigned ghr, ThreadID tid)
Definition: indirect.cc:162
void commit(InstSeqNum seq_num, ThreadID tid)
Definition: indirect.cc:88
bool isPowerOf2(const T &n)
Definition: intmath.hh:73
uint64_t InstSeqNum
Definition: inst_seq.hh:40
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:171
int floorLog2(unsigned x)
Definition: intmath.hh:100
const unsigned numSets
Definition: indirect.hh:59
const bool hashTargets
Definition: indirect.hh:58
void recordIndirect(Addr br_addr, Addr tgt_addr, InstSeqNum seq_num, ThreadID tid)
Definition: indirect.cc:79
GenericISA::SimplePCState< MachInst > PCState
Definition: types.hh:43
void recordTarget(InstSeqNum seq_num, unsigned ghr, const TheISA::PCState &target, ThreadID tid)
Definition: indirect.cc:126
IndirectPredictor(bool hash_ghr, bool hash_targets, unsigned num_sets, unsigned num_ways, unsigned tag_bits, unsigned path_len, unsigned inst_shift, unsigned num_threads)
Definition: indirect.cc:36
std::deque< HistoryEntry > pathHist
Definition: indirect.hh:90
bool lookup(Addr br_addr, unsigned ghr, TheISA::PCState &br_target, ThreadID tid)
Definition: indirect.cc:57
const unsigned instShift
Definition: indirect.hh:63
void squash(InstSeqNum seq_num, ThreadID tid)
Definition: indirect.cc:106
Bitfield< 0 > p
const bool hashGHR
Definition: indirect.hh:57
const unsigned tagBits
Definition: indirect.hh:61

Generated on Fri Jun 9 2017 13:03:44 for gem5 by doxygen 1.8.6