xref: /openbsd-src/gnu/llvm/llvm/lib/CodeGen/LiveRangeCalc.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
1097a140dSpatrick //===- LiveRangeCalc.cpp - Calculate live ranges -------------------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // Implementation of the LiveRangeCalc class.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
1309467b48Spatrick #include "llvm/CodeGen/LiveRangeCalc.h"
1409467b48Spatrick #include "llvm/ADT/BitVector.h"
1509467b48Spatrick #include "llvm/ADT/STLExtras.h"
1609467b48Spatrick #include "llvm/ADT/SetVector.h"
1709467b48Spatrick #include "llvm/ADT/SmallVector.h"
1809467b48Spatrick #include "llvm/CodeGen/LiveInterval.h"
1909467b48Spatrick #include "llvm/CodeGen/MachineBasicBlock.h"
2009467b48Spatrick #include "llvm/CodeGen/MachineDominators.h"
2109467b48Spatrick #include "llvm/CodeGen/MachineFunction.h"
2209467b48Spatrick #include "llvm/CodeGen/MachineInstr.h"
2309467b48Spatrick #include "llvm/CodeGen/MachineRegisterInfo.h"
2409467b48Spatrick #include "llvm/CodeGen/SlotIndexes.h"
2509467b48Spatrick #include "llvm/CodeGen/TargetRegisterInfo.h"
2609467b48Spatrick #include "llvm/Support/ErrorHandling.h"
2709467b48Spatrick #include "llvm/Support/raw_ostream.h"
2809467b48Spatrick #include <algorithm>
2909467b48Spatrick #include <cassert>
3009467b48Spatrick #include <iterator>
3109467b48Spatrick #include <tuple>
3209467b48Spatrick #include <utility>
3309467b48Spatrick 
3409467b48Spatrick using namespace llvm;
3509467b48Spatrick 
3609467b48Spatrick #define DEBUG_TYPE "regalloc"
3709467b48Spatrick 
3809467b48Spatrick // Reserve an address that indicates a value that is known to be "undef".
3909467b48Spatrick static VNInfo UndefVNI(0xbad, SlotIndex());
4009467b48Spatrick 
resetLiveOutMap()4109467b48Spatrick void LiveRangeCalc::resetLiveOutMap() {
4209467b48Spatrick   unsigned NumBlocks = MF->getNumBlockIDs();
4309467b48Spatrick   Seen.clear();
4409467b48Spatrick   Seen.resize(NumBlocks);
4509467b48Spatrick   EntryInfos.clear();
4609467b48Spatrick   Map.resize(NumBlocks);
4709467b48Spatrick }
4809467b48Spatrick 
reset(const MachineFunction * mf,SlotIndexes * SI,MachineDominatorTree * MDT,VNInfo::Allocator * VNIA)4909467b48Spatrick void LiveRangeCalc::reset(const MachineFunction *mf,
5009467b48Spatrick                           SlotIndexes *SI,
5109467b48Spatrick                           MachineDominatorTree *MDT,
5209467b48Spatrick                           VNInfo::Allocator *VNIA) {
5309467b48Spatrick   MF = mf;
5409467b48Spatrick   MRI = &MF->getRegInfo();
5509467b48Spatrick   Indexes = SI;
5609467b48Spatrick   DomTree = MDT;
5709467b48Spatrick   Alloc = VNIA;
5809467b48Spatrick   resetLiveOutMap();
5909467b48Spatrick   LiveIn.clear();
6009467b48Spatrick }
6109467b48Spatrick 
updateFromLiveIns()6209467b48Spatrick void LiveRangeCalc::updateFromLiveIns() {
6309467b48Spatrick   LiveRangeUpdater Updater;
6409467b48Spatrick   for (const LiveInBlock &I : LiveIn) {
6509467b48Spatrick     if (!I.DomNode)
6609467b48Spatrick       continue;
6709467b48Spatrick     MachineBasicBlock *MBB = I.DomNode->getBlock();
6809467b48Spatrick     assert(I.Value && "No live-in value found");
6909467b48Spatrick     SlotIndex Start, End;
7009467b48Spatrick     std::tie(Start, End) = Indexes->getMBBRange(MBB);
7109467b48Spatrick 
7209467b48Spatrick     if (I.Kill.isValid())
7309467b48Spatrick       // Value is killed inside this block.
7409467b48Spatrick       End = I.Kill;
7509467b48Spatrick     else {
7609467b48Spatrick       // The value is live-through, update LiveOut as well.
7709467b48Spatrick       // Defer the Domtree lookup until it is needed.
7809467b48Spatrick       assert(Seen.test(MBB->getNumber()));
7909467b48Spatrick       Map[MBB] = LiveOutPair(I.Value, nullptr);
8009467b48Spatrick     }
8109467b48Spatrick     Updater.setDest(&I.LR);
8209467b48Spatrick     Updater.add(Start, End, I.Value);
8309467b48Spatrick   }
8409467b48Spatrick   LiveIn.clear();
8509467b48Spatrick }
8609467b48Spatrick 
extend(LiveRange & LR,SlotIndex Use,unsigned PhysReg,ArrayRef<SlotIndex> Undefs)8709467b48Spatrick void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
8809467b48Spatrick                            ArrayRef<SlotIndex> Undefs) {
8909467b48Spatrick   assert(Use.isValid() && "Invalid SlotIndex");
9009467b48Spatrick   assert(Indexes && "Missing SlotIndexes");
9109467b48Spatrick   assert(DomTree && "Missing dominator tree");
9209467b48Spatrick 
9309467b48Spatrick   MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
9409467b48Spatrick   assert(UseMBB && "No MBB at Use");
9509467b48Spatrick 
9609467b48Spatrick   // Is there a def in the same MBB we can extend?
9709467b48Spatrick   auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use);
9809467b48Spatrick   if (EP.first != nullptr || EP.second)
9909467b48Spatrick     return;
10009467b48Spatrick 
10109467b48Spatrick   // Find the single reaching def, or determine if Use is jointly dominated by
10209467b48Spatrick   // multiple values, and we may need to create even more phi-defs to preserve
10309467b48Spatrick   // VNInfo SSA form.  Perform a search for all predecessor blocks where we
10409467b48Spatrick   // know the dominating VNInfo.
10509467b48Spatrick   if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
10609467b48Spatrick     return;
10709467b48Spatrick 
10809467b48Spatrick   // When there were multiple different values, we may need new PHIs.
10909467b48Spatrick   calculateValues();
11009467b48Spatrick }
11109467b48Spatrick 
11209467b48Spatrick // This function is called by a client after using the low-level API to add
11309467b48Spatrick // live-out and live-in blocks.  The unique value optimization is not
11409467b48Spatrick // available, SplitEditor::transferValues handles that case directly anyway.
calculateValues()11509467b48Spatrick void LiveRangeCalc::calculateValues() {
11609467b48Spatrick   assert(Indexes && "Missing SlotIndexes");
11709467b48Spatrick   assert(DomTree && "Missing dominator tree");
11809467b48Spatrick   updateSSA();
11909467b48Spatrick   updateFromLiveIns();
12009467b48Spatrick }
12109467b48Spatrick 
isDefOnEntry(LiveRange & LR,ArrayRef<SlotIndex> Undefs,MachineBasicBlock & MBB,BitVector & DefOnEntry,BitVector & UndefOnEntry)12209467b48Spatrick bool LiveRangeCalc::isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
12309467b48Spatrick                                  MachineBasicBlock &MBB, BitVector &DefOnEntry,
12409467b48Spatrick                                  BitVector &UndefOnEntry) {
12509467b48Spatrick   unsigned BN = MBB.getNumber();
12609467b48Spatrick   if (DefOnEntry[BN])
12709467b48Spatrick     return true;
12809467b48Spatrick   if (UndefOnEntry[BN])
12909467b48Spatrick     return false;
13009467b48Spatrick 
13109467b48Spatrick   auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool {
13209467b48Spatrick     for (MachineBasicBlock *S : B.successors())
13309467b48Spatrick       DefOnEntry[S->getNumber()] = true;
13409467b48Spatrick     DefOnEntry[BN] = true;
13509467b48Spatrick     return true;
13609467b48Spatrick   };
13709467b48Spatrick 
13809467b48Spatrick   SetVector<unsigned> WorkList;
13909467b48Spatrick   // Checking if the entry of MBB is reached by some def: add all predecessors
14009467b48Spatrick   // that are potentially defined-on-exit to the work list.
14109467b48Spatrick   for (MachineBasicBlock *P : MBB.predecessors())
14209467b48Spatrick     WorkList.insert(P->getNumber());
14309467b48Spatrick 
14409467b48Spatrick   for (unsigned i = 0; i != WorkList.size(); ++i) {
14509467b48Spatrick     // Determine if the exit from the block is reached by some def.
14609467b48Spatrick     unsigned N = WorkList[i];
14709467b48Spatrick     MachineBasicBlock &B = *MF->getBlockNumbered(N);
14809467b48Spatrick     if (Seen[N]) {
14909467b48Spatrick       const LiveOutPair &LOB = Map[&B];
15009467b48Spatrick       if (LOB.first != nullptr && LOB.first != &UndefVNI)
15109467b48Spatrick         return MarkDefined(B);
15209467b48Spatrick     }
15309467b48Spatrick     SlotIndex Begin, End;
15409467b48Spatrick     std::tie(Begin, End) = Indexes->getMBBRange(&B);
15509467b48Spatrick     // Treat End as not belonging to B.
15609467b48Spatrick     // If LR has a segment S that starts at the next block, i.e. [End, ...),
15709467b48Spatrick     // std::upper_bound will return the segment following S. Instead,
15809467b48Spatrick     // S should be treated as the first segment that does not overlap B.
159*73471bf0Spatrick     LiveRange::iterator UB = upper_bound(LR, End.getPrevSlot());
16009467b48Spatrick     if (UB != LR.begin()) {
16109467b48Spatrick       LiveRange::Segment &Seg = *std::prev(UB);
16209467b48Spatrick       if (Seg.end > Begin) {
16309467b48Spatrick         // There is a segment that overlaps B. If the range is not explicitly
16409467b48Spatrick         // undefined between the end of the segment and the end of the block,
16509467b48Spatrick         // treat the block as defined on exit. If it is, go to the next block
16609467b48Spatrick         // on the work list.
16709467b48Spatrick         if (LR.isUndefIn(Undefs, Seg.end, End))
16809467b48Spatrick           continue;
16909467b48Spatrick         return MarkDefined(B);
17009467b48Spatrick       }
17109467b48Spatrick     }
17209467b48Spatrick 
17309467b48Spatrick     // No segment overlaps with this block. If this block is not defined on
17409467b48Spatrick     // entry, or it undefines the range, do not process its predecessors.
17509467b48Spatrick     if (UndefOnEntry[N] || LR.isUndefIn(Undefs, Begin, End)) {
17609467b48Spatrick       UndefOnEntry[N] = true;
17709467b48Spatrick       continue;
17809467b48Spatrick     }
17909467b48Spatrick     if (DefOnEntry[N])
18009467b48Spatrick       return MarkDefined(B);
18109467b48Spatrick 
18209467b48Spatrick     // Still don't know: add all predecessors to the work list.
18309467b48Spatrick     for (MachineBasicBlock *P : B.predecessors())
18409467b48Spatrick       WorkList.insert(P->getNumber());
18509467b48Spatrick   }
18609467b48Spatrick 
18709467b48Spatrick   UndefOnEntry[BN] = true;
18809467b48Spatrick   return false;
18909467b48Spatrick }
19009467b48Spatrick 
findReachingDefs(LiveRange & LR,MachineBasicBlock & UseMBB,SlotIndex Use,unsigned PhysReg,ArrayRef<SlotIndex> Undefs)19109467b48Spatrick bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
19209467b48Spatrick                                      SlotIndex Use, unsigned PhysReg,
19309467b48Spatrick                                      ArrayRef<SlotIndex> Undefs) {
19409467b48Spatrick   unsigned UseMBBNum = UseMBB.getNumber();
19509467b48Spatrick 
19609467b48Spatrick   // Block numbers where LR should be live-in.
19709467b48Spatrick   SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
19809467b48Spatrick 
19909467b48Spatrick   // Remember if we have seen more than one value.
20009467b48Spatrick   bool UniqueVNI = true;
20109467b48Spatrick   VNInfo *TheVNI = nullptr;
20209467b48Spatrick 
20309467b48Spatrick   bool FoundUndef = false;
20409467b48Spatrick 
20509467b48Spatrick   // Using Seen as a visited set, perform a BFS for all reaching defs.
20609467b48Spatrick   for (unsigned i = 0; i != WorkList.size(); ++i) {
20709467b48Spatrick     MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
20809467b48Spatrick 
20909467b48Spatrick #ifndef NDEBUG
21009467b48Spatrick     if (MBB->pred_empty()) {
21109467b48Spatrick       MBB->getParent()->verify();
21209467b48Spatrick       errs() << "Use of " << printReg(PhysReg, MRI->getTargetRegisterInfo())
21309467b48Spatrick              << " does not have a corresponding definition on every path:\n";
21409467b48Spatrick       const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
21509467b48Spatrick       if (MI != nullptr)
21609467b48Spatrick         errs() << Use << " " << *MI;
21709467b48Spatrick       report_fatal_error("Use not jointly dominated by defs.");
21809467b48Spatrick     }
21909467b48Spatrick 
22009467b48Spatrick     if (Register::isPhysicalRegister(PhysReg) && !MBB->isLiveIn(PhysReg)) {
22109467b48Spatrick       MBB->getParent()->verify();
22209467b48Spatrick       const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
22309467b48Spatrick       errs() << "The register " << printReg(PhysReg, TRI)
22409467b48Spatrick              << " needs to be live in to " << printMBBReference(*MBB)
22509467b48Spatrick              << ", but is missing from the live-in list.\n";
22609467b48Spatrick       report_fatal_error("Invalid global physical register");
22709467b48Spatrick     }
22809467b48Spatrick #endif
22909467b48Spatrick     FoundUndef |= MBB->pred_empty();
23009467b48Spatrick 
23109467b48Spatrick     for (MachineBasicBlock *Pred : MBB->predecessors()) {
23209467b48Spatrick        // Is this a known live-out block?
23309467b48Spatrick        if (Seen.test(Pred->getNumber())) {
23409467b48Spatrick          if (VNInfo *VNI = Map[Pred].first) {
23509467b48Spatrick            if (TheVNI && TheVNI != VNI)
23609467b48Spatrick              UniqueVNI = false;
23709467b48Spatrick            TheVNI = VNI;
23809467b48Spatrick          }
23909467b48Spatrick          continue;
24009467b48Spatrick        }
24109467b48Spatrick 
24209467b48Spatrick        SlotIndex Start, End;
24309467b48Spatrick        std::tie(Start, End) = Indexes->getMBBRange(Pred);
24409467b48Spatrick 
24509467b48Spatrick        // First time we see Pred.  Try to determine the live-out value, but set
24609467b48Spatrick        // it as null if Pred is live-through with an unknown value.
24709467b48Spatrick        auto EP = LR.extendInBlock(Undefs, Start, End);
24809467b48Spatrick        VNInfo *VNI = EP.first;
24909467b48Spatrick        FoundUndef |= EP.second;
25009467b48Spatrick        setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI);
25109467b48Spatrick        if (VNI) {
25209467b48Spatrick          if (TheVNI && TheVNI != VNI)
25309467b48Spatrick            UniqueVNI = false;
25409467b48Spatrick          TheVNI = VNI;
25509467b48Spatrick        }
25609467b48Spatrick        if (VNI || EP.second)
25709467b48Spatrick          continue;
25809467b48Spatrick 
25909467b48Spatrick        // No, we need a live-in value for Pred as well
26009467b48Spatrick        if (Pred != &UseMBB)
26109467b48Spatrick          WorkList.push_back(Pred->getNumber());
26209467b48Spatrick        else
26309467b48Spatrick           // Loopback to UseMBB, so value is really live through.
26409467b48Spatrick          Use = SlotIndex();
26509467b48Spatrick     }
26609467b48Spatrick   }
26709467b48Spatrick 
26809467b48Spatrick   LiveIn.clear();
26909467b48Spatrick   FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI);
27009467b48Spatrick   if (!Undefs.empty() && FoundUndef)
27109467b48Spatrick     UniqueVNI = false;
27209467b48Spatrick 
27309467b48Spatrick   // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
27409467b48Spatrick   // neither require it. Skip the sorting overhead for small updates.
27509467b48Spatrick   if (WorkList.size() > 4)
27609467b48Spatrick     array_pod_sort(WorkList.begin(), WorkList.end());
27709467b48Spatrick 
27809467b48Spatrick   // If a unique reaching def was found, blit in the live ranges immediately.
27909467b48Spatrick   if (UniqueVNI) {
28009467b48Spatrick     assert(TheVNI != nullptr && TheVNI != &UndefVNI);
28109467b48Spatrick     LiveRangeUpdater Updater(&LR);
28209467b48Spatrick     for (unsigned BN : WorkList) {
28309467b48Spatrick       SlotIndex Start, End;
28409467b48Spatrick       std::tie(Start, End) = Indexes->getMBBRange(BN);
28509467b48Spatrick       // Trim the live range in UseMBB.
28609467b48Spatrick       if (BN == UseMBBNum && Use.isValid())
28709467b48Spatrick         End = Use;
28809467b48Spatrick       else
28909467b48Spatrick         Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr);
29009467b48Spatrick       Updater.add(Start, End, TheVNI);
29109467b48Spatrick     }
29209467b48Spatrick     return true;
29309467b48Spatrick   }
29409467b48Spatrick 
29509467b48Spatrick   // Prepare the defined/undefined bit vectors.
29609467b48Spatrick   EntryInfoMap::iterator Entry;
29709467b48Spatrick   bool DidInsert;
29809467b48Spatrick   std::tie(Entry, DidInsert) = EntryInfos.insert(
29909467b48Spatrick       std::make_pair(&LR, std::make_pair(BitVector(), BitVector())));
30009467b48Spatrick   if (DidInsert) {
30109467b48Spatrick     // Initialize newly inserted entries.
30209467b48Spatrick     unsigned N = MF->getNumBlockIDs();
30309467b48Spatrick     Entry->second.first.resize(N);
30409467b48Spatrick     Entry->second.second.resize(N);
30509467b48Spatrick   }
30609467b48Spatrick   BitVector &DefOnEntry = Entry->second.first;
30709467b48Spatrick   BitVector &UndefOnEntry = Entry->second.second;
30809467b48Spatrick 
30909467b48Spatrick   // Multiple values were found, so transfer the work list to the LiveIn array
31009467b48Spatrick   // where UpdateSSA will use it as a work list.
31109467b48Spatrick   LiveIn.reserve(WorkList.size());
31209467b48Spatrick   for (unsigned BN : WorkList) {
31309467b48Spatrick     MachineBasicBlock *MBB = MF->getBlockNumbered(BN);
31409467b48Spatrick     if (!Undefs.empty() &&
31509467b48Spatrick         !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
31609467b48Spatrick       continue;
31709467b48Spatrick     addLiveInBlock(LR, DomTree->getNode(MBB));
31809467b48Spatrick     if (MBB == &UseMBB)
31909467b48Spatrick       LiveIn.back().Kill = Use;
32009467b48Spatrick   }
32109467b48Spatrick 
32209467b48Spatrick   return false;
32309467b48Spatrick }
32409467b48Spatrick 
32509467b48Spatrick // This is essentially the same iterative algorithm that SSAUpdater uses,
32609467b48Spatrick // except we already have a dominator tree, so we don't have to recompute it.
updateSSA()32709467b48Spatrick void LiveRangeCalc::updateSSA() {
32809467b48Spatrick   assert(Indexes && "Missing SlotIndexes");
32909467b48Spatrick   assert(DomTree && "Missing dominator tree");
33009467b48Spatrick 
33109467b48Spatrick   // Interate until convergence.
33209467b48Spatrick   bool Changed;
33309467b48Spatrick   do {
33409467b48Spatrick     Changed = false;
33509467b48Spatrick     // Propagate live-out values down the dominator tree, inserting phi-defs
33609467b48Spatrick     // when necessary.
33709467b48Spatrick     for (LiveInBlock &I : LiveIn) {
33809467b48Spatrick       MachineDomTreeNode *Node = I.DomNode;
33909467b48Spatrick       // Skip block if the live-in value has already been determined.
34009467b48Spatrick       if (!Node)
34109467b48Spatrick         continue;
34209467b48Spatrick       MachineBasicBlock *MBB = Node->getBlock();
34309467b48Spatrick       MachineDomTreeNode *IDom = Node->getIDom();
34409467b48Spatrick       LiveOutPair IDomValue;
34509467b48Spatrick 
34609467b48Spatrick       // We need a live-in value to a block with no immediate dominator?
34709467b48Spatrick       // This is probably an unreachable block that has survived somehow.
34809467b48Spatrick       bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
34909467b48Spatrick 
35009467b48Spatrick       // IDom dominates all of our predecessors, but it may not be their
35109467b48Spatrick       // immediate dominator. Check if any of them have live-out values that are
35209467b48Spatrick       // properly dominated by IDom. If so, we need a phi-def here.
35309467b48Spatrick       if (!needPHI) {
35409467b48Spatrick         IDomValue = Map[IDom->getBlock()];
35509467b48Spatrick 
35609467b48Spatrick         // Cache the DomTree node that defined the value.
35709467b48Spatrick         if (IDomValue.first && IDomValue.first != &UndefVNI &&
35809467b48Spatrick             !IDomValue.second) {
35909467b48Spatrick           Map[IDom->getBlock()].second = IDomValue.second =
36009467b48Spatrick             DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
36109467b48Spatrick         }
36209467b48Spatrick 
36309467b48Spatrick         for (MachineBasicBlock *Pred : MBB->predecessors()) {
36409467b48Spatrick           LiveOutPair &Value = Map[Pred];
36509467b48Spatrick           if (!Value.first || Value.first == IDomValue.first)
36609467b48Spatrick             continue;
36709467b48Spatrick           if (Value.first == &UndefVNI) {
36809467b48Spatrick             needPHI = true;
36909467b48Spatrick             break;
37009467b48Spatrick           }
37109467b48Spatrick 
37209467b48Spatrick           // Cache the DomTree node that defined the value.
37309467b48Spatrick           if (!Value.second)
37409467b48Spatrick             Value.second =
37509467b48Spatrick               DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
37609467b48Spatrick 
37709467b48Spatrick           // This predecessor is carrying something other than IDomValue.
37809467b48Spatrick           // It could be because IDomValue hasn't propagated yet, or it could be
37909467b48Spatrick           // because MBB is in the dominance frontier of that value.
38009467b48Spatrick           if (DomTree->dominates(IDom, Value.second)) {
38109467b48Spatrick             needPHI = true;
38209467b48Spatrick             break;
38309467b48Spatrick           }
38409467b48Spatrick         }
38509467b48Spatrick       }
38609467b48Spatrick 
38709467b48Spatrick       // The value may be live-through even if Kill is set, as can happen when
38809467b48Spatrick       // we are called from extendRange. In that case LiveOutSeen is true, and
38909467b48Spatrick       // LiveOut indicates a foreign or missing value.
39009467b48Spatrick       LiveOutPair &LOP = Map[MBB];
39109467b48Spatrick 
39209467b48Spatrick       // Create a phi-def if required.
39309467b48Spatrick       if (needPHI) {
39409467b48Spatrick         Changed = true;
39509467b48Spatrick         assert(Alloc && "Need VNInfo allocator to create PHI-defs");
39609467b48Spatrick         SlotIndex Start, End;
39709467b48Spatrick         std::tie(Start, End) = Indexes->getMBBRange(MBB);
39809467b48Spatrick         LiveRange &LR = I.LR;
39909467b48Spatrick         VNInfo *VNI = LR.getNextValue(Start, *Alloc);
40009467b48Spatrick         I.Value = VNI;
40109467b48Spatrick         // This block is done, we know the final value.
40209467b48Spatrick         I.DomNode = nullptr;
40309467b48Spatrick 
40409467b48Spatrick         // Add liveness since updateFromLiveIns now skips this node.
40509467b48Spatrick         if (I.Kill.isValid()) {
40609467b48Spatrick           if (VNI)
40709467b48Spatrick             LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
40809467b48Spatrick         } else {
40909467b48Spatrick           if (VNI)
41009467b48Spatrick             LR.addSegment(LiveInterval::Segment(Start, End, VNI));
41109467b48Spatrick           LOP = LiveOutPair(VNI, Node);
41209467b48Spatrick         }
41309467b48Spatrick       } else if (IDomValue.first && IDomValue.first != &UndefVNI) {
41409467b48Spatrick         // No phi-def here. Remember incoming value.
41509467b48Spatrick         I.Value = IDomValue.first;
41609467b48Spatrick 
41709467b48Spatrick         // If the IDomValue is killed in the block, don't propagate through.
41809467b48Spatrick         if (I.Kill.isValid())
41909467b48Spatrick           continue;
42009467b48Spatrick 
42109467b48Spatrick         // Propagate IDomValue if it isn't killed:
42209467b48Spatrick         // MBB is live-out and doesn't define its own value.
42309467b48Spatrick         if (LOP.first == IDomValue.first)
42409467b48Spatrick           continue;
42509467b48Spatrick         Changed = true;
42609467b48Spatrick         LOP = IDomValue;
42709467b48Spatrick       }
42809467b48Spatrick     }
42909467b48Spatrick   } while (Changed);
43009467b48Spatrick }
43109467b48Spatrick 
isJointlyDominated(const MachineBasicBlock * MBB,ArrayRef<SlotIndex> Defs,const SlotIndexes & Indexes)43209467b48Spatrick bool LiveRangeCalc::isJointlyDominated(const MachineBasicBlock *MBB,
43309467b48Spatrick                                        ArrayRef<SlotIndex> Defs,
43409467b48Spatrick                                        const SlotIndexes &Indexes) {
43509467b48Spatrick   const MachineFunction &MF = *MBB->getParent();
43609467b48Spatrick   BitVector DefBlocks(MF.getNumBlockIDs());
43709467b48Spatrick   for (SlotIndex I : Defs)
43809467b48Spatrick     DefBlocks.set(Indexes.getMBBFromIndex(I)->getNumber());
43909467b48Spatrick 
44009467b48Spatrick   SetVector<unsigned> PredQueue;
44109467b48Spatrick   PredQueue.insert(MBB->getNumber());
44209467b48Spatrick   for (unsigned i = 0; i != PredQueue.size(); ++i) {
44309467b48Spatrick     unsigned BN = PredQueue[i];
44409467b48Spatrick     if (DefBlocks[BN])
44509467b48Spatrick       return true;
44609467b48Spatrick     const MachineBasicBlock *B = MF.getBlockNumbered(BN);
44709467b48Spatrick     for (const MachineBasicBlock *P : B->predecessors())
44809467b48Spatrick       PredQueue.insert(P->getNumber());
44909467b48Spatrick   }
45009467b48Spatrick   return false;
45109467b48Spatrick }
452