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> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 16 | #include <iostream> |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 18 | typedef std::vector<unsigned int> ReservedColorListType; |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 19 | |
| 20 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 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 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 38 | class RegClass |
| 39 | { |
| 40 | |
| 41 | private: |
| 42 | const Method *const Meth; // Method we are working on |
| 43 | |
| 44 | const MachineRegClassInfo *const MRC; // corresponding MRC |
| 45 | |
| 46 | const unsigned RegClassID; // my int ID |
| 47 | |
| 48 | InterferenceGraph IG; // Interference graph - constructed by |
| 49 | // buildInterferenceGraph |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 50 | std::stack<IGNode *> IGNodeStack; // the stack used for coloring |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 51 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 52 | const ReservedColorListType *const ReservedColorList; |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 53 | // |
| 54 | // for passing registers that are pre-allocated and cannot be used by the |
| 55 | // register allocator for this method. |
| 56 | |
| 57 | bool *IsColorUsedArr; |
| 58 | // |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 59 | // An array used for coloring each node. This array must be of size |
| 60 | // MRC->getNumOfAllRegs(). Allocated once in the constructor |
| 61 | // for efficiency. |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 62 | |
| 63 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 64 | //--------------------------- private methods ------------------------------ |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 65 | |
| 66 | void pushAllIGNodes(); |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 67 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 68 | bool pushUnconstrainedIGNodes(); |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 69 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 70 | IGNode * getIGNodeWithMinSpillCost(); |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 71 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 72 | void colorIGNode(IGNode *const Node); |
| 73 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 74 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 75 | public: |
| 76 | |
| 77 | RegClass(const Method *const M, |
| 78 | const MachineRegClassInfo *const MRC, |
| 79 | const ReservedColorListType *const RCL = NULL); |
| 80 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 81 | ~RegClass() { delete[] IsColorUsedArr; }; |
| 82 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 83 | inline void createInterferenceGraph() |
| 84 | { IG.createGraph(); } |
| 85 | |
| 86 | inline InterferenceGraph &getIG() { return IG; } |
| 87 | |
| 88 | inline const unsigned getID() const { return RegClassID; } |
| 89 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 90 | // main method called for coloring regs |
| 91 | // |
| 92 | void colorAllRegs(); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 93 | |
| 94 | inline unsigned getNumOfAvailRegs() const |
| 95 | { return MRC->getNumOfAvailRegs(); } |
| 96 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 97 | |
| 98 | // --- following methods are provided to access the IG contained within this |
| 99 | // ---- RegClass easilly. |
| 100 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 101 | inline void addLRToIG(LiveRange *const LR) |
| 102 | { IG.addLRToIG(LR); } |
| 103 | |
| 104 | inline void setInterference(const LiveRange *const LR1, |
| 105 | const LiveRange *const LR2) |
| 106 | { IG.setInterference(LR1, LR2); } |
| 107 | |
| 108 | inline unsigned getInterference(const LiveRange *const LR1, |
| 109 | const LiveRange *const LR2) const |
| 110 | { return IG.getInterference(LR1, LR2); } |
| 111 | |
| 112 | inline void mergeIGNodesOfLRs(const LiveRange *const LR1, |
| 113 | LiveRange *const LR2) |
| 114 | { IG.mergeIGNodesOfLRs(LR1, LR2); } |
| 115 | |
| 116 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 117 | inline bool * getIsColorUsedArr() { return IsColorUsedArr; } |
| 118 | |
| 119 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 120 | inline void printIGNodeList() const { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 121 | std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n"; |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 122 | IG.printIGNodeList(); |
| 123 | } |
| 124 | |
| 125 | inline void printIG() { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 126 | std::cerr << "IG for Register Class " << RegClassID << ":" << "\n"; |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 127 | IG.printIG(); |
| 128 | } |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 131 | #endif |