| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 1 | //===-------------- PPCVSXCopy.cpp - VSX Copy Legalization ----------------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 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 | |
| Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/PPCPredicates.h" |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "PPC.h" |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 17 | #include "PPCHazardRecognizers.h" |
| 18 | #include "PPCInstrBuilder.h" |
| Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 19 | #include "PPCInstrInfo.h" |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 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" |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 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 | |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers |
| 41 | // (Altivec and scalar floating-point registers), we need to transform the |
| 42 | // copies into subregister copies with other restrictions. |
| 43 | struct PPCVSXCopy : public MachineFunctionPass { |
| 44 | static char ID; |
| 45 | PPCVSXCopy() : MachineFunctionPass(ID) { |
| 46 | initializePPCVSXCopyPass(*PassRegistry::getPassRegistry()); |
| 47 | } |
| 48 | |
| 49 | const TargetInstrInfo *TII; |
| 50 | |
| 51 | bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC, |
| 52 | MachineRegisterInfo &MRI) { |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 53 | if (Register::isVirtualRegister(Reg)) { |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 54 | return RC->hasSubClassEq(MRI.getRegClass(Reg)); |
| 55 | } else if (RC->contains(Reg)) { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) { |
| 63 | return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI); |
| 64 | } |
| 65 | |
| 66 | bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) { |
| 67 | return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI); |
| 68 | } |
| 69 | |
| 70 | bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) { |
| 71 | return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI); |
| 72 | } |
| 73 | |
| Nemanja Ivanovic | 1c39ca6 | 2015-08-13 17:40:44 +0000 | [diff] [blame] | 74 | bool IsVSFReg(unsigned Reg, MachineRegisterInfo &MRI) { |
| 75 | return IsRegInClass(Reg, &PPC::VSFRCRegClass, MRI); |
| 76 | } |
| 77 | |
| Nemanja Ivanovic | 8922476 | 2015-12-15 14:50:34 +0000 | [diff] [blame] | 78 | bool IsVSSReg(unsigned Reg, MachineRegisterInfo &MRI) { |
| 79 | return IsRegInClass(Reg, &PPC::VSSRCRegClass, MRI); |
| 80 | } |
| 81 | |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 82 | protected: |
| 83 | bool processBlock(MachineBasicBlock &MBB) { |
| 84 | bool Changed = false; |
| 85 | |
| 86 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 87 | for (MachineInstr &MI : MBB) { |
| 88 | if (!MI.isFullCopy()) |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 89 | continue; |
| 90 | |
| Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 91 | MachineOperand &DstMO = MI.getOperand(0); |
| 92 | MachineOperand &SrcMO = MI.getOperand(1); |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 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 | |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 99 | const TargetRegisterClass *SrcRC = &PPC::VSLRCRegClass; |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 100 | assert((IsF8Reg(SrcMO.getReg(), MRI) || |
| Nemanja Ivanovic | 8922476 | 2015-12-15 14:50:34 +0000 | [diff] [blame] | 101 | IsVSSReg(SrcMO.getReg(), MRI) || |
| Nemanja Ivanovic | 1c39ca6 | 2015-08-13 17:40:44 +0000 | [diff] [blame] | 102 | IsVSFReg(SrcMO.getReg(), MRI)) && |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 103 | "Unknown source for a VSX copy"); |
| 104 | |
| Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 105 | Register NewVReg = MRI.createVirtualRegister(SrcRC); |
| Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 106 | BuildMI(MBB, MI, MI.getDebugLoc(), |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 107 | TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg) |
| Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 108 | .addImm(1) // add 1, not 0, because there is no implicit clearing |
| 109 | // of the high bits. |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 110 | .add(SrcMO) |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 111 | .addImm(PPC::sub_64); |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 112 | |
| 113 | // The source of the original copy is now the new virtual register. |
| 114 | SrcMO.setReg(NewVReg); |
| 115 | } else if (!IsVSReg(DstMO.getReg(), MRI) && |
| 116 | IsVSReg(SrcMO.getReg(), MRI)) { |
| 117 | // This is a copy *from* a VSX register to a non-VSX register. |
| 118 | Changed = true; |
| 119 | |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 120 | const TargetRegisterClass *DstRC = &PPC::VSLRCRegClass; |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 121 | assert((IsF8Reg(DstMO.getReg(), MRI) || |
| Nemanja Ivanovic | d389657 | 2015-10-09 11:12:18 +0000 | [diff] [blame] | 122 | IsVSFReg(DstMO.getReg(), MRI) || |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 123 | IsVSSReg(DstMO.getReg(), MRI)) && |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 124 | "Unknown destination for a VSX copy"); |
| 125 | |
| 126 | // Copy the VSX value into a new VSX register of the correct subclass. |
| Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 127 | Register NewVReg = MRI.createVirtualRegister(DstRC); |
| Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 128 | BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(TargetOpcode::COPY), |
| 129 | NewVReg) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 130 | .add(SrcMO); |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 131 | |
| 132 | // Transform the original copy into a subregister extraction copy. |
| 133 | SrcMO.setReg(NewVReg); |
| Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 134 | SrcMO.setSubReg(PPC::sub_64); |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | return Changed; |
| 139 | } |
| 140 | |
| 141 | public: |
| 142 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 143 | // If we don't have VSX on the subtarget, don't do anything. |
| 144 | const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>(); |
| 145 | if (!STI.hasVSX()) |
| 146 | return false; |
| 147 | TII = STI.getInstrInfo(); |
| 148 | |
| 149 | bool Changed = false; |
| 150 | |
| 151 | for (MachineFunction::iterator I = MF.begin(); I != MF.end();) { |
| 152 | MachineBasicBlock &B = *I++; |
| 153 | if (processBlock(B)) |
| 154 | Changed = true; |
| 155 | } |
| 156 | |
| 157 | return Changed; |
| 158 | } |
| 159 | |
| 160 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 161 | MachineFunctionPass::getAnalysisUsage(AU); |
| 162 | } |
| 163 | }; |
| Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 164 | } |
| Hal Finkel | 76c751d | 2015-02-01 22:01:29 +0000 | [diff] [blame] | 165 | |
| 166 | INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE, |
| 167 | "PowerPC VSX Copy Legalization", false, false) |
| 168 | |
| 169 | char PPCVSXCopy::ID = 0; |
| 170 | FunctionPass* |
| 171 | llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); } |
| 172 | |