rename.hh revision 9444:ab47fe7f03f0
12623SN/A/* 22623SN/A * Copyright (c) 2012 ARM Limited 32623SN/A * All rights reserved 42623SN/A * 52623SN/A * The license below extends only to copyright in the software and shall 62623SN/A * not be construed as granting a license to any other intellectual 72623SN/A * property including but not limited to intellectual property relating 82623SN/A * to a hardware implementation of the functionality of the software 92623SN/A * licensed hereunder. You may use the software subject to the license 102623SN/A * terms below provided that you ensure that this notice is replicated 112623SN/A * unmodified and in its entirety in all distributions of the software, 122623SN/A * modified or unmodified, in source code or in binary form. 132623SN/A * 142623SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan 152623SN/A * All rights reserved. 162623SN/A * 172623SN/A * Redistribution and use in source and binary forms, with or without 182623SN/A * modification, are permitted provided that the following conditions are 192623SN/A * met: redistributions of source code must retain the above copyright 202623SN/A * notice, this list of conditions and the following disclaimer; 212623SN/A * redistributions in binary form must reproduce the above copyright 222623SN/A * notice, this list of conditions and the following disclaimer in the 232623SN/A * documentation and/or other materials provided with the distribution; 242623SN/A * neither the name of the copyright holders nor the names of its 252623SN/A * contributors may be used to endorse or promote products derived from 262623SN/A * this software without specific prior written permission. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 292623SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 302623SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 312623SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 322623SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 332623SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 342623SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 352623SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 362623SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 372623SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 382623SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 392623SN/A * 402623SN/A * Authors: Kevin Lim 412623SN/A */ 422623SN/A 432623SN/A#ifndef __CPU_O3_RENAME_HH__ 442623SN/A#define __CPU_O3_RENAME_HH__ 452623SN/A 462623SN/A#include <list> 472623SN/A 482623SN/A#include "base/statistics.hh" 492623SN/A#include "config/the_isa.hh" 502623SN/A#include "cpu/timebuf.hh" 512623SN/A 522623SN/Astruct DerivO3CPUParams; 532623SN/A 542623SN/A/** 552623SN/A * DefaultRename handles both single threaded and SMT rename. Its 562623SN/A * width is specified by the parameters; each cycle it tries to rename 572623SN/A * that many instructions. It holds onto the rename history of all 582623SN/A * instructions with destination registers, storing the 592623SN/A * arch. register, the new physical register, and the old physical 602623SN/A * register, to allow for undoing of mappings if squashing happens, or 612623SN/A * freeing up registers upon commit. Rename handles blocking if the 622623SN/A * ROB, IQ, or LSQ is going to be full. Rename also handles barriers, 632623SN/A * and does so by stalling on the instruction until the ROB is empty 642623SN/A * and there are no instructions in flight to the ROB. 652623SN/A */ 662623SN/Atemplate<class Impl> 672623SN/Aclass DefaultRename 682623SN/A{ 692623SN/A public: 702623SN/A // Typedefs from the Impl. 712623SN/A typedef typename Impl::CPUPol CPUPol; 722623SN/A typedef typename Impl::DynInstPtr DynInstPtr; 732623SN/A typedef typename Impl::O3CPU O3CPU; 742623SN/A 752623SN/A // Typedefs from the CPUPol 762640Sstever@eecs.umich.edu typedef typename CPUPol::DecodeStruct DecodeStruct; 772640Sstever@eecs.umich.edu typedef typename CPUPol::RenameStruct RenameStruct; 782623SN/A typedef typename CPUPol::TimeStruct TimeStruct; 792623SN/A typedef typename CPUPol::FreeList FreeList; 802623SN/A typedef typename CPUPol::RenameMap RenameMap; 812623SN/A // These are used only for initialization. 822630SN/A typedef typename CPUPol::IEW IEW; 832623SN/A typedef typename CPUPol::Commit Commit; 842630SN/A 852623SN/A // Typedefs from the ISA. 862623SN/A typedef TheISA::RegIndex RegIndex; 872623SN/A 882623SN/A // A list is used to queue the instructions. Barrier insts must 892623SN/A // be added to the front of the list, which is the only reason for 902623SN/A // using a list instead of a queue. (Most other stages use a 912623SN/A // queue) 922623SN/A typedef std::list<DynInstPtr> InstQueue; 932623SN/A typedef typename std::list<DynInstPtr>::iterator ListIt; 942623SN/A 952623SN/A public: 962623SN/A /** Overall rename status. Used to determine if the CPU can 972623SN/A * deschedule itself due to a lack of activity. 982640Sstever@eecs.umich.edu */ 992623SN/A enum RenameStatus { 1002623SN/A Active, 1012623SN/A Inactive 1022623SN/A }; 1032630SN/A 1042623SN/A /** Individual thread status. */ 1052657Ssaidi@eecs.umich.edu enum ThreadStatus { 1062623SN/A Running, 1072623SN/A Idle, 1082623SN/A StartSquash, 1092623SN/A Squashing, 1102623SN/A Blocked, 1112623SN/A Unblocking, 1122623SN/A SerializeStall 1132640Sstever@eecs.umich.edu }; 1142623SN/A 1152623SN/A private: 1162623SN/A /** Rename status. */ 1172623SN/A RenameStatus _status; 1182630SN/A 1192623SN/A /** Per-thread status. */ 1202657Ssaidi@eecs.umich.edu ThreadStatus renameStatus[Impl::MaxThreads]; 1212623SN/A 1222623SN/A public: 1232623SN/A /** DefaultRename constructor. */ 1242623SN/A DefaultRename(O3CPU *_cpu, DerivO3CPUParams *params); 1252623SN/A 1262623SN/A /** Returns the name of rename. */ 1272623SN/A std::string name() const; 1282623SN/A 1292623SN/A /** Registers statistics. */ 1302623SN/A void regStats(); 1312623SN/A 1322623SN/A /** Sets the main backwards communication time buffer pointer. */ 1332623SN/A void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr); 1342623SN/A 1352623SN/A /** Sets pointer to time buffer used to communicate to the next stage. */ 1362623SN/A void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr); 1372623SN/A 1382623SN/A /** Sets pointer to time buffer coming from decode. */ 1392623SN/A void setDecodeQueue(TimeBuffer<DecodeStruct> *dq_ptr); 1402623SN/A 1412623SN/A /** Sets pointer to IEW stage. Used only for initialization. */ 1422623SN/A void setIEWStage(IEW *iew_stage) 1432623SN/A { iew_ptr = iew_stage; } 1442623SN/A 1452623SN/A /** Sets pointer to commit stage. Used only for initialization. */ 1462623SN/A void setCommitStage(Commit *commit_stage) 1472644Sstever@eecs.umich.edu { commit_ptr = commit_stage; } 1482623SN/A 1492644Sstever@eecs.umich.edu private: 1502623SN/A /** Pointer to IEW stage. Used only for initialization. */ 1512623SN/A IEW *iew_ptr; 1522623SN/A 153 /** Pointer to commit stage. Used only for initialization. */ 154 Commit *commit_ptr; 155 156 public: 157 /** Initializes variables for the stage. */ 158 void startupStage(); 159 160 /** Sets pointer to list of active threads. */ 161 void setActiveThreads(std::list<ThreadID> *at_ptr); 162 163 /** Sets pointer to rename maps (per-thread structures). */ 164 void setRenameMap(RenameMap rm_ptr[Impl::MaxThreads]); 165 166 /** Sets pointer to the free list. */ 167 void setFreeList(FreeList *fl_ptr); 168 169 /** Sets pointer to the scoreboard. */ 170 void setScoreboard(Scoreboard *_scoreboard); 171 172 /** Perform sanity checks after a drain. */ 173 void drainSanityCheck() const; 174 175 /** Has the stage drained? */ 176 bool isDrained() const; 177 178 /** Takes over from another CPU's thread. */ 179 void takeOverFrom(); 180 181 /** Squashes all instructions in a thread. */ 182 void squash(const InstSeqNum &squash_seq_num, ThreadID tid); 183 184 /** Ticks rename, which processes all input signals and attempts to rename 185 * as many instructions as possible. 186 */ 187 void tick(); 188 189 /** Debugging function used to dump history buffer of renamings. */ 190 void dumpHistory(); 191 192 private: 193 /** Reset this pipeline stage */ 194 void resetStage(); 195 196 /** Determines what to do based on rename's current status. 197 * @param status_change rename() sets this variable if there was a status 198 * change (ie switching from blocking to unblocking). 199 * @param tid Thread id to rename instructions from. 200 */ 201 void rename(bool &status_change, ThreadID tid); 202 203 /** Renames instructions for the given thread. Also handles serializing 204 * instructions. 205 */ 206 void renameInsts(ThreadID tid); 207 208 /** Inserts unused instructions from a given thread into the skid buffer, 209 * to be renamed once rename unblocks. 210 */ 211 void skidInsert(ThreadID tid); 212 213 /** Separates instructions from decode into individual lists of instructions 214 * sorted by thread. 215 */ 216 void sortInsts(); 217 218 /** Returns if all of the skid buffers are empty. */ 219 bool skidsEmpty(); 220 221 /** Updates overall rename status based on all of the threads' statuses. */ 222 void updateStatus(); 223 224 /** Switches rename to blocking, and signals back that rename has become 225 * blocked. 226 * @return Returns true if there is a status change. 227 */ 228 bool block(ThreadID tid); 229 230 /** Switches rename to unblocking if the skid buffer is empty, and signals 231 * back that rename has unblocked. 232 * @return Returns true if there is a status change. 233 */ 234 bool unblock(ThreadID tid); 235 236 /** Executes actual squash, removing squashed instructions. */ 237 void doSquash(const InstSeqNum &squash_seq_num, ThreadID tid); 238 239 /** Removes a committed instruction's rename history. */ 240 void removeFromHistory(InstSeqNum inst_seq_num, ThreadID tid); 241 242 /** Renames the source registers of an instruction. */ 243 inline void renameSrcRegs(DynInstPtr &inst, ThreadID tid); 244 245 /** Renames the destination registers of an instruction. */ 246 inline void renameDestRegs(DynInstPtr &inst, ThreadID tid); 247 248 /** Calculates the number of free ROB entries for a specific thread. */ 249 inline int calcFreeROBEntries(ThreadID tid); 250 251 /** Calculates the number of free IQ entries for a specific thread. */ 252 inline int calcFreeIQEntries(ThreadID tid); 253 254 /** Calculates the number of free LSQ entries for a specific thread. */ 255 inline int calcFreeLSQEntries(ThreadID tid); 256 257 /** Returns the number of valid instructions coming from decode. */ 258 unsigned validInsts(); 259 260 /** Reads signals telling rename to block/unblock. */ 261 void readStallSignals(ThreadID tid); 262 263 /** Checks if any stages are telling rename to block. */ 264 bool checkStall(ThreadID tid); 265 266 /** Gets the number of free entries for a specific thread. */ 267 void readFreeEntries(ThreadID tid); 268 269 /** Checks the signals and updates the status. */ 270 bool checkSignalsAndUpdate(ThreadID tid); 271 272 /** Either serializes on the next instruction available in the InstQueue, 273 * or records that it must serialize on the next instruction to enter 274 * rename. 275 * @param inst_list The list of younger, unprocessed instructions for the 276 * thread that has the serializeAfter instruction. 277 * @param tid The thread id. 278 */ 279 void serializeAfter(InstQueue &inst_list, ThreadID tid); 280 281 /** Holds the information for each destination register rename. It holds 282 * the instruction's sequence number, the arch register, the old physical 283 * register for that arch. register, and the new physical register. 284 */ 285 struct RenameHistory { 286 RenameHistory(InstSeqNum _instSeqNum, RegIndex _archReg, 287 PhysRegIndex _newPhysReg, PhysRegIndex _prevPhysReg) 288 : instSeqNum(_instSeqNum), archReg(_archReg), 289 newPhysReg(_newPhysReg), prevPhysReg(_prevPhysReg) 290 { 291 } 292 293 /** The sequence number of the instruction that renamed. */ 294 InstSeqNum instSeqNum; 295 /** The architectural register index that was renamed. */ 296 RegIndex archReg; 297 /** The new physical register that the arch. register is renamed to. */ 298 PhysRegIndex newPhysReg; 299 /** The old physical register that the arch. register was renamed to. */ 300 PhysRegIndex prevPhysReg; 301 }; 302 303 /** A per-thread list of all destination register renames, used to either 304 * undo rename mappings or free old physical registers. 305 */ 306 std::list<RenameHistory> historyBuffer[Impl::MaxThreads]; 307 308 /** Pointer to CPU. */ 309 O3CPU *cpu; 310 311 /** Pointer to main time buffer used for backwards communication. */ 312 TimeBuffer<TimeStruct> *timeBuffer; 313 314 /** Wire to get IEW's output from backwards time buffer. */ 315 typename TimeBuffer<TimeStruct>::wire fromIEW; 316 317 /** Wire to get commit's output from backwards time buffer. */ 318 typename TimeBuffer<TimeStruct>::wire fromCommit; 319 320 /** Wire to write infromation heading to previous stages. */ 321 typename TimeBuffer<TimeStruct>::wire toDecode; 322 323 /** Rename instruction queue. */ 324 TimeBuffer<RenameStruct> *renameQueue; 325 326 /** Wire to write any information heading to IEW. */ 327 typename TimeBuffer<RenameStruct>::wire toIEW; 328 329 /** Decode instruction queue interface. */ 330 TimeBuffer<DecodeStruct> *decodeQueue; 331 332 /** Wire to get decode's output from decode queue. */ 333 typename TimeBuffer<DecodeStruct>::wire fromDecode; 334 335 /** Queue of all instructions coming from decode this cycle. */ 336 InstQueue insts[Impl::MaxThreads]; 337 338 /** Skid buffer between rename and decode. */ 339 InstQueue skidBuffer[Impl::MaxThreads]; 340 341 /** Rename map interface. */ 342 RenameMap *renameMap[Impl::MaxThreads]; 343 344 /** Free list interface. */ 345 FreeList *freeList; 346 347 /** Pointer to the list of active threads. */ 348 std::list<ThreadID> *activeThreads; 349 350 /** Pointer to the scoreboard. */ 351 Scoreboard *scoreboard; 352 353 /** Count of instructions in progress that have been sent off to the IQ 354 * and ROB, but are not yet included in their occupancy counts. 355 */ 356 int instsInProgress[Impl::MaxThreads]; 357 358 /** Variable that tracks if decode has written to the time buffer this 359 * cycle. Used to tell CPU if there is activity this cycle. 360 */ 361 bool wroteToTimeBuffer; 362 363 /** Structures whose free entries impact the amount of instructions that 364 * can be renamed. 365 */ 366 struct FreeEntries { 367 unsigned iqEntries; 368 unsigned lsqEntries; 369 unsigned robEntries; 370 }; 371 372 /** Per-thread tracking of the number of free entries of back-end 373 * structures. 374 */ 375 FreeEntries freeEntries[Impl::MaxThreads]; 376 377 /** Records if the ROB is empty. In SMT mode the ROB may be dynamically 378 * partitioned between threads, so the ROB must tell rename when it is 379 * empty. 380 */ 381 bool emptyROB[Impl::MaxThreads]; 382 383 /** Source of possible stalls. */ 384 struct Stalls { 385 bool iew; 386 bool commit; 387 }; 388 389 /** Tracks which stages are telling decode to stall. */ 390 Stalls stalls[Impl::MaxThreads]; 391 392 /** The serialize instruction that rename has stalled on. */ 393 DynInstPtr serializeInst[Impl::MaxThreads]; 394 395 /** Records if rename needs to serialize on the next instruction for any 396 * thread. 397 */ 398 bool serializeOnNextInst[Impl::MaxThreads]; 399 400 /** Delay between iew and rename, in ticks. */ 401 int iewToRenameDelay; 402 403 /** Delay between decode and rename, in ticks. */ 404 int decodeToRenameDelay; 405 406 /** Delay between commit and rename, in ticks. */ 407 unsigned commitToRenameDelay; 408 409 /** Rename width, in instructions. */ 410 unsigned renameWidth; 411 412 /** Commit width, in instructions. Used so rename knows how many 413 * instructions might have freed registers in the previous cycle. 414 */ 415 unsigned commitWidth; 416 417 /** The index of the instruction in the time buffer to IEW that rename is 418 * currently using. 419 */ 420 unsigned toIEWIndex; 421 422 /** Whether or not rename needs to block this cycle. */ 423 bool blockThisCycle; 424 425 /** Whether or not rename needs to resume a serialize instruction 426 * after squashing. */ 427 bool resumeSerialize; 428 429 /** Whether or not rename needs to resume clearing out the skidbuffer 430 * after squashing. */ 431 bool resumeUnblocking; 432 433 /** The number of threads active in rename. */ 434 ThreadID numThreads; 435 436 /** The maximum skid buffer size. */ 437 unsigned skidBufferMax; 438 439 PhysRegIndex maxPhysicalRegs; 440 441 /** Enum to record the source of a structure full stall. Can come from 442 * either ROB, IQ, LSQ, and it is priortized in that order. 443 */ 444 enum FullSource { 445 ROB, 446 IQ, 447 LSQ, 448 NONE 449 }; 450 451 /** Function used to increment the stat that corresponds to the source of 452 * the stall. 453 */ 454 inline void incrFullStat(const FullSource &source); 455 456 /** Stat for total number of cycles spent squashing. */ 457 Stats::Scalar renameSquashCycles; 458 /** Stat for total number of cycles spent idle. */ 459 Stats::Scalar renameIdleCycles; 460 /** Stat for total number of cycles spent blocking. */ 461 Stats::Scalar renameBlockCycles; 462 /** Stat for total number of cycles spent stalling for a serializing inst. */ 463 Stats::Scalar renameSerializeStallCycles; 464 /** Stat for total number of cycles spent running normally. */ 465 Stats::Scalar renameRunCycles; 466 /** Stat for total number of cycles spent unblocking. */ 467 Stats::Scalar renameUnblockCycles; 468 /** Stat for total number of renamed instructions. */ 469 Stats::Scalar renameRenamedInsts; 470 /** Stat for total number of squashed instructions that rename discards. */ 471 Stats::Scalar renameSquashedInsts; 472 /** Stat for total number of times that the ROB starts a stall in rename. */ 473 Stats::Scalar renameROBFullEvents; 474 /** Stat for total number of times that the IQ starts a stall in rename. */ 475 Stats::Scalar renameIQFullEvents; 476 /** Stat for total number of times that the LSQ starts a stall in rename. */ 477 Stats::Scalar renameLSQFullEvents; 478 /** Stat for total number of times that rename runs out of free registers 479 * to use to rename. */ 480 Stats::Scalar renameFullRegistersEvents; 481 /** Stat for total number of renamed destination registers. */ 482 Stats::Scalar renameRenamedOperands; 483 /** Stat for total number of source register rename lookups. */ 484 Stats::Scalar renameRenameLookups; 485 Stats::Scalar intRenameLookups; 486 Stats::Scalar fpRenameLookups; 487 /** Stat for total number of committed renaming mappings. */ 488 Stats::Scalar renameCommittedMaps; 489 /** Stat for total number of mappings that were undone due to a squash. */ 490 Stats::Scalar renameUndoneMaps; 491 /** Number of serialize instructions handled. */ 492 Stats::Scalar renamedSerializing; 493 /** Number of instructions marked as temporarily serializing. */ 494 Stats::Scalar renamedTempSerializing; 495 /** Number of instructions inserted into skid buffers. */ 496 Stats::Scalar renameSkidInsts; 497}; 498 499#endif // __CPU_O3_RENAME_HH__ 500