2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
29 #ifndef __CPU_O3_CPU_ROB_IMPL_HH__
30 #define __CPU_O3_CPU_ROB_IMPL_HH__
32 #include "config/full_system.hh"
33 #include "cpu/o3/rob.hh"
36 ROB<Impl>::ROB(unsigned _numEntries, unsigned _squashWidth)
37 : numEntries(_numEntries),
38 squashWidth(_squashWidth),
47 ROB<Impl>::setCPU(FullCPU *cpu_ptr)
51 // Set the tail to the beginning of the CPU instruction list so that
52 // upon the first instruction being inserted into the ROB, the tail
53 // iterator can simply be incremented.
54 tail = cpu->instList.begin();
56 // Set the squash iterator to the end of the instruction list.
57 squashIt = cpu->instList.end();
62 ROB<Impl>::countInsts()
64 // Start at 1; if the tail matches cpu->instList.begin(), then there is
65 // one inst in the ROB.
68 // There are quite a few special cases. Do not use this function other
69 // than for debugging purposes.
70 if (cpu->instList.begin() == cpu->instList.end()) {
71 // In this case there are no instructions in the list. The ROB
74 } else if (tail == cpu->instList.end()) {
75 // In this case, the tail is not yet pointing to anything valid.
76 // The ROB must be empty.
80 // Iterate through the ROB from the head to the tail, counting the
82 for (InstIt_t i = cpu->instList.begin(); i != tail; ++i)
84 assert(i != cpu->instList.end());
90 // Because the head won't be tracked properly until the ROB gets the
91 // first instruction, and any time that the ROB is empty and has not
92 // yet gotten the instruction, this function doesn't work.
93 // return numInstsInROB;
98 ROB<Impl>::insertInst(DynInstPtr &inst)
100 // Make sure we have the right number of instructions.
101 assert(numInstsInROB == countInsts());
102 // Make sure the instruction is valid.
105 DPRINTF(ROB, "ROB: Adding inst PC %#x to the ROB.\n", inst->readPC());
107 // If the ROB is full then exit.
108 assert(numInstsInROB != numEntries);
112 // Increment the tail iterator, moving it one instruction back.
113 // There is a special case if the ROB was empty prior to this insertion,
114 // in which case the tail will be pointing at instList.end(). If that
115 // happens, then reset the tail to the beginning of the list.
116 if (tail != cpu->instList.end()) {
119 tail = cpu->instList.begin();
122 // Make sure the tail iterator is actually pointing at the instruction
124 assert((*tail) == inst);
126 DPRINTF(ROB, "ROB: Now has %d instructions.\n", numInstsInROB);
130 // Whatever calls this function needs to ensure that it properly frees up
131 // registers prior to this function.
132 template <class Impl>
134 ROB<Impl>::retireHead()
136 assert(numInstsInROB == countInsts());
137 assert(numInstsInROB > 0);
139 // Get the head ROB instruction.
140 DynInstPtr head_inst = cpu->instList.front();
142 // Make certain this can retire.
143 assert(head_inst->readyToCommit());
145 DPRINTF(ROB, "ROB: Retiring head instruction of the ROB, "
146 "instruction PC %#x, seq num %i\n", head_inst->readPC(),
149 // Keep track of how many instructions are in the ROB.
152 // Tell CPU to remove the instruction from the list of instructions.
153 // A special case is needed if the instruction being retired is the
154 // only instruction in the ROB; otherwise the tail iterator will become
156 cpu->removeFrontInst(head_inst);
158 if (numInstsInROB == 0) {
159 tail = cpu->instList.end();
163 template <class Impl>
165 ROB<Impl>::isHeadReady()
167 if (numInstsInROB != 0) {
168 return cpu->instList.front()->readyToCommit();
174 template <class Impl>
176 ROB<Impl>::numFreeEntries()
178 assert(numInstsInROB == countInsts());
180 return numEntries - numInstsInROB;
183 template <class Impl>
185 ROB<Impl>::doSquash()
187 DPRINTF(ROB, "ROB: Squashing instructions.\n");
189 assert(squashIt != cpu->instList.end());
191 for (int numSquashed = 0;
192 numSquashed < squashWidth && (*squashIt)->seqNum != squashedSeqNum;
195 // Ensure that the instruction is younger.
196 assert((*squashIt)->seqNum > squashedSeqNum);
198 DPRINTF(ROB, "ROB: Squashing instruction PC %#x, seq num %i.\n",
199 (*squashIt)->readPC(), (*squashIt)->seqNum);
201 // Mark the instruction as squashed, and ready to commit so that
202 // it can drain out of the pipeline.
203 (*squashIt)->setSquashed();
205 (*squashIt)->setCanCommit();
207 // Special case for when squashing due to a syscall. It's possible
208 // that the squash happened after the head instruction was already
209 // committed, meaning that (*squashIt)->seqNum != squashedSeqNum
210 // will never be false. Normally the squash would never be able
211 // to go past the head of the ROB; in this case it might, so it
212 // must be handled otherwise it will segfault.
214 if (squashIt == cpu->instList.begin()) {
215 DPRINTF(ROB, "ROB: Reached head of instruction list while "
218 squashIt = cpu->instList.end();
220 doneSquashing = true;
226 // Move the tail iterator to the next instruction.
231 // Check if ROB is done squashing.
232 if ((*squashIt)->seqNum == squashedSeqNum) {
233 DPRINTF(ROB, "ROB: Done squashing instructions.\n");
235 squashIt = cpu->instList.end();
237 doneSquashing = true;
241 template <class Impl>
243 ROB<Impl>::squash(InstSeqNum squash_num)
245 DPRINTF(ROB, "ROB: Starting to squash within the ROB.\n");
246 doneSquashing = false;
248 squashedSeqNum = squash_num;
250 assert(tail != cpu->instList.end());
257 template <class Impl>
259 ROB<Impl>::readHeadPC()
261 assert(numInstsInROB == countInsts());
263 DynInstPtr head_inst = cpu->instList.front();
265 return head_inst->readPC();
268 template <class Impl>
270 ROB<Impl>::readHeadNextPC()
272 assert(numInstsInROB == countInsts());
274 DynInstPtr head_inst = cpu->instList.front();
276 return head_inst->readNextPC();
279 template <class Impl>
281 ROB<Impl>::readHeadSeqNum()
283 // Return the last sequence number that has not been squashed. Other
284 // stages can use it to squash any instructions younger than the current
286 DynInstPtr head_inst = cpu->instList.front();
288 return head_inst->seqNum;
291 template <class Impl>
293 ROB<Impl>::readTailPC()
295 assert(numInstsInROB == countInsts());
297 assert(tail != cpu->instList.end());
299 return (*tail)->readPC();
302 template <class Impl>
304 ROB<Impl>::readTailSeqNum()
306 // Return the last sequence number that has not been squashed. Other
307 // stages can use it to squash any instructions younger than the current
309 return (*tail)->seqNum;
312 #endif // __CPU_O3_CPU_ROB_IMPL_HH__