Skip to content

Commit b2b1a8b

Browse files
committed
[LiveIntervals] Improve repair after convertToThreeAddress
After TwoAddressInstructionPass calls TargetInstrInfo::convertToThreeAddress, improve the LiveIntervals repair to cope with convertToThreeAddress creating more than one new instruction. This mostly seems to benefit X86. For example in test/CodeGen/X86/zext-trunc.ll it converts: %4:gr32 = ADD32rr %3:gr32(tied-def 0), %2:gr32, implicit-def dead $eflags to: undef %6.sub_32bit:gr64 = COPY %3:gr32 undef %7.sub_32bit:gr64_nosp = COPY %2:gr32 %4:gr32 = LEA64_32r killed %6:gr64, 1, killed %7:gr64_nosp, 0, $noreg Differential Revision: https://reviews.llvm.org/D110335
1 parent 90babc8 commit b2b1a8b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

llvm/lib/CodeGen/TwoAddressInstructionPass.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,14 @@ bool TwoAddressInstructionPass::isProfitableToConv3Addr(Register RegA,
590590
bool TwoAddressInstructionPass::convertInstTo3Addr(
591591
MachineBasicBlock::iterator &mi, MachineBasicBlock::iterator &nmi,
592592
Register RegA, Register RegB, unsigned Dist) {
593+
MachineInstrSpan MIS(mi, MBB);
593594
MachineInstr *NewMI = TII->convertToThreeAddress(*mi, LV);
594595
if (!NewMI)
595596
return false;
596597

597598
LLVM_DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi);
598599
LLVM_DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI);
599600

600-
if (LIS)
601-
LIS->ReplaceMachineInstrInMaps(*mi, *NewMI);
602-
603601
// If the old instruction is debug value tracked, an update is required.
604602
if (auto OldInstrNum = mi->peekDebugInstrNum()) {
605603
// Sanity check.
@@ -618,8 +616,26 @@ bool TwoAddressInstructionPass::convertInstTo3Addr(
618616
std::make_pair(NewInstrNum, NewIdx));
619617
}
620618

619+
// If convertToThreeAddress created a single new instruction, assume it has
620+
// exactly the same effect on liveness as the old instruction. This is much
621+
// more efficient than calling repairIntervalsInRange.
622+
bool SingleInst = std::next(MIS.begin(), 2) == MIS.end();
623+
if (LIS && SingleInst)
624+
LIS->ReplaceMachineInstrInMaps(*mi, *NewMI);
625+
626+
SmallVector<Register> OrigRegs;
627+
if (LIS && !SingleInst) {
628+
for (const MachineOperand &MO : mi->operands()) {
629+
if (MO.isReg())
630+
OrigRegs.push_back(MO.getReg());
631+
}
632+
}
633+
621634
MBB->erase(mi); // Nuke the old inst.
622635

636+
if (LIS && !SingleInst)
637+
LIS->repairIntervalsInRange(MBB, MIS.begin(), MIS.end(), OrigRegs);
638+
623639
DistanceMap.insert(std::make_pair(NewMI, Dist));
624640
mi = NewMI;
625641
nmi = std::next(mi);

llvm/test/CodeGen/X86/zext-trunc.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
22
; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s
3+
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -early-live-intervals -verify-machineinstrs | FileCheck %s
34
; rdar://7570931
45

56
define i64 @foo(i64 %a, i64 %b) nounwind {

0 commit comments

Comments
 (0)