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