blob: ecd75b4ffaa3b16470fc98e0217d98c476e4cacc [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
Matthias Braune25dde52013-10-10 21:28:57 +000039void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000040 assert(MRI && Indexes && "call reset() first");
41
42 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braune25dde52013-10-10 21:28:57 +000043 // LR.createDeadDef() will deduplicate.
Stephen Hines36b56882014-04-23 16:57:46 -070044 for (MachineOperand &MO : MRI->def_operands(Reg)) {
45 const MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000046 // Find the corresponding slot index.
47 SlotIndex Idx;
48 if (MI->isPHI())
49 // PHI defs begin at the basic block start index.
50 Idx = Indexes->getMBBStartIdx(MI->getParent());
51 else
52 // Instructions are either normal 'r', or early clobber 'e'.
53 Idx = Indexes->getInstructionIndex(MI)
Stephen Hines36b56882014-04-23 16:57:46 -070054 .getRegSlot(MO.isEarlyClobber());
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000055
Matthias Braune25dde52013-10-10 21:28:57 +000056 // Create the def in LR. This may find an existing def.
57 LR.createDeadDef(Idx, *Alloc);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000058 }
59}
60
61
Matthias Braune25dde52013-10-10 21:28:57 +000062void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000063 assert(MRI && Indexes && "call reset() first");
64
65 // Visit all operands that read Reg. This may include partial defs.
Stephen Hines36b56882014-04-23 16:57:46 -070066 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
Jakob Stoklund Olesenf9dff0e2012-09-06 18:15:15 +000067 // Clear all kill flags. They will be reinserted after register allocation
68 // by LiveIntervalAnalysis::addKillFlags().
69 if (MO.isUse())
70 MO.setIsKill(false);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000071 if (!MO.readsReg())
72 continue;
73 // MI is reading Reg. We may have visited MI before if it happens to be
74 // reading Reg multiple times. That is OK, extend() is idempotent.
Stephen Hines36b56882014-04-23 16:57:46 -070075 const MachineInstr *MI = MO.getParent();
76 unsigned OpNo = (&MO - &MI->getOperand(0));
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000077
78 // Find the SlotIndex being read.
79 SlotIndex Idx;
80 if (MI->isPHI()) {
81 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
82 // PHI operands are paired: (Reg, PredMBB).
83 // Extend the live range to be live-out from PredMBB.
Stephen Hines36b56882014-04-23 16:57:46 -070084 Idx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000085 } else {
86 // This is a normal instruction.
87 Idx = Indexes->getInstructionIndex(MI).getRegSlot();
88 // Check for early-clobber redefs.
89 unsigned DefIdx;
90 if (MO.isDef()) {
91 if (MO.isEarlyClobber())
92 Idx = Idx.getRegSlot(true);
Stephen Hines36b56882014-04-23 16:57:46 -070093 } else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000094 // FIXME: This would be a lot easier if tied early-clobber uses also
95 // had an early-clobber flag.
96 if (MI->getOperand(DefIdx).isEarlyClobber())
97 Idx = Idx.getRegSlot(true);
98 }
99 }
Matthias Braune25dde52013-10-10 21:28:57 +0000100 extend(LR, Idx, Reg);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000101 }
102}
103
104
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000105// Transfer information from the LiveIn vector to the live ranges.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000106void LiveRangeCalc::updateLiveIns() {
107 LiveRangeUpdater Updater;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000108 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
109 E = LiveIn.end(); I != E; ++I) {
110 if (!I->DomNode)
111 continue;
112 MachineBasicBlock *MBB = I->DomNode->getBlock();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000113 assert(I->Value && "No live-in value found");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000114 SlotIndex Start, End;
Stephen Hines36b56882014-04-23 16:57:46 -0700115 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000116
117 if (I->Kill.isValid())
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000118 // Value is killed inside this block.
119 End = I->Kill;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000120 else {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000121 // The value is live-through, update LiveOut as well.
122 // Defer the Domtree lookup until it is needed.
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000123 assert(Seen.test(MBB->getNumber()));
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000124 LiveOut[MBB] = LiveOutPair(I->Value, (MachineDomTreeNode *)0);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000125 }
Matthias Braune25dde52013-10-10 21:28:57 +0000126 Updater.setDest(&I->LR);
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000127 Updater.add(Start, End, I->Value);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000128 }
129 LiveIn.clear();
130}
131
132
Matthias Braune25dde52013-10-10 21:28:57 +0000133void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000134 assert(Kill.isValid() && "Invalid SlotIndex");
135 assert(Indexes && "Missing SlotIndexes");
136 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000137
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000138 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot());
Lang Hamesaa134822011-12-20 20:23:40 +0000139 assert(KillMBB && "No MBB at Kill");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000140
141 // Is there a def in the same MBB we can extend?
Matthias Braune25dde52013-10-10 21:28:57 +0000142 if (LR.extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill))
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000143 return;
144
145 // Find the single reaching def, or determine if Kill is jointly dominated by
146 // multiple values, and we may need to create even more phi-defs to preserve
147 // VNInfo SSA form. Perform a search for all predecessor blocks where we
148 // know the dominating VNInfo.
Matthias Braune25dde52013-10-10 21:28:57 +0000149 if (findReachingDefs(LR, *KillMBB, Kill, PhysReg))
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000150 return;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000151
152 // When there were multiple different values, we may need new PHIs.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000153 calculateValues();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000154}
155
156
157// This function is called by a client after using the low-level API to add
158// live-out and live-in blocks. The unique value optimization is not
159// available, SplitEditor::transferValues handles that case directly anyway.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000160void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000161 assert(Indexes && "Missing SlotIndexes");
162 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000163 updateSSA();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000164 updateLiveIns();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000165}
166
167
Matthias Braune25dde52013-10-10 21:28:57 +0000168bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB,
169 SlotIndex Kill, unsigned PhysReg) {
170 unsigned KillMBBNum = KillMBB.getNumber();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000171
Matthias Braune25dde52013-10-10 21:28:57 +0000172 // Block numbers where LR should be live-in.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000173 SmallVector<unsigned, 16> WorkList(1, KillMBBNum);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000174
175 // Remember if we have seen more than one value.
176 bool UniqueVNI = true;
177 VNInfo *TheVNI = 0;
178
179 // Using Seen as a visited set, perform a BFS for all reaching defs.
180 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000181 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000182
183#ifndef NDEBUG
184 if (MBB->pred_empty()) {
185 MBB->getParent()->verify();
186 llvm_unreachable("Use not jointly dominated by defs.");
187 }
188
189 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
190 !MBB->isLiveIn(PhysReg)) {
191 MBB->getParent()->verify();
192 errs() << "The register needs to be live in to BB#" << MBB->getNumber()
193 << ", but is missing from the live-in list.\n";
194 llvm_unreachable("Invalid global physical register");
195 }
196#endif
197
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000198 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
Matthias Braune25dde52013-10-10 21:28:57 +0000199 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000200 MachineBasicBlock *Pred = *PI;
201
202 // Is this a known live-out block?
203 if (Seen.test(Pred->getNumber())) {
204 if (VNInfo *VNI = LiveOut[Pred].first) {
205 if (TheVNI && TheVNI != VNI)
206 UniqueVNI = false;
207 TheVNI = VNI;
208 }
209 continue;
210 }
211
212 SlotIndex Start, End;
Stephen Hines36b56882014-04-23 16:57:46 -0700213 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000214
215 // First time we see Pred. Try to determine the live-out value, but set
216 // it as null if Pred is live-through with an unknown value.
Matthias Braune25dde52013-10-10 21:28:57 +0000217 VNInfo *VNI = LR.extendInBlock(Start, End);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000218 setLiveOutValue(Pred, VNI);
219 if (VNI) {
220 if (TheVNI && TheVNI != VNI)
221 UniqueVNI = false;
222 TheVNI = VNI;
223 continue;
224 }
225
226 // No, we need a live-in value for Pred as well
Matthias Braune25dde52013-10-10 21:28:57 +0000227 if (Pred != &KillMBB)
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000228 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000229 else
230 // Loopback to KillMBB, so value is really live through.
231 Kill = SlotIndex();
232 }
233 }
234
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000235 LiveIn.clear();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000236
237 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
238 // neither require it. Skip the sorting overhead for small updates.
239 if (WorkList.size() > 4)
240 array_pod_sort(WorkList.begin(), WorkList.end());
241
242 // If a unique reaching def was found, blit in the live ranges immediately.
243 if (UniqueVNI) {
Matthias Braune25dde52013-10-10 21:28:57 +0000244 LiveRangeUpdater Updater(&LR);
245 for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(),
246 E = WorkList.end(); I != E; ++I) {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000247 SlotIndex Start, End;
Stephen Hines36b56882014-04-23 16:57:46 -0700248 std::tie(Start, End) = Indexes->getMBBRange(*I);
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000249 // Trim the live range in KillMBB.
250 if (*I == KillMBBNum && Kill.isValid())
251 End = Kill;
252 else
253 LiveOut[MF->getBlockNumbered(*I)] =
254 LiveOutPair(TheVNI, (MachineDomTreeNode *)0);
255 Updater.add(Start, End, TheVNI);
256 }
257 return true;
258 }
259
260 // Multiple values were found, so transfer the work list to the LiveIn array
261 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000262 LiveIn.reserve(WorkList.size());
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000263 for (SmallVectorImpl<unsigned>::const_iterator
264 I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
265 MachineBasicBlock *MBB = MF->getBlockNumbered(*I);
Matthias Braune25dde52013-10-10 21:28:57 +0000266 addLiveInBlock(LR, DomTree->getNode(MBB));
267 if (MBB == &KillMBB)
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000268 LiveIn.back().Kill = Kill;
269 }
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000270
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000271 return false;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000272}
273
274
275// This is essentially the same iterative algorithm that SSAUpdater uses,
276// except we already have a dominator tree, so we don't have to recompute it.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000277void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000278 assert(Indexes && "Missing SlotIndexes");
279 assert(DomTree && "Missing dominator tree");
280
281 // Interate until convergence.
282 unsigned Changes;
283 do {
284 Changes = 0;
285 // Propagate live-out values down the dominator tree, inserting phi-defs
286 // when necessary.
287 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
288 E = LiveIn.end(); I != E; ++I) {
289 MachineDomTreeNode *Node = I->DomNode;
290 // Skip block if the live-in value has already been determined.
291 if (!Node)
292 continue;
293 MachineBasicBlock *MBB = Node->getBlock();
294 MachineDomTreeNode *IDom = Node->getIDom();
295 LiveOutPair IDomValue;
296
297 // We need a live-in value to a block with no immediate dominator?
298 // This is probably an unreachable block that has survived somehow.
299 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
300
301 // IDom dominates all of our predecessors, but it may not be their
302 // immediate dominator. Check if any of them have live-out values that are
303 // properly dominated by IDom. If so, we need a phi-def here.
304 if (!needPHI) {
305 IDomValue = LiveOut[IDom->getBlock()];
306
307 // Cache the DomTree node that defined the value.
308 if (IDomValue.first && !IDomValue.second)
309 LiveOut[IDom->getBlock()].second = IDomValue.second =
310 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
311
312 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
313 PE = MBB->pred_end(); PI != PE; ++PI) {
314 LiveOutPair &Value = LiveOut[*PI];
315 if (!Value.first || Value.first == IDomValue.first)
316 continue;
317
318 // Cache the DomTree node that defined the value.
319 if (!Value.second)
320 Value.second =
321 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
322
323 // This predecessor is carrying something other than IDomValue.
324 // It could be because IDomValue hasn't propagated yet, or it could be
325 // because MBB is in the dominance frontier of that value.
326 if (DomTree->dominates(IDom, Value.second)) {
327 needPHI = true;
328 break;
329 }
330 }
331 }
332
333 // The value may be live-through even if Kill is set, as can happen when
334 // we are called from extendRange. In that case LiveOutSeen is true, and
335 // LiveOut indicates a foreign or missing value.
336 LiveOutPair &LOP = LiveOut[MBB];
337
338 // Create a phi-def if required.
339 if (needPHI) {
340 ++Changes;
341 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
342 SlotIndex Start, End;
Stephen Hines36b56882014-04-23 16:57:46 -0700343 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braune25dde52013-10-10 21:28:57 +0000344 LiveRange &LR = I->LR;
345 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000346 I->Value = VNI;
347 // This block is done, we know the final value.
348 I->DomNode = 0;
349
350 // Add liveness since updateLiveIns now skips this node.
351 if (I->Kill.isValid())
Matthias Braune25dde52013-10-10 21:28:57 +0000352 LR.addSegment(LiveInterval::Segment(Start, I->Kill, VNI));
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000353 else {
Matthias Braune25dde52013-10-10 21:28:57 +0000354 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000355 LOP = LiveOutPair(VNI, Node);
356 }
357 } else if (IDomValue.first) {
358 // No phi-def here. Remember incoming value.
359 I->Value = IDomValue.first;
360
361 // If the IDomValue is killed in the block, don't propagate through.
362 if (I->Kill.isValid())
363 continue;
364
365 // Propagate IDomValue if it isn't killed:
366 // MBB is live-out and doesn't define its own value.
367 if (LOP.first == IDomValue.first)
368 continue;
369 ++Changes;
370 LOP = IDomValue;
371 }
372 }
373 } while (Changes);
374}