blob: a558e142e01398488efb3a04e7132c9a88f851dc [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
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000014#include "LiveRangeCalc.h"
15#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000016#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000017
18using namespace llvm;
19
Stephen Hinesdce4a402014-05-29 02:49:00 -070020#define DEBUG_TYPE "regalloc"
21
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +000022void LiveRangeCalc::reset(const MachineFunction *mf,
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000023 SlotIndexes *SI,
24 MachineDominatorTree *MDT,
25 VNInfo::Allocator *VNIA) {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +000026 MF = mf;
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000027 MRI = &MF->getRegInfo();
28 Indexes = SI;
29 DomTree = MDT;
30 Alloc = VNIA;
31
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000032 unsigned N = MF->getNumBlockIDs();
33 Seen.clear();
34 Seen.resize(N);
35 LiveOut.resize(N);
36 LiveIn.clear();
37}
38
39
Matthias Braune25dde52013-10-10 21:28:57 +000040void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000041 assert(MRI && Indexes && "call reset() first");
42
43 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braune25dde52013-10-10 21:28:57 +000044 // LR.createDeadDef() will deduplicate.
Stephen Hines36b56882014-04-23 16:57:46 -070045 for (MachineOperand &MO : MRI->def_operands(Reg)) {
46 const MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000047 // 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)
Stephen Hines36b56882014-04-23 16:57:46 -070055 .getRegSlot(MO.isEarlyClobber());
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000056
Matthias Braune25dde52013-10-10 21:28:57 +000057 // Create the def in LR. This may find an existing def.
58 LR.createDeadDef(Idx, *Alloc);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000059 }
60}
61
62
Matthias Braune25dde52013-10-10 21:28:57 +000063void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000064 assert(MRI && Indexes && "call reset() first");
65
66 // Visit all operands that read Reg. This may include partial defs.
Stephen Hines36b56882014-04-23 16:57:46 -070067 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
Jakob Stoklund Olesenf9dff0e2012-09-06 18:15:15 +000068 // Clear all kill flags. They will be reinserted after register allocation
69 // by LiveIntervalAnalysis::addKillFlags().
70 if (MO.isUse())
71 MO.setIsKill(false);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000072 if (!MO.readsReg())
73 continue;
74 // MI is reading Reg. We may have visited MI before if it happens to be
75 // reading Reg multiple times. That is OK, extend() is idempotent.
Stephen Hines36b56882014-04-23 16:57:46 -070076 const MachineInstr *MI = MO.getParent();
77 unsigned OpNo = (&MO - &MI->getOperand(0));
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000078
79 // Find the SlotIndex being read.
80 SlotIndex Idx;
81 if (MI->isPHI()) {
82 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
83 // PHI operands are paired: (Reg, PredMBB).
84 // Extend the live range to be live-out from PredMBB.
Stephen Hines36b56882014-04-23 16:57:46 -070085 Idx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000086 } else {
87 // This is a normal instruction.
88 Idx = Indexes->getInstructionIndex(MI).getRegSlot();
89 // Check for early-clobber redefs.
90 unsigned DefIdx;
91 if (MO.isDef()) {
92 if (MO.isEarlyClobber())
93 Idx = Idx.getRegSlot(true);
Stephen Hines36b56882014-04-23 16:57:46 -070094 } else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000095 // FIXME: This would be a lot easier if tied early-clobber uses also
96 // had an early-clobber flag.
97 if (MI->getOperand(DefIdx).isEarlyClobber())
98 Idx = Idx.getRegSlot(true);
99 }
100 }
Matthias Braune25dde52013-10-10 21:28:57 +0000101 extend(LR, Idx, Reg);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000102 }
103}
104
105
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000106// Transfer information from the LiveIn vector to the live ranges.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000107void LiveRangeCalc::updateLiveIns() {
108 LiveRangeUpdater Updater;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000109 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
110 E = LiveIn.end(); I != E; ++I) {
111 if (!I->DomNode)
112 continue;
113 MachineBasicBlock *MBB = I->DomNode->getBlock();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000114 assert(I->Value && "No live-in value found");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000115 SlotIndex Start, End;
Stephen Hines36b56882014-04-23 16:57:46 -0700116 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000117
118 if (I->Kill.isValid())
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000119 // Value is killed inside this block.
120 End = I->Kill;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000121 else {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000122 // The value is live-through, update LiveOut as well.
123 // Defer the Domtree lookup until it is needed.
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000124 assert(Seen.test(MBB->getNumber()));
Stephen Hinesdce4a402014-05-29 02:49:00 -0700125 LiveOut[MBB] = LiveOutPair(I->Value, (MachineDomTreeNode *)nullptr);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000126 }
Matthias Braune25dde52013-10-10 21:28:57 +0000127 Updater.setDest(&I->LR);
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000128 Updater.add(Start, End, I->Value);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000129 }
130 LiveIn.clear();
131}
132
133
Matthias Braune25dde52013-10-10 21:28:57 +0000134void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000135 assert(Kill.isValid() && "Invalid SlotIndex");
136 assert(Indexes && "Missing SlotIndexes");
137 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000138
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000139 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot());
Lang Hamesaa134822011-12-20 20:23:40 +0000140 assert(KillMBB && "No MBB at Kill");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000141
142 // Is there a def in the same MBB we can extend?
Matthias Braune25dde52013-10-10 21:28:57 +0000143 if (LR.extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill))
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000144 return;
145
146 // Find the single reaching def, or determine if Kill is jointly dominated by
147 // multiple values, and we may need to create even more phi-defs to preserve
148 // VNInfo SSA form. Perform a search for all predecessor blocks where we
149 // know the dominating VNInfo.
Matthias Braune25dde52013-10-10 21:28:57 +0000150 if (findReachingDefs(LR, *KillMBB, Kill, PhysReg))
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000151 return;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000152
153 // When there were multiple different values, we may need new PHIs.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000154 calculateValues();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000155}
156
157
158// This function is called by a client after using the low-level API to add
159// live-out and live-in blocks. The unique value optimization is not
160// available, SplitEditor::transferValues handles that case directly anyway.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000161void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000162 assert(Indexes && "Missing SlotIndexes");
163 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000164 updateSSA();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000165 updateLiveIns();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000166}
167
168
Matthias Braune25dde52013-10-10 21:28:57 +0000169bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB,
170 SlotIndex Kill, unsigned PhysReg) {
171 unsigned KillMBBNum = KillMBB.getNumber();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000172
Matthias Braune25dde52013-10-10 21:28:57 +0000173 // Block numbers where LR should be live-in.
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000174 SmallVector<unsigned, 16> WorkList(1, KillMBBNum);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000175
176 // Remember if we have seen more than one value.
177 bool UniqueVNI = true;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700178 VNInfo *TheVNI = nullptr;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000179
180 // Using Seen as a visited set, perform a BFS for all reaching defs.
181 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000182 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000183
184#ifndef NDEBUG
185 if (MBB->pred_empty()) {
186 MBB->getParent()->verify();
187 llvm_unreachable("Use not jointly dominated by defs.");
188 }
189
190 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
191 !MBB->isLiveIn(PhysReg)) {
192 MBB->getParent()->verify();
193 errs() << "The register needs to be live in to BB#" << MBB->getNumber()
194 << ", but is missing from the live-in list.\n";
195 llvm_unreachable("Invalid global physical register");
196 }
197#endif
198
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000199 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
Matthias Braune25dde52013-10-10 21:28:57 +0000200 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000201 MachineBasicBlock *Pred = *PI;
202
203 // Is this a known live-out block?
204 if (Seen.test(Pred->getNumber())) {
205 if (VNInfo *VNI = LiveOut[Pred].first) {
206 if (TheVNI && TheVNI != VNI)
207 UniqueVNI = false;
208 TheVNI = VNI;
209 }
210 continue;
211 }
212
213 SlotIndex Start, End;
Stephen Hines36b56882014-04-23 16:57:46 -0700214 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000215
216 // First time we see Pred. Try to determine the live-out value, but set
217 // it as null if Pred is live-through with an unknown value.
Matthias Braune25dde52013-10-10 21:28:57 +0000218 VNInfo *VNI = LR.extendInBlock(Start, End);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000219 setLiveOutValue(Pred, VNI);
220 if (VNI) {
221 if (TheVNI && TheVNI != VNI)
222 UniqueVNI = false;
223 TheVNI = VNI;
224 continue;
225 }
226
227 // No, we need a live-in value for Pred as well
Matthias Braune25dde52013-10-10 21:28:57 +0000228 if (Pred != &KillMBB)
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000229 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000230 else
231 // Loopback to KillMBB, so value is really live through.
232 Kill = SlotIndex();
233 }
234 }
235
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000236 LiveIn.clear();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000237
238 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
239 // neither require it. Skip the sorting overhead for small updates.
240 if (WorkList.size() > 4)
241 array_pod_sort(WorkList.begin(), WorkList.end());
242
243 // If a unique reaching def was found, blit in the live ranges immediately.
244 if (UniqueVNI) {
Matthias Braune25dde52013-10-10 21:28:57 +0000245 LiveRangeUpdater Updater(&LR);
246 for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(),
247 E = WorkList.end(); I != E; ++I) {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000248 SlotIndex Start, End;
Stephen Hines36b56882014-04-23 16:57:46 -0700249 std::tie(Start, End) = Indexes->getMBBRange(*I);
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000250 // Trim the live range in KillMBB.
251 if (*I == KillMBBNum && Kill.isValid())
252 End = Kill;
253 else
254 LiveOut[MF->getBlockNumbered(*I)] =
Stephen Hinesdce4a402014-05-29 02:49:00 -0700255 LiveOutPair(TheVNI, nullptr);
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000256 Updater.add(Start, End, TheVNI);
257 }
258 return true;
259 }
260
261 // Multiple values were found, so transfer the work list to the LiveIn array
262 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000263 LiveIn.reserve(WorkList.size());
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000264 for (SmallVectorImpl<unsigned>::const_iterator
265 I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
266 MachineBasicBlock *MBB = MF->getBlockNumbered(*I);
Matthias Braune25dde52013-10-10 21:28:57 +0000267 addLiveInBlock(LR, DomTree->getNode(MBB));
268 if (MBB == &KillMBB)
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000269 LiveIn.back().Kill = Kill;
270 }
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000271
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000272 return false;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000273}
274
275
276// This is essentially the same iterative algorithm that SSAUpdater uses,
277// except we already have a dominator tree, so we don't have to recompute it.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000278void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000279 assert(Indexes && "Missing SlotIndexes");
280 assert(DomTree && "Missing dominator tree");
281
282 // Interate until convergence.
283 unsigned Changes;
284 do {
285 Changes = 0;
286 // Propagate live-out values down the dominator tree, inserting phi-defs
287 // when necessary.
288 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
289 E = LiveIn.end(); I != E; ++I) {
290 MachineDomTreeNode *Node = I->DomNode;
291 // Skip block if the live-in value has already been determined.
292 if (!Node)
293 continue;
294 MachineBasicBlock *MBB = Node->getBlock();
295 MachineDomTreeNode *IDom = Node->getIDom();
296 LiveOutPair IDomValue;
297
298 // We need a live-in value to a block with no immediate dominator?
299 // This is probably an unreachable block that has survived somehow.
300 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
301
302 // IDom dominates all of our predecessors, but it may not be their
303 // immediate dominator. Check if any of them have live-out values that are
304 // properly dominated by IDom. If so, we need a phi-def here.
305 if (!needPHI) {
306 IDomValue = LiveOut[IDom->getBlock()];
307
308 // Cache the DomTree node that defined the value.
309 if (IDomValue.first && !IDomValue.second)
310 LiveOut[IDom->getBlock()].second = IDomValue.second =
311 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
312
313 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
314 PE = MBB->pred_end(); PI != PE; ++PI) {
315 LiveOutPair &Value = LiveOut[*PI];
316 if (!Value.first || Value.first == IDomValue.first)
317 continue;
318
319 // Cache the DomTree node that defined the value.
320 if (!Value.second)
321 Value.second =
322 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
323
324 // This predecessor is carrying something other than IDomValue.
325 // It could be because IDomValue hasn't propagated yet, or it could be
326 // because MBB is in the dominance frontier of that value.
327 if (DomTree->dominates(IDom, Value.second)) {
328 needPHI = true;
329 break;
330 }
331 }
332 }
333
334 // The value may be live-through even if Kill is set, as can happen when
335 // we are called from extendRange. In that case LiveOutSeen is true, and
336 // LiveOut indicates a foreign or missing value.
337 LiveOutPair &LOP = LiveOut[MBB];
338
339 // Create a phi-def if required.
340 if (needPHI) {
341 ++Changes;
342 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
343 SlotIndex Start, End;
Stephen Hines36b56882014-04-23 16:57:46 -0700344 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braune25dde52013-10-10 21:28:57 +0000345 LiveRange &LR = I->LR;
346 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000347 I->Value = VNI;
348 // This block is done, we know the final value.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700349 I->DomNode = nullptr;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000350
351 // Add liveness since updateLiveIns now skips this node.
352 if (I->Kill.isValid())
Matthias Braune25dde52013-10-10 21:28:57 +0000353 LR.addSegment(LiveInterval::Segment(Start, I->Kill, VNI));
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000354 else {
Matthias Braune25dde52013-10-10 21:28:57 +0000355 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000356 LOP = LiveOutPair(VNI, Node);
357 }
358 } else if (IDomValue.first) {
359 // No phi-def here. Remember incoming value.
360 I->Value = IDomValue.first;
361
362 // If the IDomValue is killed in the block, don't propagate through.
363 if (I->Kill.isValid())
364 continue;
365
366 // Propagate IDomValue if it isn't killed:
367 // MBB is live-out and doesn't define its own value.
368 if (LOP.first == IDomValue.first)
369 continue;
370 ++Changes;
371 LOP = IDomValue;
372 }
373 }
374 } while (Changes);
375}