1 //===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This register allocator allocates registers to a basic block at a time,
11 // attempting to keep values in registers and reusing registers as appropriate.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "regalloc"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/SSARegMap.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/LiveVariables.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "Support/CommandLine.h"
25 #include "Support/Debug.h"
26 #include "Support/Statistic.h"
30 Statistic<> NumSpilled ("ra-local", "Number of registers spilled");
31 Statistic<> NumReloaded("ra-local", "Number of registers reloaded");
32 cl::opt<bool> DisableKill("disable-kill", cl::Hidden,
33 cl::desc("Disable register kill in local-ra"));
35 class RA : public MachineFunctionPass {
36 const TargetMachine *TM;
38 const MRegisterInfo *RegInfo;
41 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
42 // values are spilled.
43 std::map<unsigned, int> StackSlotForVirtReg;
45 // Virt2PhysRegMap - This map contains entries for each virtual register
46 // that is currently available in a physical register.
48 std::map<unsigned, unsigned> Virt2PhysRegMap;
50 // PhysRegsUsed - This map contains entries for each physical register that
51 // currently has a value (ie, it is in Virt2PhysRegMap). The value mapped
52 // to is the virtual register corresponding to the physical register (the
53 // inverse of the Virt2PhysRegMap), or 0. The value is set to 0 if this
54 // register is pinned because it is used by a future instruction.
56 std::map<unsigned, unsigned> PhysRegsUsed;
58 // PhysRegsUseOrder - This contains a list of the physical registers that
59 // currently have a virtual register value in them. This list provides an
60 // ordering of registers, imposing a reallocation order. This list is only
61 // used if all registers are allocated and we have to spill one, in which
62 // case we spill the least recently used register. Entries at the front of
63 // the list are the least recently used registers, entries at the back are
64 // the most recently used.
66 std::vector<unsigned> PhysRegsUseOrder;
68 // VirtRegModified - This bitset contains information about which virtual
69 // registers need to be spilled back to memory when their registers are
70 // scavenged. If a virtual register has simply been rematerialized, there
71 // is no reason to spill it to memory when we need the register back.
73 std::vector<bool> VirtRegModified;
75 void markVirtRegModified(unsigned Reg, bool Val = true) {
76 assert(Reg >= MRegisterInfo::FirstVirtualRegister && "Illegal VirtReg!");
77 Reg -= MRegisterInfo::FirstVirtualRegister;
78 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
79 VirtRegModified[Reg] = Val;
82 bool isVirtRegModified(unsigned Reg) const {
83 assert(Reg >= MRegisterInfo::FirstVirtualRegister && "Illegal VirtReg!");
84 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
85 && "Illegal virtual register!");
86 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
89 void MarkPhysRegRecentlyUsed(unsigned Reg) {
90 assert(!PhysRegsUseOrder.empty() && "No registers used!");
91 if (PhysRegsUseOrder.back() == Reg) return; // Already most recently used
93 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
94 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
95 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
96 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
97 // Add it to the end of the list
98 PhysRegsUseOrder.push_back(RegMatch);
100 return; // Found an exact match, exit early
105 virtual const char *getPassName() const {
106 return "Local Register Allocator";
109 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
111 AU.addRequired<LiveVariables>();
112 AU.addRequiredID(PHIEliminationID);
113 MachineFunctionPass::getAnalysisUsage(AU);
117 /// runOnMachineFunction - Register allocate the whole function
118 bool runOnMachineFunction(MachineFunction &Fn);
120 /// AllocateBasicBlock - Register allocate the specified basic block.
121 void AllocateBasicBlock(MachineBasicBlock &MBB);
124 /// areRegsEqual - This method returns true if the specified registers are
125 /// related to each other. To do this, it checks to see if they are equal
126 /// or if the first register is in the alias set of the second register.
128 bool areRegsEqual(unsigned R1, unsigned R2) const {
129 if (R1 == R2) return true;
130 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
131 *AliasSet; ++AliasSet) {
132 if (*AliasSet == R1) return true;
137 /// getStackSpaceFor - This returns the frame index of the specified virtual
138 /// register on the stack, allocating space if necessary.
139 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
141 /// removePhysReg - This method marks the specified physical register as no
142 /// longer being in use.
144 void removePhysReg(unsigned PhysReg);
146 /// spillVirtReg - This method spills the value specified by PhysReg into
147 /// the virtual register slot specified by VirtReg. It then updates the RA
148 /// data structures to indicate the fact that PhysReg is now available.
150 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
151 unsigned VirtReg, unsigned PhysReg);
153 /// spillPhysReg - This method spills the specified physical register into
154 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
155 /// true, then the request is ignored if the physical register does not
156 /// contain a virtual register.
158 void spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
159 unsigned PhysReg, bool OnlyVirtRegs = false);
161 /// assignVirtToPhysReg - This method updates local state so that we know
162 /// that PhysReg is the proper container for VirtReg now. The physical
163 /// register must not be used for anything else when this is called.
165 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
167 /// liberatePhysReg - Make sure the specified physical register is available
168 /// for use. If there is currently a value in it, it is either moved out of
169 /// the way or spilled to memory.
171 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
174 /// isPhysRegAvailable - Return true if the specified physical register is
175 /// free and available for use. This also includes checking to see if
176 /// aliased registers are all free...
178 bool isPhysRegAvailable(unsigned PhysReg) const;
180 /// getFreeReg - Look to see if there is a free register available in the
181 /// specified register class. If not, return 0.
183 unsigned getFreeReg(const TargetRegisterClass *RC);
185 /// getReg - Find a physical register to hold the specified virtual
186 /// register. If all compatible physical registers are used, this method
187 /// spills the last used virtual register to the stack, and uses that
190 unsigned getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
193 /// reloadVirtReg - This method loads the specified virtual register into a
194 /// physical register, returning the physical register chosen. This updates
195 /// the regalloc data structures to reflect the fact that the virtual reg is
196 /// now alive in a physical register, and the previous one isn't.
198 unsigned reloadVirtReg(MachineBasicBlock &MBB,
199 MachineBasicBlock::iterator &I, unsigned VirtReg);
201 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
207 /// getStackSpaceFor - This allocates space for the specified virtual register
208 /// to be held on the stack.
209 int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
210 // Find the location Reg would belong...
211 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
213 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
214 return I->second; // Already has space allocated?
216 // Allocate a new stack object for this spill location...
217 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
219 // Assign the slot...
220 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
225 /// removePhysReg - This method marks the specified physical register as no
226 /// longer being in use.
228 void RA::removePhysReg(unsigned PhysReg) {
229 PhysRegsUsed.erase(PhysReg); // PhyReg no longer used
231 std::vector<unsigned>::iterator It =
232 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
233 assert(It != PhysRegsUseOrder.end() &&
234 "Spilled a physical register, but it was not in use list!");
235 PhysRegsUseOrder.erase(It);
239 /// spillVirtReg - This method spills the value specified by PhysReg into the
240 /// virtual register slot specified by VirtReg. It then updates the RA data
241 /// structures to indicate the fact that PhysReg is now available.
243 void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
244 unsigned VirtReg, unsigned PhysReg) {
245 if (!VirtReg && DisableKill) return;
246 assert(VirtReg && "Spilling a physical register is illegal!"
247 " Must not have appropriate kill for the register or use exists beyond"
248 " the intended one.");
249 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
250 std::cerr << " containing %reg" << VirtReg;
251 if (!isVirtRegModified(VirtReg))
252 std::cerr << " which has not been modified, so no store necessary!");
254 // Otherwise, there is a virtual register corresponding to this physical
255 // register. We only need to spill it into its stack slot if it has been
257 if (isVirtRegModified(VirtReg)) {
258 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
259 int FrameIndex = getStackSpaceFor(VirtReg, RC);
260 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
261 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
262 ++NumSpilled; // Update statistics
264 Virt2PhysRegMap.erase(VirtReg); // VirtReg no longer available
266 DEBUG(std::cerr << "\n");
267 removePhysReg(PhysReg);
271 /// spillPhysReg - This method spills the specified physical register into the
272 /// virtual register slot associated with it. If OnlyVirtRegs is set to true,
273 /// then the request is ignored if the physical register does not contain a
274 /// virtual register.
276 void RA::spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
277 unsigned PhysReg, bool OnlyVirtRegs) {
278 std::map<unsigned, unsigned>::iterator PI = PhysRegsUsed.find(PhysReg);
279 if (PI != PhysRegsUsed.end()) { // Only spill it if it's used!
280 if (PI->second || !OnlyVirtRegs)
281 spillVirtReg(MBB, I, PI->second, PhysReg);
283 // If the selected register aliases any other registers, we must make
284 // sure that one of the aliases isn't alive...
285 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
286 *AliasSet; ++AliasSet) {
287 PI = PhysRegsUsed.find(*AliasSet);
288 if (PI != PhysRegsUsed.end()) // Spill aliased register...
289 if (PI->second || !OnlyVirtRegs)
290 spillVirtReg(MBB, I, PI->second, *AliasSet);
296 /// assignVirtToPhysReg - This method updates local state so that we know
297 /// that PhysReg is the proper container for VirtReg now. The physical
298 /// register must not be used for anything else when this is called.
300 void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
301 assert(PhysRegsUsed.find(PhysReg) == PhysRegsUsed.end() &&
302 "Phys reg already assigned!");
303 // Update information to note the fact that this register was just used, and
305 PhysRegsUsed[PhysReg] = VirtReg;
306 Virt2PhysRegMap[VirtReg] = PhysReg;
307 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
311 /// isPhysRegAvailable - Return true if the specified physical register is free
312 /// and available for use. This also includes checking to see if aliased
313 /// registers are all free...
315 bool RA::isPhysRegAvailable(unsigned PhysReg) const {
316 if (PhysRegsUsed.count(PhysReg)) return false;
318 // If the selected register aliases any other allocated registers, it is
320 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
321 *AliasSet; ++AliasSet)
322 if (PhysRegsUsed.count(*AliasSet)) // Aliased register in use?
323 return false; // Can't use this reg then.
328 /// getFreeReg - Look to see if there is a free register available in the
329 /// specified register class. If not, return 0.
331 unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
332 // Get iterators defining the range of registers that are valid to allocate in
333 // this class, which also specifies the preferred allocation order.
334 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
335 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
337 for (; RI != RE; ++RI)
338 if (isPhysRegAvailable(*RI)) { // Is reg unused?
339 assert(*RI != 0 && "Cannot use register!");
340 return *RI; // Found an unused register!
346 /// liberatePhysReg - Make sure the specified physical register is available for
347 /// use. If there is currently a value in it, it is either moved out of the way
348 /// or spilled to memory.
350 void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
352 // FIXME: This code checks to see if a register is available, but it really
353 // wants to know if a reg is available BEFORE the instruction executes. If
354 // called after killed operands are freed, it runs the risk of reallocating a
357 if (isPhysRegAvailable(PhysReg)) return; // Already available...
359 // Check to see if the register is directly used, not indirectly used through
360 // aliases. If aliased registers are the ones actually used, we cannot be
361 // sure that we will be able to save the whole thing if we do a reg-reg copy.
362 std::map<unsigned, unsigned>::iterator PRUI = PhysRegsUsed.find(PhysReg);
363 if (PRUI != PhysRegsUsed.end()) {
364 unsigned VirtReg = PRUI->second; // The virtual register held...
366 // Check to see if there is a compatible register available. If so, we can
367 // move the value into the new register...
369 const TargetRegisterClass *RC = RegInfo->getRegClass(PhysReg);
370 if (unsigned NewReg = getFreeReg(RC)) {
371 // Emit the code to copy the value...
372 RegInfo->copyRegToReg(MBB, I, NewReg, PhysReg, RC);
374 // Update our internal state to indicate that PhysReg is available and Reg
376 Virt2PhysRegMap.erase(VirtReg);
377 removePhysReg(PhysReg); // Free the physreg
379 // Move reference over to new register...
380 assignVirtToPhysReg(VirtReg, NewReg);
385 spillPhysReg(MBB, I, PhysReg);
389 /// getReg - Find a physical register to hold the specified virtual
390 /// register. If all compatible physical registers are used, this method spills
391 /// the last used virtual register to the stack, and uses that register.
393 unsigned RA::getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
395 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
397 // First check to see if we have a free register of the requested type...
398 unsigned PhysReg = getFreeReg(RC);
400 // If we didn't find an unused register, scavenge one now!
402 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
404 // Loop over all of the preallocated registers from the least recently used
405 // to the most recently used. When we find one that is capable of holding
406 // our register, use it.
407 for (unsigned i = 0; PhysReg == 0; ++i) {
408 assert(i != PhysRegsUseOrder.size() &&
409 "Couldn't find a register of the appropriate class!");
411 unsigned R = PhysRegsUseOrder[i];
413 // We can only use this register if it holds a virtual register (ie, it
414 // can be spilled). Do not use it if it is an explicitly allocated
415 // physical register!
416 assert(PhysRegsUsed.count(R) &&
417 "PhysReg in PhysRegsUseOrder, but is not allocated?");
418 if (PhysRegsUsed[R]) {
419 // If the current register is compatible, use it.
420 if (RegInfo->getRegClass(R) == RC) {
424 // If one of the registers aliased to the current register is
425 // compatible, use it.
426 for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
427 *AliasSet; ++AliasSet) {
428 if (RegInfo->getRegClass(*AliasSet) == RC) {
429 PhysReg = *AliasSet; // Take an aliased register
437 assert(PhysReg && "Physical register not assigned!?!?");
439 // At this point PhysRegsUseOrder[i] is the least recently used register of
440 // compatible register class. Spill it to memory and reap its remains.
441 spillPhysReg(MBB, I, PhysReg);
444 // Now that we know which register we need to assign this to, do it now!
445 assignVirtToPhysReg(VirtReg, PhysReg);
450 /// reloadVirtReg - This method loads the specified virtual register into a
451 /// physical register, returning the physical register chosen. This updates the
452 /// regalloc data structures to reflect the fact that the virtual reg is now
453 /// alive in a physical register, and the previous one isn't.
455 unsigned RA::reloadVirtReg(MachineBasicBlock &MBB,
456 MachineBasicBlock::iterator &I,
458 std::map<unsigned, unsigned>::iterator It = Virt2PhysRegMap.find(VirtReg);
459 if (It != Virt2PhysRegMap.end()) {
460 MarkPhysRegRecentlyUsed(It->second);
461 return It->second; // Already have this value available!
464 unsigned PhysReg = getReg(MBB, I, VirtReg);
466 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
467 int FrameIndex = getStackSpaceFor(VirtReg, RC);
469 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
471 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
472 << RegInfo->getName(PhysReg) << "\n");
474 // Add move instruction(s)
475 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIndex, RC);
476 ++NumReloaded; // Update statistics
482 void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
483 // loop over each instruction
484 MachineBasicBlock::iterator I = MBB.begin();
485 for (; I != MBB.end(); ++I) {
486 MachineInstr *MI = *I;
487 const TargetInstrDescriptor &TID = TM->getInstrInfo().get(MI->getOpcode());
488 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
489 std::cerr << " Regs have values: ";
490 for (std::map<unsigned, unsigned>::const_iterator
491 I = PhysRegsUsed.begin(), E = PhysRegsUsed.end(); I != E; ++I)
492 std::cerr << "[" << RegInfo->getName(I->first)
493 << ",%reg" << I->second << "] ";
496 // Loop over the implicit uses, making sure that they are at the head of the
497 // use order list, so they don't get reallocated.
498 for (const unsigned *ImplicitUses = TID.ImplicitUses;
499 *ImplicitUses; ++ImplicitUses)
500 MarkPhysRegRecentlyUsed(*ImplicitUses);
502 // Get the used operands into registers. This has the potential to spill
503 // incoming values if we are out of registers. Note that we completely
504 // ignore physical register uses here. We assume that if an explicit
505 // physical register is referenced by the instruction, that it is guaranteed
506 // to be live-in, or the input is badly hosed.
508 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
509 if (MI->getOperand(i).opIsUse() && MI->getOperand(i).isVirtualRegister()){
510 unsigned VirtSrcReg = MI->getOperand(i).getAllocatedRegNum();
511 unsigned PhysSrcReg = reloadVirtReg(MBB, I, VirtSrcReg);
512 MI->SetMachineOperandReg(i, PhysSrcReg); // Assign the input register
516 // If this instruction is the last user of anything in registers, kill the
517 // value, freeing the register being used, so it doesn't need to be
518 // spilled to memory.
520 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
521 KE = LV->killed_end(MI); KI != KE; ++KI) {
522 unsigned VirtReg = KI->second;
523 unsigned PhysReg = VirtReg;
524 if (VirtReg >= MRegisterInfo::FirstVirtualRegister) {
525 std::map<unsigned, unsigned>::iterator I =
526 Virt2PhysRegMap.find(VirtReg);
527 assert(I != Virt2PhysRegMap.end());
529 Virt2PhysRegMap.erase(I);
533 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
534 << "[%reg" << VirtReg <<"], removing it from live set\n");
535 removePhysReg(PhysReg);
540 // Loop over all of the operands of the instruction, spilling registers that
541 // are defined, and marking explicit destinations in the PhysRegsUsed map.
542 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
543 if ((MI->getOperand(i).opIsDefOnly() ||
544 MI->getOperand(i).opIsDefAndUse()) &&
545 MI->getOperand(i).isPhysicalRegister()) {
546 unsigned Reg = MI->getOperand(i).getAllocatedRegNum();
547 spillPhysReg(MBB, I, Reg, true); // Spill any existing value in the reg
548 PhysRegsUsed[Reg] = 0; // It is free and reserved now
549 PhysRegsUseOrder.push_back(Reg);
552 // Loop over the implicit defs, spilling them as well.
553 if (const unsigned *ImplicitDefs = TID.ImplicitDefs)
554 for (unsigned i = 0; ImplicitDefs[i]; ++i) {
555 unsigned Reg = ImplicitDefs[i];
556 spillPhysReg(MBB, I, Reg);
557 PhysRegsUseOrder.push_back(Reg);
558 PhysRegsUsed[Reg] = 0; // It is free and reserved now
561 // Okay, we have allocated all of the source operands and spilled any values
562 // that would be destroyed by defs of this instruction. Loop over the
563 // implicit defs and assign them to a register, spilling incoming values if
564 // we need to scavenge a register.
566 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
567 if ((MI->getOperand(i).opIsDefOnly() || MI->getOperand(i).opIsDefAndUse())
568 && MI->getOperand(i).isVirtualRegister()) {
569 unsigned DestVirtReg = MI->getOperand(i).getAllocatedRegNum();
570 unsigned DestPhysReg;
572 // If DestVirtReg already has a value, forget about it. Why doesn't
573 // getReg do this right?
574 std::map<unsigned, unsigned>::iterator DestI =
575 Virt2PhysRegMap.find(DestVirtReg);
576 if (DestI != Virt2PhysRegMap.end()) {
577 unsigned PhysReg = DestI->second;
578 Virt2PhysRegMap.erase(DestI);
579 removePhysReg(PhysReg);
582 if (TM->getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
583 // must be same register number as the first operand
584 // This maps a = b + c into b += c, and saves b into a's spot
585 assert(MI->getOperand(1).isRegister() &&
586 MI->getOperand(1).getAllocatedRegNum() &&
587 MI->getOperand(1).opIsUse() &&
588 "Two address instruction invalid!");
589 DestPhysReg = MI->getOperand(1).getAllocatedRegNum();
591 liberatePhysReg(MBB, I, DestPhysReg);
592 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
594 DestPhysReg = getReg(MBB, I, DestVirtReg);
596 markVirtRegModified(DestVirtReg);
597 MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register
601 // If this instruction defines any registers that are immediately dead,
604 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
605 KE = LV->dead_end(MI); KI != KE; ++KI) {
606 unsigned VirtReg = KI->second;
607 unsigned PhysReg = VirtReg;
608 if (VirtReg >= MRegisterInfo::FirstVirtualRegister) {
609 std::map<unsigned, unsigned>::iterator I =
610 Virt2PhysRegMap.find(VirtReg);
611 assert(I != Virt2PhysRegMap.end());
613 Virt2PhysRegMap.erase(I);
617 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
618 << " [%reg" << VirtReg
619 << "] is never used, removing it frame live list\n");
620 removePhysReg(PhysReg);
626 // Rewind the iterator to point to the first flow control instruction...
627 const TargetInstrInfo &TII = TM->getInstrInfo();
629 while (I != MBB.begin() && TII.isTerminatorInstr((*(I-1))->getOpcode()))
632 // Spill all physical registers holding virtual registers now.
633 while (!PhysRegsUsed.empty())
634 if (unsigned VirtReg = PhysRegsUsed.begin()->second)
635 spillVirtReg(MBB, I, VirtReg, PhysRegsUsed.begin()->first);
637 removePhysReg(PhysRegsUsed.begin()->first);
639 for (std::map<unsigned, unsigned>::iterator I = Virt2PhysRegMap.begin(),
640 E = Virt2PhysRegMap.end(); I != E; ++I)
641 std::cerr << "Register still mapped: " << I->first << " -> "
642 << I->second << "\n";
644 assert(Virt2PhysRegMap.empty() && "Virtual registers still in phys regs?");
646 // Clear any physical register which appear live at the end of the basic
647 // block, but which do not hold any virtual registers. e.g., the stack
649 PhysRegsUseOrder.clear();
653 /// runOnMachineFunction - Register allocate the whole function
655 bool RA::runOnMachineFunction(MachineFunction &Fn) {
656 DEBUG(std::cerr << "Machine Function " << "\n");
658 TM = &Fn.getTarget();
659 RegInfo = TM->getRegisterInfo();
662 LV = &getAnalysis<LiveVariables>();
664 // Loop over all of the basic blocks, eliminating virtual register references
665 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
667 AllocateBasicBlock(*MBB);
669 StackSlotForVirtReg.clear();
670 VirtRegModified.clear();
674 FunctionPass *createLocalRegisterAllocator() {