blob: e4989e5687fe245cf9d2c53b22b9850a0079d332 [file] [log] [blame]
Hal Finkel76c751d2015-02-01 22:01:29 +00001//===-------------- 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
Hal Finkel76c751d2015-02-01 22:01:29 +000016#include "PPC.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000017#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel76c751d2015-02-01 22:01:29 +000018#include "PPCHazardRecognizers.h"
19#include "PPCInstrBuilder.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000020#include "PPCInstrInfo.h"
Hal Finkel76c751d2015-02-01 22:01:29 +000021#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"
Hal Finkel76c751d2015-02-01 22:01:29 +000031#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
36using namespace llvm;
37
38#define DEBUG_TYPE "ppc-vsx-copy"
39
40namespace llvm {
41 void initializePPCVSXCopyPass(PassRegistry&);
42}
43
44namespace {
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
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +000079 bool IsVSFReg(unsigned Reg, MachineRegisterInfo &MRI) {
80 return IsRegInClass(Reg, &PPC::VSFRCRegClass, MRI);
81 }
82
Nemanja Ivanovic89224762015-12-15 14:50:34 +000083 bool IsVSSReg(unsigned Reg, MachineRegisterInfo &MRI) {
84 return IsRegInClass(Reg, &PPC::VSSRCRegClass, MRI);
85 }
86
Hal Finkel76c751d2015-02-01 22:01:29 +000087protected:
88 bool processBlock(MachineBasicBlock &MBB) {
89 bool Changed = false;
90
91 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +000092 for (MachineInstr &MI : MBB) {
93 if (!MI.isFullCopy())
Hal Finkel76c751d2015-02-01 22:01:29 +000094 continue;
95
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +000096 MachineOperand &DstMO = MI.getOperand(0);
97 MachineOperand &SrcMO = MI.getOperand(1);
Hal Finkel76c751d2015-02-01 22:01:29 +000098
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;
103
104 const TargetRegisterClass *SrcRC =
105 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
106 &PPC::VSLRCRegClass;
107 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000108 IsVRReg(SrcMO.getReg(), MRI) ||
Nemanja Ivanovic89224762015-12-15 14:50:34 +0000109 IsVSSReg(SrcMO.getReg(), MRI) ||
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000110 IsVSFReg(SrcMO.getReg(), MRI)) &&
Hal Finkel76c751d2015-02-01 22:01:29 +0000111 "Unknown source for a VSX copy");
112
113 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000114 BuildMI(MBB, MI, MI.getDebugLoc(),
Hal Finkel76c751d2015-02-01 22:01:29 +0000115 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000116 .addImm(1) // add 1, not 0, because there is no implicit clearing
117 // of the high bits.
118 .addOperand(SrcMO)
119 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128
120 : PPC::sub_64);
Hal Finkel76c751d2015-02-01 22:01:29 +0000121
122 // The source of the original copy is now the new virtual register.
123 SrcMO.setReg(NewVReg);
124 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
125 IsVSReg(SrcMO.getReg(), MRI)) {
126 // This is a copy *from* a VSX register to a non-VSX register.
127 Changed = true;
128
129 const TargetRegisterClass *DstRC =
130 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
131 &PPC::VSLRCRegClass;
132 assert((IsF8Reg(DstMO.getReg(), MRI) ||
Nemanja Ivanovicd3896572015-10-09 11:12:18 +0000133 IsVSFReg(DstMO.getReg(), MRI) ||
Nemanja Ivanovic89224762015-12-15 14:50:34 +0000134 IsVSSReg(DstMO.getReg(), MRI) ||
Hal Finkel76c751d2015-02-01 22:01:29 +0000135 IsVRReg(DstMO.getReg(), MRI)) &&
136 "Unknown destination for a VSX copy");
137
138 // Copy the VSX value into a new VSX register of the correct subclass.
139 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000140 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(TargetOpcode::COPY),
141 NewVReg)
142 .addOperand(SrcMO);
Hal Finkel76c751d2015-02-01 22:01:29 +0000143
144 // Transform the original copy into a subregister extraction copy.
145 SrcMO.setReg(NewVReg);
146 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
147 PPC::sub_64);
148 }
149 }
150
151 return Changed;
152 }
153
154public:
155 bool runOnMachineFunction(MachineFunction &MF) override {
156 // If we don't have VSX on the subtarget, don't do anything.
157 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
158 if (!STI.hasVSX())
159 return false;
160 TII = STI.getInstrInfo();
161
162 bool Changed = false;
163
164 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
165 MachineBasicBlock &B = *I++;
166 if (processBlock(B))
167 Changed = true;
168 }
169
170 return Changed;
171 }
172
173 void getAnalysisUsage(AnalysisUsage &AU) const override {
174 MachineFunctionPass::getAnalysisUsage(AU);
175 }
176 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000177}
Hal Finkel76c751d2015-02-01 22:01:29 +0000178
179INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
180 "PowerPC VSX Copy Legalization", false, false)
181
182char PPCVSXCopy::ID = 0;
183FunctionPass*
184llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
185