* Minor cleanups
[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 "llvm/Analysis/LoopDepth.h"
36 #include <deque>
37 class MachineCodeForMethod;
38 class MachineRegInfo;
39 class MethodLiveVarInfo;
40 class MachineInstr;
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
72   std::vector<RegClass *> RegClassList; // vector of register classes
73   const TargetMachine &TM;              // target machine
74   const Method* Meth;                   // name of the method we work on
75   MachineCodeForMethod &mcInfo;         // descriptor for method's native code
76   MethodLiveVarInfo *const LVI;         // LV information for this method 
77                                         // (already computed for BBs) 
78   LiveRangeInfo LRI;                    // LR info  (will be computed)
79   const MachineRegInfo &MRI;            // Machine Register information
80   const unsigned NumOfRegClasses;       // recorded here for efficiency
81
82   
83   AddedInstrMapType AddedInstrMap;      // to store instrns added in this phase
84   cfg::LoopDepthCalculator LoopDepthCalc;    // to calculate loop depths 
85   ReservedColorListType ResColList;     // A set of reserved regs if desired.
86                                         // currently not used
87
88
89
90   //------- ------------------ private methods---------------------------------
91
92   void addInterference(const Value *const Def, const LiveVarSet *const LVSet, 
93                        const bool isCallInst);
94
95   void addInterferencesForArgs();
96   void createIGNodeListsAndIGs();
97   void buildInterferenceGraphs();
98
99   void setCallInterferences(const MachineInstr *MInst, 
100                             const LiveVarSet *const LVSetAft );
101
102   void move2DelayedInstr(const MachineInstr *OrigMI, 
103                          const MachineInstr *DelayedMI );
104
105   void markUnusableSugColors();
106   void allocateStackSpace4SpilledLRs();
107
108   void insertCode4SpilledLR     (const LiveRange *LR, 
109                                  MachineInstr *MInst,
110                                  const BasicBlock *BB,
111                                  const unsigned OpNum);
112
113   inline void constructLiveRanges() { LRI.constructLiveRanges(); }      
114
115   void colorIncomingArgs();
116   void colorCallRetArgs();
117   void updateMachineCode();
118
119   void printLabel(const Value *const Val);
120   void printMachineCode();
121
122   friend class UltraSparcRegInfo;
123
124
125   int getUsableUniRegAtMI(RegClass *RC,  const int RegType, 
126                           const MachineInstr *MInst,
127                           const LiveVarSet *LVSetBef, MachineInstr *MIBef, 
128                           MachineInstr *MIAft );
129
130   int getUnusedUniRegAtMI(RegClass *RC,  const MachineInstr *MInst, 
131                        const LiveVarSet *LVSetBef);
132
133   void setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst );
134   int getUniRegNotUsedByThisInst(RegClass *RC, const MachineInstr *MInst);
135
136   void addInterf4PseudoInstr(const MachineInstr *MInst);
137
138  public:
139   PhyRegAlloc(Method *const M, const TargetMachine& TM, 
140               MethodLiveVarInfo *const Lvi);
141   ~PhyRegAlloc(); 
142
143   // main method called for allocating registers
144   //
145   void allocateRegisters();           
146
147 };
148
149
150 #endif
151