Changed `MachineCodeForMethod' to `MachineFunction'.
[oota-llvm.git] / lib / CodeGen / RegAlloc / PhyRegAlloc.h
1 /* Title:   PhyRegAlloc.h   -*- C++ -*-
2    Author:  Ruchira Sasanka
3    Date:    Aug 20, 01
4    Purpose: This is the main entry point for register allocation.
5
6    Notes:
7    =====
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
18    Register allocation must be done  as:        
19
20       FunctionLiveVarInfo LVI(*FunctionI );           // compute LV info
21       LVI.analyze();
22
23       TargetMachine &target = ....                              
24
25
26       PhyRegAlloc PRA(*FunctionI, target, &LVI);     // allocate regs
27       PRA.allocateRegisters();
28 */ 
29
30 #ifndef PHY_REG_ALLOC_H
31 #define PHY_REG_ALLOC_H
32
33 #include "llvm/CodeGen/RegClass.h"
34 #include "llvm/CodeGen/LiveRangeInfo.h"
35 #include <vector>
36 #include <map>
37
38 class MachineFunction;
39 class MachineRegInfo;
40 class FunctionLiveVarInfo;
41 class MachineInstr;
42 class LoopInfo;
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
53 struct AddedInstrns {
54   std::vector<MachineInstr*> InstrnsBefore;// Added insts BEFORE an existing inst
55   std::vector<MachineInstr*> InstrnsAfter; // Added insts AFTER an existing inst
56 };
57
58 typedef std::map<const MachineInstr *, AddedInstrns> AddedInstrMapType;
59
60
61
62 //----------------------------------------------------------------------------
63 // class PhyRegAlloc:
64 // Main class the register allocator. Call allocateRegisters() to allocate
65 // registers for a Function.
66 //----------------------------------------------------------------------------
67
68
69 class PhyRegAlloc: public NonCopyable {
70
71   std::vector<RegClass *> RegClassList; // vector of register classes
72   const TargetMachine &TM;              // target machine
73   const Function *Meth;                 // name of the function we work on
74   MachineFunction &mcInfo;              // descriptor for method's native code
75   FunctionLiveVarInfo *const LVI;       // LV information for this method 
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
81   
82   AddedInstrMapType AddedInstrMap;      // to store instrns added in this phase
83   AddedInstrns AddedInstrAtEntry;       // to store instrns added at entry
84   LoopInfo *LoopDepthCalc;              // to calculate loop depths 
85   ReservedColorListType ResColList;     // A set of reserved regs if desired.
86                                         // currently not used
87
88 public:
89   PhyRegAlloc(Function *F, const TargetMachine& TM, FunctionLiveVarInfo *Lvi,
90               LoopInfo *LoopDepthCalc);
91   ~PhyRegAlloc();
92
93   // main method called for allocating registers
94   //
95   void allocateRegisters();           
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   
107 private:
108
109
110
111   //------- ------------------ private methods---------------------------------
112
113   void addInterference(const Value *Def, const ValueSet *LVSet, 
114                        bool isCallInst);
115
116   void addInterferencesForArgs();
117   void createIGNodeListsAndIGs();
118   void buildInterferenceGraphs();
119
120   void setCallInterferences(const MachineInstr *MInst, 
121                             const ValueSet *LVSetAft );
122
123   void move2DelayedInstr(const MachineInstr *OrigMI, 
124                          const MachineInstr *DelayedMI );
125
126   void markUnusableSugColors();
127   void allocateStackSpace4SpilledLRs();
128
129   void insertCode4SpilledLR     (const LiveRange *LR, 
130                                  MachineInstr *MInst,
131                                  const BasicBlock *BB,
132                                  const unsigned OpNum);
133
134   inline void constructLiveRanges() { LRI.constructLiveRanges(); }      
135
136   void colorIncomingArgs();
137   void colorCallRetArgs();
138   void updateMachineCode();
139
140   void printLabel(const Value *const Val);
141   void printMachineCode();
142
143   friend class UltraSparcRegInfo;
144
145
146   int getUsableUniRegAtMI(int RegType, 
147                           const ValueSet *LVSetBef,
148                           MachineInstr *MInst,
149                           std::vector<MachineInstr*>& MIBef,
150                           std::vector<MachineInstr*>& MIAft);
151   
152   int getUnusedUniRegAtMI(RegClass *RC,  const MachineInstr *MInst, 
153                           const ValueSet *LVSetBef);
154
155   void setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst );
156   int getUniRegNotUsedByThisInst(RegClass *RC, const MachineInstr *MInst);
157
158   void addInterf4PseudoInstr(const MachineInstr *MInst);
159 };
160
161
162 #endif
163