2017-08-25 05:21:39 +08:00
|
|
|
//===- LiveRangeCalc.cpp - Calculate live ranges --------------------------===//
|
2011-09-13 09:34:21 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-09-13 09:34:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implementation of the LiveRangeCalc class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LiveRangeCalc.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-08-24 21:37:55 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2011-09-13 09:34:21 +08:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2012-06-06 05:54:09 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2017-08-25 05:21:39 +08:00
|
|
|
#include "llvm/MC/LaneBitmask.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <iterator>
|
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
2017-06-28 05:30:46 +08:00
|
|
|
// Reserve an address that indicates a value that is known to be "undef".
|
|
|
|
static VNInfo UndefVNI(0xbad, SlotIndex());
|
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
void LiveRangeCalc::resetLiveOutMap() {
|
|
|
|
unsigned NumBlocks = MF->getNumBlockIDs();
|
|
|
|
Seen.clear();
|
|
|
|
Seen.resize(NumBlocks);
|
2017-06-28 02:05:26 +08:00
|
|
|
EntryInfos.clear();
|
2014-12-16 12:03:38 +08:00
|
|
|
Map.resize(NumBlocks);
|
|
|
|
}
|
|
|
|
|
2013-02-21 07:08:26 +08:00
|
|
|
void LiveRangeCalc::reset(const MachineFunction *mf,
|
2012-06-05 02:21:16 +08:00
|
|
|
SlotIndexes *SI,
|
|
|
|
MachineDominatorTree *MDT,
|
|
|
|
VNInfo::Allocator *VNIA) {
|
2013-02-21 07:08:26 +08:00
|
|
|
MF = mf;
|
2012-06-05 02:21:16 +08:00
|
|
|
MRI = &MF->getRegInfo();
|
|
|
|
Indexes = SI;
|
|
|
|
DomTree = MDT;
|
|
|
|
Alloc = VNIA;
|
2014-12-16 12:03:38 +08:00
|
|
|
resetLiveOutMap();
|
2014-12-16 05:36:35 +08:00
|
|
|
LiveIn.clear();
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
|
|
|
|
LiveRange &LR, const MachineOperand &MO) {
|
2016-02-27 14:40:41 +08:00
|
|
|
const MachineInstr &MI = *MO.getParent();
|
|
|
|
SlotIndex DefIdx =
|
|
|
|
Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
|
2014-12-16 05:36:35 +08:00
|
|
|
|
2016-02-27 14:40:41 +08:00
|
|
|
// Create the def in LR. This may find an existing def.
|
|
|
|
LR.createDeadDef(DefIdx, Alloc);
|
2014-12-10 09:12:12 +08:00
|
|
|
}
|
|
|
|
|
2015-03-19 08:21:58 +08:00
|
|
|
void LiveRangeCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
|
2014-12-10 09:12:12 +08:00
|
|
|
assert(MRI && Indexes && "call reset() first");
|
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
// Step 1: Create minimal live segments for every definition of Reg.
|
2014-12-10 09:12:12 +08:00
|
|
|
// Visit all def operands. If the same instruction has multiple defs of Reg,
|
2014-12-16 12:03:38 +08:00
|
|
|
// createDeadDef() will deduplicate.
|
2014-12-10 09:12:12 +08:00
|
|
|
const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
|
|
|
|
unsigned Reg = LI.reg;
|
2014-12-16 12:03:38 +08:00
|
|
|
for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
|
|
|
|
if (!MO.isDef() && !MO.readsReg())
|
|
|
|
continue;
|
|
|
|
|
2014-12-10 09:12:12 +08:00
|
|
|
unsigned SubReg = MO.getSubReg();
|
2015-03-19 08:21:58 +08:00
|
|
|
if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
|
2016-08-24 21:37:55 +08:00
|
|
|
LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
|
2016-08-29 21:15:35 +08:00
|
|
|
: MRI->getMaxLaneMaskForVReg(Reg);
|
2014-12-10 09:12:12 +08:00
|
|
|
// If this is the first time we see a subregister def, initialize
|
|
|
|
// subranges by creating a copy of the main range.
|
|
|
|
if (!LI.hasSubRanges() && !LI.empty()) {
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
|
2014-12-10 09:12:12 +08:00
|
|
|
LI.createSubRangeFrom(*Alloc, ClassMask, LI);
|
|
|
|
}
|
|
|
|
|
2017-03-04 03:05:34 +08:00
|
|
|
LI.refineSubRanges(*Alloc, SubMask,
|
2019-03-27 05:27:15 +08:00
|
|
|
[&MO, this](LiveInterval::SubRange &SR) {
|
|
|
|
if (MO.isDef())
|
|
|
|
createDeadDef(*Indexes, *Alloc, SR, MO);
|
|
|
|
},
|
|
|
|
*Indexes, TRI);
|
2014-12-10 09:12:12 +08:00
|
|
|
}
|
|
|
|
|
2014-12-24 10:11:51 +08:00
|
|
|
// Create the def in the main liverange. We do not have to do this if
|
|
|
|
// subranges are tracked as we recreate the main range later in this case.
|
|
|
|
if (MO.isDef() && !LI.hasSubRanges())
|
2014-12-16 12:03:38 +08:00
|
|
|
createDeadDef(*Indexes, *Alloc, LI, MO);
|
2014-12-16 05:36:35 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
// We may have created empty live ranges for partially undefined uses, we
|
|
|
|
// can't keep them because we won't find defs in them later.
|
|
|
|
LI.removeEmptySubRanges();
|
2014-12-16 05:36:35 +08:00
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
// Step 2: Extend live segments to all uses, constructing SSA form as
|
|
|
|
// necessary.
|
2014-12-24 10:11:51 +08:00
|
|
|
if (LI.hasSubRanges()) {
|
|
|
|
for (LiveInterval::SubRange &S : LI.subranges()) {
|
2016-08-24 21:37:55 +08:00
|
|
|
LiveRangeCalc SubLRC;
|
|
|
|
SubLRC.reset(MF, Indexes, DomTree, Alloc);
|
|
|
|
SubLRC.extendToUses(S, Reg, S.LaneMask, &LI);
|
2014-12-24 10:11:51 +08:00
|
|
|
}
|
|
|
|
LI.clear();
|
2016-05-21 07:14:56 +08:00
|
|
|
constructMainRangeFromSubranges(LI);
|
2014-12-24 10:11:51 +08:00
|
|
|
} else {
|
2014-12-16 12:03:38 +08:00
|
|
|
resetLiveOutMap();
|
2016-12-15 22:36:06 +08:00
|
|
|
extendToUses(LI, Reg, LaneBitmask::getAll());
|
2014-12-10 09:12:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 07:14:56 +08:00
|
|
|
void LiveRangeCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
|
|
|
|
// First create dead defs at all defs found in subranges.
|
|
|
|
LiveRange &MainRange = LI;
|
|
|
|
assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
|
|
|
|
"Expect empty main liverange");
|
|
|
|
|
|
|
|
for (const LiveInterval::SubRange &SR : LI.subranges()) {
|
|
|
|
for (const VNInfo *VNI : SR.valnos) {
|
|
|
|
if (!VNI->isUnused() && !VNI->isPHIDef())
|
|
|
|
MainRange.createDeadDef(VNI->def, *Alloc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resetLiveOutMap();
|
2016-12-15 22:36:06 +08:00
|
|
|
extendToUses(MainRange, LI.reg, LaneBitmask::getAll(), &LI);
|
2016-05-21 07:14:56 +08:00
|
|
|
}
|
2014-12-10 09:12:12 +08:00
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
|
2014-12-16 05:36:35 +08:00
|
|
|
assert(MRI && Indexes && "call reset() first");
|
2012-06-06 05:54:09 +08:00
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
// Visit all def operands. If the same instruction has multiple defs of Reg,
|
|
|
|
// LR.createDeadDef() will deduplicate.
|
|
|
|
for (MachineOperand &MO : MRI->def_operands(Reg))
|
|
|
|
createDeadDef(*Indexes, *Alloc, LR, MO);
|
2014-12-16 05:36:35 +08:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:37:55 +08:00
|
|
|
void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask Mask,
|
|
|
|
LiveInterval *LI) {
|
|
|
|
SmallVector<SlotIndex, 4> Undefs;
|
|
|
|
if (LI != nullptr)
|
|
|
|
LI->computeSubRangeUndefs(Undefs, Mask, *MRI, *Indexes);
|
|
|
|
|
2014-12-16 05:36:35 +08:00
|
|
|
// Visit all operands that read Reg. This may include partial defs.
|
2016-12-15 22:36:06 +08:00
|
|
|
bool IsSubRange = !Mask.all();
|
2014-12-16 12:03:38 +08:00
|
|
|
const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
|
2014-12-10 09:12:12 +08:00
|
|
|
for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
|
|
|
|
// Clear all kill flags. They will be reinserted after register allocation
|
2017-12-13 10:51:04 +08:00
|
|
|
// by LiveIntervals::addKillFlags().
|
2014-12-10 09:12:12 +08:00
|
|
|
if (MO.isUse())
|
|
|
|
MO.setIsKill(false);
|
2016-09-03 03:48:55 +08:00
|
|
|
// MO::readsReg returns "true" for subregister defs. This is for keeping
|
|
|
|
// liveness of the entire register (i.e. for the main range of the live
|
|
|
|
// interval). For subranges, definitions of non-overlapping subregisters
|
|
|
|
// do not count as uses.
|
|
|
|
if (!MO.readsReg() || (IsSubRange && MO.isDef()))
|
2014-12-10 09:12:12 +08:00
|
|
|
continue;
|
2016-08-24 21:37:55 +08:00
|
|
|
|
2014-12-10 09:12:12 +08:00
|
|
|
unsigned SubReg = MO.getSubReg();
|
2014-12-16 12:03:38 +08:00
|
|
|
if (SubReg != 0) {
|
2016-08-24 21:37:55 +08:00
|
|
|
LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg);
|
|
|
|
if (MO.isDef())
|
2016-08-29 21:15:35 +08:00
|
|
|
SLM = ~SLM;
|
|
|
|
// Ignore uses not reading the current (sub)range.
|
2016-12-15 22:36:06 +08:00
|
|
|
if ((SLM & Mask).none())
|
2014-12-16 12:03:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-10 09:12:12 +08:00
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
// Determine the actual place of the use.
|
|
|
|
const MachineInstr *MI = MO.getParent();
|
|
|
|
unsigned OpNo = (&MO - &MI->getOperand(0));
|
|
|
|
SlotIndex UseIdx;
|
|
|
|
if (MI->isPHI()) {
|
|
|
|
assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
|
|
|
|
// The actual place where a phi operand is used is the end of the pred
|
|
|
|
// MBB. PHI operands are paired: (Reg, PredMBB).
|
|
|
|
UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
|
|
|
|
} else {
|
|
|
|
// Check for early-clobber redefs.
|
|
|
|
bool isEarlyClobber = false;
|
|
|
|
unsigned DefIdx;
|
|
|
|
if (MO.isDef())
|
|
|
|
isEarlyClobber = MO.isEarlyClobber();
|
|
|
|
else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
|
|
|
|
// FIXME: This would be a lot easier if tied early-clobber uses also
|
|
|
|
// had an early-clobber flag.
|
|
|
|
isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
|
2014-12-16 05:36:35 +08:00
|
|
|
}
|
2016-02-27 14:40:41 +08:00
|
|
|
UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
|
2012-06-06 05:54:09 +08:00
|
|
|
}
|
2014-12-16 12:03:38 +08:00
|
|
|
|
|
|
|
// MI is reading Reg. We may have visited MI before if it happens to be
|
|
|
|
// reading Reg multiple times. That is OK, extend() is idempotent.
|
2016-08-24 21:37:55 +08:00
|
|
|
extend(LR, UseIdx, Reg, Undefs);
|
2012-06-06 05:54:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 12:03:38 +08:00
|
|
|
void LiveRangeCalc::updateFromLiveIns() {
|
2013-02-21 07:08:26 +08:00
|
|
|
LiveRangeUpdater Updater;
|
2014-12-16 03:40:46 +08:00
|
|
|
for (const LiveInBlock &I : LiveIn) {
|
|
|
|
if (!I.DomNode)
|
2011-09-13 09:34:21 +08:00
|
|
|
continue;
|
2014-12-16 03:40:46 +08:00
|
|
|
MachineBasicBlock *MBB = I.DomNode->getBlock();
|
|
|
|
assert(I.Value && "No live-in value found");
|
2011-09-13 09:34:21 +08:00
|
|
|
SlotIndex Start, End;
|
2014-03-02 21:30:33 +08:00
|
|
|
std::tie(Start, End) = Indexes->getMBBRange(MBB);
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2014-12-16 03:40:46 +08:00
|
|
|
if (I.Kill.isValid())
|
2013-02-21 07:08:26 +08:00
|
|
|
// Value is killed inside this block.
|
2014-12-16 03:40:46 +08:00
|
|
|
End = I.Kill;
|
2011-09-13 09:34:21 +08:00
|
|
|
else {
|
2013-02-21 07:08:26 +08:00
|
|
|
// The value is live-through, update LiveOut as well.
|
|
|
|
// Defer the Domtree lookup until it is needed.
|
2014-12-16 12:03:38 +08:00
|
|
|
assert(Seen.test(MBB->getNumber()));
|
|
|
|
Map[MBB] = LiveOutPair(I.Value, nullptr);
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
2014-12-16 03:40:46 +08:00
|
|
|
Updater.setDest(&I.LR);
|
|
|
|
Updater.add(Start, End, I.Value);
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
|
|
|
LiveIn.clear();
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:37:55 +08:00
|
|
|
void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
|
|
|
|
ArrayRef<SlotIndex> Undefs) {
|
2015-02-18 09:50:52 +08:00
|
|
|
assert(Use.isValid() && "Invalid SlotIndex");
|
2011-09-13 09:34:21 +08:00
|
|
|
assert(Indexes && "Missing SlotIndexes");
|
|
|
|
assert(DomTree && "Missing dominator tree");
|
|
|
|
|
2015-02-18 09:50:52 +08:00
|
|
|
MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
|
|
|
|
assert(UseMBB && "No MBB at Use");
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// Is there a def in the same MBB we can extend?
|
2016-08-24 21:37:55 +08:00
|
|
|
auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use);
|
|
|
|
if (EP.first != nullptr || EP.second)
|
2011-09-13 09:34:21 +08:00
|
|
|
return;
|
|
|
|
|
2015-02-18 09:50:52 +08:00
|
|
|
// Find the single reaching def, or determine if Use is jointly dominated by
|
2011-09-13 09:34:21 +08:00
|
|
|
// multiple values, and we may need to create even more phi-defs to preserve
|
|
|
|
// VNInfo SSA form. Perform a search for all predecessor blocks where we
|
|
|
|
// know the dominating VNInfo.
|
2016-08-24 21:37:55 +08:00
|
|
|
if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
|
2013-02-21 07:08:26 +08:00
|
|
|
return;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// When there were multiple different values, we may need new PHIs.
|
2014-12-16 12:03:38 +08:00
|
|
|
calculateValues();
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is called by a client after using the low-level API to add
|
|
|
|
// live-out and live-in blocks. The unique value optimization is not
|
|
|
|
// available, SplitEditor::transferValues handles that case directly anyway.
|
2014-12-16 12:03:38 +08:00
|
|
|
void LiveRangeCalc::calculateValues() {
|
2011-09-13 09:34:21 +08:00
|
|
|
assert(Indexes && "Missing SlotIndexes");
|
|
|
|
assert(DomTree && "Missing dominator tree");
|
2014-12-16 12:03:38 +08:00
|
|
|
updateSSA();
|
|
|
|
updateFromLiveIns();
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:37:55 +08:00
|
|
|
bool LiveRangeCalc::isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
|
|
|
|
MachineBasicBlock &MBB, BitVector &DefOnEntry,
|
|
|
|
BitVector &UndefOnEntry) {
|
|
|
|
unsigned BN = MBB.getNumber();
|
|
|
|
if (DefOnEntry[BN])
|
|
|
|
return true;
|
|
|
|
if (UndefOnEntry[BN])
|
|
|
|
return false;
|
|
|
|
|
2017-01-14 01:12:16 +08:00
|
|
|
auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool {
|
2016-08-24 21:37:55 +08:00
|
|
|
for (MachineBasicBlock *S : B.successors())
|
|
|
|
DefOnEntry[S->getNumber()] = true;
|
|
|
|
DefOnEntry[BN] = true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
SetVector<unsigned> WorkList;
|
|
|
|
// Checking if the entry of MBB is reached by some def: add all predecessors
|
|
|
|
// that are potentially defined-on-exit to the work list.
|
|
|
|
for (MachineBasicBlock *P : MBB.predecessors())
|
|
|
|
WorkList.insert(P->getNumber());
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != WorkList.size(); ++i) {
|
|
|
|
// Determine if the exit from the block is reached by some def.
|
|
|
|
unsigned N = WorkList[i];
|
|
|
|
MachineBasicBlock &B = *MF->getBlockNumbered(N);
|
2017-06-28 05:30:46 +08:00
|
|
|
if (Seen[N]) {
|
|
|
|
const LiveOutPair &LOB = Map[&B];
|
|
|
|
if (LOB.first != nullptr && LOB.first != &UndefVNI)
|
|
|
|
return MarkDefined(B);
|
|
|
|
}
|
2016-08-24 21:37:55 +08:00
|
|
|
SlotIndex Begin, End;
|
|
|
|
std::tie(Begin, End) = Indexes->getMBBRange(&B);
|
2017-01-19 07:12:19 +08:00
|
|
|
// Treat End as not belonging to B.
|
|
|
|
// If LR has a segment S that starts at the next block, i.e. [End, ...),
|
|
|
|
// std::upper_bound will return the segment following S. Instead,
|
|
|
|
// S should be treated as the first segment that does not overlap B.
|
|
|
|
LiveRange::iterator UB = std::upper_bound(LR.begin(), LR.end(),
|
|
|
|
End.getPrevSlot());
|
2016-08-24 21:37:55 +08:00
|
|
|
if (UB != LR.begin()) {
|
|
|
|
LiveRange::Segment &Seg = *std::prev(UB);
|
|
|
|
if (Seg.end > Begin) {
|
|
|
|
// There is a segment that overlaps B. If the range is not explicitly
|
|
|
|
// undefined between the end of the segment and the end of the block,
|
|
|
|
// treat the block as defined on exit. If it is, go to the next block
|
|
|
|
// on the work list.
|
|
|
|
if (LR.isUndefIn(Undefs, Seg.end, End))
|
|
|
|
continue;
|
|
|
|
return MarkDefined(B);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No segment overlaps with this block. If this block is not defined on
|
|
|
|
// entry, or it undefines the range, do not process its predecessors.
|
|
|
|
if (UndefOnEntry[N] || LR.isUndefIn(Undefs, Begin, End)) {
|
|
|
|
UndefOnEntry[N] = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (DefOnEntry[N])
|
|
|
|
return MarkDefined(B);
|
|
|
|
|
|
|
|
// Still don't know: add all predecessors to the work list.
|
|
|
|
for (MachineBasicBlock *P : B.predecessors())
|
|
|
|
WorkList.insert(P->getNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
UndefOnEntry[BN] = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-18 09:50:52 +08:00
|
|
|
bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
|
2016-08-24 21:37:55 +08:00
|
|
|
SlotIndex Use, unsigned PhysReg,
|
|
|
|
ArrayRef<SlotIndex> Undefs) {
|
2015-02-18 09:50:52 +08:00
|
|
|
unsigned UseMBBNum = UseMBB.getNumber();
|
2013-02-21 07:08:26 +08:00
|
|
|
|
2013-10-11 05:28:57 +08:00
|
|
|
// Block numbers where LR should be live-in.
|
2015-02-18 09:50:52 +08:00
|
|
|
SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// Remember if we have seen more than one value.
|
|
|
|
bool UniqueVNI = true;
|
2014-04-14 08:51:57 +08:00
|
|
|
VNInfo *TheVNI = nullptr;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2016-08-24 21:37:55 +08:00
|
|
|
bool FoundUndef = false;
|
|
|
|
|
2011-09-13 09:34:21 +08:00
|
|
|
// Using Seen as a visited set, perform a BFS for all reaching defs.
|
|
|
|
for (unsigned i = 0; i != WorkList.size(); ++i) {
|
2013-02-21 07:08:26 +08:00
|
|
|
MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
|
2012-07-14 07:39:05 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2016-09-20 00:49:45 +08:00
|
|
|
if (MBB->pred_empty()) {
|
2012-07-14 07:39:05 +08:00
|
|
|
MBB->getParent()->verify();
|
2018-10-30 09:11:31 +08:00
|
|
|
errs() << "Use of " << printReg(PhysReg, MRI->getTargetRegisterInfo())
|
2015-05-12 02:47:47 +08:00
|
|
|
<< " does not have a corresponding definition on every path:\n";
|
|
|
|
const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
|
|
|
|
if (MI != nullptr)
|
|
|
|
errs() << Use << " " << *MI;
|
2016-09-20 00:49:45 +08:00
|
|
|
report_fatal_error("Use not jointly dominated by defs.");
|
2012-07-14 07:39:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
|
|
|
|
!MBB->isLiveIn(PhysReg)) {
|
|
|
|
MBB->getParent()->verify();
|
2016-09-03 14:57:49 +08:00
|
|
|
const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
|
2017-11-28 20:42:37 +08:00
|
|
|
errs() << "The register " << printReg(PhysReg, TRI)
|
2017-12-05 01:18:51 +08:00
|
|
|
<< " needs to be live in to " << printMBBReference(*MBB)
|
2012-07-14 07:39:05 +08:00
|
|
|
<< ", but is missing from the live-in list.\n";
|
2016-09-20 00:49:45 +08:00
|
|
|
report_fatal_error("Invalid global physical register");
|
2012-07-14 07:39:05 +08:00
|
|
|
}
|
|
|
|
#endif
|
2016-08-24 21:37:55 +08:00
|
|
|
FoundUndef |= MBB->pred_empty();
|
2012-07-14 07:39:05 +08:00
|
|
|
|
2017-06-29 00:02:00 +08:00
|
|
|
for (MachineBasicBlock *Pred : MBB->predecessors()) {
|
2011-09-13 09:34:21 +08:00
|
|
|
// Is this a known live-out block?
|
2014-12-16 12:03:38 +08:00
|
|
|
if (Seen.test(Pred->getNumber())) {
|
|
|
|
if (VNInfo *VNI = Map[Pred].first) {
|
2011-09-13 09:34:21 +08:00
|
|
|
if (TheVNI && TheVNI != VNI)
|
|
|
|
UniqueVNI = false;
|
|
|
|
TheVNI = VNI;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SlotIndex Start, End;
|
2014-03-02 21:30:33 +08:00
|
|
|
std::tie(Start, End) = Indexes->getMBBRange(Pred);
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// First time we see Pred. Try to determine the live-out value, but set
|
|
|
|
// it as null if Pred is live-through with an unknown value.
|
2016-08-24 21:37:55 +08:00
|
|
|
auto EP = LR.extendInBlock(Undefs, Start, End);
|
|
|
|
VNInfo *VNI = EP.first;
|
|
|
|
FoundUndef |= EP.second;
|
2017-06-28 05:30:46 +08:00
|
|
|
setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI);
|
2011-09-13 09:34:21 +08:00
|
|
|
if (VNI) {
|
|
|
|
if (TheVNI && TheVNI != VNI)
|
|
|
|
UniqueVNI = false;
|
|
|
|
TheVNI = VNI;
|
|
|
|
}
|
2016-08-24 21:37:55 +08:00
|
|
|
if (VNI || EP.second)
|
|
|
|
continue;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// No, we need a live-in value for Pred as well
|
2015-02-18 09:50:52 +08:00
|
|
|
if (Pred != &UseMBB)
|
2016-08-24 21:37:55 +08:00
|
|
|
WorkList.push_back(Pred->getNumber());
|
2011-09-13 09:34:21 +08:00
|
|
|
else
|
2015-02-18 09:50:52 +08:00
|
|
|
// Loopback to UseMBB, so value is really live through.
|
|
|
|
Use = SlotIndex();
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LiveIn.clear();
|
2017-06-28 23:46:16 +08:00
|
|
|
FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI);
|
2017-08-25 05:21:39 +08:00
|
|
|
if (!Undefs.empty() && FoundUndef)
|
2016-08-24 21:37:55 +08:00
|
|
|
UniqueVNI = false;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2013-02-21 07:08:26 +08:00
|
|
|
// Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
|
|
|
|
// neither require it. Skip the sorting overhead for small updates.
|
|
|
|
if (WorkList.size() > 4)
|
|
|
|
array_pod_sort(WorkList.begin(), WorkList.end());
|
|
|
|
|
|
|
|
// If a unique reaching def was found, blit in the live ranges immediately.
|
|
|
|
if (UniqueVNI) {
|
2017-06-28 05:30:46 +08:00
|
|
|
assert(TheVNI != nullptr && TheVNI != &UndefVNI);
|
2013-10-11 05:28:57 +08:00
|
|
|
LiveRangeUpdater Updater(&LR);
|
2016-08-24 21:37:55 +08:00
|
|
|
for (unsigned BN : WorkList) {
|
|
|
|
SlotIndex Start, End;
|
|
|
|
std::tie(Start, End) = Indexes->getMBBRange(BN);
|
|
|
|
// Trim the live range in UseMBB.
|
|
|
|
if (BN == UseMBBNum && Use.isValid())
|
|
|
|
End = Use;
|
|
|
|
else
|
|
|
|
Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr);
|
|
|
|
Updater.add(Start, End, TheVNI);
|
2013-02-21 07:08:26 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:37:55 +08:00
|
|
|
// Prepare the defined/undefined bit vectors.
|
2017-06-28 02:05:26 +08:00
|
|
|
EntryInfoMap::iterator Entry;
|
|
|
|
bool DidInsert;
|
|
|
|
std::tie(Entry, DidInsert) = EntryInfos.insert(
|
|
|
|
std::make_pair(&LR, std::make_pair(BitVector(), BitVector())));
|
|
|
|
if (DidInsert) {
|
|
|
|
// Initialize newly inserted entries.
|
2016-08-24 21:37:55 +08:00
|
|
|
unsigned N = MF->getNumBlockIDs();
|
2017-06-28 02:05:26 +08:00
|
|
|
Entry->second.first.resize(N);
|
|
|
|
Entry->second.second.resize(N);
|
2016-08-24 21:37:55 +08:00
|
|
|
}
|
2017-06-28 02:05:26 +08:00
|
|
|
BitVector &DefOnEntry = Entry->second.first;
|
|
|
|
BitVector &UndefOnEntry = Entry->second.second;
|
2016-08-24 21:37:55 +08:00
|
|
|
|
2013-02-21 07:08:26 +08:00
|
|
|
// Multiple values were found, so transfer the work list to the LiveIn array
|
|
|
|
// where UpdateSSA will use it as a work list.
|
|
|
|
LiveIn.reserve(WorkList.size());
|
2016-08-24 21:37:55 +08:00
|
|
|
for (unsigned BN : WorkList) {
|
|
|
|
MachineBasicBlock *MBB = MF->getBlockNumbered(BN);
|
2017-08-25 05:21:39 +08:00
|
|
|
if (!Undefs.empty() &&
|
2017-06-29 00:02:00 +08:00
|
|
|
!isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
|
2016-08-24 21:37:55 +08:00
|
|
|
continue;
|
2013-10-11 05:28:57 +08:00
|
|
|
addLiveInBlock(LR, DomTree->getNode(MBB));
|
2015-02-18 09:50:52 +08:00
|
|
|
if (MBB == &UseMBB)
|
|
|
|
LiveIn.back().Kill = Use;
|
2013-02-21 07:08:26 +08:00
|
|
|
}
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2013-02-21 07:08:26 +08:00
|
|
|
return false;
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is essentially the same iterative algorithm that SSAUpdater uses,
|
|
|
|
// except we already have a dominator tree, so we don't have to recompute it.
|
2014-12-16 12:03:38 +08:00
|
|
|
void LiveRangeCalc::updateSSA() {
|
2011-09-13 09:34:21 +08:00
|
|
|
assert(Indexes && "Missing SlotIndexes");
|
|
|
|
assert(DomTree && "Missing dominator tree");
|
|
|
|
|
|
|
|
// Interate until convergence.
|
2017-06-29 00:02:00 +08:00
|
|
|
bool Changed;
|
2011-09-13 09:34:21 +08:00
|
|
|
do {
|
2017-06-29 00:02:00 +08:00
|
|
|
Changed = false;
|
2011-09-13 09:34:21 +08:00
|
|
|
// Propagate live-out values down the dominator tree, inserting phi-defs
|
|
|
|
// when necessary.
|
2014-12-16 03:40:46 +08:00
|
|
|
for (LiveInBlock &I : LiveIn) {
|
|
|
|
MachineDomTreeNode *Node = I.DomNode;
|
2011-09-13 09:34:21 +08:00
|
|
|
// Skip block if the live-in value has already been determined.
|
|
|
|
if (!Node)
|
|
|
|
continue;
|
|
|
|
MachineBasicBlock *MBB = Node->getBlock();
|
|
|
|
MachineDomTreeNode *IDom = Node->getIDom();
|
|
|
|
LiveOutPair IDomValue;
|
|
|
|
|
|
|
|
// We need a live-in value to a block with no immediate dominator?
|
|
|
|
// This is probably an unreachable block that has survived somehow.
|
2014-12-16 12:03:38 +08:00
|
|
|
bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// IDom dominates all of our predecessors, but it may not be their
|
|
|
|
// immediate dominator. Check if any of them have live-out values that are
|
|
|
|
// properly dominated by IDom. If so, we need a phi-def here.
|
|
|
|
if (!needPHI) {
|
2014-12-16 12:03:38 +08:00
|
|
|
IDomValue = Map[IDom->getBlock()];
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// Cache the DomTree node that defined the value.
|
2017-06-29 00:02:00 +08:00
|
|
|
if (IDomValue.first && IDomValue.first != &UndefVNI &&
|
|
|
|
!IDomValue.second) {
|
|
|
|
Map[IDom->getBlock()].second = IDomValue.second =
|
|
|
|
DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (MachineBasicBlock *Pred : MBB->predecessors()) {
|
|
|
|
LiveOutPair &Value = Map[Pred];
|
2011-09-13 09:34:21 +08:00
|
|
|
if (!Value.first || Value.first == IDomValue.first)
|
|
|
|
continue;
|
2017-06-28 05:30:46 +08:00
|
|
|
if (Value.first == &UndefVNI) {
|
|
|
|
needPHI = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// Cache the DomTree node that defined the value.
|
|
|
|
if (!Value.second)
|
|
|
|
Value.second =
|
|
|
|
DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
|
|
|
|
|
|
|
|
// This predecessor is carrying something other than IDomValue.
|
|
|
|
// It could be because IDomValue hasn't propagated yet, or it could be
|
|
|
|
// because MBB is in the dominance frontier of that value.
|
|
|
|
if (DomTree->dominates(IDom, Value.second)) {
|
|
|
|
needPHI = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The value may be live-through even if Kill is set, as can happen when
|
|
|
|
// we are called from extendRange. In that case LiveOutSeen is true, and
|
|
|
|
// LiveOut indicates a foreign or missing value.
|
2014-12-16 12:03:38 +08:00
|
|
|
LiveOutPair &LOP = Map[MBB];
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// Create a phi-def if required.
|
|
|
|
if (needPHI) {
|
2017-06-29 00:02:00 +08:00
|
|
|
Changed = true;
|
2011-09-13 09:34:21 +08:00
|
|
|
assert(Alloc && "Need VNInfo allocator to create PHI-defs");
|
|
|
|
SlotIndex Start, End;
|
2014-03-02 21:30:33 +08:00
|
|
|
std::tie(Start, End) = Indexes->getMBBRange(MBB);
|
2014-12-16 03:40:46 +08:00
|
|
|
LiveRange &LR = I.LR;
|
2013-10-11 05:28:57 +08:00
|
|
|
VNInfo *VNI = LR.getNextValue(Start, *Alloc);
|
2014-12-16 03:40:46 +08:00
|
|
|
I.Value = VNI;
|
2011-09-13 09:34:21 +08:00
|
|
|
// This block is done, we know the final value.
|
2014-12-16 03:40:46 +08:00
|
|
|
I.DomNode = nullptr;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
2014-12-10 09:12:12 +08:00
|
|
|
// Add liveness since updateFromLiveIns now skips this node.
|
2016-08-24 21:37:55 +08:00
|
|
|
if (I.Kill.isValid()) {
|
|
|
|
if (VNI)
|
|
|
|
LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
|
|
|
|
} else {
|
|
|
|
if (VNI)
|
|
|
|
LR.addSegment(LiveInterval::Segment(Start, End, VNI));
|
2011-09-13 09:34:21 +08:00
|
|
|
LOP = LiveOutPair(VNI, Node);
|
|
|
|
}
|
2017-06-28 05:30:46 +08:00
|
|
|
} else if (IDomValue.first && IDomValue.first != &UndefVNI) {
|
2011-09-13 09:34:21 +08:00
|
|
|
// No phi-def here. Remember incoming value.
|
2014-12-16 03:40:46 +08:00
|
|
|
I.Value = IDomValue.first;
|
2011-09-13 09:34:21 +08:00
|
|
|
|
|
|
|
// If the IDomValue is killed in the block, don't propagate through.
|
2014-12-16 03:40:46 +08:00
|
|
|
if (I.Kill.isValid())
|
2011-09-13 09:34:21 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Propagate IDomValue if it isn't killed:
|
|
|
|
// MBB is live-out and doesn't define its own value.
|
|
|
|
if (LOP.first == IDomValue.first)
|
|
|
|
continue;
|
2017-06-29 00:02:00 +08:00
|
|
|
Changed = true;
|
2011-09-13 09:34:21 +08:00
|
|
|
LOP = IDomValue;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 00:02:00 +08:00
|
|
|
} while (Changed);
|
2011-09-13 09:34:21 +08:00
|
|
|
}
|
2018-06-26 22:37:16 +08:00
|
|
|
|
|
|
|
bool LiveRangeCalc::isJointlyDominated(const MachineBasicBlock *MBB,
|
|
|
|
ArrayRef<SlotIndex> Defs,
|
|
|
|
const SlotIndexes &Indexes) {
|
|
|
|
const MachineFunction &MF = *MBB->getParent();
|
|
|
|
BitVector DefBlocks(MF.getNumBlockIDs());
|
|
|
|
for (SlotIndex I : Defs)
|
|
|
|
DefBlocks.set(Indexes.getMBBFromIndex(I)->getNumber());
|
|
|
|
|
|
|
|
SetVector<unsigned> PredQueue;
|
|
|
|
PredQueue.insert(MBB->getNumber());
|
|
|
|
for (unsigned i = 0; i != PredQueue.size(); ++i) {
|
|
|
|
unsigned BN = PredQueue[i];
|
|
|
|
if (DefBlocks[BN])
|
|
|
|
return true;
|
|
|
|
const MachineBasicBlock *B = MF.getBlockNumbered(BN);
|
|
|
|
for (const MachineBasicBlock *P : B->predecessors())
|
|
|
|
PredQueue.insert(P->getNumber());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|