This is an archive of the discontinued LLVM Phabricator instance.

[PHIElimination] Fix the killed flag for LowerPHINode()
ClosedPublic

Authored by ZhangKang on May 31 2020, 3:09 AM.

Details

Summary

In the phi-node-elimination pass, we set the killed flag incorrectly.
When we eliminate the PHI node, we replace the PHI with a copy for the incoming value.

Before this patch, we will set incoming value as killed(PHICopy). And we will remove the killed flag
from last using incoming value(OldKill). This is correct, only if the new PHICopy is after the OldKill.

Below is an example:

%35:gprc = PHI %26, %bb.2, %28, %bb.3
%36:gprc = PHI %19, %bb.2, %35, %bb.3
%37:gprc = PHI %26, %bb.2, %28, %bb.3
%38:g8rc_and_g8rc_nox0 = PHI %25, %bb.2, %34, %bb.3

After phi-node-elimination pass, we will get:

%38:g8rc_and_g8rc_nox0 = COPY killed %59
%37:gprc = COPY killed %57
%36:gprc = COPY killed %58
%35:gprc = COPY %57

It's obvious that, we have set the wrong killed flag for %57, if we enabled the verification for
phi-node-elimination pass, we will get the error Using a killed virtual register.

In fact, the right output should be this:

%38:g8rc_and_g8rc_nox0 = COPY killed %59
%37:gprc = COPY %57
%36:gprc = COPY killed %58
%35:gprc = COPY killed %57

This patch is to fix above error, it can fix below cases:

LLVM :: CodeGen/Hexagon/late_instr.ll
LLVM :: CodeGen/Hexagon/rdf-filter-defs.ll
LLVM :: CodeGen/Hexagon/swp-epilog-phi11.ll
LLVM :: CodeGen/Hexagon/swp-epilog-phi13.ll
LLVM :: CodeGen/Hexagon/swp-epilog-phi7.ll
LLVM :: CodeGen/Hexagon/swp-lots-deps.ll
LLVM :: CodeGen/Hexagon/swp-phi-def-use.ll
LLVM :: CodeGen/Hexagon/swp-phi-dep1.ll
LLVM :: CodeGen/Hexagon/swp-phi.ll
LLVM :: CodeGen/Hexagon/swp-stages5.ll
LLVM :: CodeGen/PowerPC/sms-phi-2.ll
LLVM :: CodeGen/Thumb/2009-08-12-ConstIslandAssert.ll
LLVM :: CodeGen/X86/2009-04-27-CoalescerAssert.ll
LLVM :: CodeGen/X86/avx512-masked_memop-16-8.ll
LLVM :: CodeGen/X86/crash.ll
LLVM :: CodeGen/X86/mmx-coalescing.ll
LLVM :: CodeGen/X86/statepoint-vector.ll

Diff Detail

Event Timeline

ZhangKang created this revision.May 31 2020, 3:09 AM
bjope added inline comments.Jun 3 2020, 6:29 AM
llvm/lib/CodeGen/PHIElimination.cpp
327–344

This findKill was guarded by if (reusedIncoming) in the past. I guess it won't hurt to keep it that way (to avoid any unpleasant surprises with build speeds, not that I think it will cause any big problems but OldKill is only of interest if reusedIncoming is true).

llvm/test/CodeGen/PowerPC/phi-eliminate.mir
174

Could be seen as an unrelated comment, but I've always wondered why PHIElimination swap the order of the defs when converting PHIs to COPYs. I guess it could be easier to deal with the liveness attributes such as "killed" is not reversing the order (but I guess those target hooks, that makes it impossible to know where the COPY is inserted, would still be a nuisance).

ZhangKang updated this revision to Diff 268247.Jun 3 2020, 10:10 AM
ZhangKang marked 4 inline comments as done.

Add guard for OldKill.

llvm/lib/CodeGen/PHIElimination.cpp
327–344

I will use reusedIncoming to guard the OldKill.

llvm/test/CodeGen/PowerPC/phi-eliminate.mir
174

In fact, for general cases without target hook, reversing the order is easier.

If it's not reversed order, you can think the IsPHICopyAfterOldKill is always true, I need remove the killed flag for OldKill and add killed for the coming reg. The killed flag added for the OldKill is not needed.

But if it's reversed order, you can think the IsPHICopyAfterOldKill is always false, I only need consider(line 362) whether I should add killed for the coming reg,

bjope accepted this revision.Jun 4 2020, 11:42 AM

LGTM, but you might wanna wait a day or two before landing this, in case someone else got some opinions about it.

This revision is now accepted and ready to land.Jun 4 2020, 11:42 AM
This revision was landed with ongoing or failed builds.Jul 30 2020, 1:19 AM
This revision was automatically updated to reflect the committed changes.