Skip to content

Commit

Permalink
[LiveIntervals] Improve repair after convertToThreeAddress
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jayfoad committed Sep 28, 2021
1 parent 90babc8 commit b2b1a8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions llvm/lib/CodeGen/TwoAddressInstructionPass.cpp
Expand Up @@ -590,16 +590,14 @@ bool TwoAddressInstructionPass::isProfitableToConv3Addr(Register RegA,
bool TwoAddressInstructionPass::convertInstTo3Addr(
MachineBasicBlock::iterator &mi, MachineBasicBlock::iterator &nmi,
Register RegA, Register RegB, unsigned Dist) {
MachineInstrSpan MIS(mi, MBB);
MachineInstr *NewMI = TII->convertToThreeAddress(*mi, LV);
if (!NewMI)
return false;

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

if (LIS)
LIS->ReplaceMachineInstrInMaps(*mi, *NewMI);

// If the old instruction is debug value tracked, an update is required.
if (auto OldInstrNum = mi->peekDebugInstrNum()) {
// Sanity check.
Expand All @@ -618,8 +616,26 @@ bool TwoAddressInstructionPass::convertInstTo3Addr(
std::make_pair(NewInstrNum, NewIdx));
}

// If convertToThreeAddress created a single new instruction, assume it has
// exactly the same effect on liveness as the old instruction. This is much
// more efficient than calling repairIntervalsInRange.
bool SingleInst = std::next(MIS.begin(), 2) == MIS.end();
if (LIS && SingleInst)
LIS->ReplaceMachineInstrInMaps(*mi, *NewMI);

SmallVector<Register> OrigRegs;
if (LIS && !SingleInst) {
for (const MachineOperand &MO : mi->operands()) {
if (MO.isReg())
OrigRegs.push_back(MO.getReg());
}
}

MBB->erase(mi); // Nuke the old inst.

if (LIS && !SingleInst)
LIS->repairIntervalsInRange(MBB, MIS.begin(), MIS.end(), OrigRegs);

DistanceMap.insert(std::make_pair(NewMI, Dist));
mi = NewMI;
nmi = std::next(mi);
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/zext-trunc.ll
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s
; RUN: llc < %s -mtriple=x86_64-unknown-unknown -early-live-intervals -verify-machineinstrs | FileCheck %s
; rdar://7570931

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

0 comments on commit b2b1a8b

Please sign in to comment.