Finegrainify namespacification
[oota-llvm.git] / lib / CodeGen / RegAlloc / RegClass.h
1 //===-- RegClass.h - Machine Independent register coloring ------*- 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 /* Title:   RegClass.h   -*- C++ -*-
11    Author:  Ruchira Sasanka
12    Date:    Aug 20, 01
13    Purpose: Contains machine independent methods for register coloring.
14
15 */
16
17 #ifndef REGCLASS_H
18 #define REGCLASS_H
19
20 #include "llvm/Target/TargetRegInfo.h"
21 #include "InterferenceGraph.h"
22 #include <stack>
23
24 namespace llvm {
25
26 class TargetRegClassInfo;
27
28
29 //-----------------------------------------------------------------------------
30 // Class RegClass
31 //
32 //   Implements a machine independent register class. 
33 //
34 //   This is the class that contains all data structures and common algos
35 //   for coloring a particular register class (e.g., int class, fp class).  
36 //   This class is hardware independent. This class accepts a hardware 
37 //   dependent description of machine registers (TargetRegInfo class) to 
38 //   get hardware specific info and to color an individual IG node.
39 //
40 //   This class contains the InterferenceGraph (IG).
41 //   Also it contains an IGNode stack that can be used for coloring. 
42 //   The class provides some easy access methods to the IG methods, since these
43 //   methods are called thru a register class.
44 //
45 //-----------------------------------------------------------------------------
46 class RegClass {
47   const Function *const Meth;           // Function we are working on
48   const TargetRegInfo *MRI;             // Machine register information 
49   const TargetRegClassInfo *const MRC;  // Machine reg. class for this RegClass
50   const unsigned RegClassID;            // my int ID
51
52   InterferenceGraph IG;                 // Interference graph - constructed by
53                                         // buildInterferenceGraph
54   std::stack<IGNode *> IGNodeStack;     // the stack used for coloring
55
56   // IsColorUsedArr - An array used for coloring each node. This array must be
57   // of size MRC->getNumOfAllRegs(). Allocated once in the constructor for
58   // efficiency.
59   //
60   std::vector<bool> IsColorUsedArr;
61
62
63
64   //--------------------------- private methods ------------------------------
65
66   void pushAllIGNodes();
67
68   bool  pushUnconstrainedIGNodes();
69
70   IGNode * getIGNodeWithMinSpillCost();
71
72   void colorIGNode(IGNode *const Node);
73
74   // This directly marks the colors used by a particular register number
75   // within the register class.  External users should use the public
76   // versions of this function below.
77   inline void markColorUsed(unsigned classRegNum) {
78     assert(classRegNum < IsColorUsedArr.size() && "Invalid register used?");
79     IsColorUsedArr[classRegNum] = true;
80   }
81
82   inline bool isColorUsed(unsigned regNum) const {
83     assert(regNum < IsColorUsedArr.size() && "Invalid register used?");
84     return IsColorUsedArr[regNum];
85   }
86
87  public:
88
89   RegClass(const Function *M,
90            const TargetRegInfo *_MRI_,
91            const TargetRegClassInfo *_MRC_);
92
93   inline void createInterferenceGraph() { IG.createGraph(); }
94
95   inline InterferenceGraph &getIG() { return IG; }
96
97   inline const unsigned getID() const { return RegClassID; }
98
99   inline const TargetRegClassInfo* getTargetRegClass() const { return MRC; }
100
101   // main method called for coloring regs
102   //
103   void colorAllRegs();                 
104
105   inline unsigned getNumOfAvailRegs() const 
106     { return MRC->getNumOfAvailRegs(); }
107
108
109   // --- following methods are provided to access the IG contained within this
110   // ---- RegClass easilly.
111
112   inline void addLRToIG(LiveRange *const LR) 
113     { IG.addLRToIG(LR); }
114
115   inline void setInterference(const LiveRange *const LR1,
116                               const LiveRange *const LR2)  
117     { IG.setInterference(LR1, LR2); }
118
119   inline unsigned getInterference(const LiveRange *const LR1,
120                               const LiveRange *const LR2) const 
121     { return IG.getInterference(LR1, LR2); }
122
123   inline void mergeIGNodesOfLRs(const LiveRange *const LR1,
124                                 LiveRange *const LR2) 
125     { IG.mergeIGNodesOfLRs(LR1, LR2); }
126
127
128   inline void clearColorsUsed() {
129     IsColorUsedArr.clear();
130     IsColorUsedArr.resize(MRC->getNumOfAllRegs());
131   }
132   inline void markColorsUsed(unsigned ClassRegNum,
133                              int UserRegType,
134                              int RegTypeWanted) {
135     MRC->markColorsUsed(ClassRegNum, UserRegType, RegTypeWanted,IsColorUsedArr);
136   }
137   inline int getUnusedColor(int machineRegType) const {
138     return MRC->findUnusedColor(machineRegType, IsColorUsedArr);
139   }
140
141   void printIGNodeList() const;
142   void printIG();
143 };
144
145 } // End llvm namespace
146
147 #endif