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