Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 1 | //===-- RegAllocSimple.cpp - A simple generic register allocator --- ------===// |
| 2 | // |
| 3 | // This file implements a simple register allocator. *Very* simple. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 7 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | abe8dd5 | 2002-12-15 18:19:24 +0000 | [diff] [blame] | 8 | #include "llvm/CodeGen/MachineInstr.h" |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 9 | #include "llvm/Target/MachineInstrInfo.h" |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 10 | #include "llvm/Target/TargetMachine.h" |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 11 | #include "Support/Statistic.h" |
Chris Lattner | abe8dd5 | 2002-12-15 18:19:24 +0000 | [diff] [blame] | 12 | #include <iostream> |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 13 | #include <set> |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 14 | |
Chris Lattner | dd444f9 | 2002-12-15 18:38:59 +0000 | [diff] [blame] | 15 | /// PhysRegClassMap - Construct a mapping of physical register numbers to their |
| 16 | /// register classes. |
| 17 | /// |
| 18 | /// NOTE: This class will eventually be pulled out to somewhere shared. |
| 19 | /// |
| 20 | class PhysRegClassMap { |
| 21 | std::map<unsigned, const TargetRegisterClass*> PhysReg2RegClassMap; |
| 22 | public: |
| 23 | PhysRegClassMap(const MRegisterInfo *RI) { |
| 24 | for (MRegisterInfo::const_iterator I = RI->regclass_begin(), |
| 25 | E = RI->regclass_end(); I != E; ++I) |
| 26 | for (unsigned i=0; i < (*I)->getNumRegs(); ++i) |
| 27 | PhysReg2RegClassMap[(*I)->getRegister(i)] = *I; |
| 28 | } |
| 29 | |
| 30 | const TargetRegisterClass *operator[](unsigned Reg) { |
| 31 | assert(PhysReg2RegClassMap[Reg] && "Register is not a known physreg!"); |
| 32 | return PhysReg2RegClassMap[Reg]; |
| 33 | } |
| 34 | |
| 35 | const TargetRegisterClass *get(unsigned Reg) { return operator[](Reg); } |
| 36 | }; |
| 37 | |
| 38 | |
Misha Brukman | 59b3eed | 2002-12-13 10:42:31 +0000 | [diff] [blame] | 39 | namespace { |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 40 | Statistic<> NumSpilled ("ra-simple", "Number of registers spilled"); |
| 41 | Statistic<> NumReloaded("ra-simple", "Number of registers reloaded"); |
| 42 | |
| 43 | class RegAllocSimple : public FunctionPass { |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 44 | TargetMachine &TM; |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 45 | MachineFunction *MF; |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 46 | const MRegisterInfo *RegInfo; |
Chris Lattner | 9593fb1 | 2002-12-15 19:07:34 +0000 | [diff] [blame] | 47 | unsigned NumBytesAllocated; |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 48 | |
| 49 | // Maps SSA Regs => offsets on the stack where these values are stored |
Chris Lattner | ad44bd9 | 2002-12-15 18:15:24 +0000 | [diff] [blame] | 50 | std::map<unsigned, unsigned> VirtReg2OffsetMap; |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 51 | |
| 52 | // Maps SSA Regs => physical regs |
| 53 | std::map<unsigned, unsigned> SSA2PhysRegMap; |
Misha Brukman | dc2ec00 | 2002-12-03 23:15:19 +0000 | [diff] [blame] | 54 | |
| 55 | // Maps physical register to their register classes |
Chris Lattner | dd444f9 | 2002-12-15 18:38:59 +0000 | [diff] [blame] | 56 | PhysRegClassMap PhysRegClasses; |
Misha Brukman | d1bedcc | 2002-12-12 23:20:31 +0000 | [diff] [blame] | 57 | |
| 58 | // Made to combat the incorrect allocation of r2 = add r1, r1 |
| 59 | std::map<unsigned, unsigned> VirtReg2PhysRegMap; |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 60 | |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 61 | // RegsUsed - Keep track of what registers are currently in use. |
| 62 | std::set<unsigned> RegsUsed; |
| 63 | |
| 64 | // RegClassIdx - Maps RegClass => which index we can take a register |
| 65 | // from. Since this is a simple register allocator, when we need a register |
| 66 | // of a certain class, we just take the next available one. |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 67 | std::map<const TargetRegisterClass*, unsigned> RegClassIdx; |
| 68 | |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 69 | public: |
| 70 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 71 | RegAllocSimple(TargetMachine &tm) |
| 72 | : TM(tm), RegInfo(tm.getRegisterInfo()), PhysRegClasses(RegInfo) { |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 73 | RegsUsed.insert(RegInfo->getFramePointer()); |
| 74 | RegsUsed.insert(RegInfo->getStackPointer()); |
Misha Brukman | cea2245 | 2002-12-13 04:34:02 +0000 | [diff] [blame] | 75 | |
| 76 | cleanupAfterFunction(); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 79 | bool runOnFunction(Function &Fn) { |
| 80 | return runOnMachineFunction(MachineFunction::get(&Fn)); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | /// runOnMachineFunction - Register allocate the whole function |
| 85 | bool runOnMachineFunction(MachineFunction &Fn); |
| 86 | |
| 87 | /// AllocateBasicBlock - Register allocate the specified basic block. |
| 88 | void AllocateBasicBlock(MachineBasicBlock &MBB); |
| 89 | |
| 90 | /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions |
| 91 | /// in predecessor basic blocks. |
| 92 | void EliminatePHINodes(MachineBasicBlock &MBB); |
| 93 | |
| 94 | |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 95 | bool isAvailableReg(unsigned Reg) { |
| 96 | // assert(Reg < MRegisterInfo::FirstVirtualReg && "..."); |
| 97 | return RegsUsed.find(Reg) == RegsUsed.end(); |
| 98 | } |
| 99 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 100 | /// allocateStackSpaceFor - This allocates space for the specified virtual |
| 101 | /// register to be held on the stack. |
Misha Brukman | f514d51 | 2002-12-02 21:11:58 +0000 | [diff] [blame] | 102 | unsigned allocateStackSpaceFor(unsigned VirtReg, |
| 103 | const TargetRegisterClass *regClass); |
| 104 | |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 105 | /// Given a virtual register, returns a physical register that is currently |
| 106 | /// unused. |
| 107 | /// |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 108 | /// Side effect: marks that register as being used until manually cleared |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 109 | /// |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 110 | unsigned getFreeReg(unsigned virtualReg); |
| 111 | |
| 112 | /// Returns all `borrowed' registers back to the free pool |
| 113 | void clearAllRegs() { |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 114 | RegClassIdx.clear(); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Misha Brukman | 972b03f | 2002-12-13 11:33:22 +0000 | [diff] [blame] | 117 | /// Invalidates any references, real or implicit, to physical registers |
| 118 | /// |
| 119 | void invalidatePhysRegs(const MachineInstr *MI) { |
| 120 | unsigned Opcode = MI->getOpcode(); |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 121 | const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode); |
Misha Brukman | 972b03f | 2002-12-13 11:33:22 +0000 | [diff] [blame] | 122 | const unsigned *regs = Desc.ImplicitUses; |
| 123 | while (*regs) |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 124 | RegsUsed.insert(*regs++); |
Misha Brukman | 972b03f | 2002-12-13 11:33:22 +0000 | [diff] [blame] | 125 | |
| 126 | regs = Desc.ImplicitDefs; |
| 127 | while (*regs) |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 128 | RegsUsed.insert(*regs++); |
Misha Brukman | 972b03f | 2002-12-13 11:33:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 131 | void cleanupAfterFunction() { |
Chris Lattner | ad44bd9 | 2002-12-15 18:15:24 +0000 | [diff] [blame] | 132 | VirtReg2OffsetMap.clear(); |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 133 | SSA2PhysRegMap.clear(); |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 134 | NumBytesAllocated = 4; // FIXME: This is X86 specific |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 137 | /// Moves value from memory into that register |
| 138 | MachineBasicBlock::iterator |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 139 | moveUseToReg (MachineBasicBlock &MBB, |
Misha Brukman | 203b769 | 2002-12-13 09:54:36 +0000 | [diff] [blame] | 140 | MachineBasicBlock::iterator I, unsigned VirtReg, |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 141 | unsigned &PhysReg); |
| 142 | |
| 143 | /// Saves reg value on the stack (maps virtual register to stack value) |
| 144 | MachineBasicBlock::iterator |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 145 | saveVirtRegToStack (MachineBasicBlock &MBB, |
Misha Brukman | 203b769 | 2002-12-13 09:54:36 +0000 | [diff] [blame] | 146 | MachineBasicBlock::iterator I, unsigned VirtReg, |
Misha Brukman | dc2ec00 | 2002-12-03 23:15:19 +0000 | [diff] [blame] | 147 | unsigned PhysReg); |
| 148 | |
| 149 | MachineBasicBlock::iterator |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 150 | savePhysRegToStack (MachineBasicBlock &MBB, |
Misha Brukman | 203b769 | 2002-12-13 09:54:36 +0000 | [diff] [blame] | 151 | MachineBasicBlock::iterator I, unsigned PhysReg); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
Misha Brukman | 59b3eed | 2002-12-13 10:42:31 +0000 | [diff] [blame] | 154 | } |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 155 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 156 | /// allocateStackSpaceFor - This allocates space for the specified virtual |
| 157 | /// register to be held on the stack. |
Misha Brukman | f514d51 | 2002-12-02 21:11:58 +0000 | [diff] [blame] | 158 | unsigned RegAllocSimple::allocateStackSpaceFor(unsigned VirtReg, |
| 159 | const TargetRegisterClass *regClass) |
| 160 | { |
Chris Lattner | ad44bd9 | 2002-12-15 18:15:24 +0000 | [diff] [blame] | 161 | if (VirtReg2OffsetMap.find(VirtReg) == VirtReg2OffsetMap.end()) { |
Chris Lattner | 9593fb1 | 2002-12-15 19:07:34 +0000 | [diff] [blame] | 162 | unsigned RegSize = regClass->getDataSize(); |
| 163 | |
| 164 | // Align NumBytesAllocated. We should be using TargetData alignment stuff |
| 165 | // to determine this, but we don't know the LLVM type associated with the |
| 166 | // virtual register. Instead, just align to a multiple of the size for now. |
| 167 | NumBytesAllocated += RegSize-1; |
| 168 | NumBytesAllocated = NumBytesAllocated/RegSize*RegSize; |
| 169 | |
| 170 | // Assign the slot... |
Chris Lattner | ad44bd9 | 2002-12-15 18:15:24 +0000 | [diff] [blame] | 171 | VirtReg2OffsetMap[VirtReg] = NumBytesAllocated; |
Chris Lattner | 9593fb1 | 2002-12-15 19:07:34 +0000 | [diff] [blame] | 172 | |
| 173 | // Reserve the space! |
| 174 | NumBytesAllocated += RegSize; |
Misha Brukman | f514d51 | 2002-12-02 21:11:58 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | ad44bd9 | 2002-12-15 18:15:24 +0000 | [diff] [blame] | 176 | return VirtReg2OffsetMap[VirtReg]; |
Misha Brukman | f514d51 | 2002-12-02 21:11:58 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 179 | unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) { |
| 180 | const TargetRegisterClass* regClass = MF->getRegClass(virtualReg); |
| 181 | unsigned physReg; |
| 182 | assert(regClass); |
| 183 | if (RegClassIdx.find(regClass) != RegClassIdx.end()) { |
| 184 | unsigned regIdx = RegClassIdx[regClass]++; |
| 185 | assert(regIdx < regClass->getNumRegs() && "Not enough registers!"); |
| 186 | physReg = regClass->getRegister(regIdx); |
| 187 | } else { |
| 188 | physReg = regClass->getRegister(0); |
| 189 | // assert(physReg < regClass->getNumRegs() && "No registers in class!"); |
| 190 | RegClassIdx[regClass] = 1; |
| 191 | } |
| 192 | |
| 193 | if (isAvailableReg(physReg)) |
| 194 | return physReg; |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 195 | else |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 196 | return getFreeReg(virtualReg); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | MachineBasicBlock::iterator |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 200 | RegAllocSimple::moveUseToReg (MachineBasicBlock &MBB, |
Misha Brukman | 203b769 | 2002-12-13 09:54:36 +0000 | [diff] [blame] | 201 | MachineBasicBlock::iterator I, |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 202 | unsigned VirtReg, unsigned &PhysReg) |
| 203 | { |
| 204 | const TargetRegisterClass* regClass = MF->getRegClass(VirtReg); |
| 205 | assert(regClass); |
| 206 | |
Misha Brukman | f514d51 | 2002-12-02 21:11:58 +0000 | [diff] [blame] | 207 | unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 208 | PhysReg = getFreeReg(VirtReg); |
| 209 | |
Misha Brukman | f514d51 | 2002-12-02 21:11:58 +0000 | [diff] [blame] | 210 | // Add move instruction(s) |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 211 | ++NumReloaded; |
Chris Lattner | 198ab64 | 2002-12-15 20:06:35 +0000 | [diff] [blame] | 212 | return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg, |
Misha Brukman | f514d51 | 2002-12-02 21:11:58 +0000 | [diff] [blame] | 213 | RegInfo->getFramePointer(), |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 214 | -stackOffset, regClass->getDataSize()); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | MachineBasicBlock::iterator |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 218 | RegAllocSimple::saveVirtRegToStack (MachineBasicBlock &MBB, |
Misha Brukman | 203b769 | 2002-12-13 09:54:36 +0000 | [diff] [blame] | 219 | MachineBasicBlock::iterator I, |
Misha Brukman | dc2ec00 | 2002-12-03 23:15:19 +0000 | [diff] [blame] | 220 | unsigned VirtReg, unsigned PhysReg) |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 221 | { |
| 222 | const TargetRegisterClass* regClass = MF->getRegClass(VirtReg); |
| 223 | assert(regClass); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 224 | |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 225 | unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass); |
Misha Brukman | f514d51 | 2002-12-02 21:11:58 +0000 | [diff] [blame] | 226 | |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 227 | // Add move instruction(s) |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 228 | ++NumSpilled; |
Chris Lattner | 198ab64 | 2002-12-15 20:06:35 +0000 | [diff] [blame] | 229 | return RegInfo->storeReg2RegOffset(MBB, I, PhysReg, |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 230 | RegInfo->getFramePointer(), |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 231 | -stackOffset, regClass->getDataSize()); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Misha Brukman | dc2ec00 | 2002-12-03 23:15:19 +0000 | [diff] [blame] | 234 | MachineBasicBlock::iterator |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 235 | RegAllocSimple::savePhysRegToStack (MachineBasicBlock &MBB, |
Misha Brukman | 203b769 | 2002-12-13 09:54:36 +0000 | [diff] [blame] | 236 | MachineBasicBlock::iterator I, |
Misha Brukman | dc2ec00 | 2002-12-03 23:15:19 +0000 | [diff] [blame] | 237 | unsigned PhysReg) |
| 238 | { |
| 239 | const TargetRegisterClass* regClass = MF->getRegClass(PhysReg); |
| 240 | assert(regClass); |
| 241 | |
| 242 | unsigned offset = allocateStackSpaceFor(PhysReg, regClass); |
| 243 | |
| 244 | // Add move instruction(s) |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 245 | ++NumSpilled; |
Chris Lattner | 198ab64 | 2002-12-15 20:06:35 +0000 | [diff] [blame] | 246 | return RegInfo->storeReg2RegOffset(MBB, I, PhysReg, |
Misha Brukman | dc2ec00 | 2002-12-03 23:15:19 +0000 | [diff] [blame] | 247 | RegInfo->getFramePointer(), |
| 248 | offset, regClass->getDataSize()); |
| 249 | } |
| 250 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 251 | /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in |
| 252 | /// predecessor basic blocks. |
| 253 | void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) { |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 254 | const MachineInstrInfo &MII = TM.getInstrInfo(); |
| 255 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 256 | while (MBB.front()->getOpcode() == 0) { |
| 257 | MachineInstr *MI = MBB.front(); |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 258 | // Unlink the PHI node from the basic block... but don't delete the PHI |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 259 | MBB.erase(MBB.begin()); |
| 260 | |
| 261 | // a preliminary pass that will invalidate any registers that |
| 262 | // are used by the instruction (including implicit uses) |
| 263 | invalidatePhysRegs(MI); |
| 264 | |
| 265 | DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n"); |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 266 | DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n"); |
| 267 | MachineOperand &targetReg = MI->getOperand(0); |
| 268 | |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 269 | // If it's a virtual register, allocate a physical one otherwise, just use |
| 270 | // whatever register is there now note: it MUST be a register -- we're |
| 271 | // assigning to it! |
| 272 | // |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 273 | unsigned virtualReg = (unsigned) targetReg.getAllocatedRegNum(); |
| 274 | unsigned physReg; |
| 275 | if (targetReg.isVirtualRegister()) { |
| 276 | physReg = getFreeReg(virtualReg); |
| 277 | } else { |
| 278 | physReg = virtualReg; |
| 279 | } |
| 280 | |
| 281 | // Find the register class of the target register: should be the |
| 282 | // same as the values we're trying to store there |
| 283 | const TargetRegisterClass* regClass = PhysRegClasses[physReg]; |
| 284 | assert(regClass && "Target register class not found!"); |
| 285 | unsigned dataSize = regClass->getDataSize(); |
Chris Lattner | 3f91ad7 | 2002-12-15 20:48:03 +0000 | [diff] [blame] | 286 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 287 | for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) { |
| 288 | MachineOperand &opVal = MI->getOperand(i-1); |
| 289 | |
| 290 | // Get the MachineBasicBlock equivalent of the BasicBlock that is the |
| 291 | // source path the phi |
| 292 | MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock(); |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 3f91ad7 | 2002-12-15 20:48:03 +0000 | [diff] [blame] | 294 | // Check to make sure we haven't already emitted the copy for this block. |
| 295 | // This can happen because PHI nodes may have multiple entries for the |
| 296 | // same basic block. It doesn't matter which entry we use though, because |
| 297 | // all incoming values are guaranteed to be the same for a particular bb. |
| 298 | // |
| 299 | // Note that this is N^2 in the number of phi node entries, but since the |
| 300 | // # of entries is tiny, this is not a problem. |
| 301 | // |
| 302 | bool HaveNotEmitted = true; |
| 303 | for (int op = MI->getNumOperands() - 1; op != i; op -= 2) |
| 304 | if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) { |
| 305 | HaveNotEmitted = false; |
| 306 | break; |
| 307 | } |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 3f91ad7 | 2002-12-15 20:48:03 +0000 | [diff] [blame] | 309 | if (HaveNotEmitted) { |
| 310 | MachineBasicBlock::iterator opI = opBlock.end(); |
| 311 | MachineInstr *opMI = *--opI; |
| 312 | |
| 313 | // must backtrack over ALL the branches in the previous block |
| 314 | while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin()) |
| 315 | opMI = *--opI; |
| 316 | |
| 317 | // move back to the first branch instruction so new instructions |
| 318 | // are inserted right in front of it and not in front of a non-branch |
| 319 | if (!MII.isBranch(opMI->getOpcode())) |
| 320 | ++opI; |
| 321 | |
| 322 | // Retrieve the constant value from this op, move it to target |
| 323 | // register of the phi |
| 324 | if (opVal.isImmediate()) { |
| 325 | opI = RegInfo->moveImm2Reg(opBlock, opI, physReg, |
| 326 | (unsigned) opVal.getImmedValue(), |
| 327 | dataSize); |
| 328 | saveVirtRegToStack(opBlock, opI, virtualReg, physReg); |
| 329 | } else { |
| 330 | // Allocate a physical register and add a move in the BB |
| 331 | unsigned opVirtualReg = (unsigned) opVal.getAllocatedRegNum(); |
| 332 | unsigned opPhysReg; |
| 333 | opI = moveUseToReg(opBlock, opI, opVirtualReg, physReg); |
| 334 | |
| 335 | // Save that register value to the stack of the TARGET REG |
| 336 | saveVirtRegToStack(opBlock, opI, virtualReg, physReg); |
| 337 | } |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 338 | } |
Chris Lattner | 3f91ad7 | 2002-12-15 20:48:03 +0000 | [diff] [blame] | 339 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 340 | // make regs available to other instructions |
| 341 | clearAllRegs(); |
| 342 | } |
| 343 | |
| 344 | // really delete the instruction |
| 345 | delete MI; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | |
| 350 | void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) { |
| 351 | // Handle PHI instructions specially: add moves to each pred block |
| 352 | EliminatePHINodes(MBB); |
| 353 | |
| 354 | //loop over each basic block |
| 355 | for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) { |
| 356 | MachineInstr *MI = *I; |
| 357 | |
| 358 | // a preliminary pass that will invalidate any registers that |
| 359 | // are used by the instruction (including implicit uses) |
| 360 | invalidatePhysRegs(MI); |
| 361 | |
| 362 | // Loop over uses, move from memory into registers |
| 363 | for (int i = MI->getNumOperands() - 1; i >= 0; --i) { |
| 364 | MachineOperand &op = MI->getOperand(i); |
| 365 | |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 366 | if (op.isVirtualRegister()) { |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 367 | unsigned virtualReg = (unsigned) op.getAllocatedRegNum(); |
| 368 | DEBUG(std::cerr << "op: " << op << "\n"); |
| 369 | DEBUG(std::cerr << "\t inst[" << i << "]: "; |
| 370 | MI->print(std::cerr, TM)); |
| 371 | |
| 372 | // make sure the same virtual register maps to the same physical |
| 373 | // register in any given instruction |
| 374 | unsigned physReg; |
| 375 | if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) { |
| 376 | physReg = VirtReg2PhysRegMap[virtualReg]; |
| 377 | } else { |
| 378 | if (op.opIsDef()) { |
| 379 | if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) { |
| 380 | // must be same register number as the first operand |
| 381 | // This maps a = b + c into b += c, and saves b into a's spot |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 382 | physReg = MI->getOperand(1).getAllocatedRegNum(); |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 383 | } else { |
| 384 | physReg = getFreeReg(virtualReg); |
| 385 | } |
| 386 | MachineBasicBlock::iterator J = I; |
| 387 | J = saveVirtRegToStack(MBB, ++J, virtualReg, physReg); |
| 388 | I = --J; |
| 389 | } else { |
| 390 | I = moveUseToReg(MBB, I, virtualReg, physReg); |
| 391 | } |
| 392 | VirtReg2PhysRegMap[virtualReg] = physReg; |
| 393 | } |
| 394 | MI->SetMachineOperandReg(i, physReg); |
| 395 | DEBUG(std::cerr << "virt: " << virtualReg << |
| 396 | ", phys: " << op.getAllocatedRegNum() << "\n"); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | clearAllRegs(); |
| 401 | VirtReg2PhysRegMap.clear(); |
| 402 | } |
| 403 | } |
| 404 | |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 405 | /// runOnMachineFunction - Register allocate the whole function |
| 406 | /// |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 407 | bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) { |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 408 | DEBUG(std::cerr << "Machine Function " << "\n"); |
| 409 | MF = &Fn; |
Misha Brukman | dc2ec00 | 2002-12-03 23:15:19 +0000 | [diff] [blame] | 410 | |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 411 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 412 | MBB != MBBe; ++MBB) |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 413 | AllocateBasicBlock(*MBB); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 414 | |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 415 | // add prologue we should preserve callee-save registers... |
Chris Lattner | 198ab64 | 2002-12-15 20:06:35 +0000 | [diff] [blame] | 416 | RegInfo->emitPrologue(Fn, NumBytesAllocated); |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 417 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 418 | const MachineInstrInfo &MII = TM.getInstrInfo(); |
| 419 | |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 420 | // add epilogue to restore the callee-save registers |
| 421 | // loop over the basic block |
| 422 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 423 | MBB != MBBe; ++MBB) { |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 424 | // check if last instruction is a RET |
Chris Lattner | da7e453 | 2002-12-15 20:36:09 +0000 | [diff] [blame] | 425 | if (MII.isReturn(MBB->back()->getOpcode())) { |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 426 | // this block has a return instruction, add epilogue |
Chris Lattner | 198ab64 | 2002-12-15 20:06:35 +0000 | [diff] [blame] | 427 | RegInfo->emitEpilogue(*MBB, NumBytesAllocated); |
Misha Brukman | dd46e2a | 2002-12-04 23:58:08 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 430 | |
Chris Lattner | c2db1a9 | 2002-12-15 19:51:14 +0000 | [diff] [blame] | 431 | cleanupAfterFunction(); |
Misha Brukman | 0721867 | 2002-11-22 22:44:32 +0000 | [diff] [blame] | 432 | return false; // We never modify the LLVM itself. |
| 433 | } |
| 434 | |
| 435 | Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) { |
| 436 | return new RegAllocSimple(TM); |
| 437 | } |