LLVM  3.7.0
PPCVSXCopy.cpp
Go to the documentation of this file.
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"
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"
30 #include "llvm/MC/MCAsmInfo.h"
32 #include "llvm/Support/Debug.h"
36 
37 using namespace llvm;
38 
39 #define DEBUG_TYPE "ppc-vsx-copy"
40 
41 namespace llvm {
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;
51  PPCVSXCopy() : MachineFunctionPass(ID) {
53  }
54 
55  const TargetInstrInfo *TII;
56 
57  bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
58  MachineRegisterInfo &MRI) {
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 
80 protected:
81  bool processBlock(MachineBasicBlock &MBB) {
82  bool Changed = false;
83 
85  for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
86  I != IE; ++I) {
87  MachineInstr *MI = I;
88  if (!MI->isFullCopy())
89  continue;
90 
91  MachineOperand &DstMO = MI->getOperand(0);
92  MachineOperand &SrcMO = MI->getOperand(1);
93 
94  if ( IsVSReg(DstMO.getReg(), MRI) &&
95  !IsVSReg(SrcMO.getReg(), MRI)) {
96  // This is a copy *to* a VSX register from a non-VSX register.
97  Changed = true;
98 
99  const TargetRegisterClass *SrcRC =
100  IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
101  &PPC::VSLRCRegClass;
102  assert((IsF8Reg(SrcMO.getReg(), MRI) ||
103  IsVRReg(SrcMO.getReg(), MRI)) &&
104  "Unknown source for a VSX copy");
105 
106  unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
107  BuildMI(MBB, MI, MI->getDebugLoc(),
108  TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
109  .addImm(1) // add 1, not 0, because there is no implicit clearing
110  // of the high bits.
111  .addOperand(SrcMO)
112  .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
113  PPC::sub_64);
114 
115  // The source of the original copy is now the new virtual register.
116  SrcMO.setReg(NewVReg);
117  } else if (!IsVSReg(DstMO.getReg(), MRI) &&
118  IsVSReg(SrcMO.getReg(), MRI)) {
119  // This is a copy *from* a VSX register to a non-VSX register.
120  Changed = true;
121 
122  const TargetRegisterClass *DstRC =
123  IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
124  &PPC::VSLRCRegClass;
125  assert((IsF8Reg(DstMO.getReg(), MRI) ||
126  IsVRReg(DstMO.getReg(), MRI)) &&
127  "Unknown destination for a VSX copy");
128 
129  // Copy the VSX value into a new VSX register of the correct subclass.
130  unsigned NewVReg = MRI.createVirtualRegister(DstRC);
131  BuildMI(MBB, MI, MI->getDebugLoc(),
132  TII->get(TargetOpcode::COPY), NewVReg)
133  .addOperand(SrcMO);
134 
135  // Transform the original copy into a subregister extraction copy.
136  SrcMO.setReg(NewVReg);
137  SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
138  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 {
166  }
167  };
168 }
169 
170 INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
171  "PowerPC VSX Copy Legalization", false, false)
172 
173 char PPCVSXCopy::ID = 0;
175 llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
176 
void initializePPCVSXCopyPass(PassRegistry &)
bool isFullCopy() const
Definition: MachineInstr.h:781
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool hasSubClassEq(const TargetRegisterClass *RC) const
hasSubClassEq - Returns true if RC is a sub-class of or equal to this class.
static bool isVirtualRegister(unsigned Reg)
isVirtualRegister - Return true if the specified register number is in the virtual register namespace...
COPY - Target-independent register copy.
Definition: TargetOpcodes.h:86
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
const TargetRegisterClass * getRegClass(unsigned Reg) const
getRegClass - Return the register class of the specified virtual register.
Reg
All possible values of the reg field in the ModR/M byte.
const MachineInstrBuilder & addImm(int64_t Val) const
addImm - Add a new immediate operand.
TargetInstrInfo - Interface to description of machine instruction set.
bundle_iterator< MachineInstr, instr_iterator > iterator
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
#define DEBUG_TYPE
Definition: PPCVSXCopy.cpp:39
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
BuildMI - Builder interface.
FunctionPass * createPPCVSXCopyPass()
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
const PPCInstrInfo * getInstrInfo() const override
Definition: PPCSubtarget.h:163
MachineOperand class - Representation of each machine instruction operand.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:238
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
#define I(x, y, z)
Definition: MD5.cpp:54
void setSubReg(unsigned subReg)
bool hasVSX() const
Definition: PPCSubtarget.h:224
unsigned getReg() const
getReg - Returns the register number.
const MachineInstrBuilder & addOperand(const MachineOperand &MO) const
BasicBlockListType::iterator iterator
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:41
SUBREG_TO_REG - This instruction is similar to INSERT_SUBREG except that the first operand is an imme...
Definition: TargetOpcodes.h:58
bool contains(unsigned Reg) const
contains - Return true if the specified register is included in this register class.