1089b8ecfSHal Finkel //===--------------- PPCVSXFMAMutate.cpp - VSX FMA Mutation ---------------===//
2089b8ecfSHal Finkel //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6089b8ecfSHal Finkel //
7089b8ecfSHal Finkel //===----------------------------------------------------------------------===//
8089b8ecfSHal Finkel //
9089b8ecfSHal Finkel // This pass mutates the form of VSX FMA instructions to avoid unnecessary
10089b8ecfSHal Finkel // copies.
11089b8ecfSHal Finkel //
12089b8ecfSHal Finkel //===----------------------------------------------------------------------===//
13089b8ecfSHal Finkel
14089b8ecfSHal Finkel #include "MCTargetDesc/PPCPredicates.h"
15089b8ecfSHal Finkel #include "PPC.h"
16089b8ecfSHal Finkel #include "PPCInstrBuilder.h"
176bda14b3SChandler Carruth #include "PPCInstrInfo.h"
18089b8ecfSHal Finkel #include "PPCMachineFunctionInfo.h"
19089b8ecfSHal Finkel #include "PPCTargetMachine.h"
20089b8ecfSHal Finkel #include "llvm/ADT/STLExtras.h"
21089b8ecfSHal Finkel #include "llvm/ADT/Statistic.h"
22f842297dSMatthias Braun #include "llvm/CodeGen/LiveIntervals.h"
2331095d2fSKrzysztof Parzyszek #include "llvm/CodeGen/MachineDominators.h"
24089b8ecfSHal Finkel #include "llvm/CodeGen/MachineFrameInfo.h"
25089b8ecfSHal Finkel #include "llvm/CodeGen/MachineFunctionPass.h"
26089b8ecfSHal Finkel #include "llvm/CodeGen/MachineInstrBuilder.h"
27089b8ecfSHal Finkel #include "llvm/CodeGen/MachineMemOperand.h"
28089b8ecfSHal Finkel #include "llvm/CodeGen/MachineRegisterInfo.h"
29089b8ecfSHal Finkel #include "llvm/CodeGen/PseudoSourceValue.h"
30089b8ecfSHal Finkel #include "llvm/CodeGen/ScheduleDAG.h"
31089b8ecfSHal Finkel #include "llvm/CodeGen/SlotIndexes.h"
3205da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
33089b8ecfSHal Finkel #include "llvm/MC/MCAsmInfo.h"
3489b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h"
35089b8ecfSHal Finkel #include "llvm/Support/CommandLine.h"
36089b8ecfSHal Finkel #include "llvm/Support/Debug.h"
37089b8ecfSHal Finkel #include "llvm/Support/ErrorHandling.h"
38089b8ecfSHal Finkel #include "llvm/Support/raw_ostream.h"
39089b8ecfSHal Finkel
40089b8ecfSHal Finkel using namespace llvm;
41089b8ecfSHal Finkel
426e676a84STim Shen // Temporarily disable FMA mutation by default, since it doesn't handle
436e676a84STim Shen // cross-basic-block intervals well.
446e676a84STim Shen // See: http://lists.llvm.org/pipermail/llvm-dev/2016-February/095669.html
456e676a84STim Shen // http://reviews.llvm.org/D17087
466e676a84STim Shen static cl::opt<bool> DisableVSXFMAMutate(
476e676a84STim Shen "disable-ppc-vsx-fma-mutation",
486e676a84STim Shen cl::desc("Disable VSX FMA instruction mutation"), cl::init(true),
496e676a84STim Shen cl::Hidden);
50089b8ecfSHal Finkel
51089b8ecfSHal Finkel #define DEBUG_TYPE "ppc-vsx-fma-mutate"
52089b8ecfSHal Finkel
53089b8ecfSHal Finkel namespace llvm { namespace PPC {
54089b8ecfSHal Finkel int getAltVSXFMAOpcode(uint16_t Opcode);
55089b8ecfSHal Finkel } }
56089b8ecfSHal Finkel
57089b8ecfSHal Finkel namespace {
58089b8ecfSHal Finkel // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers
59089b8ecfSHal Finkel // (Altivec and scalar floating-point registers), we need to transform the
60089b8ecfSHal Finkel // copies into subregister copies with other restrictions.
61089b8ecfSHal Finkel struct PPCVSXFMAMutate : public MachineFunctionPass {
62089b8ecfSHal Finkel static char ID;
PPCVSXFMAMutate__anon2db1c68d0111::PPCVSXFMAMutate63089b8ecfSHal Finkel PPCVSXFMAMutate() : MachineFunctionPass(ID) {
64089b8ecfSHal Finkel initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
65089b8ecfSHal Finkel }
66089b8ecfSHal Finkel
67089b8ecfSHal Finkel LiveIntervals *LIS;
68089b8ecfSHal Finkel const PPCInstrInfo *TII;
69089b8ecfSHal Finkel
70089b8ecfSHal Finkel protected:
processBlock__anon2db1c68d0111::PPCVSXFMAMutate71089b8ecfSHal Finkel bool processBlock(MachineBasicBlock &MBB) {
72089b8ecfSHal Finkel bool Changed = false;
73089b8ecfSHal Finkel
74089b8ecfSHal Finkel MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
75089b8ecfSHal Finkel const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
76089b8ecfSHal Finkel for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
77089b8ecfSHal Finkel I != IE; ++I) {
78e5a22f44SDuncan P. N. Exon Smith MachineInstr &MI = *I;
79089b8ecfSHal Finkel
80089b8ecfSHal Finkel // The default (A-type) VSX FMA form kills the addend (it is taken from
81089b8ecfSHal Finkel // the target register, which is then updated to reflect the result of
82089b8ecfSHal Finkel // the FMA). If the instruction, however, kills one of the registers
83089b8ecfSHal Finkel // used for the product, then we can use the M-form instruction (which
84089b8ecfSHal Finkel // will take that value from the to-be-defined register).
85089b8ecfSHal Finkel
86e5a22f44SDuncan P. N. Exon Smith int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode());
87089b8ecfSHal Finkel if (AltOpc == -1)
88089b8ecfSHal Finkel continue;
89089b8ecfSHal Finkel
90089b8ecfSHal Finkel // This pass is run after register coalescing, and so we're looking for
91089b8ecfSHal Finkel // a situation like this:
92089b8ecfSHal Finkel // ...
93a8a83d15SFrancis Visoiu Mistrih // %5 = COPY %9; VSLRC:%5,%9
9493ef1458SFrancis Visoiu Mistrih // %5<def,tied1> = XSMADDADP %5<tied0>, %17, %16,
95a8a83d15SFrancis Visoiu Mistrih // implicit %rm; VSLRC:%5,%17,%16
96089b8ecfSHal Finkel // ...
9793ef1458SFrancis Visoiu Mistrih // %9<def,tied1> = XSMADDADP %9<tied0>, %17, %19,
98a8a83d15SFrancis Visoiu Mistrih // implicit %rm; VSLRC:%9,%17,%19
99089b8ecfSHal Finkel // ...
100089b8ecfSHal Finkel // Where we can eliminate the copy by changing from the A-type to the
101089b8ecfSHal Finkel // M-type instruction. Specifically, for this example, this means:
10293ef1458SFrancis Visoiu Mistrih // %5<def,tied1> = XSMADDADP %5<tied0>, %17, %16,
103a8a83d15SFrancis Visoiu Mistrih // implicit %rm; VSLRC:%5,%17,%16
104089b8ecfSHal Finkel // is replaced by:
10593ef1458SFrancis Visoiu Mistrih // %16<def,tied1> = XSMADDMDP %16<tied0>, %18, %9,
106a8a83d15SFrancis Visoiu Mistrih // implicit %rm; VSLRC:%16,%18,%9
107a8a83d15SFrancis Visoiu Mistrih // and we remove: %5 = COPY %9; VSLRC:%5,%9
108089b8ecfSHal Finkel
109e5a22f44SDuncan P. N. Exon Smith SlotIndex FMAIdx = LIS->getInstructionIndex(MI);
110089b8ecfSHal Finkel
111089b8ecfSHal Finkel VNInfo *AddendValNo =
112e5a22f44SDuncan P. N. Exon Smith LIS->getInterval(MI.getOperand(1).getReg()).Query(FMAIdx).valueIn();
1137ffe55aeSHal Finkel
114ff9639d6SHal Finkel // This can be null if the register is undef.
1157ffe55aeSHal Finkel if (!AddendValNo)
116ff9639d6SHal Finkel continue;
117ff9639d6SHal Finkel
118089b8ecfSHal Finkel MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
119089b8ecfSHal Finkel
120089b8ecfSHal Finkel // The addend and this instruction must be in the same block.
121089b8ecfSHal Finkel
122e5a22f44SDuncan P. N. Exon Smith if (!AddendMI || AddendMI->getParent() != MI.getParent())
123089b8ecfSHal Finkel continue;
124089b8ecfSHal Finkel
125089b8ecfSHal Finkel // The addend must be a full copy within the same register class.
126089b8ecfSHal Finkel
127089b8ecfSHal Finkel if (!AddendMI->isFullCopy())
128089b8ecfSHal Finkel continue;
129089b8ecfSHal Finkel
1300c476111SDaniel Sanders Register AddendSrcReg = AddendMI->getOperand(1).getReg();
1312bea69bfSDaniel Sanders if (Register::isVirtualRegister(AddendSrcReg)) {
132089b8ecfSHal Finkel if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
133089b8ecfSHal Finkel MRI.getRegClass(AddendSrcReg))
134089b8ecfSHal Finkel continue;
135089b8ecfSHal Finkel } else {
136089b8ecfSHal Finkel // If AddendSrcReg is a physical register, make sure the destination
137089b8ecfSHal Finkel // register class contains it.
138089b8ecfSHal Finkel if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
139089b8ecfSHal Finkel ->contains(AddendSrcReg))
140089b8ecfSHal Finkel continue;
141089b8ecfSHal Finkel }
142089b8ecfSHal Finkel
143089b8ecfSHal Finkel // In theory, there could be other uses of the addend copy before this
144089b8ecfSHal Finkel // fma. We could deal with this, but that would require additional
145089b8ecfSHal Finkel // logic below and I suspect it will not occur in any relevant
146089b8ecfSHal Finkel // situations. Additionally, check whether the copy source is killed
147089b8ecfSHal Finkel // prior to the fma. In order to replace the addend here with the
148089b8ecfSHal Finkel // source of the copy, it must still be live here. We can't use
149089b8ecfSHal Finkel // interval testing for a physical register, so as long as we're
150089b8ecfSHal Finkel // walking the MIs we may as well test liveness here.
1518acae527SHal Finkel //
1528acae527SHal Finkel // FIXME: There is a case that occurs in practice, like this:
153a8a83d15SFrancis Visoiu Mistrih // %9 = COPY %f1; VSSRC:%9
1548acae527SHal Finkel // ...
155a8a83d15SFrancis Visoiu Mistrih // %6 = COPY %9; VSSRC:%6,%9
156a8a83d15SFrancis Visoiu Mistrih // %7 = COPY %9; VSSRC:%7,%9
15793ef1458SFrancis Visoiu Mistrih // %9<def,tied1> = XSMADDASP %9<tied0>, %1, %4; VSSRC:
15893ef1458SFrancis Visoiu Mistrih // %6<def,tied1> = XSMADDASP %6<tied0>, %1, %2; VSSRC:
15993ef1458SFrancis Visoiu Mistrih // %7<def,tied1> = XSMADDASP %7<tied0>, %1, %3; VSSRC:
1608acae527SHal Finkel // which prevents an otherwise-profitable transformation.
161089b8ecfSHal Finkel bool OtherUsers = false, KillsAddendSrc = false;
162089b8ecfSHal Finkel for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
163089b8ecfSHal Finkel J != JE; --J) {
164089b8ecfSHal Finkel if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
165089b8ecfSHal Finkel OtherUsers = true;
166089b8ecfSHal Finkel break;
167089b8ecfSHal Finkel }
168089b8ecfSHal Finkel if (J->modifiesRegister(AddendSrcReg, TRI) ||
169089b8ecfSHal Finkel J->killsRegister(AddendSrcReg, TRI)) {
170089b8ecfSHal Finkel KillsAddendSrc = true;
171089b8ecfSHal Finkel break;
172089b8ecfSHal Finkel }
173089b8ecfSHal Finkel }
174089b8ecfSHal Finkel
175089b8ecfSHal Finkel if (OtherUsers || KillsAddendSrc)
176089b8ecfSHal Finkel continue;
177089b8ecfSHal Finkel
178089b8ecfSHal Finkel
179d62d8b77SKyle Butt // The transformation doesn't work well with things like:
18093ef1458SFrancis Visoiu Mistrih // %5 = A-form-op %5, %11, %5;
18193ef1458SFrancis Visoiu Mistrih // unless %11 is also a kill, so skip when it is not,
182d62d8b77SKyle Butt // and check operand 3 to see it is also a kill to handle the case:
18393ef1458SFrancis Visoiu Mistrih // %5 = A-form-op %5, %5, %11;
18493ef1458SFrancis Visoiu Mistrih // where %5 and %11 are both kills. This case would be skipped
185d62d8b77SKyle Butt // otherwise.
1860c476111SDaniel Sanders Register OldFMAReg = MI.getOperand(0).getReg();
187d62d8b77SKyle Butt
188d62d8b77SKyle Butt // Find one of the product operands that is killed by this instruction.
189089b8ecfSHal Finkel unsigned KilledProdOp = 0, OtherProdOp = 0;
1900c476111SDaniel Sanders Register Reg2 = MI.getOperand(2).getReg();
1910c476111SDaniel Sanders Register Reg3 = MI.getOperand(3).getReg();
192d62d8b77SKyle Butt if (LIS->getInterval(Reg2).Query(FMAIdx).isKill()
193d62d8b77SKyle Butt && Reg2 != OldFMAReg) {
194089b8ecfSHal Finkel KilledProdOp = 2;
195089b8ecfSHal Finkel OtherProdOp = 3;
196d62d8b77SKyle Butt } else if (LIS->getInterval(Reg3).Query(FMAIdx).isKill()
197d62d8b77SKyle Butt && Reg3 != OldFMAReg) {
198089b8ecfSHal Finkel KilledProdOp = 3;
199089b8ecfSHal Finkel OtherProdOp = 2;
200089b8ecfSHal Finkel }
201089b8ecfSHal Finkel
202d62d8b77SKyle Butt // If there are no usable killed product operands, then this
203d62d8b77SKyle Butt // transformation is likely not profitable.
204089b8ecfSHal Finkel if (!KilledProdOp)
205089b8ecfSHal Finkel continue;
206089b8ecfSHal Finkel
2070f2ddcb8SHal Finkel // If the addend copy is used only by this MI, then the addend source
2080f2ddcb8SHal Finkel // register is likely not live here. This could be fixed (based on the
2090f2ddcb8SHal Finkel // legality checks above, the live range for the addend source register
2100f2ddcb8SHal Finkel // could be extended), but it seems likely that such a trivial copy can
2110f2ddcb8SHal Finkel // be coalesced away later, and thus is not worth the effort.
2122bea69bfSDaniel Sanders if (Register::isVirtualRegister(AddendSrcReg) &&
2130f2ddcb8SHal Finkel !LIS->getInterval(AddendSrcReg).liveAt(FMAIdx))
2140f2ddcb8SHal Finkel continue;
215089b8ecfSHal Finkel
216089b8ecfSHal Finkel // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
217089b8ecfSHal Finkel
2180c476111SDaniel Sanders Register KilledProdReg = MI.getOperand(KilledProdOp).getReg();
2190c476111SDaniel Sanders Register OtherProdReg = MI.getOperand(OtherProdOp).getReg();
220089b8ecfSHal Finkel
221089b8ecfSHal Finkel unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
222e5a22f44SDuncan P. N. Exon Smith unsigned KilledProdSubReg = MI.getOperand(KilledProdOp).getSubReg();
223e5a22f44SDuncan P. N. Exon Smith unsigned OtherProdSubReg = MI.getOperand(OtherProdOp).getSubReg();
224089b8ecfSHal Finkel
225089b8ecfSHal Finkel bool AddRegKill = AddendMI->getOperand(1).isKill();
226e5a22f44SDuncan P. N. Exon Smith bool KilledProdRegKill = MI.getOperand(KilledProdOp).isKill();
227e5a22f44SDuncan P. N. Exon Smith bool OtherProdRegKill = MI.getOperand(OtherProdOp).isKill();
228089b8ecfSHal Finkel
229089b8ecfSHal Finkel bool AddRegUndef = AddendMI->getOperand(1).isUndef();
230e5a22f44SDuncan P. N. Exon Smith bool KilledProdRegUndef = MI.getOperand(KilledProdOp).isUndef();
231e5a22f44SDuncan P. N. Exon Smith bool OtherProdRegUndef = MI.getOperand(OtherProdOp).isUndef();
232089b8ecfSHal Finkel
23328b01a51SKyle Butt // If there isn't a class that fits, we can't perform the transform.
23428b01a51SKyle Butt // This is needed for correctness with a mixture of VSX and Altivec
23528b01a51SKyle Butt // instructions to make sure that a low VSX register is not assigned to
23628b01a51SKyle Butt // the Altivec instruction.
23728b01a51SKyle Butt if (!MRI.constrainRegClass(KilledProdReg,
23828b01a51SKyle Butt MRI.getRegClass(OldFMAReg)))
23928b01a51SKyle Butt continue;
24028b01a51SKyle Butt
241089b8ecfSHal Finkel assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
242089b8ecfSHal Finkel "Addend copy not tied to old FMA output!");
243089b8ecfSHal Finkel
244d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "VSX FMA Mutation:\n " << MI);
245089b8ecfSHal Finkel
246e5a22f44SDuncan P. N. Exon Smith MI.getOperand(0).setReg(KilledProdReg);
247e5a22f44SDuncan P. N. Exon Smith MI.getOperand(1).setReg(KilledProdReg);
248e5a22f44SDuncan P. N. Exon Smith MI.getOperand(3).setReg(AddendSrcReg);
249089b8ecfSHal Finkel
250e5a22f44SDuncan P. N. Exon Smith MI.getOperand(0).setSubReg(KilledProdSubReg);
251e5a22f44SDuncan P. N. Exon Smith MI.getOperand(1).setSubReg(KilledProdSubReg);
252e5a22f44SDuncan P. N. Exon Smith MI.getOperand(3).setSubReg(AddSubReg);
253089b8ecfSHal Finkel
254e5a22f44SDuncan P. N. Exon Smith MI.getOperand(1).setIsKill(KilledProdRegKill);
255e5a22f44SDuncan P. N. Exon Smith MI.getOperand(3).setIsKill(AddRegKill);
256089b8ecfSHal Finkel
257e5a22f44SDuncan P. N. Exon Smith MI.getOperand(1).setIsUndef(KilledProdRegUndef);
258e5a22f44SDuncan P. N. Exon Smith MI.getOperand(3).setIsUndef(AddRegUndef);
259089b8ecfSHal Finkel
260e5a22f44SDuncan P. N. Exon Smith MI.setDesc(TII->get(AltOpc));
261089b8ecfSHal Finkel
262d62d8b77SKyle Butt // If the addend is also a multiplicand, replace it with the addend
263d62d8b77SKyle Butt // source in both places.
264d62d8b77SKyle Butt if (OtherProdReg == AddendMI->getOperand(0).getReg()) {
265e5a22f44SDuncan P. N. Exon Smith MI.getOperand(2).setReg(AddendSrcReg);
266e5a22f44SDuncan P. N. Exon Smith MI.getOperand(2).setSubReg(AddSubReg);
267e5a22f44SDuncan P. N. Exon Smith MI.getOperand(2).setIsKill(AddRegKill);
268e5a22f44SDuncan P. N. Exon Smith MI.getOperand(2).setIsUndef(AddRegUndef);
269d62d8b77SKyle Butt } else {
270e5a22f44SDuncan P. N. Exon Smith MI.getOperand(2).setReg(OtherProdReg);
271e5a22f44SDuncan P. N. Exon Smith MI.getOperand(2).setSubReg(OtherProdSubReg);
272e5a22f44SDuncan P. N. Exon Smith MI.getOperand(2).setIsKill(OtherProdRegKill);
273e5a22f44SDuncan P. N. Exon Smith MI.getOperand(2).setIsUndef(OtherProdRegUndef);
274d62d8b77SKyle Butt }
275d62d8b77SKyle Butt
276d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " -> " << MI);
277089b8ecfSHal Finkel
278089b8ecfSHal Finkel // The killed product operand was killed here, so we can reuse it now
279089b8ecfSHal Finkel // for the result of the fma.
280089b8ecfSHal Finkel
281089b8ecfSHal Finkel LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
282089b8ecfSHal Finkel VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
283089b8ecfSHal Finkel for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
284089b8ecfSHal Finkel UI != UE;) {
285089b8ecfSHal Finkel MachineOperand &UseMO = *UI;
286089b8ecfSHal Finkel MachineInstr *UseMI = UseMO.getParent();
287089b8ecfSHal Finkel ++UI;
288089b8ecfSHal Finkel
289089b8ecfSHal Finkel // Don't replace the result register of the copy we're about to erase.
290089b8ecfSHal Finkel if (UseMI == AddendMI)
291089b8ecfSHal Finkel continue;
292089b8ecfSHal Finkel
29328b01a51SKyle Butt UseMO.substVirtReg(KilledProdReg, KilledProdSubReg, *TRI);
294089b8ecfSHal Finkel }
295089b8ecfSHal Finkel
296089b8ecfSHal Finkel // Extend the live intervals of the killed product operand to hold the
297089b8ecfSHal Finkel // fma result.
298089b8ecfSHal Finkel
299089b8ecfSHal Finkel LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
300*bf039a86SKazu Hirata for (auto &AI : FMAInt) {
301089b8ecfSHal Finkel // Don't add the segment that corresponds to the original copy.
302*bf039a86SKazu Hirata if (AI.valno == AddendValNo)
303089b8ecfSHal Finkel continue;
304089b8ecfSHal Finkel
305089b8ecfSHal Finkel VNInfo *NewFMAValNo =
306*bf039a86SKazu Hirata NewFMAInt.getNextValue(AI.start, LIS->getVNInfoAllocator());
307089b8ecfSHal Finkel
308*bf039a86SKazu Hirata NewFMAInt.addSegment(
309*bf039a86SKazu Hirata LiveInterval::Segment(AI.start, AI.end, NewFMAValNo));
310089b8ecfSHal Finkel }
311d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " extended: " << NewFMAInt << '\n');
312089b8ecfSHal Finkel
313673b493eSHal Finkel // Extend the live interval of the addend source (it might end at the
314673b493eSHal Finkel // copy to be removed, or somewhere in between there and here). This
315673b493eSHal Finkel // is necessary only if it is a physical register.
3162ac3a7d0SMircea Trofin if (!AddendSrcReg.isVirtual())
3172ac3a7d0SMircea Trofin for (MCRegUnitIterator Units(AddendSrcReg.asMCReg(), TRI);
3182ac3a7d0SMircea Trofin Units.isValid(); ++Units) {
319673b493eSHal Finkel unsigned Unit = *Units;
320673b493eSHal Finkel
321673b493eSHal Finkel LiveRange &AddendSrcRange = LIS->getRegUnit(Unit);
322673b493eSHal Finkel AddendSrcRange.extendInBlock(LIS->getMBBStartIdx(&MBB),
323673b493eSHal Finkel FMAIdx.getRegSlot());
324d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " extended: " << AddendSrcRange << '\n');
325673b493eSHal Finkel }
326673b493eSHal Finkel
327089b8ecfSHal Finkel FMAInt.removeValNo(FMAValNo);
328d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " trimmed: " << FMAInt << '\n');
329089b8ecfSHal Finkel
330089b8ecfSHal Finkel // Remove the (now unused) copy.
331089b8ecfSHal Finkel
332d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " removing: " << *AddendMI << '\n');
3333ac9cc61SDuncan P. N. Exon Smith LIS->RemoveMachineInstrFromMaps(*AddendMI);
334089b8ecfSHal Finkel AddendMI->eraseFromParent();
335089b8ecfSHal Finkel
336089b8ecfSHal Finkel Changed = true;
337089b8ecfSHal Finkel }
338089b8ecfSHal Finkel
339089b8ecfSHal Finkel return Changed;
340089b8ecfSHal Finkel }
341089b8ecfSHal Finkel
342089b8ecfSHal Finkel public:
runOnMachineFunction__anon2db1c68d0111::PPCVSXFMAMutate343089b8ecfSHal Finkel bool runOnMachineFunction(MachineFunction &MF) override {
344f1caa283SMatthias Braun if (skipFunction(MF.getFunction()))
345289bd5f6SAndrew Kaylor return false;
346289bd5f6SAndrew Kaylor
347089b8ecfSHal Finkel // If we don't have VSX then go ahead and return without doing
348089b8ecfSHal Finkel // anything.
349089b8ecfSHal Finkel const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
350089b8ecfSHal Finkel if (!STI.hasVSX())
351089b8ecfSHal Finkel return false;
352089b8ecfSHal Finkel
353089b8ecfSHal Finkel LIS = &getAnalysis<LiveIntervals>();
354089b8ecfSHal Finkel
355089b8ecfSHal Finkel TII = STI.getInstrInfo();
356089b8ecfSHal Finkel
357089b8ecfSHal Finkel bool Changed = false;
358089b8ecfSHal Finkel
359089b8ecfSHal Finkel if (DisableVSXFMAMutate)
360089b8ecfSHal Finkel return Changed;
361089b8ecfSHal Finkel
3622c4ba3e9SKazu Hirata for (MachineBasicBlock &B : llvm::make_early_inc_range(MF))
363089b8ecfSHal Finkel if (processBlock(B))
364089b8ecfSHal Finkel Changed = true;
365089b8ecfSHal Finkel
366089b8ecfSHal Finkel return Changed;
367089b8ecfSHal Finkel }
368089b8ecfSHal Finkel
getAnalysisUsage__anon2db1c68d0111::PPCVSXFMAMutate369089b8ecfSHal Finkel void getAnalysisUsage(AnalysisUsage &AU) const override {
370089b8ecfSHal Finkel AU.addRequired<LiveIntervals>();
371089b8ecfSHal Finkel AU.addPreserved<LiveIntervals>();
372089b8ecfSHal Finkel AU.addRequired<SlotIndexes>();
373089b8ecfSHal Finkel AU.addPreserved<SlotIndexes>();
37431095d2fSKrzysztof Parzyszek AU.addRequired<MachineDominatorTree>();
37531095d2fSKrzysztof Parzyszek AU.addPreserved<MachineDominatorTree>();
376089b8ecfSHal Finkel MachineFunctionPass::getAnalysisUsage(AU);
377089b8ecfSHal Finkel }
378089b8ecfSHal Finkel };
379f00654e3SAlexander Kornienko }
380089b8ecfSHal Finkel
381089b8ecfSHal Finkel INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
382089b8ecfSHal Finkel "PowerPC VSX FMA Mutation", false, false)
383089b8ecfSHal Finkel INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
384089b8ecfSHal Finkel INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
38531095d2fSKrzysztof Parzyszek INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
386089b8ecfSHal Finkel INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
387089b8ecfSHal Finkel "PowerPC VSX FMA Mutation", false, false)
388089b8ecfSHal Finkel
389089b8ecfSHal Finkel char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
390089b8ecfSHal Finkel
391089b8ecfSHal Finkel char PPCVSXFMAMutate::ID = 0;
createPPCVSXFMAMutatePass()39270ad98acSNAKAMURA Takumi FunctionPass *llvm::createPPCVSXFMAMutatePass() {
39370ad98acSNAKAMURA Takumi return new PPCVSXFMAMutate();
39470ad98acSNAKAMURA Takumi }
395