2003-09-23 15:13:04 +00:00
|
|
|
//===-- PhyRegAlloc.h - Graph Coloring Register Allocator -------*- c++ -*-===//
|
2003-10-21 15:17:13 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-23 15:13:04 +00:00
|
|
|
//
|
|
|
|
// This is the main entry point for register allocation.
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
// * RegisterClasses: Each RegClass accepts a
|
|
|
|
// TargetRegClass which contains machine specific info about that register
|
|
|
|
// class. The code in the RegClass is machine independent and they use
|
|
|
|
// access functions in the TargetRegClass object passed into it to get
|
|
|
|
// machine specific info.
|
|
|
|
//
|
|
|
|
// * Machine dependent work: All parts of the register coloring algorithm
|
|
|
|
// except coloring of an individual node are machine independent.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-08 14:22:50 +00:00
|
|
|
|
2003-09-21 02:31:25 +00:00
|
|
|
#ifndef PHYREGALLOC_H
|
|
|
|
#define PHYREGALLOC_H
|
2001-09-08 14:22:50 +00:00
|
|
|
|
2003-09-01 20:12:17 +00:00
|
|
|
#include "LiveRangeInfo.h"
|
2003-09-21 01:23:46 +00:00
|
|
|
#include "llvm/Pass.h"
|
2003-07-29 19:37:41 +00:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2003-09-21 01:23:46 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-04-23 18:15:47 +00:00
|
|
|
#include "../SparcV9RegInfo.h"
|
2002-04-28 20:40:16 +00:00
|
|
|
#include <map>
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-10-28 00:28:31 +00:00
|
|
|
class MachineFunction;
|
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;
|
2003-09-24 18:08:54 +00:00
|
|
|
class Constant;
|
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
|
2003-09-21 01:23:46 +00:00
|
|
|
inline void clear () { InstrnsBefore.clear (); InstrnsAfter.clear (); }
|
2001-09-08 14:22:50 +00:00
|
|
|
};
|
|
|
|
|
2001-10-28 18:15:12 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// class PhyRegAlloc:
|
2003-09-21 01:23:46 +00:00
|
|
|
// Main class the register allocator. Call runOnFunction() to allocate
|
2002-04-08 22:03:57 +00:00
|
|
|
// registers for a Function.
|
2001-10-28 18:15:12 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2003-09-21 01:23:46 +00:00
|
|
|
class PhyRegAlloc : public FunctionPass {
|
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
|
2003-09-21 01:23:46 +00:00
|
|
|
MachineFunction *MF; // descriptor for method's native code
|
|
|
|
FunctionLiveVarInfo *LVI; // LV information for this method
|
2001-09-08 14:22:50 +00:00
|
|
|
// (already computed for BBs)
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRangeInfo *LRI; // LR info (will be computed)
|
2004-06-03 02:45:09 +00:00
|
|
|
const SparcV9RegInfo &MRI; // Machine Register information
|
2001-09-08 14:22:50 +00:00
|
|
|
const unsigned NumOfRegClasses; // recorded here for efficiency
|
|
|
|
|
2003-09-16 15:36:50 +00:00
|
|
|
// Map to indicate whether operands of each MachineInstr have been
|
|
|
|
// updated according to their assigned colors. This is only used in
|
|
|
|
// assertion checking (debug builds).
|
2003-05-31 07:41:54 +00:00
|
|
|
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
|
2003-09-21 01:23:46 +00:00
|
|
|
const 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:
|
2003-10-24 21:21:58 +00:00
|
|
|
typedef std::map<const Function *, std::vector<AllocInfo> > SavedStateMapTy;
|
|
|
|
|
2003-09-21 01:23:46 +00:00
|
|
|
inline PhyRegAlloc (const TargetMachine &TM_) :
|
2004-06-02 05:55:25 +00:00
|
|
|
TM (TM_), MRI (*TM.getRegInfo ()),
|
2003-09-21 01:23:46 +00:00
|
|
|
NumOfRegClasses (MRI.getNumOfRegClasses ()) { }
|
|
|
|
virtual ~PhyRegAlloc() { }
|
|
|
|
|
|
|
|
/// runOnFunction - Main method called for allocating registers.
|
|
|
|
///
|
|
|
|
virtual bool runOnFunction (Function &F);
|
|
|
|
|
2003-09-24 18:08:54 +00:00
|
|
|
virtual bool doFinalization (Module &M);
|
|
|
|
|
2003-09-23 15:13:04 +00:00
|
|
|
virtual void getAnalysisUsage (AnalysisUsage &AU) const;
|
2002-03-18 03:26:48 +00:00
|
|
|
|
2003-09-21 01:23:46 +00:00
|
|
|
const char *getPassName () const {
|
|
|
|
return "Traditional graph-coloring reg. allocator";
|
|
|
|
}
|
2003-09-21 02:24:09 +00:00
|
|
|
|
|
|
|
inline const RegClass* getRegClassByID(unsigned id) const {
|
2003-07-25 21:00:13 +00:00
|
|
|
return RegClassList[id];
|
2002-03-18 03:26:48 +00:00
|
|
|
}
|
2003-09-21 02:24:09 +00:00
|
|
|
inline RegClass *getRegClassByID(unsigned id) { return RegClassList[id]; }
|
2003-07-29 19:37:41 +00:00
|
|
|
|
2002-02-04 17:38:48 +00:00
|
|
|
private:
|
2003-10-24 21:21:58 +00:00
|
|
|
SavedStateMapTy FnAllocState;
|
|
|
|
|
2002-02-05 02:51:01 +00:00
|
|
|
void addInterference(const Value *Def, const ValueSet *LVSet,
|
|
|
|
bool isCallInst);
|
2003-09-21 01:23:46 +00:00
|
|
|
bool markAllocatedRegs(MachineInstr* MInst);
|
2001-09-08 14:22:50 +00:00
|
|
|
|
|
|
|
void addInterferencesForArgs();
|
|
|
|
void createIGNodeListsAndIGs();
|
|
|
|
void buildInterferenceGraphs();
|
2003-11-10 00:05:26 +00:00
|
|
|
|
|
|
|
void saveStateForValue (std::vector<AllocInfo> &state,
|
2004-03-08 23:22:03 +00:00
|
|
|
const Value *V, int Insn, int Opnd);
|
2003-09-24 18:08:54 +00:00
|
|
|
void saveState();
|
2004-05-30 04:22:24 +00:00
|
|
|
void dumpSavedState();
|
2004-03-11 06:45:52 +00:00
|
|
|
void finishSavingState(Module &M);
|
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-10-23 18:06:27 +00:00
|
|
|
/// Method for inserting caller saving code. The caller must save all the
|
|
|
|
/// volatile registers live across a call.
|
|
|
|
///
|
2003-07-29 19:37:41 +00:00
|
|
|
void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
|
|
|
|
std::vector<MachineInstr*>& instrnsAfter,
|
|
|
|
MachineInstr *CallMI,
|
|
|
|
const BasicBlock *BB);
|
|
|
|
|
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
|
|
|
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-10-23 18:06:27 +00:00
|
|
|
/// Callback method used to find unused registers.
|
|
|
|
/// LVSetBef is the live variable set to search for an unused register.
|
|
|
|
/// If it is not specified, the LV set before the current MI is used.
|
|
|
|
/// 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
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-09-08 14:22:50 +00:00
|
|
|
#endif
|