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/CodeGen/RegAllocRegistry.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/Statistic.h"
35 static Statistic<> NumStores("ra-local", "Number of stores added");
36 static Statistic<> NumLoads ("ra-local", "Number of loads added");
37 static Statistic<> NumFolded("ra-local", "Number of loads/stores folded "
40 static RegisterRegAlloc
41 localRegAlloc("local", " local register allocator",
42 createLocalRegisterAllocator);
45 class VISIBILITY_HIDDEN RA : public MachineFunctionPass {
46 const TargetMachine *TM;
48 const MRegisterInfo *RegInfo;
50 bool *PhysRegsEverUsed;
52 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
53 // values are spilled.
54 std::map<unsigned, int> StackSlotForVirtReg;
56 // Virt2PhysRegMap - This map contains entries for each virtual register
57 // that is currently available in a physical register.
58 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
60 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
61 return Virt2PhysRegMap[VirtReg];
64 // PhysRegsUsed - This array is effectively a map, containing entries for
65 // each physical register that currently has a value (ie, it is in
66 // Virt2PhysRegMap). The value mapped to is the virtual register
67 // corresponding to the physical register (the inverse of the
68 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
69 // because it is used by a future instruction. If the entry for a physical
70 // register is -1, then the physical register is "not in the map".
72 std::vector<int> PhysRegsUsed;
74 // PhysRegsUseOrder - This contains a list of the physical registers that
75 // currently have a virtual register value in them. This list provides an
76 // ordering of registers, imposing a reallocation order. This list is only
77 // used if all registers are allocated and we have to spill one, in which
78 // case we spill the least recently used register. Entries at the front of
79 // the list are the least recently used registers, entries at the back are
80 // the most recently used.
82 std::vector<unsigned> PhysRegsUseOrder;
84 // VirtRegModified - This bitset contains information about which virtual
85 // registers need to be spilled back to memory when their registers are
86 // scavenged. If a virtual register has simply been rematerialized, there
87 // is no reason to spill it to memory when we need the register back.
89 std::vector<bool> VirtRegModified;
91 void markVirtRegModified(unsigned Reg, bool Val = true) {
92 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
93 Reg -= MRegisterInfo::FirstVirtualRegister;
94 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
95 VirtRegModified[Reg] = Val;
98 bool isVirtRegModified(unsigned Reg) const {
99 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
100 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
101 && "Illegal virtual register!");
102 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
105 void MarkPhysRegRecentlyUsed(unsigned Reg) {
106 if (PhysRegsUseOrder.empty() ||
107 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
109 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
110 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
111 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
112 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
113 // Add it to the end of the list
114 PhysRegsUseOrder.push_back(RegMatch);
116 return; // Found an exact match, exit early
121 virtual const char *getPassName() const {
122 return "Local Register Allocator";
125 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.addRequired<LiveVariables>();
127 AU.addRequiredID(PHIEliminationID);
128 AU.addRequiredID(TwoAddressInstructionPassID);
129 MachineFunctionPass::getAnalysisUsage(AU);
133 /// runOnMachineFunction - Register allocate the whole function
134 bool runOnMachineFunction(MachineFunction &Fn);
136 /// AllocateBasicBlock - Register allocate the specified basic block.
137 void AllocateBasicBlock(MachineBasicBlock &MBB);
140 /// areRegsEqual - This method returns true if the specified registers are
141 /// related to each other. To do this, it checks to see if they are equal
142 /// or if the first register is in the alias set of the second register.
144 bool areRegsEqual(unsigned R1, unsigned R2) const {
145 if (R1 == R2) return true;
146 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
147 *AliasSet; ++AliasSet) {
148 if (*AliasSet == R1) return true;
153 /// getStackSpaceFor - This returns the frame index of the specified virtual
154 /// register on the stack, allocating space if necessary.
155 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
157 /// removePhysReg - This method marks the specified physical register as no
158 /// longer being in use.
160 void removePhysReg(unsigned PhysReg);
162 /// spillVirtReg - This method spills the value specified by PhysReg into
163 /// the virtual register slot specified by VirtReg. It then updates the RA
164 /// data structures to indicate the fact that PhysReg is now available.
166 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
167 unsigned VirtReg, unsigned PhysReg);
169 /// spillPhysReg - This method spills the specified physical register into
170 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
171 /// true, then the request is ignored if the physical register does not
172 /// contain a virtual register.
174 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
175 unsigned PhysReg, bool OnlyVirtRegs = false);
177 /// assignVirtToPhysReg - This method updates local state so that we know
178 /// that PhysReg is the proper container for VirtReg now. The physical
179 /// register must not be used for anything else when this is called.
181 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
183 /// liberatePhysReg - Make sure the specified physical register is available
184 /// for use. If there is currently a value in it, it is either moved out of
185 /// the way or spilled to memory.
187 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
190 /// isPhysRegAvailable - Return true if the specified physical register is
191 /// free and available for use. This also includes checking to see if
192 /// aliased registers are all free...
194 bool isPhysRegAvailable(unsigned PhysReg) const;
196 /// getFreeReg - Look to see if there is a free register available in the
197 /// specified register class. If not, return 0.
199 unsigned getFreeReg(const TargetRegisterClass *RC);
201 /// getReg - Find a physical register to hold the specified virtual
202 /// register. If all compatible physical registers are used, this method
203 /// spills the last used virtual register to the stack, and uses that
206 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
209 /// reloadVirtReg - This method transforms the specified specified virtual
210 /// register use to refer to a physical register. This method may do this
211 /// in one of several ways: if the register is available in a physical
212 /// register already, it uses that physical register. If the value is not
213 /// in a physical register, and if there are physical registers available,
214 /// it loads it into a register. If register pressure is high, and it is
215 /// possible, it tries to fold the load of the virtual register into the
216 /// instruction itself. It avoids doing this if register pressure is low to
217 /// improve the chance that subsequent instructions can use the reloaded
218 /// value. This method returns the modified instruction.
220 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
224 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
229 /// getStackSpaceFor - This allocates space for the specified virtual register
230 /// to be held on the stack.
231 int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
232 // Find the location Reg would belong...
233 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
235 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
236 return I->second; // Already has space allocated?
238 // Allocate a new stack object for this spill location...
239 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
242 // Assign the slot...
243 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
248 /// removePhysReg - This method marks the specified physical register as no
249 /// longer being in use.
251 void RA::removePhysReg(unsigned PhysReg) {
252 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
254 std::vector<unsigned>::iterator It =
255 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
256 if (It != PhysRegsUseOrder.end())
257 PhysRegsUseOrder.erase(It);
261 /// spillVirtReg - This method spills the value specified by PhysReg into the
262 /// virtual register slot specified by VirtReg. It then updates the RA data
263 /// structures to indicate the fact that PhysReg is now available.
265 void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
266 unsigned VirtReg, unsigned PhysReg) {
267 assert(VirtReg && "Spilling a physical register is illegal!"
268 " Must not have appropriate kill for the register or use exists beyond"
269 " the intended one.");
270 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
271 std::cerr << " containing %reg" << VirtReg;
272 if (!isVirtRegModified(VirtReg))
273 std::cerr << " which has not been modified, so no store necessary!");
275 // Otherwise, there is a virtual register corresponding to this physical
276 // register. We only need to spill it into its stack slot if it has been
278 if (isVirtRegModified(VirtReg)) {
279 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
280 int FrameIndex = getStackSpaceFor(VirtReg, RC);
281 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
282 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
283 ++NumStores; // Update statistics
286 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
288 DEBUG(std::cerr << "\n");
289 removePhysReg(PhysReg);
293 /// spillPhysReg - This method spills the specified physical register into the
294 /// virtual register slot associated with it. If OnlyVirtRegs is set to true,
295 /// then the request is ignored if the physical register does not contain a
296 /// virtual register.
298 void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
299 unsigned PhysReg, bool OnlyVirtRegs) {
300 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
301 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
302 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
304 // If the selected register aliases any other registers, we must make
305 // sure that one of the aliases isn't alive...
306 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
307 *AliasSet; ++AliasSet)
308 if (PhysRegsUsed[*AliasSet] != -1) // Spill aliased register...
309 if (PhysRegsUsed[*AliasSet] || !OnlyVirtRegs)
310 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
315 /// assignVirtToPhysReg - This method updates local state so that we know
316 /// that PhysReg is the proper container for VirtReg now. The physical
317 /// register must not be used for anything else when this is called.
319 void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
320 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
321 // Update information to note the fact that this register was just used, and
323 PhysRegsUsed[PhysReg] = VirtReg;
324 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
325 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
329 /// isPhysRegAvailable - Return true if the specified physical register is free
330 /// and available for use. This also includes checking to see if aliased
331 /// registers are all free...
333 bool RA::isPhysRegAvailable(unsigned PhysReg) const {
334 if (PhysRegsUsed[PhysReg] != -1) return false;
336 // If the selected register aliases any other allocated registers, it is
338 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
339 *AliasSet; ++AliasSet)
340 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
341 return false; // Can't use this reg then.
346 /// getFreeReg - Look to see if there is a free register available in the
347 /// specified register class. If not, return 0.
349 unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
350 // Get iterators defining the range of registers that are valid to allocate in
351 // this class, which also specifies the preferred allocation order.
352 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
353 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
355 for (; RI != RE; ++RI)
356 if (isPhysRegAvailable(*RI)) { // Is reg unused?
357 assert(*RI != 0 && "Cannot use register!");
358 return *RI; // Found an unused register!
364 /// liberatePhysReg - Make sure the specified physical register is available for
365 /// use. If there is currently a value in it, it is either moved out of the way
366 /// or spilled to memory.
368 void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
370 spillPhysReg(MBB, I, PhysReg);
374 /// getReg - Find a physical register to hold the specified virtual
375 /// register. If all compatible physical registers are used, this method spills
376 /// the last used virtual register to the stack, and uses that register.
378 unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
380 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
382 // First check to see if we have a free register of the requested type...
383 unsigned PhysReg = getFreeReg(RC);
385 // If we didn't find an unused register, scavenge one now!
387 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
389 // Loop over all of the preallocated registers from the least recently used
390 // to the most recently used. When we find one that is capable of holding
391 // our register, use it.
392 for (unsigned i = 0; PhysReg == 0; ++i) {
393 assert(i != PhysRegsUseOrder.size() &&
394 "Couldn't find a register of the appropriate class!");
396 unsigned R = PhysRegsUseOrder[i];
398 // We can only use this register if it holds a virtual register (ie, it
399 // can be spilled). Do not use it if it is an explicitly allocated
400 // physical register!
401 assert(PhysRegsUsed[R] != -1 &&
402 "PhysReg in PhysRegsUseOrder, but is not allocated?");
403 if (PhysRegsUsed[R]) {
404 // If the current register is compatible, use it.
405 if (RC->contains(R)) {
409 // If one of the registers aliased to the current register is
410 // compatible, use it.
411 for (const unsigned *AliasIt = RegInfo->getAliasSet(R);
412 *AliasIt; ++AliasIt) {
413 if (RC->contains(*AliasIt) &&
414 // If this is pinned down for some reason, don't use it. For
415 // example, if CL is pinned, and we run across CH, don't use
416 // CH as justification for using scavenging ECX (which will
418 PhysRegsUsed[*AliasIt] != 0) {
419 PhysReg = *AliasIt; // Take an aliased register
427 assert(PhysReg && "Physical register not assigned!?!?");
429 // At this point PhysRegsUseOrder[i] is the least recently used register of
430 // compatible register class. Spill it to memory and reap its remains.
431 spillPhysReg(MBB, I, PhysReg);
434 // Now that we know which register we need to assign this to, do it now!
435 assignVirtToPhysReg(VirtReg, PhysReg);
440 /// reloadVirtReg - This method transforms the specified specified virtual
441 /// register use to refer to a physical register. This method may do this in
442 /// one of several ways: if the register is available in a physical register
443 /// already, it uses that physical register. If the value is not in a physical
444 /// register, and if there are physical registers available, it loads it into a
445 /// register. If register pressure is high, and it is possible, it tries to
446 /// fold the load of the virtual register into the instruction itself. It
447 /// avoids doing this if register pressure is low to improve the chance that
448 /// subsequent instructions can use the reloaded value. This method returns the
449 /// modified instruction.
451 MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
453 unsigned VirtReg = MI->getOperand(OpNum).getReg();
455 // If the virtual register is already available, just update the instruction
457 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
458 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
459 MI->getOperand(OpNum).setReg(PR); // Assign the input register
463 // Otherwise, we need to fold it into the current instruction, or reload it.
464 // If we have registers available to hold the value, use them.
465 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
466 unsigned PhysReg = getFreeReg(RC);
467 int FrameIndex = getStackSpaceFor(VirtReg, RC);
469 if (PhysReg) { // Register is available, allocate it!
470 assignVirtToPhysReg(VirtReg, PhysReg);
471 } else { // No registers available.
472 // If we can fold this spill into this instruction, do so now.
473 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
475 // Since we changed the address of MI, make sure to update live variables
476 // to know that the new instruction has the properties of the old one.
477 LV->instructionChanged(MI, FMI);
478 return MBB.insert(MBB.erase(MI), FMI);
481 // It looks like we can't fold this virtual register load into this
482 // instruction. Force some poor hapless value out of the register file to
483 // make room for the new register, and reload it.
484 PhysReg = getReg(MBB, MI, VirtReg);
487 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
489 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
490 << RegInfo->getName(PhysReg) << "\n");
492 // Add move instruction(s)
493 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
494 ++NumLoads; // Update statistics
496 PhysRegsEverUsed[PhysReg] = true;
497 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
503 void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
504 // loop over each instruction
505 MachineBasicBlock::iterator MII = MBB.begin();
506 const TargetInstrInfo &TII = *TM->getInstrInfo();
508 // If this is the first basic block in the machine function, add live-in
509 // registers as active.
510 if (&MBB == &*MF->begin()) {
511 for (MachineFunction::livein_iterator I = MF->livein_begin(),
512 E = MF->livein_end(); I != E; ++I) {
513 unsigned Reg = I->first;
514 PhysRegsEverUsed[Reg] = true;
515 PhysRegsUsed[Reg] = 0; // It is free and reserved now
516 PhysRegsUseOrder.push_back(Reg);
517 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
518 *AliasSet; ++AliasSet) {
519 PhysRegsUseOrder.push_back(*AliasSet);
520 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
521 PhysRegsEverUsed[*AliasSet] = true;
526 // Otherwise, sequentially allocate each instruction in the MBB.
527 while (MII != MBB.end()) {
528 MachineInstr *MI = MII++;
529 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
530 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
531 std::cerr << " Regs have values: ";
532 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
533 if (PhysRegsUsed[i] != -1)
534 std::cerr << "[" << RegInfo->getName(i)
535 << ",%reg" << PhysRegsUsed[i] << "] ";
538 // Loop over the implicit uses, making sure that they are at the head of the
539 // use order list, so they don't get reallocated.
540 if (TID.ImplicitUses) {
541 for (const unsigned *ImplicitUses = TID.ImplicitUses;
542 *ImplicitUses; ++ImplicitUses)
543 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 MachineOperand& MO = MI->getOperand(i);
554 // here we are looking for only used operands (never def&use)
555 if (!MO.isDef() && MO.isRegister() && MO.getReg() &&
556 MRegisterInfo::isVirtualRegister(MO.getReg()))
557 MI = reloadVirtReg(MBB, MI, i);
560 // If this instruction is the last user of anything in registers, kill the
561 // value, freeing the register being used, so it doesn't need to be
562 // spilled to memory.
564 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
565 KE = LV->killed_end(MI); KI != KE; ++KI) {
566 unsigned VirtReg = *KI;
567 unsigned PhysReg = VirtReg;
568 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
569 // If the virtual register was never materialized into a register, it
570 // might not be in the map, but it won't hurt to zero it out anyway.
571 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
572 PhysReg = PhysRegSlot;
577 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
578 << "[%reg" << VirtReg <<"], removing it from live set\n");
579 removePhysReg(PhysReg);
583 // Loop over all of the operands of the instruction, spilling registers that
584 // are defined, and marking explicit destinations in the PhysRegsUsed map.
585 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
586 MachineOperand& MO = MI->getOperand(i);
587 if (MO.isDef() && MO.isRegister() && MO.getReg() &&
588 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
589 unsigned Reg = MO.getReg();
590 PhysRegsEverUsed[Reg] = true;
591 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in the reg
592 PhysRegsUsed[Reg] = 0; // It is free and reserved now
593 PhysRegsUseOrder.push_back(Reg);
594 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
595 *AliasSet; ++AliasSet) {
596 PhysRegsUseOrder.push_back(*AliasSet);
597 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
598 PhysRegsEverUsed[*AliasSet] = true;
603 // Loop over the implicit defs, spilling them as well.
604 if (TID.ImplicitDefs) {
605 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
606 *ImplicitDefs; ++ImplicitDefs) {
607 unsigned Reg = *ImplicitDefs;
608 spillPhysReg(MBB, MI, Reg, true);
609 PhysRegsUseOrder.push_back(Reg);
610 PhysRegsUsed[Reg] = 0; // It is free and reserved now
611 PhysRegsEverUsed[Reg] = true;
613 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
614 *AliasSet; ++AliasSet) {
615 PhysRegsUseOrder.push_back(*AliasSet);
616 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
617 PhysRegsEverUsed[*AliasSet] = true;
622 // Okay, we have allocated all of the source operands and spilled any values
623 // that would be destroyed by defs of this instruction. Loop over the
624 // explicit defs and assign them to a register, spilling incoming values if
625 // we need to scavenge a register.
627 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
628 MachineOperand& MO = MI->getOperand(i);
629 if (MO.isDef() && MO.isRegister() && MO.getReg() &&
630 MRegisterInfo::isVirtualRegister(MO.getReg())) {
631 unsigned DestVirtReg = MO.getReg();
632 unsigned DestPhysReg;
634 // If DestVirtReg already has a value, use it.
635 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
636 DestPhysReg = getReg(MBB, MI, DestVirtReg);
637 PhysRegsEverUsed[DestPhysReg] = true;
638 markVirtRegModified(DestVirtReg);
639 MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
643 // If this instruction defines any registers that are immediately dead,
646 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
647 KE = LV->dead_end(MI); KI != KE; ++KI) {
648 unsigned VirtReg = *KI;
649 unsigned PhysReg = VirtReg;
650 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
651 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
652 PhysReg = PhysRegSlot;
653 assert(PhysReg != 0);
658 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
659 << " [%reg" << VirtReg
660 << "] is never used, removing it frame live list\n");
661 removePhysReg(PhysReg);
665 // Finally, if this is a noop copy instruction, zap it.
666 unsigned SrcReg, DstReg;
667 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) {
668 LV->removeVirtualRegistersKilled(MI);
669 LV->removeVirtualRegistersDead(MI);
674 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
676 // Spill all physical registers holding virtual registers now.
677 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
678 if (PhysRegsUsed[i] != -1)
679 if (unsigned VirtReg = PhysRegsUsed[i])
680 spillVirtReg(MBB, MI, VirtReg, i);
685 // This checking code is very expensive.
687 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
688 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
689 if (unsigned PR = Virt2PhysRegMap[i]) {
690 std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
693 assert(AllOk && "Virtual registers still in phys regs?");
696 // Clear any physical register which appear live at the end of the basic
697 // block, but which do not hold any virtual registers. e.g., the stack
699 PhysRegsUseOrder.clear();
703 /// runOnMachineFunction - Register allocate the whole function
705 bool RA::runOnMachineFunction(MachineFunction &Fn) {
706 DEBUG(std::cerr << "Machine Function " << "\n");
708 TM = &Fn.getTarget();
709 RegInfo = TM->getRegisterInfo();
710 LV = &getAnalysis<LiveVariables>();
712 PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
713 std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
714 Fn.setUsedPhysRegs(PhysRegsEverUsed);
716 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
718 // initialize the virtual->physical register map to have a 'null'
719 // mapping for all virtual registers
720 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
722 // Loop over all of the basic blocks, eliminating virtual register references
723 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
725 AllocateBasicBlock(*MBB);
727 StackSlotForVirtReg.clear();
728 PhysRegsUsed.clear();
729 VirtRegModified.clear();
730 Virt2PhysRegMap.clear();
734 FunctionPass *llvm::createLocalRegisterAllocator() {