Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 1 | //===-- HexagonHazardRecognizer.cpp - Hexagon Post RA Hazard Recognizer ---===// |
| 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 | // |
| 10 | // This file defines the hazard recognizer for scheduling on Hexagon. |
| 11 | // Use a DFA based hazard recognizer. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "HexagonHazardRecognizer.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineOperand.h" |
| 19 | #include "llvm/CodeGen/ScheduleDAG.h" |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 58655bb | 2016-12-17 01:09:05 +0000 | [diff] [blame] | 22 | #include <cassert> |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | #define DEBUG_TYPE "post-RA-sched" |
| 27 | |
| 28 | void HexagonHazardRecognizer::Reset() { |
| 29 | DEBUG(dbgs() << "Reset hazard recognizer\n"); |
| 30 | Resources->clearResources(); |
| 31 | PacketNum = 0; |
| 32 | UsesDotCur = nullptr; |
| 33 | DotCurPNum = -1; |
Krzysztof Parzyszek | f81a8d0 | 2018-03-16 20:55:49 +0000 | [diff] [blame] | 34 | UsesLoad = false; |
Krzysztof Parzyszek | 5ffd808 | 2018-03-20 14:54:01 +0000 | [diff] [blame^] | 35 | PrefVectorStoreNew = nullptr; |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 36 | RegDefs.clear(); |
| 37 | } |
| 38 | |
| 39 | ScheduleHazardRecognizer::HazardType |
| 40 | HexagonHazardRecognizer::getHazardType(SUnit *SU, int stalls) { |
| 41 | MachineInstr *MI = SU->getInstr(); |
| 42 | if (!MI || TII->isZeroCost(MI->getOpcode())) |
| 43 | return NoHazard; |
| 44 | |
| 45 | if (!Resources->canReserveResources(*MI)) { |
| 46 | DEBUG(dbgs() << "*** Hazard in cycle " << PacketNum << ", " << *MI); |
| 47 | HazardType RetVal = Hazard; |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 48 | if (TII->mayBeNewStore(*MI)) { |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 49 | // Make sure the register to be stored is defined by an instruction in the |
| 50 | // packet. |
| 51 | MachineOperand &MO = MI->getOperand(MI->getNumOperands() - 1); |
| 52 | if (!MO.isReg() || RegDefs.count(MO.getReg()) == 0) |
| 53 | return Hazard; |
| 54 | // The .new store version uses different resources so check if it |
| 55 | // causes a hazard. |
| 56 | MachineFunction *MF = MI->getParent()->getParent(); |
| 57 | MachineInstr *NewMI = |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 58 | MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)), |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 59 | MI->getDebugLoc()); |
| 60 | if (Resources->canReserveResources(*NewMI)) |
| 61 | RetVal = NoHazard; |
| 62 | DEBUG(dbgs() << "*** Try .new version? " << (RetVal == NoHazard) << "\n"); |
| 63 | MF->DeleteMachineInstr(NewMI); |
| 64 | } |
| 65 | return RetVal; |
| 66 | } |
| 67 | |
| 68 | if (SU == UsesDotCur && DotCurPNum != (int)PacketNum) { |
| 69 | DEBUG(dbgs() << "*** .cur Hazard in cycle " << PacketNum << ", " << *MI); |
| 70 | return Hazard; |
| 71 | } |
| 72 | |
| 73 | return NoHazard; |
| 74 | } |
| 75 | |
| 76 | void HexagonHazardRecognizer::AdvanceCycle() { |
| 77 | DEBUG(dbgs() << "Advance cycle, clear state\n"); |
| 78 | Resources->clearResources(); |
| 79 | if (DotCurPNum != -1 && DotCurPNum != (int)PacketNum) { |
| 80 | UsesDotCur = nullptr; |
| 81 | DotCurPNum = -1; |
| 82 | } |
Krzysztof Parzyszek | f81a8d0 | 2018-03-16 20:55:49 +0000 | [diff] [blame] | 83 | UsesLoad = false; |
Krzysztof Parzyszek | 5ffd808 | 2018-03-20 14:54:01 +0000 | [diff] [blame^] | 84 | PrefVectorStoreNew = nullptr; |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 85 | PacketNum++; |
| 86 | RegDefs.clear(); |
| 87 | } |
| 88 | |
Krzysztof Parzyszek | f81a8d0 | 2018-03-16 20:55:49 +0000 | [diff] [blame] | 89 | /// Handle the cases when we prefer one instruction over another. Case 1 - we |
| 90 | /// prefer not to generate multiple loads in the packet to avoid a potential |
| 91 | /// bank conflict. Case 2 - if a packet contains a dot cur instruction, then we |
| 92 | /// prefer the instruction that can use the dot cur result. However, if the use |
| 93 | /// is not scheduled in the same packet, then prefer other instructions in the |
Krzysztof Parzyszek | 5ffd808 | 2018-03-20 14:54:01 +0000 | [diff] [blame^] | 94 | /// subsequent packet. Case 3 - we prefer a vector store that can be converted |
| 95 | /// to a .new store. The packetizer will not generate the .new store if the |
| 96 | /// store doesn't have resources to fit in the packet (but the .new store may |
| 97 | /// have resources). We attempt to schedule the store as soon as possible to |
| 98 | /// help packetize the two instructions together. |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 99 | bool HexagonHazardRecognizer::ShouldPreferAnother(SUnit *SU) { |
Krzysztof Parzyszek | 5ffd808 | 2018-03-20 14:54:01 +0000 | [diff] [blame^] | 100 | if (PrefVectorStoreNew != nullptr && PrefVectorStoreNew != SU) |
| 101 | return true; |
Krzysztof Parzyszek | f81a8d0 | 2018-03-16 20:55:49 +0000 | [diff] [blame] | 102 | if (UsesLoad && SU->isInstr() && SU->getInstr()->mayLoad()) |
| 103 | return true; |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 104 | return UsesDotCur && ((SU == UsesDotCur) ^ (DotCurPNum == (int)PacketNum)); |
| 105 | } |
| 106 | |
| 107 | void HexagonHazardRecognizer::EmitInstruction(SUnit *SU) { |
| 108 | MachineInstr *MI = SU->getInstr(); |
| 109 | if (!MI) |
| 110 | return; |
| 111 | |
| 112 | // Keep the set of definitions for each packet, which is used to determine |
| 113 | // if a .new can be used. |
Matthias Braun | fc37155 | 2016-10-24 21:36:43 +0000 | [diff] [blame] | 114 | for (const MachineOperand &MO : MI->operands()) |
| 115 | if (MO.isReg() && MO.isDef() && !MO.isImplicit()) |
| 116 | RegDefs.insert(MO.getReg()); |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 117 | |
| 118 | if (TII->isZeroCost(MI->getOpcode())) |
| 119 | return; |
| 120 | |
| 121 | if (!Resources->canReserveResources(*MI)) { |
| 122 | // It must be a .new store since other instructions must be able to be |
| 123 | // reserved at this point. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 124 | assert(TII->mayBeNewStore(*MI) && "Expecting .new store"); |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 125 | MachineFunction *MF = MI->getParent()->getParent(); |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 126 | MachineInstr *NewMI = |
| 127 | MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)), |
| 128 | MI->getDebugLoc()); |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 129 | assert(Resources->canReserveResources(*NewMI)); |
| 130 | Resources->reserveResources(*NewMI); |
| 131 | MF->DeleteMachineInstr(NewMI); |
| 132 | } |
| 133 | else |
| 134 | Resources->reserveResources(*MI); |
| 135 | DEBUG(dbgs() << " Add instruction " << *MI); |
| 136 | |
| 137 | // When scheduling a dot cur instruction, check if there is an instruction |
| 138 | // that can use the dot cur in the same packet. If so, we'll attempt to |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 139 | // schedule it before other instructions. We only do this if the load has a |
| 140 | // single zero-latency use. |
Krzysztof Parzyszek | f0b34a5 | 2016-07-29 21:49:42 +0000 | [diff] [blame] | 141 | if (TII->mayBeCurLoad(*MI)) |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 142 | for (auto &S : SU->Succs) |
| 143 | if (S.isAssignedRegDep() && S.getLatency() == 0 && |
Krzysztof Parzyszek | dca3831 | 2018-03-20 12:28:43 +0000 | [diff] [blame] | 144 | S.getSUnit()->NumPredsLeft == 1) { |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 145 | UsesDotCur = S.getSUnit(); |
| 146 | DotCurPNum = PacketNum; |
| 147 | break; |
| 148 | } |
| 149 | if (SU == UsesDotCur) { |
| 150 | UsesDotCur = nullptr; |
| 151 | DotCurPNum = -1; |
| 152 | } |
Krzysztof Parzyszek | f81a8d0 | 2018-03-16 20:55:49 +0000 | [diff] [blame] | 153 | |
| 154 | UsesLoad = MI->mayLoad(); |
Krzysztof Parzyszek | 5ffd808 | 2018-03-20 14:54:01 +0000 | [diff] [blame^] | 155 | |
| 156 | if (TII->isHVXVec(*MI) && !MI->mayLoad() && !MI->mayStore()) |
| 157 | for (auto &S : SU->Succs) |
| 158 | if (S.isAssignedRegDep() && S.getLatency() == 0 && |
| 159 | TII->mayBeNewStore(*S.getSUnit()->getInstr()) && |
| 160 | Resources->canReserveResources(*S.getSUnit()->getInstr())) { |
| 161 | PrefVectorStoreNew = S.getSUnit(); |
| 162 | break; |
| 163 | } |
Krzysztof Parzyszek | 0005a72 | 2016-07-29 13:59:55 +0000 | [diff] [blame] | 164 | } |