2012-02-18 01:35:10 +08:00
|
|
|
//===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===//
|
2010-10-15 07:49:52 +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
|
2010-10-15 07:49:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LiveRangeEdit class represents changes done to a virtual register when it
|
|
|
|
// is spilled or split.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/LiveRangeEdit.h"
|
2011-05-06 01:22:53 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2020-06-26 01:12:55 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2011-03-30 05:20:19 +08:00
|
|
|
#include "llvm/CodeGen/CalcSpillWeights.h"
|
2017-12-13 10:51:04 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervals.h"
|
2010-10-15 07:49:52 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2012-11-29 03:13:06 +08:00
|
|
|
#include "llvm/CodeGen/VirtRegMap.h"
|
2011-03-09 06:46:11 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-10-15 07:49:52 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
2011-05-06 01:22:53 +08:00
|
|
|
STATISTIC(NumDCEDeleted, "Number of instructions deleted by DCE");
|
|
|
|
STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE");
|
|
|
|
STATISTIC(NumFracRanges, "Number of live ranges fractured by DCE");
|
|
|
|
|
2011-12-20 10:50:00 +08:00
|
|
|
void LiveRangeEdit::Delegate::anchor() { }
|
|
|
|
|
2020-06-30 23:57:24 +08:00
|
|
|
LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(Register OldReg,
|
2018-01-11 05:41:02 +08:00
|
|
|
bool createSubRanges) {
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
|
2018-01-11 05:41:02 +08:00
|
|
|
if (VRM)
|
2012-04-03 08:28:46 +08:00
|
|
|
VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
|
2018-01-11 05:41:02 +08:00
|
|
|
|
2013-08-15 07:50:16 +08:00
|
|
|
LiveInterval &LI = LIS.createEmptyInterval(VReg);
|
2017-02-03 04:44:36 +08:00
|
|
|
if (Parent && !Parent->isSpillable())
|
|
|
|
LI.markNotSpillable();
|
2018-01-11 05:41:02 +08:00
|
|
|
if (createSubRanges) {
|
|
|
|
// Create empty subranges if the OldReg's interval has them. Do not create
|
|
|
|
// the main range here---it will be constructed later after the subranges
|
|
|
|
// have been finalized.
|
|
|
|
LiveInterval &OldLI = LIS.getInterval(OldReg);
|
|
|
|
VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
|
|
|
|
for (LiveInterval::SubRange &S : OldLI.subranges())
|
|
|
|
LI.createSubRange(Alloc, S.LaneMask);
|
|
|
|
}
|
2011-03-18 04:37:07 +08:00
|
|
|
return LI;
|
2010-10-15 07:49:52 +08:00
|
|
|
}
|
|
|
|
|
2020-06-30 23:57:24 +08:00
|
|
|
Register LiveRangeEdit::createFrom(Register OldReg) {
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
|
2013-08-15 07:50:16 +08:00
|
|
|
if (VRM) {
|
|
|
|
VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
|
|
|
|
}
|
2017-02-03 04:44:36 +08:00
|
|
|
// FIXME: Getting the interval here actually computes it.
|
|
|
|
// In theory, this may not be what we want, but in practice
|
|
|
|
// the createEmptyIntervalFrom API is used when this is not
|
|
|
|
// the case. Generally speaking we just want to annotate the
|
|
|
|
// LiveInterval when it gets created but we cannot do that at
|
|
|
|
// the moment.
|
|
|
|
if (Parent && !Parent->isSpillable())
|
|
|
|
LIS.getInterval(VReg).markNotSpillable();
|
2013-08-15 07:50:16 +08:00
|
|
|
return VReg;
|
|
|
|
}
|
|
|
|
|
2011-04-21 06:14:20 +08:00
|
|
|
bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
|
2011-03-29 11:12:02 +08:00
|
|
|
const MachineInstr *DefMI,
|
2020-06-26 01:12:55 +08:00
|
|
|
AAResults *aa) {
|
2011-03-29 11:12:02 +08:00
|
|
|
assert(DefMI && "Missing instruction");
|
2012-05-19 06:10:15 +08:00
|
|
|
ScannedRemattable = true;
|
2016-06-30 08:01:54 +08:00
|
|
|
if (!TII.isTriviallyReMaterializable(*DefMI, aa))
|
2011-04-21 06:14:20 +08:00
|
|
|
return false;
|
2012-05-19 06:10:15 +08:00
|
|
|
Remattable.insert(VNI);
|
2011-04-21 06:14:20 +08:00
|
|
|
return true;
|
2011-03-29 11:12:02 +08:00
|
|
|
}
|
|
|
|
|
2020-06-26 01:12:55 +08:00
|
|
|
void LiveRangeEdit::scanRemattable(AAResults *aa) {
|
2014-12-11 07:07:54 +08:00
|
|
|
for (VNInfo *VNI : getParent().valnos) {
|
2010-10-21 06:00:51 +08:00
|
|
|
if (VNI->isUnused())
|
|
|
|
continue;
|
2016-04-13 11:08:27 +08:00
|
|
|
unsigned Original = VRM->getOriginal(getReg());
|
|
|
|
LiveInterval &OrigLI = LIS.getInterval(Original);
|
|
|
|
VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
|
2016-08-24 21:37:55 +08:00
|
|
|
if (!OrigVNI)
|
|
|
|
continue;
|
2016-04-13 11:08:27 +08:00
|
|
|
MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
|
2010-10-21 06:00:51 +08:00
|
|
|
if (!DefMI)
|
|
|
|
continue;
|
2016-04-13 11:08:27 +08:00
|
|
|
checkRematerializable(OrigVNI, DefMI, aa);
|
2010-10-21 06:00:51 +08:00
|
|
|
}
|
2012-05-19 06:10:15 +08:00
|
|
|
ScannedRemattable = true;
|
2010-10-21 06:00:51 +08:00
|
|
|
}
|
|
|
|
|
2020-06-26 01:12:55 +08:00
|
|
|
bool LiveRangeEdit::anyRematerializable(AAResults *aa) {
|
2012-05-19 06:10:15 +08:00
|
|
|
if (!ScannedRemattable)
|
2012-04-03 06:22:53 +08:00
|
|
|
scanRemattable(aa);
|
2012-05-19 06:10:15 +08:00
|
|
|
return !Remattable.empty();
|
2010-10-21 06:00:51 +08:00
|
|
|
}
|
|
|
|
|
2010-10-15 07:49:52 +08:00
|
|
|
/// allUsesAvailableAt - Return true if all registers used by OrigMI at
|
|
|
|
/// OrigIdx are also available with the same value at UseIdx.
|
|
|
|
bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
|
|
|
|
SlotIndex OrigIdx,
|
2013-03-19 07:40:46 +08:00
|
|
|
SlotIndex UseIdx) const {
|
2011-11-14 04:45:27 +08:00
|
|
|
OrigIdx = OrigIdx.getRegSlot(true);
|
|
|
|
UseIdx = UseIdx.getRegSlot(true);
|
2010-10-15 07:49:52 +08:00
|
|
|
for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = OrigMI->getOperand(i);
|
2012-06-23 01:31:01 +08:00
|
|
|
if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
|
2010-10-15 07:49:52 +08:00
|
|
|
continue;
|
|
|
|
|
2012-06-23 01:31:01 +08:00
|
|
|
// We can't remat physreg uses, unless it is a constant.
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isPhysicalRegister(MO.getReg())) {
|
2016-10-29 02:05:09 +08:00
|
|
|
if (MRI.isConstantPhysReg(MO.getReg()))
|
2012-06-23 01:31:01 +08:00
|
|
|
continue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-03 06:22:53 +08:00
|
|
|
LiveInterval &li = LIS.getInterval(MO.getReg());
|
2010-10-15 07:49:52 +08:00
|
|
|
const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
|
|
|
|
if (!OVNI)
|
|
|
|
continue;
|
2012-10-17 06:51:58 +08:00
|
|
|
|
|
|
|
// Don't allow rematerialization immediately after the original def.
|
|
|
|
// It would be incorrect if OrigMI redefines the register.
|
|
|
|
// See PR14098.
|
|
|
|
if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
|
|
|
|
return false;
|
|
|
|
|
2010-10-15 07:49:52 +08:00
|
|
|
if (OVNI != li.getVNInfoAt(UseIdx))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-13 11:08:27 +08:00
|
|
|
bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
|
|
|
|
SlotIndex UseIdx, bool cheapAsAMove) {
|
2012-05-19 06:10:15 +08:00
|
|
|
assert(ScannedRemattable && "Call anyRematerializable first");
|
2010-10-21 06:00:51 +08:00
|
|
|
|
|
|
|
// Use scanRemattable info.
|
2016-04-13 11:08:27 +08:00
|
|
|
if (!Remattable.count(OrigVNI))
|
2010-11-10 09:05:12 +08:00
|
|
|
return false;
|
2010-10-21 06:00:51 +08:00
|
|
|
|
2011-03-29 11:12:02 +08:00
|
|
|
// No defining instruction provided.
|
|
|
|
SlotIndex DefIdx;
|
2016-04-13 11:08:27 +08:00
|
|
|
assert(RM.OrigMI && "No defining instruction for remattable value");
|
|
|
|
DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
|
2010-10-21 06:00:51 +08:00
|
|
|
|
|
|
|
// If only cheap remats were requested, bail out early.
|
2016-06-30 08:01:54 +08:00
|
|
|
if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
|
2010-11-10 09:05:12 +08:00
|
|
|
return false;
|
2010-10-21 06:00:51 +08:00
|
|
|
|
|
|
|
// Verify that all used registers are available with the same values.
|
2012-04-03 06:22:53 +08:00
|
|
|
if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
|
2010-11-10 09:05:12 +08:00
|
|
|
return false;
|
2010-10-21 06:00:51 +08:00
|
|
|
|
2010-11-10 09:05:12 +08:00
|
|
|
return true;
|
2010-10-21 06:00:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
unsigned DestReg,
|
|
|
|
const Remat &RM,
|
2011-05-02 13:29:58 +08:00
|
|
|
const TargetRegisterInfo &tri,
|
|
|
|
bool Late) {
|
2010-10-21 06:00:51 +08:00
|
|
|
assert(RM.OrigMI && "Invalid remat");
|
2016-06-30 08:01:54 +08:00
|
|
|
TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
|
2016-04-13 11:08:27 +08:00
|
|
|
// DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
|
|
|
|
// to false anyway in case the isDead flag of RM.OrigMI's dest register
|
|
|
|
// is true.
|
|
|
|
(*--MI).getOperand(0).setIsDead(false);
|
2012-05-19 06:10:15 +08:00
|
|
|
Rematted.insert(RM.ParentVNI);
|
2016-04-13 11:08:27 +08:00
|
|
|
return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
|
2010-10-21 06:00:51 +08:00
|
|
|
}
|
|
|
|
|
2020-06-30 23:57:24 +08:00
|
|
|
void LiveRangeEdit::eraseVirtReg(Register Reg) {
|
2012-05-19 06:10:15 +08:00
|
|
|
if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
|
2011-03-13 09:23:11 +08:00
|
|
|
LIS.removeInterval(Reg);
|
|
|
|
}
|
|
|
|
|
2011-04-06 04:20:26 +08:00
|
|
|
bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
|
2012-04-03 06:22:53 +08:00
|
|
|
SmallVectorImpl<MachineInstr*> &Dead) {
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineInstr *DefMI = nullptr, *UseMI = nullptr;
|
2011-04-06 04:20:26 +08:00
|
|
|
|
|
|
|
// Check that there is a single def and a single use.
|
2014-03-18 03:36:09 +08:00
|
|
|
for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
|
|
|
|
MachineInstr *MI = MO.getParent();
|
2011-04-06 04:20:26 +08:00
|
|
|
if (MO.isDef()) {
|
|
|
|
if (DefMI && DefMI != MI)
|
|
|
|
return false;
|
2011-12-07 15:15:52 +08:00
|
|
|
if (!MI->canFoldAsLoad())
|
2011-04-06 04:20:26 +08:00
|
|
|
return false;
|
|
|
|
DefMI = MI;
|
|
|
|
} else if (!MO.isUndef()) {
|
|
|
|
if (UseMI && UseMI != MI)
|
|
|
|
return false;
|
|
|
|
// FIXME: Targets don't know how to fold subreg uses.
|
|
|
|
if (MO.getSubReg())
|
|
|
|
return false;
|
|
|
|
UseMI = MI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!DefMI || !UseMI)
|
|
|
|
return false;
|
|
|
|
|
2012-07-21 05:29:31 +08:00
|
|
|
// Since we're moving the DefMI load, make sure we're not extending any live
|
|
|
|
// ranges.
|
2016-02-27 14:40:41 +08:00
|
|
|
if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
|
|
|
|
LIS.getInstructionIndex(*UseMI)))
|
2012-07-21 05:29:31 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// We also need to make sure it is safe to move the load.
|
|
|
|
// Assume there are stores between DefMI and UseMI.
|
|
|
|
bool SawStore = true;
|
2015-05-20 05:22:20 +08:00
|
|
|
if (!DefMI->isSafeToMove(nullptr, SawStore))
|
2012-07-21 05:29:31 +08:00
|
|
|
return false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
|
|
|
|
<< " into single use: " << *UseMI);
|
2011-04-06 04:20:26 +08:00
|
|
|
|
|
|
|
SmallVector<unsigned, 8> Ops;
|
|
|
|
if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
|
|
|
|
return false;
|
|
|
|
|
2016-06-30 08:01:54 +08:00
|
|
|
MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
|
2011-04-06 04:20:26 +08:00
|
|
|
if (!FoldMI)
|
|
|
|
return false;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " folded: " << *FoldMI);
|
2016-02-27 14:40:41 +08:00
|
|
|
LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
|
2020-02-10 16:49:14 +08:00
|
|
|
// Update the call site info.
|
2020-02-27 18:44:53 +08:00
|
|
|
if (UseMI->shouldUpdateCallSiteInfo())
|
2019-10-08 23:43:12 +08:00
|
|
|
UseMI->getMF()->moveCallSiteInfo(UseMI, FoldMI);
|
2019-06-27 22:31:52 +08:00
|
|
|
UseMI->eraseFromParent();
|
2014-04-14 08:51:57 +08:00
|
|
|
DefMI->addRegisterDead(LI->reg, nullptr);
|
2011-04-06 04:20:26 +08:00
|
|
|
Dead.push_back(DefMI);
|
2011-05-06 01:22:53 +08:00
|
|
|
++NumDCEFoldedLoads;
|
2011-04-06 04:20:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-02 05:26:26 +08:00
|
|
|
bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
|
|
|
|
const MachineOperand &MO) const {
|
2016-02-27 14:40:41 +08:00
|
|
|
const MachineInstr &MI = *MO.getParent();
|
2015-06-02 05:26:26 +08:00
|
|
|
SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
|
|
|
|
if (LI.Query(Idx).isKill())
|
|
|
|
return true;
|
|
|
|
const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
|
|
|
|
unsigned SubReg = MO.getSubReg();
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
|
2015-06-02 05:26:26 +08:00
|
|
|
for (const LiveInterval::SubRange &S : LI.subranges()) {
|
2016-12-17 03:11:56 +08:00
|
|
|
if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
|
2015-06-02 05:26:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-22 02:33:17 +08:00
|
|
|
/// Find all live intervals that need to shrink, then remove the instruction.
|
2016-07-09 05:08:09 +08:00
|
|
|
void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
|
2020-06-26 01:12:55 +08:00
|
|
|
AAResults *AA) {
|
2013-06-22 02:33:17 +08:00
|
|
|
assert(MI->allDefsAreDead() && "Def isn't really dead");
|
2016-02-27 14:40:41 +08:00
|
|
|
SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
|
2011-03-09 06:46:11 +08:00
|
|
|
|
2013-06-22 08:33:48 +08:00
|
|
|
// Never delete a bundled instruction.
|
|
|
|
if (MI->isBundled()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-22 02:33:17 +08:00
|
|
|
// Never delete inline asm.
|
|
|
|
if (MI->isInlineAsm()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
|
2013-06-22 02:33:17 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-03-09 06:46:11 +08:00
|
|
|
|
2013-06-22 02:33:17 +08:00
|
|
|
// Use the same criteria as DeadMachineInstructionElim.
|
|
|
|
bool SawStore = false;
|
2015-05-20 05:22:20 +08:00
|
|
|
if (!MI->isSafeToMove(nullptr, SawStore)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
|
2013-06-22 02:33:17 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-03-09 06:46:11 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
|
2013-06-22 02:33:17 +08:00
|
|
|
|
|
|
|
// Collect virtual registers to be erased after MI is gone.
|
|
|
|
SmallVector<unsigned, 8> RegsToErase;
|
|
|
|
bool ReadsPhysRegs = false;
|
2016-04-13 11:08:27 +08:00
|
|
|
bool isOrigDef = false;
|
|
|
|
unsigned Dest;
|
2016-12-16 03:55:19 +08:00
|
|
|
// Only optimize rematerialize case when the instruction has one def, since
|
|
|
|
// otherwise we could leave some dead defs in the code. This case is
|
|
|
|
// extremely rare.
|
|
|
|
if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
|
|
|
|
MI->getDesc().getNumDefs() == 1) {
|
2016-04-13 11:08:27 +08:00
|
|
|
Dest = MI->getOperand(0).getReg();
|
|
|
|
unsigned Original = VRM->getOriginal(Dest);
|
|
|
|
LiveInterval &OrigLI = LIS.getInterval(Original);
|
|
|
|
VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
|
[LiveRangeEdit] Fix a crash in eliminateDeadDef.
When we delete a live-range, we check if that live-range is the origin of others
to keep it around for rematerialization. For that we check that the instruction
we are about to remove is the same as the definition of the VNI of the original
live-range.
If this is the case, we just shrink the live-range to an empty one.
Now, when we try to delete one of the children of such live-range (product of
splitting), we do the same check.
However, now the original live-range is empty and there is no way we can
access the VNI to check its definition, and we crash.
When we cannot get the VNI for the original live-range, that means we are not in
the presence of the original definition. Thus, this check does not need to happen
in that case and the crash is sloved!
This bug was introduced in r266162 | wmi | 2016-04-12 20:08:27. It affects every
target that uses the greedy register allocator.
To happen, we need to delete both a the original instruction and its split
products, in that order. This is likely to happen when rematerialization comes
into play.
Trying to produce a more robust test case. Will follow in a coming commit.
This fixes llvm.org/PR27983.
rdar://problem/26651519
llvm-svn: 272314
2016-06-10 05:34:31 +08:00
|
|
|
// The original live-range may have been shrunk to
|
|
|
|
// an empty live-range. It happens when it is dead, but
|
|
|
|
// we still keep it around to be able to rematerialize
|
|
|
|
// other values that depend on it.
|
|
|
|
if (OrigVNI)
|
|
|
|
isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
|
2016-04-13 11:08:27 +08:00
|
|
|
}
|
2011-03-09 06:46:11 +08:00
|
|
|
|
2013-06-22 02:33:17 +08:00
|
|
|
// Check for live intervals that may shrink
|
|
|
|
for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
|
|
|
|
MOE = MI->operands_end(); MOI != MOE; ++MOI) {
|
|
|
|
if (!MOI->isReg())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MOI->getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Reg)) {
|
2013-06-22 02:33:17 +08:00
|
|
|
// Check if MI reads any unreserved physregs.
|
|
|
|
if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
|
|
|
|
ReadsPhysRegs = true;
|
2015-01-22 02:50:21 +08:00
|
|
|
else if (MOI->isDef())
|
|
|
|
LIS.removePhysRegDefAt(Reg, Idx);
|
2013-06-22 02:33:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LiveInterval &LI = LIS.getInterval(Reg);
|
|
|
|
|
|
|
|
// Shrink read registers, unless it is likely to be expensive and
|
|
|
|
// unlikely to change anything. We typically don't want to shrink the
|
|
|
|
// PIC base register that has lots of uses everywhere.
|
|
|
|
// Always shrink COPY uses that probably come from live range splitting.
|
2015-06-02 05:26:26 +08:00
|
|
|
if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
|
|
|
|
(MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
|
2013-06-22 02:33:17 +08:00
|
|
|
ToShrink.insert(&LI);
|
|
|
|
|
|
|
|
// Remove defined value.
|
|
|
|
if (MOI->isDef()) {
|
2015-01-22 03:02:30 +08:00
|
|
|
if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
|
|
|
|
TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
|
|
|
|
LIS.removeVRegDefAt(LI, Idx);
|
|
|
|
if (LI.empty())
|
|
|
|
RegsToErase.push_back(Reg);
|
2013-06-22 02:33:17 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-22 22:52:12 +08:00
|
|
|
|
2013-06-22 02:33:17 +08:00
|
|
|
// Currently, we don't support DCE of physreg live ranges. If MI reads
|
|
|
|
// any unreserved physregs, don't erase the instruction, but turn it into
|
|
|
|
// a KILL instead. This way, the physreg live ranges don't end up
|
|
|
|
// dangling.
|
|
|
|
// FIXME: It would be better to have something like shrinkToUses() for
|
|
|
|
// physregs. That could potentially enable more DCE and it would free up
|
|
|
|
// the physreg. It would not happen often, though.
|
|
|
|
if (ReadsPhysRegs) {
|
|
|
|
MI->setDesc(TII.get(TargetOpcode::KILL));
|
|
|
|
// Remove all operands that aren't physregs.
|
|
|
|
for (unsigned i = MI->getNumOperands(); i; --i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i-1);
|
2019-08-02 07:27:28 +08:00
|
|
|
if (MO.isReg() && Register::isPhysicalRegister(MO.getReg()))
|
2013-06-22 02:33:17 +08:00
|
|
|
continue;
|
|
|
|
MI->RemoveOperand(i-1);
|
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
|
2013-06-22 02:33:17 +08:00
|
|
|
} else {
|
2016-07-09 05:08:09 +08:00
|
|
|
// If the dest of MI is an original reg and MI is reMaterializable,
|
|
|
|
// don't delete the inst. Replace the dest with a new reg, and keep
|
|
|
|
// the inst for remat of other siblings. The inst is saved in
|
|
|
|
// LiveRangeEdit::DeadRemats and will be deleted after all the
|
|
|
|
// allocations of the func are done.
|
|
|
|
if (isOrigDef && DeadRemats && TII.isTriviallyReMaterializable(*MI, AA)) {
|
2018-01-11 05:41:02 +08:00
|
|
|
LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
|
2016-04-13 11:08:27 +08:00
|
|
|
VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
|
|
|
|
NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
|
|
|
|
pop_back();
|
2018-01-11 06:36:26 +08:00
|
|
|
DeadRemats->insert(MI);
|
2016-04-13 11:08:27 +08:00
|
|
|
const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
|
|
|
|
MI->substituteRegister(Dest, NewLI.reg, 0, TRI);
|
|
|
|
MI->getOperand(0).setIsDead(true);
|
|
|
|
} else {
|
|
|
|
if (TheDelegate)
|
|
|
|
TheDelegate->LRE_WillEraseInstruction(MI);
|
|
|
|
LIS.RemoveMachineInstrFromMaps(*MI);
|
|
|
|
MI->eraseFromParent();
|
|
|
|
++NumDCEDeleted;
|
|
|
|
}
|
2013-06-22 02:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Erase any virtregs that are now empty and unused. There may be <undef>
|
|
|
|
// uses around. Keep the empty live range in that case.
|
|
|
|
for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
|
2020-06-30 23:57:24 +08:00
|
|
|
Register Reg = RegsToErase[i];
|
2013-06-22 02:33:17 +08:00
|
|
|
if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
|
|
|
|
ToShrink.remove(&LIS.getInterval(Reg));
|
|
|
|
eraseVirtReg(Reg);
|
2011-03-09 06:46:11 +08:00
|
|
|
}
|
2013-06-22 02:33:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 11:08:27 +08:00
|
|
|
void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
|
2020-06-30 23:57:24 +08:00
|
|
|
ArrayRef<Register> RegsBeingSpilled,
|
2020-06-26 01:12:55 +08:00
|
|
|
AAResults *AA) {
|
2013-06-22 02:33:17 +08:00
|
|
|
ToShrinkSet ToShrink;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Erase all dead defs.
|
|
|
|
while (!Dead.empty())
|
2016-07-09 05:08:09 +08:00
|
|
|
eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
|
2011-03-09 06:46:11 +08:00
|
|
|
|
|
|
|
if (ToShrink.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Shrink just one live interval. Then delete new dead defs.
|
2011-03-17 06:56:16 +08:00
|
|
|
LiveInterval *LI = ToShrink.back();
|
2011-03-09 06:46:11 +08:00
|
|
|
ToShrink.pop_back();
|
2012-04-03 06:22:53 +08:00
|
|
|
if (foldAsLoad(LI, Dead))
|
2011-04-06 04:20:26 +08:00
|
|
|
continue;
|
2015-09-22 11:44:41 +08:00
|
|
|
unsigned VReg = LI->reg;
|
2012-05-19 06:10:15 +08:00
|
|
|
if (TheDelegate)
|
2015-09-22 11:44:41 +08:00
|
|
|
TheDelegate->LRE_WillShrinkVirtReg(VReg);
|
2011-03-18 04:37:07 +08:00
|
|
|
if (!LIS.shrinkToUses(LI, &Dead))
|
|
|
|
continue;
|
2013-06-22 02:33:14 +08:00
|
|
|
|
2011-12-13 06:16:27 +08:00
|
|
|
// Don't create new intervals for a register being spilled.
|
|
|
|
// The new intervals would have to be spilled anyway so its not worth it.
|
|
|
|
// Also they currently aren't spilled so creating them and not spilling
|
|
|
|
// them results in incorrect code.
|
|
|
|
bool BeingSpilled = false;
|
|
|
|
for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
|
2015-09-22 11:44:41 +08:00
|
|
|
if (VReg == RegsBeingSpilled[i]) {
|
2011-12-13 06:16:27 +08:00
|
|
|
BeingSpilled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-22 02:33:14 +08:00
|
|
|
|
2011-12-13 06:16:27 +08:00
|
|
|
if (BeingSpilled) continue;
|
2011-03-18 04:37:07 +08:00
|
|
|
|
|
|
|
// LI may have been separated, create new intervals.
|
2013-08-15 01:28:52 +08:00
|
|
|
LI->RenumberValues();
|
2015-09-22 11:44:41 +08:00
|
|
|
SmallVector<LiveInterval*, 8> SplitLIs;
|
|
|
|
LIS.splitSeparateComponents(*LI, SplitLIs);
|
|
|
|
if (!SplitLIs.empty())
|
|
|
|
++NumFracRanges;
|
|
|
|
|
|
|
|
unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
|
|
|
|
for (const LiveInterval *SplitLI : SplitLIs) {
|
2011-07-05 23:38:41 +08:00
|
|
|
// If LI is an original interval that hasn't been split yet, make the new
|
|
|
|
// intervals their own originals instead of referring to LI. The original
|
|
|
|
// interval must contain all the split products, and LI doesn't.
|
2015-09-22 11:44:41 +08:00
|
|
|
if (Original != VReg && Original != 0)
|
|
|
|
VRM->setIsSplitFromReg(SplitLI->reg, Original);
|
2012-05-19 06:10:15 +08:00
|
|
|
if (TheDelegate)
|
2015-09-22 11:44:41 +08:00
|
|
|
TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
|
2011-03-30 10:52:39 +08:00
|
|
|
}
|
2011-03-09 06:46:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-15 07:50:09 +08:00
|
|
|
// Keep track of new virtual registers created via
|
|
|
|
// MachineRegisterInfo::createVirtualRegister.
|
|
|
|
void
|
2020-01-31 05:05:34 +08:00
|
|
|
LiveRangeEdit::MRI_NoteNewVirtualRegister(Register VReg) {
|
2013-08-15 07:50:09 +08:00
|
|
|
if (VRM)
|
|
|
|
VRM->grow();
|
|
|
|
|
|
|
|
NewRegs.push_back(VReg);
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:36 +08:00
|
|
|
void
|
|
|
|
LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
|
|
|
|
const MachineLoopInfo &Loops,
|
|
|
|
const MachineBlockFrequencyInfo &MBFI) {
|
2015-08-10 19:59:44 +08:00
|
|
|
VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI);
|
2013-08-15 07:50:04 +08:00
|
|
|
for (unsigned I = 0, Size = size(); I < Size; ++I) {
|
|
|
|
LiveInterval &LI = LIS.getInterval(get(I));
|
2015-01-27 09:15:16 +08:00
|
|
|
if (MRI.recomputeRegClass(LI.reg))
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2014-11-17 13:50:14 +08:00
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
2017-11-28 20:42:37 +08:00
|
|
|
dbgs() << "Inflated " << printReg(LI.reg) << " to "
|
2014-11-17 13:58:26 +08:00
|
|
|
<< TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
|
2014-11-17 13:50:14 +08:00
|
|
|
});
|
2013-11-12 03:04:45 +08:00
|
|
|
VRAI.calculateSpillWeightAndHint(LI);
|
2011-03-30 05:20:19 +08:00
|
|
|
}
|
|
|
|
}
|