1 //===-------------- PPCVSXCopy.cpp - VSX Copy Legalization ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A pass which deals with the complexity of generating legal VSX register
10 // copies to/from register classes which partially overlap with the VSX
11 // register file.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "MCTargetDesc/PPCPredicates.h"
16 #include "PPC.h"
17 #include "PPCHazardRecognizers.h"
18 #include "PPCInstrBuilder.h"
19 #include "PPCInstrInfo.h"
20 #include "PPCMachineFunctionInfo.h"
21 #include "PPCTargetMachine.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineMemOperand.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34 
35 using namespace llvm;
36 
37 #define DEBUG_TYPE "ppc-vsx-copy"
38 
39 namespace llvm {
40   void initializePPCVSXCopyPass(PassRegistry&);
41 }
42 
43 namespace {
44   // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
45   // (Altivec and scalar floating-point registers), we need to transform the
46   // copies into subregister copies with other restrictions.
47   struct PPCVSXCopy : public MachineFunctionPass {
48     static char ID;
49     PPCVSXCopy() : MachineFunctionPass(ID) {
50       initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
51     }
52 
53     const TargetInstrInfo *TII;
54 
55     bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
56                       MachineRegisterInfo &MRI) {
57       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
58         return RC->hasSubClassEq(MRI.getRegClass(Reg));
59       } else if (RC->contains(Reg)) {
60         return true;
61       }
62 
63       return false;
64     }
65 
66     bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
67       return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
68     }
69 
70     bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
71       return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
72     }
73 
74     bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
75       return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
76     }
77 
78     bool IsVSFReg(unsigned Reg, MachineRegisterInfo &MRI) {
79       return IsRegInClass(Reg, &PPC::VSFRCRegClass, MRI);
80     }
81 
82     bool IsVSSReg(unsigned Reg, MachineRegisterInfo &MRI) {
83       return IsRegInClass(Reg, &PPC::VSSRCRegClass, MRI);
84     }
85 
86 protected:
87     bool processBlock(MachineBasicBlock &MBB) {
88       bool Changed = false;
89 
90       MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
91       for (MachineInstr &MI : MBB) {
92         if (!MI.isFullCopy())
93           continue;
94 
95         MachineOperand &DstMO = MI.getOperand(0);
96         MachineOperand &SrcMO = MI.getOperand(1);
97 
98         if ( IsVSReg(DstMO.getReg(), MRI) &&
99             !IsVSReg(SrcMO.getReg(), MRI)) {
100           // This is a copy *to* a VSX register from a non-VSX register.
101           Changed = true;
102 
103           const TargetRegisterClass *SrcRC = &PPC::VSLRCRegClass;
104           assert((IsF8Reg(SrcMO.getReg(), MRI) ||
105                   IsVSSReg(SrcMO.getReg(), MRI) ||
106                   IsVSFReg(SrcMO.getReg(), MRI)) &&
107                  "Unknown source for a VSX copy");
108 
109           unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
110           BuildMI(MBB, MI, MI.getDebugLoc(),
111                   TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
112               .addImm(1) // add 1, not 0, because there is no implicit clearing
113                          // of the high bits.
114               .add(SrcMO)
115               .addImm(PPC::sub_64);
116 
117           // The source of the original copy is now the new virtual register.
118           SrcMO.setReg(NewVReg);
119         } else if (!IsVSReg(DstMO.getReg(), MRI) &&
120                     IsVSReg(SrcMO.getReg(), MRI)) {
121           // This is a copy *from* a VSX register to a non-VSX register.
122           Changed = true;
123 
124           const TargetRegisterClass *DstRC = &PPC::VSLRCRegClass;
125           assert((IsF8Reg(DstMO.getReg(), MRI) ||
126                   IsVSFReg(DstMO.getReg(), MRI) ||
127                   IsVSSReg(DstMO.getReg(), MRI)) &&
128                  "Unknown destination for a VSX copy");
129 
130           // Copy the VSX value into a new VSX register of the correct subclass.
131           unsigned NewVReg = MRI.createVirtualRegister(DstRC);
132           BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(TargetOpcode::COPY),
133                   NewVReg)
134               .add(SrcMO);
135 
136           // Transform the original copy into a subregister extraction copy.
137           SrcMO.setReg(NewVReg);
138           SrcMO.setSubReg(PPC::sub_64);
139         }
140       }
141 
142       return Changed;
143     }
144 
145 public:
146     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 
164     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*
175 llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
176 
177