blob: 195275fa6563a2a55bdade85de767c2d00410a81 [file] [log] [blame]
Chris Lattner7f74a562002-01-20 22:54:45 +00001/* Title: PhyRegAlloc.h -*- C++ -*-
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +00002 Author: Ruchira Sasanka
3 Date: Aug 20, 01
4 Purpose: This is the main entry point for register allocation.
5
6 Notes:
Ruchira Sasankaf20079d2002-01-07 19:16:26 +00007 =====
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +00008
9 * RegisterClasses: Each RegClass accepts a
Chris Lattnerf9781b52002-12-29 03:13:05 +000010 TargetRegClass which contains machine specific info about that register
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000011 class. The code in the RegClass is machine independent and they use
Chris Lattnerf9781b52002-12-29 03:13:05 +000012 access functions in the TargetRegClass object passed into it to get
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000013 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.
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000017*/
18
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000019#ifndef PHY_REG_ALLOC_H
20#define PHY_REG_ALLOC_H
21
Chris Lattnere80612a2003-09-01 20:12:17 +000022#include "LiveRangeInfo.h"
Vikram S. Adve91e75d82003-07-29 19:37:41 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner50cf8f12002-04-28 20:40:16 +000024#include <map>
25
Misha Brukman7ae7f842002-10-28 00:28:31 +000026class MachineFunction;
Chris Lattnerf9781b52002-12-29 03:13:05 +000027class TargetRegInfo;
Chris Lattnerf9986852002-04-27 07:27:19 +000028class FunctionLiveVarInfo;
Chris Lattnerb0da8b22002-02-04 05:52:08 +000029class MachineInstr;
Chris Lattner002958c2002-04-28 16:19:42 +000030class LoopInfo;
Chris Lattner19a7cb22003-01-15 19:56:21 +000031class RegClass;
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000032
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
Chris Lattner30e23da2002-04-09 05:13:04 +000042struct AddedInstrns {
Chris Lattnerb1e39b52002-10-28 19:43:23 +000043 std::vector<MachineInstr*> InstrnsBefore;//Insts added BEFORE an existing inst
44 std::vector<MachineInstr*> InstrnsAfter; //Insts added AFTER an existing inst
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000045};
46
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000047//----------------------------------------------------------------------------
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000048// class PhyRegAlloc:
49// Main class the register allocator. Call allocateRegisters() to allocate
Chris Lattnerf739fa82002-04-08 22:03:57 +000050// registers for a Function.
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000051//----------------------------------------------------------------------------
52
Chris Lattnere62a2a72003-08-05 22:03:27 +000053class PhyRegAlloc {
Chris Lattner7f74a562002-01-20 22:54:45 +000054 std::vector<RegClass *> RegClassList; // vector of register classes
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000055 const TargetMachine &TM; // target machine
Chris Lattnerb1e39b52002-10-28 19:43:23 +000056 const Function *Fn; // name of the function we work on
57 MachineFunction &MF; // descriptor for method's native code
Chris Lattner002958c2002-04-28 16:19:42 +000058 FunctionLiveVarInfo *const LVI; // LV information for this method
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000059 // (already computed for BBs)
60 LiveRangeInfo LRI; // LR info (will be computed)
Chris Lattnerf9781b52002-12-29 03:13:05 +000061 const TargetRegInfo &MRI; // Machine Register information
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000062 const unsigned NumOfRegClasses; // recorded here for efficiency
63
Vikram S. Adve24ce4d82003-05-31 07:41:54 +000064 // Map to indicate whether operands of each MachineInstr have been updated
65 // according to their assigned colors. This is primarily for debugging and
66 // could be removed in the long run.
67 std::map<const MachineInstr *, bool> OperandsColoredMap;
Ruchira Sasankaca632ed2001-11-03 17:14:44 +000068
Chris Lattner76014b92002-10-29 17:08:05 +000069 // AddedInstrMap - Used to store instrns added in this phase
70 std::map<const MachineInstr *, AddedInstrns> AddedInstrMap;
71
Chris Lattner92f5fb52003-08-05 22:09:31 +000072 // ScratchRegsUsed - Contains scratch register uses for a particular MI.
73 typedef std::multimap<const MachineInstr*, int> ScratchRegsUsedTy;
74 ScratchRegsUsedTy ScratchRegsUsed;
75
Vikram S. Adve40221aa2002-04-25 04:46:28 +000076 AddedInstrns AddedInstrAtEntry; // to store instrns added at entry
Chris Lattner002958c2002-04-28 16:19:42 +000077 LoopInfo *LoopDepthCalc; // to calculate loop depths
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000078
Chris Lattnere62a2a72003-08-05 22:03:27 +000079 PhyRegAlloc(const PhyRegAlloc&); // DO NOT IMPLEMENT
80 void operator=(const PhyRegAlloc&); // DO NOT IMPLEMENT
Chris Lattner669a74c2002-02-04 17:38:48 +000081public:
Chris Lattnerf9986852002-04-27 07:27:19 +000082 PhyRegAlloc(Function *F, const TargetMachine& TM, FunctionLiveVarInfo *Lvi,
Chris Lattner002958c2002-04-28 16:19:42 +000083 LoopInfo *LoopDepthCalc);
Chris Lattner669a74c2002-02-04 17:38:48 +000084 ~PhyRegAlloc();
85
86 // main method called for allocating registers
87 //
88 void allocateRegisters();
Vikram S. Advececde712002-03-18 03:26:48 +000089
Vikram S. Advececde712002-03-18 03:26:48 +000090 // access to register classes by class ID
91 //
Chris Lattnere62a2a72003-08-05 22:03:27 +000092 const RegClass* getRegClassByID(unsigned id) const {
Vikram S. Adveabcd8d72003-07-25 21:00:13 +000093 return RegClassList[id];
Vikram S. Advececde712002-03-18 03:26:48 +000094 }
Chris Lattnere62a2a72003-08-05 22:03:27 +000095 RegClass* getRegClassByID(unsigned id) {
Vikram S. Adveabcd8d72003-07-25 21:00:13 +000096 return RegClassList[id];
97 }
Vikram S. Adve91e75d82003-07-29 19:37:41 +000098
Chris Lattner669a74c2002-02-04 17:38:48 +000099private:
Chris Lattnerb1def732002-02-05 02:51:01 +0000100 void addInterference(const Value *Def, const ValueSet *LVSet,
101 bool isCallInst);
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000102
103 void addInterferencesForArgs();
104 void createIGNodeListsAndIGs();
105 void buildInterferenceGraphs();
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000106
Chris Lattnere62a2a72003-08-05 22:03:27 +0000107 void setCallInterferences(const MachineInstr *MI,
108 const ValueSet *LVSetAft);
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000109
Ruchira Sasanka33b0d852001-10-23 21:38:42 +0000110 void move2DelayedInstr(const MachineInstr *OrigMI,
Chris Lattnere62a2a72003-08-05 22:03:27 +0000111 const MachineInstr *DelayedMI);
Ruchira Sasanka33b0d852001-10-23 21:38:42 +0000112
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000113 void markUnusableSugColors();
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000114 void allocateStackSpace4SpilledLRs();
115
Chris Lattnere62a2a72003-08-05 22:03:27 +0000116 void insertCode4SpilledLR(const LiveRange *LR,
117 MachineBasicBlock::iterator& MII,
118 MachineBasicBlock &MBB, unsigned OpNum);
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000119
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000120 // Method for inserting caller saving code. The caller must save all the
121 // volatile registers live across a call.
122 void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
123 std::vector<MachineInstr*>& instrnsAfter,
124 MachineInstr *CallMI,
125 const BasicBlock *BB);
126
Chris Lattner7f74a562002-01-20 22:54:45 +0000127 inline void constructLiveRanges() { LRI.constructLiveRanges(); }
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000128
129 void colorIncomingArgs();
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000130 void colorCallRetArgs();
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000131 void updateMachineCode();
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000132 void updateInstruction(MachineBasicBlock::iterator& MII,
133 MachineBasicBlock &MBB);
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000134
Chris Lattnere62a2a72003-08-05 22:03:27 +0000135 void printLabel(const Value *Val);
Ruchira Sasanka86b2ad42001-09-15 19:08:41 +0000136 void printMachineCode();
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000137
Ruchira Sasanka51fc1c22001-11-03 20:41:22 +0000138
Chris Lattnere62a2a72003-08-05 22:03:27 +0000139 int getUsableUniRegAtMI(int RegType, const ValueSet *LVSetBef,
140 MachineInstr *MI,
Vikram S. Advebeb36402002-07-08 22:39:36 +0000141 std::vector<MachineInstr*>& MIBef,
142 std::vector<MachineInstr*>& MIAft);
143
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000144 // Callback method used to find unused registers.
145 // LVSetBef is the live variable set to search for an unused register.
Chris Lattnere62a2a72003-08-05 22:03:27 +0000146 // If it is not specified, the LV set before the current MI is used.
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000147 // This is sufficient as long as no new copy instructions are generated
148 // to copy the free register to memory.
149 //
Chris Lattnere62a2a72003-08-05 22:03:27 +0000150 int getUnusedUniRegAtMI(RegClass *RC, int RegType,
151 const MachineInstr *MI,
Vikram S. Adve91e75d82003-07-29 19:37:41 +0000152 const ValueSet *LVSetBef = 0);
153
Chris Lattnere62a2a72003-08-05 22:03:27 +0000154 void setRelRegsUsedByThisInst(RegClass *RC, int RegType,
155 const MachineInstr *MI);
Vikram S. Adveabcd8d72003-07-25 21:00:13 +0000156
Chris Lattnere62a2a72003-08-05 22:03:27 +0000157 int getUniRegNotUsedByThisInst(RegClass *RC, int RegType,
158 const MachineInstr *MI);
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000159
Chris Lattnere62a2a72003-08-05 22:03:27 +0000160 void addInterf4PseudoInstr(const MachineInstr *MI);
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000161};
162
163
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000164#endif
165