1089b8ecfSHal Finkel //===--------------- PPCVSXFMAMutate.cpp - VSX FMA Mutation ---------------===//
2089b8ecfSHal Finkel //
3089b8ecfSHal Finkel //                     The LLVM Compiler Infrastructure
4089b8ecfSHal Finkel //
5089b8ecfSHal Finkel // This file is distributed under the University of Illinois Open Source
6089b8ecfSHal Finkel // License. See LICENSE.TXT for details.
7089b8ecfSHal Finkel //
8089b8ecfSHal Finkel //===----------------------------------------------------------------------===//
9089b8ecfSHal Finkel //
10089b8ecfSHal Finkel // This pass mutates the form of VSX FMA instructions to avoid unnecessary
11089b8ecfSHal Finkel // copies.
12089b8ecfSHal Finkel //
13089b8ecfSHal Finkel //===----------------------------------------------------------------------===//
14089b8ecfSHal Finkel 
15089b8ecfSHal Finkel #include "PPCInstrInfo.h"
16089b8ecfSHal Finkel #include "MCTargetDesc/PPCPredicates.h"
17089b8ecfSHal Finkel #include "PPC.h"
18089b8ecfSHal Finkel #include "PPCInstrBuilder.h"
19089b8ecfSHal Finkel #include "PPCMachineFunctionInfo.h"
20089b8ecfSHal Finkel #include "PPCTargetMachine.h"
21089b8ecfSHal Finkel #include "llvm/ADT/STLExtras.h"
22089b8ecfSHal Finkel #include "llvm/ADT/Statistic.h"
23089b8ecfSHal Finkel #include "llvm/CodeGen/LiveIntervalAnalysis.h"
24*31095d2fSKrzysztof Parzyszek #include "llvm/CodeGen/MachineDominators.h"
25089b8ecfSHal Finkel #include "llvm/CodeGen/MachineFrameInfo.h"
26089b8ecfSHal Finkel #include "llvm/CodeGen/MachineFunctionPass.h"
27089b8ecfSHal Finkel #include "llvm/CodeGen/MachineInstrBuilder.h"
28089b8ecfSHal Finkel #include "llvm/CodeGen/MachineMemOperand.h"
29089b8ecfSHal Finkel #include "llvm/CodeGen/MachineRegisterInfo.h"
30089b8ecfSHal Finkel #include "llvm/CodeGen/PseudoSourceValue.h"
31089b8ecfSHal Finkel #include "llvm/CodeGen/ScheduleDAG.h"
32089b8ecfSHal Finkel #include "llvm/CodeGen/SlotIndexes.h"
33089b8ecfSHal Finkel #include "llvm/MC/MCAsmInfo.h"
34089b8ecfSHal Finkel #include "llvm/Support/CommandLine.h"
35089b8ecfSHal Finkel #include "llvm/Support/Debug.h"
36089b8ecfSHal Finkel #include "llvm/Support/ErrorHandling.h"
37089b8ecfSHal Finkel #include "llvm/Support/TargetRegistry.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;
63089b8ecfSHal 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:
71089b8ecfSHal 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         //   ...
93089b8ecfSHal Finkel         //   %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
94089b8ecfSHal Finkel         //   %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
95089b8ecfSHal Finkel         //                         %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
96089b8ecfSHal Finkel         //   ...
97089b8ecfSHal Finkel         //   %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19,
98089b8ecfSHal Finkel         //                         %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19
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:
102089b8ecfSHal Finkel         //   %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
103089b8ecfSHal Finkel         //                         %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
104089b8ecfSHal Finkel         // is replaced by:
105089b8ecfSHal Finkel         //   %vreg16<def,tied1> = XSMADDMDP %vreg16<tied0>, %vreg18, %vreg9,
106089b8ecfSHal Finkel         //                         %RM<imp-use>; VSLRC:%vreg16,%vreg18,%vreg9
107089b8ecfSHal Finkel         // and we remove: %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
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 
130089b8ecfSHal Finkel         unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
131089b8ecfSHal Finkel         if (TargetRegisterInfo::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:
1538acae527SHal Finkel         //   %vreg9<def> = COPY %F1; VSSRC:%vreg9
1548acae527SHal Finkel         //   ...
1558acae527SHal Finkel         //   %vreg6<def> = COPY %vreg9; VSSRC:%vreg6,%vreg9
1568acae527SHal Finkel         //   %vreg7<def> = COPY %vreg9; VSSRC:%vreg7,%vreg9
1578acae527SHal Finkel         //   %vreg9<def,tied1> = XSMADDASP %vreg9<tied0>, %vreg1, %vreg4; VSSRC:
1588acae527SHal Finkel         //   %vreg6<def,tied1> = XSMADDASP %vreg6<tied0>, %vreg1, %vreg2; VSSRC:
1598acae527SHal Finkel         //   %vreg7<def,tied1> = XSMADDASP %vreg7<tied0>, %vreg1, %vreg3; 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:
180d62d8b77SKyle Butt         //    %vreg5 = A-form-op %vreg5, %vreg11, %vreg5;
181d62d8b77SKyle Butt         // unless vreg11 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:
183d62d8b77SKyle Butt         //   %vreg5 = A-form-op %vreg5, %vreg5, %vreg11;
184d62d8b77SKyle Butt         // where vreg5 and vreg11 are both kills. This case would be skipped
185d62d8b77SKyle Butt         // otherwise.
186e5a22f44SDuncan P. N. Exon Smith         unsigned 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;
190e5a22f44SDuncan P. N. Exon Smith         unsigned Reg2 = MI.getOperand(2).getReg();
191e5a22f44SDuncan P. N. Exon Smith         unsigned 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.
2120f2ddcb8SHal Finkel         if (TargetRegisterInfo::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 
218e5a22f44SDuncan P. N. Exon Smith         unsigned KilledProdReg = MI.getOperand(KilledProdOp).getReg();
219e5a22f44SDuncan P. N. Exon Smith         unsigned 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 
244e5a22f44SDuncan P. N. Exon Smith         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 
276e5a22f44SDuncan P. N. Exon Smith         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);
300089b8ecfSHal Finkel         for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
301089b8ecfSHal Finkel              AI != AE; ++AI) {
302089b8ecfSHal Finkel           // Don't add the segment that corresponds to the original copy.
303089b8ecfSHal Finkel           if (AI->valno == AddendValNo)
304089b8ecfSHal Finkel             continue;
305089b8ecfSHal Finkel 
306089b8ecfSHal Finkel           VNInfo *NewFMAValNo =
307089b8ecfSHal Finkel             NewFMAInt.getNextValue(AI->start,
308089b8ecfSHal Finkel                                    LIS->getVNInfoAllocator());
309089b8ecfSHal Finkel 
310089b8ecfSHal Finkel           NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
311089b8ecfSHal Finkel                                                      NewFMAValNo));
312089b8ecfSHal Finkel         }
313089b8ecfSHal Finkel         DEBUG(dbgs() << "  extended: " << NewFMAInt << '\n');
314089b8ecfSHal Finkel 
315673b493eSHal Finkel         // Extend the live interval of the addend source (it might end at the
316673b493eSHal Finkel         // copy to be removed, or somewhere in between there and here). This
317673b493eSHal Finkel         // is necessary only if it is a physical register.
318673b493eSHal Finkel         if (!TargetRegisterInfo::isVirtualRegister(AddendSrcReg))
319673b493eSHal Finkel           for (MCRegUnitIterator Units(AddendSrcReg, TRI); Units.isValid();
320673b493eSHal Finkel                ++Units) {
321673b493eSHal Finkel             unsigned Unit = *Units;
322673b493eSHal Finkel 
323673b493eSHal Finkel             LiveRange &AddendSrcRange = LIS->getRegUnit(Unit);
324673b493eSHal Finkel             AddendSrcRange.extendInBlock(LIS->getMBBStartIdx(&MBB),
325673b493eSHal Finkel                                          FMAIdx.getRegSlot());
326673b493eSHal Finkel             DEBUG(dbgs() << "  extended: " << AddendSrcRange << '\n');
327673b493eSHal Finkel           }
328673b493eSHal Finkel 
329089b8ecfSHal Finkel         FMAInt.removeValNo(FMAValNo);
330089b8ecfSHal Finkel         DEBUG(dbgs() << "  trimmed:  " << FMAInt << '\n');
331089b8ecfSHal Finkel 
332089b8ecfSHal Finkel         // Remove the (now unused) copy.
333089b8ecfSHal Finkel 
334089b8ecfSHal Finkel         DEBUG(dbgs() << "  removing: " << *AddendMI << '\n');
3353ac9cc61SDuncan P. N. Exon Smith         LIS->RemoveMachineInstrFromMaps(*AddendMI);
336089b8ecfSHal Finkel         AddendMI->eraseFromParent();
337089b8ecfSHal Finkel 
338089b8ecfSHal Finkel         Changed = true;
339089b8ecfSHal Finkel       }
340089b8ecfSHal Finkel 
341089b8ecfSHal Finkel       return Changed;
342089b8ecfSHal Finkel     }
343089b8ecfSHal Finkel 
344089b8ecfSHal Finkel public:
345089b8ecfSHal Finkel     bool runOnMachineFunction(MachineFunction &MF) override {
346289bd5f6SAndrew Kaylor       if (skipFunction(*MF.getFunction()))
347289bd5f6SAndrew Kaylor         return false;
348289bd5f6SAndrew Kaylor 
349089b8ecfSHal Finkel       // If we don't have VSX then go ahead and return without doing
350089b8ecfSHal Finkel       // anything.
351089b8ecfSHal Finkel       const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
352089b8ecfSHal Finkel       if (!STI.hasVSX())
353089b8ecfSHal Finkel         return false;
354089b8ecfSHal Finkel 
355089b8ecfSHal Finkel       LIS = &getAnalysis<LiveIntervals>();
356089b8ecfSHal Finkel 
357089b8ecfSHal Finkel       TII = STI.getInstrInfo();
358089b8ecfSHal Finkel 
359089b8ecfSHal Finkel       bool Changed = false;
360089b8ecfSHal Finkel 
361089b8ecfSHal Finkel       if (DisableVSXFMAMutate)
362089b8ecfSHal Finkel         return Changed;
363089b8ecfSHal Finkel 
364089b8ecfSHal Finkel       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
365089b8ecfSHal Finkel         MachineBasicBlock &B = *I++;
366089b8ecfSHal Finkel         if (processBlock(B))
367089b8ecfSHal Finkel           Changed = true;
368089b8ecfSHal Finkel       }
369089b8ecfSHal Finkel 
370089b8ecfSHal Finkel       return Changed;
371089b8ecfSHal Finkel     }
372089b8ecfSHal Finkel 
373089b8ecfSHal Finkel     void getAnalysisUsage(AnalysisUsage &AU) const override {
374089b8ecfSHal Finkel       AU.addRequired<LiveIntervals>();
375089b8ecfSHal Finkel       AU.addPreserved<LiveIntervals>();
376089b8ecfSHal Finkel       AU.addRequired<SlotIndexes>();
377089b8ecfSHal Finkel       AU.addPreserved<SlotIndexes>();
378*31095d2fSKrzysztof Parzyszek       AU.addRequired<MachineDominatorTree>();
379*31095d2fSKrzysztof Parzyszek       AU.addPreserved<MachineDominatorTree>();
380089b8ecfSHal Finkel       MachineFunctionPass::getAnalysisUsage(AU);
381089b8ecfSHal Finkel     }
382089b8ecfSHal Finkel   };
383f00654e3SAlexander Kornienko }
384089b8ecfSHal Finkel 
385089b8ecfSHal Finkel INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
386089b8ecfSHal Finkel                       "PowerPC VSX FMA Mutation", false, false)
387089b8ecfSHal Finkel INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
388089b8ecfSHal Finkel INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
389*31095d2fSKrzysztof Parzyszek INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
390089b8ecfSHal Finkel INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
391089b8ecfSHal Finkel                     "PowerPC VSX FMA Mutation", false, false)
392089b8ecfSHal Finkel 
393089b8ecfSHal Finkel char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
394089b8ecfSHal Finkel 
395089b8ecfSHal Finkel char PPCVSXFMAMutate::ID = 0;
39670ad98acSNAKAMURA Takumi FunctionPass *llvm::createPPCVSXFMAMutatePass() {
39770ad98acSNAKAMURA Takumi   return new PPCVSXFMAMutate();
39870ad98acSNAKAMURA Takumi }
399