LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LiveRegUnits.cpp
Go to the documentation of this file.
1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
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 implements the LiveRegUnits utility for tracking liveness of
11 // physical register units across machine instructions in forward or backward
12 // order.
13 //
14 //===----------------------------------------------------------------------===//
15 
18 using namespace llvm;
19 
20 /// Return true if the given MachineOperand clobbers the given register unit.
21 /// A register unit is only clobbered if all its super-registers are clobbered.
22 static bool operClobbersUnit(const MachineOperand *MO, unsigned Unit,
23  const MCRegisterInfo *MCRI) {
24  for (MCRegUnitRootIterator RI(Unit, MCRI); RI.isValid(); ++RI) {
25  for (MCSuperRegIterator SI(*RI, MCRI, true); SI.isValid(); ++SI) {
26  if (!MO->clobbersPhysReg(*SI))
27  return false;
28  }
29  }
30  return true;
31 }
32 
33 /// We assume the high bits of a physical super register are not preserved
34 /// unless the instruction has an implicit-use operand reading the
35 /// super-register or a register unit for the upper bits is available.
37  const MCRegisterInfo &MCRI) {
38  SparseSet<unsigned>::iterator LUI = LiveUnits.begin();
39  while (LUI != LiveUnits.end()) {
40  if (operClobbersUnit(&Op, *LUI, &MCRI))
41  LUI = LiveUnits.erase(LUI);
42  else
43  ++LUI;
44  }
45 }
46 
48  const MCRegisterInfo &MCRI) {
49  // Remove defined registers and regmask kills from the set.
50  for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
51  if (O->isReg()) {
52  if (!O->isDef())
53  continue;
54  unsigned Reg = O->getReg();
55  if (Reg == 0)
56  continue;
57  removeReg(Reg, MCRI);
58  } else if (O->isRegMask()) {
59  removeRegsInMask(*O, MCRI);
60  }
61  }
62  // Add uses to the set.
63  for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
64  if (!O->isReg() || !O->readsReg() || O->isUndef())
65  continue;
66  unsigned Reg = O->getReg();
67  if (Reg == 0)
68  continue;
69  addReg(Reg, MCRI);
70  }
71 }
72 
73 /// Uses with kill flag get removed from the set, defs added. If possible
74 /// use StepBackward() instead of this function because some kill flags may
75 /// be missing.
77  const MCRegisterInfo &MCRI) {
79  // Remove killed registers from the set.
80  for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
81  if (O->isReg()) {
82  unsigned Reg = O->getReg();
83  if (Reg == 0)
84  continue;
85  if (O->isDef()) {
86  if (!O->isDead())
87  Defs.push_back(Reg);
88  } else {
89  if (!O->isKill())
90  continue;
91  assert(O->isUse());
92  removeReg(Reg, MCRI);
93  }
94  } else if (O->isRegMask()) {
95  removeRegsInMask(*O, MCRI);
96  }
97  }
98  // Add defs to the set.
99  for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
100  addReg(Defs[i], MCRI);
101  }
102 }
103 
104 /// Adds all registers in the live-in list of block @p BB.
106  const MCRegisterInfo &MCRI) {
108  LE = MBB->livein_end(); L != LE; ++L) {
109  addReg(*L, MCRI);
110  }
111 }
void push_back(const T &Elt)
Definition: SmallVector.h:236
bool isValid() const
Check if the iterator is at the end of the list.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
std::vector< unsigned >::const_iterator livein_iterator
void stepForward(const MachineInstr &MI, const MCRegisterInfo &MCRI)
Simulates liveness when stepping forward over an instruction(bundle): Remove killed-uses, add defs.
livein_iterator livein_begin() const
const_iterator end() const
Definition: SparseSet.h:170
MCRegUnitRootIterator enumerates the root registers of a register unit.
iterator erase(iterator I)
Definition: SparseSet.h:277
static bool operClobbersUnit(const MachineOperand *MO, unsigned Unit, const MCRegisterInfo *MCRI)
void addLiveIns(const MachineBasicBlock *MBB, const MCRegisterInfo &MCRI)
Adds all registers in the live-in list of block BB.
livein_iterator livein_end() const
void removeRegsInMask(const MachineOperand &Op, const MCRegisterInfo &MCRI)
Removes registers clobbered by the regmask operand Op.
const_iterator begin() const
Definition: SparseSet.h:169
void addReg(unsigned Reg, const MCRegisterInfo &MCRI)
Adds a register to the set.
Definition: LiveRegUnits.h:50
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
void stepBackward(const MachineInstr &MI, const MCRegisterInfo &MCRI)
Simulates liveness when stepping backwards over an instruction(bundle): Remove Defs, add uses.
bool isValid() const
isValid - Returns true until all the operands have been visited.
void removeReg(unsigned Reg, const MCRegisterInfo &MCRI)
Removes a register from the set.
Definition: LiveRegUnits.h:56