1 //===- llvm/CodeGen/MachineRegisterInfo.h -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachineRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
15 #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/IndexedMap.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
26 #include "llvm/CodeGen/LowLevelType.h"
27 #include "llvm/CodeGen/MachineBasicBlock.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBundle.h"
30 #include "llvm/CodeGen/MachineOperand.h"
31 #include "llvm/CodeGen/TargetRegisterInfo.h"
32 #include "llvm/CodeGen/TargetSubtargetInfo.h"
33 #include "llvm/MC/LaneBitmask.h"
46 /// Convenient type to represent either a register class or a register bank.
47 using RegClassOrRegBank =
48 PointerUnion<const TargetRegisterClass *, const RegisterBank *>;
50 /// MachineRegisterInfo - Keep track of information for virtual and physical
51 /// registers, including vreg register classes, use/def chains for registers,
53 class MachineRegisterInfo {
56 virtual void anchor();
59 virtual ~Delegate() = default;
61 virtual void MRI_NoteNewVirtualRegister(unsigned Reg) = 0;
66 Delegate *TheDelegate = nullptr;
68 /// True if subregister liveness is tracked.
69 const bool TracksSubRegLiveness;
71 /// VRegInfo - Information we keep for each virtual register.
73 /// Each element in this list contains the register class of the vreg and the
74 /// start of the use/def list for the register.
75 IndexedMap<std::pair<RegClassOrRegBank, MachineOperand *>,
79 /// Map for recovering vreg name from vreg number.
80 /// This map is used by the MIR Printer.
81 IndexedMap<std::string, VirtReg2IndexFunctor> VReg2Name;
83 /// StringSet that is used to unique vreg names.
84 StringSet<> VRegNames;
86 /// The flag is true upon \p UpdatedCSRs initialization
87 /// and false otherwise.
88 bool IsUpdatedCSRsInitialized;
90 /// Contains the updated callee saved register list.
91 /// As opposed to the static list defined in register info,
92 /// all registers that were disabled are removed from the list.
93 SmallVector<MCPhysReg, 16> UpdatedCSRs;
95 /// RegAllocHints - This vector records register allocation hints for
96 /// virtual registers. For each virtual register, it keeps a pair of hint
97 /// type and hints vector making up the allocation hints. Only the first
98 /// hint may be target specific, and in that case this is reflected by the
99 /// first member of the pair being non-zero. If the hinted register is
100 /// virtual, it means the allocator should prefer the physical register
101 /// allocated to it if any.
102 IndexedMap<std::pair<unsigned, SmallVector<unsigned, 4>>,
103 VirtReg2IndexFunctor> RegAllocHints;
105 /// PhysRegUseDefLists - This is an array of the head of the use/def list for
106 /// physical registers.
107 std::unique_ptr<MachineOperand *[]> PhysRegUseDefLists;
109 /// getRegUseDefListHead - Return the head pointer for the register use/def
110 /// list for the specified virtual or physical register.
111 MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
112 if (TargetRegisterInfo::isVirtualRegister(RegNo))
113 return VRegInfo[RegNo].second;
114 return PhysRegUseDefLists[RegNo];
117 MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
118 if (TargetRegisterInfo::isVirtualRegister(RegNo))
119 return VRegInfo[RegNo].second;
120 return PhysRegUseDefLists[RegNo];
123 /// Get the next element in the use-def chain.
124 static MachineOperand *getNextOperandForReg(const MachineOperand *MO) {
125 assert(MO && MO->isReg() && "This is not a register operand!");
126 return MO->Contents.Reg.Next;
129 /// UsedPhysRegMask - Additional used physregs including aliases.
130 /// This bit vector represents all the registers clobbered by function calls.
131 BitVector UsedPhysRegMask;
133 /// ReservedRegs - This is a bit vector of reserved registers. The target
134 /// may change its mind about which registers should be reserved. This
135 /// vector is the frozen set of reserved registers when register allocation
137 BitVector ReservedRegs;
139 using VRegToTypeMap = IndexedMap<LLT, VirtReg2IndexFunctor>;
140 /// Map generic virtual registers to their low-level type.
141 VRegToTypeMap VRegToType;
143 /// Keep track of the physical registers that are live in to the function.
144 /// Live in values are typically arguments in registers. LiveIn values are
145 /// allowed to have virtual registers associated with them, stored in the
147 std::vector<std::pair<unsigned, unsigned>> LiveIns;
150 explicit MachineRegisterInfo(MachineFunction *MF);
151 MachineRegisterInfo(const MachineRegisterInfo &) = delete;
152 MachineRegisterInfo &operator=(const MachineRegisterInfo &) = delete;
154 const TargetRegisterInfo *getTargetRegisterInfo() const {
155 return MF->getSubtarget().getRegisterInfo();
158 void resetDelegate(Delegate *delegate) {
159 // Ensure another delegate does not take over unless the current
160 // delegate first unattaches itself. If we ever need to multicast
161 // notifications, we will need to change to using a list.
162 assert(TheDelegate == delegate &&
163 "Only the current delegate can perform reset!");
164 TheDelegate = nullptr;
167 void setDelegate(Delegate *delegate) {
168 assert(delegate && !TheDelegate &&
169 "Attempted to set delegate to null, or to change it without "
170 "first resetting it!");
172 TheDelegate = delegate;
175 //===--------------------------------------------------------------------===//
177 //===--------------------------------------------------------------------===//
179 // isSSA - Returns true when the machine function is in SSA form. Early
180 // passes require the machine function to be in SSA form where every virtual
181 // register has a single defining instruction.
183 // The TwoAddressInstructionPass and PHIElimination passes take the machine
184 // function out of SSA form when they introduce multiple defs per virtual
187 return MF->getProperties().hasProperty(
188 MachineFunctionProperties::Property::IsSSA);
191 // leaveSSA - Indicates that the machine function is no longer in SSA form.
193 MF->getProperties().reset(MachineFunctionProperties::Property::IsSSA);
196 /// tracksLiveness - Returns true when tracking register liveness accurately.
197 /// (see MachineFUnctionProperties::Property description for details)
198 bool tracksLiveness() const {
199 return MF->getProperties().hasProperty(
200 MachineFunctionProperties::Property::TracksLiveness);
203 /// invalidateLiveness - Indicates that register liveness is no longer being
204 /// tracked accurately.
206 /// This should be called by late passes that invalidate the liveness
208 void invalidateLiveness() {
209 MF->getProperties().reset(
210 MachineFunctionProperties::Property::TracksLiveness);
213 /// Returns true if liveness for register class @p RC should be tracked at
214 /// the subregister level.
215 bool shouldTrackSubRegLiveness(const TargetRegisterClass &RC) const {
216 return subRegLivenessEnabled() && RC.HasDisjunctSubRegs;
218 bool shouldTrackSubRegLiveness(unsigned VReg) const {
219 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Must pass a VReg");
220 return shouldTrackSubRegLiveness(*getRegClass(VReg));
222 bool subRegLivenessEnabled() const {
223 return TracksSubRegLiveness;
226 //===--------------------------------------------------------------------===//
228 //===--------------------------------------------------------------------===//
230 /// Returns true if the updated CSR list was initialized and false otherwise.
231 bool isUpdatedCSRsInitialized() const { return IsUpdatedCSRsInitialized; }
233 /// Disables the register from the list of CSRs.
234 /// I.e. the register will not appear as part of the CSR mask.
235 /// \see UpdatedCalleeSavedRegs.
236 void disableCalleeSavedRegister(unsigned Reg);
238 /// Returns list of callee saved registers.
239 /// The function returns the updated CSR list (after taking into account
240 /// registers that are disabled from the CSR list).
241 const MCPhysReg *getCalleeSavedRegs() const;
243 /// Sets the updated Callee Saved Registers list.
244 /// Notice that it will override ant previously disabled/saved CSRs.
245 void setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs);
247 // Strictly for use by MachineInstr.cpp.
248 void addRegOperandToUseList(MachineOperand *MO);
250 // Strictly for use by MachineInstr.cpp.
251 void removeRegOperandFromUseList(MachineOperand *MO);
253 // Strictly for use by MachineInstr.cpp.
254 void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
256 /// Verify the sanity of the use list for Reg.
257 void verifyUseList(unsigned Reg) const;
259 /// Verify the use list of all registers.
260 void verifyUseLists() const;
262 /// reg_begin/reg_end - Provide iteration support to walk over all definitions
263 /// and uses of a register within the MachineFunction that corresponds to this
264 /// MachineRegisterInfo object.
265 template<bool Uses, bool Defs, bool SkipDebug,
266 bool ByOperand, bool ByInstr, bool ByBundle>
267 class defusechain_iterator;
268 template<bool Uses, bool Defs, bool SkipDebug,
269 bool ByOperand, bool ByInstr, bool ByBundle>
270 class defusechain_instr_iterator;
272 // Make it a friend so it can access getNextOperandForReg().
273 template<bool, bool, bool, bool, bool, bool>
274 friend class defusechain_iterator;
275 template<bool, bool, bool, bool, bool, bool>
276 friend class defusechain_instr_iterator;
278 /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
281 defusechain_iterator<true, true, false, true, false, false>;
282 reg_iterator reg_begin(unsigned RegNo) const {
283 return reg_iterator(getRegUseDefListHead(RegNo));
285 static reg_iterator reg_end() { return reg_iterator(nullptr); }
287 inline iterator_range<reg_iterator> reg_operands(unsigned Reg) const {
288 return make_range(reg_begin(Reg), reg_end());
291 /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses
292 /// of the specified register, stepping by MachineInstr.
293 using reg_instr_iterator =
294 defusechain_instr_iterator<true, true, false, false, true, false>;
295 reg_instr_iterator reg_instr_begin(unsigned RegNo) const {
296 return reg_instr_iterator(getRegUseDefListHead(RegNo));
298 static reg_instr_iterator reg_instr_end() {
299 return reg_instr_iterator(nullptr);
302 inline iterator_range<reg_instr_iterator>
303 reg_instructions(unsigned Reg) const {
304 return make_range(reg_instr_begin(Reg), reg_instr_end());
307 /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses
308 /// of the specified register, stepping by bundle.
309 using reg_bundle_iterator =
310 defusechain_instr_iterator<true, true, false, false, false, true>;
311 reg_bundle_iterator reg_bundle_begin(unsigned RegNo) const {
312 return reg_bundle_iterator(getRegUseDefListHead(RegNo));
314 static reg_bundle_iterator reg_bundle_end() {
315 return reg_bundle_iterator(nullptr);
318 inline iterator_range<reg_bundle_iterator> reg_bundles(unsigned Reg) const {
319 return make_range(reg_bundle_begin(Reg), reg_bundle_end());
322 /// reg_empty - Return true if there are no instructions using or defining the
323 /// specified register (it may be live-in).
324 bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
326 /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
327 /// of the specified register, skipping those marked as Debug.
328 using reg_nodbg_iterator =
329 defusechain_iterator<true, true, true, true, false, false>;
330 reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const {
331 return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
333 static reg_nodbg_iterator reg_nodbg_end() {
334 return reg_nodbg_iterator(nullptr);
337 inline iterator_range<reg_nodbg_iterator>
338 reg_nodbg_operands(unsigned Reg) const {
339 return make_range(reg_nodbg_begin(Reg), reg_nodbg_end());
342 /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk
343 /// all defs and uses of the specified register, stepping by MachineInstr,
344 /// skipping those marked as Debug.
345 using reg_instr_nodbg_iterator =
346 defusechain_instr_iterator<true, true, true, false, true, false>;
347 reg_instr_nodbg_iterator reg_instr_nodbg_begin(unsigned RegNo) const {
348 return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
350 static reg_instr_nodbg_iterator reg_instr_nodbg_end() {
351 return reg_instr_nodbg_iterator(nullptr);
354 inline iterator_range<reg_instr_nodbg_iterator>
355 reg_nodbg_instructions(unsigned Reg) const {
356 return make_range(reg_instr_nodbg_begin(Reg), reg_instr_nodbg_end());
359 /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk
360 /// all defs and uses of the specified register, stepping by bundle,
361 /// skipping those marked as Debug.
362 using reg_bundle_nodbg_iterator =
363 defusechain_instr_iterator<true, true, true, false, false, true>;
364 reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(unsigned RegNo) const {
365 return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
367 static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() {
368 return reg_bundle_nodbg_iterator(nullptr);
371 inline iterator_range<reg_bundle_nodbg_iterator>
372 reg_nodbg_bundles(unsigned Reg) const {
373 return make_range(reg_bundle_nodbg_begin(Reg), reg_bundle_nodbg_end());
376 /// reg_nodbg_empty - Return true if the only instructions using or defining
377 /// Reg are Debug instructions.
378 bool reg_nodbg_empty(unsigned RegNo) const {
379 return reg_nodbg_begin(RegNo) == reg_nodbg_end();
382 /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
384 defusechain_iterator<false, true, false, true, false, false>;
385 def_iterator def_begin(unsigned RegNo) const {
386 return def_iterator(getRegUseDefListHead(RegNo));
388 static def_iterator def_end() { return def_iterator(nullptr); }
390 inline iterator_range<def_iterator> def_operands(unsigned Reg) const {
391 return make_range(def_begin(Reg), def_end());
394 /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the
395 /// specified register, stepping by MachineInst.
396 using def_instr_iterator =
397 defusechain_instr_iterator<false, true, false, false, true, false>;
398 def_instr_iterator def_instr_begin(unsigned RegNo) const {
399 return def_instr_iterator(getRegUseDefListHead(RegNo));
401 static def_instr_iterator def_instr_end() {
402 return def_instr_iterator(nullptr);
405 inline iterator_range<def_instr_iterator>
406 def_instructions(unsigned Reg) const {
407 return make_range(def_instr_begin(Reg), def_instr_end());
410 /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the
411 /// specified register, stepping by bundle.
412 using def_bundle_iterator =
413 defusechain_instr_iterator<false, true, false, false, false, true>;
414 def_bundle_iterator def_bundle_begin(unsigned RegNo) const {
415 return def_bundle_iterator(getRegUseDefListHead(RegNo));
417 static def_bundle_iterator def_bundle_end() {
418 return def_bundle_iterator(nullptr);
421 inline iterator_range<def_bundle_iterator> def_bundles(unsigned Reg) const {
422 return make_range(def_bundle_begin(Reg), def_bundle_end());
425 /// def_empty - Return true if there are no instructions defining the
426 /// specified register (it may be live-in).
427 bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
429 StringRef getVRegName(unsigned Reg) const {
430 return VReg2Name.inBounds(Reg) ? StringRef(VReg2Name[Reg]) : "";
433 void insertVRegByName(StringRef Name, unsigned Reg) {
434 assert((Name.empty() || VRegNames.find(Name) == VRegNames.end()) &&
435 "Named VRegs Must be Unique.");
437 VRegNames.insert(Name);
439 VReg2Name[Reg] = Name.str();
443 /// Return true if there is exactly one operand defining the specified
445 bool hasOneDef(unsigned RegNo) const {
446 def_iterator DI = def_begin(RegNo);
449 return ++DI == def_end();
452 /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
454 defusechain_iterator<true, false, false, true, false, false>;
455 use_iterator use_begin(unsigned RegNo) const {
456 return use_iterator(getRegUseDefListHead(RegNo));
458 static use_iterator use_end() { return use_iterator(nullptr); }
460 inline iterator_range<use_iterator> use_operands(unsigned Reg) const {
461 return make_range(use_begin(Reg), use_end());
464 /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the
465 /// specified register, stepping by MachineInstr.
466 using use_instr_iterator =
467 defusechain_instr_iterator<true, false, false, false, true, false>;
468 use_instr_iterator use_instr_begin(unsigned RegNo) const {
469 return use_instr_iterator(getRegUseDefListHead(RegNo));
471 static use_instr_iterator use_instr_end() {
472 return use_instr_iterator(nullptr);
475 inline iterator_range<use_instr_iterator>
476 use_instructions(unsigned Reg) const {
477 return make_range(use_instr_begin(Reg), use_instr_end());
480 /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the
481 /// specified register, stepping by bundle.
482 using use_bundle_iterator =
483 defusechain_instr_iterator<true, false, false, false, false, true>;
484 use_bundle_iterator use_bundle_begin(unsigned RegNo) const {
485 return use_bundle_iterator(getRegUseDefListHead(RegNo));
487 static use_bundle_iterator use_bundle_end() {
488 return use_bundle_iterator(nullptr);
491 inline iterator_range<use_bundle_iterator> use_bundles(unsigned Reg) const {
492 return make_range(use_bundle_begin(Reg), use_bundle_end());
495 /// use_empty - Return true if there are no instructions using the specified
497 bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
499 /// hasOneUse - Return true if there is exactly one instruction using the
500 /// specified register.
501 bool hasOneUse(unsigned RegNo) const {
502 use_iterator UI = use_begin(RegNo);
505 return ++UI == use_end();
508 /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
509 /// specified register, skipping those marked as Debug.
510 using use_nodbg_iterator =
511 defusechain_iterator<true, false, true, true, false, false>;
512 use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const {
513 return use_nodbg_iterator(getRegUseDefListHead(RegNo));
515 static use_nodbg_iterator use_nodbg_end() {
516 return use_nodbg_iterator(nullptr);
519 inline iterator_range<use_nodbg_iterator>
520 use_nodbg_operands(unsigned Reg) const {
521 return make_range(use_nodbg_begin(Reg), use_nodbg_end());
524 /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk
525 /// all uses of the specified register, stepping by MachineInstr, skipping
526 /// those marked as Debug.
527 using use_instr_nodbg_iterator =
528 defusechain_instr_iterator<true, false, true, false, true, false>;
529 use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const {
530 return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
532 static use_instr_nodbg_iterator use_instr_nodbg_end() {
533 return use_instr_nodbg_iterator(nullptr);
536 inline iterator_range<use_instr_nodbg_iterator>
537 use_nodbg_instructions(unsigned Reg) const {
538 return make_range(use_instr_nodbg_begin(Reg), use_instr_nodbg_end());
541 /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk
542 /// all uses of the specified register, stepping by bundle, skipping
543 /// those marked as Debug.
544 using use_bundle_nodbg_iterator =
545 defusechain_instr_iterator<true, false, true, false, false, true>;
546 use_bundle_nodbg_iterator use_bundle_nodbg_begin(unsigned RegNo) const {
547 return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
549 static use_bundle_nodbg_iterator use_bundle_nodbg_end() {
550 return use_bundle_nodbg_iterator(nullptr);
553 inline iterator_range<use_bundle_nodbg_iterator>
554 use_nodbg_bundles(unsigned Reg) const {
555 return make_range(use_bundle_nodbg_begin(Reg), use_bundle_nodbg_end());
558 /// use_nodbg_empty - Return true if there are no non-Debug instructions
559 /// using the specified register.
560 bool use_nodbg_empty(unsigned RegNo) const {
561 return use_nodbg_begin(RegNo) == use_nodbg_end();
564 /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
565 /// instruction using the specified register.
566 bool hasOneNonDBGUse(unsigned RegNo) const;
568 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
569 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
570 /// except that it also changes any definitions of the register as well.
572 /// Note that it is usually necessary to first constrain ToReg's register
573 /// class and register bank to match the FromReg constraints using one of the
576 /// constrainRegClass(ToReg, getRegClass(FromReg))
577 /// constrainRegAttrs(ToReg, FromReg)
578 /// RegisterBankInfo::constrainGenericRegister(ToReg,
579 /// *MRI.getRegClass(FromReg), MRI)
581 /// These functions will return a falsy result if the virtual registers have
582 /// incompatible constraints.
584 /// Note that if ToReg is a physical register the function will replace and
585 /// apply sub registers to ToReg in order to obtain a final/proper physical
587 void replaceRegWith(unsigned FromReg, unsigned ToReg);
589 /// getVRegDef - Return the machine instr that defines the specified virtual
590 /// register or null if none is found. This assumes that the code is in SSA
591 /// form, so there should only be one definition.
592 MachineInstr *getVRegDef(unsigned Reg) const;
594 /// getUniqueVRegDef - Return the unique machine instr that defines the
595 /// specified virtual register or null if none is found. If there are
596 /// multiple definitions or no definition, return null.
597 MachineInstr *getUniqueVRegDef(unsigned Reg) const;
599 /// clearKillFlags - Iterate over all the uses of the given register and
600 /// clear the kill flag from the MachineOperand. This function is used by
601 /// optimization passes which extend register lifetimes and need only
602 /// preserve conservative kill flag information.
603 void clearKillFlags(unsigned Reg) const;
605 void dumpUses(unsigned RegNo) const;
607 /// Returns true if PhysReg is unallocatable and constant throughout the
608 /// function. Writing to a constant register has no effect.
609 bool isConstantPhysReg(unsigned PhysReg) const;
611 /// Returns true if either isConstantPhysReg or TRI->isCallerPreservedPhysReg
612 /// returns true. This is a utility member function.
613 bool isCallerPreservedOrConstPhysReg(unsigned PhysReg) const;
615 /// Get an iterator over the pressure sets affected by the given physical or
616 /// virtual register. If RegUnit is physical, it must be a register unit (from
617 /// MCRegUnitIterator).
618 PSetIterator getPressureSets(unsigned RegUnit) const;
620 //===--------------------------------------------------------------------===//
621 // Virtual Register Info
622 //===--------------------------------------------------------------------===//
624 /// Return the register class of the specified virtual register.
625 /// This shouldn't be used directly unless \p Reg has a register class.
626 /// \see getRegClassOrNull when this might happen.
627 const TargetRegisterClass *getRegClass(unsigned Reg) const {
628 assert(VRegInfo[Reg].first.is<const TargetRegisterClass *>() &&
629 "Register class not set, wrong accessor");
630 return VRegInfo[Reg].first.get<const TargetRegisterClass *>();
633 /// Return the register class of \p Reg, or null if Reg has not been assigned
634 /// a register class yet.
636 /// \note A null register class can only happen when these two
637 /// conditions are met:
638 /// 1. Generic virtual registers are created.
639 /// 2. The machine function has not completely been through the
640 /// instruction selection process.
641 /// None of this condition is possible without GlobalISel for now.
642 /// In other words, if GlobalISel is not used or if the query happens after
643 /// the select pass, using getRegClass is safe.
644 const TargetRegisterClass *getRegClassOrNull(unsigned Reg) const {
645 const RegClassOrRegBank &Val = VRegInfo[Reg].first;
646 return Val.dyn_cast<const TargetRegisterClass *>();
649 /// Return the register bank of \p Reg, or null if Reg has not been assigned
650 /// a register bank or has been assigned a register class.
651 /// \note It is possible to get the register bank from the register class via
652 /// RegisterBankInfo::getRegBankFromRegClass.
653 const RegisterBank *getRegBankOrNull(unsigned Reg) const {
654 const RegClassOrRegBank &Val = VRegInfo[Reg].first;
655 return Val.dyn_cast<const RegisterBank *>();
658 /// Return the register bank or register class of \p Reg.
659 /// \note Before the register bank gets assigned (i.e., before the
660 /// RegBankSelect pass) \p Reg may not have either.
661 const RegClassOrRegBank &getRegClassOrRegBank(unsigned Reg) const {
662 return VRegInfo[Reg].first;
665 /// setRegClass - Set the register class of the specified virtual register.
666 void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
668 /// Set the register bank to \p RegBank for \p Reg.
669 void setRegBank(unsigned Reg, const RegisterBank &RegBank);
671 void setRegClassOrRegBank(unsigned Reg,
672 const RegClassOrRegBank &RCOrRB){
673 VRegInfo[Reg].first = RCOrRB;
676 /// constrainRegClass - Constrain the register class of the specified virtual
677 /// register to be a common subclass of RC and the current register class,
678 /// but only if the new class has at least MinNumRegs registers. Return the
679 /// new register class, or NULL if no such class exists.
680 /// This should only be used when the constraint is known to be trivial, like
681 /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
683 /// \note Assumes that the register has a register class assigned.
684 /// Use RegisterBankInfo::constrainGenericRegister in GlobalISel's
685 /// InstructionSelect pass and constrainRegAttrs in every other pass,
686 /// including non-select passes of GlobalISel, instead.
687 const TargetRegisterClass *constrainRegClass(unsigned Reg,
688 const TargetRegisterClass *RC,
689 unsigned MinNumRegs = 0);
691 /// Constrain the register class or the register bank of the virtual register
692 /// \p Reg (and low-level type) to be a common subclass or a common bank of
693 /// both registers provided respectively (and a common low-level type). Do
694 /// nothing if any of the attributes (classes, banks, or low-level types) of
695 /// the registers are deemed incompatible, or if the resulting register will
696 /// have a class smaller than before and of size less than \p MinNumRegs.
697 /// Return true if such register attributes exist, false otherwise.
699 /// \note Use this method instead of constrainRegClass and
700 /// RegisterBankInfo::constrainGenericRegister everywhere but SelectionDAG
701 /// ISel / FastISel and GlobalISel's InstructionSelect pass respectively.
702 bool constrainRegAttrs(unsigned Reg, unsigned ConstrainingReg,
703 unsigned MinNumRegs = 0);
705 /// recomputeRegClass - Try to find a legal super-class of Reg's register
706 /// class that still satisfies the constraints from the instructions using
707 /// Reg. Returns true if Reg was upgraded.
709 /// This method can be used after constraints have been removed from a
710 /// virtual register, for example after removing instructions or splitting
712 bool recomputeRegClass(unsigned Reg);
714 /// createVirtualRegister - Create and return a new virtual register in the
715 /// function with the specified register class.
716 unsigned createVirtualRegister(const TargetRegisterClass *RegClass,
717 StringRef Name = "");
719 /// Create and return a new virtual register in the function with the same
720 /// attributes as the given register.
721 unsigned cloneVirtualRegister(unsigned VReg, StringRef Name = "");
723 /// Get the low-level type of \p Reg or LLT{} if Reg is not a generic
724 /// (target independent) virtual register.
725 LLT getType(unsigned Reg) const {
726 if (TargetRegisterInfo::isVirtualRegister(Reg) && VRegToType.inBounds(Reg))
727 return VRegToType[Reg];
731 /// Set the low-level type of \p VReg to \p Ty.
732 void setType(unsigned VReg, LLT Ty);
734 /// Create and return a new generic virtual register with low-level
736 unsigned createGenericVirtualRegister(LLT Ty, StringRef Name = "");
738 /// Remove all types associated to virtual registers (after instruction
739 /// selection and constraining of all generic virtual registers).
740 void clearVirtRegTypes();
742 /// Creates a new virtual register that has no register class, register bank
743 /// or size assigned yet. This is only allowed to be used
744 /// temporarily while constructing machine instructions. Most operations are
745 /// undefined on an incomplete register until one of setRegClass(),
746 /// setRegBank() or setSize() has been called on it.
747 unsigned createIncompleteVirtualRegister(StringRef Name = "");
749 /// getNumVirtRegs - Return the number of virtual registers created.
750 unsigned getNumVirtRegs() const { return VRegInfo.size(); }
752 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
753 void clearVirtRegs();
755 /// setRegAllocationHint - Specify a register allocation hint for the
756 /// specified virtual register. This is typically used by target, and in case
757 /// of an earlier hint it will be overwritten.
758 void setRegAllocationHint(unsigned VReg, unsigned Type, unsigned PrefReg) {
759 assert(TargetRegisterInfo::isVirtualRegister(VReg));
760 RegAllocHints[VReg].first = Type;
761 RegAllocHints[VReg].second.clear();
762 RegAllocHints[VReg].second.push_back(PrefReg);
765 /// addRegAllocationHint - Add a register allocation hint to the hints
767 void addRegAllocationHint(unsigned VReg, unsigned PrefReg) {
768 assert(TargetRegisterInfo::isVirtualRegister(VReg));
769 RegAllocHints[VReg].second.push_back(PrefReg);
772 /// Specify the preferred (target independent) register allocation hint for
773 /// the specified virtual register.
774 void setSimpleHint(unsigned VReg, unsigned PrefReg) {
775 setRegAllocationHint(VReg, /*Type=*/0, PrefReg);
778 void clearSimpleHint(unsigned VReg) {
779 assert (RegAllocHints[VReg].first == 0 &&
780 "Expected to clear a non-target hint!");
781 RegAllocHints[VReg].second.clear();
784 /// getRegAllocationHint - Return the register allocation hint for the
785 /// specified virtual register. If there are many hints, this returns the
786 /// one with the greatest weight.
787 std::pair<unsigned, unsigned>
788 getRegAllocationHint(unsigned VReg) const {
789 assert(TargetRegisterInfo::isVirtualRegister(VReg));
790 unsigned BestHint = (RegAllocHints[VReg].second.size() ?
791 RegAllocHints[VReg].second[0] : 0);
792 return std::pair<unsigned, unsigned>(RegAllocHints[VReg].first, BestHint);
795 /// getSimpleHint - same as getRegAllocationHint except it will only return
796 /// a target independent hint.
797 unsigned getSimpleHint(unsigned VReg) const {
798 assert(TargetRegisterInfo::isVirtualRegister(VReg));
799 std::pair<unsigned, unsigned> Hint = getRegAllocationHint(VReg);
800 return Hint.first ? 0 : Hint.second;
803 /// getRegAllocationHints - Return a reference to the vector of all
804 /// register allocation hints for VReg.
805 const std::pair<unsigned, SmallVector<unsigned, 4>>
806 &getRegAllocationHints(unsigned VReg) const {
807 assert(TargetRegisterInfo::isVirtualRegister(VReg));
808 return RegAllocHints[VReg];
811 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
812 /// specified register as undefined which causes the DBG_VALUE to be
813 /// deleted during LiveDebugVariables analysis.
814 void markUsesInDebugValueAsUndef(unsigned Reg) const;
816 /// Return true if the specified register is modified in this function.
817 /// This checks that no defining machine operands exist for the register or
818 /// any of its aliases. Definitions found on functions marked noreturn are
819 /// ignored, to consider them pass 'true' for optional parameter
820 /// SkipNoReturnDef. The register is also considered modified when it is set
821 /// in the UsedPhysRegMask.
822 bool isPhysRegModified(unsigned PhysReg, bool SkipNoReturnDef = false) const;
824 /// Return true if the specified register is modified or read in this
825 /// function. This checks that no machine operands exist for the register or
826 /// any of its aliases. The register is also considered used when it is set
827 /// in the UsedPhysRegMask.
828 bool isPhysRegUsed(unsigned PhysReg) const;
830 /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
831 /// This corresponds to the bit mask attached to register mask operands.
832 void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
833 UsedPhysRegMask.setBitsNotInMask(RegMask);
836 const BitVector &getUsedPhysRegsMask() const { return UsedPhysRegMask; }
838 //===--------------------------------------------------------------------===//
839 // Reserved Register Info
840 //===--------------------------------------------------------------------===//
842 // The set of reserved registers must be invariant during register
843 // allocation. For example, the target cannot suddenly decide it needs a
844 // frame pointer when the register allocator has already used the frame
845 // pointer register for something else.
847 // These methods can be used by target hooks like hasFP() to avoid changing
848 // the reserved register set during register allocation.
850 /// freezeReservedRegs - Called by the register allocator to freeze the set
851 /// of reserved registers before allocation begins.
852 void freezeReservedRegs(const MachineFunction&);
854 /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
855 /// to ensure the set of reserved registers stays constant.
856 bool reservedRegsFrozen() const {
857 return !ReservedRegs.empty();
860 /// canReserveReg - Returns true if PhysReg can be used as a reserved
861 /// register. Any register can be reserved before freezeReservedRegs() is
863 bool canReserveReg(unsigned PhysReg) const {
864 return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
867 /// getReservedRegs - Returns a reference to the frozen set of reserved
868 /// registers. This method should always be preferred to calling
869 /// TRI::getReservedRegs() when possible.
870 const BitVector &getReservedRegs() const {
871 assert(reservedRegsFrozen() &&
872 "Reserved registers haven't been frozen yet. "
873 "Use TRI::getReservedRegs().");
877 /// isReserved - Returns true when PhysReg is a reserved register.
879 /// Reserved registers may belong to an allocatable register class, but the
880 /// target has explicitly requested that they are not used.
881 bool isReserved(unsigned PhysReg) const {
882 return getReservedRegs().test(PhysReg);
885 /// Returns true when the given register unit is considered reserved.
887 /// Register units are considered reserved when for at least one of their
888 /// root registers, the root register and all super registers are reserved.
889 /// This currently iterates the register hierarchy and may be slower than
891 bool isReservedRegUnit(unsigned Unit) const;
893 /// isAllocatable - Returns true when PhysReg belongs to an allocatable
894 /// register class and it hasn't been reserved.
896 /// Allocatable registers may show up in the allocation order of some virtual
897 /// register, so a register allocator needs to track its liveness and
899 bool isAllocatable(unsigned PhysReg) const {
900 return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) &&
901 !isReserved(PhysReg);
904 //===--------------------------------------------------------------------===//
906 //===--------------------------------------------------------------------===//
908 /// addLiveIn - Add the specified register as a live-in. Note that it
909 /// is an error to add the same register to the same set more than once.
910 void addLiveIn(unsigned Reg, unsigned vreg = 0) {
911 LiveIns.push_back(std::make_pair(Reg, vreg));
914 // Iteration support for the live-ins set. It's kept in sorted order
915 // by register number.
916 using livein_iterator =
917 std::vector<std::pair<unsigned,unsigned>>::const_iterator;
918 livein_iterator livein_begin() const { return LiveIns.begin(); }
919 livein_iterator livein_end() const { return LiveIns.end(); }
920 bool livein_empty() const { return LiveIns.empty(); }
922 ArrayRef<std::pair<unsigned, unsigned>> liveins() const {
926 bool isLiveIn(unsigned Reg) const;
928 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
929 /// corresponding live-in physical register.
930 unsigned getLiveInPhysReg(unsigned VReg) const;
932 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
933 /// corresponding live-in physical register.
934 unsigned getLiveInVirtReg(unsigned PReg) const;
936 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
937 /// into the given entry block.
938 void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
939 const TargetRegisterInfo &TRI,
940 const TargetInstrInfo &TII);
942 /// Returns a mask covering all bits that can appear in lane masks of
943 /// subregisters of the virtual register @p Reg.
944 LaneBitmask getMaxLaneMaskForVReg(unsigned Reg) const;
946 /// defusechain_iterator - This class provides iterator support for machine
947 /// operands in the function that use or define a specific register. If
948 /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
949 /// returns defs. If neither are true then you are silly and it always
950 /// returns end(). If SkipDebug is true it skips uses marked Debug
951 /// when incrementing.
952 template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
953 bool ByOperand, bool ByInstr, bool ByBundle>
954 class defusechain_iterator
955 : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
956 friend class MachineRegisterInfo;
958 MachineOperand *Op = nullptr;
960 explicit defusechain_iterator(MachineOperand *op) : Op(op) {
961 // If the first node isn't one we're interested in, advance to one that
962 // we are interested in.
964 if ((!ReturnUses && op->isUse()) ||
965 (!ReturnDefs && op->isDef()) ||
966 (SkipDebug && op->isDebug()))
972 assert(Op && "Cannot increment end iterator!");
973 Op = getNextOperandForReg(Op);
975 // All defs come before the uses, so stop def_iterator early.
981 assert(!Op->isDebug() && "Can't have debug defs");
984 // If this is an operand we don't care about, skip it.
985 while (Op && ((!ReturnDefs && Op->isDef()) ||
986 (SkipDebug && Op->isDebug())))
987 Op = getNextOperandForReg(Op);
992 using reference = std::iterator<std::forward_iterator_tag,
993 MachineInstr, ptrdiff_t>::reference;
994 using pointer = std::iterator<std::forward_iterator_tag,
995 MachineInstr, ptrdiff_t>::pointer;
997 defusechain_iterator() = default;
999 bool operator==(const defusechain_iterator &x) const {
1002 bool operator!=(const defusechain_iterator &x) const {
1003 return !operator==(x);
1006 /// atEnd - return true if this iterator is equal to reg_end() on the value.
1007 bool atEnd() const { return Op == nullptr; }
1009 // Iterator traversal: forward iteration only
1010 defusechain_iterator &operator++() { // Preincrement
1011 assert(Op && "Cannot increment end iterator!");
1015 MachineInstr *P = Op->getParent();
1018 } while (Op && Op->getParent() == P);
1019 } else if (ByBundle) {
1020 MachineBasicBlock::instr_iterator P =
1021 getBundleStart(Op->getParent()->getIterator());
1024 } while (Op && getBundleStart(Op->getParent()->getIterator()) == P);
1029 defusechain_iterator operator++(int) { // Postincrement
1030 defusechain_iterator tmp = *this; ++*this; return tmp;
1033 /// getOperandNo - Return the operand # of this MachineOperand in its
1035 unsigned getOperandNo() const {
1036 assert(Op && "Cannot dereference end iterator!");
1037 return Op - &Op->getParent()->getOperand(0);
1040 // Retrieve a reference to the current operand.
1041 MachineOperand &operator*() const {
1042 assert(Op && "Cannot dereference end iterator!");
1046 MachineOperand *operator->() const {
1047 assert(Op && "Cannot dereference end iterator!");
1052 /// defusechain_iterator - This class provides iterator support for machine
1053 /// operands in the function that use or define a specific register. If
1054 /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
1055 /// returns defs. If neither are true then you are silly and it always
1056 /// returns end(). If SkipDebug is true it skips uses marked Debug
1057 /// when incrementing.
1058 template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
1059 bool ByOperand, bool ByInstr, bool ByBundle>
1060 class defusechain_instr_iterator
1061 : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
1062 friend class MachineRegisterInfo;
1064 MachineOperand *Op = nullptr;
1066 explicit defusechain_instr_iterator(MachineOperand *op) : Op(op) {
1067 // If the first node isn't one we're interested in, advance to one that
1068 // we are interested in.
1070 if ((!ReturnUses && op->isUse()) ||
1071 (!ReturnDefs && op->isDef()) ||
1072 (SkipDebug && op->isDebug()))
1078 assert(Op && "Cannot increment end iterator!");
1079 Op = getNextOperandForReg(Op);
1081 // All defs come before the uses, so stop def_iterator early.
1087 assert(!Op->isDebug() && "Can't have debug defs");
1090 // If this is an operand we don't care about, skip it.
1091 while (Op && ((!ReturnDefs && Op->isDef()) ||
1092 (SkipDebug && Op->isDebug())))
1093 Op = getNextOperandForReg(Op);
1098 using reference = std::iterator<std::forward_iterator_tag,
1099 MachineInstr, ptrdiff_t>::reference;
1100 using pointer = std::iterator<std::forward_iterator_tag,
1101 MachineInstr, ptrdiff_t>::pointer;
1103 defusechain_instr_iterator() = default;
1105 bool operator==(const defusechain_instr_iterator &x) const {
1108 bool operator!=(const defusechain_instr_iterator &x) const {
1109 return !operator==(x);
1112 /// atEnd - return true if this iterator is equal to reg_end() on the value.
1113 bool atEnd() const { return Op == nullptr; }
1115 // Iterator traversal: forward iteration only
1116 defusechain_instr_iterator &operator++() { // Preincrement
1117 assert(Op && "Cannot increment end iterator!");
1121 MachineInstr *P = Op->getParent();
1124 } while (Op && Op->getParent() == P);
1125 } else if (ByBundle) {
1126 MachineBasicBlock::instr_iterator P =
1127 getBundleStart(Op->getParent()->getIterator());
1130 } while (Op && getBundleStart(Op->getParent()->getIterator()) == P);
1135 defusechain_instr_iterator operator++(int) { // Postincrement
1136 defusechain_instr_iterator tmp = *this; ++*this; return tmp;
1139 // Retrieve a reference to the current operand.
1140 MachineInstr &operator*() const {
1141 assert(Op && "Cannot dereference end iterator!");
1143 return *getBundleStart(Op->getParent()->getIterator());
1144 return *Op->getParent();
1147 MachineInstr *operator->() const { return &operator*(); }
1151 /// Iterate over the pressure sets affected by the given physical or virtual
1152 /// register. If Reg is physical, it must be a register unit (from
1153 /// MCRegUnitIterator).
1154 class PSetIterator {
1155 const int *PSet = nullptr;
1156 unsigned Weight = 0;
1159 PSetIterator() = default;
1161 PSetIterator(unsigned RegUnit, const MachineRegisterInfo *MRI) {
1162 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
1163 if (TargetRegisterInfo::isVirtualRegister(RegUnit)) {
1164 const TargetRegisterClass *RC = MRI->getRegClass(RegUnit);
1165 PSet = TRI->getRegClassPressureSets(RC);
1166 Weight = TRI->getRegClassWeight(RC).RegWeight;
1169 PSet = TRI->getRegUnitPressureSets(RegUnit);
1170 Weight = TRI->getRegUnitWeight(RegUnit);
1176 bool isValid() const { return PSet; }
1178 unsigned getWeight() const { return Weight; }
1180 unsigned operator*() const { return *PSet; }
1183 assert(isValid() && "Invalid PSetIterator.");
1190 inline PSetIterator MachineRegisterInfo::
1191 getPressureSets(unsigned RegUnit) const {
1192 return PSetIterator(RegUnit, this);
1195 } // end namespace llvm
1197 #endif // LLVM_CODEGEN_MACHINEREGISTERINFO_H