1*9880d681SAndroid Build Coastguard Worker //===-------------- PPCVSXCopy.cpp - VSX Copy Legalization ----------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // A pass which deals with the complexity of generating legal VSX register
11*9880d681SAndroid Build Coastguard Worker // copies to/from register classes which partially overlap with the VSX
12*9880d681SAndroid Build Coastguard Worker // register file.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker #include "PPC.h"
17*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/PPCPredicates.h"
18*9880d681SAndroid Build Coastguard Worker #include "PPCHazardRecognizers.h"
19*9880d681SAndroid Build Coastguard Worker #include "PPCInstrBuilder.h"
20*9880d681SAndroid Build Coastguard Worker #include "PPCInstrInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "PPCMachineFunctionInfo.h"
22*9880d681SAndroid Build Coastguard Worker #include "PPCTargetMachine.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineMemOperand.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker using namespace llvm;
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "ppc-vsx-copy"
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker namespace llvm {
41*9880d681SAndroid Build Coastguard Worker void initializePPCVSXCopyPass(PassRegistry&);
42*9880d681SAndroid Build Coastguard Worker }
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker namespace {
45*9880d681SAndroid Build Coastguard Worker // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
46*9880d681SAndroid Build Coastguard Worker // (Altivec and scalar floating-point registers), we need to transform the
47*9880d681SAndroid Build Coastguard Worker // copies into subregister copies with other restrictions.
48*9880d681SAndroid Build Coastguard Worker struct PPCVSXCopy : public MachineFunctionPass {
49*9880d681SAndroid Build Coastguard Worker static char ID;
PPCVSXCopy__anon78a169ac0111::PPCVSXCopy50*9880d681SAndroid Build Coastguard Worker PPCVSXCopy() : MachineFunctionPass(ID) {
51*9880d681SAndroid Build Coastguard Worker initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII;
55*9880d681SAndroid Build Coastguard Worker
IsRegInClass__anon78a169ac0111::PPCVSXCopy56*9880d681SAndroid Build Coastguard Worker bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
57*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI) {
58*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(Reg)) {
59*9880d681SAndroid Build Coastguard Worker return RC->hasSubClassEq(MRI.getRegClass(Reg));
60*9880d681SAndroid Build Coastguard Worker } else if (RC->contains(Reg)) {
61*9880d681SAndroid Build Coastguard Worker return true;
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker return false;
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker
IsVSReg__anon78a169ac0111::PPCVSXCopy67*9880d681SAndroid Build Coastguard Worker bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
68*9880d681SAndroid Build Coastguard Worker return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
69*9880d681SAndroid Build Coastguard Worker }
70*9880d681SAndroid Build Coastguard Worker
IsVRReg__anon78a169ac0111::PPCVSXCopy71*9880d681SAndroid Build Coastguard Worker bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
72*9880d681SAndroid Build Coastguard Worker return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
73*9880d681SAndroid Build Coastguard Worker }
74*9880d681SAndroid Build Coastguard Worker
IsF8Reg__anon78a169ac0111::PPCVSXCopy75*9880d681SAndroid Build Coastguard Worker bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
76*9880d681SAndroid Build Coastguard Worker return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
77*9880d681SAndroid Build Coastguard Worker }
78*9880d681SAndroid Build Coastguard Worker
IsVSFReg__anon78a169ac0111::PPCVSXCopy79*9880d681SAndroid Build Coastguard Worker bool IsVSFReg(unsigned Reg, MachineRegisterInfo &MRI) {
80*9880d681SAndroid Build Coastguard Worker return IsRegInClass(Reg, &PPC::VSFRCRegClass, MRI);
81*9880d681SAndroid Build Coastguard Worker }
82*9880d681SAndroid Build Coastguard Worker
IsVSSReg__anon78a169ac0111::PPCVSXCopy83*9880d681SAndroid Build Coastguard Worker bool IsVSSReg(unsigned Reg, MachineRegisterInfo &MRI) {
84*9880d681SAndroid Build Coastguard Worker return IsRegInClass(Reg, &PPC::VSSRCRegClass, MRI);
85*9880d681SAndroid Build Coastguard Worker }
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker protected:
processBlock__anon78a169ac0111::PPCVSXCopy88*9880d681SAndroid Build Coastguard Worker bool processBlock(MachineBasicBlock &MBB) {
89*9880d681SAndroid Build Coastguard Worker bool Changed = false;
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
92*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
93*9880d681SAndroid Build Coastguard Worker I != IE; ++I) {
94*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = I;
95*9880d681SAndroid Build Coastguard Worker if (!MI->isFullCopy())
96*9880d681SAndroid Build Coastguard Worker continue;
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker MachineOperand &DstMO = MI->getOperand(0);
99*9880d681SAndroid Build Coastguard Worker MachineOperand &SrcMO = MI->getOperand(1);
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker if ( IsVSReg(DstMO.getReg(), MRI) &&
102*9880d681SAndroid Build Coastguard Worker !IsVSReg(SrcMO.getReg(), MRI)) {
103*9880d681SAndroid Build Coastguard Worker // This is a copy *to* a VSX register from a non-VSX register.
104*9880d681SAndroid Build Coastguard Worker Changed = true;
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *SrcRC =
107*9880d681SAndroid Build Coastguard Worker IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
108*9880d681SAndroid Build Coastguard Worker &PPC::VSLRCRegClass;
109*9880d681SAndroid Build Coastguard Worker assert((IsF8Reg(SrcMO.getReg(), MRI) ||
110*9880d681SAndroid Build Coastguard Worker IsVRReg(SrcMO.getReg(), MRI) ||
111*9880d681SAndroid Build Coastguard Worker IsVSSReg(SrcMO.getReg(), MRI) ||
112*9880d681SAndroid Build Coastguard Worker IsVSFReg(SrcMO.getReg(), MRI)) &&
113*9880d681SAndroid Build Coastguard Worker "Unknown source for a VSX copy");
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
116*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MI, MI->getDebugLoc(),
117*9880d681SAndroid Build Coastguard Worker TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
118*9880d681SAndroid Build Coastguard Worker .addImm(1) // add 1, not 0, because there is no implicit clearing
119*9880d681SAndroid Build Coastguard Worker // of the high bits.
120*9880d681SAndroid Build Coastguard Worker .addOperand(SrcMO)
121*9880d681SAndroid Build Coastguard Worker .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
122*9880d681SAndroid Build Coastguard Worker PPC::sub_64);
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker // The source of the original copy is now the new virtual register.
125*9880d681SAndroid Build Coastguard Worker SrcMO.setReg(NewVReg);
126*9880d681SAndroid Build Coastguard Worker } else if (!IsVSReg(DstMO.getReg(), MRI) &&
127*9880d681SAndroid Build Coastguard Worker IsVSReg(SrcMO.getReg(), MRI)) {
128*9880d681SAndroid Build Coastguard Worker // This is a copy *from* a VSX register to a non-VSX register.
129*9880d681SAndroid Build Coastguard Worker Changed = true;
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *DstRC =
132*9880d681SAndroid Build Coastguard Worker IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
133*9880d681SAndroid Build Coastguard Worker &PPC::VSLRCRegClass;
134*9880d681SAndroid Build Coastguard Worker assert((IsF8Reg(DstMO.getReg(), MRI) ||
135*9880d681SAndroid Build Coastguard Worker IsVSFReg(DstMO.getReg(), MRI) ||
136*9880d681SAndroid Build Coastguard Worker IsVSSReg(DstMO.getReg(), MRI) ||
137*9880d681SAndroid Build Coastguard Worker IsVRReg(DstMO.getReg(), MRI)) &&
138*9880d681SAndroid Build Coastguard Worker "Unknown destination for a VSX copy");
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker // Copy the VSX value into a new VSX register of the correct subclass.
141*9880d681SAndroid Build Coastguard Worker unsigned NewVReg = MRI.createVirtualRegister(DstRC);
142*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MI, MI->getDebugLoc(),
143*9880d681SAndroid Build Coastguard Worker TII->get(TargetOpcode::COPY), NewVReg)
144*9880d681SAndroid Build Coastguard Worker .addOperand(SrcMO);
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker // Transform the original copy into a subregister extraction copy.
147*9880d681SAndroid Build Coastguard Worker SrcMO.setReg(NewVReg);
148*9880d681SAndroid Build Coastguard Worker SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
149*9880d681SAndroid Build Coastguard Worker PPC::sub_64);
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker }
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker return Changed;
154*9880d681SAndroid Build Coastguard Worker }
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker public:
runOnMachineFunction__anon78a169ac0111::PPCVSXCopy157*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override {
158*9880d681SAndroid Build Coastguard Worker // If we don't have VSX on the subtarget, don't do anything.
159*9880d681SAndroid Build Coastguard Worker const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
160*9880d681SAndroid Build Coastguard Worker if (!STI.hasVSX())
161*9880d681SAndroid Build Coastguard Worker return false;
162*9880d681SAndroid Build Coastguard Worker TII = STI.getInstrInfo();
163*9880d681SAndroid Build Coastguard Worker
164*9880d681SAndroid Build Coastguard Worker bool Changed = false;
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
167*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &B = *I++;
168*9880d681SAndroid Build Coastguard Worker if (processBlock(B))
169*9880d681SAndroid Build Coastguard Worker Changed = true;
170*9880d681SAndroid Build Coastguard Worker }
171*9880d681SAndroid Build Coastguard Worker
172*9880d681SAndroid Build Coastguard Worker return Changed;
173*9880d681SAndroid Build Coastguard Worker }
174*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anon78a169ac0111::PPCVSXCopy175*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
176*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker };
179*9880d681SAndroid Build Coastguard Worker }
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
182*9880d681SAndroid Build Coastguard Worker "PowerPC VSX Copy Legalization", false, false)
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker char PPCVSXCopy::ID = 0;
185*9880d681SAndroid Build Coastguard Worker FunctionPass*
createPPCVSXCopyPass()186*9880d681SAndroid Build Coastguard Worker llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
187*9880d681SAndroid Build Coastguard Worker
188