Big bug fix: getUsableUniRegAtMI needed to return values in arguments
[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       MethodLiveVarInfo LVI(*MethodI );           // compute LV info
21       LVI.analyze();
22
23       TargetMachine &target = ....                              
24
25
26       PhyRegAlloc PRA(*MethodI, 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 MethodLiveVarInfo;
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 class AddedInstrns
52 {
53  public:
54   std::deque<MachineInstr*> InstrnsBefore;// Added insts BEFORE an existing inst
55   std::deque<MachineInstr*> InstrnsAfter; // Added insts AFTER an existing inst
56 };
57
58 typedef std::hash_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 Method.
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 Method* Meth;                   // name of the method we work on
74   MachineCodeForMethod &mcInfo;         // descriptor for method's native code
75   MethodLiveVarInfo *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   cfg::LoopInfo *LoopDepthCalc;         // to calculate loop depths 
84   ReservedColorListType ResColList;     // A set of reserved regs if desired.
85                                         // currently not used
86
87 public:
88   PhyRegAlloc(Method *M, const TargetMachine& TM, MethodLiveVarInfo *Lvi,
89               cfg::LoopInfo *LoopDepthCalc);
90   ~PhyRegAlloc();
91
92   // main method called for allocating registers
93   //
94   void allocateRegisters();           
95
96
97   // access to register classes by class ID
98   // 
99   const RegClass*  getRegClassByID(unsigned int id) const {
100                                                     return RegClassList[id];
101   }
102         RegClass*  getRegClassByID(unsigned int id)       {
103                                                     return RegClassList[id]; }
104   
105   
106 private:
107
108
109
110   //------- ------------------ private methods---------------------------------
111
112   void addInterference(const Value *Def, const ValueSet *LVSet, 
113                        bool isCallInst);
114
115   void addInterferencesForArgs();
116   void createIGNodeListsAndIGs();
117   void buildInterferenceGraphs();
118
119   void setCallInterferences(const MachineInstr *MInst, 
120                             const ValueSet *LVSetAft );
121
122   void move2DelayedInstr(const MachineInstr *OrigMI, 
123                          const MachineInstr *DelayedMI );
124
125   void markUnusableSugColors();
126   void allocateStackSpace4SpilledLRs();
127
128   void insertCode4SpilledLR     (const LiveRange *LR, 
129                                  MachineInstr *MInst,
130                                  const BasicBlock *BB,
131                                  const unsigned OpNum);
132
133   inline void constructLiveRanges() { LRI.constructLiveRanges(); }      
134
135   void colorIncomingArgs();
136   void colorCallRetArgs();
137   void updateMachineCode();
138
139   void printLabel(const Value *const Val);
140   void printMachineCode();
141
142   friend class UltraSparcRegInfo;
143
144
145   int getUsableUniRegAtMI(RegClass *RC, int RegType, 
146                           const MachineInstr *MInst,
147                           const ValueSet *LVSetBef, MachineInstr *&MIBef, 
148                           MachineInstr *&MIAft );
149
150   int getUnusedUniRegAtMI(RegClass *RC,  const MachineInstr *MInst, 
151                        const ValueSet *LVSetBef);
152
153   void setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst );
154   int getUniRegNotUsedByThisInst(RegClass *RC, const MachineInstr *MInst);
155
156   void addInterf4PseudoInstr(const MachineInstr *MInst);
157 };
158
159
160 #endif
161