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