LLVM 24.0.0git
AArch64StorePairSuppress.cpp
Go to the documentation of this file.
1//===--- AArch64StorePairSuppress.cpp --- Suppress store pair formation ---===//
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// This pass identifies floating point stores that should not be combined into
10// store pairs. Later we may do the same for floating point loads.
11// ===---------------------------------------------------------------------===//
12
13#include "AArch64InstrInfo.h"
14#include "AArch64Subtarget.h"
21#include "llvm/Support/Debug.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "aarch64-stp-suppress"
27
28#define STPSUPPRESS_PASS_NAME "AArch64 Store Pair Suppression"
29
30namespace {
31class AArch64StorePairSuppress : public MachineFunctionPass {
32 const AArch64InstrInfo *TII;
33 TargetSchedModel SchedModel;
34 MachineTraceMetrics *Traces;
36
37public:
38 static char ID;
39 AArch64StorePairSuppress() : MachineFunctionPass(ID) {}
40
41 StringRef getPassName() const override { return STPSUPPRESS_PASS_NAME; }
42
43 bool runOnMachineFunction(MachineFunction &F) override;
44
45private:
46 bool shouldAddSTPToBlock(const MachineBasicBlock *BB);
47
48 bool isNarrowFPStore(const MachineInstr &MI);
49
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.setPreservesCFG();
55 }
56};
57char AArch64StorePairSuppress::ID = 0;
58} // anonymous
59
60INITIALIZE_PASS(AArch64StorePairSuppress, "aarch64-stp-suppress",
61 STPSUPPRESS_PASS_NAME, false, false)
62
64 return new AArch64StorePairSuppress();
65}
66
67/// Return true if an STP can be added to this block without increasing the
68/// critical resource height. STP is good to form in Ld/St limited blocks and
69/// bad to form in float-point limited blocks. This is true independent of the
70/// critical path. If the critical path is longer than the resource height, the
71/// extra vector ops can limit physreg renaming. Otherwise, it could simply
72/// oversaturate the vector units.
73bool AArch64StorePairSuppress::shouldAddSTPToBlock(const MachineBasicBlock *BB) {
74 if (!MinInstr)
75 MinInstr = Traces->getEnsemble(MachineTraceStrategy::TS_MinInstrCount);
76
77 MachineTraceMetrics::Trace BBTrace = MinInstr->getTrace(BB);
78 unsigned ResLength = BBTrace.getResourceLength();
79
80 // Get the machine model's scheduling class for STPDi and STRDui.
81 // Bypass TargetSchedule's SchedClass resolution since we only have an opcode.
82 unsigned SCIdx = TII->get(AArch64::STPDi).getSchedClass();
83 const MCSchedClassDesc *PairSCDesc =
84 SchedModel.getMCSchedModel()->getSchedClassDesc(SCIdx);
85
86 unsigned SCIdx2 = TII->get(AArch64::STRDui).getSchedClass();
87 const MCSchedClassDesc *SingleSCDesc =
88 SchedModel.getMCSchedModel()->getSchedClassDesc(SCIdx2);
89
90 // If a subtarget does not define resources for STPDi, bail here.
91 if (PairSCDesc->isValid() && !PairSCDesc->isVariant() &&
92 SingleSCDesc->isValid() && !SingleSCDesc->isVariant()) {
93 // Compute the new critical resource length after replacing 2 separate
94 // STRDui with one STPDi.
95 unsigned ResLenWithSTP =
96 BBTrace.getResourceLength({}, PairSCDesc, {SingleSCDesc, SingleSCDesc});
97 if (ResLenWithSTP > ResLength) {
98 LLVM_DEBUG(dbgs() << " Suppress STP in BB: " << BB->getNumber()
99 << " resources " << ResLength << " -> " << ResLenWithSTP
100 << "\n");
101 return false;
102 }
103 }
104 return true;
105}
106
107/// Return true if this is a floating-point store smaller than the V reg. On
108/// cyclone, these require a vector shuffle before storing a pair.
109/// Ideally we would call getMatchingPairOpcode() and have the machine model
110/// tell us if it's profitable with no cpu knowledge here.
111///
112/// FIXME: We plan to develop a decent Target abstraction for simple loads and
113/// stores. Until then use a nasty switch similar to AArch64LoadStoreOptimizer.
114bool AArch64StorePairSuppress::isNarrowFPStore(const MachineInstr &MI) {
115 switch (MI.getOpcode()) {
116 default:
117 return false;
118 case AArch64::STRSui:
119 case AArch64::STRDui:
120 case AArch64::STURSi:
121 case AArch64::STURDi:
122 return true;
123 }
124}
125
126bool AArch64StorePairSuppress::runOnMachineFunction(MachineFunction &MF) {
127 if (skipFunction(MF.getFunction()) || MF.getFunction().hasOptSize())
128 return false;
129
130 const AArch64Subtarget &ST = MF.getSubtarget<AArch64Subtarget>();
131 if (!ST.enableStorePairSuppress())
132 return false;
133
134 TII = ST.getInstrInfo();
135 SchedModel.init(&ST);
136 Traces = &getAnalysis<MachineTraceMetricsWrapperPass>().getMTM();
137 MinInstr = nullptr;
138
139 LLVM_DEBUG(dbgs() << "*** " << getPassName() << ": " << MF.getName() << '\n');
140
141 if (!SchedModel.hasInstrSchedModel()) {
142 LLVM_DEBUG(dbgs() << " Skipping pass: no machine model present.\n");
143 return false;
144 }
145
146 // Check for a sequence of stores to the same base address. We don't need to
147 // precisely determine whether a store pair can be formed. But we do want to
148 // filter out most situations where we can't form store pairs to avoid
149 // computing trace metrics in those cases.
150 for (auto &MBB : MF) {
151 bool SuppressSTP = false;
152 unsigned PrevBaseReg = 0;
153 for (auto &MI : MBB) {
154 if (!isNarrowFPStore(MI))
155 continue;
156 const MachineOperand *BaseOp;
157 int64_t Offset;
158 bool OffsetIsScalable;
159 if (TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable) &&
160 BaseOp->isReg()) {
161 Register BaseReg = BaseOp->getReg();
162 if (PrevBaseReg == BaseReg) {
163 // If this block can take STPs, skip ahead to the next block.
164 if (!SuppressSTP && shouldAddSTPToBlock(MI.getParent()))
165 break;
166 // Otherwise, continue unpairing the stores in this block.
167 LLVM_DEBUG(dbgs() << "Unpairing store " << MI << "\n");
168 SuppressSTP = true;
169 TII->suppressLdStPair(MI);
170 }
171 PrevBaseReg = BaseReg;
172 } else
173 PrevBaseReg = 0;
174 }
175 }
176 // This pass just sets some internal MachineMemOperand flags. It can't really
177 // invalidate anything.
178 return false;
179}
#define STPSUPPRESS_PASS_NAME
MachineBasicBlock & MBB
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:708
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
A trace ensemble is a collection of traces selected using the same strategy, for example 'minimum res...
Trace getTrace(const MachineBasicBlock *MBB)
Get the trace that passes through MBB.
LLVM_ABI unsigned getResourceLength(ArrayRef< const MachineBasicBlock * > Extrablocks={}, ArrayRef< const MCSchedClassDesc * > ExtraInstrs={}, ArrayRef< const MCSchedClassDesc * > RemoveInstrs={}) const
Return the resource length of the trace.
LLVM_ABI Ensemble * getEnsemble(MachineTraceStrategy)
Get the trace ensemble representing the given trace selection strategy.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Provide an instruction scheduling machine model to CodeGen passes.
const MCSchedModel * getMCSchedModel() const
LLVM_ABI bool hasInstrSchedModel() const
Return true if this machine model includes an instruction-level scheduling model.
LLVM_ABI void init(const TargetSubtargetInfo *TSInfo, bool EnableSModel=true, bool EnableSItins=true)
Initialize the machine model for instruction scheduling.
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createAArch64StorePairSuppressPass()
bool isVariant() const
Definition MCSchedule.h:145
const MCSchedClassDesc * getSchedClassDesc(unsigned SchedClassIdx) const
Definition MCSchedule.h:376