Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / CodeGen / MachineRegisterInfo.h
blobd25cd98bc55dec5136c857f6f56d85816a36b322
1 //===- llvm/CodeGen/MachineRegisterInfo.h -----------------------*- 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 defines the MachineRegisterInfo class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
14 #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/IndexedMap.h"
20 #include "llvm/ADT/PointerUnion.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringSet.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
25 #include "llvm/CodeGen/LowLevelType.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstrBundle.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/TargetRegisterInfo.h"
31 #include "llvm/CodeGen/TargetSubtargetInfo.h"
32 #include "llvm/MC/LaneBitmask.h"
33 #include <cassert>
34 #include <cstddef>
35 #include <cstdint>
36 #include <iterator>
37 #include <memory>
38 #include <utility>
39 #include <vector>
41 namespace llvm {
43 class PSetIterator;
45 /// Convenient type to represent either a register class or a register bank.
46 using RegClassOrRegBank =
47 PointerUnion<const TargetRegisterClass *, const RegisterBank *>;
49 /// MachineRegisterInfo - Keep track of information for virtual and physical
50 /// registers, including vreg register classes, use/def chains for registers,
51 /// etc.
52 class MachineRegisterInfo {
53 public:
54 class Delegate {
55 virtual void anchor();
57 public:
58 virtual ~Delegate() = default;
60 virtual void MRI_NoteNewVirtualRegister(unsigned Reg) = 0;
63 private:
64 MachineFunction *MF;
65 Delegate *TheDelegate = nullptr;
67 /// True if subregister liveness is tracked.
68 const bool TracksSubRegLiveness;
70 /// VRegInfo - Information we keep for each virtual register.
71 ///
72 /// Each element in this list contains the register class of the vreg and the
73 /// start of the use/def list for the register.
74 IndexedMap<std::pair<RegClassOrRegBank, MachineOperand *>,
75 VirtReg2IndexFunctor>
76 VRegInfo;
78 /// Map for recovering vreg name from vreg number.
79 /// This map is used by the MIR Printer.
80 IndexedMap<std::string, VirtReg2IndexFunctor> VReg2Name;
82 /// StringSet that is used to unique vreg names.
83 StringSet<> VRegNames;
85 /// The flag is true upon \p UpdatedCSRs initialization
86 /// and false otherwise.
87 bool IsUpdatedCSRsInitialized;
89 /// Contains the updated callee saved register list.
90 /// As opposed to the static list defined in register info,
91 /// all registers that were disabled are removed from the list.
92 SmallVector<MCPhysReg, 16> UpdatedCSRs;
94 /// RegAllocHints - This vector records register allocation hints for
95 /// virtual registers. For each virtual register, it keeps a pair of hint
96 /// type and hints vector making up the allocation hints. Only the first
97 /// hint may be target specific, and in that case this is reflected by the
98 /// first member of the pair being non-zero. If the hinted register is
99 /// virtual, it means the allocator should prefer the physical register
100 /// allocated to it if any.
101 IndexedMap<std::pair<unsigned, SmallVector<unsigned, 4>>,
102 VirtReg2IndexFunctor> RegAllocHints;
104 /// PhysRegUseDefLists - This is an array of the head of the use/def list for
105 /// physical registers.
106 std::unique_ptr<MachineOperand *[]> PhysRegUseDefLists;
108 /// getRegUseDefListHead - Return the head pointer for the register use/def
109 /// list for the specified virtual or physical register.
110 MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
111 if (TargetRegisterInfo::isVirtualRegister(RegNo))
112 return VRegInfo[RegNo].second;
113 return PhysRegUseDefLists[RegNo];
116 MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
117 if (TargetRegisterInfo::isVirtualRegister(RegNo))
118 return VRegInfo[RegNo].second;
119 return PhysRegUseDefLists[RegNo];
122 /// Get the next element in the use-def chain.
123 static MachineOperand *getNextOperandForReg(const MachineOperand *MO) {
124 assert(MO && MO->isReg() && "This is not a register operand!");
125 return MO->Contents.Reg.Next;
128 /// UsedPhysRegMask - Additional used physregs including aliases.
129 /// This bit vector represents all the registers clobbered by function calls.
130 BitVector UsedPhysRegMask;
132 /// ReservedRegs - This is a bit vector of reserved registers. The target
133 /// may change its mind about which registers should be reserved. This
134 /// vector is the frozen set of reserved registers when register allocation
135 /// started.
136 BitVector ReservedRegs;
138 using VRegToTypeMap = IndexedMap<LLT, VirtReg2IndexFunctor>;
139 /// Map generic virtual registers to their low-level type.
140 VRegToTypeMap VRegToType;
142 /// Keep track of the physical registers that are live in to the function.
143 /// Live in values are typically arguments in registers. LiveIn values are
144 /// allowed to have virtual registers associated with them, stored in the
145 /// second element.
146 std::vector<std::pair<unsigned, unsigned>> LiveIns;
148 public:
149 explicit MachineRegisterInfo(MachineFunction *MF);
150 MachineRegisterInfo(const MachineRegisterInfo &) = delete;
151 MachineRegisterInfo &operator=(const MachineRegisterInfo &) = delete;
153 const TargetRegisterInfo *getTargetRegisterInfo() const {
154 return MF->getSubtarget().getRegisterInfo();
157 void resetDelegate(Delegate *delegate) {
158 // Ensure another delegate does not take over unless the current
159 // delegate first unattaches itself. If we ever need to multicast
160 // notifications, we will need to change to using a list.
161 assert(TheDelegate == delegate &&
162 "Only the current delegate can perform reset!");
163 TheDelegate = nullptr;
166 void setDelegate(Delegate *delegate) {
167 assert(delegate && !TheDelegate &&
168 "Attempted to set delegate to null, or to change it without "
169 "first resetting it!");
171 TheDelegate = delegate;
174 //===--------------------------------------------------------------------===//
175 // Function State
176 //===--------------------------------------------------------------------===//
178 // isSSA - Returns true when the machine function is in SSA form. Early
179 // passes require the machine function to be in SSA form where every virtual
180 // register has a single defining instruction.
182 // The TwoAddressInstructionPass and PHIElimination passes take the machine
183 // function out of SSA form when they introduce multiple defs per virtual
184 // register.
185 bool isSSA() const {
186 return MF->getProperties().hasProperty(
187 MachineFunctionProperties::Property::IsSSA);
190 // leaveSSA - Indicates that the machine function is no longer in SSA form.
191 void leaveSSA() {
192 MF->getProperties().reset(MachineFunctionProperties::Property::IsSSA);
195 /// tracksLiveness - Returns true when tracking register liveness accurately.
196 /// (see MachineFUnctionProperties::Property description for details)
197 bool tracksLiveness() const {
198 return MF->getProperties().hasProperty(
199 MachineFunctionProperties::Property::TracksLiveness);
202 /// invalidateLiveness - Indicates that register liveness is no longer being
203 /// tracked accurately.
205 /// This should be called by late passes that invalidate the liveness
206 /// information.
207 void invalidateLiveness() {
208 MF->getProperties().reset(
209 MachineFunctionProperties::Property::TracksLiveness);
212 /// Returns true if liveness for register class @p RC should be tracked at
213 /// the subregister level.
214 bool shouldTrackSubRegLiveness(const TargetRegisterClass &RC) const {
215 return subRegLivenessEnabled() && RC.HasDisjunctSubRegs;
217 bool shouldTrackSubRegLiveness(unsigned VReg) const {
218 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Must pass a VReg");
219 return shouldTrackSubRegLiveness(*getRegClass(VReg));
221 bool subRegLivenessEnabled() const {
222 return TracksSubRegLiveness;
225 //===--------------------------------------------------------------------===//
226 // Register Info
227 //===--------------------------------------------------------------------===//
229 /// Returns true if the updated CSR list was initialized and false otherwise.
230 bool isUpdatedCSRsInitialized() const { return IsUpdatedCSRsInitialized; }
232 /// Disables the register from the list of CSRs.
233 /// I.e. the register will not appear as part of the CSR mask.
234 /// \see UpdatedCalleeSavedRegs.
235 void disableCalleeSavedRegister(unsigned Reg);
237 /// Returns list of callee saved registers.
238 /// The function returns the updated CSR list (after taking into account
239 /// registers that are disabled from the CSR list).
240 const MCPhysReg *getCalleeSavedRegs() const;
242 /// Sets the updated Callee Saved Registers list.
243 /// Notice that it will override ant previously disabled/saved CSRs.
244 void setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs);
246 // Strictly for use by MachineInstr.cpp.
247 void addRegOperandToUseList(MachineOperand *MO);
249 // Strictly for use by MachineInstr.cpp.
250 void removeRegOperandFromUseList(MachineOperand *MO);
252 // Strictly for use by MachineInstr.cpp.
253 void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
255 /// Verify the sanity of the use list for Reg.
256 void verifyUseList(unsigned Reg) const;
258 /// Verify the use list of all registers.
259 void verifyUseLists() const;
261 /// reg_begin/reg_end - Provide iteration support to walk over all definitions
262 /// and uses of a register within the MachineFunction that corresponds to this
263 /// MachineRegisterInfo object.
264 template<bool Uses, bool Defs, bool SkipDebug,
265 bool ByOperand, bool ByInstr, bool ByBundle>
266 class defusechain_iterator;
267 template<bool Uses, bool Defs, bool SkipDebug,
268 bool ByOperand, bool ByInstr, bool ByBundle>
269 class defusechain_instr_iterator;
271 // Make it a friend so it can access getNextOperandForReg().
272 template<bool, bool, bool, bool, bool, bool>
273 friend class defusechain_iterator;
274 template<bool, bool, bool, bool, bool, bool>
275 friend class defusechain_instr_iterator;
277 /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
278 /// register.
279 using reg_iterator =
280 defusechain_iterator<true, true, false, true, false, false>;
281 reg_iterator reg_begin(unsigned RegNo) const {
282 return reg_iterator(getRegUseDefListHead(RegNo));
284 static reg_iterator reg_end() { return reg_iterator(nullptr); }
286 inline iterator_range<reg_iterator> reg_operands(unsigned Reg) const {
287 return make_range(reg_begin(Reg), reg_end());
290 /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses
291 /// of the specified register, stepping by MachineInstr.
292 using reg_instr_iterator =
293 defusechain_instr_iterator<true, true, false, false, true, false>;
294 reg_instr_iterator reg_instr_begin(unsigned RegNo) const {
295 return reg_instr_iterator(getRegUseDefListHead(RegNo));
297 static reg_instr_iterator reg_instr_end() {
298 return reg_instr_iterator(nullptr);
301 inline iterator_range<reg_instr_iterator>
302 reg_instructions(unsigned Reg) const {
303 return make_range(reg_instr_begin(Reg), reg_instr_end());
306 /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses
307 /// of the specified register, stepping by bundle.
308 using reg_bundle_iterator =
309 defusechain_instr_iterator<true, true, false, false, false, true>;
310 reg_bundle_iterator reg_bundle_begin(unsigned RegNo) const {
311 return reg_bundle_iterator(getRegUseDefListHead(RegNo));
313 static reg_bundle_iterator reg_bundle_end() {
314 return reg_bundle_iterator(nullptr);
317 inline iterator_range<reg_bundle_iterator> reg_bundles(unsigned Reg) const {
318 return make_range(reg_bundle_begin(Reg), reg_bundle_end());
321 /// reg_empty - Return true if there are no instructions using or defining the
322 /// specified register (it may be live-in).
323 bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
325 /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
326 /// of the specified register, skipping those marked as Debug.
327 using reg_nodbg_iterator =
328 defusechain_iterator<true, true, true, true, false, false>;
329 reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const {
330 return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
332 static reg_nodbg_iterator reg_nodbg_end() {
333 return reg_nodbg_iterator(nullptr);
336 inline iterator_range<reg_nodbg_iterator>
337 reg_nodbg_operands(unsigned Reg) const {
338 return make_range(reg_nodbg_begin(Reg), reg_nodbg_end());
341 /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk
342 /// all defs and uses of the specified register, stepping by MachineInstr,
343 /// skipping those marked as Debug.
344 using reg_instr_nodbg_iterator =
345 defusechain_instr_iterator<true, true, true, false, true, false>;
346 reg_instr_nodbg_iterator reg_instr_nodbg_begin(unsigned RegNo) const {
347 return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
349 static reg_instr_nodbg_iterator reg_instr_nodbg_end() {
350 return reg_instr_nodbg_iterator(nullptr);
353 inline iterator_range<reg_instr_nodbg_iterator>
354 reg_nodbg_instructions(unsigned Reg) const {
355 return make_range(reg_instr_nodbg_begin(Reg), reg_instr_nodbg_end());
358 /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk
359 /// all defs and uses of the specified register, stepping by bundle,
360 /// skipping those marked as Debug.
361 using reg_bundle_nodbg_iterator =
362 defusechain_instr_iterator<true, true, true, false, false, true>;
363 reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(unsigned RegNo) const {
364 return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
366 static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() {
367 return reg_bundle_nodbg_iterator(nullptr);
370 inline iterator_range<reg_bundle_nodbg_iterator>
371 reg_nodbg_bundles(unsigned Reg) const {
372 return make_range(reg_bundle_nodbg_begin(Reg), reg_bundle_nodbg_end());
375 /// reg_nodbg_empty - Return true if the only instructions using or defining
376 /// Reg are Debug instructions.
377 bool reg_nodbg_empty(unsigned RegNo) const {
378 return reg_nodbg_begin(RegNo) == reg_nodbg_end();
381 /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
382 using def_iterator =
383 defusechain_iterator<false, true, false, true, false, false>;
384 def_iterator def_begin(unsigned RegNo) const {
385 return def_iterator(getRegUseDefListHead(RegNo));
387 static def_iterator def_end() { return def_iterator(nullptr); }
389 inline iterator_range<def_iterator> def_operands(unsigned Reg) const {
390 return make_range(def_begin(Reg), def_end());
393 /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the
394 /// specified register, stepping by MachineInst.
395 using def_instr_iterator =
396 defusechain_instr_iterator<false, true, false, false, true, false>;
397 def_instr_iterator def_instr_begin(unsigned RegNo) const {
398 return def_instr_iterator(getRegUseDefListHead(RegNo));
400 static def_instr_iterator def_instr_end() {
401 return def_instr_iterator(nullptr);
404 inline iterator_range<def_instr_iterator>
405 def_instructions(unsigned Reg) const {
406 return make_range(def_instr_begin(Reg), def_instr_end());
409 /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the
410 /// specified register, stepping by bundle.
411 using def_bundle_iterator =
412 defusechain_instr_iterator<false, true, false, false, false, true>;
413 def_bundle_iterator def_bundle_begin(unsigned RegNo) const {
414 return def_bundle_iterator(getRegUseDefListHead(RegNo));
416 static def_bundle_iterator def_bundle_end() {
417 return def_bundle_iterator(nullptr);
420 inline iterator_range<def_bundle_iterator> def_bundles(unsigned Reg) const {
421 return make_range(def_bundle_begin(Reg), def_bundle_end());
424 /// def_empty - Return true if there are no instructions defining the
425 /// specified register (it may be live-in).
426 bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
428 StringRef getVRegName(unsigned Reg) const {
429 return VReg2Name.inBounds(Reg) ? StringRef(VReg2Name[Reg]) : "";
432 void insertVRegByName(StringRef Name, unsigned Reg) {
433 assert((Name.empty() || VRegNames.find(Name) == VRegNames.end()) &&
434 "Named VRegs Must be Unique.");
435 if (!Name.empty()) {
436 VRegNames.insert(Name);
437 VReg2Name.grow(Reg);
438 VReg2Name[Reg] = Name.str();
442 /// Return true if there is exactly one operand defining the specified
443 /// register.
444 bool hasOneDef(unsigned RegNo) const {
445 def_iterator DI = def_begin(RegNo);
446 if (DI == def_end())
447 return false;
448 return ++DI == def_end();
451 /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
452 using use_iterator =
453 defusechain_iterator<true, false, false, true, false, false>;
454 use_iterator use_begin(unsigned RegNo) const {
455 return use_iterator(getRegUseDefListHead(RegNo));
457 static use_iterator use_end() { return use_iterator(nullptr); }
459 inline iterator_range<use_iterator> use_operands(unsigned Reg) const {
460 return make_range(use_begin(Reg), use_end());
463 /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the
464 /// specified register, stepping by MachineInstr.
465 using use_instr_iterator =
466 defusechain_instr_iterator<true, false, false, false, true, false>;
467 use_instr_iterator use_instr_begin(unsigned RegNo) const {
468 return use_instr_iterator(getRegUseDefListHead(RegNo));
470 static use_instr_iterator use_instr_end() {
471 return use_instr_iterator(nullptr);
474 inline iterator_range<use_instr_iterator>
475 use_instructions(unsigned Reg) const {
476 return make_range(use_instr_begin(Reg), use_instr_end());
479 /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the
480 /// specified register, stepping by bundle.
481 using use_bundle_iterator =
482 defusechain_instr_iterator<true, false, false, false, false, true>;
483 use_bundle_iterator use_bundle_begin(unsigned RegNo) const {
484 return use_bundle_iterator(getRegUseDefListHead(RegNo));
486 static use_bundle_iterator use_bundle_end() {
487 return use_bundle_iterator(nullptr);
490 inline iterator_range<use_bundle_iterator> use_bundles(unsigned Reg) const {
491 return make_range(use_bundle_begin(Reg), use_bundle_end());
494 /// use_empty - Return true if there are no instructions using the specified
495 /// register.
496 bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
498 /// hasOneUse - Return true if there is exactly one instruction using the
499 /// specified register.
500 bool hasOneUse(unsigned RegNo) const {
501 use_iterator UI = use_begin(RegNo);
502 if (UI == use_end())
503 return false;
504 return ++UI == use_end();
507 /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
508 /// specified register, skipping those marked as Debug.
509 using use_nodbg_iterator =
510 defusechain_iterator<true, false, true, true, false, false>;
511 use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const {
512 return use_nodbg_iterator(getRegUseDefListHead(RegNo));
514 static use_nodbg_iterator use_nodbg_end() {
515 return use_nodbg_iterator(nullptr);
518 inline iterator_range<use_nodbg_iterator>
519 use_nodbg_operands(unsigned Reg) const {
520 return make_range(use_nodbg_begin(Reg), use_nodbg_end());
523 /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk
524 /// all uses of the specified register, stepping by MachineInstr, skipping
525 /// those marked as Debug.
526 using use_instr_nodbg_iterator =
527 defusechain_instr_iterator<true, false, true, false, true, false>;
528 use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const {
529 return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
531 static use_instr_nodbg_iterator use_instr_nodbg_end() {
532 return use_instr_nodbg_iterator(nullptr);
535 inline iterator_range<use_instr_nodbg_iterator>
536 use_nodbg_instructions(unsigned Reg) const {
537 return make_range(use_instr_nodbg_begin(Reg), use_instr_nodbg_end());
540 /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk
541 /// all uses of the specified register, stepping by bundle, skipping
542 /// those marked as Debug.
543 using use_bundle_nodbg_iterator =
544 defusechain_instr_iterator<true, false, true, false, false, true>;
545 use_bundle_nodbg_iterator use_bundle_nodbg_begin(unsigned RegNo) const {
546 return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
548 static use_bundle_nodbg_iterator use_bundle_nodbg_end() {
549 return use_bundle_nodbg_iterator(nullptr);
552 inline iterator_range<use_bundle_nodbg_iterator>
553 use_nodbg_bundles(unsigned Reg) const {
554 return make_range(use_bundle_nodbg_begin(Reg), use_bundle_nodbg_end());
557 /// use_nodbg_empty - Return true if there are no non-Debug instructions
558 /// using the specified register.
559 bool use_nodbg_empty(unsigned RegNo) const {
560 return use_nodbg_begin(RegNo) == use_nodbg_end();
563 /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
564 /// instruction using the specified register.
565 bool hasOneNonDBGUse(unsigned RegNo) const;
567 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
568 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
569 /// except that it also changes any definitions of the register as well.
571 /// Note that it is usually necessary to first constrain ToReg's register
572 /// class and register bank to match the FromReg constraints using one of the
573 /// methods:
575 /// constrainRegClass(ToReg, getRegClass(FromReg))
576 /// constrainRegAttrs(ToReg, FromReg)
577 /// RegisterBankInfo::constrainGenericRegister(ToReg,
578 /// *MRI.getRegClass(FromReg), MRI)
580 /// These functions will return a falsy result if the virtual registers have
581 /// incompatible constraints.
583 /// Note that if ToReg is a physical register the function will replace and
584 /// apply sub registers to ToReg in order to obtain a final/proper physical
585 /// register.
586 void replaceRegWith(unsigned FromReg, unsigned ToReg);
588 /// getVRegDef - Return the machine instr that defines the specified virtual
589 /// register or null if none is found. This assumes that the code is in SSA
590 /// form, so there should only be one definition.
591 MachineInstr *getVRegDef(unsigned Reg) const;
593 /// getUniqueVRegDef - Return the unique machine instr that defines the
594 /// specified virtual register or null if none is found. If there are
595 /// multiple definitions or no definition, return null.
596 MachineInstr *getUniqueVRegDef(unsigned Reg) const;
598 /// clearKillFlags - Iterate over all the uses of the given register and
599 /// clear the kill flag from the MachineOperand. This function is used by
600 /// optimization passes which extend register lifetimes and need only
601 /// preserve conservative kill flag information.
602 void clearKillFlags(unsigned Reg) const;
604 void dumpUses(unsigned RegNo) const;
606 /// Returns true if PhysReg is unallocatable and constant throughout the
607 /// function. Writing to a constant register has no effect.
608 bool isConstantPhysReg(unsigned PhysReg) const;
610 /// Returns true if either isConstantPhysReg or TRI->isCallerPreservedPhysReg
611 /// returns true. This is a utility member function.
612 bool isCallerPreservedOrConstPhysReg(unsigned PhysReg) const;
614 /// Get an iterator over the pressure sets affected by the given physical or
615 /// virtual register. If RegUnit is physical, it must be a register unit (from
616 /// MCRegUnitIterator).
617 PSetIterator getPressureSets(unsigned RegUnit) const;
619 //===--------------------------------------------------------------------===//
620 // Virtual Register Info
621 //===--------------------------------------------------------------------===//
623 /// Return the register class of the specified virtual register.
624 /// This shouldn't be used directly unless \p Reg has a register class.
625 /// \see getRegClassOrNull when this might happen.
626 const TargetRegisterClass *getRegClass(unsigned Reg) const {
627 assert(VRegInfo[Reg].first.is<const TargetRegisterClass *>() &&
628 "Register class not set, wrong accessor");
629 return VRegInfo[Reg].first.get<const TargetRegisterClass *>();
632 /// Return the register class of \p Reg, or null if Reg has not been assigned
633 /// a register class yet.
635 /// \note A null register class can only happen when these two
636 /// conditions are met:
637 /// 1. Generic virtual registers are created.
638 /// 2. The machine function has not completely been through the
639 /// instruction selection process.
640 /// None of this condition is possible without GlobalISel for now.
641 /// In other words, if GlobalISel is not used or if the query happens after
642 /// the select pass, using getRegClass is safe.
643 const TargetRegisterClass *getRegClassOrNull(unsigned Reg) const {
644 const RegClassOrRegBank &Val = VRegInfo[Reg].first;
645 return Val.dyn_cast<const TargetRegisterClass *>();
648 /// Return the register bank of \p Reg, or null if Reg has not been assigned
649 /// a register bank or has been assigned a register class.
650 /// \note It is possible to get the register bank from the register class via
651 /// RegisterBankInfo::getRegBankFromRegClass.
652 const RegisterBank *getRegBankOrNull(unsigned Reg) const {
653 const RegClassOrRegBank &Val = VRegInfo[Reg].first;
654 return Val.dyn_cast<const RegisterBank *>();
657 /// Return the register bank or register class of \p Reg.
658 /// \note Before the register bank gets assigned (i.e., before the
659 /// RegBankSelect pass) \p Reg may not have either.
660 const RegClassOrRegBank &getRegClassOrRegBank(unsigned Reg) const {
661 return VRegInfo[Reg].first;
664 /// setRegClass - Set the register class of the specified virtual register.
665 void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
667 /// Set the register bank to \p RegBank for \p Reg.
668 void setRegBank(unsigned Reg, const RegisterBank &RegBank);
670 void setRegClassOrRegBank(unsigned Reg,
671 const RegClassOrRegBank &RCOrRB){
672 VRegInfo[Reg].first = RCOrRB;
675 /// constrainRegClass - Constrain the register class of the specified virtual
676 /// register to be a common subclass of RC and the current register class,
677 /// but only if the new class has at least MinNumRegs registers. Return the
678 /// new register class, or NULL if no such class exists.
679 /// This should only be used when the constraint is known to be trivial, like
680 /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
682 /// \note Assumes that the register has a register class assigned.
683 /// Use RegisterBankInfo::constrainGenericRegister in GlobalISel's
684 /// InstructionSelect pass and constrainRegAttrs in every other pass,
685 /// including non-select passes of GlobalISel, instead.
686 const TargetRegisterClass *constrainRegClass(unsigned Reg,
687 const TargetRegisterClass *RC,
688 unsigned MinNumRegs = 0);
690 /// Constrain the register class or the register bank of the virtual register
691 /// \p Reg (and low-level type) to be a common subclass or a common bank of
692 /// both registers provided respectively (and a common low-level type). Do
693 /// nothing if any of the attributes (classes, banks, or low-level types) of
694 /// the registers are deemed incompatible, or if the resulting register will
695 /// have a class smaller than before and of size less than \p MinNumRegs.
696 /// Return true if such register attributes exist, false otherwise.
698 /// \note Use this method instead of constrainRegClass and
699 /// RegisterBankInfo::constrainGenericRegister everywhere but SelectionDAG
700 /// ISel / FastISel and GlobalISel's InstructionSelect pass respectively.
701 bool constrainRegAttrs(unsigned Reg, unsigned ConstrainingReg,
702 unsigned MinNumRegs = 0);
704 /// recomputeRegClass - Try to find a legal super-class of Reg's register
705 /// class that still satisfies the constraints from the instructions using
706 /// Reg. Returns true if Reg was upgraded.
708 /// This method can be used after constraints have been removed from a
709 /// virtual register, for example after removing instructions or splitting
710 /// the live range.
711 bool recomputeRegClass(unsigned Reg);
713 /// createVirtualRegister - Create and return a new virtual register in the
714 /// function with the specified register class.
715 unsigned createVirtualRegister(const TargetRegisterClass *RegClass,
716 StringRef Name = "");
718 /// Create and return a new virtual register in the function with the same
719 /// attributes as the given register.
720 unsigned cloneVirtualRegister(unsigned VReg, StringRef Name = "");
722 /// Get the low-level type of \p Reg or LLT{} if Reg is not a generic
723 /// (target independent) virtual register.
724 LLT getType(unsigned Reg) const {
725 if (TargetRegisterInfo::isVirtualRegister(Reg) && VRegToType.inBounds(Reg))
726 return VRegToType[Reg];
727 return LLT{};
730 /// Set the low-level type of \p VReg to \p Ty.
731 void setType(unsigned VReg, LLT Ty);
733 /// Create and return a new generic virtual register with low-level
734 /// type \p Ty.
735 unsigned createGenericVirtualRegister(LLT Ty, StringRef Name = "");
737 /// Remove all types associated to virtual registers (after instruction
738 /// selection and constraining of all generic virtual registers).
739 void clearVirtRegTypes();
741 /// Creates a new virtual register that has no register class, register bank
742 /// or size assigned yet. This is only allowed to be used
743 /// temporarily while constructing machine instructions. Most operations are
744 /// undefined on an incomplete register until one of setRegClass(),
745 /// setRegBank() or setSize() has been called on it.
746 unsigned createIncompleteVirtualRegister(StringRef Name = "");
748 /// getNumVirtRegs - Return the number of virtual registers created.
749 unsigned getNumVirtRegs() const { return VRegInfo.size(); }
751 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
752 void clearVirtRegs();
754 /// setRegAllocationHint - Specify a register allocation hint for the
755 /// specified virtual register. This is typically used by target, and in case
756 /// of an earlier hint it will be overwritten.
757 void setRegAllocationHint(unsigned VReg, unsigned Type, unsigned PrefReg) {
758 assert(TargetRegisterInfo::isVirtualRegister(VReg));
759 RegAllocHints[VReg].first = Type;
760 RegAllocHints[VReg].second.clear();
761 RegAllocHints[VReg].second.push_back(PrefReg);
764 /// addRegAllocationHint - Add a register allocation hint to the hints
765 /// vector for VReg.
766 void addRegAllocationHint(unsigned VReg, unsigned PrefReg) {
767 assert(TargetRegisterInfo::isVirtualRegister(VReg));
768 RegAllocHints[VReg].second.push_back(PrefReg);
771 /// Specify the preferred (target independent) register allocation hint for
772 /// the specified virtual register.
773 void setSimpleHint(unsigned VReg, unsigned PrefReg) {
774 setRegAllocationHint(VReg, /*Type=*/0, PrefReg);
777 void clearSimpleHint(unsigned VReg) {
778 assert (RegAllocHints[VReg].first == 0 &&
779 "Expected to clear a non-target hint!");
780 RegAllocHints[VReg].second.clear();
783 /// getRegAllocationHint - Return the register allocation hint for the
784 /// specified virtual register. If there are many hints, this returns the
785 /// one with the greatest weight.
786 std::pair<unsigned, unsigned>
787 getRegAllocationHint(unsigned VReg) const {
788 assert(TargetRegisterInfo::isVirtualRegister(VReg));
789 unsigned BestHint = (RegAllocHints[VReg].second.size() ?
790 RegAllocHints[VReg].second[0] : 0);
791 return std::pair<unsigned, unsigned>(RegAllocHints[VReg].first, BestHint);
794 /// getSimpleHint - same as getRegAllocationHint except it will only return
795 /// a target independent hint.
796 unsigned getSimpleHint(unsigned VReg) const {
797 assert(TargetRegisterInfo::isVirtualRegister(VReg));
798 std::pair<unsigned, unsigned> Hint = getRegAllocationHint(VReg);
799 return Hint.first ? 0 : Hint.second;
802 /// getRegAllocationHints - Return a reference to the vector of all
803 /// register allocation hints for VReg.
804 const std::pair<unsigned, SmallVector<unsigned, 4>>
805 &getRegAllocationHints(unsigned VReg) const {
806 assert(TargetRegisterInfo::isVirtualRegister(VReg));
807 return RegAllocHints[VReg];
810 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
811 /// specified register as undefined which causes the DBG_VALUE to be
812 /// deleted during LiveDebugVariables analysis.
813 void markUsesInDebugValueAsUndef(unsigned Reg) const;
815 /// Return true if the specified register is modified in this function.
816 /// This checks that no defining machine operands exist for the register or
817 /// any of its aliases. Definitions found on functions marked noreturn are
818 /// ignored, to consider them pass 'true' for optional parameter
819 /// SkipNoReturnDef. The register is also considered modified when it is set
820 /// in the UsedPhysRegMask.
821 bool isPhysRegModified(unsigned PhysReg, bool SkipNoReturnDef = false) const;
823 /// Return true if the specified register is modified or read in this
824 /// function. This checks that no machine operands exist for the register or
825 /// any of its aliases. The register is also considered used when it is set
826 /// in the UsedPhysRegMask.
827 bool isPhysRegUsed(unsigned PhysReg) const;
829 /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
830 /// This corresponds to the bit mask attached to register mask operands.
831 void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
832 UsedPhysRegMask.setBitsNotInMask(RegMask);
835 const BitVector &getUsedPhysRegsMask() const { return UsedPhysRegMask; }
837 //===--------------------------------------------------------------------===//
838 // Reserved Register Info
839 //===--------------------------------------------------------------------===//
841 // The set of reserved registers must be invariant during register
842 // allocation. For example, the target cannot suddenly decide it needs a
843 // frame pointer when the register allocator has already used the frame
844 // pointer register for something else.
846 // These methods can be used by target hooks like hasFP() to avoid changing
847 // the reserved register set during register allocation.
849 /// freezeReservedRegs - Called by the register allocator to freeze the set
850 /// of reserved registers before allocation begins.
851 void freezeReservedRegs(const MachineFunction&);
853 /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
854 /// to ensure the set of reserved registers stays constant.
855 bool reservedRegsFrozen() const {
856 return !ReservedRegs.empty();
859 /// canReserveReg - Returns true if PhysReg can be used as a reserved
860 /// register. Any register can be reserved before freezeReservedRegs() is
861 /// called.
862 bool canReserveReg(unsigned PhysReg) const {
863 return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
866 /// getReservedRegs - Returns a reference to the frozen set of reserved
867 /// registers. This method should always be preferred to calling
868 /// TRI::getReservedRegs() when possible.
869 const BitVector &getReservedRegs() const {
870 assert(reservedRegsFrozen() &&
871 "Reserved registers haven't been frozen yet. "
872 "Use TRI::getReservedRegs().");
873 return ReservedRegs;
876 /// isReserved - Returns true when PhysReg is a reserved register.
878 /// Reserved registers may belong to an allocatable register class, but the
879 /// target has explicitly requested that they are not used.
880 bool isReserved(unsigned PhysReg) const {
881 return getReservedRegs().test(PhysReg);
884 /// Returns true when the given register unit is considered reserved.
886 /// Register units are considered reserved when for at least one of their
887 /// root registers, the root register and all super registers are reserved.
888 /// This currently iterates the register hierarchy and may be slower than
889 /// expected.
890 bool isReservedRegUnit(unsigned Unit) const;
892 /// isAllocatable - Returns true when PhysReg belongs to an allocatable
893 /// register class and it hasn't been reserved.
895 /// Allocatable registers may show up in the allocation order of some virtual
896 /// register, so a register allocator needs to track its liveness and
897 /// availability.
898 bool isAllocatable(unsigned PhysReg) const {
899 return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) &&
900 !isReserved(PhysReg);
903 //===--------------------------------------------------------------------===//
904 // LiveIn Management
905 //===--------------------------------------------------------------------===//
907 /// addLiveIn - Add the specified register as a live-in. Note that it
908 /// is an error to add the same register to the same set more than once.
909 void addLiveIn(unsigned Reg, unsigned vreg = 0) {
910 LiveIns.push_back(std::make_pair(Reg, vreg));
913 // Iteration support for the live-ins set. It's kept in sorted order
914 // by register number.
915 using livein_iterator =
916 std::vector<std::pair<unsigned,unsigned>>::const_iterator;
917 livein_iterator livein_begin() const { return LiveIns.begin(); }
918 livein_iterator livein_end() const { return LiveIns.end(); }
919 bool livein_empty() const { return LiveIns.empty(); }
921 ArrayRef<std::pair<unsigned, unsigned>> liveins() const {
922 return LiveIns;
925 bool isLiveIn(unsigned Reg) const;
927 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
928 /// corresponding live-in physical register.
929 unsigned getLiveInPhysReg(unsigned VReg) const;
931 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
932 /// corresponding live-in physical register.
933 unsigned getLiveInVirtReg(unsigned PReg) const;
935 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
936 /// into the given entry block.
937 void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
938 const TargetRegisterInfo &TRI,
939 const TargetInstrInfo &TII);
941 /// Returns a mask covering all bits that can appear in lane masks of
942 /// subregisters of the virtual register @p Reg.
943 LaneBitmask getMaxLaneMaskForVReg(unsigned Reg) const;
945 /// defusechain_iterator - This class provides iterator support for machine
946 /// operands in the function that use or define a specific register. If
947 /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
948 /// returns defs. If neither are true then you are silly and it always
949 /// returns end(). If SkipDebug is true it skips uses marked Debug
950 /// when incrementing.
951 template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
952 bool ByOperand, bool ByInstr, bool ByBundle>
953 class defusechain_iterator
954 : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
955 friend class MachineRegisterInfo;
957 MachineOperand *Op = nullptr;
959 explicit defusechain_iterator(MachineOperand *op) : Op(op) {
960 // If the first node isn't one we're interested in, advance to one that
961 // we are interested in.
962 if (op) {
963 if ((!ReturnUses && op->isUse()) ||
964 (!ReturnDefs && op->isDef()) ||
965 (SkipDebug && op->isDebug()))
966 advance();
970 void advance() {
971 assert(Op && "Cannot increment end iterator!");
972 Op = getNextOperandForReg(Op);
974 // All defs come before the uses, so stop def_iterator early.
975 if (!ReturnUses) {
976 if (Op) {
977 if (Op->isUse())
978 Op = nullptr;
979 else
980 assert(!Op->isDebug() && "Can't have debug defs");
982 } else {
983 // If this is an operand we don't care about, skip it.
984 while (Op && ((!ReturnDefs && Op->isDef()) ||
985 (SkipDebug && Op->isDebug())))
986 Op = getNextOperandForReg(Op);
990 public:
991 using reference = std::iterator<std::forward_iterator_tag,
992 MachineInstr, ptrdiff_t>::reference;
993 using pointer = std::iterator<std::forward_iterator_tag,
994 MachineInstr, ptrdiff_t>::pointer;
996 defusechain_iterator() = default;
998 bool operator==(const defusechain_iterator &x) const {
999 return Op == x.Op;
1001 bool operator!=(const defusechain_iterator &x) const {
1002 return !operator==(x);
1005 /// atEnd - return true if this iterator is equal to reg_end() on the value.
1006 bool atEnd() const { return Op == nullptr; }
1008 // Iterator traversal: forward iteration only
1009 defusechain_iterator &operator++() { // Preincrement
1010 assert(Op && "Cannot increment end iterator!");
1011 if (ByOperand)
1012 advance();
1013 else if (ByInstr) {
1014 MachineInstr *P = Op->getParent();
1015 do {
1016 advance();
1017 } while (Op && Op->getParent() == P);
1018 } else if (ByBundle) {
1019 MachineBasicBlock::instr_iterator P =
1020 getBundleStart(Op->getParent()->getIterator());
1021 do {
1022 advance();
1023 } while (Op && getBundleStart(Op->getParent()->getIterator()) == P);
1026 return *this;
1028 defusechain_iterator operator++(int) { // Postincrement
1029 defusechain_iterator tmp = *this; ++*this; return tmp;
1032 /// getOperandNo - Return the operand # of this MachineOperand in its
1033 /// MachineInstr.
1034 unsigned getOperandNo() const {
1035 assert(Op && "Cannot dereference end iterator!");
1036 return Op - &Op->getParent()->getOperand(0);
1039 // Retrieve a reference to the current operand.
1040 MachineOperand &operator*() const {
1041 assert(Op && "Cannot dereference end iterator!");
1042 return *Op;
1045 MachineOperand *operator->() const {
1046 assert(Op && "Cannot dereference end iterator!");
1047 return Op;
1051 /// defusechain_iterator - This class provides iterator support for machine
1052 /// operands in the function that use or define a specific register. If
1053 /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
1054 /// returns defs. If neither are true then you are silly and it always
1055 /// returns end(). If SkipDebug is true it skips uses marked Debug
1056 /// when incrementing.
1057 template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
1058 bool ByOperand, bool ByInstr, bool ByBundle>
1059 class defusechain_instr_iterator
1060 : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
1061 friend class MachineRegisterInfo;
1063 MachineOperand *Op = nullptr;
1065 explicit defusechain_instr_iterator(MachineOperand *op) : Op(op) {
1066 // If the first node isn't one we're interested in, advance to one that
1067 // we are interested in.
1068 if (op) {
1069 if ((!ReturnUses && op->isUse()) ||
1070 (!ReturnDefs && op->isDef()) ||
1071 (SkipDebug && op->isDebug()))
1072 advance();
1076 void advance() {
1077 assert(Op && "Cannot increment end iterator!");
1078 Op = getNextOperandForReg(Op);
1080 // All defs come before the uses, so stop def_iterator early.
1081 if (!ReturnUses) {
1082 if (Op) {
1083 if (Op->isUse())
1084 Op = nullptr;
1085 else
1086 assert(!Op->isDebug() && "Can't have debug defs");
1088 } else {
1089 // If this is an operand we don't care about, skip it.
1090 while (Op && ((!ReturnDefs && Op->isDef()) ||
1091 (SkipDebug && Op->isDebug())))
1092 Op = getNextOperandForReg(Op);
1096 public:
1097 using reference = std::iterator<std::forward_iterator_tag,
1098 MachineInstr, ptrdiff_t>::reference;
1099 using pointer = std::iterator<std::forward_iterator_tag,
1100 MachineInstr, ptrdiff_t>::pointer;
1102 defusechain_instr_iterator() = default;
1104 bool operator==(const defusechain_instr_iterator &x) const {
1105 return Op == x.Op;
1107 bool operator!=(const defusechain_instr_iterator &x) const {
1108 return !operator==(x);
1111 /// atEnd - return true if this iterator is equal to reg_end() on the value.
1112 bool atEnd() const { return Op == nullptr; }
1114 // Iterator traversal: forward iteration only
1115 defusechain_instr_iterator &operator++() { // Preincrement
1116 assert(Op && "Cannot increment end iterator!");
1117 if (ByOperand)
1118 advance();
1119 else if (ByInstr) {
1120 MachineInstr *P = Op->getParent();
1121 do {
1122 advance();
1123 } while (Op && Op->getParent() == P);
1124 } else if (ByBundle) {
1125 MachineBasicBlock::instr_iterator P =
1126 getBundleStart(Op->getParent()->getIterator());
1127 do {
1128 advance();
1129 } while (Op && getBundleStart(Op->getParent()->getIterator()) == P);
1132 return *this;
1134 defusechain_instr_iterator operator++(int) { // Postincrement
1135 defusechain_instr_iterator tmp = *this; ++*this; return tmp;
1138 // Retrieve a reference to the current operand.
1139 MachineInstr &operator*() const {
1140 assert(Op && "Cannot dereference end iterator!");
1141 if (ByBundle)
1142 return *getBundleStart(Op->getParent()->getIterator());
1143 return *Op->getParent();
1146 MachineInstr *operator->() const { return &operator*(); }
1150 /// Iterate over the pressure sets affected by the given physical or virtual
1151 /// register. If Reg is physical, it must be a register unit (from
1152 /// MCRegUnitIterator).
1153 class PSetIterator {
1154 const int *PSet = nullptr;
1155 unsigned Weight = 0;
1157 public:
1158 PSetIterator() = default;
1160 PSetIterator(unsigned RegUnit, const MachineRegisterInfo *MRI) {
1161 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
1162 if (TargetRegisterInfo::isVirtualRegister(RegUnit)) {
1163 const TargetRegisterClass *RC = MRI->getRegClass(RegUnit);
1164 PSet = TRI->getRegClassPressureSets(RC);
1165 Weight = TRI->getRegClassWeight(RC).RegWeight;
1167 else {
1168 PSet = TRI->getRegUnitPressureSets(RegUnit);
1169 Weight = TRI->getRegUnitWeight(RegUnit);
1171 if (*PSet == -1)
1172 PSet = nullptr;
1175 bool isValid() const { return PSet; }
1177 unsigned getWeight() const { return Weight; }
1179 unsigned operator*() const { return *PSet; }
1181 void operator++() {
1182 assert(isValid() && "Invalid PSetIterator.");
1183 ++PSet;
1184 if (*PSet == -1)
1185 PSet = nullptr;
1189 inline PSetIterator MachineRegisterInfo::
1190 getPressureSets(unsigned RegUnit) const {
1191 return PSetIterator(RegUnit, this);
1194 } // end namespace llvm
1196 #endif // LLVM_CODEGEN_MACHINEREGISTERINFO_H