xref: /src/contrib/llvm-project/llvm/lib/CodeGen/LiveRangeCalc.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1cfca06d7SDimitry Andric //===- LiveRangeCalc.cpp - Calculate live ranges -------------------------===//
230815c53SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630815c53SDimitry Andric //
730815c53SDimitry Andric //===----------------------------------------------------------------------===//
830815c53SDimitry Andric //
930815c53SDimitry Andric // Implementation of the LiveRangeCalc class.
1030815c53SDimitry Andric //
1130815c53SDimitry Andric //===----------------------------------------------------------------------===//
1230815c53SDimitry Andric 
131d5ae102SDimitry Andric #include "llvm/CodeGen/LiveRangeCalc.h"
14044eb2f6SDimitry Andric #include "llvm/ADT/BitVector.h"
15044eb2f6SDimitry Andric #include "llvm/ADT/STLExtras.h"
16b915e9e0SDimitry Andric #include "llvm/ADT/SetVector.h"
17044eb2f6SDimitry Andric #include "llvm/ADT/SmallVector.h"
18044eb2f6SDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
19044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
2030815c53SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
21044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
22044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
2358b69754SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
24044eb2f6SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
25044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
26044eb2f6SDimitry Andric #include "llvm/Support/ErrorHandling.h"
27044eb2f6SDimitry Andric #include "llvm/Support/raw_ostream.h"
28044eb2f6SDimitry Andric #include <algorithm>
29044eb2f6SDimitry Andric #include <cassert>
30044eb2f6SDimitry Andric #include <iterator>
31044eb2f6SDimitry Andric #include <tuple>
32044eb2f6SDimitry Andric #include <utility>
3330815c53SDimitry Andric 
3430815c53SDimitry Andric using namespace llvm;
3530815c53SDimitry Andric 
365ca98fd9SDimitry Andric #define DEBUG_TYPE "regalloc"
375ca98fd9SDimitry Andric 
389df3605dSDimitry Andric // Reserve an address that indicates a value that is known to be "undef".
399df3605dSDimitry Andric static VNInfo UndefVNI(0xbad, SlotIndex());
409df3605dSDimitry Andric 
resetLiveOutMap()4167c32a98SDimitry Andric void LiveRangeCalc::resetLiveOutMap() {
4267c32a98SDimitry Andric   unsigned NumBlocks = MF->getNumBlockIDs();
4367c32a98SDimitry Andric   Seen.clear();
4467c32a98SDimitry Andric   Seen.resize(NumBlocks);
459df3605dSDimitry Andric   EntryInfos.clear();
4667c32a98SDimitry Andric   Map.resize(NumBlocks);
4767c32a98SDimitry Andric }
4867c32a98SDimitry Andric 
reset(const MachineFunction * mf,SlotIndexes * SI,MachineDominatorTree * MDT,VNInfo::Allocator * VNIA)494a16efa3SDimitry Andric void LiveRangeCalc::reset(const MachineFunction *mf,
5058b69754SDimitry Andric                           SlotIndexes *SI,
5158b69754SDimitry Andric                           MachineDominatorTree *MDT,
5258b69754SDimitry Andric                           VNInfo::Allocator *VNIA) {
534a16efa3SDimitry Andric   MF = mf;
5458b69754SDimitry Andric   MRI = &MF->getRegInfo();
5558b69754SDimitry Andric   Indexes = SI;
5658b69754SDimitry Andric   DomTree = MDT;
5758b69754SDimitry Andric   Alloc = VNIA;
5867c32a98SDimitry Andric   resetLiveOutMap();
5930815c53SDimitry Andric   LiveIn.clear();
6030815c53SDimitry Andric }
6130815c53SDimitry Andric 
updateFromLiveIns()6267c32a98SDimitry Andric void LiveRangeCalc::updateFromLiveIns() {
634a16efa3SDimitry Andric   LiveRangeUpdater Updater;
6467c32a98SDimitry Andric   for (const LiveInBlock &I : LiveIn) {
6567c32a98SDimitry Andric     if (!I.DomNode)
6630815c53SDimitry Andric       continue;
6767c32a98SDimitry Andric     MachineBasicBlock *MBB = I.DomNode->getBlock();
6867c32a98SDimitry Andric     assert(I.Value && "No live-in value found");
6930815c53SDimitry Andric     SlotIndex Start, End;
705ca98fd9SDimitry Andric     std::tie(Start, End) = Indexes->getMBBRange(MBB);
7130815c53SDimitry Andric 
7267c32a98SDimitry Andric     if (I.Kill.isValid())
734a16efa3SDimitry Andric       // Value is killed inside this block.
7467c32a98SDimitry Andric       End = I.Kill;
7530815c53SDimitry Andric     else {
764a16efa3SDimitry Andric       // The value is live-through, update LiveOut as well.
774a16efa3SDimitry Andric       // Defer the Domtree lookup until it is needed.
7830815c53SDimitry Andric       assert(Seen.test(MBB->getNumber()));
7967c32a98SDimitry Andric       Map[MBB] = LiveOutPair(I.Value, nullptr);
8030815c53SDimitry Andric     }
8167c32a98SDimitry Andric     Updater.setDest(&I.LR);
8267c32a98SDimitry Andric     Updater.add(Start, End, I.Value);
8330815c53SDimitry Andric   }
8430815c53SDimitry Andric   LiveIn.clear();
8530815c53SDimitry Andric }
8630815c53SDimitry Andric 
extend(LiveRange & LR,SlotIndex Use,unsigned PhysReg,ArrayRef<SlotIndex> Undefs)87b915e9e0SDimitry Andric void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
88b915e9e0SDimitry Andric                            ArrayRef<SlotIndex> Undefs) {
895a5ac124SDimitry Andric   assert(Use.isValid() && "Invalid SlotIndex");
9030815c53SDimitry Andric   assert(Indexes && "Missing SlotIndexes");
9130815c53SDimitry Andric   assert(DomTree && "Missing dominator tree");
9230815c53SDimitry Andric 
935a5ac124SDimitry Andric   MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
945a5ac124SDimitry Andric   assert(UseMBB && "No MBB at Use");
9530815c53SDimitry Andric 
9630815c53SDimitry Andric   // Is there a def in the same MBB we can extend?
97b915e9e0SDimitry Andric   auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use);
98b915e9e0SDimitry Andric   if (EP.first != nullptr || EP.second)
9930815c53SDimitry Andric     return;
10030815c53SDimitry Andric 
1015a5ac124SDimitry Andric   // Find the single reaching def, or determine if Use is jointly dominated by
10230815c53SDimitry Andric   // multiple values, and we may need to create even more phi-defs to preserve
10330815c53SDimitry Andric   // VNInfo SSA form.  Perform a search for all predecessor blocks where we
10430815c53SDimitry Andric   // know the dominating VNInfo.
105b915e9e0SDimitry Andric   if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
1064a16efa3SDimitry Andric     return;
10730815c53SDimitry Andric 
10830815c53SDimitry Andric   // When there were multiple different values, we may need new PHIs.
1094a16efa3SDimitry Andric   calculateValues();
11030815c53SDimitry Andric }
11130815c53SDimitry Andric 
11230815c53SDimitry Andric // This function is called by a client after using the low-level API to add
11330815c53SDimitry Andric // live-out and live-in blocks.  The unique value optimization is not
11430815c53SDimitry Andric // available, SplitEditor::transferValues handles that case directly anyway.
calculateValues()11558b69754SDimitry Andric void LiveRangeCalc::calculateValues() {
11630815c53SDimitry Andric   assert(Indexes && "Missing SlotIndexes");
11730815c53SDimitry Andric   assert(DomTree && "Missing dominator tree");
11858b69754SDimitry Andric   updateSSA();
11967c32a98SDimitry Andric   updateFromLiveIns();
12030815c53SDimitry Andric }
12130815c53SDimitry Andric 
isDefOnEntry(LiveRange & LR,ArrayRef<SlotIndex> Undefs,MachineBasicBlock & MBB,BitVector & DefOnEntry,BitVector & UndefOnEntry)122b915e9e0SDimitry Andric bool LiveRangeCalc::isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
123b915e9e0SDimitry Andric                                  MachineBasicBlock &MBB, BitVector &DefOnEntry,
124b915e9e0SDimitry Andric                                  BitVector &UndefOnEntry) {
125b915e9e0SDimitry Andric   unsigned BN = MBB.getNumber();
126b915e9e0SDimitry Andric   if (DefOnEntry[BN])
127b915e9e0SDimitry Andric     return true;
128b915e9e0SDimitry Andric   if (UndefOnEntry[BN])
129b915e9e0SDimitry Andric     return false;
130b915e9e0SDimitry Andric 
13171d5a254SDimitry Andric   auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool {
132b915e9e0SDimitry Andric     for (MachineBasicBlock *S : B.successors())
133b915e9e0SDimitry Andric       DefOnEntry[S->getNumber()] = true;
134b915e9e0SDimitry Andric     DefOnEntry[BN] = true;
135b915e9e0SDimitry Andric     return true;
136b915e9e0SDimitry Andric   };
137b915e9e0SDimitry Andric 
138b915e9e0SDimitry Andric   SetVector<unsigned> WorkList;
139b915e9e0SDimitry Andric   // Checking if the entry of MBB is reached by some def: add all predecessors
140b915e9e0SDimitry Andric   // that are potentially defined-on-exit to the work list.
141b915e9e0SDimitry Andric   for (MachineBasicBlock *P : MBB.predecessors())
142b915e9e0SDimitry Andric     WorkList.insert(P->getNumber());
143b915e9e0SDimitry Andric 
144b915e9e0SDimitry Andric   for (unsigned i = 0; i != WorkList.size(); ++i) {
145b915e9e0SDimitry Andric     // Determine if the exit from the block is reached by some def.
146b915e9e0SDimitry Andric     unsigned N = WorkList[i];
147b915e9e0SDimitry Andric     MachineBasicBlock &B = *MF->getBlockNumbered(N);
1489df3605dSDimitry Andric     if (Seen[N]) {
1499df3605dSDimitry Andric       const LiveOutPair &LOB = Map[&B];
1509df3605dSDimitry Andric       if (LOB.first != nullptr && LOB.first != &UndefVNI)
151b915e9e0SDimitry Andric         return MarkDefined(B);
1529df3605dSDimitry Andric     }
153b915e9e0SDimitry Andric     SlotIndex Begin, End;
154b915e9e0SDimitry Andric     std::tie(Begin, End) = Indexes->getMBBRange(&B);
15571d5a254SDimitry Andric     // Treat End as not belonging to B.
15671d5a254SDimitry Andric     // If LR has a segment S that starts at the next block, i.e. [End, ...),
15771d5a254SDimitry Andric     // std::upper_bound will return the segment following S. Instead,
15871d5a254SDimitry Andric     // S should be treated as the first segment that does not overlap B.
159344a3780SDimitry Andric     LiveRange::iterator UB = upper_bound(LR, End.getPrevSlot());
160b915e9e0SDimitry Andric     if (UB != LR.begin()) {
161b915e9e0SDimitry Andric       LiveRange::Segment &Seg = *std::prev(UB);
162b915e9e0SDimitry Andric       if (Seg.end > Begin) {
163b915e9e0SDimitry Andric         // There is a segment that overlaps B. If the range is not explicitly
164b915e9e0SDimitry Andric         // undefined between the end of the segment and the end of the block,
165b915e9e0SDimitry Andric         // treat the block as defined on exit. If it is, go to the next block
166b915e9e0SDimitry Andric         // on the work list.
167b915e9e0SDimitry Andric         if (LR.isUndefIn(Undefs, Seg.end, End))
168b915e9e0SDimitry Andric           continue;
169b915e9e0SDimitry Andric         return MarkDefined(B);
170b915e9e0SDimitry Andric       }
171b915e9e0SDimitry Andric     }
172b915e9e0SDimitry Andric 
173b915e9e0SDimitry Andric     // No segment overlaps with this block. If this block is not defined on
174b915e9e0SDimitry Andric     // entry, or it undefines the range, do not process its predecessors.
175b915e9e0SDimitry Andric     if (UndefOnEntry[N] || LR.isUndefIn(Undefs, Begin, End)) {
176b915e9e0SDimitry Andric       UndefOnEntry[N] = true;
177b915e9e0SDimitry Andric       continue;
178b915e9e0SDimitry Andric     }
179b915e9e0SDimitry Andric     if (DefOnEntry[N])
180b915e9e0SDimitry Andric       return MarkDefined(B);
181b915e9e0SDimitry Andric 
182b915e9e0SDimitry Andric     // Still don't know: add all predecessors to the work list.
183b915e9e0SDimitry Andric     for (MachineBasicBlock *P : B.predecessors())
184b915e9e0SDimitry Andric       WorkList.insert(P->getNumber());
185b915e9e0SDimitry Andric   }
186b915e9e0SDimitry Andric 
187b915e9e0SDimitry Andric   UndefOnEntry[BN] = true;
188b915e9e0SDimitry Andric   return false;
189b915e9e0SDimitry Andric }
190b915e9e0SDimitry Andric 
findReachingDefs(LiveRange & LR,MachineBasicBlock & UseMBB,SlotIndex Use,unsigned PhysReg,ArrayRef<SlotIndex> Undefs)1915a5ac124SDimitry Andric bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
192b915e9e0SDimitry Andric                                      SlotIndex Use, unsigned PhysReg,
193b915e9e0SDimitry Andric                                      ArrayRef<SlotIndex> Undefs) {
1945a5ac124SDimitry Andric   unsigned UseMBBNum = UseMBB.getNumber();
1954a16efa3SDimitry Andric 
196f8af5cf6SDimitry Andric   // Block numbers where LR should be live-in.
1975a5ac124SDimitry Andric   SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
19830815c53SDimitry Andric 
19930815c53SDimitry Andric   // Remember if we have seen more than one value.
20030815c53SDimitry Andric   bool UniqueVNI = true;
2015ca98fd9SDimitry Andric   VNInfo *TheVNI = nullptr;
20230815c53SDimitry Andric 
203b915e9e0SDimitry Andric   bool FoundUndef = false;
204b915e9e0SDimitry Andric 
20530815c53SDimitry Andric   // Using Seen as a visited set, perform a BFS for all reaching defs.
20630815c53SDimitry Andric   for (unsigned i = 0; i != WorkList.size(); ++i) {
2074a16efa3SDimitry Andric     MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
20858b69754SDimitry Andric 
20958b69754SDimitry Andric #ifndef NDEBUG
21058b69754SDimitry Andric     if (MBB->pred_empty()) {
21158b69754SDimitry Andric       MBB->getParent()->verify();
212d8e91e46SDimitry Andric       errs() << "Use of " << printReg(PhysReg, MRI->getTargetRegisterInfo())
2135a5ac124SDimitry Andric              << " does not have a corresponding definition on every path:\n";
2145a5ac124SDimitry Andric       const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
2155a5ac124SDimitry Andric       if (MI != nullptr)
2165a5ac124SDimitry Andric         errs() << Use << " " << *MI;
217b915e9e0SDimitry Andric       report_fatal_error("Use not jointly dominated by defs.");
21858b69754SDimitry Andric     }
21958b69754SDimitry Andric 
220b1c73532SDimitry Andric     if (Register::isPhysicalRegister(PhysReg)) {
221b915e9e0SDimitry Andric       const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
222b1c73532SDimitry Andric       bool IsLiveIn = MBB->isLiveIn(PhysReg);
223b1c73532SDimitry Andric       for (MCRegAliasIterator Alias(PhysReg, TRI, false); !IsLiveIn && Alias.isValid(); ++Alias)
224b1c73532SDimitry Andric         IsLiveIn = MBB->isLiveIn(*Alias);
225b1c73532SDimitry Andric       if (!IsLiveIn) {
226b1c73532SDimitry Andric         MBB->getParent()->verify();
227044eb2f6SDimitry Andric         errs() << "The register " << printReg(PhysReg, TRI)
228044eb2f6SDimitry Andric                << " needs to be live in to " << printMBBReference(*MBB)
22958b69754SDimitry Andric                << ", but is missing from the live-in list.\n";
230b915e9e0SDimitry Andric         report_fatal_error("Invalid global physical register");
23158b69754SDimitry Andric       }
232b1c73532SDimitry Andric     }
23358b69754SDimitry Andric #endif
234b915e9e0SDimitry Andric     FoundUndef |= MBB->pred_empty();
23558b69754SDimitry Andric 
2369df3605dSDimitry Andric     for (MachineBasicBlock *Pred : MBB->predecessors()) {
23730815c53SDimitry Andric        // Is this a known live-out block?
23830815c53SDimitry Andric        if (Seen.test(Pred->getNumber())) {
23967c32a98SDimitry Andric          if (VNInfo *VNI = Map[Pred].first) {
24030815c53SDimitry Andric            if (TheVNI && TheVNI != VNI)
24130815c53SDimitry Andric              UniqueVNI = false;
24230815c53SDimitry Andric            TheVNI = VNI;
24330815c53SDimitry Andric          }
24430815c53SDimitry Andric          continue;
24530815c53SDimitry Andric        }
24630815c53SDimitry Andric 
24730815c53SDimitry Andric        SlotIndex Start, End;
2485ca98fd9SDimitry Andric        std::tie(Start, End) = Indexes->getMBBRange(Pred);
24930815c53SDimitry Andric 
25030815c53SDimitry Andric        // First time we see Pred.  Try to determine the live-out value, but set
25130815c53SDimitry Andric        // it as null if Pred is live-through with an unknown value.
252b915e9e0SDimitry Andric        auto EP = LR.extendInBlock(Undefs, Start, End);
253b915e9e0SDimitry Andric        VNInfo *VNI = EP.first;
254b915e9e0SDimitry Andric        FoundUndef |= EP.second;
2559df3605dSDimitry Andric        setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI);
25630815c53SDimitry Andric        if (VNI) {
25730815c53SDimitry Andric          if (TheVNI && TheVNI != VNI)
25830815c53SDimitry Andric            UniqueVNI = false;
25930815c53SDimitry Andric          TheVNI = VNI;
26030815c53SDimitry Andric        }
261b915e9e0SDimitry Andric        if (VNI || EP.second)
262b915e9e0SDimitry Andric          continue;
26330815c53SDimitry Andric 
26430815c53SDimitry Andric        // No, we need a live-in value for Pred as well
2655a5ac124SDimitry Andric        if (Pred != &UseMBB)
2664a16efa3SDimitry Andric          WorkList.push_back(Pred->getNumber());
26730815c53SDimitry Andric        else
2685a5ac124SDimitry Andric           // Loopback to UseMBB, so value is really live through.
2695a5ac124SDimitry Andric          Use = SlotIndex();
27030815c53SDimitry Andric     }
27130815c53SDimitry Andric   }
27230815c53SDimitry Andric 
27330815c53SDimitry Andric   LiveIn.clear();
2749df3605dSDimitry Andric   FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI);
275044eb2f6SDimitry Andric   if (!Undefs.empty() && FoundUndef)
276b915e9e0SDimitry Andric     UniqueVNI = false;
2774a16efa3SDimitry Andric 
2784a16efa3SDimitry Andric   // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
2794a16efa3SDimitry Andric   // neither require it. Skip the sorting overhead for small updates.
2804a16efa3SDimitry Andric   if (WorkList.size() > 4)
2814a16efa3SDimitry Andric     array_pod_sort(WorkList.begin(), WorkList.end());
2824a16efa3SDimitry Andric 
2834a16efa3SDimitry Andric   // If a unique reaching def was found, blit in the live ranges immediately.
2844a16efa3SDimitry Andric   if (UniqueVNI) {
2859df3605dSDimitry Andric     assert(TheVNI != nullptr && TheVNI != &UndefVNI);
286f8af5cf6SDimitry Andric     LiveRangeUpdater Updater(&LR);
287b915e9e0SDimitry Andric     for (unsigned BN : WorkList) {
2884a16efa3SDimitry Andric       SlotIndex Start, End;
289b915e9e0SDimitry Andric       std::tie(Start, End) = Indexes->getMBBRange(BN);
2905a5ac124SDimitry Andric       // Trim the live range in UseMBB.
291b915e9e0SDimitry Andric       if (BN == UseMBBNum && Use.isValid())
2925a5ac124SDimitry Andric         End = Use;
2934a16efa3SDimitry Andric       else
294b915e9e0SDimitry Andric         Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr);
2954a16efa3SDimitry Andric       Updater.add(Start, End, TheVNI);
2964a16efa3SDimitry Andric     }
2974a16efa3SDimitry Andric     return true;
2984a16efa3SDimitry Andric   }
2994a16efa3SDimitry Andric 
300b915e9e0SDimitry Andric   // Prepare the defined/undefined bit vectors.
3019df3605dSDimitry Andric   EntryInfoMap::iterator Entry;
3029df3605dSDimitry Andric   bool DidInsert;
3039df3605dSDimitry Andric   std::tie(Entry, DidInsert) = EntryInfos.insert(
3049df3605dSDimitry Andric       std::make_pair(&LR, std::make_pair(BitVector(), BitVector())));
3059df3605dSDimitry Andric   if (DidInsert) {
3069df3605dSDimitry Andric     // Initialize newly inserted entries.
307b915e9e0SDimitry Andric     unsigned N = MF->getNumBlockIDs();
3089df3605dSDimitry Andric     Entry->second.first.resize(N);
3099df3605dSDimitry Andric     Entry->second.second.resize(N);
310b915e9e0SDimitry Andric   }
3119df3605dSDimitry Andric   BitVector &DefOnEntry = Entry->second.first;
3129df3605dSDimitry Andric   BitVector &UndefOnEntry = Entry->second.second;
313b915e9e0SDimitry Andric 
3144a16efa3SDimitry Andric   // Multiple values were found, so transfer the work list to the LiveIn array
3154a16efa3SDimitry Andric   // where UpdateSSA will use it as a work list.
31630815c53SDimitry Andric   LiveIn.reserve(WorkList.size());
317b915e9e0SDimitry Andric   for (unsigned BN : WorkList) {
318b915e9e0SDimitry Andric     MachineBasicBlock *MBB = MF->getBlockNumbered(BN);
319044eb2f6SDimitry Andric     if (!Undefs.empty() &&
3209df3605dSDimitry Andric         !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
321b915e9e0SDimitry Andric       continue;
322f8af5cf6SDimitry Andric     addLiveInBlock(LR, DomTree->getNode(MBB));
3235a5ac124SDimitry Andric     if (MBB == &UseMBB)
3245a5ac124SDimitry Andric       LiveIn.back().Kill = Use;
3254a16efa3SDimitry Andric   }
32630815c53SDimitry Andric 
3274a16efa3SDimitry Andric   return false;
32830815c53SDimitry Andric }
32930815c53SDimitry Andric 
33030815c53SDimitry Andric // This is essentially the same iterative algorithm that SSAUpdater uses,
33130815c53SDimitry Andric // except we already have a dominator tree, so we don't have to recompute it.
updateSSA()33258b69754SDimitry Andric void LiveRangeCalc::updateSSA() {
33330815c53SDimitry Andric   assert(Indexes && "Missing SlotIndexes");
33430815c53SDimitry Andric   assert(DomTree && "Missing dominator tree");
33530815c53SDimitry Andric 
33630815c53SDimitry Andric   // Interate until convergence.
3379df3605dSDimitry Andric   bool Changed;
33830815c53SDimitry Andric   do {
3399df3605dSDimitry Andric     Changed = false;
34030815c53SDimitry Andric     // Propagate live-out values down the dominator tree, inserting phi-defs
34130815c53SDimitry Andric     // when necessary.
34267c32a98SDimitry Andric     for (LiveInBlock &I : LiveIn) {
34367c32a98SDimitry Andric       MachineDomTreeNode *Node = I.DomNode;
34430815c53SDimitry Andric       // Skip block if the live-in value has already been determined.
34530815c53SDimitry Andric       if (!Node)
34630815c53SDimitry Andric         continue;
34730815c53SDimitry Andric       MachineBasicBlock *MBB = Node->getBlock();
34830815c53SDimitry Andric       MachineDomTreeNode *IDom = Node->getIDom();
34930815c53SDimitry Andric       LiveOutPair IDomValue;
35030815c53SDimitry Andric 
35130815c53SDimitry Andric       // We need a live-in value to a block with no immediate dominator?
35230815c53SDimitry Andric       // This is probably an unreachable block that has survived somehow.
35330815c53SDimitry Andric       bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
35430815c53SDimitry Andric 
35530815c53SDimitry Andric       // IDom dominates all of our predecessors, but it may not be their
35630815c53SDimitry Andric       // immediate dominator. Check if any of them have live-out values that are
35730815c53SDimitry Andric       // properly dominated by IDom. If so, we need a phi-def here.
35830815c53SDimitry Andric       if (!needPHI) {
35967c32a98SDimitry Andric         IDomValue = Map[IDom->getBlock()];
36030815c53SDimitry Andric 
36130815c53SDimitry Andric         // Cache the DomTree node that defined the value.
3629df3605dSDimitry Andric         if (IDomValue.first && IDomValue.first != &UndefVNI &&
3639df3605dSDimitry Andric             !IDomValue.second) {
36467c32a98SDimitry Andric           Map[IDom->getBlock()].second = IDomValue.second =
36530815c53SDimitry Andric             DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
3669df3605dSDimitry Andric         }
36730815c53SDimitry Andric 
3689df3605dSDimitry Andric         for (MachineBasicBlock *Pred : MBB->predecessors()) {
3699df3605dSDimitry Andric           LiveOutPair &Value = Map[Pred];
37030815c53SDimitry Andric           if (!Value.first || Value.first == IDomValue.first)
37130815c53SDimitry Andric             continue;
3729df3605dSDimitry Andric           if (Value.first == &UndefVNI) {
3739df3605dSDimitry Andric             needPHI = true;
3749df3605dSDimitry Andric             break;
3759df3605dSDimitry Andric           }
37630815c53SDimitry Andric 
37730815c53SDimitry Andric           // Cache the DomTree node that defined the value.
37830815c53SDimitry Andric           if (!Value.second)
37930815c53SDimitry Andric             Value.second =
38030815c53SDimitry Andric               DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
38130815c53SDimitry Andric 
38230815c53SDimitry Andric           // This predecessor is carrying something other than IDomValue.
38330815c53SDimitry Andric           // It could be because IDomValue hasn't propagated yet, or it could be
38430815c53SDimitry Andric           // because MBB is in the dominance frontier of that value.
38530815c53SDimitry Andric           if (DomTree->dominates(IDom, Value.second)) {
38630815c53SDimitry Andric             needPHI = true;
38730815c53SDimitry Andric             break;
38830815c53SDimitry Andric           }
38930815c53SDimitry Andric         }
39030815c53SDimitry Andric       }
39130815c53SDimitry Andric 
39230815c53SDimitry Andric       // The value may be live-through even if Kill is set, as can happen when
39330815c53SDimitry Andric       // we are called from extendRange. In that case LiveOutSeen is true, and
39430815c53SDimitry Andric       // LiveOut indicates a foreign or missing value.
39567c32a98SDimitry Andric       LiveOutPair &LOP = Map[MBB];
39630815c53SDimitry Andric 
39730815c53SDimitry Andric       // Create a phi-def if required.
39830815c53SDimitry Andric       if (needPHI) {
3999df3605dSDimitry Andric         Changed = true;
40030815c53SDimitry Andric         assert(Alloc && "Need VNInfo allocator to create PHI-defs");
40130815c53SDimitry Andric         SlotIndex Start, End;
4025ca98fd9SDimitry Andric         std::tie(Start, End) = Indexes->getMBBRange(MBB);
40367c32a98SDimitry Andric         LiveRange &LR = I.LR;
404f8af5cf6SDimitry Andric         VNInfo *VNI = LR.getNextValue(Start, *Alloc);
40567c32a98SDimitry Andric         I.Value = VNI;
40630815c53SDimitry Andric         // This block is done, we know the final value.
40767c32a98SDimitry Andric         I.DomNode = nullptr;
40830815c53SDimitry Andric 
40967c32a98SDimitry Andric         // Add liveness since updateFromLiveIns now skips this node.
410b915e9e0SDimitry Andric         if (I.Kill.isValid()) {
411b915e9e0SDimitry Andric           if (VNI)
41267c32a98SDimitry Andric             LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
413b915e9e0SDimitry Andric         } else {
414b915e9e0SDimitry Andric           if (VNI)
415f8af5cf6SDimitry Andric             LR.addSegment(LiveInterval::Segment(Start, End, VNI));
41630815c53SDimitry Andric           LOP = LiveOutPair(VNI, Node);
41730815c53SDimitry Andric         }
4189df3605dSDimitry Andric       } else if (IDomValue.first && IDomValue.first != &UndefVNI) {
41930815c53SDimitry Andric         // No phi-def here. Remember incoming value.
42067c32a98SDimitry Andric         I.Value = IDomValue.first;
42130815c53SDimitry Andric 
42230815c53SDimitry Andric         // If the IDomValue is killed in the block, don't propagate through.
42367c32a98SDimitry Andric         if (I.Kill.isValid())
42430815c53SDimitry Andric           continue;
42530815c53SDimitry Andric 
42630815c53SDimitry Andric         // Propagate IDomValue if it isn't killed:
42730815c53SDimitry Andric         // MBB is live-out and doesn't define its own value.
42830815c53SDimitry Andric         if (LOP.first == IDomValue.first)
42930815c53SDimitry Andric           continue;
4309df3605dSDimitry Andric         Changed = true;
43130815c53SDimitry Andric         LOP = IDomValue;
43230815c53SDimitry Andric       }
43330815c53SDimitry Andric     }
4349df3605dSDimitry Andric   } while (Changed);
43530815c53SDimitry Andric }
436eb11fae6SDimitry Andric 
isJointlyDominated(const MachineBasicBlock * MBB,ArrayRef<SlotIndex> Defs,const SlotIndexes & Indexes)437eb11fae6SDimitry Andric bool LiveRangeCalc::isJointlyDominated(const MachineBasicBlock *MBB,
438eb11fae6SDimitry Andric                                        ArrayRef<SlotIndex> Defs,
439eb11fae6SDimitry Andric                                        const SlotIndexes &Indexes) {
440eb11fae6SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
441eb11fae6SDimitry Andric   BitVector DefBlocks(MF.getNumBlockIDs());
442eb11fae6SDimitry Andric   for (SlotIndex I : Defs)
443eb11fae6SDimitry Andric     DefBlocks.set(Indexes.getMBBFromIndex(I)->getNumber());
444eb11fae6SDimitry Andric 
445eb11fae6SDimitry Andric   SetVector<unsigned> PredQueue;
446eb11fae6SDimitry Andric   PredQueue.insert(MBB->getNumber());
447eb11fae6SDimitry Andric   for (unsigned i = 0; i != PredQueue.size(); ++i) {
448eb11fae6SDimitry Andric     unsigned BN = PredQueue[i];
449eb11fae6SDimitry Andric     if (DefBlocks[BN])
450eb11fae6SDimitry Andric       return true;
451eb11fae6SDimitry Andric     const MachineBasicBlock *B = MF.getBlockNumbered(BN);
452eb11fae6SDimitry Andric     for (const MachineBasicBlock *P : B->predecessors())
453eb11fae6SDimitry Andric       PredQueue.insert(P->getNumber());
454eb11fae6SDimitry Andric   }
455eb11fae6SDimitry Andric   return false;
456eb11fae6SDimitry Andric }
457