blob: dede490d91bac4aeb61e915fdfbe106d6ff2c82b [file] [log] [blame]
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +00001//===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===//
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// Implementation of the LiveRangeCalc class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "regalloc"
15#include "LiveRangeCalc.h"
16#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000018
19using namespace llvm;
20
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +000021void LiveRangeCalc::reset(const MachineFunction *mf,
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000022 SlotIndexes *SI,
23 MachineDominatorTree *MDT,
24 VNInfo::Allocator *VNIA) {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +000025 MF = mf;
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000026 MRI = &MF->getRegInfo();
27 Indexes = SI;
28 DomTree = MDT;
29 Alloc = VNIA;
30
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000031 unsigned N = MF->getNumBlockIDs();
32 Seen.clear();
33 Seen.resize(N);
34 LiveOut.resize(N);
35 LiveIn.clear();
36}
37
38
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000039void LiveRangeCalc::createDeadDefs(LiveInterval *LI, unsigned Reg) {
40 assert(MRI && Indexes && "call reset() first");
41
42 // Visit all def operands. If the same instruction has multiple defs of Reg,
43 // LI->createDeadDef() will deduplicate.
44 for (MachineRegisterInfo::def_iterator
45 I = MRI->def_begin(Reg), E = MRI->def_end(); I != E; ++I) {
46 const MachineInstr *MI = &*I;
47 // Find the corresponding slot index.
48 SlotIndex Idx;
49 if (MI->isPHI())
50 // PHI defs begin at the basic block start index.
51 Idx = Indexes->getMBBStartIdx(MI->getParent());
52 else
53 // Instructions are either normal 'r', or early clobber 'e'.
54 Idx = Indexes->getInstructionIndex(MI)
55 .getRegSlot(I.getOperand().isEarlyClobber());
56
57 // Create the def in LI. This may find an existing def.
Jakob Stoklund Olesenb18d7792012-07-27 21:11:14 +000058 LI->createDeadDef(Idx, *Alloc);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000059 }
60}
61
62
63void LiveRangeCalc::extendToUses(LiveInterval *LI, unsigned Reg) {
64 assert(MRI && Indexes && "call reset() first");
65
66 // Visit all operands that read Reg. This may include partial defs.
67 for (MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(Reg),
68 E = MRI->reg_nodbg_end(); I != E; ++I) {
Jakob Stoklund Olesenf9dff0e2012-09-06 18:15:15 +000069 MachineOperand &MO = I.getOperand();
70 // Clear all kill flags. They will be reinserted after register allocation
71 // by LiveIntervalAnalysis::addKillFlags().
72 if (MO.isUse())
73 MO.setIsKill(false);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000074 if (!MO.readsReg())
75 continue;
76 // MI is reading Reg. We may have visited MI before if it happens to be
77 // reading Reg multiple times. That is OK, extend() is idempotent.
78 const MachineInstr *MI = &*I;
79
80 // Find the SlotIndex being read.
81 SlotIndex Idx;
82 if (MI->isPHI()) {
83 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
84 // PHI operands are paired: (Reg, PredMBB).
85 // Extend the live range to be live-out from PredMBB.
86 Idx = Indexes->getMBBEndIdx(MI->getOperand(I.getOperandNo()+1).getMBB());
87 } else {
88 // This is a normal instruction.
89 Idx = Indexes->getInstructionIndex(MI).getRegSlot();
90 // Check for early-clobber redefs.
91 unsigned DefIdx;
92 if (MO.isDef()) {
93 if (MO.isEarlyClobber())
94 Idx = Idx.getRegSlot(true);
95 } else if (MI->isRegTiedToDefOperand(I.getOperandNo(), &DefIdx)) {
96 // FIXME: This would be a lot easier if tied early-clobber uses also
97 // had an early-clobber flag.
98 if (MI->getOperand(DefIdx).isEarlyClobber())
99 Idx = Idx.getRegSlot(true);
100 }
101 }
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000102 extend(LI, Idx, Reg);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000103 }
104}
105
106
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000107// Transfer information from the LiveIn vector to the live ranges.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000108void LiveRangeCalc::updateLiveIns() {
109 LiveRangeUpdater Updater;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000110 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
111 E = LiveIn.end(); I != E; ++I) {
112 if (!I->DomNode)
113 continue;
114 MachineBasicBlock *MBB = I->DomNode->getBlock();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000115 assert(I->Value && "No live-in value found");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000116 SlotIndex Start, End;
117 tie(Start, End) = Indexes->getMBBRange(MBB);
118
119 if (I->Kill.isValid())
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000120 // Value is killed inside this block.
121 End = I->Kill;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000122 else {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000123 // The value is live-through, update LiveOut as well.
124 // Defer the Domtree lookup until it is needed.
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000125 assert(Seen.test(MBB->getNumber()));
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000126 LiveOut[MBB] = LiveOutPair(I->Value, (MachineDomTreeNode *)0);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000127 }
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000128 Updater.setDest(I->LI);
129 Updater.add(Start, End, I->Value);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000130 }
131 LiveIn.clear();
132}
133
134
135void LiveRangeCalc::extend(LiveInterval *LI,
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000136 SlotIndex Kill,
137 unsigned PhysReg) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000138 assert(LI && "Missing live range");
139 assert(Kill.isValid() && "Invalid SlotIndex");
140 assert(Indexes && "Missing SlotIndexes");
141 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000142
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000143 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot());
Lang Hamesaa134822011-12-20 20:23:40 +0000144 assert(KillMBB && "No MBB at Kill");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000145
146 // Is there a def in the same MBB we can extend?
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000147 if (LI->extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill))
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000148 return;
149
150 // Find the single reaching def, or determine if Kill is jointly dominated by
151 // multiple values, and we may need to create even more phi-defs to preserve
152 // VNInfo SSA form. Perform a search for all predecessor blocks where we
153 // know the dominating VNInfo.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000154 if (findReachingDefs(LI, KillMBB, Kill, PhysReg))
155 return;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000156
157 // When there were multiple different values, we may need new PHIs.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000158 calculateValues();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000159}
160
161
162// This function is called by a client after using the low-level API to add
163// live-out and live-in blocks. The unique value optimization is not
164// available, SplitEditor::transferValues handles that case directly anyway.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000165void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000166 assert(Indexes && "Missing SlotIndexes");
167 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000168 updateSSA();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000169 updateLiveIns();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000170}
171
172
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000173bool LiveRangeCalc::findReachingDefs(LiveInterval *LI,
174 MachineBasicBlock *KillMBB,
175 SlotIndex Kill,
176 unsigned PhysReg) {
177 unsigned KillMBBNum = KillMBB->getNumber();
178
179 // Block numbers where LI should be live-in.
180 SmallVector<unsigned, 16> WorkList(1, KillMBBNum);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000181
182 // Remember if we have seen more than one value.
183 bool UniqueVNI = true;
184 VNInfo *TheVNI = 0;
185
186 // Using Seen as a visited set, perform a BFS for all reaching defs.
187 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000188 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000189
190#ifndef NDEBUG
191 if (MBB->pred_empty()) {
192 MBB->getParent()->verify();
193 llvm_unreachable("Use not jointly dominated by defs.");
194 }
195
196 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
197 !MBB->isLiveIn(PhysReg)) {
198 MBB->getParent()->verify();
199 errs() << "The register needs to be live in to BB#" << MBB->getNumber()
200 << ", but is missing from the live-in list.\n";
201 llvm_unreachable("Invalid global physical register");
202 }
203#endif
204
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000205 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
206 PE = MBB->pred_end(); PI != PE; ++PI) {
207 MachineBasicBlock *Pred = *PI;
208
209 // Is this a known live-out block?
210 if (Seen.test(Pred->getNumber())) {
211 if (VNInfo *VNI = LiveOut[Pred].first) {
212 if (TheVNI && TheVNI != VNI)
213 UniqueVNI = false;
214 TheVNI = VNI;
215 }
216 continue;
217 }
218
219 SlotIndex Start, End;
220 tie(Start, End) = Indexes->getMBBRange(Pred);
221
222 // First time we see Pred. Try to determine the live-out value, but set
223 // it as null if Pred is live-through with an unknown value.
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000224 VNInfo *VNI = LI->extendInBlock(Start, End);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000225 setLiveOutValue(Pred, VNI);
226 if (VNI) {
227 if (TheVNI && TheVNI != VNI)
228 UniqueVNI = false;
229 TheVNI = VNI;
230 continue;
231 }
232
233 // No, we need a live-in value for Pred as well
234 if (Pred != KillMBB)
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000235 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000236 else
237 // Loopback to KillMBB, so value is really live through.
238 Kill = SlotIndex();
239 }
240 }
241
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000242 LiveIn.clear();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000243
244 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
245 // neither require it. Skip the sorting overhead for small updates.
246 if (WorkList.size() > 4)
247 array_pod_sort(WorkList.begin(), WorkList.end());
248
249 // If a unique reaching def was found, blit in the live ranges immediately.
250 if (UniqueVNI) {
251 LiveRangeUpdater Updater(LI);
252 for (SmallVectorImpl<unsigned>::const_iterator
253 I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
254 SlotIndex Start, End;
255 tie(Start, End) = Indexes->getMBBRange(*I);
256 // Trim the live range in KillMBB.
257 if (*I == KillMBBNum && Kill.isValid())
258 End = Kill;
259 else
260 LiveOut[MF->getBlockNumbered(*I)] =
261 LiveOutPair(TheVNI, (MachineDomTreeNode *)0);
262 Updater.add(Start, End, TheVNI);
263 }
264 return true;
265 }
266
267 // Multiple values were found, so transfer the work list to the LiveIn array
268 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000269 LiveIn.reserve(WorkList.size());
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000270 for (SmallVectorImpl<unsigned>::const_iterator
271 I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
272 MachineBasicBlock *MBB = MF->getBlockNumbered(*I);
273 addLiveInBlock(LI, DomTree->getNode(MBB));
274 if (MBB == KillMBB)
275 LiveIn.back().Kill = Kill;
276 }
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000277
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000278 return false;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000279}
280
281
282// This is essentially the same iterative algorithm that SSAUpdater uses,
283// except we already have a dominator tree, so we don't have to recompute it.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000284void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000285 assert(Indexes && "Missing SlotIndexes");
286 assert(DomTree && "Missing dominator tree");
287
288 // Interate until convergence.
289 unsigned Changes;
290 do {
291 Changes = 0;
292 // Propagate live-out values down the dominator tree, inserting phi-defs
293 // when necessary.
294 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
295 E = LiveIn.end(); I != E; ++I) {
296 MachineDomTreeNode *Node = I->DomNode;
297 // Skip block if the live-in value has already been determined.
298 if (!Node)
299 continue;
300 MachineBasicBlock *MBB = Node->getBlock();
301 MachineDomTreeNode *IDom = Node->getIDom();
302 LiveOutPair IDomValue;
303
304 // We need a live-in value to a block with no immediate dominator?
305 // This is probably an unreachable block that has survived somehow.
306 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
307
308 // IDom dominates all of our predecessors, but it may not be their
309 // immediate dominator. Check if any of them have live-out values that are
310 // properly dominated by IDom. If so, we need a phi-def here.
311 if (!needPHI) {
312 IDomValue = LiveOut[IDom->getBlock()];
313
314 // Cache the DomTree node that defined the value.
315 if (IDomValue.first && !IDomValue.second)
316 LiveOut[IDom->getBlock()].second = IDomValue.second =
317 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
318
319 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
320 PE = MBB->pred_end(); PI != PE; ++PI) {
321 LiveOutPair &Value = LiveOut[*PI];
322 if (!Value.first || Value.first == IDomValue.first)
323 continue;
324
325 // Cache the DomTree node that defined the value.
326 if (!Value.second)
327 Value.second =
328 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
329
330 // This predecessor is carrying something other than IDomValue.
331 // It could be because IDomValue hasn't propagated yet, or it could be
332 // because MBB is in the dominance frontier of that value.
333 if (DomTree->dominates(IDom, Value.second)) {
334 needPHI = true;
335 break;
336 }
337 }
338 }
339
340 // The value may be live-through even if Kill is set, as can happen when
341 // we are called from extendRange. In that case LiveOutSeen is true, and
342 // LiveOut indicates a foreign or missing value.
343 LiveOutPair &LOP = LiveOut[MBB];
344
345 // Create a phi-def if required.
346 if (needPHI) {
347 ++Changes;
348 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
349 SlotIndex Start, End;
350 tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000351 VNInfo *VNI = I->LI->getNextValue(Start, *Alloc);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000352 I->Value = VNI;
353 // This block is done, we know the final value.
354 I->DomNode = 0;
355
356 // Add liveness since updateLiveIns now skips this node.
357 if (I->Kill.isValid())
358 I->LI->addRange(LiveRange(Start, I->Kill, VNI));
359 else {
360 I->LI->addRange(LiveRange(Start, End, VNI));
361 LOP = LiveOutPair(VNI, Node);
362 }
363 } else if (IDomValue.first) {
364 // No phi-def here. Remember incoming value.
365 I->Value = IDomValue.first;
366
367 // If the IDomValue is killed in the block, don't propagate through.
368 if (I->Kill.isValid())
369 continue;
370
371 // Propagate IDomValue if it isn't killed:
372 // MBB is live-out and doesn't define its own value.
373 if (LOP.first == IDomValue.first)
374 continue;
375 ++Changes;
376 LOP = IDomValue;
377 }
378 }
379 } while (Changes);
380}