+//===-- LiveRangeInfo.cpp -------------------------------------------------===//
+//
+// Live range construction for coloring-based register allocation for LLVM.
+//
+//===----------------------------------------------------------------------===//
+
#include "llvm/CodeGen/LiveRangeInfo.h"
+#include "llvm/CodeGen/RegAllocCommon.h"
#include "llvm/CodeGen/RegClass.h"
#include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/MachineInstrInfo.h"
#include "llvm/Function.h"
-#include "llvm/BasicBlock.h"
#include "Support/SetOperations.h"
-#include <iostream>
using std::cerr;
LiveRangeInfo::LiveRangeInfo(const Function *F, const TargetMachine &tm,
}
+//---------------------------------------------------------------------------
+// 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
+ DefRange->setRegClass(RegClassList[MRI.getRegClassIDOfValue(Def, isCC)]);
+
+ 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) {
+ DefRange = createNewLiveRange(Def, isCC);
+ } 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;
+}
+
//---------------------------------------------------------------------------
// Method for constructing all live ranges in a function. It creates live
//---------------------------------------------------------------------------
void LiveRangeInfo::constructLiveRanges() {
- if (DEBUG_RA)
- cerr << "Consturcting Live Ranges ...\n";
+ if (DEBUG_RA >= RA_DEBUG_LiveRanges)
+ cerr << "Constructing Live Ranges ...\n";
// first find the live ranges for all incoming args of the function since
// those LRs start from the start of the function
-
- // get the argument list
- const Function::ArgumentListType& ArgList = Meth->getArgumentList();
-
- Function::ArgumentListType::const_iterator ArgIt = ArgList.begin();
- for( ; ArgIt != ArgList.end() ; ++ArgIt) { // for each argument
- LiveRange * ArgRange = new LiveRange(); // creates a new LR and
- const Value *Val = (const Value *) *ArgIt;
-
- ArgRange->insert(Val); // add the arg (def) to it
- LiveRangeMap[Val] = ArgRange;
-
- // create a temp machine op to find the register class of value
- //const MachineOperand Op(MachineOperand::MO_VirtualRegister);
-
- unsigned rcid = MRI.getRegClassIDOfValue( Val );
- ArgRange->setRegClass(RegClassList[ rcid ] );
-
-
- if( DEBUG_RA > 1) {
- cerr << " adding LiveRange for argument "
- << RAV((const Value *)*ArgIt) << "\n";
- }
- }
+ for (Function::const_aiterator AI = Meth->abegin(); AI != Meth->aend(); ++AI)
+ createNewLiveRange(AI, /*isCC*/ false);
// Now suggest hardware registers for these function args
MRI.suggestRegs4MethodArgs(Meth, *this);
-
- // Now find speical LLVM instructions (CALL, RET) and LRs in machine
- // instructions.
+ // 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.
//
- for (Function::const_iterator BBI = Meth->begin(); BBI != Meth->end(); ++BBI){
- // Now find all LRs for machine the 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.
-
- // get the iterator for machine instructions
- const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
+ MachineFunction &MF = MachineFunction::get(Meth);
+ for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
+ MachineBasicBlock &MBB = *BBI;
// iterate over all the machine instructions in BB
- for(MachineCodeForBasicBlock::const_iterator MInstIterator = MIVec.begin();
- MInstIterator != MIVec.end(); ++MInstIterator) {
- const MachineInstr *MInst = *MInstIterator;
-
- // Now if the machine instruction is a call/return instruction,
- // add it to CallRetInstrList for processing its implicit operands
+ for(MachineBasicBlock::iterator MInstIterator = MBB.begin();
+ MInstIterator != MBB.end(); ++MInstIterator) {
+ MachineInstr *MInst = *MInstIterator;
+ // If the machine instruction is a call/return instruction, add it to
+ // CallRetInstrList for processing its args, ret value, and ret addr.
+ //
if(TM.getInstrInfo().isReturn(MInst->getOpCode()) ||
TM.getInstrInfo().isCall(MInst->getOpCode()))
- CallRetInstrList.push_back( MInst );
+ CallRetInstrList.push_back(MInst);
-
- // iterate over MI operands to find defs
- for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
- OpE = MInst->end(); OpI != OpE; ++OpI) {
- if(DEBUG_RA) {
- MachineOperand::MachineOperandType OpTyp =
- OpI.getMachineOperand().getOperandType();
-
- if (OpTyp == MachineOperand::MO_CCRegister)
- cerr << "\n**CC reg found. Is Def=" << OpI.isDef() << " Val:"
- << RAV(OpI.getMachineOperand().getVRegValue()) << "\n";
- }
-
- // create a new LR iff this operand is a def
+ // iterate over explicit MI operands and create a new LR
+ // for each operand that is defined by the instruction
+ for (MachineInstr::val_op_iterator OpI = MInst->begin(),
+ OpE = MInst->end(); OpI != OpE; ++OpI)
if (OpI.isDef()) {
const Value *Def = *OpI;
+ bool isCC = (OpI.getMachineOperand().getType()
+ == MachineOperand::MO_CCRegister);
+ createOrAddToLiveRange(Def, isCC);
+ }
- // Only instruction values are accepted for live ranges here
- if (Def->getValueType() != Value::InstructionVal ) {
- cerr << "\n**%%Error: Def is not an instruction val. Def="
- << RAV(Def) << "\n";
- continue;
- }
-
- LiveRange *DefRange = LiveRangeMap[Def];
-
- // see LR already there (because of multiple defs)
- if( !DefRange) { // if it is not in LiveRangeMap
- DefRange = new LiveRange(); // creates a new live range and
- DefRange->insert(Def); // add the instruction (def) to it
- LiveRangeMap[ Def ] = DefRange; // update the map
-
- if (DEBUG_RA > 1)
- cerr << " creating a LR for def: " << RAV(Def) << "\n";
-
- // set the register class of the new live range
- //assert( RegClassList.size() );
- MachineOperand::MachineOperandType OpTy =
- OpI.getMachineOperand().getOperandType();
-
- bool isCC = ( OpTy == MachineOperand::MO_CCRegister);
- unsigned rcid = MRI.getRegClassIDOfValue(
- OpI.getMachineOperand().getVRegValue(), isCC );
-
-
- if (isCC && DEBUG_RA)
- cerr << "\a**created a LR for a CC reg:"
- << RAV(OpI.getMachineOperand().getVRegValue());
-
- DefRange->setRegClass(RegClassList[rcid]);
- } else {
- DefRange->insert(Def); // add the opearand to def range
- // update the map - Operand points
- // to the merged set
- LiveRangeMap[Def] = DefRange;
-
- if (DEBUG_RA > 1)
- cerr << " added to an existing LR for def: "
- << RAV(Def) << "\n";
- }
-
- } // if isDef()
-
- } // for all opereands in machine instructions
+ // 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)
+ if (MInst->implicitRefIsDefined(i)) {
+ const Value *Def = MInst->getImplicitRef(i);
+ createOrAddToLiveRange(Def, /*isCC*/ false);
+ }
} // for all machine instructions in the BB
} // for all BBs in function
-
// 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
-
+ //
suggestRegs4CallRets();
- if( DEBUG_RA)
+ if( DEBUG_RA >= RA_DEBUG_LiveRanges)
cerr << "Initial Live Ranges constructed!\n";
-
}
//---------------------------------------------------------------------------
void LiveRangeInfo::suggestRegs4CallRets()
{
- CallRetInstrListType::const_iterator It = CallRetInstrList.begin();
+ CallRetInstrListType::iterator It = CallRetInstrList.begin();
for( ; It != CallRetInstrList.end(); ++It ) {
- const MachineInstr *MInst = *It;
+ MachineInstr *MInst = *It;
MachineOpCode OpCode = MInst->getOpCode();
if( (TM.getInstrInfo()).isReturn(OpCode) )
MRI.suggestReg4RetValue( MInst, *this);
else if( (TM.getInstrInfo()).isCall( OpCode ) )
- MRI.suggestRegs4CallArgs( MInst, *this, RegClassList );
+ MRI.suggestRegs4CallArgs( MInst, *this);
else
assert( 0 && "Non call/ret instr in CallRetInstrList" );
//---------------------------------------------------------------------------
void LiveRangeInfo::coalesceLRs()
{
- if(DEBUG_RA)
- cerr << "\nCoalscing LRs ...\n";
-
- for(Function::const_iterator BBI = Meth->begin(), BBE = Meth->end();
- BBI != BBE; ++BBI) {
+ if(DEBUG_RA >= RA_DEBUG_LiveRanges)
+ cerr << "\nCoalescing LRs ...\n";
- // get the iterator for machine instructions
- const MachineCodeForBasicBlock& MIVec = (*BBI)->getMachineInstrVec();
- MachineCodeForBasicBlock::const_iterator MInstIterator = MIVec.begin();
+ MachineFunction &MF = MachineFunction::get(Meth);
+ for (MachineFunction::iterator BBI = MF.begin(); BBI != MF.end(); ++BBI) {
+ MachineBasicBlock &MBB = *BBI;
// iterate over all the machine instructions in BB
- for( ; MInstIterator != MIVec.end(); ++MInstIterator) {
-
- const MachineInstr * MInst = *MInstIterator;
+ for(MachineBasicBlock::iterator MII = MBB.begin(); MII != MBB.end(); ++MII){
+ const MachineInstr *MI = *MII;
- if( DEBUG_RA > 1) {
+ if( DEBUG_RA >= RA_DEBUG_LiveRanges) {
cerr << " *Iterating over machine instr ";
- MInst->dump();
+ MI->dump();
cerr << "\n";
}
-
// iterate over MI operands to find defs
- for(MachineInstr::const_val_op_iterator DefI = MInst->begin(),
- DefE = MInst->end(); DefI != DefE; ++DefI) {
+ for(MachineInstr::const_val_op_iterator DefI = MI->begin(),
+ DefE = MI->end(); DefI != DefE; ++DefI) {
if (DefI.isDef()) { // iff this operand is a def
LiveRange *LROfDef = getLiveRangeForValue( *DefI );
RegClass *RCOfDef = LROfDef->getRegClass();
- MachineInstr::const_val_op_iterator UseI = MInst->begin(),
- UseE = MInst->end();
- for( ; UseI != UseE; ++UseI){ // for all uses
-
+ MachineInstr::const_val_op_iterator UseI = MI->begin(),
+ UseE = MI->end();
+ for( ; UseI != UseE; ++UseI) { // for all uses
LiveRange *LROfUse = getLiveRangeForValue( *UseI );
if (!LROfUse) { // if LR of use is not found
//don't warn about labels
- if (!isa<BasicBlock>(*UseI) && DEBUG_RA)
+ if (!isa<BasicBlock>(*UseI) && DEBUG_RA >= RA_DEBUG_LiveRanges)
cerr << " !! Warning: No LR for use " << RAV(*UseI) << "\n";
continue; // ignore and continue
}
LROfDef->getUserIGNode()->getNumOfNeighbors() +
LROfUse->getUserIGNode()->getNumOfNeighbors();
+ if (CombinedDegree > RCOfDef->getNumOfAvailRegs()) {
+ // get more precise estimate of combined degree
+ CombinedDegree = LROfDef->getUserIGNode()->
+ getCombinedDegree(LROfUse->getUserIGNode());
+ }
+
if (CombinedDegree <= RCOfDef->getNumOfAvailRegs()) {
// if both LRs do not have suggested colors
if (!(LROfDef->hasSuggestedColor() &&
} // for all machine instructions
} // for all BBs
- if (DEBUG_RA)
- cerr << "\nCoalscing Done!\n";
+ if (DEBUG_RA >= RA_DEBUG_LiveRanges)
+ cerr << "\nCoalescing Done!\n";
}
cerr << "\nPrinting Live Ranges from Hash Map:\n";
for( ; HMI != LiveRangeMap.end(); ++HMI) {
if (HMI->first && HMI->second) {
- cerr << " " << RAV(HMI->first) << "\t: ";
- printSet(*HMI->second); cerr << "\n";
+ cerr << " Value* " << RAV(HMI->first) << "\t: ";
+ if (IGNode* igNode = HMI->second->getUserIGNode())
+ cerr << "LR# " << igNode->getIndex();
+ else
+ cerr << "LR# " << "<no-IGNode>";
+ cerr << "\t:Values = "; printSet(*HMI->second); cerr << "\n";
}
}
}