2002-01-20 22:54:45 +00:00
|
|
|
/* Title: PhyRegAlloc.h -*- C++ -*-
|
2001-09-08 14:22:50 +00:00
|
|
|
Author: Ruchira Sasanka
|
|
|
|
Date: Aug 20, 01
|
|
|
|
Purpose: This is the main entry point for register allocation.
|
|
|
|
|
|
|
|
Notes:
|
2002-01-07 19:16:26 +00:00
|
|
|
=====
|
2001-09-08 14:22:50 +00:00
|
|
|
|
|
|
|
* RegisterClasses: Each RegClass accepts a
|
2002-12-29 03:13:05 +00:00
|
|
|
TargetRegClass which contains machine specific info about that register
|
2001-09-08 14:22:50 +00:00
|
|
|
class. The code in the RegClass is machine independent and they use
|
2002-12-29 03:13:05 +00:00
|
|
|
access functions in the TargetRegClass object passed into it to get
|
2001-09-08 14:22:50 +00:00
|
|
|
machine specific info.
|
|
|
|
|
|
|
|
* Machine dependent work: All parts of the register coloring algorithm
|
|
|
|
except coloring of an individual node are machine independent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PHY_REG_ALLOC_H
|
|
|
|
#define PHY_REG_ALLOC_H
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/LiveRangeInfo.h"
|
2003-07-29 19:37:41 +00:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2002-04-28 20:40:16 +00:00
|
|
|
#include <map>
|
|
|
|
|
2002-10-28 00:28:31 +00:00
|
|
|
class MachineFunction;
|
2002-12-29 03:13:05 +00:00
|
|
|
class TargetRegInfo;
|
2002-04-27 07:27:19 +00:00
|
|
|
class FunctionLiveVarInfo;
|
2002-02-04 05:52:08 +00:00
|
|
|
class MachineInstr;
|
2002-04-28 16:19:42 +00:00
|
|
|
class LoopInfo;
|
2003-01-15 19:56:21 +00:00
|
|
|
class RegClass;
|
2001-10-28 18:15:12 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Class AddedInstrns:
|
|
|
|
// When register allocator inserts new instructions in to the existing
|
|
|
|
// instruction stream, it does NOT directly modify the instruction stream.
|
|
|
|
// Rather, it creates an object of AddedInstrns and stick it in the
|
|
|
|
// AddedInstrMap for an existing instruction. This class contains two vectors
|
|
|
|
// to store such instructions added before and after an existing instruction.
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2002-04-09 05:13:04 +00:00
|
|
|
struct AddedInstrns {
|
2002-10-28 19:43:23 +00:00
|
|
|
std::vector<MachineInstr*> InstrnsBefore;//Insts added BEFORE an existing inst
|
|
|
|
std::vector<MachineInstr*> InstrnsAfter; //Insts added AFTER an existing inst
|
2001-09-08 14:22:50 +00:00
|
|
|
};
|
|
|
|
|
2001-10-28 18:15:12 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// class PhyRegAlloc:
|
|
|
|
// Main class the register allocator. Call allocateRegisters() to allocate
|
2002-04-08 22:03:57 +00:00
|
|
|
// registers for a Function.
|
2001-10-28 18:15:12 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
class PhyRegAlloc {
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<RegClass *> RegClassList; // vector of register classes
|
2001-09-08 14:22:50 +00:00
|
|
|
const TargetMachine &TM; // target machine
|
2002-10-28 19:43:23 +00:00
|
|
|
const Function *Fn; // name of the function we work on
|
|
|
|
MachineFunction &MF; // descriptor for method's native code
|
2002-04-28 16:19:42 +00:00
|
|
|
FunctionLiveVarInfo *const LVI; // LV information for this method
|
2001-09-08 14:22:50 +00:00
|
|
|
// (already computed for BBs)
|
|
|
|
LiveRangeInfo LRI; // LR info (will be computed)
|
2002-12-29 03:13:05 +00:00
|
|
|
const TargetRegInfo &MRI; // Machine Register information
|
2001-09-08 14:22:50 +00:00
|
|
|
const unsigned NumOfRegClasses; // recorded here for efficiency
|
|
|
|
|
2003-05-31 07:41:54 +00:00
|
|
|
// Map to indicate whether operands of each MachineInstr have been updated
|
|
|
|
// according to their assigned colors. This is primarily for debugging and
|
|
|
|
// could be removed in the long run.
|
|
|
|
std::map<const MachineInstr *, bool> OperandsColoredMap;
|
2001-11-03 17:14:44 +00:00
|
|
|
|
2002-10-29 17:08:05 +00:00
|
|
|
// AddedInstrMap - Used to store instrns added in this phase
|
|
|
|
std::map<const MachineInstr *, AddedInstrns> AddedInstrMap;
|
|
|
|
|
2003-08-05 22:09:31 +00:00
|
|
|
// ScratchRegsUsed - Contains scratch register uses for a particular MI.
|
|
|
|
typedef std::multimap<const MachineInstr*, int> ScratchRegsUsedTy;
|
|
|
|
ScratchRegsUsedTy ScratchRegsUsed;
|
|
|
|
|
2002-04-25 04:46:28 +00:00
|
|
|
AddedInstrns AddedInstrAtEntry; // to store instrns added at entry
|
2002-04-28 16:19:42 +00:00
|
|
|
LoopInfo *LoopDepthCalc; // to calculate loop depths
|
2001-09-08 14:22:50 +00:00
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
PhyRegAlloc(const PhyRegAlloc&); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const PhyRegAlloc&); // DO NOT IMPLEMENT
|
2002-02-04 17:38:48 +00:00
|
|
|
public:
|
2002-04-27 07:27:19 +00:00
|
|
|
PhyRegAlloc(Function *F, const TargetMachine& TM, FunctionLiveVarInfo *Lvi,
|
2002-04-28 16:19:42 +00:00
|
|
|
LoopInfo *LoopDepthCalc);
|
2002-02-04 17:38:48 +00:00
|
|
|
~PhyRegAlloc();
|
|
|
|
|
|
|
|
// main method called for allocating registers
|
|
|
|
//
|
|
|
|
void allocateRegisters();
|
2002-03-18 03:26:48 +00:00
|
|
|
|
|
|
|
// access to register classes by class ID
|
|
|
|
//
|
2003-08-05 22:03:27 +00:00
|
|
|
const RegClass* getRegClassByID(unsigned id) const {
|
2003-07-25 21:00:13 +00:00
|
|
|
return RegClassList[id];
|
|
|
|
}
|
2003-08-05 22:03:27 +00:00
|
|
|
RegClass* getRegClassByID(unsigned id) {
|
2003-07-25 21:00:13 +00:00
|
|
|
return RegClassList[id];
|
2002-03-18 03:26:48 +00:00
|
|
|
}
|
2003-07-29 19:37:41 +00:00
|
|
|
|
2002-02-04 17:38:48 +00:00
|
|
|
private:
|
2002-02-05 02:51:01 +00:00
|
|
|
void addInterference(const Value *Def, const ValueSet *LVSet,
|
|
|
|
bool isCallInst);
|
2001-09-08 14:22:50 +00:00
|
|
|
|
|
|
|
void addInterferencesForArgs();
|
|
|
|
void createIGNodeListsAndIGs();
|
|
|
|
void buildInterferenceGraphs();
|
2001-10-16 01:23:19 +00:00
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
void setCallInterferences(const MachineInstr *MI,
|
|
|
|
const ValueSet *LVSetAft);
|
2001-09-08 14:22:50 +00:00
|
|
|
|
2001-10-23 21:38:42 +00:00
|
|
|
void move2DelayedInstr(const MachineInstr *OrigMI,
|
2003-08-05 22:03:27 +00:00
|
|
|
const MachineInstr *DelayedMI);
|
2001-10-23 21:38:42 +00:00
|
|
|
|
2001-10-19 21:42:06 +00:00
|
|
|
void markUnusableSugColors();
|
2001-10-28 18:15:12 +00:00
|
|
|
void allocateStackSpace4SpilledLRs();
|
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
void insertCode4SpilledLR(const LiveRange *LR,
|
|
|
|
MachineBasicBlock::iterator& MII,
|
|
|
|
MachineBasicBlock &MBB, unsigned OpNum);
|
2001-10-19 21:42:06 +00:00
|
|
|
|
2003-07-29 19:37:41 +00:00
|
|
|
// Method for inserting caller saving code. The caller must save all the
|
|
|
|
// volatile registers live across a call.
|
|
|
|
void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
|
|
|
|
std::vector<MachineInstr*>& instrnsAfter,
|
|
|
|
MachineInstr *CallMI,
|
|
|
|
const BasicBlock *BB);
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
inline void constructLiveRanges() { LRI.constructLiveRanges(); }
|
2001-09-08 14:22:50 +00:00
|
|
|
|
|
|
|
void colorIncomingArgs();
|
2001-09-30 23:19:57 +00:00
|
|
|
void colorCallRetArgs();
|
2001-09-08 14:22:50 +00:00
|
|
|
void updateMachineCode();
|
2003-07-29 19:37:41 +00:00
|
|
|
void updateInstruction(MachineBasicBlock::iterator& MII,
|
|
|
|
MachineBasicBlock &MBB);
|
2001-09-30 23:19:57 +00:00
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
void printLabel(const Value *Val);
|
2001-09-15 19:08:41 +00:00
|
|
|
void printMachineCode();
|
2001-10-28 18:15:12 +00:00
|
|
|
|
2001-11-03 20:41:22 +00:00
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
int getUsableUniRegAtMI(int RegType, const ValueSet *LVSetBef,
|
|
|
|
MachineInstr *MI,
|
2002-07-08 22:39:36 +00:00
|
|
|
std::vector<MachineInstr*>& MIBef,
|
|
|
|
std::vector<MachineInstr*>& MIAft);
|
|
|
|
|
2003-07-29 19:37:41 +00:00
|
|
|
// Callback method used to find unused registers.
|
|
|
|
// LVSetBef is the live variable set to search for an unused register.
|
2003-08-05 22:03:27 +00:00
|
|
|
// If it is not specified, the LV set before the current MI is used.
|
2003-07-29 19:37:41 +00:00
|
|
|
// This is sufficient as long as no new copy instructions are generated
|
|
|
|
// to copy the free register to memory.
|
|
|
|
//
|
2003-08-05 22:03:27 +00:00
|
|
|
int getUnusedUniRegAtMI(RegClass *RC, int RegType,
|
|
|
|
const MachineInstr *MI,
|
2003-07-29 19:37:41 +00:00
|
|
|
const ValueSet *LVSetBef = 0);
|
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
void setRelRegsUsedByThisInst(RegClass *RC, int RegType,
|
|
|
|
const MachineInstr *MI);
|
2001-11-03 20:41:22 +00:00
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
int getUniRegNotUsedByThisInst(RegClass *RC, int RegType,
|
|
|
|
const MachineInstr *MI);
|
2001-10-28 18:15:12 +00:00
|
|
|
|
2003-08-05 22:03:27 +00:00
|
|
|
void addInterf4PseudoInstr(const MachineInstr *MI);
|
2001-09-08 14:22:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|