blob: ec7257b2d144fb7e7e0d2371cdeb9b26662caa2e [file] [log] [blame]
Evan Cheng48575f62010-12-05 22:04:16 +00001//===-- 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 "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/Target/TargetRegisterInfo.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SmallSet.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"
29using namespace llvm;
30
31static cl::opt<bool>
32ForceExapnd("expand-all-fp-mlx", cl::init(false), cl::Hidden);
33static cl::opt<unsigned>
34ExpandLimit("expand-limit", cl::init(~0U), cl::Hidden);
35
36STATISTIC(NumExpand, "Number of fp MLA / MLS instructions expanded");
37
38namespace {
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 unsigned HazardLimit;
55 unsigned MIIdx;
56 MachineInstr* LastMIs[4];
57
58 void clearStack();
59 void pushStack(MachineInstr *MI);
60 MachineInstr *getAccDefMI(MachineInstr *MI) const;
61 unsigned getDefReg(MachineInstr *MI) const;
62 bool hasRAWHazard(unsigned Reg, MachineInstr *MI) const;
63 bool FindMLxHazard(MachineInstr *MI) const;
64 void ExpandFPMLxInstruction(MachineBasicBlock &MBB, MachineInstr *MI,
65 unsigned MulOpc, unsigned AddSubOpc,
66 bool NegAcc, bool HasLane);
67 bool ExpandFPMLxInstructions(MachineBasicBlock &MBB);
68 };
69 char MLxExpansion::ID = 0;
70}
71
72void MLxExpansion::clearStack() {
73 std::fill(LastMIs, LastMIs + 4, (MachineInstr*)0);
74 MIIdx = 0;
75}
76
77void MLxExpansion::pushStack(MachineInstr *MI) {
78 LastMIs[MIIdx] = MI;
79 if (++MIIdx == 4)
80 MIIdx = 0;
81}
82
83MachineInstr *MLxExpansion::getAccDefMI(MachineInstr *MI) const {
84 // Look past COPY and INSERT_SUBREG instructions to find the
85 // real definition MI. This is important for _sfp instructions.
86 unsigned Reg = MI->getOperand(1).getReg();
87 if (TargetRegisterInfo::isPhysicalRegister(Reg))
88 return 0;
89
90 MachineBasicBlock *MBB = MI->getParent();
91 MachineInstr *DefMI = MRI->getVRegDef(Reg);
92 while (true) {
93 if (DefMI->getParent() != MBB)
94 break;
95 if (DefMI->isCopyLike()) {
96 Reg = DefMI->getOperand(1).getReg();
97 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
98 DefMI = MRI->getVRegDef(Reg);
99 continue;
100 }
101 } else if (DefMI->isInsertSubreg()) {
102 Reg = DefMI->getOperand(2).getReg();
103 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
104 DefMI = MRI->getVRegDef(Reg);
105 continue;
106 }
107 }
108 break;
109 }
110 return DefMI;
111}
112
113unsigned MLxExpansion::getDefReg(MachineInstr *MI) const {
114 unsigned Reg = MI->getOperand(0).getReg();
115 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
116 !MRI->hasOneNonDBGUse(Reg))
117 return Reg;
118
119 MachineBasicBlock *MBB = MI->getParent();
120 MachineInstr *UseMI = &*MRI->use_nodbg_begin(Reg);
121 if (UseMI->getParent() != MBB)
122 return Reg;
123
124 while (UseMI->isCopy() || UseMI->isInsertSubreg()) {
125 Reg = UseMI->getOperand(0).getReg();
126 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
127 !MRI->hasOneNonDBGUse(Reg))
128 return Reg;
129 UseMI = &*MRI->use_nodbg_begin(Reg);
130 if (UseMI->getParent() != MBB)
131 return Reg;
132 }
133
134 return Reg;
135}
136
137bool MLxExpansion::hasRAWHazard(unsigned Reg, MachineInstr *MI) const {
138 const TargetInstrDesc &TID = MI->getDesc();
139 // FIXME: Detect integer instructions properly.
140 unsigned Domain = TID.TSFlags & ARMII::DomainMask;
141 if (Domain == ARMII::DomainVFP) {
142 unsigned Opcode = TID.getOpcode();
143 if (Opcode == ARM::VSTRS || Opcode == ARM::VSTRD ||
144 Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
145 return false;
146 } else if (Domain == ARMII::DomainNEON) {
147 if (TID.mayStore() || TID.mayLoad())
148 return false;
149 } else {
150 return false;
151 }
152
153 return MI->readsRegister(Reg, TRI);
154 return false;
155}
156
157
158bool MLxExpansion::FindMLxHazard(MachineInstr *MI) const {
159 if (NumExpand >= ExpandLimit)
160 return false;
161
162 if (ForceExapnd)
163 return true;
164
165 MachineInstr *DefMI = getAccDefMI(MI);
166 if (TII->isFpMLxInstruction(DefMI->getOpcode()))
167 // r0 = vmla
168 // r3 = vmla r0, r1, r2
169 // takes 16 - 17 cycles
170 //
171 // r0 = vmla
172 // r4 = vmul r1, r2
173 // r3 = vadd r0, r4
174 // takes about 14 - 15 cycles even with vmul stalling for 4 cycles.
175 return true;
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 for (unsigned i = 1; i <= 4; ++i) {
183 int Idx = ((int)MIIdx - i + 4) % 4;
184 MachineInstr *NextMI = LastMIs[Idx];
185 if (!NextMI)
186 continue;
187
188 if (TII->canCauseFpMLxStall(NextMI->getOpcode()))
189 return true;
190
191 // Look for VMLx RAW hazard.
192 if (hasRAWHazard(getDefReg(MI), NextMI))
193 return true;
194 }
195
196 return false;
197}
198
199/// ExpandFPMLxInstructions - Expand a MLA / MLS instruction into a pair
200/// of MUL + ADD / SUB instructions.
201void
202MLxExpansion::ExpandFPMLxInstruction(MachineBasicBlock &MBB, MachineInstr *MI,
203 unsigned MulOpc, unsigned AddSubOpc,
204 bool NegAcc, bool HasLane) {
205 unsigned DstReg = MI->getOperand(0).getReg();
206 bool DstDead = MI->getOperand(0).isDead();
207 unsigned AccReg = MI->getOperand(1).getReg();
208 unsigned Src1Reg = MI->getOperand(2).getReg();
209 unsigned Src2Reg = MI->getOperand(3).getReg();
210 bool Src1Kill = MI->getOperand(2).isKill();
211 bool Src2Kill = MI->getOperand(3).isKill();
212 unsigned LaneImm = HasLane ? MI->getOperand(4).getImm() : 0;
213 unsigned NextOp = HasLane ? 5 : 4;
214 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NextOp).getImm();
215 unsigned PredReg = MI->getOperand(++NextOp).getReg();
216
217 const TargetInstrDesc &TID1 = TII->get(MulOpc);
218 const TargetInstrDesc &TID2 = TII->get(AddSubOpc);
219 unsigned TmpReg = MRI->createVirtualRegister(TID1.getRegClass(0, TRI));
220
221 MachineInstrBuilder MIB = BuildMI(MBB, *MI, MI->getDebugLoc(), TID1, TmpReg)
222 .addReg(Src1Reg, getKillRegState(Src1Kill))
223 .addReg(Src2Reg, getKillRegState(Src2Kill));
224 if (HasLane)
225 MIB.addImm(LaneImm);
226 MIB.addImm(Pred).addReg(PredReg);
227
228 MIB = BuildMI(MBB, *MI, MI->getDebugLoc(), TID2)
229 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstDead));
230
231 if (NegAcc) {
232 bool AccKill = MRI->hasOneNonDBGUse(AccReg);
233 MIB.addReg(TmpReg, getKillRegState(true))
234 .addReg(AccReg, getKillRegState(AccKill));
235 } else {
236 MIB.addReg(AccReg).addReg(TmpReg, getKillRegState(true));
237 }
238 MIB.addImm(Pred).addReg(PredReg);
239
240 DEBUG({
241 dbgs() << "Expanding: " << *MI;
242 dbgs() << " to:\n";
243 MachineBasicBlock::iterator MII = MI;
244 MII = llvm::prior(MII);
245 MachineInstr &MI2 = *MII;
246 MII = llvm::prior(MII);
247 MachineInstr &MI1 = *MII;
248 dbgs() << " " << MI1;
249 dbgs() << " " << MI2;
250 });
251
252 MI->eraseFromParent();
253 ++NumExpand;
254}
255
256bool MLxExpansion::ExpandFPMLxInstructions(MachineBasicBlock &MBB) {
257 bool Changed = false;
258
259 clearStack();
260
261 unsigned Skip = 0;
262 MachineBasicBlock::reverse_iterator MII = MBB.rbegin(), E = MBB.rend();
263 while (MII != E) {
264 MachineInstr *MI = &*MII;
265
266 if (MI->isLabel() || MI->isImplicitDef() || MI->isCopy()) {
267 ++MII;
268 continue;
269 }
270
271 const TargetInstrDesc &TID = MI->getDesc();
272 if (TID.isBarrier()) {
273 clearStack();
274 Skip = 0;
275 ++MII;
276 continue;
277 }
278
279 unsigned Domain = TID.TSFlags & ARMII::DomainMask;
280 if (Domain == ARMII::DomainGeneral) {
281 if (++Skip == 2)
282 // Assume dual issues of non-VFP / NEON instructions.
283 pushStack(0);
284 } else {
285 Skip = 0;
286
287 unsigned MulOpc, AddSubOpc;
288 bool NegAcc, HasLane;
289 if (!TII->isFpMLxInstruction(TID.getOpcode(),
290 MulOpc, AddSubOpc, NegAcc, HasLane) ||
291 !FindMLxHazard(MI))
292 pushStack(MI);
293 else {
294 ExpandFPMLxInstruction(MBB, MI, MulOpc, AddSubOpc, NegAcc, HasLane);
295 E = MBB.rend(); // May have changed if MI was the 1st instruction.
296 Changed = true;
297 continue;
298 }
299 }
300
301 ++MII;
302 }
303
304 return Changed;
305}
306
307bool MLxExpansion::runOnMachineFunction(MachineFunction &Fn) {
308 TII = static_cast<const ARMBaseInstrInfo*>(Fn.getTarget().getInstrInfo());
309 TRI = Fn.getTarget().getRegisterInfo();
310 MRI = &Fn.getRegInfo();
311
312 bool Modified = false;
313 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
314 ++MFI) {
315 MachineBasicBlock &MBB = *MFI;
316 Modified |= ExpandFPMLxInstructions(MBB);
317 }
318
319 return Modified;
320}
321
322FunctionPass *llvm::createMLxExpansionPass() {
323 return new MLxExpansion();
324}