blob: 115ef8f429e23279d0d4f5f871ed0b667dcd670d [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===----------------------- LSUnit.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///
11/// A Load-Store Unit for the llvm-mca tool.
12///
13//===----------------------------------------------------------------------===//
14
Andrea Di Biagio2dee62b2018-03-22 14:14:49 +000015#include "Instruction.h"
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000016#include "LSUnit.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "llvm-mca"
23
24namespace mca {
25
26#ifndef NDEBUG
27void LSUnit::dump() const {
28 dbgs() << "[LSUnit] LQ_Size = " << LQ_Size << '\n';
29 dbgs() << "[LSUnit] SQ_Size = " << SQ_Size << '\n';
30 dbgs() << "[LSUnit] NextLQSlotIdx = " << LoadQueue.size() << '\n';
31 dbgs() << "[LSUnit] NextSQSlotIdx = " << StoreQueue.size() << '\n';
32}
33#endif
34
35void LSUnit::assignLQSlot(unsigned Index) {
36 assert(!isLQFull());
37 assert(LoadQueue.count(Index) == 0);
38
39 DEBUG(dbgs() << "[LSUnit] - AssignLQSlot <Idx=" << Index
40 << ",slot=" << LoadQueue.size() << ">\n");
41 LoadQueue.insert(Index);
42}
43
44void LSUnit::assignSQSlot(unsigned Index) {
45 assert(!isSQFull());
46 assert(StoreQueue.count(Index) == 0);
47
48 DEBUG(dbgs() << "[LSUnit] - AssignSQSlot <Idx=" << Index
49 << ",slot=" << StoreQueue.size() << ">\n");
50 StoreQueue.insert(Index);
51}
52
Andrea Di Biagio2dee62b2018-03-22 14:14:49 +000053bool LSUnit::reserve(unsigned Index, const InstrDesc &Desc) {
54 unsigned MayLoad = Desc.MayLoad;
55 unsigned MayStore = Desc.MayStore;
56 unsigned IsMemBarrier = Desc.HasSideEffects;
57 if (!MayLoad && !MayStore)
58 return false;
59
60 if (MayLoad) {
61 if (IsMemBarrier)
62 LoadBarriers.insert(Index);
63 assignLQSlot(Index);
64 }
65 if (MayStore) {
66 if (IsMemBarrier)
67 StoreBarriers.insert(Index);
68 assignSQSlot(Index);
69 }
70 return true;
71}
72
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000073bool LSUnit::isReady(unsigned Index) const {
74 bool IsALoad = LoadQueue.count(Index) != 0;
75 bool IsAStore = StoreQueue.count(Index) != 0;
Andrea Di Biagio2dee62b2018-03-22 14:14:49 +000076 assert((IsALoad || IsAStore) && "Instruction is not in queue!");
77
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000078 unsigned LoadBarrierIndex = LoadBarriers.empty() ? 0 : *LoadBarriers.begin();
79 unsigned StoreBarrierIndex = StoreBarriers.empty() ? 0 : *StoreBarriers.begin();
80
81 if (IsALoad && LoadBarrierIndex) {
82 if (Index > LoadBarrierIndex)
83 return false;
84 if (Index == LoadBarrierIndex && Index != *LoadQueue.begin())
85 return false;
86 }
87
88 if (IsAStore && StoreBarrierIndex) {
89 if (Index > StoreBarrierIndex)
90 return false;
91 if (Index == StoreBarrierIndex && Index != *StoreQueue.begin())
92 return false;
93 }
94
95 if (NoAlias && IsALoad)
96 return true;
97
98 if (StoreQueue.size()) {
99 // Check if this memory operation is younger than the older store.
100 if (Index > *StoreQueue.begin())
101 return false;
102 }
103
104 // Okay, we are older than the oldest store in the queue.
105 // If there are no pending loads, then we can say for sure that this
106 // instruction is ready.
107 if (isLQEmpty())
108 return true;
109
110 // Check if there are no older loads.
111 if (Index <= *LoadQueue.begin())
112 return true;
113
114 // There is at least one younger load.
115 return !IsAStore;
116}
117
118void LSUnit::onInstructionExecuted(unsigned Index) {
119 std::set<unsigned>::iterator it = LoadQueue.find(Index);
120 if (it != LoadQueue.end()) {
121 DEBUG(dbgs() << "[LSUnit]: Instruction idx=" << Index
122 << " has been removed from the load queue.\n");
123 LoadQueue.erase(it);
124 }
125
126 it = StoreQueue.find(Index);
127 if (it != StoreQueue.end()) {
128 DEBUG(dbgs() << "[LSUnit]: Instruction idx=" << Index
129 << " has been removed from the store queue.\n");
130 StoreQueue.erase(it);
131 }
132
133 if (!StoreBarriers.empty() && Index == *StoreBarriers.begin())
134 StoreBarriers.erase(StoreBarriers.begin());
135 if (!LoadBarriers.empty() && Index == *LoadBarriers.begin())
136 LoadBarriers.erase(LoadBarriers.begin());
137}
138} // namespace mca