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"
32 Statistic<> NumSpilled ("ra-local", "Number of registers spilled");
33 Statistic<> NumReloaded("ra-local", "Number of registers reloaded");
34 cl::opt<bool> DisableKill("disable-kill", cl::Hidden,
35 cl::desc("Disable register kill in local-ra"));
37 class RA : public MachineFunctionPass {
38 const TargetMachine *TM;
40 const MRegisterInfo *RegInfo;
43 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
44 // values are spilled.
45 std::map<unsigned, int> StackSlotForVirtReg;
47 // Virt2PhysRegMap - This map contains entries for each virtual register
48 // that is currently available in a physical register.
50 std::map<unsigned, unsigned> Virt2PhysRegMap;
52 // PhysRegsUsed - This map contains entries for each physical register that
53 // currently has a value (ie, it is in Virt2PhysRegMap). The value mapped
54 // to is the virtual register corresponding to the physical register (the
55 // inverse of the Virt2PhysRegMap), or 0. The value is set to 0 if this
56 // register is pinned because it is used by a future instruction.
58 std::map<unsigned, unsigned> PhysRegsUsed;
60 // PhysRegsUseOrder - This contains a list of the physical registers that
61 // currently have a virtual register value in them. This list provides an
62 // ordering of registers, imposing a reallocation order. This list is only
63 // used if all registers are allocated and we have to spill one, in which
64 // case we spill the least recently used register. Entries at the front of
65 // the list are the least recently used registers, entries at the back are
66 // the most recently used.
68 std::vector<unsigned> PhysRegsUseOrder;
70 // VirtRegModified - This bitset contains information about which virtual
71 // registers need to be spilled back to memory when their registers are
72 // scavenged. If a virtual register has simply been rematerialized, there
73 // is no reason to spill it to memory when we need the register back.
75 std::vector<bool> VirtRegModified;
77 void markVirtRegModified(unsigned Reg, bool Val = true) {
78 assert(Reg >= MRegisterInfo::FirstVirtualRegister && "Illegal VirtReg!");
79 Reg -= MRegisterInfo::FirstVirtualRegister;
80 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
81 VirtRegModified[Reg] = Val;
84 bool isVirtRegModified(unsigned Reg) const {
85 assert(Reg >= MRegisterInfo::FirstVirtualRegister && "Illegal VirtReg!");
86 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
87 && "Illegal virtual register!");
88 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
91 void MarkPhysRegRecentlyUsed(unsigned Reg) {
92 assert(!PhysRegsUseOrder.empty() && "No registers used!");
93 if (PhysRegsUseOrder.back() == Reg) return; // Already most recently used
95 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
96 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
97 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
98 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
99 // Add it to the end of the list
100 PhysRegsUseOrder.push_back(RegMatch);
102 return; // Found an exact match, exit early
107 virtual const char *getPassName() const {
108 return "Local Register Allocator";
111 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
113 AU.addRequired<LiveVariables>();
114 AU.addRequiredID(PHIEliminationID);
115 AU.addRequiredID(TwoAddressInstructionPassID);
116 MachineFunctionPass::getAnalysisUsage(AU);
120 /// runOnMachineFunction - Register allocate the whole function
121 bool runOnMachineFunction(MachineFunction &Fn);
123 /// AllocateBasicBlock - Register allocate the specified basic block.
124 void AllocateBasicBlock(MachineBasicBlock &MBB);
127 /// areRegsEqual - This method returns true if the specified registers are
128 /// related to each other. To do this, it checks to see if they are equal
129 /// or if the first register is in the alias set of the second register.
131 bool areRegsEqual(unsigned R1, unsigned R2) const {
132 if (R1 == R2) return true;
133 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
134 *AliasSet; ++AliasSet) {
135 if (*AliasSet == R1) return true;
140 /// getStackSpaceFor - This returns the frame index of the specified virtual
141 /// register on the stack, allocating space if necessary.
142 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
144 /// removePhysReg - This method marks the specified physical register as no
145 /// longer being in use.
147 void removePhysReg(unsigned PhysReg);
149 /// spillVirtReg - This method spills the value specified by PhysReg into
150 /// the virtual register slot specified by VirtReg. It then updates the RA
151 /// data structures to indicate the fact that PhysReg is now available.
153 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
154 unsigned VirtReg, unsigned PhysReg);
156 /// spillPhysReg - This method spills the specified physical register into
157 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
158 /// true, then the request is ignored if the physical register does not
159 /// contain a virtual register.
161 void spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
162 unsigned PhysReg, bool OnlyVirtRegs = false);
164 /// assignVirtToPhysReg - This method updates local state so that we know
165 /// that PhysReg is the proper container for VirtReg now. The physical
166 /// register must not be used for anything else when this is called.
168 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
170 /// liberatePhysReg - Make sure the specified physical register is available
171 /// for use. If there is currently a value in it, it is either moved out of
172 /// the way or spilled to memory.
174 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
177 /// isPhysRegAvailable - Return true if the specified physical register is
178 /// free and available for use. This also includes checking to see if
179 /// aliased registers are all free...
181 bool isPhysRegAvailable(unsigned PhysReg) const;
183 /// getFreeReg - Look to see if there is a free register available in the
184 /// specified register class. If not, return 0.
186 unsigned getFreeReg(const TargetRegisterClass *RC);
188 /// getReg - Find a physical register to hold the specified virtual
189 /// register. If all compatible physical registers are used, this method
190 /// spills the last used virtual register to the stack, and uses that
193 unsigned getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
196 /// reloadVirtReg - This method loads the specified virtual register into a
197 /// physical register, returning the physical register chosen. This updates
198 /// the regalloc data structures to reflect the fact that the virtual reg is
199 /// now alive in a physical register, and the previous one isn't.
201 unsigned reloadVirtReg(MachineBasicBlock &MBB,
202 MachineBasicBlock::iterator &I, unsigned VirtReg);
204 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
209 /// getStackSpaceFor - This allocates space for the specified virtual register
210 /// to be held on the stack.
211 int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
212 // Find the location Reg would belong...
213 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
215 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
216 return I->second; // Already has space allocated?
218 // Allocate a new stack object for this spill location...
219 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
221 // Assign the slot...
222 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
227 /// removePhysReg - This method marks the specified physical register as no
228 /// longer being in use.
230 void RA::removePhysReg(unsigned PhysReg) {
231 PhysRegsUsed.erase(PhysReg); // PhyReg no longer used
233 std::vector<unsigned>::iterator It =
234 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
235 if (It != PhysRegsUseOrder.end())
236 PhysRegsUseOrder.erase(It);
240 /// spillVirtReg - This method spills the value specified by PhysReg into the
241 /// virtual register slot specified by VirtReg. It then updates the RA data
242 /// structures to indicate the fact that PhysReg is now available.
244 void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
245 unsigned VirtReg, unsigned PhysReg) {
246 if (!VirtReg && DisableKill) return;
247 assert(VirtReg && "Spilling a physical register is illegal!"
248 " Must not have appropriate kill for the register or use exists beyond"
249 " the intended one.");
250 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
251 std::cerr << " containing %reg" << VirtReg;
252 if (!isVirtRegModified(VirtReg))
253 std::cerr << " which has not been modified, so no store necessary!");
255 // Otherwise, there is a virtual register corresponding to this physical
256 // register. We only need to spill it into its stack slot if it has been
258 if (isVirtRegModified(VirtReg)) {
259 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
260 int FrameIndex = getStackSpaceFor(VirtReg, RC);
261 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
262 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
263 ++NumSpilled; // Update statistics
265 Virt2PhysRegMap.erase(VirtReg); // VirtReg no longer available
267 DEBUG(std::cerr << "\n");
268 removePhysReg(PhysReg);
272 /// spillPhysReg - This method spills the specified physical register into the
273 /// virtual register slot associated with it. If OnlyVirtRegs is set to true,
274 /// then the request is ignored if the physical register does not contain a
275 /// virtual register.
277 void RA::spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
278 unsigned PhysReg, bool OnlyVirtRegs) {
279 std::map<unsigned, unsigned>::iterator PI = PhysRegsUsed.find(PhysReg);
280 if (PI != PhysRegsUsed.end()) { // Only spill it if it's used!
281 if (PI->second || !OnlyVirtRegs)
282 spillVirtReg(MBB, I, PI->second, PhysReg);
284 // If the selected register aliases any other registers, we must make
285 // sure that one of the aliases isn't alive...
286 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
287 *AliasSet; ++AliasSet) {
288 PI = PhysRegsUsed.find(*AliasSet);
289 if (PI != PhysRegsUsed.end()) // Spill aliased register...
290 if (PI->second || !OnlyVirtRegs)
291 spillVirtReg(MBB, I, PI->second, *AliasSet);
297 /// assignVirtToPhysReg - This method updates local state so that we know
298 /// that PhysReg is the proper container for VirtReg now. The physical
299 /// register must not be used for anything else when this is called.
301 void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
302 assert(PhysRegsUsed.find(PhysReg) == PhysRegsUsed.end() &&
303 "Phys reg already assigned!");
304 // Update information to note the fact that this register was just used, and
306 PhysRegsUsed[PhysReg] = VirtReg;
307 Virt2PhysRegMap[VirtReg] = PhysReg;
308 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
312 /// isPhysRegAvailable - Return true if the specified physical register is free
313 /// and available for use. This also includes checking to see if aliased
314 /// registers are all free...
316 bool RA::isPhysRegAvailable(unsigned PhysReg) const {
317 if (PhysRegsUsed.count(PhysReg)) return false;
319 // If the selected register aliases any other allocated registers, it is
321 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
322 *AliasSet; ++AliasSet)
323 if (PhysRegsUsed.count(*AliasSet)) // Aliased register in use?
324 return false; // Can't use this reg then.
329 /// getFreeReg - Look to see if there is a free register available in the
330 /// specified register class. If not, return 0.
332 unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
333 // Get iterators defining the range of registers that are valid to allocate in
334 // this class, which also specifies the preferred allocation order.
335 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
336 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
338 for (; RI != RE; ++RI)
339 if (isPhysRegAvailable(*RI)) { // Is reg unused?
340 assert(*RI != 0 && "Cannot use register!");
341 return *RI; // Found an unused register!
347 /// liberatePhysReg - Make sure the specified physical register is available for
348 /// use. If there is currently a value in it, it is either moved out of the way
349 /// or spilled to memory.
351 void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
353 // FIXME: This code checks to see if a register is available, but it really
354 // wants to know if a reg is available BEFORE the instruction executes. If
355 // called after killed operands are freed, it runs the risk of reallocating a
358 if (isPhysRegAvailable(PhysReg)) return; // Already available...
360 // Check to see if the register is directly used, not indirectly used through
361 // aliases. If aliased registers are the ones actually used, we cannot be
362 // sure that we will be able to save the whole thing if we do a reg-reg copy.
363 std::map<unsigned, unsigned>::iterator PRUI = PhysRegsUsed.find(PhysReg);
364 if (PRUI != PhysRegsUsed.end()) {
365 unsigned VirtReg = PRUI->second; // The virtual register held...
367 // Check to see if there is a compatible register available. If so, we can
368 // move the value into the new register...
370 const TargetRegisterClass *RC = RegInfo->getRegClass(PhysReg);
371 if (unsigned NewReg = getFreeReg(RC)) {
372 // Emit the code to copy the value...
373 RegInfo->copyRegToReg(MBB, I, NewReg, PhysReg, RC);
375 // Update our internal state to indicate that PhysReg is available and Reg
377 Virt2PhysRegMap.erase(VirtReg);
378 removePhysReg(PhysReg); // Free the physreg
380 // Move reference over to new register...
381 assignVirtToPhysReg(VirtReg, NewReg);
386 spillPhysReg(MBB, I, PhysReg);
390 /// getReg - Find a physical register to hold the specified virtual
391 /// register. If all compatible physical registers are used, this method spills
392 /// the last used virtual register to the stack, and uses that register.
394 unsigned RA::getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
396 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
398 // First check to see if we have a free register of the requested type...
399 unsigned PhysReg = getFreeReg(RC);
401 // If we didn't find an unused register, scavenge one now!
403 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
405 // Loop over all of the preallocated registers from the least recently used
406 // to the most recently used. When we find one that is capable of holding
407 // our register, use it.
408 for (unsigned i = 0; PhysReg == 0; ++i) {
409 assert(i != PhysRegsUseOrder.size() &&
410 "Couldn't find a register of the appropriate class!");
412 unsigned R = PhysRegsUseOrder[i];
414 // We can only use this register if it holds a virtual register (ie, it
415 // can be spilled). Do not use it if it is an explicitly allocated
416 // physical register!
417 assert(PhysRegsUsed.count(R) &&
418 "PhysReg in PhysRegsUseOrder, but is not allocated?");
419 if (PhysRegsUsed[R]) {
420 // If the current register is compatible, use it.
421 if (RegInfo->getRegClass(R) == RC) {
425 // If one of the registers aliased to the current register is
426 // compatible, use it.
427 for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
428 *AliasSet; ++AliasSet) {
429 if (RegInfo->getRegClass(*AliasSet) == RC) {
430 PhysReg = *AliasSet; // Take an aliased register
438 assert(PhysReg && "Physical register not assigned!?!?");
440 // At this point PhysRegsUseOrder[i] is the least recently used register of
441 // compatible register class. Spill it to memory and reap its remains.
442 spillPhysReg(MBB, I, PhysReg);
445 // Now that we know which register we need to assign this to, do it now!
446 assignVirtToPhysReg(VirtReg, PhysReg);
451 /// reloadVirtReg - This method loads the specified virtual register into a
452 /// physical register, returning the physical register chosen. This updates the
453 /// regalloc data structures to reflect the fact that the virtual reg is now
454 /// alive in a physical register, and the previous one isn't.
456 unsigned RA::reloadVirtReg(MachineBasicBlock &MBB,
457 MachineBasicBlock::iterator &I,
459 std::map<unsigned, unsigned>::iterator It = Virt2PhysRegMap.find(VirtReg);
460 if (It != Virt2PhysRegMap.end()) {
461 MarkPhysRegRecentlyUsed(It->second);
462 return It->second; // Already have this value available!
465 unsigned PhysReg = getReg(MBB, I, VirtReg);
467 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
468 int FrameIndex = getStackSpaceFor(VirtReg, RC);
470 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
472 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
473 << RegInfo->getName(PhysReg) << "\n");
475 // Add move instruction(s)
476 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIndex, RC);
477 ++NumReloaded; // Update statistics
483 void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
484 // loop over each instruction
485 MachineBasicBlock::iterator I = MBB.begin();
486 for (; I != MBB.end(); ++I) {
487 MachineInstr *MI = *I;
488 const TargetInstrDescriptor &TID = TM->getInstrInfo().get(MI->getOpcode());
489 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
490 std::cerr << " Regs have values: ";
491 for (std::map<unsigned, unsigned>::const_iterator
492 I = PhysRegsUsed.begin(), E = PhysRegsUsed.end(); I != E; ++I)
493 std::cerr << "[" << RegInfo->getName(I->first)
494 << ",%reg" << I->second << "] ";
497 // Loop over the implicit uses, making sure that they are at the head of the
498 // use order list, so they don't get reallocated.
499 for (const unsigned *ImplicitUses = TID.ImplicitUses;
500 *ImplicitUses; ++ImplicitUses)
501 MarkPhysRegRecentlyUsed(*ImplicitUses);
503 // Get the used operands into registers. This has the potential to spill
504 // incoming values if we are out of registers. Note that we completely
505 // ignore physical register uses here. We assume that if an explicit
506 // physical register is referenced by the instruction, that it is guaranteed
507 // to be live-in, or the input is badly hosed.
509 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
510 if (MI->getOperand(i).isUse() &&
511 !MI->getOperand(i).isDef() &&
512 MI->getOperand(i).isVirtualRegister()){
513 unsigned VirtSrcReg = MI->getOperand(i).getAllocatedRegNum();
514 unsigned PhysSrcReg = reloadVirtReg(MBB, I, VirtSrcReg);
515 MI->SetMachineOperandReg(i, PhysSrcReg); // Assign the input register
519 // If this instruction is the last user of anything in registers, kill the
520 // value, freeing the register being used, so it doesn't need to be
521 // spilled to memory.
523 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
524 KE = LV->killed_end(MI); KI != KE; ++KI) {
525 unsigned VirtReg = KI->second;
526 unsigned PhysReg = VirtReg;
527 if (VirtReg >= MRegisterInfo::FirstVirtualRegister) {
528 std::map<unsigned, unsigned>::iterator I =
529 Virt2PhysRegMap.find(VirtReg);
530 assert(I != Virt2PhysRegMap.end());
532 Virt2PhysRegMap.erase(I);
536 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
537 << "[%reg" << VirtReg <<"], removing it from live set\n");
538 removePhysReg(PhysReg);
543 // Loop over all of the operands of the instruction, spilling registers that
544 // are defined, and marking explicit destinations in the PhysRegsUsed map.
545 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
546 if (MI->getOperand(i).isDef() &&
547 MI->getOperand(i).isPhysicalRegister()) {
548 unsigned Reg = MI->getOperand(i).getAllocatedRegNum();
549 spillPhysReg(MBB, I, Reg, true); // Spill any existing value in the reg
550 PhysRegsUsed[Reg] = 0; // It is free and reserved now
551 PhysRegsUseOrder.push_back(Reg);
552 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
553 *AliasSet; ++AliasSet) {
554 PhysRegsUseOrder.push_back(*AliasSet);
555 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
559 // Loop over the implicit defs, spilling them as well.
560 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
561 *ImplicitDefs; ++ImplicitDefs) {
562 unsigned Reg = *ImplicitDefs;
563 spillPhysReg(MBB, I, Reg);
564 PhysRegsUseOrder.push_back(Reg);
565 PhysRegsUsed[Reg] = 0; // It is free and reserved now
566 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
567 *AliasSet; ++AliasSet) {
568 PhysRegsUseOrder.push_back(*AliasSet);
569 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
573 // Okay, we have allocated all of the source operands and spilled any values
574 // that would be destroyed by defs of this instruction. Loop over the
575 // implicit defs and assign them to a register, spilling incoming values if
576 // we need to scavenge a register.
578 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
579 if (MI->getOperand(i).isDef() &&
580 MI->getOperand(i).isVirtualRegister()) {
581 unsigned DestVirtReg = MI->getOperand(i).getAllocatedRegNum();
582 unsigned DestPhysReg;
584 // If DestVirtReg already has a value, use it.
585 std::map<unsigned, unsigned>::iterator DestI =
586 Virt2PhysRegMap.find(DestVirtReg);
587 if (DestI != Virt2PhysRegMap.end()) {
588 DestPhysReg = DestI->second;
591 DestPhysReg = getReg(MBB, I, DestVirtReg);
593 markVirtRegModified(DestVirtReg);
594 MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register
598 // If this instruction defines any registers that are immediately dead,
601 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
602 KE = LV->dead_end(MI); KI != KE; ++KI) {
603 unsigned VirtReg = KI->second;
604 unsigned PhysReg = VirtReg;
605 if (VirtReg >= MRegisterInfo::FirstVirtualRegister) {
606 std::map<unsigned, unsigned>::iterator I =
607 Virt2PhysRegMap.find(VirtReg);
608 assert(I != Virt2PhysRegMap.end());
610 Virt2PhysRegMap.erase(I);
614 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
615 << " [%reg" << VirtReg
616 << "] is never used, removing it frame live list\n");
617 removePhysReg(PhysReg);
623 // Rewind the iterator to point to the first flow control instruction...
624 const TargetInstrInfo &TII = TM->getInstrInfo();
626 while (I != MBB.begin() && TII.isTerminatorInstr((*(I-1))->getOpcode()))
629 // Spill all physical registers holding virtual registers now.
630 while (!PhysRegsUsed.empty())
631 if (unsigned VirtReg = PhysRegsUsed.begin()->second)
632 spillVirtReg(MBB, I, VirtReg, PhysRegsUsed.begin()->first);
634 removePhysReg(PhysRegsUsed.begin()->first);
636 for (std::map<unsigned, unsigned>::iterator I = Virt2PhysRegMap.begin(),
637 E = Virt2PhysRegMap.end(); I != E; ++I)
638 std::cerr << "Register still mapped: " << I->first << " -> "
639 << I->second << "\n";
641 assert(Virt2PhysRegMap.empty() && "Virtual registers still in phys regs?");
643 // Clear any physical register which appear live at the end of the basic
644 // block, but which do not hold any virtual registers. e.g., the stack
646 PhysRegsUseOrder.clear();
650 /// runOnMachineFunction - Register allocate the whole function
652 bool RA::runOnMachineFunction(MachineFunction &Fn) {
653 DEBUG(std::cerr << "Machine Function " << "\n");
655 TM = &Fn.getTarget();
656 RegInfo = TM->getRegisterInfo();
659 LV = &getAnalysis<LiveVariables>();
661 // Loop over all of the basic blocks, eliminating virtual register references
662 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
664 AllocateBasicBlock(*MBB);
666 StackSlotForVirtReg.clear();
667 VirtRegModified.clear();
671 FunctionPass *createLocalRegisterAllocator() {
675 } // End llvm namespace