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"
32 Statistic<> NumStores("ra-local", "Number of stores added");
33 Statistic<> NumLoads ("ra-local", "Number of loads added");
34 Statistic<> NumFolded("ra-local", "Number of loads/stores folded into "
36 class RA : public MachineFunctionPass {
37 const TargetMachine *TM;
39 const MRegisterInfo *RegInfo;
41 bool *PhysRegsEverUsed;
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.
49 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
51 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
52 return Virt2PhysRegMap[VirtReg];
55 // PhysRegsUsed - This array is effectively a map, containing entries for
56 // each physical register that currently has a value (ie, it is in
57 // Virt2PhysRegMap). The value mapped to is the virtual register
58 // corresponding to the physical register (the inverse of the
59 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
60 // because it is used by a future instruction. If the entry for a physical
61 // register is -1, then the physical register is "not in the map".
63 std::vector<int> PhysRegsUsed;
65 // PhysRegsUseOrder - This contains a list of the physical registers that
66 // currently have a virtual register value in them. This list provides an
67 // ordering of registers, imposing a reallocation order. This list is only
68 // used if all registers are allocated and we have to spill one, in which
69 // case we spill the least recently used register. Entries at the front of
70 // the list are the least recently used registers, entries at the back are
71 // the most recently used.
73 std::vector<unsigned> PhysRegsUseOrder;
75 // VirtRegModified - This bitset contains information about which virtual
76 // registers need to be spilled back to memory when their registers are
77 // scavenged. If a virtual register has simply been rematerialized, there
78 // is no reason to spill it to memory when we need the register back.
80 std::vector<bool> VirtRegModified;
82 void markVirtRegModified(unsigned Reg, bool Val = true) {
83 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
84 Reg -= MRegisterInfo::FirstVirtualRegister;
85 if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
86 VirtRegModified[Reg] = Val;
89 bool isVirtRegModified(unsigned Reg) const {
90 assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
91 assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
92 && "Illegal virtual register!");
93 return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
96 void MarkPhysRegRecentlyUsed(unsigned Reg) {
97 if(PhysRegsUseOrder.empty() ||
98 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
100 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
101 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
102 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
103 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
104 // Add it to the end of the list
105 PhysRegsUseOrder.push_back(RegMatch);
107 return; // Found an exact match, exit early
112 virtual const char *getPassName() const {
113 return "Local Register Allocator";
116 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
117 AU.addRequired<LiveVariables>();
118 AU.addRequiredID(PHIEliminationID);
119 AU.addRequiredID(TwoAddressInstructionPassID);
120 MachineFunctionPass::getAnalysisUsage(AU);
124 /// runOnMachineFunction - Register allocate the whole function
125 bool runOnMachineFunction(MachineFunction &Fn);
127 /// AllocateBasicBlock - Register allocate the specified basic block.
128 void AllocateBasicBlock(MachineBasicBlock &MBB);
131 /// areRegsEqual - This method returns true if the specified registers are
132 /// related to each other. To do this, it checks to see if they are equal
133 /// or if the first register is in the alias set of the second register.
135 bool areRegsEqual(unsigned R1, unsigned R2) const {
136 if (R1 == R2) return true;
137 for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
138 *AliasSet; ++AliasSet) {
139 if (*AliasSet == R1) return true;
144 /// getStackSpaceFor - This returns the frame index of the specified virtual
145 /// register on the stack, allocating space if necessary.
146 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
148 /// removePhysReg - This method marks the specified physical register as no
149 /// longer being in use.
151 void removePhysReg(unsigned PhysReg);
153 /// spillVirtReg - This method spills the value specified by PhysReg into
154 /// the virtual register slot specified by VirtReg. It then updates the RA
155 /// data structures to indicate the fact that PhysReg is now available.
157 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
158 unsigned VirtReg, unsigned PhysReg);
160 /// spillPhysReg - This method spills the specified physical register into
161 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
162 /// true, then the request is ignored if the physical register does not
163 /// contain a virtual register.
165 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
166 unsigned PhysReg, bool OnlyVirtRegs = false);
168 /// assignVirtToPhysReg - This method updates local state so that we know
169 /// that PhysReg is the proper container for VirtReg now. The physical
170 /// register must not be used for anything else when this is called.
172 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
174 /// liberatePhysReg - Make sure the specified physical register is available
175 /// for use. If there is currently a value in it, it is either moved out of
176 /// the way or spilled to memory.
178 void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
181 /// isPhysRegAvailable - Return true if the specified physical register is
182 /// free and available for use. This also includes checking to see if
183 /// aliased registers are all free...
185 bool isPhysRegAvailable(unsigned PhysReg) const;
187 /// getFreeReg - Look to see if there is a free register available in the
188 /// specified register class. If not, return 0.
190 unsigned getFreeReg(const TargetRegisterClass *RC);
192 /// getReg - Find a physical register to hold the specified virtual
193 /// register. If all compatible physical registers are used, this method
194 /// spills the last used virtual register to the stack, and uses that
197 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
200 /// reloadVirtReg - This method transforms the specified specified virtual
201 /// register use to refer to a physical register. This method may do this
202 /// in one of several ways: if the register is available in a physical
203 /// register already, it uses that physical register. If the value is not
204 /// in a physical register, and if there are physical registers available,
205 /// it loads it into a register. If register pressure is high, and it is
206 /// possible, it tries to fold the load of the virtual register into the
207 /// instruction itself. It avoids doing this if register pressure is low to
208 /// improve the chance that subsequent instructions can use the reloaded
209 /// value. This method returns the modified instruction.
211 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
215 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
220 /// getStackSpaceFor - This allocates space for the specified virtual register
221 /// to be held on the stack.
222 int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
223 // Find the location Reg would belong...
224 std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
226 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
227 return I->second; // Already has space allocated?
229 // Allocate a new stack object for this spill location...
230 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
233 // Assign the slot...
234 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
239 /// removePhysReg - This method marks the specified physical register as no
240 /// longer being in use.
242 void RA::removePhysReg(unsigned PhysReg) {
243 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
245 std::vector<unsigned>::iterator It =
246 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
247 if (It != PhysRegsUseOrder.end())
248 PhysRegsUseOrder.erase(It);
252 /// spillVirtReg - This method spills the value specified by PhysReg into the
253 /// virtual register slot specified by VirtReg. It then updates the RA data
254 /// structures to indicate the fact that PhysReg is now available.
256 void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
257 unsigned VirtReg, unsigned PhysReg) {
258 assert(VirtReg && "Spilling a physical register is illegal!"
259 " Must not have appropriate kill for the register or use exists beyond"
260 " the intended one.");
261 DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
262 std::cerr << " containing %reg" << VirtReg;
263 if (!isVirtRegModified(VirtReg))
264 std::cerr << " which has not been modified, so no store necessary!");
266 // Otherwise, there is a virtual register corresponding to this physical
267 // register. We only need to spill it into its stack slot if it has been
269 if (isVirtRegModified(VirtReg)) {
270 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
271 int FrameIndex = getStackSpaceFor(VirtReg, RC);
272 DEBUG(std::cerr << " to stack slot #" << FrameIndex);
273 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
274 ++NumStores; // Update statistics
277 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
279 DEBUG(std::cerr << "\n");
280 removePhysReg(PhysReg);
284 /// spillPhysReg - This method spills the specified physical register into the
285 /// virtual register slot associated with it. If OnlyVirtRegs is set to true,
286 /// then the request is ignored if the physical register does not contain a
287 /// virtual register.
289 void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
290 unsigned PhysReg, bool OnlyVirtRegs) {
291 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
292 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
293 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
295 // If the selected register aliases any other registers, we must make
296 // sure that one of the aliases isn't alive...
297 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
298 *AliasSet; ++AliasSet)
299 if (PhysRegsUsed[*AliasSet] != -1) // Spill aliased register...
300 if (PhysRegsUsed[*AliasSet] || !OnlyVirtRegs)
301 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
306 /// assignVirtToPhysReg - This method updates local state so that we know
307 /// that PhysReg is the proper container for VirtReg now. The physical
308 /// register must not be used for anything else when this is called.
310 void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
311 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
312 // Update information to note the fact that this register was just used, and
314 PhysRegsUsed[PhysReg] = VirtReg;
315 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
316 PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
320 /// isPhysRegAvailable - Return true if the specified physical register is free
321 /// and available for use. This also includes checking to see if aliased
322 /// registers are all free...
324 bool RA::isPhysRegAvailable(unsigned PhysReg) const {
325 if (PhysRegsUsed[PhysReg] != -1) return false;
327 // If the selected register aliases any other allocated registers, it is
329 for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
330 *AliasSet; ++AliasSet)
331 if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
332 return false; // Can't use this reg then.
337 /// getFreeReg - Look to see if there is a free register available in the
338 /// specified register class. If not, return 0.
340 unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
341 // Get iterators defining the range of registers that are valid to allocate in
342 // this class, which also specifies the preferred allocation order.
343 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
344 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
346 for (; RI != RE; ++RI)
347 if (isPhysRegAvailable(*RI)) { // Is reg unused?
348 assert(*RI != 0 && "Cannot use register!");
349 return *RI; // Found an unused register!
355 /// liberatePhysReg - Make sure the specified physical register is available for
356 /// use. If there is currently a value in it, it is either moved out of the way
357 /// or spilled to memory.
359 void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
361 spillPhysReg(MBB, I, PhysReg);
365 /// getReg - Find a physical register to hold the specified virtual
366 /// register. If all compatible physical registers are used, this method spills
367 /// the last used virtual register to the stack, and uses that register.
369 unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
371 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
373 // First check to see if we have a free register of the requested type...
374 unsigned PhysReg = getFreeReg(RC);
376 // If we didn't find an unused register, scavenge one now!
378 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
380 // Loop over all of the preallocated registers from the least recently used
381 // to the most recently used. When we find one that is capable of holding
382 // our register, use it.
383 for (unsigned i = 0; PhysReg == 0; ++i) {
384 assert(i != PhysRegsUseOrder.size() &&
385 "Couldn't find a register of the appropriate class!");
387 unsigned R = PhysRegsUseOrder[i];
389 // We can only use this register if it holds a virtual register (ie, it
390 // can be spilled). Do not use it if it is an explicitly allocated
391 // physical register!
392 assert(PhysRegsUsed[R] != -1 &&
393 "PhysReg in PhysRegsUseOrder, but is not allocated?");
394 if (PhysRegsUsed[R]) {
395 // If the current register is compatible, use it.
396 if (RC->contains(R)) {
400 // If one of the registers aliased to the current register is
401 // compatible, use it.
402 for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
403 *AliasSet; ++AliasSet) {
404 if (RC->contains(*AliasSet)) {
405 PhysReg = *AliasSet; // Take an aliased register
413 assert(PhysReg && "Physical register not assigned!?!?");
415 // At this point PhysRegsUseOrder[i] is the least recently used register of
416 // compatible register class. Spill it to memory and reap its remains.
417 spillPhysReg(MBB, I, PhysReg);
420 // Now that we know which register we need to assign this to, do it now!
421 assignVirtToPhysReg(VirtReg, PhysReg);
426 /// reloadVirtReg - This method transforms the specified specified virtual
427 /// register use to refer to a physical register. This method may do this in
428 /// one of several ways: if the register is available in a physical register
429 /// already, it uses that physical register. If the value is not in a physical
430 /// register, and if there are physical registers available, it loads it into a
431 /// register. If register pressure is high, and it is possible, it tries to
432 /// fold the load of the virtual register into the instruction itself. It
433 /// avoids doing this if register pressure is low to improve the chance that
434 /// subsequent instructions can use the reloaded value. This method returns the
435 /// modified instruction.
437 MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
439 unsigned VirtReg = MI->getOperand(OpNum).getReg();
441 // If the virtual register is already available, just update the instruction
443 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
444 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
445 MI->SetMachineOperandReg(OpNum, PR); // Assign the input register
449 // Otherwise, we need to fold it into the current instruction, or reload it.
450 // If we have registers available to hold the value, use them.
451 const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
452 unsigned PhysReg = getFreeReg(RC);
453 int FrameIndex = getStackSpaceFor(VirtReg, RC);
455 if (PhysReg) { // Register is available, allocate it!
456 assignVirtToPhysReg(VirtReg, PhysReg);
457 } else { // No registers available.
458 // If we can fold this spill into this instruction, do so now.
459 if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
461 // Since we changed the address of MI, make sure to update live variables
462 // to know that the new instruction has the properties of the old one.
463 LV->instructionChanged(MI, FMI);
464 return MBB.insert(MBB.erase(MI), FMI);
467 // It looks like we can't fold this virtual register load into this
468 // instruction. Force some poor hapless value out of the register file to
469 // make room for the new register, and reload it.
470 PhysReg = getReg(MBB, MI, VirtReg);
473 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
475 DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
476 << RegInfo->getName(PhysReg) << "\n");
478 // Add move instruction(s)
479 RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
480 ++NumLoads; // Update statistics
482 PhysRegsEverUsed[PhysReg] = true;
483 MI->SetMachineOperandReg(OpNum, PhysReg); // Assign the input register
489 void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
490 // loop over each instruction
491 MachineBasicBlock::iterator MII = MBB.begin();
492 const TargetInstrInfo &TII = *TM->getInstrInfo();
493 while (MII != MBB.end()) {
494 MachineInstr *MI = MII++;
495 const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
496 DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
497 std::cerr << " Regs have values: ";
498 for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
499 if (PhysRegsUsed[i] != -1)
500 std::cerr << "[" << RegInfo->getName(i)
501 << ",%reg" << PhysRegsUsed[i] << "] ";
504 // Loop over the implicit uses, making sure that they are at the head of the
505 // use order list, so they don't get reallocated.
506 for (const unsigned *ImplicitUses = TID.ImplicitUses;
507 *ImplicitUses; ++ImplicitUses)
508 MarkPhysRegRecentlyUsed(*ImplicitUses);
510 // Get the used operands into registers. This has the potential to spill
511 // incoming values if we are out of registers. Note that we completely
512 // ignore physical register uses here. We assume that if an explicit
513 // physical register is referenced by the instruction, that it is guaranteed
514 // to be live-in, or the input is badly hosed.
516 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
517 MachineOperand& MO = MI->getOperand(i);
518 // here we are looking for only used operands (never def&use)
519 if (!MO.isDef() && MO.isRegister() && MO.getReg() &&
520 MRegisterInfo::isVirtualRegister(MO.getReg()))
521 MI = reloadVirtReg(MBB, MI, i);
524 // If this instruction is the last user of anything in registers, kill the
525 // value, freeing the register being used, so it doesn't need to be
526 // spilled to memory.
528 for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
529 KE = LV->killed_end(MI); KI != KE; ++KI) {
530 unsigned VirtReg = *KI;
531 unsigned PhysReg = VirtReg;
532 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
533 // If the virtual register was never materialized into a register, it
534 // might not be in the map, but it won't hurt to zero it out anyway.
535 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
536 PhysReg = PhysRegSlot;
541 DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
542 << "[%reg" << VirtReg <<"], removing it from live set\n");
543 removePhysReg(PhysReg);
547 // Loop over all of the operands of the instruction, spilling registers that
548 // are defined, and marking explicit destinations in the PhysRegsUsed map.
549 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
550 MachineOperand& MO = MI->getOperand(i);
551 if (MO.isDef() && MO.isRegister() && MO.getReg() &&
552 MRegisterInfo::isPhysicalRegister(MO.getReg())) {
553 unsigned Reg = MO.getReg();
554 PhysRegsEverUsed[Reg] = true;
555 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in the reg
556 PhysRegsUsed[Reg] = 0; // It is free and reserved now
557 PhysRegsUseOrder.push_back(Reg);
558 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
559 *AliasSet; ++AliasSet) {
560 PhysRegsUseOrder.push_back(*AliasSet);
561 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
562 PhysRegsEverUsed[*AliasSet] = true;
567 // Loop over the implicit defs, spilling them as well.
568 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
569 *ImplicitDefs; ++ImplicitDefs) {
570 unsigned Reg = *ImplicitDefs;
571 spillPhysReg(MBB, MI, Reg, true);
572 PhysRegsUseOrder.push_back(Reg);
573 PhysRegsUsed[Reg] = 0; // It is free and reserved now
574 PhysRegsEverUsed[Reg] = true;
576 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
577 *AliasSet; ++AliasSet) {
578 PhysRegsUseOrder.push_back(*AliasSet);
579 PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
580 PhysRegsEverUsed[*AliasSet] = true;
584 // Okay, we have allocated all of the source operands and spilled any values
585 // that would be destroyed by defs of this instruction. Loop over the
586 // explicit defs and assign them to a register, spilling incoming values if
587 // we need to scavenge a register.
589 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
590 MachineOperand& MO = MI->getOperand(i);
591 if (MO.isDef() && MO.isRegister() && MO.getReg() &&
592 MRegisterInfo::isVirtualRegister(MO.getReg())) {
593 unsigned DestVirtReg = MO.getReg();
594 unsigned DestPhysReg;
596 // If DestVirtReg already has a value, use it.
597 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
598 DestPhysReg = getReg(MBB, MI, DestVirtReg);
599 PhysRegsEverUsed[DestPhysReg] = true;
600 markVirtRegModified(DestVirtReg);
601 MI->SetMachineOperandReg(i, DestPhysReg); // Assign the output register
605 // If this instruction defines any registers that are immediately dead,
608 for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
609 KE = LV->dead_end(MI); KI != KE; ++KI) {
610 unsigned VirtReg = *KI;
611 unsigned PhysReg = VirtReg;
612 if (MRegisterInfo::isVirtualRegister(VirtReg)) {
613 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
614 PhysReg = PhysRegSlot;
615 assert(PhysReg != 0);
620 DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
621 << " [%reg" << VirtReg
622 << "] is never used, removing it frame live list\n");
623 removePhysReg(PhysReg);
627 // Finally, if this is a noop copy instruction, zap it.
628 unsigned SrcReg, DstReg;
629 if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg)
633 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
635 // Spill all physical registers holding virtual registers now.
636 for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
637 if (PhysRegsUsed[i] != -1)
638 if (unsigned VirtReg = PhysRegsUsed[i])
639 spillVirtReg(MBB, MI, VirtReg, i);
644 // This checking code is very expensive.
646 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
647 e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
648 if (unsigned PR = Virt2PhysRegMap[i]) {
649 std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
652 assert(AllOk && "Virtual registers still in phys regs?");
655 // Clear any physical register which appear live at the end of the basic
656 // block, but which do not hold any virtual registers. e.g., the stack
658 PhysRegsUseOrder.clear();
662 /// runOnMachineFunction - Register allocate the whole function
664 bool RA::runOnMachineFunction(MachineFunction &Fn) {
665 DEBUG(std::cerr << "Machine Function " << "\n");
667 TM = &Fn.getTarget();
668 RegInfo = TM->getRegisterInfo();
669 LV = &getAnalysis<LiveVariables>();
671 PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
672 std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
673 Fn.setUsedPhysRegs(PhysRegsEverUsed);
675 PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
677 // initialize the virtual->physical register map to have a 'null'
678 // mapping for all virtual registers
679 Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
681 // Loop over all of the basic blocks, eliminating virtual register references
682 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
684 AllocateBasicBlock(*MBB);
686 StackSlotForVirtReg.clear();
687 PhysRegsUsed.clear();
688 VirtRegModified.clear();
689 Virt2PhysRegMap.clear();
693 FunctionPass *llvm::createLocalRegisterAllocator() {