LLVM 24.0.0git
ARMHazardRecognizer.cpp
Go to the documentation of this file.
1//===-- ARMHazardRecognizer.cpp - ARM postra hazard recognizer ------------===//
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
10#include "ARMBaseInstrInfo.h"
11#include "ARMSubtarget.h"
17
18using namespace llvm;
19
21 const TargetRegisterInfo &TRI) {
22 // FIXME: Detect integer instructions properly.
23 const MCInstrDesc &MCID = MI->getDesc();
24 unsigned Domain = MCID.TSFlags & ARMII::DomainMask;
25 if (MI->mayStore())
26 return false;
27 unsigned Opcode = MCID.getOpcode();
28 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
29 return false;
31 return MI->readsRegister(DefMI->getOperand(0).getReg(), &TRI);
32 return false;
33}
34
37 assert(Stalls == 0 && "ARM hazards don't support scoreboard lookahead");
38
39 MachineInstr *MI = SU->getInstr();
40
41 if (!MI->isDebugInstr()) {
42 // Look for special VMLA / VMLS hazards. A VMUL / VADD / VSUB following
43 // a VMLA / VMLS will cause 4 cycle stall.
44 const MCInstrDesc &MCID = MI->getDesc();
45 if (LastMI && (MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainGeneral) {
46 MachineInstr *DefMI = LastMI;
47 const MCInstrDesc &LastMCID = LastMI->getDesc();
48 const MachineFunction *MF = MI->getParent()->getParent();
49 const ARMBaseInstrInfo &TII = *static_cast<const ARMBaseInstrInfo *>(
50 MF->getSubtarget().getInstrInfo());
51
52 // Skip over one non-VFP / NEON instruction.
53 if (!LastMI->isBarrier() &&
54 !(TII.getSubtarget().hasMuxedUnits() && LastMI->mayLoadOrStore()) &&
57 if (I != LastMI->getParent()->begin()) {
58 I = std::prev(I);
59 DefMI = &*I;
60 }
61 }
62
63 if (TII.isFpMLxInstruction(DefMI->getOpcode()) &&
64 (TII.canCauseFpMLxStall(MI->getOpcode()) ||
65 hasRAWHazard(DefMI, MI, TII.getRegisterInfo()))) {
66 // Try to schedule another instruction for the next 4 cycles.
67 if (FpMLxStalls == 0)
68 FpMLxStalls = 4;
69 return Hazard;
70 }
71 }
72 }
73 return NoHazard;
74}
75
77 LastMI = nullptr;
78 FpMLxStalls = 0;
79}
80
82 MachineInstr *MI = SU->getInstr();
83 if (!MI->isDebugInstr()) {
84 LastMI = MI;
85 FpMLxStalls = 0;
86 }
87}
88
90 if (FpMLxStalls && --FpMLxStalls == 0)
91 // Stalled for 4 cycles but still can't schedule any other instructions.
92 LastMI = nullptr;
93}
94
96 llvm_unreachable("reverse ARM hazard checking unsupported");
97}
98
99///////// Bank conflicts handled as hazards //////////////
100
101static bool getBaseOffset(const MachineInstr &MI, const MachineOperand *&BaseOp,
102 int64_t &Offset) {
103
104 uint64_t TSFlags = MI.getDesc().TSFlags;
105 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
106 unsigned IndexMode =
108
109 // Address mode tells us what we want to know about operands for T2
110 // instructions (but not size). It tells us size (but not about operands)
111 // for T1 instructions.
112 switch (AddrMode) {
113 default:
114 return false;
116 // t2LDRBT, t2LDRB_POST, t2LDRB_PRE, t2LDRBi8,
117 // t2LDRHT, t2LDRH_POST, t2LDRH_PRE, t2LDRHi8,
118 // t2LDRSBT, t2LDRSB_POST, t2LDRSB_PRE, t2LDRSBi8,
119 // t2LDRSHT, t2LDRSH_POST, t2LDRSH_PRE, t2LDRSHi8,
120 // t2LDRT, t2LDR_POST, t2LDR_PRE, t2LDRi8
121 BaseOp = &MI.getOperand(1);
122 Offset = (IndexMode == ARMII::IndexModePost)
123 ? 0
124 : (IndexMode == ARMII::IndexModePre ||
125 IndexMode == ARMII::IndexModeUpd)
126 ? MI.getOperand(3).getImm()
127 : MI.getOperand(2).getImm();
128 return true;
130 // t2LDRBi12, t2LDRHi12
131 // t2LDRSBi12, t2LDRSHi12
132 // t2LDRi12
133 BaseOp = &MI.getOperand(1);
134 Offset = MI.getOperand(2).getImm();
135 return true;
137 // t2LDRD_POST, t2LDRD_PRE, t2LDRDi8
138 BaseOp = &MI.getOperand(2);
139 Offset = (IndexMode == ARMII::IndexModePost)
140 ? 0
141 : (IndexMode == ARMII::IndexModePre ||
142 IndexMode == ARMII::IndexModeUpd)
143 ? MI.getOperand(4).getImm()
144 : MI.getOperand(3).getImm();
145 return true;
147 // tLDRBi, tLDRBr (watch out!), TLDRSB
149 // tLDRHi, tLDRHr (watch out!), TLDRSH
151 // tLDRi, tLDRr (watch out!)
152 BaseOp = &MI.getOperand(1);
153 Offset = MI.getOperand(2).isImm() ? MI.getOperand(2).getImm() : 0;
154 return MI.getOperand(2).isImm();
155 }
156 return false;
157}
158
160 const ScheduleDAG *DAG, int64_t DataMask, bool AssumeITCMBankConflict)
161 : MF(DAG->MF), DL(DAG->MF.getDataLayout()), DataMask(DataMask),
162 AssumeITCMBankConflict(AssumeITCMBankConflict) {
163 MaxLookAhead = 1;
164}
165
167ARMBankConflictHazardRecognizer::CheckOffsets(unsigned O0, unsigned O1) {
168 return (((O0 ^ O1) & DataMask) != 0) ? NoHazard : Hazard;
169}
170
173 MachineInstr &L0 = *SU->getInstr();
174 if (!L0.mayLoad() || L0.mayStore() || L0.getNumMemOperands() != 1)
175 return NoHazard;
176
177 auto MO0 = *L0.memoperands().begin();
178 auto BaseVal0 = MO0->getValue();
179 auto BasePseudoVal0 = MO0->getPseudoValue();
180 int64_t Offset0 = 0;
181
182 if (!MO0->getSize().hasValue() || MO0->getSize().getValue() > 4)
183 return NoHazard;
184
185 bool SPvalid = false;
186 const MachineOperand *SP = nullptr;
187 int64_t SPOffset0 = 0;
188
189 for (auto L1 : Accesses) {
190 auto MO1 = *L1->memoperands().begin();
191 auto BaseVal1 = MO1->getValue();
192 auto BasePseudoVal1 = MO1->getPseudoValue();
193 int64_t Offset1 = 0;
194
195 // Pointers to the same object
196 if (BaseVal0 && BaseVal1) {
197 const Value *Ptr0, *Ptr1;
198 Ptr0 = GetPointerBaseWithConstantOffset(BaseVal0, Offset0, DL, true);
199 Ptr1 = GetPointerBaseWithConstantOffset(BaseVal1, Offset1, DL, true);
200 if (Ptr0 == Ptr1 && Ptr0)
201 return CheckOffsets(Offset0, Offset1);
202 }
203
204 if (BasePseudoVal0 && BasePseudoVal1 &&
205 BasePseudoVal0->kind() == BasePseudoVal1->kind() &&
206 BasePseudoVal0->kind() == PseudoSourceValue::FixedStack) {
207 // Spills/fills
208 auto FS0 = cast<FixedStackPseudoSourceValue>(BasePseudoVal0);
209 auto FS1 = cast<FixedStackPseudoSourceValue>(BasePseudoVal1);
210 Offset0 = MF.getFrameInfo().getObjectOffset(FS0->getFrameIndex());
211 Offset1 = MF.getFrameInfo().getObjectOffset(FS1->getFrameIndex());
212 return CheckOffsets(Offset0, Offset1);
213 }
214
215 // Constant pools (likely in ITCM)
216 if (BasePseudoVal0 && BasePseudoVal1 &&
217 BasePseudoVal0->kind() == BasePseudoVal1->kind() &&
218 BasePseudoVal0->isConstantPool() && AssumeITCMBankConflict)
219 return Hazard;
220
221 // Is this a stack pointer-relative access? We could in general try to
222 // use "is this the same register and is it unchanged?", but the
223 // memory operand tracking is highly likely to have already found that.
224 // What we're after here is bank conflicts between different objects in
225 // the stack frame.
226 if (!SPvalid) { // set up SP
227 if (!getBaseOffset(L0, SP, SPOffset0) || SP->getReg().id() != ARM::SP)
228 SP = nullptr;
229 SPvalid = true;
230 }
231 if (SP) {
232 int64_t SPOffset1;
233 const MachineOperand *SP1;
234 if (getBaseOffset(*L1, SP1, SPOffset1) && SP1->getReg().id() == ARM::SP)
235 return CheckOffsets(SPOffset0, SPOffset1);
236 }
237 }
238
239 return NoHazard;
240}
241
242void ARMBankConflictHazardRecognizer::Reset() { Accesses.clear(); }
243
245 MachineInstr &MI = *SU->getInstr();
246 if (!MI.mayLoad() || MI.mayStore() || MI.getNumMemOperands() != 1)
247 return;
248
249 auto MO = *MI.memoperands().begin();
250 LocationSize Size1 = MO->getSize();
251 if (Size1.hasValue() && Size1.getValue() > 4)
252 return;
253 Accesses.push_back(&MI);
254}
255
257
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
uint64_t uint64_t
static bool getBaseOffset(const MachineInstr &MI, const MachineOperand *&BaseOp, int64_t &Offset)
static bool hasRAWHazard(MachineInstr *DefMI, MachineInstr *MI, const TargetRegisterInfo &TRI)
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
void Reset() override
Reset - This callback is invoked when a new block of instructions is about to be schedule.
void AdvanceCycle() override
AdvanceCycle - This callback is invoked whenever the next top-down instruction to be scheduled cannot...
void EmitInstruction(SUnit *SU) override
EmitInstruction - This callback is invoked when an instruction is emitted, to advance the hazard stat...
ARMBankConflictHazardRecognizer(const ScheduleDAG *DAG, int64_t DDM, bool ABC)
void RecedeCycle() override
RecedeCycle - This callback is invoked whenever the next bottom-up instruction to be scheduled cannot...
HazardType getHazardType(SUnit *SU, int Stalls) override
getHazardType - Return the hazard type of emitting this node.
void EmitInstruction(SUnit *SU) override
EmitInstruction - This callback is invoked when an instruction is emitted, to advance the hazard stat...
void RecedeCycle() override
RecedeCycle - This callback is invoked whenever the next bottom-up instruction to be scheduled cannot...
void AdvanceCycle() override
AdvanceCycle - This callback is invoked whenever the next top-down instruction to be scheduled cannot...
void Reset() override
Reset - This callback is invoked when a new block of instructions is about to be schedule.
HazardType getHazardType(SUnit *SU, int Stalls) override
getHazardType - Return the hazard type of emitting this node.
bool hasValue() const
TypeSize getValue() const
Describe properties that are true of each instruction in the target description file.
MachineInstrBundleIterator< MachineInstr > iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Representation of each machine instruction.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
unsigned getNumMemOperands() const
Return the number of memory operands.
ArrayRef< MachineMemOperand * > memoperands() const
Access to memory operands of the instruction.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
constexpr unsigned id() const
Definition Register.h:100
Scheduling unit. This is a node in the scheduling DAG.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
unsigned MaxLookAhead
MaxLookAhead - Indicate the number of cycles in the scoreboard state.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
@ O1
Optimize quickly without destroying debuggability.
@ O0
Disable as many optimizations as possible.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559