[GlobalISel] Introduce G_CONSTANT_FOLD_BARRIER and use it to prevent constant folding
authorAmara Emerson <amara@apple.com>
Thu, 1 Jun 2023 22:11:24 +0000 (15:11 -0700)
committerAmara Emerson <amara@apple.com>
Fri, 9 Jun 2023 18:45:06 +0000 (11:45 -0700)
commit1c2c668846698eab388fe591ba03bfee65a037ed
tree2433163e0c5e7f9b31c516606968cd20846334b4
parenta5ef51d786f241cdd9d1bec62fbc2bf89a766b7d
[GlobalISel] Introduce G_CONSTANT_FOLD_BARRIER and use it to prevent constant folding
hoisted constants.

The constant hoisting pass tries to hoist large constants into predecessors and also
generates remat instructions in terms of the hoisted constants. These aim to prevent
codegen from rematerializing expensive constants multiple times. So we can re-use
this optimization, we can preserve the no-op bitcasts that are used to anchor
constants to the predecessor blocks.

SelectionDAG achieves this by having the OpaqueConstant node, which is just a
normal constant with an opaque flag set. I've opted to avoid introducing a new
constant generic instruction here. Instead, we have a new G_CONSTANT_FOLD_BARRIER
operation that constitutes a folding barrier.

These are somewhat like the optimization hints, G_ASSERT_ZEXT in that they're
eliminated by the generic instruction selection code.

This change by itself has very minor improvements in -Os CTMark overall. What this
does allow is better optimizations when future combines are added that rely on having
expensive constants remain unfolded.

Differential Revision: https://reviews.llvm.org/D144336
llvm/docs/GlobalISel/GenericOpcode.rst
llvm/include/llvm/Support/TargetOpcodes.def
llvm/include/llvm/Target/GenericOpcodes.td
llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp
llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
llvm/test/CodeGen/AArch64/GlobalISel/irtranslator-bitcast.ll
llvm/test/CodeGen/AArch64/GlobalISel/irtranslator-hoisted-constants.ll [new file with mode: 0644]
llvm/test/CodeGen/AArch64/GlobalISel/legalizer-info-validation.mir
llvm/test/CodeGen/AArch64/GlobalISel/localizer-arm64-tti.ll