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" |
| 16 | #include "llvm/Support/Debug.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | #define DEBUG_TYPE "llvm-mca" |
| 22 | |
| 23 | namespace mca { |
| 24 | |
| 25 | #ifndef NDEBUG |
| 26 | void LSUnit::dump() const { |
| 27 | dbgs() << "[LSUnit] LQ_Size = " << LQ_Size << '\n'; |
| 28 | dbgs() << "[LSUnit] SQ_Size = " << SQ_Size << '\n'; |
| 29 | dbgs() << "[LSUnit] NextLQSlotIdx = " << LoadQueue.size() << '\n'; |
| 30 | dbgs() << "[LSUnit] NextSQSlotIdx = " << StoreQueue.size() << '\n'; |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | void LSUnit::assignLQSlot(unsigned Index) { |
| 35 | assert(!isLQFull()); |
| 36 | assert(LoadQueue.count(Index) == 0); |
| 37 | |
| 38 | DEBUG(dbgs() << "[LSUnit] - AssignLQSlot <Idx=" << Index |
| 39 | << ",slot=" << LoadQueue.size() << ">\n"); |
| 40 | LoadQueue.insert(Index); |
| 41 | } |
| 42 | |
| 43 | void LSUnit::assignSQSlot(unsigned Index) { |
| 44 | assert(!isSQFull()); |
| 45 | assert(StoreQueue.count(Index) == 0); |
| 46 | |
| 47 | DEBUG(dbgs() << "[LSUnit] - AssignSQSlot <Idx=" << Index |
| 48 | << ",slot=" << StoreQueue.size() << ">\n"); |
| 49 | StoreQueue.insert(Index); |
| 50 | } |
| 51 | |
| 52 | bool LSUnit::isReady(unsigned Index) const { |
| 53 | bool IsALoad = LoadQueue.count(Index) != 0; |
| 54 | bool IsAStore = StoreQueue.count(Index) != 0; |
| 55 | unsigned LoadBarrierIndex = LoadBarriers.empty() ? 0 : *LoadBarriers.begin(); |
| 56 | unsigned StoreBarrierIndex = StoreBarriers.empty() ? 0 : *StoreBarriers.begin(); |
| 57 | |
| 58 | if (IsALoad && LoadBarrierIndex) { |
| 59 | if (Index > LoadBarrierIndex) |
| 60 | return false; |
| 61 | if (Index == LoadBarrierIndex && Index != *LoadQueue.begin()) |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (IsAStore && StoreBarrierIndex) { |
| 66 | if (Index > StoreBarrierIndex) |
| 67 | return false; |
| 68 | if (Index == StoreBarrierIndex && Index != *StoreQueue.begin()) |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | if (NoAlias && IsALoad) |
| 73 | return true; |
| 74 | |
| 75 | if (StoreQueue.size()) { |
| 76 | // Check if this memory operation is younger than the older store. |
| 77 | if (Index > *StoreQueue.begin()) |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // Okay, we are older than the oldest store in the queue. |
| 82 | // If there are no pending loads, then we can say for sure that this |
| 83 | // instruction is ready. |
| 84 | if (isLQEmpty()) |
| 85 | return true; |
| 86 | |
| 87 | // Check if there are no older loads. |
| 88 | if (Index <= *LoadQueue.begin()) |
| 89 | return true; |
| 90 | |
| 91 | // There is at least one younger load. |
| 92 | return !IsAStore; |
| 93 | } |
| 94 | |
| 95 | void LSUnit::onInstructionExecuted(unsigned Index) { |
| 96 | std::set<unsigned>::iterator it = LoadQueue.find(Index); |
| 97 | if (it != LoadQueue.end()) { |
| 98 | DEBUG(dbgs() << "[LSUnit]: Instruction idx=" << Index |
| 99 | << " has been removed from the load queue.\n"); |
| 100 | LoadQueue.erase(it); |
| 101 | } |
| 102 | |
| 103 | it = StoreQueue.find(Index); |
| 104 | if (it != StoreQueue.end()) { |
| 105 | DEBUG(dbgs() << "[LSUnit]: Instruction idx=" << Index |
| 106 | << " has been removed from the store queue.\n"); |
| 107 | StoreQueue.erase(it); |
| 108 | } |
| 109 | |
| 110 | if (!StoreBarriers.empty() && Index == *StoreBarriers.begin()) |
| 111 | StoreBarriers.erase(StoreBarriers.begin()); |
| 112 | if (!LoadBarriers.empty() && Index == *LoadBarriers.begin()) |
| 113 | LoadBarriers.erase(LoadBarriers.begin()); |
| 114 | } |
| 115 | } // namespace mca |