blob: de9f24552c38be4190bd364fc3f7b17868ae53c8 [file] [log] [blame]
Andrea Di Biagioe2492c82018-05-15 09:31:32 +00001//===---------------------- RetireControlUnit.cpp ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000011/// This file simulates the hardware responsible for retiring instructions.
Andrea Di Biagioe2492c82018-05-15 09:31:32 +000012///
13//===----------------------------------------------------------------------===//
14
Clement Courbetcc5e6a72018-12-17 08:08:31 +000015#include "llvm/MCA/HardwareUnits/RetireControlUnit.h"
Matt Davis6aa5dcd2018-05-01 23:04:01 +000016#include "llvm/Support/Debug.h"
17
Matt Davis6aa5dcd2018-05-01 23:04:01 +000018#define DEBUG_TYPE "llvm-mca"
19
Fangrui Song5a8fd652018-10-30 15:56:08 +000020namespace llvm {
Matt Davis6aa5dcd2018-05-01 23:04:01 +000021namespace mca {
22
Andrea Di Biagioa7699122018-09-28 10:47:24 +000023RetireControlUnit::RetireControlUnit(const MCSchedModel &SM)
Matt Davis6aa5dcd2018-05-01 23:04:01 +000024 : NextAvailableSlotIdx(0), CurrentInstructionSlotIdx(0),
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000025 AvailableSlots(SM.MicroOpBufferSize), MaxRetirePerCycle(0) {
Matt Davis6aa5dcd2018-05-01 23:04:01 +000026 // Check if the scheduling model provides extra information about the machine
27 // processor. If so, then use that information to set the reorder buffer size
28 // and the maximum number of instructions retired per cycle.
29 if (SM.hasExtraProcessorInfo()) {
30 const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo();
31 if (EPI.ReorderBufferSize)
32 AvailableSlots = EPI.ReorderBufferSize;
33 MaxRetirePerCycle = EPI.MaxRetirePerCycle;
34 }
35
36 assert(AvailableSlots && "Invalid reorder buffer size!");
37 Queue.resize(AvailableSlots);
38}
39
40// Reserves a number of slots, and returns a new token.
Andrea Di Biagio904684c2018-05-15 10:30:39 +000041unsigned RetireControlUnit::reserveSlot(const InstRef &IR,
42 unsigned NumMicroOps) {
Andrea Di Biagioaacd5e12018-10-04 10:36:49 +000043 assert(isAvailable(NumMicroOps) && "Reorder Buffer unavailable!");
Matt Davis6aa5dcd2018-05-01 23:04:01 +000044 unsigned NormalizedQuantity =
45 std::min(NumMicroOps, static_cast<unsigned>(Queue.size()));
Andrea Di Biagioaacd5e12018-10-04 10:36:49 +000046 // Zero latency instructions may have zero uOps. Artificially bump this
Matt Davis6aa5dcd2018-05-01 23:04:01 +000047 // value to 1. Although zero latency instructions don't consume scheduler
48 // resources, they still consume one slot in the retire queue.
49 NormalizedQuantity = std::max(NormalizedQuantity, 1U);
50 unsigned TokenID = NextAvailableSlotIdx;
Matt Davis21a8d322018-05-07 18:29:15 +000051 Queue[NextAvailableSlotIdx] = {IR, NormalizedQuantity, false};
Matt Davis6aa5dcd2018-05-01 23:04:01 +000052 NextAvailableSlotIdx += NormalizedQuantity;
53 NextAvailableSlotIdx %= Queue.size();
54 AvailableSlots -= NormalizedQuantity;
55 return TokenID;
56}
57
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000058const RetireControlUnit::RUToken &RetireControlUnit::peekCurrentToken() const {
59 return Queue[CurrentInstructionSlotIdx];
60}
Matt Davis6aa5dcd2018-05-01 23:04:01 +000061
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000062void RetireControlUnit::consumeCurrentToken() {
Andrea Di Biagiodffec122018-11-09 12:29:57 +000063 RetireControlUnit::RUToken &Current = Queue[CurrentInstructionSlotIdx];
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000064 assert(Current.NumSlots && "Reserved zero slots?");
Matt Davisb5d5deb2018-10-24 20:27:47 +000065 assert(Current.IR && "Invalid RUToken in the RCU queue.");
Andrea Di Biagiodffec122018-11-09 12:29:57 +000066 Current.IR.getInstruction()->retire();
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000067
68 // Update the slot index to be the next item in the circular queue.
69 CurrentInstructionSlotIdx += Current.NumSlots;
70 CurrentInstructionSlotIdx %= Queue.size();
71 AvailableSlots += Current.NumSlots;
Matt Davis6aa5dcd2018-05-01 23:04:01 +000072}
73
74void RetireControlUnit::onInstructionExecuted(unsigned TokenID) {
75 assert(Queue.size() > TokenID);
Matt Davisb5d5deb2018-10-24 20:27:47 +000076 assert(Queue[TokenID].Executed == false && Queue[TokenID].IR);
Matt Davis6aa5dcd2018-05-01 23:04:01 +000077 Queue[TokenID].Executed = true;
78}
79
80#ifndef NDEBUG
81void RetireControlUnit::dump() const {
82 dbgs() << "Retire Unit: { Total Slots=" << Queue.size()
83 << ", Available Slots=" << AvailableSlots << " }\n";
84}
85#endif
86
87} // namespace mca
Fangrui Song5a8fd652018-10-30 15:56:08 +000088} // namespace llvm