1 //===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===//
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 // Implementation of the LiveRangeCalc class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "regalloc"
15 #include "LiveRangeCalc.h"
16 #include "llvm/CodeGen/MachineDominators.h"
20 void LiveRangeCalc::reset(const MachineFunction *MF) {
21 unsigned N = MF->getNumBlockIDs();
29 // Transfer information from the LiveIn vector to the live ranges.
30 void LiveRangeCalc::updateLiveIns(VNInfo *OverrideVNI, SlotIndexes *Indexes) {
31 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
32 E = LiveIn.end(); I != E; ++I) {
35 MachineBasicBlock *MBB = I->DomNode->getBlock();
37 VNInfo *VNI = OverrideVNI ? OverrideVNI : I->Value;
38 assert(VNI && "No live-in value found");
41 tie(Start, End) = Indexes->getMBBRange(MBB);
43 if (I->Kill.isValid())
44 I->LI->addRange(LiveRange(Start, I->Kill, VNI));
46 I->LI->addRange(LiveRange(Start, End, VNI));
47 // The value is live-through, update LiveOut as well. Defer the Domtree
48 // lookup until it is needed.
49 assert(Seen.test(MBB->getNumber()));
50 LiveOut[MBB] = LiveOutPair(VNI, (MachineDomTreeNode *)0);
57 void LiveRangeCalc::extend(LiveInterval *LI,
60 MachineDominatorTree *DomTree,
61 VNInfo::Allocator *Alloc) {
62 assert(LI && "Missing live range");
63 assert(Kill.isValid() && "Invalid SlotIndex");
64 assert(Indexes && "Missing SlotIndexes");
65 assert(DomTree && "Missing dominator tree");
66 SlotIndex LastUse = Kill.getPrevSlot();
68 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(LastUse);
69 assert(Kill && "No MBB at Kill");
71 // Is there a def in the same MBB we can extend?
72 if (LI->extendInBlock(Indexes->getMBBStartIdx(KillMBB), LastUse))
75 // Find the single reaching def, or determine if Kill is jointly dominated by
76 // multiple values, and we may need to create even more phi-defs to preserve
77 // VNInfo SSA form. Perform a search for all predecessor blocks where we
78 // know the dominating VNInfo.
79 VNInfo *VNI = findReachingDefs(LI, KillMBB, Kill, Indexes, DomTree);
81 // When there were multiple different values, we may need new PHIs.
83 updateSSA(Indexes, DomTree, Alloc);
85 updateLiveIns(VNI, Indexes);
89 // This function is called by a client after using the low-level API to add
90 // live-out and live-in blocks. The unique value optimization is not
91 // available, SplitEditor::transferValues handles that case directly anyway.
92 void LiveRangeCalc::calculateValues(SlotIndexes *Indexes,
93 MachineDominatorTree *DomTree,
94 VNInfo::Allocator *Alloc) {
95 assert(Indexes && "Missing SlotIndexes");
96 assert(DomTree && "Missing dominator tree");
97 updateSSA(Indexes, DomTree, Alloc);
98 updateLiveIns(0, Indexes);
102 VNInfo *LiveRangeCalc::findReachingDefs(LiveInterval *LI,
103 MachineBasicBlock *KillMBB,
105 SlotIndexes *Indexes,
106 MachineDominatorTree *DomTree) {
107 // Blocks where LI should be live-in.
108 SmallVector<MachineBasicBlock*, 16> WorkList(1, KillMBB);
110 // Remember if we have seen more than one value.
111 bool UniqueVNI = true;
114 // Using Seen as a visited set, perform a BFS for all reaching defs.
115 for (unsigned i = 0; i != WorkList.size(); ++i) {
116 MachineBasicBlock *MBB = WorkList[i];
117 assert(!MBB->pred_empty() && "Value live-in to entry block?");
118 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
119 PE = MBB->pred_end(); PI != PE; ++PI) {
120 MachineBasicBlock *Pred = *PI;
122 // Is this a known live-out block?
123 if (Seen.test(Pred->getNumber())) {
124 if (VNInfo *VNI = LiveOut[Pred].first) {
125 if (TheVNI && TheVNI != VNI)
132 SlotIndex Start, End;
133 tie(Start, End) = Indexes->getMBBRange(Pred);
135 // First time we see Pred. Try to determine the live-out value, but set
136 // it as null if Pred is live-through with an unknown value.
137 VNInfo *VNI = LI->extendInBlock(Start, End.getPrevSlot());
138 setLiveOutValue(Pred, VNI);
140 if (TheVNI && TheVNI != VNI)
146 // No, we need a live-in value for Pred as well
148 WorkList.push_back(Pred);
150 // Loopback to KillMBB, so value is really live through.
155 // Transfer WorkList to LiveInBlocks in reverse order.
156 // This ordering works best with updateSSA().
158 LiveIn.reserve(WorkList.size());
159 while(!WorkList.empty())
160 addLiveInBlock(LI, DomTree->getNode(WorkList.pop_back_val()));
162 // The kill block may not be live-through.
163 assert(LiveIn.back().DomNode->getBlock() == KillMBB);
164 LiveIn.back().Kill = Kill;
166 return UniqueVNI ? TheVNI : 0;
170 // This is essentially the same iterative algorithm that SSAUpdater uses,
171 // except we already have a dominator tree, so we don't have to recompute it.
172 void LiveRangeCalc::updateSSA(SlotIndexes *Indexes,
173 MachineDominatorTree *DomTree,
174 VNInfo::Allocator *Alloc) {
175 assert(Indexes && "Missing SlotIndexes");
176 assert(DomTree && "Missing dominator tree");
178 // Interate until convergence.
182 // Propagate live-out values down the dominator tree, inserting phi-defs
184 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
185 E = LiveIn.end(); I != E; ++I) {
186 MachineDomTreeNode *Node = I->DomNode;
187 // Skip block if the live-in value has already been determined.
190 MachineBasicBlock *MBB = Node->getBlock();
191 MachineDomTreeNode *IDom = Node->getIDom();
192 LiveOutPair IDomValue;
194 // We need a live-in value to a block with no immediate dominator?
195 // This is probably an unreachable block that has survived somehow.
196 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
198 // IDom dominates all of our predecessors, but it may not be their
199 // immediate dominator. Check if any of them have live-out values that are
200 // properly dominated by IDom. If so, we need a phi-def here.
202 IDomValue = LiveOut[IDom->getBlock()];
204 // Cache the DomTree node that defined the value.
205 if (IDomValue.first && !IDomValue.second)
206 LiveOut[IDom->getBlock()].second = IDomValue.second =
207 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
209 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
210 PE = MBB->pred_end(); PI != PE; ++PI) {
211 LiveOutPair &Value = LiveOut[*PI];
212 if (!Value.first || Value.first == IDomValue.first)
215 // Cache the DomTree node that defined the value.
218 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
220 // This predecessor is carrying something other than IDomValue.
221 // It could be because IDomValue hasn't propagated yet, or it could be
222 // because MBB is in the dominance frontier of that value.
223 if (DomTree->dominates(IDom, Value.second)) {
230 // The value may be live-through even if Kill is set, as can happen when
231 // we are called from extendRange. In that case LiveOutSeen is true, and
232 // LiveOut indicates a foreign or missing value.
233 LiveOutPair &LOP = LiveOut[MBB];
235 // Create a phi-def if required.
238 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
239 SlotIndex Start, End;
240 tie(Start, End) = Indexes->getMBBRange(MBB);
241 VNInfo *VNI = I->LI->getNextValue(Start, 0, *Alloc);
242 VNI->setIsPHIDef(true);
244 // This block is done, we know the final value.
247 // Add liveness since updateLiveIns now skips this node.
248 if (I->Kill.isValid())
249 I->LI->addRange(LiveRange(Start, I->Kill, VNI));
251 I->LI->addRange(LiveRange(Start, End, VNI));
252 LOP = LiveOutPair(VNI, Node);
254 } else if (IDomValue.first) {
255 // No phi-def here. Remember incoming value.
256 I->Value = IDomValue.first;
258 // If the IDomValue is killed in the block, don't propagate through.
259 if (I->Kill.isValid())
262 // Propagate IDomValue if it isn't killed:
263 // MBB is live-out and doesn't define its own value.
264 if (LOP.first == IDomValue.first)