1 //===-- ARMHazardRecognizer.cpp - ARM postra hazard recognizer ------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ARMHazardRecognizer.h"
10 #include "ARMBaseInstrInfo.h"
11 #include "ARMBaseRegisterInfo.h"
12 #include "ARMSubtarget.h"
13 #include "llvm/CodeGen/MachineInstr.h"
14 #include "llvm/CodeGen/ScheduleDAG.h"
15 #include "llvm/CodeGen/TargetRegisterInfo.h"
18 static bool hasRAWHazard(MachineInstr
*DefMI
, MachineInstr
*MI
,
19 const TargetRegisterInfo
&TRI
) {
20 // FIXME: Detect integer instructions properly.
21 const MCInstrDesc
&MCID
= MI
->getDesc();
22 unsigned Domain
= MCID
.TSFlags
& ARMII::DomainMask
;
25 unsigned Opcode
= MCID
.getOpcode();
26 if (Opcode
== ARM::VMOVRS
|| Opcode
== ARM::VMOVRRD
)
28 if ((Domain
& ARMII::DomainVFP
) || (Domain
& ARMII::DomainNEON
))
29 return MI
->readsRegister(DefMI
->getOperand(0).getReg(), &TRI
);
33 ScheduleHazardRecognizer::HazardType
34 ARMHazardRecognizer::getHazardType(SUnit
*SU
, int Stalls
) {
35 assert(Stalls
== 0 && "ARM hazards don't support scoreboard lookahead");
37 MachineInstr
*MI
= SU
->getInstr();
39 if (!MI
->isDebugInstr()) {
40 // Look for special VMLA / VMLS hazards. A VMUL / VADD / VSUB following
41 // a VMLA / VMLS will cause 4 cycle stall.
42 const MCInstrDesc
&MCID
= MI
->getDesc();
43 if (LastMI
&& (MCID
.TSFlags
& ARMII::DomainMask
) != ARMII::DomainGeneral
) {
44 MachineInstr
*DefMI
= LastMI
;
45 const MCInstrDesc
&LastMCID
= LastMI
->getDesc();
46 const MachineFunction
*MF
= MI
->getParent()->getParent();
47 const ARMBaseInstrInfo
&TII
= *static_cast<const ARMBaseInstrInfo
*>(
48 MF
->getSubtarget().getInstrInfo());
50 // Skip over one non-VFP / NEON instruction.
51 if (!LastMI
->isBarrier() &&
52 !(TII
.getSubtarget().hasMuxedUnits() && LastMI
->mayLoadOrStore()) &&
53 (LastMCID
.TSFlags
& ARMII::DomainMask
) == ARMII::DomainGeneral
) {
54 MachineBasicBlock::iterator I
= LastMI
;
55 if (I
!= LastMI
->getParent()->begin()) {
61 if (TII
.isFpMLxInstruction(DefMI
->getOpcode()) &&
62 (TII
.canCauseFpMLxStall(MI
->getOpcode()) ||
63 hasRAWHazard(DefMI
, MI
, TII
.getRegisterInfo()))) {
64 // Try to schedule another instruction for the next 4 cycles.
72 return ScoreboardHazardRecognizer::getHazardType(SU
, Stalls
);
75 void ARMHazardRecognizer::Reset() {
78 ScoreboardHazardRecognizer::Reset();
81 void ARMHazardRecognizer::EmitInstruction(SUnit
*SU
) {
82 MachineInstr
*MI
= SU
->getInstr();
83 if (!MI
->isDebugInstr()) {
88 ScoreboardHazardRecognizer::EmitInstruction(SU
);
91 void ARMHazardRecognizer::AdvanceCycle() {
92 if (FpMLxStalls
&& --FpMLxStalls
== 0)
93 // Stalled for 4 cycles but still can't schedule any other instructions.
95 ScoreboardHazardRecognizer::AdvanceCycle();
98 void ARMHazardRecognizer::RecedeCycle() {
99 llvm_unreachable("reverse ARM hazard checking unsupported");