Move LiveRegUnits implementation into .cpp. Comment and format.
[oota-llvm.git] / lib / CodeGen / LiveRegUnits.cpp
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
16 #include "llvm/CodeGen/LiveRegUnits.h"
17 #include "llvm/CodeGen/MachineInstrBundle.h"
18 using namespace llvm;
19
20 /// We assume the high bits of a physical super register are not preserved
21 /// unless the instruction has an implicit-use operand reading the
22 /// super-register or a register unit for the upper bits is available.
23 void LiveRegUnits::removeRegsInMask(const MachineOperand &Op,
24                                     const MCRegisterInfo &MCRI) {
25   const uint32_t *Mask = Op.getRegMask();
26   unsigned Bit = 0;
27   for (unsigned R = 0; R < MCRI.getNumRegs(); ++R) {
28     if ((*Mask & (1u << Bit)) == 0)
29       removeReg(R, MCRI);
30     ++Bit;
31     if (Bit >= 32) {
32       Bit = 0;
33       ++Mask;
34     }
35   }
36 }
37
38 void LiveRegUnits::stepBackward(const MachineInstr &MI,
39                                 const MCRegisterInfo &MCRI) {
40   // Remove defined registers and regmask kills from the set.
41   for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
42     if (O->isReg()) {
43       if (!O->isDef())
44         continue;
45       unsigned Reg = O->getReg();
46       if (Reg == 0)
47         continue;
48       removeReg(Reg, MCRI);
49     } else if (O->isRegMask()) {
50       removeRegsInMask(*O, MCRI);
51     }
52   }
53   // Add uses to the set.
54   for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
55     if (!O->isReg() || !O->readsReg() || O->isUndef())
56       continue;
57     unsigned Reg = O->getReg();
58     if (Reg == 0)
59       continue;
60     addReg(Reg, MCRI);
61   }
62 }
63
64 /// Uses with kill flag get removed from the set, defs added. If possible
65 /// use StepBackward() instead of this function because some kill flags may
66 /// be missing.
67 void LiveRegUnits::stepForward(const MachineInstr &MI,
68                                const MCRegisterInfo &MCRI) {
69   SmallVector<unsigned, 4> Defs;
70   // Remove killed registers from the set.
71   for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
72     if (O->isReg()) {
73       unsigned Reg = O->getReg();
74       if (Reg == 0)
75         continue;
76       if (O->isDef()) {
77         if (!O->isDead())
78           Defs.push_back(Reg);
79       } else {
80         if (!O->isKill())
81           continue;
82         assert(O->isUse());
83         removeReg(Reg, MCRI);
84       }
85     } else if (O->isRegMask()) {
86       removeRegsInMask(*O, MCRI);
87     }
88   }
89   // Add defs to the set.
90   for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
91     addReg(Defs[i], MCRI);
92   }
93 }
94
95 /// Adds all registers in the live-in list of block @p BB.
96 void LiveRegUnits::addLiveIns(const MachineBasicBlock &BB,
97                               const MCRegisterInfo &MCRI) {
98   for (MachineBasicBlock::livein_iterator L = BB.livein_begin(),
99        LE = BB.livein_end(); L != LE; ++L) {
100     addReg(*L, MCRI);
101   }
102 }