s/Method/Function
[oota-llvm.git] / lib / CodeGen / RegAlloc / RegClass.h
1 /* Title:   RegClass.h   -*- C++ -*-
2    Author:  Ruchira Sasanka
3    Date:    Aug 20, 01
4    Purpose: Contains machine independent methods for register coloring.
5
6 */
7
8 #ifndef REG_CLASS_H
9 #define REG_CLASS_H
10
11 #include "llvm/CodeGen/IGNode.h"
12 #include "llvm/CodeGen/InterferenceGraph.h"
13 #include "llvm/Target/MachineRegInfo.h"
14 #include <stack>
15 #include <iostream>
16 class MachineRegClassInfo;
17
18 typedef std::vector<unsigned> ReservedColorListType;
19
20
21 //-----------------------------------------------------------------------------
22 // Class RegClass
23 //
24 //   Implements a machine independant register class. 
25 //
26 //   This is the class that contains all data structures and common algos
27 //   for coloring a particular register class (e.g., int class, fp class).  
28 //   This class is hardware independent. This class accepts a hardware 
29 //   dependent description of machine registers (MachineRegInfo class) to 
30 //   get hardware specific info and to color an individual IG node.
31 //
32 //   This class contains the InterferenceGraph (IG).
33 //   Also it contains an IGNode stack that can be used for coloring. 
34 //   The class provides some easy access methods to the IG methods, since these
35 //   methods are called thru a register class.
36 //
37 //-----------------------------------------------------------------------------
38 class RegClass {
39   const Function *const Meth;           // Function we are working on
40   const MachineRegClassInfo *const MRC; // corresponding MRC
41   const unsigned RegClassID;            // my int ID
42
43   InterferenceGraph IG;                 // Interference graph - constructed by
44                                         // buildInterferenceGraph
45   std::stack<IGNode *> IGNodeStack;     // the stack used for coloring
46
47   const ReservedColorListType *const ReservedColorList;
48   //
49   // for passing registers that are pre-allocated and cannot be used by the
50   // register allocator for this function.
51   
52   bool *IsColorUsedArr;
53   //
54   // An array used for coloring each node. This array must be of size 
55   // MRC->getNumOfAllRegs(). Allocated once in the constructor
56   // for efficiency.
57
58
59   //--------------------------- private methods ------------------------------
60
61   void pushAllIGNodes();
62
63   bool  pushUnconstrainedIGNodes();
64
65   IGNode * getIGNodeWithMinSpillCost();
66
67   void colorIGNode(IGNode *const Node);
68
69
70  public:
71
72   RegClass(const Function *M,
73            const MachineRegClassInfo *MRC,
74            const ReservedColorListType *RCL = 0);
75
76   ~RegClass() { delete[] IsColorUsedArr; }
77
78   inline void createInterferenceGraph() { IG.createGraph(); }
79
80   inline InterferenceGraph &getIG() { return IG; }
81
82   inline const unsigned getID() const { return RegClassID; }
83
84   // main method called for coloring regs
85   //
86   void colorAllRegs();                 
87
88   inline unsigned getNumOfAvailRegs() const 
89     { return MRC->getNumOfAvailRegs(); }
90
91
92   // --- following methods are provided to access the IG contained within this
93   // ---- RegClass easilly.
94
95   inline void addLRToIG(LiveRange *const LR) 
96     { IG.addLRToIG(LR); }
97
98   inline void setInterference(const LiveRange *const LR1,
99                               const LiveRange *const LR2)  
100     { IG.setInterference(LR1, LR2); }
101
102   inline unsigned getInterference(const LiveRange *const LR1,
103                               const LiveRange *const LR2) const 
104     { return IG.getInterference(LR1, LR2); }
105
106   inline void mergeIGNodesOfLRs(const LiveRange *const LR1,
107                                 LiveRange *const LR2) 
108     { IG.mergeIGNodesOfLRs(LR1, LR2); }
109
110
111   inline bool * getIsColorUsedArr() { return IsColorUsedArr; }
112
113
114   inline void printIGNodeList() const {
115     std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n";
116     IG.printIGNodeList(); 
117   }
118
119   inline void printIG() {  
120     std::cerr << "IG for Register Class " << RegClassID << ":" << "\n";
121     IG.printIG(); 
122   }
123 };
124
125 #endif