1 //===-- llvm/CodeGen/MachineRegisterInfo.h ----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MachineRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
15 #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
16
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/IndexedMap.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBundle.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23 #include "llvm/Target/TargetSubtargetInfo.h"
24 #include <vector>
25
26 namespace llvm {
27 class PSetIterator;
28
29 /// MachineRegisterInfo - Keep track of information for virtual and physical
30 /// registers, including vreg register classes, use/def chains for registers,
31 /// etc.
32 class MachineRegisterInfo {
33 public:
34 class Delegate {
35 virtual void anchor();
36 public:
37 virtual void MRI_NoteNewVirtualRegister(unsigned Reg) = 0;
38
~Delegate()39 virtual ~Delegate() {}
40 };
41
42 private:
43 const MachineFunction *MF;
44 Delegate *TheDelegate;
45
46 /// IsSSA - True when the machine function is in SSA form and virtual
47 /// registers have a single def.
48 bool IsSSA;
49
50 /// TracksLiveness - True while register liveness is being tracked accurately.
51 /// Basic block live-in lists, kill flags, and implicit defs may not be
52 /// accurate when after this flag is cleared.
53 bool TracksLiveness;
54
55 /// True if subregister liveness is tracked.
56 bool TracksSubRegLiveness;
57
58 /// VRegInfo - Information we keep for each virtual register.
59 ///
60 /// Each element in this list contains the register class of the vreg and the
61 /// start of the use/def list for the register.
62 IndexedMap<std::pair<const TargetRegisterClass*, MachineOperand*>,
63 VirtReg2IndexFunctor> VRegInfo;
64
65 /// RegAllocHints - This vector records register allocation hints for virtual
66 /// registers. For each virtual register, it keeps a register and hint type
67 /// pair making up the allocation hint. Hint type is target specific except
68 /// for the value 0 which means the second value of the pair is the preferred
69 /// register for allocation. For example, if the hint is <0, 1024>, it means
70 /// the allocator should prefer the physical register allocated to the virtual
71 /// register of the hint.
72 IndexedMap<std::pair<unsigned, unsigned>, VirtReg2IndexFunctor> RegAllocHints;
73
74 /// PhysRegUseDefLists - This is an array of the head of the use/def list for
75 /// physical registers.
76 std::unique_ptr<MachineOperand *[]> PhysRegUseDefLists;
77
78 /// getRegUseDefListHead - Return the head pointer for the register use/def
79 /// list for the specified virtual or physical register.
getRegUseDefListHead(unsigned RegNo)80 MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
81 if (TargetRegisterInfo::isVirtualRegister(RegNo))
82 return VRegInfo[RegNo].second;
83 return PhysRegUseDefLists[RegNo];
84 }
85
getRegUseDefListHead(unsigned RegNo)86 MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
87 if (TargetRegisterInfo::isVirtualRegister(RegNo))
88 return VRegInfo[RegNo].second;
89 return PhysRegUseDefLists[RegNo];
90 }
91
92 /// Get the next element in the use-def chain.
getNextOperandForReg(const MachineOperand * MO)93 static MachineOperand *getNextOperandForReg(const MachineOperand *MO) {
94 assert(MO && MO->isReg() && "This is not a register operand!");
95 return MO->Contents.Reg.Next;
96 }
97
98 /// UsedPhysRegMask - Additional used physregs including aliases.
99 /// This bit vector represents all the registers clobbered by function calls.
100 BitVector UsedPhysRegMask;
101
102 /// ReservedRegs - This is a bit vector of reserved registers. The target
103 /// may change its mind about which registers should be reserved. This
104 /// vector is the frozen set of reserved registers when register allocation
105 /// started.
106 BitVector ReservedRegs;
107
108 /// Keep track of the physical registers that are live in to the function.
109 /// Live in values are typically arguments in registers. LiveIn values are
110 /// allowed to have virtual registers associated with them, stored in the
111 /// second element.
112 std::vector<std::pair<unsigned, unsigned> > LiveIns;
113
114 MachineRegisterInfo(const MachineRegisterInfo&) = delete;
115 void operator=(const MachineRegisterInfo&) = delete;
116 public:
117 explicit MachineRegisterInfo(const MachineFunction *MF);
118
getTargetRegisterInfo()119 const TargetRegisterInfo *getTargetRegisterInfo() const {
120 return MF->getSubtarget().getRegisterInfo();
121 }
122
resetDelegate(Delegate * delegate)123 void resetDelegate(Delegate *delegate) {
124 // Ensure another delegate does not take over unless the current
125 // delegate first unattaches itself. If we ever need to multicast
126 // notifications, we will need to change to using a list.
127 assert(TheDelegate == delegate &&
128 "Only the current delegate can perform reset!");
129 TheDelegate = nullptr;
130 }
131
setDelegate(Delegate * delegate)132 void setDelegate(Delegate *delegate) {
133 assert(delegate && !TheDelegate &&
134 "Attempted to set delegate to null, or to change it without "
135 "first resetting it!");
136
137 TheDelegate = delegate;
138 }
139
140 //===--------------------------------------------------------------------===//
141 // Function State
142 //===--------------------------------------------------------------------===//
143
144 // isSSA - Returns true when the machine function is in SSA form. Early
145 // passes require the machine function to be in SSA form where every virtual
146 // register has a single defining instruction.
147 //
148 // The TwoAddressInstructionPass and PHIElimination passes take the machine
149 // function out of SSA form when they introduce multiple defs per virtual
150 // register.
isSSA()151 bool isSSA() const { return IsSSA; }
152
153 // leaveSSA - Indicates that the machine function is no longer in SSA form.
leaveSSA()154 void leaveSSA() { IsSSA = false; }
155
156 /// tracksLiveness - Returns true when tracking register liveness accurately.
157 ///
158 /// While this flag is true, register liveness information in basic block
159 /// live-in lists and machine instruction operands is accurate. This means it
160 /// can be used to change the code in ways that affect the values in
161 /// registers, for example by the register scavenger.
162 ///
163 /// When this flag is false, liveness is no longer reliable.
tracksLiveness()164 bool tracksLiveness() const { return TracksLiveness; }
165
166 /// invalidateLiveness - Indicates that register liveness is no longer being
167 /// tracked accurately.
168 ///
169 /// This should be called by late passes that invalidate the liveness
170 /// information.
invalidateLiveness()171 void invalidateLiveness() { TracksLiveness = false; }
172
173 /// Returns true if liveness for register class @p RC should be tracked at
174 /// the subregister level.
shouldTrackSubRegLiveness(const TargetRegisterClass & RC)175 bool shouldTrackSubRegLiveness(const TargetRegisterClass &RC) const {
176 return subRegLivenessEnabled() && RC.HasDisjunctSubRegs;
177 }
shouldTrackSubRegLiveness(unsigned VReg)178 bool shouldTrackSubRegLiveness(unsigned VReg) const {
179 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Must pass a VReg");
180 return shouldTrackSubRegLiveness(*getRegClass(VReg));
181 }
subRegLivenessEnabled()182 bool subRegLivenessEnabled() const {
183 return TracksSubRegLiveness;
184 }
185
186 void enableSubRegLiveness(bool Enable = true) {
187 TracksSubRegLiveness = Enable;
188 }
189
190 //===--------------------------------------------------------------------===//
191 // Register Info
192 //===--------------------------------------------------------------------===//
193
194 // Strictly for use by MachineInstr.cpp.
195 void addRegOperandToUseList(MachineOperand *MO);
196
197 // Strictly for use by MachineInstr.cpp.
198 void removeRegOperandFromUseList(MachineOperand *MO);
199
200 // Strictly for use by MachineInstr.cpp.
201 void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
202
203 /// Verify the sanity of the use list for Reg.
204 void verifyUseList(unsigned Reg) const;
205
206 /// Verify the use list of all registers.
207 void verifyUseLists() const;
208
209 /// reg_begin/reg_end - Provide iteration support to walk over all definitions
210 /// and uses of a register within the MachineFunction that corresponds to this
211 /// MachineRegisterInfo object.
212 template<bool Uses, bool Defs, bool SkipDebug,
213 bool ByOperand, bool ByInstr, bool ByBundle>
214 class defusechain_iterator;
215 template<bool Uses, bool Defs, bool SkipDebug,
216 bool ByOperand, bool ByInstr, bool ByBundle>
217 class defusechain_instr_iterator;
218
219 // Make it a friend so it can access getNextOperandForReg().
220 template<bool, bool, bool, bool, bool, bool>
221 friend class defusechain_iterator;
222 template<bool, bool, bool, bool, bool, bool>
223 friend class defusechain_instr_iterator;
224
225
226
227 /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
228 /// register.
229 typedef defusechain_iterator<true,true,false,true,false,false>
230 reg_iterator;
reg_begin(unsigned RegNo)231 reg_iterator reg_begin(unsigned RegNo) const {
232 return reg_iterator(getRegUseDefListHead(RegNo));
233 }
reg_end()234 static reg_iterator reg_end() { return reg_iterator(nullptr); }
235
reg_operands(unsigned Reg)236 inline iterator_range<reg_iterator> reg_operands(unsigned Reg) const {
237 return make_range(reg_begin(Reg), reg_end());
238 }
239
240 /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses
241 /// of the specified register, stepping by MachineInstr.
242 typedef defusechain_instr_iterator<true,true,false,false,true,false>
243 reg_instr_iterator;
reg_instr_begin(unsigned RegNo)244 reg_instr_iterator reg_instr_begin(unsigned RegNo) const {
245 return reg_instr_iterator(getRegUseDefListHead(RegNo));
246 }
reg_instr_end()247 static reg_instr_iterator reg_instr_end() {
248 return reg_instr_iterator(nullptr);
249 }
250
251 inline iterator_range<reg_instr_iterator>
reg_instructions(unsigned Reg)252 reg_instructions(unsigned Reg) const {
253 return make_range(reg_instr_begin(Reg), reg_instr_end());
254 }
255
256 /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses
257 /// of the specified register, stepping by bundle.
258 typedef defusechain_instr_iterator<true,true,false,false,false,true>
259 reg_bundle_iterator;
reg_bundle_begin(unsigned RegNo)260 reg_bundle_iterator reg_bundle_begin(unsigned RegNo) const {
261 return reg_bundle_iterator(getRegUseDefListHead(RegNo));
262 }
reg_bundle_end()263 static reg_bundle_iterator reg_bundle_end() {
264 return reg_bundle_iterator(nullptr);
265 }
266
reg_bundles(unsigned Reg)267 inline iterator_range<reg_bundle_iterator> reg_bundles(unsigned Reg) const {
268 return make_range(reg_bundle_begin(Reg), reg_bundle_end());
269 }
270
271 /// reg_empty - Return true if there are no instructions using or defining the
272 /// specified register (it may be live-in).
reg_empty(unsigned RegNo)273 bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
274
275 /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
276 /// of the specified register, skipping those marked as Debug.
277 typedef defusechain_iterator<true,true,true,true,false,false>
278 reg_nodbg_iterator;
reg_nodbg_begin(unsigned RegNo)279 reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const {
280 return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
281 }
reg_nodbg_end()282 static reg_nodbg_iterator reg_nodbg_end() {
283 return reg_nodbg_iterator(nullptr);
284 }
285
286 inline iterator_range<reg_nodbg_iterator>
reg_nodbg_operands(unsigned Reg)287 reg_nodbg_operands(unsigned Reg) const {
288 return make_range(reg_nodbg_begin(Reg), reg_nodbg_end());
289 }
290
291 /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk
292 /// all defs and uses of the specified register, stepping by MachineInstr,
293 /// skipping those marked as Debug.
294 typedef defusechain_instr_iterator<true,true,true,false,true,false>
295 reg_instr_nodbg_iterator;
reg_instr_nodbg_begin(unsigned RegNo)296 reg_instr_nodbg_iterator reg_instr_nodbg_begin(unsigned RegNo) const {
297 return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
298 }
reg_instr_nodbg_end()299 static reg_instr_nodbg_iterator reg_instr_nodbg_end() {
300 return reg_instr_nodbg_iterator(nullptr);
301 }
302
303 inline iterator_range<reg_instr_nodbg_iterator>
reg_nodbg_instructions(unsigned Reg)304 reg_nodbg_instructions(unsigned Reg) const {
305 return make_range(reg_instr_nodbg_begin(Reg), reg_instr_nodbg_end());
306 }
307
308 /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk
309 /// all defs and uses of the specified register, stepping by bundle,
310 /// skipping those marked as Debug.
311 typedef defusechain_instr_iterator<true,true,true,false,false,true>
312 reg_bundle_nodbg_iterator;
reg_bundle_nodbg_begin(unsigned RegNo)313 reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(unsigned RegNo) const {
314 return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
315 }
reg_bundle_nodbg_end()316 static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() {
317 return reg_bundle_nodbg_iterator(nullptr);
318 }
319
320 inline iterator_range<reg_bundle_nodbg_iterator>
reg_nodbg_bundles(unsigned Reg)321 reg_nodbg_bundles(unsigned Reg) const {
322 return make_range(reg_bundle_nodbg_begin(Reg), reg_bundle_nodbg_end());
323 }
324
325 /// reg_nodbg_empty - Return true if the only instructions using or defining
326 /// Reg are Debug instructions.
reg_nodbg_empty(unsigned RegNo)327 bool reg_nodbg_empty(unsigned RegNo) const {
328 return reg_nodbg_begin(RegNo) == reg_nodbg_end();
329 }
330
331 /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
332 typedef defusechain_iterator<false,true,false,true,false,false>
333 def_iterator;
def_begin(unsigned RegNo)334 def_iterator def_begin(unsigned RegNo) const {
335 return def_iterator(getRegUseDefListHead(RegNo));
336 }
def_end()337 static def_iterator def_end() { return def_iterator(nullptr); }
338
def_operands(unsigned Reg)339 inline iterator_range<def_iterator> def_operands(unsigned Reg) const {
340 return make_range(def_begin(Reg), def_end());
341 }
342
343 /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the
344 /// specified register, stepping by MachineInst.
345 typedef defusechain_instr_iterator<false,true,false,false,true,false>
346 def_instr_iterator;
def_instr_begin(unsigned RegNo)347 def_instr_iterator def_instr_begin(unsigned RegNo) const {
348 return def_instr_iterator(getRegUseDefListHead(RegNo));
349 }
def_instr_end()350 static def_instr_iterator def_instr_end() {
351 return def_instr_iterator(nullptr);
352 }
353
354 inline iterator_range<def_instr_iterator>
def_instructions(unsigned Reg)355 def_instructions(unsigned Reg) const {
356 return make_range(def_instr_begin(Reg), def_instr_end());
357 }
358
359 /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the
360 /// specified register, stepping by bundle.
361 typedef defusechain_instr_iterator<false,true,false,false,false,true>
362 def_bundle_iterator;
def_bundle_begin(unsigned RegNo)363 def_bundle_iterator def_bundle_begin(unsigned RegNo) const {
364 return def_bundle_iterator(getRegUseDefListHead(RegNo));
365 }
def_bundle_end()366 static def_bundle_iterator def_bundle_end() {
367 return def_bundle_iterator(nullptr);
368 }
369
def_bundles(unsigned Reg)370 inline iterator_range<def_bundle_iterator> def_bundles(unsigned Reg) const {
371 return make_range(def_bundle_begin(Reg), def_bundle_end());
372 }
373
374 /// def_empty - Return true if there are no instructions defining the
375 /// specified register (it may be live-in).
def_empty(unsigned RegNo)376 bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
377
378 /// hasOneDef - Return true if there is exactly one instruction defining the
379 /// specified register.
hasOneDef(unsigned RegNo)380 bool hasOneDef(unsigned RegNo) const {
381 def_iterator DI = def_begin(RegNo);
382 if (DI == def_end())
383 return false;
384 return ++DI == def_end();
385 }
386
387 /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
388 typedef defusechain_iterator<true,false,false,true,false,false>
389 use_iterator;
use_begin(unsigned RegNo)390 use_iterator use_begin(unsigned RegNo) const {
391 return use_iterator(getRegUseDefListHead(RegNo));
392 }
use_end()393 static use_iterator use_end() { return use_iterator(nullptr); }
394
use_operands(unsigned Reg)395 inline iterator_range<use_iterator> use_operands(unsigned Reg) const {
396 return make_range(use_begin(Reg), use_end());
397 }
398
399 /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the
400 /// specified register, stepping by MachineInstr.
401 typedef defusechain_instr_iterator<true,false,false,false,true,false>
402 use_instr_iterator;
use_instr_begin(unsigned RegNo)403 use_instr_iterator use_instr_begin(unsigned RegNo) const {
404 return use_instr_iterator(getRegUseDefListHead(RegNo));
405 }
use_instr_end()406 static use_instr_iterator use_instr_end() {
407 return use_instr_iterator(nullptr);
408 }
409
410 inline iterator_range<use_instr_iterator>
use_instructions(unsigned Reg)411 use_instructions(unsigned Reg) const {
412 return make_range(use_instr_begin(Reg), use_instr_end());
413 }
414
415 /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the
416 /// specified register, stepping by bundle.
417 typedef defusechain_instr_iterator<true,false,false,false,false,true>
418 use_bundle_iterator;
use_bundle_begin(unsigned RegNo)419 use_bundle_iterator use_bundle_begin(unsigned RegNo) const {
420 return use_bundle_iterator(getRegUseDefListHead(RegNo));
421 }
use_bundle_end()422 static use_bundle_iterator use_bundle_end() {
423 return use_bundle_iterator(nullptr);
424 }
425
use_bundles(unsigned Reg)426 inline iterator_range<use_bundle_iterator> use_bundles(unsigned Reg) const {
427 return make_range(use_bundle_begin(Reg), use_bundle_end());
428 }
429
430 /// use_empty - Return true if there are no instructions using the specified
431 /// register.
use_empty(unsigned RegNo)432 bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
433
434 /// hasOneUse - Return true if there is exactly one instruction using the
435 /// specified register.
hasOneUse(unsigned RegNo)436 bool hasOneUse(unsigned RegNo) const {
437 use_iterator UI = use_begin(RegNo);
438 if (UI == use_end())
439 return false;
440 return ++UI == use_end();
441 }
442
443 /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
444 /// specified register, skipping those marked as Debug.
445 typedef defusechain_iterator<true,false,true,true,false,false>
446 use_nodbg_iterator;
use_nodbg_begin(unsigned RegNo)447 use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const {
448 return use_nodbg_iterator(getRegUseDefListHead(RegNo));
449 }
use_nodbg_end()450 static use_nodbg_iterator use_nodbg_end() {
451 return use_nodbg_iterator(nullptr);
452 }
453
454 inline iterator_range<use_nodbg_iterator>
use_nodbg_operands(unsigned Reg)455 use_nodbg_operands(unsigned Reg) const {
456 return make_range(use_nodbg_begin(Reg), use_nodbg_end());
457 }
458
459 /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk
460 /// all uses of the specified register, stepping by MachineInstr, skipping
461 /// those marked as Debug.
462 typedef defusechain_instr_iterator<true,false,true,false,true,false>
463 use_instr_nodbg_iterator;
use_instr_nodbg_begin(unsigned RegNo)464 use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const {
465 return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
466 }
use_instr_nodbg_end()467 static use_instr_nodbg_iterator use_instr_nodbg_end() {
468 return use_instr_nodbg_iterator(nullptr);
469 }
470
471 inline iterator_range<use_instr_nodbg_iterator>
use_nodbg_instructions(unsigned Reg)472 use_nodbg_instructions(unsigned Reg) const {
473 return make_range(use_instr_nodbg_begin(Reg), use_instr_nodbg_end());
474 }
475
476 /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk
477 /// all uses of the specified register, stepping by bundle, skipping
478 /// those marked as Debug.
479 typedef defusechain_instr_iterator<true,false,true,false,false,true>
480 use_bundle_nodbg_iterator;
use_bundle_nodbg_begin(unsigned RegNo)481 use_bundle_nodbg_iterator use_bundle_nodbg_begin(unsigned RegNo) const {
482 return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
483 }
use_bundle_nodbg_end()484 static use_bundle_nodbg_iterator use_bundle_nodbg_end() {
485 return use_bundle_nodbg_iterator(nullptr);
486 }
487
488 inline iterator_range<use_bundle_nodbg_iterator>
use_nodbg_bundles(unsigned Reg)489 use_nodbg_bundles(unsigned Reg) const {
490 return make_range(use_bundle_nodbg_begin(Reg), use_bundle_nodbg_end());
491 }
492
493 /// use_nodbg_empty - Return true if there are no non-Debug instructions
494 /// using the specified register.
use_nodbg_empty(unsigned RegNo)495 bool use_nodbg_empty(unsigned RegNo) const {
496 return use_nodbg_begin(RegNo) == use_nodbg_end();
497 }
498
499 /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
500 /// instruction using the specified register.
501 bool hasOneNonDBGUse(unsigned RegNo) const;
502
503 /// replaceRegWith - Replace all instances of FromReg with ToReg in the
504 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
505 /// except that it also changes any definitions of the register as well.
506 ///
507 /// Note that it is usually necessary to first constrain ToReg's register
508 /// class to match the FromReg constraints using:
509 ///
510 /// constrainRegClass(ToReg, getRegClass(FromReg))
511 ///
512 /// That function will return NULL if the virtual registers have incompatible
513 /// constraints.
514 ///
515 /// Note that if ToReg is a physical register the function will replace and
516 /// apply sub registers to ToReg in order to obtain a final/proper physical
517 /// register.
518 void replaceRegWith(unsigned FromReg, unsigned ToReg);
519
520 /// getVRegDef - Return the machine instr that defines the specified virtual
521 /// register or null if none is found. This assumes that the code is in SSA
522 /// form, so there should only be one definition.
523 MachineInstr *getVRegDef(unsigned Reg) const;
524
525 /// getUniqueVRegDef - Return the unique machine instr that defines the
526 /// specified virtual register or null if none is found. If there are
527 /// multiple definitions or no definition, return null.
528 MachineInstr *getUniqueVRegDef(unsigned Reg) const;
529
530 /// clearKillFlags - Iterate over all the uses of the given register and
531 /// clear the kill flag from the MachineOperand. This function is used by
532 /// optimization passes which extend register lifetimes and need only
533 /// preserve conservative kill flag information.
534 void clearKillFlags(unsigned Reg) const;
535
536 #ifndef NDEBUG
537 void dumpUses(unsigned RegNo) const;
538 #endif
539
540 /// isConstantPhysReg - Returns true if PhysReg is unallocatable and constant
541 /// throughout the function. It is safe to move instructions that read such
542 /// a physreg.
543 bool isConstantPhysReg(unsigned PhysReg, const MachineFunction &MF) const;
544
545 /// Get an iterator over the pressure sets affected by the given physical or
546 /// virtual register. If RegUnit is physical, it must be a register unit (from
547 /// MCRegUnitIterator).
548 PSetIterator getPressureSets(unsigned RegUnit) const;
549
550 //===--------------------------------------------------------------------===//
551 // Virtual Register Info
552 //===--------------------------------------------------------------------===//
553
554 /// getRegClass - Return the register class of the specified virtual register.
555 ///
getRegClass(unsigned Reg)556 const TargetRegisterClass *getRegClass(unsigned Reg) const {
557 return VRegInfo[Reg].first;
558 }
559
560 /// setRegClass - Set the register class of the specified virtual register.
561 ///
562 void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
563
564 /// constrainRegClass - Constrain the register class of the specified virtual
565 /// register to be a common subclass of RC and the current register class,
566 /// but only if the new class has at least MinNumRegs registers. Return the
567 /// new register class, or NULL if no such class exists.
568 /// This should only be used when the constraint is known to be trivial, like
569 /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
570 ///
571 const TargetRegisterClass *constrainRegClass(unsigned Reg,
572 const TargetRegisterClass *RC,
573 unsigned MinNumRegs = 0);
574
575 /// recomputeRegClass - Try to find a legal super-class of Reg's register
576 /// class that still satisfies the constraints from the instructions using
577 /// Reg. Returns true if Reg was upgraded.
578 ///
579 /// This method can be used after constraints have been removed from a
580 /// virtual register, for example after removing instructions or splitting
581 /// the live range.
582 ///
583 bool recomputeRegClass(unsigned Reg);
584
585 /// createVirtualRegister - Create and return a new virtual register in the
586 /// function with the specified register class.
587 ///
588 unsigned createVirtualRegister(const TargetRegisterClass *RegClass);
589
590 /// getNumVirtRegs - Return the number of virtual registers created.
591 ///
getNumVirtRegs()592 unsigned getNumVirtRegs() const { return VRegInfo.size(); }
593
594 /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
595 void clearVirtRegs();
596
597 /// setRegAllocationHint - Specify a register allocation hint for the
598 /// specified virtual register.
setRegAllocationHint(unsigned VReg,unsigned Type,unsigned PrefReg)599 void setRegAllocationHint(unsigned VReg, unsigned Type, unsigned PrefReg) {
600 assert(TargetRegisterInfo::isVirtualRegister(VReg));
601 RegAllocHints[VReg].first = Type;
602 RegAllocHints[VReg].second = PrefReg;
603 }
604
605 /// Specify the preferred register allocation hint for the specified virtual
606 /// register.
setSimpleHint(unsigned VReg,unsigned PrefReg)607 void setSimpleHint(unsigned VReg, unsigned PrefReg) {
608 setRegAllocationHint(VReg, /*Type=*/0, PrefReg);
609 }
610
611 /// getRegAllocationHint - Return the register allocation hint for the
612 /// specified virtual register.
613 std::pair<unsigned, unsigned>
getRegAllocationHint(unsigned VReg)614 getRegAllocationHint(unsigned VReg) const {
615 assert(TargetRegisterInfo::isVirtualRegister(VReg));
616 return RegAllocHints[VReg];
617 }
618
619 /// getSimpleHint - Return the preferred register allocation hint, or 0 if a
620 /// standard simple hint (Type == 0) is not set.
getSimpleHint(unsigned VReg)621 unsigned getSimpleHint(unsigned VReg) const {
622 assert(TargetRegisterInfo::isVirtualRegister(VReg));
623 std::pair<unsigned, unsigned> Hint = getRegAllocationHint(VReg);
624 return Hint.first ? 0 : Hint.second;
625 }
626
627 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
628 /// specified register as undefined which causes the DBG_VALUE to be
629 /// deleted during LiveDebugVariables analysis.
630 void markUsesInDebugValueAsUndef(unsigned Reg) const;
631
632 /// Return true if the specified register is modified in this function.
633 /// This checks that no defining machine operands exist for the register or
634 /// any of its aliases. Definitions found on functions marked noreturn are
635 /// ignored. The register is also considered modified when it is set in the
636 /// UsedPhysRegMask.
637 bool isPhysRegModified(unsigned PhysReg) const;
638
639 /// Return true if the specified register is modified or read in this
640 /// function. This checks that no machine operands exist for the register or
641 /// any of its aliases. The register is also considered used when it is set
642 /// in the UsedPhysRegMask.
643 bool isPhysRegUsed(unsigned PhysReg) const;
644
645 /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
646 /// This corresponds to the bit mask attached to register mask operands.
addPhysRegsUsedFromRegMask(const uint32_t * RegMask)647 void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
648 UsedPhysRegMask.setBitsNotInMask(RegMask);
649 }
650
getUsedPhysRegsMask()651 const BitVector &getUsedPhysRegsMask() const { return UsedPhysRegMask; }
652
setUsedPhysRegMask(BitVector & Mask)653 void setUsedPhysRegMask(BitVector &Mask) { UsedPhysRegMask = Mask; }
654
655 //===--------------------------------------------------------------------===//
656 // Reserved Register Info
657 //===--------------------------------------------------------------------===//
658 //
659 // The set of reserved registers must be invariant during register
660 // allocation. For example, the target cannot suddenly decide it needs a
661 // frame pointer when the register allocator has already used the frame
662 // pointer register for something else.
663 //
664 // These methods can be used by target hooks like hasFP() to avoid changing
665 // the reserved register set during register allocation.
666
667 /// freezeReservedRegs - Called by the register allocator to freeze the set
668 /// of reserved registers before allocation begins.
669 void freezeReservedRegs(const MachineFunction&);
670
671 /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
672 /// to ensure the set of reserved registers stays constant.
reservedRegsFrozen()673 bool reservedRegsFrozen() const {
674 return !ReservedRegs.empty();
675 }
676
677 /// canReserveReg - Returns true if PhysReg can be used as a reserved
678 /// register. Any register can be reserved before freezeReservedRegs() is
679 /// called.
canReserveReg(unsigned PhysReg)680 bool canReserveReg(unsigned PhysReg) const {
681 return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
682 }
683
684 /// getReservedRegs - Returns a reference to the frozen set of reserved
685 /// registers. This method should always be preferred to calling
686 /// TRI::getReservedRegs() when possible.
getReservedRegs()687 const BitVector &getReservedRegs() const {
688 assert(reservedRegsFrozen() &&
689 "Reserved registers haven't been frozen yet. "
690 "Use TRI::getReservedRegs().");
691 return ReservedRegs;
692 }
693
694 /// isReserved - Returns true when PhysReg is a reserved register.
695 ///
696 /// Reserved registers may belong to an allocatable register class, but the
697 /// target has explicitly requested that they are not used.
698 ///
isReserved(unsigned PhysReg)699 bool isReserved(unsigned PhysReg) const {
700 return getReservedRegs().test(PhysReg);
701 }
702
703 /// isAllocatable - Returns true when PhysReg belongs to an allocatable
704 /// register class and it hasn't been reserved.
705 ///
706 /// Allocatable registers may show up in the allocation order of some virtual
707 /// register, so a register allocator needs to track its liveness and
708 /// availability.
isAllocatable(unsigned PhysReg)709 bool isAllocatable(unsigned PhysReg) const {
710 return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) &&
711 !isReserved(PhysReg);
712 }
713
714 //===--------------------------------------------------------------------===//
715 // LiveIn Management
716 //===--------------------------------------------------------------------===//
717
718 /// addLiveIn - Add the specified register as a live-in. Note that it
719 /// is an error to add the same register to the same set more than once.
720 void addLiveIn(unsigned Reg, unsigned vreg = 0) {
721 LiveIns.push_back(std::make_pair(Reg, vreg));
722 }
723
724 // Iteration support for the live-ins set. It's kept in sorted order
725 // by register number.
726 typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
727 livein_iterator;
livein_begin()728 livein_iterator livein_begin() const { return LiveIns.begin(); }
livein_end()729 livein_iterator livein_end() const { return LiveIns.end(); }
livein_empty()730 bool livein_empty() const { return LiveIns.empty(); }
731
732 bool isLiveIn(unsigned Reg) const;
733
734 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
735 /// corresponding live-in physical register.
736 unsigned getLiveInPhysReg(unsigned VReg) const;
737
738 /// getLiveInVirtReg - If PReg is a live-in physical register, return the
739 /// corresponding live-in physical register.
740 unsigned getLiveInVirtReg(unsigned PReg) const;
741
742 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
743 /// into the given entry block.
744 void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
745 const TargetRegisterInfo &TRI,
746 const TargetInstrInfo &TII);
747
748 /// Returns a mask covering all bits that can appear in lane masks of
749 /// subregisters of the virtual register @p Reg.
750 LaneBitmask getMaxLaneMaskForVReg(unsigned Reg) const;
751
752 /// defusechain_iterator - This class provides iterator support for machine
753 /// operands in the function that use or define a specific register. If
754 /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
755 /// returns defs. If neither are true then you are silly and it always
756 /// returns end(). If SkipDebug is true it skips uses marked Debug
757 /// when incrementing.
758 template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
759 bool ByOperand, bool ByInstr, bool ByBundle>
760 class defusechain_iterator
761 : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
762 MachineOperand *Op;
defusechain_iterator(MachineOperand * op)763 explicit defusechain_iterator(MachineOperand *op) : Op(op) {
764 // If the first node isn't one we're interested in, advance to one that
765 // we are interested in.
766 if (op) {
767 if ((!ReturnUses && op->isUse()) ||
768 (!ReturnDefs && op->isDef()) ||
769 (SkipDebug && op->isDebug()))
770 advance();
771 }
772 }
773 friend class MachineRegisterInfo;
774
advance()775 void advance() {
776 assert(Op && "Cannot increment end iterator!");
777 Op = getNextOperandForReg(Op);
778
779 // All defs come before the uses, so stop def_iterator early.
780 if (!ReturnUses) {
781 if (Op) {
782 if (Op->isUse())
783 Op = nullptr;
784 else
785 assert(!Op->isDebug() && "Can't have debug defs");
786 }
787 } else {
788 // If this is an operand we don't care about, skip it.
789 while (Op && ((!ReturnDefs && Op->isDef()) ||
790 (SkipDebug && Op->isDebug())))
791 Op = getNextOperandForReg(Op);
792 }
793 }
794 public:
795 typedef std::iterator<std::forward_iterator_tag,
796 MachineInstr, ptrdiff_t>::reference reference;
797 typedef std::iterator<std::forward_iterator_tag,
798 MachineInstr, ptrdiff_t>::pointer pointer;
799
defusechain_iterator()800 defusechain_iterator() : Op(nullptr) {}
801
802 bool operator==(const defusechain_iterator &x) const {
803 return Op == x.Op;
804 }
805 bool operator!=(const defusechain_iterator &x) const {
806 return !operator==(x);
807 }
808
809 /// atEnd - return true if this iterator is equal to reg_end() on the value.
atEnd()810 bool atEnd() const { return Op == nullptr; }
811
812 // Iterator traversal: forward iteration only
813 defusechain_iterator &operator++() { // Preincrement
814 assert(Op && "Cannot increment end iterator!");
815 if (ByOperand)
816 advance();
817 else if (ByInstr) {
818 MachineInstr *P = Op->getParent();
819 do {
820 advance();
821 } while (Op && Op->getParent() == P);
822 } else if (ByBundle) {
823 MachineInstr *P = getBundleStart(Op->getParent());
824 do {
825 advance();
826 } while (Op && getBundleStart(Op->getParent()) == P);
827 }
828
829 return *this;
830 }
831 defusechain_iterator operator++(int) { // Postincrement
832 defusechain_iterator tmp = *this; ++*this; return tmp;
833 }
834
835 /// getOperandNo - Return the operand # of this MachineOperand in its
836 /// MachineInstr.
getOperandNo()837 unsigned getOperandNo() const {
838 assert(Op && "Cannot dereference end iterator!");
839 return Op - &Op->getParent()->getOperand(0);
840 }
841
842 // Retrieve a reference to the current operand.
843 MachineOperand &operator*() const {
844 assert(Op && "Cannot dereference end iterator!");
845 return *Op;
846 }
847
848 MachineOperand *operator->() const {
849 assert(Op && "Cannot dereference end iterator!");
850 return Op;
851 }
852 };
853
854 /// defusechain_iterator - This class provides iterator support for machine
855 /// operands in the function that use or define a specific register. If
856 /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
857 /// returns defs. If neither are true then you are silly and it always
858 /// returns end(). If SkipDebug is true it skips uses marked Debug
859 /// when incrementing.
860 template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
861 bool ByOperand, bool ByInstr, bool ByBundle>
862 class defusechain_instr_iterator
863 : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
864 MachineOperand *Op;
defusechain_instr_iterator(MachineOperand * op)865 explicit defusechain_instr_iterator(MachineOperand *op) : Op(op) {
866 // If the first node isn't one we're interested in, advance to one that
867 // we are interested in.
868 if (op) {
869 if ((!ReturnUses && op->isUse()) ||
870 (!ReturnDefs && op->isDef()) ||
871 (SkipDebug && op->isDebug()))
872 advance();
873 }
874 }
875 friend class MachineRegisterInfo;
876
advance()877 void advance() {
878 assert(Op && "Cannot increment end iterator!");
879 Op = getNextOperandForReg(Op);
880
881 // All defs come before the uses, so stop def_iterator early.
882 if (!ReturnUses) {
883 if (Op) {
884 if (Op->isUse())
885 Op = nullptr;
886 else
887 assert(!Op->isDebug() && "Can't have debug defs");
888 }
889 } else {
890 // If this is an operand we don't care about, skip it.
891 while (Op && ((!ReturnDefs && Op->isDef()) ||
892 (SkipDebug && Op->isDebug())))
893 Op = getNextOperandForReg(Op);
894 }
895 }
896 public:
897 typedef std::iterator<std::forward_iterator_tag,
898 MachineInstr, ptrdiff_t>::reference reference;
899 typedef std::iterator<std::forward_iterator_tag,
900 MachineInstr, ptrdiff_t>::pointer pointer;
901
defusechain_instr_iterator()902 defusechain_instr_iterator() : Op(nullptr) {}
903
904 bool operator==(const defusechain_instr_iterator &x) const {
905 return Op == x.Op;
906 }
907 bool operator!=(const defusechain_instr_iterator &x) const {
908 return !operator==(x);
909 }
910
911 /// atEnd - return true if this iterator is equal to reg_end() on the value.
atEnd()912 bool atEnd() const { return Op == nullptr; }
913
914 // Iterator traversal: forward iteration only
915 defusechain_instr_iterator &operator++() { // Preincrement
916 assert(Op && "Cannot increment end iterator!");
917 if (ByOperand)
918 advance();
919 else if (ByInstr) {
920 MachineInstr *P = Op->getParent();
921 do {
922 advance();
923 } while (Op && Op->getParent() == P);
924 } else if (ByBundle) {
925 MachineInstr *P = getBundleStart(Op->getParent());
926 do {
927 advance();
928 } while (Op && getBundleStart(Op->getParent()) == P);
929 }
930
931 return *this;
932 }
933 defusechain_instr_iterator operator++(int) { // Postincrement
934 defusechain_instr_iterator tmp = *this; ++*this; return tmp;
935 }
936
937 // Retrieve a reference to the current operand.
938 MachineInstr &operator*() const {
939 assert(Op && "Cannot dereference end iterator!");
940 if (ByBundle) return *(getBundleStart(Op->getParent()));
941 return *Op->getParent();
942 }
943
944 MachineInstr *operator->() const {
945 assert(Op && "Cannot dereference end iterator!");
946 if (ByBundle) return getBundleStart(Op->getParent());
947 return Op->getParent();
948 }
949 };
950 };
951
952 /// Iterate over the pressure sets affected by the given physical or virtual
953 /// register. If Reg is physical, it must be a register unit (from
954 /// MCRegUnitIterator).
955 class PSetIterator {
956 const int *PSet;
957 unsigned Weight;
958 public:
PSetIterator()959 PSetIterator(): PSet(nullptr), Weight(0) {}
PSetIterator(unsigned RegUnit,const MachineRegisterInfo * MRI)960 PSetIterator(unsigned RegUnit, const MachineRegisterInfo *MRI) {
961 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
962 if (TargetRegisterInfo::isVirtualRegister(RegUnit)) {
963 const TargetRegisterClass *RC = MRI->getRegClass(RegUnit);
964 PSet = TRI->getRegClassPressureSets(RC);
965 Weight = TRI->getRegClassWeight(RC).RegWeight;
966 }
967 else {
968 PSet = TRI->getRegUnitPressureSets(RegUnit);
969 Weight = TRI->getRegUnitWeight(RegUnit);
970 }
971 if (*PSet == -1)
972 PSet = nullptr;
973 }
isValid()974 bool isValid() const { return PSet; }
975
getWeight()976 unsigned getWeight() const { return Weight; }
977
978 unsigned operator*() const { return *PSet; }
979
980 void operator++() {
981 assert(isValid() && "Invalid PSetIterator.");
982 ++PSet;
983 if (*PSet == -1)
984 PSet = nullptr;
985 }
986 };
987
988 inline PSetIterator MachineRegisterInfo::
getPressureSets(unsigned RegUnit)989 getPressureSets(unsigned RegUnit) const {
990 return PSetIterator(RegUnit, this);
991 }
992
993 } // End llvm namespace
994
995 #endif
996