[PowerPC] Convert r+r instructions to r+i (pre and post RA)
[llvm-core.git] / lib / Target / PowerPC / PPCVSXCopy.cpp
blob93fe3230ab815a81720892767e69b86ed8a1dd0d
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.
14 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/PPCPredicates.h"
17 #include "PPC.h"
18 #include "PPCHazardRecognizers.h"
19 #include "PPCInstrBuilder.h"
20 #include "PPCInstrInfo.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/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
36 using namespace llvm;
38 #define DEBUG_TYPE "ppc-vsx-copy"
40 namespace llvm {
41 void initializePPCVSXCopyPass(PassRegistry&);
44 namespace {
45 // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
46 // (Altivec and scalar floating-point registers), we need to transform the
47 // copies into subregister copies with other restrictions.
48 struct PPCVSXCopy : public MachineFunctionPass {
49 static char ID;
50 PPCVSXCopy() : MachineFunctionPass(ID) {
51 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
54 const TargetInstrInfo *TII;
56 bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
57 MachineRegisterInfo &MRI) {
58 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
59 return RC->hasSubClassEq(MRI.getRegClass(Reg));
60 } else if (RC->contains(Reg)) {
61 return true;
64 return false;
67 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
68 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
71 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
72 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
75 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
76 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
79 bool IsVSFReg(unsigned Reg, MachineRegisterInfo &MRI) {
80 return IsRegInClass(Reg, &PPC::VSFRCRegClass, MRI);
83 bool IsVSSReg(unsigned Reg, MachineRegisterInfo &MRI) {
84 return IsRegInClass(Reg, &PPC::VSSRCRegClass, MRI);
87 protected:
88 bool processBlock(MachineBasicBlock &MBB) {
89 bool Changed = false;
91 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
92 for (MachineInstr &MI : MBB) {
93 if (!MI.isFullCopy())
94 continue;
96 MachineOperand &DstMO = MI.getOperand(0);
97 MachineOperand &SrcMO = MI.getOperand(1);
99 if ( IsVSReg(DstMO.getReg(), MRI) &&
100 !IsVSReg(SrcMO.getReg(), MRI)) {
101 // This is a copy *to* a VSX register from a non-VSX register.
102 Changed = true;
104 const TargetRegisterClass *SrcRC = &PPC::VSLRCRegClass;
105 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
106 IsVSSReg(SrcMO.getReg(), MRI) ||
107 IsVSFReg(SrcMO.getReg(), MRI)) &&
108 "Unknown source for a VSX copy");
110 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
111 BuildMI(MBB, MI, MI.getDebugLoc(),
112 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
113 .addImm(1) // add 1, not 0, because there is no implicit clearing
114 // of the high bits.
115 .add(SrcMO)
116 .addImm(PPC::sub_64);
118 // The source of the original copy is now the new virtual register.
119 SrcMO.setReg(NewVReg);
120 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
121 IsVSReg(SrcMO.getReg(), MRI)) {
122 // This is a copy *from* a VSX register to a non-VSX register.
123 Changed = true;
125 const TargetRegisterClass *DstRC = &PPC::VSLRCRegClass;
126 assert((IsF8Reg(DstMO.getReg(), MRI) ||
127 IsVSFReg(DstMO.getReg(), MRI) ||
128 IsVSSReg(DstMO.getReg(), MRI)) &&
129 "Unknown destination for a VSX copy");
131 // Copy the VSX value into a new VSX register of the correct subclass.
132 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
133 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(TargetOpcode::COPY),
134 NewVReg)
135 .add(SrcMO);
137 // Transform the original copy into a subregister extraction copy.
138 SrcMO.setReg(NewVReg);
139 SrcMO.setSubReg(PPC::sub_64);
143 return Changed;
146 public:
147 bool runOnMachineFunction(MachineFunction &MF) override {
148 // If we don't have VSX on the subtarget, don't do anything.
149 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
150 if (!STI.hasVSX())
151 return false;
152 TII = STI.getInstrInfo();
154 bool Changed = false;
156 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
157 MachineBasicBlock &B = *I++;
158 if (processBlock(B))
159 Changed = true;
162 return Changed;
165 void getAnalysisUsage(AnalysisUsage &AU) const override {
166 MachineFunctionPass::getAnalysisUsage(AU);
171 INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
172 "PowerPC VSX Copy Legalization", false, false)
174 char PPCVSXCopy::ID = 0;
175 FunctionPass*
176 llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }