//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
//
// This register allocator allocates registers to a basic block at a time,
// attempting to keep values in registers and reusing registers as appropriate.
const MRegisterInfo *RegInfo;
LiveVariables *LV;
- // StackSlotForVirtReg - Maps SSA Regs => frame index where these values are
- // spilled
+ // StackSlotForVirtReg - Maps virtual regs to the frame index where these
+ // values are spilled.
std::map<unsigned, int> StackSlotForVirtReg;
// Virt2PhysRegMap - This map contains entries for each virtual register
///
bool areRegsEqual(unsigned R1, unsigned R2) const {
if (R1 == R2) return true;
- if (const unsigned *AliasSet = RegInfo->getAliasSet(R2))
- for (unsigned i = 0; AliasSet[i]; ++i)
- if (AliasSet[i] == R1) return true;
+ for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
+ *AliasSet; ++AliasSet) {
+ if (*AliasSet == R1) return true;
+ }
return false;
}
/// getStackSpaceFor - This returns the frame index of the specified virtual
- /// register on the stack, allocating space if neccesary.
+ /// register on the stack, allocating space if necessary.
int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
+ /// removePhysReg - This method marks the specified physical register as no
+ /// longer being in use.
+ ///
void removePhysReg(unsigned PhysReg);
/// spillVirtReg - This method spills the value specified by PhysReg into
unsigned VirtReg, unsigned PhysReg);
/// spillPhysReg - This method spills the specified physical register into
- /// the virtual register slot associated with it.
+ /// the virtual register slot associated with it. If OnlyVirtRegs is set to
+ /// true, then the request is ignored if the physical register does not
+ /// contain a virtual register.
///
void spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
- unsigned PhysReg);
+ unsigned PhysReg, bool OnlyVirtRegs = false);
/// assignVirtToPhysReg - This method updates local state so that we know
/// that PhysReg is the proper container for VirtReg now. The physical
///
unsigned reloadVirtReg(MachineBasicBlock &MBB,
MachineBasicBlock::iterator &I, unsigned VirtReg);
+
+ void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
+ unsigned PhysReg);
};
}
-/// getStackSpaceFor - This allocates space for the specified virtual
-/// register to be held on the stack.
-int RA::getStackSpaceFor(unsigned VirtReg,
- const TargetRegisterClass *RC) {
- // Find the location VirtReg would belong...
- std::map<unsigned, int>::iterator I =
- StackSlotForVirtReg.lower_bound(VirtReg);
+/// getStackSpaceFor - This allocates space for the specified virtual register
+/// to be held on the stack.
+int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
+ // Find the location Reg would belong...
+ std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
return I->second; // Already has space allocated?
///
void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
unsigned VirtReg, unsigned PhysReg) {
- // If this is just a marker register, we don't need to spill it.
- if (VirtReg != 0) {
- const TargetRegisterClass *RegClass =
- MF->getSSARegMap()->getRegClass(VirtReg);
- int FrameIndex = getStackSpaceFor(VirtReg, RegClass);
-
- // If we need to spill this value, do so now...
- if (isVirtRegModified(VirtReg)) {
- // Add move instruction(s)
- RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RegClass);
- ++NumSpilled; // Update statistics
- }
- Virt2PhysRegMap.erase(VirtReg); // VirtReg no longer available
+ if (!VirtReg && DisableKill) return;
+ assert(VirtReg && "Spilling a physical register is illegal!"
+ " Must not have appropriate kill for the register or use exists beyond"
+ " the intended one.");
+ DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
+ std::cerr << " containing %reg" << VirtReg;
+ if (!isVirtRegModified(VirtReg))
+ std::cerr << " which has not been modified, so no store necessary!");
+
+ // Otherwise, there is a virtual register corresponding to this physical
+ // register. We only need to spill it into its stack slot if it has been
+ // modified.
+ if (isVirtRegModified(VirtReg)) {
+ const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
+ int FrameIndex = getStackSpaceFor(VirtReg, RC);
+ DEBUG(std::cerr << " to stack slot #" << FrameIndex);
+ RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
+ ++NumSpilled; // Update statistics
}
+ Virt2PhysRegMap.erase(VirtReg); // VirtReg no longer available
+ DEBUG(std::cerr << "\n");
removePhysReg(PhysReg);
}
/// spillPhysReg - This method spills the specified physical register into the
-/// virtual register slot associated with it.
+/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
+/// then the request is ignored if the physical register does not contain a
+/// virtual register.
///
void RA::spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
- unsigned PhysReg) {
+ unsigned PhysReg, bool OnlyVirtRegs) {
std::map<unsigned, unsigned>::iterator PI = PhysRegsUsed.find(PhysReg);
if (PI != PhysRegsUsed.end()) { // Only spill it if it's used!
- spillVirtReg(MBB, I, PI->second, PhysReg);
- } else if (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg)) {
+ if (PI->second || !OnlyVirtRegs)
+ spillVirtReg(MBB, I, PI->second, PhysReg);
+ } else {
// If the selected register aliases any other registers, we must make
// sure that one of the aliases isn't alive...
- for (unsigned i = 0; AliasSet[i]; ++i) {
- PI = PhysRegsUsed.find(AliasSet[i]);
+ for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
+ *AliasSet; ++AliasSet) {
+ PI = PhysRegsUsed.find(*AliasSet);
if (PI != PhysRegsUsed.end()) // Spill aliased register...
- spillVirtReg(MBB, I, PI->second, AliasSet[i]);
+ if (PI->second || !OnlyVirtRegs)
+ spillVirtReg(MBB, I, PI->second, *AliasSet);
}
}
}
// If the selected register aliases any other allocated registers, it is
// not free!
- if (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg))
- for (unsigned i = 0; AliasSet[i]; ++i)
- if (PhysRegsUsed.count(AliasSet[i])) // Aliased register in use?
- return false; // Can't use this reg then.
+ for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
+ *AliasSet; ++AliasSet)
+ if (PhysRegsUsed.count(*AliasSet)) // Aliased register in use?
+ return false; // Can't use this reg then.
return true;
}
"Couldn't find a register of the appropriate class!");
unsigned R = PhysRegsUseOrder[i];
- // If the current register is compatible, use it.
- if (RegInfo->getRegClass(R) == RC) {
- PhysReg = R;
- break;
- } else {
- // If one of the registers aliased to the current register is
- // compatible, use it.
- if (const unsigned *AliasSet = RegInfo->getAliasSet(R))
- for (unsigned a = 0; AliasSet[a]; ++a)
- if (RegInfo->getRegClass(AliasSet[a]) == RC) {
- PhysReg = AliasSet[a]; // Take an aliased register
- break;
- }
+
+ // We can only use this register if it holds a virtual register (ie, it
+ // can be spilled). Do not use it if it is an explicitly allocated
+ // physical register!
+ assert(PhysRegsUsed.count(R) &&
+ "PhysReg in PhysRegsUseOrder, but is not allocated?");
+ if (PhysRegsUsed[R]) {
+ // If the current register is compatible, use it.
+ if (RegInfo->getRegClass(R) == RC) {
+ PhysReg = R;
+ break;
+ } else {
+ // If one of the registers aliased to the current register is
+ // compatible, use it.
+ for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
+ *AliasSet; ++AliasSet) {
+ if (RegInfo->getRegClass(*AliasSet) == RC) {
+ PhysReg = *AliasSet; // Take an aliased register
+ break;
+ }
+ }
+ }
}
}
markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
+ DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
+ << RegInfo->getName(PhysReg) << "\n");
+
// Add move instruction(s)
RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIndex, RC);
++NumReloaded; // Update statistics
return PhysReg;
}
+
+
void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
// loop over each instruction
MachineBasicBlock::iterator I = MBB.begin();
for (; I != MBB.end(); ++I) {
MachineInstr *MI = *I;
const TargetInstrDescriptor &TID = TM->getInstrInfo().get(MI->getOpcode());
+ DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
+ std::cerr << " Regs have values: ";
+ for (std::map<unsigned, unsigned>::const_iterator
+ I = PhysRegsUsed.begin(), E = PhysRegsUsed.end(); I != E; ++I)
+ std::cerr << "[" << RegInfo->getName(I->first)
+ << ",%reg" << I->second << "] ";
+ std::cerr << "\n");
// Loop over the implicit uses, making sure that they are at the head of the
// use order list, so they don't get reallocated.
- if (const unsigned *ImplicitUses = TID.ImplicitUses)
- for (unsigned i = 0; ImplicitUses[i]; ++i)
- MarkPhysRegRecentlyUsed(ImplicitUses[i]);
-
- // Get the used operands into registers. This has the potiential to spill
- // incoming values if we are out of registers.
+ for (const unsigned *ImplicitUses = TID.ImplicitUses;
+ *ImplicitUses; ++ImplicitUses)
+ MarkPhysRegRecentlyUsed(*ImplicitUses);
+
+ // Get the used operands into registers. This has the potential to spill
+ // incoming values if we are out of registers. Note that we completely
+ // ignore physical register uses here. We assume that if an explicit
+ // physical register is referenced by the instruction, that it is guaranteed
+ // to be live-in, or the input is badly hosed.
//
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
- if (MI->getOperand(i).opIsUse() &&
- MI->getOperand(i).isVirtualRegister()) {
+ if (MI->getOperand(i).opIsUse() && MI->getOperand(i).isVirtualRegister()){
unsigned VirtSrcReg = MI->getOperand(i).getAllocatedRegNum();
unsigned PhysSrcReg = reloadVirtReg(MBB, I, VirtSrcReg);
MI->SetMachineOperandReg(i, PhysSrcReg); // Assign the input register
}
if (PhysReg) {
- DEBUG(std::cerr << "V: " << VirtReg << " P: " << PhysReg
- << " Killed by: " << *MI);
+ DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
+ << "[%reg" << VirtReg <<"], removing it from live set\n");
removePhysReg(PhysReg);
}
}
MI->getOperand(i).opIsDefAndUse()) &&
MI->getOperand(i).isPhysicalRegister()) {
unsigned Reg = MI->getOperand(i).getAllocatedRegNum();
- spillPhysReg(MBB, I, Reg); // Spill any existing value in the reg
+ spillPhysReg(MBB, I, Reg, true); // Spill any existing value in the reg
PhysRegsUsed[Reg] = 0; // It is free and reserved now
PhysRegsUseOrder.push_back(Reg);
}
}
if (PhysReg) {
- DEBUG(std::cerr << "V: " << VirtReg << " P: " << PhysReg
- << " dead after: " << *MI);
+ DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
+ << " [%reg" << VirtReg
+ << "] is never used, removing it frame live list\n");
removePhysReg(PhysReg);
}
}
// Spill all physical registers holding virtual registers now.
while (!PhysRegsUsed.empty())
- spillVirtReg(MBB, I, PhysRegsUsed.begin()->second,
- PhysRegsUsed.begin()->first);
+ if (unsigned VirtReg = PhysRegsUsed.begin()->second)
+ spillVirtReg(MBB, I, VirtReg, PhysRegsUsed.begin()->first);
+ else
+ removePhysReg(PhysRegsUsed.begin()->first);
for (std::map<unsigned, unsigned>::iterator I = Virt2PhysRegMap.begin(),
E = Virt2PhysRegMap.end(); I != E; ++I)
<< I->second << "\n";
assert(Virt2PhysRegMap.empty() && "Virtual registers still in phys regs?");
- assert(PhysRegsUseOrder.empty() && "Physical regs still allocated?");
+
+ // Clear any physical register which appear live at the end of the basic
+ // block, but which do not hold any virtual registers. e.g., the stack
+ // pointer.
+ PhysRegsUseOrder.clear();
}
return true;
}
-Pass *createLocalRegisterAllocator() {
+FunctionPass *createLocalRegisterAllocator() {
return new RA();
}