LLVM 22.0.0git
HexagonVExtract.cpp
Go to the documentation of this file.
1//===- HexagonVExtract.cpp ------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8// This pass will replace multiple occurrences of V6_extractw from the same
9// vector register with a combination of a vector store and scalar loads.
10//===----------------------------------------------------------------------===//
11
12#include "Hexagon.h"
13#include "HexagonInstrInfo.h"
15#include "HexagonRegisterInfo.h"
16#include "HexagonSubtarget.h"
18#include "llvm/Pass.h"
25
26#include <map>
27
28using namespace llvm;
29
31 "hexagon-vextract-threshold", cl::Hidden, cl::init(1),
32 cl::desc("Threshold for triggering vextract replacement"));
33
34namespace {
35 class HexagonVExtract : public MachineFunctionPass {
36 public:
37 static char ID;
38 HexagonVExtract() : MachineFunctionPass(ID) {}
39
40 StringRef getPassName() const override {
41 return "Hexagon optimize vextract";
42 }
43 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 }
46 bool runOnMachineFunction(MachineFunction &MF) override;
47
48 private:
49 const HexagonSubtarget *HST = nullptr;
50 const HexagonInstrInfo *HII = nullptr;
51
52 unsigned genElemLoad(MachineInstr *ExtI, unsigned BaseR,
54 };
55
56 char HexagonVExtract::ID = 0;
57}
58
59INITIALIZE_PASS(HexagonVExtract, "hexagon-vextract",
60 "Hexagon optimize vextract", false, false)
61
62unsigned HexagonVExtract::genElemLoad(MachineInstr *ExtI, unsigned BaseR,
64 MachineBasicBlock &ExtB = *ExtI->getParent();
65 DebugLoc DL = ExtI->getDebugLoc();
66 Register ElemR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
67
68 Register ExtIdxR = ExtI->getOperand(2).getReg();
69 unsigned ExtIdxS = ExtI->getOperand(2).getSubReg();
70
71 // Simplified check for a compile-time constant value of ExtIdxR.
72 if (ExtIdxS == 0) {
73 MachineInstr *DI = MRI.getVRegDef(ExtIdxR);
74 if (DI->getOpcode() == Hexagon::A2_tfrsi) {
75 unsigned V = DI->getOperand(1).getImm();
76 V &= (HST->getVectorLength()-1) & -4u;
77
78 BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::L2_loadri_io), ElemR)
79 .addReg(BaseR)
80 .addImm(V);
81 return ElemR;
82 }
83 }
84
85 Register IdxR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
86 BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::A2_andir), IdxR)
87 .add(ExtI->getOperand(2))
88 .addImm(-4);
89 BuildMI(ExtB, ExtI, DL, HII->get(Hexagon::L4_loadri_rr), ElemR)
90 .addReg(BaseR)
91 .addReg(IdxR)
92 .addImm(0);
93 return ElemR;
94}
95
96bool HexagonVExtract::runOnMachineFunction(MachineFunction &MF) {
97 HST = &MF.getSubtarget<HexagonSubtarget>();
98 HII = HST->getInstrInfo();
99 const auto &HRI = *HST->getRegisterInfo();
101 MachineFrameInfo &MFI = MF.getFrameInfo();
102 Register AR =
103 MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
104 std::map<unsigned, SmallVector<MachineInstr *, 4>> VExtractMap;
105 bool Changed = false;
106
107 for (MachineBasicBlock &MBB : MF) {
108 for (MachineInstr &MI : MBB) {
109 unsigned Opc = MI.getOpcode();
110 if (Opc != Hexagon::V6_extractw)
111 continue;
112 Register VecR = MI.getOperand(1).getReg();
113 VExtractMap[VecR].push_back(&MI);
114 }
115 }
116
117 auto EmitAddr = [&] (MachineBasicBlock &BB, MachineBasicBlock::iterator At,
118 DebugLoc dl, int FI, unsigned Offset) {
119 Register AddrR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
120 unsigned FiOpc = AR != 0 ? Hexagon::PS_fia : Hexagon::PS_fi;
121 auto MIB = BuildMI(BB, At, dl, HII->get(FiOpc), AddrR);
122 if (AR)
123 MIB.addReg(AR);
124 MIB.addFrameIndex(FI).addImm(Offset);
125 return AddrR;
126 };
127
128 MaybeAlign MaxAlign;
129 for (auto &P : VExtractMap) {
130 unsigned VecR = P.first;
131 if (P.second.size() <= VExtractThreshold)
132 continue;
133
134 const auto &VecRC = *MRI.getRegClass(VecR);
135 Align Alignment = HRI.getSpillAlign(VecRC);
136 MaxAlign = std::max(MaxAlign.valueOrOne(), Alignment);
137 // Make sure this is not a spill slot: spill slots cannot be aligned
138 // if there are variable-sized objects on the stack. They must be
139 // accessible via FP (which is not aligned), because SP is unknown,
140 // and AP may not be available at the location of the load/store.
141 int FI = MFI.CreateStackObject(HRI.getSpillSize(VecRC), Alignment,
142 /*isSpillSlot*/ false);
143
144 MachineInstr *DefI = MRI.getVRegDef(VecR);
145 MachineBasicBlock::iterator At = std::next(DefI->getIterator());
146 MachineBasicBlock &DefB = *DefI->getParent();
147 unsigned StoreOpc = VecRC.getID() == Hexagon::HvxVRRegClassID
148 ? Hexagon::V6_vS32b_ai
149 : Hexagon::PS_vstorerw_ai;
150 Register AddrR = EmitAddr(DefB, At, DefI->getDebugLoc(), FI, 0);
151 BuildMI(DefB, At, DefI->getDebugLoc(), HII->get(StoreOpc))
152 .addReg(AddrR)
153 .addImm(0)
154 .addReg(VecR);
155
156 unsigned VecSize = HRI.getRegSizeInBits(VecRC) / 8;
157
158 for (MachineInstr *ExtI : P.second) {
159 assert(ExtI->getOpcode() == Hexagon::V6_extractw);
160 unsigned SR = ExtI->getOperand(1).getSubReg();
161 assert(ExtI->getOperand(1).getReg() == VecR);
162
163 MachineBasicBlock &ExtB = *ExtI->getParent();
164 Register BaseR = EmitAddr(ExtB, ExtI, ExtI->getDebugLoc(), FI,
165 SR == 0 ? 0 : VecSize/2);
166
167 unsigned ElemR = genElemLoad(ExtI, BaseR, MRI);
168 Register ExtR = ExtI->getOperand(0).getReg();
169 MRI.replaceRegWith(ExtR, ElemR);
170 ExtB.erase(ExtI);
171 Changed = true;
172 }
173 }
174
175 if (AR && MaxAlign) {
176 // Update the required stack alignment.
177 MachineInstr *AlignaI = MRI.getVRegDef(AR);
178 assert(AlignaI->getOpcode() == Hexagon::PS_aligna);
179 MachineOperand &Op = AlignaI->getOperand(1);
180 if (*MaxAlign > Op.getImm())
181 Op.setImm(MaxAlign->value());
182 }
183
184 return Changed;
185}
186
188 return new HexagonVExtract();
189}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static cl::opt< unsigned > VExtractThreshold("hexagon-vextract-threshold", cl::Hidden, cl::init(1), cl::desc("Threshold for triggering vextract replacement"))
IRTranslator LLVM IR MI
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
This file defines the SmallVector class.
Represent the analysis usage information of a pass.
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:124
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
Hexagon target-specific information for each MachineFunction.
const HexagonInstrInfo * getInstrInfo() const override
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:72
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:587
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:359
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:511
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:595
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
int64_t getImm() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
self_iterator getIterator()
Definition: ilist_node.h:134
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createHexagonVExtract()
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141