36128c65a8806256f8fa96454721ab3bda52fe7a
[oota-llvm.git] / lib / CodeGen / PhysRegTracker.h
1 //===-- llvm/CodeGen/LiveInterval.h - Live Interval Analysis ----*- 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 addPhysRegUse and
12 // delPhysRegUse functions while abstracting away register aliases.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_PHYSREGTRACKER_H
17 #define LLVM_CODEGEN_PHYSREGTRACKER_H
18
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include <vector>
21
22 namespace llvm {
23
24     class PhysRegTracker {
25     private:
26         const MRegisterInfo* mri_;
27         std::vector<unsigned> regUse_;
28
29     public:
30         PhysRegTracker(MachineFunction* mf)
31             : mri_(mf ? mf->getTarget().getRegisterInfo() : NULL) {
32             if (mri_) {
33                 regUse_.assign(mri_->getNumRegs(), 0);
34             }
35         }
36
37         PhysRegTracker(const PhysRegTracker& rhs)
38             : mri_(rhs.mri_),
39               regUse_(rhs.regUse_) {
40         }
41
42         const PhysRegTracker& operator=(const PhysRegTracker& rhs) {
43             mri_ = rhs.mri_;
44             regUse_ = rhs.regUse_;
45             return *this;
46         }
47
48         void addRegUse(unsigned physReg) {
49             assert(MRegisterInfo::isPhysicalRegister(physReg) &&
50                    "should be physical register!");
51             ++regUse_[physReg];
52             for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as) {
53                 physReg = *as;
54                 ++regUse_[physReg];
55             }
56         }
57
58         void delRegUse(unsigned physReg) {
59             assert(MRegisterInfo::isPhysicalRegister(physReg) &&
60                    "should be physical register!");
61             assert(regUse_[physReg] != 0);
62             --regUse_[physReg];
63             for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as) {
64                 physReg = *as;
65                 assert(regUse_[physReg] != 0);
66                 --regUse_[physReg];
67             }
68         }
69
70         bool isPhysRegAvail(unsigned physReg) const {
71             assert(MRegisterInfo::isPhysicalRegister(physReg) &&
72                    "should be physical register!");
73             return regUse_[physReg] == 0;
74         }
75     };
76
77 } // End llvm namespace
78
79 #endif