Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1 | /* Title: PhyRegAlloc.h -*- C++ -*- |
Ruchira Sasanka | f5788aa | 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 | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 7 | ===== |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 8 | |
| 9 | * RegisterClasses: Each RegClass accepts a |
Chris Lattner | f9781b5 | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 10 | TargetRegClass which contains machine specific info about that register |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 11 | class. The code in the RegClass is machine independent and they use |
Chris Lattner | f9781b5 | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 12 | access functions in the TargetRegClass object passed into it to get |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 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. |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 19 | #ifndef PHY_REG_ALLOC_H |
| 20 | #define PHY_REG_ALLOC_H |
| 21 | |
Chris Lattner | e80612a | 2003-09-01 20:12:17 +0000 | [diff] [blame^] | 22 | #include "LiveRangeInfo.h" |
Vikram S. Adve | 91e75d8 | 2003-07-29 19:37:41 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chris Lattner | 50cf8f1 | 2002-04-28 20:40:16 +0000 | [diff] [blame] | 24 | #include <map> |
| 25 | |
Misha Brukman | 7ae7f84 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 26 | class MachineFunction; |
Chris Lattner | f9781b5 | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 27 | class TargetRegInfo; |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 28 | class FunctionLiveVarInfo; |
Chris Lattner | b0da8b2 | 2002-02-04 05:52:08 +0000 | [diff] [blame] | 29 | class MachineInstr; |
Chris Lattner | 002958c | 2002-04-28 16:19:42 +0000 | [diff] [blame] | 30 | class LoopInfo; |
Chris Lattner | 19a7cb2 | 2003-01-15 19:56:21 +0000 | [diff] [blame] | 31 | class RegClass; |
Ruchira Sasanka | 9c38dbc | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 32 | |
| 33 | //---------------------------------------------------------------------------- |
| 34 | // Class AddedInstrns: |
| 35 | // When register allocator inserts new instructions in to the existing |
| 36 | // instruction stream, it does NOT directly modify the instruction stream. |
| 37 | // Rather, it creates an object of AddedInstrns and stick it in the |
| 38 | // AddedInstrMap for an existing instruction. This class contains two vectors |
| 39 | // to store such instructions added before and after an existing instruction. |
| 40 | //---------------------------------------------------------------------------- |
| 41 | |
Chris Lattner | 30e23da | 2002-04-09 05:13:04 +0000 | [diff] [blame] | 42 | struct AddedInstrns { |
Chris Lattner | b1e39b5 | 2002-10-28 19:43:23 +0000 | [diff] [blame] | 43 | std::vector<MachineInstr*> InstrnsBefore;//Insts added BEFORE an existing inst |
| 44 | std::vector<MachineInstr*> InstrnsAfter; //Insts added AFTER an existing inst |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Ruchira Sasanka | 9c38dbc | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 47 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 9c38dbc | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 48 | // class PhyRegAlloc: |
| 49 | // Main class the register allocator. Call allocateRegisters() to allocate |
Chris Lattner | f739fa8 | 2002-04-08 22:03:57 +0000 | [diff] [blame] | 50 | // registers for a Function. |
Ruchira Sasanka | 9c38dbc | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 51 | //---------------------------------------------------------------------------- |
| 52 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 53 | class PhyRegAlloc { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 54 | std::vector<RegClass *> RegClassList; // vector of register classes |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 55 | const TargetMachine &TM; // target machine |
Chris Lattner | b1e39b5 | 2002-10-28 19:43:23 +0000 | [diff] [blame] | 56 | const Function *Fn; // name of the function we work on |
| 57 | MachineFunction &MF; // descriptor for method's native code |
Chris Lattner | 002958c | 2002-04-28 16:19:42 +0000 | [diff] [blame] | 58 | FunctionLiveVarInfo *const LVI; // LV information for this method |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 59 | // (already computed for BBs) |
| 60 | LiveRangeInfo LRI; // LR info (will be computed) |
Chris Lattner | f9781b5 | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 61 | const TargetRegInfo &MRI; // Machine Register information |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 62 | const unsigned NumOfRegClasses; // recorded here for efficiency |
| 63 | |
Vikram S. Adve | 24ce4d8 | 2003-05-31 07:41:54 +0000 | [diff] [blame] | 64 | // Map to indicate whether operands of each MachineInstr have been updated |
| 65 | // according to their assigned colors. This is primarily for debugging and |
| 66 | // could be removed in the long run. |
| 67 | std::map<const MachineInstr *, bool> OperandsColoredMap; |
Ruchira Sasanka | ca632ed | 2001-11-03 17:14:44 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 76014b9 | 2002-10-29 17:08:05 +0000 | [diff] [blame] | 69 | // AddedInstrMap - Used to store instrns added in this phase |
| 70 | std::map<const MachineInstr *, AddedInstrns> AddedInstrMap; |
| 71 | |
Chris Lattner | 92f5fb5 | 2003-08-05 22:09:31 +0000 | [diff] [blame] | 72 | // ScratchRegsUsed - Contains scratch register uses for a particular MI. |
| 73 | typedef std::multimap<const MachineInstr*, int> ScratchRegsUsedTy; |
| 74 | ScratchRegsUsedTy ScratchRegsUsed; |
| 75 | |
Vikram S. Adve | 40221aa | 2002-04-25 04:46:28 +0000 | [diff] [blame] | 76 | AddedInstrns AddedInstrAtEntry; // to store instrns added at entry |
Chris Lattner | 002958c | 2002-04-28 16:19:42 +0000 | [diff] [blame] | 77 | LoopInfo *LoopDepthCalc; // to calculate loop depths |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 78 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 79 | PhyRegAlloc(const PhyRegAlloc&); // DO NOT IMPLEMENT |
| 80 | void operator=(const PhyRegAlloc&); // DO NOT IMPLEMENT |
Chris Lattner | 669a74c | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 81 | public: |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 82 | PhyRegAlloc(Function *F, const TargetMachine& TM, FunctionLiveVarInfo *Lvi, |
Chris Lattner | 002958c | 2002-04-28 16:19:42 +0000 | [diff] [blame] | 83 | LoopInfo *LoopDepthCalc); |
Chris Lattner | 669a74c | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 84 | ~PhyRegAlloc(); |
| 85 | |
| 86 | // main method called for allocating registers |
| 87 | // |
| 88 | void allocateRegisters(); |
Vikram S. Adve | cecde71 | 2002-03-18 03:26:48 +0000 | [diff] [blame] | 89 | |
Vikram S. Adve | cecde71 | 2002-03-18 03:26:48 +0000 | [diff] [blame] | 90 | // access to register classes by class ID |
| 91 | // |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 92 | const RegClass* getRegClassByID(unsigned id) const { |
Vikram S. Adve | abcd8d7 | 2003-07-25 21:00:13 +0000 | [diff] [blame] | 93 | return RegClassList[id]; |
Vikram S. Adve | cecde71 | 2002-03-18 03:26:48 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 95 | RegClass* getRegClassByID(unsigned id) { |
Vikram S. Adve | abcd8d7 | 2003-07-25 21:00:13 +0000 | [diff] [blame] | 96 | return RegClassList[id]; |
| 97 | } |
Vikram S. Adve | 91e75d8 | 2003-07-29 19:37:41 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 669a74c | 2002-02-04 17:38:48 +0000 | [diff] [blame] | 99 | private: |
Chris Lattner | b1def73 | 2002-02-05 02:51:01 +0000 | [diff] [blame] | 100 | void addInterference(const Value *Def, const ValueSet *LVSet, |
| 101 | bool isCallInst); |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 102 | |
| 103 | void addInterferencesForArgs(); |
| 104 | void createIGNodeListsAndIGs(); |
| 105 | void buildInterferenceGraphs(); |
Ruchira Sasanka | 5b8971f | 2001-10-16 01:23:19 +0000 | [diff] [blame] | 106 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 107 | void setCallInterferences(const MachineInstr *MI, |
| 108 | const ValueSet *LVSetAft); |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 109 | |
Ruchira Sasanka | 33b0d85 | 2001-10-23 21:38:42 +0000 | [diff] [blame] | 110 | void move2DelayedInstr(const MachineInstr *OrigMI, |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 111 | const MachineInstr *DelayedMI); |
Ruchira Sasanka | 33b0d85 | 2001-10-23 21:38:42 +0000 | [diff] [blame] | 112 | |
Ruchira Sasanka | 53516cd | 2001-10-19 21:42:06 +0000 | [diff] [blame] | 113 | void markUnusableSugColors(); |
Ruchira Sasanka | 9c38dbc | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 114 | void allocateStackSpace4SpilledLRs(); |
| 115 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 116 | void insertCode4SpilledLR(const LiveRange *LR, |
| 117 | MachineBasicBlock::iterator& MII, |
| 118 | MachineBasicBlock &MBB, unsigned OpNum); |
Ruchira Sasanka | 53516cd | 2001-10-19 21:42:06 +0000 | [diff] [blame] | 119 | |
Vikram S. Adve | 91e75d8 | 2003-07-29 19:37:41 +0000 | [diff] [blame] | 120 | // Method for inserting caller saving code. The caller must save all the |
| 121 | // volatile registers live across a call. |
| 122 | void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore, |
| 123 | std::vector<MachineInstr*>& instrnsAfter, |
| 124 | MachineInstr *CallMI, |
| 125 | const BasicBlock *BB); |
| 126 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 127 | inline void constructLiveRanges() { LRI.constructLiveRanges(); } |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 128 | |
| 129 | void colorIncomingArgs(); |
Ruchira Sasanka | 560b0ad | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 130 | void colorCallRetArgs(); |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 131 | void updateMachineCode(); |
Vikram S. Adve | 91e75d8 | 2003-07-29 19:37:41 +0000 | [diff] [blame] | 132 | void updateInstruction(MachineBasicBlock::iterator& MII, |
| 133 | MachineBasicBlock &MBB); |
Ruchira Sasanka | 560b0ad | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 134 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 135 | void printLabel(const Value *Val); |
Ruchira Sasanka | 86b2ad4 | 2001-09-15 19:08:41 +0000 | [diff] [blame] | 136 | void printMachineCode(); |
Ruchira Sasanka | 9c38dbc | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 137 | |
Ruchira Sasanka | 51fc1c2 | 2001-11-03 20:41:22 +0000 | [diff] [blame] | 138 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 139 | int getUsableUniRegAtMI(int RegType, const ValueSet *LVSetBef, |
| 140 | MachineInstr *MI, |
Vikram S. Adve | beb3640 | 2002-07-08 22:39:36 +0000 | [diff] [blame] | 141 | std::vector<MachineInstr*>& MIBef, |
| 142 | std::vector<MachineInstr*>& MIAft); |
| 143 | |
Vikram S. Adve | 91e75d8 | 2003-07-29 19:37:41 +0000 | [diff] [blame] | 144 | // Callback method used to find unused registers. |
| 145 | // LVSetBef is the live variable set to search for an unused register. |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 146 | // If it is not specified, the LV set before the current MI is used. |
Vikram S. Adve | 91e75d8 | 2003-07-29 19:37:41 +0000 | [diff] [blame] | 147 | // This is sufficient as long as no new copy instructions are generated |
| 148 | // to copy the free register to memory. |
| 149 | // |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 150 | int getUnusedUniRegAtMI(RegClass *RC, int RegType, |
| 151 | const MachineInstr *MI, |
Vikram S. Adve | 91e75d8 | 2003-07-29 19:37:41 +0000 | [diff] [blame] | 152 | const ValueSet *LVSetBef = 0); |
| 153 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 154 | void setRelRegsUsedByThisInst(RegClass *RC, int RegType, |
| 155 | const MachineInstr *MI); |
Vikram S. Adve | abcd8d7 | 2003-07-25 21:00:13 +0000 | [diff] [blame] | 156 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 157 | int getUniRegNotUsedByThisInst(RegClass *RC, int RegType, |
| 158 | const MachineInstr *MI); |
Ruchira Sasanka | 9c38dbc | 2001-10-28 18:15:12 +0000 | [diff] [blame] | 159 | |
Chris Lattner | e62a2a7 | 2003-08-05 22:03:27 +0000 | [diff] [blame] | 160 | void addInterf4PseudoInstr(const MachineInstr *MI); |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | |
Ruchira Sasanka | f5788aa | 2001-09-08 14:22:50 +0000 | [diff] [blame] | 164 | #endif |
| 165 | |