blob: 149f93fa69ccbb7b0b622217daa9144cd14b88a8 [file] [log] [blame]
Marcello Maggioniea11f472020-03-22 19:08:29 -07001//===- LiveRangeCalc.cpp - Calculate live ranges -------------------------===//
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of the LiveRangeCalc class.
10//
11//===----------------------------------------------------------------------===//
12
Marcello Maggioni6fc9563d2019-10-17 03:12:51 +000013#include "llvm/CodeGen/LiveRangeCalc.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000014#include "llvm/ADT/BitVector.h"
15#include "llvm/ADT/STLExtras.h"
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000016#include "llvm/ADT/SetVector.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/CodeGen/LiveInterval.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000020#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstr.h"
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000024#include "llvm/CodeGen/SlotIndexes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000025#include "llvm/CodeGen/TargetRegisterInfo.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000026#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000028#include <cassert>
29#include <iterator>
30#include <tuple>
31#include <utility>
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000032
33using namespace llvm;
34
Chandler Carruth1b9dde02014-04-22 02:02:50 +000035#define DEBUG_TYPE "regalloc"
36
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +000037// Reserve an address that indicates a value that is known to be "undef".
38static VNInfo UndefVNI(0xbad, SlotIndex());
39
Matthias Braun1aed6ff2014-12-16 04:03:38 +000040void LiveRangeCalc::resetLiveOutMap() {
41 unsigned NumBlocks = MF->getNumBlockIDs();
42 Seen.clear();
43 Seen.resize(NumBlocks);
Matthias Brauna6e77402017-06-27 18:05:26 +000044 EntryInfos.clear();
Matthias Braun1aed6ff2014-12-16 04:03:38 +000045 Map.resize(NumBlocks);
46}
47
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000048void LiveRangeCalc::reset(const MachineFunction *mf,
Jakob Stoklund Olesen5ef0e0b22012-06-04 18:21:16 +000049 SlotIndexes *SI,
50 MachineDominatorTree *MDT,
51 VNInfo::Allocator *VNIA) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000052 MF = mf;
Jakob Stoklund Olesen5ef0e0b22012-06-04 18:21:16 +000053 MRI = &MF->getRegInfo();
54 Indexes = SI;
55 DomTree = MDT;
56 Alloc = VNIA;
Matthias Braun1aed6ff2014-12-16 04:03:38 +000057 resetLiveOutMap();
Matthias Braunc3a72c22014-12-15 21:36:35 +000058 LiveIn.clear();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000059}
60
Matthias Braun1aed6ff2014-12-16 04:03:38 +000061void LiveRangeCalc::updateFromLiveIns() {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000062 LiveRangeUpdater Updater;
Matthias Braun42fab342014-12-15 19:40:46 +000063 for (const LiveInBlock &I : LiveIn) {
64 if (!I.DomNode)
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000065 continue;
Matthias Braun42fab342014-12-15 19:40:46 +000066 MachineBasicBlock *MBB = I.DomNode->getBlock();
67 assert(I.Value && "No live-in value found");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000068 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +000069 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000070
Matthias Braun42fab342014-12-15 19:40:46 +000071 if (I.Kill.isValid())
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000072 // Value is killed inside this block.
Matthias Braun42fab342014-12-15 19:40:46 +000073 End = I.Kill;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000074 else {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000075 // The value is live-through, update LiveOut as well.
76 // Defer the Domtree lookup until it is needed.
Matthias Braun1aed6ff2014-12-16 04:03:38 +000077 assert(Seen.test(MBB->getNumber()));
78 Map[MBB] = LiveOutPair(I.Value, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000079 }
Matthias Braun42fab342014-12-15 19:40:46 +000080 Updater.setDest(&I.LR);
81 Updater.add(Start, End, I.Value);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000082 }
83 LiveIn.clear();
84}
85
Craig Topperbdf50f02025-03-06 08:56:47 -080086void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, Register PhysReg,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000087 ArrayRef<SlotIndex> Undefs) {
Matthias Braun11042c82015-02-18 01:50:52 +000088 assert(Use.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000089 assert(Indexes && "Missing SlotIndexes");
90 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000091
Matthias Braun11042c82015-02-18 01:50:52 +000092 MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
93 assert(UseMBB && "No MBB at Use");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000094
95 // Is there a def in the same MBB we can extend?
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000096 auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use);
97 if (EP.first != nullptr || EP.second)
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000098 return;
99
Matthias Braun11042c82015-02-18 01:50:52 +0000100 // Find the single reaching def, or determine if Use is jointly dominated by
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000101 // multiple values, and we may need to create even more phi-defs to preserve
102 // VNInfo SSA form. Perform a search for all predecessor blocks where we
103 // know the dominating VNInfo.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000104 if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000105 return;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000106
107 // When there were multiple different values, we may need new PHIs.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000108 calculateValues();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000109}
110
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000111// This function is called by a client after using the low-level API to add
112// live-out and live-in blocks. The unique value optimization is not
113// available, SplitEditor::transferValues handles that case directly anyway.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000114void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000115 assert(Indexes && "Missing SlotIndexes");
116 assert(DomTree && "Missing dominator tree");
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000117 updateSSA();
118 updateFromLiveIns();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000119}
120
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000121bool LiveRangeCalc::isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
122 MachineBasicBlock &MBB, BitVector &DefOnEntry,
123 BitVector &UndefOnEntry) {
124 unsigned BN = MBB.getNumber();
125 if (DefOnEntry[BN])
126 return true;
127 if (UndefOnEntry[BN])
128 return false;
129
Malcolm Parsons17d266b2017-01-13 17:12:16 +0000130 auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000131 for (MachineBasicBlock *S : B.successors())
132 DefOnEntry[S->getNumber()] = true;
133 DefOnEntry[BN] = true;
134 return true;
135 };
136
137 SetVector<unsigned> WorkList;
138 // Checking if the entry of MBB is reached by some def: add all predecessors
139 // that are potentially defined-on-exit to the work list.
140 for (MachineBasicBlock *P : MBB.predecessors())
141 WorkList.insert(P->getNumber());
142
143 for (unsigned i = 0; i != WorkList.size(); ++i) {
144 // Determine if the exit from the block is reached by some def.
145 unsigned N = WorkList[i];
146 MachineBasicBlock &B = *MF->getBlockNumbered(N);
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000147 if (Seen[N]) {
148 const LiveOutPair &LOB = Map[&B];
149 if (LOB.first != nullptr && LOB.first != &UndefVNI)
150 return MarkDefined(B);
151 }
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000152 SlotIndex Begin, End;
153 std::tie(Begin, End) = Indexes->getMBBRange(&B);
Krzysztof Parzyszekde44c9d2017-01-18 23:12:19 +0000154 // Treat End as not belonging to B.
155 // If LR has a segment S that starts at the next block, i.e. [End, ...),
156 // std::upper_bound will return the segment following S. Instead,
157 // S should be treated as the first segment that does not overlap B.
Kazu Hirata1a2d67f2021-01-29 23:23:35 -0800158 LiveRange::iterator UB = upper_bound(LR, End.getPrevSlot());
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000159 if (UB != LR.begin()) {
160 LiveRange::Segment &Seg = *std::prev(UB);
161 if (Seg.end > Begin) {
162 // There is a segment that overlaps B. If the range is not explicitly
163 // undefined between the end of the segment and the end of the block,
164 // treat the block as defined on exit. If it is, go to the next block
165 // on the work list.
166 if (LR.isUndefIn(Undefs, Seg.end, End))
167 continue;
168 return MarkDefined(B);
169 }
170 }
171
172 // No segment overlaps with this block. If this block is not defined on
173 // entry, or it undefines the range, do not process its predecessors.
174 if (UndefOnEntry[N] || LR.isUndefIn(Undefs, Begin, End)) {
175 UndefOnEntry[N] = true;
176 continue;
177 }
178 if (DefOnEntry[N])
179 return MarkDefined(B);
180
181 // Still don't know: add all predecessors to the work list.
182 for (MachineBasicBlock *P : B.predecessors())
183 WorkList.insert(P->getNumber());
184 }
185
186 UndefOnEntry[BN] = true;
187 return false;
188}
189
Matthias Braun11042c82015-02-18 01:50:52 +0000190bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
Craig Topperbdf50f02025-03-06 08:56:47 -0800191 SlotIndex Use, Register PhysReg,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000192 ArrayRef<SlotIndex> Undefs) {
Matthias Braun11042c82015-02-18 01:50:52 +0000193 unsigned UseMBBNum = UseMBB.getNumber();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000194
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000195 // Block numbers where LR should be live-in.
Matthias Braun11042c82015-02-18 01:50:52 +0000196 SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000197
198 // Remember if we have seen more than one value.
199 bool UniqueVNI = true;
Craig Topperc0196b12014-04-14 00:51:57 +0000200 VNInfo *TheVNI = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000201
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000202 bool FoundUndef = false;
203
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000204 // Using Seen as a visited set, perform a BFS for all reaching defs.
205 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000206 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000207
208#ifndef NDEBUG
Matthias Braune1d96282016-09-19 16:49:45 +0000209 if (MBB->pred_empty()) {
Matt Arsenaultb7b945b2024-09-25 11:35:16 +0400210 MBB->getParent()->verify(nullptr, nullptr, &errs());
Matt Arsenault901a95f2018-10-30 01:11:31 +0000211 errs() << "Use of " << printReg(PhysReg, MRI->getTargetRegisterInfo())
Matthias Braun53917542015-05-11 18:47:47 +0000212 << " does not have a corresponding definition on every path:\n";
213 const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
214 if (MI != nullptr)
215 errs() << Use << " " << *MI;
Matthias Braune1d96282016-09-19 16:49:45 +0000216 report_fatal_error("Use not jointly dominated by defs.");
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000217 }
218
Craig Topperbdf50f02025-03-06 08:56:47 -0800219 if (PhysReg.isPhysical()) {
Matt Arsenaultf3d1a1a2016-09-03 06:57:49 +0000220 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
Carl Ritsond0e246f2023-08-16 17:22:29 +0900221 bool IsLiveIn = MBB->isLiveIn(PhysReg);
222 for (MCRegAliasIterator Alias(PhysReg, TRI, false); !IsLiveIn && Alias.isValid(); ++Alias)
223 IsLiveIn = MBB->isLiveIn(*Alias);
224 if (!IsLiveIn) {
Matt Arsenaultb7b945b2024-09-25 11:35:16 +0400225 MBB->getParent()->verify(nullptr, nullptr, &errs());
Carl Ritsond0e246f2023-08-16 17:22:29 +0900226 errs() << "The register " << printReg(PhysReg, TRI)
227 << " needs to be live in to " << printMBBReference(*MBB)
228 << ", but is missing from the live-in list.\n";
229 report_fatal_error("Invalid global physical register");
230 }
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000231 }
232#endif
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000233 FoundUndef |= MBB->pred_empty();
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000234
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000235 for (MachineBasicBlock *Pred : MBB->predecessors()) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000236 // Is this a known live-out block?
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000237 if (Seen.test(Pred->getNumber())) {
238 if (VNInfo *VNI = Map[Pred].first) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000239 if (TheVNI && TheVNI != VNI)
240 UniqueVNI = false;
241 TheVNI = VNI;
242 }
243 continue;
244 }
245
246 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000247 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000248
249 // First time we see Pred. Try to determine the live-out value, but set
250 // it as null if Pred is live-through with an unknown value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000251 auto EP = LR.extendInBlock(Undefs, Start, End);
252 VNInfo *VNI = EP.first;
253 FoundUndef |= EP.second;
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000254 setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000255 if (VNI) {
256 if (TheVNI && TheVNI != VNI)
257 UniqueVNI = false;
258 TheVNI = VNI;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000259 }
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000260 if (VNI || EP.second)
261 continue;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000262
263 // No, we need a live-in value for Pred as well
Matthias Braun11042c82015-02-18 01:50:52 +0000264 if (Pred != &UseMBB)
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000265 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000266 else
Matthias Braun11042c82015-02-18 01:50:52 +0000267 // Loopback to UseMBB, so value is really live through.
268 Use = SlotIndex();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000269 }
270 }
271
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000272 LiveIn.clear();
Krzysztof Parzyszek30085942017-06-28 15:46:16 +0000273 FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI);
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000274 if (!Undefs.empty() && FoundUndef)
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000275 UniqueVNI = false;
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000276
277 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
278 // neither require it. Skip the sorting overhead for small updates.
279 if (WorkList.size() > 4)
280 array_pod_sort(WorkList.begin(), WorkList.end());
281
282 // If a unique reaching def was found, blit in the live ranges immediately.
283 if (UniqueVNI) {
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000284 assert(TheVNI != nullptr && TheVNI != &UndefVNI);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000285 LiveRangeUpdater Updater(&LR);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000286 for (unsigned BN : WorkList) {
287 SlotIndex Start, End;
288 std::tie(Start, End) = Indexes->getMBBRange(BN);
289 // Trim the live range in UseMBB.
290 if (BN == UseMBBNum && Use.isValid())
291 End = Use;
292 else
293 Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr);
294 Updater.add(Start, End, TheVNI);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000295 }
296 return true;
297 }
298
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000299 // Prepare the defined/undefined bit vectors.
Matthias Brauna6e77402017-06-27 18:05:26 +0000300 EntryInfoMap::iterator Entry;
301 bool DidInsert;
302 std::tie(Entry, DidInsert) = EntryInfos.insert(
303 std::make_pair(&LR, std::make_pair(BitVector(), BitVector())));
304 if (DidInsert) {
305 // Initialize newly inserted entries.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000306 unsigned N = MF->getNumBlockIDs();
Matthias Brauna6e77402017-06-27 18:05:26 +0000307 Entry->second.first.resize(N);
308 Entry->second.second.resize(N);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000309 }
Matthias Brauna6e77402017-06-27 18:05:26 +0000310 BitVector &DefOnEntry = Entry->second.first;
311 BitVector &UndefOnEntry = Entry->second.second;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000312
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000313 // Multiple values were found, so transfer the work list to the LiveIn array
314 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000315 LiveIn.reserve(WorkList.size());
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000316 for (unsigned BN : WorkList) {
317 MachineBasicBlock *MBB = MF->getBlockNumbered(BN);
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000318 if (!Undefs.empty() &&
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000319 !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000320 continue;
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000321 addLiveInBlock(LR, DomTree->getNode(MBB));
Matthias Braun11042c82015-02-18 01:50:52 +0000322 if (MBB == &UseMBB)
323 LiveIn.back().Kill = Use;
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000324 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000325
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000326 return false;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000327}
328
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000329// This is essentially the same iterative algorithm that SSAUpdater uses,
330// except we already have a dominator tree, so we don't have to recompute it.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000331void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000332 assert(Indexes && "Missing SlotIndexes");
333 assert(DomTree && "Missing dominator tree");
334
335 // Interate until convergence.
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000336 bool Changed;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000337 do {
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000338 Changed = false;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000339 // Propagate live-out values down the dominator tree, inserting phi-defs
340 // when necessary.
Matthias Braun42fab342014-12-15 19:40:46 +0000341 for (LiveInBlock &I : LiveIn) {
342 MachineDomTreeNode *Node = I.DomNode;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000343 // Skip block if the live-in value has already been determined.
344 if (!Node)
345 continue;
346 MachineBasicBlock *MBB = Node->getBlock();
347 MachineDomTreeNode *IDom = Node->getIDom();
348 LiveOutPair IDomValue;
349
350 // We need a live-in value to a block with no immediate dominator?
351 // This is probably an unreachable block that has survived somehow.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000352 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000353
354 // IDom dominates all of our predecessors, but it may not be their
355 // immediate dominator. Check if any of them have live-out values that are
356 // properly dominated by IDom. If so, we need a phi-def here.
357 if (!needPHI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000358 IDomValue = Map[IDom->getBlock()];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000359
360 // Cache the DomTree node that defined the value.
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000361 if (IDomValue.first && IDomValue.first != &UndefVNI &&
362 !IDomValue.second) {
363 Map[IDom->getBlock()].second = IDomValue.second =
364 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
365 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000366
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000367 for (MachineBasicBlock *Pred : MBB->predecessors()) {
368 LiveOutPair &Value = Map[Pred];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000369 if (!Value.first || Value.first == IDomValue.first)
370 continue;
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000371 if (Value.first == &UndefVNI) {
372 needPHI = true;
373 break;
374 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000375
376 // Cache the DomTree node that defined the value.
377 if (!Value.second)
378 Value.second =
379 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
380
381 // This predecessor is carrying something other than IDomValue.
382 // It could be because IDomValue hasn't propagated yet, or it could be
383 // because MBB is in the dominance frontier of that value.
384 if (DomTree->dominates(IDom, Value.second)) {
385 needPHI = true;
386 break;
387 }
388 }
389 }
390
391 // The value may be live-through even if Kill is set, as can happen when
392 // we are called from extendRange. In that case LiveOutSeen is true, and
393 // LiveOut indicates a foreign or missing value.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000394 LiveOutPair &LOP = Map[MBB];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000395
396 // Create a phi-def if required.
397 if (needPHI) {
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000398 Changed = true;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000399 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
400 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000401 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braun42fab342014-12-15 19:40:46 +0000402 LiveRange &LR = I.LR;
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000403 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Matthias Braun42fab342014-12-15 19:40:46 +0000404 I.Value = VNI;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000405 // This block is done, we know the final value.
Matthias Braun42fab342014-12-15 19:40:46 +0000406 I.DomNode = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000407
Matthias Braun2f662322014-12-10 01:12:12 +0000408 // Add liveness since updateFromLiveIns now skips this node.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000409 if (I.Kill.isValid()) {
410 if (VNI)
411 LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
412 } else {
413 if (VNI)
414 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000415 LOP = LiveOutPair(VNI, Node);
416 }
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000417 } else if (IDomValue.first && IDomValue.first != &UndefVNI) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000418 // No phi-def here. Remember incoming value.
Matthias Braun42fab342014-12-15 19:40:46 +0000419 I.Value = IDomValue.first;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000420
421 // If the IDomValue is killed in the block, don't propagate through.
Matthias Braun42fab342014-12-15 19:40:46 +0000422 if (I.Kill.isValid())
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000423 continue;
424
425 // Propagate IDomValue if it isn't killed:
426 // MBB is live-out and doesn't define its own value.
427 if (LOP.first == IDomValue.first)
428 continue;
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000429 Changed = true;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000430 LOP = IDomValue;
431 }
432 }
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000433 } while (Changed);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000434}
Krzysztof Parzyszek70f02702018-06-26 14:37:16 +0000435
436bool LiveRangeCalc::isJointlyDominated(const MachineBasicBlock *MBB,
437 ArrayRef<SlotIndex> Defs,
438 const SlotIndexes &Indexes) {
439 const MachineFunction &MF = *MBB->getParent();
440 BitVector DefBlocks(MF.getNumBlockIDs());
441 for (SlotIndex I : Defs)
442 DefBlocks.set(Indexes.getMBBFromIndex(I)->getNumber());
443
Jay Foada33ae1b2024-11-13 13:36:48 +0000444 unsigned EntryNum = MF.front().getNumber();
Krzysztof Parzyszek70f02702018-06-26 14:37:16 +0000445 SetVector<unsigned> PredQueue;
446 PredQueue.insert(MBB->getNumber());
447 for (unsigned i = 0; i != PredQueue.size(); ++i) {
448 unsigned BN = PredQueue[i];
449 if (DefBlocks[BN])
Jay Foada33ae1b2024-11-13 13:36:48 +0000450 continue;
451 if (BN == EntryNum) {
452 // We found a path from MBB back to the entry block without hitting any of
453 // the def blocks.
454 return false;
455 }
Krzysztof Parzyszek70f02702018-06-26 14:37:16 +0000456 const MachineBasicBlock *B = MF.getBlockNumbered(BN);
457 for (const MachineBasicBlock *P : B->predecessors())
458 PredQueue.insert(P->getNumber());
459 }
Jay Foada33ae1b2024-11-13 13:36:48 +0000460 return true;
Krzysztof Parzyszek70f02702018-06-26 14:37:16 +0000461}