gem5  v22.1.0.0
decode_cache.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Google
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 #ifndef __CPU_DECODE_CACHE_HH__
30 #define __CPU_DECODE_CACHE_HH__
31 
32 #include <unordered_map>
33 
34 #include "base/bitfield.hh"
35 #include "base/compiler.hh"
36 #include "cpu/static_inst_fwd.hh"
37 
38 namespace gem5
39 {
40 
41 GEM5_DEPRECATED_NAMESPACE(DecodeCache, decode_cache);
42 namespace decode_cache
43 {
44 
46 template <typename EMI>
47 using InstMap = std::unordered_map<EMI, StaticInstPtr>;
48 
50 template<class Value, Addr CacheChunkShift = 12>
51 class AddrMap
52 {
53  protected:
54  static constexpr Addr CacheChunkBytes = 1ULL << CacheChunkShift;
55 
56  static constexpr Addr
58  {
59  return addr & (CacheChunkBytes - 1);
60  }
61 
62  static constexpr Addr
64  {
65  return addr & ~(CacheChunkBytes - 1);
66  }
67 
68  // A chunk of cache entries.
69  struct CacheChunk
70  {
72  };
73  // A map of cache chunks which allows a sparse mapping.
74  typedef typename std::unordered_map<Addr, CacheChunk *> ChunkMap;
75  typedef typename ChunkMap::iterator ChunkIt;
76  // Mini cache of recent lookups.
79 
82  void
83  update(ChunkIt recentest)
84  {
85  recent[1] = recent[0];
86  recent[0] = recentest;
87  }
88 
93  CacheChunk *
95  {
96  Addr chunk_addr = chunkStart(addr);
97 
98  // Check against recent lookups.
99  if (recent[0] != chunkMap.end()) {
100  if (recent[0]->first == chunk_addr)
101  return recent[0]->second;
102  if (recent[1] != chunkMap.end() &&
103  recent[1]->first == chunk_addr) {
104  update(recent[1]);
105  // recent[1] has just become recent[0].
106  return recent[0]->second;
107  }
108  }
109 
110  // Actually look in the hash_map.
111  ChunkIt it = chunkMap.find(chunk_addr);
112  if (it != chunkMap.end()) {
113  update(it);
114  return it->second;
115  }
116 
117  // Didn't find an existing chunk, so add a new one.
118  CacheChunk *newChunk = new CacheChunk;
119  typename ChunkMap::value_type to_insert(chunk_addr, newChunk);
120  update(chunkMap.insert(to_insert).first);
121  return newChunk;
122  }
123 
124  public:
127  {
128  recent[0] = recent[1] = chunkMap.end();
129  }
130 
131  Value &
133  {
134  CacheChunk *chunk = getChunk(addr);
135  return chunk->items[chunkOffset(addr)];
136  }
137 };
138 
139 } // namespace decode_cache
140 } // namespace gem5
141 
142 #endif // __CPU_DECODE_CACHE_HH__
A sparse map from an Addr to a Value, stored in page chunks.
Definition: decode_cache.hh:52
static constexpr Addr chunkOffset(Addr addr)
Definition: decode_cache.hh:57
CacheChunk * getChunk(Addr addr)
Attempt to find the CacheChunk which goes with a particular address.
Definition: decode_cache.hh:94
static constexpr Addr chunkStart(Addr addr)
Definition: decode_cache.hh:63
void update(ChunkIt recentest)
Update the mini cache of recent lookups.
Definition: decode_cache.hh:83
ChunkMap::iterator ChunkIt
Definition: decode_cache.hh:75
std::unordered_map< Addr, CacheChunk * > ChunkMap
Definition: decode_cache.hh:74
static constexpr Addr CacheChunkBytes
Definition: decode_cache.hh:54
Value & lookup(Addr addr)
Bitfield< 3 > addr
Definition: types.hh:84
std::unordered_map< EMI, StaticInstPtr > InstMap
Hash for decoded instructions.
Definition: decode_cache.hh:47
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)

Generated on Wed Dec 21 2022 10:22:27 for gem5 by doxygen 1.9.1