Move stack slot assignments into LiveRangeEdit.
[oota-llvm.git] / lib / CodeGen / LiveRangeEdit.cpp
1 //===--- LiveRangeEdit.cpp - Basic tools for editing a register live range --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The LiveRangeEdit class represents changes done to a virtual register when it
11 // is spilled or split.
12 //===----------------------------------------------------------------------===//
13
14 #include "LiveRangeEdit.h"
15 #include "VirtRegMap.h"
16 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18
19 using namespace llvm;
20
21 int LiveRangeEdit::assignStackSlot(VirtRegMap &vrm) {
22   int ss = vrm.getStackSlot(getReg());
23   if (ss != VirtRegMap::NO_STACK_SLOT)
24     return ss;
25   return vrm.assignVirt2StackSlot(getReg());
26 }
27
28 LiveInterval &LiveRangeEdit::create(MachineRegisterInfo &mri,
29                                     LiveIntervals &lis,
30                                     VirtRegMap &vrm) {
31   const TargetRegisterClass *RC = mri.getRegClass(parent_.reg);
32   unsigned VReg = mri.createVirtualRegister(RC);
33   vrm.grow();
34   // Immediately assign to the same stack slot as parent.
35   vrm.assignVirt2StackSlot(VReg, assignStackSlot(vrm));
36   LiveInterval &li = lis.getOrCreateInterval(VReg);
37   newRegs_.push_back(&li);
38   return li;
39 }
40
41 /// allUsesAvailableAt - Return true if all registers used by OrigMI at
42 /// OrigIdx are also available with the same value at UseIdx.
43 bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
44                                        SlotIndex OrigIdx,
45                                        SlotIndex UseIdx,
46                                        LiveIntervals &lis) {
47   OrigIdx = OrigIdx.getUseIndex();
48   UseIdx = UseIdx.getUseIndex();
49   for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
50     const MachineOperand &MO = OrigMI->getOperand(i);
51     if (!MO.isReg() || !MO.getReg() || MO.getReg() == getReg())
52       continue;
53     // Reserved registers are OK.
54     if (MO.isUndef() || !lis.hasInterval(MO.getReg()))
55       continue;
56     // We don't want to move any defs.
57     if (MO.isDef())
58       return false;
59     // We cannot depend on virtual registers in uselessRegs_.
60     for (unsigned ui = 0, ue = uselessRegs_.size(); ui != ue; ++ui)
61       if (uselessRegs_[ui]->reg == MO.getReg())
62         return false;
63
64     LiveInterval &li = lis.getInterval(MO.getReg());
65     const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
66     if (!OVNI)
67       continue;
68     if (OVNI != li.getVNInfoAt(UseIdx))
69       return false;
70   }
71   return true;
72 }
73