Added support method for setting interference for pseudo instructions
[oota-llvm.git] / lib / Target / SparcV9 / RegAlloc / PhyRegAlloc.h
1 /* Title:   PhyRegAlloc.h
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  * RegisterClasses: Each RegClass accepts a 
9    MachineRegClass which contains machine specific info about that register
10    class. The code in the RegClass is machine independent and they use
11    access functions in the MachineRegClass object passed into it to get
12    machine specific info.
13
14  * Machine dependent work: All parts of the register coloring algorithm
15    except coloring of an individual node are machine independent.
16
17    Register allocation must be done  as:
18
19      static const MachineRegInfo MRI = MachineRegInfo();  // machine reg info 
20
21      MethodLiveVarInfo LVI(*MethodI );                    // compute LV info
22      LVI.analyze();
23
24      PhyRegAlloc PRA(*MethodI, &MRI, &LVI);               // allocate regs
25      PRA.allocateRegisters();
26
27    Assumptions: 
28      All values in a live range will be of the same physical reg class.
29
30 */ 
31
32 #ifndef PHY_REG_ALLOC_H
33 #define PHY_REG_ALLOC_H
34
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/RegClass.h"
37 #include "llvm/CodeGen/LiveRangeInfo.h"
38 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
39
40 #include <deque>
41
42
43 //----------------------------------------------------------------------------
44 // Class AddedInstrns:
45 // When register allocator inserts new instructions in to the existing 
46 // instruction stream, it does NOT directly modify the instruction stream.
47 // Rather, it creates an object of AddedInstrns and stick it in the 
48 // AddedInstrMap for an existing instruction. This class contains two vectors
49 // to store such instructions added before and after an existing instruction.
50 //----------------------------------------------------------------------------
51
52 class AddedInstrns
53 {
54  public:
55   deque<MachineInstr *> InstrnsBefore;  // Added insts BEFORE an existing inst
56   deque<MachineInstr *> InstrnsAfter;   // Added insts AFTER an existing inst
57
58   AddedInstrns() : InstrnsBefore(), InstrnsAfter() { }
59 };
60
61 typedef hash_map<const MachineInstr *, AddedInstrns *> AddedInstrMapType;
62
63
64
65 //----------------------------------------------------------------------------
66 // class PhyRegAlloc:
67 // Main class the register allocator. Call allocateRegisters() to allocate
68 // registers for a Method.
69 //----------------------------------------------------------------------------
70
71
72 class PhyRegAlloc: public NonCopyable
73 {
74
75   vector<RegClass *> RegClassList  ;    // vector of register classes
76   const TargetMachine &TM;              // target machine
77   const Method* Meth;                   // name of the method we work on
78   MachineCodeForMethod& mcInfo;         // descriptor for method's native code
79   MethodLiveVarInfo *const LVI;         // LV information for this method 
80                                         // (already computed for BBs) 
81   LiveRangeInfo LRI;                    // LR info  (will be computed)
82   const MachineRegInfo &MRI;            // Machine Register information
83   const unsigned NumOfRegClasses;       // recorded here for efficiency
84
85   //vector<const Instruction *> CallInstrList;  // a list of all call instrs
86   //vector<const Instruction *> RetInstrList;   // a list of all return instrs
87
88   
89   AddedInstrMapType AddedInstrMap;      // to store instrns added in this phase
90
91   //vector<const MachineInstr *> PhiInstList;   // a list of all phi instrs
92
93   //------- private methods ---------------------------------------------------
94
95   void addInterference(const Value *const Def, const LiveVarSet *const LVSet, 
96                        const bool isCallInst);
97
98   void addInterferencesForArgs();
99   void createIGNodeListsAndIGs();
100   void buildInterferenceGraphs();
101   //void insertCallerSavingCode(const MachineInstr *MInst, 
102   //                          const BasicBlock *BB );
103
104   void setCallInterferences(const MachineInstr *MInst, 
105                             const LiveVarSet *const LVSetAft );
106
107   void move2DelayedInstr(const MachineInstr *OrigMI, 
108                          const MachineInstr *DelayedMI );
109
110   void markUnusableSugColors();
111   void allocateStackSpace4SpilledLRs();
112
113   void insertCode4SpilledLR     (const LiveRange *LR, 
114                                  MachineInstr *MInst,
115                                  const BasicBlock *BB,
116                                  const unsigned OpNum);
117
118   inline void constructLiveRanges() 
119     { LRI.constructLiveRanges(); }      
120
121   void colorIncomingArgs();
122   void colorCallRetArgs();
123   void updateMachineCode();
124
125   void printLabel(const Value *const Val);
126   void printMachineCode();
127
128   friend class UltraSparcRegInfo;
129
130
131   int getUsableRegAtMI(RegClass *RC,  const int RegType, const MachineInstr *MInst,
132                        const LiveVarSet *LVSetBef, MachineInstr *MIBef, 
133                        MachineInstr *MIAft );
134
135   int getUnusedRegAtMI(RegClass *RC,  const MachineInstr *MInst, 
136                        const LiveVarSet *LVSetBef);
137
138   void setRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst );
139   int getRegNotUsedByThisInst(RegClass *RC, const MachineInstr *MInst);
140
141   void addInterf4PseudoInstr(const MachineInstr *MInst);
142
143
144  public:
145
146   PhyRegAlloc(Method *const M, const TargetMachine& TM, 
147               MethodLiveVarInfo *const Lvi);
148
149   void allocateRegisters();             // main method called for allocatin
150
151 };
152
153
154 #endif
155