1*0b57cec5SDimitry Andric //===- HexagonVExtract.cpp ------------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // This pass will replace multiple occurrences of V6_extractw from the same 9*0b57cec5SDimitry Andric // vector register with a combination of a vector store and scalar loads. 10*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 11*0b57cec5SDimitry Andric 12*0b57cec5SDimitry Andric #include "Hexagon.h" 13*0b57cec5SDimitry Andric #include "HexagonInstrInfo.h" 14*0b57cec5SDimitry Andric #include "HexagonRegisterInfo.h" 15*0b57cec5SDimitry Andric #include "HexagonSubtarget.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 17*0b57cec5SDimitry Andric #include "llvm/PassSupport.h" 18*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 19*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 20*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 23*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 24*0b57cec5SDimitry Andric 25*0b57cec5SDimitry Andric #include <map> 26*0b57cec5SDimitry Andric 27*0b57cec5SDimitry Andric using namespace llvm; 28*0b57cec5SDimitry Andric 29*0b57cec5SDimitry Andric static cl::opt<unsigned> VExtractThreshold("hexagon-vextract-threshold", 30*0b57cec5SDimitry Andric cl::Hidden, cl::ZeroOrMore, cl::init(1), 31*0b57cec5SDimitry Andric cl::desc("Threshold for triggering vextract replacement")); 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric namespace llvm { 34*0b57cec5SDimitry Andric void initializeHexagonVExtractPass(PassRegistry& Registry); 35*0b57cec5SDimitry Andric FunctionPass *createHexagonVExtract(); 36*0b57cec5SDimitry Andric } 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric namespace { 39*0b57cec5SDimitry Andric class HexagonVExtract : public MachineFunctionPass { 40*0b57cec5SDimitry Andric public: 41*0b57cec5SDimitry Andric static char ID; 42*0b57cec5SDimitry Andric HexagonVExtract() : MachineFunctionPass(ID) {} 43*0b57cec5SDimitry Andric 44*0b57cec5SDimitry Andric StringRef getPassName() const override { 45*0b57cec5SDimitry Andric return "Hexagon optimize vextract"; 46*0b57cec5SDimitry Andric } 47*0b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 48*0b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 49*0b57cec5SDimitry Andric } 50*0b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 51*0b57cec5SDimitry Andric 52*0b57cec5SDimitry Andric private: 53*0b57cec5SDimitry Andric const HexagonSubtarget *HST = nullptr; 54*0b57cec5SDimitry Andric const HexagonInstrInfo *HII = nullptr; 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric unsigned genElemLoad(MachineInstr *ExtI, unsigned BaseR, 57*0b57cec5SDimitry Andric MachineRegisterInfo &MRI); 58*0b57cec5SDimitry Andric }; 59*0b57cec5SDimitry Andric 60*0b57cec5SDimitry Andric char HexagonVExtract::ID = 0; 61*0b57cec5SDimitry Andric } 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric INITIALIZE_PASS(HexagonVExtract, "hexagon-vextract", 64*0b57cec5SDimitry Andric "Hexagon optimize vextract", false, false) 65*0b57cec5SDimitry Andric 66*0b57cec5SDimitry Andric unsigned HexagonVExtract::genElemLoad(MachineInstr *ExtI, unsigned BaseR, 67*0b57cec5SDimitry Andric MachineRegisterInfo &MRI) { 68*0b57cec5SDimitry Andric MachineBasicBlock &ExtB = *ExtI->getParent(); 69*0b57cec5SDimitry Andric DebugLoc DL = ExtI->getDebugLoc(); 70*0b57cec5SDimitry Andric unsigned ElemR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); 71*0b57cec5SDimitry Andric 72*0b57cec5SDimitry Andric unsigned ExtIdxR = ExtI->getOperand(2).getReg(); 73*0b57cec5SDimitry Andric unsigned ExtIdxS = ExtI->getOperand(2).getSubReg(); 74*0b57cec5SDimitry Andric 75*0b57cec5SDimitry Andric // Simplified check for a compile-time constant value of ExtIdxR. 76*0b57cec5SDimitry Andric if (ExtIdxS == 0) { 77*0b57cec5SDimitry Andric MachineInstr *DI = MRI.getVRegDef(ExtIdxR); 78*0b57cec5SDimitry Andric if (DI->getOpcode() == Hexagon::A2_tfrsi) { 79*0b57cec5SDimitry Andric unsigned V = DI->getOperand(1).getImm(); 80*0b57cec5SDimitry Andric V &= (HST->getVectorLength()-1) & -4u; 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::L2_loadri_io), ElemR) 83*0b57cec5SDimitry Andric .addReg(BaseR) 84*0b57cec5SDimitry Andric .addImm(V); 85*0b57cec5SDimitry Andric return ElemR; 86*0b57cec5SDimitry Andric } 87*0b57cec5SDimitry Andric } 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric unsigned IdxR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); 90*0b57cec5SDimitry Andric BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::A2_andir), IdxR) 91*0b57cec5SDimitry Andric .add(ExtI->getOperand(2)) 92*0b57cec5SDimitry Andric .addImm(-4); 93*0b57cec5SDimitry Andric BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::L4_loadri_rr), ElemR) 94*0b57cec5SDimitry Andric .addReg(BaseR) 95*0b57cec5SDimitry Andric .addReg(IdxR) 96*0b57cec5SDimitry Andric .addImm(0); 97*0b57cec5SDimitry Andric return ElemR; 98*0b57cec5SDimitry Andric } 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric bool HexagonVExtract::runOnMachineFunction(MachineFunction &MF) { 101*0b57cec5SDimitry Andric HST = &MF.getSubtarget<HexagonSubtarget>(); 102*0b57cec5SDimitry Andric HII = HST->getInstrInfo(); 103*0b57cec5SDimitry Andric const auto &HRI = *HST->getRegisterInfo(); 104*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 105*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 106*0b57cec5SDimitry Andric std::map<unsigned, SmallVector<MachineInstr*,4>> VExtractMap; 107*0b57cec5SDimitry Andric bool Changed = false; 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 110*0b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 111*0b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 112*0b57cec5SDimitry Andric if (Opc != Hexagon::V6_extractw) 113*0b57cec5SDimitry Andric continue; 114*0b57cec5SDimitry Andric unsigned VecR = MI.getOperand(1).getReg(); 115*0b57cec5SDimitry Andric VExtractMap[VecR].push_back(&MI); 116*0b57cec5SDimitry Andric } 117*0b57cec5SDimitry Andric } 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric for (auto &P : VExtractMap) { 120*0b57cec5SDimitry Andric unsigned VecR = P.first; 121*0b57cec5SDimitry Andric if (P.second.size() <= VExtractThreshold) 122*0b57cec5SDimitry Andric continue; 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric const auto &VecRC = *MRI.getRegClass(VecR); 125*0b57cec5SDimitry Andric int FI = MFI.CreateSpillStackObject(HRI.getSpillSize(VecRC), 126*0b57cec5SDimitry Andric HRI.getSpillAlignment(VecRC)); 127*0b57cec5SDimitry Andric MachineInstr *DefI = MRI.getVRegDef(VecR); 128*0b57cec5SDimitry Andric MachineBasicBlock::iterator At = std::next(DefI->getIterator()); 129*0b57cec5SDimitry Andric MachineBasicBlock &DefB = *DefI->getParent(); 130*0b57cec5SDimitry Andric unsigned StoreOpc = VecRC.getID() == Hexagon::HvxVRRegClassID 131*0b57cec5SDimitry Andric ? Hexagon::V6_vS32b_ai 132*0b57cec5SDimitry Andric : Hexagon::PS_vstorerw_ai; 133*0b57cec5SDimitry Andric BuildMI(DefB, At, DefI->getDebugLoc(), HII->get(StoreOpc)) 134*0b57cec5SDimitry Andric .addFrameIndex(FI) 135*0b57cec5SDimitry Andric .addImm(0) 136*0b57cec5SDimitry Andric .addReg(VecR); 137*0b57cec5SDimitry Andric 138*0b57cec5SDimitry Andric unsigned VecSize = HRI.getRegSizeInBits(VecRC) / 8; 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric for (MachineInstr *ExtI : P.second) { 141*0b57cec5SDimitry Andric assert(ExtI->getOpcode() == Hexagon::V6_extractw); 142*0b57cec5SDimitry Andric unsigned SR = ExtI->getOperand(1).getSubReg(); 143*0b57cec5SDimitry Andric assert(ExtI->getOperand(1).getReg() == VecR); 144*0b57cec5SDimitry Andric 145*0b57cec5SDimitry Andric MachineBasicBlock &ExtB = *ExtI->getParent(); 146*0b57cec5SDimitry Andric DebugLoc DL = ExtI->getDebugLoc(); 147*0b57cec5SDimitry Andric unsigned BaseR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass); 148*0b57cec5SDimitry Andric BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::PS_fi), BaseR) 149*0b57cec5SDimitry Andric .addFrameIndex(FI) 150*0b57cec5SDimitry Andric .addImm(SR == 0 ? 0 : VecSize/2); 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric unsigned ElemR = genElemLoad(ExtI, BaseR, MRI); 153*0b57cec5SDimitry Andric unsigned ExtR = ExtI->getOperand(0).getReg(); 154*0b57cec5SDimitry Andric MRI.replaceRegWith(ExtR, ElemR); 155*0b57cec5SDimitry Andric ExtB.erase(ExtI); 156*0b57cec5SDimitry Andric Changed = true; 157*0b57cec5SDimitry Andric } 158*0b57cec5SDimitry Andric } 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric return Changed; 161*0b57cec5SDimitry Andric } 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric FunctionPass *llvm::createHexagonVExtract() { 164*0b57cec5SDimitry Andric return new HexagonVExtract(); 165*0b57cec5SDimitry Andric } 166