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