[SelectionDAG] Fix EmitCopyFromReg for cloned nodes
authorJay Foad <jay.foad@amd.com>
Tue, 20 Dec 2022 17:19:01 +0000 (17:19 +0000)
committerJay Foad <jay.foad@amd.com>
Wed, 21 Dec 2022 10:44:45 +0000 (10:44 +0000)
commite73b35699be0166d2de8cbb7cb6ac544a3e63f5f
tree1a0e391e43a87749f6e9b061cbb5f2d777aac317
parent087cd5e5d16759f41b0701545b6ae11c95cf1fae
[SelectionDAG] Fix EmitCopyFromReg for cloned nodes

Change EmitCopyFromReg to check all users of cloned nodes (as well as
non-cloned nodes) instead of assuming that they all copy the defined
value back to the same physical register.

This partially reverts 968e2e7b3db1 (svn r62356) which claimed:

  CreateVirtualRegisters does trivial copy coalescing. If a node def is
  used by a single CopyToReg, it reuses the virtual register assigned to
  the CopyToReg. This won't work for SDNode that is a clone or is itself
  cloned. Disable this optimization for those nodes or it can end up
  with non-SSA machine instructions.

This is true for CreateVirtualRegisters but r62356 also updated
EmitCopyFromReg where it is not true. Firstly EmitCopyFromReg only
coalesces physical register copies, so the concern about SSA form does
not apply. Secondly making the loop over users in EmitCopyFromReg
conditional on `!IsClone && !IsCloned` breaks the handling of cloned
nodes, because it leaves MatchReg set to true by default, so it assumes
that all users will copy the defined value back to the same physical
register instead of actually checking.

Differential Revision: https://reviews.llvm.org/D140417
llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp
llvm/lib/CodeGen/SelectionDAG/InstrEmitter.h
llvm/test/CodeGen/AMDGPU/bug-sdag-emitcopyfromreg.ll