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