xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/LiveIntervalCalc.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
15ffd83dbSDimitry Andric //===- LiveIntervalCalc.cpp - Calculate live interval --------------------===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric //
95ffd83dbSDimitry Andric // Implementation of the LiveIntervalCalc class.
105ffd83dbSDimitry Andric //
115ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
125ffd83dbSDimitry Andric 
135ffd83dbSDimitry Andric #include "llvm/CodeGen/LiveIntervalCalc.h"
145ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h"
155ffd83dbSDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
165ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
175ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
185ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
195ffd83dbSDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
205ffd83dbSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
215ffd83dbSDimitry Andric #include "llvm/MC/LaneBitmask.h"
225ffd83dbSDimitry Andric #include "llvm/Support/ErrorHandling.h"
235ffd83dbSDimitry Andric #include <cassert>
245ffd83dbSDimitry Andric 
255ffd83dbSDimitry Andric using namespace llvm;
265ffd83dbSDimitry Andric 
275ffd83dbSDimitry Andric #define DEBUG_TYPE "regalloc"
285ffd83dbSDimitry Andric 
295ffd83dbSDimitry Andric // Reserve an address that indicates a value that is known to be "undef".
305ffd83dbSDimitry Andric static VNInfo UndefVNI(0xbad, SlotIndex());
315ffd83dbSDimitry Andric 
createDeadDef(SlotIndexes & Indexes,VNInfo::Allocator & Alloc,LiveRange & LR,const MachineOperand & MO)325ffd83dbSDimitry Andric static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
335ffd83dbSDimitry Andric                           LiveRange &LR, const MachineOperand &MO) {
345ffd83dbSDimitry Andric   const MachineInstr &MI = *MO.getParent();
355ffd83dbSDimitry Andric   SlotIndex DefIdx =
365ffd83dbSDimitry Andric       Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
375ffd83dbSDimitry Andric 
385ffd83dbSDimitry Andric   // Create the def in LR. This may find an existing def.
395ffd83dbSDimitry Andric   LR.createDeadDef(DefIdx, Alloc);
405ffd83dbSDimitry Andric }
415ffd83dbSDimitry Andric 
calculate(LiveInterval & LI,bool TrackSubRegs)425ffd83dbSDimitry Andric void LiveIntervalCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
435ffd83dbSDimitry Andric   const MachineRegisterInfo *MRI = getRegInfo();
445ffd83dbSDimitry Andric   SlotIndexes *Indexes = getIndexes();
455ffd83dbSDimitry Andric   VNInfo::Allocator *Alloc = getVNAlloc();
465ffd83dbSDimitry Andric 
475ffd83dbSDimitry Andric   assert(MRI && Indexes && "call reset() first");
485ffd83dbSDimitry Andric 
495ffd83dbSDimitry Andric   // Step 1: Create minimal live segments for every definition of Reg.
505ffd83dbSDimitry Andric   // Visit all def operands. If the same instruction has multiple defs of Reg,
515ffd83dbSDimitry Andric   // createDeadDef() will deduplicate.
525ffd83dbSDimitry Andric   const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
53*bdd1243dSDimitry Andric   Register Reg = LI.reg();
545ffd83dbSDimitry Andric   for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
555ffd83dbSDimitry Andric     if (!MO.isDef() && !MO.readsReg())
565ffd83dbSDimitry Andric       continue;
575ffd83dbSDimitry Andric 
585ffd83dbSDimitry Andric     unsigned SubReg = MO.getSubReg();
595ffd83dbSDimitry Andric     if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
605ffd83dbSDimitry Andric       LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
615ffd83dbSDimitry Andric                                         : MRI->getMaxLaneMaskForVReg(Reg);
625ffd83dbSDimitry Andric       // If this is the first time we see a subregister def, initialize
635ffd83dbSDimitry Andric       // subranges by creating a copy of the main range.
645ffd83dbSDimitry Andric       if (!LI.hasSubRanges() && !LI.empty()) {
655ffd83dbSDimitry Andric         LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
665ffd83dbSDimitry Andric         LI.createSubRangeFrom(*Alloc, ClassMask, LI);
675ffd83dbSDimitry Andric       }
685ffd83dbSDimitry Andric 
695ffd83dbSDimitry Andric       LI.refineSubRanges(
705ffd83dbSDimitry Andric           *Alloc, SubMask,
715ffd83dbSDimitry Andric           [&MO, Indexes, Alloc](LiveInterval::SubRange &SR) {
725ffd83dbSDimitry Andric             if (MO.isDef())
735ffd83dbSDimitry Andric               createDeadDef(*Indexes, *Alloc, SR, MO);
745ffd83dbSDimitry Andric           },
755ffd83dbSDimitry Andric           *Indexes, TRI);
765ffd83dbSDimitry Andric     }
775ffd83dbSDimitry Andric 
785ffd83dbSDimitry Andric     // Create the def in the main liverange. We do not have to do this if
795ffd83dbSDimitry Andric     // subranges are tracked as we recreate the main range later in this case.
805ffd83dbSDimitry Andric     if (MO.isDef() && !LI.hasSubRanges())
815ffd83dbSDimitry Andric       createDeadDef(*Indexes, *Alloc, LI, MO);
825ffd83dbSDimitry Andric   }
835ffd83dbSDimitry Andric 
845ffd83dbSDimitry Andric   // We may have created empty live ranges for partially undefined uses, we
855ffd83dbSDimitry Andric   // can't keep them because we won't find defs in them later.
865ffd83dbSDimitry Andric   LI.removeEmptySubRanges();
875ffd83dbSDimitry Andric 
885ffd83dbSDimitry Andric   const MachineFunction *MF = getMachineFunction();
895ffd83dbSDimitry Andric   MachineDominatorTree *DomTree = getDomTree();
905ffd83dbSDimitry Andric   // Step 2: Extend live segments to all uses, constructing SSA form as
915ffd83dbSDimitry Andric   // necessary.
925ffd83dbSDimitry Andric   if (LI.hasSubRanges()) {
935ffd83dbSDimitry Andric     for (LiveInterval::SubRange &S : LI.subranges()) {
945ffd83dbSDimitry Andric       LiveIntervalCalc SubLIC;
955ffd83dbSDimitry Andric       SubLIC.reset(MF, Indexes, DomTree, Alloc);
965ffd83dbSDimitry Andric       SubLIC.extendToUses(S, Reg, S.LaneMask, &LI);
975ffd83dbSDimitry Andric     }
985ffd83dbSDimitry Andric     LI.clear();
995ffd83dbSDimitry Andric     constructMainRangeFromSubranges(LI);
1005ffd83dbSDimitry Andric   } else {
1015ffd83dbSDimitry Andric     resetLiveOutMap();
1025ffd83dbSDimitry Andric     extendToUses(LI, Reg, LaneBitmask::getAll());
1035ffd83dbSDimitry Andric   }
1045ffd83dbSDimitry Andric }
1055ffd83dbSDimitry Andric 
constructMainRangeFromSubranges(LiveInterval & LI)1065ffd83dbSDimitry Andric void LiveIntervalCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
1075ffd83dbSDimitry Andric   // First create dead defs at all defs found in subranges.
1085ffd83dbSDimitry Andric   LiveRange &MainRange = LI;
1095ffd83dbSDimitry Andric   assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
1105ffd83dbSDimitry Andric          "Expect empty main liverange");
1115ffd83dbSDimitry Andric 
1125ffd83dbSDimitry Andric   VNInfo::Allocator *Alloc = getVNAlloc();
1135ffd83dbSDimitry Andric   for (const LiveInterval::SubRange &SR : LI.subranges()) {
1145ffd83dbSDimitry Andric     for (const VNInfo *VNI : SR.valnos) {
1155ffd83dbSDimitry Andric       if (!VNI->isUnused() && !VNI->isPHIDef())
1165ffd83dbSDimitry Andric         MainRange.createDeadDef(VNI->def, *Alloc);
1175ffd83dbSDimitry Andric     }
1185ffd83dbSDimitry Andric   }
1195ffd83dbSDimitry Andric   resetLiveOutMap();
120e8d8bef9SDimitry Andric   extendToUses(MainRange, LI.reg(), LaneBitmask::getAll(), &LI);
1215ffd83dbSDimitry Andric }
1225ffd83dbSDimitry Andric 
createDeadDefs(LiveRange & LR,Register Reg)1235ffd83dbSDimitry Andric void LiveIntervalCalc::createDeadDefs(LiveRange &LR, Register Reg) {
1245ffd83dbSDimitry Andric   const MachineRegisterInfo *MRI = getRegInfo();
1255ffd83dbSDimitry Andric   SlotIndexes *Indexes = getIndexes();
1265ffd83dbSDimitry Andric   VNInfo::Allocator *Alloc = getVNAlloc();
1275ffd83dbSDimitry Andric   assert(MRI && Indexes && "call reset() first");
1285ffd83dbSDimitry Andric 
1295ffd83dbSDimitry Andric   // Visit all def operands. If the same instruction has multiple defs of Reg,
1305ffd83dbSDimitry Andric   // LR.createDeadDef() will deduplicate.
1315ffd83dbSDimitry Andric   for (MachineOperand &MO : MRI->def_operands(Reg))
1325ffd83dbSDimitry Andric     createDeadDef(*Indexes, *Alloc, LR, MO);
1335ffd83dbSDimitry Andric }
1345ffd83dbSDimitry Andric 
extendToUses(LiveRange & LR,Register Reg,LaneBitmask Mask,LiveInterval * LI)1355ffd83dbSDimitry Andric void LiveIntervalCalc::extendToUses(LiveRange &LR, Register Reg,
1365ffd83dbSDimitry Andric                                     LaneBitmask Mask, LiveInterval *LI) {
1375ffd83dbSDimitry Andric   const MachineRegisterInfo *MRI = getRegInfo();
1385ffd83dbSDimitry Andric   SlotIndexes *Indexes = getIndexes();
1395ffd83dbSDimitry Andric   SmallVector<SlotIndex, 4> Undefs;
1405ffd83dbSDimitry Andric   if (LI != nullptr)
1415ffd83dbSDimitry Andric     LI->computeSubRangeUndefs(Undefs, Mask, *MRI, *Indexes);
1425ffd83dbSDimitry Andric 
1435ffd83dbSDimitry Andric   // Visit all operands that read Reg. This may include partial defs.
1445ffd83dbSDimitry Andric   bool IsSubRange = !Mask.all();
1455ffd83dbSDimitry Andric   const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
1465ffd83dbSDimitry Andric   for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
1475ffd83dbSDimitry Andric     // Clear all kill flags. They will be reinserted after register allocation
1485ffd83dbSDimitry Andric     // by LiveIntervals::addKillFlags().
1495ffd83dbSDimitry Andric     if (MO.isUse())
1505ffd83dbSDimitry Andric       MO.setIsKill(false);
1515ffd83dbSDimitry Andric     // MO::readsReg returns "true" for subregister defs. This is for keeping
1525ffd83dbSDimitry Andric     // liveness of the entire register (i.e. for the main range of the live
1535ffd83dbSDimitry Andric     // interval). For subranges, definitions of non-overlapping subregisters
1545ffd83dbSDimitry Andric     // do not count as uses.
1555ffd83dbSDimitry Andric     if (!MO.readsReg() || (IsSubRange && MO.isDef()))
1565ffd83dbSDimitry Andric       continue;
1575ffd83dbSDimitry Andric 
1585ffd83dbSDimitry Andric     unsigned SubReg = MO.getSubReg();
1595ffd83dbSDimitry Andric     if (SubReg != 0) {
1605ffd83dbSDimitry Andric       LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg);
1615ffd83dbSDimitry Andric       if (MO.isDef())
1625ffd83dbSDimitry Andric         SLM = ~SLM;
1635ffd83dbSDimitry Andric       // Ignore uses not reading the current (sub)range.
1645ffd83dbSDimitry Andric       if ((SLM & Mask).none())
1655ffd83dbSDimitry Andric         continue;
1665ffd83dbSDimitry Andric     }
1675ffd83dbSDimitry Andric 
1685ffd83dbSDimitry Andric     // Determine the actual place of the use.
1695ffd83dbSDimitry Andric     const MachineInstr *MI = MO.getParent();
1705ffd83dbSDimitry Andric     unsigned OpNo = (&MO - &MI->getOperand(0));
1715ffd83dbSDimitry Andric     SlotIndex UseIdx;
1725ffd83dbSDimitry Andric     if (MI->isPHI()) {
1735ffd83dbSDimitry Andric       assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
1745ffd83dbSDimitry Andric       // The actual place where a phi operand is used is the end of the pred
1755ffd83dbSDimitry Andric       // MBB. PHI operands are paired: (Reg, PredMBB).
1765ffd83dbSDimitry Andric       UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo + 1).getMBB());
1775ffd83dbSDimitry Andric     } else {
1785ffd83dbSDimitry Andric       // Check for early-clobber redefs.
1795ffd83dbSDimitry Andric       bool isEarlyClobber = false;
1805ffd83dbSDimitry Andric       unsigned DefIdx;
1815ffd83dbSDimitry Andric       if (MO.isDef())
1825ffd83dbSDimitry Andric         isEarlyClobber = MO.isEarlyClobber();
1835ffd83dbSDimitry Andric       else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
1845ffd83dbSDimitry Andric         // FIXME: This would be a lot easier if tied early-clobber uses also
1855ffd83dbSDimitry Andric         // had an early-clobber flag.
1865ffd83dbSDimitry Andric         isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
1875ffd83dbSDimitry Andric       }
1885ffd83dbSDimitry Andric       UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
1895ffd83dbSDimitry Andric     }
1905ffd83dbSDimitry Andric 
1915ffd83dbSDimitry Andric     // MI is reading Reg. We may have visited MI before if it happens to be
1925ffd83dbSDimitry Andric     // reading Reg multiple times. That is OK, extend() is idempotent.
1935ffd83dbSDimitry Andric     extend(LR, UseIdx, Reg, Undefs);
1945ffd83dbSDimitry Andric   }
1955ffd83dbSDimitry Andric }
196