Some code cleanups from Chris
[oota-llvm.git] / lib / CodeGen / PhysRegTracker.h
1 //===-- llvm/CodeGen/PhysRegTracker.h - Physical Register Tracker -*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a physical register tracker. The tracker
11 // tracks physical register usage through addRegUse and
12 // delRegUse. isRegAvail checks if a physical register is available or
13 // not taking into consideration register aliases.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_PHYSREGTRACKER_H
18 #define LLVM_CODEGEN_PHYSREGTRACKER_H
19
20 #include "llvm/CodeGen/MachineFunction.h"
21
22 namespace llvm {
23
24     class PhysRegTracker {
25         const MRegisterInfo* mri_;
26         std::vector<unsigned> regUse_;
27
28     public:
29         PhysRegTracker(MachineFunction* mf)
30             : mri_(mf ? mf->getTarget().getRegisterInfo() : NULL) {
31             if (mri_) {
32                 regUse_.assign(mri_->getNumRegs(), 0);
33             }
34         }
35
36         PhysRegTracker(const PhysRegTracker& rhs)
37             : mri_(rhs.mri_),
38               regUse_(rhs.regUse_) {
39         }
40
41         const PhysRegTracker& operator=(const PhysRegTracker& rhs) {
42             mri_ = rhs.mri_;
43             regUse_ = rhs.regUse_;
44             return *this;
45         }
46
47         void addRegUse(unsigned physReg) {
48             assert(MRegisterInfo::isPhysicalRegister(physReg) &&
49                    "should be physical register!");
50             ++regUse_[physReg];
51             for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as)
52                 ++regUse_[*as];
53         }
54
55         void delRegUse(unsigned physReg) {
56             assert(MRegisterInfo::isPhysicalRegister(physReg) &&
57                    "should be physical register!");
58             assert(regUse_[physReg] != 0);
59             --regUse_[physReg];
60             for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as) {
61                 assert(regUse_[*as] != 0);
62                 --regUse_[*as];
63             }
64         }
65
66         bool isRegAvail(unsigned physReg) const {
67             assert(MRegisterInfo::isPhysicalRegister(physReg) &&
68                    "should be physical register!");
69             return regUse_[physReg] == 0;
70         }
71     };
72
73 } // End llvm namespace
74
75 #endif