1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
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 implements the LiveRegUnits utility for tracking liveness of
11 // physical register units across machine instructions in forward or backward
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/LiveRegUnits.h"
17 #include "llvm/CodeGen/MachineInstrBundle.h"
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))
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.
36 void LiveRegUnits::removeRegsInMask(const MachineOperand &Op,
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);
47 void LiveRegUnits::stepBackward(const MachineInstr &MI,
48 const MCRegisterInfo &MCRI) {
49 // Remove defined registers and regmask kills from the set.
50 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
54 unsigned Reg = O->getReg();
58 } else if (O->isRegMask()) {
59 removeRegsInMask(*O, MCRI);
62 // Add uses to the set.
63 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
64 if (!O->isReg() || !O->readsReg() || O->isUndef())
66 unsigned Reg = O->getReg();
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
76 void LiveRegUnits::stepForward(const MachineInstr &MI,
77 const MCRegisterInfo &MCRI) {
78 SmallVector<unsigned, 4> Defs;
79 // Remove killed registers from the set.
80 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
82 unsigned Reg = O->getReg();
94 } else if (O->isRegMask()) {
95 removeRegsInMask(*O, MCRI);
98 // Add defs to the set.
99 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
100 addReg(Defs[i], MCRI);
104 /// Adds all registers in the live-in list of block @p BB.
105 void LiveRegUnits::addLiveIns(const MachineBasicBlock *MBB,
106 const MCRegisterInfo &MCRI) {
107 for (MachineBasicBlock::livein_iterator L = MBB->livein_begin(),
108 LE = MBB->livein_end(); L != LE; ++L) {