blob: 8f543eeb8c2a0d918fb2987a266fbd30425581e9 [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
Matt Davis271ce762018-08-27 17:16:32 +000015#include "HardwareUnits/RetireControlUnit.h"
Matt Davis6aa5dcd2018-05-01 23:04:01 +000016#include "llvm/Support/Debug.h"
17
18using namespace llvm;
19
20#define DEBUG_TYPE "llvm-mca"
21
22namespace mca {
23
Andrea Di Biagioa7699122018-09-28 10:47:24 +000024RetireControlUnit::RetireControlUnit(const MCSchedModel &SM)
Matt Davis6aa5dcd2018-05-01 23:04:01 +000025 : NextAvailableSlotIdx(0), CurrentInstructionSlotIdx(0),
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000026 AvailableSlots(SM.MicroOpBufferSize), MaxRetirePerCycle(0) {
Matt Davis6aa5dcd2018-05-01 23:04:01 +000027 // Check if the scheduling model provides extra information about the machine
28 // processor. If so, then use that information to set the reorder buffer size
29 // and the maximum number of instructions retired per cycle.
30 if (SM.hasExtraProcessorInfo()) {
31 const MCExtraProcessorInfo &EPI = SM.getExtraProcessorInfo();
32 if (EPI.ReorderBufferSize)
33 AvailableSlots = EPI.ReorderBufferSize;
34 MaxRetirePerCycle = EPI.MaxRetirePerCycle;
35 }
36
37 assert(AvailableSlots && "Invalid reorder buffer size!");
38 Queue.resize(AvailableSlots);
39}
40
41// Reserves a number of slots, and returns a new token.
Andrea Di Biagio904684c2018-05-15 10:30:39 +000042unsigned RetireControlUnit::reserveSlot(const InstRef &IR,
43 unsigned NumMicroOps) {
Andrea Di Biagioaacd5e12018-10-04 10:36:49 +000044 assert(isAvailable(NumMicroOps) && "Reorder Buffer unavailable!");
Matt Davis6aa5dcd2018-05-01 23:04:01 +000045 unsigned NormalizedQuantity =
46 std::min(NumMicroOps, static_cast<unsigned>(Queue.size()));
Andrea Di Biagioaacd5e12018-10-04 10:36:49 +000047 // Zero latency instructions may have zero uOps. Artificially bump this
Matt Davis6aa5dcd2018-05-01 23:04:01 +000048 // value to 1. Although zero latency instructions don't consume scheduler
49 // resources, they still consume one slot in the retire queue.
50 NormalizedQuantity = std::max(NormalizedQuantity, 1U);
51 unsigned TokenID = NextAvailableSlotIdx;
Matt Davis21a8d322018-05-07 18:29:15 +000052 Queue[NextAvailableSlotIdx] = {IR, NormalizedQuantity, false};
Matt Davis6aa5dcd2018-05-01 23:04:01 +000053 NextAvailableSlotIdx += NormalizedQuantity;
54 NextAvailableSlotIdx %= Queue.size();
55 AvailableSlots -= NormalizedQuantity;
56 return TokenID;
57}
58
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000059const RetireControlUnit::RUToken &RetireControlUnit::peekCurrentToken() const {
60 return Queue[CurrentInstructionSlotIdx];
61}
Matt Davis6aa5dcd2018-05-01 23:04:01 +000062
Matt Davis5b79ffc5b2018-05-25 18:00:25 +000063void RetireControlUnit::consumeCurrentToken() {
64 const RetireControlUnit::RUToken &Current = peekCurrentToken();
65 assert(Current.NumSlots && "Reserved zero slots?");
Matt Davisb5d5deb2018-10-24 20:27:47 +000066 assert(Current.IR && "Invalid RUToken in the RCU queue.");
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