blob: 76b681de7f4bb63ac827852de421c006a0ce57bb [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
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
37using namespace llvm;
38
39#define DEBUG_TYPE "ppc-vsx-copy"
40
41namespace llvm {
42 void initializePPCVSXCopyPass(PassRegistry&);
43}
44
45namespace {
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;
51 PPCVSXCopy() : MachineFunctionPass(ID) {
52 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
53 }
54
55 const TargetInstrInfo *TII;
56
57 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
68 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
69 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
70 }
71
72 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
73 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
74 }
75
76 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
77 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
78 }
79
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +000080 bool IsVSFReg(unsigned Reg, MachineRegisterInfo &MRI) {
81 return IsRegInClass(Reg, &PPC::VSFRCRegClass, MRI);
82 }
83
Hal Finkel76c751d2015-02-01 22:01:29 +000084protected:
85 bool processBlock(MachineBasicBlock &MBB) {
86 bool Changed = false;
87
88 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
89 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
90 I != IE; ++I) {
91 MachineInstr *MI = I;
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 =
104 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
105 &PPC::VSLRCRegClass;
106 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
Nemanja Ivanovic1c39ca62015-08-13 17:40:44 +0000107 IsVRReg(SrcMO.getReg(), MRI) ||
108 IsVSFReg(SrcMO.getReg(), MRI)) &&
Hal Finkel76c751d2015-02-01 22:01:29 +0000109 "Unknown source for a VSX copy");
110
111 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
112 BuildMI(MBB, MI, MI->getDebugLoc(),
113 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
114 .addImm(1) // add 1, not 0, because there is no implicit clearing
115 // of the high bits.
116 .addOperand(SrcMO)
117 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
118 PPC::sub_64);
119
120 // The source of the original copy is now the new virtual register.
121 SrcMO.setReg(NewVReg);
122 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
123 IsVSReg(SrcMO.getReg(), MRI)) {
124 // This is a copy *from* a VSX register to a non-VSX register.
125 Changed = true;
126
127 const TargetRegisterClass *DstRC =
128 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
129 &PPC::VSLRCRegClass;
130 assert((IsF8Reg(DstMO.getReg(), MRI) ||
131 IsVRReg(DstMO.getReg(), MRI)) &&
132 "Unknown destination for a VSX copy");
133
134 // Copy the VSX value into a new VSX register of the correct subclass.
135 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
136 BuildMI(MBB, MI, MI->getDebugLoc(),
137 TII->get(TargetOpcode::COPY), NewVReg)
138 .addOperand(SrcMO);
139
140 // Transform the original copy into a subregister extraction copy.
141 SrcMO.setReg(NewVReg);
142 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
143 PPC::sub_64);
144 }
145 }
146
147 return Changed;
148 }
149
150public:
151 bool runOnMachineFunction(MachineFunction &MF) override {
152 // If we don't have VSX on the subtarget, don't do anything.
153 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
154 if (!STI.hasVSX())
155 return false;
156 TII = STI.getInstrInfo();
157
158 bool Changed = false;
159
160 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
161 MachineBasicBlock &B = *I++;
162 if (processBlock(B))
163 Changed = true;
164 }
165
166 return Changed;
167 }
168
169 void getAnalysisUsage(AnalysisUsage &AU) const override {
170 MachineFunctionPass::getAnalysisUsage(AU);
171 }
172 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000173}
Hal Finkel76c751d2015-02-01 22:01:29 +0000174
175INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
176 "PowerPC VSX Copy Legalization", false, false)
177
178char PPCVSXCopy::ID = 0;
179FunctionPass*
180llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
181