Fix late rematerialization operands check
authorStanislav Mekhanoshin <Stanislav.Mekhanoshin@amd.com>
Fri, 20 Aug 2021 17:14:31 +0000 (10:14 -0700)
committerStanislav Mekhanoshin <Stanislav.Mekhanoshin@amd.com>
Mon, 23 Aug 2021 19:23:58 +0000 (12:23 -0700)
commit401a45c61bac76378735ef94fd507e5fbf763edc
tree1e30675ef6f2e100a8eab5a847d6dffdcd7179be
parentb575bbd0c78c462d2fd7ca4c1a2ae76632d2a6bc
Fix late rematerialization operands check

D106408 enables rematerialization of instructions with virtual
register uses. That has uncovered the bug in the allUsesAvailableAt
implementation: https://bugs.llvm.org/show_bug.cgi?id=51516.

In the majority of cases canRematerializeAt() called to check if
an instruction can be rematerialized before the given UseIdx.
However, SplitEditor::enterIntvAtEnd() calls it to rematerialize
an instruction at the end of a block passing LIS.getMBBEndIdx()
into the check. In the testcase from the bug it has attempted to
rematerialize ADDXri after STRXui in bb.17. The use operand %55
of the ADD is killed by the STRX but that is undetected by the check
because it adjusts passed UseIdx to the reg slot, before the kill.
The value is dead at the index passed to the check however.

This change uses a later of passed UseIdx and its reg slot. This
shall be correct because if are checking an availability of operands
before an instruction that instruction cannot be the one defining
these operands. If we are checking for late rematerialization we
are really interested if operands live past the instruction.

The bug is not exploitable without D106408 but needed to reland
reverted D106408.

Differential Revision: https://reviews.llvm.org/D108475
llvm/lib/CodeGen/LiveRangeEdit.cpp
llvm/test/CodeGen/AArch64/pr51516.mir [new file with mode: 0644]
llvm/test/CodeGen/AMDGPU/pr51516.mir [new file with mode: 0644]