OSDN Git Service

CodeGen: Add/Factor out LiveRegUnits class; NFCI
[android-x86/external-llvm.git] / include / llvm / CodeGen / LiveRegUnits.h
1 //===- llvm/CodeGen/LiveRegUnits.h - Register Unit Set ----------*- 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 /// \file
11 /// A set of register units. It is intended for register liveness tracking.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_LIVEREGUNITS_H
16 #define LLVM_CODEGEN_LIVEREGUNITS_H
17
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20
21 namespace llvm {
22
23 class MachineInstr;
24 class MachineBasicBlock;
25
26 /// A set of register units used to track register liveness.
27 class LiveRegUnits {
28   const TargetRegisterInfo *TRI;
29   BitVector Units;
30
31 public:
32   /// Constructs a new empty LiveRegUnits set.
33   LiveRegUnits() : TRI(nullptr) {}
34
35   /// Constructs and initialize an empty LiveRegUnits set.
36   LiveRegUnits(const TargetRegisterInfo &TRI) {
37     init(TRI);
38   }
39
40   /// Initialize and clear the set.
41   void init(const TargetRegisterInfo &TRI) {
42     this->TRI = &TRI;
43     Units.reset();
44     Units.resize(TRI.getNumRegUnits());
45   }
46
47   /// Clears the set.
48   void clear() { Units.reset(); }
49
50   /// Returns true if the set is empty.
51   bool empty() const { return Units.empty(); }
52
53   /// Adds register units covered by physical register \p Reg.
54   void addReg(unsigned Reg) {
55     for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit)
56       Units.set(*Unit);
57   }
58
59   /// \brief Adds register units covered by physical register \p Reg that are
60   /// part of the lanemask \p Mask.
61   void addRegMasked(unsigned Reg, LaneBitmask Mask) {
62     for (MCRegUnitMaskIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) {
63       LaneBitmask UnitMask = (*Unit).second;
64       if (UnitMask.none() || (UnitMask & Mask).any())
65         Units.set((*Unit).first);
66     }
67   }
68
69   /// Removes all register units covered by physical register \p Reg.
70   void removeReg(unsigned Reg) {
71     for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit)
72       Units.reset(*Unit);
73   }
74
75   /// Removes register units not preserved by the regmask \p RegMask.
76   /// The regmask has the same format as the one in the RegMask machine operand.
77   void removeRegsNotPreserved(const uint32_t *RegMask);
78
79   /// Returns true if no part of physical register \p Reg is live.
80   bool available(unsigned Reg) const {
81     for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) {
82       if (Units.test(*Unit))
83         return false;
84     }
85     return true;
86   }
87
88   /// Updates liveness when stepping backwards over the instruction \p MI.
89   void stepBackward(const MachineInstr &MI);
90
91   /// Adds registers living out of block \p MBB.
92   /// Live out registers are the union of the live-in registers of the successor
93   /// blocks and pristine registers. Live out registers of the end block are the
94   /// callee saved registers.
95   void addLiveOuts(const MachineBasicBlock &MBB);
96
97   /// Adds registers living into block \p MBB.
98   void addLiveIns(const MachineBasicBlock &MBB);
99
100   /// Adds all register units marked in the bitvector \p RegUnits.
101   void addUnits(const BitVector &RegUnits) {
102     Units |= RegUnits;
103   }
104   /// Removes all register units marked in the bitvector \p RegUnits.
105   void removeUnits(const BitVector &RegUnits) {
106     Units.reset(RegUnits);
107   }
108   /// Return the internal bitvector representation of the set.
109   const BitVector &getBitVector() const {
110     return Units;
111   }
112 };
113
114 } // namespace llvm
115
116 #endif