Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1 | /* Title: PhyRegAlloc.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: This is the main entry point for register allocation. |
| 5 | |
| 6 | Notes: |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 7 | ===== |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 8 | |
| 9 | * RegisterClasses: Each RegClass accepts a |
| 10 | MachineRegClass which contains machine specific info about that register |
| 11 | class. The code in the RegClass is machine independent and they use |
| 12 | access functions in the MachineRegClass object passed into it to get |
| 13 | machine specific info. |
| 14 | |
| 15 | * Machine dependent work: All parts of the register coloring algorithm |
| 16 | except coloring of an individual node are machine independent. |
| 17 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 18 | Register allocation must be done as: |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 19 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 20 | MethodLiveVarInfo LVI(*MethodI ); // compute LV info |
| 21 | LVI.analyze(); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 22 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 23 | TargetMachine &target = .... |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 24 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 25 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 26 | PhyRegAlloc PRA(*MethodI, target, &LVI); // allocate regs |
| 27 | PRA.allocateRegisters(); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 30 | #ifndef PHY_REG_ALLOC_H |
| 31 | #define PHY_REG_ALLOC_H |
| 32 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/RegClass.h" |
| 34 | #include "llvm/CodeGen/LiveRangeInfo.h" |
Ruchira Sasanka | 21721b6 | 2001-10-15 16:22:44 +0000 | [diff] [blame] | 35 | #include <deque> |
Chris Lattner | 29f4c06 | 2002-02-03 07:13:04 +0000 | [diff] [blame] | 36 | class MachineCodeForMethod; |
Chris Lattner | 2182c78 | 2002-02-04 05:52:08 +0000 | [diff] [blame] | 37 | class MachineRegInfo; |
| 38 | class MethodLiveVarInfo; |
| 39 | class MachineInstr; |
Chris Lattner | 14ab1ce | 2002-02-04 17:48:00 +0000 | [diff] [blame] | 40 | namespace cfg { class LoopInfo; } |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 41 | |
| 42 | //---------------------------------------------------------------------------- |
| 43 | // Class AddedInstrns: |
| 44 | // When register allocator inserts new instructions in to the existing |
| 45 | // instruction stream, it does NOT directly modify the instruction stream. |
| 46 | // Rather, it creates an object of AddedInstrns and stick it in the |
| 47 | // AddedInstrMap for an existing instruction. This class contains two vectors |
| 48 | // to store such instructions added before and after an existing instruction. |
| 49 | //---------------------------------------------------------------------------- |
| 50 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 51 | class AddedInstrns |
| 52 | { |
| 53 | public: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 54 | std::deque<MachineInstr*> InstrnsBefore;// Added insts BEFORE an existing inst |
| 55 | std::deque<MachineInstr*> InstrnsAfter; // Added insts AFTER an existing inst |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 58 | typedef std::hash_map<const MachineInstr *, AddedInstrns *> AddedInstrMapType; |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 62 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 63 | // class PhyRegAlloc: |
| 64 | // Main class the register allocator. Call allocateRegisters() to allocate |
| 65 | // registers for a Method. |
| 66 | //---------------------------------------------------------------------------- |
| 67 | |
| 68 | |
Chris Lattner | 3e0f828 | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 69 | class PhyRegAlloc: public NonCopyable { |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 71 | std::vector<RegClass *> RegClassList; // vector of register classes |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 72 | const TargetMachine &TM; // target machine |
Vikram S. Adve | 12af164 | 2001-11-08 04:48:50 +0000 | [diff] [blame] | 73 | const Method* Meth; // name of the method we work on |
Chris Lattner | 29f4c06 | 2002-02-03 07:13:04 +0000 | [diff] [blame] | 74 | MachineCodeForMethod &mcInfo; // descriptor for method's native code |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 75 | MethodLiveVarInfo *const LVI; // LV information for this method |
| 76 | // (already computed for BBs) |
| 77 | LiveRangeInfo LRI; // LR info (will be computed) |
| 78 | const MachineRegInfo &MRI; // Machine Register information |
| 79 | const unsigned NumOfRegClasses; // recorded here for efficiency |
| 80 | |
Ruchira Sasanka | 51bc0e7 | 2001-11-03 17:14:44 +0000 | [diff] [blame] | 81 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 82 | AddedInstrMapType AddedInstrMap; // to store instrns added in this phase |
Chris Lattner | 14ab1ce | 2002-02-04 17:48:00 +0000 | [diff] [blame] | 83 | cfg::LoopInfo *LoopDepthCalc; // to calculate loop depths |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 84 | ReservedColorListType ResColList; // A set of reserved regs if desired. |
| 85 | // currently not used |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 3e0f828 | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 87 | public: |
| 88 | PhyRegAlloc(Method *M, const TargetMachine& TM, MethodLiveVarInfo *Lvi, |
Chris Lattner | 14ab1ce | 2002-02-04 17:48:00 +0000 | [diff] [blame] | 89 | cfg::LoopInfo *LoopDepthCalc); |
Chris Lattner | 3e0f828 | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 90 | ~PhyRegAlloc(); |
| 91 | |
| 92 | // main method called for allocating registers |
| 93 | // |
| 94 | void allocateRegisters(); |
Vikram S. Adve | 705f95e | 2002-03-18 03:26:48 +0000 | [diff] [blame^] | 95 | |
| 96 | |
| 97 | // access to register classes by class ID |
| 98 | // |
| 99 | const RegClass* getRegClassByID(unsigned int id) const { |
| 100 | return RegClassList[id]; |
| 101 | } |
| 102 | RegClass* getRegClassByID(unsigned int id) { |
| 103 | return RegClassList[id]; } |
| 104 | |
| 105 | |
Chris Lattner | 3e0f828 | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 106 | private: |
| 107 | |
Ruchira Sasanka | 51bc0e7 | 2001-11-03 17:14:44 +0000 | [diff] [blame] | 108 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 109 | |
| 110 | //------- ------------------ private methods--------------------------------- |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 5e5dfa3 | 2002-02-05 02:51:01 +0000 | [diff] [blame] | 112 | void addInterference(const Value *Def, const ValueSet *LVSet, |
| 113 | bool isCallInst); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 114 | |
| 115 | void addInterferencesForArgs(); |
| 116 | void createIGNodeListsAndIGs(); |
| 117 | void buildInterferenceGraphs(); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 118 | |
Ruchira Sasanka | 36f7707 | 2001-10-19 17:21:59 +0000 | [diff] [blame] | 119 | void setCallInterferences(const MachineInstr *MInst, |
Chris Lattner | 5e5dfa3 | 2002-02-05 02:51:01 +0000 | [diff] [blame] | 120 | const ValueSet *LVSetAft ); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 121 | |
Ruchira Sasanka | f7434f0 | 2001-10-23 21:38:42 +0000 | [diff] [blame] | 122 | void move2DelayedInstr(const MachineInstr *OrigMI, |
| 123 | const MachineInstr *DelayedMI ); |
| 124 | |
Ruchira Sasanka | 44d2b94 | 2001-10-19 21:42:06 +0000 | [diff] [blame] | 125 | void markUnusableSugColors(); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 126 | void allocateStackSpace4SpilledLRs(); |
| 127 | |
Chris Lattner | 00d91c6 | 2001-11-08 20:55:05 +0000 | [diff] [blame] | 128 | void insertCode4SpilledLR (const LiveRange *LR, |
| 129 | MachineInstr *MInst, |
| 130 | const BasicBlock *BB, |
| 131 | const unsigned OpNum); |
Ruchira Sasanka | 44d2b94 | 2001-10-19 21:42:06 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 133 | inline void constructLiveRanges() { LRI.constructLiveRanges(); } |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 134 | |
| 135 | void colorIncomingArgs(); |
Ruchira Sasanka | ab304c4 | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 136 | void colorCallRetArgs(); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 137 | void updateMachineCode(); |
Ruchira Sasanka | ab304c4 | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 138 | |
Ruchira Sasanka | 6053b93 | 2001-09-15 19:08:41 +0000 | [diff] [blame] | 139 | void printLabel(const Value *const Val); |
| 140 | void printMachineCode(); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 141 | |
| 142 | friend class UltraSparcRegInfo; |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 143 | |
| 144 | |
Chris Lattner | 5e5dfa3 | 2002-02-05 02:51:01 +0000 | [diff] [blame] | 145 | int getUsableUniRegAtMI(RegClass *RC, int RegType, |
Ruchira Sasanka | 825dd55 | 2001-11-15 20:22:37 +0000 | [diff] [blame] | 146 | const MachineInstr *MInst, |
Vikram S. Adve | 705f95e | 2002-03-18 03:26:48 +0000 | [diff] [blame^] | 147 | const ValueSet *LVSetBef, MachineInstr *&MIBef, |
| 148 | MachineInstr *&MIAft ); |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 149 | |
Ruchira Sasanka | 825dd55 | 2001-11-15 20:22:37 +0000 | [diff] [blame] | 150 | int getUnusedUniRegAtMI(RegClass *RC, const MachineInstr *MInst, |
Chris Lattner | 5e5dfa3 | 2002-02-05 02:51:01 +0000 | [diff] [blame] | 151 | const ValueSet *LVSetBef); |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 152 | |
Ruchira Sasanka | 825dd55 | 2001-11-15 20:22:37 +0000 | [diff] [blame] | 153 | void setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst ); |
| 154 | int getUniRegNotUsedByThisInst(RegClass *RC, const MachineInstr *MInst); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 155 | |
Ruchira Sasanka | cbddf49 | 2001-11-14 15:37:13 +0000 | [diff] [blame] | 156 | void addInterf4PseudoInstr(const MachineInstr *MInst); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 160 | #endif |
| 161 | |