cpu-minor: this is a bug fix for MinorCPU for thread cloning.
[gem5.git] / src / cpu / decode_cache.hh
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 "cpu/static_inst_fwd.hh"
36
37 namespace DecodeCache
38 {
39
40 /// Hash for decoded instructions.
41 template <typename EMI>
42 using InstMap = std::unordered_map<EMI, StaticInstPtr>;
43
44 /// A sparse map from an Addr to a Value, stored in page chunks.
45 template<class Value, Addr CacheChunkShift = 12>
46 class AddrMap
47 {
48 protected:
49 static constexpr Addr CacheChunkBytes = 1ULL << CacheChunkShift;
50
51 static constexpr Addr
52 chunkOffset(Addr addr)
53 {
54 return addr & (CacheChunkBytes - 1);
55 }
56
57 static constexpr Addr
58 chunkStart(Addr addr)
59 {
60 return addr & ~(CacheChunkBytes - 1);
61 }
62
63 // A chunk of cache entries.
64 struct CacheChunk
65 {
66 Value items[CacheChunkBytes];
67 };
68 // A map of cache chunks which allows a sparse mapping.
69 typedef typename std::unordered_map<Addr, CacheChunk *> ChunkMap;
70 typedef typename ChunkMap::iterator ChunkIt;
71 // Mini cache of recent lookups.
72 ChunkIt recent[2];
73 ChunkMap chunkMap;
74
75 /// Update the mini cache of recent lookups.
76 /// @param recentest The most recent result;
77 void
78 update(ChunkIt recentest)
79 {
80 recent[1] = recent[0];
81 recent[0] = recentest;
82 }
83
84 /// Attempt to find the CacheChunk which goes with a particular
85 /// address. First check the small cache of recent results, then
86 /// actually look in the hash map.
87 /// @param addr The address to look up.
88 CacheChunk *
89 getChunk(Addr addr)
90 {
91 Addr chunk_addr = chunkStart(addr);
92
93 // Check against recent lookups.
94 if (recent[0] != chunkMap.end()) {
95 if (recent[0]->first == chunk_addr)
96 return recent[0]->second;
97 if (recent[1] != chunkMap.end() &&
98 recent[1]->first == chunk_addr) {
99 update(recent[1]);
100 // recent[1] has just become recent[0].
101 return recent[0]->second;
102 }
103 }
104
105 // Actually look in the hash_map.
106 ChunkIt it = chunkMap.find(chunk_addr);
107 if (it != chunkMap.end()) {
108 update(it);
109 return it->second;
110 }
111
112 // Didn't find an existing chunk, so add a new one.
113 CacheChunk *newChunk = new CacheChunk;
114 typename ChunkMap::value_type to_insert(chunk_addr, newChunk);
115 update(chunkMap.insert(to_insert).first);
116 return newChunk;
117 }
118
119 public:
120 /// Constructor
121 AddrMap()
122 {
123 recent[0] = recent[1] = chunkMap.end();
124 }
125
126 Value &
127 lookup(Addr addr)
128 {
129 CacheChunk *chunk = getChunk(addr);
130 return chunk->items[chunkOffset(addr)];
131 }
132 };
133
134 } // namespace DecodeCache
135
136 #endif // __CPU_DECODE_CACHE_HH__