4a82fc5ef63325bc80852e75909d104bb5690553
[oota-llvm.git] / include / llvm / CodeGen / RegisterScavenging.h
1 //===-- RegisterScavenging.h - Machine register scavenging ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the Evan Cheng and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the machine register scavenger class. It can provide
11 // information such as unused register at any point in a machine basic block.
12 // It also provides a mechanism to make registers availbale by evicting them
13 // to spill slots.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_REGISTER_SCAVENGING_H
18 #define LLVM_CODEGEN_REGISTER_SCAVENGING_H
19
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/ADT/BitVector.h"
22
23 namespace llvm {
24
25 class TargetRegisterClass;
26
27 class RegScavenger {
28   MachineBasicBlock *MBB;
29   MachineBasicBlock::iterator MBBI;
30   unsigned NumPhysRegs;
31
32   /// Initialized - All states are initialized and ready to go!
33   bool Initialized;
34
35   /// RegStates - The current state of all the physical registers immediately
36   /// before MBBI. One bit per physical register. If bit is set that means it's
37   /// available, unset means the register is currently being used.
38   BitVector RegStates;
39
40 public:
41   RegScavenger()
42     : MBB(NULL), Initialized(false) {};
43
44   RegScavenger(MachineBasicBlock *mbb)
45     : MBB(mbb), Initialized(false) {};
46
47   /// Init - Initialize the states.
48   ///
49   void init(MachineBasicBlock *mbb = NULL);
50
51   /// Reset - Discard previous states and re-initialize the states given for
52   /// the specific basic block.
53   void reset(MachineBasicBlock *mbb) {
54     clear();
55     init(mbb);
56   }
57
58   /// forward / backward - Move the internal MBB iterator and update register
59   /// states.
60   void forward();
61   void backward();
62
63   /// forward / backward - Move the internal MBB iterator and update register
64   /// states until it has reached but not processed the specific iterator.
65   void forward(MachineBasicBlock::iterator I) {
66     while (MBBI != I) forward();
67   }
68   void backward(MachineBasicBlock::iterator I) {
69     while (MBBI != I) backward();
70   }
71
72   /// isReserved - Returns true if a register is reserved. It is never "unused".
73   bool isReserved(unsigned Reg) const { return ReservedRegs[Reg]; }
74
75   /// isUsed / isUsed - Test if a register is currently being used.
76   ///
77   bool isUsed(unsigned Reg) const   { return !RegStates[Reg]; }
78   bool isUnused(unsigned Reg) const { return RegStates[Reg]; }
79
80   /// setUsed / setUnused - Mark the state of one or a number of registers.
81   ///
82   void setUsed(unsigned Reg)     { RegStates.reset(Reg); }
83   void setUsed(BitVector Regs)   { RegStates &= ~Regs; }
84   void setUnused(unsigned Reg)   { RegStates.set(Reg); }
85   void setUnused(BitVector Regs) { RegStates |= Regs; }
86
87   /// FindUnusedReg - Find a unused register of the specified register class.
88   /// Exclude callee saved registers if directed. It return 0 is none is found.
89   unsigned FindUnusedReg(const TargetRegisterClass *RegClass,
90                          bool ExCalleeSaved = false) const;
91
92 private:
93   /// clear - Clear states.
94   ///
95   void clear();
96
97   /// CalleeSavedrRegs - A bitvector of callee saved registers for the target.
98   ///
99   BitVector CalleeSavedRegs;
100
101   /// ReservedRegs - A bitvector of reserved registers.
102   ///
103   BitVector ReservedRegs;
104 };
105  
106 } // End llvm namespace
107
108 #endif