1 /* Title: PhyRegAlloc.h -*- C++ -*-
2 Author: Ruchira Sasanka
4 Purpose: This is the main entry point for register allocation.
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.
15 * Machine dependent work: All parts of the register coloring algorithm
16 except coloring of an individual node are machine independent.
18 Register allocation must be done as:
20 FunctionLiveVarInfo LVI(*FunctionI ); // compute LV info
23 TargetMachine &target = ....
26 PhyRegAlloc PRA(*FunctionI, target, &LVI); // allocate regs
27 PRA.allocateRegisters();
30 #ifndef PHY_REG_ALLOC_H
31 #define PHY_REG_ALLOC_H
33 #include "llvm/CodeGen/RegClass.h"
34 #include "llvm/CodeGen/LiveRangeInfo.h"
38 class MachineCodeForMethod;
40 class FunctionLiveVarInfo;
44 //----------------------------------------------------------------------------
45 // Class AddedInstrns:
46 // When register allocator inserts new instructions in to the existing
47 // instruction stream, it does NOT directly modify the instruction stream.
48 // Rather, it creates an object of AddedInstrns and stick it in the
49 // AddedInstrMap for an existing instruction. This class contains two vectors
50 // to store such instructions added before and after an existing instruction.
51 //----------------------------------------------------------------------------
54 std::vector<MachineInstr*> InstrnsBefore;// Added insts BEFORE an existing inst
55 std::vector<MachineInstr*> InstrnsAfter; // Added insts AFTER an existing inst
58 typedef std::map<const MachineInstr *, AddedInstrns> AddedInstrMapType;
62 //----------------------------------------------------------------------------
64 // Main class the register allocator. Call allocateRegisters() to allocate
65 // registers for a Function.
66 //----------------------------------------------------------------------------
69 class PhyRegAlloc: public NonCopyable {
71 std::vector<RegClass *> RegClassList; // vector of register classes
72 const TargetMachine &TM; // target machine
73 const Function *Meth; // name of the function we work on
74 MachineCodeForMethod &mcInfo; // descriptor for method's native code
75 FunctionLiveVarInfo *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
82 AddedInstrMapType AddedInstrMap; // to store instrns added in this phase
83 AddedInstrns AddedInstrAtEntry; // to store instrns added at entry
84 LoopInfo *LoopDepthCalc; // to calculate loop depths
85 ReservedColorListType ResColList; // A set of reserved regs if desired.
89 PhyRegAlloc(Function *F, const TargetMachine& TM, FunctionLiveVarInfo *Lvi,
90 LoopInfo *LoopDepthCalc);
93 // main method called for allocating registers
95 void allocateRegisters();
98 // access to register classes by class ID
100 const RegClass* getRegClassByID(unsigned int id) const {
101 return RegClassList[id];
103 RegClass* getRegClassByID(unsigned int id) {
104 return RegClassList[id]; }
111 //------- ------------------ private methods---------------------------------
113 void addInterference(const Value *Def, const ValueSet *LVSet,
116 void addInterferencesForArgs();
117 void createIGNodeListsAndIGs();
118 void buildInterferenceGraphs();
120 void setCallInterferences(const MachineInstr *MInst,
121 const ValueSet *LVSetAft );
123 void move2DelayedInstr(const MachineInstr *OrigMI,
124 const MachineInstr *DelayedMI );
126 void markUnusableSugColors();
127 void allocateStackSpace4SpilledLRs();
129 void insertCode4SpilledLR (const LiveRange *LR,
131 const BasicBlock *BB,
132 const unsigned OpNum);
134 inline void constructLiveRanges() { LRI.constructLiveRanges(); }
136 void colorIncomingArgs();
137 void colorCallRetArgs();
138 void updateMachineCode();
140 void printLabel(const Value *const Val);
141 void printMachineCode();
143 friend class UltraSparcRegInfo;
146 int getUsableUniRegAtMI(int RegType,
147 const ValueSet *LVSetBef,
149 std::vector<MachineInstr*>& MIBef,
150 std::vector<MachineInstr*>& MIAft);
152 int getUnusedUniRegAtMI(RegClass *RC, const MachineInstr *MInst,
153 const ValueSet *LVSetBef);
155 void setRelRegsUsedByThisInst(RegClass *RC, const MachineInstr *MInst );
156 int getUniRegNotUsedByThisInst(RegClass *RC, const MachineInstr *MInst);
158 void addInterf4PseudoInstr(const MachineInstr *MInst);