blob: 7f212403398283149b5cd6fec8a8a6da059bb7e9 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- MLxExpansionPass.cpp - Expand MLx instrs to avoid hazards ---------===//
Evan Cheng62c7b5b2010-12-05 22:04:16 +00002//
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
Evan Cheng62c7b5b2010-12-05 22:04:16 +000015#include "ARM.h"
16#include "ARMBaseInstrInfo.h"
Bob Wilson0858c3a2011-04-19 18:11:57 +000017#include "ARMSubtarget.h"
Bob Wilson0858c3a2011-04-19 18:11:57 +000018#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000019#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstr.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000028using namespace llvm;
29
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "mlx-expansion"
31
Evan Cheng62c7b5b2010-12-05 22:04:16 +000032static cl::opt<bool>
33ForceExapnd("expand-all-fp-mlx", cl::init(false), cl::Hidden);
34static cl::opt<unsigned>
35ExpandLimit("expand-limit", cl::init(~0U), cl::Hidden);
36
37STATISTIC(NumExpand, "Number of fp MLA / MLS instructions expanded");
38
39namespace {
40 struct MLxExpansion : public MachineFunctionPass {
41 static char ID;
42 MLxExpansion() : MachineFunctionPass(ID) {}
43
Craig Topper6bc27bf2014-03-10 02:09:33 +000044 bool runOnMachineFunction(MachineFunction &Fn) override;
Evan Cheng62c7b5b2010-12-05 22:04:16 +000045
Craig Topper6bc27bf2014-03-10 02:09:33 +000046 const char *getPassName() const override {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000047 return "ARM MLA / MLS expansion pass";
48 }
49
50 private:
51 const ARMBaseInstrInfo *TII;
52 const TargetRegisterInfo *TRI;
53 MachineRegisterInfo *MRI;
54
Silviu Barangab47bb942012-09-13 15:05:10 +000055 bool isLikeA9;
Bob Wilsone8a549c2012-09-29 21:43:49 +000056 bool isSwift;
Evan Cheng62c7b5b2010-12-05 22:04:16 +000057 unsigned MIIdx;
58 MachineInstr* LastMIs[4];
Bob Wilson0858c3a2011-04-19 18:11:57 +000059 SmallPtrSet<MachineInstr*, 4> IgnoreStall;
Evan Cheng62c7b5b2010-12-05 22:04:16 +000060
61 void clearStack();
62 void pushStack(MachineInstr *MI);
63 MachineInstr *getAccDefMI(MachineInstr *MI) const;
64 unsigned getDefReg(MachineInstr *MI) const;
Bob Wilsone8a549c2012-09-29 21:43:49 +000065 bool hasLoopHazard(MachineInstr *MI) const;
Evan Cheng62c7b5b2010-12-05 22:04:16 +000066 bool hasRAWHazard(unsigned Reg, MachineInstr *MI) const;
Bob Wilson0858c3a2011-04-19 18:11:57 +000067 bool FindMLxHazard(MachineInstr *MI);
Evan Cheng62c7b5b2010-12-05 22:04:16 +000068 void ExpandFPMLxInstruction(MachineBasicBlock &MBB, MachineInstr *MI,
69 unsigned MulOpc, unsigned AddSubOpc,
70 bool NegAcc, bool HasLane);
71 bool ExpandFPMLxInstructions(MachineBasicBlock &MBB);
72 };
73 char MLxExpansion::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000074}
Evan Cheng62c7b5b2010-12-05 22:04:16 +000075
76void MLxExpansion::clearStack() {
Craig Topper062a2ba2014-04-25 05:30:21 +000077 std::fill(LastMIs, LastMIs + 4, nullptr);
Evan Cheng62c7b5b2010-12-05 22:04:16 +000078 MIIdx = 0;
79}
80
81void MLxExpansion::pushStack(MachineInstr *MI) {
82 LastMIs[MIIdx] = MI;
83 if (++MIIdx == 4)
84 MIIdx = 0;
85}
86
87MachineInstr *MLxExpansion::getAccDefMI(MachineInstr *MI) const {
88 // Look past COPY and INSERT_SUBREG instructions to find the
89 // real definition MI. This is important for _sfp instructions.
90 unsigned Reg = MI->getOperand(1).getReg();
91 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +000092 return nullptr;
Evan Cheng62c7b5b2010-12-05 22:04:16 +000093
94 MachineBasicBlock *MBB = MI->getParent();
95 MachineInstr *DefMI = MRI->getVRegDef(Reg);
96 while (true) {
97 if (DefMI->getParent() != MBB)
98 break;
99 if (DefMI->isCopyLike()) {
100 Reg = DefMI->getOperand(1).getReg();
101 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
102 DefMI = MRI->getVRegDef(Reg);
103 continue;
104 }
105 } else if (DefMI->isInsertSubreg()) {
106 Reg = DefMI->getOperand(2).getReg();
107 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
108 DefMI = MRI->getVRegDef(Reg);
109 continue;
110 }
111 }
112 break;
113 }
114 return DefMI;
115}
116
117unsigned MLxExpansion::getDefReg(MachineInstr *MI) const {
118 unsigned Reg = MI->getOperand(0).getReg();
119 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
120 !MRI->hasOneNonDBGUse(Reg))
121 return Reg;
122
123 MachineBasicBlock *MBB = MI->getParent();
Owen Anderson16c6bf42014-03-13 23:12:04 +0000124 MachineInstr *UseMI = &*MRI->use_instr_nodbg_begin(Reg);
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000125 if (UseMI->getParent() != MBB)
126 return Reg;
127
128 while (UseMI->isCopy() || UseMI->isInsertSubreg()) {
129 Reg = UseMI->getOperand(0).getReg();
130 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
131 !MRI->hasOneNonDBGUse(Reg))
132 return Reg;
Owen Anderson16c6bf42014-03-13 23:12:04 +0000133 UseMI = &*MRI->use_instr_nodbg_begin(Reg);
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000134 if (UseMI->getParent() != MBB)
135 return Reg;
136 }
137
138 return Reg;
139}
140
Bob Wilsone8a549c2012-09-29 21:43:49 +0000141/// hasLoopHazard - Check whether an MLx instruction is chained to itself across
142/// a single-MBB loop.
143bool MLxExpansion::hasLoopHazard(MachineInstr *MI) const {
144 unsigned Reg = MI->getOperand(1).getReg();
145 if (TargetRegisterInfo::isPhysicalRegister(Reg))
146 return false;
147
148 MachineBasicBlock *MBB = MI->getParent();
149 MachineInstr *DefMI = MRI->getVRegDef(Reg);
150 while (true) {
151outer_continue:
152 if (DefMI->getParent() != MBB)
153 break;
154
155 if (DefMI->isPHI()) {
156 for (unsigned i = 1, e = DefMI->getNumOperands(); i < e; i += 2) {
157 if (DefMI->getOperand(i + 1).getMBB() == MBB) {
158 unsigned SrcReg = DefMI->getOperand(i).getReg();
159 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
160 DefMI = MRI->getVRegDef(SrcReg);
161 goto outer_continue;
162 }
163 }
164 }
165 } else if (DefMI->isCopyLike()) {
166 Reg = DefMI->getOperand(1).getReg();
167 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
168 DefMI = MRI->getVRegDef(Reg);
169 continue;
170 }
171 } else if (DefMI->isInsertSubreg()) {
172 Reg = DefMI->getOperand(2).getReg();
173 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
174 DefMI = MRI->getVRegDef(Reg);
175 continue;
176 }
177 }
178
179 break;
180 }
181
182 return DefMI == MI;
183}
184
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000185bool MLxExpansion::hasRAWHazard(unsigned Reg, MachineInstr *MI) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000186 // FIXME: Detect integer instructions properly.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000187 const MCInstrDesc &MCID = MI->getDesc();
188 unsigned Domain = MCID.TSFlags & ARMII::DomainMask;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000189 if (MI->mayStore())
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000190 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000191 unsigned Opcode = MCID.getOpcode();
Evan Cheng04ad35b2011-02-22 19:53:14 +0000192 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
193 return false;
194 if ((Domain & ARMII::DomainVFP) || (Domain & ARMII::DomainNEON))
195 return MI->readsRegister(Reg, TRI);
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000196 return false;
197}
198
Bob Wilsone8a549c2012-09-29 21:43:49 +0000199static bool isFpMulInstruction(unsigned Opcode) {
200 switch (Opcode) {
201 case ARM::VMULS:
202 case ARM::VMULfd:
203 case ARM::VMULfq:
204 case ARM::VMULD:
205 case ARM::VMULslfd:
206 case ARM::VMULslfq:
207 return true;
208 default:
209 return false;
210 }
211}
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000212
Bob Wilson0858c3a2011-04-19 18:11:57 +0000213bool MLxExpansion::FindMLxHazard(MachineInstr *MI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000214 if (NumExpand >= ExpandLimit)
215 return false;
216
217 if (ForceExapnd)
218 return true;
219
220 MachineInstr *DefMI = getAccDefMI(MI);
Bob Wilson0858c3a2011-04-19 18:11:57 +0000221 if (TII->isFpMLxInstruction(DefMI->getOpcode())) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000222 // r0 = vmla
223 // r3 = vmla r0, r1, r2
224 // takes 16 - 17 cycles
225 //
226 // r0 = vmla
227 // r4 = vmul r1, r2
228 // r3 = vadd r0, r4
229 // takes about 14 - 15 cycles even with vmul stalling for 4 cycles.
Bob Wilson0858c3a2011-04-19 18:11:57 +0000230 IgnoreStall.insert(DefMI);
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000231 return true;
Bob Wilson0858c3a2011-04-19 18:11:57 +0000232 }
233
Bob Wilsone8a549c2012-09-29 21:43:49 +0000234 // On Swift, we mostly care about hazards from multiplication instructions
235 // writing the accumulator and the pipelining of loop iterations by out-of-
236 // order execution.
237 if (isSwift)
238 return isFpMulInstruction(DefMI->getOpcode()) || hasLoopHazard(MI);
239
Bob Wilson0858c3a2011-04-19 18:11:57 +0000240 if (IgnoreStall.count(MI))
241 return false;
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000242
243 // If a VMLA.F is followed by an VADD.F or VMUL.F with no RAW hazard, the
244 // VADD.F or VMUL.F will stall 4 cycles before issue. The 4 cycle stall
245 // preserves the in-order retirement of the instructions.
246 // Look at the next few instructions, if *most* of them can cause hazards,
247 // then the scheduler can't *fix* this, we'd better break up the VMLA.
Silviu Barangab47bb942012-09-13 15:05:10 +0000248 unsigned Limit1 = isLikeA9 ? 1 : 4;
249 unsigned Limit2 = isLikeA9 ? 1 : 4;
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000250 for (unsigned i = 1; i <= 4; ++i) {
251 int Idx = ((int)MIIdx - i + 4) % 4;
252 MachineInstr *NextMI = LastMIs[Idx];
253 if (!NextMI)
254 continue;
255
Bob Wilson0858c3a2011-04-19 18:11:57 +0000256 if (TII->canCauseFpMLxStall(NextMI->getOpcode())) {
257 if (i <= Limit1)
258 return true;
259 }
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000260
261 // Look for VMLx RAW hazard.
Bob Wilson0858c3a2011-04-19 18:11:57 +0000262 if (i <= Limit2 && hasRAWHazard(getDefReg(MI), NextMI))
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000263 return true;
264 }
265
266 return false;
267}
268
269/// ExpandFPMLxInstructions - Expand a MLA / MLS instruction into a pair
270/// of MUL + ADD / SUB instructions.
271void
272MLxExpansion::ExpandFPMLxInstruction(MachineBasicBlock &MBB, MachineInstr *MI,
273 unsigned MulOpc, unsigned AddSubOpc,
274 bool NegAcc, bool HasLane) {
275 unsigned DstReg = MI->getOperand(0).getReg();
276 bool DstDead = MI->getOperand(0).isDead();
277 unsigned AccReg = MI->getOperand(1).getReg();
278 unsigned Src1Reg = MI->getOperand(2).getReg();
279 unsigned Src2Reg = MI->getOperand(3).getReg();
280 bool Src1Kill = MI->getOperand(2).isKill();
281 bool Src2Kill = MI->getOperand(3).isKill();
282 unsigned LaneImm = HasLane ? MI->getOperand(4).getImm() : 0;
283 unsigned NextOp = HasLane ? 5 : 4;
284 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NextOp).getImm();
285 unsigned PredReg = MI->getOperand(++NextOp).getReg();
286
Evan Cheng6cc775f2011-06-28 19:10:37 +0000287 const MCInstrDesc &MCID1 = TII->get(MulOpc);
288 const MCInstrDesc &MCID2 = TII->get(AddSubOpc);
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000289 const MachineFunction &MF = *MI->getParent()->getParent();
290 unsigned TmpReg = MRI->createVirtualRegister(
291 TII->getRegClass(MCID1, 0, TRI, MF));
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000292
Evan Cheng7fae11b2011-12-14 02:11:42 +0000293 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(), MCID1, TmpReg)
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000294 .addReg(Src1Reg, getKillRegState(Src1Kill))
295 .addReg(Src2Reg, getKillRegState(Src2Kill));
296 if (HasLane)
297 MIB.addImm(LaneImm);
298 MIB.addImm(Pred).addReg(PredReg);
299
Evan Cheng7fae11b2011-12-14 02:11:42 +0000300 MIB = BuildMI(MBB, MI, MI->getDebugLoc(), MCID2)
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000301 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstDead));
302
303 if (NegAcc) {
304 bool AccKill = MRI->hasOneNonDBGUse(AccReg);
305 MIB.addReg(TmpReg, getKillRegState(true))
306 .addReg(AccReg, getKillRegState(AccKill));
307 } else {
308 MIB.addReg(AccReg).addReg(TmpReg, getKillRegState(true));
309 }
310 MIB.addImm(Pred).addReg(PredReg);
311
312 DEBUG({
313 dbgs() << "Expanding: " << *MI;
314 dbgs() << " to:\n";
315 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000316 MII = std::prev(MII);
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000317 MachineInstr &MI2 = *MII;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000318 MII = std::prev(MII);
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000319 MachineInstr &MI1 = *MII;
320 dbgs() << " " << MI1;
321 dbgs() << " " << MI2;
322 });
323
324 MI->eraseFromParent();
325 ++NumExpand;
326}
327
328bool MLxExpansion::ExpandFPMLxInstructions(MachineBasicBlock &MBB) {
329 bool Changed = false;
330
331 clearStack();
Bob Wilson0858c3a2011-04-19 18:11:57 +0000332 IgnoreStall.clear();
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000333
334 unsigned Skip = 0;
335 MachineBasicBlock::reverse_iterator MII = MBB.rbegin(), E = MBB.rend();
336 while (MII != E) {
337 MachineInstr *MI = &*MII;
338
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000339 if (MI->isPosition() || MI->isImplicitDef() || MI->isCopy()) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000340 ++MII;
341 continue;
342 }
343
Evan Cheng6cc775f2011-06-28 19:10:37 +0000344 const MCInstrDesc &MCID = MI->getDesc();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000345 if (MI->isBarrier()) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000346 clearStack();
347 Skip = 0;
348 ++MII;
349 continue;
350 }
351
Evan Cheng6cc775f2011-06-28 19:10:37 +0000352 unsigned Domain = MCID.TSFlags & ARMII::DomainMask;
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000353 if (Domain == ARMII::DomainGeneral) {
354 if (++Skip == 2)
355 // Assume dual issues of non-VFP / NEON instructions.
Craig Topper062a2ba2014-04-25 05:30:21 +0000356 pushStack(nullptr);
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000357 } else {
358 Skip = 0;
359
360 unsigned MulOpc, AddSubOpc;
361 bool NegAcc, HasLane;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000362 if (!TII->isFpMLxInstruction(MCID.getOpcode(),
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000363 MulOpc, AddSubOpc, NegAcc, HasLane) ||
364 !FindMLxHazard(MI))
365 pushStack(MI);
366 else {
367 ExpandFPMLxInstruction(MBB, MI, MulOpc, AddSubOpc, NegAcc, HasLane);
368 E = MBB.rend(); // May have changed if MI was the 1st instruction.
369 Changed = true;
370 continue;
371 }
372 }
373
374 ++MII;
375 }
376
377 return Changed;
378}
379
380bool MLxExpansion::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kaylora2b91112016-04-25 22:01:04 +0000381 if (skipFunction(*Fn.getFunction()))
382 return false;
383
Eric Christopherfc6de422014-08-05 02:39:49 +0000384 TII = static_cast<const ARMBaseInstrInfo *>(Fn.getSubtarget().getInstrInfo());
385 TRI = Fn.getSubtarget().getRegisterInfo();
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000386 MRI = &Fn.getRegInfo();
Eric Christopher22b2ad22015-02-20 08:24:37 +0000387 const ARMSubtarget *STI = &Fn.getSubtarget<ARMSubtarget>();
Diana Picus575f2bb2016-07-07 09:11:39 +0000388 if (!STI->expandMLx())
Eric Christopher63b44882015-03-05 00:23:40 +0000389 return false;
Bob Wilsone8a549c2012-09-29 21:43:49 +0000390 isLikeA9 = STI->isLikeA9() || STI->isSwift();
391 isSwift = STI->isSwift();
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000392
393 bool Modified = false;
Owen Anderson56112b92014-03-11 17:37:48 +0000394 for (MachineBasicBlock &MBB : Fn)
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000395 Modified |= ExpandFPMLxInstructions(MBB);
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000396
397 return Modified;
398}
399
400FunctionPass *llvm::createMLxExpansionPass() {
401 return new MLxExpansion();
402}