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 "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/Statistic.h"
31 Statistic<> NumStores("ra-local", "Number of stores added");
32 Statistic<> NumLoads ("ra-local", "Number of loads added");
33 Statistic<> NumFolded("ra-local", "Number of loads/stores folded into "
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.
47 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
49 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
50 return Virt2PhysRegMap[VirtReg];
53 // PhysRegsUsed - This array is effectively a map, containing entries for
54 // each physical register that currently has a value (ie, it is in
55 // Virt2PhysRegMap). The value mapped to is the virtual register
56 // corresponding to the physical register (the inverse of the
57 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
58 // because it is used by a future instruction. If the entry for a physical
59 // register is -1, then the physical register is "not in the map".
61 std::vector<int> PhysRegsUsed;
63 // PhysRegsUseOrder - This contains a list of the physical registers that
64 // currently have a virtual register value in them. This list provides an
65 // ordering of registers, imposing a reallocation order. This list is only
66 // used if all registers are allocated and we have to spill one, in which
67 // case we spill the least recently used register. Entries at the front of
68 // the list are the least recently used registers, entries at the back are
69 // the most recently used.
71 std::vector<unsigned> PhysRegsUseOrder;
73 // VirtRegModified - This bitset contains information about which virtual
74 // registers need to be spilled back to memory when their registers are
75 // scavenged. If a virtual register has simply been rematerialized, there
76 // is no reason to spill it to memory when we need the register back.
78 std::vector<bool> VirtRegModified;
80 void markVirtRegModified(unsigned Reg, bool Val = true) {
81 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
82 Reg -= MRegisterInfo::FirstVirtualRegister;
83 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
84 VirtRegModified[Reg] = Val;
87 bool isVirtRegModified(unsigned Reg) const {
88 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
89 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
90 && "Illegal virtual register!");
91 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
94 void MarkPhysRegRecentlyUsed(unsigned Reg) {
95 if(PhysRegsUseOrder.empty() ||
96 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
98 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
99 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
100 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
101 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
102 // Add it to the end of the list
103 PhysRegsUseOrder.push_back(RegMatch);
105 return; // Found an exact match, exit early
110 virtual const char *getPassName() const {
111 return "Local Register Allocator";
114 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
115 AU.addRequired<LiveVariables>();
116 AU.addRequiredID(PHIEliminationID);
117 AU.addRequiredID(TwoAddressInstructionPassID);
118 MachineFunctionPass::getAnalysisUsage(AU);
122 /// runOnMachineFunction - Register allocate the whole function
123 bool runOnMachineFunction(MachineFunction &Fn);
125 /// AllocateBasicBlock - Register allocate the specified basic block.
126 void AllocateBasicBlock(MachineBasicBlock &MBB);
129 /// areRegsEqual - This method returns true if the specified registers are
130 /// related to each other. To do this, it checks to see if they are equal
131 /// or if the first register is in the alias set of the second register.
133 bool areRegsEqual(unsigned R1, unsigned R2) const {
134 if (R1 == R2) return true;
135 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
136 *AliasSet; ++AliasSet) {
137 if (*AliasSet == R1) return true;
142 /// getStackSpaceFor - This returns the frame index of the specified virtual
143 /// register on the stack, allocating space if necessary.
144 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
146 /// removePhysReg - This method marks the specified physical register as no
147 /// longer being in use.
149 void removePhysReg(unsigned PhysReg);
151 /// spillVirtReg - This method spills the value specified by PhysReg into
152 /// the virtual register slot specified by VirtReg. It then updates the RA
153 /// data structures to indicate the fact that PhysReg is now available.
155 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
156 unsigned VirtReg, unsigned PhysReg);
158 /// spillPhysReg - This method spills the specified physical register into
159 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
160 /// true, then the request is ignored if the physical register does not
161 /// contain a virtual register.
163 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
164 unsigned PhysReg, bool OnlyVirtRegs = false);
166 /// assignVirtToPhysReg - This method updates local state so that we know
167 /// that PhysReg is the proper container for VirtReg now. The physical
168 /// register must not be used for anything else when this is called.
170 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
172 /// liberatePhysReg - Make sure the specified physical register is available
173 /// for use. If there is currently a value in it, it is either moved out of
174 /// the way or spilled to memory.
176 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
179 /// isPhysRegAvailable - Return true if the specified physical register is
180 /// free and available for use. This also includes checking to see if
181 /// aliased registers are all free...
183 bool isPhysRegAvailable(unsigned PhysReg) const;
185 /// getFreeReg - Look to see if there is a free register available in the
186 /// specified register class. If not, return 0.
188 unsigned getFreeReg(const TargetRegisterClass *RC);
190 /// getReg - Find a physical register to hold the specified virtual
191 /// register. If all compatible physical registers are used, this method
192 /// spills the last used virtual register to the stack, and uses that
195 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
198 /// reloadVirtReg - This method transforms the specified specified virtual
199 /// register use to refer to a physical register. This method may do this
200 /// in one of several ways: if the register is available in a physical
201 /// register already, it uses that physical register. If the value is not
202 /// in a physical register, and if there are physical registers available,
203 /// it loads it into a register. If register pressure is high, and it is
204 /// possible, it tries to fold the load of the virtual register into the
205 /// instruction itself. It avoids doing this if register pressure is low to
206 /// improve the chance that subsequent instructions can use the reloaded
207 /// value. This method returns the modified instruction.
209 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
213 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
218 /// getStackSpaceFor - This allocates space for the specified virtual register
219 /// to be held on the stack.
220 int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
221 // Find the location Reg would belong...
222 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
224 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
225 return I->second; // Already has space allocated?
227 // Allocate a new stack object for this spill location...
228 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
231 // Assign the slot...
232 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
237 /// removePhysReg - This method marks the specified physical register as no
238 /// longer being in use.
240 void RA::removePhysReg(unsigned PhysReg) {
241 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
243 std::vector<unsigned>::iterator It =
244 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
245 if (It != PhysRegsUseOrder.end())
246 PhysRegsUseOrder.erase(It);
250 /// spillVirtReg - This method spills the value specified by PhysReg into the
251 /// virtual register slot specified by VirtReg. It then updates the RA data
252 /// structures to indicate the fact that PhysReg is now available.
254 void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
255 unsigned VirtReg, unsigned PhysReg) {
256 assert(VirtReg && "Spilling a physical register is illegal!"
257 " Must not have appropriate kill for the register or use exists beyond"
258 " the intended one.");
259 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
260 std::cerr << " containing %reg" << VirtReg;
261 if (!isVirtRegModified(VirtReg))
262 std::cerr << " which has not been modified, so no store necessary!");
264 // Otherwise, there is a virtual register corresponding to this physical
265 // register. We only need to spill it into its stack slot if it has been
267 if (isVirtRegModified(VirtReg)) {
268 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
269 int FrameIndex = getStackSpaceFor(VirtReg, RC);
270 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
271 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex);
272 ++NumStores; // Update statistics
275 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
277 DEBUG(std::cerr << "\n");
278 removePhysReg(PhysReg);
282 /// spillPhysReg - This method spills the specified physical register into the
283 /// virtual register slot associated with it. If OnlyVirtRegs is set to true,
284 /// then the request is ignored if the physical register does not contain a
285 /// virtual register.
287 void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
288 unsigned PhysReg, bool OnlyVirtRegs) {
289 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
290 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
291 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
293 // If the selected register aliases any other registers, we must make
294 // sure that one of the aliases isn't alive...
295 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
296 *AliasSet; ++AliasSet)
297 if (PhysRegsUsed[*AliasSet] != -1) // Spill aliased register...
298 if (PhysRegsUsed[*AliasSet] || !OnlyVirtRegs)
299 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
304 /// assignVirtToPhysReg - This method updates local state so that we know
305 /// that PhysReg is the proper container for VirtReg now. The physical
306 /// register must not be used for anything else when this is called.
308 void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
309 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
310 // Update information to note the fact that this register was just used, and
312 PhysRegsUsed[PhysReg] = VirtReg;
313 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
314 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
318 /// isPhysRegAvailable - Return true if the specified physical register is free
319 /// and available for use. This also includes checking to see if aliased
320 /// registers are all free...
322 bool RA::isPhysRegAvailable(unsigned PhysReg) const {
323 if (PhysRegsUsed[PhysReg] != -1) return false;
325 // If the selected register aliases any other allocated registers, it is
327 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
328 *AliasSet; ++AliasSet)
329 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
330 return false; // Can't use this reg then.
335 /// getFreeReg - Look to see if there is a free register available in the
336 /// specified register class. If not, return 0.
338 unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
339 // Get iterators defining the range of registers that are valid to allocate in
340 // this class, which also specifies the preferred allocation order.
341 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
342 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
344 for (; RI != RE; ++RI)
345 if (isPhysRegAvailable(*RI)) { // Is reg unused?
346 assert(*RI != 0 && "Cannot use register!");
347 return *RI; // Found an unused register!
353 /// liberatePhysReg - Make sure the specified physical register is available for
354 /// use. If there is currently a value in it, it is either moved out of the way
355 /// or spilled to memory.
357 void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
359 spillPhysReg(MBB, I, PhysReg);
363 /// getReg - Find a physical register to hold the specified virtual
364 /// register. If all compatible physical registers are used, this method spills
365 /// the last used virtual register to the stack, and uses that register.
367 unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
369 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
371 // First check to see if we have a free register of the requested type...
372 unsigned PhysReg = getFreeReg(RC);
374 // If we didn't find an unused register, scavenge one now!
376 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
378 // Loop over all of the preallocated registers from the least recently used
379 // to the most recently used. When we find one that is capable of holding
380 // our register, use it.
381 for (unsigned i = 0; PhysReg == 0; ++i) {
382 assert(i != PhysRegsUseOrder.size() &&
383 "Couldn't find a register of the appropriate class!");
385 unsigned R = PhysRegsUseOrder[i];
387 // We can only use this register if it holds a virtual register (ie, it
388 // can be spilled). Do not use it if it is an explicitly allocated
389 // physical register!
390 assert(PhysRegsUsed[R] != -1 &&
391 "PhysReg in PhysRegsUseOrder, but is not allocated?");
392 if (PhysRegsUsed[R]) {
393 // If the current register is compatible, use it.
394 if (RC->contains(R)) {
398 // If one of the registers aliased to the current register is
399 // compatible, use it.
400 for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
401 *AliasSet; ++AliasSet) {
402 if (RC->contains(*AliasSet)) {
403 PhysReg = *AliasSet; // Take an aliased register
411 assert(PhysReg && "Physical register not assigned!?!?");
413 // At this point PhysRegsUseOrder[i] is the least recently used register of
414 // compatible register class. Spill it to memory and reap its remains.
415 spillPhysReg(MBB, I, PhysReg);
418 // Now that we know which register we need to assign this to, do it now!
419 assignVirtToPhysReg(VirtReg, PhysReg);
424 /// reloadVirtReg - This method transforms the specified specified virtual
425 /// register use to refer to a physical register. This method may do this in
426 /// one of several ways: if the register is available in a physical register
427 /// already, it uses that physical register. If the value is not in a physical
428 /// register, and if there are physical registers available, it loads it into a
429 /// register. If register pressure is high, and it is possible, it tries to
430 /// fold the load of the virtual register into the instruction itself. It
431 /// avoids doing this if register pressure is low to improve the chance that
432 /// subsequent instructions can use the reloaded value. This method returns the
433 /// modified instruction.
435 MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
437 unsigned VirtReg = MI->getOperand(OpNum).getReg();
439 // If the virtual register is already available, just update the instruction
441 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
442 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
443 MI->SetMachineOperandReg(OpNum, PR); // Assign the input register
447 // Otherwise, we need to fold it into the current instruction, or reload it.
448 // If we have registers available to hold the value, use them.
449 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
450 unsigned PhysReg = getFreeReg(RC);
451 int FrameIndex = getStackSpaceFor(VirtReg, RC);
453 if (PhysReg) { // Register is available, allocate it!
454 assignVirtToPhysReg(VirtReg, PhysReg);
455 } else { // No registers available.
456 // If we can fold this spill into this instruction, do so now.
457 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
459 // Since we changed the address of MI, make sure to update live variables
460 // to know that the new instruction has the properties of the old one.
461 LV->instructionChanged(MI, FMI);
462 return MBB.insert(MBB.erase(MI), FMI);
465 // It looks like we can't fold this virtual register load into this
466 // instruction. Force some poor hapless value out of the register file to
467 // make room for the new register, and reload it.
468 PhysReg = getReg(MBB, MI, VirtReg);
471 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
473 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
474 << RegInfo->getName(PhysReg) << "\n");
476 // Add move instruction(s)
477 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex);
478 ++NumLoads; // Update statistics
480 MI->SetMachineOperandReg(OpNum, PhysReg); // Assign the input register
486 void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
487 // loop over each instruction
488 MachineBasicBlock::iterator MI = MBB.begin();
489 for (; MI != MBB.end(); ++MI) {
490 const TargetInstrDescriptor &TID = TM->getInstrInfo()->get(MI->getOpcode());
491 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
492 std::cerr << " Regs have values: ";
493 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
494 if (PhysRegsUsed[i] != -1)
495 std::cerr << "[" << RegInfo->getName(i)
496 << ",%reg" << PhysRegsUsed[i] << "] ";
499 // Loop over the implicit uses, making sure that they are at the head of the
500 // use order list, so they don't get reallocated.
501 for (const unsigned *ImplicitUses = TID.ImplicitUses;
502 *ImplicitUses; ++ImplicitUses)
503 MarkPhysRegRecentlyUsed(*ImplicitUses);
505 // Get the used operands into registers. This has the potential to spill
506 // incoming values if we are out of registers. Note that we completely
507 // ignore physical register uses here. We assume that if an explicit
508 // physical register is referenced by the instruction, that it is guaranteed
509 // to be live-in, or the input is badly hosed.
511 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
512 MachineOperand& MO = MI->getOperand(i);
513 // here we are looking for only used operands (never def&use)
514 if (!MO.isDef() && MO.isRegister() && MO.getReg() &&
515 MRegisterInfo::isVirtualRegister(MO.getReg()))
516 MI = reloadVirtReg(MBB, MI, i);
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 (MRegisterInfo::isVirtualRegister(VirtReg)) {
528 // If the virtual register was never materialized into a register, it
529 // might not be in the map, but it won't hurt to zero it out anyway.
530 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
531 PhysReg = PhysRegSlot;
536 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
537 << "[%reg" << VirtReg <<"], removing it from live set\n");
538 removePhysReg(PhysReg);
542 // Loop over all of the operands of the instruction, spilling registers that
543 // are defined, and marking explicit destinations in the PhysRegsUsed map.
544 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
545 MachineOperand& MO = MI->getOperand(i);
546 if (MO.isDef() && MO.isRegister() && MO.getReg() &&
547 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
548 unsigned Reg = MO.getReg();
549 spillPhysReg(MBB, MI, 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
560 // Loop over the implicit defs, spilling them as well.
561 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
562 *ImplicitDefs; ++ImplicitDefs) {
563 unsigned Reg = *ImplicitDefs;
564 spillPhysReg(MBB, MI, Reg, true);
565 PhysRegsUseOrder.push_back(Reg);
566 PhysRegsUsed[Reg] = 0; // It is free and reserved now
567 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
568 *AliasSet; ++AliasSet) {
569 PhysRegsUseOrder.push_back(*AliasSet);
570 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
574 // Okay, we have allocated all of the source operands and spilled any values
575 // that would be destroyed by defs of this instruction. Loop over the
576 // implicit defs and assign them to a register, spilling incoming values if
577 // we need to scavenge a register.
579 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
580 MachineOperand& MO = MI->getOperand(i);
581 if (MO.isDef() && MO.isRegister() && MO.getReg() &&
582 MRegisterInfo::isVirtualRegister(MO.getReg())) {
583 unsigned DestVirtReg = MO.getReg();
584 unsigned DestPhysReg;
586 // If DestVirtReg already has a value, use it.
587 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
588 DestPhysReg = getReg(MBB, MI, DestVirtReg);
589 markVirtRegModified(DestVirtReg);
590 MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register
594 // If this instruction defines any registers that are immediately dead,
597 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
598 KE = LV->dead_end(MI); KI != KE; ++KI) {
599 unsigned VirtReg = KI->second;
600 unsigned PhysReg = VirtReg;
601 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
602 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
603 PhysReg = PhysRegSlot;
604 assert(PhysReg != 0);
609 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
610 << " [%reg" << VirtReg
611 << "] is never used, removing it frame live list\n");
612 removePhysReg(PhysReg);
617 MI = MBB.getFirstTerminator();
619 // Spill all physical registers holding virtual registers now.
620 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
621 if (PhysRegsUsed[i] != -1)
622 if (unsigned VirtReg = PhysRegsUsed[i])
623 spillVirtReg(MBB, MI, VirtReg, i);
629 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
630 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
631 if (unsigned PR = Virt2PhysRegMap[i]) {
632 std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
635 assert(AllOk && "Virtual registers still in phys regs?");
638 // Clear any physical register which appear live at the end of the basic
639 // block, but which do not hold any virtual registers. e.g., the stack
641 PhysRegsUseOrder.clear();
645 /// runOnMachineFunction - Register allocate the whole function
647 bool RA::runOnMachineFunction(MachineFunction &Fn) {
648 DEBUG(std::cerr << "Machine Function " << "\n");
650 TM = &Fn.getTarget();
651 RegInfo = TM->getRegisterInfo();
652 LV = &getAnalysis<LiveVariables>();
654 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
656 // initialize the virtual->physical register map to have a 'null'
657 // mapping for all virtual registers
658 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
660 // Loop over all of the basic blocks, eliminating virtual register references
661 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
663 AllocateBasicBlock(*MBB);
665 StackSlotForVirtReg.clear();
666 PhysRegsUsed.clear();
667 VirtRegModified.clear();
668 Virt2PhysRegMap.clear();
672 FunctionPass *llvm::createLocalRegisterAllocator() {