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