1089b8ecfSHal Finkel //===--------------- PPCVSXFMAMutate.cpp - VSX FMA Mutation ---------------===//
2089b8ecfSHal Finkel //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler 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"
32089b8ecfSHal Finkel #include "llvm/MC/MCAsmInfo.h"
33089b8ecfSHal Finkel #include "llvm/Support/CommandLine.h"
34089b8ecfSHal Finkel #include "llvm/Support/Debug.h"
35089b8ecfSHal Finkel #include "llvm/Support/ErrorHandling.h"
36089b8ecfSHal Finkel #include "llvm/Support/TargetRegistry.h"
37089b8ecfSHal Finkel #include "llvm/Support/raw_ostream.h"
38089b8ecfSHal Finkel 
39089b8ecfSHal Finkel using namespace llvm;
40089b8ecfSHal Finkel 
416e676a84STim Shen // Temporarily disable FMA mutation by default, since it doesn't handle
426e676a84STim Shen // cross-basic-block intervals well.
436e676a84STim Shen // See: http://lists.llvm.org/pipermail/llvm-dev/2016-February/095669.html
446e676a84STim Shen //      http://reviews.llvm.org/D17087
456e676a84STim Shen static cl::opt<bool> DisableVSXFMAMutate(
466e676a84STim Shen     "disable-ppc-vsx-fma-mutation",
476e676a84STim Shen     cl::desc("Disable VSX FMA instruction mutation"), cl::init(true),
486e676a84STim Shen     cl::Hidden);
49089b8ecfSHal Finkel 
50089b8ecfSHal Finkel #define DEBUG_TYPE "ppc-vsx-fma-mutate"
51089b8ecfSHal Finkel 
52089b8ecfSHal Finkel namespace llvm { namespace PPC {
53089b8ecfSHal Finkel   int getAltVSXFMAOpcode(uint16_t Opcode);
54089b8ecfSHal Finkel } }
55089b8ecfSHal Finkel 
56089b8ecfSHal Finkel namespace {
57089b8ecfSHal Finkel   // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers
58089b8ecfSHal Finkel   // (Altivec and scalar floating-point registers), we need to transform the
59089b8ecfSHal Finkel   // copies into subregister copies with other restrictions.
60089b8ecfSHal Finkel   struct PPCVSXFMAMutate : public MachineFunctionPass {
61089b8ecfSHal Finkel     static char ID;
62089b8ecfSHal Finkel     PPCVSXFMAMutate() : MachineFunctionPass(ID) {
63089b8ecfSHal Finkel       initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
64089b8ecfSHal Finkel     }
65089b8ecfSHal Finkel 
66089b8ecfSHal Finkel     LiveIntervals *LIS;
67089b8ecfSHal Finkel     const PPCInstrInfo *TII;
68089b8ecfSHal Finkel 
69089b8ecfSHal Finkel protected:
70089b8ecfSHal Finkel     bool processBlock(MachineBasicBlock &MBB) {
71089b8ecfSHal Finkel       bool Changed = false;
72089b8ecfSHal Finkel 
73089b8ecfSHal Finkel       MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
74089b8ecfSHal Finkel       const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
75089b8ecfSHal Finkel       for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
76089b8ecfSHal Finkel            I != IE; ++I) {
77e5a22f44SDuncan P. N. Exon Smith         MachineInstr &MI = *I;
78089b8ecfSHal Finkel 
79089b8ecfSHal Finkel         // The default (A-type) VSX FMA form kills the addend (it is taken from
80089b8ecfSHal Finkel         // the target register, which is then updated to reflect the result of
81089b8ecfSHal Finkel         // the FMA). If the instruction, however, kills one of the registers
82089b8ecfSHal Finkel         // used for the product, then we can use the M-form instruction (which
83089b8ecfSHal Finkel         // will take that value from the to-be-defined register).
84089b8ecfSHal Finkel 
85e5a22f44SDuncan P. N. Exon Smith         int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode());
86089b8ecfSHal Finkel         if (AltOpc == -1)
87089b8ecfSHal Finkel           continue;
88089b8ecfSHal Finkel 
89089b8ecfSHal Finkel         // This pass is run after register coalescing, and so we're looking for
90089b8ecfSHal Finkel         // a situation like this:
91089b8ecfSHal Finkel         //   ...
92a8a83d15SFrancis Visoiu Mistrih         //   %5 = COPY %9; VSLRC:%5,%9
9393ef1458SFrancis Visoiu Mistrih         //   %5<def,tied1> = XSMADDADP %5<tied0>, %17, %16,
94a8a83d15SFrancis Visoiu Mistrih         //                         implicit %rm; VSLRC:%5,%17,%16
95089b8ecfSHal Finkel         //   ...
9693ef1458SFrancis Visoiu Mistrih         //   %9<def,tied1> = XSMADDADP %9<tied0>, %17, %19,
97a8a83d15SFrancis Visoiu Mistrih         //                         implicit %rm; VSLRC:%9,%17,%19
98089b8ecfSHal Finkel         //   ...
99089b8ecfSHal Finkel         // Where we can eliminate the copy by changing from the A-type to the
100089b8ecfSHal Finkel         // M-type instruction. Specifically, for this example, this means:
10193ef1458SFrancis Visoiu Mistrih         //   %5<def,tied1> = XSMADDADP %5<tied0>, %17, %16,
102a8a83d15SFrancis Visoiu Mistrih         //                         implicit %rm; VSLRC:%5,%17,%16
103089b8ecfSHal Finkel         // is replaced by:
10493ef1458SFrancis Visoiu Mistrih         //   %16<def,tied1> = XSMADDMDP %16<tied0>, %18, %9,
105a8a83d15SFrancis Visoiu Mistrih         //                         implicit %rm; VSLRC:%16,%18,%9
106a8a83d15SFrancis Visoiu Mistrih         // and we remove: %5 = COPY %9; VSLRC:%5,%9
107089b8ecfSHal Finkel 
108e5a22f44SDuncan P. N. Exon Smith         SlotIndex FMAIdx = LIS->getInstructionIndex(MI);
109089b8ecfSHal Finkel 
110089b8ecfSHal Finkel         VNInfo *AddendValNo =
111e5a22f44SDuncan P. N. Exon Smith             LIS->getInterval(MI.getOperand(1).getReg()).Query(FMAIdx).valueIn();
1127ffe55aeSHal Finkel 
113ff9639d6SHal Finkel         // This can be null if the register is undef.
1147ffe55aeSHal Finkel         if (!AddendValNo)
115ff9639d6SHal Finkel           continue;
116ff9639d6SHal Finkel 
117089b8ecfSHal Finkel         MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
118089b8ecfSHal Finkel 
119089b8ecfSHal Finkel         // The addend and this instruction must be in the same block.
120089b8ecfSHal Finkel 
121e5a22f44SDuncan P. N. Exon Smith         if (!AddendMI || AddendMI->getParent() != MI.getParent())
122089b8ecfSHal Finkel           continue;
123089b8ecfSHal Finkel 
124089b8ecfSHal Finkel         // The addend must be a full copy within the same register class.
125089b8ecfSHal Finkel 
126089b8ecfSHal Finkel         if (!AddendMI->isFullCopy())
127089b8ecfSHal Finkel           continue;
128089b8ecfSHal Finkel 
129089b8ecfSHal Finkel         unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
130089b8ecfSHal Finkel         if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) {
131089b8ecfSHal Finkel           if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
132089b8ecfSHal Finkel               MRI.getRegClass(AddendSrcReg))
133089b8ecfSHal Finkel             continue;
134089b8ecfSHal Finkel         } else {
135089b8ecfSHal Finkel           // If AddendSrcReg is a physical register, make sure the destination
136089b8ecfSHal Finkel           // register class contains it.
137089b8ecfSHal Finkel           if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
138089b8ecfSHal Finkel                 ->contains(AddendSrcReg))
139089b8ecfSHal Finkel             continue;
140089b8ecfSHal Finkel         }
141089b8ecfSHal Finkel 
142089b8ecfSHal Finkel         // In theory, there could be other uses of the addend copy before this
143089b8ecfSHal Finkel         // fma.  We could deal with this, but that would require additional
144089b8ecfSHal Finkel         // logic below and I suspect it will not occur in any relevant
145089b8ecfSHal Finkel         // situations.  Additionally, check whether the copy source is killed
146089b8ecfSHal Finkel         // prior to the fma.  In order to replace the addend here with the
147089b8ecfSHal Finkel         // source of the copy, it must still be live here.  We can't use
148089b8ecfSHal Finkel         // interval testing for a physical register, so as long as we're
149089b8ecfSHal Finkel         // walking the MIs we may as well test liveness here.
1508acae527SHal Finkel         //
1518acae527SHal Finkel         // FIXME: There is a case that occurs in practice, like this:
152a8a83d15SFrancis Visoiu Mistrih         //   %9 = COPY %f1; VSSRC:%9
1538acae527SHal Finkel         //   ...
154a8a83d15SFrancis Visoiu Mistrih         //   %6 = COPY %9; VSSRC:%6,%9
155a8a83d15SFrancis Visoiu Mistrih         //   %7 = COPY %9; VSSRC:%7,%9
15693ef1458SFrancis Visoiu Mistrih         //   %9<def,tied1> = XSMADDASP %9<tied0>, %1, %4; VSSRC:
15793ef1458SFrancis Visoiu Mistrih         //   %6<def,tied1> = XSMADDASP %6<tied0>, %1, %2; VSSRC:
15893ef1458SFrancis Visoiu Mistrih         //   %7<def,tied1> = XSMADDASP %7<tied0>, %1, %3; VSSRC:
1598acae527SHal Finkel         // which prevents an otherwise-profitable transformation.
160089b8ecfSHal Finkel         bool OtherUsers = false, KillsAddendSrc = false;
161089b8ecfSHal Finkel         for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
162089b8ecfSHal Finkel              J != JE; --J) {
163089b8ecfSHal Finkel           if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
164089b8ecfSHal Finkel             OtherUsers = true;
165089b8ecfSHal Finkel             break;
166089b8ecfSHal Finkel           }
167089b8ecfSHal Finkel           if (J->modifiesRegister(AddendSrcReg, TRI) ||
168089b8ecfSHal Finkel               J->killsRegister(AddendSrcReg, TRI)) {
169089b8ecfSHal Finkel             KillsAddendSrc = true;
170089b8ecfSHal Finkel             break;
171089b8ecfSHal Finkel           }
172089b8ecfSHal Finkel         }
173089b8ecfSHal Finkel 
174089b8ecfSHal Finkel         if (OtherUsers || KillsAddendSrc)
175089b8ecfSHal Finkel           continue;
176089b8ecfSHal Finkel 
177089b8ecfSHal Finkel 
178d62d8b77SKyle Butt         // The transformation doesn't work well with things like:
17993ef1458SFrancis Visoiu Mistrih         //    %5 = A-form-op %5, %11, %5;
18093ef1458SFrancis Visoiu Mistrih         // unless %11 is also a kill, so skip when it is not,
181d62d8b77SKyle Butt         // and check operand 3 to see it is also a kill to handle the case:
18293ef1458SFrancis Visoiu Mistrih         //   %5 = A-form-op %5, %5, %11;
18393ef1458SFrancis Visoiu Mistrih         // where %5 and %11 are both kills. This case would be skipped
184d62d8b77SKyle Butt         // otherwise.
185e5a22f44SDuncan P. N. Exon Smith         unsigned OldFMAReg = MI.getOperand(0).getReg();
186d62d8b77SKyle Butt 
187d62d8b77SKyle Butt         // Find one of the product operands that is killed by this instruction.
188089b8ecfSHal Finkel         unsigned KilledProdOp = 0, OtherProdOp = 0;
189e5a22f44SDuncan P. N. Exon Smith         unsigned Reg2 = MI.getOperand(2).getReg();
190e5a22f44SDuncan P. N. Exon Smith         unsigned Reg3 = MI.getOperand(3).getReg();
191d62d8b77SKyle Butt         if (LIS->getInterval(Reg2).Query(FMAIdx).isKill()
192d62d8b77SKyle Butt             && Reg2 != OldFMAReg) {
193089b8ecfSHal Finkel           KilledProdOp = 2;
194089b8ecfSHal Finkel           OtherProdOp  = 3;
195d62d8b77SKyle Butt         } else if (LIS->getInterval(Reg3).Query(FMAIdx).isKill()
196d62d8b77SKyle Butt             && Reg3 != OldFMAReg) {
197089b8ecfSHal Finkel           KilledProdOp = 3;
198089b8ecfSHal Finkel           OtherProdOp  = 2;
199089b8ecfSHal Finkel         }
200089b8ecfSHal Finkel 
201d62d8b77SKyle Butt         // If there are no usable killed product operands, then this
202d62d8b77SKyle Butt         // transformation is likely not profitable.
203089b8ecfSHal Finkel         if (!KilledProdOp)
204089b8ecfSHal Finkel           continue;
205089b8ecfSHal Finkel 
2060f2ddcb8SHal Finkel         // If the addend copy is used only by this MI, then the addend source
2070f2ddcb8SHal Finkel         // register is likely not live here. This could be fixed (based on the
2080f2ddcb8SHal Finkel         // legality checks above, the live range for the addend source register
2090f2ddcb8SHal Finkel         // could be extended), but it seems likely that such a trivial copy can
2100f2ddcb8SHal Finkel         // be coalesced away later, and thus is not worth the effort.
2110f2ddcb8SHal Finkel         if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg) &&
2120f2ddcb8SHal Finkel             !LIS->getInterval(AddendSrcReg).liveAt(FMAIdx))
2130f2ddcb8SHal Finkel           continue;
214089b8ecfSHal Finkel 
215089b8ecfSHal Finkel         // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
216089b8ecfSHal Finkel 
217e5a22f44SDuncan P. N. Exon Smith         unsigned KilledProdReg = MI.getOperand(KilledProdOp).getReg();
218e5a22f44SDuncan P. N. Exon Smith         unsigned OtherProdReg = MI.getOperand(OtherProdOp).getReg();
219089b8ecfSHal Finkel 
220089b8ecfSHal Finkel         unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
221e5a22f44SDuncan P. N. Exon Smith         unsigned KilledProdSubReg = MI.getOperand(KilledProdOp).getSubReg();
222e5a22f44SDuncan P. N. Exon Smith         unsigned OtherProdSubReg = MI.getOperand(OtherProdOp).getSubReg();
223089b8ecfSHal Finkel 
224089b8ecfSHal Finkel         bool AddRegKill = AddendMI->getOperand(1).isKill();
225e5a22f44SDuncan P. N. Exon Smith         bool KilledProdRegKill = MI.getOperand(KilledProdOp).isKill();
226e5a22f44SDuncan P. N. Exon Smith         bool OtherProdRegKill = MI.getOperand(OtherProdOp).isKill();
227089b8ecfSHal Finkel 
228089b8ecfSHal Finkel         bool AddRegUndef = AddendMI->getOperand(1).isUndef();
229e5a22f44SDuncan P. N. Exon Smith         bool KilledProdRegUndef = MI.getOperand(KilledProdOp).isUndef();
230e5a22f44SDuncan P. N. Exon Smith         bool OtherProdRegUndef = MI.getOperand(OtherProdOp).isUndef();
231089b8ecfSHal Finkel 
23228b01a51SKyle Butt         // If there isn't a class that fits, we can't perform the transform.
23328b01a51SKyle Butt         // This is needed for correctness with a mixture of VSX and Altivec
23428b01a51SKyle Butt         // instructions to make sure that a low VSX register is not assigned to
23528b01a51SKyle Butt         // the Altivec instruction.
23628b01a51SKyle Butt         if (!MRI.constrainRegClass(KilledProdReg,
23728b01a51SKyle Butt                                    MRI.getRegClass(OldFMAReg)))
23828b01a51SKyle Butt           continue;
23928b01a51SKyle Butt 
240089b8ecfSHal Finkel         assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
241089b8ecfSHal Finkel                "Addend copy not tied to old FMA output!");
242089b8ecfSHal Finkel 
243d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "VSX FMA Mutation:\n    " << MI);
244089b8ecfSHal Finkel 
245e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(0).setReg(KilledProdReg);
246e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(1).setReg(KilledProdReg);
247e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(3).setReg(AddendSrcReg);
248089b8ecfSHal Finkel 
249e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(0).setSubReg(KilledProdSubReg);
250e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(1).setSubReg(KilledProdSubReg);
251e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(3).setSubReg(AddSubReg);
252089b8ecfSHal Finkel 
253e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(1).setIsKill(KilledProdRegKill);
254e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(3).setIsKill(AddRegKill);
255089b8ecfSHal Finkel 
256e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(1).setIsUndef(KilledProdRegUndef);
257e5a22f44SDuncan P. N. Exon Smith         MI.getOperand(3).setIsUndef(AddRegUndef);
258089b8ecfSHal Finkel 
259e5a22f44SDuncan P. N. Exon Smith         MI.setDesc(TII->get(AltOpc));
260089b8ecfSHal Finkel 
261d62d8b77SKyle Butt         // If the addend is also a multiplicand, replace it with the addend
262d62d8b77SKyle Butt         // source in both places.
263d62d8b77SKyle Butt         if (OtherProdReg == AddendMI->getOperand(0).getReg()) {
264e5a22f44SDuncan P. N. Exon Smith           MI.getOperand(2).setReg(AddendSrcReg);
265e5a22f44SDuncan P. N. Exon Smith           MI.getOperand(2).setSubReg(AddSubReg);
266e5a22f44SDuncan P. N. Exon Smith           MI.getOperand(2).setIsKill(AddRegKill);
267e5a22f44SDuncan P. N. Exon Smith           MI.getOperand(2).setIsUndef(AddRegUndef);
268d62d8b77SKyle Butt         } else {
269e5a22f44SDuncan P. N. Exon Smith           MI.getOperand(2).setReg(OtherProdReg);
270e5a22f44SDuncan P. N. Exon Smith           MI.getOperand(2).setSubReg(OtherProdSubReg);
271e5a22f44SDuncan P. N. Exon Smith           MI.getOperand(2).setIsKill(OtherProdRegKill);
272e5a22f44SDuncan P. N. Exon Smith           MI.getOperand(2).setIsUndef(OtherProdRegUndef);
273d62d8b77SKyle Butt         }
274d62d8b77SKyle Butt 
275d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << " -> " << MI);
276089b8ecfSHal Finkel 
277089b8ecfSHal Finkel         // The killed product operand was killed here, so we can reuse it now
278089b8ecfSHal Finkel         // for the result of the fma.
279089b8ecfSHal Finkel 
280089b8ecfSHal Finkel         LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
281089b8ecfSHal Finkel         VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
282089b8ecfSHal Finkel         for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
283089b8ecfSHal Finkel              UI != UE;) {
284089b8ecfSHal Finkel           MachineOperand &UseMO = *UI;
285089b8ecfSHal Finkel           MachineInstr *UseMI = UseMO.getParent();
286089b8ecfSHal Finkel           ++UI;
287089b8ecfSHal Finkel 
288089b8ecfSHal Finkel           // Don't replace the result register of the copy we're about to erase.
289089b8ecfSHal Finkel           if (UseMI == AddendMI)
290089b8ecfSHal Finkel             continue;
291089b8ecfSHal Finkel 
29228b01a51SKyle Butt           UseMO.substVirtReg(KilledProdReg, KilledProdSubReg, *TRI);
293089b8ecfSHal Finkel         }
294089b8ecfSHal Finkel 
295089b8ecfSHal Finkel         // Extend the live intervals of the killed product operand to hold the
296089b8ecfSHal Finkel         // fma result.
297089b8ecfSHal Finkel 
298089b8ecfSHal Finkel         LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
299089b8ecfSHal Finkel         for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
300089b8ecfSHal Finkel              AI != AE; ++AI) {
301089b8ecfSHal Finkel           // Don't add the segment that corresponds to the original copy.
302089b8ecfSHal Finkel           if (AI->valno == AddendValNo)
303089b8ecfSHal Finkel             continue;
304089b8ecfSHal Finkel 
305089b8ecfSHal Finkel           VNInfo *NewFMAValNo =
306089b8ecfSHal Finkel             NewFMAInt.getNextValue(AI->start,
307089b8ecfSHal Finkel                                    LIS->getVNInfoAllocator());
308089b8ecfSHal Finkel 
309089b8ecfSHal Finkel           NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
310089b8ecfSHal Finkel                                                      NewFMAValNo));
311089b8ecfSHal Finkel         }
312d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "  extended: " << NewFMAInt << '\n');
313089b8ecfSHal Finkel 
314673b493eSHal Finkel         // Extend the live interval of the addend source (it might end at the
315673b493eSHal Finkel         // copy to be removed, or somewhere in between there and here). This
316673b493eSHal Finkel         // is necessary only if it is a physical register.
317673b493eSHal Finkel         if (!TargetRegisterInfo::isVirtualRegister(AddendSrcReg))
318673b493eSHal Finkel           for (MCRegUnitIterator Units(AddendSrcReg, TRI); Units.isValid();
319673b493eSHal Finkel                ++Units) {
320673b493eSHal Finkel             unsigned Unit = *Units;
321673b493eSHal Finkel 
322673b493eSHal Finkel             LiveRange &AddendSrcRange = LIS->getRegUnit(Unit);
323673b493eSHal Finkel             AddendSrcRange.extendInBlock(LIS->getMBBStartIdx(&MBB),
324673b493eSHal Finkel                                          FMAIdx.getRegSlot());
325d34e60caSNicola Zaghen             LLVM_DEBUG(dbgs() << "  extended: " << AddendSrcRange << '\n');
326673b493eSHal Finkel           }
327673b493eSHal Finkel 
328089b8ecfSHal Finkel         FMAInt.removeValNo(FMAValNo);
329d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "  trimmed:  " << FMAInt << '\n');
330089b8ecfSHal Finkel 
331089b8ecfSHal Finkel         // Remove the (now unused) copy.
332089b8ecfSHal Finkel 
333d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "  removing: " << *AddendMI << '\n');
3343ac9cc61SDuncan P. N. Exon Smith         LIS->RemoveMachineInstrFromMaps(*AddendMI);
335089b8ecfSHal Finkel         AddendMI->eraseFromParent();
336089b8ecfSHal Finkel 
337089b8ecfSHal Finkel         Changed = true;
338089b8ecfSHal Finkel       }
339089b8ecfSHal Finkel 
340089b8ecfSHal Finkel       return Changed;
341089b8ecfSHal Finkel     }
342089b8ecfSHal Finkel 
343089b8ecfSHal Finkel public:
344089b8ecfSHal Finkel     bool runOnMachineFunction(MachineFunction &MF) override {
345f1caa283SMatthias Braun       if (skipFunction(MF.getFunction()))
346289bd5f6SAndrew Kaylor         return false;
347289bd5f6SAndrew Kaylor 
348089b8ecfSHal Finkel       // If we don't have VSX then go ahead and return without doing
349089b8ecfSHal Finkel       // anything.
350089b8ecfSHal Finkel       const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
351089b8ecfSHal Finkel       if (!STI.hasVSX())
352089b8ecfSHal Finkel         return false;
353089b8ecfSHal Finkel 
354089b8ecfSHal Finkel       LIS = &getAnalysis<LiveIntervals>();
355089b8ecfSHal Finkel 
356089b8ecfSHal Finkel       TII = STI.getInstrInfo();
357089b8ecfSHal Finkel 
358089b8ecfSHal Finkel       bool Changed = false;
359089b8ecfSHal Finkel 
360089b8ecfSHal Finkel       if (DisableVSXFMAMutate)
361089b8ecfSHal Finkel         return Changed;
362089b8ecfSHal Finkel 
363089b8ecfSHal Finkel       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
364089b8ecfSHal Finkel         MachineBasicBlock &B = *I++;
365089b8ecfSHal Finkel         if (processBlock(B))
366089b8ecfSHal Finkel           Changed = true;
367089b8ecfSHal Finkel       }
368089b8ecfSHal Finkel 
369089b8ecfSHal Finkel       return Changed;
370089b8ecfSHal Finkel     }
371089b8ecfSHal Finkel 
372089b8ecfSHal Finkel     void getAnalysisUsage(AnalysisUsage &AU) const override {
373089b8ecfSHal Finkel       AU.addRequired<LiveIntervals>();
374089b8ecfSHal Finkel       AU.addPreserved<LiveIntervals>();
375089b8ecfSHal Finkel       AU.addRequired<SlotIndexes>();
376089b8ecfSHal Finkel       AU.addPreserved<SlotIndexes>();
37731095d2fSKrzysztof Parzyszek       AU.addRequired<MachineDominatorTree>();
37831095d2fSKrzysztof Parzyszek       AU.addPreserved<MachineDominatorTree>();
379089b8ecfSHal Finkel       MachineFunctionPass::getAnalysisUsage(AU);
380089b8ecfSHal Finkel     }
381089b8ecfSHal Finkel   };
382f00654e3SAlexander Kornienko }
383089b8ecfSHal Finkel 
384089b8ecfSHal Finkel INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
385089b8ecfSHal Finkel                       "PowerPC VSX FMA Mutation", false, false)
386089b8ecfSHal Finkel INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
387089b8ecfSHal Finkel INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
38831095d2fSKrzysztof Parzyszek INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
389089b8ecfSHal Finkel INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
390089b8ecfSHal Finkel                     "PowerPC VSX FMA Mutation", false, false)
391089b8ecfSHal Finkel 
392089b8ecfSHal Finkel char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
393089b8ecfSHal Finkel 
394089b8ecfSHal Finkel char PPCVSXFMAMutate::ID = 0;
39570ad98acSNAKAMURA Takumi FunctionPass *llvm::createPPCVSXFMAMutatePass() {
39670ad98acSNAKAMURA Takumi   return new PPCVSXFMAMutate();
39770ad98acSNAKAMURA Takumi }
398