xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonVExtract.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- HexagonVExtract.cpp ------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric // This pass will replace multiple occurrences of V6_extractw from the same
90b57cec5SDimitry Andric // vector register with a combination of a vector store and scalar loads.
100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "Hexagon.h"
130b57cec5SDimitry Andric #include "HexagonInstrInfo.h"
14480093f4SDimitry Andric #include "HexagonMachineFunctionInfo.h"
150b57cec5SDimitry Andric #include "HexagonRegisterInfo.h"
160b57cec5SDimitry Andric #include "HexagonSubtarget.h"
170b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
185ffd83dbSDimitry Andric #include "llvm/Pass.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
240b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric #include <map>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric 
3081ad6265SDimitry Andric static cl::opt<unsigned> VExtractThreshold(
3181ad6265SDimitry Andric     "hexagon-vextract-threshold", cl::Hidden, cl::init(1),
320b57cec5SDimitry Andric     cl::desc("Threshold for triggering vextract replacement"));
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric namespace llvm {
350b57cec5SDimitry Andric   void initializeHexagonVExtractPass(PassRegistry& Registry);
360b57cec5SDimitry Andric   FunctionPass *createHexagonVExtract();
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace {
400b57cec5SDimitry Andric   class HexagonVExtract : public MachineFunctionPass {
410b57cec5SDimitry Andric   public:
420b57cec5SDimitry Andric     static char ID;
HexagonVExtract()430b57cec5SDimitry Andric     HexagonVExtract() : MachineFunctionPass(ID) {}
440b57cec5SDimitry Andric 
getPassName() const450b57cec5SDimitry Andric     StringRef getPassName() const override {
460b57cec5SDimitry Andric       return "Hexagon optimize vextract";
470b57cec5SDimitry Andric     }
getAnalysisUsage(AnalysisUsage & AU) const480b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
490b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
500b57cec5SDimitry Andric     }
510b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   private:
540b57cec5SDimitry Andric     const HexagonSubtarget *HST = nullptr;
550b57cec5SDimitry Andric     const HexagonInstrInfo *HII = nullptr;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric     unsigned genElemLoad(MachineInstr *ExtI, unsigned BaseR,
580b57cec5SDimitry Andric                          MachineRegisterInfo &MRI);
590b57cec5SDimitry Andric   };
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   char HexagonVExtract::ID = 0;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric INITIALIZE_PASS(HexagonVExtract, "hexagon-vextract",
650b57cec5SDimitry Andric   "Hexagon optimize vextract", false, false)
660b57cec5SDimitry Andric 
genElemLoad(MachineInstr * ExtI,unsigned BaseR,MachineRegisterInfo & MRI)670b57cec5SDimitry Andric unsigned HexagonVExtract::genElemLoad(MachineInstr *ExtI, unsigned BaseR,
680b57cec5SDimitry Andric                                       MachineRegisterInfo &MRI) {
690b57cec5SDimitry Andric   MachineBasicBlock &ExtB = *ExtI->getParent();
700b57cec5SDimitry Andric   DebugLoc DL = ExtI->getDebugLoc();
718bcb0991SDimitry Andric   Register ElemR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
720b57cec5SDimitry Andric 
738bcb0991SDimitry Andric   Register ExtIdxR = ExtI->getOperand(2).getReg();
740b57cec5SDimitry Andric   unsigned ExtIdxS = ExtI->getOperand(2).getSubReg();
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   // Simplified check for a compile-time constant value of ExtIdxR.
770b57cec5SDimitry Andric   if (ExtIdxS == 0) {
780b57cec5SDimitry Andric     MachineInstr *DI = MRI.getVRegDef(ExtIdxR);
790b57cec5SDimitry Andric     if (DI->getOpcode() == Hexagon::A2_tfrsi) {
800b57cec5SDimitry Andric       unsigned V = DI->getOperand(1).getImm();
810b57cec5SDimitry Andric       V &= (HST->getVectorLength()-1) & -4u;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric       BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::L2_loadri_io), ElemR)
840b57cec5SDimitry Andric         .addReg(BaseR)
850b57cec5SDimitry Andric         .addImm(V);
860b57cec5SDimitry Andric       return ElemR;
870b57cec5SDimitry Andric     }
880b57cec5SDimitry Andric   }
890b57cec5SDimitry Andric 
908bcb0991SDimitry Andric   Register IdxR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
910b57cec5SDimitry Andric   BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::A2_andir), IdxR)
920b57cec5SDimitry Andric     .add(ExtI->getOperand(2))
930b57cec5SDimitry Andric     .addImm(-4);
940b57cec5SDimitry Andric   BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::L4_loadri_rr), ElemR)
950b57cec5SDimitry Andric     .addReg(BaseR)
960b57cec5SDimitry Andric     .addReg(IdxR)
970b57cec5SDimitry Andric     .addImm(0);
980b57cec5SDimitry Andric   return ElemR;
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)1010b57cec5SDimitry Andric bool HexagonVExtract::runOnMachineFunction(MachineFunction &MF) {
1020b57cec5SDimitry Andric   HST = &MF.getSubtarget<HexagonSubtarget>();
1030b57cec5SDimitry Andric   HII = HST->getInstrInfo();
1040b57cec5SDimitry Andric   const auto &HRI = *HST->getRegisterInfo();
1050b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
1060b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
107480093f4SDimitry Andric   Register AR =
108*bdd1243dSDimitry Andric       MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
1090b57cec5SDimitry Andric   std::map<unsigned, SmallVector<MachineInstr *, 4>> VExtractMap;
1100b57cec5SDimitry Andric   bool Changed = false;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
1130b57cec5SDimitry Andric     for (MachineInstr &MI : MBB) {
1140b57cec5SDimitry Andric       unsigned Opc = MI.getOpcode();
1150b57cec5SDimitry Andric       if (Opc != Hexagon::V6_extractw)
1160b57cec5SDimitry Andric         continue;
1178bcb0991SDimitry Andric       Register VecR = MI.getOperand(1).getReg();
1180b57cec5SDimitry Andric       VExtractMap[VecR].push_back(&MI);
1190b57cec5SDimitry Andric     }
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric 
122480093f4SDimitry Andric   auto EmitAddr = [&] (MachineBasicBlock &BB, MachineBasicBlock::iterator At,
123480093f4SDimitry Andric                        DebugLoc dl, int FI, unsigned Offset) {
124480093f4SDimitry Andric     Register AddrR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
125480093f4SDimitry Andric     unsigned FiOpc = AR != 0 ? Hexagon::PS_fia : Hexagon::PS_fi;
126480093f4SDimitry Andric     auto MIB = BuildMI(BB, At, dl, HII->get(FiOpc), AddrR);
127480093f4SDimitry Andric     if (AR)
128480093f4SDimitry Andric       MIB.addReg(AR);
129480093f4SDimitry Andric     MIB.addFrameIndex(FI).addImm(Offset);
130480093f4SDimitry Andric     return AddrR;
131480093f4SDimitry Andric   };
132480093f4SDimitry Andric 
13381ad6265SDimitry Andric   MaybeAlign MaxAlign;
1340b57cec5SDimitry Andric   for (auto &P : VExtractMap) {
1350b57cec5SDimitry Andric     unsigned VecR = P.first;
1360b57cec5SDimitry Andric     if (P.second.size() <= VExtractThreshold)
1370b57cec5SDimitry Andric       continue;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric     const auto &VecRC = *MRI.getRegClass(VecR);
1405ffd83dbSDimitry Andric     Align Alignment = HRI.getSpillAlign(VecRC);
14181ad6265SDimitry Andric     MaxAlign = std::max(MaxAlign.valueOrOne(), Alignment);
142480093f4SDimitry Andric     // Make sure this is not a spill slot: spill slots cannot be aligned
143480093f4SDimitry Andric     // if there are variable-sized objects on the stack. They must be
144480093f4SDimitry Andric     // accessible via FP (which is not aligned), because SP is unknown,
145480093f4SDimitry Andric     // and AP may not be available at the location of the load/store.
1465ffd83dbSDimitry Andric     int FI = MFI.CreateStackObject(HRI.getSpillSize(VecRC), Alignment,
147480093f4SDimitry Andric                                    /*isSpillSlot*/ false);
148480093f4SDimitry Andric 
1490b57cec5SDimitry Andric     MachineInstr *DefI = MRI.getVRegDef(VecR);
1500b57cec5SDimitry Andric     MachineBasicBlock::iterator At = std::next(DefI->getIterator());
1510b57cec5SDimitry Andric     MachineBasicBlock &DefB = *DefI->getParent();
1520b57cec5SDimitry Andric     unsigned StoreOpc = VecRC.getID() == Hexagon::HvxVRRegClassID
1530b57cec5SDimitry Andric                           ? Hexagon::V6_vS32b_ai
1540b57cec5SDimitry Andric                           : Hexagon::PS_vstorerw_ai;
155480093f4SDimitry Andric     Register AddrR = EmitAddr(DefB, At, DefI->getDebugLoc(), FI, 0);
1560b57cec5SDimitry Andric     BuildMI(DefB, At, DefI->getDebugLoc(), HII->get(StoreOpc))
157480093f4SDimitry Andric       .addReg(AddrR)
1580b57cec5SDimitry Andric       .addImm(0)
1590b57cec5SDimitry Andric       .addReg(VecR);
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric     unsigned VecSize = HRI.getRegSizeInBits(VecRC) / 8;
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric     for (MachineInstr *ExtI : P.second) {
1640b57cec5SDimitry Andric       assert(ExtI->getOpcode() == Hexagon::V6_extractw);
1650b57cec5SDimitry Andric       unsigned SR = ExtI->getOperand(1).getSubReg();
1660b57cec5SDimitry Andric       assert(ExtI->getOperand(1).getReg() == VecR);
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric       MachineBasicBlock &ExtB = *ExtI->getParent();
1690b57cec5SDimitry Andric       DebugLoc DL = ExtI->getDebugLoc();
170480093f4SDimitry Andric       Register BaseR = EmitAddr(ExtB, ExtI, ExtI->getDebugLoc(), FI,
171480093f4SDimitry Andric                                 SR == 0 ? 0 : VecSize/2);
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric       unsigned ElemR = genElemLoad(ExtI, BaseR, MRI);
1748bcb0991SDimitry Andric       Register ExtR = ExtI->getOperand(0).getReg();
1750b57cec5SDimitry Andric       MRI.replaceRegWith(ExtR, ElemR);
1760b57cec5SDimitry Andric       ExtB.erase(ExtI);
1770b57cec5SDimitry Andric       Changed = true;
1780b57cec5SDimitry Andric     }
1790b57cec5SDimitry Andric   }
1800b57cec5SDimitry Andric 
1815ffd83dbSDimitry Andric   if (AR && MaxAlign) {
182480093f4SDimitry Andric     // Update the required stack alignment.
183480093f4SDimitry Andric     MachineInstr *AlignaI = MRI.getVRegDef(AR);
184480093f4SDimitry Andric     assert(AlignaI->getOpcode() == Hexagon::PS_aligna);
185480093f4SDimitry Andric     MachineOperand &Op = AlignaI->getOperand(1);
1865ffd83dbSDimitry Andric     if (*MaxAlign > Op.getImm())
1875ffd83dbSDimitry Andric       Op.setImm(MaxAlign->value());
188480093f4SDimitry Andric   }
189480093f4SDimitry Andric 
1900b57cec5SDimitry Andric   return Changed;
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric 
createHexagonVExtract()1930b57cec5SDimitry Andric FunctionPass *llvm::createHexagonVExtract() {
1940b57cec5SDimitry Andric   return new HexagonVExtract();
1950b57cec5SDimitry Andric }
196