1 //===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "liveintervals"
19 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
20 #include "VirtRegMap.h"
21 #include "llvm/Value.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/CodeGen/LiveVariables.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineLoopInfo.h"
28 #include "llvm/CodeGen/MachineMemOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/Passes.h"
31 #include "llvm/CodeGen/ProcessImplicitDefs.h"
32 #include "llvm/Target/TargetRegisterInfo.h"
33 #include "llvm/Target/TargetInstrInfo.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/ADT/DepthFirstIterator.h"
41 #include "llvm/ADT/SmallSet.h"
42 #include "llvm/ADT/Statistic.h"
43 #include "llvm/ADT/STLExtras.h"
49 // Hidden options for help debugging.
50 static cl::opt<bool> DisableReMat("disable-rematerialization",
51 cl::init(false), cl::Hidden);
53 static cl::opt<bool> EnableFastSpilling("fast-spill",
54 cl::init(false), cl::Hidden);
56 STATISTIC(numIntervals , "Number of original intervals");
57 STATISTIC(numFolds , "Number of loads/stores folded into instructions");
58 STATISTIC(numSplits , "Number of intervals split");
60 char LiveIntervals::ID = 0;
61 static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
63 void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.addRequired<AliasAnalysis>();
66 AU.addPreserved<AliasAnalysis>();
67 AU.addPreserved<LiveVariables>();
68 AU.addRequired<LiveVariables>();
69 AU.addPreservedID(MachineLoopInfoID);
70 AU.addPreservedID(MachineDominatorsID);
73 AU.addPreservedID(PHIEliminationID);
74 AU.addRequiredID(PHIEliminationID);
77 AU.addRequiredID(TwoAddressInstructionPassID);
78 AU.addPreserved<ProcessImplicitDefs>();
79 AU.addRequired<ProcessImplicitDefs>();
80 AU.addPreserved<SlotIndexes>();
81 AU.addRequiredTransitive<SlotIndexes>();
82 MachineFunctionPass::getAnalysisUsage(AU);
85 void LiveIntervals::releaseMemory() {
86 // Free the live intervals themselves.
87 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
88 E = r2iMap_.end(); I != E; ++I)
93 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
94 VNInfoAllocator.Reset();
95 while (!CloneMIs.empty()) {
96 MachineInstr *MI = CloneMIs.back();
98 mf_->DeleteMachineInstr(MI);
102 /// runOnMachineFunction - Register allocate the whole function
104 bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
106 mri_ = &mf_->getRegInfo();
107 tm_ = &fn.getTarget();
108 tri_ = tm_->getRegisterInfo();
109 tii_ = tm_->getInstrInfo();
110 aa_ = &getAnalysis<AliasAnalysis>();
111 lv_ = &getAnalysis<LiveVariables>();
112 indexes_ = &getAnalysis<SlotIndexes>();
113 allocatableRegs_ = tri_->getAllocatableSet(fn);
117 numIntervals += getNumIntervals();
123 /// print - Implement the dump method.
124 void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
125 OS << "********** INTERVALS **********\n";
126 for (const_iterator I = begin(), E = end(); I != E; ++I) {
127 I->second->print(OS, tri_);
134 void LiveIntervals::printInstrs(raw_ostream &OS) const {
135 OS << "********** MACHINEINSTRS **********\n";
137 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
138 mbbi != mbbe; ++mbbi) {
139 OS << "BB#" << mbbi->getNumber()
140 << ":\t\t# derived from " << mbbi->getName() << "\n";
141 for (MachineBasicBlock::iterator mii = mbbi->begin(),
142 mie = mbbi->end(); mii != mie; ++mii) {
143 OS << getInstructionIndex(mii) << '\t' << *mii;
148 void LiveIntervals::dumpInstrs() const {
152 /// conflictsWithPhysRegDef - Returns true if the specified register
153 /// is defined during the duration of the specified interval.
154 bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
155 VirtRegMap &vrm, unsigned reg) {
156 for (LiveInterval::Ranges::const_iterator
157 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
158 for (SlotIndex index = I->start.getBaseIndex(),
159 end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
161 index = index.getNextIndex()) {
162 // skip deleted instructions
163 while (index != end && !getInstructionFromIndex(index))
164 index = index.getNextIndex();
165 if (index == end) break;
167 MachineInstr *MI = getInstructionFromIndex(index);
168 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
169 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
170 if (SrcReg == li.reg || DstReg == li.reg)
172 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
173 MachineOperand& mop = MI->getOperand(i);
176 unsigned PhysReg = mop.getReg();
177 if (PhysReg == 0 || PhysReg == li.reg)
179 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
180 if (!vrm.hasPhys(PhysReg))
182 PhysReg = vrm.getPhys(PhysReg);
184 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
193 /// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
194 /// it can check use as well.
195 bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
196 unsigned Reg, bool CheckUse,
197 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
198 for (LiveInterval::Ranges::const_iterator
199 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
200 for (SlotIndex index = I->start.getBaseIndex(),
201 end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
203 index = index.getNextIndex()) {
204 // Skip deleted instructions.
205 MachineInstr *MI = 0;
206 while (index != end) {
207 MI = getInstructionFromIndex(index);
210 index = index.getNextIndex();
212 if (index == end) break;
214 if (JoinedCopies.count(MI))
216 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
217 MachineOperand& MO = MI->getOperand(i);
220 if (MO.isUse() && !CheckUse)
222 unsigned PhysReg = MO.getReg();
223 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
225 if (tri_->isSubRegister(Reg, PhysReg))
235 static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
236 if (TargetRegisterInfo::isPhysicalRegister(reg))
237 errs() << tri_->getName(reg);
239 errs() << "%reg" << reg;
243 void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
244 MachineBasicBlock::iterator mi,
248 LiveInterval &interval) {
250 errs() << "\t\tregister: ";
251 printRegName(interval.reg, tri_);
254 // Virtual registers may be defined multiple times (due to phi
255 // elimination and 2-addr elimination). Much of what we do only has to be
256 // done once for the vreg. We use an empty interval to detect the first
257 // time we see a vreg.
258 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
259 if (interval.empty()) {
260 // Get the Idx of the defining instructions.
261 SlotIndex defIndex = MIIdx.getDefIndex();
262 // Earlyclobbers move back one, so that they overlap the live range
264 if (MO.isEarlyClobber())
265 defIndex = MIIdx.getUseIndex();
267 MachineInstr *CopyMI = NULL;
268 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
269 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
270 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
271 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
272 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
274 // Earlyclobbers move back one.
275 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
277 assert(ValNo->id == 0 && "First value in interval is not 0?");
279 // Loop over all of the blocks that the vreg is defined in. There are
280 // two cases we have to handle here. The most common case is a vreg
281 // whose lifetime is contained within a basic block. In this case there
282 // will be a single kill, in MBB, which comes after the definition.
283 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
284 // FIXME: what about dead vars?
286 if (vi.Kills[0] != mi)
287 killIdx = getInstructionIndex(vi.Kills[0]).getDefIndex();
289 killIdx = defIndex.getStoreIndex();
291 // If the kill happens after the definition, we have an intra-block
293 if (killIdx > defIndex) {
294 assert(vi.AliveBlocks.empty() &&
295 "Shouldn't be alive across any blocks!");
296 LiveRange LR(defIndex, killIdx, ValNo);
297 interval.addRange(LR);
298 DEBUG(errs() << " +" << LR << "\n");
299 ValNo->addKill(killIdx);
304 // The other case we handle is when a virtual register lives to the end
305 // of the defining block, potentially live across some blocks, then is
306 // live into some number of blocks, but gets killed. Start by adding a
307 // range that goes from this definition to the end of the defining block.
308 LiveRange NewLR(defIndex, getMBBEndIdx(mbb).getNextIndex().getLoadIndex(),
310 DEBUG(errs() << " +" << NewLR);
311 interval.addRange(NewLR);
313 // Iterate over all of the blocks that the variable is completely
314 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
316 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
317 E = vi.AliveBlocks.end(); I != E; ++I) {
319 getMBBStartIdx(mf_->getBlockNumbered(*I)),
320 getMBBEndIdx(mf_->getBlockNumbered(*I)).getNextIndex().getLoadIndex(),
322 interval.addRange(LR);
323 DEBUG(errs() << " +" << LR);
326 // Finally, this virtual register is live from the start of any killing
327 // block to the 'use' slot of the killing instruction.
328 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
329 MachineInstr *Kill = vi.Kills[i];
331 getInstructionIndex(Kill).getDefIndex();
332 LiveRange LR(getMBBStartIdx(Kill->getParent()), killIdx, ValNo);
333 interval.addRange(LR);
334 ValNo->addKill(killIdx);
335 DEBUG(errs() << " +" << LR);
339 // If this is the second time we see a virtual register definition, it
340 // must be due to phi elimination or two addr elimination. If this is
341 // the result of two address elimination, then the vreg is one of the
342 // def-and-use register operand.
343 if (mi->isRegTiedToUseOperand(MOIdx)) {
344 // If this is a two-address definition, then we have already processed
345 // the live range. The only problem is that we didn't realize there
346 // are actually two values in the live interval. Because of this we
347 // need to take the LiveRegion that defines this register and split it
349 assert(interval.containsOneValue());
350 SlotIndex DefIndex = interval.getValNumInfo(0)->def.getDefIndex();
351 SlotIndex RedefIndex = MIIdx.getDefIndex();
352 if (MO.isEarlyClobber())
353 RedefIndex = MIIdx.getUseIndex();
355 const LiveRange *OldLR =
356 interval.getLiveRangeContaining(RedefIndex.getUseIndex());
357 VNInfo *OldValNo = OldLR->valno;
359 // Delete the initial value, which should be short and continuous,
360 // because the 2-addr copy must be in the same MBB as the redef.
361 interval.removeRange(DefIndex, RedefIndex);
363 // Two-address vregs should always only be redefined once. This means
364 // that at this point, there should be exactly one value number in it.
365 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
367 // The new value number (#1) is defined by the instruction we claimed
369 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
370 false, // update at *
372 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
374 // Value#0 is now defined by the 2-addr instruction.
375 OldValNo->def = RedefIndex;
376 OldValNo->setCopy(0);
378 // Add the new live interval which replaces the range for the input copy.
379 LiveRange LR(DefIndex, RedefIndex, ValNo);
380 DEBUG(errs() << " replace range with " << LR);
381 interval.addRange(LR);
382 ValNo->addKill(RedefIndex);
384 // If this redefinition is dead, we need to add a dummy unit live
385 // range covering the def slot.
387 interval.addRange(LiveRange(RedefIndex, RedefIndex.getStoreIndex(),
391 errs() << " RESULT: ";
392 interval.print(errs(), tri_);
395 // Otherwise, this must be because of phi elimination. If this is the
396 // first redefinition of the vreg that we have seen, go back and change
397 // the live range in the PHI block to be a different value number.
398 if (interval.containsOneValue()) {
399 // Remove the old range that we now know has an incorrect number.
400 VNInfo *VNI = interval.getValNumInfo(0);
401 MachineInstr *Killer = vi.Kills[0];
402 SlotIndex Start = getMBBStartIdx(Killer->getParent());
403 SlotIndex End = getInstructionIndex(Killer).getDefIndex();
405 errs() << " Removing [" << Start << "," << End << "] from: ";
406 interval.print(errs(), tri_);
409 interval.removeRange(Start, End);
410 assert(interval.ranges.size() == 1 &&
411 "Newly discovered PHI interval has >1 ranges.");
412 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
413 VNI->addKill(indexes_->getTerminatorGap(killMBB));
414 VNI->setHasPHIKill(true);
416 errs() << " RESULT: ";
417 interval.print(errs(), tri_);
420 // Replace the interval with one of a NEW value number. Note that this
421 // value number isn't actually defined by an instruction, weird huh? :)
422 LiveRange LR(Start, End,
423 interval.getNextValue(SlotIndex(getMBBStartIdx(mbb), true),
424 0, false, VNInfoAllocator));
425 LR.valno->setIsPHIDef(true);
426 DEBUG(errs() << " replace range with " << LR);
427 interval.addRange(LR);
428 LR.valno->addKill(End);
430 errs() << " RESULT: ";
431 interval.print(errs(), tri_);
435 // In the case of PHI elimination, each variable definition is only
436 // live until the end of the block. We've already taken care of the
437 // rest of the live range.
438 SlotIndex defIndex = MIIdx.getDefIndex();
439 if (MO.isEarlyClobber())
440 defIndex = MIIdx.getUseIndex();
443 MachineInstr *CopyMI = NULL;
444 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
445 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
446 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
447 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
448 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
450 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
452 SlotIndex killIndex = getMBBEndIdx(mbb).getNextIndex().getLoadIndex();
453 LiveRange LR(defIndex, killIndex, ValNo);
454 interval.addRange(LR);
455 ValNo->addKill(indexes_->getTerminatorGap(mbb));
456 ValNo->setHasPHIKill(true);
457 DEBUG(errs() << " +" << LR);
461 DEBUG(errs() << '\n');
464 void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
465 MachineBasicBlock::iterator mi,
468 LiveInterval &interval,
469 MachineInstr *CopyMI) {
470 // A physical register cannot be live across basic block, so its
471 // lifetime must end somewhere in its defining basic block.
473 errs() << "\t\tregister: ";
474 printRegName(interval.reg, tri_);
477 SlotIndex baseIndex = MIIdx;
478 SlotIndex start = baseIndex.getDefIndex();
479 // Earlyclobbers move back one.
480 if (MO.isEarlyClobber())
481 start = MIIdx.getUseIndex();
482 SlotIndex end = start;
484 // If it is not used after definition, it is considered dead at
485 // the instruction defining it. Hence its interval is:
486 // [defSlot(def), defSlot(def)+1)
487 // For earlyclobbers, the defSlot was pushed back one; the extra
488 // advance below compensates.
490 DEBUG(errs() << " dead");
491 end = start.getStoreIndex();
495 // If it is not dead on definition, it must be killed by a
496 // subsequent instruction. Hence its interval is:
497 // [defSlot(def), useSlot(kill)+1)
498 baseIndex = baseIndex.getNextIndex();
499 while (++mi != MBB->end()) {
501 if (getInstructionFromIndex(baseIndex) == 0)
502 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
504 if (mi->killsRegister(interval.reg, tri_)) {
505 DEBUG(errs() << " killed");
506 end = baseIndex.getDefIndex();
509 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
511 if (mi->isRegTiedToUseOperand(DefIdx)) {
512 // Two-address instruction.
513 end = baseIndex.getDefIndex();
515 // Another instruction redefines the register before it is ever read.
516 // Then the register is essentially dead at the instruction that defines
517 // it. Hence its interval is:
518 // [defSlot(def), defSlot(def)+1)
519 DEBUG(errs() << " dead");
520 end = start.getStoreIndex();
526 baseIndex = baseIndex.getNextIndex();
529 // The only case we should have a dead physreg here without a killing or
530 // instruction where we know it's dead is if it is live-in to the function
531 // and never used. Another possible case is the implicit use of the
532 // physical register has been deleted by two-address pass.
533 end = start.getStoreIndex();
536 assert(start < end && "did not find end of interval?");
538 // Already exists? Extend old live interval.
539 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
540 bool Extend = OldLR != interval.end();
541 VNInfo *ValNo = Extend
542 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
543 if (MO.isEarlyClobber() && Extend)
544 ValNo->setHasRedefByEC(true);
545 LiveRange LR(start, end, ValNo);
546 interval.addRange(LR);
547 LR.valno->addKill(end);
548 DEBUG(errs() << " +" << LR << '\n');
551 void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
552 MachineBasicBlock::iterator MI,
556 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
557 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
558 getOrCreateInterval(MO.getReg()));
559 else if (allocatableRegs_[MO.getReg()]) {
560 MachineInstr *CopyMI = NULL;
561 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
562 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
563 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
564 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
565 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
567 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
568 getOrCreateInterval(MO.getReg()), CopyMI);
569 // Def of a register also defines its sub-registers.
570 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
571 // If MI also modifies the sub-register explicitly, avoid processing it
572 // more than once. Do not pass in TRI here so it checks for exact match.
573 if (!MI->modifiesRegister(*AS))
574 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
575 getOrCreateInterval(*AS), 0);
579 void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
581 LiveInterval &interval, bool isAlias) {
583 errs() << "\t\tlivein register: ";
584 printRegName(interval.reg, tri_);
587 // Look for kills, if it reaches a def before it's killed, then it shouldn't
588 // be considered a livein.
589 MachineBasicBlock::iterator mi = MBB->begin();
590 SlotIndex baseIndex = MIIdx;
591 SlotIndex start = baseIndex;
592 if (getInstructionFromIndex(baseIndex) == 0)
593 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
595 SlotIndex end = baseIndex;
596 bool SeenDefUse = false;
598 while (mi != MBB->end()) {
599 if (mi->killsRegister(interval.reg, tri_)) {
600 DEBUG(errs() << " killed");
601 end = baseIndex.getDefIndex();
604 } else if (mi->modifiesRegister(interval.reg, tri_)) {
605 // Another instruction redefines the register before it is ever read.
606 // Then the register is essentially dead at the instruction that defines
607 // it. Hence its interval is:
608 // [defSlot(def), defSlot(def)+1)
609 DEBUG(errs() << " dead");
610 end = start.getStoreIndex();
616 if (mi != MBB->end()) {
617 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
621 // Live-in register might not be used at all.
624 DEBUG(errs() << " dead");
625 end = MIIdx.getStoreIndex();
627 DEBUG(errs() << " live through");
633 interval.getNextValue(SlotIndex(getMBBStartIdx(MBB), true),
634 0, false, VNInfoAllocator);
635 vni->setIsPHIDef(true);
636 LiveRange LR(start, end, vni);
638 interval.addRange(LR);
639 LR.valno->addKill(end);
640 DEBUG(errs() << " +" << LR << '\n');
643 /// computeIntervals - computes the live intervals for virtual
644 /// registers. for some ordering of the machine instructions [1,N] a
645 /// live interval is an interval [i, j) where 1 <= i <= j < N for
646 /// which a variable is live
647 void LiveIntervals::computeIntervals() {
648 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
649 << "********** Function: "
650 << ((Value*)mf_->getFunction())->getName() << '\n');
652 SmallVector<unsigned, 8> UndefUses;
653 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
655 MachineBasicBlock *MBB = MBBI;
656 // Track the index of the current machine instr.
657 SlotIndex MIIndex = getMBBStartIdx(MBB);
658 DEBUG(errs() << MBB->getName() << ":\n");
660 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
662 // Create intervals for live-ins to this BB first.
663 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
664 LE = MBB->livein_end(); LI != LE; ++LI) {
665 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
666 // Multiple live-ins can alias the same register.
667 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
668 if (!hasInterval(*AS))
669 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
673 // Skip over empty initial indices.
674 if (getInstructionFromIndex(MIIndex) == 0)
675 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
677 for (; MI != miEnd; ++MI) {
678 DEBUG(errs() << MIIndex << "\t" << *MI);
681 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
682 MachineOperand &MO = MI->getOperand(i);
683 if (!MO.isReg() || !MO.getReg())
686 // handle register defs - build intervals
688 handleRegisterDef(MBB, MI, MIIndex, MO, i);
689 else if (MO.isUndef())
690 UndefUses.push_back(MO.getReg());
693 // Move to the next instr slot.
694 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
698 // Create empty intervals for registers defined by implicit_def's (except
699 // for those implicit_def that define values which are liveout of their
701 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
702 unsigned UndefReg = UndefUses[i];
703 (void)getOrCreateInterval(UndefReg);
707 LiveInterval* LiveIntervals::createInterval(unsigned reg) {
708 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
709 return new LiveInterval(reg, Weight);
712 /// dupInterval - Duplicate a live interval. The caller is responsible for
713 /// managing the allocated memory.
714 LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
715 LiveInterval *NewLI = createInterval(li->reg);
716 NewLI->Copy(*li, mri_, getVNInfoAllocator());
720 /// getVNInfoSourceReg - Helper function that parses the specified VNInfo
721 /// copy field and returns the source register that defines it.
722 unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
726 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
727 // If it's extracting out of a physical register, return the sub-register.
728 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
729 if (TargetRegisterInfo::isPhysicalRegister(Reg))
730 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
732 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
733 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
734 return VNI->getCopy()->getOperand(2).getReg();
736 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
737 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
739 llvm_unreachable("Unrecognized copy instruction!");
743 //===----------------------------------------------------------------------===//
744 // Register allocator hooks.
747 /// getReMatImplicitUse - If the remat definition MI has one (for now, we only
748 /// allow one) virtual register operand, then its uses are implicitly using
749 /// the register. Returns the virtual register.
750 unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
751 MachineInstr *MI) const {
753 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
754 MachineOperand &MO = MI->getOperand(i);
755 if (!MO.isReg() || !MO.isUse())
757 unsigned Reg = MO.getReg();
758 if (Reg == 0 || Reg == li.reg)
761 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
762 !allocatableRegs_[Reg])
764 // FIXME: For now, only remat MI with at most one register operand.
766 "Can't rematerialize instruction with multiple register operand!");
775 /// isValNoAvailableAt - Return true if the val# of the specified interval
776 /// which reaches the given instruction also reaches the specified use index.
777 bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
778 SlotIndex UseIdx) const {
779 SlotIndex Index = getInstructionIndex(MI);
780 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
781 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
782 return UI != li.end() && UI->valno == ValNo;
785 /// isReMaterializable - Returns true if the definition MI of the specified
786 /// val# of the specified interval is re-materializable.
787 bool LiveIntervals::isReMaterializable(const LiveInterval &li,
788 const VNInfo *ValNo, MachineInstr *MI,
789 SmallVectorImpl<LiveInterval*> &SpillIs,
794 if (!tii_->isTriviallyReMaterializable(MI, aa_))
797 // Target-specific code can mark an instruction as being rematerializable
798 // if it has one virtual reg use, though it had better be something like
799 // a PIC base register which is likely to be live everywhere.
800 unsigned ImpUse = getReMatImplicitUse(li, MI);
802 const LiveInterval &ImpLi = getInterval(ImpUse);
803 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
804 re = mri_->use_end(); ri != re; ++ri) {
805 MachineInstr *UseMI = &*ri;
806 SlotIndex UseIdx = getInstructionIndex(UseMI);
807 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
809 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
813 // If a register operand of the re-materialized instruction is going to
814 // be spilled next, then it's not legal to re-materialize this instruction.
815 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
816 if (ImpUse == SpillIs[i]->reg)
822 /// isReMaterializable - Returns true if the definition MI of the specified
823 /// val# of the specified interval is re-materializable.
824 bool LiveIntervals::isReMaterializable(const LiveInterval &li,
825 const VNInfo *ValNo, MachineInstr *MI) {
826 SmallVector<LiveInterval*, 4> Dummy1;
828 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
831 /// isReMaterializable - Returns true if every definition of MI of every
832 /// val# of the specified interval is re-materializable.
833 bool LiveIntervals::isReMaterializable(const LiveInterval &li,
834 SmallVectorImpl<LiveInterval*> &SpillIs,
837 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
839 const VNInfo *VNI = *i;
841 continue; // Dead val#.
842 // Is the def for the val# rematerializable?
843 if (!VNI->isDefAccurate())
845 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
846 bool DefIsLoad = false;
848 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
855 /// FilterFoldedOps - Filter out two-address use operands. Return
856 /// true if it finds any issue with the operands that ought to prevent
858 static bool FilterFoldedOps(MachineInstr *MI,
859 SmallVector<unsigned, 2> &Ops,
861 SmallVector<unsigned, 2> &FoldOps) {
863 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
864 unsigned OpIdx = Ops[i];
865 MachineOperand &MO = MI->getOperand(OpIdx);
866 // FIXME: fold subreg use.
870 MRInfo |= (unsigned)VirtRegMap::isMod;
872 // Filter out two-address use operand(s).
873 if (MI->isRegTiedToDefOperand(OpIdx)) {
874 MRInfo = VirtRegMap::isModRef;
877 MRInfo |= (unsigned)VirtRegMap::isRef;
879 FoldOps.push_back(OpIdx);
885 /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
886 /// slot / to reg or any rematerialized load into ith operand of specified
887 /// MI. If it is successul, MI is updated with the newly created MI and
889 bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
890 VirtRegMap &vrm, MachineInstr *DefMI,
892 SmallVector<unsigned, 2> &Ops,
893 bool isSS, int Slot, unsigned Reg) {
894 // If it is an implicit def instruction, just delete it.
895 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
896 RemoveMachineInstrFromMaps(MI);
897 vrm.RemoveMachineInstrFromMaps(MI);
898 MI->eraseFromParent();
903 // Filter the list of operand indexes that are to be folded. Abort if
904 // any operand will prevent folding.
906 SmallVector<unsigned, 2> FoldOps;
907 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
910 // The only time it's safe to fold into a two address instruction is when
911 // it's folding reload and spill from / into a spill stack slot.
912 if (DefMI && (MRInfo & VirtRegMap::isMod))
915 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
916 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
918 // Remember this instruction uses the spill slot.
919 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
921 // Attempt to fold the memory reference into the instruction. If
922 // we can do this, we don't need to insert spill code.
923 MachineBasicBlock &MBB = *MI->getParent();
924 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
925 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
926 vrm.transferSpillPts(MI, fmi);
927 vrm.transferRestorePts(MI, fmi);
928 vrm.transferEmergencySpills(MI, fmi);
929 ReplaceMachineInstrInMaps(MI, fmi);
930 MI = MBB.insert(MBB.erase(MI), fmi);
937 /// canFoldMemoryOperand - Returns true if the specified load / store
938 /// folding is possible.
939 bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
940 SmallVector<unsigned, 2> &Ops,
942 // Filter the list of operand indexes that are to be folded. Abort if
943 // any operand will prevent folding.
945 SmallVector<unsigned, 2> FoldOps;
946 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
949 // It's only legal to remat for a use, not a def.
950 if (ReMat && (MRInfo & VirtRegMap::isMod))
953 return tii_->canFoldMemoryOperand(MI, FoldOps);
956 bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
957 LiveInterval::Ranges::const_iterator itr = li.ranges.begin();
959 MachineBasicBlock *mbb = indexes_->getMBBCoveringRange(itr->start, itr->end);
964 for (++itr; itr != li.ranges.end(); ++itr) {
965 MachineBasicBlock *mbb2 =
966 indexes_->getMBBCoveringRange(itr->start, itr->end);
975 /// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
976 /// interval on to-be re-materialized operands of MI) with new register.
977 void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
978 MachineInstr *MI, unsigned NewVReg,
980 // There is an implicit use. That means one of the other operand is
981 // being remat'ed and the remat'ed instruction has li.reg as an
982 // use operand. Make sure we rewrite that as well.
983 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
984 MachineOperand &MO = MI->getOperand(i);
987 unsigned Reg = MO.getReg();
988 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
990 if (!vrm.isReMaterialized(Reg))
992 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
993 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
995 UseMO->setReg(NewVReg);
999 /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1000 /// for addIntervalsForSpills to rewrite uses / defs for the given live range.
1001 bool LiveIntervals::
1002 rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
1003 bool TrySplit, SlotIndex index, SlotIndex end,
1005 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
1006 unsigned Slot, int LdSlot,
1007 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
1009 const TargetRegisterClass* rc,
1010 SmallVector<int, 4> &ReMatIds,
1011 const MachineLoopInfo *loopInfo,
1012 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
1013 DenseMap<unsigned,unsigned> &MBBVRegsMap,
1014 std::vector<LiveInterval*> &NewLIs) {
1015 bool CanFold = false;
1017 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1018 MachineOperand& mop = MI->getOperand(i);
1021 unsigned Reg = mop.getReg();
1022 unsigned RegI = Reg;
1023 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1028 bool TryFold = !DefIsReMat;
1029 bool FoldSS = true; // Default behavior unless it's a remat.
1030 int FoldSlot = Slot;
1032 // If this is the rematerializable definition MI itself and
1033 // all of its uses are rematerialized, simply delete it.
1034 if (MI == ReMatOrigDefMI && CanDelete) {
1035 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1037 RemoveMachineInstrFromMaps(MI);
1038 vrm.RemoveMachineInstrFromMaps(MI);
1039 MI->eraseFromParent();
1043 // If def for this use can't be rematerialized, then try folding.
1044 // If def is rematerializable and it's a load, also try folding.
1045 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
1047 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1053 // Scan all of the operands of this instruction rewriting operands
1054 // to use NewVReg instead of li.reg as appropriate. We do this for
1057 // 1. If the instr reads the same spilled vreg multiple times, we
1058 // want to reuse the NewVReg.
1059 // 2. If the instr is a two-addr instruction, we are required to
1060 // keep the src/dst regs pinned.
1062 // Keep track of whether we replace a use and/or def so that we can
1063 // create the spill interval with the appropriate range.
1065 HasUse = mop.isUse();
1066 HasDef = mop.isDef();
1067 SmallVector<unsigned, 2> Ops;
1069 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
1070 const MachineOperand &MOj = MI->getOperand(j);
1073 unsigned RegJ = MOj.getReg();
1074 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
1078 if (!MOj.isUndef()) {
1079 HasUse |= MOj.isUse();
1080 HasDef |= MOj.isDef();
1085 // Create a new virtual register for the spill interval.
1086 // Create the new register now so we can map the fold instruction
1087 // to the new register so when it is unfolded we get the correct
1089 bool CreatedNewVReg = false;
1091 NewVReg = mri_->createVirtualRegister(rc);
1093 CreatedNewVReg = true;
1095 // The new virtual register should get the same allocation hints as the
1097 std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(Reg);
1098 if (Hint.first || Hint.second)
1099 mri_->setRegAllocationHint(NewVReg, Hint.first, Hint.second);
1105 // Do not fold load / store here if we are splitting. We'll find an
1106 // optimal point to insert a load / store later.
1108 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1109 Ops, FoldSS, FoldSlot, NewVReg)) {
1110 // Folding the load/store can completely change the instruction in
1111 // unpredictable ways, rescan it from the beginning.
1114 // We need to give the new vreg the same stack slot as the
1115 // spilled interval.
1116 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1122 if (isNotInMIMap(MI))
1124 goto RestartInstruction;
1127 // We'll try to fold it later if it's profitable.
1128 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
1132 mop.setReg(NewVReg);
1133 if (mop.isImplicit())
1134 rewriteImplicitOps(li, MI, NewVReg, vrm);
1136 // Reuse NewVReg for other reads.
1137 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1138 MachineOperand &mopj = MI->getOperand(Ops[j]);
1139 mopj.setReg(NewVReg);
1140 if (mopj.isImplicit())
1141 rewriteImplicitOps(li, MI, NewVReg, vrm);
1144 if (CreatedNewVReg) {
1146 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
1147 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
1148 // Each valnum may have its own remat id.
1149 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
1151 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
1153 if (!CanDelete || (HasUse && HasDef)) {
1154 // If this is a two-addr instruction then its use operands are
1155 // rematerializable but its def is not. It should be assigned a
1157 vrm.assignVirt2StackSlot(NewVReg, Slot);
1160 vrm.assignVirt2StackSlot(NewVReg, Slot);
1162 } else if (HasUse && HasDef &&
1163 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1164 // If this interval hasn't been assigned a stack slot (because earlier
1165 // def is a deleted remat def), do it now.
1166 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1167 vrm.assignVirt2StackSlot(NewVReg, Slot);
1170 // Re-matting an instruction with virtual register use. Add the
1171 // register as an implicit use on the use MI.
1172 if (DefIsReMat && ImpUse)
1173 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1175 // Create a new register interval for this spill / remat.
1176 LiveInterval &nI = getOrCreateInterval(NewVReg);
1177 if (CreatedNewVReg) {
1178 NewLIs.push_back(&nI);
1179 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
1181 vrm.setIsSplitFromReg(NewVReg, li.reg);
1185 if (CreatedNewVReg) {
1186 LiveRange LR(index.getLoadIndex(), index.getDefIndex(),
1187 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
1188 DEBUG(errs() << " +" << LR);
1191 // Extend the split live interval to this def / use.
1192 SlotIndex End = index.getDefIndex();
1193 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1194 nI.getValNumInfo(nI.getNumValNums()-1));
1195 DEBUG(errs() << " +" << LR);
1200 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1201 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
1202 DEBUG(errs() << " +" << LR);
1207 errs() << "\t\t\t\tAdded new interval: ";
1208 nI.print(errs(), tri_);
1214 bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
1216 MachineBasicBlock *MBB,
1217 SlotIndex Idx) const {
1218 SlotIndex End = getMBBEndIdx(MBB);
1219 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1220 if (VNI->kills[j].isPHI())
1223 SlotIndex KillIdx = VNI->kills[j];
1224 if (KillIdx > Idx && KillIdx < End)
1230 /// RewriteInfo - Keep track of machine instrs that will be rewritten
1231 /// during spilling.
1233 struct RewriteInfo {
1238 RewriteInfo(SlotIndex i, MachineInstr *mi, bool u, bool d)
1239 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1242 struct RewriteInfoCompare {
1243 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1244 return LHS.Index < RHS.Index;
1249 void LiveIntervals::
1250 rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
1251 LiveInterval::Ranges::const_iterator &I,
1252 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
1253 unsigned Slot, int LdSlot,
1254 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
1256 const TargetRegisterClass* rc,
1257 SmallVector<int, 4> &ReMatIds,
1258 const MachineLoopInfo *loopInfo,
1259 BitVector &SpillMBBs,
1260 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
1261 BitVector &RestoreMBBs,
1262 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1263 DenseMap<unsigned,unsigned> &MBBVRegsMap,
1264 std::vector<LiveInterval*> &NewLIs) {
1265 bool AllCanFold = true;
1266 unsigned NewVReg = 0;
1267 SlotIndex start = I->start.getBaseIndex();
1268 SlotIndex end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
1270 // First collect all the def / use in this live range that will be rewritten.
1271 // Make sure they are sorted according to instruction index.
1272 std::vector<RewriteInfo> RewriteMIs;
1273 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1274 re = mri_->reg_end(); ri != re; ) {
1275 MachineInstr *MI = &*ri;
1276 MachineOperand &O = ri.getOperand();
1278 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
1279 SlotIndex index = getInstructionIndex(MI);
1280 if (index < start || index >= end)
1284 // Must be defined by an implicit def. It should not be spilled. Note,
1285 // this is for correctness reason. e.g.
1286 // 8 %reg1024<def> = IMPLICIT_DEF
1287 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1288 // The live range [12, 14) are not part of the r1024 live interval since
1289 // it's defined by an implicit def. It will not conflicts with live
1290 // interval of r1025. Now suppose both registers are spilled, you can
1291 // easily see a situation where both registers are reloaded before
1292 // the INSERT_SUBREG and both target registers that would overlap.
1294 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1296 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1298 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
1299 // Now rewrite the defs and uses.
1300 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1301 RewriteInfo &rwi = RewriteMIs[i];
1303 SlotIndex index = rwi.Index;
1304 bool MIHasUse = rwi.HasUse;
1305 bool MIHasDef = rwi.HasDef;
1306 MachineInstr *MI = rwi.MI;
1307 // If MI def and/or use the same register multiple times, then there
1308 // are multiple entries.
1309 unsigned NumUses = MIHasUse;
1310 while (i != e && RewriteMIs[i].MI == MI) {
1311 assert(RewriteMIs[i].Index == index);
1312 bool isUse = RewriteMIs[i].HasUse;
1313 if (isUse) ++NumUses;
1315 MIHasDef |= RewriteMIs[i].HasDef;
1318 MachineBasicBlock *MBB = MI->getParent();
1320 if (ImpUse && MI != ReMatDefMI) {
1321 // Re-matting an instruction with virtual register use. Update the
1322 // register interval's spill weight to HUGE_VALF to prevent it from
1324 LiveInterval &ImpLi = getInterval(ImpUse);
1325 ImpLi.weight = HUGE_VALF;
1328 unsigned MBBId = MBB->getNumber();
1329 unsigned ThisVReg = 0;
1331 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
1332 if (NVI != MBBVRegsMap.end()) {
1333 ThisVReg = NVI->second;
1340 // It's better to start a new interval to avoid artifically
1341 // extend the new interval.
1342 if (MIHasDef && !MIHasUse) {
1343 MBBVRegsMap.erase(MBB->getNumber());
1349 bool IsNew = ThisVReg == 0;
1351 // This ends the previous live interval. If all of its def / use
1352 // can be folded, give it a low spill weight.
1353 if (NewVReg && TrySplit && AllCanFold) {
1354 LiveInterval &nI = getOrCreateInterval(NewVReg);
1361 bool HasDef = false;
1362 bool HasUse = false;
1363 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
1364 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1365 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1366 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
1367 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
1368 if (!HasDef && !HasUse)
1371 AllCanFold &= CanFold;
1373 // Update weight of spill interval.
1374 LiveInterval &nI = getOrCreateInterval(NewVReg);
1376 // The spill weight is now infinity as it cannot be spilled again.
1377 nI.weight = HUGE_VALF;
1381 // Keep track of the last def and first use in each MBB.
1383 if (MI != ReMatOrigDefMI || !CanDelete) {
1384 bool HasKill = false;
1386 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, index.getDefIndex());
1388 // If this is a two-address code, then this index starts a new VNInfo.
1389 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(index.getDefIndex());
1391 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, index.getDefIndex());
1393 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
1394 SpillIdxes.find(MBBId);
1396 if (SII == SpillIdxes.end()) {
1397 std::vector<SRInfo> S;
1398 S.push_back(SRInfo(index, NewVReg, true));
1399 SpillIdxes.insert(std::make_pair(MBBId, S));
1400 } else if (SII->second.back().vreg != NewVReg) {
1401 SII->second.push_back(SRInfo(index, NewVReg, true));
1402 } else if (index > SII->second.back().index) {
1403 // If there is an earlier def and this is a two-address
1404 // instruction, then it's not possible to fold the store (which
1405 // would also fold the load).
1406 SRInfo &Info = SII->second.back();
1408 Info.canFold = !HasUse;
1410 SpillMBBs.set(MBBId);
1411 } else if (SII != SpillIdxes.end() &&
1412 SII->second.back().vreg == NewVReg &&
1413 index > SII->second.back().index) {
1414 // There is an earlier def that's not killed (must be two-address).
1415 // The spill is no longer needed.
1416 SII->second.pop_back();
1417 if (SII->second.empty()) {
1418 SpillIdxes.erase(MBBId);
1419 SpillMBBs.reset(MBBId);
1426 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
1427 SpillIdxes.find(MBBId);
1428 if (SII != SpillIdxes.end() &&
1429 SII->second.back().vreg == NewVReg &&
1430 index > SII->second.back().index)
1431 // Use(s) following the last def, it's not safe to fold the spill.
1432 SII->second.back().canFold = false;
1433 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
1434 RestoreIdxes.find(MBBId);
1435 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
1436 // If we are splitting live intervals, only fold if it's the first
1437 // use and there isn't another use later in the MBB.
1438 RII->second.back().canFold = false;
1440 // Only need a reload if there isn't an earlier def / use.
1441 if (RII == RestoreIdxes.end()) {
1442 std::vector<SRInfo> Infos;
1443 Infos.push_back(SRInfo(index, NewVReg, true));
1444 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1446 RII->second.push_back(SRInfo(index, NewVReg, true));
1448 RestoreMBBs.set(MBBId);
1452 // Update spill weight.
1453 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
1454 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
1457 if (NewVReg && TrySplit && AllCanFold) {
1458 // If all of its def / use can be folded, give it a low spill weight.
1459 LiveInterval &nI = getOrCreateInterval(NewVReg);
1464 bool LiveIntervals::alsoFoldARestore(int Id, SlotIndex index,
1465 unsigned vr, BitVector &RestoreMBBs,
1466 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1467 if (!RestoreMBBs[Id])
1469 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1470 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1471 if (Restores[i].index == index &&
1472 Restores[i].vreg == vr &&
1473 Restores[i].canFold)
1478 void LiveIntervals::eraseRestoreInfo(int Id, SlotIndex index,
1479 unsigned vr, BitVector &RestoreMBBs,
1480 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1481 if (!RestoreMBBs[Id])
1483 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1484 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1485 if (Restores[i].index == index && Restores[i].vreg)
1486 Restores[i].index = SlotIndex();
1489 /// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1490 /// spilled and create empty intervals for their uses.
1492 LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1493 const TargetRegisterClass* rc,
1494 std::vector<LiveInterval*> &NewLIs) {
1495 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1496 re = mri_->reg_end(); ri != re; ) {
1497 MachineOperand &O = ri.getOperand();
1498 MachineInstr *MI = &*ri;
1501 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1502 "Register def was not rewritten?");
1503 RemoveMachineInstrFromMaps(MI);
1504 vrm.RemoveMachineInstrFromMaps(MI);
1505 MI->eraseFromParent();
1507 // This must be an use of an implicit_def so it's not part of the live
1508 // interval. Create a new empty live interval for it.
1509 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1510 unsigned NewVReg = mri_->createVirtualRegister(rc);
1512 vrm.setIsImplicitlyDefined(NewVReg);
1513 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1514 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1515 MachineOperand &MO = MI->getOperand(i);
1516 if (MO.isReg() && MO.getReg() == li.reg) {
1525 std::vector<LiveInterval*> LiveIntervals::
1526 addIntervalsForSpillsFast(const LiveInterval &li,
1527 const MachineLoopInfo *loopInfo,
1529 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
1531 std::vector<LiveInterval*> added;
1533 assert(li.weight != HUGE_VALF &&
1534 "attempt to spill already spilled interval!");
1537 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1542 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1544 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1545 while (RI != mri_->reg_end()) {
1546 MachineInstr* MI = &*RI;
1548 SmallVector<unsigned, 2> Indices;
1549 bool HasUse = false;
1550 bool HasDef = false;
1552 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1553 MachineOperand& mop = MI->getOperand(i);
1554 if (!mop.isReg() || mop.getReg() != li.reg) continue;
1556 HasUse |= MI->getOperand(i).isUse();
1557 HasDef |= MI->getOperand(i).isDef();
1559 Indices.push_back(i);
1562 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
1563 Indices, true, slot, li.reg)) {
1564 unsigned NewVReg = mri_->createVirtualRegister(rc);
1566 vrm.assignVirt2StackSlot(NewVReg, slot);
1568 // create a new register for this spill
1569 LiveInterval &nI = getOrCreateInterval(NewVReg);
1571 // the spill weight is now infinity as it
1572 // cannot be spilled again
1573 nI.weight = HUGE_VALF;
1575 // Rewrite register operands to use the new vreg.
1576 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
1577 E = Indices.end(); I != E; ++I) {
1578 MI->getOperand(*I).setReg(NewVReg);
1580 if (MI->getOperand(*I).isUse())
1581 MI->getOperand(*I).setIsKill(true);
1584 // Fill in the new live interval.
1585 SlotIndex index = getInstructionIndex(MI);
1587 LiveRange LR(index.getLoadIndex(), index.getUseIndex(),
1588 nI.getNextValue(SlotIndex(), 0, false,
1589 getVNInfoAllocator()));
1590 DEBUG(errs() << " +" << LR);
1592 vrm.addRestorePoint(NewVReg, MI);
1595 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1596 nI.getNextValue(SlotIndex(), 0, false,
1597 getVNInfoAllocator()));
1598 DEBUG(errs() << " +" << LR);
1600 vrm.addSpillPoint(NewVReg, true, MI);
1603 added.push_back(&nI);
1606 errs() << "\t\t\t\tadded new interval: ";
1613 RI = mri_->reg_begin(li.reg);
1619 std::vector<LiveInterval*> LiveIntervals::
1620 addIntervalsForSpills(const LiveInterval &li,
1621 SmallVectorImpl<LiveInterval*> &SpillIs,
1622 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
1624 if (EnableFastSpilling)
1625 return addIntervalsForSpillsFast(li, loopInfo, vrm);
1627 assert(li.weight != HUGE_VALF &&
1628 "attempt to spill already spilled interval!");
1631 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1632 li.print(errs(), tri_);
1636 // Each bit specify whether a spill is required in the MBB.
1637 BitVector SpillMBBs(mf_->getNumBlockIDs());
1638 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
1639 BitVector RestoreMBBs(mf_->getNumBlockIDs());
1640 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
1641 DenseMap<unsigned,unsigned> MBBVRegsMap;
1642 std::vector<LiveInterval*> NewLIs;
1643 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1645 unsigned NumValNums = li.getNumValNums();
1646 SmallVector<MachineInstr*, 4> ReMatDefs;
1647 ReMatDefs.resize(NumValNums, NULL);
1648 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1649 ReMatOrigDefs.resize(NumValNums, NULL);
1650 SmallVector<int, 4> ReMatIds;
1651 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1652 BitVector ReMatDelete(NumValNums);
1653 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1655 // Spilling a split live interval. It cannot be split any further. Also,
1656 // it's also guaranteed to be a single val# / range interval.
1657 if (vrm.getPreSplitReg(li.reg)) {
1658 vrm.setIsSplitFromReg(li.reg, 0);
1659 // Unset the split kill marker on the last use.
1660 SlotIndex KillIdx = vrm.getKillPoint(li.reg);
1661 if (KillIdx != SlotIndex()) {
1662 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1663 assert(KillMI && "Last use disappeared?");
1664 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1665 assert(KillOp != -1 && "Last use disappeared?");
1666 KillMI->getOperand(KillOp).setIsKill(false);
1668 vrm.removeKillPoint(li.reg);
1669 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1670 Slot = vrm.getStackSlot(li.reg);
1671 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1672 MachineInstr *ReMatDefMI = DefIsReMat ?
1673 vrm.getReMaterializedMI(li.reg) : NULL;
1675 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1676 bool isLoad = isLoadSS ||
1677 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
1678 bool IsFirstRange = true;
1679 for (LiveInterval::Ranges::const_iterator
1680 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1681 // If this is a split live interval with multiple ranges, it means there
1682 // are two-address instructions that re-defined the value. Only the
1683 // first def can be rematerialized!
1685 // Note ReMatOrigDefMI has already been deleted.
1686 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1687 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1688 false, vrm, rc, ReMatIds, loopInfo,
1689 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
1690 MBBVRegsMap, NewLIs);
1692 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1693 Slot, 0, false, false, false,
1694 false, vrm, rc, ReMatIds, loopInfo,
1695 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
1696 MBBVRegsMap, NewLIs);
1698 IsFirstRange = false;
1701 handleSpilledImpDefs(li, vrm, rc, NewLIs);
1705 bool TrySplit = !intervalIsInOneMBB(li);
1708 bool NeedStackSlot = false;
1709 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1711 const VNInfo *VNI = *i;
1712 unsigned VN = VNI->id;
1713 if (VNI->isUnused())
1714 continue; // Dead val#.
1715 // Is the def for the val# rematerializable?
1716 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
1717 ? getInstructionFromIndex(VNI->def) : 0;
1719 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
1720 // Remember how to remat the def of this val#.
1721 ReMatOrigDefs[VN] = ReMatDefMI;
1722 // Original def may be modified so we have to make a copy here.
1723 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
1724 CloneMIs.push_back(Clone);
1725 ReMatDefs[VN] = Clone;
1727 bool CanDelete = true;
1728 if (VNI->hasPHIKill()) {
1729 // A kill is a phi node, not all of its uses can be rematerialized.
1730 // It must not be deleted.
1732 // Need a stack slot if there is any live range where uses cannot be
1734 NeedStackSlot = true;
1737 ReMatDelete.set(VN);
1739 // Need a stack slot if there is any live range where uses cannot be
1741 NeedStackSlot = true;
1745 // One stack slot per live interval.
1746 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
1747 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
1748 Slot = vrm.assignVirt2StackSlot(li.reg);
1750 // This case only occurs when the prealloc splitter has already assigned
1751 // a stack slot to this vreg.
1753 Slot = vrm.getStackSlot(li.reg);
1756 // Create new intervals and rewrite defs and uses.
1757 for (LiveInterval::Ranges::const_iterator
1758 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1759 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1760 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1761 bool DefIsReMat = ReMatDefMI != NULL;
1762 bool CanDelete = ReMatDelete[I->valno->id];
1764 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1765 bool isLoad = isLoadSS ||
1766 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
1767 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
1768 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1769 CanDelete, vrm, rc, ReMatIds, loopInfo,
1770 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
1771 MBBVRegsMap, NewLIs);
1774 // Insert spills / restores if we are splitting.
1776 handleSpilledImpDefs(li, vrm, rc, NewLIs);
1780 SmallPtrSet<LiveInterval*, 4> AddedKill;
1781 SmallVector<unsigned, 2> Ops;
1782 if (NeedStackSlot) {
1783 int Id = SpillMBBs.find_first();
1785 std::vector<SRInfo> &spills = SpillIdxes[Id];
1786 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1787 SlotIndex index = spills[i].index;
1788 unsigned VReg = spills[i].vreg;
1789 LiveInterval &nI = getOrCreateInterval(VReg);
1790 bool isReMat = vrm.isReMaterialized(VReg);
1791 MachineInstr *MI = getInstructionFromIndex(index);
1792 bool CanFold = false;
1793 bool FoundUse = false;
1795 if (spills[i].canFold) {
1797 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1798 MachineOperand &MO = MI->getOperand(j);
1799 if (!MO.isReg() || MO.getReg() != VReg)
1806 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1807 RestoreMBBs, RestoreIdxes))) {
1808 // MI has two-address uses of the same register. If the use
1809 // isn't the first and only use in the BB, then we can't fold
1810 // it. FIXME: Move this to rewriteInstructionsForSpills.
1817 // Fold the store into the def if possible.
1818 bool Folded = false;
1819 if (CanFold && !Ops.empty()) {
1820 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
1823 // Also folded uses, do not issue a load.
1824 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
1825 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
1827 nI.removeRange(index.getDefIndex(), index.getStoreIndex());
1831 // Otherwise tell the spiller to issue a spill.
1833 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1834 bool isKill = LR->end == index.getStoreIndex();
1835 if (!MI->registerDefIsDead(nI.reg))
1836 // No need to spill a dead def.
1837 vrm.addSpillPoint(VReg, isKill, MI);
1839 AddedKill.insert(&nI);
1842 Id = SpillMBBs.find_next(Id);
1846 int Id = RestoreMBBs.find_first();
1848 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1849 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1850 SlotIndex index = restores[i].index;
1851 if (index == SlotIndex())
1853 unsigned VReg = restores[i].vreg;
1854 LiveInterval &nI = getOrCreateInterval(VReg);
1855 bool isReMat = vrm.isReMaterialized(VReg);
1856 MachineInstr *MI = getInstructionFromIndex(index);
1857 bool CanFold = false;
1859 if (restores[i].canFold) {
1861 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1862 MachineOperand &MO = MI->getOperand(j);
1863 if (!MO.isReg() || MO.getReg() != VReg)
1867 // If this restore were to be folded, it would have been folded
1876 // Fold the load into the use if possible.
1877 bool Folded = false;
1878 if (CanFold && !Ops.empty()) {
1880 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1882 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1884 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1885 // If the rematerializable def is a load, also try to fold it.
1886 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
1887 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1888 Ops, isLoadSS, LdSlot, VReg);
1890 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1892 // Re-matting an instruction with virtual register use. Add the
1893 // register as an implicit use on the use MI and update the register
1894 // interval's spill weight to HUGE_VALF to prevent it from being
1896 LiveInterval &ImpLi = getInterval(ImpUse);
1897 ImpLi.weight = HUGE_VALF;
1898 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1903 // If folding is not possible / failed, then tell the spiller to issue a
1904 // load / rematerialization for us.
1906 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
1908 vrm.addRestorePoint(VReg, MI);
1910 Id = RestoreMBBs.find_next(Id);
1913 // Finalize intervals: add kills, finalize spill weights, and filter out
1915 std::vector<LiveInterval*> RetNewLIs;
1916 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1917 LiveInterval *LI = NewLIs[i];
1919 LI->weight /= SlotIndex::NUM * getApproximateInstructionCount(*LI);
1920 if (!AddedKill.count(LI)) {
1921 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
1922 SlotIndex LastUseIdx = LR->end.getBaseIndex();
1923 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
1924 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
1925 assert(UseIdx != -1);
1926 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
1927 LastUse->getOperand(UseIdx).setIsKill();
1928 vrm.addKillPoint(LI->reg, LastUseIdx);
1931 RetNewLIs.push_back(LI);
1935 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
1939 /// hasAllocatableSuperReg - Return true if the specified physical register has
1940 /// any super register that's allocatable.
1941 bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1942 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1943 if (allocatableRegs_[*AS] && hasInterval(*AS))
1948 /// getRepresentativeReg - Find the largest super register of the specified
1949 /// physical register.
1950 unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1951 // Find the largest super-register that is allocatable.
1952 unsigned BestReg = Reg;
1953 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1954 unsigned SuperReg = *AS;
1955 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1963 /// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1964 /// specified interval that conflicts with the specified physical register.
1965 unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1966 unsigned PhysReg) const {
1967 unsigned NumConflicts = 0;
1968 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1969 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1970 E = mri_->reg_end(); I != E; ++I) {
1971 MachineOperand &O = I.getOperand();
1972 MachineInstr *MI = O.getParent();
1973 SlotIndex Index = getInstructionIndex(MI);
1974 if (pli.liveAt(Index))
1977 return NumConflicts;
1980 /// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1981 /// around all defs and uses of the specified interval. Return true if it
1982 /// was able to cut its interval.
1983 bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1984 unsigned PhysReg, VirtRegMap &vrm) {
1985 unsigned SpillReg = getRepresentativeReg(PhysReg);
1987 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1988 // If there are registers which alias PhysReg, but which are not a
1989 // sub-register of the chosen representative super register. Assert
1990 // since we can't handle it yet.
1991 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
1992 tri_->isSuperRegister(*AS, SpillReg));
1995 SmallVector<unsigned, 4> PRegs;
1996 if (hasInterval(SpillReg))
1997 PRegs.push_back(SpillReg);
1999 SmallSet<unsigned, 4> Added;
2000 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS)
2001 if (Added.insert(*AS) && hasInterval(*AS)) {
2002 PRegs.push_back(*AS);
2003 for (const unsigned* ASS = tri_->getSubRegisters(*AS); *ASS; ++ASS)
2008 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2009 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2010 E = mri_->reg_end(); I != E; ++I) {
2011 MachineOperand &O = I.getOperand();
2012 MachineInstr *MI = O.getParent();
2013 if (SeenMIs.count(MI))
2016 SlotIndex Index = getInstructionIndex(MI);
2017 for (unsigned i = 0, e = PRegs.size(); i != e; ++i) {
2018 unsigned PReg = PRegs[i];
2019 LiveInterval &pli = getInterval(PReg);
2020 if (!pli.liveAt(Index))
2022 vrm.addEmergencySpill(PReg, MI);
2023 SlotIndex StartIdx = Index.getLoadIndex();
2024 SlotIndex EndIdx = Index.getNextIndex().getBaseIndex();
2025 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
2026 pli.removeRange(StartIdx, EndIdx);
2030 raw_string_ostream Msg(msg);
2031 Msg << "Ran out of registers during register allocation!";
2032 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
2033 Msg << "\nPlease check your inline asm statement for invalid "
2034 << "constraints:\n";
2035 MI->print(Msg, tm_);
2037 llvm_report_error(Msg.str());
2039 for (const unsigned* AS = tri_->getSubRegisters(PReg); *AS; ++AS) {
2040 if (!hasInterval(*AS))
2042 LiveInterval &spli = getInterval(*AS);
2043 if (spli.liveAt(Index))
2044 spli.removeRange(Index.getLoadIndex(),
2045 Index.getNextIndex().getBaseIndex());
2052 LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
2053 MachineInstr* startInst) {
2054 LiveInterval& Interval = getOrCreateInterval(reg);
2055 VNInfo* VN = Interval.getNextValue(
2056 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
2057 startInst, true, getVNInfoAllocator());
2058 VN->setHasPHIKill(true);
2059 VN->kills.push_back(indexes_->getTerminatorGap(startInst->getParent()));
2061 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
2062 getMBBEndIdx(startInst->getParent()).getNextIndex().getBaseIndex(), VN);
2063 Interval.addRange(LR);