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 
IsVSFReg__anonc2db99360111::PPCVSXCopy80     bool IsVSFReg(unsigned Reg, MachineRegisterInfo &MRI) {
81       return IsRegInClass(Reg, &PPC::VSFRCRegClass, MRI);
82     }
83 
IsVSSReg__anonc2db99360111::PPCVSXCopy84     bool IsVSSReg(unsigned Reg, MachineRegisterInfo &MRI) {
85       return IsRegInClass(Reg, &PPC::VSSRCRegClass, MRI);
86     }
87 
88 protected:
processBlock__anonc2db99360111::PPCVSXCopy89     bool processBlock(MachineBasicBlock &MBB) {
90       bool Changed = false;
91 
92       MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
93       for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
94            I != IE; ++I) {
95         MachineInstr *MI = I;
96         if (!MI->isFullCopy())
97           continue;
98 
99         MachineOperand &DstMO = MI->getOperand(0);
100         MachineOperand &SrcMO = MI->getOperand(1);
101 
102         if ( IsVSReg(DstMO.getReg(), MRI) &&
103             !IsVSReg(SrcMO.getReg(), MRI)) {
104           // This is a copy *to* a VSX register from a non-VSX register.
105           Changed = true;
106 
107           const TargetRegisterClass *SrcRC =
108             IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
109                                            &PPC::VSLRCRegClass;
110           assert((IsF8Reg(SrcMO.getReg(), MRI) ||
111                   IsVRReg(SrcMO.getReg(), MRI) ||
112                   IsVSSReg(SrcMO.getReg(), MRI) ||
113                   IsVSFReg(SrcMO.getReg(), MRI)) &&
114                  "Unknown source for a VSX copy");
115 
116           unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
117           BuildMI(MBB, MI, MI->getDebugLoc(),
118                   TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
119             .addImm(1) // add 1, not 0, because there is no implicit clearing
120                        // of the high bits.
121             .addOperand(SrcMO)
122             .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
123                                                    PPC::sub_64);
124 
125           // The source of the original copy is now the new virtual register.
126           SrcMO.setReg(NewVReg);
127         } else if (!IsVSReg(DstMO.getReg(), MRI) &&
128                     IsVSReg(SrcMO.getReg(), MRI)) {
129           // This is a copy *from* a VSX register to a non-VSX register.
130           Changed = true;
131 
132           const TargetRegisterClass *DstRC =
133             IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
134                                            &PPC::VSLRCRegClass;
135           assert((IsF8Reg(DstMO.getReg(), MRI) ||
136                   IsVSFReg(DstMO.getReg(), MRI) ||
137                   IsVSSReg(DstMO.getReg(), MRI) ||
138                   IsVRReg(DstMO.getReg(), MRI)) &&
139                  "Unknown destination for a VSX copy");
140 
141           // Copy the VSX value into a new VSX register of the correct subclass.
142           unsigned NewVReg = MRI.createVirtualRegister(DstRC);
143           BuildMI(MBB, MI, MI->getDebugLoc(),
144                   TII->get(TargetOpcode::COPY), NewVReg)
145             .addOperand(SrcMO);
146 
147           // Transform the original copy into a subregister extraction copy.
148           SrcMO.setReg(NewVReg);
149           SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
150                                                          PPC::sub_64);
151         }
152       }
153 
154       return Changed;
155     }
156 
157 public:
runOnMachineFunction__anonc2db99360111::PPCVSXCopy158     bool runOnMachineFunction(MachineFunction &MF) override {
159       // If we don't have VSX on the subtarget, don't do anything.
160       const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
161       if (!STI.hasVSX())
162         return false;
163       TII = STI.getInstrInfo();
164 
165       bool Changed = false;
166 
167       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
168         MachineBasicBlock &B = *I++;
169         if (processBlock(B))
170           Changed = true;
171       }
172 
173       return Changed;
174     }
175 
getAnalysisUsage__anonc2db99360111::PPCVSXCopy176     void getAnalysisUsage(AnalysisUsage &AU) const override {
177       MachineFunctionPass::getAnalysisUsage(AU);
178     }
179   };
180 }
181 
182 INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
183                 "PowerPC VSX Copy Legalization", false, false)
184 
185 char PPCVSXCopy::ID = 0;
186 FunctionPass*
createPPCVSXCopyPass()187 llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
188 
189