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