1//===- HexagonMachineScheduler.cpp - MI Scheduler for Hexagon -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// MachineScheduler schedules machine instructions after phi elimination. It
10// preserves LiveIntervals so it can be invoked before register allocation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonMachineScheduler.h"
15#include "HexagonInstrInfo.h"
16#include "HexagonSubtarget.h"
17#include "llvm/CodeGen/MachineScheduler.h"
18#include "llvm/CodeGen/ScheduleDAG.h"
19#include "llvm/CodeGen/VLIWMachineScheduler.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "machine-scheduler"
24
25/// Return true if there is a dependence between SUd and SUu.
26bool HexagonVLIWResourceModel::hasDependence(const SUnit *SUd,
27 const SUnit *SUu) {
28 const auto *QII = static_cast<const HexagonInstrInfo *>(TII);
29
30 // Enable .cur formation.
31 if (QII->mayBeCurLoad(MI: *SUd->getInstr()))
32 return false;
33
34 if (QII->canExecuteInBundle(First: *SUd->getInstr(), Second: *SUu->getInstr()))
35 return false;
36
37 return VLIWResourceModel::hasDependence(SUd, SUu);
38}
39
40VLIWResourceModel *HexagonConvergingVLIWScheduler::createVLIWResourceModel(
41 const TargetSubtargetInfo &STI, const TargetSchedModel *SchedModel) const {
42 return new HexagonVLIWResourceModel(STI, SchedModel);
43}
44
45int HexagonConvergingVLIWScheduler::SchedulingCost(ReadyQueue &Q, SUnit *SU,
46 SchedCandidate &Candidate,
47 RegPressureDelta &Delta,
48 bool verbose) {
49 int ResCount =
50 ConvergingVLIWScheduler::SchedulingCost(Q, SU, Candidate, Delta, verbose);
51
52 if (!SU || SU->isScheduled || !SU->isInstr())
53 return ResCount;
54
55 auto &QST = DAG->MF.getSubtarget<HexagonSubtarget>();
56 auto &QII = *QST.getInstrInfo();
57 MachineInstr *MI = SU->getInstr();
58 if (QII.mayBeCurLoad(MI: *MI)) {
59 if (Q.getID() == TopQID &&
60 Top.ResourceModel->isResourceAvailable(SU, IsTop: true)) {
61 ResCount += PriorityTwo;
62 LLVM_DEBUG(if (verbose) dbgs() << "C|");
63 } else if (Q.getID() == BotQID &&
64 Bot.ResourceModel->isResourceAvailable(SU, IsTop: false)) {
65 ResCount += PriorityTwo;
66 LLVM_DEBUG(if (verbose) dbgs() << "C|");
67 }
68 return ResCount;
69 }
70
71 // Keep HLX load uses and store definitions close to their pseudos so the
72 // packetizer can place each pair in the same packet.
73 if (Q.getID() == TopQID) {
74 if (QII.isNewStorePseudo(MI: *MI)) {
75 ResCount += PriorityOne;
76 } else if (!QII.isTmpLoadPseudo(MI: *MI)) {
77 for (const SDep &PredDep : SU->Preds) {
78 SUnit *Pred = PredDep.getSUnit();
79 if (PredDep.getKind() == SDep::Data && Pred->isInstr() &&
80 QII.isTmpLoadPseudo(MI: *Pred->getInstr()) && Pred->NumSuccs < 3)
81 ResCount += PriorityOne;
82 }
83 }
84 } else if (Q.getID() == BotQID) {
85 if (QII.isTmpLoadPseudo(MI: *MI) && SU->NumSuccs < 3) {
86 ResCount += PriorityOne;
87 } else if (!QII.isNewStorePseudo(MI: *MI)) {
88 for (const SDep &SuccDep : SU->Succs) {
89 SUnit *Succ = SuccDep.getSUnit();
90 if (SuccDep.getKind() == SDep::Data && Succ->isInstr() &&
91 QII.isNewStorePseudo(MI: *Succ->getInstr()))
92 ResCount += PriorityOne;
93 }
94 }
95 }
96
97 return ResCount;
98}
99