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/Target/TargetRegisterInfo.h"
18 #include "llvm/CodeGen/MachineInstrBundle.h"
19 #include "llvm/ADT/BitVector.h"
20 #include "llvm/ADT/IndexedMap.h"
25 /// MachineRegisterInfo - Keep track of information for virtual and physical
26 /// registers, including vreg register classes, use/def chains for registers,
28 class MachineRegisterInfo {
29 const TargetRegisterInfo *const TRI;
31 /// IsSSA - True when the machine function is in SSA form and virtual
32 /// registers have a single def.
35 /// TracksLiveness - True while register liveness is being tracked accurately.
36 /// Basic block live-in lists, kill flags, and implicit defs may not be
37 /// accurate when after this flag is cleared.
40 /// VRegInfo - Information we keep for each virtual register.
42 /// Each element in this list contains the register class of the vreg and the
43 /// start of the use/def list for the register.
44 IndexedMap<std::pair<const TargetRegisterClass*, MachineOperand*>,
45 VirtReg2IndexFunctor> VRegInfo;
47 /// RegAllocHints - This vector records register allocation hints for virtual
48 /// registers. For each virtual register, it keeps a register and hint type
49 /// pair making up the allocation hint. Hint type is target specific except
50 /// for the value 0 which means the second value of the pair is the preferred
51 /// register for allocation. For example, if the hint is <0, 1024>, it means
52 /// the allocator should prefer the physical register allocated to the virtual
53 /// register of the hint.
54 IndexedMap<std::pair<unsigned, unsigned>, VirtReg2IndexFunctor> RegAllocHints;
56 /// PhysRegUseDefLists - This is an array of the head of the use/def list for
57 /// physical registers.
58 MachineOperand **PhysRegUseDefLists;
60 /// UsedPhysRegs - This is a bit vector that is computed and set by the
61 /// register allocator, and must be kept up to date by passes that run after
62 /// register allocation (though most don't modify this). This is used
63 /// so that the code generator knows which callee save registers to save and
64 /// for other target specific uses.
65 /// This vector only has bits set for registers explicitly used, not their
67 BitVector UsedPhysRegs;
69 /// UsedPhysRegMask - Additional used physregs, but including aliases.
70 BitVector UsedPhysRegMask;
72 /// ReservedRegs - This is a bit vector of reserved registers. The target
73 /// may change its mind about which registers should be reserved. This
74 /// vector is the frozen set of reserved registers when register allocation
76 BitVector ReservedRegs;
78 /// AllocatableRegs - From TRI->getAllocatableSet.
79 mutable BitVector AllocatableRegs;
81 /// LiveIns/LiveOuts - Keep track of the physical registers that are
82 /// livein/liveout of the function. Live in values are typically arguments in
83 /// registers, live out values are typically return values in registers.
84 /// LiveIn values are allowed to have virtual registers associated with them,
85 /// stored in the second element.
86 std::vector<std::pair<unsigned, unsigned> > LiveIns;
87 std::vector<unsigned> LiveOuts;
89 MachineRegisterInfo(const MachineRegisterInfo&); // DO NOT IMPLEMENT
90 void operator=(const MachineRegisterInfo&); // DO NOT IMPLEMENT
92 explicit MachineRegisterInfo(const TargetRegisterInfo &TRI);
93 ~MachineRegisterInfo();
95 //===--------------------------------------------------------------------===//
97 //===--------------------------------------------------------------------===//
99 // isSSA - Returns true when the machine function is in SSA form. Early
100 // passes require the machine function to be in SSA form where every virtual
101 // register has a single defining instruction.
103 // The TwoAddressInstructionPass and PHIElimination passes take the machine
104 // function out of SSA form when they introduce multiple defs per virtual
106 bool isSSA() const { return IsSSA; }
108 // leaveSSA - Indicates that the machine function is no longer in SSA form.
109 void leaveSSA() { IsSSA = false; }
111 /// tracksLiveness - Returns true when tracking register liveness accurately.
113 /// While this flag is true, register liveness information in basic block
114 /// live-in lists and machine instruction operands is accurate. This means it
115 /// can be used to change the code in ways that affect the values in
116 /// registers, for example by the register scavenger.
118 /// When this flag is false, liveness is no longer reliable.
119 bool tracksLiveness() const { return TracksLiveness; }
121 /// invalidateLiveness - Indicates that register liveness is no longer being
122 /// tracked accurately.
124 /// This should be called by late passes that invalidate the liveness
126 void invalidateLiveness() { TracksLiveness = false; }
128 //===--------------------------------------------------------------------===//
130 //===--------------------------------------------------------------------===//
132 /// reg_begin/reg_end - Provide iteration support to walk over all definitions
133 /// and uses of a register within the MachineFunction that corresponds to this
134 /// MachineRegisterInfo object.
135 template<bool Uses, bool Defs, bool SkipDebug>
136 class defusechain_iterator;
138 /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
140 typedef defusechain_iterator<true,true,false> reg_iterator;
141 reg_iterator reg_begin(unsigned RegNo) const {
142 return reg_iterator(getRegUseDefListHead(RegNo));
144 static reg_iterator reg_end() { return reg_iterator(0); }
146 /// reg_empty - Return true if there are no instructions using or defining the
147 /// specified register (it may be live-in).
148 bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
150 /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
151 /// of the specified register, skipping those marked as Debug.
152 typedef defusechain_iterator<true,true,true> reg_nodbg_iterator;
153 reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const {
154 return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
156 static reg_nodbg_iterator reg_nodbg_end() { return reg_nodbg_iterator(0); }
158 /// reg_nodbg_empty - Return true if the only instructions using or defining
159 /// Reg are Debug instructions.
160 bool reg_nodbg_empty(unsigned RegNo) const {
161 return reg_nodbg_begin(RegNo) == reg_nodbg_end();
164 /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
165 typedef defusechain_iterator<false,true,false> def_iterator;
166 def_iterator def_begin(unsigned RegNo) const {
167 return def_iterator(getRegUseDefListHead(RegNo));
169 static def_iterator def_end() { return def_iterator(0); }
171 /// def_empty - Return true if there are no instructions defining the
172 /// specified register (it may be live-in).
173 bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
175 /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
176 typedef defusechain_iterator<true,false,false> use_iterator;
177 use_iterator use_begin(unsigned RegNo) const {
178 return use_iterator(getRegUseDefListHead(RegNo));
180 static use_iterator use_end() { return use_iterator(0); }
182 /// use_empty - Return true if there are no instructions using the specified
184 bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
186 /// hasOneUse - Return true if there is exactly one instruction using the
187 /// specified register.
188 bool hasOneUse(unsigned RegNo) const {
189 use_iterator UI = use_begin(RegNo);
192 return ++UI == use_end();
195 /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
196 /// specified register, skipping those marked as Debug.
197 typedef defusechain_iterator<true,false,true> use_nodbg_iterator;
198 use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const {
199 return use_nodbg_iterator(getRegUseDefListHead(RegNo));
201 static use_nodbg_iterator use_nodbg_end() { return use_nodbg_iterator(0); }
203 /// use_nodbg_empty - Return true if there are no non-Debug instructions
204 /// using the specified register.
205 bool use_nodbg_empty(unsigned RegNo) const {
206 return use_nodbg_begin(RegNo) == use_nodbg_end();
209 /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
210 /// instruction using the specified register.
211 bool hasOneNonDBGUse(unsigned RegNo) const;
213 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
214 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
215 /// except that it also changes any definitions of the register as well.
217 /// Note that it is usually necessary to first constrain ToReg's register
218 /// class to match the FromReg constraints using:
220 /// constrainRegClass(ToReg, getRegClass(FromReg))
222 /// That function will return NULL if the virtual registers have incompatible
224 void replaceRegWith(unsigned FromReg, unsigned ToReg);
226 /// getRegUseDefListHead - Return the head pointer for the register use/def
227 /// list for the specified virtual or physical register.
228 MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
229 if (TargetRegisterInfo::isVirtualRegister(RegNo))
230 return VRegInfo[RegNo].second;
231 return PhysRegUseDefLists[RegNo];
234 MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
235 if (TargetRegisterInfo::isVirtualRegister(RegNo))
236 return VRegInfo[RegNo].second;
237 return PhysRegUseDefLists[RegNo];
240 /// getVRegDef - Return the machine instr that defines the specified virtual
241 /// register or null if none is found. This assumes that the code is in SSA
242 /// form, so there should only be one definition.
243 MachineInstr *getVRegDef(unsigned Reg) const;
245 /// getUniqueVRegDef - Return the unique machine instr that defines the
246 /// specified virtual register or null if none is found. If there are
247 /// multiple definitions or no definition, return null.
248 MachineInstr *getUniqueVRegDef(unsigned Reg) const;
250 /// clearKillFlags - Iterate over all the uses of the given register and
251 /// clear the kill flag from the MachineOperand. This function is used by
252 /// optimization passes which extend register lifetimes and need only
253 /// preserve conservative kill flag information.
254 void clearKillFlags(unsigned Reg) const;
257 void dumpUses(unsigned RegNo) const;
260 /// isConstantPhysReg - Returns true if PhysReg is unallocatable and constant
261 /// throughout the function. It is safe to move instructions that read such
263 bool isConstantPhysReg(unsigned PhysReg, const MachineFunction &MF) const;
265 //===--------------------------------------------------------------------===//
266 // Virtual Register Info
267 //===--------------------------------------------------------------------===//
269 /// getRegClass - Return the register class of the specified virtual register.
271 const TargetRegisterClass *getRegClass(unsigned Reg) const {
272 return VRegInfo[Reg].first;
275 /// setRegClass - Set the register class of the specified virtual register.
277 void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
279 /// constrainRegClass - Constrain the register class of the specified virtual
280 /// register to be a common subclass of RC and the current register class,
281 /// but only if the new class has at least MinNumRegs registers. Return the
282 /// new register class, or NULL if no such class exists.
283 /// This should only be used when the constraint is known to be trivial, like
284 /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
286 const TargetRegisterClass *constrainRegClass(unsigned Reg,
287 const TargetRegisterClass *RC,
288 unsigned MinNumRegs = 0);
290 /// recomputeRegClass - Try to find a legal super-class of Reg's register
291 /// class that still satisfies the constraints from the instructions using
292 /// Reg. Returns true if Reg was upgraded.
294 /// This method can be used after constraints have been removed from a
295 /// virtual register, for example after removing instructions or splitting
298 bool recomputeRegClass(unsigned Reg, const TargetMachine&);
300 /// createVirtualRegister - Create and return a new virtual register in the
301 /// function with the specified register class.
303 unsigned createVirtualRegister(const TargetRegisterClass *RegClass);
305 /// getNumVirtRegs - Return the number of virtual registers created.
307 unsigned getNumVirtRegs() const { return VRegInfo.size(); }
309 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
310 void clearVirtRegs();
312 /// setRegAllocationHint - Specify a register allocation hint for the
313 /// specified virtual register.
314 void setRegAllocationHint(unsigned Reg, unsigned Type, unsigned PrefReg) {
315 RegAllocHints[Reg].first = Type;
316 RegAllocHints[Reg].second = PrefReg;
319 /// getRegAllocationHint - Return the register allocation hint for the
320 /// specified virtual register.
321 std::pair<unsigned, unsigned>
322 getRegAllocationHint(unsigned Reg) const {
323 return RegAllocHints[Reg];
326 /// getSimpleHint - Return the preferred register allocation hint, or 0 if a
327 /// standard simple hint (Type == 0) is not set.
328 unsigned getSimpleHint(unsigned Reg) const {
329 std::pair<unsigned, unsigned> Hint = getRegAllocationHint(Reg);
330 return Hint.first ? 0 : Hint.second;
334 //===--------------------------------------------------------------------===//
335 // Physical Register Use Info
336 //===--------------------------------------------------------------------===//
338 /// isPhysRegUsed - Return true if the specified register is used in this
339 /// function. This only works after register allocation.
340 bool isPhysRegUsed(unsigned Reg) const {
341 return UsedPhysRegs.test(Reg) || UsedPhysRegMask.test(Reg);
344 /// isPhysRegOrOverlapUsed - Return true if Reg or any overlapping register
345 /// is used in this function.
346 bool isPhysRegOrOverlapUsed(unsigned Reg) const {
347 if (UsedPhysRegMask.test(Reg))
349 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
350 if (UsedPhysRegs.test(*AI))
355 /// setPhysRegUsed - Mark the specified register used in this function.
356 /// This should only be called during and after register allocation.
357 void setPhysRegUsed(unsigned Reg) { UsedPhysRegs.set(Reg); }
359 /// addPhysRegsUsed - Mark the specified registers used in this function.
360 /// This should only be called during and after register allocation.
361 void addPhysRegsUsed(const BitVector &Regs) { UsedPhysRegs |= Regs; }
363 /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
364 /// This corresponds to the bit mask attached to register mask operands.
365 void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
366 UsedPhysRegMask.setBitsNotInMask(RegMask);
369 /// setPhysRegUnused - Mark the specified register unused in this function.
370 /// This should only be called during and after register allocation.
371 void setPhysRegUnused(unsigned Reg) {
372 UsedPhysRegs.reset(Reg);
373 UsedPhysRegMask.reset(Reg);
377 //===--------------------------------------------------------------------===//
378 // Reserved Register Info
379 //===--------------------------------------------------------------------===//
381 // The set of reserved registers must be invariant during register
382 // allocation. For example, the target cannot suddenly decide it needs a
383 // frame pointer when the register allocator has already used the frame
384 // pointer register for something else.
386 // These methods can be used by target hooks like hasFP() to avoid changing
387 // the reserved register set during register allocation.
389 /// freezeReservedRegs - Called by the register allocator to freeze the set
390 /// of reserved registers before allocation begins.
391 void freezeReservedRegs(const MachineFunction&);
393 /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
394 /// to ensure the set of reserved registers stays constant.
395 bool reservedRegsFrozen() const {
396 return !ReservedRegs.empty();
399 /// canReserveReg - Returns true if PhysReg can be used as a reserved
400 /// register. Any register can be reserved before freezeReservedRegs() is
402 bool canReserveReg(unsigned PhysReg) const {
403 return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
407 //===--------------------------------------------------------------------===//
408 // LiveIn/LiveOut Management
409 //===--------------------------------------------------------------------===//
411 /// addLiveIn/Out - Add the specified register as a live in/out. Note that it
412 /// is an error to add the same register to the same set more than once.
413 void addLiveIn(unsigned Reg, unsigned vreg = 0) {
414 LiveIns.push_back(std::make_pair(Reg, vreg));
416 void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
418 // Iteration support for live in/out sets. These sets are kept in sorted
419 // order by their register number.
420 typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
422 typedef std::vector<unsigned>::const_iterator liveout_iterator;
423 livein_iterator livein_begin() const { return LiveIns.begin(); }
424 livein_iterator livein_end() const { return LiveIns.end(); }
425 bool livein_empty() const { return LiveIns.empty(); }
426 liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
427 liveout_iterator liveout_end() const { return LiveOuts.end(); }
428 bool liveout_empty() const { return LiveOuts.empty(); }
430 bool isLiveIn(unsigned Reg) const;
431 bool isLiveOut(unsigned Reg) const;
433 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
434 /// corresponding live-in physical register.
435 unsigned getLiveInPhysReg(unsigned VReg) const;
437 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
438 /// corresponding live-in physical register.
439 unsigned getLiveInVirtReg(unsigned PReg) const;
441 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
442 /// into the given entry block.
443 void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
444 const TargetRegisterInfo &TRI,
445 const TargetInstrInfo &TII);
448 void HandleVRegListReallocation();
451 /// defusechain_iterator - This class provides iterator support for machine
452 /// operands in the function that use or define a specific register. If
453 /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
454 /// returns defs. If neither are true then you are silly and it always
455 /// returns end(). If SkipDebug is true it skips uses marked Debug
456 /// when incrementing.
457 template<bool ReturnUses, bool ReturnDefs, bool SkipDebug>
458 class defusechain_iterator
459 : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
461 explicit defusechain_iterator(MachineOperand *op) : Op(op) {
462 // If the first node isn't one we're interested in, advance to one that
463 // we are interested in.
465 if ((!ReturnUses && op->isUse()) ||
466 (!ReturnDefs && op->isDef()) ||
467 (SkipDebug && op->isDebug()))
471 friend class MachineRegisterInfo;
473 typedef std::iterator<std::forward_iterator_tag,
474 MachineInstr, ptrdiff_t>::reference reference;
475 typedef std::iterator<std::forward_iterator_tag,
476 MachineInstr, ptrdiff_t>::pointer pointer;
478 defusechain_iterator(const defusechain_iterator &I) : Op(I.Op) {}
479 defusechain_iterator() : Op(0) {}
481 bool operator==(const defusechain_iterator &x) const {
484 bool operator!=(const defusechain_iterator &x) const {
485 return !operator==(x);
488 /// atEnd - return true if this iterator is equal to reg_end() on the value.
489 bool atEnd() const { return Op == 0; }
491 // Iterator traversal: forward iteration only
492 defusechain_iterator &operator++() { // Preincrement
493 assert(Op && "Cannot increment end iterator!");
494 Op = Op->getNextOperandForReg();
496 // If this is an operand we don't care about, skip it.
497 while (Op && ((!ReturnUses && Op->isUse()) ||
498 (!ReturnDefs && Op->isDef()) ||
499 (SkipDebug && Op->isDebug())))
500 Op = Op->getNextOperandForReg();
504 defusechain_iterator operator++(int) { // Postincrement
505 defusechain_iterator tmp = *this; ++*this; return tmp;
508 /// skipInstruction - move forward until reaching a different instruction.
509 /// Return the skipped instruction that is no longer pointed to, or NULL if
510 /// already pointing to end().
511 MachineInstr *skipInstruction() {
513 MachineInstr *MI = Op->getParent();
515 while (Op && Op->getParent() == MI);
519 MachineInstr *skipBundle() {
521 MachineInstr *MI = getBundleStart(Op->getParent());
523 while (Op && getBundleStart(Op->getParent()) == MI);
527 MachineOperand &getOperand() const {
528 assert(Op && "Cannot dereference end iterator!");
532 /// getOperandNo - Return the operand # of this MachineOperand in its
534 unsigned getOperandNo() const {
535 assert(Op && "Cannot dereference end iterator!");
536 return Op - &Op->getParent()->getOperand(0);
539 // Retrieve a reference to the current operand.
540 MachineInstr &operator*() const {
541 assert(Op && "Cannot dereference end iterator!");
542 return *Op->getParent();
545 MachineInstr *operator->() const {
546 assert(Op && "Cannot dereference end iterator!");
547 return Op->getParent();
553 } // End llvm namespace