LLVM 24.0.0git
TargetRegisterInfo.h
Go to the documentation of this file.
1//==- CodeGen/TargetRegisterInfo.h - Target Register Information -*- C++ -*-==//
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 describes an abstract interface used to get information about a
10// target machines register file. This information is used for a variety of
11// purposed, especially register allocation.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_TARGETREGISTERINFO_H
16#define LLVM_CODEGEN_TARGETREGISTERINFO_H
17
18#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/StringRef.h"
25#include "llvm/IR/CallingConv.h"
26#include "llvm/MC/LaneBitmask.h"
32#include <cassert>
33#include <cstdint>
34
35namespace llvm {
36
37class BitVector;
38class DIExpression;
39class LiveRegMatrix;
40class MachineFunction;
41class MachineInstr;
43class RegScavenger;
44class VirtRegMap;
45class LiveIntervals;
46class LiveInterval;
47
48// TODO: Remove.
50
51/// Extra information, not in MCRegisterDesc, about registers.
52/// These are used by codegen, not by MC.
54 const uint8_t *CostPerUse; // Extra cost of instructions using register.
55 unsigned NumCosts; // Number of cost values associated with each register.
56 const bool
57 *InAllocatableClass; // Register belongs to an allocatable regclass.
58};
59
60/// Each TargetRegisterClass has a per register weight, and weight
61/// limit which must be less than the limits of its pressure sets.
63 unsigned RegWeight;
64 unsigned WeightLimit;
65};
66
67/// TargetRegisterInfo base class - We assume that the target defines a static
68/// array of TargetRegisterDesc objects that represent all of the machine
69/// registers that the target has. As such, we simply have to track a pointer
70/// to this array so that we can turn register number into a register
71/// descriptor.
72///
74public:
76 struct RegClassInfo {
78 unsigned VTListOffset;
79 };
80
81 /// SubRegCoveredBits - Emitted by tablegen: bit range covered by a subreg
82 /// index, -1 in any being invalid.
87
88private:
89 const TargetRegisterInfoDesc *InfoDesc; // Extra desc array for codegen
90 const char *SubRegIndexStrings; // Names of subreg indexes.
91 ArrayRef<uint32_t> SubRegIndexNameOffsets;
92 const SubRegCoveredBits *SubRegIdxRanges; // Pointer to the subreg covered
93 // bit ranges array.
94
95 // Pointer to array of lane masks, one per sub-reg index.
96 const LaneBitmask *SubRegIndexLaneMasks;
97
98 LaneBitmask CoveringLanes;
99 const RegClassInfo *const RCInfos;
100 const MVT::SimpleValueType *const RCVTLists;
101 unsigned HwMode;
102
103protected:
105 const char *SubRegIndexStrings,
106 ArrayRef<uint32_t> SubRegIndexNameOffsets,
107 const SubRegCoveredBits *SubRegIdxRanges,
108 const LaneBitmask *SubRegIndexLaneMasks,
109 LaneBitmask CoveringLanes,
110 const RegClassInfo *const RCInfos,
111 const MVT::SimpleValueType *const RCVTLists,
112 unsigned Mode = 0);
113
114public:
116
117 /// Return the number of registers for the function. (may overestimate)
118 virtual unsigned getNumSupportedRegs(const MachineFunction &) const {
119 return getNumRegs();
120 }
121
122 // Register numbers can represent physical registers, virtual registers, and
123 // sometimes stack slots. The unsigned values are divided into these ranges:
124 //
125 // 0 Not a register, can be used as a sentinel.
126 // [1;2^30) Physical registers assigned by TableGen.
127 // [2^30;2^31) Stack slots. (Rarely used.)
128 // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
129 //
130 // Further sentinels can be allocated from the small negative integers.
131 // DenseMapInfo<unsigned> uses -1u and -2u.
132
133 /// Return the size in bits of a register from class RC.
137
138 /// Return the size in bytes of the stack slot allocated to hold a spilled
139 /// copy of a register from class RC.
140 unsigned getSpillSize(const TargetRegisterClass &RC) const {
141 return getRegClassInfo(RC).SpillSize / 8;
142 }
143
144 /// Return the minimum required alignment in bytes for a spill slot for
145 /// a register of this class.
147 return Align(getRegClassInfo(RC).SpillAlignment / 8);
148 }
149
150 /// Return the stack ID for spill slots holding a spilled copy of a register
151 /// from this class.
153 return static_cast<TargetStackID::Value>(RC.SpillStackID);
154 }
155
156 /// Return true if the given TargetRegisterClass has the ValueType T.
158 for (auto I = legalclasstypes_begin(RC); *I != MVT::Other; ++I)
159 if (MVT(*I) == T)
160 return true;
161 return false;
162 }
163
164 /// Return true if the given TargetRegisterClass is compatible with LLT T.
166 for (auto I = legalclasstypes_begin(RC); *I != MVT::Other; ++I) {
167 MVT VT(*I);
168 if (VT == MVT::Untyped)
169 return true;
170
171 if (LLT(VT) == T)
172 return true;
173 }
174 return false;
175 }
176
177 /// Loop over all of the value types that can be represented by values
178 /// in the given register class.
180 return &RCVTLists[getRegClassInfo(RC).VTListOffset];
181 }
182
185 while (*I != MVT::Other)
186 ++I;
187 return I;
188 }
189
190 /// Returns the Register Class of a physical register, picking the smallest
191 /// register subclass that contains this physreg.
192 virtual const TargetRegisterClass *
194
195 /// Returns the common Register Class of two physical registers, picking the
196 /// smallest register subclass that contains these two physregs.
197 const TargetRegisterClass *
199
200 /// Return the maximal subclass of the given register class that is
201 /// allocatable or NULL.
202 const TargetRegisterClass *
204
205 /// Returns a bitset indexed by register number indicating if a register is
206 /// allocatable or not. If a register class is specified, returns the subset
207 /// for the class.
209 const TargetRegisterClass *RC = nullptr) const;
210
211 /// Get a list of cost values for all registers that correspond to the index
212 /// returned by RegisterCostTableIndex.
214 unsigned Idx = getRegisterCostTableIndex(MF);
215 unsigned NumRegs = getNumRegs();
216 assert(Idx < InfoDesc->NumCosts && "CostPerUse index out of bounds");
217
218 return ArrayRef(&InfoDesc->CostPerUse[Idx * NumRegs], NumRegs);
219 }
220
221 /// Return true if the register is in the allocation of any register class.
223 return InfoDesc->InAllocatableClass[RegNo];
224 }
225
226 /// Return the human-readable symbolic target-specific name for the specified
227 /// SubRegIndex.
228 const char *getSubRegIndexName(unsigned SubIdx) const {
229 assert(SubIdx && SubIdx < getNumSubRegIndices() &&
230 "This is not a subregister index");
231 return SubRegIndexStrings + SubRegIndexNameOffsets[SubIdx - 1];
232 }
233
234 /// Get the size of the bit range covered by a sub-register index.
235 /// If the index isn't continuous, return the sum of the sizes of its parts.
236 /// If the index is used to access subregisters of different sizes, return -1.
237 unsigned getSubRegIdxSize(unsigned Idx) const;
238
239 /// Get the offset of the bit range covered by a sub-register index.
240 /// If an Offset doesn't make sense (the index isn't continuous, or is used to
241 /// access sub-registers at different offsets), return -1.
242 unsigned getSubRegIdxOffset(unsigned Idx) const;
243
244 /// Return a bitmask representing the parts of a register that are covered by
245 /// SubIdx \see LaneBitmask.
246 ///
247 /// SubIdx == 0 is allowed, it has the lane mask ~0u.
248 LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const {
249 assert(SubIdx < getNumSubRegIndices() && "This is not a subregister index");
250 return SubRegIndexLaneMasks[SubIdx];
251 }
252
253 /// Try to find one or more subregister indexes to cover \p LaneMask.
254 ///
255 /// If this is possible, returns true and appends the best matching set of
256 /// indexes to \p Indexes. If this is not possible, returns false.
257 bool getCoveringSubRegIndexes(const TargetRegisterClass *RC,
258 LaneBitmask LaneMask,
259 SmallVectorImpl<unsigned> &Indexes) const;
260
261 /// The lane masks returned by getSubRegIndexLaneMask() above can only be
262 /// used to determine if sub-registers overlap - they can't be used to
263 /// determine if a set of sub-registers completely cover another
264 /// sub-register.
265 ///
266 /// The X86 general purpose registers have two lanes corresponding to the
267 /// sub_8bit and sub_8bit_hi sub-registers. Both sub_32bit and sub_16bit have
268 /// lane masks '3', but the sub_16bit sub-register doesn't fully cover the
269 /// sub_32bit sub-register.
270 ///
271 /// On the other hand, the ARM NEON lanes fully cover their registers: The
272 /// dsub_0 sub-register is completely covered by the ssub_0 and ssub_1 lanes.
273 /// This is related to the CoveredBySubRegs property on register definitions.
274 ///
275 /// This function returns a bit mask of lanes that completely cover their
276 /// sub-registers. More precisely, given:
277 ///
278 /// Covering = getCoveringLanes();
279 /// MaskA = getSubRegIndexLaneMask(SubA);
280 /// MaskB = getSubRegIndexLaneMask(SubB);
281 ///
282 /// If (MaskA & ~(MaskB & Covering)) == 0, then SubA is completely covered by
283 /// SubB.
284 LaneBitmask getCoveringLanes() const { return CoveringLanes; }
285
286 /// Returns true if the two registers are equal or alias each other.
287 /// The registers may be virtual registers.
288 bool regsOverlap(Register RegA, Register RegB) const {
289 if (RegA == RegB)
290 return true;
291 if (RegA.isPhysical() && RegB.isPhysical())
292 return MCRegisterInfo::regsOverlap(RegA.asMCReg(), RegB.asMCReg());
293 return false;
294 }
295
296 /// Returns true if the two subregisters are equal or overlap.
297 /// The registers may be virtual registers.
298 bool checkSubRegInterference(Register RegA, unsigned SubA, Register RegB,
299 unsigned SubB) const;
300
301 /// Returns true if Reg contains RegUnit.
302 bool hasRegUnit(MCRegister Reg, MCRegUnit RegUnit) const {
303 return llvm::is_contained(regunits(Reg), RegUnit);
304 }
305
306 /// Returns the original SrcReg unless it is the target of a copy-like
307 /// operation, in which case we chain backwards through all such operations
308 /// to the ultimate source register. If a physical register is encountered,
309 /// we stop the search.
310 virtual Register lookThruCopyLike(Register SrcReg,
311 const MachineRegisterInfo *MRI) const;
312
313 /// Find the original SrcReg unless it is the target of a copy-like operation,
314 /// in which case we chain backwards through all such operations to the
315 /// ultimate source register. If a physical register is encountered, we stop
316 /// the search.
317 /// Return the original SrcReg if all the definitions in the chain only have
318 /// one user and not a physical register.
319 virtual Register
320 lookThruSingleUseCopyChain(Register SrcReg,
321 const MachineRegisterInfo *MRI) const;
322
323 /// Return a null-terminated list of all of the callee-saved registers on
324 /// this target. The register should be in the order of desired callee-save
325 /// stack frame offset. The first register is closest to the incoming stack
326 /// pointer if stack grows down, and vice versa.
327 /// Notice: This function does not take into account disabled CSRs.
328 /// In most cases you will want to use instead the function
329 /// getCalleeSavedRegs that is implemented in MachineRegisterInfo.
330 virtual const MCPhysReg*
332
333 /// Return a null-terminated list of all of the callee-saved registers on
334 /// this target when IPRA is on. The list should include any non-allocatable
335 /// registers that the backend uses and assumes will be saved by all calling
336 /// conventions. This is typically the ISA-standard frame pointer, but could
337 /// include the thread pointer, TOC pointer, or base pointer for different
338 /// targets.
339 virtual const MCPhysReg *getIPRACSRegs(const MachineFunction *MF) const {
340 return nullptr;
341 }
342
343 /// Return a mask of call-preserved registers for the given calling convention
344 /// on the current function. The mask should include all call-preserved
345 /// aliases. This is used by the register allocator to determine which
346 /// registers can be live across a call.
347 ///
348 /// The mask is an array containing (TRI::getNumRegs()+31)/32 entries.
349 /// A set bit indicates that all bits of the corresponding register are
350 /// preserved across the function call. The bit mask is expected to be
351 /// sub-register complete, i.e. if A is preserved, so are all its
352 /// sub-registers.
353 ///
354 /// Bits are numbered from the LSB, so the bit for physical register Reg can
355 /// be found as (Mask[Reg / 32] >> Reg % 32) & 1.
356 ///
357 /// A NULL pointer means that no register mask will be used, and call
358 /// instructions should use implicit-def operands to indicate call clobbered
359 /// registers.
360 ///
362 CallingConv::ID) const {
363 // The default mask clobbers everything. All targets should override.
364 return nullptr;
365 }
366
367 /// Return a register mask for the registers preserved by the unwinder,
368 /// or nullptr if no custom mask is needed.
369 virtual const uint32_t *
371 return nullptr;
372 }
373
374 /// Return a register mask that clobbers everything.
375 virtual const uint32_t *getNoPreservedMask() const {
376 llvm_unreachable("target does not provide no preserved mask");
377 }
378
379 /// Return a list of all of the registers which are clobbered "inside" a call
380 /// to the given function. For example, these might be needed for PLT
381 /// sequences of long-branch veneers.
382 virtual ArrayRef<MCPhysReg>
384 return {};
385 }
386
387 /// Return true if all bits that are set in mask \p mask0 are also set in
388 /// \p mask1.
389 bool regmaskSubsetEqual(const uint32_t *mask0, const uint32_t *mask1) const;
390
391 /// Return all the call-preserved register masks defined for this target.
394
395 /// Returns a bitset indexed by physical register number indicating if a
396 /// register is a special register that has particular uses and should be
397 /// considered unavailable at all times, e.g. stack pointer, return address.
398 /// A reserved register:
399 /// - is not allocatable
400 /// - is considered always live
401 /// - is ignored by liveness tracking
402 /// It is often necessary to reserve the super registers of a reserved
403 /// register as well, to avoid them getting allocated indirectly. You may use
404 /// markSuperRegs() and checkAllSuperRegsMarked() in this case.
405 virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0;
406
407 /// Returns either a string explaining why the given register is reserved for
408 /// this function, or an empty optional if no explanation has been written.
409 /// The absence of an explanation does not mean that the register is not
410 /// reserved (meaning, you should check that PhysReg is in fact reserved
411 /// before calling this).
412 virtual std::optional<std::string>
414 return {};
415 }
416
417 /// Returns false if we can't guarantee that Physreg, specified as an IR asm
418 /// clobber constraint, will be preserved across the statement.
419 virtual bool isAsmClobberable(const MachineFunction &MF,
420 MCRegister PhysReg) const {
421 return true;
422 }
423
424 /// Returns true if PhysReg cannot be written to in inline asm statements.
426 MCRegister PhysReg) const {
427 return false;
428 }
429
430 /// Returns true if PhysReg is unallocatable and constant throughout the
431 /// function. Used by MachineRegisterInfo::isConstantPhysReg().
432 virtual bool isConstantPhysReg(MCRegister PhysReg) const { return false; }
433
434 /// Returns true if the register class is considered divergent.
435 virtual bool isDivergentRegClass(const TargetRegisterClass *RC) const {
436 return false;
437 }
438
439 /// Returns true if the register is considered uniform.
440 virtual bool isUniformReg(const MachineRegisterInfo &MRI,
441 const RegisterBankInfo &RBI, Register Reg) const {
442 return false;
443 }
444
445 /// Returns true if MachineLoopInfo should analyze the given physreg
446 /// for loop invariance.
448 return false;
449 }
450
451 /// Physical registers that may be modified within a function but are
452 /// guaranteed to be restored before any uses. This is useful for targets that
453 /// have call sequences where a GOT register may be updated by the caller
454 /// prior to a call and is guaranteed to be restored (also by the caller)
455 /// after the call.
457 const MachineFunction &MF) const {
458 return false;
459 }
460
461 /// This is a wrapper around getCallPreservedMask().
462 /// Return true if the register is preserved after the call.
463 virtual bool isCalleeSavedPhysReg(MCRegister PhysReg,
464 const MachineFunction &MF) const;
465
466 /// Returns true if PhysReg can be used as an argument to a function.
467 virtual bool isArgumentRegister(const MachineFunction &MF,
468 MCRegister PhysReg) const {
469 return false;
470 }
471
472 /// Returns true if PhysReg is a fixed register.
473 virtual bool isFixedRegister(const MachineFunction &MF,
474 MCRegister PhysReg) const {
475 return false;
476 }
477
478 /// Returns true if PhysReg is a general purpose register.
480 MCRegister PhysReg) const {
481 return false;
482 }
483
484 /// Returns true if RC is a class/subclass of general purpose register.
485 virtual bool
487 return false;
488 }
489
490 /// Prior to adding the live-out mask to a stackmap or patchpoint
491 /// instruction, provide the target the opportunity to adjust it (mainly to
492 /// remove pseudo-registers that should be ignored).
493 virtual void adjustStackMapLiveOutMask(uint32_t *Mask) const {}
494
495 /// Return a subclass of the register class \p A so that each register in it
496 /// has a sub-register of sub-register index \p Idx which is in the register
497 /// class \p B.
498 ///
499 /// TableGen will synthesize missing A sub-classes.
500 virtual const TargetRegisterClass *
501 getMatchingSuperRegClass(const TargetRegisterClass *A,
502 const TargetRegisterClass *B, unsigned Idx) const;
503
504 /// Find a common register class that can accomodate both the source and
505 /// destination operands of a copy-like instruction:
506 ///
507 /// DefRC:DefSubReg = COPY SrcRC:SrcSubReg
508 ///
509 /// This is a generalized form of getMatchingSuperRegClass,
510 /// getCommonSuperRegClass, and getCommonSubClass which handles 0, 1, or 2
511 /// subregister indexes. Those utilities should be preferred if the number of
512 /// non-0 subregister indexes is known.
513 const TargetRegisterClass *
514 findCommonRegClass(const TargetRegisterClass *DefRC, unsigned DefSubReg,
515 const TargetRegisterClass *SrcRC,
516 unsigned SrcSubReg) const;
517
518 // For a copy-like instruction that defines a register of class DefRC with
519 // subreg index DefSubReg, reading from another source with class SrcRC and
520 // subregister SrcSubReg return true if this is a preferable copy
521 // instruction or an earlier use should be used.
522 virtual bool shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
523 unsigned DefSubReg,
524 const TargetRegisterClass *SrcRC,
525 unsigned SrcSubReg) const {
526 // If this source does not incur a cross register bank copy, use it.
527 return findCommonRegClass(DefRC, DefSubReg, SrcRC, SrcSubReg) != nullptr;
528 }
529
530 /// Returns the largest legal sub-class of \p RC that supports the
531 /// sub-register index \p Idx.
532 /// If no such sub-class exists, return NULL.
533 /// If all registers in RC already have an Idx sub-register, return RC.
534 ///
535 /// TableGen generates a version of this function that is good enough in most
536 /// cases. Targets can override if they have constraints that TableGen
537 /// doesn't understand. For example, the x86 sub_8bit sub-register index is
538 /// supported by the full GR32 register class in 64-bit mode, but only by the
539 /// GR32_ABCD regiister class in 32-bit mode.
540 ///
541 /// TableGen will synthesize missing RC sub-classes.
542 virtual const TargetRegisterClass *
543 getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const {
544 assert(Idx == 0 && "Target has no sub-registers");
545 return RC;
546 }
547
548 /// Returns the register class of all sub-registers of \p SuperRC obtained by
549 /// applying the sub-register index \p SubRegIdx.
550 ///
551 /// TableGen *may not* synthesize the missing sub-register classes, so this
552 /// function may return null even if SubRegIdx can be applied to all registers
553 /// in SuperRC, i.e., even if
554 /// isSubRegValidForRegClass(SuperRC, SubRegIdx) is true.
555 virtual const TargetRegisterClass *
557 unsigned SubRegIdx) const {
558 return nullptr;
559 }
560
561 /// Returns true if sub-register \p Idx can be used with register class \p RC.
562 /// Idx is valid if the largest subclass of RC that supports sub-register
563 /// index Idx is same as RC. That is, every physical register in RC supports
564 /// sub-register index Idx.
566 unsigned Idx) const {
567 return getSubClassWithSubReg(RC, Idx) == RC;
568 }
569
570 /// Return the subregister index you get from composing
571 /// two subregister indices.
572 ///
573 /// The special null sub-register index composes as the identity.
574 ///
575 /// If R:a:b is the same register as R:c, then composeSubRegIndices(a, b)
576 /// returns c. Note that composeSubRegIndices does not tell you about illegal
577 /// compositions. If R does not have a subreg a, or R:a does not have a subreg
578 /// b, composeSubRegIndices doesn't tell you.
579 ///
580 /// The ARM register Q0 has two D subregs dsub_0:D0 and dsub_1:D1. It also has
581 /// ssub_0:S0 - ssub_3:S3 subregs.
582 /// If you compose subreg indices dsub_1, ssub_0 you get ssub_2.
583 unsigned composeSubRegIndices(unsigned a, unsigned b) const {
584 if (!a) return b;
585 if (!b) return a;
586 return composeSubRegIndicesImpl(a, b);
587 }
588
589 /// Return a subregister index that will compose to give you the subregister
590 /// index.
591 ///
592 /// Finds a subregister index x such that composeSubRegIndices(a, x) ==
593 /// b. Note that this relationship does not hold if
594 /// reverseComposeSubRegIndices returns the null subregister.
595 ///
596 /// The special null sub-register index composes as the identity.
597 unsigned reverseComposeSubRegIndices(unsigned a, unsigned b) const {
598 if (!a)
599 return b;
600 if (!b)
601 return a;
603 }
604
605 /// Transforms a LaneMask computed for one subregister to the lanemask that
606 /// would have been computed when composing the subsubregisters with IdxA
607 /// first. @sa composeSubRegIndices()
609 LaneBitmask Mask) const {
610 if (!IdxA)
611 return Mask;
612 return composeSubRegIndexLaneMaskImpl(IdxA, Mask);
613 }
614
615 /// Transform a lanemask given for a virtual register to the corresponding
616 /// lanemask before using subregister with index \p IdxA.
617 /// This is the reverse of composeSubRegIndexLaneMask(), assuming Mask is a
618 /// valie lane mask (no invalid bits set) the following holds:
619 /// X0 = composeSubRegIndexLaneMask(Idx, Mask)
620 /// X1 = reverseComposeSubRegIndexLaneMask(Idx, X0)
621 /// => X1 == Mask
623 LaneBitmask LaneMask) const {
624 if (!IdxA)
625 return LaneMask;
626 return reverseComposeSubRegIndexLaneMaskImpl(IdxA, LaneMask);
627 }
628
629 /// Debugging helper: dump register in human readable form to dbgs() stream.
630 static void dumpReg(Register Reg, unsigned SubRegIndex = 0,
631 const TargetRegisterInfo *TRI = nullptr);
632
633 /// Return target defined base register class for a physical register.
634 /// This is the register class with the lowest BaseClassOrder containing the
635 /// register.
636 /// Will be nullptr if the register is not in any base register class.
638 return nullptr;
639 }
640
641protected:
642 /// Overridden by TableGen in targets that have sub-registers.
643 virtual unsigned composeSubRegIndicesImpl(unsigned, unsigned) const {
644 llvm_unreachable("Target has no sub-registers");
645 }
646
647 /// Overridden by TableGen in targets that have sub-registers.
648 virtual unsigned reverseComposeSubRegIndicesImpl(unsigned, unsigned) const {
649 llvm_unreachable("Target has no sub-registers");
650 }
651
652 /// Overridden by TableGen in targets that have sub-registers.
653 virtual LaneBitmask
655 llvm_unreachable("Target has no sub-registers");
656 }
657
659 LaneBitmask) const {
660 llvm_unreachable("Target has no sub-registers");
661 }
662
663 /// Return the register cost table index. This implementation is sufficient
664 /// for most architectures and can be overriden by targets in case there are
665 /// multiple cost values associated with each register.
666 virtual unsigned getRegisterCostTableIndex(const MachineFunction &MF) const {
667 return 0;
668 }
669
670public:
671 /// Find a common super-register class if it exists.
672 ///
673 /// Find a register class, SuperRC and two sub-register indices, PreA and
674 /// PreB, such that:
675 ///
676 /// 1. PreA + SubA == PreB + SubB (using composeSubRegIndices()), and
677 ///
678 /// 2. For all Reg in SuperRC: Reg:PreA in RCA and Reg:PreB in RCB, and
679 ///
680 /// 3. SuperRC->getSize() >= max(RCA->getSize(), RCB->getSize()).
681 ///
682 /// SuperRC will be chosen such that no super-class of SuperRC satisfies the
683 /// requirements, and there is no register class with a smaller spill size
684 /// that satisfies the requirements.
685 ///
686 /// SubA and SubB must not be 0. Use getMatchingSuperRegClass() instead.
687 ///
688 /// Either of the PreA and PreB sub-register indices may be returned as 0. In
689 /// that case, the returned register class will be a sub-class of the
690 /// corresponding argument register class.
691 ///
692 /// The function returns NULL if no register class can be found.
694 getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
695 const TargetRegisterClass *RCB, unsigned SubB,
696 unsigned &PreA, unsigned &PreB) const;
697
698 //===--------------------------------------------------------------------===//
699 // Register Class Information
700 //
701protected:
703 return RCInfos[getNumRegClasses() * HwMode + RC.getID()];
704 }
705
706 /// Custom reordering of the allocation order.
707 virtual void filterAndSortForAntiHintedRegs(
708 Register VirtReg, MutableArrayRef<MCPhysReg> CustomOrder,
709 const BitVector &AntiHintedRegUnits, const MachineFunction &MF,
710 const LiveRegMatrix *Matrix = nullptr,
711 const RegisterClassInfo *RegClassInfo = nullptr) const;
712
713public:
714 /// Returns the register class associated with the enumeration value.
715 /// See class MCOperandInfo.
716 const TargetRegisterClass *getRegClass(unsigned i) const {
718 }
719
720 /// Find the largest common subclass of A and B.
721 /// Return NULL if there is no common subclass.
722 const TargetRegisterClass *
723 getCommonSubClass(const TargetRegisterClass *A,
724 const TargetRegisterClass *B) const;
725
726 /// Returns a legal register class to copy a register in the specified class
727 /// to or from. If it is possible to copy the register directly without using
728 /// a cross register class copy, return the specified RC. Returns NULL if it
729 /// is not possible to copy between two registers of the specified class.
730 virtual const TargetRegisterClass *
732 return RC;
733 }
734
735 /// Returns the largest super class of RC that is legal to use in the current
736 /// sub-target and has the same spill size.
737 /// The returned register class can be used to create virtual registers which
738 /// means that all its registers can be copied and spilled.
739 virtual const TargetRegisterClass *
741 const MachineFunction &) const {
742 /// The default implementation is very conservative and doesn't allow the
743 /// register allocator to inflate register classes.
744 return RC;
745 }
746
747 /// Return the register pressure "high water mark" for the specific register
748 /// class. The scheduler is in high register pressure mode (for the specific
749 /// register class) if it goes over the limit.
750 ///
751 /// Note: this is the old register pressure model that relies on a manually
752 /// specified representative register class per value type.
753 virtual unsigned getRegPressureLimit(const TargetRegisterClass *RC,
754 MachineFunction &MF) const {
755 return 0;
756 }
757
758 /// Return a heuristic for the machine scheduler to compare the profitability
759 /// of increasing one register pressure set versus another. The scheduler
760 /// will prefer increasing the register pressure of the set which returns
761 /// the largest value for this function.
762 virtual unsigned getRegPressureSetScore(const MachineFunction &MF,
763 unsigned PSetID) const {
764 return PSetID;
765 }
766
767 /// Get the weight in units of pressure for this register class.
769 const TargetRegisterClass *RC) const = 0;
770
771 /// Returns size in bits of a phys/virtual/generic register.
773
774 /// Get the weight in units of pressure for this register unit.
775 virtual unsigned getRegUnitWeight(MCRegUnit RegUnit) const = 0;
776
777 /// Get the number of dimensions of register pressure.
778 virtual unsigned getNumRegPressureSets() const = 0;
779
780 /// Get the name of this register unit pressure set.
781 virtual const char *getRegPressureSetName(unsigned Idx) const = 0;
782
783 /// Get the register unit pressure limit for this dimension.
784 /// This limit must be adjusted dynamically for reserved registers.
785 virtual unsigned getRegPressureSetLimit(const MachineFunction &MF,
786 unsigned Idx) const = 0;
787
788 /// Get the register class for this pressure set with the largest
789 /// `RegClassWeight::WeightLimit`.
790 virtual const TargetRegisterClass *
791 getLargestRegClassForRegPressureSet(unsigned Idx) const = 0;
792
793 /// Get the dimensions of register pressure impacted by this register class.
794 /// Returns a -1 terminated array of pressure set IDs.
795 virtual const int *getRegClassPressureSets(
796 const TargetRegisterClass *RC) const = 0;
797
798 /// Get the dimensions of register pressure impacted by this register unit.
799 /// Returns a -1 terminated array of pressure set IDs.
800 virtual const int *getRegUnitPressureSets(MCRegUnit RegUnit) const = 0;
801
802 /// Get the scale factor of spill weight for this register class.
803 virtual float getSpillWeightScaleFactor(const TargetRegisterClass *RC) const;
804
805 /// Returns the preferred order for allocating registers from this register
806 /// class in MF. The raw order comes directly from the .td file and may
807 /// include reserved registers that are not allocatable.
808 /// Register allocators should also make sure to allocate
809 /// callee-saved registers only after all the volatiles are used. The
810 /// RegisterClassInfo class provides filtered allocation orders with
811 /// callee-saved registers moved to the end.
812 ///
813 /// The MachineFunction argument can be used to tune the allocatable
814 /// registers based on the characteristics of the function, subtarget, or
815 /// other criteria.
816 ///
817 /// By default, this method returns all registers in the class.
818 virtual ArrayRef<MCPhysReg>
820 bool /*Rev*/ = false) const {
821 return RC.getRegisters();
822 }
823
824 /// Get a list of 'hint' registers that the register allocator should try
825 /// first when allocating a physical register for the virtual register
826 /// VirtReg. These registers are effectively moved to the front of the
827 /// allocation order. If true is returned, regalloc will try to only use
828 /// hints to the greatest extent possible even if it means spilling.
829 ///
830 /// The Order argument is the allocation order for VirtReg's register class
831 /// as returned from RegisterClassInfo::getOrder(). The hint registers must
832 /// come from Order, and they must not be reserved.
833 ///
834 /// The default implementation of this function will only add target
835 /// independent register allocation hints. Targets that override this
836 /// function should typically call this default implementation as well and
837 /// expect to see generic copy hints added.
838 virtual bool
839 getRegAllocationHints(Register VirtReg, ArrayRef<MCPhysReg> Order,
841 const MachineFunction &MF,
842 const VirtRegMap *VRM = nullptr,
843 const LiveRegMatrix *Matrix = nullptr) const;
844
845 /// A callback to allow target a chance to update register allocation hints
846 /// when a register is "changed" (e.g. coalesced) to another register.
847 /// e.g. On ARM, some virtual registers should target register pairs,
848 /// if one of pair is coalesced to another register, the allocation hint of
849 /// the other half of the pair should be changed to point to the new register.
851 MachineFunction &MF) const {
852 // Do nothing.
853 }
854
855 /// Return true if Reg overlaps one of the anti-hinted register units.
856 bool isAntiHintedReg(MCPhysReg Reg,
857 const BitVector &AntiHintedRegUnits) const;
858
859 /// Apply anti-hints to the allocation order.
860 void applyRegAllocationAntiHints(
861 Register VirtReg, ArrayRef<MCPhysReg> Order,
862 SmallVectorImpl<MCPhysReg> &HintsAndCustomOrder, unsigned NumHints,
863 const BitVector &AntiHintedRegUnits, const MachineFunction &MF,
864 const LiveRegMatrix *Matrix = nullptr,
865 const RegisterClassInfo *RegClassInfo = nullptr) const;
866
867 /// Allow the target to reverse allocation order of local live ranges. This
868 /// will generally allocate shorter local live ranges first. For targets with
869 /// many registers, this could reduce regalloc compile time by a large
870 /// factor. It is disabled by default for three reasons:
871 /// (1) Top-down allocation is simpler and easier to debug for targets that
872 /// don't benefit from reversing the order.
873 /// (2) Bottom-up allocation could result in poor evicition decisions on some
874 /// targets affecting the performance of compiled code.
875 /// (3) Bottom-up allocation is no longer guaranteed to optimally color.
876 virtual bool reverseLocalAssignment() const { return false; }
877
878 /// Allow the target to override the cost of using a callee-saved register for
879 /// the first time. Default value of 0 means we will use a callee-saved
880 /// register if it is available.
881 virtual unsigned getCSRFirstUseCost(const MachineFunction &MF) const {
882 return 0;
883 }
884 /// FIXME: We should deprecate this usage.
885 virtual unsigned getCSRCost() const { return 0; }
886
887 /// Scale the CSRFirstUseCost with this number.
888 /// The scale is a percentage (e.g., 30 means 30% of the base cost).
889 /// Target can tune and override this default value.
890 virtual unsigned getCSRCostScale(const MachineFunction &MF) const {
891 return 30;
892 }
893
894 /// Returns true if the target requires (and can make use of) the register
895 /// scavenger.
896 virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
897 return false;
898 }
899
900 /// Returns true if the target wants to use frame pointer based accesses to
901 /// spill to the scavenger emergency spill slot.
902 virtual bool useFPForScavengingIndex(const MachineFunction &MF) const {
903 return true;
904 }
905
906 /// Returns true if the target requires post PEI scavenging of registers for
907 /// materializing frame index constants.
908 virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const {
909 return false;
910 }
911
912 /// Returns true if the target requires using the RegScavenger directly for
913 /// frame elimination despite using requiresFrameIndexScavenging.
915 const MachineFunction &MF) const {
916 return false;
917 }
918
919 /// Returns true if the target wants the LocalStackAllocation pass to be run
920 /// and virtual base registers used for more efficient stack access.
921 virtual bool requiresVirtualBaseRegisters(const MachineFunction &MF) const {
922 return false;
923 }
924
925 /// Return true if target has reserved a spill slot in the stack frame of
926 /// the given function for the specified register. e.g. On x86, if the frame
927 /// register is required, the first fixed stack object is reserved as its
928 /// spill slot. This tells PEI not to create a new stack frame
929 /// object for the given register. It should be called only after
930 /// determineCalleeSaves().
932 int &FrameIdx) const {
933 return false;
934 }
935
936 /// Returns true if the live-ins should be tracked after register allocation.
937 virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
938 return true;
939 }
940
941 /// True if the stack can be realigned for the target.
942 virtual bool canRealignStack(const MachineFunction &MF) const;
943
944 /// True if storage within the function requires the stack pointer to be
945 /// aligned more than the normal calling convention calls for.
946 virtual bool shouldRealignStack(const MachineFunction &MF) const;
947
948 /// True if stack realignment is required and still possible.
949 bool hasStackRealignment(const MachineFunction &MF) const {
950 return shouldRealignStack(MF) && canRealignStack(MF);
951 }
952
953 /// Get the offset from the referenced frame index in the instruction,
954 /// if there is one.
956 int Idx) const {
957 return 0;
958 }
959
960 /// Returns true if the instruction's frame index reference would be better
961 /// served by a base register other than FP or SP.
962 /// Used by LocalStackFrameAllocation to determine which frame index
963 /// references it should create new base registers for.
964 virtual bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
965 return false;
966 }
967
968 /// Insert defining instruction(s) for a pointer to FrameIdx before
969 /// insertion point I. Return materialized frame pointer.
971 int FrameIdx,
972 int64_t Offset) const {
973 llvm_unreachable("materializeFrameBaseRegister does not exist on this "
974 "target");
975 }
976
977 /// Resolve a frame index operand of an instruction
978 /// to reference the indicated base register plus offset instead.
980 int64_t Offset) const {
981 llvm_unreachable("resolveFrameIndex does not exist on this target");
982 }
983
984 /// Determine whether a given base register plus offset immediate is
985 /// encodable to resolve a frame index.
986 virtual bool isFrameOffsetLegal(const MachineInstr *MI, Register BaseReg,
987 int64_t Offset) const {
988 llvm_unreachable("isFrameOffsetLegal does not exist on this target");
989 }
990
991 /// Gets the DWARF expression opcodes for \p Offset.
992 virtual void getOffsetOpcodes(const StackOffset &Offset,
994
995 /// Prepends a DWARF expression for \p Offset to DIExpression \p Expr.
997 prependOffsetExpression(const DIExpression *Expr, unsigned PrependFlags,
998 const StackOffset &Offset) const;
999
1000 virtual int64_t getDwarfRegNumForVirtReg(Register RegNum, bool isEH) const {
1001 llvm_unreachable("getDwarfRegNumForVirtReg does not exist on this target");
1002 }
1003
1004 /// Spill the register so it can be used by the register scavenger.
1005 /// Return true if the register was spilled, false otherwise.
1006 /// If this function does not spill the register, the scavenger
1007 /// will instead spill it to the emergency spill slot.
1011 const TargetRegisterClass *RC,
1012 Register Reg) const {
1013 return false;
1014 }
1015
1016 /// Process frame indices in reverse block order. This changes the behavior of
1017 /// the RegScavenger passed to eliminateFrameIndex. If this is true targets
1018 /// should scavengeRegisterBackwards in eliminateFrameIndex. New targets
1019 /// should prefer reverse scavenging behavior.
1020 /// TODO: Remove this when all targets return true.
1021 virtual bool eliminateFrameIndicesBackwards() const { return true; }
1022
1023 /// This method must be overriden to eliminate abstract frame indices from
1024 /// instructions which may use them. The instruction referenced by the
1025 /// iterator contains an MO_FrameIndex operand which must be eliminated by
1026 /// this method. This method may modify or replace the specified instruction,
1027 /// as long as it keeps the iterator pointing at the finished product.
1028 /// SPAdj is the SP adjustment due to call frame setup instruction.
1029 /// FIOperandNum is the FI operand number.
1030 /// Returns true if the current instruction was removed and the iterator
1031 /// is not longer valid
1033 int SPAdj, unsigned FIOperandNum,
1034 RegScavenger *RS = nullptr) const = 0;
1035
1036 /// Return the assembly name for \p Reg.
1038 // FIXME: We are assuming that the assembly name is equal to the TableGen
1039 // name converted to lower case
1040 //
1041 // The TableGen name is the name of the definition for this register in the
1042 // target's tablegen files. For example, the TableGen name of
1043 // def EAX : Register <...>; is "EAX"
1044 return StringRef(getName(Reg));
1045 }
1046
1047 //===--------------------------------------------------------------------===//
1048 /// Subtarget Hooks
1049
1050 /// SrcRC and DstRC will be morphed into NewRC if this returns true.
1052 const TargetRegisterClass *SrcRC,
1053 unsigned SubReg,
1054 const TargetRegisterClass *DstRC,
1055 unsigned DstSubReg,
1056 const TargetRegisterClass *NewRC,
1057 LiveIntervals &LIS) const
1058 { return true; }
1059
1060 /// Region split has a high compile time cost especially for large live range.
1061 /// This method is used to decide whether or not \p VirtReg should
1062 /// go through this expensive splitting heuristic.
1063 virtual bool shouldRegionSplitForVirtReg(const MachineFunction &MF,
1064 const LiveInterval &VirtReg) const;
1065
1066 /// Last chance recoloring has a high compile time cost especially for
1067 /// targets with a lot of registers.
1068 /// This method is used to decide whether or not \p VirtReg should
1069 /// go through this expensive heuristic.
1070 /// When this target hook is hit, by returning false, there is a high
1071 /// chance that the register allocation will fail altogether (usually with
1072 /// "ran out of registers").
1073 /// That said, this error usually points to another problem in the
1074 /// optimization pipeline.
1075 virtual bool
1077 const LiveInterval &VirtReg) const {
1078 return true;
1079 }
1080
1081 /// When prioritizing live ranges in register allocation, if this hook returns
1082 /// true then the AllocationPriority of the register class will be treated as
1083 /// more important than whether the range is local to a basic block or global.
1084 virtual bool
1086 return false;
1087 }
1088
1089 //===--------------------------------------------------------------------===//
1090 /// Debug information queries.
1091
1092 /// getFrameRegister - This method should return the register used as a base
1093 /// for values allocated in the current stack frame.
1094 virtual Register getFrameRegister(const MachineFunction &MF) const = 0;
1095
1096 /// Mark a register and all its aliases as reserved in the given set.
1097 void markSuperRegs(BitVector &RegisterSet, MCRegister Reg) const;
1098
1099 /// Returns true if for every register in the set all super registers are part
1100 /// of the set as well.
1101 bool checkAllSuperRegsMarked(const BitVector &RegisterSet,
1102 ArrayRef<MCPhysReg> Exceptions = ArrayRef<MCPhysReg>()) const;
1103
1104 virtual const TargetRegisterClass *
1106 const MachineRegisterInfo &MRI) const {
1107 return nullptr;
1108 }
1109
1110 /// Some targets have non-allocatable registers that aren't technically part
1111 /// of the explicit callee saved register list, but should be handled as such
1112 /// in certain cases.
1114 return false;
1115 }
1116
1117 /// Some targets delay assigning the frame until late and use a placeholder
1118 /// to represent it earlier. This method can be used to identify the frame
1119 /// register placeholder.
1120 virtual bool isVirtualFrameRegister(MCRegister Reg) const { return false; }
1121
1122 virtual std::optional<uint8_t> getVRegFlagValue(StringRef Name) const {
1123 return {};
1124 }
1125
1128 return {};
1129 }
1130
1131 // Whether this register should be ignored when generating CodeView debug
1132 // info, because it's a known there is no mapping available.
1133 virtual bool isIgnoredCVReg(MCRegister LLVMReg) const { return false; }
1134};
1135
1136//===----------------------------------------------------------------------===//
1137// SuperRegClassIterator
1138//===----------------------------------------------------------------------===//
1139//
1140// Iterate over the possible super-registers for a given register class. The
1141// iterator will visit a list of pairs (Idx, Mask) corresponding to the
1142// possible classes of super-registers.
1143//
1144// Each bit mask will have at least one set bit, and each set bit in Mask
1145// corresponds to a SuperRC such that:
1146//
1147// For all Reg in SuperRC: Reg:Idx is in RC.
1148//
1149// The iterator can include (O, RC->getSubClassMask()) as the first entry which
1150// also satisfies the above requirement, assuming Reg:0 == Reg.
1151//
1153 const unsigned RCMaskWords;
1154 unsigned SubReg = 0;
1155 const uint16_t *Idx;
1156 const uint32_t *Mask;
1157
1158public:
1159 /// Create a SuperRegClassIterator that visits all the super-register classes
1160 /// of RC. When IncludeSelf is set, also include the (0, sub-classes) entry.
1162 const TargetRegisterInfo *TRI,
1163 bool IncludeSelf = false)
1164 : RCMaskWords((TRI->getNumRegClasses() + 31) / 32),
1165 Idx(RC->getSuperRegIndices()), Mask(RC->getSubClassMask()) {
1166 if (!IncludeSelf)
1167 ++*this;
1168 }
1169
1170 /// Returns true if this iterator is still pointing at a valid entry.
1171 bool isValid() const { return Idx; }
1172
1173 /// Returns the current sub-register index.
1174 unsigned getSubReg() const { return SubReg; }
1175
1176 /// Returns the bit mask of register classes that getSubReg() projects into
1177 /// RC.
1178 /// See TargetRegisterClass::getSubClassMask() for how to use it.
1179 const uint32_t *getMask() const { return Mask; }
1180
1181 /// Advance iterator to the next entry.
1182 void operator++() {
1183 assert(isValid() && "Cannot move iterator past end.");
1184 Mask += RCMaskWords;
1185 SubReg = *Idx++;
1186 if (!SubReg)
1187 Idx = nullptr;
1188 }
1189};
1190
1191//===----------------------------------------------------------------------===//
1192// BitMaskClassIterator
1193//===----------------------------------------------------------------------===//
1194/// This class encapuslates the logic to iterate over bitmask returned by
1195/// the various RegClass related APIs.
1196/// E.g., this class can be used to iterate over the subclasses provided by
1197/// TargetRegisterClass::getSubClassMask or SuperRegClassIterator::getMask.
1199 /// Total number of register classes.
1200 const unsigned NumRegClasses;
1201 /// Base index of CurrentChunk.
1202 /// In other words, the number of bit we read to get at the
1203 /// beginning of that chunck.
1204 unsigned Base = 0;
1205 /// Adjust base index of CurrentChunk.
1206 /// Base index + how many bit we read within CurrentChunk.
1207 unsigned Idx = 0;
1208 /// Current register class ID.
1209 unsigned ID = 0;
1210 /// Mask we are iterating over.
1211 const uint32_t *Mask;
1212 /// Current chunk of the Mask we are traversing.
1213 uint32_t CurrentChunk;
1214
1215 /// Move ID to the next set bit.
1216 void moveToNextID() {
1217 // If the current chunk of memory is empty, move to the next one,
1218 // while making sure we do not go pass the number of register
1219 // classes.
1220 while (!CurrentChunk) {
1221 // Move to the next chunk.
1222 Base += 32;
1223 if (Base >= NumRegClasses) {
1224 ID = NumRegClasses;
1225 return;
1226 }
1227 CurrentChunk = *++Mask;
1228 Idx = Base;
1229 }
1230 // Otherwise look for the first bit set from the right
1231 // (representation of the class ID is big endian).
1232 // See getSubClassMask for more details on the representation.
1233 unsigned Offset = llvm::countr_zero(CurrentChunk);
1234 // Add the Offset to the adjusted base number of this chunk: Idx.
1235 // This is the ID of the register class.
1236 ID = Idx + Offset;
1237
1238 // Consume the zeros, if any, and the bit we just read
1239 // so that we are at the right spot for the next call.
1240 // Do not do Offset + 1 because Offset may be 31 and 32
1241 // will be UB for the shift, though in that case we could
1242 // have make the chunk being equal to 0, but that would
1243 // have introduced a if statement.
1244 moveNBits(Offset);
1245 moveNBits(1);
1246 }
1247
1248 /// Move \p NumBits Bits forward in CurrentChunk.
1249 void moveNBits(unsigned NumBits) {
1250 assert(NumBits < 32 && "Undefined behavior spotted!");
1251 // Consume the bit we read for the next call.
1252 CurrentChunk >>= NumBits;
1253 // Adjust the base for the chunk.
1254 Idx += NumBits;
1255 }
1256
1257public:
1258 /// Create a BitMaskClassIterator that visits all the register classes
1259 /// represented by \p Mask.
1260 ///
1261 /// \pre \p Mask != nullptr
1263 : NumRegClasses(TRI.getNumRegClasses()), Mask(Mask), CurrentChunk(*Mask) {
1264 // Move to the first ID.
1265 moveToNextID();
1266 }
1267
1268 /// Returns true if this iterator is still pointing at a valid entry.
1269 bool isValid() const { return getID() != NumRegClasses; }
1270
1271 /// Returns the current register class ID.
1272 unsigned getID() const { return ID; }
1273
1274 /// Advance iterator to the next entry.
1275 void operator++() {
1276 assert(isValid() && "Cannot move iterator past end.");
1277 moveToNextID();
1278 }
1279};
1280
1281// This is useful when building IndexedMaps keyed on virtual registers
1284 unsigned operator()(Register Reg) const { return Reg.virtRegIndex(); }
1285};
1286
1287/// Prints virtual and physical registers with or without a TRI instance.
1288///
1289/// The format is:
1290/// %noreg - NoRegister
1291/// %5 - a virtual register.
1292/// %5:sub_8bit - a virtual register with sub-register index (with TRI).
1293/// %eax - a physical register
1294/// %physreg17 - a physical register when no TRI instance given.
1295///
1296/// Usage: OS << printReg(Reg, TRI, SubRegIdx) << '\n';
1297LLVM_ABI Printable printReg(Register Reg,
1298 const TargetRegisterInfo *TRI = nullptr,
1299 unsigned SubIdx = 0,
1300 const MachineRegisterInfo *MRI = nullptr);
1301
1302/// Create Printable object to print register units on a \ref raw_ostream.
1303///
1304/// Register units are named after their root registers:
1305///
1306/// al - Single root.
1307/// fp0~st7 - Dual roots.
1308///
1309/// Usage: OS << printRegUnit(Unit, TRI) << '\n';
1310LLVM_ABI Printable printRegUnit(MCRegUnit Unit, const TargetRegisterInfo *TRI);
1311
1312/// Create Printable object to print virtual registers and physical
1313/// registers on a \ref raw_ostream.
1314LLVM_ABI Printable printVRegOrUnit(VirtRegOrUnit VRegOrUnit,
1315 const TargetRegisterInfo *TRI);
1316
1317/// Create Printable object to print register classes or register banks
1318/// on a \ref raw_ostream.
1320 const MachineRegisterInfo &RegInfo,
1321 const TargetRegisterInfo *TRI);
1322
1323} // end namespace llvm
1324
1325#endif // LLVM_CODEGEN_TARGETREGISTERINFO_H
MachineInstrBuilder & UseMI
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
A common definition of LaneBitmask for use in TableGen and CodeGen.
Live Register Matrix
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
static StringRef getName(Value *V)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file defines the SmallVector class.
static const TargetRegisterClass * getCommonMinimalPhysRegClass(const TargetRegisterInfo *TRI, MCRegister Reg1, MCRegister Reg2)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
void operator++()
Advance iterator to the next entry.
unsigned getID() const
Returns the current register class ID.
BitMaskClassIterator(const uint32_t *Mask, const TargetRegisterInfo &TRI)
Create a BitMaskClassIterator that visits all the register classes represented by Mask.
bool isValid() const
Returns true if this iterator is still pointing at a valid entry.
DWARF expression.
LiveInterval - This class represents the liveness of a register, or stack slot.
MCRegisterClass - Base class of TargetRegisterClass.
const uint8_t SpillStackID
unsigned getID() const
getID() - Return the register class ID number.
ArrayRef< MCPhysReg > getRegisters() const
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
unsigned getNumSubRegIndices() const
Return the number of sub-register indices understood by the target.
bool regsOverlap(MCRegister RegA, MCRegister RegB) const
Returns true if the two registers are equal or alias each other.
unsigned getNumRegClasses() const
iota_range< MCRegUnit > regunits() const
Returns an iterator range over all regunits.
const MCRegisterClass & getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Machine Value Type.
MachineInstrBundleIterator< MachineInstr > iterator
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
Holds all the information related to register banks.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:30
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
void operator++()
Advance iterator to the next entry.
unsigned getSubReg() const
Returns the current sub-register index.
const uint32_t * getMask() const
Returns the bit mask of register classes that getSubReg() projects into RC.
SuperRegClassIterator(const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, bool IncludeSelf=false)
Create a SuperRegClassIterator that visits all the super-register classes of RC.
bool isValid() const
Returns true if this iterator is still pointing at a valid entry.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual bool isConstantPhysReg(MCRegister PhysReg) const
Returns true if PhysReg is unallocatable and constant throughout the function.
virtual SmallVector< StringLiteral > getVRegFlagsOfReg(Register Reg, const MachineFunction &MF) const
virtual bool isFrameOffsetLegal(const MachineInstr *MI, Register BaseReg, int64_t Offset) const
Determine whether a given base register plus offset immediate is encodable to resolve a frame index.
virtual ArrayRef< MCPhysReg > getRawAllocationOrder(const TargetRegisterClass &RC, const MachineFunction &, bool=false) const
Returns the preferred order for allocating registers from this register class in MF.
vt_iterator legalclasstypes_end(const TargetRegisterClass &RC) const
bool isTypeLegalForClass(const TargetRegisterClass &RC, LLT T) const
Return true if the given TargetRegisterClass is compatible with LLT T.
bool hasRegUnit(MCRegister Reg, MCRegUnit RegUnit) const
Returns true if Reg contains RegUnit.
virtual unsigned getNumRegPressureSets() const =0
Get the number of dimensions of register pressure.
~TargetRegisterInfo() override
unsigned reverseComposeSubRegIndices(unsigned a, unsigned b) const
Return a subregister index that will compose to give you the subregister index.
virtual const int * getRegUnitPressureSets(MCRegUnit RegUnit) const =0
Get the dimensions of register pressure impacted by this register unit.
virtual const TargetRegisterClass * getPhysRegBaseClass(MCRegister Reg) const
Return target defined base register class for a physical register.
virtual bool canRealignStack(const MachineFunction &MF) const
True if the stack can be realigned for the target.
virtual bool isAsmClobberable(const MachineFunction &MF, MCRegister PhysReg) const
Returns false if we can't guarantee that Physreg, specified as an IR asm clobber constraint,...
virtual const TargetRegisterClass * getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const
Returns the largest legal sub-class of RC that supports the sub-register index Idx.
virtual const TargetRegisterClass * getLargestRegClassForRegPressureSet(unsigned Idx) const =0
Get the register class for this pressure set with the largest RegClassWeight::WeightLimit.
const TargetRegisterClass * getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
virtual bool useFPForScavengingIndex(const MachineFunction &MF) const
Returns true if the target wants to use frame pointer based accesses to spill to the scavenger emerge...
virtual const TargetRegisterClass * getCrossCopyRegClass(const TargetRegisterClass *RC) const
Returns a legal register class to copy a register in the specified class to or from.
virtual bool isVirtualFrameRegister(MCRegister Reg) const
Some targets delay assigning the frame until late and use a placeholder to represent it earlier.
virtual bool shouldUseLastChanceRecoloringForVirtReg(const MachineFunction &MF, const LiveInterval &VirtReg) const
Last chance recoloring has a high compile time cost especially for targets with a lot of registers.
virtual bool eliminateFrameIndicesBackwards() const
Process frame indices in reverse block order.
unsigned composeSubRegIndices(unsigned a, unsigned b) const
Return the subregister index you get from composing two subregister indices.
virtual LaneBitmask composeSubRegIndexLaneMaskImpl(unsigned, LaneBitmask) const
Overridden by TableGen in targets that have sub-registers.
virtual bool isIgnoredCVReg(MCRegister LLVMReg) const
virtual bool isGeneralPurposeRegisterClass(const TargetRegisterClass *RC) const
Returns true if RC is a class/subclass of general purpose register.
void markSuperRegs(BitVector &RegisterSet, MCRegister Reg) const
Mark a register and all its aliases as reserved in the given set.
virtual const MCPhysReg * getIPRACSRegs(const MachineFunction *MF) const
Return a null-terminated list of all of the callee-saved registers on this target when IPRA is on.
virtual const uint32_t * getCustomEHPadPreservedMask(const MachineFunction &MF) const
Return a register mask for the registers preserved by the unwinder, or nullptr if no custom mask is n...
virtual float getSpillWeightScaleFactor(const TargetRegisterClass *RC) const
Get the scale factor of spill weight for this register class.
const MVT::SimpleValueType * vt_iterator
virtual bool isUniformReg(const MachineRegisterInfo &MRI, const RegisterBankInfo &RBI, Register Reg) const
Returns true if the register is considered uniform.
TypeSize getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
virtual std::optional< std::string > explainReservedReg(const MachineFunction &MF, MCRegister PhysReg) const
Returns either a string explaining why the given register is reserved for this function,...
virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const
Returns true if the target requires post PEI scavenging of registers for materializing frame index co...
const char * getSubRegIndexName(unsigned SubIdx) const
Return the human-readable symbolic target-specific name for the specified SubRegIndex.
virtual const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const
Return a mask of call-preserved registers for the given calling convention on the current function.
virtual const char * getRegPressureSetName(unsigned Idx) const =0
Get the name of this register unit pressure set.
virtual LaneBitmask reverseComposeSubRegIndexLaneMaskImpl(unsigned, LaneBitmask) const
LaneBitmask getCoveringLanes() const
The lane masks returned by getSubRegIndexLaneMask() above can only be used to determine if sub-regist...
virtual int64_t getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const
Get the offset from the referenced frame index in the instruction, if there is one.
ArrayRef< uint8_t > getRegisterCosts(const MachineFunction &MF) const
Get a list of cost values for all registers that correspond to the index returned by RegisterCostTabl...
virtual bool isGeneralPurposeRegister(const MachineFunction &MF, MCRegister PhysReg) const
Returns true if PhysReg is a general purpose register.
virtual ArrayRef< const uint32_t * > getRegMasks() const =0
Return all the call-preserved register masks defined for this target.
LaneBitmask reverseComposeSubRegIndexLaneMask(unsigned IdxA, LaneBitmask LaneMask) const
Transform a lanemask given for a virtual register to the corresponding lanemask before using subregis...
virtual unsigned getRegPressureSetScore(const MachineFunction &MF, unsigned PSetID) const
Return a heuristic for the machine scheduler to compare the profitability of increasing one register ...
virtual const int * getRegClassPressureSets(const TargetRegisterClass *RC) const =0
Get the dimensions of register pressure impacted by this register class.
virtual unsigned getCSRCostScale(const MachineFunction &MF) const
Scale the CSRFirstUseCost with this number.
virtual const RegClassWeight & getRegClassWeight(const TargetRegisterClass *RC) const =0
Get the weight in units of pressure for this register class.
virtual ArrayRef< MCPhysReg > getIntraCallClobberedRegs(const MachineFunction *MF) const
Return a list of all of the registers which are clobbered "inside" a call to the given function.
virtual bool reverseLocalAssignment() const
Allow the target to reverse allocation order of local live ranges.
virtual bool isNonallocatableRegisterCalleeSave(MCRegister Reg) const
Some targets have non-allocatable registers that aren't technically part of the explicit callee saved...
vt_iterator legalclasstypes_begin(const TargetRegisterClass &RC) const
Loop over all of the value types that can be represented by values in the given register class.
virtual unsigned getRegPressureLimit(const TargetRegisterClass *RC, MachineFunction &MF) const
Return the register pressure "high water mark" for the specific register class.
LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const
Return a bitmask representing the parts of a register that are covered by SubIdx.
virtual const TargetRegisterClass * getMinimalPhysRegClass(MCRegister Reg) const =0
Returns the Register Class of a physical register, picking the smallest register subclass that contai...
bool checkAllSuperRegsMarked(const BitVector &RegisterSet, ArrayRef< MCPhysReg > Exceptions=ArrayRef< MCPhysReg >()) const
Returns true if for every register in the set all super registers are part of the set as well.
virtual int64_t getDwarfRegNumForVirtReg(Register RegNum, bool isEH) const
virtual const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &) const
Returns the largest super class of RC that is legal to use in the current sub-target and has the same...
virtual BitVector getReservedRegs(const MachineFunction &MF) const =0
Returns a bitset indexed by physical register number indicating if a register is a special register t...
const RegClassInfo & getRegClassInfo(const TargetRegisterClass &RC) const
virtual const uint32_t * getNoPreservedMask() const
Return a register mask that clobbers everything.
virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const
Returns true if the live-ins should be tracked after register allocation.
virtual bool isArgumentRegister(const MachineFunction &MF, MCRegister PhysReg) const
Returns true if PhysReg can be used as an argument to a function.
Align getSpillAlign(const TargetRegisterClass &RC) const
Return the minimum required alignment in bytes for a spill slot for a register of this class.
virtual std::optional< uint8_t > getVRegFlagValue(StringRef Name) const
virtual const TargetRegisterClass * getSubRegisterClass(const TargetRegisterClass *SuperRC, unsigned SubRegIdx) const
Returns the register class of all sub-registers of SuperRC obtained by applying the sub-register inde...
virtual unsigned getRegPressureSetLimit(const MachineFunction &MF, unsigned Idx) const =0
Get the register unit pressure limit for this dimension.
virtual bool requiresFrameIndexReplacementScavenging(const MachineFunction &MF) const
Returns true if the target requires using the RegScavenger directly for frame elimination despite usi...
virtual bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const =0
This method must be overriden to eliminate abstract frame indices from instructions which may use the...
virtual unsigned getRegUnitWeight(MCRegUnit RegUnit) const =0
Get the weight in units of pressure for this register unit.
virtual bool requiresRegisterScavenging(const MachineFunction &MF) const
Returns true if the target requires (and can make use of) the register scavenger.
const TargetRegisterClass * getAllocatableClass(const TargetRegisterClass *RC) const
Return the maximal subclass of the given register class that is allocatable or NULL.
LaneBitmask composeSubRegIndexLaneMask(unsigned IdxA, LaneBitmask Mask) const
Transforms a LaneMask computed for one subregister to the lanemask that would have been computed when...
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
virtual bool shouldAnalyzePhysregInMachineLoopInfo(MCRegister R) const
Returns true if MachineLoopInfo should analyze the given physreg for loop invariance.
virtual bool saveScavengerRegister(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, MachineBasicBlock::iterator &UseMI, const TargetRegisterClass *RC, Register Reg) const
Spill the register so it can be used by the register scavenger.
virtual bool shouldRewriteCopySrc(const TargetRegisterClass *DefRC, unsigned DefSubReg, const TargetRegisterClass *SrcRC, unsigned SrcSubReg) const
virtual bool isCallerPreservedPhysReg(MCRegister PhysReg, const MachineFunction &MF) const
Physical registers that may be modified within a function but are guaranteed to be restored before an...
virtual bool hasReservedSpillSlot(const MachineFunction &MF, Register Reg, int &FrameIdx) const
Return true if target has reserved a spill slot in the stack frame of the given function for the spec...
virtual void resolveFrameIndex(MachineInstr &MI, Register BaseReg, int64_t Offset) const
Resolve a frame index operand of an instruction to reference the indicated base register plus offset ...
virtual bool isDivergentRegClass(const TargetRegisterClass *RC) const
Returns true if the register class is considered divergent.
virtual Register materializeFrameBaseRegister(MachineBasicBlock *MBB, int FrameIdx, int64_t Offset) const
Insert defining instruction(s) for a pointer to FrameIdx before insertion point I.
bool regsOverlap(Register RegA, Register RegB) const
Returns true if the two registers are equal or alias each other.
virtual bool shouldRealignStack(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
virtual unsigned getNumSupportedRegs(const MachineFunction &) const
Return the number of registers for the function. (may overestimate)
TargetStackID::Value getSpillStackID(const TargetRegisterClass &RC) const
Return the stack ID for spill slots holding a spilled copy of a register from this class.
virtual unsigned getCSRCost() const
FIXME: We should deprecate this usage.
virtual ArrayRef< const char * > getRegMaskNames() const =0
virtual bool isFixedRegister(const MachineFunction &MF, MCRegister PhysReg) const
Returns true if PhysReg is a fixed register.
virtual const TargetRegisterClass * getConstrainedRegClassForReg(Register Reg, const MachineRegisterInfo &MRI) const
const TargetRegisterClass * findCommonRegClass(const TargetRegisterClass *DefRC, unsigned DefSubReg, const TargetRegisterClass *SrcRC, unsigned SrcSubReg) const
Find a common register class that can accomodate both the source and destination operands of a copy-l...
unsigned getSpillSize(const TargetRegisterClass &RC) const
Return the size in bytes of the stack slot allocated to hold a spilled copy of a register from class ...
virtual StringRef getRegAsmName(MCRegister Reg) const
Return the assembly name for Reg.
virtual const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const =0
Return a null-terminated list of all of the callee-saved registers on this target.
TargetRegisterInfo(const TargetRegisterInfoDesc *ID, const char *SubRegIndexStrings, ArrayRef< uint32_t > SubRegIndexNameOffsets, const SubRegCoveredBits *SubRegIdxRanges, const LaneBitmask *SubRegIndexLaneMasks, LaneBitmask CoveringLanes, const RegClassInfo *const RCInfos, const MVT::SimpleValueType *const RCVTLists, unsigned Mode=0)
bool isTypeLegalForClass(const TargetRegisterClass &RC, MVT T) const
Return true if the given TargetRegisterClass has the ValueType T.
virtual unsigned getRegisterCostTableIndex(const MachineFunction &MF) const
Return the register cost table index.
virtual bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const
Returns true if the instruction's frame index reference would be better served by a base register oth...
virtual unsigned getCSRFirstUseCost(const MachineFunction &MF) const
Allow the target to override the cost of using a callee-saved register for the first time.
virtual unsigned composeSubRegIndicesImpl(unsigned, unsigned) const
Overridden by TableGen in targets that have sub-registers.
virtual unsigned reverseComposeSubRegIndicesImpl(unsigned, unsigned) const
Overridden by TableGen in targets that have sub-registers.
virtual void adjustStackMapLiveOutMask(uint32_t *Mask) const
Prior to adding the live-out mask to a stackmap or patchpoint instruction, provide the target the opp...
bool isSubRegValidForRegClass(const TargetRegisterClass *RC, unsigned Idx) const
Returns true if sub-register Idx can be used with register class RC.
virtual bool isInlineAsmReadOnlyReg(const MachineFunction &MF, MCRegister PhysReg) const
Returns true if PhysReg cannot be written to in inline asm statements.
virtual bool shouldCoalesce(MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, const TargetRegisterClass *DstRC, unsigned DstSubReg, const TargetRegisterClass *NewRC, LiveIntervals &LIS) const
Subtarget Hooks.
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
virtual bool regClassPriorityTrumpsGlobalness(const MachineFunction &MF) const
When prioritizing live ranges in register allocation, if this hook returns true then the AllocationPr...
bool isInAllocatableClass(MCRegister RegNo) const
Return true if the register is in the allocation of any register class.
BitVector getAllocatableSet(const MachineFunction &MF, const TargetRegisterClass *RC=nullptr) const
Returns a bitset indexed by register number indicating if a register is allocatable or not.
virtual void updateRegAllocHint(Register Reg, Register NewReg, MachineFunction &MF) const
A callback to allow target a chance to update register allocation hints when a register is "changed" ...
virtual bool requiresVirtualBaseRegisters(const MachineFunction &MF) const
Returns true if the target wants the LocalStackAllocation pass to be run and virtual base registers u...
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:339
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#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
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
LLVM_ABI Printable printRegUnit(MCRegUnit Unit, const TargetRegisterInfo *TRI)
Create Printable object to print register units on a raw_ostream.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI Printable printRegClassOrBank(Register Reg, const MachineRegisterInfo &RegInfo, const TargetRegisterInfo *TRI)
Create Printable object to print register classes or register banks on a raw_ostream.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printVRegOrUnit(VirtRegOrUnit VRegOrUnit, const TargetRegisterInfo *TRI)
Create Printable object to print virtual registers and physical registers on a raw_ostream.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Each TargetRegisterClass has a per register weight, and weight limit which must be less than the limi...
Extra information, not in MCRegisterDesc, about registers.
SubRegCoveredBits - Emitted by tablegen: bit range covered by a subreg index, -1 in any being invalid...
unsigned operator()(Register Reg) const