LLVM 23.0.0git
HexagonRegisterInfo.cpp
Go to the documentation of this file.
1//===-- HexagonRegisterInfo.cpp - Hexagon Register Information ------------===//
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//
9// This file contains the Hexagon implementation of the TargetRegisterInfo
10// class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonRegisterInfo.h"
16#include "HexagonSubtarget.h"
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallSet.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/Type.h"
32#include "llvm/Support/Debug.h"
36
37#define GET_REGINFO_TARGET_DESC
38#include "HexagonGenRegisterInfo.inc"
39
40using namespace llvm;
41
43 "hexagon-frame-index-search-range", cl::init(32), cl::Hidden,
44 cl::desc("Limit on instruction search range in frame index elimination"));
45
47 "hexagon-frame-index-reuse-limit", cl::init(~0), cl::Hidden,
48 cl::desc("Limit on the number of reused registers in frame index "
49 "elimination"));
50
52 : HexagonGenRegisterInfo(Hexagon::R31, 0/*DwarfFlavor*/, 0/*EHFlavor*/,
53 0/*PC*/, HwMode) {}
54
56 switch (Reg) {
57 case Hexagon::R29:
58 case Hexagon::R30:
59 case Hexagon::R31:
60 return true;
61 }
62 return false;
63}
64
66 // VF0-VF31 are fake registers used as sub-registers in HVX vector pairs
67 if (Reg >= Hexagon::VF0 && Reg <= Hexagon::VF31)
68 return true;
69 // VFR0-VFR31 are fake registers used for reversed vector pairs
70 if (Reg >= Hexagon::VFR0 && Reg <= Hexagon::VFR31)
71 return true;
72 return false;
73}
74
76 return R == Hexagon::R0 || R == Hexagon::R1 || R == Hexagon::R2 ||
77 R == Hexagon::R3 || R == Hexagon::D0 || R == Hexagon::D1;
78}
79
80const MCPhysReg *
82 const TargetRegisterClass *RC) const {
83 using namespace Hexagon;
84
85 static const MCPhysReg Int32[] = {
86 R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, 0
87 };
88 static const MCPhysReg Int64[] = {
89 D0, D1, D2, D3, D4, D5, D6, D7, 0
90 };
91 static const MCPhysReg Pred[] = {
92 P0, P1, P2, P3, 0
93 };
94 static const MCPhysReg VecSgl[] = {
95 V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13,
96 V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27,
97 V28, V29, V30, V31, 0
98 };
99 static const MCPhysReg VecDbl[] = {
100 W0, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10, W11, W12, W13, W14, W15, 0
101 };
102 static const MCPhysReg VecPred[] = {
103 Q0, Q1, Q2, Q3, 0
104 };
105
106 switch (RC->getID()) {
107 case IntRegsRegClassID:
108 return Int32;
109 case DoubleRegsRegClassID:
110 return Int64;
111 case PredRegsRegClassID:
112 return Pred;
113 case HvxVRRegClassID:
114 return VecSgl;
115 case HvxWRRegClassID:
116 return VecDbl;
117 case HvxQRRegClassID:
118 return VecPred;
119 default:
120 break;
121 }
122
123 static const MCPhysReg Empty[] = { 0 };
124#ifndef NDEBUG
125 dbgs() << "Register class: " << getRegClassName(RC) << "\n";
126#endif
127 llvm_unreachable("Unexpected register class");
128 return Empty;
129}
130
131
132const MCPhysReg *
134 static const MCPhysReg CalleeSavedRegsV3[] = {
135 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
136 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
137 Hexagon::R24, Hexagon::R25, Hexagon::R26, Hexagon::R27, 0
138 };
139
140 // Functions that contain a call to __builtin_eh_return also save the first 4
141 // parameter registers.
142 static const MCPhysReg CalleeSavedRegsV3EHReturn[] = {
143 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,
144 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
145 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
146 Hexagon::R24, Hexagon::R25, Hexagon::R26, Hexagon::R27, 0
147 };
148
149 bool HasEHReturn = MF->getInfo<HexagonMachineFunctionInfo>()->hasEHReturn();
150
151 return HasEHReturn ? CalleeSavedRegsV3EHReturn : CalleeSavedRegsV3;
152}
153
154
156 const MachineFunction &MF, CallingConv::ID) const {
157 return HexagonCSR_RegMask;
158}
159
160
162 const {
163 BitVector Reserved(getNumRegs());
164 Reserved.set(Hexagon::R29);
165 Reserved.set(Hexagon::R30);
166 Reserved.set(Hexagon::R31);
167 Reserved.set(Hexagon::VTMP);
168
169 // Guest registers.
170 Reserved.set(Hexagon::GELR); // G0
171 Reserved.set(Hexagon::GSR); // G1
172 Reserved.set(Hexagon::GOSP); // G2
173 Reserved.set(Hexagon::G3); // G3
174
175 // Control registers.
176 Reserved.set(Hexagon::SA0); // C0
177 Reserved.set(Hexagon::LC0); // C1
178 Reserved.set(Hexagon::SA1); // C2
179 Reserved.set(Hexagon::LC1); // C3
180 Reserved.set(Hexagon::P3_0); // C4
181 Reserved.set(Hexagon::USR); // C8
182 Reserved.set(Hexagon::PC); // C9
183 Reserved.set(Hexagon::UGP); // C10
184 Reserved.set(Hexagon::GP); // C11
185 Reserved.set(Hexagon::CS0); // C12
186 Reserved.set(Hexagon::CS1); // C13
187 Reserved.set(Hexagon::UPCYCLELO); // C14
188 Reserved.set(Hexagon::UPCYCLEHI); // C15
189 Reserved.set(Hexagon::FRAMELIMIT); // C16
190 Reserved.set(Hexagon::FRAMEKEY); // C17
191 Reserved.set(Hexagon::PKTCOUNTLO); // C18
192 Reserved.set(Hexagon::PKTCOUNTHI); // C19
193 Reserved.set(Hexagon::UTIMERLO); // C30
194 Reserved.set(Hexagon::UTIMERHI); // C31
195 // Out of the control registers, only C8 is explicitly defined in
196 // HexagonRegisterInfo.td. If others are defined, make sure to add
197 // them here as well.
198 Reserved.set(Hexagon::C8);
199 Reserved.set(Hexagon::USR_OVF);
200
201 // Leveraging these registers will require more work to recognize
202 // the new semantics posed, Hi/LoVec patterns, etc.
203 // Note well: if enabled, they should be restricted to only
204 // where `HST.useHVXOps() && HST.hasV67Ops()` is true.
205 for (auto Reg : Hexagon_MC::GetVectRegRev())
206 Reserved.set(Reg);
207
208 static const MCPhysReg RRegs[] = {
209 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19, Hexagon::R20,
210 Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24, Hexagon::R25,
211 Hexagon::R26, Hexagon::R27, Hexagon::R28};
212 for (MCPhysReg Reg : RRegs)
214 Reserved.set(Reg);
215
216 Register AP =
217 MF.getInfo<HexagonMachineFunctionInfo>()->getStackAlignBaseReg();
218 if (AP.isValid())
219 Reserved.set(AP);
220
221 for (int x = Reserved.find_first(); x >= 0; x = Reserved.find_next(x))
222 markSuperRegs(Reserved, x);
223
224 return Reserved;
225}
226
228 int SPAdj, unsigned FIOp,
229 RegScavenger *RS) const {
230 static unsigned ReuseCount = 0;
231 //
232 // Hexagon_TODO: Do we need to enforce this for Hexagon?
233 assert(SPAdj == 0 && "Unexpected");
234
235 MachineInstr &MI = *II;
236 MachineBasicBlock &MB = *MI.getParent();
237 MachineFunction &MF = *MB.getParent();
238 auto &HST = MF.getSubtarget<HexagonSubtarget>();
239 auto &HII = *HST.getInstrInfo();
240 auto &HFI = *HST.getFrameLowering();
241
242 Register BP;
243 int FI = MI.getOperand(FIOp).getIndex();
244 // Select the base pointer (BP) and calculate the actual offset from BP
245 // to the beginning of the object at index FI.
246 int Offset = HFI.getFrameIndexReference(MF, FI, BP).getFixed();
247 // Add the offset from the instruction.
248 int RealOffset = Offset + MI.getOperand(FIOp+1).getImm();
249
250 unsigned Opc = MI.getOpcode();
251 switch (Opc) {
252 case Hexagon::PS_fia:
253 MI.setDesc(HII.get(Hexagon::A2_addi));
254 MI.getOperand(FIOp).ChangeToImmediate(RealOffset);
255 MI.removeOperand(FIOp+1);
256 return false;
257 case Hexagon::PS_fi:
258 // Set up the instruction for updating below.
259 MI.setDesc(HII.get(Hexagon::A2_addi));
260 break;
261 }
262
263 if (!HII.isValidOffset(Opc, RealOffset, this)) {
264 // If the offset is not valid, calculate the address in a temporary
265 // register and use it with offset 0.
266 int InstOffset = 0;
267 // The actual base register (BP) is typically shared between many
268 // instructions where frame indices are being replaced. In scalar
269 // instructions the offset range is large, and the need for an extra
270 // add instruction is infrequent. Vector loads/stores, however, have
271 // a much smaller offset range: [-8, 7), or #s4. In those cases it
272 // makes sense to "standardize" the immediate in the "addi" instruction
273 // so that multiple loads/stores could be based on it.
274 bool IsPair = false;
275 switch (MI.getOpcode()) {
276 // All of these instructions have the same format: base+#s4.
277 case Hexagon::PS_vloadrw_ai:
278 case Hexagon::PS_vloadrw_nt_ai:
279 case Hexagon::PS_vstorerw_ai:
280 case Hexagon::PS_vstorerw_nt_ai:
281 IsPair = true;
282 [[fallthrough]];
283 case Hexagon::PS_vloadrv_ai:
284 case Hexagon::PS_vloadrv_nt_ai:
285 case Hexagon::PS_vstorerv_ai:
286 case Hexagon::PS_vstorerv_nt_ai:
287 case Hexagon::V6_vL32b_ai:
288 case Hexagon::V6_vS32b_ai: {
289 unsigned HwLen = HST.getVectorLength();
290 if (RealOffset % HwLen == 0) {
291 int VecOffset = RealOffset / HwLen;
292 // Rewrite the offset as "base + [-8, 7)".
293 VecOffset += 8;
294 // Pairs are expanded into two instructions: make sure that both
295 // can use the same base (i.e. VecOffset+1 is not a different
296 // multiple of 16 than VecOffset).
297 if (!IsPair || (VecOffset + 1) % 16 != 0) {
298 RealOffset = (VecOffset & -16) * HwLen;
299 InstOffset = (VecOffset % 16 - 8) * HwLen;
300 }
301 }
302 }
303 }
304
305 // Search backwards in the block for "Reg = A2_addi BP, RealOffset".
306 // This will give us a chance to avoid creating a new register.
307 Register ReuseBP;
308
309 if (ReuseCount < FrameIndexReuseLimit) {
310 unsigned SearchCount = 0, SearchRange = FrameIndexSearchRange;
311 SmallSet<Register,2> SeenVRegs;
312 bool PassedCall = false;
313 LiveRegUnits Defs(*this), Uses(*this);
314
315 for (auto I = std::next(II.getReverse()), E = MB.rend(); I != E; ++I) {
316 if (SearchCount == SearchRange)
317 break;
318 ++SearchCount;
319 const MachineInstr &BI = *I;
321 PassedCall |= BI.isCall();
322 for (const MachineOperand &Op : BI.operands()) {
323 if (SeenVRegs.size() > 1)
324 break;
325 if (Op.isReg() && Op.getReg().isVirtual())
326 SeenVRegs.insert(Op.getReg());
327 }
328 if (BI.getOpcode() != Hexagon::A2_addi)
329 continue;
330 if (BI.getOperand(1).getReg() != BP)
331 continue;
332 const auto &Op2 = BI.getOperand(2);
333 if (!Op2.isImm() || Op2.getImm() != RealOffset)
334 continue;
335
336 Register R = BI.getOperand(0).getReg();
337 if (R.isPhysical()) {
338 if (Defs.available(R))
339 ReuseBP = R;
340 } else if (R.isVirtual()) {
341 // Extending a range of a virtual register can be dangerous,
342 // since the scavenger will need to find a physical register
343 // for it. Avoid extending the range past a function call,
344 // and avoid overlapping it with another virtual register.
345 if (!PassedCall && SeenVRegs.size() <= 1)
346 ReuseBP = R;
347 }
348 break;
349 }
350 if (ReuseBP)
351 ++ReuseCount;
352 }
353
354 auto &MRI = MF.getRegInfo();
355 if (!ReuseBP) {
356 ReuseBP = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
357 const DebugLoc &DL = MI.getDebugLoc();
358 BuildMI(MB, II, DL, HII.get(Hexagon::A2_addi), ReuseBP)
359 .addReg(BP)
360 .addImm(RealOffset);
361 }
362 BP = ReuseBP;
363 RealOffset = InstOffset;
364 }
365
366 MI.getOperand(FIOp).ChangeToRegister(BP, false, false, false);
367 MI.getOperand(FIOp+1).ChangeToImmediate(RealOffset);
368 return false;
369}
370
371
373 const TargetRegisterClass *SrcRC, unsigned SubReg,
374 const TargetRegisterClass *DstRC, unsigned DstSubReg,
375 const TargetRegisterClass *NewRC, LiveIntervals &LIS) const {
376 // Coalescing will extend the live interval of the destination register.
377 // If the destination register is a vector pair, avoid introducing function
378 // calls into the interval, since it could result in a spilling of a pair
379 // instead of a single vector.
380 MachineFunction &MF = *MI->getParent()->getParent();
382 if (!HST.useHVXOps() || NewRC->getID() != Hexagon::HvxWRRegClass.getID())
383 return true;
384 bool SmallSrc = SrcRC->getID() == Hexagon::HvxVRRegClass.getID();
385 bool SmallDst = DstRC->getID() == Hexagon::HvxVRRegClass.getID();
386 if (!SmallSrc && !SmallDst)
387 return true;
388
389 Register DstReg = MI->getOperand(0).getReg();
390 Register SrcReg = MI->getOperand(1).getReg();
391 const SlotIndexes &Indexes = *LIS.getSlotIndexes();
392 auto HasCall = [&Indexes] (const LiveInterval::Segment &S) {
393 for (SlotIndex I = S.start.getBaseIndex(), E = S.end.getBaseIndex();
394 I != E; I = I.getNextIndex()) {
395 if (const MachineInstr *MI = Indexes.getInstructionFromIndex(I))
396 if (MI->isCall())
397 return true;
398 }
399 return false;
400 };
401
402 if (SmallSrc == SmallDst) {
403 // Both must be true, because the case for both being false was
404 // checked earlier. Both registers will be coalesced into a register
405 // of a wider class (HvxWR), and we don't want its live range to
406 // span over calls.
407 return !any_of(LIS.getInterval(DstReg), HasCall) &&
408 !any_of(LIS.getInterval(SrcReg), HasCall);
409 }
410
411 // If one register is large (HvxWR) and the other is small (HvxVR), then
412 // coalescing is ok if the large is already live across a function call,
413 // or if the small one is not.
414 Register SmallReg = SmallSrc ? SrcReg : DstReg;
415 Register LargeReg = SmallSrc ? DstReg : SrcReg;
416 return any_of(LIS.getInterval(LargeReg), HasCall) ||
417 !any_of(LIS.getInterval(SmallReg), HasCall);
418}
419
420
422 &MF) const {
423 const HexagonFrameLowering *TFI = getFrameLowering(MF);
424 if (TFI->hasFP(MF))
425 return getFrameRegister();
426 return getStackRegister();
427}
428
429
431 return Hexagon::R30;
432}
433
434
436 return Hexagon::R29;
437}
438
439
441 const TargetRegisterClass &RC, unsigned GenIdx) const {
442 assert(GenIdx == Hexagon::ps_sub_lo || GenIdx == Hexagon::ps_sub_hi);
443
444 static const unsigned ISub[] = { Hexagon::isub_lo, Hexagon::isub_hi };
445 static const unsigned VSub[] = { Hexagon::vsub_lo, Hexagon::vsub_hi };
446 static const unsigned WSub[] = { Hexagon::wsub_lo, Hexagon::wsub_hi };
447
448 switch (RC.getID()) {
449 case Hexagon::CtrRegs64RegClassID:
450 case Hexagon::DoubleRegsRegClassID:
451 return ISub[GenIdx];
452 case Hexagon::HvxWRRegClassID:
453 return VSub[GenIdx];
454 case Hexagon::HvxVQRRegClassID:
455 return WSub[GenIdx];
456 }
457
458 if (!RC.superclasses().empty())
460 GenIdx);
461
462 llvm_unreachable("Invalid register class");
463}
464
466 const {
467 return MF.getSubtarget<HexagonSubtarget>().getFrameLowering()->hasFP(MF);
468}
469
472 return &Hexagon::IntRegsRegClass;
473}
static const TargetRegisterClass * getRegClass(const MachineInstr &MI, Register Reg)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static cl::opt< unsigned > FrameIndexReuseLimit("hexagon-frame-index-reuse-limit", cl::init(~0), cl::Hidden, cl::desc("Limit on the number of reused registers in frame index " "elimination"))
static cl::opt< unsigned > FrameIndexSearchRange("hexagon-frame-index-search-range", cl::init(32), cl::Hidden, cl::desc("Limit on instruction search range in frame index elimination"))
IRTranslator LLVM IR MI
A set of register units.
#define I(x, y, z)
Definition MD5.cpp:57
#define R4(n)
#define R2(n)
#define R6(n)
uint64_t IntrinsicInst * II
This file declares the machine register scavenger class.
Remove Loads Into Fake Uses
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallSet class.
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
A debug info location.
Definition DebugLoc.h:123
Hexagon target-specific information for each MachineFunction.
bool eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
bool isEHReturnCalleeSaveReg(Register Reg) const
BitVector getReservedRegs(const MachineFunction &MF) const override
const TargetRegisterClass * getPointerRegClass(unsigned Kind=0) const override
bool useFPForScavengingIndex(const MachineFunction &MF) const override
Returns true if the frame pointer is valid.
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
Code Generation virtual methods...
unsigned getHexagonSubRegIndex(const TargetRegisterClass &RC, unsigned GenIdx) const
bool shouldCoalesce(MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, const TargetRegisterClass *DstRC, unsigned DstSubReg, const TargetRegisterClass *NewRC, LiveIntervals &LIS) const override
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const override
bool isFakeReg(MCPhysReg Reg) const
Returns true if the given reserved physical register Reg is live across function calls/returns.
const MCPhysReg * getCallerSavedRegs(const MachineFunction *MF, const TargetRegisterClass *RC) const
bool isGlobalReg(MCPhysReg Reg) const
Returns true if the given reserved physical register is live across function calls/returns.
const HexagonInstrInfo * getInstrInfo() const override
SlotIndexes * getSlotIndexes() const
LiveInterval & getInterval(Register Reg)
A set of register units used to track register liveness.
static void accumulateUsedDefed(const MachineInstr &MI, LiveRegUnits &ModifiedRegUnits, LiveRegUnits &UsedRegUnits, const TargetRegisterInfo *TRI)
For a machine instruction MI, adds all register units used in UsedRegUnits and defined or clobbered i...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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 & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isCall(QueryType Type=AnyInBundle) const
mop_range operands()
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
SlotIndexes pass.
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction for the given index, or null if the given index has no instruction associated...
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
size_type size() const
Definition SmallSet.h:171
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
unsigned getID() const
Return the register class ID number.
ArrayRef< unsigned > superclasses() const
Returns a list of super-classes.
virtual bool isRegisterReservedByUser(Register R) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
llvm::ArrayRef< MCPhysReg > GetVectRegRev()
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1745
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
This represents a simple continuous liveness interval for a value.