Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 1 | //===-- LiveIntervals.cpp - Live Interval Analysis ------------------------===// |
| 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 the LiveInterval analysis pass which is used |
| 11 | // by the Linear Scan Register allocator. This pass linearizes the |
| 12 | // basic blocks of the function in DFS order and uses the |
| 13 | // LiveVariables pass to conservatively compute live intervals for |
| 14 | // each virtual and physical register. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #define DEBUG_TYPE "liveintervals" |
Alkis Evlogimenos | 98e17cf | 2004-02-23 01:01:21 +0000 | [diff] [blame] | 19 | #include "LiveIntervals.h" |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveVariables.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstr.h" |
| 24 | #include "llvm/CodeGen/Passes.h" |
| 25 | #include "llvm/CodeGen/SSARegMap.h" |
| 26 | #include "llvm/Target/MRegisterInfo.h" |
| 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | #include "llvm/Target/TargetMachine.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CFG.h" |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 30 | #include "Support/CommandLine.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 31 | #include "Support/Debug.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 32 | #include "Support/Statistic.h" |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 33 | #include "Support/STLExtras.h" |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame^] | 34 | #include "VirtRegMap.h" |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 35 | #include <cmath> |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 36 | #include <iostream> |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 37 | #include <limits> |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace llvm; |
| 40 | |
| 41 | namespace { |
| 42 | RegisterAnalysis<LiveIntervals> X("liveintervals", |
| 43 | "Live Interval Analysis"); |
| 44 | |
Alkis Evlogimenos | 007726c | 2004-02-20 20:53:26 +0000 | [diff] [blame] | 45 | Statistic<> numIntervals |
| 46 | ("liveintervals", "Number of original intervals"); |
| 47 | |
| 48 | Statistic<> numIntervalsAfter |
| 49 | ("liveintervals", "Number of intervals after coalescing"); |
| 50 | |
| 51 | Statistic<> numJoins |
| 52 | ("liveintervals", "Number of interval joins performed"); |
| 53 | |
| 54 | Statistic<> numPeep |
| 55 | ("liveintervals", "Number of identity moves eliminated after coalescing"); |
| 56 | |
| 57 | Statistic<> numFolded |
Alkis Evlogimenos | d6f6d1a | 2004-02-21 18:07:33 +0000 | [diff] [blame] | 58 | ("liveintervals", "Number of loads/stores folded into instructions"); |
Alkis Evlogimenos | 007726c | 2004-02-20 20:53:26 +0000 | [diff] [blame] | 59 | |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 60 | cl::opt<bool> |
| 61 | join("join-liveintervals", |
| 62 | cl::desc("Join compatible live intervals"), |
Alkis Evlogimenos | 79b0c3f | 2004-01-23 13:37:51 +0000 | [diff] [blame] | 63 | cl::init(true)); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const |
| 67 | { |
Alkis Evlogimenos | f6f91bf | 2003-12-15 04:55:38 +0000 | [diff] [blame] | 68 | AU.addPreserved<LiveVariables>(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 69 | AU.addRequired<LiveVariables>(); |
Alkis Evlogimenos | f6f91bf | 2003-12-15 04:55:38 +0000 | [diff] [blame] | 70 | AU.addPreservedID(PHIEliminationID); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 71 | AU.addRequiredID(PHIEliminationID); |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 72 | AU.addRequiredID(TwoAddressInstructionPassID); |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 73 | AU.addRequired<LoopInfo>(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 74 | MachineFunctionPass::getAnalysisUsage(AU); |
| 75 | } |
| 76 | |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 77 | void LiveIntervals::releaseMemory() |
| 78 | { |
| 79 | mbbi2mbbMap_.clear(); |
| 80 | mi2iMap_.clear(); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 81 | i2miMap_.clear(); |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 82 | r2iMap_.clear(); |
| 83 | r2rMap_.clear(); |
| 84 | intervals_.clear(); |
| 85 | } |
| 86 | |
| 87 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 88 | /// runOnMachineFunction - Register allocate the whole function |
| 89 | /// |
| 90 | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 91 | mf_ = &fn; |
| 92 | tm_ = &fn.getTarget(); |
| 93 | mri_ = tm_->getRegisterInfo(); |
| 94 | lv_ = &getAnalysis<LiveVariables>(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 95 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 96 | // number MachineInstrs |
| 97 | unsigned miIndex = 0; |
| 98 | for (MachineFunction::iterator mbb = mf_->begin(), mbbEnd = mf_->end(); |
| 99 | mbb != mbbEnd; ++mbb) { |
| 100 | const std::pair<MachineBasicBlock*, unsigned>& entry = |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 101 | lv_->getMachineBasicBlockInfo(mbb); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 102 | bool inserted = mbbi2mbbMap_.insert(std::make_pair(entry.second, |
| 103 | entry.first)).second; |
| 104 | assert(inserted && "multiple index -> MachineBasicBlock"); |
| 105 | |
| 106 | for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end(); |
| 107 | mi != miEnd; ++mi) { |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 108 | inserted = mi2iMap_.insert(std::make_pair(mi, miIndex)).second; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 109 | assert(inserted && "multiple MachineInstr -> index mappings"); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 110 | i2miMap_.push_back(mi); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 111 | miIndex += InstrSlots::NUM; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | computeIntervals(); |
| 116 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 117 | numIntervals += intervals_.size(); |
Alkis Evlogimenos | 7a40eaa | 2003-12-24 15:44:53 +0000 | [diff] [blame] | 118 | |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 119 | // join intervals if requested |
| 120 | if (join) joinIntervals(); |
| 121 | |
Alkis Evlogimenos | 007726c | 2004-02-20 20:53:26 +0000 | [diff] [blame] | 122 | numIntervalsAfter += intervals_.size(); |
| 123 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 124 | // perform a final pass over the instructions and compute spill |
| 125 | // weights, coalesce virtual registers and remove identity moves |
| 126 | const LoopInfo& loopInfo = getAnalysis<LoopInfo>(); |
| 127 | const TargetInstrInfo& tii = tm_->getInstrInfo(); |
| 128 | |
| 129 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 130 | mbbi != mbbe; ++mbbi) { |
| 131 | MachineBasicBlock* mbb = mbbi; |
| 132 | unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock()); |
| 133 | |
| 134 | for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end(); |
| 135 | mii != mie; ) { |
| 136 | for (unsigned i = 0; i < mii->getNumOperands(); ++i) { |
| 137 | const MachineOperand& mop = mii->getOperand(i); |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 138 | if (mop.isRegister() && mop.getReg()) { |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 139 | // replace register with representative register |
| 140 | unsigned reg = rep(mop.getReg()); |
| 141 | mii->SetMachineOperandReg(i, reg); |
| 142 | |
| 143 | if (MRegisterInfo::isVirtualRegister(reg)) { |
| 144 | Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg); |
| 145 | assert(r2iit != r2iMap_.end()); |
| 146 | r2iit->second->weight += pow(10.0F, loopDepth); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // if the move is now an identity move delete it |
| 152 | unsigned srcReg, dstReg; |
| 153 | if (tii.isMoveInstr(*mii, srcReg, dstReg) && srcReg == dstReg) { |
| 154 | // remove index -> MachineInstr and |
| 155 | // MachineInstr -> index mappings |
| 156 | Mi2IndexMap::iterator mi2i = mi2iMap_.find(mii); |
| 157 | if (mi2i != mi2iMap_.end()) { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 158 | i2miMap_[mi2i->second/InstrSlots::NUM] = 0; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 159 | mi2iMap_.erase(mi2i); |
| 160 | } |
| 161 | mii = mbbi->erase(mii); |
| 162 | ++numPeep; |
| 163 | } |
| 164 | else |
| 165 | ++mii; |
| 166 | } |
| 167 | } |
Alkis Evlogimenos | d6e40a6 | 2004-01-14 10:44:29 +0000 | [diff] [blame] | 168 | |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 169 | intervals_.sort(StartPointComp()); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 170 | DEBUG(std::cerr << "********** INTERVALS **********\n"); |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 171 | DEBUG(std::copy(intervals_.begin(), intervals_.end(), |
| 172 | std::ostream_iterator<Interval>(std::cerr, "\n"))); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 173 | DEBUG(std::cerr << "********** MACHINEINSTRS **********\n"); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 174 | DEBUG( |
Alkis Evlogimenos | 0f338a1 | 2004-02-22 05:46:04 +0000 | [diff] [blame] | 175 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 176 | mbbi != mbbe; ++mbbi) { |
| 177 | std::cerr << mbbi->getBasicBlock()->getName() << ":\n"; |
| 178 | for (MachineBasicBlock::iterator mii = mbbi->begin(), |
| 179 | mie = mbbi->end(); mii != mie; ++mii) { |
| 180 | std::cerr << getInstructionIndex(mii) << '\t'; |
| 181 | mii->print(std::cerr, *tm_); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 182 | } |
| 183 | }); |
| 184 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 185 | return true; |
| 186 | } |
| 187 | |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame^] | 188 | void LiveIntervals::updateSpilledInterval(Interval& li, |
| 189 | VirtRegMap& vrm, |
| 190 | int slot) |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 191 | { |
| 192 | assert(li.weight != std::numeric_limits<float>::infinity() && |
| 193 | "attempt to spill already spilled interval!"); |
| 194 | Interval::Ranges oldRanges; |
| 195 | swap(oldRanges, li.ranges); |
| 196 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 197 | DEBUG(std::cerr << "\t\t\t\tupdating interval: " << li); |
| 198 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 199 | for (Interval::Ranges::iterator i = oldRanges.begin(), e = oldRanges.end(); |
| 200 | i != e; ++i) { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 201 | unsigned index = getBaseIndex(i->first); |
| 202 | unsigned end = getBaseIndex(i->second-1) + InstrSlots::NUM; |
| 203 | for (; index < end; index += InstrSlots::NUM) { |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 204 | // skip deleted instructions |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 205 | while (!getInstructionFromIndex(index)) index += InstrSlots::NUM; |
| 206 | MachineBasicBlock::iterator mi = getInstructionFromIndex(index); |
| 207 | |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame^] | 208 | for_operand: |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 209 | for (unsigned i = 0; i < mi->getNumOperands(); ++i) { |
| 210 | MachineOperand& mop = mi->getOperand(i); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 211 | if (mop.isRegister() && mop.getReg() == li.reg) { |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame^] | 212 | MachineInstr* old = mi; |
| 213 | if (mri_->foldMemoryOperand(mi, i, slot)) { |
| 214 | lv_->instructionChanged(old, mi); |
| 215 | vrm.virtFolded(li.reg, old, mi); |
| 216 | mi2iMap_.erase(old); |
| 217 | i2miMap_[index/InstrSlots::NUM] = mi; |
| 218 | mi2iMap_[mi] = index; |
| 219 | ++numFolded; |
| 220 | goto for_operand; |
| 221 | } |
| 222 | else { |
| 223 | // This is tricky. We need to add information in |
| 224 | // the interval about the spill code so we have to |
| 225 | // use our extra load/store slots. |
| 226 | // |
| 227 | // If we have a use we are going to have a load so |
| 228 | // we start the interval from the load slot |
| 229 | // onwards. Otherwise we start from the def slot. |
| 230 | unsigned start = (mop.isUse() ? |
| 231 | getLoadIndex(index) : |
| 232 | getDefIndex(index)); |
| 233 | // If we have a def we are going to have a store |
| 234 | // right after it so we end the interval after the |
| 235 | // use of the next instruction. Otherwise we end |
| 236 | // after the use of this instruction. |
| 237 | unsigned end = 1 + (mop.isDef() ? |
| 238 | getUseIndex(index+InstrSlots::NUM) : |
| 239 | getUseIndex(index)); |
| 240 | li.addRange(start, end); |
| 241 | } |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | // the new spill weight is now infinity as it cannot be spilled again |
| 247 | li.weight = std::numeric_limits<float>::infinity(); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 248 | DEBUG(std::cerr << '\n'); |
Alkis Evlogimenos | 7200c6b | 2004-02-22 04:05:13 +0000 | [diff] [blame] | 249 | DEBUG(std::cerr << "\t\t\t\tupdated interval: " << li << '\n'); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 252 | void LiveIntervals::printRegName(unsigned reg) const |
| 253 | { |
Alkis Evlogimenos | 4f67b86 | 2004-02-01 01:27:01 +0000 | [diff] [blame] | 254 | if (MRegisterInfo::isPhysicalRegister(reg)) |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 255 | std::cerr << mri_->getName(reg); |
| 256 | else |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 257 | std::cerr << "%reg" << reg; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb, |
| 261 | MachineBasicBlock::iterator mi, |
| 262 | unsigned reg) |
| 263 | { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 264 | DEBUG(std::cerr << "\t\tregister: "; printRegName(reg)); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 265 | LiveVariables::VarInfo& vi = lv_->getVarInfo(reg); |
| 266 | |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 267 | Interval* interval = 0; |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 268 | Reg2IntervalMap::iterator r2iit = r2iMap_.lower_bound(reg); |
| 269 | if (r2iit == r2iMap_.end() || r2iit->first != reg) { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 270 | // add new interval |
| 271 | intervals_.push_back(Interval(reg)); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 272 | // update interval index for this register |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 273 | r2iMap_.insert(r2iit, std::make_pair(reg, --intervals_.end())); |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 274 | interval = &intervals_.back(); |
Alkis Evlogimenos | ad48cd6 | 2004-02-05 20:45:40 +0000 | [diff] [blame] | 275 | |
| 276 | // iterate over all of the blocks that the variable is |
| 277 | // completely live in, adding them to the live |
| 278 | // interval. obviously we only need to do this once. |
| 279 | for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) { |
| 280 | if (vi.AliveBlocks[i]) { |
| 281 | MachineBasicBlock* mbb = lv_->getIndexMachineBasicBlock(i); |
| 282 | if (!mbb->empty()) { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 283 | interval->addRange( |
| 284 | getInstructionIndex(&mbb->front()), |
| 285 | getInstructionIndex(&mbb->back()) + InstrSlots::NUM); |
Alkis Evlogimenos | ad48cd6 | 2004-02-05 20:45:40 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 289 | } |
| 290 | else { |
Alkis Evlogimenos | f5f1689 | 2004-01-16 16:06:59 +0000 | [diff] [blame] | 291 | interval = &*r2iit->second; |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 292 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 293 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 294 | unsigned baseIndex = getInstructionIndex(mi); |
Alkis Evlogimenos | 0b8cb2b | 2004-02-05 22:55:25 +0000 | [diff] [blame] | 295 | |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 296 | bool killedInDefiningBasicBlock = false; |
| 297 | for (int i = 0, e = vi.Kills.size(); i != e; ++i) { |
| 298 | MachineBasicBlock* killerBlock = vi.Kills[i].first; |
| 299 | MachineInstr* killerInstr = vi.Kills[i].second; |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 300 | unsigned start = (mbb == killerBlock ? |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 301 | getDefIndex(baseIndex) : |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 302 | getInstructionIndex(&killerBlock->front())); |
| 303 | unsigned end = (killerInstr == mi ? |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 304 | // dead |
| 305 | start + 1 : |
| 306 | // killed |
| 307 | getUseIndex(getInstructionIndex(killerInstr))+1); |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 308 | // we do not want to add invalid ranges. these can happen when |
| 309 | // a variable has its latest use and is redefined later on in |
| 310 | // the same basic block (common with variables introduced by |
| 311 | // PHI elimination) |
Alkis Evlogimenos | 43f692f | 2003-12-18 08:53:43 +0000 | [diff] [blame] | 312 | if (start < end) { |
| 313 | killedInDefiningBasicBlock |= mbb == killerBlock; |
| 314 | interval->addRange(start, end); |
| 315 | } |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 316 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 317 | |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 318 | if (!killedInDefiningBasicBlock) { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 319 | unsigned end = getInstructionIndex(&mbb->back()) + InstrSlots::NUM; |
| 320 | interval->addRange(getDefIndex(baseIndex), end); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 321 | } |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 322 | DEBUG(std::cerr << '\n'); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock* mbb, |
| 326 | MachineBasicBlock::iterator mi, |
| 327 | unsigned reg) |
| 328 | { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 329 | DEBUG(std::cerr << "\t\tregister: "; printRegName(reg)); |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 330 | typedef LiveVariables::killed_iterator KillIter; |
| 331 | |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 332 | MachineBasicBlock::iterator e = mbb->end(); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 333 | unsigned baseIndex = getInstructionIndex(mi); |
| 334 | unsigned start = getDefIndex(baseIndex); |
| 335 | unsigned end = start; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 336 | |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 337 | // a variable can be dead by the instruction defining it |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 338 | for (KillIter ki = lv_->dead_begin(mi), ke = lv_->dead_end(mi); |
Alkis Evlogimenos | af25473 | 2004-01-13 22:26:14 +0000 | [diff] [blame] | 339 | ki != ke; ++ki) { |
| 340 | if (reg == ki->second) { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 341 | DEBUG(std::cerr << " dead"); |
| 342 | end = getDefIndex(start) + 1; |
Alkis Evlogimenos | af25473 | 2004-01-13 22:26:14 +0000 | [diff] [blame] | 343 | goto exit; |
| 344 | } |
| 345 | } |
Alkis Evlogimenos | af25473 | 2004-01-13 22:26:14 +0000 | [diff] [blame] | 346 | |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 347 | // a variable can only be killed by subsequent instructions |
| 348 | do { |
| 349 | ++mi; |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 350 | baseIndex += InstrSlots::NUM; |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 351 | for (KillIter ki = lv_->killed_begin(mi), ke = lv_->killed_end(mi); |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 352 | ki != ke; ++ki) { |
| 353 | if (reg == ki->second) { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 354 | DEBUG(std::cerr << " killed"); |
| 355 | end = getUseIndex(baseIndex) + 1; |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 356 | goto exit; |
| 357 | } |
| 358 | } |
Alkis Evlogimenos | 02ba13c | 2004-01-31 23:13:30 +0000 | [diff] [blame] | 359 | } while (mi != e); |
| 360 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 361 | exit: |
| 362 | assert(start < end && "did not find end of interval?"); |
| 363 | |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 364 | Reg2IntervalMap::iterator r2iit = r2iMap_.lower_bound(reg); |
| 365 | if (r2iit != r2iMap_.end() && r2iit->first == reg) { |
| 366 | r2iit->second->addRange(start, end); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 367 | } |
| 368 | else { |
| 369 | intervals_.push_back(Interval(reg)); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 370 | // update interval index for this register |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 371 | r2iMap_.insert(r2iit, std::make_pair(reg, --intervals_.end())); |
| 372 | intervals_.back().addRange(start, end); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 373 | } |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 374 | DEBUG(std::cerr << '\n'); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void LiveIntervals::handleRegisterDef(MachineBasicBlock* mbb, |
| 378 | MachineBasicBlock::iterator mi, |
| 379 | unsigned reg) |
| 380 | { |
Alkis Evlogimenos | 4f67b86 | 2004-02-01 01:27:01 +0000 | [diff] [blame] | 381 | if (MRegisterInfo::isPhysicalRegister(reg)) { |
Alkis Evlogimenos | 1a119e2 | 2004-01-13 22:10:43 +0000 | [diff] [blame] | 382 | if (lv_->getAllocatablePhysicalRegisters()[reg]) { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 383 | handlePhysicalRegisterDef(mbb, mi, reg); |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 384 | for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) |
| 385 | handlePhysicalRegisterDef(mbb, mi, *as); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | else { |
| 389 | handleVirtualRegisterDef(mbb, mi, reg); |
| 390 | } |
| 391 | } |
| 392 | |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 393 | unsigned LiveIntervals::getInstructionIndex(MachineInstr* instr) const |
| 394 | { |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 395 | Mi2IndexMap::const_iterator it = mi2iMap_.find(instr); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 396 | return (it == mi2iMap_.end() ? |
| 397 | std::numeric_limits<unsigned>::max() : |
| 398 | it->second); |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | MachineInstr* LiveIntervals::getInstructionFromIndex(unsigned index) const |
| 402 | { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 403 | index /= InstrSlots::NUM; // convert index to vector index |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 404 | assert(index < i2miMap_.size() && |
| 405 | "index does not correspond to an instruction"); |
| 406 | return i2miMap_[index]; |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 409 | /// computeIntervals - computes the live intervals for virtual |
Alkis Evlogimenos | 4d46e1e | 2004-01-31 14:37:41 +0000 | [diff] [blame] | 410 | /// registers. for some ordering of the machine instructions [1,N] a |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 411 | /// live interval is an interval [i, j) where 1 <= i <= j < N for |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 412 | /// which a variable is live |
| 413 | void LiveIntervals::computeIntervals() |
| 414 | { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 415 | DEBUG(std::cerr << "********** COMPUTING LIVE INTERVALS **********\n"); |
| 416 | DEBUG(std::cerr << "********** Function: " |
| 417 | << mf_->getFunction()->getName() << '\n'); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 418 | |
| 419 | for (MbbIndex2MbbMap::iterator |
| 420 | it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end(); |
| 421 | it != itEnd; ++it) { |
| 422 | MachineBasicBlock* mbb = it->second; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 423 | DEBUG(std::cerr << mbb->getBasicBlock()->getName() << ":\n"); |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 424 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 425 | for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end(); |
| 426 | mi != miEnd; ++mi) { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 427 | const TargetInstrDescriptor& tid = |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 428 | tm_->getInstrInfo().get(mi->getOpcode()); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 429 | DEBUG(std::cerr << getInstructionIndex(mi) << "\t"; |
| 430 | mi->print(std::cerr, *tm_)); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 431 | |
| 432 | // handle implicit defs |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 433 | for (const unsigned* id = tid.ImplicitDefs; *id; ++id) |
| 434 | handleRegisterDef(mbb, mi, *id); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 435 | |
| 436 | // handle explicit defs |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 437 | for (int i = mi->getNumOperands() - 1; i >= 0; --i) { |
| 438 | MachineOperand& mop = mi->getOperand(i); |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 439 | // handle register defs - build intervals |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 440 | if (mop.isRegister() && mop.getReg() && mop.isDef()) |
Alkis Evlogimenos | be766c7 | 2004-02-13 21:01:20 +0000 | [diff] [blame] | 441 | handleRegisterDef(mbb, mi, mop.getReg()); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 445 | } |
Alkis Evlogimenos | b27ef24 | 2003-12-05 10:38:28 +0000 | [diff] [blame] | 446 | |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 447 | unsigned LiveIntervals::rep(unsigned reg) |
| 448 | { |
| 449 | Reg2RegMap::iterator it = r2rMap_.find(reg); |
| 450 | if (it != r2rMap_.end()) |
| 451 | return it->second = rep(it->second); |
| 452 | return reg; |
| 453 | } |
| 454 | |
| 455 | void LiveIntervals::joinIntervals() |
| 456 | { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 457 | DEBUG(std::cerr << "********** JOINING INTERVALS ***********\n"); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 458 | |
| 459 | const TargetInstrInfo& tii = tm_->getInstrInfo(); |
| 460 | |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 461 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 462 | mbbi != mbbe; ++mbbi) { |
| 463 | MachineBasicBlock* mbb = mbbi; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 464 | DEBUG(std::cerr << mbb->getBasicBlock()->getName() << ":\n"); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 465 | |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 466 | for (MachineBasicBlock::iterator mi = mbb->begin(), mie = mbb->end(); |
| 467 | mi != mie; ++mi) { |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 468 | const TargetInstrDescriptor& tid = |
| 469 | tm_->getInstrInfo().get(mi->getOpcode()); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 470 | DEBUG(std::cerr << getInstructionIndex(mi) << '\t'; |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 471 | mi->print(std::cerr, *tm_);); |
| 472 | |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 473 | // we only join virtual registers with allocatable |
| 474 | // physical registers since we do not have liveness information |
| 475 | // on not allocatable physical registers |
| 476 | unsigned regA, regB; |
| 477 | if (tii.isMoveInstr(*mi, regA, regB) && |
| 478 | (MRegisterInfo::isVirtualRegister(regA) || |
| 479 | lv_->getAllocatablePhysicalRegisters()[regA]) && |
| 480 | (MRegisterInfo::isVirtualRegister(regB) || |
| 481 | lv_->getAllocatablePhysicalRegisters()[regB])) { |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 482 | |
| 483 | // get representative registers |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 484 | regA = rep(regA); |
| 485 | regB = rep(regB); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 486 | |
| 487 | // if they are already joined we continue |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 488 | if (regA == regB) |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 489 | continue; |
| 490 | |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 491 | Reg2IntervalMap::iterator r2iA = r2iMap_.find(regA); |
| 492 | assert(r2iA != r2iMap_.end()); |
| 493 | Reg2IntervalMap::iterator r2iB = r2iMap_.find(regB); |
| 494 | assert(r2iB != r2iMap_.end()); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 495 | |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 496 | Intervals::iterator intA = r2iA->second; |
| 497 | Intervals::iterator intB = r2iB->second; |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 498 | |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 499 | // both A and B are virtual registers |
| 500 | if (MRegisterInfo::isVirtualRegister(intA->reg) && |
| 501 | MRegisterInfo::isVirtualRegister(intB->reg)) { |
| 502 | |
| 503 | const TargetRegisterClass *rcA, *rcB; |
| 504 | rcA = mf_->getSSARegMap()->getRegClass(intA->reg); |
| 505 | rcB = mf_->getSSARegMap()->getRegClass(intB->reg); |
| 506 | assert(rcA == rcB && "registers must be of the same class"); |
| 507 | |
| 508 | // if their intervals do not overlap we join them |
| 509 | if (!intB->overlaps(*intA)) { |
| 510 | intA->join(*intB); |
| 511 | r2iB->second = r2iA->second; |
| 512 | r2rMap_.insert(std::make_pair(intB->reg, intA->reg)); |
| 513 | intervals_.erase(intB); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
Alkis Evlogimenos | b0b0eba | 2004-02-01 08:19:25 +0000 | [diff] [blame] | 516 | else if (MRegisterInfo::isPhysicalRegister(intA->reg) ^ |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 517 | MRegisterInfo::isPhysicalRegister(intB->reg)) { |
| 518 | if (MRegisterInfo::isPhysicalRegister(intB->reg)) { |
| 519 | std::swap(regA, regB); |
| 520 | std::swap(intA, intB); |
| 521 | std::swap(r2iA, r2iB); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 522 | } |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 523 | |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 524 | assert(MRegisterInfo::isPhysicalRegister(intA->reg) && |
| 525 | MRegisterInfo::isVirtualRegister(intB->reg) && |
| 526 | "A must be physical and B must be virtual"); |
Alkis Evlogimenos | 676cf8c | 2004-02-01 02:21:31 +0000 | [diff] [blame] | 527 | |
Alkis Evlogimenos | 01e74a2 | 2004-02-01 02:18:31 +0000 | [diff] [blame] | 528 | if (!intA->overlaps(*intB) && |
| 529 | !overlapsAliases(*intA, *intB)) { |
| 530 | intA->join(*intB); |
| 531 | r2iB->second = r2iA->second; |
| 532 | r2rMap_.insert(std::make_pair(intB->reg, intA->reg)); |
| 533 | intervals_.erase(intB); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | } |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Alkis Evlogimenos | 79b0c3f | 2004-01-23 13:37:51 +0000 | [diff] [blame] | 541 | bool LiveIntervals::overlapsAliases(const Interval& lhs, |
| 542 | const Interval& rhs) const |
| 543 | { |
Alkis Evlogimenos | 4f67b86 | 2004-02-01 01:27:01 +0000 | [diff] [blame] | 544 | assert(MRegisterInfo::isPhysicalRegister(lhs.reg) && |
Alkis Evlogimenos | 79b0c3f | 2004-01-23 13:37:51 +0000 | [diff] [blame] | 545 | "first interval must describe a physical register"); |
| 546 | |
| 547 | for (const unsigned* as = mri_->getAliasSet(lhs.reg); *as; ++as) { |
| 548 | Reg2IntervalMap::const_iterator r2i = r2iMap_.find(*as); |
| 549 | assert(r2i != r2iMap_.end() && "alias does not have interval?"); |
| 550 | if (rhs.overlaps(*r2i->second)) |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | return false; |
| 555 | } |
| 556 | |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 557 | LiveIntervals::Interval::Interval(unsigned r) |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 558 | : reg(r), |
Alkis Evlogimenos | 4f67b86 | 2004-02-01 01:27:01 +0000 | [diff] [blame] | 559 | weight((MRegisterInfo::isPhysicalRegister(r) ? |
Alkis Evlogimenos | 6ab5c15 | 2004-02-14 00:44:07 +0000 | [diff] [blame] | 560 | std::numeric_limits<float>::infinity() : 0.0F)) |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 561 | { |
| 562 | |
| 563 | } |
| 564 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 565 | bool LiveIntervals::Interval::spilled() const |
| 566 | { |
| 567 | return (weight == std::numeric_limits<float>::infinity() && |
| 568 | MRegisterInfo::isVirtualRegister(reg)); |
| 569 | } |
| 570 | |
Alkis Evlogimenos | 0b8cb2b | 2004-02-05 22:55:25 +0000 | [diff] [blame] | 571 | // An example for liveAt(): |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 572 | // |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 573 | // this = [1,4), liveAt(0) will return false. The instruction defining |
| 574 | // this spans slots [0,3]. The interval belongs to an spilled |
| 575 | // definition of the variable it represents. This is because slot 1 is |
| 576 | // used (def slot) and spans up to slot 3 (store slot). |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 577 | // |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 578 | bool LiveIntervals::Interval::liveAt(unsigned index) const |
| 579 | { |
Alkis Evlogimenos | 97017de | 2004-01-31 16:54:54 +0000 | [diff] [blame] | 580 | Range dummy(index, index+1); |
| 581 | Ranges::const_iterator r = std::upper_bound(ranges.begin(), |
| 582 | ranges.end(), |
| 583 | dummy); |
| 584 | if (r == ranges.begin()) |
| 585 | return false; |
| 586 | |
| 587 | --r; |
Alkis Evlogimenos | 0b8cb2b | 2004-02-05 22:55:25 +0000 | [diff] [blame] | 588 | return index >= r->first && index < r->second; |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Alkis Evlogimenos | 0b8cb2b | 2004-02-05 22:55:25 +0000 | [diff] [blame] | 591 | // An example for overlaps(): |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 592 | // |
| 593 | // 0: A = ... |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 594 | // 4: B = ... |
| 595 | // 8: C = A + B ;; last use of A |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 596 | // |
| 597 | // The live intervals should look like: |
| 598 | // |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 599 | // A = [3, 11) |
| 600 | // B = [7, x) |
| 601 | // C = [11, y) |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 602 | // |
| 603 | // A->overlaps(C) should return false since we want to be able to join |
| 604 | // A and C. |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 605 | bool LiveIntervals::Interval::overlaps(const Interval& other) const |
| 606 | { |
Alkis Evlogimenos | 80b378c | 2004-01-07 01:45:58 +0000 | [diff] [blame] | 607 | Ranges::const_iterator i = ranges.begin(); |
Alkis Evlogimenos | 97017de | 2004-01-31 16:54:54 +0000 | [diff] [blame] | 608 | Ranges::const_iterator ie = ranges.end(); |
Alkis Evlogimenos | 80b378c | 2004-01-07 01:45:58 +0000 | [diff] [blame] | 609 | Ranges::const_iterator j = other.ranges.begin(); |
Alkis Evlogimenos | 97017de | 2004-01-31 16:54:54 +0000 | [diff] [blame] | 610 | Ranges::const_iterator je = other.ranges.end(); |
| 611 | if (i->first < j->first) { |
| 612 | i = std::upper_bound(i, ie, *j); |
| 613 | if (i != ranges.begin()) --i; |
| 614 | } |
| 615 | else if (j->first < i->first) { |
| 616 | j = std::upper_bound(j, je, *i); |
| 617 | if (j != other.ranges.begin()) --j; |
| 618 | } |
Alkis Evlogimenos | 80b378c | 2004-01-07 01:45:58 +0000 | [diff] [blame] | 619 | |
Alkis Evlogimenos | 97017de | 2004-01-31 16:54:54 +0000 | [diff] [blame] | 620 | while (i != ie && j != je) { |
| 621 | if (i->first == j->first) { |
| 622 | return true; |
| 623 | } |
| 624 | else { |
| 625 | if (i->first > j->first) { |
| 626 | swap(i, j); |
| 627 | swap(ie, je); |
| 628 | } |
| 629 | assert(i->first < j->first); |
| 630 | |
Alkis Evlogimenos | 0b8cb2b | 2004-02-05 22:55:25 +0000 | [diff] [blame] | 631 | if (i->second > j->first) { |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 632 | return true; |
Alkis Evlogimenos | 80b378c | 2004-01-07 01:45:58 +0000 | [diff] [blame] | 633 | } |
| 634 | else { |
| 635 | ++i; |
| 636 | } |
| 637 | } |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | return false; |
| 641 | } |
| 642 | |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 643 | void LiveIntervals::Interval::addRange(unsigned start, unsigned end) |
| 644 | { |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 645 | assert(start < end && "Invalid range to add!"); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 646 | DEBUG(std::cerr << " +[" << start << ',' << end << ")"); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 647 | //assert(start < end && "invalid range?"); |
| 648 | Range range = std::make_pair(start, end); |
| 649 | Ranges::iterator it = |
| 650 | ranges.insert(std::upper_bound(ranges.begin(), ranges.end(), range), |
| 651 | range); |
| 652 | |
| 653 | it = mergeRangesForward(it); |
| 654 | it = mergeRangesBackward(it); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void LiveIntervals::Interval::join(const LiveIntervals::Interval& other) |
| 658 | { |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 659 | DEBUG(std::cerr << "\t\tjoining " << *this << " with " << other << '\n'); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 660 | Ranges::iterator cur = ranges.begin(); |
| 661 | |
| 662 | for (Ranges::const_iterator i = other.ranges.begin(), |
| 663 | e = other.ranges.end(); i != e; ++i) { |
| 664 | cur = ranges.insert(std::upper_bound(cur, ranges.end(), *i), *i); |
| 665 | cur = mergeRangesForward(cur); |
| 666 | cur = mergeRangesBackward(cur); |
| 667 | } |
Alkis Evlogimenos | cea4471 | 2004-02-20 20:43:08 +0000 | [diff] [blame] | 668 | weight += other.weight; |
| 669 | ++numJoins; |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | LiveIntervals::Interval::Ranges::iterator |
| 673 | LiveIntervals::Interval::mergeRangesForward(Ranges::iterator it) |
| 674 | { |
Alkis Evlogimenos | 7200c6b | 2004-02-22 04:05:13 +0000 | [diff] [blame] | 675 | Ranges::iterator n; |
| 676 | while ((n = next(it)) != ranges.end()) { |
| 677 | if (n->first > it->second) |
| 678 | break; |
Alkis Evlogimenos | 23c114f | 2004-02-18 04:38:37 +0000 | [diff] [blame] | 679 | it->second = std::max(it->second, n->second); |
| 680 | n = ranges.erase(n); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 681 | } |
| 682 | return it; |
| 683 | } |
| 684 | |
| 685 | LiveIntervals::Interval::Ranges::iterator |
| 686 | LiveIntervals::Interval::mergeRangesBackward(Ranges::iterator it) |
| 687 | { |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 688 | while (it != ranges.begin()) { |
Alkis Evlogimenos | 23c114f | 2004-02-18 04:38:37 +0000 | [diff] [blame] | 689 | Ranges::iterator p = prior(it); |
Alkis Evlogimenos | 7200c6b | 2004-02-22 04:05:13 +0000 | [diff] [blame] | 690 | if (it->first > p->second) |
| 691 | break; |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 692 | |
Alkis Evlogimenos | 23c114f | 2004-02-18 04:38:37 +0000 | [diff] [blame] | 693 | it->first = std::min(it->first, p->first); |
| 694 | it->second = std::max(it->second, p->second); |
| 695 | it = ranges.erase(p); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | return it; |
| 699 | } |
| 700 | |
Alkis Evlogimenos | b27ef24 | 2003-12-05 10:38:28 +0000 | [diff] [blame] | 701 | std::ostream& llvm::operator<<(std::ostream& os, |
| 702 | const LiveIntervals::Interval& li) |
| 703 | { |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 704 | os << "%reg" << li.reg << ',' << li.weight << " = "; |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 705 | if (li.empty()) |
| 706 | return os << "EMPTY"; |
Alkis Evlogimenos | b27ef24 | 2003-12-05 10:38:28 +0000 | [diff] [blame] | 707 | for (LiveIntervals::Interval::Ranges::const_iterator |
| 708 | i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) { |
Alkis Evlogimenos | 63841bc | 2004-01-13 21:17:47 +0000 | [diff] [blame] | 709 | os << "[" << i->first << "," << i->second << ")"; |
Alkis Evlogimenos | b27ef24 | 2003-12-05 10:38:28 +0000 | [diff] [blame] | 710 | } |
| 711 | return os; |
| 712 | } |