Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 1 | //===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a linear scan register allocator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #define DEBUG_TYPE "regalloc" |
| 14 | #include "llvm/Function.h" |
| 15 | #include "llvm/CodeGen/LiveIntervals.h" |
| 16 | #include "llvm/CodeGen/LiveVariables.h" |
| 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/MachineInstr.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/CodeGen/SSARegMap.h" |
| 22 | #include "llvm/Target/MRegisterInfo.h" |
| 23 | #include "llvm/Target/TargetInstrInfo.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
| 26 | #include "Support/Debug.h" |
| 27 | #include "Support/DepthFirstIterator.h" |
| 28 | #include "Support/Statistic.h" |
| 29 | #include "Support/STLExtras.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | Statistic<> numSpilled ("ra-linearscan", "Number of registers spilled"); |
Chris Lattner | 5e46b51 | 2003-12-18 20:25:31 +0000 | [diff] [blame^] | 34 | Statistic<> numReloaded("ra-linearscan", "Number of registers reloaded"); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 35 | |
| 36 | class RA : public MachineFunctionPass { |
| 37 | public: |
| 38 | typedef std::vector<const LiveIntervals::Interval*> IntervalPtrs; |
| 39 | |
| 40 | private: |
| 41 | MachineFunction* mf_; |
| 42 | const TargetMachine* tm_; |
| 43 | const MRegisterInfo* mri_; |
| 44 | MachineBasicBlock* currentMbb_; |
| 45 | MachineBasicBlock::iterator currentInstr_; |
| 46 | typedef LiveIntervals::Intervals Intervals; |
| 47 | const Intervals* li_; |
| 48 | IntervalPtrs active_, inactive_; |
| 49 | |
| 50 | typedef std::vector<unsigned> Regs; |
| 51 | Regs tempUseOperands_; |
| 52 | Regs tempDefOperands_; |
| 53 | |
| 54 | Regs reserved_; |
| 55 | |
| 56 | typedef LiveIntervals::MachineBasicBlockPtrs MachineBasicBlockPtrs; |
| 57 | MachineBasicBlockPtrs mbbs_; |
| 58 | |
| 59 | typedef std::vector<unsigned> Phys2VirtMap; |
| 60 | Phys2VirtMap p2vMap_; |
| 61 | |
| 62 | typedef std::map<unsigned, unsigned> Virt2PhysMap; |
| 63 | Virt2PhysMap v2pMap_; |
| 64 | |
| 65 | typedef std::map<unsigned, int> Virt2StackSlotMap; |
| 66 | Virt2StackSlotMap v2ssMap_; |
| 67 | |
| 68 | int instrAdded_; |
| 69 | |
| 70 | public: |
| 71 | virtual const char* getPassName() const { |
| 72 | return "Linear Scan Register Allocator"; |
| 73 | } |
| 74 | |
| 75 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 76 | AU.addRequired<LiveVariables>(); |
| 77 | AU.addRequired<LiveIntervals>(); |
| 78 | MachineFunctionPass::getAnalysisUsage(AU); |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | /// runOnMachineFunction - register allocate the whole function |
| 83 | bool runOnMachineFunction(MachineFunction&); |
| 84 | |
Alkis Evlogimenos | a71e05a | 2003-12-18 13:15:02 +0000 | [diff] [blame] | 85 | /// verifyIntervals - verify that we have no inconsistencies |
| 86 | /// in the register assignments we have in active and inactive |
| 87 | /// lists |
| 88 | bool verifyIntervals(); |
| 89 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 90 | /// processActiveIntervals - expire old intervals and move |
| 91 | /// non-overlapping ones to the incative list |
| 92 | void processActiveIntervals(Intervals::const_iterator cur); |
| 93 | |
| 94 | /// processInactiveIntervals - expire old intervals and move |
| 95 | /// overlapping ones to the active list |
| 96 | void processInactiveIntervals(Intervals::const_iterator cur); |
| 97 | |
| 98 | /// assignStackSlotAtInterval - choose and spill |
| 99 | /// interval. Currently we spill the interval with the last |
| 100 | /// end point in the active and inactive lists and the current |
| 101 | /// interval |
| 102 | void assignStackSlotAtInterval(Intervals::const_iterator cur); |
| 103 | |
| 104 | /// |
| 105 | /// register handling helpers |
| 106 | /// |
| 107 | |
| 108 | /// reservePhysReg - reserves a physical register and spills |
| 109 | /// any value assigned to it if any |
| 110 | void reservePhysReg(unsigned reg); |
| 111 | |
| 112 | /// clearReservedPhysReg - marks pysical register as free for |
| 113 | /// use |
| 114 | void clearReservedPhysReg(unsigned reg); |
| 115 | |
| 116 | /// physRegAvailable - returns true if the specifed physical |
| 117 | /// register is available |
| 118 | bool physRegAvailable(unsigned physReg); |
| 119 | |
| 120 | /// getFreePhysReg - return a free physical register for this |
| 121 | /// virtual register if we have one, otherwise return 0 |
| 122 | unsigned getFreePhysReg(unsigned virtReg); |
| 123 | |
| 124 | |
| 125 | /// tempPhysRegAvailable - returns true if the specifed |
| 126 | /// temporary physical register is available |
| 127 | bool tempPhysRegAvailable(unsigned physReg); |
| 128 | |
| 129 | /// getFreeTempPhysReg - return a free temprorary physical |
| 130 | /// register for this register class if we have one (should |
| 131 | /// never return 0) |
| 132 | unsigned getFreeTempPhysReg(const TargetRegisterClass* rc); |
| 133 | |
| 134 | /// getFreeTempPhysReg - return a free temprorary physical |
| 135 | /// register for this virtual register if we have one (should |
| 136 | /// never return 0) |
| 137 | unsigned getFreeTempPhysReg(unsigned virtReg) { |
| 138 | const TargetRegisterClass* rc = |
| 139 | mf_->getSSARegMap()->getRegClass(virtReg); |
| 140 | return getFreeTempPhysReg(rc); |
| 141 | } |
| 142 | |
| 143 | /// assignVirt2PhysReg - assigns the free physical register to |
| 144 | /// the virtual register passed as arguments |
| 145 | void assignVirt2PhysReg(unsigned virtReg, unsigned physReg); |
| 146 | |
| 147 | /// clearVirtReg - free the physical register associated with this |
| 148 | /// virtual register and disassociate virtual->physical and |
| 149 | /// physical->virtual mappings |
| 150 | void clearVirtReg(unsigned virtReg); |
| 151 | |
| 152 | /// assignVirt2StackSlot - assigns this virtual register to a |
| 153 | /// stack slot |
| 154 | void assignVirt2StackSlot(unsigned virtReg); |
| 155 | |
Alkis Evlogimenos | 69546d5 | 2003-12-04 03:57:28 +0000 | [diff] [blame] | 156 | /// getStackSlot - returns the offset of the specified |
| 157 | /// register on the stack |
| 158 | int getStackSlot(unsigned virtReg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 159 | |
| 160 | /// spillVirtReg - spills the virtual register |
| 161 | void spillVirtReg(unsigned virtReg); |
| 162 | |
| 163 | /// loadPhysReg - loads to the physical register the value of |
| 164 | /// the virtual register specifed. Virtual register must have |
| 165 | /// an assigned stack slot |
| 166 | void loadVirt2PhysReg(unsigned virtReg, unsigned physReg); |
| 167 | |
| 168 | void printVirt2PhysMap() const { |
| 169 | std::cerr << "allocated registers:\n"; |
| 170 | for (Virt2PhysMap::const_iterator |
| 171 | i = v2pMap_.begin(), e = v2pMap_.end(); i != e; ++i) { |
| 172 | std::cerr << '[' << i->first << ',' |
| 173 | << mri_->getName(i->second) << "]\n"; |
| 174 | } |
| 175 | std::cerr << '\n'; |
| 176 | } |
| 177 | void printIntervals(const char* const str, |
| 178 | RA::IntervalPtrs::const_iterator i, |
| 179 | RA::IntervalPtrs::const_iterator e) const { |
| 180 | if (str) std::cerr << str << " intervals:\n"; |
| 181 | for (; i != e; ++i) { |
| 182 | std::cerr << "\t\t" << **i << " -> "; |
| 183 | if ((*i)->reg < MRegisterInfo::FirstVirtualRegister) { |
| 184 | std::cerr << mri_->getName((*i)->reg); |
| 185 | } |
| 186 | else { |
| 187 | std::cerr << mri_->getName(v2pMap_.find((*i)->reg)->second); |
| 188 | } |
| 189 | std::cerr << '\n'; |
| 190 | } |
| 191 | } |
| 192 | }; |
| 193 | } |
| 194 | |
| 195 | bool RA::runOnMachineFunction(MachineFunction &fn) { |
| 196 | mf_ = &fn; |
| 197 | tm_ = &fn.getTarget(); |
| 198 | mri_ = tm_->getRegisterInfo(); |
| 199 | li_ = &getAnalysis<LiveIntervals>().getIntervals(); |
| 200 | active_.clear(); |
| 201 | inactive_.clear(); |
| 202 | mbbs_ = getAnalysis<LiveIntervals>().getOrderedMachineBasicBlockPtrs(); |
| 203 | p2vMap_.resize(MRegisterInfo::FirstVirtualRegister-1); |
| 204 | p2vMap_.clear(); |
| 205 | v2pMap_.clear(); |
| 206 | v2ssMap_.clear(); |
| 207 | |
Alkis Evlogimenos | 5858707 | 2003-11-30 23:40:39 +0000 | [diff] [blame] | 208 | DEBUG( |
Alkis Evlogimenos | f6e610c | 2003-12-13 05:48:57 +0000 | [diff] [blame] | 209 | unsigned i = 0; |
Alkis Evlogimenos | 5858707 | 2003-11-30 23:40:39 +0000 | [diff] [blame] | 210 | for (MachineBasicBlockPtrs::iterator |
| 211 | mbbi = mbbs_.begin(), mbbe = mbbs_.end(); |
| 212 | mbbi != mbbe; ++mbbi) { |
| 213 | MachineBasicBlock* mbb = *mbbi; |
| 214 | std::cerr << mbb->getBasicBlock()->getName() << '\n'; |
| 215 | for (MachineBasicBlock::iterator |
| 216 | ii = mbb->begin(), ie = mbb->end(); |
| 217 | ii != ie; ++ii) { |
| 218 | MachineInstr* instr = *ii; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 219 | |
Alkis Evlogimenos | f6e610c | 2003-12-13 05:48:57 +0000 | [diff] [blame] | 220 | std::cerr << i++ << "\t"; |
Alkis Evlogimenos | 5858707 | 2003-11-30 23:40:39 +0000 | [diff] [blame] | 221 | instr->print(std::cerr, *tm_); |
| 222 | } |
| 223 | } |
| 224 | ); |
| 225 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 226 | // FIXME: this will work only for the X86 backend. I need to |
| 227 | // device an algorthm to select the minimal (considering register |
| 228 | // aliasing) number of temp registers to reserve so that we have 2 |
| 229 | // registers for each register class available. |
| 230 | |
| 231 | // reserve R32: EDI, EBX, |
| 232 | // R16: DI, BX, |
Alkis Evlogimenos | a3d0e5c | 2003-12-18 13:12:18 +0000 | [diff] [blame] | 233 | // R8: BH, BL |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 234 | // RFP: FP5, FP6 |
| 235 | reserved_.push_back(19); /* EDI */ |
| 236 | reserved_.push_back(17); /* EBX */ |
| 237 | reserved_.push_back(12); /* DI */ |
| 238 | reserved_.push_back( 7); /* BX */ |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 239 | reserved_.push_back( 4); /* BH */ |
Alkis Evlogimenos | a3d0e5c | 2003-12-18 13:12:18 +0000 | [diff] [blame] | 240 | reserved_.push_back( 5); /* BL */ |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 241 | reserved_.push_back(28); /* FP5 */ |
| 242 | reserved_.push_back(29); /* FP6 */ |
| 243 | |
| 244 | // liner scan algorithm |
| 245 | for (Intervals::const_iterator |
| 246 | i = li_->begin(), e = li_->end(); i != e; ++i) { |
| 247 | DEBUG(std::cerr << "processing current interval: " << *i << '\n'); |
| 248 | |
| 249 | DEBUG(printIntervals("\tactive", active_.begin(), active_.end())); |
| 250 | DEBUG(printIntervals("\tinactive", inactive_.begin(), inactive_.end())); |
Alkis Evlogimenos | a71e05a | 2003-12-18 13:15:02 +0000 | [diff] [blame] | 251 | assert(verifyIntervals()); |
| 252 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 253 | processActiveIntervals(i); |
| 254 | // processInactiveIntervals(i); |
| 255 | |
| 256 | // if this register is preallocated, look for an interval that |
| 257 | // overlaps with it and assign it to a memory location |
| 258 | if (i->reg < MRegisterInfo::FirstVirtualRegister) { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 259 | reservePhysReg(i->reg); |
| 260 | active_.push_back(&*i); |
| 261 | } |
| 262 | // otherwise we are allocating a virtual register. try to find |
| 263 | // a free physical register or spill an interval in order to |
| 264 | // assign it one (we could spill the current though). |
| 265 | else { |
| 266 | unsigned physReg = getFreePhysReg(i->reg); |
| 267 | if (!physReg) { |
| 268 | assignStackSlotAtInterval(i); |
| 269 | } |
| 270 | else { |
| 271 | assignVirt2PhysReg(i->reg, physReg); |
| 272 | active_.push_back(&*i); |
| 273 | } |
| 274 | } |
| 275 | } |
Alkis Evlogimenos | 7d65a12 | 2003-12-13 05:50:19 +0000 | [diff] [blame] | 276 | // expire any remaining active intervals |
| 277 | for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) { |
| 278 | unsigned reg = (*i)->reg; |
| 279 | DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n"); |
| 280 | if (reg < MRegisterInfo::FirstVirtualRegister) { |
| 281 | clearReservedPhysReg(reg); |
| 282 | } |
| 283 | else { |
| 284 | p2vMap_[v2pMap_[reg]] = 0; |
| 285 | } |
| 286 | // remove interval from active |
| 287 | } |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 288 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 289 | DEBUG(std::cerr << "finished register allocation\n"); |
| 290 | DEBUG(printVirt2PhysMap()); |
| 291 | |
| 292 | DEBUG(std::cerr << "Rewrite machine code:\n"); |
| 293 | for (MachineBasicBlockPtrs::iterator |
| 294 | mbbi = mbbs_.begin(), mbbe = mbbs_.end(); mbbi != mbbe; ++mbbi) { |
| 295 | instrAdded_ = 0; |
| 296 | currentMbb_ = *mbbi; |
| 297 | |
| 298 | for (currentInstr_ = currentMbb_->begin(); |
| 299 | currentInstr_ != currentMbb_->end(); ++currentInstr_) { |
| 300 | |
| 301 | DEBUG(std::cerr << "\tinstruction: "; |
| 302 | (*currentInstr_)->print(std::cerr, *tm_);); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 303 | |
| 304 | // use our current mapping and actually replace and |
| 305 | // virtual register with its allocated physical registers |
| 306 | DEBUG(std::cerr << "\t\treplacing virtual registers with mapped " |
| 307 | "physical registers:\n"); |
| 308 | for (unsigned i = 0, e = (*currentInstr_)->getNumOperands(); |
| 309 | i != e; ++i) { |
| 310 | MachineOperand& op = (*currentInstr_)->getOperand(i); |
| 311 | if (op.isVirtualRegister()) { |
| 312 | unsigned virtReg = op.getAllocatedRegNum(); |
| 313 | unsigned physReg = v2pMap_[virtReg]; |
| 314 | // if this virtual registers lives on the stack, |
| 315 | // load it to a temporary physical register |
| 316 | if (physReg) { |
| 317 | DEBUG(std::cerr << "\t\t\t%reg" << virtReg |
| 318 | << " -> " << mri_->getName(physReg) << '\n'); |
| 319 | (*currentInstr_)->SetMachineOperandReg(i, physReg); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | DEBUG(std::cerr << "\t\tloading temporarily used operands to " |
| 325 | "registers:\n"); |
| 326 | for (unsigned i = 0, e = (*currentInstr_)->getNumOperands(); |
| 327 | i != e; ++i) { |
| 328 | MachineOperand& op = (*currentInstr_)->getOperand(i); |
Alkis Evlogimenos | a71e05a | 2003-12-18 13:15:02 +0000 | [diff] [blame] | 329 | if (op.isVirtualRegister() && op.isUse() && !op.isDef()) { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 330 | unsigned virtReg = op.getAllocatedRegNum(); |
| 331 | unsigned physReg = v2pMap_[virtReg]; |
| 332 | if (!physReg) { |
| 333 | physReg = getFreeTempPhysReg(virtReg); |
| 334 | } |
| 335 | loadVirt2PhysReg(virtReg, physReg); |
| 336 | tempUseOperands_.push_back(virtReg); |
| 337 | (*currentInstr_)->SetMachineOperandReg(i, physReg); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | DEBUG(std::cerr << "\t\tclearing temporarily used operands:\n"); |
| 342 | for (unsigned i = 0, e = tempUseOperands_.size(); i != e; ++i) { |
| 343 | clearVirtReg(tempUseOperands_[i]); |
| 344 | } |
| 345 | tempUseOperands_.clear(); |
| 346 | |
| 347 | DEBUG(std::cerr << "\t\tassigning temporarily defined operands to " |
| 348 | "registers:\n"); |
| 349 | for (unsigned i = 0, e = (*currentInstr_)->getNumOperands(); |
| 350 | i != e; ++i) { |
| 351 | MachineOperand& op = (*currentInstr_)->getOperand(i); |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 352 | if (op.isVirtualRegister() && op.isDef()) { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 353 | unsigned virtReg = op.getAllocatedRegNum(); |
| 354 | unsigned physReg = v2pMap_[virtReg]; |
| 355 | if (!physReg) { |
| 356 | physReg = getFreeTempPhysReg(virtReg); |
| 357 | } |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 358 | if (op.isUse()) { // def and use |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 359 | loadVirt2PhysReg(virtReg, physReg); |
| 360 | } |
| 361 | else { |
| 362 | assignVirt2PhysReg(virtReg, physReg); |
| 363 | } |
| 364 | tempDefOperands_.push_back(virtReg); |
| 365 | (*currentInstr_)->SetMachineOperandReg(i, physReg); |
| 366 | } |
| 367 | } |
| 368 | |
Alkis Evlogimenos | 5858707 | 2003-11-30 23:40:39 +0000 | [diff] [blame] | 369 | DEBUG(std::cerr << "\t\tspilling temporarily defined operands " |
| 370 | "of this instruction:\n"); |
| 371 | ++currentInstr_; // we want to insert after this instruction |
| 372 | for (unsigned i = 0, e = tempDefOperands_.size(); i != e; ++i) { |
| 373 | spillVirtReg(tempDefOperands_[i]); |
| 374 | } |
| 375 | --currentInstr_; // restore currentInstr_ iterator |
| 376 | tempDefOperands_.clear(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | for (unsigned i = 0, e = p2vMap_.size(); i != e; ++i) { |
| 380 | assert(p2vMap_[i] != i && |
| 381 | "reserved physical registers at end of basic block?"); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return true; |
| 386 | } |
| 387 | |
Alkis Evlogimenos | a71e05a | 2003-12-18 13:15:02 +0000 | [diff] [blame] | 388 | bool RA::verifyIntervals() |
| 389 | { |
| 390 | std::set<unsigned> assignedRegisters; |
| 391 | for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) { |
| 392 | if ((*i)->reg >= MRegisterInfo::FirstVirtualRegister) { |
| 393 | unsigned reg = v2pMap_.find((*i)->reg)->second; |
| 394 | |
| 395 | bool inserted = assignedRegisters.insert(reg).second; |
| 396 | assert(inserted && "registers in active list conflict"); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) { |
| 401 | unsigned reg = (*i)->reg; |
| 402 | if (reg >= MRegisterInfo::FirstVirtualRegister) { |
| 403 | reg = v2pMap_.find((*i)->reg)->second; |
| 404 | } |
| 405 | |
| 406 | for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) { |
| 407 | assert(assignedRegisters.find(*as) == assignedRegisters.end() && |
| 408 | "registers in active list alias each other"); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // TODO: add checks between active and inactive and make sure we |
| 413 | // do not overlap anywhere |
| 414 | return true; |
| 415 | } |
| 416 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 417 | void RA::processActiveIntervals(Intervals::const_iterator cur) |
| 418 | { |
| 419 | DEBUG(std::cerr << "\tprocessing active intervals:\n"); |
| 420 | for (IntervalPtrs::iterator i = active_.begin(); i != active_.end();) { |
| 421 | unsigned reg = (*i)->reg; |
| 422 | // remove expired intervals. we expire earlier because this if |
| 423 | // an interval expires this is going to be the last use. in |
| 424 | // this case we can reuse the register for a def in the same |
| 425 | // instruction |
Alkis Evlogimenos | 485ec3c | 2003-12-18 08:56:11 +0000 | [diff] [blame] | 426 | if ((*i)->expiredAt(cur->start() + 1)) { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 427 | DEBUG(std::cerr << "\t\tinterval " << **i << " expired\n"); |
| 428 | if (reg < MRegisterInfo::FirstVirtualRegister) { |
| 429 | clearReservedPhysReg(reg); |
| 430 | } |
| 431 | else { |
| 432 | p2vMap_[v2pMap_[reg]] = 0; |
| 433 | } |
| 434 | // remove interval from active |
| 435 | i = active_.erase(i); |
| 436 | } |
| 437 | // move not active intervals to inactive list |
| 438 | // else if (!(*i)->overlaps(curIndex)) { |
| 439 | // DEBUG(std::cerr << "\t\t\tinterval " << **i << " inactive\n"); |
| 440 | // unmarkReg(virtReg); |
| 441 | // // add interval to inactive |
| 442 | // inactive_.push_back(*i); |
| 443 | // // remove interval from active |
| 444 | // i = active_.erase(i); |
| 445 | // } |
| 446 | else { |
| 447 | ++i; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | void RA::processInactiveIntervals(Intervals::const_iterator cur) |
| 453 | { |
| 454 | // DEBUG(std::cerr << "\tprocessing inactive intervals:\n"); |
| 455 | // for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end();) { |
| 456 | // unsigned virtReg = (*i)->reg; |
| 457 | // // remove expired intervals |
| 458 | // if ((*i)->expired(curIndex)) { |
| 459 | // DEBUG(std::cerr << "\t\t\tinterval " << **i << " expired\n"); |
| 460 | // freePhysReg(virtReg); |
| 461 | // // remove from inactive |
| 462 | // i = inactive_.erase(i); |
| 463 | // } |
| 464 | // // move re-activated intervals in active list |
| 465 | // else if ((*i)->overlaps(curIndex)) { |
| 466 | // DEBUG(std::cerr << "\t\t\tinterval " << **i << " active\n"); |
| 467 | // markReg(virtReg); |
| 468 | // // add to active |
| 469 | // active_.push_back(*i); |
| 470 | // // remove from inactive |
| 471 | // i = inactive_.erase(i); |
| 472 | // } |
| 473 | // else { |
| 474 | // ++i; |
| 475 | // } |
| 476 | // } |
| 477 | } |
| 478 | |
| 479 | void RA::assignStackSlotAtInterval(Intervals::const_iterator cur) |
| 480 | { |
| 481 | DEBUG(std::cerr << "\t\tassigning stack slot at interval " |
| 482 | << *cur << ":\n"); |
| 483 | assert(!active_.empty() && |
| 484 | "active set cannot be empty when choosing a register to spill"); |
| 485 | const TargetRegisterClass* rcCur = |
| 486 | mf_->getSSARegMap()->getRegClass(cur->reg); |
| 487 | |
| 488 | // find the interval for a virtual register that ends last in |
| 489 | // active and belongs to the same register class as the current |
| 490 | // interval |
| 491 | IntervalPtrs::iterator lastEndActive = active_.begin(); |
| 492 | for (IntervalPtrs::iterator e = active_.end(); |
| 493 | lastEndActive != e; ++lastEndActive) { |
| 494 | if ((*lastEndActive)->reg >= MRegisterInfo::FirstVirtualRegister) { |
| 495 | const TargetRegisterClass* rc = |
| 496 | mri_->getRegClass(v2pMap_[(*lastEndActive)->reg]); |
| 497 | if (rcCur == rc) { |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | for (IntervalPtrs::iterator i = lastEndActive, e = active_.end(); |
| 503 | i != e; ++i) { |
| 504 | if ((*i)->reg >= MRegisterInfo::FirstVirtualRegister) { |
| 505 | const TargetRegisterClass* rc = |
| 506 | mri_->getRegClass(v2pMap_[(*i)->reg]); |
| 507 | if (rcCur == rc && |
| 508 | (*lastEndActive)->end() < (*i)->end()) { |
| 509 | lastEndActive = i; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // find the interval for a virtual register that ends last in |
| 515 | // inactive and belongs to the same register class as the current |
| 516 | // interval |
| 517 | IntervalPtrs::iterator lastEndInactive = inactive_.begin(); |
| 518 | for (IntervalPtrs::iterator e = inactive_.end(); |
| 519 | lastEndInactive != e; ++lastEndInactive) { |
| 520 | if ((*lastEndInactive)->reg >= MRegisterInfo::FirstVirtualRegister) { |
| 521 | const TargetRegisterClass* rc = |
| 522 | mri_->getRegClass(v2pMap_[(*lastEndInactive)->reg]); |
| 523 | if (rcCur == rc) { |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | for (IntervalPtrs::iterator i = lastEndInactive, e = inactive_.end(); |
| 529 | i != e; ++i) { |
| 530 | if ((*i)->reg >= MRegisterInfo::FirstVirtualRegister) { |
| 531 | const TargetRegisterClass* rc = |
| 532 | mri_->getRegClass(v2pMap_[(*i)->reg]); |
| 533 | if (rcCur == rc && |
| 534 | (*lastEndInactive)->end() < (*i)->end()) { |
| 535 | lastEndInactive = i; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | unsigned lastEndActiveInactive = 0; |
| 541 | if (lastEndActive != active_.end() && |
| 542 | lastEndActiveInactive < (*lastEndActive)->end()) { |
| 543 | lastEndActiveInactive = (*lastEndActive)->end(); |
| 544 | } |
| 545 | if (lastEndInactive != inactive_.end() && |
| 546 | lastEndActiveInactive < (*lastEndInactive)->end()) { |
| 547 | lastEndActiveInactive = (*lastEndInactive)->end(); |
| 548 | } |
| 549 | |
| 550 | if (lastEndActiveInactive > cur->end()) { |
| 551 | if (lastEndInactive == inactive_.end() || |
| 552 | (*lastEndActive)->end() > (*lastEndInactive)->end()) { |
| 553 | assignVirt2StackSlot((*lastEndActive)->reg); |
| 554 | active_.erase(lastEndActive); |
| 555 | } |
| 556 | else { |
| 557 | assignVirt2StackSlot((*lastEndInactive)->reg); |
| 558 | inactive_.erase(lastEndInactive); |
| 559 | } |
| 560 | unsigned physReg = getFreePhysReg(cur->reg); |
| 561 | assert(physReg && "no free physical register after spill?"); |
| 562 | assignVirt2PhysReg(cur->reg, physReg); |
| 563 | active_.push_back(&*cur); |
| 564 | } |
| 565 | else { |
| 566 | assignVirt2StackSlot(cur->reg); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | void RA::reservePhysReg(unsigned physReg) |
| 571 | { |
Alkis Evlogimenos | 49787e3 | 2003-12-05 11:17:55 +0000 | [diff] [blame] | 572 | DEBUG(std::cerr << "\t\t\treserving physical register: " |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 573 | << mri_->getName(physReg) << '\n'); |
| 574 | // if this register holds a value spill it |
| 575 | unsigned virtReg = p2vMap_[physReg]; |
| 576 | if (virtReg != 0) { |
| 577 | assert(virtReg != physReg && "reserving an already reserved phus reg?"); |
| 578 | // remove interval from active |
| 579 | for (IntervalPtrs::iterator i = active_.begin(), e = active_.end(); |
| 580 | i != e; ++i) { |
| 581 | if ((*i)->reg == virtReg) { |
| 582 | active_.erase(i); |
| 583 | break; |
| 584 | } |
| 585 | } |
Alkis Evlogimenos | 49787e3 | 2003-12-05 11:17:55 +0000 | [diff] [blame] | 586 | assignVirt2StackSlot(virtReg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 587 | } |
| 588 | p2vMap_[physReg] = physReg; // this denotes a reserved physical register |
Alkis Evlogimenos | 94743e4 | 2003-12-13 11:58:10 +0000 | [diff] [blame] | 589 | |
| 590 | // if it also aliases any other registers with values spill them too |
| 591 | for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as) { |
| 592 | unsigned virtReg = p2vMap_[*as]; |
| 593 | if (virtReg != 0 && virtReg != *as) { |
| 594 | // remove interval from active |
| 595 | for (IntervalPtrs::iterator i = active_.begin(), e = active_.end(); |
| 596 | i != e; ++i) { |
| 597 | if ((*i)->reg == virtReg) { |
| 598 | active_.erase(i); |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | assignVirt2StackSlot(virtReg); |
| 603 | } |
| 604 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void RA::clearReservedPhysReg(unsigned physReg) |
| 608 | { |
Alkis Evlogimenos | 49787e3 | 2003-12-05 11:17:55 +0000 | [diff] [blame] | 609 | DEBUG(std::cerr << "\t\t\tclearing reserved physical register: " |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 610 | << mri_->getName(physReg) << '\n'); |
| 611 | assert(p2vMap_[physReg] == physReg && |
| 612 | "attempt to clear a non reserved physical register"); |
| 613 | p2vMap_[physReg] = 0; |
| 614 | } |
| 615 | |
| 616 | bool RA::physRegAvailable(unsigned physReg) |
| 617 | { |
| 618 | if (p2vMap_[physReg]) { |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | // if it aliases other registers it is still not free |
| 623 | for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as) { |
| 624 | if (p2vMap_[*as]) { |
| 625 | return false; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | // if it is one of the reserved registers it is still not free |
| 630 | if (find(reserved_.begin(), reserved_.end(), physReg) != reserved_.end()) { |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | return true; |
| 635 | } |
| 636 | |
| 637 | unsigned RA::getFreePhysReg(unsigned virtReg) |
| 638 | { |
| 639 | DEBUG(std::cerr << "\t\tgetting free physical register: "); |
| 640 | const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(virtReg); |
| 641 | TargetRegisterClass::iterator reg = rc->allocation_order_begin(*mf_); |
| 642 | TargetRegisterClass::iterator regEnd = rc->allocation_order_end(*mf_); |
| 643 | |
| 644 | for (; reg != regEnd; ++reg) { |
| 645 | if (physRegAvailable(*reg)) { |
| 646 | assert(*reg != 0 && "Cannot use register!"); |
| 647 | DEBUG(std::cerr << mri_->getName(*reg) << '\n'); |
| 648 | return *reg; // Found an unused register! |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | DEBUG(std::cerr << "no free register\n"); |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | bool RA::tempPhysRegAvailable(unsigned physReg) |
| 657 | { |
| 658 | assert(find(reserved_.begin(), reserved_.end(), physReg) != reserved_.end() |
| 659 | && "cannot call this method with a non reserved temp register"); |
| 660 | |
| 661 | if (p2vMap_[physReg]) { |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | // if it aliases other registers it is still not free |
| 666 | for (const unsigned* as = mri_->getAliasSet(physReg); *as; ++as) { |
| 667 | if (p2vMap_[*as]) { |
| 668 | return false; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | unsigned RA::getFreeTempPhysReg(const TargetRegisterClass* rc) |
| 676 | { |
| 677 | DEBUG(std::cerr << "\t\tgetting free temporary physical register: "); |
| 678 | |
| 679 | for (Regs::const_iterator |
| 680 | reg = reserved_.begin(), regEnd = reserved_.end(); |
| 681 | reg != regEnd; ++reg) { |
| 682 | if (rc == mri_->getRegClass(*reg) && tempPhysRegAvailable(*reg)) { |
| 683 | assert(*reg != 0 && "Cannot use register!"); |
| 684 | DEBUG(std::cerr << mri_->getName(*reg) << '\n'); |
| 685 | return *reg; // Found an unused register! |
| 686 | } |
| 687 | } |
| 688 | assert(0 && "no free temporary physical register?"); |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | void RA::assignVirt2PhysReg(unsigned virtReg, unsigned physReg) |
| 693 | { |
| 694 | assert((physRegAvailable(physReg) || |
| 695 | find(reserved_.begin(), |
| 696 | reserved_.end(), |
| 697 | physReg) != reserved_.end()) && |
| 698 | "attempt to allocate to a not available physical register"); |
| 699 | v2pMap_[virtReg] = physReg; |
| 700 | p2vMap_[physReg] = virtReg; |
| 701 | } |
| 702 | |
| 703 | void RA::clearVirtReg(unsigned virtReg) |
| 704 | { |
| 705 | Virt2PhysMap::iterator it = v2pMap_.find(virtReg); |
| 706 | assert(it != v2pMap_.end() && |
| 707 | "attempting to clear a not allocated virtual register"); |
| 708 | unsigned physReg = it->second; |
| 709 | p2vMap_[physReg] = 0; |
| 710 | v2pMap_[virtReg] = 0; // this marks that this virtual register |
| 711 | // lives on the stack |
| 712 | DEBUG(std::cerr << "\t\t\tcleared register " << mri_->getName(physReg) |
| 713 | << "\n"); |
| 714 | } |
| 715 | |
| 716 | void RA::assignVirt2StackSlot(unsigned virtReg) |
| 717 | { |
| 718 | const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(virtReg); |
| 719 | int frameIndex = mf_->getFrameInfo()->CreateStackObject(rc); |
| 720 | |
| 721 | bool inserted = v2ssMap_.insert(std::make_pair(virtReg, frameIndex)).second; |
| 722 | assert(inserted && |
| 723 | "attempt to assign stack slot to already assigned register?"); |
| 724 | // if the virtual register was previously assigned clear the mapping |
| 725 | // and free the virtual register |
| 726 | if (v2pMap_.find(virtReg) != v2pMap_.end()) { |
| 727 | clearVirtReg(virtReg); |
| 728 | } |
Alkis Evlogimenos | 69546d5 | 2003-12-04 03:57:28 +0000 | [diff] [blame] | 729 | else { |
| 730 | v2pMap_[virtReg] = 0; // this marks that this virtual register |
| 731 | // lives on the stack |
| 732 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Alkis Evlogimenos | 69546d5 | 2003-12-04 03:57:28 +0000 | [diff] [blame] | 735 | int RA::getStackSlot(unsigned virtReg) |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 736 | { |
| 737 | // use lower_bound so that we can do a possibly O(1) insert later |
| 738 | // if necessary |
Alkis Evlogimenos | 69546d5 | 2003-12-04 03:57:28 +0000 | [diff] [blame] | 739 | Virt2StackSlotMap::iterator it = v2ssMap_.find(virtReg); |
| 740 | assert(it != v2ssMap_.end() && |
| 741 | "attempt to get stack slot on register that does not live on the stack"); |
| 742 | return it->second; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | void RA::spillVirtReg(unsigned virtReg) |
| 746 | { |
| 747 | DEBUG(std::cerr << "\t\t\tspilling register: " << virtReg); |
| 748 | const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(virtReg); |
Alkis Evlogimenos | 69546d5 | 2003-12-04 03:57:28 +0000 | [diff] [blame] | 749 | int frameIndex = getStackSlot(virtReg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 750 | DEBUG(std::cerr << " to stack slot #" << frameIndex << '\n'); |
| 751 | ++numSpilled; |
| 752 | instrAdded_ += mri_->storeRegToStackSlot(*currentMbb_, currentInstr_, |
| 753 | v2pMap_[virtReg], frameIndex, rc); |
| 754 | clearVirtReg(virtReg); |
| 755 | } |
| 756 | |
| 757 | void RA::loadVirt2PhysReg(unsigned virtReg, unsigned physReg) |
| 758 | { |
| 759 | DEBUG(std::cerr << "\t\t\tloading register: " << virtReg); |
| 760 | const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(virtReg); |
Alkis Evlogimenos | 69546d5 | 2003-12-04 03:57:28 +0000 | [diff] [blame] | 761 | int frameIndex = getStackSlot(virtReg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 762 | DEBUG(std::cerr << " from stack slot #" << frameIndex << '\n'); |
Chris Lattner | 5e46b51 | 2003-12-18 20:25:31 +0000 | [diff] [blame^] | 763 | ++numReloaded; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 764 | instrAdded_ += mri_->loadRegFromStackSlot(*currentMbb_, currentInstr_, |
| 765 | physReg, frameIndex, rc); |
| 766 | assignVirt2PhysReg(virtReg, physReg); |
| 767 | } |
| 768 | |
| 769 | FunctionPass* llvm::createLinearScanRegisterAllocator() { |
| 770 | return new RA(); |
| 771 | } |