[LICM] Don't duplicate instructions just because they're free
authorNikita Popov <npopov@redhat.com>
Wed, 26 Apr 2023 08:00:42 +0000 (10:00 +0200)
committerNikita Popov <npopov@redhat.com>
Fri, 28 Apr 2023 12:31:23 +0000 (14:31 +0200)
commit0659000ff79decc1173aac140d4b0325fe696c57
tree2504e79347ab0b4cf595e81b54c684570e192c60
parent78d8d016490909ac759c6f76c5f8679bc7a58b2e
[LICM] Don't duplicate instructions just because they're free

D37076 makes LICM duplicate instructions into exit blocks if the
instruction is free. For GEPs, the motivation appears to be that
this allows the GEP to be folded into addressing modes, while
non-foldable users outside the loop might prevent this. TBH I don't
think LICM is the place to do this (why doesn't CGP apply this
heuristic itself?) but at least I understand the motivation.

However, the transform is also applied to all other "free"
instructions, which are just that (removed during lowering and not
"folded" in some way). For such instructions, this transform seems
somewhere between useless, counter-productive (undoing CSE/GVN) and
actively incorrect. For example, this transform can duplicate freeze
instructions, which is illegal.

This patch limits the transform to just foldable GEPs, though we
might want to drop it from LICM entirely as a followup.

This is a small compile-time improvement, because querying TTI cost
model for every single instruction is expensive.

Differential Revision: https://reviews.llvm.org/D149136
llvm/lib/Transforms/Scalar/LICM.cpp
llvm/test/CodeGen/PowerPC/atomicrmw-uinc-udec-wrap.ll
llvm/test/Transforms/LICM/pr23608.ll
llvm/test/Transforms/LICM/sink-foldable.ll
llvm/test/Transforms/LICM/sinking.ll