blob: 44f1f554c662d2efb0e79bb6b9a95e80c60103d3 [file] [log] [blame]
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +00001//===-- 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 Zelenko58655bb2016-12-17 01:09:05 +000016#include "llvm/CodeGen/MachineFunction.h"
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000017#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000018#include "llvm/CodeGen/MachineOperand.h"
19#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000022#include <cassert>
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000023
24using namespace llvm;
25
26#define DEBUG_TYPE "post-RA-sched"
27
28void HexagonHazardRecognizer::Reset() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000029 LLVM_DEBUG(dbgs() << "Reset hazard recognizer\n");
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000030 Resources->clearResources();
31 PacketNum = 0;
32 UsesDotCur = nullptr;
33 DotCurPNum = -1;
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +000034 UsesLoad = false;
Krzysztof Parzyszek5ffd8082018-03-20 14:54:01 +000035 PrefVectorStoreNew = nullptr;
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000036 RegDefs.clear();
37}
38
39ScheduleHazardRecognizer::HazardType
40HexagonHazardRecognizer::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)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000046 LLVM_DEBUG(dbgs() << "*** Hazard in cycle " << PacketNum << ", " << *MI);
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000047 HazardType RetVal = Hazard;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +000048 if (TII->mayBeNewStore(*MI)) {
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000049 // 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 Parzyszekf0b34a52016-07-29 21:49:42 +000058 MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)),
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000059 MI->getDebugLoc());
60 if (Resources->canReserveResources(*NewMI))
61 RetVal = NoHazard;
Nicola Zaghend34e60c2018-05-14 12:53:11 +000062 LLVM_DEBUG(dbgs() << "*** Try .new version? " << (RetVal == NoHazard)
63 << "\n");
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000064 MF->DeleteMachineInstr(NewMI);
65 }
66 return RetVal;
67 }
68
69 if (SU == UsesDotCur && DotCurPNum != (int)PacketNum) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000070 LLVM_DEBUG(dbgs() << "*** .cur Hazard in cycle " << PacketNum << ", "
71 << *MI);
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000072 return Hazard;
73 }
74
75 return NoHazard;
76}
77
78void HexagonHazardRecognizer::AdvanceCycle() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000079 LLVM_DEBUG(dbgs() << "Advance cycle, clear state\n");
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000080 Resources->clearResources();
81 if (DotCurPNum != -1 && DotCurPNum != (int)PacketNum) {
82 UsesDotCur = nullptr;
83 DotCurPNum = -1;
84 }
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +000085 UsesLoad = false;
Krzysztof Parzyszek5ffd8082018-03-20 14:54:01 +000086 PrefVectorStoreNew = nullptr;
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000087 PacketNum++;
88 RegDefs.clear();
89}
90
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +000091/// Handle the cases when we prefer one instruction over another. Case 1 - we
92/// prefer not to generate multiple loads in the packet to avoid a potential
93/// bank conflict. Case 2 - if a packet contains a dot cur instruction, then we
94/// prefer the instruction that can use the dot cur result. However, if the use
95/// is not scheduled in the same packet, then prefer other instructions in the
Krzysztof Parzyszek5ffd8082018-03-20 14:54:01 +000096/// subsequent packet. Case 3 - we prefer a vector store that can be converted
97/// to a .new store. The packetizer will not generate the .new store if the
98/// store doesn't have resources to fit in the packet (but the .new store may
99/// have resources). We attempt to schedule the store as soon as possible to
100/// help packetize the two instructions together.
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000101bool HexagonHazardRecognizer::ShouldPreferAnother(SUnit *SU) {
Krzysztof Parzyszek5ffd8082018-03-20 14:54:01 +0000102 if (PrefVectorStoreNew != nullptr && PrefVectorStoreNew != SU)
103 return true;
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +0000104 if (UsesLoad && SU->isInstr() && SU->getInstr()->mayLoad())
105 return true;
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000106 return UsesDotCur && ((SU == UsesDotCur) ^ (DotCurPNum == (int)PacketNum));
107}
108
109void HexagonHazardRecognizer::EmitInstruction(SUnit *SU) {
110 MachineInstr *MI = SU->getInstr();
111 if (!MI)
112 return;
113
114 // Keep the set of definitions for each packet, which is used to determine
115 // if a .new can be used.
Matthias Braunfc371552016-10-24 21:36:43 +0000116 for (const MachineOperand &MO : MI->operands())
117 if (MO.isReg() && MO.isDef() && !MO.isImplicit())
118 RegDefs.insert(MO.getReg());
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000119
120 if (TII->isZeroCost(MI->getOpcode()))
121 return;
122
123 if (!Resources->canReserveResources(*MI)) {
124 // It must be a .new store since other instructions must be able to be
125 // reserved at this point.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000126 assert(TII->mayBeNewStore(*MI) && "Expecting .new store");
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000127 MachineFunction *MF = MI->getParent()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000128 MachineInstr *NewMI =
129 MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)),
130 MI->getDebugLoc());
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000131 assert(Resources->canReserveResources(*NewMI));
132 Resources->reserveResources(*NewMI);
133 MF->DeleteMachineInstr(NewMI);
134 }
135 else
136 Resources->reserveResources(*MI);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000137 LLVM_DEBUG(dbgs() << " Add instruction " << *MI);
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000138
139 // When scheduling a dot cur instruction, check if there is an instruction
140 // that can use the dot cur in the same packet. If so, we'll attempt to
Krzysztof Parzyszekdca38312018-03-20 12:28:43 +0000141 // schedule it before other instructions. We only do this if the load has a
142 // single zero-latency use.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000143 if (TII->mayBeCurLoad(*MI))
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000144 for (auto &S : SU->Succs)
145 if (S.isAssignedRegDep() && S.getLatency() == 0 &&
Krzysztof Parzyszekdca38312018-03-20 12:28:43 +0000146 S.getSUnit()->NumPredsLeft == 1) {
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000147 UsesDotCur = S.getSUnit();
148 DotCurPNum = PacketNum;
149 break;
150 }
151 if (SU == UsesDotCur) {
152 UsesDotCur = nullptr;
153 DotCurPNum = -1;
154 }
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +0000155
156 UsesLoad = MI->mayLoad();
Krzysztof Parzyszek5ffd8082018-03-20 14:54:01 +0000157
158 if (TII->isHVXVec(*MI) && !MI->mayLoad() && !MI->mayStore())
159 for (auto &S : SU->Succs)
160 if (S.isAssignedRegDep() && S.getLatency() == 0 &&
161 TII->mayBeNewStore(*S.getSUnit()->getInstr()) &&
162 Resources->canReserveResources(*S.getSUnit()->getInstr())) {
163 PrefVectorStoreNew = S.getSUnit();
164 break;
165 }
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000166}