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