blob: 2de8d79512b872b4bde28d0c9c6c464f2e3aacec [file] [log] [blame]
Hal Finkel089b8ec2015-02-01 21:51:22 +00001//===--------------- PPCVSXFMAMutate.cpp - VSX FMA Mutation ---------------===//
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 mutates the form of VSX FMA instructions to avoid unnecessary
11// copies.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PPCInstrInfo.h"
16#include "MCTargetDesc/PPCPredicates.h"
17#include "PPC.h"
18#include "PPCInstrBuilder.h"
19#include "PPCMachineFunctionInfo.h"
20#include "PPCTargetMachine.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/CodeGen/LiveIntervalAnalysis.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineMemOperand.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/CodeGen/PseudoSourceValue.h"
30#include "llvm/CodeGen/ScheduleDAG.h"
31#include "llvm/CodeGen/SlotIndexes.h"
Hal Finkel089b8ec2015-02-01 21:51:22 +000032#include "llvm/MC/MCAsmInfo.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/raw_ostream.h"
38
39using namespace llvm;
40
41static cl::opt<bool> DisableVSXFMAMutate("disable-ppc-vsx-fma-mutation",
42cl::desc("Disable VSX FMA instruction mutation"), cl::Hidden);
43
44#define DEBUG_TYPE "ppc-vsx-fma-mutate"
45
46namespace llvm { namespace PPC {
47 int getAltVSXFMAOpcode(uint16_t Opcode);
48} }
49
50namespace {
51 // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers
52 // (Altivec and scalar floating-point registers), we need to transform the
53 // copies into subregister copies with other restrictions.
54 struct PPCVSXFMAMutate : public MachineFunctionPass {
55 static char ID;
56 PPCVSXFMAMutate() : MachineFunctionPass(ID) {
57 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
58 }
59
60 LiveIntervals *LIS;
61 const PPCInstrInfo *TII;
62
63protected:
64 bool processBlock(MachineBasicBlock &MBB) {
65 bool Changed = false;
66
67 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
68 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
69 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
70 I != IE; ++I) {
71 MachineInstr *MI = I;
72
73 // The default (A-type) VSX FMA form kills the addend (it is taken from
74 // the target register, which is then updated to reflect the result of
75 // the FMA). If the instruction, however, kills one of the registers
76 // used for the product, then we can use the M-form instruction (which
77 // will take that value from the to-be-defined register).
78
79 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
80 if (AltOpc == -1)
81 continue;
82
83 // This pass is run after register coalescing, and so we're looking for
84 // a situation like this:
85 // ...
86 // %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
87 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
88 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
89 // ...
90 // %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19,
91 // %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19
92 // ...
93 // Where we can eliminate the copy by changing from the A-type to the
94 // M-type instruction. Specifically, for this example, this means:
95 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
96 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
97 // is replaced by:
98 // %vreg16<def,tied1> = XSMADDMDP %vreg16<tied0>, %vreg18, %vreg9,
99 // %RM<imp-use>; VSLRC:%vreg16,%vreg18,%vreg9
100 // and we remove: %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
101
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000102 SlotIndex FMAIdx = LIS->getInstructionIndex(*MI);
Hal Finkel089b8ec2015-02-01 21:51:22 +0000103
104 VNInfo *AddendValNo =
105 LIS->getInterval(MI->getOperand(1).getReg()).Query(FMAIdx).valueIn();
Hal Finkel7ffe55a2015-08-26 23:41:53 +0000106
107 // This can be null if the register is undef.
108 if (!AddendValNo)
Hal Finkelff9639d2015-08-21 21:34:24 +0000109 continue;
Hal Finkelff9639d2015-08-21 21:34:24 +0000110
Hal Finkel089b8ec2015-02-01 21:51:22 +0000111 MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
112
113 // The addend and this instruction must be in the same block.
114
115 if (!AddendMI || AddendMI->getParent() != MI->getParent())
116 continue;
117
118 // The addend must be a full copy within the same register class.
119
120 if (!AddendMI->isFullCopy())
121 continue;
122
123 unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
124 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) {
125 if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
126 MRI.getRegClass(AddendSrcReg))
127 continue;
128 } else {
129 // If AddendSrcReg is a physical register, make sure the destination
130 // register class contains it.
131 if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
132 ->contains(AddendSrcReg))
133 continue;
134 }
135
136 // In theory, there could be other uses of the addend copy before this
137 // fma. We could deal with this, but that would require additional
138 // logic below and I suspect it will not occur in any relevant
139 // situations. Additionally, check whether the copy source is killed
140 // prior to the fma. In order to replace the addend here with the
141 // source of the copy, it must still be live here. We can't use
142 // interval testing for a physical register, so as long as we're
143 // walking the MIs we may as well test liveness here.
Hal Finkel8acae522015-07-14 20:02:02 +0000144 //
145 // FIXME: There is a case that occurs in practice, like this:
146 // %vreg9<def> = COPY %F1; VSSRC:%vreg9
147 // ...
148 // %vreg6<def> = COPY %vreg9; VSSRC:%vreg6,%vreg9
149 // %vreg7<def> = COPY %vreg9; VSSRC:%vreg7,%vreg9
150 // %vreg9<def,tied1> = XSMADDASP %vreg9<tied0>, %vreg1, %vreg4; VSSRC:
151 // %vreg6<def,tied1> = XSMADDASP %vreg6<tied0>, %vreg1, %vreg2; VSSRC:
152 // %vreg7<def,tied1> = XSMADDASP %vreg7<tied0>, %vreg1, %vreg3; VSSRC:
153 // which prevents an otherwise-profitable transformation.
Hal Finkel089b8ec2015-02-01 21:51:22 +0000154 bool OtherUsers = false, KillsAddendSrc = false;
155 for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
156 J != JE; --J) {
157 if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
158 OtherUsers = true;
159 break;
160 }
161 if (J->modifiesRegister(AddendSrcReg, TRI) ||
162 J->killsRegister(AddendSrcReg, TRI)) {
163 KillsAddendSrc = true;
164 break;
165 }
166 }
167
168 if (OtherUsers || KillsAddendSrc)
169 continue;
170
Hal Finkel089b8ec2015-02-01 21:51:22 +0000171
Kyle Buttd62d8b72016-02-03 01:41:09 +0000172 // The transformation doesn't work well with things like:
173 // %vreg5 = A-form-op %vreg5, %vreg11, %vreg5;
174 // unless vreg11 is also a kill, so skip when it is not,
175 // and check operand 3 to see it is also a kill to handle the case:
176 // %vreg5 = A-form-op %vreg5, %vreg5, %vreg11;
177 // where vreg5 and vreg11 are both kills. This case would be skipped
178 // otherwise.
179 unsigned OldFMAReg = MI->getOperand(0).getReg();
180
181 // Find one of the product operands that is killed by this instruction.
Hal Finkel089b8ec2015-02-01 21:51:22 +0000182 unsigned KilledProdOp = 0, OtherProdOp = 0;
Kyle Buttd62d8b72016-02-03 01:41:09 +0000183 unsigned Reg2 = MI->getOperand(2).getReg();
184 unsigned Reg3 = MI->getOperand(3).getReg();
185 if (LIS->getInterval(Reg2).Query(FMAIdx).isKill()
186 && Reg2 != OldFMAReg) {
Hal Finkel089b8ec2015-02-01 21:51:22 +0000187 KilledProdOp = 2;
188 OtherProdOp = 3;
Kyle Buttd62d8b72016-02-03 01:41:09 +0000189 } else if (LIS->getInterval(Reg3).Query(FMAIdx).isKill()
190 && Reg3 != OldFMAReg) {
Hal Finkel089b8ec2015-02-01 21:51:22 +0000191 KilledProdOp = 3;
192 OtherProdOp = 2;
193 }
194
Kyle Buttd62d8b72016-02-03 01:41:09 +0000195 // If there are no usable killed product operands, then this
196 // transformation is likely not profitable.
Hal Finkel089b8ec2015-02-01 21:51:22 +0000197 if (!KilledProdOp)
198 continue;
199
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000200 // If the addend copy is used only by this MI, then the addend source
201 // register is likely not live here. This could be fixed (based on the
202 // legality checks above, the live range for the addend source register
203 // could be extended), but it seems likely that such a trivial copy can
204 // be coalesced away later, and thus is not worth the effort.
205 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg) &&
Hal Finkel0f2ddcb2015-08-24 23:48:28 +0000206 !LIS->getInterval(AddendSrcReg).liveAt(FMAIdx))
207 continue;
Hal Finkel089b8ec2015-02-01 21:51:22 +0000208
209 // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
210
Hal Finkel089b8ec2015-02-01 21:51:22 +0000211 unsigned KilledProdReg = MI->getOperand(KilledProdOp).getReg();
212 unsigned OtherProdReg = MI->getOperand(OtherProdOp).getReg();
213
214 unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
215 unsigned KilledProdSubReg = MI->getOperand(KilledProdOp).getSubReg();
216 unsigned OtherProdSubReg = MI->getOperand(OtherProdOp).getSubReg();
217
218 bool AddRegKill = AddendMI->getOperand(1).isKill();
219 bool KilledProdRegKill = MI->getOperand(KilledProdOp).isKill();
220 bool OtherProdRegKill = MI->getOperand(OtherProdOp).isKill();
221
222 bool AddRegUndef = AddendMI->getOperand(1).isUndef();
223 bool KilledProdRegUndef = MI->getOperand(KilledProdOp).isUndef();
224 bool OtherProdRegUndef = MI->getOperand(OtherProdOp).isUndef();
225
Kyle Butt28b01a52015-12-10 21:28:40 +0000226 // If there isn't a class that fits, we can't perform the transform.
227 // This is needed for correctness with a mixture of VSX and Altivec
228 // instructions to make sure that a low VSX register is not assigned to
229 // the Altivec instruction.
230 if (!MRI.constrainRegClass(KilledProdReg,
231 MRI.getRegClass(OldFMAReg)))
232 continue;
233
Hal Finkel089b8ec2015-02-01 21:51:22 +0000234 assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
235 "Addend copy not tied to old FMA output!");
236
237 DEBUG(dbgs() << "VSX FMA Mutation:\n " << *MI;);
238
239 MI->getOperand(0).setReg(KilledProdReg);
240 MI->getOperand(1).setReg(KilledProdReg);
Hal Finkel673b4932015-07-15 08:23:03 +0000241 MI->getOperand(3).setReg(AddendSrcReg);
Hal Finkel089b8ec2015-02-01 21:51:22 +0000242
243 MI->getOperand(0).setSubReg(KilledProdSubReg);
244 MI->getOperand(1).setSubReg(KilledProdSubReg);
245 MI->getOperand(3).setSubReg(AddSubReg);
Hal Finkel089b8ec2015-02-01 21:51:22 +0000246
247 MI->getOperand(1).setIsKill(KilledProdRegKill);
248 MI->getOperand(3).setIsKill(AddRegKill);
Hal Finkel089b8ec2015-02-01 21:51:22 +0000249
250 MI->getOperand(1).setIsUndef(KilledProdRegUndef);
251 MI->getOperand(3).setIsUndef(AddRegUndef);
Hal Finkel089b8ec2015-02-01 21:51:22 +0000252
253 MI->setDesc(TII->get(AltOpc));
254
Kyle Buttd62d8b72016-02-03 01:41:09 +0000255 // If the addend is also a multiplicand, replace it with the addend
256 // source in both places.
257 if (OtherProdReg == AddendMI->getOperand(0).getReg()) {
258 MI->getOperand(2).setReg(AddendSrcReg);
259 MI->getOperand(2).setSubReg(AddSubReg);
260 MI->getOperand(2).setIsKill(AddRegKill);
261 MI->getOperand(2).setIsUndef(AddRegUndef);
262 } else {
263 MI->getOperand(2).setReg(OtherProdReg);
264 MI->getOperand(2).setSubReg(OtherProdSubReg);
265 MI->getOperand(2).setIsKill(OtherProdRegKill);
266 MI->getOperand(2).setIsUndef(OtherProdRegUndef);
267 }
268
Hal Finkel089b8ec2015-02-01 21:51:22 +0000269 DEBUG(dbgs() << " -> " << *MI);
270
271 // The killed product operand was killed here, so we can reuse it now
272 // for the result of the fma.
273
274 LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
275 VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
276 for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
277 UI != UE;) {
278 MachineOperand &UseMO = *UI;
279 MachineInstr *UseMI = UseMO.getParent();
280 ++UI;
281
282 // Don't replace the result register of the copy we're about to erase.
283 if (UseMI == AddendMI)
284 continue;
285
Kyle Butt28b01a52015-12-10 21:28:40 +0000286 UseMO.substVirtReg(KilledProdReg, KilledProdSubReg, *TRI);
Hal Finkel089b8ec2015-02-01 21:51:22 +0000287 }
288
289 // Extend the live intervals of the killed product operand to hold the
290 // fma result.
291
292 LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
293 for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
294 AI != AE; ++AI) {
295 // Don't add the segment that corresponds to the original copy.
296 if (AI->valno == AddendValNo)
297 continue;
298
299 VNInfo *NewFMAValNo =
300 NewFMAInt.getNextValue(AI->start,
301 LIS->getVNInfoAllocator());
302
303 NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
304 NewFMAValNo));
305 }
306 DEBUG(dbgs() << " extended: " << NewFMAInt << '\n');
307
Hal Finkel673b4932015-07-15 08:23:03 +0000308 // Extend the live interval of the addend source (it might end at the
309 // copy to be removed, or somewhere in between there and here). This
310 // is necessary only if it is a physical register.
311 if (!TargetRegisterInfo::isVirtualRegister(AddendSrcReg))
312 for (MCRegUnitIterator Units(AddendSrcReg, TRI); Units.isValid();
313 ++Units) {
314 unsigned Unit = *Units;
315
316 LiveRange &AddendSrcRange = LIS->getRegUnit(Unit);
317 AddendSrcRange.extendInBlock(LIS->getMBBStartIdx(&MBB),
318 FMAIdx.getRegSlot());
319 DEBUG(dbgs() << " extended: " << AddendSrcRange << '\n');
320 }
321
Hal Finkel089b8ec2015-02-01 21:51:22 +0000322 FMAInt.removeValNo(FMAValNo);
323 DEBUG(dbgs() << " trimmed: " << FMAInt << '\n');
324
325 // Remove the (now unused) copy.
326
327 DEBUG(dbgs() << " removing: " << *AddendMI << '\n');
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000328 LIS->RemoveMachineInstrFromMaps(*AddendMI);
Hal Finkel089b8ec2015-02-01 21:51:22 +0000329 AddendMI->eraseFromParent();
330
331 Changed = true;
332 }
333
334 return Changed;
335 }
336
337public:
338 bool runOnMachineFunction(MachineFunction &MF) override {
339 // If we don't have VSX then go ahead and return without doing
340 // anything.
341 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
342 if (!STI.hasVSX())
343 return false;
344
345 LIS = &getAnalysis<LiveIntervals>();
346
347 TII = STI.getInstrInfo();
348
349 bool Changed = false;
350
351 if (DisableVSXFMAMutate)
352 return Changed;
353
354 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
355 MachineBasicBlock &B = *I++;
356 if (processBlock(B))
357 Changed = true;
358 }
359
360 return Changed;
361 }
362
363 void getAnalysisUsage(AnalysisUsage &AU) const override {
364 AU.addRequired<LiveIntervals>();
365 AU.addPreserved<LiveIntervals>();
366 AU.addRequired<SlotIndexes>();
367 AU.addPreserved<SlotIndexes>();
368 MachineFunctionPass::getAnalysisUsage(AU);
369 }
370 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000371}
Hal Finkel089b8ec2015-02-01 21:51:22 +0000372
373INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
374 "PowerPC VSX FMA Mutation", false, false)
375INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
376INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
377INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
378 "PowerPC VSX FMA Mutation", false, false)
379
380char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
381
382char PPCVSXFMAMutate::ID = 0;
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000383FunctionPass *llvm::createPPCVSXFMAMutatePass() {
384 return new PPCVSXFMAMutate();
385}