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 | |
Chris Lattner | 483e14e | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 20 | FunctionLiveVarInfo LVI(*FunctionI ); // compute LV info |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 483e14e | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 26 | PhyRegAlloc PRA(*FunctionI, target, &LVI); // allocate regs |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 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" |
Vikram S. Adve | 0243ff9 | 2002-05-19 15:41:33 +0000 | [diff] [blame] | 35 | #include <vector> |
Chris Lattner | 97453d6 | 2002-04-28 20:40:16 +0000 | [diff] [blame] | 36 | #include <map> |
| 37 | |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 38 | class MachineFunction; |
Chris Lattner | 2182c78 | 2002-02-04 05:52:08 +0000 | [diff] [blame] | 39 | class MachineRegInfo; |
Chris Lattner | 483e14e | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 40 | class FunctionLiveVarInfo; |
Chris Lattner | 2182c78 | 2002-02-04 05:52:08 +0000 | [diff] [blame] | 41 | class MachineInstr; |
Chris Lattner | 8fc2f20 | 2002-04-28 16:19:42 +0000 | [diff] [blame] | 42 | class LoopInfo; |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 43 | |
| 44 | //---------------------------------------------------------------------------- |
| 45 | // Class AddedInstrns: |
| 46 | // When register allocator inserts new instructions in to the existing |
| 47 | // instruction stream, it does NOT directly modify the instruction stream. |
| 48 | // Rather, it creates an object of AddedInstrns and stick it in the |
| 49 | // AddedInstrMap for an existing instruction. This class contains two vectors |
| 50 | // to store such instructions added before and after an existing instruction. |
| 51 | //---------------------------------------------------------------------------- |
| 52 | |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 53 | struct AddedInstrns { |
Chris Lattner | ccdf23e | 2002-10-28 19:43:23 +0000 | [diff] [blame^] | 54 | std::vector<MachineInstr*> InstrnsBefore;//Insts added BEFORE an existing inst |
| 55 | std::vector<MachineInstr*> InstrnsAfter; //Insts added AFTER an existing inst |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Chris Lattner | 0b0ffa0 | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 58 | typedef std::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 |
Chris Lattner | b7653df | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 65 | // registers for a Function. |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 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 |
Chris Lattner | ccdf23e | 2002-10-28 19:43:23 +0000 | [diff] [blame^] | 73 | const Function *Fn; // name of the function we work on |
| 74 | MachineFunction &MF; // descriptor for method's native code |
Chris Lattner | 8fc2f20 | 2002-04-28 16:19:42 +0000 | [diff] [blame] | 75 | FunctionLiveVarInfo *const LVI; // LV information for this method |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 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 |
Vikram S. Adve | d23a229 | 2002-04-25 04:46:28 +0000 | [diff] [blame] | 83 | AddedInstrns AddedInstrAtEntry; // to store instrns added at entry |
Chris Lattner | 8fc2f20 | 2002-04-28 16:19:42 +0000 | [diff] [blame] | 84 | LoopInfo *LoopDepthCalc; // to calculate loop depths |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 85 | ReservedColorListType ResColList; // A set of reserved regs if desired. |
| 86 | // currently not used |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 3e0f828 | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 88 | public: |
Chris Lattner | 483e14e | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 89 | PhyRegAlloc(Function *F, const TargetMachine& TM, FunctionLiveVarInfo *Lvi, |
Chris Lattner | 8fc2f20 | 2002-04-28 16:19:42 +0000 | [diff] [blame] | 90 | LoopInfo *LoopDepthCalc); |
Chris Lattner | 3e0f828 | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 91 | ~PhyRegAlloc(); |
| 92 | |
| 93 | // main method called for allocating registers |
| 94 | // |
| 95 | void allocateRegisters(); |
Vikram S. Adve | 705f95e | 2002-03-18 03:26:48 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | // access to register classes by class ID |
| 99 | // |
| 100 | const RegClass* getRegClassByID(unsigned int id) const { |
| 101 | return RegClassList[id]; |
| 102 | } |
| 103 | RegClass* getRegClassByID(unsigned int id) { |
| 104 | return RegClassList[id]; } |
| 105 | |
| 106 | |
Chris Lattner | 3e0f828 | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 107 | private: |
| 108 | |
Ruchira Sasanka | 51bc0e7 | 2001-11-03 17:14:44 +0000 | [diff] [blame] | 109 | |
Ruchira Sasanka | 42bd177 | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 110 | |
| 111 | //------- ------------------ private methods--------------------------------- |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 5e5dfa3 | 2002-02-05 02:51:01 +0000 | [diff] [blame] | 113 | void addInterference(const Value *Def, const ValueSet *LVSet, |
| 114 | bool isCallInst); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 115 | |
| 116 | void addInterferencesForArgs(); |
| 117 | void createIGNodeListsAndIGs(); |
| 118 | void buildInterferenceGraphs(); |
Ruchira Sasanka | c4d4b76 | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 119 | |
Ruchira Sasanka | 36f7707 | 2001-10-19 17:21:59 +0000 | [diff] [blame] | 120 | void setCallInterferences(const MachineInstr *MInst, |
Chris Lattner | 5e5dfa3 | 2002-02-05 02:51:01 +0000 | [diff] [blame] | 121 | const ValueSet *LVSetAft ); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 122 | |
Ruchira Sasanka | f7434f0 | 2001-10-23 21:38:42 +0000 | [diff] [blame] | 123 | void move2DelayedInstr(const MachineInstr *OrigMI, |
| 124 | const MachineInstr *DelayedMI ); |
| 125 | |
Ruchira Sasanka | 44d2b94 | 2001-10-19 21:42:06 +0000 | [diff] [blame] | 126 | void markUnusableSugColors(); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 127 | void allocateStackSpace4SpilledLRs(); |
| 128 | |
Chris Lattner | 00d91c6 | 2001-11-08 20:55:05 +0000 | [diff] [blame] | 129 | void insertCode4SpilledLR (const LiveRange *LR, |
| 130 | MachineInstr *MInst, |
| 131 | const BasicBlock *BB, |
| 132 | const unsigned OpNum); |
Ruchira Sasanka | 44d2b94 | 2001-10-19 21:42:06 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 134 | inline void constructLiveRanges() { LRI.constructLiveRanges(); } |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 135 | |
| 136 | void colorIncomingArgs(); |
Ruchira Sasanka | ab304c4 | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 137 | void colorCallRetArgs(); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 138 | void updateMachineCode(); |
Ruchira Sasanka | ab304c4 | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 139 | |
Ruchira Sasanka | 6053b93 | 2001-09-15 19:08:41 +0000 | [diff] [blame] | 140 | void printLabel(const Value *const Val); |
| 141 | void printMachineCode(); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 142 | |
| 143 | friend class UltraSparcRegInfo; |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 144 | |
| 145 | |
Vikram S. Adve | c2580dd | 2002-07-08 22:39:36 +0000 | [diff] [blame] | 146 | int getUsableUniRegAtMI(int RegType, |
| 147 | const ValueSet *LVSetBef, |
| 148 | MachineInstr *MInst, |
| 149 | std::vector<MachineInstr*>& MIBef, |
| 150 | std::vector<MachineInstr*>& MIAft); |
| 151 | |
Ruchira Sasanka | 825dd55 | 2001-11-15 20:22:37 +0000 | [diff] [blame] | 152 | int getUnusedUniRegAtMI(RegClass *RC, const MachineInstr *MInst, |
Vikram S. Adve | c2580dd | 2002-07-08 22:39:36 +0000 | [diff] [blame] | 153 | const ValueSet *LVSetBef); |
Ruchira Sasanka | 80b1a1a | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 154 | |
Ruchira Sasanka | 825dd55 | 2001-11-15 20:22:37 +0000 | [diff] [blame] | 155 | void setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst ); |
| 156 | int getUniRegNotUsedByThisInst(RegClass *RC, const MachineInstr *MInst); |
Ruchira Sasanka | 20c82b1 | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 157 | |
Ruchira Sasanka | cbddf49 | 2001-11-14 15:37:13 +0000 | [diff] [blame] | 158 | void addInterf4PseudoInstr(const MachineInstr *MInst); |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | |
Ruchira Sasanka | 7cd2ca1 | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 162 | #endif |
| 163 | |