xref: /aosp_15_r20/external/llvm/lib/Target/AArch64/AArch64StorePairSuppress.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===--- AArch64StorePairSuppress.cpp --- Suppress store pair formation ---===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass identifies floating point stores that should not be combined into
11*9880d681SAndroid Build Coastguard Worker // store pairs. Later we may do the same for floating point loads.
12*9880d681SAndroid Build Coastguard Worker // ===---------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "AArch64InstrInfo.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineTraceMetrics.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/TargetSchedule.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
23*9880d681SAndroid Build Coastguard Worker 
24*9880d681SAndroid Build Coastguard Worker using namespace llvm;
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "aarch64-stp-suppress"
27*9880d681SAndroid Build Coastguard Worker 
28*9880d681SAndroid Build Coastguard Worker namespace {
29*9880d681SAndroid Build Coastguard Worker class AArch64StorePairSuppress : public MachineFunctionPass {
30*9880d681SAndroid Build Coastguard Worker   const AArch64InstrInfo *TII;
31*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI;
32*9880d681SAndroid Build Coastguard Worker   const MachineRegisterInfo *MRI;
33*9880d681SAndroid Build Coastguard Worker   TargetSchedModel SchedModel;
34*9880d681SAndroid Build Coastguard Worker   MachineTraceMetrics *Traces;
35*9880d681SAndroid Build Coastguard Worker   MachineTraceMetrics::Ensemble *MinInstr;
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker public:
38*9880d681SAndroid Build Coastguard Worker   static char ID;
AArch64StorePairSuppress()39*9880d681SAndroid Build Coastguard Worker   AArch64StorePairSuppress() : MachineFunctionPass(ID) {}
40*9880d681SAndroid Build Coastguard Worker 
getPassName() const41*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override {
42*9880d681SAndroid Build Coastguard Worker     return "AArch64 Store Pair Suppression";
43*9880d681SAndroid Build Coastguard Worker   }
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &F) override;
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker private:
48*9880d681SAndroid Build Coastguard Worker   bool shouldAddSTPToBlock(const MachineBasicBlock *BB);
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker   bool isNarrowFPStore(const MachineInstr &MI);
51*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const52*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override {
53*9880d681SAndroid Build Coastguard Worker     AU.setPreservesCFG();
54*9880d681SAndroid Build Coastguard Worker     AU.addRequired<MachineTraceMetrics>();
55*9880d681SAndroid Build Coastguard Worker     AU.addPreserved<MachineTraceMetrics>();
56*9880d681SAndroid Build Coastguard Worker     MachineFunctionPass::getAnalysisUsage(AU);
57*9880d681SAndroid Build Coastguard Worker   }
58*9880d681SAndroid Build Coastguard Worker };
59*9880d681SAndroid Build Coastguard Worker char AArch64StorePairSuppress::ID = 0;
60*9880d681SAndroid Build Coastguard Worker } // anonymous
61*9880d681SAndroid Build Coastguard Worker 
createAArch64StorePairSuppressPass()62*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createAArch64StorePairSuppressPass() {
63*9880d681SAndroid Build Coastguard Worker   return new AArch64StorePairSuppress();
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker /// Return true if an STP can be added to this block without increasing the
67*9880d681SAndroid Build Coastguard Worker /// critical resource height. STP is good to form in Ld/St limited blocks and
68*9880d681SAndroid Build Coastguard Worker /// bad to form in float-point limited blocks. This is true independent of the
69*9880d681SAndroid Build Coastguard Worker /// critical path. If the critical path is longer than the resource height, the
70*9880d681SAndroid Build Coastguard Worker /// extra vector ops can limit physreg renaming. Otherwise, it could simply
71*9880d681SAndroid Build Coastguard Worker /// oversaturate the vector units.
shouldAddSTPToBlock(const MachineBasicBlock * BB)72*9880d681SAndroid Build Coastguard Worker bool AArch64StorePairSuppress::shouldAddSTPToBlock(const MachineBasicBlock *BB) {
73*9880d681SAndroid Build Coastguard Worker   if (!MinInstr)
74*9880d681SAndroid Build Coastguard Worker     MinInstr = Traces->getEnsemble(MachineTraceMetrics::TS_MinInstrCount);
75*9880d681SAndroid Build Coastguard Worker 
76*9880d681SAndroid Build Coastguard Worker   MachineTraceMetrics::Trace BBTrace = MinInstr->getTrace(BB);
77*9880d681SAndroid Build Coastguard Worker   unsigned ResLength = BBTrace.getResourceLength();
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker   // Get the machine model's scheduling class for STPQi.
80*9880d681SAndroid Build Coastguard Worker   // Bypass TargetSchedule's SchedClass resolution since we only have an opcode.
81*9880d681SAndroid Build Coastguard Worker   unsigned SCIdx = TII->get(AArch64::STPDi).getSchedClass();
82*9880d681SAndroid Build Coastguard Worker   const MCSchedClassDesc *SCDesc =
83*9880d681SAndroid Build Coastguard Worker       SchedModel.getMCSchedModel()->getSchedClassDesc(SCIdx);
84*9880d681SAndroid Build Coastguard Worker 
85*9880d681SAndroid Build Coastguard Worker   // If a subtarget does not define resources for STPQi, bail here.
86*9880d681SAndroid Build Coastguard Worker   if (SCDesc->isValid() && !SCDesc->isVariant()) {
87*9880d681SAndroid Build Coastguard Worker     unsigned ResLenWithSTP = BBTrace.getResourceLength(None, SCDesc);
88*9880d681SAndroid Build Coastguard Worker     if (ResLenWithSTP > ResLength) {
89*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "  Suppress STP in BB: " << BB->getNumber()
90*9880d681SAndroid Build Coastguard Worker                    << " resources " << ResLength << " -> " << ResLenWithSTP
91*9880d681SAndroid Build Coastguard Worker                    << "\n");
92*9880d681SAndroid Build Coastguard Worker       return false;
93*9880d681SAndroid Build Coastguard Worker     }
94*9880d681SAndroid Build Coastguard Worker   }
95*9880d681SAndroid Build Coastguard Worker   return true;
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker /// Return true if this is a floating-point store smaller than the V reg. On
99*9880d681SAndroid Build Coastguard Worker /// cyclone, these require a vector shuffle before storing a pair.
100*9880d681SAndroid Build Coastguard Worker /// Ideally we would call getMatchingPairOpcode() and have the machine model
101*9880d681SAndroid Build Coastguard Worker /// tell us if it's profitable with no cpu knowledge here.
102*9880d681SAndroid Build Coastguard Worker ///
103*9880d681SAndroid Build Coastguard Worker /// FIXME: We plan to develop a decent Target abstraction for simple loads and
104*9880d681SAndroid Build Coastguard Worker /// stores. Until then use a nasty switch similar to AArch64LoadStoreOptimizer.
isNarrowFPStore(const MachineInstr & MI)105*9880d681SAndroid Build Coastguard Worker bool AArch64StorePairSuppress::isNarrowFPStore(const MachineInstr &MI) {
106*9880d681SAndroid Build Coastguard Worker   switch (MI.getOpcode()) {
107*9880d681SAndroid Build Coastguard Worker   default:
108*9880d681SAndroid Build Coastguard Worker     return false;
109*9880d681SAndroid Build Coastguard Worker   case AArch64::STRSui:
110*9880d681SAndroid Build Coastguard Worker   case AArch64::STRDui:
111*9880d681SAndroid Build Coastguard Worker   case AArch64::STURSi:
112*9880d681SAndroid Build Coastguard Worker   case AArch64::STURDi:
113*9880d681SAndroid Build Coastguard Worker     return true;
114*9880d681SAndroid Build Coastguard Worker   }
115*9880d681SAndroid Build Coastguard Worker }
116*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & MF)117*9880d681SAndroid Build Coastguard Worker bool AArch64StorePairSuppress::runOnMachineFunction(MachineFunction &MF) {
118*9880d681SAndroid Build Coastguard Worker   if (skipFunction(*MF.getFunction()))
119*9880d681SAndroid Build Coastguard Worker     return false;
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker   const TargetSubtargetInfo &ST = MF.getSubtarget();
122*9880d681SAndroid Build Coastguard Worker   TII = static_cast<const AArch64InstrInfo *>(ST.getInstrInfo());
123*9880d681SAndroid Build Coastguard Worker   TRI = ST.getRegisterInfo();
124*9880d681SAndroid Build Coastguard Worker   MRI = &MF.getRegInfo();
125*9880d681SAndroid Build Coastguard Worker   SchedModel.init(ST.getSchedModel(), &ST, TII);
126*9880d681SAndroid Build Coastguard Worker   Traces = &getAnalysis<MachineTraceMetrics>();
127*9880d681SAndroid Build Coastguard Worker   MinInstr = nullptr;
128*9880d681SAndroid Build Coastguard Worker 
129*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "*** " << getPassName() << ": " << MF.getName() << '\n');
130*9880d681SAndroid Build Coastguard Worker 
131*9880d681SAndroid Build Coastguard Worker   if (!SchedModel.hasInstrSchedModel()) {
132*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "  Skipping pass: no machine model present.\n");
133*9880d681SAndroid Build Coastguard Worker     return false;
134*9880d681SAndroid Build Coastguard Worker   }
135*9880d681SAndroid Build Coastguard Worker 
136*9880d681SAndroid Build Coastguard Worker   // Check for a sequence of stores to the same base address. We don't need to
137*9880d681SAndroid Build Coastguard Worker   // precisely determine whether a store pair can be formed. But we do want to
138*9880d681SAndroid Build Coastguard Worker   // filter out most situations where we can't form store pairs to avoid
139*9880d681SAndroid Build Coastguard Worker   // computing trace metrics in those cases.
140*9880d681SAndroid Build Coastguard Worker   for (auto &MBB : MF) {
141*9880d681SAndroid Build Coastguard Worker     bool SuppressSTP = false;
142*9880d681SAndroid Build Coastguard Worker     unsigned PrevBaseReg = 0;
143*9880d681SAndroid Build Coastguard Worker     for (auto &MI : MBB) {
144*9880d681SAndroid Build Coastguard Worker       if (!isNarrowFPStore(MI))
145*9880d681SAndroid Build Coastguard Worker         continue;
146*9880d681SAndroid Build Coastguard Worker       unsigned BaseReg;
147*9880d681SAndroid Build Coastguard Worker       int64_t Offset;
148*9880d681SAndroid Build Coastguard Worker       if (TII->getMemOpBaseRegImmOfs(MI, BaseReg, Offset, TRI)) {
149*9880d681SAndroid Build Coastguard Worker         if (PrevBaseReg == BaseReg) {
150*9880d681SAndroid Build Coastguard Worker           // If this block can take STPs, skip ahead to the next block.
151*9880d681SAndroid Build Coastguard Worker           if (!SuppressSTP && shouldAddSTPToBlock(MI.getParent()))
152*9880d681SAndroid Build Coastguard Worker             break;
153*9880d681SAndroid Build Coastguard Worker           // Otherwise, continue unpairing the stores in this block.
154*9880d681SAndroid Build Coastguard Worker           DEBUG(dbgs() << "Unpairing store " << MI << "\n");
155*9880d681SAndroid Build Coastguard Worker           SuppressSTP = true;
156*9880d681SAndroid Build Coastguard Worker           TII->suppressLdStPair(MI);
157*9880d681SAndroid Build Coastguard Worker         }
158*9880d681SAndroid Build Coastguard Worker         PrevBaseReg = BaseReg;
159*9880d681SAndroid Build Coastguard Worker       } else
160*9880d681SAndroid Build Coastguard Worker         PrevBaseReg = 0;
161*9880d681SAndroid Build Coastguard Worker     }
162*9880d681SAndroid Build Coastguard Worker   }
163*9880d681SAndroid Build Coastguard Worker   // This pass just sets some internal MachineMemOperand flags. It can't really
164*9880d681SAndroid Build Coastguard Worker   // invalidate anything.
165*9880d681SAndroid Build Coastguard Worker   return false;
166*9880d681SAndroid Build Coastguard Worker }
167