blob: 8fcd7822cd7a01923db59cb723c7d73082be2ec5 [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"
16#include "llvm/CodeGen/ScheduleDAG.h"
17#include "llvm/Target/TargetInstrInfo.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineInstrBundle.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "post-RA-sched"
26
27void HexagonHazardRecognizer::Reset() {
28 DEBUG(dbgs() << "Reset hazard recognizer\n");
29 Resources->clearResources();
30 PacketNum = 0;
31 UsesDotCur = nullptr;
32 DotCurPNum = -1;
33 RegDefs.clear();
34}
35
36ScheduleHazardRecognizer::HazardType
37HexagonHazardRecognizer::getHazardType(SUnit *SU, int stalls) {
38 MachineInstr *MI = SU->getInstr();
39 if (!MI || TII->isZeroCost(MI->getOpcode()))
40 return NoHazard;
41
42 if (!Resources->canReserveResources(*MI)) {
43 DEBUG(dbgs() << "*** Hazard in cycle " << PacketNum << ", " << *MI);
44 HazardType RetVal = Hazard;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +000045 if (TII->mayBeNewStore(*MI)) {
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000046 // Make sure the register to be stored is defined by an instruction in the
47 // packet.
48 MachineOperand &MO = MI->getOperand(MI->getNumOperands() - 1);
49 if (!MO.isReg() || RegDefs.count(MO.getReg()) == 0)
50 return Hazard;
51 // The .new store version uses different resources so check if it
52 // causes a hazard.
53 MachineFunction *MF = MI->getParent()->getParent();
54 MachineInstr *NewMI =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +000055 MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)),
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +000056 MI->getDebugLoc());
57 if (Resources->canReserveResources(*NewMI))
58 RetVal = NoHazard;
59 DEBUG(dbgs() << "*** Try .new version? " << (RetVal == NoHazard) << "\n");
60 MF->DeleteMachineInstr(NewMI);
61 }
62 return RetVal;
63 }
64
65 if (SU == UsesDotCur && DotCurPNum != (int)PacketNum) {
66 DEBUG(dbgs() << "*** .cur Hazard in cycle " << PacketNum << ", " << *MI);
67 return Hazard;
68 }
69
70 return NoHazard;
71}
72
73void HexagonHazardRecognizer::AdvanceCycle() {
74 DEBUG(dbgs() << "Advance cycle, clear state\n");
75 Resources->clearResources();
76 if (DotCurPNum != -1 && DotCurPNum != (int)PacketNum) {
77 UsesDotCur = nullptr;
78 DotCurPNum = -1;
79 }
80 PacketNum++;
81 RegDefs.clear();
82}
83
84/// If a packet contains a dot cur instruction, then we may prefer the
85/// instruction that can use the dot cur result. Or, if the use
86/// isn't scheduled in the same packet, then prefer other instructions
87/// in the subsequent packet.
88bool HexagonHazardRecognizer::ShouldPreferAnother(SUnit *SU) {
89 return UsesDotCur && ((SU == UsesDotCur) ^ (DotCurPNum == (int)PacketNum));
90}
91
92void HexagonHazardRecognizer::EmitInstruction(SUnit *SU) {
93 MachineInstr *MI = SU->getInstr();
94 if (!MI)
95 return;
96
97 // Keep the set of definitions for each packet, which is used to determine
98 // if a .new can be used.
99 for (ConstMIOperands MO(*MI); MO.isValid(); ++MO)
100 if (MO->isReg() && MO->isDef() && !MO->isImplicit())
101 RegDefs.insert(MO->getReg());
102
103 if (TII->isZeroCost(MI->getOpcode()))
104 return;
105
106 if (!Resources->canReserveResources(*MI)) {
107 // It must be a .new store since other instructions must be able to be
108 // reserved at this point.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000109 assert(TII->mayBeNewStore(*MI) && "Expecting .new store");
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000110 MachineFunction *MF = MI->getParent()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000111 MachineInstr *NewMI =
112 MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)),
113 MI->getDebugLoc());
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000114 assert(Resources->canReserveResources(*NewMI));
115 Resources->reserveResources(*NewMI);
116 MF->DeleteMachineInstr(NewMI);
117 }
118 else
119 Resources->reserveResources(*MI);
120 DEBUG(dbgs() << " Add instruction " << *MI);
121
122 // When scheduling a dot cur instruction, check if there is an instruction
123 // that can use the dot cur in the same packet. If so, we'll attempt to
124 // schedule it before other instructions. We only do this if the use has
125 // the same height as the dot cur. Otherwise, we may miss scheduling an
126 // instruction with a greater height, which is more important.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000127 if (TII->mayBeCurLoad(*MI))
Krzysztof Parzyszek0005a722016-07-29 13:59:55 +0000128 for (auto &S : SU->Succs)
129 if (S.isAssignedRegDep() && S.getLatency() == 0 &&
130 SU->getHeight() == S.getSUnit()->getHeight()) {
131 UsesDotCur = S.getSUnit();
132 DotCurPNum = PacketNum;
133 break;
134 }
135 if (SU == UsesDotCur) {
136 UsesDotCur = nullptr;
137 DotCurPNum = -1;
138 }
139}