Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- LiveIntervalAnalysis.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" |
| 19 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 20 | #include "VirtRegMap.h" |
| 21 | #include "llvm/Value.h" |
| 22 | #include "llvm/Analysis/LoopInfo.h" |
| 23 | #include "llvm/CodeGen/LiveVariables.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/MachineInstr.h" |
| 26 | #include "llvm/CodeGen/Passes.h" |
| 27 | #include "llvm/CodeGen/SSARegMap.h" |
| 28 | #include "llvm/Target/MRegisterInfo.h" |
| 29 | #include "llvm/Target/TargetInstrInfo.h" |
| 30 | #include "llvm/Target/TargetMachine.h" |
| 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include "llvm/ADT/STLExtras.h" |
| 35 | #include <algorithm> |
| 36 | #include <cmath> |
| 37 | using namespace llvm; |
| 38 | |
Evan Cheng | afc07f8 | 2007-08-16 07:24:22 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | // Hidden options for help debugging. |
| 41 | cl::opt<bool> DisableReMat("disable-rematerialization", |
| 42 | cl::init(false), cl::Hidden); |
| 43 | } |
| 44 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 45 | STATISTIC(numIntervals, "Number of original intervals"); |
| 46 | STATISTIC(numIntervalsAfter, "Number of intervals after coalescing"); |
| 47 | STATISTIC(numFolded , "Number of loads/stores folded into instructions"); |
| 48 | |
| 49 | char LiveIntervals::ID = 0; |
| 50 | namespace { |
| 51 | RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis"); |
| 52 | } |
| 53 | |
| 54 | void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { |
| 55 | AU.addPreserved<LiveVariables>(); |
| 56 | AU.addRequired<LiveVariables>(); |
| 57 | AU.addPreservedID(PHIEliminationID); |
| 58 | AU.addRequiredID(PHIEliminationID); |
| 59 | AU.addRequiredID(TwoAddressInstructionPassID); |
| 60 | AU.addRequired<LoopInfo>(); |
| 61 | MachineFunctionPass::getAnalysisUsage(AU); |
| 62 | } |
| 63 | |
| 64 | void LiveIntervals::releaseMemory() { |
| 65 | mi2iMap_.clear(); |
| 66 | i2miMap_.clear(); |
| 67 | r2iMap_.clear(); |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 68 | for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i) |
| 69 | delete ClonedMIs[i]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /// runOnMachineFunction - Register allocate the whole function |
| 73 | /// |
| 74 | bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { |
| 75 | mf_ = &fn; |
| 76 | tm_ = &fn.getTarget(); |
| 77 | mri_ = tm_->getRegisterInfo(); |
| 78 | tii_ = tm_->getInstrInfo(); |
| 79 | lv_ = &getAnalysis<LiveVariables>(); |
| 80 | allocatableRegs_ = mri_->getAllocatableSet(fn); |
| 81 | |
| 82 | // Number MachineInstrs and MachineBasicBlocks. |
| 83 | // Initialize MBB indexes to a sentinal. |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 84 | MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 85 | |
| 86 | unsigned MIIndex = 0; |
| 87 | for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end(); |
| 88 | MBB != E; ++MBB) { |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 89 | unsigned StartIdx = MIIndex; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | |
| 91 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 92 | I != E; ++I) { |
| 93 | bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second; |
| 94 | assert(inserted && "multiple MachineInstr -> index mappings"); |
| 95 | i2miMap_.push_back(I); |
| 96 | MIIndex += InstrSlots::NUM; |
| 97 | } |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 98 | |
| 99 | // Set the MBB2IdxMap entry for this MBB. |
| 100 | MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | computeIntervals(); |
| 104 | |
| 105 | numIntervals += getNumIntervals(); |
| 106 | |
| 107 | DOUT << "********** INTERVALS **********\n"; |
| 108 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 109 | I->second.print(DOUT, mri_); |
| 110 | DOUT << "\n"; |
| 111 | } |
| 112 | |
| 113 | numIntervalsAfter += getNumIntervals(); |
| 114 | DEBUG(dump()); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | /// print - Implement the dump method. |
| 119 | void LiveIntervals::print(std::ostream &O, const Module* ) const { |
| 120 | O << "********** INTERVALS **********\n"; |
| 121 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
| 122 | I->second.print(DOUT, mri_); |
| 123 | DOUT << "\n"; |
| 124 | } |
| 125 | |
| 126 | O << "********** MACHINEINSTRS **********\n"; |
| 127 | for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end(); |
| 128 | mbbi != mbbe; ++mbbi) { |
| 129 | O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n"; |
| 130 | for (MachineBasicBlock::iterator mii = mbbi->begin(), |
| 131 | mie = mbbi->end(); mii != mie; ++mii) { |
| 132 | O << getInstructionIndex(mii) << '\t' << *mii; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Not called? |
| 138 | /// CreateNewLiveInterval - Create a new live interval with the given live |
| 139 | /// ranges. The new live interval will have an infinite spill weight. |
| 140 | LiveInterval& |
| 141 | LiveIntervals::CreateNewLiveInterval(const LiveInterval *LI, |
| 142 | const std::vector<LiveRange> &LRs) { |
| 143 | const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(LI->reg); |
| 144 | |
| 145 | // Create a new virtual register for the spill interval. |
| 146 | unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(RC); |
| 147 | |
| 148 | // Replace the old virtual registers in the machine operands with the shiny |
| 149 | // new one. |
| 150 | for (std::vector<LiveRange>::const_iterator |
| 151 | I = LRs.begin(), E = LRs.end(); I != E; ++I) { |
| 152 | unsigned Index = getBaseIndex(I->start); |
| 153 | unsigned End = getBaseIndex(I->end - 1) + InstrSlots::NUM; |
| 154 | |
| 155 | for (; Index != End; Index += InstrSlots::NUM) { |
| 156 | // Skip deleted instructions |
| 157 | while (Index != End && !getInstructionFromIndex(Index)) |
| 158 | Index += InstrSlots::NUM; |
| 159 | |
| 160 | if (Index == End) break; |
| 161 | |
| 162 | MachineInstr *MI = getInstructionFromIndex(Index); |
| 163 | |
| 164 | for (unsigned J = 0, e = MI->getNumOperands(); J != e; ++J) { |
| 165 | MachineOperand &MOp = MI->getOperand(J); |
| 166 | if (MOp.isRegister() && MOp.getReg() == LI->reg) |
| 167 | MOp.setReg(NewVReg); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | LiveInterval &NewLI = getOrCreateInterval(NewVReg); |
| 173 | |
| 174 | // The spill weight is now infinity as it cannot be spilled again |
| 175 | NewLI.weight = float(HUGE_VAL); |
| 176 | |
| 177 | for (std::vector<LiveRange>::const_iterator |
| 178 | I = LRs.begin(), E = LRs.end(); I != E; ++I) { |
| 179 | DOUT << " Adding live range " << *I << " to new interval\n"; |
| 180 | NewLI.addRange(*I); |
| 181 | } |
| 182 | |
| 183 | DOUT << "Created new live interval " << NewLI << "\n"; |
| 184 | return NewLI; |
| 185 | } |
| 186 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 187 | /// isReDefinedByTwoAddr - Returns true if the Reg re-definition is due to |
| 188 | /// two addr elimination. |
| 189 | static bool isReDefinedByTwoAddr(MachineInstr *MI, unsigned Reg, |
| 190 | const TargetInstrInfo *TII) { |
| 191 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 192 | MachineOperand &MO1 = MI->getOperand(i); |
| 193 | if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) { |
| 194 | for (unsigned j = i+1; j < e; ++j) { |
| 195 | MachineOperand &MO2 = MI->getOperand(j); |
| 196 | if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg && |
| 197 | MI->getInstrDescriptor()-> |
| 198 | getOperandConstraint(j, TOI::TIED_TO) == (int)i) |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 206 | /// isReMaterializable - Returns true if the definition MI of the specified |
| 207 | /// val# of the specified interval is re-materializable. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 208 | bool LiveIntervals::isReMaterializable(const LiveInterval &li, |
| 209 | const VNInfo *ValNo, MachineInstr *MI) { |
Evan Cheng | afc07f8 | 2007-08-16 07:24:22 +0000 | [diff] [blame] | 210 | if (DisableReMat) |
| 211 | return false; |
| 212 | |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 213 | if (tii_->isTriviallyReMaterializable(MI)) |
| 214 | return true; |
| 215 | |
| 216 | int FrameIdx = 0; |
| 217 | if (!tii_->isLoadFromStackSlot(MI, FrameIdx) || |
| 218 | !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx)) |
| 219 | return false; |
| 220 | |
| 221 | // This is a load from fixed stack slot. It can be rematerialized unless it's |
| 222 | // re-defined by a two-address instruction. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 223 | for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end(); |
| 224 | i != e; ++i) { |
| 225 | const VNInfo *VNI = *i; |
| 226 | if (VNI == ValNo) |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 227 | continue; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 228 | unsigned DefIdx = VNI->def; |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 229 | if (DefIdx == ~1U) |
| 230 | continue; // Dead val#. |
| 231 | MachineInstr *DefMI = (DefIdx == ~0u) |
| 232 | ? NULL : getInstructionFromIndex(DefIdx); |
| 233 | if (DefMI && isReDefinedByTwoAddr(DefMI, li.reg, tii_)) |
| 234 | return false; |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
Evan Cheng | 0322543 | 2007-08-30 05:53:02 +0000 | [diff] [blame^] | 239 | /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from |
| 240 | /// slot / to reg or any rematerialized load into ith operand of specified |
| 241 | /// MI. If it is successul, MI is updated with the newly created MI and |
| 242 | /// returns true. |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 243 | bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm, |
| 244 | unsigned index, unsigned i, |
Evan Cheng | 0322543 | 2007-08-30 05:53:02 +0000 | [diff] [blame^] | 245 | bool isSS, MachineInstr *DefMI, |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 246 | int slot, unsigned reg) { |
Evan Cheng | 0322543 | 2007-08-30 05:53:02 +0000 | [diff] [blame^] | 247 | MachineInstr *fmi = isSS |
| 248 | ? mri_->foldMemoryOperand(MI, i, slot) |
| 249 | : mri_->foldMemoryOperand(MI, i, DefMI); |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 250 | if (fmi) { |
| 251 | // Attempt to fold the memory reference into the instruction. If |
| 252 | // we can do this, we don't need to insert spill code. |
| 253 | if (lv_) |
| 254 | lv_->instructionChanged(MI, fmi); |
| 255 | MachineBasicBlock &MBB = *MI->getParent(); |
| 256 | vrm.virtFolded(reg, MI, i, fmi); |
| 257 | mi2iMap_.erase(MI); |
| 258 | i2miMap_[index/InstrSlots::NUM] = fmi; |
| 259 | mi2iMap_[fmi] = index; |
| 260 | MI = MBB.insert(MBB.erase(MI), fmi); |
| 261 | ++numFolded; |
| 262 | return true; |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | std::vector<LiveInterval*> LiveIntervals:: |
| 268 | addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) { |
| 269 | // since this is called after the analysis is done we don't know if |
| 270 | // LiveVariables is available |
| 271 | lv_ = getAnalysisToUpdate<LiveVariables>(); |
| 272 | |
| 273 | std::vector<LiveInterval*> added; |
| 274 | |
| 275 | assert(li.weight != HUGE_VALF && |
| 276 | "attempt to spill already spilled interval!"); |
| 277 | |
| 278 | DOUT << "\t\t\t\tadding intervals for spills for interval: "; |
| 279 | li.print(DOUT, mri_); |
| 280 | DOUT << '\n'; |
| 281 | |
| 282 | const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg); |
| 283 | |
| 284 | unsigned NumValNums = li.getNumValNums(); |
| 285 | SmallVector<MachineInstr*, 4> ReMatDefs; |
| 286 | ReMatDefs.resize(NumValNums, NULL); |
| 287 | SmallVector<MachineInstr*, 4> ReMatOrigDefs; |
| 288 | ReMatOrigDefs.resize(NumValNums, NULL); |
| 289 | SmallVector<int, 4> ReMatIds; |
| 290 | ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT); |
| 291 | BitVector ReMatDelete(NumValNums); |
| 292 | unsigned slot = VirtRegMap::MAX_STACK_SLOT; |
| 293 | |
| 294 | bool NeedStackSlot = false; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 295 | for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end(); |
| 296 | i != e; ++i) { |
| 297 | const VNInfo *VNI = *i; |
| 298 | unsigned VN = VNI->id; |
| 299 | unsigned DefIdx = VNI->def; |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 300 | if (DefIdx == ~1U) |
| 301 | continue; // Dead val#. |
| 302 | // Is the def for the val# rematerializable? |
| 303 | MachineInstr *DefMI = (DefIdx == ~0u) |
| 304 | ? NULL : getInstructionFromIndex(DefIdx); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 305 | if (DefMI && isReMaterializable(li, VNI, DefMI)) { |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 306 | // Remember how to remat the def of this val#. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 307 | ReMatOrigDefs[VN] = DefMI; |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 308 | // Original def may be modified so we have to make a copy here. vrm must |
| 309 | // delete these! |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 310 | ReMatDefs[VN] = DefMI = DefMI->clone(); |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 311 | vrm.setVirtIsReMaterialized(reg, DefMI); |
| 312 | |
| 313 | bool CanDelete = true; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 314 | for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) { |
| 315 | unsigned KillIdx = VNI->kills[j]; |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 316 | MachineInstr *KillMI = (KillIdx & 1) |
| 317 | ? NULL : getInstructionFromIndex(KillIdx); |
| 318 | // Kill is a phi node, not all of its uses can be rematerialized. |
| 319 | // It must not be deleted. |
| 320 | if (!KillMI) { |
| 321 | CanDelete = false; |
| 322 | // Need a stack slot if there is any live range where uses cannot be |
| 323 | // rematerialized. |
| 324 | NeedStackSlot = true; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (CanDelete) |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 330 | ReMatDelete.set(VN); |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 331 | } else { |
| 332 | // Need a stack slot if there is any live range where uses cannot be |
| 333 | // rematerialized. |
| 334 | NeedStackSlot = true; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // One stack slot per live interval. |
| 339 | if (NeedStackSlot) |
| 340 | slot = vrm.assignVirt2StackSlot(reg); |
| 341 | |
| 342 | for (LiveInterval::Ranges::const_iterator |
| 343 | I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 344 | MachineInstr *DefMI = ReMatDefs[I->valno->id]; |
| 345 | MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id]; |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 346 | bool DefIsReMat = DefMI != NULL; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 347 | bool CanDelete = ReMatDelete[I->valno->id]; |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 348 | int LdSlot = 0; |
| 349 | bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot); |
Evan Cheng | 0322543 | 2007-08-30 05:53:02 +0000 | [diff] [blame^] | 350 | bool isLoad = isLoadSS || |
| 351 | (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG)); |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 352 | unsigned index = getBaseIndex(I->start); |
| 353 | unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM; |
| 354 | for (; index != end; index += InstrSlots::NUM) { |
| 355 | // skip deleted instructions |
| 356 | while (index != end && !getInstructionFromIndex(index)) |
| 357 | index += InstrSlots::NUM; |
| 358 | if (index == end) break; |
| 359 | |
| 360 | MachineInstr *MI = getInstructionFromIndex(index); |
| 361 | |
| 362 | RestartInstruction: |
| 363 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 364 | MachineOperand& mop = MI->getOperand(i); |
| 365 | if (mop.isRegister() && mop.getReg() == li.reg) { |
| 366 | if (DefIsReMat) { |
| 367 | // If this is the rematerializable definition MI itself and |
| 368 | // all of its uses are rematerialized, simply delete it. |
| 369 | if (MI == OrigDefMI) { |
| 370 | if (CanDelete) { |
| 371 | RemoveMachineInstrFromMaps(MI); |
| 372 | MI->eraseFromParent(); |
| 373 | break; |
Evan Cheng | 0322543 | 2007-08-30 05:53:02 +0000 | [diff] [blame^] | 374 | } else if (tryFoldMemoryOperand(MI, vrm, index, i, true, |
| 375 | DefMI, slot, li.reg)) { |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 376 | // Folding the load/store can completely change the instruction |
| 377 | // in unpredictable ways, rescan it from the beginning. |
| 378 | goto RestartInstruction; |
Evan Cheng | 0322543 | 2007-08-30 05:53:02 +0000 | [diff] [blame^] | 379 | } |
| 380 | } else if (isLoad && |
| 381 | tryFoldMemoryOperand(MI, vrm, index, i, isLoadSS, |
| 382 | DefMI, LdSlot, li.reg)) |
| 383 | // Folding the load/store can completely change the |
| 384 | // instruction in unpredictable ways, rescan it from |
| 385 | // the beginning. |
| 386 | goto RestartInstruction; |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 387 | } else { |
Evan Cheng | 0322543 | 2007-08-30 05:53:02 +0000 | [diff] [blame^] | 388 | if (tryFoldMemoryOperand(MI, vrm, index, i, true, DefMI, |
| 389 | slot, li.reg)) |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 390 | // Folding the load/store can completely change the instruction in |
| 391 | // unpredictable ways, rescan it from the beginning. |
| 392 | goto RestartInstruction; |
| 393 | } |
| 394 | |
| 395 | // Create a new virtual register for the spill interval. |
| 396 | unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(rc); |
| 397 | |
| 398 | // Scan all of the operands of this instruction rewriting operands |
| 399 | // to use NewVReg instead of li.reg as appropriate. We do this for |
| 400 | // two reasons: |
| 401 | // |
| 402 | // 1. If the instr reads the same spilled vreg multiple times, we |
| 403 | // want to reuse the NewVReg. |
| 404 | // 2. If the instr is a two-addr instruction, we are required to |
| 405 | // keep the src/dst regs pinned. |
| 406 | // |
| 407 | // Keep track of whether we replace a use and/or def so that we can |
| 408 | // create the spill interval with the appropriate range. |
| 409 | mop.setReg(NewVReg); |
| 410 | |
| 411 | bool HasUse = mop.isUse(); |
| 412 | bool HasDef = mop.isDef(); |
| 413 | for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) { |
| 414 | if (MI->getOperand(j).isReg() && |
| 415 | MI->getOperand(j).getReg() == li.reg) { |
| 416 | MI->getOperand(j).setReg(NewVReg); |
| 417 | HasUse |= MI->getOperand(j).isUse(); |
| 418 | HasDef |= MI->getOperand(j).isDef(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | vrm.grow(); |
| 423 | if (DefIsReMat) { |
| 424 | vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 425 | if (ReMatIds[I->valno->id] == VirtRegMap::MAX_STACK_SLOT) { |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 426 | // Each valnum may have its own remat id. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 427 | ReMatIds[I->valno->id] = vrm.assignVirtReMatId(NewVReg); |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 428 | } else { |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 429 | vrm.assignVirtReMatId(NewVReg, ReMatIds[I->valno->id]); |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 430 | } |
| 431 | if (!CanDelete || (HasUse && HasDef)) { |
| 432 | // If this is a two-addr instruction then its use operands are |
| 433 | // rematerializable but its def is not. It should be assigned a |
| 434 | // stack slot. |
| 435 | vrm.assignVirt2StackSlot(NewVReg, slot); |
| 436 | } |
| 437 | } else { |
| 438 | vrm.assignVirt2StackSlot(NewVReg, slot); |
| 439 | } |
| 440 | |
| 441 | // create a new register interval for this spill / remat. |
| 442 | LiveInterval &nI = getOrCreateInterval(NewVReg); |
| 443 | assert(nI.empty()); |
| 444 | |
| 445 | // the spill weight is now infinity as it |
| 446 | // cannot be spilled again |
| 447 | nI.weight = HUGE_VALF; |
| 448 | |
| 449 | if (HasUse) { |
| 450 | LiveRange LR(getLoadIndex(index), getUseIndex(index), |
| 451 | nI.getNextValue(~0U, 0)); |
| 452 | DOUT << " +" << LR; |
| 453 | nI.addRange(LR); |
| 454 | } |
| 455 | if (HasDef) { |
| 456 | LiveRange LR(getDefIndex(index), getStoreIndex(index), |
| 457 | nI.getNextValue(~0U, 0)); |
| 458 | DOUT << " +" << LR; |
| 459 | nI.addRange(LR); |
| 460 | } |
| 461 | |
| 462 | added.push_back(&nI); |
| 463 | |
| 464 | // update live variables if it is available |
| 465 | if (lv_) |
| 466 | lv_->addVirtualRegisterKilled(NewVReg, MI); |
| 467 | |
| 468 | DOUT << "\t\t\t\tadded new interval: "; |
| 469 | nI.print(DOUT, mri_); |
| 470 | DOUT << '\n'; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return added; |
| 477 | } |
| 478 | |
| 479 | void LiveIntervals::printRegName(unsigned reg) const { |
| 480 | if (MRegisterInfo::isPhysicalRegister(reg)) |
| 481 | cerr << mri_->getName(reg); |
| 482 | else |
| 483 | cerr << "%reg" << reg; |
| 484 | } |
| 485 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 486 | void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb, |
| 487 | MachineBasicBlock::iterator mi, |
| 488 | unsigned MIIdx, |
| 489 | LiveInterval &interval) { |
| 490 | DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg)); |
| 491 | LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg); |
| 492 | |
| 493 | // Virtual registers may be defined multiple times (due to phi |
| 494 | // elimination and 2-addr elimination). Much of what we do only has to be |
| 495 | // done once for the vreg. We use an empty interval to detect the first |
| 496 | // time we see a vreg. |
| 497 | if (interval.empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 498 | // Get the Idx of the defining instructions. |
| 499 | unsigned defIndex = getDefIndex(MIIdx); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 500 | VNInfo *ValNo; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 501 | unsigned SrcReg, DstReg; |
| 502 | if (!tii_->isMoveInstr(*mi, SrcReg, DstReg)) |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 503 | ValNo = interval.getNextValue(defIndex, 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 504 | else |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 505 | ValNo = interval.getNextValue(defIndex, SrcReg); |
| 506 | |
| 507 | assert(ValNo->id == 0 && "First value in interval is not 0?"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 508 | |
| 509 | // Loop over all of the blocks that the vreg is defined in. There are |
| 510 | // two cases we have to handle here. The most common case is a vreg |
| 511 | // whose lifetime is contained within a basic block. In this case there |
| 512 | // will be a single kill, in MBB, which comes after the definition. |
| 513 | if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) { |
| 514 | // FIXME: what about dead vars? |
| 515 | unsigned killIdx; |
| 516 | if (vi.Kills[0] != mi) |
| 517 | killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1; |
| 518 | else |
| 519 | killIdx = defIndex+1; |
| 520 | |
| 521 | // If the kill happens after the definition, we have an intra-block |
| 522 | // live range. |
| 523 | if (killIdx > defIndex) { |
| 524 | assert(vi.AliveBlocks.none() && |
| 525 | "Shouldn't be alive across any blocks!"); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 526 | LiveRange LR(defIndex, killIdx, ValNo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 527 | interval.addRange(LR); |
| 528 | DOUT << " +" << LR << "\n"; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 529 | interval.addKill(*ValNo, killIdx); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // The other case we handle is when a virtual register lives to the end |
| 535 | // of the defining block, potentially live across some blocks, then is |
| 536 | // live into some number of blocks, but gets killed. Start by adding a |
| 537 | // range that goes from this definition to the end of the defining block. |
| 538 | LiveRange NewLR(defIndex, |
| 539 | getInstructionIndex(&mbb->back()) + InstrSlots::NUM, |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 540 | ValNo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 541 | DOUT << " +" << NewLR; |
| 542 | interval.addRange(NewLR); |
| 543 | |
| 544 | // Iterate over all of the blocks that the variable is completely |
| 545 | // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the |
| 546 | // live interval. |
| 547 | for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) { |
| 548 | if (vi.AliveBlocks[i]) { |
| 549 | MachineBasicBlock *MBB = mf_->getBlockNumbered(i); |
| 550 | if (!MBB->empty()) { |
| 551 | LiveRange LR(getMBBStartIdx(i), |
| 552 | getInstructionIndex(&MBB->back()) + InstrSlots::NUM, |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 553 | ValNo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 554 | interval.addRange(LR); |
| 555 | DOUT << " +" << LR; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // Finally, this virtual register is live from the start of any killing |
| 561 | // block to the 'use' slot of the killing instruction. |
| 562 | for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) { |
| 563 | MachineInstr *Kill = vi.Kills[i]; |
Evan Cheng | 58c2b76 | 2007-08-08 03:00:28 +0000 | [diff] [blame] | 564 | unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 565 | LiveRange LR(getMBBStartIdx(Kill->getParent()), |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 566 | killIdx, ValNo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 567 | interval.addRange(LR); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 568 | interval.addKill(*ValNo, killIdx); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 569 | DOUT << " +" << LR; |
| 570 | } |
| 571 | |
| 572 | } else { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 573 | // If this is the second time we see a virtual register definition, it |
| 574 | // must be due to phi elimination or two addr elimination. If this is |
| 575 | // the result of two address elimination, then the vreg is one of the |
| 576 | // def-and-use register operand. |
| 577 | if (isReDefinedByTwoAddr(mi, interval.reg, tii_)) { |
| 578 | // If this is a two-address definition, then we have already processed |
| 579 | // the live range. The only problem is that we didn't realize there |
| 580 | // are actually two values in the live interval. Because of this we |
| 581 | // need to take the LiveRegion that defines this register and split it |
| 582 | // into two values. |
| 583 | unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst)); |
| 584 | unsigned RedefIndex = getDefIndex(MIIdx); |
| 585 | |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 586 | const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 587 | VNInfo *OldValNo = OldLR->valno; |
Evan Cheng | 816a7f3 | 2007-08-11 00:59:19 +0000 | [diff] [blame] | 588 | unsigned OldEnd = OldLR->end; |
| 589 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 590 | // Delete the initial value, which should be short and continuous, |
| 591 | // because the 2-addr copy must be in the same MBB as the redef. |
| 592 | interval.removeRange(DefIndex, RedefIndex); |
| 593 | |
| 594 | // Two-address vregs should always only be redefined once. This means |
| 595 | // that at this point, there should be exactly one value number in it. |
| 596 | assert(interval.containsOneValue() && "Unexpected 2-addr liveint!"); |
| 597 | |
| 598 | // The new value number (#1) is defined by the instruction we claimed |
| 599 | // defined value #0. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 600 | VNInfo *ValNo = interval.getNextValue(0, 0); |
| 601 | interval.copyValNumInfo(*ValNo, *OldValNo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 602 | |
| 603 | // Value#0 is now defined by the 2-addr instruction. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 604 | OldValNo->def = RedefIndex; |
| 605 | OldValNo->reg = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 606 | |
| 607 | // Add the new live interval which replaces the range for the input copy. |
| 608 | LiveRange LR(DefIndex, RedefIndex, ValNo); |
| 609 | DOUT << " replace range with " << LR; |
| 610 | interval.addRange(LR); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 611 | interval.addKill(*ValNo, RedefIndex); |
| 612 | interval.removeKills(*ValNo, RedefIndex, OldEnd); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 613 | |
| 614 | // If this redefinition is dead, we need to add a dummy unit live |
| 615 | // range covering the def slot. |
| 616 | if (lv_->RegisterDefIsDead(mi, interval.reg)) |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 617 | interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 618 | |
| 619 | DOUT << " RESULT: "; |
| 620 | interval.print(DOUT, mri_); |
| 621 | |
| 622 | } else { |
| 623 | // Otherwise, this must be because of phi elimination. If this is the |
| 624 | // first redefinition of the vreg that we have seen, go back and change |
| 625 | // the live range in the PHI block to be a different value number. |
| 626 | if (interval.containsOneValue()) { |
| 627 | assert(vi.Kills.size() == 1 && |
| 628 | "PHI elimination vreg should have one kill, the PHI itself!"); |
| 629 | |
| 630 | // Remove the old range that we now know has an incorrect number. |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 631 | VNInfo *VNI = interval.getFirstValNumInfo(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 632 | MachineInstr *Killer = vi.Kills[0]; |
| 633 | unsigned Start = getMBBStartIdx(Killer->getParent()); |
| 634 | unsigned End = getUseIndex(getInstructionIndex(Killer))+1; |
| 635 | DOUT << " Removing [" << Start << "," << End << "] from: "; |
| 636 | interval.print(DOUT, mri_); DOUT << "\n"; |
| 637 | interval.removeRange(Start, End); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 638 | interval.addKill(*VNI, Start+1); // odd # means phi node |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 639 | DOUT << " RESULT: "; interval.print(DOUT, mri_); |
| 640 | |
| 641 | // Replace the interval with one of a NEW value number. Note that this |
| 642 | // value number isn't actually defined by an instruction, weird huh? :) |
Evan Cheng | 4151fde | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 643 | LiveRange LR(Start, End, interval.getNextValue(~0, 0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 644 | DOUT << " replace range with " << LR; |
| 645 | interval.addRange(LR); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 646 | interval.addKill(*LR.valno, End); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 647 | DOUT << " RESULT: "; interval.print(DOUT, mri_); |
| 648 | } |
| 649 | |
| 650 | // In the case of PHI elimination, each variable definition is only |
| 651 | // live until the end of the block. We've already taken care of the |
| 652 | // rest of the live range. |
| 653 | unsigned defIndex = getDefIndex(MIIdx); |
| 654 | |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 655 | VNInfo *ValNo; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 656 | unsigned SrcReg, DstReg; |
| 657 | if (!tii_->isMoveInstr(*mi, SrcReg, DstReg)) |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 658 | ValNo = interval.getNextValue(defIndex, 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 659 | else |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 660 | ValNo = interval.getNextValue(defIndex, SrcReg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 661 | |
Evan Cheng | 0f72734 | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 662 | unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM; |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 663 | LiveRange LR(defIndex, killIndex, ValNo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 664 | interval.addRange(LR); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 665 | interval.addKill(*ValNo, killIndex-1); // odd # means phi node |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 666 | DOUT << " +" << LR; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | DOUT << '\n'; |
| 671 | } |
| 672 | |
| 673 | void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB, |
| 674 | MachineBasicBlock::iterator mi, |
| 675 | unsigned MIIdx, |
| 676 | LiveInterval &interval, |
| 677 | unsigned SrcReg) { |
| 678 | // A physical register cannot be live across basic block, so its |
| 679 | // lifetime must end somewhere in its defining basic block. |
| 680 | DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg)); |
| 681 | |
| 682 | unsigned baseIndex = MIIdx; |
| 683 | unsigned start = getDefIndex(baseIndex); |
| 684 | unsigned end = start; |
| 685 | |
| 686 | // If it is not used after definition, it is considered dead at |
| 687 | // the instruction defining it. Hence its interval is: |
| 688 | // [defSlot(def), defSlot(def)+1) |
| 689 | if (lv_->RegisterDefIsDead(mi, interval.reg)) { |
| 690 | DOUT << " dead"; |
| 691 | end = getDefIndex(start) + 1; |
| 692 | goto exit; |
| 693 | } |
| 694 | |
| 695 | // If it is not dead on definition, it must be killed by a |
| 696 | // subsequent instruction. Hence its interval is: |
| 697 | // [defSlot(def), useSlot(kill)+1) |
| 698 | while (++mi != MBB->end()) { |
| 699 | baseIndex += InstrSlots::NUM; |
| 700 | if (lv_->KillsRegister(mi, interval.reg)) { |
| 701 | DOUT << " killed"; |
| 702 | end = getUseIndex(baseIndex) + 1; |
| 703 | goto exit; |
| 704 | } else if (lv_->ModifiesRegister(mi, interval.reg)) { |
| 705 | // Another instruction redefines the register before it is ever read. |
| 706 | // Then the register is essentially dead at the instruction that defines |
| 707 | // it. Hence its interval is: |
| 708 | // [defSlot(def), defSlot(def)+1) |
| 709 | DOUT << " dead"; |
| 710 | end = getDefIndex(start) + 1; |
| 711 | goto exit; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | // The only case we should have a dead physreg here without a killing or |
| 716 | // instruction where we know it's dead is if it is live-in to the function |
| 717 | // and never used. |
| 718 | assert(!SrcReg && "physreg was not killed in defining block!"); |
| 719 | end = getDefIndex(start) + 1; // It's dead. |
| 720 | |
| 721 | exit: |
| 722 | assert(start < end && "did not find end of interval?"); |
| 723 | |
| 724 | // Already exists? Extend old live interval. |
| 725 | LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 726 | VNInfo *ValNo = (OldLR != interval.end()) |
| 727 | ? OldLR->valno : interval.getNextValue(start, SrcReg); |
| 728 | LiveRange LR(start, end, ValNo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 729 | interval.addRange(LR); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 730 | interval.addKill(*LR.valno, end); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 731 | DOUT << " +" << LR << '\n'; |
| 732 | } |
| 733 | |
| 734 | void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB, |
| 735 | MachineBasicBlock::iterator MI, |
| 736 | unsigned MIIdx, |
| 737 | unsigned reg) { |
| 738 | if (MRegisterInfo::isVirtualRegister(reg)) |
| 739 | handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg)); |
| 740 | else if (allocatableRegs_[reg]) { |
| 741 | unsigned SrcReg, DstReg; |
| 742 | if (!tii_->isMoveInstr(*MI, SrcReg, DstReg)) |
| 743 | SrcReg = 0; |
| 744 | handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg); |
| 745 | // Def of a register also defines its sub-registers. |
| 746 | for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS) |
| 747 | // Avoid processing some defs more than once. |
| 748 | if (!MI->findRegisterDefOperand(*AS)) |
| 749 | handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB, |
| 754 | unsigned MIIdx, |
| 755 | LiveInterval &interval, bool isAlias) { |
| 756 | DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg)); |
| 757 | |
| 758 | // Look for kills, if it reaches a def before it's killed, then it shouldn't |
| 759 | // be considered a livein. |
| 760 | MachineBasicBlock::iterator mi = MBB->begin(); |
| 761 | unsigned baseIndex = MIIdx; |
| 762 | unsigned start = baseIndex; |
| 763 | unsigned end = start; |
| 764 | while (mi != MBB->end()) { |
| 765 | if (lv_->KillsRegister(mi, interval.reg)) { |
| 766 | DOUT << " killed"; |
| 767 | end = getUseIndex(baseIndex) + 1; |
| 768 | goto exit; |
| 769 | } else if (lv_->ModifiesRegister(mi, interval.reg)) { |
| 770 | // Another instruction redefines the register before it is ever read. |
| 771 | // Then the register is essentially dead at the instruction that defines |
| 772 | // it. Hence its interval is: |
| 773 | // [defSlot(def), defSlot(def)+1) |
| 774 | DOUT << " dead"; |
| 775 | end = getDefIndex(start) + 1; |
| 776 | goto exit; |
| 777 | } |
| 778 | |
| 779 | baseIndex += InstrSlots::NUM; |
| 780 | ++mi; |
| 781 | } |
| 782 | |
| 783 | exit: |
| 784 | // Live-in register might not be used at all. |
| 785 | if (end == MIIdx) { |
| 786 | if (isAlias) { |
| 787 | DOUT << " dead"; |
| 788 | end = getDefIndex(MIIdx) + 1; |
| 789 | } else { |
| 790 | DOUT << " live through"; |
| 791 | end = baseIndex; |
| 792 | } |
| 793 | } |
| 794 | |
Evan Cheng | 4151fde | 2007-08-07 23:49:57 +0000 | [diff] [blame] | 795 | LiveRange LR(start, end, interval.getNextValue(start, 0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 796 | interval.addRange(LR); |
Evan Cheng | 983b81d | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 797 | interval.addKill(*LR.valno, end); |
Evan Cheng | 0f72734 | 2007-08-08 07:03:29 +0000 | [diff] [blame] | 798 | DOUT << " +" << LR << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /// computeIntervals - computes the live intervals for virtual |
| 802 | /// registers. for some ordering of the machine instructions [1,N] a |
| 803 | /// live interval is an interval [i, j) where 1 <= i <= j < N for |
| 804 | /// which a variable is live |
| 805 | void LiveIntervals::computeIntervals() { |
| 806 | DOUT << "********** COMPUTING LIVE INTERVALS **********\n" |
| 807 | << "********** Function: " |
| 808 | << ((Value*)mf_->getFunction())->getName() << '\n'; |
| 809 | // Track the index of the current machine instr. |
| 810 | unsigned MIIndex = 0; |
| 811 | for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end(); |
| 812 | MBBI != E; ++MBBI) { |
| 813 | MachineBasicBlock *MBB = MBBI; |
| 814 | DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n"; |
| 815 | |
| 816 | MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end(); |
| 817 | |
| 818 | if (MBB->livein_begin() != MBB->livein_end()) { |
| 819 | // Create intervals for live-ins to this BB first. |
| 820 | for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(), |
| 821 | LE = MBB->livein_end(); LI != LE; ++LI) { |
| 822 | handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI)); |
| 823 | // Multiple live-ins can alias the same register. |
| 824 | for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS) |
| 825 | if (!hasInterval(*AS)) |
| 826 | handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS), |
| 827 | true); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | for (; MI != miEnd; ++MI) { |
| 832 | DOUT << MIIndex << "\t" << *MI; |
| 833 | |
| 834 | // Handle defs. |
| 835 | for (int i = MI->getNumOperands() - 1; i >= 0; --i) { |
| 836 | MachineOperand &MO = MI->getOperand(i); |
| 837 | // handle register defs - build intervals |
| 838 | if (MO.isRegister() && MO.getReg() && MO.isDef()) |
| 839 | handleRegisterDef(MBB, MI, MIIndex, MO.getReg()); |
| 840 | } |
| 841 | |
| 842 | MIIndex += InstrSlots::NUM; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | LiveInterval LiveIntervals::createInterval(unsigned reg) { |
| 848 | float Weight = MRegisterInfo::isPhysicalRegister(reg) ? |
| 849 | HUGE_VALF : 0.0F; |
| 850 | return LiveInterval(reg, Weight); |
| 851 | } |