1*0b57cec5SDimitry Andric //===-- HexagonHazardRecognizer.cpp - Hexagon Post RA Hazard Recognizer ---===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file defines the hazard recognizer for scheduling on Hexagon. 10*0b57cec5SDimitry Andric // Use a DFA based hazard recognizer. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "HexagonHazardRecognizer.h" 15*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 16*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 17*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 18*0b57cec5SDimitry Andric #include "llvm/CodeGen/ScheduleDAG.h" 19*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 20*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 21*0b57cec5SDimitry Andric #include <cassert> 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric using namespace llvm; 24*0b57cec5SDimitry Andric 25*0b57cec5SDimitry Andric #define DEBUG_TYPE "post-RA-sched" 26*0b57cec5SDimitry Andric 27*0b57cec5SDimitry Andric void HexagonHazardRecognizer::Reset() { 28*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Reset hazard recognizer\n"); 29*0b57cec5SDimitry Andric Resources->clearResources(); 30*0b57cec5SDimitry Andric PacketNum = 0; 31*0b57cec5SDimitry Andric UsesDotCur = nullptr; 32*0b57cec5SDimitry Andric DotCurPNum = -1; 33*0b57cec5SDimitry Andric UsesLoad = false; 34*0b57cec5SDimitry Andric PrefVectorStoreNew = nullptr; 35*0b57cec5SDimitry Andric RegDefs.clear(); 36*0b57cec5SDimitry Andric } 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric ScheduleHazardRecognizer::HazardType 39*0b57cec5SDimitry Andric HexagonHazardRecognizer::getHazardType(SUnit *SU, int stalls) { 40*0b57cec5SDimitry Andric MachineInstr *MI = SU->getInstr(); 41*0b57cec5SDimitry Andric if (!MI || TII->isZeroCost(MI->getOpcode())) 42*0b57cec5SDimitry Andric return NoHazard; 43*0b57cec5SDimitry Andric 44*0b57cec5SDimitry Andric if (!Resources->canReserveResources(*MI)) { 45*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "*** Hazard in cycle " << PacketNum << ", " << *MI); 46*0b57cec5SDimitry Andric HazardType RetVal = Hazard; 47*0b57cec5SDimitry Andric if (TII->mayBeNewStore(*MI)) { 48*0b57cec5SDimitry Andric // Make sure the register to be stored is defined by an instruction in the 49*0b57cec5SDimitry Andric // packet. 50*0b57cec5SDimitry Andric MachineOperand &MO = MI->getOperand(MI->getNumOperands() - 1); 51*0b57cec5SDimitry Andric if (!MO.isReg() || RegDefs.count(MO.getReg()) == 0) 52*0b57cec5SDimitry Andric return Hazard; 53*0b57cec5SDimitry Andric // The .new store version uses different resources so check if it 54*0b57cec5SDimitry Andric // causes a hazard. 55*0b57cec5SDimitry Andric MachineFunction *MF = MI->getParent()->getParent(); 56*0b57cec5SDimitry Andric MachineInstr *NewMI = 57*0b57cec5SDimitry Andric MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)), 58*0b57cec5SDimitry Andric MI->getDebugLoc()); 59*0b57cec5SDimitry Andric if (Resources->canReserveResources(*NewMI)) 60*0b57cec5SDimitry Andric RetVal = NoHazard; 61*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "*** Try .new version? " << (RetVal == NoHazard) 62*0b57cec5SDimitry Andric << "\n"); 63*0b57cec5SDimitry Andric MF->DeleteMachineInstr(NewMI); 64*0b57cec5SDimitry Andric } 65*0b57cec5SDimitry Andric return RetVal; 66*0b57cec5SDimitry Andric } 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric if (SU == UsesDotCur && DotCurPNum != (int)PacketNum) { 69*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "*** .cur Hazard in cycle " << PacketNum << ", " 70*0b57cec5SDimitry Andric << *MI); 71*0b57cec5SDimitry Andric return Hazard; 72*0b57cec5SDimitry Andric } 73*0b57cec5SDimitry Andric 74*0b57cec5SDimitry Andric return NoHazard; 75*0b57cec5SDimitry Andric } 76*0b57cec5SDimitry Andric 77*0b57cec5SDimitry Andric void HexagonHazardRecognizer::AdvanceCycle() { 78*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Advance cycle, clear state\n"); 79*0b57cec5SDimitry Andric Resources->clearResources(); 80*0b57cec5SDimitry Andric if (DotCurPNum != -1 && DotCurPNum != (int)PacketNum) { 81*0b57cec5SDimitry Andric UsesDotCur = nullptr; 82*0b57cec5SDimitry Andric DotCurPNum = -1; 83*0b57cec5SDimitry Andric } 84*0b57cec5SDimitry Andric UsesLoad = false; 85*0b57cec5SDimitry Andric PrefVectorStoreNew = nullptr; 86*0b57cec5SDimitry Andric PacketNum++; 87*0b57cec5SDimitry Andric RegDefs.clear(); 88*0b57cec5SDimitry Andric } 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric /// Handle the cases when we prefer one instruction over another. Case 1 - we 91*0b57cec5SDimitry Andric /// prefer not to generate multiple loads in the packet to avoid a potential 92*0b57cec5SDimitry Andric /// bank conflict. Case 2 - if a packet contains a dot cur instruction, then we 93*0b57cec5SDimitry Andric /// prefer the instruction that can use the dot cur result. However, if the use 94*0b57cec5SDimitry Andric /// is not scheduled in the same packet, then prefer other instructions in the 95*0b57cec5SDimitry Andric /// subsequent packet. Case 3 - we prefer a vector store that can be converted 96*0b57cec5SDimitry Andric /// to a .new store. The packetizer will not generate the .new store if the 97*0b57cec5SDimitry Andric /// store doesn't have resources to fit in the packet (but the .new store may 98*0b57cec5SDimitry Andric /// have resources). We attempt to schedule the store as soon as possible to 99*0b57cec5SDimitry Andric /// help packetize the two instructions together. 100*0b57cec5SDimitry Andric bool HexagonHazardRecognizer::ShouldPreferAnother(SUnit *SU) { 101*0b57cec5SDimitry Andric if (PrefVectorStoreNew != nullptr && PrefVectorStoreNew != SU) 102*0b57cec5SDimitry Andric return true; 103*0b57cec5SDimitry Andric if (UsesLoad && SU->isInstr() && SU->getInstr()->mayLoad()) 104*0b57cec5SDimitry Andric return true; 105*0b57cec5SDimitry Andric return UsesDotCur && ((SU == UsesDotCur) ^ (DotCurPNum == (int)PacketNum)); 106*0b57cec5SDimitry Andric } 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric void HexagonHazardRecognizer::EmitInstruction(SUnit *SU) { 109*0b57cec5SDimitry Andric MachineInstr *MI = SU->getInstr(); 110*0b57cec5SDimitry Andric if (!MI) 111*0b57cec5SDimitry Andric return; 112*0b57cec5SDimitry Andric 113*0b57cec5SDimitry Andric // Keep the set of definitions for each packet, which is used to determine 114*0b57cec5SDimitry Andric // if a .new can be used. 115*0b57cec5SDimitry Andric for (const MachineOperand &MO : MI->operands()) 116*0b57cec5SDimitry Andric if (MO.isReg() && MO.isDef() && !MO.isImplicit()) 117*0b57cec5SDimitry Andric RegDefs.insert(MO.getReg()); 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric if (TII->isZeroCost(MI->getOpcode())) 120*0b57cec5SDimitry Andric return; 121*0b57cec5SDimitry Andric 122*0b57cec5SDimitry Andric if (!Resources->canReserveResources(*MI)) { 123*0b57cec5SDimitry Andric // It must be a .new store since other instructions must be able to be 124*0b57cec5SDimitry Andric // reserved at this point. 125*0b57cec5SDimitry Andric assert(TII->mayBeNewStore(*MI) && "Expecting .new store"); 126*0b57cec5SDimitry Andric MachineFunction *MF = MI->getParent()->getParent(); 127*0b57cec5SDimitry Andric MachineInstr *NewMI = 128*0b57cec5SDimitry Andric MF->CreateMachineInstr(TII->get(TII->getDotNewOp(*MI)), 129*0b57cec5SDimitry Andric MI->getDebugLoc()); 130*0b57cec5SDimitry Andric assert(Resources->canReserveResources(*NewMI)); 131*0b57cec5SDimitry Andric Resources->reserveResources(*NewMI); 132*0b57cec5SDimitry Andric MF->DeleteMachineInstr(NewMI); 133*0b57cec5SDimitry Andric } 134*0b57cec5SDimitry Andric else 135*0b57cec5SDimitry Andric Resources->reserveResources(*MI); 136*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Add instruction " << *MI); 137*0b57cec5SDimitry Andric 138*0b57cec5SDimitry Andric // When scheduling a dot cur instruction, check if there is an instruction 139*0b57cec5SDimitry Andric // that can use the dot cur in the same packet. If so, we'll attempt to 140*0b57cec5SDimitry Andric // schedule it before other instructions. We only do this if the load has a 141*0b57cec5SDimitry Andric // single zero-latency use. 142*0b57cec5SDimitry Andric if (TII->mayBeCurLoad(*MI)) 143*0b57cec5SDimitry Andric for (auto &S : SU->Succs) 144*0b57cec5SDimitry Andric if (S.isAssignedRegDep() && S.getLatency() == 0 && 145*0b57cec5SDimitry Andric S.getSUnit()->NumPredsLeft == 1) { 146*0b57cec5SDimitry Andric UsesDotCur = S.getSUnit(); 147*0b57cec5SDimitry Andric DotCurPNum = PacketNum; 148*0b57cec5SDimitry Andric break; 149*0b57cec5SDimitry Andric } 150*0b57cec5SDimitry Andric if (SU == UsesDotCur) { 151*0b57cec5SDimitry Andric UsesDotCur = nullptr; 152*0b57cec5SDimitry Andric DotCurPNum = -1; 153*0b57cec5SDimitry Andric } 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric UsesLoad = MI->mayLoad(); 156*0b57cec5SDimitry Andric 157*0b57cec5SDimitry Andric if (TII->isHVXVec(*MI) && !MI->mayLoad() && !MI->mayStore()) 158*0b57cec5SDimitry Andric for (auto &S : SU->Succs) 159*0b57cec5SDimitry Andric if (S.isAssignedRegDep() && S.getLatency() == 0 && 160*0b57cec5SDimitry Andric TII->mayBeNewStore(*S.getSUnit()->getInstr()) && 161*0b57cec5SDimitry Andric Resources->canReserveResources(*S.getSUnit()->getInstr())) { 162*0b57cec5SDimitry Andric PrefVectorStoreNew = S.getSUnit(); 163*0b57cec5SDimitry Andric break; 164*0b57cec5SDimitry Andric } 165*0b57cec5SDimitry Andric } 166