1 //===--------------- PPCVSXFMAMutate.cpp - VSX FMA Mutation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass mutates the form of VSX FMA instructions to avoid unnecessary
13 //===----------------------------------------------------------------------===//
15 #include "PPCInstrInfo.h"
16 #include "MCTargetDesc/PPCPredicates.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"
32 #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"
41 static cl::opt<bool> DisableVSXFMAMutate("disable-ppc-vsx-fma-mutation",
42 cl::desc("Disable VSX FMA instruction mutation"), cl::Hidden);
44 #define DEBUG_TYPE "ppc-vsx-fma-mutate"
46 namespace llvm { namespace PPC {
47 int getAltVSXFMAOpcode(uint16_t Opcode);
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 {
56 PPCVSXFMAMutate() : MachineFunctionPass(ID) {
57 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
61 const PPCInstrInfo *TII;
64 bool processBlock(MachineBasicBlock &MBB) {
67 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
68 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
69 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
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).
79 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
83 // This pass is run after register coalescing, and so we're looking for
84 // a situation like this:
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
90 // %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19,
91 // %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19
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
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
102 SlotIndex FMAIdx = LIS->getInstructionIndex(MI);
104 VNInfo *AddendValNo =
105 LIS->getInterval(MI->getOperand(1).getReg()).Query(FMAIdx).valueIn();
106 MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
108 // The addend and this instruction must be in the same block.
110 if (!AddendMI || AddendMI->getParent() != MI->getParent())
113 // The addend must be a full copy within the same register class.
115 if (!AddendMI->isFullCopy())
118 unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
119 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) {
120 if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
121 MRI.getRegClass(AddendSrcReg))
124 // If AddendSrcReg is a physical register, make sure the destination
125 // register class contains it.
126 if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
127 ->contains(AddendSrcReg))
131 // In theory, there could be other uses of the addend copy before this
132 // fma. We could deal with this, but that would require additional
133 // logic below and I suspect it will not occur in any relevant
134 // situations. Additionally, check whether the copy source is killed
135 // prior to the fma. In order to replace the addend here with the
136 // source of the copy, it must still be live here. We can't use
137 // interval testing for a physical register, so as long as we're
138 // walking the MIs we may as well test liveness here.
140 // FIXME: There is a case that occurs in practice, like this:
141 // %vreg9<def> = COPY %F1; VSSRC:%vreg9
143 // %vreg6<def> = COPY %vreg9; VSSRC:%vreg6,%vreg9
144 // %vreg7<def> = COPY %vreg9; VSSRC:%vreg7,%vreg9
145 // %vreg9<def,tied1> = XSMADDASP %vreg9<tied0>, %vreg1, %vreg4; VSSRC:
146 // %vreg6<def,tied1> = XSMADDASP %vreg6<tied0>, %vreg1, %vreg2; VSSRC:
147 // %vreg7<def,tied1> = XSMADDASP %vreg7<tied0>, %vreg1, %vreg3; VSSRC:
148 // which prevents an otherwise-profitable transformation.
149 bool OtherUsers = false, KillsAddendSrc = false;
150 for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
152 if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
156 if (J->modifiesRegister(AddendSrcReg, TRI) ||
157 J->killsRegister(AddendSrcReg, TRI)) {
158 KillsAddendSrc = true;
163 if (OtherUsers || KillsAddendSrc)
166 // Find one of the product operands that is killed by this instruction.
168 unsigned KilledProdOp = 0, OtherProdOp = 0;
169 if (LIS->getInterval(MI->getOperand(2).getReg())
170 .Query(FMAIdx).isKill()) {
173 } else if (LIS->getInterval(MI->getOperand(3).getReg())
174 .Query(FMAIdx).isKill()) {
179 // If there are no killed product operands, then this transformation is
180 // likely not profitable.
184 // For virtual registers, verify that the addend source register
185 // is live here (as should have been assured above).
186 assert((!TargetRegisterInfo::isVirtualRegister(AddendSrcReg) ||
187 LIS->getInterval(AddendSrcReg).liveAt(FMAIdx)) &&
188 "Addend source register is not live!");
190 // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
192 unsigned KilledProdReg = MI->getOperand(KilledProdOp).getReg();
193 unsigned OtherProdReg = MI->getOperand(OtherProdOp).getReg();
195 unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
196 unsigned KilledProdSubReg = MI->getOperand(KilledProdOp).getSubReg();
197 unsigned OtherProdSubReg = MI->getOperand(OtherProdOp).getSubReg();
199 bool AddRegKill = AddendMI->getOperand(1).isKill();
200 bool KilledProdRegKill = MI->getOperand(KilledProdOp).isKill();
201 bool OtherProdRegKill = MI->getOperand(OtherProdOp).isKill();
203 bool AddRegUndef = AddendMI->getOperand(1).isUndef();
204 bool KilledProdRegUndef = MI->getOperand(KilledProdOp).isUndef();
205 bool OtherProdRegUndef = MI->getOperand(OtherProdOp).isUndef();
207 unsigned OldFMAReg = MI->getOperand(0).getReg();
209 // The transformation doesn't work well with things like:
210 // %vreg5 = A-form-op %vreg5, %vreg11, %vreg5;
211 // so leave such things alone.
212 if (OldFMAReg == KilledProdReg)
215 assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
216 "Addend copy not tied to old FMA output!");
218 DEBUG(dbgs() << "VSX FMA Mutation:\n " << *MI;);
220 MI->getOperand(0).setReg(KilledProdReg);
221 MI->getOperand(1).setReg(KilledProdReg);
222 MI->getOperand(3).setReg(AddendSrcReg);
223 MI->getOperand(2).setReg(OtherProdReg);
225 MI->getOperand(0).setSubReg(KilledProdSubReg);
226 MI->getOperand(1).setSubReg(KilledProdSubReg);
227 MI->getOperand(3).setSubReg(AddSubReg);
228 MI->getOperand(2).setSubReg(OtherProdSubReg);
230 MI->getOperand(1).setIsKill(KilledProdRegKill);
231 MI->getOperand(3).setIsKill(AddRegKill);
232 MI->getOperand(2).setIsKill(OtherProdRegKill);
234 MI->getOperand(1).setIsUndef(KilledProdRegUndef);
235 MI->getOperand(3).setIsUndef(AddRegUndef);
236 MI->getOperand(2).setIsUndef(OtherProdRegUndef);
238 MI->setDesc(TII->get(AltOpc));
240 DEBUG(dbgs() << " -> " << *MI);
242 // The killed product operand was killed here, so we can reuse it now
243 // for the result of the fma.
245 LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
246 VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
247 for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
249 MachineOperand &UseMO = *UI;
250 MachineInstr *UseMI = UseMO.getParent();
253 // Don't replace the result register of the copy we're about to erase.
254 if (UseMI == AddendMI)
257 UseMO.setReg(KilledProdReg);
258 UseMO.setSubReg(KilledProdSubReg);
261 // Extend the live intervals of the killed product operand to hold the
264 LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
265 for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
267 // Don't add the segment that corresponds to the original copy.
268 if (AI->valno == AddendValNo)
271 VNInfo *NewFMAValNo =
272 NewFMAInt.getNextValue(AI->start,
273 LIS->getVNInfoAllocator());
275 NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
278 DEBUG(dbgs() << " extended: " << NewFMAInt << '\n');
280 // Extend the live interval of the addend source (it might end at the
281 // copy to be removed, or somewhere in between there and here). This
282 // is necessary only if it is a physical register.
283 if (!TargetRegisterInfo::isVirtualRegister(AddendSrcReg))
284 for (MCRegUnitIterator Units(AddendSrcReg, TRI); Units.isValid();
286 unsigned Unit = *Units;
288 LiveRange &AddendSrcRange = LIS->getRegUnit(Unit);
289 AddendSrcRange.extendInBlock(LIS->getMBBStartIdx(&MBB),
290 FMAIdx.getRegSlot());
291 DEBUG(dbgs() << " extended: " << AddendSrcRange << '\n');
294 FMAInt.removeValNo(FMAValNo);
295 DEBUG(dbgs() << " trimmed: " << FMAInt << '\n');
297 // Remove the (now unused) copy.
299 DEBUG(dbgs() << " removing: " << *AddendMI << '\n');
300 LIS->RemoveMachineInstrFromMaps(AddendMI);
301 AddendMI->eraseFromParent();
310 bool runOnMachineFunction(MachineFunction &MF) override {
311 // If we don't have VSX then go ahead and return without doing
313 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
317 LIS = &getAnalysis<LiveIntervals>();
319 TII = STI.getInstrInfo();
321 bool Changed = false;
323 if (DisableVSXFMAMutate)
326 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
327 MachineBasicBlock &B = *I++;
335 void getAnalysisUsage(AnalysisUsage &AU) const override {
336 AU.addRequired<LiveIntervals>();
337 AU.addPreserved<LiveIntervals>();
338 AU.addRequired<SlotIndexes>();
339 AU.addPreserved<SlotIndexes>();
340 MachineFunctionPass::getAnalysisUsage(AU);
345 INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
346 "PowerPC VSX FMA Mutation", false, false)
347 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
348 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
349 INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
350 "PowerPC VSX FMA Mutation", false, false)
352 char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
354 char PPCVSXFMAMutate::ID = 0;
356 llvm::createPPCVSXFMAMutatePass() { return new PPCVSXFMAMutate(); }