2002-08-09 20:08:03 +00:00
|
|
|
//===-- PhyRegAlloc.cpp ---------------------------------------------------===//
|
2001-11-08 04:48:50 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +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-24 18:16:23 +00:00
|
|
|
// Traditional graph-coloring global register allocator currently used
|
|
|
|
// by the SPARC back-end.
|
|
|
|
//
|
|
|
|
// NOTE: This register allocator has some special support
|
|
|
|
// for the Reoptimizer, such as not saving some registers on calls to
|
|
|
|
// the first-level instrumentation function.
|
|
|
|
//
|
|
|
|
// NOTE 2: This register allocator can save its state in a global
|
|
|
|
// variable in the module it's working on. This feature is not
|
|
|
|
// thread-safe; if you have doubts, leave it turned off.
|
2002-08-09 20:08:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-08 04:48:50 +00:00
|
|
|
|
2003-10-23 20:32:55 +00:00
|
|
|
#include "AllocInfo.h"
|
2003-10-23 18:06:27 +00:00
|
|
|
#include "IGNode.h"
|
2003-09-01 20:09:04 +00:00
|
|
|
#include "PhyRegAlloc.h"
|
2003-01-15 19:57:07 +00:00
|
|
|
#include "RegAllocCommon.h"
|
2003-01-15 21:14:01 +00:00
|
|
|
#include "RegClass.h"
|
2004-02-24 19:46:00 +00:00
|
|
|
#include "../LiveVar/FunctionLiveVarInfo.h"
|
2003-10-23 18:06:27 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2003-09-30 20:13:59 +00:00
|
|
|
#include "llvm/CodeGen/InstrSelection.h"
|
2003-10-30 21:21:33 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeForInstruction.h"
|
2003-10-23 18:06:27 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionInfo.h"
|
2003-09-21 02:50:21 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2003-01-15 18:08:07 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2004-02-29 19:12:51 +00:00
|
|
|
#include "../MachineInstrAnnot.h"
|
2003-09-30 20:13:59 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Support/InstIterator.h"
|
2003-10-23 18:06:27 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2002-09-15 07:07:55 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2003-10-23 18:06:27 +00:00
|
|
|
#include "Support/SetOperations.h"
|
|
|
|
#include "Support/STLExtras.h"
|
2003-09-21 03:57:37 +00:00
|
|
|
#include <cmath>
|
2001-11-08 04:48:50 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-05-22 17:08:27 +00:00
|
|
|
RegAllocDebugLevel_t DEBUG_RA;
|
2002-09-14 23:05:33 +00:00
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
static cl::opt<RegAllocDebugLevel_t, true>
|
|
|
|
DRA_opt("dregalloc", cl::Hidden, cl::location(DEBUG_RA),
|
|
|
|
cl::desc("enable register allocation debugging information"),
|
|
|
|
cl::values(
|
2002-09-14 23:05:33 +00:00
|
|
|
clEnumValN(RA_DEBUG_None , "n", "disable debug output"),
|
|
|
|
clEnumValN(RA_DEBUG_Results, "y", "debug output for allocation results"),
|
|
|
|
clEnumValN(RA_DEBUG_Coloring, "c", "debug output for graph coloring step"),
|
|
|
|
clEnumValN(RA_DEBUG_Interference,"ig","debug output for interference graphs"),
|
|
|
|
clEnumValN(RA_DEBUG_LiveRanges , "lr","debug output for live ranges"),
|
|
|
|
clEnumValN(RA_DEBUG_Verbose, "v", "extra debug output"),
|
2002-07-22 02:10:13 +00:00
|
|
|
0));
|
2001-09-15 21:11:11 +00:00
|
|
|
|
2004-03-10 22:21:03 +00:00
|
|
|
/// The reoptimizer wants to be able to grovel through the register
|
|
|
|
/// allocator's state after it has done its job. This is a hack.
|
|
|
|
///
|
|
|
|
PhyRegAlloc::SavedStateMapTy ExportedFnAllocState;
|
|
|
|
bool SaveRegAllocState = false;
|
|
|
|
bool SaveStateToModule = true;
|
|
|
|
static cl::opt<bool, true>
|
|
|
|
SaveRegAllocStateOpt("save-ra-state", cl::Hidden,
|
|
|
|
cl::location (SaveRegAllocState),
|
|
|
|
cl::init(false),
|
2003-09-24 17:50:28 +00:00
|
|
|
cl::desc("write reg. allocator state into module"));
|
|
|
|
|
2003-08-14 06:09:32 +00:00
|
|
|
FunctionPass *getRegisterAllocator(TargetMachine &T) {
|
2003-09-21 01:23:46 +00:00
|
|
|
return new PhyRegAlloc (T);
|
2002-02-04 15:54:09 +00:00
|
|
|
}
|
2002-02-04 00:33:08 +00:00
|
|
|
|
2003-09-23 15:13:04 +00:00
|
|
|
void PhyRegAlloc::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<LoopInfo> ();
|
|
|
|
AU.addRequired<FunctionLiveVarInfo> ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Initialize interference graphs (one in each reg class) and IGNodeLists
|
|
|
|
/// (one in each IG). The actual nodes will be pushed later.
|
|
|
|
///
|
2002-02-03 07:46:34 +00:00
|
|
|
void PhyRegAlloc::createIGNodeListsAndIGs() {
|
2003-09-01 20:05:47 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "Creating LR lists ...\n";
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
|
|
|
|
LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
|
2002-02-03 07:46:34 +00:00
|
|
|
|
|
|
|
for (; HMI != HMIEnd ; ++HMI ) {
|
|
|
|
if (HMI->first) {
|
|
|
|
LiveRange *L = HMI->second; // get the LiveRange
|
|
|
|
if (!L) {
|
2004-03-29 21:58:41 +00:00
|
|
|
if (DEBUG_RA && !isa<ConstantIntegral> (HMI->first))
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\n**** ?!?WARNING: NULL LIVE RANGE FOUND FOR: "
|
2002-09-14 23:05:33 +00:00
|
|
|
<< RAV(HMI->first) << "****\n";
|
2002-02-03 07:46:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-09-14 23:05:33 +00:00
|
|
|
|
|
|
|
// if the Value * is not null, and LR is not yet written to the IGNodeList
|
2002-06-25 16:13:24 +00:00
|
|
|
if (!(L->getUserIGNode()) ) {
|
2002-02-03 07:46:34 +00:00
|
|
|
RegClass *const RC = // RegClass of first value in the LR
|
2003-09-24 17:50:28 +00:00
|
|
|
RegClassList[ L->getRegClassID() ];
|
2002-02-03 07:46:34 +00:00
|
|
|
RC->addLRToIG(L); // add this LR to an IG
|
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-03 07:46:34 +00:00
|
|
|
|
|
|
|
// init RegClassList
|
2002-06-25 16:13:24 +00:00
|
|
|
for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
|
2002-02-03 07:46:34 +00:00
|
|
|
RegClassList[rc]->createInterferenceGraph();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2003-09-01 20:05:47 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges) std::cerr << "LRLists Created!\n";
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Add all interferences for a given instruction. Interference occurs only
|
|
|
|
/// if the LR of Def (Inst or Arg) is of the same reg class as that of live
|
|
|
|
/// var. The live var passed to this function is the LVset AFTER the
|
|
|
|
/// instruction.
|
|
|
|
///
|
|
|
|
void PhyRegAlloc::addInterference(const Value *Def, const ValueSet *LVSet,
|
2002-02-05 02:52:05 +00:00
|
|
|
bool isCallInst) {
|
|
|
|
ValueSet::const_iterator LIt = LVSet->begin();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// get the live range of instruction
|
2003-09-21 01:23:46 +00:00
|
|
|
const LiveRange *const LROfDef = LRI->getLiveRangeForValue( Def );
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
IGNode *const IGNodeOfDef = LROfDef->getUserIGNode();
|
|
|
|
assert( IGNodeOfDef );
|
|
|
|
|
|
|
|
RegClass *const RCOfDef = LROfDef->getRegClass();
|
|
|
|
|
|
|
|
// for each live var in live variable set
|
2002-06-25 16:13:24 +00:00
|
|
|
for ( ; LIt != LVSet->end(); ++LIt) {
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_Verbose)
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "< Def=" << RAV(Def) << ", Lvar=" << RAV(*LIt) << "> ";
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// get the live range corresponding to live var
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRange *LROfVar = LRI->getLiveRangeForValue(*LIt);
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// LROfVar can be null if it is a const since a const
|
|
|
|
// doesn't have a dominating def - see Assumptions above
|
2002-09-14 23:05:33 +00:00
|
|
|
if (LROfVar)
|
|
|
|
if (LROfDef != LROfVar) // do not set interf for same LR
|
|
|
|
if (RCOfDef == LROfVar->getRegClass()) // 2 reg classes are the same
|
|
|
|
RCOfDef->setInterference( LROfDef, LROfVar);
|
2001-10-19 17:21:03 +00:00
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 17:21:03 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// For a call instruction, this method sets the CallInterference flag in
|
|
|
|
/// the LR of each variable live in the Live Variable Set live after the
|
|
|
|
/// call instruction (except the return value of the call instruction - since
|
|
|
|
/// the return value does not interfere with that call itself).
|
|
|
|
///
|
2001-10-19 17:21:03 +00:00
|
|
|
void PhyRegAlloc::setCallInterferences(const MachineInstr *MInst,
|
2002-02-05 02:52:05 +00:00
|
|
|
const ValueSet *LVSetAft) {
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_Interference)
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\n For call inst: " << *MInst;
|
2001-10-19 17:21:03 +00:00
|
|
|
|
|
|
|
// for each live var in live variable set after machine inst
|
2003-07-02 01:24:00 +00:00
|
|
|
for (ValueSet::const_iterator LIt = LVSetAft->begin(), LEnd = LVSetAft->end();
|
|
|
|
LIt != LEnd; ++LIt) {
|
2001-10-19 17:21:03 +00:00
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// get the live range corresponding to live var
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRange *const LR = LRI->getLiveRangeForValue(*LIt );
|
2001-10-19 17:21:03 +00:00
|
|
|
|
|
|
|
// LR can be null if it is a const since a const
|
|
|
|
// doesn't have a dominating def - see Assumptions above
|
2002-09-14 23:05:33 +00:00
|
|
|
if (LR ) {
|
|
|
|
if (DEBUG_RA >= RA_DEBUG_Interference) {
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\n\tLR after Call: ";
|
2002-09-14 23:05:33 +00:00
|
|
|
printSet(*LR);
|
|
|
|
}
|
2001-10-19 17:21:03 +00:00
|
|
|
LR->setCallInterference();
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_Interference) {
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\n ++After adding call interference for LR: " ;
|
2002-02-05 02:52:05 +00:00
|
|
|
printSet(*LR);
|
2001-10-19 17:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-03-31 18:54:37 +00:00
|
|
|
// Now find the LR of the return value of the call
|
|
|
|
// We do this because, we look at the LV set *after* the instruction
|
|
|
|
// to determine, which LRs must be saved across calls. The return value
|
|
|
|
// of the call is live in this set - but it does not interfere with call
|
|
|
|
// (i.e., we can allocate a volatile register to the return value)
|
2002-05-19 15:29:31 +00:00
|
|
|
CallArgsDescriptor* argDesc = CallArgsDescriptor::get(MInst);
|
|
|
|
|
|
|
|
if (const Value *RetVal = argDesc->getReturnValue()) {
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRange *RetValLR = LRI->getLiveRangeForValue( RetVal );
|
2002-03-31 18:54:37 +00:00
|
|
|
assert( RetValLR && "No LR for RetValue of call");
|
|
|
|
RetValLR->clearCallInterference();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the CALL is an indirect call, find the LR of the function pointer.
|
|
|
|
// That has a call interference because it conflicts with outgoing args.
|
2002-06-25 16:13:24 +00:00
|
|
|
if (const Value *AddrVal = argDesc->getIndirectFuncPtr()) {
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRange *AddrValLR = LRI->getLiveRangeForValue( AddrVal );
|
2002-03-31 18:54:37 +00:00
|
|
|
assert( AddrValLR && "No LR for indirect addr val of call");
|
|
|
|
AddrValLR->setCallInterference();
|
|
|
|
}
|
2001-10-19 17:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Create interferences in the IG of each RegClass, and calculate the spill
|
|
|
|
/// cost of each Live Range (it is done in this method to save another pass
|
|
|
|
/// over the code).
|
|
|
|
///
|
|
|
|
void PhyRegAlloc::buildInterferenceGraphs() {
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_Interference)
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "Creating interference graphs ...\n";
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
unsigned BBLoopDepthCost;
|
2003-09-21 01:23:46 +00:00
|
|
|
for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
|
2002-04-07 20:49:59 +00:00
|
|
|
BBI != BBE; ++BBI) {
|
2002-10-28 19:22:04 +00:00
|
|
|
const MachineBasicBlock &MBB = *BBI;
|
|
|
|
const BasicBlock *BB = MBB.getBasicBlock();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// find the 10^(loop_depth) of this BB
|
2002-10-28 19:22:04 +00:00
|
|
|
BBLoopDepthCost = (unsigned)pow(10.0, LoopDepthCalc->getLoopDepth(BB));
|
2002-01-07 19:19:18 +00:00
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
// get the iterator for machine instructions
|
2002-10-28 19:22:04 +00:00
|
|
|
MachineBasicBlock::const_iterator MII = MBB.begin();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// iterate over all the machine instructions in BB
|
2002-10-28 19:22:04 +00:00
|
|
|
for ( ; MII != MBB.end(); ++MII) {
|
2004-02-12 02:27:10 +00:00
|
|
|
const MachineInstr *MInst = MII;
|
2001-09-14 21:18:34 +00:00
|
|
|
|
|
|
|
// get the LV set after the instruction
|
2002-10-28 19:22:04 +00:00
|
|
|
const ValueSet &LVSetAI = LVI->getLiveVarSetAfterMInst(MInst, BB);
|
2004-02-11 20:47:34 +00:00
|
|
|
bool isCallInst = TM.getInstrInfo().isCall(MInst->getOpcode());
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
if (isCallInst) {
|
2003-09-11 22:34:13 +00:00
|
|
|
// set the isCallInterference flag of each live range which extends
|
|
|
|
// across this call instruction. This information is used by graph
|
|
|
|
// coloring algorithm to avoid allocating volatile colors to live ranges
|
2001-10-19 17:21:03 +00:00
|
|
|
// that span across calls (since they have to be saved/restored)
|
2002-02-05 04:20:12 +00:00
|
|
|
setCallInterferences(MInst, &LVSetAI);
|
2001-10-19 17:21:03 +00:00
|
|
|
}
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// iterate over all MI operands to find defs
|
2002-02-05 06:02:59 +00:00
|
|
|
for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
|
|
|
|
OpE = MInst->end(); OpI != OpE; ++OpI) {
|
2003-12-14 13:24:17 +00:00
|
|
|
if (OpI.isDef()) // create a new LR since def
|
2002-02-05 04:20:12 +00:00
|
|
|
addInterference(*OpI, &LVSetAI, isCallInst);
|
2002-01-07 19:19:18 +00:00
|
|
|
|
|
|
|
// Calculate the spill cost of each live range
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRange *LR = LRI->getLiveRangeForValue(*OpI);
|
2002-02-05 06:02:59 +00:00
|
|
|
if (LR) LR->addSpillCost(BBLoopDepthCost);
|
2002-01-07 19:19:18 +00:00
|
|
|
}
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
// Mark all operands of pseudo-instructions as interfering with one
|
|
|
|
// another. This must be done because pseudo-instructions may be
|
|
|
|
// expanded to multiple instructions by the assembler, so all the
|
|
|
|
// operands must get distinct registers.
|
2004-02-11 20:47:34 +00:00
|
|
|
if (TM.getInstrInfo().isPseudoInstr(MInst->getOpcode()))
|
2001-11-14 15:33:58 +00:00
|
|
|
addInterf4PseudoInstr(MInst);
|
|
|
|
|
2001-10-16 01:23:19 +00:00
|
|
|
// Also add interference for any implicit definitions in a machine
|
|
|
|
// instr (currently, only calls have this).
|
|
|
|
unsigned NumOfImpRefs = MInst->getNumImplicitRefs();
|
2003-05-27 00:05:23 +00:00
|
|
|
for (unsigned z=0; z < NumOfImpRefs; z++)
|
2003-12-14 13:24:17 +00:00
|
|
|
if (MInst->getImplicitOp(z).isDef())
|
2003-05-27 00:05:23 +00:00
|
|
|
addInterference( MInst->getImplicitRef(z), &LVSetAI, isCallInst );
|
2001-11-03 17:13:27 +00:00
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
} // for all machine instructions in BB
|
2002-04-07 20:49:59 +00:00
|
|
|
} // for all BBs in function
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2003-09-11 22:34:13 +00:00
|
|
|
// add interferences for function arguments. Since there are no explicit
|
2002-04-07 20:49:59 +00:00
|
|
|
// defs in the function for args, we have to add them manually
|
2002-01-07 19:19:18 +00:00
|
|
|
addInterferencesForArgs();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_Interference)
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "Interference graphs calculated!\n";
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Mark all operands of the given MachineInstr as interfering with one
|
|
|
|
/// another.
|
|
|
|
///
|
2003-09-21 02:24:09 +00:00
|
|
|
void PhyRegAlloc::addInterf4PseudoInstr(const MachineInstr *MInst) {
|
2001-11-15 15:00:53 +00:00
|
|
|
bool setInterf = false;
|
|
|
|
|
2003-09-21 02:24:09 +00:00
|
|
|
// iterate over MI operands to find defs
|
2002-02-05 06:02:59 +00:00
|
|
|
for (MachineInstr::const_val_op_iterator It1 = MInst->begin(),
|
|
|
|
ItE = MInst->end(); It1 != ItE; ++It1) {
|
2003-09-21 01:23:46 +00:00
|
|
|
const LiveRange *LROfOp1 = LRI->getLiveRangeForValue(*It1);
|
2003-12-14 13:24:17 +00:00
|
|
|
assert((LROfOp1 || It1.isDef()) && "No LR for Def in PSEUDO insruction");
|
2002-02-05 06:02:59 +00:00
|
|
|
|
|
|
|
MachineInstr::const_val_op_iterator It2 = It1;
|
2002-06-25 16:13:24 +00:00
|
|
|
for (++It2; It2 != ItE; ++It2) {
|
2003-09-21 01:23:46 +00:00
|
|
|
const LiveRange *LROfOp2 = LRI->getLiveRangeForValue(*It2);
|
2002-02-05 06:02:59 +00:00
|
|
|
|
|
|
|
if (LROfOp2) {
|
|
|
|
RegClass *RCOfOp1 = LROfOp1->getRegClass();
|
|
|
|
RegClass *RCOfOp2 = LROfOp2->getRegClass();
|
2001-11-14 15:33:58 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
if (RCOfOp1 == RCOfOp2 ){
|
2001-11-14 15:33:58 +00:00
|
|
|
RCOfOp1->setInterference( LROfOp1, LROfOp2 );
|
2001-11-15 15:00:53 +00:00
|
|
|
setInterf = true;
|
2001-11-14 15:33:58 +00:00
|
|
|
}
|
|
|
|
} // if Op2 has a LR
|
|
|
|
} // for all other defs in machine instr
|
|
|
|
} // for all operands in an instruction
|
|
|
|
|
2002-02-05 06:02:59 +00:00
|
|
|
if (!setInterf && MInst->getNumOperands() > 2) {
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\nInterf not set for any operand in pseudo instr:\n";
|
|
|
|
std::cerr << *MInst;
|
2001-11-15 15:00:53 +00:00
|
|
|
assert(0 && "Interf not set for pseudo instr with > 2 operands" );
|
|
|
|
}
|
2001-11-14 15:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Add interferences for incoming arguments to a function.
|
|
|
|
///
|
2002-02-05 02:52:05 +00:00
|
|
|
void PhyRegAlloc::addInterferencesForArgs() {
|
|
|
|
// get the InSet of root BB
|
2002-10-28 19:22:04 +00:00
|
|
|
const ValueSet &InSet = LVI->getInSetOfBB(&Fn->front());
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2002-10-28 19:22:04 +00:00
|
|
|
for (Function::const_aiterator AI = Fn->abegin(); AI != Fn->aend(); ++AI) {
|
2002-06-25 16:13:24 +00:00
|
|
|
// add interferences between args and LVars at start
|
|
|
|
addInterference(AI, &InSet, false);
|
|
|
|
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_Interference)
|
2003-10-22 20:22:53 +00:00
|
|
|
std::cerr << " - %% adding interference for argument " << RAV(AI) << "\n";
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// The following are utility functions used solely by updateMachineCode and
|
|
|
|
/// the functions that it calls. They should probably be folded back into
|
|
|
|
/// updateMachineCode at some point.
|
|
|
|
///
|
|
|
|
|
|
|
|
// used by: updateMachineCode (1 time), PrependInstructions (1 time)
|
|
|
|
inline void InsertBefore(MachineInstr* newMI, MachineBasicBlock& MBB,
|
|
|
|
MachineBasicBlock::iterator& MII) {
|
2002-10-28 19:22:04 +00:00
|
|
|
MII = MBB.insert(MII, newMI);
|
2002-10-11 16:12:40 +00:00
|
|
|
++MII;
|
|
|
|
}
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
// used by: AppendInstructions (1 time)
|
|
|
|
inline void InsertAfter(MachineInstr* newMI, MachineBasicBlock& MBB,
|
|
|
|
MachineBasicBlock::iterator& MII) {
|
2002-10-11 16:12:40 +00:00
|
|
|
++MII; // insert before the next instruction
|
2002-10-28 19:22:04 +00:00
|
|
|
MII = MBB.insert(MII, newMI);
|
2002-10-11 16:12:40 +00:00
|
|
|
}
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
// used by: updateMachineCode (2 times)
|
|
|
|
inline void PrependInstructions(std::vector<MachineInstr *> &IBef,
|
|
|
|
MachineBasicBlock& MBB,
|
|
|
|
MachineBasicBlock::iterator& MII,
|
|
|
|
const std::string& msg) {
|
|
|
|
if (!IBef.empty()) {
|
2004-02-12 02:27:10 +00:00
|
|
|
MachineInstr* OrigMI = MII;
|
2002-05-19 15:29:31 +00:00
|
|
|
std::vector<MachineInstr *>::iterator AdIt;
|
2003-10-22 20:22:53 +00:00
|
|
|
for (AdIt = IBef.begin(); AdIt != IBef.end() ; ++AdIt) {
|
2002-04-25 04:34:15 +00:00
|
|
|
if (DEBUG_RA) {
|
2003-09-01 20:05:47 +00:00
|
|
|
if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
|
|
|
|
std::cerr << msg << "PREPENDed instr:\n " << **AdIt << "\n";
|
2002-04-25 04:34:15 +00:00
|
|
|
}
|
2002-10-28 19:22:04 +00:00
|
|
|
InsertBefore(*AdIt, MBB, MII);
|
2002-04-25 04:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
// used by: updateMachineCode (1 time)
|
|
|
|
inline void AppendInstructions(std::vector<MachineInstr *> &IAft,
|
|
|
|
MachineBasicBlock& MBB,
|
|
|
|
MachineBasicBlock::iterator& MII,
|
|
|
|
const std::string& msg) {
|
|
|
|
if (!IAft.empty()) {
|
2004-02-12 02:27:10 +00:00
|
|
|
MachineInstr* OrigMI = MII;
|
2002-05-19 15:29:31 +00:00
|
|
|
std::vector<MachineInstr *>::iterator AdIt;
|
2003-10-22 20:22:53 +00:00
|
|
|
for ( AdIt = IAft.begin(); AdIt != IAft.end() ; ++AdIt ) {
|
2002-06-25 16:13:24 +00:00
|
|
|
if (DEBUG_RA) {
|
2003-09-01 20:05:47 +00:00
|
|
|
if (OrigMI) std::cerr << "For MInst:\n " << *OrigMI;
|
|
|
|
std::cerr << msg << "APPENDed instr:\n " << **AdIt << "\n";
|
2002-04-25 04:34:15 +00:00
|
|
|
}
|
2002-10-28 19:22:04 +00:00
|
|
|
InsertAfter(*AdIt, MBB, MII);
|
2002-04-25 04:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Set the registers for operands in the given MachineInstr, if a register was
|
|
|
|
/// successfully allocated. Return true if any of its operands has been marked
|
|
|
|
/// for spill.
|
|
|
|
///
|
2003-09-21 01:23:46 +00:00
|
|
|
bool PhyRegAlloc::markAllocatedRegs(MachineInstr* MInst)
|
2003-05-31 07:32:01 +00:00
|
|
|
{
|
2003-07-29 19:49:21 +00:00
|
|
|
bool instrNeedsSpills = false;
|
|
|
|
|
2003-05-31 07:32:01 +00:00
|
|
|
// First, set the registers for operands in the machine instruction
|
|
|
|
// if a register was successfully allocated. Do this first because we
|
|
|
|
// will need to know which registers are already used by this instr'n.
|
2003-10-22 20:22:53 +00:00
|
|
|
for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
|
2003-05-31 07:32:01 +00:00
|
|
|
MachineOperand& Op = MInst->getOperand(OpNum);
|
|
|
|
if (Op.getType() == MachineOperand::MO_VirtualRegister ||
|
2003-10-22 20:22:53 +00:00
|
|
|
Op.getType() == MachineOperand::MO_CCRegister) {
|
2003-05-31 07:32:01 +00:00
|
|
|
const Value *const Val = Op.getVRegValue();
|
2003-09-21 01:23:46 +00:00
|
|
|
if (const LiveRange* LR = LRI->getLiveRangeForValue(Val)) {
|
2003-07-29 19:49:21 +00:00
|
|
|
// Remember if any operand needs spilling
|
|
|
|
instrNeedsSpills |= LR->isMarkedForSpill();
|
|
|
|
|
|
|
|
// An operand may have a color whether or not it needs spilling
|
2003-05-31 07:32:01 +00:00
|
|
|
if (LR->hasColor())
|
|
|
|
MInst->SetRegForOperand(OpNum,
|
2003-09-24 17:50:28 +00:00
|
|
|
MRI.getUnifiedRegNum(LR->getRegClassID(),
|
2003-05-31 07:32:01 +00:00
|
|
|
LR->getColor()));
|
2003-07-29 19:49:21 +00:00
|
|
|
}
|
2003-05-31 07:32:01 +00:00
|
|
|
}
|
|
|
|
} // for each operand
|
2003-07-29 19:49:21 +00:00
|
|
|
|
|
|
|
return instrNeedsSpills;
|
|
|
|
}
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Mark allocated registers (using markAllocatedRegs()) on the instruction
|
|
|
|
/// that MII points to. Then, if it's a call instruction, insert caller-saving
|
|
|
|
/// code before and after it. Finally, insert spill code before and after it,
|
|
|
|
/// using insertCode4SpilledLR().
|
|
|
|
///
|
2003-07-29 19:49:21 +00:00
|
|
|
void PhyRegAlloc::updateInstruction(MachineBasicBlock::iterator& MII,
|
2003-10-22 20:22:53 +00:00
|
|
|
MachineBasicBlock &MBB) {
|
2004-02-12 02:27:10 +00:00
|
|
|
MachineInstr* MInst = MII;
|
2004-02-11 20:47:34 +00:00
|
|
|
unsigned Opcode = MInst->getOpcode();
|
2003-07-29 19:49:21 +00:00
|
|
|
|
|
|
|
// Reset tmp stack positions so they can be reused for each machine instr.
|
2003-09-21 01:23:46 +00:00
|
|
|
MF->getInfo()->popAllTempValues();
|
2003-07-29 19:49:21 +00:00
|
|
|
|
|
|
|
// Mark the operands for which regs have been allocated.
|
2004-02-12 02:27:10 +00:00
|
|
|
bool instrNeedsSpills = markAllocatedRegs(MII);
|
2003-07-29 19:49:21 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Mark that the operands have been updated. Later,
|
|
|
|
// setRelRegsUsedByThisInst() is called to find registers used by each
|
|
|
|
// MachineInst, and it should not be used for an instruction until
|
|
|
|
// this is done. This flag just serves as a sanity check.
|
2003-05-31 07:32:01 +00:00
|
|
|
OperandsColoredMap[MInst] = true;
|
2003-07-29 19:49:21 +00:00
|
|
|
#endif
|
2003-05-31 07:32:01 +00:00
|
|
|
|
2003-07-25 21:06:09 +00:00
|
|
|
// Now insert caller-saving code before/after the call.
|
|
|
|
// Do this before inserting spill code since some registers must be
|
|
|
|
// used by save/restore and spill code should not use those registers.
|
|
|
|
if (TM.getInstrInfo().isCall(Opcode)) {
|
2003-05-31 07:32:01 +00:00
|
|
|
AddedInstrns &AI = AddedInstrMap[MInst];
|
2003-07-29 19:49:21 +00:00
|
|
|
insertCallerSavingCode(AI.InstrnsBefore, AI.InstrnsAfter, MInst,
|
|
|
|
MBB.getBasicBlock());
|
2003-05-31 07:32:01 +00:00
|
|
|
}
|
2003-07-25 21:06:09 +00:00
|
|
|
|
2003-05-31 07:32:01 +00:00
|
|
|
// Now insert spill code for remaining operands not allocated to
|
|
|
|
// registers. This must be done even for call return instructions
|
|
|
|
// since those are not handled by the special code above.
|
2003-07-29 19:49:21 +00:00
|
|
|
if (instrNeedsSpills)
|
2003-10-22 20:22:53 +00:00
|
|
|
for (unsigned OpNum=0; OpNum < MInst->getNumOperands(); ++OpNum) {
|
2003-07-29 19:49:21 +00:00
|
|
|
MachineOperand& Op = MInst->getOperand(OpNum);
|
|
|
|
if (Op.getType() == MachineOperand::MO_VirtualRegister ||
|
2003-10-22 20:22:53 +00:00
|
|
|
Op.getType() == MachineOperand::MO_CCRegister) {
|
2003-07-29 19:49:21 +00:00
|
|
|
const Value* Val = Op.getVRegValue();
|
2003-09-21 01:23:46 +00:00
|
|
|
if (const LiveRange *LR = LRI->getLiveRangeForValue(Val))
|
2003-07-29 19:49:21 +00:00
|
|
|
if (LR->isMarkedForSpill())
|
|
|
|
insertCode4SpilledLR(LR, MII, MBB, OpNum);
|
|
|
|
}
|
|
|
|
} // for each operand
|
2003-05-31 07:32:01 +00:00
|
|
|
}
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Iterate over all the MachineBasicBlocks in the current function and set
|
|
|
|
/// the allocated registers for each instruction (using updateInstruction()),
|
|
|
|
/// after register allocation is complete. Then move code out of delay slots.
|
|
|
|
///
|
2003-05-31 07:32:01 +00:00
|
|
|
void PhyRegAlloc::updateMachineCode()
|
|
|
|
{
|
2002-06-25 16:13:24 +00:00
|
|
|
// Insert any instructions needed at method entry
|
2003-09-21 01:23:46 +00:00
|
|
|
MachineBasicBlock::iterator MII = MF->front().begin();
|
|
|
|
PrependInstructions(AddedInstrAtEntry.InstrnsBefore, MF->front(), MII,
|
2002-06-25 16:13:24 +00:00
|
|
|
"At function entry: \n");
|
|
|
|
assert(AddedInstrAtEntry.InstrnsAfter.empty() &&
|
|
|
|
"InstrsAfter should be unnecessary since we are just inserting at "
|
|
|
|
"the function entry point here.");
|
2002-04-25 04:34:15 +00:00
|
|
|
|
2003-09-21 01:23:46 +00:00
|
|
|
for (MachineFunction::iterator BBI = MF->begin(), BBE = MF->end();
|
2002-04-07 20:49:59 +00:00
|
|
|
BBI != BBE; ++BBI) {
|
2002-10-28 19:22:04 +00:00
|
|
|
MachineBasicBlock &MBB = *BBI;
|
2003-05-31 07:32:01 +00:00
|
|
|
|
|
|
|
// Iterate over all machine instructions in BB and mark operands with
|
|
|
|
// their assigned registers or insert spill code, as appropriate.
|
|
|
|
// Also, fix operands of call/return instructions.
|
|
|
|
for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
|
2004-02-12 02:27:10 +00:00
|
|
|
if (! TM.getInstrInfo().isDummyPhiInstr(MII->getOpcode()))
|
2003-07-29 19:49:21 +00:00
|
|
|
updateInstruction(MII, MBB);
|
2003-05-31 07:32:01 +00:00
|
|
|
|
|
|
|
// Now, move code out of delay slots of branches and returns if needed.
|
|
|
|
// (Also, move "after" code from calls to the last delay slot instruction.)
|
|
|
|
// Moving code out of delay slots is needed in 2 situations:
|
|
|
|
// (1) If this is a branch and it needs instructions inserted after it,
|
|
|
|
// move any existing instructions out of the delay slot so that the
|
|
|
|
// instructions can go into the delay slot. This only supports the
|
|
|
|
// case that #instrsAfter <= #delay slots.
|
|
|
|
//
|
|
|
|
// (2) If any instruction in the delay slot needs
|
|
|
|
// instructions inserted, move it out of the delay slot and before the
|
|
|
|
// branch because putting code before or after it would be VERY BAD!
|
|
|
|
//
|
|
|
|
// If the annul bit of the branch is set, neither of these is legal!
|
|
|
|
// If so, we need to handle spill differently but annulling is not yet used.
|
2004-02-12 02:27:10 +00:00
|
|
|
for (MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII)
|
2003-05-31 07:32:01 +00:00
|
|
|
if (unsigned delaySlots =
|
2004-02-12 02:27:10 +00:00
|
|
|
TM.getInstrInfo().getNumDelaySlots(MII->getOpcode())) {
|
2004-02-14 01:18:34 +00:00
|
|
|
MachineBasicBlock::iterator DelaySlotMI = next(MII);
|
2004-02-12 02:27:10 +00:00
|
|
|
assert(DelaySlotMI != MBB.end() && "no instruction for delay slot");
|
2003-07-29 19:49:21 +00:00
|
|
|
|
2003-05-31 07:32:01 +00:00
|
|
|
// Check the 2 conditions above:
|
|
|
|
// (1) Does a branch need instructions added after it?
|
|
|
|
// (2) O/w does delay slot instr. need instrns before or after?
|
2004-02-12 02:27:10 +00:00
|
|
|
bool isBranch = (TM.getInstrInfo().isBranch(MII->getOpcode()) ||
|
|
|
|
TM.getInstrInfo().isReturn(MII->getOpcode()));
|
2003-07-29 19:49:21 +00:00
|
|
|
bool cond1 = (isBranch &&
|
2004-02-12 02:27:10 +00:00
|
|
|
AddedInstrMap.count(MII) &&
|
|
|
|
AddedInstrMap[MII].InstrnsAfter.size() > 0);
|
2003-07-29 19:49:21 +00:00
|
|
|
bool cond2 = (AddedInstrMap.count(DelaySlotMI) &&
|
|
|
|
(AddedInstrMap[DelaySlotMI].InstrnsBefore.size() > 0 ||
|
|
|
|
AddedInstrMap[DelaySlotMI].InstrnsAfter.size() > 0));
|
2003-05-31 07:32:01 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
if (cond1 || cond2) {
|
2003-07-29 19:49:21 +00:00
|
|
|
assert(delaySlots==1 &&
|
|
|
|
"InsertBefore does not yet handle >1 delay slots!");
|
|
|
|
|
|
|
|
if (DEBUG_RA) {
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\nRegAlloc: Moved instr. with added code: "
|
2003-07-29 19:49:21 +00:00
|
|
|
<< *DelaySlotMI
|
2004-02-12 02:27:10 +00:00
|
|
|
<< " out of delay slots of instr: " << *MII;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move instruction before branch
|
2004-03-31 21:59:59 +00:00
|
|
|
MBB.insert(MII, MBB.remove(DelaySlotMI++));
|
2004-02-12 02:27:10 +00:00
|
|
|
|
|
|
|
// On cond1 we are done (we already moved the
|
|
|
|
// instruction out of the delay slot). On cond2 we need
|
|
|
|
// to insert a nop in place of the moved instruction
|
|
|
|
if (cond2) {
|
|
|
|
MBB.insert(MII, BuildMI(TM.getInstrInfo().getNOPOpCode(),1));
|
2003-05-31 07:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2004-02-12 02:27:10 +00:00
|
|
|
else {
|
2003-07-29 19:49:21 +00:00
|
|
|
// For non-branch instr with delay slots (probably a call), move
|
|
|
|
// InstrAfter to the instr. in the last delay slot.
|
2004-02-14 01:18:34 +00:00
|
|
|
MachineBasicBlock::iterator tmp = next(MII, delaySlots);
|
2004-02-12 02:27:10 +00:00
|
|
|
move2DelayedInstr(MII, tmp);
|
|
|
|
}
|
|
|
|
}
|
2003-05-31 07:32:01 +00:00
|
|
|
|
|
|
|
// Finally iterate over all instructions in BB and insert before/after
|
2003-07-25 21:06:09 +00:00
|
|
|
for (MachineBasicBlock::iterator MII=MBB.begin(); MII != MBB.end(); ++MII) {
|
2004-02-12 02:27:10 +00:00
|
|
|
MachineInstr *MInst = MII;
|
2003-07-25 21:06:09 +00:00
|
|
|
|
2001-11-10 21:21:36 +00:00
|
|
|
// do not process Phis
|
2004-02-11 20:47:34 +00:00
|
|
|
if (TM.getInstrInfo().isDummyPhiInstr(MInst->getOpcode()))
|
2001-11-10 21:21:36 +00:00
|
|
|
continue;
|
|
|
|
|
2003-07-25 21:06:09 +00:00
|
|
|
// if there are any added instructions...
|
2002-06-25 16:13:24 +00:00
|
|
|
if (AddedInstrMap.count(MInst)) {
|
2003-07-25 21:06:09 +00:00
|
|
|
AddedInstrns &CallAI = AddedInstrMap[MInst];
|
|
|
|
|
2003-07-29 19:49:21 +00:00
|
|
|
#ifndef NDEBUG
|
2004-02-11 20:47:34 +00:00
|
|
|
bool isBranch = (TM.getInstrInfo().isBranch(MInst->getOpcode()) ||
|
|
|
|
TM.getInstrInfo().isReturn(MInst->getOpcode()));
|
2003-07-29 19:49:21 +00:00
|
|
|
assert((!isBranch ||
|
|
|
|
AddedInstrMap[MInst].InstrnsAfter.size() <=
|
2004-02-11 20:47:34 +00:00
|
|
|
TM.getInstrInfo().getNumDelaySlots(MInst->getOpcode())) &&
|
2003-07-29 19:49:21 +00:00
|
|
|
"Cannot put more than #delaySlots instrns after "
|
|
|
|
"branch or return! Need to handle temps differently.");
|
|
|
|
#endif
|
|
|
|
|
2003-07-25 21:06:09 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Temporary sanity checking code to detect whether the same machine
|
|
|
|
// instruction is ever inserted twice before/after a call.
|
|
|
|
// I suspect this is happening but am not sure. --Vikram, 7/1/03.
|
|
|
|
std::set<const MachineInstr*> instrsSeen;
|
|
|
|
for (int i = 0, N = CallAI.InstrnsBefore.size(); i < N; ++i) {
|
|
|
|
assert(instrsSeen.count(CallAI.InstrnsBefore[i]) == 0 &&
|
|
|
|
"Duplicate machine instruction in InstrnsBefore!");
|
|
|
|
instrsSeen.insert(CallAI.InstrnsBefore[i]);
|
|
|
|
}
|
|
|
|
for (int i = 0, N = CallAI.InstrnsAfter.size(); i < N; ++i) {
|
|
|
|
assert(instrsSeen.count(CallAI.InstrnsAfter[i]) == 0 &&
|
|
|
|
"Duplicate machine instruction in InstrnsBefore/After!");
|
|
|
|
instrsSeen.insert(CallAI.InstrnsAfter[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Now add the instructions before/after this MI.
|
|
|
|
// We do this here to ensure that spill for an instruction is inserted
|
|
|
|
// as close as possible to an instruction (see above insertCode4Spill)
|
|
|
|
if (! CallAI.InstrnsBefore.empty())
|
|
|
|
PrependInstructions(CallAI.InstrnsBefore, MBB, MII,"");
|
|
|
|
|
|
|
|
if (! CallAI.InstrnsAfter.empty())
|
|
|
|
AppendInstructions(CallAI.InstrnsAfter, MBB, MII,"");
|
|
|
|
|
|
|
|
} // if there are any added instructions
|
2001-10-23 21:38:00 +00:00
|
|
|
} // for each machine instruction
|
|
|
|
}
|
|
|
|
}
|
2001-10-16 01:23:19 +00:00
|
|
|
|
2001-10-18 22:36:26 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Insert spill code for AN operand whose LR was spilled. May be called
|
|
|
|
/// repeatedly for a single MachineInstr if it has many spilled operands. On
|
|
|
|
/// each call, it finds a register which is not live at that instruction and
|
|
|
|
/// also which is not used by other spilled operands of the same
|
|
|
|
/// instruction. Then it uses this register temporarily to accommodate the
|
|
|
|
/// spilled value.
|
|
|
|
///
|
2001-11-08 16:43:25 +00:00
|
|
|
void PhyRegAlloc::insertCode4SpilledLR(const LiveRange *LR,
|
2003-07-29 19:49:21 +00:00
|
|
|
MachineBasicBlock::iterator& MII,
|
|
|
|
MachineBasicBlock &MBB,
|
2001-11-08 16:43:25 +00:00
|
|
|
const unsigned OpNum) {
|
2004-02-12 02:27:10 +00:00
|
|
|
MachineInstr *MInst = MII;
|
2003-07-29 19:49:21 +00:00
|
|
|
const BasicBlock *BB = MBB.getBasicBlock();
|
|
|
|
|
2004-02-11 20:47:34 +00:00
|
|
|
assert((! TM.getInstrInfo().isCall(MInst->getOpcode()) || OpNum == 0) &&
|
2002-09-28 17:02:40 +00:00
|
|
|
"Outgoing arg of a call must be handled elsewhere (func arg ok)");
|
2004-02-11 20:47:34 +00:00
|
|
|
assert(! TM.getInstrInfo().isReturn(MInst->getOpcode()) &&
|
2002-09-28 17:02:40 +00:00
|
|
|
"Return value of a ret must be handled elsewhere");
|
2001-11-15 20:23:19 +00:00
|
|
|
|
2001-11-08 16:43:25 +00:00
|
|
|
MachineOperand& Op = MInst->getOperand(OpNum);
|
2003-12-14 13:24:17 +00:00
|
|
|
bool isDef = Op.isDef();
|
|
|
|
bool isUse = Op.isUse();
|
2003-07-25 21:06:09 +00:00
|
|
|
unsigned RegType = MRI.getRegTypeForLR(LR);
|
2001-11-08 16:43:25 +00:00
|
|
|
int SpillOff = LR->getSpillOffFromFP();
|
|
|
|
RegClass *RC = LR->getRegClass();
|
2003-07-29 19:49:21 +00:00
|
|
|
|
|
|
|
// Get the live-variable set to find registers free before this instr.
|
2003-08-12 22:22:24 +00:00
|
|
|
const ValueSet &LVSetBef = LVI->getLiveVarSetBeforeMInst(MInst, BB);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// If this instr. is in the delay slot of a branch or return, we need to
|
|
|
|
// include all live variables before that branch or return -- we don't want to
|
|
|
|
// trample those! Verify that the set is included in the LV set before MInst.
|
2003-07-29 19:49:21 +00:00
|
|
|
if (MII != MBB.begin()) {
|
2004-02-14 01:18:34 +00:00
|
|
|
MachineBasicBlock::iterator PredMI = prior(MII);
|
2004-02-11 20:47:34 +00:00
|
|
|
if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(PredMI->getOpcode()))
|
2003-08-12 22:22:24 +00:00
|
|
|
assert(set_difference(LVI->getLiveVarSetBeforeMInst(PredMI), LVSetBef)
|
|
|
|
.empty() && "Live-var set before branch should be included in "
|
|
|
|
"live-var set of each delay slot instruction!");
|
2003-07-29 19:49:21 +00:00
|
|
|
}
|
2003-08-12 22:22:24 +00:00
|
|
|
#endif
|
2001-11-12 23:26:35 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
|
2001-11-08 16:43:25 +00:00
|
|
|
|
2003-09-01 20:05:47 +00:00
|
|
|
std::vector<MachineInstr*> MIBef, MIAft;
|
|
|
|
std::vector<MachineInstr*> AdIMid;
|
2001-11-15 20:23:19 +00:00
|
|
|
|
2003-07-10 19:42:55 +00:00
|
|
|
// Choose a register to hold the spilled value, if one was not preallocated.
|
|
|
|
// This may insert code before and after MInst to free up the value. If so,
|
|
|
|
// this code should be first/last in the spill sequence before/after MInst.
|
|
|
|
int TmpRegU=(LR->hasColor()
|
2003-09-24 17:50:28 +00:00
|
|
|
? MRI.getUnifiedRegNum(LR->getRegClassID(),LR->getColor())
|
2003-07-10 19:42:55 +00:00
|
|
|
: getUsableUniRegAtMI(RegType, &LVSetBef, MInst, MIBef,MIAft));
|
2001-11-15 20:23:19 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
// Set the operand first so that it this register does not get used
|
|
|
|
// as a scratch register for later calls to getUsableUniRegAtMI below
|
|
|
|
MInst->SetRegForOperand(OpNum, TmpRegU);
|
|
|
|
|
|
|
|
// get the added instructions for this instruction
|
2002-04-09 05:13:04 +00:00
|
|
|
AddedInstrns &AI = AddedInstrMap[MInst];
|
2002-07-08 23:15:32 +00:00
|
|
|
|
|
|
|
// We may need a scratch register to copy the spilled value to/from memory.
|
|
|
|
// This may itself have to insert code to free up a scratch register.
|
|
|
|
// Any such code should go before (after) the spill code for a load (store).
|
2003-05-31 07:32:01 +00:00
|
|
|
// The scratch reg is not marked as used because it is only used
|
|
|
|
// for the copy and not used across MInst.
|
2002-07-08 23:15:32 +00:00
|
|
|
int scratchRegType = -1;
|
|
|
|
int scratchReg = -1;
|
2003-10-22 20:22:53 +00:00
|
|
|
if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
|
2002-10-22 23:16:21 +00:00
|
|
|
scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
|
|
|
|
MInst, MIBef, MIAft);
|
2002-07-08 23:15:32 +00:00
|
|
|
assert(scratchReg != MRI.getInvalidRegNum());
|
|
|
|
}
|
|
|
|
|
2003-12-14 13:24:17 +00:00
|
|
|
if (isUse) {
|
2001-11-08 16:43:25 +00:00
|
|
|
// for a USE, we have to load the value of LR from stack to a TmpReg
|
|
|
|
// and use the TmpReg as one operand of instruction
|
2002-05-19 15:29:31 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
// actual loading instruction(s)
|
2003-07-29 19:49:21 +00:00
|
|
|
MRI.cpMem2RegMI(AdIMid, MRI.getFramePointer(), SpillOff, TmpRegU,
|
|
|
|
RegType, scratchReg);
|
2001-11-08 19:11:30 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
// the actual load should be after the instructions to free up TmpRegU
|
|
|
|
MIBef.insert(MIBef.end(), AdIMid.begin(), AdIMid.end());
|
|
|
|
AdIMid.clear();
|
|
|
|
}
|
|
|
|
|
2003-12-14 13:24:17 +00:00
|
|
|
if (isDef) { // if this is a Def
|
2001-11-08 16:43:25 +00:00
|
|
|
// for a DEF, we have to store the value produced by this instruction
|
|
|
|
// on the stack position allocated for this LR
|
2002-05-19 15:29:31 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
// actual storing instruction(s)
|
2003-07-29 19:49:21 +00:00
|
|
|
MRI.cpReg2MemMI(AdIMid, TmpRegU, MRI.getFramePointer(), SpillOff,
|
|
|
|
RegType, scratchReg);
|
2002-05-19 15:29:31 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
MIAft.insert(MIAft.begin(), AdIMid.begin(), AdIMid.end());
|
2001-11-08 16:43:25 +00:00
|
|
|
} // if !DEF
|
2002-05-19 15:29:31 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
// Finally, insert the entire spill code sequences before/after MInst
|
|
|
|
AI.InstrnsBefore.insert(AI.InstrnsBefore.end(), MIBef.begin(), MIBef.end());
|
|
|
|
AI.InstrnsAfter.insert(AI.InstrnsAfter.begin(), MIAft.begin(), MIAft.end());
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
if (DEBUG_RA) {
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\nFor Inst:\n " << *MInst;
|
|
|
|
std::cerr << "SPILLED LR# " << LR->getUserIGNode()->getIndex();
|
|
|
|
std::cerr << "; added Instructions:";
|
2002-07-09 19:18:56 +00:00
|
|
|
for_each(MIBef.begin(), MIBef.end(), std::mem_fun(&MachineInstr::dump));
|
|
|
|
for_each(MIAft.begin(), MIAft.end(), std::mem_fun(&MachineInstr::dump));
|
2002-06-25 16:13:24 +00:00
|
|
|
}
|
2001-11-08 16:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Insert caller saving/restoring instructions before/after a call machine
|
|
|
|
/// instruction (before or after any other instructions that were inserted for
|
|
|
|
/// the call).
|
|
|
|
///
|
2003-07-29 19:49:21 +00:00
|
|
|
void
|
|
|
|
PhyRegAlloc::insertCallerSavingCode(std::vector<MachineInstr*> &instrnsBefore,
|
|
|
|
std::vector<MachineInstr*> &instrnsAfter,
|
|
|
|
MachineInstr *CallMI,
|
2003-10-22 20:22:53 +00:00
|
|
|
const BasicBlock *BB) {
|
2004-02-11 20:47:34 +00:00
|
|
|
assert(TM.getInstrInfo().isCall(CallMI->getOpcode()));
|
2003-07-29 19:49:21 +00:00
|
|
|
|
2003-09-21 02:24:09 +00:00
|
|
|
// hash set to record which registers were saved/restored
|
2003-07-29 19:49:21 +00:00
|
|
|
hash_set<unsigned> PushedRegSet;
|
|
|
|
|
|
|
|
CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
|
|
|
|
|
|
|
|
// if the call is to a instrumentation function, do not insert save and
|
|
|
|
// restore instructions the instrumentation function takes care of save
|
|
|
|
// restore for volatile regs.
|
|
|
|
//
|
|
|
|
// FIXME: this should be made general, not specific to the reoptimizer!
|
|
|
|
const Function *Callee = argDesc->getCallInst()->getCalledFunction();
|
|
|
|
bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
|
|
|
|
|
|
|
|
// Now check if the call has a return value (using argDesc) and if so,
|
|
|
|
// find the LR of the TmpInstruction representing the return value register.
|
|
|
|
// (using the last or second-last *implicit operand* of the call MI).
|
|
|
|
// Insert it to to the PushedRegSet since we must not save that register
|
|
|
|
// and restore it after the call.
|
|
|
|
// We do this because, we look at the LV set *after* the instruction
|
|
|
|
// to determine, which LRs must be saved across calls. The return value
|
|
|
|
// of the call is live in this set - but we must not save/restore it.
|
|
|
|
if (const Value *origRetVal = argDesc->getReturnValue()) {
|
|
|
|
unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
|
|
|
|
(argDesc->getIndirectFuncPtr()? 1 : 2));
|
|
|
|
const TmpInstruction* tmpRetVal =
|
|
|
|
cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
|
|
|
|
assert(tmpRetVal->getOperand(0) == origRetVal &&
|
|
|
|
tmpRetVal->getType() == origRetVal->getType() &&
|
|
|
|
"Wrong implicit ref?");
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRange *RetValLR = LRI->getLiveRangeForValue(tmpRetVal);
|
2003-07-29 19:49:21 +00:00
|
|
|
assert(RetValLR && "No LR for RetValue of call");
|
|
|
|
|
|
|
|
if (! RetValLR->isMarkedForSpill())
|
|
|
|
PushedRegSet.insert(MRI.getUnifiedRegNum(RetValLR->getRegClassID(),
|
|
|
|
RetValLR->getColor()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ValueSet &LVSetAft = LVI->getLiveVarSetAfterMInst(CallMI, BB);
|
|
|
|
ValueSet::const_iterator LIt = LVSetAft.begin();
|
|
|
|
|
|
|
|
// for each live var in live variable set after machine inst
|
|
|
|
for( ; LIt != LVSetAft.end(); ++LIt) {
|
2003-09-21 02:24:09 +00:00
|
|
|
// get the live range corresponding to live var
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRange *const LR = LRI->getLiveRangeForValue(*LIt);
|
2003-07-29 19:49:21 +00:00
|
|
|
|
|
|
|
// LR can be null if it is a const since a const
|
|
|
|
// doesn't have a dominating def - see Assumptions above
|
2003-10-22 20:22:53 +00:00
|
|
|
if (LR) {
|
|
|
|
if (! LR->isMarkedForSpill()) {
|
2003-07-29 19:49:21 +00:00
|
|
|
assert(LR->hasColor() && "LR is neither spilled nor colored?");
|
|
|
|
unsigned RCID = LR->getRegClassID();
|
|
|
|
unsigned Color = LR->getColor();
|
|
|
|
|
|
|
|
if (MRI.isRegVolatile(RCID, Color) ) {
|
2003-09-21 02:24:09 +00:00
|
|
|
// if this is a call to the first-level reoptimizer
|
|
|
|
// instrumentation entry point, and the register is not
|
|
|
|
// modified by call, don't save and restore it.
|
2003-07-29 19:49:21 +00:00
|
|
|
if (isLLVMFirstTrigger && !MRI.modifiedByCall(RCID, Color))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// if the value is in both LV sets (i.e., live before and after
|
|
|
|
// the call machine instruction)
|
|
|
|
unsigned Reg = MRI.getUnifiedRegNum(RCID, Color);
|
|
|
|
|
2003-09-21 02:24:09 +00:00
|
|
|
// if we haven't already pushed this register...
|
2003-07-29 19:49:21 +00:00
|
|
|
if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
|
|
|
|
unsigned RegType = MRI.getRegTypeForLR(LR);
|
|
|
|
|
|
|
|
// Now get two instructions - to push on stack and pop from stack
|
|
|
|
// and add them to InstrnsBefore and InstrnsAfter of the
|
|
|
|
// call instruction
|
|
|
|
int StackOff =
|
2003-09-21 01:23:46 +00:00
|
|
|
MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
|
2003-07-29 19:49:21 +00:00
|
|
|
|
|
|
|
//---- Insert code for pushing the reg on stack ----------
|
|
|
|
|
|
|
|
std::vector<MachineInstr*> AdIBef, AdIAft;
|
|
|
|
|
|
|
|
// We may need a scratch register to copy the saved value
|
|
|
|
// to/from memory. This may itself have to insert code to
|
|
|
|
// free up a scratch register. Any such code should go before
|
|
|
|
// the save code. The scratch register, if any, is by default
|
|
|
|
// temporary and not "used" by the instruction unless the
|
|
|
|
// copy code itself decides to keep the value in the scratch reg.
|
|
|
|
int scratchRegType = -1;
|
|
|
|
int scratchReg = -1;
|
|
|
|
if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
|
|
|
|
{ // Find a register not live in the LVSet before CallMI
|
|
|
|
const ValueSet &LVSetBef =
|
|
|
|
LVI->getLiveVarSetBeforeMInst(CallMI, BB);
|
|
|
|
scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetBef,
|
|
|
|
CallMI, AdIBef, AdIAft);
|
|
|
|
assert(scratchReg != MRI.getInvalidRegNum());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AdIBef.size() > 0)
|
|
|
|
instrnsBefore.insert(instrnsBefore.end(),
|
|
|
|
AdIBef.begin(), AdIBef.end());
|
|
|
|
|
|
|
|
MRI.cpReg2MemMI(instrnsBefore, Reg, MRI.getFramePointer(),
|
|
|
|
StackOff, RegType, scratchReg);
|
|
|
|
|
|
|
|
if (AdIAft.size() > 0)
|
|
|
|
instrnsBefore.insert(instrnsBefore.end(),
|
|
|
|
AdIAft.begin(), AdIAft.end());
|
|
|
|
|
|
|
|
//---- Insert code for popping the reg from the stack ----------
|
|
|
|
AdIBef.clear();
|
|
|
|
AdIAft.clear();
|
|
|
|
|
|
|
|
// We may need a scratch register to copy the saved value
|
|
|
|
// from memory. This may itself have to insert code to
|
|
|
|
// free up a scratch register. Any such code should go
|
|
|
|
// after the save code. As above, scratch is not marked "used".
|
|
|
|
scratchRegType = -1;
|
|
|
|
scratchReg = -1;
|
|
|
|
if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType))
|
|
|
|
{ // Find a register not live in the LVSet after CallMI
|
|
|
|
scratchReg = getUsableUniRegAtMI(scratchRegType, &LVSetAft,
|
|
|
|
CallMI, AdIBef, AdIAft);
|
|
|
|
assert(scratchReg != MRI.getInvalidRegNum());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AdIBef.size() > 0)
|
|
|
|
instrnsAfter.insert(instrnsAfter.end(),
|
|
|
|
AdIBef.begin(), AdIBef.end());
|
|
|
|
|
|
|
|
MRI.cpMem2RegMI(instrnsAfter, MRI.getFramePointer(), StackOff,
|
|
|
|
Reg, RegType, scratchReg);
|
|
|
|
|
|
|
|
if (AdIAft.size() > 0)
|
|
|
|
instrnsAfter.insert(instrnsAfter.end(),
|
|
|
|
AdIAft.begin(), AdIAft.end());
|
|
|
|
|
|
|
|
PushedRegSet.insert(Reg);
|
|
|
|
|
|
|
|
if(DEBUG_RA) {
|
|
|
|
std::cerr << "\nFor call inst:" << *CallMI;
|
|
|
|
std::cerr << " -inserted caller saving instrs: Before:\n\t ";
|
|
|
|
for_each(instrnsBefore.begin(), instrnsBefore.end(),
|
|
|
|
std::mem_fun(&MachineInstr::dump));
|
|
|
|
std::cerr << " -and After:\n\t ";
|
|
|
|
for_each(instrnsAfter.begin(), instrnsAfter.end(),
|
|
|
|
std::mem_fun(&MachineInstr::dump));
|
|
|
|
}
|
|
|
|
} // if not already pushed
|
|
|
|
} // if LR has a volatile color
|
|
|
|
} // if LR has color
|
|
|
|
} // if there is a LR for Var
|
|
|
|
} // for each value in the LV set after instruction
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Returns the unified register number of a temporary register to be used
|
|
|
|
/// BEFORE MInst. If no register is available, it will pick one and modify
|
|
|
|
/// MIBef and MIAft to contain instructions used to free up this returned
|
|
|
|
/// register.
|
|
|
|
///
|
2002-07-08 23:15:32 +00:00
|
|
|
int PhyRegAlloc::getUsableUniRegAtMI(const int RegType,
|
|
|
|
const ValueSet *LVSetBef,
|
|
|
|
MachineInstr *MInst,
|
|
|
|
std::vector<MachineInstr*>& MIBef,
|
|
|
|
std::vector<MachineInstr*>& MIAft) {
|
2002-10-28 04:45:29 +00:00
|
|
|
RegClass* RC = getRegClassByID(MRI.getRegClassIDOfRegType(RegType));
|
2002-07-08 23:15:32 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
int RegU = getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
|
2002-07-08 23:15:32 +00:00
|
|
|
|
|
|
|
if (RegU == -1) {
|
2001-11-03 20:41:22 +00:00
|
|
|
// we couldn't find an unused register. Generate code to free up a reg by
|
2001-10-28 18:12:02 +00:00
|
|
|
// saving it on stack and restoring after the instruction
|
2002-07-08 23:15:32 +00:00
|
|
|
|
2003-09-21 01:23:46 +00:00
|
|
|
int TmpOff = MF->getInfo()->pushTempValue(MRI.getSpilledRegSize(RegType));
|
2001-11-08 04:48:50 +00:00
|
|
|
|
2003-07-25 21:06:09 +00:00
|
|
|
RegU = getUniRegNotUsedByThisInst(RC, RegType, MInst);
|
2002-05-19 15:29:31 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
// Check if we need a scratch register to copy this register to memory.
|
|
|
|
int scratchRegType = -1;
|
2003-10-22 20:22:53 +00:00
|
|
|
if (MRI.regTypeNeedsScratchReg(RegType, scratchRegType)) {
|
2002-10-28 04:45:29 +00:00
|
|
|
int scratchReg = getUsableUniRegAtMI(scratchRegType, LVSetBef,
|
|
|
|
MInst, MIBef, MIAft);
|
2002-07-08 23:15:32 +00:00
|
|
|
assert(scratchReg != MRI.getInvalidRegNum());
|
|
|
|
|
|
|
|
// We may as well hold the value in the scratch register instead
|
|
|
|
// of copying it to memory and back. But we have to mark the
|
|
|
|
// register as used by this instruction, so it does not get used
|
|
|
|
// as a scratch reg. by another operand or anyone else.
|
2003-08-05 22:11:13 +00:00
|
|
|
ScratchRegsUsed.insert(std::make_pair(MInst, scratchReg));
|
2002-07-08 23:15:32 +00:00
|
|
|
MRI.cpReg2RegMI(MIBef, RegU, scratchReg, RegType);
|
|
|
|
MRI.cpReg2RegMI(MIAft, scratchReg, RegU, RegType);
|
2003-10-22 20:22:53 +00:00
|
|
|
} else { // the register can be copied directly to/from memory so do it.
|
2002-07-08 23:15:32 +00:00
|
|
|
MRI.cpReg2MemMI(MIBef, RegU, MRI.getFramePointer(), TmpOff, RegType);
|
|
|
|
MRI.cpMem2RegMI(MIAft, MRI.getFramePointer(), TmpOff, RegU, RegType);
|
2003-10-22 20:22:53 +00:00
|
|
|
}
|
2001-10-28 18:12:02 +00:00
|
|
|
}
|
2002-07-08 23:15:32 +00:00
|
|
|
|
2001-11-15 20:23:19 +00:00
|
|
|
return RegU;
|
2001-10-28 18:12:02 +00:00
|
|
|
}
|
|
|
|
|
2003-07-29 19:49:21 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Returns the register-class register number of a new unused register that
|
|
|
|
/// can be used to accommodate a temporary value. May be called repeatedly
|
|
|
|
/// for a single MachineInstr. On each call, it finds a register which is not
|
|
|
|
/// live at that instruction and which is not used by any spilled operands of
|
|
|
|
/// that instruction.
|
|
|
|
///
|
|
|
|
int PhyRegAlloc::getUnusedUniRegAtMI(RegClass *RC, const int RegType,
|
2003-07-29 19:49:21 +00:00
|
|
|
const MachineInstr *MInst,
|
|
|
|
const ValueSet* LVSetBef) {
|
2003-07-25 21:06:09 +00:00
|
|
|
RC->clearColorsUsed(); // Reset array
|
2003-07-29 19:49:21 +00:00
|
|
|
|
|
|
|
if (LVSetBef == NULL) {
|
|
|
|
LVSetBef = &LVI->getLiveVarSetBeforeMInst(MInst);
|
|
|
|
assert(LVSetBef != NULL && "Unable to get live-var set before MInst?");
|
|
|
|
}
|
|
|
|
|
2002-02-05 02:52:05 +00:00
|
|
|
ValueSet::const_iterator LIt = LVSetBef->begin();
|
2001-10-28 18:12:02 +00:00
|
|
|
|
|
|
|
// for each live var in live variable set after machine inst
|
2002-06-25 16:13:24 +00:00
|
|
|
for ( ; LIt != LVSetBef->end(); ++LIt) {
|
2003-09-21 02:24:09 +00:00
|
|
|
// Get the live range corresponding to live var, and its RegClass
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRange *const LRofLV = LRI->getLiveRangeForValue(*LIt );
|
2001-10-28 18:12:02 +00:00
|
|
|
|
|
|
|
// LR can be null if it is a const since a const
|
|
|
|
// doesn't have a dominating def - see Assumptions above
|
2003-07-25 21:06:09 +00:00
|
|
|
if (LRofLV && LRofLV->getRegClass() == RC && LRofLV->hasColor())
|
|
|
|
RC->markColorsUsed(LRofLV->getColor(),
|
|
|
|
MRI.getRegTypeForLR(LRofLV), RegType);
|
2001-10-28 18:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// It is possible that one operand of this MInst was already spilled
|
|
|
|
// and it received some register temporarily. If that's the case,
|
|
|
|
// it is recorded in machine operand. We must skip such registers.
|
2003-07-25 21:06:09 +00:00
|
|
|
setRelRegsUsedByThisInst(RC, RegType, MInst);
|
|
|
|
|
|
|
|
int unusedReg = RC->getUnusedColor(RegType); // find first unused color
|
|
|
|
if (unusedReg >= 0)
|
|
|
|
return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
|
2001-10-28 18:12:02 +00:00
|
|
|
|
2002-05-23 15:50:03 +00:00
|
|
|
return -1;
|
2001-10-28 18:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Return the unified register number of a register in class RC which is not
|
|
|
|
/// used by any operands of MInst.
|
|
|
|
///
|
2001-11-15 20:23:19 +00:00
|
|
|
int PhyRegAlloc::getUniRegNotUsedByThisInst(RegClass *RC,
|
2003-07-25 21:06:09 +00:00
|
|
|
const int RegType,
|
2002-05-23 15:50:03 +00:00
|
|
|
const MachineInstr *MInst) {
|
2003-07-25 21:06:09 +00:00
|
|
|
RC->clearColorsUsed();
|
2001-11-15 20:23:19 +00:00
|
|
|
|
2003-07-25 21:06:09 +00:00
|
|
|
setRelRegsUsedByThisInst(RC, RegType, MInst);
|
2001-11-15 20:23:19 +00:00
|
|
|
|
2003-07-25 21:06:09 +00:00
|
|
|
// find the first unused color
|
|
|
|
int unusedReg = RC->getUnusedColor(RegType);
|
|
|
|
assert(unusedReg >= 0 &&
|
|
|
|
"FATAL: No free register could be found in reg class!!");
|
2001-11-15 20:23:19 +00:00
|
|
|
|
2003-07-25 21:06:09 +00:00
|
|
|
return MRI.getUnifiedRegNum(RC->getID(), unusedReg);
|
2001-11-15 20:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Modify the IsColorUsedArr of register class RC, by setting the bits
|
|
|
|
/// corresponding to register RegNo. This is a helper method of
|
|
|
|
/// setRelRegsUsedByThisInst().
|
|
|
|
///
|
2003-08-05 21:55:58 +00:00
|
|
|
static void markRegisterUsed(int RegNo, RegClass *RC, int RegType,
|
|
|
|
const TargetRegInfo &TRI) {
|
|
|
|
unsigned classId = 0;
|
|
|
|
int classRegNum = TRI.getClassRegNum(RegNo, classId);
|
|
|
|
if (RC->getID() == classId)
|
|
|
|
RC->markColorsUsed(classRegNum, RegType, RegType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhyRegAlloc::setRelRegsUsedByThisInst(RegClass *RC, int RegType,
|
2003-10-22 20:22:53 +00:00
|
|
|
const MachineInstr *MI) {
|
2003-08-05 21:55:58 +00:00
|
|
|
assert(OperandsColoredMap[MI] == true &&
|
2003-05-31 07:32:01 +00:00
|
|
|
"Illegal to call setRelRegsUsedByThisInst() until colored operands "
|
|
|
|
"are marked for an instruction.");
|
2001-10-28 18:12:02 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
// Add the registers already marked as used by the instruction. Both
|
|
|
|
// explicit and implicit operands are set.
|
2003-08-05 21:55:58 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
|
|
|
|
if (MI->getOperand(i).hasAllocatedReg())
|
2004-02-13 21:01:20 +00:00
|
|
|
markRegisterUsed(MI->getOperand(i).getReg(), RC, RegType,MRI);
|
2003-08-05 21:55:58 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = MI->getNumImplicitRefs(); i != e; ++i)
|
|
|
|
if (MI->getImplicitOp(i).hasAllocatedReg())
|
2004-02-13 21:01:20 +00:00
|
|
|
markRegisterUsed(MI->getImplicitOp(i).getReg(), RC, RegType,MRI);
|
2003-08-05 21:55:58 +00:00
|
|
|
|
2003-08-05 22:11:13 +00:00
|
|
|
// Add all of the scratch registers that are used to save values across the
|
|
|
|
// instruction (e.g., for saving state register values).
|
|
|
|
std::pair<ScratchRegsUsedTy::iterator, ScratchRegsUsedTy::iterator>
|
|
|
|
IR = ScratchRegsUsed.equal_range(MI);
|
|
|
|
for (ScratchRegsUsedTy::iterator I = IR.first; I != IR.second; ++I)
|
|
|
|
markRegisterUsed(I->second, RC, RegType, MRI);
|
2003-05-31 07:32:01 +00:00
|
|
|
|
2002-07-08 23:15:32 +00:00
|
|
|
// If there are implicit references, mark their allocated regs as well
|
2003-08-05 21:55:58 +00:00
|
|
|
for (unsigned z=0; z < MI->getNumImplicitRefs(); z++)
|
2002-07-08 23:15:32 +00:00
|
|
|
if (const LiveRange*
|
2003-09-21 01:23:46 +00:00
|
|
|
LRofImpRef = LRI->getLiveRangeForValue(MI->getImplicitRef(z)))
|
2002-07-08 23:15:32 +00:00
|
|
|
if (LRofImpRef->hasColor())
|
|
|
|
// this implicit reference is in a LR that received a color
|
2003-07-25 21:06:09 +00:00
|
|
|
RC->markColorsUsed(LRofImpRef->getColor(),
|
|
|
|
MRI.getRegTypeForLR(LRofImpRef), RegType);
|
2001-10-28 18:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// If there are delay slots for an instruction, the instructions added after
|
|
|
|
/// it must really go after the delayed instruction(s). So, we Move the
|
|
|
|
/// InstrAfter of that instruction to the corresponding delayed instruction
|
|
|
|
/// using the following method.
|
|
|
|
///
|
2003-05-31 07:32:01 +00:00
|
|
|
void PhyRegAlloc::move2DelayedInstr(const MachineInstr *OrigMI,
|
|
|
|
const MachineInstr *DelayedMI)
|
|
|
|
{
|
2003-08-12 22:22:24 +00:00
|
|
|
// "added after" instructions of the original instr
|
|
|
|
std::vector<MachineInstr *> &OrigAft = AddedInstrMap[OrigMI].InstrnsAfter;
|
|
|
|
|
|
|
|
if (DEBUG_RA && OrigAft.size() > 0) {
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\nRegAlloc: Moved InstrnsAfter for: " << *OrigMI;
|
|
|
|
std::cerr << " to last delay slot instrn: " << *DelayedMI;
|
2003-07-29 19:49:21 +00:00
|
|
|
}
|
|
|
|
|
2001-10-23 21:38:00 +00:00
|
|
|
// "added after" instructions of the delayed instr
|
2003-07-29 19:49:21 +00:00
|
|
|
std::vector<MachineInstr *> &DelayedAft=AddedInstrMap[DelayedMI].InstrnsAfter;
|
2001-09-15 19:06:58 +00:00
|
|
|
|
2001-10-23 21:38:00 +00:00
|
|
|
// go thru all the "added after instructions" of the original instruction
|
2003-05-31 07:32:01 +00:00
|
|
|
// and append them to the "added after instructions" of the delayed
|
2001-10-23 21:38:00 +00:00
|
|
|
// instructions
|
2002-01-20 22:54:45 +00:00
|
|
|
DelayedAft.insert(DelayedAft.end(), OrigAft.begin(), OrigAft.end());
|
2001-10-23 21:38:00 +00:00
|
|
|
|
|
|
|
// empty the "added after instructions" of the original instruction
|
|
|
|
OrigAft.clear();
|
|
|
|
}
|
2001-09-15 19:06:58 +00:00
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2001-09-30 23:11:59 +00:00
|
|
|
void PhyRegAlloc::colorIncomingArgs()
|
|
|
|
{
|
2003-09-21 01:23:46 +00:00
|
|
|
MRI.colorMethodArgs(Fn, *LRI, AddedInstrAtEntry.InstrnsBefore,
|
2003-07-29 19:49:21 +00:00
|
|
|
AddedInstrAtEntry.InstrnsAfter);
|
2001-09-30 23:11:59 +00:00
|
|
|
}
|
|
|
|
|
2001-09-18 22:43:57 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Determine whether the suggested color of each live range is really usable,
|
|
|
|
/// and then call its setSuggestedColorUsable() method to record the answer. A
|
|
|
|
/// suggested color is NOT usable when the suggested color is volatile AND
|
|
|
|
/// when there are call interferences.
|
|
|
|
///
|
2001-10-19 21:39:31 +00:00
|
|
|
void PhyRegAlloc::markUnusableSugColors()
|
|
|
|
{
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRangeMapType::const_iterator HMI = (LRI->getLiveRangeMap())->begin();
|
|
|
|
LiveRangeMapType::const_iterator HMIEnd = (LRI->getLiveRangeMap())->end();
|
2001-10-19 21:39:31 +00:00
|
|
|
|
2003-09-21 02:24:09 +00:00
|
|
|
for (; HMI != HMIEnd ; ++HMI ) {
|
|
|
|
if (HMI->first) {
|
|
|
|
LiveRange *L = HMI->second; // get the LiveRange
|
2003-09-24 17:50:28 +00:00
|
|
|
if (L && L->hasSuggestedColor ())
|
2003-09-24 18:08:54 +00:00
|
|
|
L->setSuggestedColorUsable
|
|
|
|
(!(MRI.isRegVolatile (L->getRegClassID (), L->getSuggestedColor ())
|
|
|
|
&& L->isCallInterference ()));
|
2003-09-21 02:24:09 +00:00
|
|
|
}
|
|
|
|
} // for all LR's in hash map
|
2001-10-19 21:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// For each live range that is spilled, allocates a new spill position on the
|
|
|
|
/// stack, and set the stack offsets of the live range that will be spilled to
|
|
|
|
/// that position. This must be called just after coloring the LRs.
|
|
|
|
///
|
2002-02-05 03:52:29 +00:00
|
|
|
void PhyRegAlloc::allocateStackSpace4SpilledLRs() {
|
2003-09-01 20:05:47 +00:00
|
|
|
if (DEBUG_RA) std::cerr << "\nSetting LR stack offsets for spills...\n";
|
2001-10-28 18:12:02 +00:00
|
|
|
|
2003-09-21 01:23:46 +00:00
|
|
|
LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap()->begin();
|
|
|
|
LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap()->end();
|
2001-10-28 18:12:02 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
for ( ; HMI != HMIEnd ; ++HMI) {
|
2002-02-05 03:52:29 +00:00
|
|
|
if (HMI->first && HMI->second) {
|
2003-07-10 19:42:55 +00:00
|
|
|
LiveRange *L = HMI->second; // get the LiveRange
|
|
|
|
if (L->isMarkedForSpill()) { // NOTE: allocating size of long Type **
|
2003-09-21 01:23:46 +00:00
|
|
|
int stackOffset = MF->getInfo()->allocateSpilledValue(Type::LongTy);
|
2002-09-14 23:05:33 +00:00
|
|
|
L->setSpillOffFromFP(stackOffset);
|
|
|
|
if (DEBUG_RA)
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << " LR# " << L->getUserIGNode()->getIndex()
|
2002-09-14 23:05:33 +00:00
|
|
|
<< ": stack-offset = " << stackOffset << "\n";
|
|
|
|
}
|
2002-02-05 03:52:29 +00:00
|
|
|
}
|
|
|
|
} // for all LR's in hash map
|
2001-10-28 18:12:02 +00:00
|
|
|
}
|
2001-10-19 21:39:31 +00:00
|
|
|
|
2003-09-21 02:50:21 +00:00
|
|
|
|
2003-11-10 00:05:26 +00:00
|
|
|
void PhyRegAlloc::saveStateForValue (std::vector<AllocInfo> &state,
|
2004-03-08 23:22:02 +00:00
|
|
|
const Value *V, int Insn, int Opnd) {
|
2003-11-10 00:05:26 +00:00
|
|
|
LiveRangeMapType::const_iterator HMI = LRI->getLiveRangeMap ()->find (V);
|
|
|
|
LiveRangeMapType::const_iterator HMIEnd = LRI->getLiveRangeMap ()->end ();
|
|
|
|
AllocInfo::AllocStateTy AllocState = AllocInfo::NotAllocated;
|
|
|
|
int Placement = -1;
|
|
|
|
if ((HMI != HMIEnd) && HMI->second) {
|
|
|
|
LiveRange *L = HMI->second;
|
|
|
|
assert ((L->hasColor () || L->isMarkedForSpill ())
|
|
|
|
&& "Live range exists but not colored or spilled");
|
|
|
|
if (L->hasColor ()) {
|
|
|
|
AllocState = AllocInfo::Allocated;
|
|
|
|
Placement = MRI.getUnifiedRegNum (L->getRegClassID (),
|
|
|
|
L->getColor ());
|
|
|
|
} else if (L->isMarkedForSpill ()) {
|
|
|
|
AllocState = AllocInfo::Spilled;
|
|
|
|
assert (L->hasSpillOffset ()
|
|
|
|
&& "Live range marked for spill but has no spill offset");
|
|
|
|
Placement = L->getSpillOffFromFP ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.push_back (AllocInfo (Insn, Opnd, AllocState, Placement));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Save the global register allocation decisions made by the register
|
|
|
|
/// allocator so that they can be accessed later (sort of like "poor man's
|
|
|
|
/// debug info").
|
|
|
|
///
|
|
|
|
void PhyRegAlloc::saveState () {
|
2003-10-23 20:32:55 +00:00
|
|
|
std::vector<AllocInfo> &state = FnAllocState[Fn];
|
2004-03-08 23:22:02 +00:00
|
|
|
unsigned ArgNum = 0;
|
|
|
|
// Arguments encoded as instruction # -1
|
|
|
|
for (Function::const_aiterator i=Fn->abegin (), e=Fn->aend (); i != e; ++i) {
|
|
|
|
const Argument *Arg = &*i;
|
|
|
|
saveStateForValue (state, Arg, -1, ArgNum);
|
|
|
|
++ArgNum;
|
|
|
|
}
|
2003-09-24 18:08:54 +00:00
|
|
|
unsigned Insn = 0;
|
2004-03-08 23:22:02 +00:00
|
|
|
// Instructions themselves encoded as operand # -1
|
2003-10-30 21:21:33 +00:00
|
|
|
for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II!=IE; ++II){
|
2004-04-27 15:13:33 +00:00
|
|
|
saveStateForValue (state, (&*II), Insn, -1);
|
|
|
|
for (unsigned i = 0; i < (*II).getNumOperands (); ++i) {
|
|
|
|
const Value *V = (*II).getOperand (i);
|
2003-11-10 00:05:26 +00:00
|
|
|
// Don't worry about it unless it's something whose reg. we'll need.
|
|
|
|
if (!isa<Argument> (V) && !isa<Instruction> (V))
|
|
|
|
continue;
|
|
|
|
saveStateForValue (state, V, Insn, i);
|
2003-09-24 18:08:54 +00:00
|
|
|
}
|
2003-10-30 21:21:33 +00:00
|
|
|
++Insn;
|
|
|
|
}
|
2003-09-24 18:08:54 +00:00
|
|
|
}
|
|
|
|
|
2003-10-23 20:32:55 +00:00
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Check the saved state filled in by saveState(), and abort if it looks
|
2003-11-04 22:42:41 +00:00
|
|
|
/// wrong. Only used when debugging. FIXME: Currently it just prints out
|
|
|
|
/// the state, which isn't quite as useful.
|
2003-10-22 20:22:53 +00:00
|
|
|
///
|
|
|
|
void PhyRegAlloc::verifySavedState () {
|
2003-10-30 21:21:33 +00:00
|
|
|
std::vector<AllocInfo> &state = FnAllocState[Fn];
|
2004-03-11 06:45:52 +00:00
|
|
|
int ArgNum = 0;
|
|
|
|
for (Function::const_aiterator i=Fn->abegin (), e=Fn->aend (); i != e; ++i) {
|
|
|
|
const Argument *Arg = &*i;
|
|
|
|
std::cerr << "Argument: " << *Arg << "\n"
|
|
|
|
<< "FnAllocState:\n";
|
|
|
|
for (unsigned i = 0; i < state.size (); ++i) {
|
|
|
|
AllocInfo &S = state[i];
|
|
|
|
if (S.Instruction == -1 && S.Operand == ArgNum)
|
|
|
|
std::cerr << " " << S << "\n";
|
|
|
|
}
|
|
|
|
std::cerr << "----------\n";
|
|
|
|
++ArgNum;
|
|
|
|
}
|
2004-03-08 23:22:02 +00:00
|
|
|
int Insn = 0;
|
2003-10-30 21:21:33 +00:00
|
|
|
for (const_inst_iterator II=inst_begin (Fn), IE=inst_end (Fn); II!=IE; ++II) {
|
2004-04-27 15:13:33 +00:00
|
|
|
const Instruction *I = &*II;
|
2003-10-30 21:21:33 +00:00
|
|
|
MachineCodeForInstruction &Instrs = MachineCodeForInstruction::get (I);
|
2004-03-11 06:45:52 +00:00
|
|
|
std::cerr << "Instruction: " << *I
|
2003-10-30 21:21:33 +00:00
|
|
|
<< "MachineCodeForInstruction:\n";
|
|
|
|
for (unsigned i = 0, n = Instrs.size (); i != n; ++i)
|
2004-03-11 06:45:52 +00:00
|
|
|
std::cerr << " " << *Instrs[i];
|
2003-10-30 21:21:33 +00:00
|
|
|
std::cerr << "FnAllocState:\n";
|
|
|
|
for (unsigned i = 0; i < state.size (); ++i) {
|
|
|
|
AllocInfo &S = state[i];
|
2004-01-28 19:05:43 +00:00
|
|
|
if (Insn == S.Instruction)
|
|
|
|
std::cerr << " " << S << "\n";
|
2003-10-30 21:21:33 +00:00
|
|
|
}
|
|
|
|
std::cerr << "----------\n";
|
|
|
|
++Insn;
|
|
|
|
}
|
2003-10-22 20:22:53 +00:00
|
|
|
}
|
2003-09-24 18:08:54 +00:00
|
|
|
|
2003-11-04 18:25:56 +00:00
|
|
|
|
2004-03-11 06:45:52 +00:00
|
|
|
bool PhyRegAlloc::doFinalization (Module &M) {
|
|
|
|
if (SaveRegAllocState) finishSavingState (M);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-23 20:32:55 +00:00
|
|
|
/// Finish the job of saveState(), by collapsing FnAllocState into an LLVM
|
2004-03-11 06:45:52 +00:00
|
|
|
/// Constant and stuffing it inside the Module.
|
2003-10-23 20:32:55 +00:00
|
|
|
///
|
2004-03-11 06:45:52 +00:00
|
|
|
/// FIXME: There should be other, better ways of storing the saved
|
|
|
|
/// state; this one is cumbersome and does not work well with the JIT.
|
|
|
|
///
|
|
|
|
void PhyRegAlloc::finishSavingState (Module &M) {
|
2004-03-11 19:46:30 +00:00
|
|
|
if (DEBUG_RA)
|
|
|
|
std::cerr << "---- Saving reg. alloc state; SaveStateToModule = "
|
|
|
|
<< SaveStateToModule << " ----\n";
|
2003-09-24 18:08:54 +00:00
|
|
|
|
2003-11-04 18:25:56 +00:00
|
|
|
// If saving state into the module, just copy new elements to the
|
|
|
|
// correct global.
|
2003-10-24 21:21:58 +00:00
|
|
|
if (!SaveStateToModule) {
|
|
|
|
ExportedFnAllocState = FnAllocState;
|
2003-11-04 18:25:56 +00:00
|
|
|
// FIXME: should ONLY copy new elements in FnAllocState
|
2004-03-11 06:45:52 +00:00
|
|
|
return;
|
2003-10-24 21:21:58 +00:00
|
|
|
}
|
|
|
|
|
2003-09-24 18:08:54 +00:00
|
|
|
// Convert FnAllocState to a single Constant array and add it
|
|
|
|
// to the Module.
|
|
|
|
ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), 0);
|
|
|
|
std::vector<const Type *> TV;
|
|
|
|
TV.push_back (Type::UIntTy);
|
|
|
|
TV.push_back (AT);
|
|
|
|
PointerType *PT = PointerType::get (StructType::get (TV));
|
|
|
|
|
|
|
|
std::vector<Constant *> allstate;
|
|
|
|
for (Module::iterator I = M.begin (), E = M.end (); I != E; ++I) {
|
|
|
|
Function *F = I;
|
2003-11-04 22:42:41 +00:00
|
|
|
if (F->isExternal ()) continue;
|
2003-09-24 18:08:54 +00:00
|
|
|
if (FnAllocState.find (F) == FnAllocState.end ()) {
|
|
|
|
allstate.push_back (ConstantPointerNull::get (PT));
|
|
|
|
} else {
|
2003-10-23 20:32:55 +00:00
|
|
|
std::vector<AllocInfo> &state = FnAllocState[F];
|
2003-10-22 20:44:23 +00:00
|
|
|
|
|
|
|
// Convert state into an LLVM ConstantArray, and put it in a
|
|
|
|
// ConstantStruct (named S) along with its size.
|
2003-10-23 20:32:55 +00:00
|
|
|
std::vector<Constant *> stateConstants;
|
|
|
|
for (unsigned i = 0, s = state.size (); i != s; ++i)
|
|
|
|
stateConstants.push_back (state[i].toConstant ());
|
|
|
|
unsigned Size = stateConstants.size ();
|
2003-10-22 20:44:23 +00:00
|
|
|
ArrayType *AT = ArrayType::get (AllocInfo::getConstantType (), Size);
|
|
|
|
std::vector<const Type *> TV;
|
|
|
|
TV.push_back (Type::UIntTy);
|
|
|
|
TV.push_back (AT);
|
|
|
|
StructType *ST = StructType::get (TV);
|
|
|
|
std::vector<Constant *> CV;
|
|
|
|
CV.push_back (ConstantUInt::get (Type::UIntTy, Size));
|
2003-10-23 20:32:55 +00:00
|
|
|
CV.push_back (ConstantArray::get (AT, stateConstants));
|
2003-10-22 20:44:23 +00:00
|
|
|
Constant *S = ConstantStruct::get (ST, CV);
|
|
|
|
|
2003-09-24 18:08:54 +00:00
|
|
|
GlobalVariable *GV =
|
2003-10-22 20:44:23 +00:00
|
|
|
new GlobalVariable (ST, true,
|
|
|
|
GlobalValue::InternalLinkage, S,
|
2003-09-24 18:08:54 +00:00
|
|
|
F->getName () + ".regAllocState", &M);
|
2003-10-22 20:44:23 +00:00
|
|
|
|
2003-11-10 00:05:26 +00:00
|
|
|
// Have: { uint, [Size x { uint, int, uint, int }] } *
|
|
|
|
// Cast it to: { uint, [0 x { uint, int, uint, int }] } *
|
2003-09-24 18:08:54 +00:00
|
|
|
Constant *CE = ConstantExpr::getCast (ConstantPointerRef::get (GV), PT);
|
|
|
|
allstate.push_back (CE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Size = allstate.size ();
|
|
|
|
// Final structure type is:
|
2003-11-10 00:05:26 +00:00
|
|
|
// { uint, [Size x { uint, [0 x { uint, int, uint, int }] } *] }
|
2003-09-24 18:08:54 +00:00
|
|
|
std::vector<const Type *> TV2;
|
|
|
|
TV2.push_back (Type::UIntTy);
|
|
|
|
ArrayType *AT2 = ArrayType::get (PT, Size);
|
|
|
|
TV2.push_back (AT2);
|
|
|
|
StructType *ST2 = StructType::get (TV2);
|
|
|
|
std::vector<Constant *> CV2;
|
|
|
|
CV2.push_back (ConstantUInt::get (Type::UIntTy, Size));
|
|
|
|
CV2.push_back (ConstantArray::get (AT2, allstate));
|
2003-11-10 07:12:01 +00:00
|
|
|
new GlobalVariable (ST2, true, GlobalValue::ExternalLinkage,
|
2003-09-24 18:08:54 +00:00
|
|
|
ConstantStruct::get (ST2, CV2), "_llvm_regAllocState",
|
|
|
|
&M);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 20:22:53 +00:00
|
|
|
/// Allocate registers for the machine code previously generated for F using
|
|
|
|
/// the graph-coloring algorithm.
|
|
|
|
///
|
2003-09-21 01:23:46 +00:00
|
|
|
bool PhyRegAlloc::runOnFunction (Function &F) {
|
|
|
|
if (DEBUG_RA)
|
|
|
|
std::cerr << "\n********* Function "<< F.getName () << " ***********\n";
|
|
|
|
|
|
|
|
Fn = &F;
|
|
|
|
MF = &MachineFunction::get (Fn);
|
|
|
|
LVI = &getAnalysis<FunctionLiveVarInfo> ();
|
|
|
|
LRI = new LiveRangeInfo (Fn, TM, RegClassList);
|
|
|
|
LoopDepthCalc = &getAnalysis<LoopInfo> ();
|
|
|
|
|
|
|
|
// Create each RegClass for the target machine and add it to the
|
|
|
|
// RegClassList. This must be done before calling constructLiveRanges().
|
|
|
|
for (unsigned rc = 0; rc != NumOfRegClasses; ++rc)
|
|
|
|
RegClassList.push_back (new RegClass (Fn, &TM.getRegInfo (),
|
|
|
|
MRI.getMachineRegClass (rc)));
|
|
|
|
|
|
|
|
LRI->constructLiveRanges(); // create LR info
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges)
|
2003-09-21 01:23:46 +00:00
|
|
|
LRI->printLiveRanges();
|
2001-09-30 23:11:59 +00:00
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
createIGNodeListsAndIGs(); // create IGNode list and IGs
|
|
|
|
|
|
|
|
buildInterferenceGraphs(); // build IGs in all reg classes
|
|
|
|
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
|
2001-09-14 21:18:34 +00:00
|
|
|
// print all LRs in all reg classes
|
2002-06-25 16:13:24 +00:00
|
|
|
for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
|
|
|
|
RegClassList[rc]->printIGNodeList();
|
2001-09-30 23:11:59 +00:00
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
// print IGs in all register classes
|
2002-06-25 16:13:24 +00:00
|
|
|
for ( unsigned rc=0; rc < NumOfRegClasses ; rc++)
|
|
|
|
RegClassList[rc]->printIG();
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
2002-01-07 19:19:18 +00:00
|
|
|
|
2003-09-21 01:23:46 +00:00
|
|
|
LRI->coalesceLRs(); // coalesce all live ranges
|
2001-11-03 17:13:27 +00:00
|
|
|
|
2002-09-14 23:05:33 +00:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
|
2001-09-14 21:18:34 +00:00
|
|
|
// print all LRs in all reg classes
|
2002-10-28 19:22:04 +00:00
|
|
|
for (unsigned rc=0; rc < NumOfRegClasses; rc++)
|
|
|
|
RegClassList[rc]->printIGNodeList();
|
2001-09-30 23:11:59 +00:00
|
|
|
|
2001-09-14 21:18:34 +00:00
|
|
|
// print IGs in all register classes
|
2002-10-28 19:22:04 +00:00
|
|
|
for (unsigned rc=0; rc < NumOfRegClasses; rc++)
|
|
|
|
RegClassList[rc]->printIG();
|
2001-09-14 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 21:39:31 +00:00
|
|
|
// mark un-usable suggested color before graph coloring algorithm.
|
|
|
|
// When this is done, the graph coloring algo will not reserve
|
|
|
|
// suggested color unnecessarily - they can be used by another LR
|
|
|
|
markUnusableSugColors();
|
|
|
|
|
|
|
|
// color all register classes using the graph coloring algo
|
2002-06-25 16:13:24 +00:00
|
|
|
for (unsigned rc=0; rc < NumOfRegClasses ; rc++)
|
2002-10-28 19:22:04 +00:00
|
|
|
RegClassList[rc]->colorAllRegs();
|
2001-09-18 22:43:57 +00:00
|
|
|
|
2003-09-11 22:34:13 +00:00
|
|
|
// After graph coloring, if some LRs did not receive a color (i.e, spilled)
|
|
|
|
// a position for such spilled LRs
|
2001-10-28 18:12:02 +00:00
|
|
|
allocateStackSpace4SpilledLRs();
|
2001-09-18 22:43:57 +00:00
|
|
|
|
2003-05-31 07:32:01 +00:00
|
|
|
// Reset the temp. area on the stack before use by the first instruction.
|
|
|
|
// This will also happen after updating each instruction.
|
2003-09-21 01:23:46 +00:00
|
|
|
MF->getInfo()->popAllTempValues();
|
2001-11-15 22:02:06 +00:00
|
|
|
|
2002-01-07 19:19:18 +00:00
|
|
|
// color incoming args - if the correct color was not received
|
|
|
|
// insert code to copy to the correct register
|
2001-09-30 23:11:59 +00:00
|
|
|
colorIncomingArgs();
|
2001-09-14 21:18:34 +00:00
|
|
|
|
2003-09-24 18:08:54 +00:00
|
|
|
// Save register allocation state for this function in a Constant.
|
2004-03-10 22:01:59 +00:00
|
|
|
if (SaveRegAllocState) {
|
2003-09-24 18:08:54 +00:00
|
|
|
saveState();
|
2004-03-11 19:46:30 +00:00
|
|
|
if (DEBUG_RA) // Check our work.
|
2004-03-10 22:01:59 +00:00
|
|
|
verifySavedState ();
|
2004-03-11 19:46:30 +00:00
|
|
|
if (!SaveStateToModule)
|
|
|
|
finishSavingState (const_cast<Module&> (*Fn->getParent ()));
|
2003-10-22 20:22:53 +00:00
|
|
|
}
|
2003-09-24 18:08:54 +00:00
|
|
|
|
2003-10-22 20:44:23 +00:00
|
|
|
// Now update the machine code with register names and add any additional
|
|
|
|
// code inserted by the register allocator to the instruction stream.
|
2001-09-15 21:11:11 +00:00
|
|
|
updateMachineCode();
|
2002-01-07 19:19:18 +00:00
|
|
|
|
2001-09-19 16:26:23 +00:00
|
|
|
if (DEBUG_RA) {
|
2003-09-01 20:05:47 +00:00
|
|
|
std::cerr << "\n**** Machine Code After Register Allocation:\n\n";
|
2003-09-21 01:23:46 +00:00
|
|
|
MF->dump();
|
2001-09-19 16:26:23 +00:00
|
|
|
}
|
2003-09-21 01:23:46 +00:00
|
|
|
|
|
|
|
// Tear down temporary data structures
|
|
|
|
for (unsigned rc = 0; rc < NumOfRegClasses; ++rc)
|
|
|
|
delete RegClassList[rc];
|
|
|
|
RegClassList.clear ();
|
|
|
|
AddedInstrMap.clear ();
|
|
|
|
OperandsColoredMap.clear ();
|
|
|
|
ScratchRegsUsed.clear ();
|
|
|
|
AddedInstrAtEntry.clear ();
|
|
|
|
delete LRI;
|
|
|
|
|
|
|
|
if (DEBUG_RA) std::cerr << "\nRegister allocation complete!\n";
|
|
|
|
return false; // Function was not modified
|
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
} // End llvm namespace
|