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