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"
31 Statistic<> NumSpilled ("ra-local", "Number of registers spilled");
32 Statistic<> NumReloaded("ra-local", "Number of registers reloaded");
33 cl::opt<bool> DisableKill("disable-kill", cl::Hidden,
34 cl::desc("Disable register kill in local-ra"));
36 class RA : public MachineFunctionPass {
37 const TargetMachine *TM;
39 const MRegisterInfo *RegInfo;
42 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
43 // values are spilled.
44 std::map<unsigned, int> StackSlotForVirtReg;
46 // Virt2PhysRegMap - This map contains entries for each virtual register
47 // that is currently available in a physical register. This is "logically"
48 // a map from virtual register numbers to physical register numbers.
49 // Instead of using a map, however, which is slow, we use a vector. The
50 // index is the VREG number - FirstVirtualRegister. If the entry is zero,
51 // then it is logically "not in the map".
53 std::vector<unsigned> Virt2PhysRegMap;
55 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
56 assert(MRegisterInfo::isVirtualRegister(VirtReg) &&"Illegal VREG #");
57 assert(VirtReg-MRegisterInfo::FirstVirtualRegister <Virt2PhysRegMap.size()
58 && "VirtReg not in map!");
59 return Virt2PhysRegMap[VirtReg-MRegisterInfo::FirstVirtualRegister];
62 // PhysRegsUsed - This array is effectively a map, containing entries for
63 // each physical register that currently has a value (ie, it is in
64 // Virt2PhysRegMap). The value mapped to is the virtual register
65 // corresponding to the physical register (the inverse of the
66 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
67 // because it is used by a future instruction. If the entry for a physical
68 // register is -1, then the physical register is "not in the map".
70 std::vector<int> PhysRegsUsed;
72 // PhysRegsUseOrder - This contains a list of the physical registers that
73 // currently have a virtual register value in them. This list provides an
74 // ordering of registers, imposing a reallocation order. This list is only
75 // used if all registers are allocated and we have to spill one, in which
76 // case we spill the least recently used register. Entries at the front of
77 // the list are the least recently used registers, entries at the back are
78 // the most recently used.
80 std::vector<unsigned> PhysRegsUseOrder;
82 // VirtRegModified - This bitset contains information about which virtual
83 // registers need to be spilled back to memory when their registers are
84 // scavenged. If a virtual register has simply been rematerialized, there
85 // is no reason to spill it to memory when we need the register back.
87 std::vector<bool> VirtRegModified;
89 void markVirtRegModified(unsigned Reg, bool Val = true) {
90 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
91 Reg -= MRegisterInfo::FirstVirtualRegister;
92 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
93 VirtRegModified[Reg] = Val;
96 bool isVirtRegModified(unsigned Reg) const {
97 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
98 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
99 && "Illegal virtual register!");
100 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
103 void MarkPhysRegRecentlyUsed(unsigned Reg) {
104 assert(!PhysRegsUseOrder.empty() && "No registers used!");
105 if (PhysRegsUseOrder.back() == Reg) return; // Already most recently used
107 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
108 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
109 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
110 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
111 // Add it to the end of the list
112 PhysRegsUseOrder.push_back(RegMatch);
114 return; // Found an exact match, exit early
119 virtual const char *getPassName() const {
120 return "Local Register Allocator";
123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
125 AU.addRequired<LiveVariables>();
126 AU.addRequiredID(PHIEliminationID);
127 AU.addRequiredID(TwoAddressInstructionPassID);
128 MachineFunctionPass::getAnalysisUsage(AU);
132 /// runOnMachineFunction - Register allocate the whole function
133 bool runOnMachineFunction(MachineFunction &Fn);
135 /// AllocateBasicBlock - Register allocate the specified basic block.
136 void AllocateBasicBlock(MachineBasicBlock &MBB);
139 /// areRegsEqual - This method returns true if the specified registers are
140 /// related to each other. To do this, it checks to see if they are equal
141 /// or if the first register is in the alias set of the second register.
143 bool areRegsEqual(unsigned R1, unsigned R2) const {
144 if (R1 == R2) return true;
145 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
146 *AliasSet; ++AliasSet) {
147 if (*AliasSet == R1) return true;
152 /// getStackSpaceFor - This returns the frame index of the specified virtual
153 /// register on the stack, allocating space if necessary.
154 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
156 /// removePhysReg - This method marks the specified physical register as no
157 /// longer being in use.
159 void removePhysReg(unsigned PhysReg);
161 /// spillVirtReg - This method spills the value specified by PhysReg into
162 /// the virtual register slot specified by VirtReg. It then updates the RA
163 /// data structures to indicate the fact that PhysReg is now available.
165 void spillVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
166 unsigned VirtReg, unsigned PhysReg);
168 /// spillPhysReg - This method spills the specified physical register into
169 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
170 /// true, then the request is ignored if the physical register does not
171 /// contain a virtual register.
173 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
174 unsigned PhysReg, bool OnlyVirtRegs = false);
176 /// assignVirtToPhysReg - This method updates local state so that we know
177 /// that PhysReg is the proper container for VirtReg now. The physical
178 /// register must not be used for anything else when this is called.
180 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
182 /// liberatePhysReg - Make sure the specified physical register is available
183 /// for use. If there is currently a value in it, it is either moved out of
184 /// the way or spilled to memory.
186 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
189 /// isPhysRegAvailable - Return true if the specified physical register is
190 /// free and available for use. This also includes checking to see if
191 /// aliased registers are all free...
193 bool isPhysRegAvailable(unsigned PhysReg) const;
195 /// getFreeReg - Look to see if there is a free register available in the
196 /// specified register class. If not, return 0.
198 unsigned getFreeReg(const TargetRegisterClass *RC);
200 /// getReg - Find a physical register to hold the specified virtual
201 /// register. If all compatible physical registers are used, this method
202 /// spills the last used virtual register to the stack, and uses that
205 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
208 /// reloadVirtReg - This method transforms the specified specified virtual
209 /// register use to refer to a physical register. This method may do this
210 /// in one of several ways: if the register is available in a physical
211 /// register already, it uses that physical register. If the value is not
212 /// in a physical register, and if there are physical registers available,
213 /// it loads it into a register. If register pressure is high, and it is
214 /// possible, it tries to fold the load of the virtual register into the
215 /// instruction itself. It avoids doing this if register pressure is low to
216 /// improve the chance that subsequent instructions can use the reloaded
217 /// value. This method returns the modified instruction.
219 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
223 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
228 /// getStackSpaceFor - This allocates space for the specified virtual register
229 /// to be held on the stack.
230 int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
231 // Find the location Reg would belong...
232 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
234 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
235 return I->second; // Already has space allocated?
237 // Allocate a new stack object for this spill location...
238 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
240 // Assign the slot...
241 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
246 /// removePhysReg - This method marks the specified physical register as no
247 /// longer being in use.
249 void RA::removePhysReg(unsigned PhysReg) {
250 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
252 std::vector<unsigned>::iterator It =
253 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
254 if (It != PhysRegsUseOrder.end())
255 PhysRegsUseOrder.erase(It);
259 /// spillVirtReg - This method spills the value specified by PhysReg into the
260 /// virtual register slot specified by VirtReg. It then updates the RA data
261 /// structures to indicate the fact that PhysReg is now available.
263 void RA::spillVirtReg(MachineBasicBlock &MBB, MachineInstr *I,
264 unsigned VirtReg, unsigned PhysReg) {
265 if (!VirtReg && DisableKill) return;
266 assert(VirtReg && "Spilling a physical register is illegal!"
267 " Must not have appropriate kill for the register or use exists beyond"
268 " the intended one.");
269 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
270 std::cerr << " containing %reg" << VirtReg;
271 if (!isVirtRegModified(VirtReg))
272 std::cerr << " which has not been modified, so no store necessary!");
274 // Otherwise, there is a virtual register corresponding to this physical
275 // register. We only need to spill it into its stack slot if it has been
277 if (isVirtRegModified(VirtReg)) {
278 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
279 int FrameIndex = getStackSpaceFor(VirtReg, RC);
280 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
281 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
282 ++NumSpilled; // Update statistics
285 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
287 DEBUG(std::cerr << "\n");
288 removePhysReg(PhysReg);
292 /// spillPhysReg - This method spills the specified physical register into the
293 /// virtual register slot associated with it. If OnlyVirtRegs is set to true,
294 /// then the request is ignored if the physical register does not contain a
295 /// virtual register.
297 void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
298 unsigned PhysReg, bool OnlyVirtRegs) {
299 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
300 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
301 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
303 // If the selected register aliases any other registers, we must make
304 // sure that one of the aliases isn't alive...
305 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
306 *AliasSet; ++AliasSet)
307 if (PhysRegsUsed[*AliasSet] != -1) // Spill aliased register...
308 if (PhysRegsUsed[*AliasSet] || !OnlyVirtRegs)
309 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
314 /// assignVirtToPhysReg - This method updates local state so that we know
315 /// that PhysReg is the proper container for VirtReg now. The physical
316 /// register must not be used for anything else when this is called.
318 void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
319 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
320 // Update information to note the fact that this register was just used, and
322 PhysRegsUsed[PhysReg] = VirtReg;
323 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
324 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
328 /// isPhysRegAvailable - Return true if the specified physical register is free
329 /// and available for use. This also includes checking to see if aliased
330 /// registers are all free...
332 bool RA::isPhysRegAvailable(unsigned PhysReg) const {
333 if (PhysRegsUsed[PhysReg] != -1) return false;
335 // If the selected register aliases any other allocated registers, it is
337 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
338 *AliasSet; ++AliasSet)
339 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
340 return false; // Can't use this reg then.
345 /// getFreeReg - Look to see if there is a free register available in the
346 /// specified register class. If not, return 0.
348 unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
349 // Get iterators defining the range of registers that are valid to allocate in
350 // this class, which also specifies the preferred allocation order.
351 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
352 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
354 for (; RI != RE; ++RI)
355 if (isPhysRegAvailable(*RI)) { // Is reg unused?
356 assert(*RI != 0 && "Cannot use register!");
357 return *RI; // Found an unused register!
363 /// liberatePhysReg - Make sure the specified physical register is available for
364 /// use. If there is currently a value in it, it is either moved out of the way
365 /// or spilled to memory.
367 void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
369 // FIXME: This code checks to see if a register is available, but it really
370 // wants to know if a reg is available BEFORE the instruction executes. If
371 // called after killed operands are freed, it runs the risk of reallocating a
374 if (isPhysRegAvailable(PhysReg)) return; // Already available...
376 // Check to see if the register is directly used, not indirectly used through
377 // aliases. If aliased registers are the ones actually used, we cannot be
378 // sure that we will be able to save the whole thing if we do a reg-reg copy.
379 if (PhysRegsUsed[PhysReg] != -1) {
380 // The virtual register held...
381 unsigned VirtReg = PhysRegsUsed[PhysReg]->second;
383 // Check to see if there is a compatible register available. If so, we can
384 // move the value into the new register...
386 const TargetRegisterClass *RC = RegInfo->getRegClass(PhysReg);
387 if (unsigned NewReg = getFreeReg(RC)) {
388 // Emit the code to copy the value...
389 RegInfo->copyRegToReg(MBB, I, NewReg, PhysReg, RC);
391 // Update our internal state to indicate that PhysReg is available and Reg
393 getVirt2PhysRegMapSlot[VirtReg] = 0;
394 removePhysReg(PhysReg); // Free the physreg
396 // Move reference over to new register...
397 assignVirtToPhysReg(VirtReg, NewReg);
402 spillPhysReg(MBB, I, PhysReg);
406 /// getReg - Find a physical register to hold the specified virtual
407 /// register. If all compatible physical registers are used, this method spills
408 /// the last used virtual register to the stack, and uses that register.
410 unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
412 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
414 // First check to see if we have a free register of the requested type...
415 unsigned PhysReg = getFreeReg(RC);
417 // If we didn't find an unused register, scavenge one now!
419 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
421 // Loop over all of the preallocated registers from the least recently used
422 // to the most recently used. When we find one that is capable of holding
423 // our register, use it.
424 for (unsigned i = 0; PhysReg == 0; ++i) {
425 assert(i != PhysRegsUseOrder.size() &&
426 "Couldn't find a register of the appropriate class!");
428 unsigned R = PhysRegsUseOrder[i];
430 // We can only use this register if it holds a virtual register (ie, it
431 // can be spilled). Do not use it if it is an explicitly allocated
432 // physical register!
433 assert(PhysRegsUsed[R] != -1 &&
434 "PhysReg in PhysRegsUseOrder, but is not allocated?");
435 if (PhysRegsUsed[R]) {
436 // If the current register is compatible, use it.
437 if (RegInfo->getRegClass(R) == RC) {
441 // If one of the registers aliased to the current register is
442 // compatible, use it.
443 for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
444 *AliasSet; ++AliasSet) {
445 if (RegInfo->getRegClass(*AliasSet) == RC) {
446 PhysReg = *AliasSet; // Take an aliased register
454 assert(PhysReg && "Physical register not assigned!?!?");
456 // At this point PhysRegsUseOrder[i] is the least recently used register of
457 // compatible register class. Spill it to memory and reap its remains.
458 spillPhysReg(MBB, I, PhysReg);
461 // Now that we know which register we need to assign this to, do it now!
462 assignVirtToPhysReg(VirtReg, PhysReg);
467 /// reloadVirtReg - This method transforms the specified specified virtual
468 /// register use to refer to a physical register. This method may do this in
469 /// one of several ways: if the register is available in a physical register
470 /// already, it uses that physical register. If the value is not in a physical
471 /// register, and if there are physical registers available, it loads it into a
472 /// register. If register pressure is high, and it is possible, it tries to
473 /// fold the load of the virtual register into the instruction itself. It
474 /// avoids doing this if register pressure is low to improve the chance that
475 /// subsequent instructions can use the reloaded value. This method returns the
476 /// modified instruction.
478 MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
480 unsigned VirtReg = MI->getOperand(OpNum).getReg();
482 // If the virtual register is already available, just update the instruction
484 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
485 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
486 MI->SetMachineOperandReg(OpNum, PR); // Assign the input register
490 // Otherwise, we need to fold it into the current instruction, or reload it.
491 // If we have registers available to hold the value, use them.
492 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
493 unsigned PhysReg = getFreeReg(RC);
495 if (PhysReg) { // PhysReg available!
496 PhysReg = getReg(MBB, MI, VirtReg);
497 } else { // No registers available...
498 /// If we can fold this spill into this instruction, do so now.
504 // It looks like we can't fold this virtual register load into this
505 // instruction. Force some poor hapless value out of the register file to
506 // make room for the new register, and reload it.
507 PhysReg = getReg(MBB, MI, VirtReg);
510 int FrameIndex = getStackSpaceFor(VirtReg, RC);
512 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
514 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
515 << RegInfo->getName(PhysReg) << "\n");
517 // Add move instruction(s)
518 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
519 ++NumReloaded; // Update statistics
521 MI->SetMachineOperandReg(OpNum, PhysReg); // Assign the input register
527 void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
528 // loop over each instruction
529 MachineBasicBlock::iterator MI = MBB.begin();
530 for (; MI != MBB.end(); ++MI) {
531 const TargetInstrDescriptor &TID = TM->getInstrInfo().get(MI->getOpcode());
532 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
533 std::cerr << " Regs have values: ";
534 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
535 if (PhysRegsUsed[i] != -1)
536 std::cerr << "[" << RegInfo->getName(i)
537 << ",%reg" << PhysRegsUsed[i] << "] ";
540 // Loop over the implicit uses, making sure that they are at the head of the
541 // use order list, so they don't get reallocated.
542 for (const unsigned *ImplicitUses = TID.ImplicitUses;
543 *ImplicitUses; ++ImplicitUses)
544 MarkPhysRegRecentlyUsed(*ImplicitUses);
546 // Get the used operands into registers. This has the potential to spill
547 // incoming values if we are out of registers. Note that we completely
548 // ignore physical register uses here. We assume that if an explicit
549 // physical register is referenced by the instruction, that it is guaranteed
550 // to be live-in, or the input is badly hosed.
552 for (unsigned i = 0; i != MI->getNumOperands(); ++i)
553 if (MI->getOperand(i).isUse() &&
554 !MI->getOperand(i).isDef() && MI->getOperand(i).isRegister() &&
555 MRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg()))
556 MI = reloadVirtReg(MBB, MI, i);
559 // If this instruction is the last user of anything in registers, kill the
560 // value, freeing the register being used, so it doesn't need to be
561 // spilled to memory.
563 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
564 KE = LV->killed_end(MI); KI != KE; ++KI) {
565 unsigned VirtReg = KI->second;
566 unsigned PhysReg = VirtReg;
567 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
568 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
569 PhysReg = PhysRegSlot;
570 assert(PhysReg != 0);
575 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
576 << "[%reg" << VirtReg <<"], removing it from live set\n");
577 removePhysReg(PhysReg);
582 // Loop over all of the operands of the instruction, spilling registers that
583 // are defined, and marking explicit destinations in the PhysRegsUsed map.
584 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
585 if (MI->getOperand(i).isDef() && MI->getOperand(i).isRegister() &&
586 MRegisterInfo::isPhysicalRegister(MI->getOperand(i).getReg())) {
587 unsigned Reg = MI->getOperand(i).getReg();
588 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in the reg
589 PhysRegsUsed[Reg] = 0; // It is free and reserved now
590 PhysRegsUseOrder.push_back(Reg);
591 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
592 *AliasSet; ++AliasSet) {
593 PhysRegsUseOrder.push_back(*AliasSet);
594 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
598 // Loop over the implicit defs, spilling them as well.
599 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
600 *ImplicitDefs; ++ImplicitDefs) {
601 unsigned Reg = *ImplicitDefs;
602 spillPhysReg(MBB, MI, Reg);
603 PhysRegsUseOrder.push_back(Reg);
604 PhysRegsUsed[Reg] = 0; // It is free and reserved now
605 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
606 *AliasSet; ++AliasSet) {
607 PhysRegsUseOrder.push_back(*AliasSet);
608 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
612 // Okay, we have allocated all of the source operands and spilled any values
613 // that would be destroyed by defs of this instruction. Loop over the
614 // implicit defs and assign them to a register, spilling incoming values if
615 // we need to scavenge a register.
617 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
618 if (MI->getOperand(i).isDef() && MI->getOperand(i).isRegister() &&
619 MRegisterInfo::isVirtualRegister(MI->getOperand(i).getReg())) {
620 unsigned DestVirtReg = MI->getOperand(i).getReg();
621 unsigned DestPhysReg;
623 // If DestVirtReg already has a value, use it.
624 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
625 DestPhysReg = getReg(MBB, MI, DestVirtReg);
626 markVirtRegModified(DestVirtReg);
627 MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register
631 // If this instruction defines any registers that are immediately dead,
634 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
635 KE = LV->dead_end(MI); KI != KE; ++KI) {
636 unsigned VirtReg = KI->second;
637 unsigned PhysReg = VirtReg;
638 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
639 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
640 PhysReg = PhysRegSlot;
641 assert(PhysReg != 0);
646 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
647 << " [%reg" << VirtReg
648 << "] is never used, removing it frame live list\n");
649 removePhysReg(PhysReg);
655 // Rewind the iterator to point to the first flow control instruction...
656 const TargetInstrInfo &TII = TM->getInstrInfo();
658 while (MI != MBB.begin() && TII.isTerminatorInstr((--MI)->getOpcode()));
661 // Spill all physical registers holding virtual registers now.
662 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
663 if (PhysRegsUsed[i] != -1)
664 if (unsigned VirtReg = PhysRegsUsed[i])
665 spillVirtReg(MBB, MI, VirtReg, i);
671 for (unsigned i = 0, e = Virt2PhysRegMap.size(); i != e; ++i)
672 if (unsigned PR = Virt2PhysRegMap[i]) {
673 std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
676 assert(AllOk && "Virtual registers still in phys regs?");
679 // Clear any physical register which appear live at the end of the basic
680 // block, but which do not hold any virtual registers. e.g., the stack
682 PhysRegsUseOrder.clear();
686 /// runOnMachineFunction - Register allocate the whole function
688 bool RA::runOnMachineFunction(MachineFunction &Fn) {
689 DEBUG(std::cerr << "Machine Function " << "\n");
691 TM = &Fn.getTarget();
692 RegInfo = TM->getRegisterInfo();
694 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
696 // initialize the virtual->physical register map to have a 'null'
697 // mapping for all virtual registers
698 Virt2PhysRegMap.assign(MF->getSSARegMap()->getNumVirtualRegs(), 0);
701 LV = &getAnalysis<LiveVariables>();
703 // Loop over all of the basic blocks, eliminating virtual register references
704 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
706 AllocateBasicBlock(*MBB);
708 StackSlotForVirtReg.clear();
709 PhysRegsUsed.clear();
710 VirtRegModified.clear();
711 Virt2PhysRegMap.clear();
715 FunctionPass *llvm::createLocalRegisterAllocator() {