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