blob: cae7318077b5c9c44a5dc2dbdebf31d6f7493400 [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() {
29 DEBUG(dbgs() << "Reset hazard recognizer\n");
30 Resources->clearResources();
31 PacketNum = 0;
32 UsesDotCur = nullptr;
33 DotCurPNum = -1;
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +000034 UsesLoad = false;
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000035 RegDefs.clear();
36}
37
38ScheduleHazardRecognizer::HazardType
39HexagonHazardRecognizer::getHazardType(SUnit *SU, int stalls) {
40 MachineInstr *MI = SU->getInstr();
41 if (!MI || TII->isZeroCost(MI->getOpcode()))
42 return NoHazard;
43
44 if (!Resources->canReserveResources(*MI)) {
45 DEBUG(dbgs() << "*** Hazard in cycle " << PacketNum << ", " << *MI);
46 HazardType RetVal = Hazard;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +000047 if (TII->mayBeNewStore(*MI)) {
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000048 // Make sure the register to be stored is defined by an instruction in the
49 // packet.
50 MachineOperand &MO = MI->getOperand(MI->getNumOperands() - 1);
51 if (!MO.isReg() || RegDefs.count(MO.getReg()) == 0)
52 return Hazard;
53 // The .new store version uses different resources so check if it
54 // causes a hazard.
55 MachineFunction *MF = MI->getParent()->getParent();
56 MachineInstr *NewMI =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +000057 MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)),
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000058 MI->getDebugLoc());
59 if (Resources->canReserveResources(*NewMI))
60 RetVal = NoHazard;
61 DEBUG(dbgs() << "*** Try .new version? " << (RetVal == NoHazard) << "\n");
62 MF->DeleteMachineInstr(NewMI);
63 }
64 return RetVal;
65 }
66
67 if (SU == UsesDotCur && DotCurPNum != (int)PacketNum) {
68 DEBUG(dbgs() << "*** .cur Hazard in cycle " << PacketNum << ", " << *MI);
69 return Hazard;
70 }
71
72 return NoHazard;
73}
74
75void HexagonHazardRecognizer::AdvanceCycle() {
76 DEBUG(dbgs() << "Advance cycle, clear state\n");
77 Resources->clearResources();
78 if (DotCurPNum != -1 && DotCurPNum != (int)PacketNum) {
79 UsesDotCur = nullptr;
80 DotCurPNum = -1;
81 }
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +000082 UsesLoad = false;
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000083 PacketNum++;
84 RegDefs.clear();
85}
86
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +000087/// Handle the cases when we prefer one instruction over another. Case 1 - we
88/// prefer not to generate multiple loads in the packet to avoid a potential
89/// bank conflict. Case 2 - if a packet contains a dot cur instruction, then we
90/// prefer the instruction that can use the dot cur result. However, if the use
91/// is not scheduled in the same packet, then prefer other instructions in the
92/// subsequent packet.
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000093bool HexagonHazardRecognizer::ShouldPreferAnother(SUnit *SU) {
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +000094 if (UsesLoad && SU->isInstr() && SU->getInstr()->mayLoad())
95 return true;
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000096 return UsesDotCur && ((SU == UsesDotCur) ^ (DotCurPNum == (int)PacketNum));
97}
98
99void HexagonHazardRecognizer::EmitInstruction(SUnit *SU) {
100 MachineInstr *MI = SU->getInstr();
101 if (!MI)
102 return;
103
104 // Keep the set of definitions for each packet, which is used to determine
105 // if a .new can be used.
Matthias Braunfc371552016-10-24 21:36:43 +0000106 for (const MachineOperand &MO : MI->operands())
107 if (MO.isReg() && MO.isDef() && !MO.isImplicit())
108 RegDefs.insert(MO.getReg());
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000109
110 if (TII->isZeroCost(MI->getOpcode()))
111 return;
112
113 if (!Resources->canReserveResources(*MI)) {
114 // It must be a .new store since other instructions must be able to be
115 // reserved at this point.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000116 assert(TII->mayBeNewStore(*MI) && "Expecting .new store");
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000117 MachineFunction *MF = MI->getParent()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000118 MachineInstr *NewMI =
119 MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)),
120 MI->getDebugLoc());
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000121 assert(Resources->canReserveResources(*NewMI));
122 Resources->reserveResources(*NewMI);
123 MF->DeleteMachineInstr(NewMI);
124 }
125 else
126 Resources->reserveResources(*MI);
127 DEBUG(dbgs() << " Add instruction " << *MI);
128
129 // When scheduling a dot cur instruction, check if there is an instruction
130 // that can use the dot cur in the same packet. If so, we'll attempt to
131 // schedule it before other instructions. We only do this if the use has
132 // the same height as the dot cur. Otherwise, we may miss scheduling an
133 // instruction with a greater height, which is more important.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000134 if (TII->mayBeCurLoad(*MI))
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000135 for (auto &S : SU->Succs)
136 if (S.isAssignedRegDep() && S.getLatency() == 0 &&
137 SU->getHeight() == S.getSUnit()->getHeight()) {
138 UsesDotCur = S.getSUnit();
139 DotCurPNum = PacketNum;
140 break;
141 }
142 if (SU == UsesDotCur) {
143 UsesDotCur = nullptr;
144 DotCurPNum = -1;
145 }
Krzysztof Parzyszekf81a8d02018-03-16 20:55:49 +0000146
147 UsesLoad = MI->mayLoad();
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000148}