Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===----------------------- 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 | |
| 15 | #include "LSUnit.h" |
Andrea Di Biagio | 94fafdf | 2018-03-24 16:05:36 +0000 | [diff] [blame^] | 16 | #include "Instruction.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | #define DEBUG_TYPE "llvm-mca" |
| 23 | |
| 24 | namespace mca { |
| 25 | |
| 26 | #ifndef NDEBUG |
| 27 | void 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 | |
| 35 | void 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 | |
| 44 | void 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 Biagio | 2dee62b | 2018-03-22 14:14:49 +0000 | [diff] [blame] | 53 | bool 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 Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 73 | bool LSUnit::isReady(unsigned Index) const { |
| 74 | bool IsALoad = LoadQueue.count(Index) != 0; |
| 75 | bool IsAStore = StoreQueue.count(Index) != 0; |
Andrea Di Biagio | 2dee62b | 2018-03-22 14:14:49 +0000 | [diff] [blame] | 76 | assert((IsALoad || IsAStore) && "Instruction is not in queue!"); |
| 77 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 78 | unsigned LoadBarrierIndex = LoadBarriers.empty() ? 0 : *LoadBarriers.begin(); |
Andrea Di Biagio | 94fafdf | 2018-03-24 16:05:36 +0000 | [diff] [blame^] | 79 | unsigned StoreBarrierIndex = |
| 80 | StoreBarriers.empty() ? 0 : *StoreBarriers.begin(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 81 | |
| 82 | if (IsALoad && LoadBarrierIndex) { |
| 83 | if (Index > LoadBarrierIndex) |
| 84 | return false; |
| 85 | if (Index == LoadBarrierIndex && Index != *LoadQueue.begin()) |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | if (IsAStore && StoreBarrierIndex) { |
| 90 | if (Index > StoreBarrierIndex) |
| 91 | return false; |
| 92 | if (Index == StoreBarrierIndex && Index != *StoreQueue.begin()) |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | if (NoAlias && IsALoad) |
| 97 | return true; |
| 98 | |
| 99 | if (StoreQueue.size()) { |
| 100 | // Check if this memory operation is younger than the older store. |
| 101 | if (Index > *StoreQueue.begin()) |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | // Okay, we are older than the oldest store in the queue. |
| 106 | // If there are no pending loads, then we can say for sure that this |
| 107 | // instruction is ready. |
| 108 | if (isLQEmpty()) |
| 109 | return true; |
| 110 | |
| 111 | // Check if there are no older loads. |
| 112 | if (Index <= *LoadQueue.begin()) |
| 113 | return true; |
| 114 | |
| 115 | // There is at least one younger load. |
| 116 | return !IsAStore; |
| 117 | } |
| 118 | |
| 119 | void LSUnit::onInstructionExecuted(unsigned Index) { |
| 120 | std::set<unsigned>::iterator it = LoadQueue.find(Index); |
| 121 | if (it != LoadQueue.end()) { |
| 122 | DEBUG(dbgs() << "[LSUnit]: Instruction idx=" << Index |
| 123 | << " has been removed from the load queue.\n"); |
| 124 | LoadQueue.erase(it); |
| 125 | } |
| 126 | |
| 127 | it = StoreQueue.find(Index); |
| 128 | if (it != StoreQueue.end()) { |
| 129 | DEBUG(dbgs() << "[LSUnit]: Instruction idx=" << Index |
| 130 | << " has been removed from the store queue.\n"); |
| 131 | StoreQueue.erase(it); |
| 132 | } |
| 133 | |
| 134 | if (!StoreBarriers.empty() && Index == *StoreBarriers.begin()) |
| 135 | StoreBarriers.erase(StoreBarriers.begin()); |
| 136 | if (!LoadBarriers.empty() && Index == *LoadBarriers.begin()) |
| 137 | LoadBarriers.erase(LoadBarriers.begin()); |
| 138 | } |
| 139 | } // namespace mca |