2002-09-15 01:05:33 +02:00
|
|
|
//===-- LiveRangeInfo.cpp -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Live range construction for coloring-based register allocation for LLVM.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-09-14 23:18:34 +02:00
|
|
|
#include "llvm/CodeGen/LiveRangeInfo.h"
|
2003-01-15 20:57:07 +01:00
|
|
|
#include "RegAllocCommon.h"
|
2003-01-15 22:14:01 +01:00
|
|
|
#include "RegClass.h"
|
2002-10-29 17:51:05 +01:00
|
|
|
#include "llvm/CodeGen/IGNode.h"
|
2002-02-04 06:56:09 +01:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-10-28 20:22:04 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-02-04 06:56:09 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2003-01-14 23:00:31 +01:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2003-07-25 23:06:09 +02:00
|
|
|
#include "llvm/Target/TargetRegInfo.h"
|
2002-04-07 22:49:59 +02:00
|
|
|
#include "llvm/Function.h"
|
2002-02-05 04:35:53 +01:00
|
|
|
#include "Support/SetOperations.h"
|
2002-01-20 23:54:45 +01:00
|
|
|
using std::cerr;
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2003-01-15 22:14:01 +01:00
|
|
|
unsigned LiveRange::getRegClassID() const { return getRegClass()->getID(); }
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
LiveRangeInfo::LiveRangeInfo(const Function *F, const TargetMachine &tm,
|
2002-01-20 23:54:45 +01:00
|
|
|
std::vector<RegClass *> &RCL)
|
2002-04-07 22:49:59 +02:00
|
|
|
: Meth(F), TM(tm), RegClassList(RCL), MRI(tm.getRegInfo()) { }
|
2001-09-14 23:18:34 +02:00
|
|
|
|
|
|
|
|
2002-01-07 20:19:18 +01:00
|
|
|
LiveRangeInfo::~LiveRangeInfo() {
|
2002-02-05 07:02:59 +01:00
|
|
|
for (LiveRangeMapType::iterator MI = LiveRangeMap.begin();
|
|
|
|
MI != LiveRangeMap.end(); ++MI) {
|
2002-01-07 20:19:18 +01:00
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
if (MI->first && MI->second) {
|
|
|
|
LiveRange *LR = MI->second;
|
2002-01-07 20:19:18 +01:00
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
// we need to be careful in deleting LiveRanges in LiveRangeMap
|
|
|
|
// since two/more Values in the live range map can point to the same
|
|
|
|
// live range. We have to make the other entries NULL when we delete
|
|
|
|
// a live range.
|
2002-01-07 20:19:18 +01:00
|
|
|
|
2002-10-29 17:51:05 +01:00
|
|
|
for (LiveRange::iterator LI = LR->begin(); LI != LR->end(); ++LI)
|
2002-01-20 23:54:45 +01:00
|
|
|
LiveRangeMap[*LI] = 0;
|
|
|
|
|
|
|
|
delete LR;
|
2002-01-07 20:19:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2001-09-14 23:18:34 +02:00
|
|
|
// union two live ranges into one. The 2nd LR is deleted. Used for coalescing.
|
2001-10-01 01:11:59 +02:00
|
|
|
// Note: the caller must make sure that L1 and L2 are distinct and both
|
|
|
|
// LRs don't have suggested colors
|
2002-01-07 20:19:18 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2002-02-05 03:52:05 +01:00
|
|
|
void LiveRangeInfo::unionAndUpdateLRs(LiveRange *L1, LiveRange *L2) {
|
|
|
|
assert(L1 != L2 && (!L1->hasSuggestedColor() || !L2->hasSuggestedColor()));
|
2003-05-31 09:32:01 +02:00
|
|
|
assert(! (L1->hasColor() && L2->hasColor()) ||
|
|
|
|
L1->getColor() == L2->getColor());
|
|
|
|
|
2002-02-05 03:52:05 +01:00
|
|
|
set_union(*L1, *L2); // add elements of L2 to L1
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2002-02-05 03:52:05 +01:00
|
|
|
for(ValueSet::iterator L2It = L2->begin(); L2It != L2->end(); ++L2It) {
|
2001-09-14 23:18:34 +02:00
|
|
|
//assert(( L1->getTypeID() == L2->getTypeID()) && "Merge:Different types");
|
|
|
|
|
2002-02-04 17:36:59 +01:00
|
|
|
L1->insert(*L2It); // add the var in L2 to L1
|
2002-04-07 22:49:59 +02:00
|
|
|
LiveRangeMap[*L2It] = L1; // now the elements in L2 should map
|
2002-01-07 20:19:18 +01:00
|
|
|
//to L1
|
2001-09-14 23:18:34 +02:00
|
|
|
}
|
2003-05-31 09:32:01 +02:00
|
|
|
|
|
|
|
// set call interference for L1 from L2
|
2002-02-05 03:52:05 +01:00
|
|
|
if (L2->isCallInterference())
|
2001-10-19 19:21:03 +02:00
|
|
|
L1->setCallInterference();
|
|
|
|
|
2002-02-05 03:52:05 +01:00
|
|
|
// add the spill costs
|
|
|
|
L1->addSpillCost(L2->getSpillCost());
|
2003-05-31 09:32:01 +02:00
|
|
|
|
|
|
|
// If L2 has a color, give L1 that color. Note that L1 may have had the same
|
|
|
|
// color or none, but would not have a different color as asserted above.
|
|
|
|
if (L2->hasColor())
|
|
|
|
L1->setColor(L2->getColor());
|
|
|
|
|
|
|
|
// Similarly, if LROfUse(L2) has a suggested color, the new range
|
|
|
|
// must have the same color.
|
|
|
|
if (L2->hasSuggestedColor())
|
|
|
|
L1->setSuggestedColor(L2->getSuggestedColor());
|
2002-02-05 03:52:05 +01:00
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
delete L2; // delete L2 as it is no longer needed
|
2001-09-14 23:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-28 19:05:22 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Method for creating a single live range for a definition.
|
|
|
|
// The definition must be represented by a virtual register (a Value).
|
|
|
|
// Note: this function does *not* check that no live range exists for def.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
LiveRange*
|
|
|
|
LiveRangeInfo::createNewLiveRange(const Value* Def, bool isCC /* = false*/)
|
|
|
|
{
|
|
|
|
LiveRange* DefRange = new LiveRange(); // Create a new live range,
|
|
|
|
DefRange->insert(Def); // add Def to it,
|
|
|
|
LiveRangeMap[Def] = DefRange; // and update the map.
|
|
|
|
|
|
|
|
// set the register class of the new live range
|
2003-01-15 22:14:01 +01:00
|
|
|
DefRange->setRegClass(RegClassList[MRI.getRegClassIDOfType(Def->getType(),
|
|
|
|
isCC)]);
|
2002-09-28 19:05:22 +02:00
|
|
|
|
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges) {
|
|
|
|
cerr << " Creating a LR for def ";
|
|
|
|
if (isCC) cerr << " (CC Register!)";
|
|
|
|
cerr << " : " << RAV(Def) << "\n";
|
|
|
|
}
|
|
|
|
return DefRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LiveRange*
|
|
|
|
LiveRangeInfo::createOrAddToLiveRange(const Value* Def, bool isCC /* = false*/)
|
|
|
|
{
|
|
|
|
LiveRange *DefRange = LiveRangeMap[Def];
|
|
|
|
|
|
|
|
// check if the LR is already there (because of multiple defs)
|
|
|
|
if (!DefRange) {
|
2002-10-28 05:45:29 +01:00
|
|
|
DefRange = createNewLiveRange(Def, isCC);
|
2002-09-28 19:05:22 +02:00
|
|
|
} else { // live range already exists
|
|
|
|
DefRange->insert(Def); // add the operand to the range
|
|
|
|
LiveRangeMap[Def] = DefRange; // make operand point to merged set
|
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges)
|
|
|
|
cerr << " Added to existing LR for def: " << RAV(Def) << "\n";
|
|
|
|
}
|
|
|
|
return DefRange;
|
|
|
|
}
|
|
|
|
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2002-01-07 20:19:18 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2002-04-07 22:49:59 +02:00
|
|
|
// Method for constructing all live ranges in a function. It creates live
|
2002-01-07 20:19:18 +01:00
|
|
|
// ranges for all values defined in the instruction stream. Also, it
|
2002-04-07 22:49:59 +02:00
|
|
|
// creates live ranges for all incoming arguments of the function.
|
2002-01-07 20:19:18 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2002-02-05 07:02:59 +01:00
|
|
|
void LiveRangeInfo::constructLiveRanges() {
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2002-09-15 01:05:33 +02:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges)
|
|
|
|
cerr << "Constructing Live Ranges ...\n";
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
// first find the live ranges for all incoming args of the function since
|
|
|
|
// those LRs start from the start of the function
|
2002-09-28 19:05:22 +02:00
|
|
|
for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI)
|
2002-10-28 05:45:29 +01:00
|
|
|
createNewLiveRange(AI, /*isCC*/ false);
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
// Now suggest hardware registers for these function args
|
2001-10-01 01:11:59 +02:00
|
|
|
MRI.suggestRegs4MethodArgs(Meth, *this);
|
|
|
|
|
2002-09-28 19:05:22 +02:00
|
|
|
// Now create LRs for machine instructions. A new LR will be created
|
|
|
|
// only for defs in the machine instr since, we assume that all Values are
|
|
|
|
// defined before they are used. However, there can be multiple defs for
|
|
|
|
// the same Value in machine instructions.
|
|
|
|
//
|
|
|
|
// Also, find CALL and RETURN instructions, which need extra work.
|
2002-02-05 07:02:59 +01:00
|
|
|
//
|
2002-10-28 20:22:04 +01:00
|
|
|
MachineFunction &MF = MachineFunction::get(Meth);
|
|
|
|
for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
|
|
|
|
MachineBasicBlock &MBB = *BBI;
|
2002-09-28 19:05:22 +02:00
|
|
|
|
2001-09-14 23:18:34 +02:00
|
|
|
// iterate over all the machine instructions in BB
|
2002-10-28 20:22:04 +01:00
|
|
|
for(MachineBasicBlock::iterator MInstIterator = MBB.begin();
|
|
|
|
MInstIterator != MBB.end(); ++MInstIterator) {
|
2002-07-09 01:07:26 +02:00
|
|
|
MachineInstr *MInst = *MInstIterator;
|
2001-10-01 01:11:59 +02:00
|
|
|
|
2002-09-28 19:05:22 +02:00
|
|
|
// If the machine instruction is a call/return instruction, add it to
|
|
|
|
// CallRetInstrList for processing its args, ret value, and ret addr.
|
|
|
|
//
|
2002-01-20 23:54:45 +01:00
|
|
|
if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ||
|
|
|
|
TM.getInstrInfo().isCall(MInst->getOpCode()))
|
2002-10-28 05:45:29 +01:00
|
|
|
CallRetInstrList.push_back(MInst);
|
2001-10-15 18:26:38 +02:00
|
|
|
|
2002-09-28 19:05:22 +02:00
|
|
|
// iterate over explicit MI operands and create a new LR
|
|
|
|
// for each operand that is defined by the instruction
|
2002-07-09 01:07:26 +02:00
|
|
|
for (MachineInstr::val_op_iterator OpI = MInst->begin(),
|
2002-09-28 19:05:22 +02:00
|
|
|
OpE = MInst->end(); OpI != OpE; ++OpI)
|
2003-05-27 02:05:23 +02:00
|
|
|
if (OpI.isDefOnly() || OpI.isDefAndUse()) {
|
2002-02-04 17:36:59 +01:00
|
|
|
const Value *Def = *OpI;
|
2002-10-28 05:45:29 +01:00
|
|
|
bool isCC = (OpI.getMachineOperand().getType()
|
2002-09-28 19:05:22 +02:00
|
|
|
== MachineOperand::MO_CCRegister);
|
2003-05-31 09:32:01 +02:00
|
|
|
LiveRange* LR = createOrAddToLiveRange(Def, isCC);
|
|
|
|
|
|
|
|
// If the operand has a pre-assigned register,
|
|
|
|
// set it directly in the LiveRange
|
|
|
|
if (OpI.getMachineOperand().hasAllocatedReg()) {
|
|
|
|
unsigned getClassId;
|
|
|
|
LR->setColor(MRI.getClassRegNum(
|
|
|
|
OpI.getMachineOperand().getAllocatedRegNum(),
|
|
|
|
getClassId));
|
|
|
|
}
|
2002-09-28 19:05:22 +02:00
|
|
|
}
|
2001-10-01 01:11:59 +02:00
|
|
|
|
2002-09-28 19:05:22 +02:00
|
|
|
// iterate over implicit MI operands and create a new LR
|
|
|
|
// for each operand that is defined by the instruction
|
|
|
|
for (unsigned i = 0; i < MInst->getNumImplicitRefs(); ++i)
|
2003-05-27 02:05:23 +02:00
|
|
|
if (MInst->getImplicitOp(i).opIsDefOnly() ||
|
|
|
|
MInst->getImplicitOp(i).opIsDefAndUse()) {
|
2002-09-28 19:05:22 +02:00
|
|
|
const Value *Def = MInst->getImplicitRef(i);
|
2003-05-31 09:32:01 +02:00
|
|
|
LiveRange* LR = createOrAddToLiveRange(Def, /*isCC*/ false);
|
|
|
|
|
|
|
|
// If the implicit operand has a pre-assigned register,
|
|
|
|
// set it directly in the LiveRange
|
|
|
|
if (MInst->getImplicitOp(i).hasAllocatedReg()) {
|
|
|
|
unsigned getClassId;
|
|
|
|
LR->setColor(MRI.getClassRegNum(
|
|
|
|
MInst->getImplicitOp(i).getAllocatedRegNum(),
|
|
|
|
getClassId));
|
|
|
|
}
|
2002-09-28 19:05:22 +02:00
|
|
|
}
|
2001-09-14 23:18:34 +02:00
|
|
|
|
|
|
|
} // for all machine instructions in the BB
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
} // for all BBs in function
|
2001-10-01 01:11:59 +02:00
|
|
|
|
2001-10-15 18:26:38 +02:00
|
|
|
// Now we have to suggest clors for call and return arg live ranges.
|
|
|
|
// Also, if there are implicit defs (e.g., retun value of a call inst)
|
|
|
|
// they must be added to the live range list
|
2002-09-28 19:05:22 +02:00
|
|
|
//
|
2001-10-15 18:26:38 +02:00
|
|
|
suggestRegs4CallRets();
|
2001-10-01 01:11:59 +02:00
|
|
|
|
2002-09-15 01:05:33 +02:00
|
|
|
if( DEBUG_RA >= RA_DEBUG_LiveRanges)
|
2002-01-20 23:54:45 +01:00
|
|
|
cerr << "Initial Live Ranges constructed!\n";
|
2001-10-15 18:26:38 +02:00
|
|
|
}
|
2001-10-01 01:11:59 +02:00
|
|
|
|
|
|
|
|
2002-01-07 20:19:18 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// If some live ranges must be colored with specific hardware registers
|
|
|
|
// (e.g., for outgoing call args), suggesting of colors for such live
|
2002-04-07 22:49:59 +02:00
|
|
|
// ranges is done using target specific function. Those functions are called
|
2002-01-07 20:19:18 +01:00
|
|
|
// from this function. The target specific methods must:
|
|
|
|
// 1) suggest colors for call and return args.
|
|
|
|
// 2) create new LRs for implicit defs in machine instructions
|
|
|
|
//---------------------------------------------------------------------------
|
2002-10-29 18:03:19 +01:00
|
|
|
void LiveRangeInfo::suggestRegs4CallRets() {
|
|
|
|
std::vector<MachineInstr*>::iterator It = CallRetInstrList.begin();
|
|
|
|
for( ; It != CallRetInstrList.end(); ++It) {
|
2002-07-09 01:07:26 +02:00
|
|
|
MachineInstr *MInst = *It;
|
2002-10-29 18:03:19 +01:00
|
|
|
MachineOpCode OpCode = MInst->getOpCode();
|
2001-10-15 18:26:38 +02:00
|
|
|
|
2002-10-29 18:03:19 +01:00
|
|
|
if ((TM.getInstrInfo()).isReturn(OpCode))
|
|
|
|
MRI.suggestReg4RetValue(MInst, *this);
|
|
|
|
else if ((TM.getInstrInfo()).isCall(OpCode))
|
|
|
|
MRI.suggestRegs4CallArgs(MInst, *this);
|
2001-10-15 18:26:38 +02:00
|
|
|
else
|
2002-10-29 18:03:19 +01:00
|
|
|
assert( 0 && "Non call/ret instr in CallRetInstrList" );
|
2001-10-15 18:26:38 +02:00
|
|
|
}
|
2001-09-14 23:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-07 20:19:18 +01:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// The following method coalesces live ranges when possible. This method
|
|
|
|
// must be called after the interference graph has been constructed.
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2001-10-15 18:26:38 +02:00
|
|
|
|
2001-09-14 23:18:34 +02:00
|
|
|
/* Algorithm:
|
2002-04-07 22:49:59 +02:00
|
|
|
for each BB in function
|
2001-09-14 23:18:34 +02:00
|
|
|
for each machine instruction (inst)
|
|
|
|
for each definition (def) in inst
|
|
|
|
for each operand (op) of inst that is a use
|
2001-11-10 01:20:24 +01:00
|
|
|
if the def and op are of the same register type
|
2001-09-14 23:18:34 +02:00
|
|
|
if the def and op do not interfere //i.e., not simultaneously live
|
|
|
|
if (degree(LR of def) + degree(LR of op)) <= # avail regs
|
2001-10-01 01:11:59 +02:00
|
|
|
if both LRs do not have suggested colors
|
|
|
|
merge2IGNodes(def, op) // i.e., merge 2 LRs
|
2001-09-14 23:18:34 +02:00
|
|
|
|
|
|
|
*/
|
2002-01-07 20:19:18 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2003-05-31 09:32:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Checks if live range LR interferes with any node assigned or suggested to
|
|
|
|
// be assigned the specified color
|
|
|
|
//
|
|
|
|
inline bool InterferesWithColor(const LiveRange& LR, unsigned color)
|
|
|
|
{
|
|
|
|
IGNode* lrNode = LR.getUserIGNode();
|
|
|
|
for (unsigned n=0, NN = lrNode->getNumOfNeighbors(); n < NN; n++) {
|
|
|
|
LiveRange *neighLR = lrNode->getAdjIGNode(n)->getParentLR();
|
|
|
|
if (neighLR->hasColor() && neighLR->getColor() == color)
|
|
|
|
return true;
|
|
|
|
if (neighLR->hasSuggestedColor() && neighLR->getSuggestedColor() == color)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot coalesce if any of the following is true:
|
|
|
|
// (1) Both LRs have suggested colors (should be "different suggested colors"?)
|
|
|
|
// (2) Both LR1 and LR2 have colors and the colors are different
|
|
|
|
// (but if the colors are the same, it is definitely safe to coalesce)
|
|
|
|
// (3) LR1 has color and LR2 interferes with any LR that has the same color
|
|
|
|
// (4) LR2 has color and LR1 interferes with any LR that has the same color
|
|
|
|
//
|
|
|
|
inline bool InterfsPreventCoalescing(const LiveRange& LROfDef,
|
|
|
|
const LiveRange& LROfUse)
|
|
|
|
{
|
|
|
|
// (4) if they have different suggested colors, cannot coalesce
|
|
|
|
if (LROfDef.hasSuggestedColor() && LROfUse.hasSuggestedColor())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// if neither has a color, nothing more to do.
|
|
|
|
if (! LROfDef.hasColor() && ! LROfUse.hasColor())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// (2, 3) if L1 has color...
|
|
|
|
if (LROfDef.hasColor()) {
|
|
|
|
if (LROfUse.hasColor())
|
|
|
|
return (LROfUse.getColor() != LROfDef.getColor());
|
|
|
|
return InterferesWithColor(LROfUse, LROfDef.getColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
// (4) else only LROfUse has a color: check if that could interfere
|
|
|
|
return InterferesWithColor(LROfDef, LROfUse.getColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-07 20:19:18 +01:00
|
|
|
void LiveRangeInfo::coalesceLRs()
|
|
|
|
{
|
2002-09-15 01:05:33 +02:00
|
|
|
if(DEBUG_RA >= RA_DEBUG_LiveRanges)
|
|
|
|
cerr << "\nCoalescing LRs ...\n";
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2002-10-28 20:22:04 +01:00
|
|
|
MachineFunction &MF = MachineFunction::get(Meth);
|
|
|
|
for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
|
|
|
|
MachineBasicBlock &MBB = *BBI;
|
2001-09-14 23:18:34 +02:00
|
|
|
|
|
|
|
// iterate over all the machine instructions in BB
|
2002-10-28 20:22:04 +01:00
|
|
|
for(MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII){
|
|
|
|
const MachineInstr *MI = *MII;
|
2001-09-14 23:18:34 +02:00
|
|
|
|
2002-09-15 01:05:33 +02:00
|
|
|
if( DEBUG_RA >= RA_DEBUG_LiveRanges) {
|
2002-01-20 23:54:45 +01:00
|
|
|
cerr << " *Iterating over machine instr ";
|
2002-10-28 20:22:04 +01:00
|
|
|
MI->dump();
|
2002-01-20 23:54:45 +01:00
|
|
|
cerr << "\n";
|
2001-09-14 23:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over MI operands to find defs
|
2002-10-28 20:22:04 +01:00
|
|
|
for(MachineInstr::const_val_op_iterator DefI = MI->begin(),
|
|
|
|
DefE = MI->end(); DefI != DefE; ++DefI) {
|
2003-05-27 02:05:23 +02:00
|
|
|
if (DefI.isDefOnly() || DefI.isDefAndUse()) { // this operand is modified
|
2002-02-05 07:02:59 +01:00
|
|
|
LiveRange *LROfDef = getLiveRangeForValue( *DefI );
|
|
|
|
RegClass *RCOfDef = LROfDef->getRegClass();
|
|
|
|
|
2002-10-28 20:22:04 +01:00
|
|
|
MachineInstr::const_val_op_iterator UseI = MI->begin(),
|
|
|
|
UseE = MI->end();
|
|
|
|
for( ; UseI != UseE; ++UseI) { // for all uses
|
2002-02-05 07:02:59 +01:00
|
|
|
LiveRange *LROfUse = getLiveRangeForValue( *UseI );
|
|
|
|
if (!LROfUse) { // if LR of use is not found
|
2001-09-14 23:18:34 +02:00
|
|
|
//don't warn about labels
|
2002-09-15 01:05:33 +02:00
|
|
|
if (!isa<BasicBlock>(*UseI) && DEBUG_RA >= RA_DEBUG_LiveRanges)
|
2002-02-05 02:43:49 +01:00
|
|
|
cerr << " !! Warning: No LR for use " << RAV(*UseI) << "\n";
|
2001-09-14 23:18:34 +02:00
|
|
|
continue; // ignore and continue
|
|
|
|
}
|
|
|
|
|
2002-02-05 07:02:59 +01:00
|
|
|
if (LROfUse == LROfDef) // nothing to merge if they are same
|
2001-09-14 23:18:34 +02:00
|
|
|
continue;
|
|
|
|
|
2003-07-25 23:06:09 +02:00
|
|
|
if (MRI.getRegTypeForLR(LROfDef) ==
|
|
|
|
MRI.getRegTypeForLR(LROfUse)) {
|
2001-11-10 01:20:24 +01:00
|
|
|
// If the two RegTypes are the same
|
2002-02-05 04:52:29 +01:00
|
|
|
if (!RCOfDef->getInterference(LROfDef, LROfUse) ) {
|
2001-09-14 23:18:34 +02:00
|
|
|
|
|
|
|
unsigned CombinedDegree =
|
|
|
|
LROfDef->getUserIGNode()->getNumOfNeighbors() +
|
|
|
|
LROfUse->getUserIGNode()->getNumOfNeighbors();
|
|
|
|
|
2002-09-20 02:45:47 +02:00
|
|
|
if (CombinedDegree > RCOfDef->getNumOfAvailRegs()) {
|
|
|
|
// get more precise estimate of combined degree
|
|
|
|
CombinedDegree = LROfDef->getUserIGNode()->
|
|
|
|
getCombinedDegree(LROfUse->getUserIGNode());
|
|
|
|
}
|
|
|
|
|
2002-02-05 04:52:29 +01:00
|
|
|
if (CombinedDegree <= RCOfDef->getNumOfAvailRegs()) {
|
2003-05-31 09:32:01 +02:00
|
|
|
// if both LRs do not have different pre-assigned colors
|
|
|
|
// and both LRs do not have suggested colors
|
|
|
|
if (! InterfsPreventCoalescing(*LROfDef, *LROfUse)) {
|
2001-10-01 01:11:59 +02:00
|
|
|
RCOfDef->mergeIGNodesOfLRs(LROfDef, LROfUse);
|
|
|
|
unionAndUpdateLRs(LROfDef, LROfUse);
|
|
|
|
}
|
|
|
|
|
2001-09-14 23:18:34 +02:00
|
|
|
} // if combined degree is less than # of regs
|
|
|
|
} // if def and use do not interfere
|
2001-10-12 19:48:18 +02:00
|
|
|
}// if reg classes are the same
|
2001-09-14 23:18:34 +02:00
|
|
|
} // for all uses
|
|
|
|
} // if def
|
|
|
|
} // for all defs
|
|
|
|
} // for all machine instructions
|
|
|
|
} // for all BBs
|
|
|
|
|
2002-09-15 01:05:33 +02:00
|
|
|
if (DEBUG_RA >= RA_DEBUG_LiveRanges)
|
|
|
|
cerr << "\nCoalescing Done!\n";
|
2001-09-14 23:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------- Debug code for printing ---------------*/
|
|
|
|
|
|
|
|
|
2002-02-05 02:43:49 +01:00
|
|
|
void LiveRangeInfo::printLiveRanges() {
|
2001-09-14 23:18:34 +02:00
|
|
|
LiveRangeMapType::iterator HMI = LiveRangeMap.begin(); // hash map iterator
|
2002-01-20 23:54:45 +01:00
|
|
|
cerr << "\nPrinting Live Ranges from Hash Map:\n";
|
2002-02-05 02:43:49 +01:00
|
|
|
for( ; HMI != LiveRangeMap.end(); ++HMI) {
|
|
|
|
if (HMI->first && HMI->second) {
|
2002-09-15 01:05:33 +02:00
|
|
|
cerr << " Value* " << RAV(HMI->first) << "\t: ";
|
2002-09-20 02:45:47 +02:00
|
|
|
if (IGNode* igNode = HMI->second->getUserIGNode())
|
|
|
|
cerr << "LR# " << igNode->getIndex();
|
|
|
|
else
|
|
|
|
cerr << "LR# " << "<no-IGNode>";
|
2002-09-15 01:05:33 +02:00
|
|
|
cerr << "\t:Values = "; printSet(*HMI->second); cerr << "\n";
|
2001-09-14 23:18:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|