[InstCombine] Preserve instruction name in replaceInstUsesWith()
authorNikita Popov <npopov@redhat.com>
Fri, 16 Dec 2022 08:45:36 +0000 (09:45 +0100)
committerNikita Popov <npopov@redhat.com>
Fri, 16 Dec 2022 15:01:25 +0000 (16:01 +0100)
commit379de1239ee3eb17ca37035c069afcc1d5e04d27
tree0f963b782f6901fd2791634951f6a098a8c8071b
parentcb3f415cd2019df7d14683842198bc4b7a492bc5
[InstCombine] Preserve instruction name in replaceInstUsesWith()

Currently InstCombine folds using the
`return replaceInstUsesWith(V, Builder.CreateFoo())`
pattern do not preserve the original name of the instruction.
To preserve the name, you either have to use something like
`return FooInst::Create(...)` which is usually less nice, or go
out of the way to preserve the name with takeName(). We often
don't do that.

This patch instead preserves the name in replaceInstUsesWith()
when replacing a named instruction with an unnamed instruction.
To be conservative, I also added a zero-use check, which is a
proxy for the case where the instruction was just created, rather
than an existing one reused. Possibly we could drop that part.

As InstCombine tests are robust against renames this does not
cause any test diffs, so I regenerated a random test to show the
effects.

Differential Revision: https://reviews.llvm.org/D140192
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/test/Transforms/InstCombine/minmax-fold.ll