2016-07-29 15:59:55 +02:00
|
|
|
//===-- HexagonHazardRecognizer.cpp - Hexagon Post RA Hazard Recognizer ---===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-07-29 15:59:55 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the hazard recognizer for scheduling on Hexagon.
|
|
|
|
// Use a DFA based hazard recognizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "HexagonHazardRecognizer.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2016-07-29 15:59:55 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
|
|
|
#include "llvm/CodeGen/ScheduleDAG.h"
|
2016-07-29 15:59:55 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include <cassert>
|
2016-07-29 15:59:55 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "post-RA-sched"
|
|
|
|
|
|
|
|
void HexagonHazardRecognizer::Reset() {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Reset hazard recognizer\n");
|
2016-07-29 15:59:55 +02:00
|
|
|
Resources->clearResources();
|
|
|
|
PacketNum = 0;
|
|
|
|
UsesDotCur = nullptr;
|
|
|
|
DotCurPNum = -1;
|
2018-03-16 21:55:49 +01:00
|
|
|
UsesLoad = false;
|
2018-03-20 15:54:01 +01:00
|
|
|
PrefVectorStoreNew = nullptr;
|
2016-07-29 15:59:55 +02:00
|
|
|
RegDefs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScheduleHazardRecognizer::HazardType
|
|
|
|
HexagonHazardRecognizer::getHazardType(SUnit *SU, int stalls) {
|
|
|
|
MachineInstr *MI = SU->getInstr();
|
|
|
|
if (!MI || TII->isZeroCost(MI->getOpcode()))
|
|
|
|
return NoHazard;
|
|
|
|
|
|
|
|
if (!Resources->canReserveResources(*MI)) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "*** Hazard in cycle " << PacketNum << ", " << *MI);
|
2016-07-29 15:59:55 +02:00
|
|
|
HazardType RetVal = Hazard;
|
2016-07-29 23:49:42 +02:00
|
|
|
if (TII->mayBeNewStore(*MI)) {
|
2016-07-29 15:59:55 +02:00
|
|
|
// Make sure the register to be stored is defined by an instruction in the
|
|
|
|
// packet.
|
|
|
|
MachineOperand &MO = MI->getOperand(MI->getNumOperands() - 1);
|
|
|
|
if (!MO.isReg() || RegDefs.count(MO.getReg()) == 0)
|
|
|
|
return Hazard;
|
|
|
|
// The .new store version uses different resources so check if it
|
|
|
|
// causes a hazard.
|
|
|
|
MachineFunction *MF = MI->getParent()->getParent();
|
|
|
|
MachineInstr *NewMI =
|
2016-07-29 23:49:42 +02:00
|
|
|
MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)),
|
2016-07-29 15:59:55 +02:00
|
|
|
MI->getDebugLoc());
|
|
|
|
if (Resources->canReserveResources(*NewMI))
|
|
|
|
RetVal = NoHazard;
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "*** Try .new version? " << (RetVal == NoHazard)
|
|
|
|
<< "\n");
|
2016-07-29 15:59:55 +02:00
|
|
|
MF->DeleteMachineInstr(NewMI);
|
|
|
|
}
|
|
|
|
return RetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SU == UsesDotCur && DotCurPNum != (int)PacketNum) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "*** .cur Hazard in cycle " << PacketNum << ", "
|
|
|
|
<< *MI);
|
2016-07-29 15:59:55 +02:00
|
|
|
return Hazard;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NoHazard;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonHazardRecognizer::AdvanceCycle() {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Advance cycle, clear state\n");
|
2016-07-29 15:59:55 +02:00
|
|
|
Resources->clearResources();
|
|
|
|
if (DotCurPNum != -1 && DotCurPNum != (int)PacketNum) {
|
|
|
|
UsesDotCur = nullptr;
|
|
|
|
DotCurPNum = -1;
|
|
|
|
}
|
2018-03-16 21:55:49 +01:00
|
|
|
UsesLoad = false;
|
2018-03-20 15:54:01 +01:00
|
|
|
PrefVectorStoreNew = nullptr;
|
2016-07-29 15:59:55 +02:00
|
|
|
PacketNum++;
|
|
|
|
RegDefs.clear();
|
|
|
|
}
|
|
|
|
|
2018-03-16 21:55:49 +01:00
|
|
|
/// Handle the cases when we prefer one instruction over another. Case 1 - we
|
|
|
|
/// prefer not to generate multiple loads in the packet to avoid a potential
|
|
|
|
/// bank conflict. Case 2 - if a packet contains a dot cur instruction, then we
|
|
|
|
/// prefer the instruction that can use the dot cur result. However, if the use
|
|
|
|
/// is not scheduled in the same packet, then prefer other instructions in the
|
2018-03-20 15:54:01 +01:00
|
|
|
/// subsequent packet. Case 3 - we prefer a vector store that can be converted
|
|
|
|
/// to a .new store. The packetizer will not generate the .new store if the
|
|
|
|
/// store doesn't have resources to fit in the packet (but the .new store may
|
|
|
|
/// have resources). We attempt to schedule the store as soon as possible to
|
|
|
|
/// help packetize the two instructions together.
|
2016-07-29 15:59:55 +02:00
|
|
|
bool HexagonHazardRecognizer::ShouldPreferAnother(SUnit *SU) {
|
2018-03-20 15:54:01 +01:00
|
|
|
if (PrefVectorStoreNew != nullptr && PrefVectorStoreNew != SU)
|
|
|
|
return true;
|
2018-03-16 21:55:49 +01:00
|
|
|
if (UsesLoad && SU->isInstr() && SU->getInstr()->mayLoad())
|
|
|
|
return true;
|
2016-07-29 15:59:55 +02:00
|
|
|
return UsesDotCur && ((SU == UsesDotCur) ^ (DotCurPNum == (int)PacketNum));
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonHazardRecognizer::EmitInstruction(SUnit *SU) {
|
|
|
|
MachineInstr *MI = SU->getInstr();
|
|
|
|
if (!MI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Keep the set of definitions for each packet, which is used to determine
|
|
|
|
// if a .new can be used.
|
2016-10-24 23:36:43 +02:00
|
|
|
for (const MachineOperand &MO : MI->operands())
|
|
|
|
if (MO.isReg() && MO.isDef() && !MO.isImplicit())
|
|
|
|
RegDefs.insert(MO.getReg());
|
2016-07-29 15:59:55 +02:00
|
|
|
|
|
|
|
if (TII->isZeroCost(MI->getOpcode()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Resources->canReserveResources(*MI)) {
|
|
|
|
// It must be a .new store since other instructions must be able to be
|
|
|
|
// reserved at this point.
|
2016-07-29 23:49:42 +02:00
|
|
|
assert(TII->mayBeNewStore(*MI) && "Expecting .new store");
|
2016-07-29 15:59:55 +02:00
|
|
|
MachineFunction *MF = MI->getParent()->getParent();
|
2016-07-29 23:49:42 +02:00
|
|
|
MachineInstr *NewMI =
|
|
|
|
MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)),
|
|
|
|
MI->getDebugLoc());
|
2016-07-29 15:59:55 +02:00
|
|
|
assert(Resources->canReserveResources(*NewMI));
|
|
|
|
Resources->reserveResources(*NewMI);
|
|
|
|
MF->DeleteMachineInstr(NewMI);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Resources->reserveResources(*MI);
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " Add instruction " << *MI);
|
2016-07-29 15:59:55 +02:00
|
|
|
|
|
|
|
// When scheduling a dot cur instruction, check if there is an instruction
|
|
|
|
// that can use the dot cur in the same packet. If so, we'll attempt to
|
2018-03-20 13:28:43 +01:00
|
|
|
// schedule it before other instructions. We only do this if the load has a
|
|
|
|
// single zero-latency use.
|
2016-07-29 23:49:42 +02:00
|
|
|
if (TII->mayBeCurLoad(*MI))
|
2016-07-29 15:59:55 +02:00
|
|
|
for (auto &S : SU->Succs)
|
|
|
|
if (S.isAssignedRegDep() && S.getLatency() == 0 &&
|
2018-03-20 13:28:43 +01:00
|
|
|
S.getSUnit()->NumPredsLeft == 1) {
|
2016-07-29 15:59:55 +02:00
|
|
|
UsesDotCur = S.getSUnit();
|
|
|
|
DotCurPNum = PacketNum;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (SU == UsesDotCur) {
|
|
|
|
UsesDotCur = nullptr;
|
|
|
|
DotCurPNum = -1;
|
|
|
|
}
|
2018-03-16 21:55:49 +01:00
|
|
|
|
|
|
|
UsesLoad = MI->mayLoad();
|
2018-03-20 15:54:01 +01:00
|
|
|
|
|
|
|
if (TII->isHVXVec(*MI) && !MI->mayLoad() && !MI->mayStore())
|
|
|
|
for (auto &S : SU->Succs)
|
|
|
|
if (S.isAssignedRegDep() && S.getLatency() == 0 &&
|
|
|
|
TII->mayBeNewStore(*S.getSUnit()->getInstr()) &&
|
|
|
|
Resources->canReserveResources(*S.getSUnit()->getInstr())) {
|
|
|
|
PrefVectorStoreNew = S.getSUnit();
|
|
|
|
break;
|
|
|
|
}
|
2016-07-29 15:59:55 +02:00
|
|
|
}
|