Marcello Maggioni | ea11f47 | 2020-03-22 19:08:29 -0700 | [diff] [blame] | 1 | //===- LiveRangeCalc.cpp - Calculate live ranges -------------------------===// |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Implementation of the LiveRangeCalc class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Marcello Maggioni | 6fc9563d | 2019-10-17 03:12:51 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/LiveRangeCalc.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/BitVector.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/CodeGen/LiveInterval.h" |
| 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstr.h" |
Jakob Stoklund Olesen | 989b3b1 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 28 | #include <cassert> |
| 29 | #include <iterator> |
| 30 | #include <tuple> |
| 31 | #include <utility> |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "regalloc" |
| 36 | |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 37 | // Reserve an address that indicates a value that is known to be "undef". |
| 38 | static VNInfo UndefVNI(0xbad, SlotIndex()); |
| 39 | |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 40 | void LiveRangeCalc::resetLiveOutMap() { |
| 41 | unsigned NumBlocks = MF->getNumBlockIDs(); |
| 42 | Seen.clear(); |
| 43 | Seen.resize(NumBlocks); |
Matthias Braun | a6e7740 | 2017-06-27 18:05:26 +0000 | [diff] [blame] | 44 | EntryInfos.clear(); |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 45 | Map.resize(NumBlocks); |
| 46 | } |
| 47 | |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 48 | void LiveRangeCalc::reset(const MachineFunction *mf, |
Jakob Stoklund Olesen | 5ef0e0b2 | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 49 | SlotIndexes *SI, |
| 50 | MachineDominatorTree *MDT, |
| 51 | VNInfo::Allocator *VNIA) { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 52 | MF = mf; |
Jakob Stoklund Olesen | 5ef0e0b2 | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 53 | MRI = &MF->getRegInfo(); |
| 54 | Indexes = SI; |
| 55 | DomTree = MDT; |
| 56 | Alloc = VNIA; |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 57 | resetLiveOutMap(); |
Matthias Braun | c3a72c2 | 2014-12-15 21:36:35 +0000 | [diff] [blame] | 58 | LiveIn.clear(); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 61 | void LiveRangeCalc::updateFromLiveIns() { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 62 | LiveRangeUpdater Updater; |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 63 | for (const LiveInBlock &I : LiveIn) { |
| 64 | if (!I.DomNode) |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 65 | continue; |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 66 | MachineBasicBlock *MBB = I.DomNode->getBlock(); |
| 67 | assert(I.Value && "No live-in value found"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 68 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 69 | std::tie(Start, End) = Indexes->getMBBRange(MBB); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 70 | |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 71 | if (I.Kill.isValid()) |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 72 | // Value is killed inside this block. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 73 | End = I.Kill; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 74 | else { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 75 | // The value is live-through, update LiveOut as well. |
| 76 | // Defer the Domtree lookup until it is needed. |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 77 | assert(Seen.test(MBB->getNumber())); |
| 78 | Map[MBB] = LiveOutPair(I.Value, nullptr); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 79 | } |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 80 | Updater.setDest(&I.LR); |
| 81 | Updater.add(Start, End, I.Value); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 82 | } |
| 83 | LiveIn.clear(); |
| 84 | } |
| 85 | |
Craig Topper | bdf50f0 | 2025-03-06 08:56:47 -0800 | [diff] [blame] | 86 | void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, Register PhysReg, |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 87 | ArrayRef<SlotIndex> Undefs) { |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 88 | assert(Use.isValid() && "Invalid SlotIndex"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 89 | assert(Indexes && "Missing SlotIndexes"); |
| 90 | assert(DomTree && "Missing dominator tree"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 91 | |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 92 | MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot()); |
| 93 | assert(UseMBB && "No MBB at Use"); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 94 | |
| 95 | // Is there a def in the same MBB we can extend? |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 96 | auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use); |
| 97 | if (EP.first != nullptr || EP.second) |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 98 | return; |
| 99 | |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 100 | // Find the single reaching def, or determine if Use is jointly dominated by |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 101 | // 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 Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 104 | if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs)) |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 105 | return; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 106 | |
| 107 | // When there were multiple different values, we may need new PHIs. |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 108 | calculateValues(); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 111 | // 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 Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 114 | void LiveRangeCalc::calculateValues() { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 115 | assert(Indexes && "Missing SlotIndexes"); |
| 116 | assert(DomTree && "Missing dominator tree"); |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 117 | updateSSA(); |
| 118 | updateFromLiveIns(); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 121 | bool 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 Parsons | 17d266b | 2017-01-13 17:12:16 +0000 | [diff] [blame] | 130 | auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool { |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 131 | 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 Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 147 | if (Seen[N]) { |
| 148 | const LiveOutPair &LOB = Map[&B]; |
| 149 | if (LOB.first != nullptr && LOB.first != &UndefVNI) |
| 150 | return MarkDefined(B); |
| 151 | } |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 152 | SlotIndex Begin, End; |
| 153 | std::tie(Begin, End) = Indexes->getMBBRange(&B); |
Krzysztof Parzyszek | de44c9d | 2017-01-18 23:12:19 +0000 | [diff] [blame] | 154 | // 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 Hirata | 1a2d67f | 2021-01-29 23:23:35 -0800 | [diff] [blame] | 158 | LiveRange::iterator UB = upper_bound(LR, End.getPrevSlot()); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 159 | 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 Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 190 | bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB, |
Craig Topper | bdf50f0 | 2025-03-06 08:56:47 -0800 | [diff] [blame] | 191 | SlotIndex Use, Register PhysReg, |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 192 | ArrayRef<SlotIndex> Undefs) { |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 193 | unsigned UseMBBNum = UseMBB.getNumber(); |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 194 | |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 195 | // Block numbers where LR should be live-in. |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 196 | SmallVector<unsigned, 16> WorkList(1, UseMBBNum); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 197 | |
| 198 | // Remember if we have seen more than one value. |
| 199 | bool UniqueVNI = true; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 200 | VNInfo *TheVNI = nullptr; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 201 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 202 | bool FoundUndef = false; |
| 203 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 204 | // Using Seen as a visited set, perform a BFS for all reaching defs. |
| 205 | for (unsigned i = 0; i != WorkList.size(); ++i) { |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 206 | MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]); |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 207 | |
| 208 | #ifndef NDEBUG |
Matthias Braun | e1d9628 | 2016-09-19 16:49:45 +0000 | [diff] [blame] | 209 | if (MBB->pred_empty()) { |
Matt Arsenault | b7b945b | 2024-09-25 11:35:16 +0400 | [diff] [blame] | 210 | MBB->getParent()->verify(nullptr, nullptr, &errs()); |
Matt Arsenault | 901a95f | 2018-10-30 01:11:31 +0000 | [diff] [blame] | 211 | errs() << "Use of " << printReg(PhysReg, MRI->getTargetRegisterInfo()) |
Matthias Braun | 5391754 | 2015-05-11 18:47:47 +0000 | [diff] [blame] | 212 | << " 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 Braun | e1d9628 | 2016-09-19 16:49:45 +0000 | [diff] [blame] | 216 | report_fatal_error("Use not jointly dominated by defs."); |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Craig Topper | bdf50f0 | 2025-03-06 08:56:47 -0800 | [diff] [blame] | 219 | if (PhysReg.isPhysical()) { |
Matt Arsenault | f3d1a1a | 2016-09-03 06:57:49 +0000 | [diff] [blame] | 220 | const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo(); |
Carl Ritson | d0e246f | 2023-08-16 17:22:29 +0900 | [diff] [blame] | 221 | 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 Arsenault | b7b945b | 2024-09-25 11:35:16 +0400 | [diff] [blame] | 225 | MBB->getParent()->verify(nullptr, nullptr, &errs()); |
Carl Ritson | d0e246f | 2023-08-16 17:22:29 +0900 | [diff] [blame] | 226 | 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 Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 231 | } |
| 232 | #endif |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 233 | FoundUndef |= MBB->pred_empty(); |
Jakob Stoklund Olesen | 3d604ab | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 234 | |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 235 | for (MachineBasicBlock *Pred : MBB->predecessors()) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 236 | // Is this a known live-out block? |
Matthias Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 237 | if (Seen.test(Pred->getNumber())) { |
| 238 | if (VNInfo *VNI = Map[Pred].first) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 239 | if (TheVNI && TheVNI != VNI) |
| 240 | UniqueVNI = false; |
| 241 | TheVNI = VNI; |
| 242 | } |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 247 | std::tie(Start, End) = Indexes->getMBBRange(Pred); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 248 | |
| 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 Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 251 | auto EP = LR.extendInBlock(Undefs, Start, End); |
| 252 | VNInfo *VNI = EP.first; |
| 253 | FoundUndef |= EP.second; |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 254 | setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 255 | if (VNI) { |
| 256 | if (TheVNI && TheVNI != VNI) |
| 257 | UniqueVNI = false; |
| 258 | TheVNI = VNI; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 259 | } |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 260 | if (VNI || EP.second) |
| 261 | continue; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 262 | |
| 263 | // No, we need a live-in value for Pred as well |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 264 | if (Pred != &UseMBB) |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 265 | WorkList.push_back(Pred->getNumber()); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 266 | else |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 267 | // Loopback to UseMBB, so value is really live through. |
| 268 | Use = SlotIndex(); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 272 | LiveIn.clear(); |
Krzysztof Parzyszek | 3008594 | 2017-06-28 15:46:16 +0000 | [diff] [blame] | 273 | FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI); |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 274 | if (!Undefs.empty() && FoundUndef) |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 275 | UniqueVNI = false; |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 276 | |
| 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 Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 284 | assert(TheVNI != nullptr && TheVNI != &UndefVNI); |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 285 | LiveRangeUpdater Updater(&LR); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 286 | 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 Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 295 | } |
| 296 | return true; |
| 297 | } |
| 298 | |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 299 | // Prepare the defined/undefined bit vectors. |
Matthias Braun | a6e7740 | 2017-06-27 18:05:26 +0000 | [diff] [blame] | 300 | 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 Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 306 | unsigned N = MF->getNumBlockIDs(); |
Matthias Braun | a6e7740 | 2017-06-27 18:05:26 +0000 | [diff] [blame] | 307 | Entry->second.first.resize(N); |
| 308 | Entry->second.second.resize(N); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 309 | } |
Matthias Braun | a6e7740 | 2017-06-27 18:05:26 +0000 | [diff] [blame] | 310 | BitVector &DefOnEntry = Entry->second.first; |
| 311 | BitVector &UndefOnEntry = Entry->second.second; |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 312 | |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 313 | // 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 Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 315 | LiveIn.reserve(WorkList.size()); |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 316 | for (unsigned BN : WorkList) { |
| 317 | MachineBasicBlock *MBB = MF->getBlockNumbered(BN); |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 318 | if (!Undefs.empty() && |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 319 | !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry)) |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 320 | continue; |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 321 | addLiveInBlock(LR, DomTree->getNode(MBB)); |
Matthias Braun | 11042c8 | 2015-02-18 01:50:52 +0000 | [diff] [blame] | 322 | if (MBB == &UseMBB) |
| 323 | LiveIn.back().Kill = Use; |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 324 | } |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 325 | |
Jakob Stoklund Olesen | b389271 | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 326 | return false; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 329 | // 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 Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 331 | void LiveRangeCalc::updateSSA() { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 332 | assert(Indexes && "Missing SlotIndexes"); |
| 333 | assert(DomTree && "Missing dominator tree"); |
| 334 | |
| 335 | // Interate until convergence. |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 336 | bool Changed; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 337 | do { |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 338 | Changed = false; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 339 | // Propagate live-out values down the dominator tree, inserting phi-defs |
| 340 | // when necessary. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 341 | for (LiveInBlock &I : LiveIn) { |
| 342 | MachineDomTreeNode *Node = I.DomNode; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 343 | // 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 Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 352 | bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber()); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 353 | |
| 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 Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 358 | IDomValue = Map[IDom->getBlock()]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 359 | |
| 360 | // Cache the DomTree node that defined the value. |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 361 | 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 Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 366 | |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 367 | for (MachineBasicBlock *Pred : MBB->predecessors()) { |
| 368 | LiveOutPair &Value = Map[Pred]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 369 | if (!Value.first || Value.first == IDomValue.first) |
| 370 | continue; |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 371 | if (Value.first == &UndefVNI) { |
| 372 | needPHI = true; |
| 373 | break; |
| 374 | } |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 375 | |
| 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 Braun | 1aed6ff | 2014-12-16 04:03:38 +0000 | [diff] [blame] | 394 | LiveOutPair &LOP = Map[MBB]; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 395 | |
| 396 | // Create a phi-def if required. |
| 397 | if (needPHI) { |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 398 | Changed = true; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 399 | assert(Alloc && "Need VNInfo allocator to create PHI-defs"); |
| 400 | SlotIndex Start, End; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 401 | std::tie(Start, End) = Indexes->getMBBRange(MBB); |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 402 | LiveRange &LR = I.LR; |
Matthias Braun | 2d5c32b | 2013-10-10 21:28:57 +0000 | [diff] [blame] | 403 | VNInfo *VNI = LR.getNextValue(Start, *Alloc); |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 404 | I.Value = VNI; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 405 | // This block is done, we know the final value. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 406 | I.DomNode = nullptr; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 407 | |
Matthias Braun | 2f66232 | 2014-12-10 01:12:12 +0000 | [diff] [blame] | 408 | // Add liveness since updateFromLiveIns now skips this node. |
Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 409 | 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 Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 415 | LOP = LiveOutPair(VNI, Node); |
| 416 | } |
Krzysztof Parzyszek | 0b7688e | 2017-06-27 21:30:46 +0000 | [diff] [blame] | 417 | } else if (IDomValue.first && IDomValue.first != &UndefVNI) { |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 418 | // No phi-def here. Remember incoming value. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 419 | I.Value = IDomValue.first; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 420 | |
| 421 | // If the IDomValue is killed in the block, don't propagate through. |
Matthias Braun | 42fab34 | 2014-12-15 19:40:46 +0000 | [diff] [blame] | 422 | if (I.Kill.isValid()) |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 423 | 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 Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 429 | Changed = true; |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 430 | LOP = IDomValue; |
| 431 | } |
| 432 | } |
Krzysztof Parzyszek | 93cf232 | 2017-06-28 16:02:00 +0000 | [diff] [blame] | 433 | } while (Changed); |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 434 | } |
Krzysztof Parzyszek | 70f0270 | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 435 | |
| 436 | bool 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 Foad | a33ae1b | 2024-11-13 13:36:48 +0000 | [diff] [blame] | 444 | unsigned EntryNum = MF.front().getNumber(); |
Krzysztof Parzyszek | 70f0270 | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 445 | 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 Foad | a33ae1b | 2024-11-13 13:36:48 +0000 | [diff] [blame] | 450 | 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 Parzyszek | 70f0270 | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 456 | const MachineBasicBlock *B = MF.getBlockNumbered(BN); |
| 457 | for (const MachineBasicBlock *P : B->predecessors()) |
| 458 | PredQueue.insert(P->getNumber()); |
| 459 | } |
Jay Foad | a33ae1b | 2024-11-13 13:36:48 +0000 | [diff] [blame] | 460 | return true; |
Krzysztof Parzyszek | 70f0270 | 2018-06-26 14:37:16 +0000 | [diff] [blame] | 461 | } |