s/Method/Function
[oota-llvm.git] / lib / Target / SparcV9 / 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 <deque>
36 class MachineCodeForMethod;
37 class MachineRegInfo;
38 class FunctionLiveVarInfo;
39 class MachineInstr;
40 namespace cfg { class LoopInfo; }
41
42 //----------------------------------------------------------------------------
43 // Class AddedInstrns:
44 // When register allocator inserts new instructions in to the existing 
45 // instruction stream, it does NOT directly modify the instruction stream.
46 // Rather, it creates an object of AddedInstrns and stick it in the 
47 // AddedInstrMap for an existing instruction. This class contains two vectors
48 // to store such instructions added before and after an existing instruction.
49 //----------------------------------------------------------------------------
50
51 struct AddedInstrns {
52   std::deque<MachineInstr*> InstrnsBefore;// Added insts BEFORE an existing inst
53   std::deque<MachineInstr*> InstrnsAfter; // Added insts AFTER an existing inst
54 };
55
56 typedef std::map<const MachineInstr *, AddedInstrns> AddedInstrMapType;
57
58
59
60 //----------------------------------------------------------------------------
61 // class PhyRegAlloc:
62 // Main class the register allocator. Call allocateRegisters() to allocate
63 // registers for a Function.
64 //----------------------------------------------------------------------------
65
66
67 class PhyRegAlloc: public NonCopyable {
68
69   std::vector<RegClass *> RegClassList; // vector of register classes
70   const TargetMachine &TM;              // target machine
71   const Function *Meth;                 // name of the function we work on
72   MachineCodeForMethod &mcInfo;         // descriptor for method's native code
73   FunctionLiveVarInfo *const LVI;         // LV information for this method 
74                                         // (already computed for BBs) 
75   LiveRangeInfo LRI;                    // LR info  (will be computed)
76   const MachineRegInfo &MRI;            // Machine Register information
77   const unsigned NumOfRegClasses;       // recorded here for efficiency
78
79   
80   AddedInstrMapType AddedInstrMap;      // to store instrns added in this phase
81   AddedInstrns AddedInstrAtEntry;       // to store instrns added at entry
82   cfg::LoopInfo *LoopDepthCalc;         // to calculate loop depths 
83   ReservedColorListType ResColList;     // A set of reserved regs if desired.
84                                         // currently not used
85
86 public:
87   PhyRegAlloc(Function *F, const TargetMachine& TM, FunctionLiveVarInfo *Lvi,
88               cfg::LoopInfo *LoopDepthCalc);
89   ~PhyRegAlloc();
90
91   // main method called for allocating registers
92   //
93   void allocateRegisters();           
94
95
96   // access to register classes by class ID
97   // 
98   const RegClass*  getRegClassByID(unsigned int id) const {
99                                                     return RegClassList[id];
100   }
101         RegClass*  getRegClassByID(unsigned int id)       {
102                                                     return RegClassList[id]; }
103   
104   
105 private:
106
107
108
109   //------- ------------------ private methods---------------------------------
110
111   void addInterference(const Value *Def, const ValueSet *LVSet, 
112                        bool isCallInst);
113
114   void addInterferencesForArgs();
115   void createIGNodeListsAndIGs();
116   void buildInterferenceGraphs();
117
118   void setCallInterferences(const MachineInstr *MInst, 
119                             const ValueSet *LVSetAft );
120
121   void move2DelayedInstr(const MachineInstr *OrigMI, 
122                          const MachineInstr *DelayedMI );
123
124   void markUnusableSugColors();
125   void allocateStackSpace4SpilledLRs();
126
127   void insertCode4SpilledLR     (const LiveRange *LR, 
128                                  MachineInstr *MInst,
129                                  const BasicBlock *BB,
130                                  const unsigned OpNum);
131
132   inline void constructLiveRanges() { LRI.constructLiveRanges(); }      
133
134   void colorIncomingArgs();
135   void colorCallRetArgs();
136   void updateMachineCode();
137
138   void printLabel(const Value *const Val);
139   void printMachineCode();
140
141   friend class UltraSparcRegInfo;
142
143
144   int getUsableUniRegAtMI(RegClass *RC, int RegType, 
145                           const MachineInstr *MInst,
146                           const ValueSet *LVSetBef, MachineInstr *&MIBef, 
147                           MachineInstr *&MIAft );
148
149   int getUnusedUniRegAtMI(RegClass *RC,  const MachineInstr *MInst, 
150                        const ValueSet *LVSetBef);
151
152   void setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst );
153   int getUniRegNotUsedByThisInst(RegClass *RC, const MachineInstr *MInst);
154
155   void addInterf4PseudoInstr(const MachineInstr *MInst);
156 };
157
158
159 #endif
160