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