2 * Copyright (c) 2004-2006 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.
31 #ifndef __CPU_O3_RENAME_HH__
32 #define __CPU_O3_RENAME_HH__
36 #include "base/statistics.hh"
37 #include "base/timebuf.hh"
40 * DefaultRename handles both single threaded and SMT rename. Its
41 * width is specified by the parameters; each cycle it tries to rename
42 * that many instructions. It holds onto the rename history of all
43 * instructions with destination registers, storing the
44 * arch. register, the new physical register, and the old physical
45 * register, to allow for undoing of mappings if squashing happens, or
46 * freeing up registers upon commit. Rename handles blocking if the
47 * ROB, IQ, or LSQ is going to be full. Rename also handles barriers,
48 * and does so by stalling on the instruction until the ROB is empty
49 * and there are no instructions in flight to the ROB.
55 // Typedefs from the Impl.
56 typedef typename Impl::CPUPol CPUPol;
57 typedef typename Impl::DynInstPtr DynInstPtr;
58 typedef typename Impl::O3CPU O3CPU;
59 typedef typename Impl::Params Params;
61 // Typedefs from the CPUPol
62 typedef typename CPUPol::DecodeStruct DecodeStruct;
63 typedef typename CPUPol::RenameStruct RenameStruct;
64 typedef typename CPUPol::TimeStruct TimeStruct;
65 typedef typename CPUPol::FreeList FreeList;
66 typedef typename CPUPol::RenameMap RenameMap;
67 // These are used only for initialization.
68 typedef typename CPUPol::IEW IEW;
69 typedef typename CPUPol::Commit Commit;
71 // Typedefs from the ISA.
72 typedef TheISA::RegIndex RegIndex;
74 // A list is used to queue the instructions. Barrier insts must
75 // be added to the front of the list, which is the only reason for
76 // using a list instead of a queue. (Most other stages use a
78 typedef std::list<DynInstPtr> InstQueue;
81 /** Overall rename status. Used to determine if the CPU can
82 * deschedule itself due to a lack of activity.
89 /** Individual thread status. */
101 /** Rename status. */
102 RenameStatus _status;
104 /** Per-thread status. */
105 ThreadStatus renameStatus[Impl::MaxThreads];
108 /** DefaultRename constructor. */
109 DefaultRename(Params *params);
111 /** Returns the name of rename. */
112 std::string name() const;
114 /** Registers statistics. */
117 /** Sets CPU pointer. */
118 void setCPU(O3CPU *cpu_ptr);
120 /** Sets the main backwards communication time buffer pointer. */
121 void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
123 /** Sets pointer to time buffer used to communicate to the next stage. */
124 void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr);
126 /** Sets pointer to time buffer coming from decode. */
127 void setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr);
129 /** Sets pointer to IEW stage. Used only for initialization. */
130 void setIEWStage(IEW *iew_stage)
131 { iew_ptr = iew_stage; }
133 /** Sets pointer to commit stage. Used only for initialization. */
134 void setCommitStage(Commit *commit_stage)
135 { commit_ptr = commit_stage; }
138 /** Pointer to IEW stage. Used only for initialization. */
141 /** Pointer to commit stage. Used only for initialization. */
145 /** Initializes variables for the stage. */
148 /** Sets pointer to list of active threads. */
149 void setActiveThreads(std::list<unsigned> *at_ptr);
151 /** Sets pointer to rename maps (per-thread structures). */
152 void setRenameMap(RenameMap rm_ptr[Impl::MaxThreads]);
154 /** Sets pointer to the free list. */
155 void setFreeList(FreeList *fl_ptr);
157 /** Sets pointer to the scoreboard. */
158 void setScoreboard(Scoreboard *_scoreboard);
160 /** Drains the rename stage. */
163 /** Resumes execution after a drain. */
166 /** Switches out the rename stage. */
169 /** Takes over from another CPU's thread. */
172 /** Squashes all instructions in a thread. */
173 void squash(unsigned tid);
175 /** Ticks rename, which processes all input signals and attempts to rename
176 * as many instructions as possible.
180 /** Debugging function used to dump history buffer of renamings. */
184 /** Determines what to do based on rename's current status.
185 * @param status_change rename() sets this variable if there was a status
186 * change (ie switching from blocking to unblocking).
187 * @param tid Thread id to rename instructions from.
189 void rename(bool &status_change, unsigned tid);
191 /** Renames instructions for the given thread. Also handles serializing
194 void renameInsts(unsigned tid);
196 /** Inserts unused instructions from a given thread into the skid buffer,
197 * to be renamed once rename unblocks.
199 void skidInsert(unsigned tid);
201 /** Separates instructions from decode into individual lists of instructions
206 /** Returns if all of the skid buffers are empty. */
209 /** Updates overall rename status based on all of the threads' statuses. */
212 /** Switches rename to blocking, and signals back that rename has become
214 * @return Returns true if there is a status change.
216 bool block(unsigned tid);
218 /** Switches rename to unblocking if the skid buffer is empty, and signals
219 * back that rename has unblocked.
220 * @return Returns true if there is a status change.
222 bool unblock(unsigned tid);
224 /** Executes actual squash, removing squashed instructions. */
225 void doSquash(unsigned tid);
227 /** Removes a committed instruction's rename history. */
228 void removeFromHistory(InstSeqNum inst_seq_num, unsigned tid);
230 /** Renames the source registers of an instruction. */
231 inline void renameSrcRegs(DynInstPtr &inst, unsigned tid);
233 /** Renames the destination registers of an instruction. */
234 inline void renameDestRegs(DynInstPtr &inst, unsigned tid);
236 /** Calculates the number of free ROB entries for a specific thread. */
237 inline int calcFreeROBEntries(unsigned tid);
239 /** Calculates the number of free IQ entries for a specific thread. */
240 inline int calcFreeIQEntries(unsigned tid);
242 /** Calculates the number of free LSQ entries for a specific thread. */
243 inline int calcFreeLSQEntries(unsigned tid);
245 /** Returns the number of valid instructions coming from decode. */
246 unsigned validInsts();
248 /** Reads signals telling rename to block/unblock. */
249 void readStallSignals(unsigned tid);
251 /** Checks if any stages are telling rename to block. */
252 bool checkStall(unsigned tid);
254 /** Gets the number of free entries for a specific thread. */
255 void readFreeEntries(unsigned tid);
257 /** Checks the signals and updates the status. */
258 bool checkSignalsAndUpdate(unsigned tid);
260 /** Either serializes on the next instruction available in the InstQueue,
261 * or records that it must serialize on the next instruction to enter
263 * @param inst_list The list of younger, unprocessed instructions for the
264 * thread that has the serializeAfter instruction.
265 * @param tid The thread id.
267 void serializeAfter(InstQueue &inst_list, unsigned tid);
269 /** Holds the information for each destination register rename. It holds
270 * the instruction's sequence number, the arch register, the old physical
271 * register for that arch. register, and the new physical register.
273 struct RenameHistory {
274 RenameHistory(InstSeqNum _instSeqNum, RegIndex _archReg,
275 PhysRegIndex _newPhysReg, PhysRegIndex _prevPhysReg)
276 : instSeqNum(_instSeqNum), archReg(_archReg),
277 newPhysReg(_newPhysReg), prevPhysReg(_prevPhysReg)
281 /** The sequence number of the instruction that renamed. */
282 InstSeqNum instSeqNum;
283 /** The architectural register index that was renamed. */
285 /** The new physical register that the arch. register is renamed to. */
286 PhysRegIndex newPhysReg;
287 /** The old physical register that the arch. register was renamed to. */
288 PhysRegIndex prevPhysReg;
291 /** A per-thread list of all destination register renames, used to either
292 * undo rename mappings or free old physical registers.
294 std::list<RenameHistory> historyBuffer[Impl::MaxThreads];
296 /** Pointer to CPU. */
299 /** Pointer to main time buffer used for backwards communication. */
300 TimeBuffer<TimeStruct> *timeBuffer;
302 /** Wire to get IEW's output from backwards time buffer. */
303 typename TimeBuffer<TimeStruct>::wire fromIEW;
305 /** Wire to get commit's output from backwards time buffer. */
306 typename TimeBuffer<TimeStruct>::wire fromCommit;
308 /** Wire to write infromation heading to previous stages. */
309 typename TimeBuffer<TimeStruct>::wire toDecode;
311 /** Rename instruction queue. */
312 TimeBuffer<RenameStruct> *renameQueue;
314 /** Wire to write any information heading to IEW. */
315 typename TimeBuffer<RenameStruct>::wire toIEW;
317 /** Decode instruction queue interface. */
318 TimeBuffer<DecodeStruct> *decodeQueue;
320 /** Wire to get decode's output from decode queue. */
321 typename TimeBuffer<DecodeStruct>::wire fromDecode;
323 /** Queue of all instructions coming from decode this cycle. */
324 InstQueue insts[Impl::MaxThreads];
326 /** Skid buffer between rename and decode. */
327 InstQueue skidBuffer[Impl::MaxThreads];
329 /** Rename map interface. */
330 RenameMap *renameMap[Impl::MaxThreads];
332 /** Free list interface. */
335 /** Pointer to the list of active threads. */
336 std::list<unsigned> *activeThreads;
338 /** Pointer to the scoreboard. */
339 Scoreboard *scoreboard;
341 /** Count of instructions in progress that have been sent off to the IQ
342 * and ROB, but are not yet included in their occupancy counts.
344 int instsInProgress[Impl::MaxThreads];
346 /** Variable that tracks if decode has written to the time buffer this
347 * cycle. Used to tell CPU if there is activity this cycle.
349 bool wroteToTimeBuffer;
351 /** Structures whose free entries impact the amount of instructions that
360 /** Per-thread tracking of the number of free entries of back-end
363 FreeEntries freeEntries[Impl::MaxThreads];
365 /** Records if the ROB is empty. In SMT mode the ROB may be dynamically
366 * partitioned between threads, so the ROB must tell rename when it is
369 bool emptyROB[Impl::MaxThreads];
371 /** Source of possible stalls. */
377 /** Tracks which stages are telling decode to stall. */
378 Stalls stalls[Impl::MaxThreads];
380 /** The serialize instruction that rename has stalled on. */
381 DynInstPtr serializeInst[Impl::MaxThreads];
383 /** Records if rename needs to serialize on the next instruction for any
386 bool serializeOnNextInst[Impl::MaxThreads];
388 /** Delay between iew and rename, in ticks. */
389 int iewToRenameDelay;
391 /** Delay between decode and rename, in ticks. */
392 int decodeToRenameDelay;
394 /** Delay between commit and rename, in ticks. */
395 unsigned commitToRenameDelay;
397 /** Rename width, in instructions. */
398 unsigned renameWidth;
400 /** Commit width, in instructions. Used so rename knows how many
401 * instructions might have freed registers in the previous cycle.
403 unsigned commitWidth;
405 /** The index of the instruction in the time buffer to IEW that rename is
410 /** Whether or not rename needs to block this cycle. */
413 /** The number of threads active in rename. */
416 /** The maximum skid buffer size. */
417 unsigned skidBufferMax;
419 /** Enum to record the source of a structure full stall. Can come from
420 * either ROB, IQ, LSQ, and it is priortized in that order.
429 /** Function used to increment the stat that corresponds to the source of
432 inline void incrFullStat(const FullSource &source);
434 /** Stat for total number of cycles spent squashing. */
435 Stats::Scalar<> renameSquashCycles;
436 /** Stat for total number of cycles spent idle. */
437 Stats::Scalar<> renameIdleCycles;
438 /** Stat for total number of cycles spent blocking. */
439 Stats::Scalar<> renameBlockCycles;
440 /** Stat for total number of cycles spent stalling for a serializing inst. */
441 Stats::Scalar<> renameSerializeStallCycles;
442 /** Stat for total number of cycles spent running normally. */
443 Stats::Scalar<> renameRunCycles;
444 /** Stat for total number of cycles spent unblocking. */
445 Stats::Scalar<> renameUnblockCycles;
446 /** Stat for total number of renamed instructions. */
447 Stats::Scalar<> renameRenamedInsts;
448 /** Stat for total number of squashed instructions that rename discards. */
449 Stats::Scalar<> renameSquashedInsts;
450 /** Stat for total number of times that the ROB starts a stall in rename. */
451 Stats::Scalar<> renameROBFullEvents;
452 /** Stat for total number of times that the IQ starts a stall in rename. */
453 Stats::Scalar<> renameIQFullEvents;
454 /** Stat for total number of times that the LSQ starts a stall in rename. */
455 Stats::Scalar<> renameLSQFullEvents;
456 /** Stat for total number of times that rename runs out of free registers
457 * to use to rename. */
458 Stats::Scalar<> renameFullRegistersEvents;
459 /** Stat for total number of renamed destination registers. */
460 Stats::Scalar<> renameRenamedOperands;
461 /** Stat for total number of source register rename lookups. */
462 Stats::Scalar<> renameRenameLookups;
463 /** Stat for total number of committed renaming mappings. */
464 Stats::Scalar<> renameCommittedMaps;
465 /** Stat for total number of mappings that were undone due to a squash. */
466 Stats::Scalar<> renameUndoneMaps;
467 /** Number of serialize instructions handled. */
468 Stats::Scalar<> renamedSerializing;
469 /** Number of instructions marked as temporarily serializing. */
470 Stats::Scalar<> renamedTempSerializing;
471 /** Number of instructions inserted into skid buffers. */
472 Stats::Scalar<> renameSkidInsts;
475 #endif // __CPU_O3_RENAME_HH__