Revert "AMDGPU: Add core backend files for R600/SI codegen v6"
[oota-llvm.git] / lib / Target / ARM / MLxExpansionPass.cpp
1 //===-- MLxExpansionPass.cpp - Expand MLx instrs to avoid hazards ---------===//
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 // Expand VFP / NEON floating point MLA / MLS instructions (each to a pair of
11 // multiple and add / sub instructions) when special VMLx hazards are detected.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "mlx-expansion"
16 #include "ARM.h"
17 #include "ARMBaseInstrInfo.h"
18 #include "ARMSubtarget.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30
31 static cl::opt<bool>
32 ForceExapnd("expand-all-fp-mlx", cl::init(false), cl::Hidden);
33 static cl::opt<unsigned>
34 ExpandLimit("expand-limit", cl::init(~0U), cl::Hidden);
35
36 STATISTIC(NumExpand, "Number of fp MLA / MLS instructions expanded");
37
38 namespace {
39   struct MLxExpansion : public MachineFunctionPass {
40     static char ID;
41     MLxExpansion() : MachineFunctionPass(ID) {}
42
43     virtual bool runOnMachineFunction(MachineFunction &Fn);
44
45     virtual const char *getPassName() const {
46       return "ARM MLA / MLS expansion pass";
47     }
48
49   private:
50     const ARMBaseInstrInfo *TII;
51     const TargetRegisterInfo *TRI;
52     MachineRegisterInfo *MRI;
53
54     bool isA9;
55     unsigned MIIdx;
56     MachineInstr* LastMIs[4];
57     SmallPtrSet<MachineInstr*, 4> IgnoreStall;
58
59     void clearStack();
60     void pushStack(MachineInstr *MI);
61     MachineInstr *getAccDefMI(MachineInstr *MI) const;
62     unsigned getDefReg(MachineInstr *MI) const;
63     bool hasRAWHazard(unsigned Reg, MachineInstr *MI) const;
64     bool FindMLxHazard(MachineInstr *MI);
65     void ExpandFPMLxInstruction(MachineBasicBlock &MBB, MachineInstr *MI,
66                                 unsigned MulOpc, unsigned AddSubOpc,
67                                 bool NegAcc, bool HasLane);
68     bool ExpandFPMLxInstructions(MachineBasicBlock &MBB);
69   };
70   char MLxExpansion::ID = 0;
71 }
72
73 void MLxExpansion::clearStack() {
74   std::fill(LastMIs, LastMIs + 4, (MachineInstr*)0);
75   MIIdx = 0;
76 }
77
78 void MLxExpansion::pushStack(MachineInstr *MI) {
79   LastMIs[MIIdx] = MI;
80   if (++MIIdx == 4)
81     MIIdx = 0;
82 }
83
84 MachineInstr *MLxExpansion::getAccDefMI(MachineInstr *MI) const {
85   // Look past COPY and INSERT_SUBREG instructions to find the
86   // real definition MI. This is important for _sfp instructions.
87   unsigned Reg = MI->getOperand(1).getReg();
88   if (TargetRegisterInfo::isPhysicalRegister(Reg))
89     return 0;
90
91   MachineBasicBlock *MBB = MI->getParent();
92   MachineInstr *DefMI = MRI->getVRegDef(Reg);
93   while (true) {
94     if (DefMI->getParent() != MBB)
95       break;
96     if (DefMI->isCopyLike()) {
97       Reg = DefMI->getOperand(1).getReg();
98       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
99         DefMI = MRI->getVRegDef(Reg);
100         continue;
101       }
102     } else if (DefMI->isInsertSubreg()) {
103       Reg = DefMI->getOperand(2).getReg();
104       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
105         DefMI = MRI->getVRegDef(Reg);
106         continue;
107       }
108     }
109     break;
110   }
111   return DefMI;
112 }
113
114 unsigned MLxExpansion::getDefReg(MachineInstr *MI) const {
115   unsigned Reg = MI->getOperand(0).getReg();
116   if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
117       !MRI->hasOneNonDBGUse(Reg))
118     return Reg;
119
120   MachineBasicBlock *MBB = MI->getParent();
121   MachineInstr *UseMI = &*MRI->use_nodbg_begin(Reg);
122   if (UseMI->getParent() != MBB)
123     return Reg;
124
125   while (UseMI->isCopy() || UseMI->isInsertSubreg()) {
126     Reg = UseMI->getOperand(0).getReg();
127     if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
128         !MRI->hasOneNonDBGUse(Reg))
129       return Reg;
130     UseMI = &*MRI->use_nodbg_begin(Reg);
131     if (UseMI->getParent() != MBB)
132       return Reg;
133   }
134
135   return Reg;
136 }
137
138 bool MLxExpansion::hasRAWHazard(unsigned Reg, MachineInstr *MI) const {
139   // FIXME: Detect integer instructions properly.
140   const MCInstrDesc &MCID = MI->getDesc();
141   unsigned Domain = MCID.TSFlags & ARMII::DomainMask;
142   if (MI->mayStore())
143     return false;
144   unsigned Opcode = MCID.getOpcode();
145   if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
146     return false;
147   if ((Domain & ARMII::DomainVFP) || (Domain & ARMII::DomainNEON))
148     return MI->readsRegister(Reg, TRI);
149   return false;
150 }
151
152
153 bool MLxExpansion::FindMLxHazard(MachineInstr *MI) {
154   if (NumExpand >= ExpandLimit)
155     return false;
156
157   if (ForceExapnd)
158     return true;
159
160   MachineInstr *DefMI = getAccDefMI(MI);
161   if (TII->isFpMLxInstruction(DefMI->getOpcode())) {
162     // r0 = vmla
163     // r3 = vmla r0, r1, r2
164     // takes 16 - 17 cycles
165     //
166     // r0 = vmla
167     // r4 = vmul r1, r2
168     // r3 = vadd r0, r4
169     // takes about 14 - 15 cycles even with vmul stalling for 4 cycles.
170     IgnoreStall.insert(DefMI);
171     return true;
172   }
173
174   if (IgnoreStall.count(MI))
175     return false;
176
177   // If a VMLA.F is followed by an VADD.F or VMUL.F with no RAW hazard, the
178   // VADD.F or VMUL.F will stall 4 cycles before issue. The 4 cycle stall
179   // preserves the in-order retirement of the instructions.
180   // Look at the next few instructions, if *most* of them can cause hazards,
181   // then the scheduler can't *fix* this, we'd better break up the VMLA.
182   unsigned Limit1 = isA9 ? 1 : 4;
183   unsigned Limit2 = isA9 ? 1 : 4;
184   for (unsigned i = 1; i <= 4; ++i) {
185     int Idx = ((int)MIIdx - i + 4) % 4;
186     MachineInstr *NextMI = LastMIs[Idx];
187     if (!NextMI)
188       continue;
189
190     if (TII->canCauseFpMLxStall(NextMI->getOpcode())) {
191       if (i <= Limit1)
192         return true;
193     }
194
195     // Look for VMLx RAW hazard.
196     if (i <= Limit2 && hasRAWHazard(getDefReg(MI), NextMI))
197       return true;
198   }
199
200   return false;
201 }
202
203 /// ExpandFPMLxInstructions - Expand a MLA / MLS instruction into a pair
204 /// of MUL + ADD / SUB instructions.
205 void
206 MLxExpansion::ExpandFPMLxInstruction(MachineBasicBlock &MBB, MachineInstr *MI,
207                                      unsigned MulOpc, unsigned AddSubOpc,
208                                      bool NegAcc, bool HasLane) {
209   unsigned DstReg = MI->getOperand(0).getReg();
210   bool DstDead = MI->getOperand(0).isDead();
211   unsigned AccReg = MI->getOperand(1).getReg();
212   unsigned Src1Reg = MI->getOperand(2).getReg();
213   unsigned Src2Reg = MI->getOperand(3).getReg();
214   bool Src1Kill = MI->getOperand(2).isKill();
215   bool Src2Kill = MI->getOperand(3).isKill();
216   unsigned LaneImm = HasLane ? MI->getOperand(4).getImm() : 0;
217   unsigned NextOp = HasLane ? 5 : 4;
218   ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NextOp).getImm();
219   unsigned PredReg = MI->getOperand(++NextOp).getReg();
220
221   const MCInstrDesc &MCID1 = TII->get(MulOpc);
222   const MCInstrDesc &MCID2 = TII->get(AddSubOpc);
223   const MachineFunction &MF = *MI->getParent()->getParent();
224   unsigned TmpReg = MRI->createVirtualRegister(
225                       TII->getRegClass(MCID1, 0, TRI, MF));
226
227   MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(), MCID1, TmpReg)
228     .addReg(Src1Reg, getKillRegState(Src1Kill))
229     .addReg(Src2Reg, getKillRegState(Src2Kill));
230   if (HasLane)
231     MIB.addImm(LaneImm);
232   MIB.addImm(Pred).addReg(PredReg);
233
234   MIB = BuildMI(MBB, MI, MI->getDebugLoc(), MCID2)
235     .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstDead));
236
237   if (NegAcc) {
238     bool AccKill = MRI->hasOneNonDBGUse(AccReg);
239     MIB.addReg(TmpReg, getKillRegState(true))
240        .addReg(AccReg, getKillRegState(AccKill));
241   } else {
242     MIB.addReg(AccReg).addReg(TmpReg, getKillRegState(true));
243   }
244   MIB.addImm(Pred).addReg(PredReg);
245
246   DEBUG({
247       dbgs() << "Expanding: " << *MI;
248       dbgs() << "  to:\n";
249       MachineBasicBlock::iterator MII = MI;
250       MII = llvm::prior(MII);
251       MachineInstr &MI2 = *MII;
252       MII = llvm::prior(MII);
253       MachineInstr &MI1 = *MII;
254       dbgs() << "    " << MI1;
255       dbgs() << "    " << MI2;
256    });
257
258   MI->eraseFromParent();
259   ++NumExpand;
260 }
261
262 bool MLxExpansion::ExpandFPMLxInstructions(MachineBasicBlock &MBB) {
263   bool Changed = false;
264
265   clearStack();
266   IgnoreStall.clear();
267
268   unsigned Skip = 0;
269   MachineBasicBlock::reverse_iterator MII = MBB.rbegin(), E = MBB.rend();
270   while (MII != E) {
271     MachineInstr *MI = &*MII;
272
273     if (MI->isLabel() || MI->isImplicitDef() || MI->isCopy()) {
274       ++MII;
275       continue;
276     }
277
278     const MCInstrDesc &MCID = MI->getDesc();
279     if (MI->isBarrier()) {
280       clearStack();
281       Skip = 0;
282       ++MII;
283       continue;
284     }
285
286     unsigned Domain = MCID.TSFlags & ARMII::DomainMask;
287     if (Domain == ARMII::DomainGeneral) {
288       if (++Skip == 2)
289         // Assume dual issues of non-VFP / NEON instructions.
290         pushStack(0);
291     } else {
292       Skip = 0;
293
294       unsigned MulOpc, AddSubOpc;
295       bool NegAcc, HasLane;
296       if (!TII->isFpMLxInstruction(MCID.getOpcode(),
297                                    MulOpc, AddSubOpc, NegAcc, HasLane) ||
298           !FindMLxHazard(MI))
299         pushStack(MI);
300       else {
301         ExpandFPMLxInstruction(MBB, MI, MulOpc, AddSubOpc, NegAcc, HasLane);
302         E = MBB.rend(); // May have changed if MI was the 1st instruction.
303         Changed = true;
304         continue;
305       }
306     }
307
308     ++MII;
309   }
310
311   return Changed;
312 }
313
314 bool MLxExpansion::runOnMachineFunction(MachineFunction &Fn) {
315   TII = static_cast<const ARMBaseInstrInfo*>(Fn.getTarget().getInstrInfo());
316   TRI = Fn.getTarget().getRegisterInfo();
317   MRI = &Fn.getRegInfo();
318   const ARMSubtarget *STI = &Fn.getTarget().getSubtarget<ARMSubtarget>();
319   isA9 = STI->isCortexA9();
320
321   bool Modified = false;
322   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
323        ++MFI) {
324     MachineBasicBlock &MBB = *MFI;
325     Modified |= ExpandFPMLxInstructions(MBB);
326   }
327
328   return Modified;
329 }
330
331 FunctionPass *llvm::createMLxExpansionPass() {
332   return new MLxExpansion();
333 }