[AArch64][GlobalISel] Fold constants into G_GLOBAL_VALUE
authorJessica Paquette <jpaquette@apple.com>
Fri, 12 Feb 2021 01:00:00 +0000 (17:00 -0800)
committerJessica Paquette <jpaquette@apple.com>
Fri, 12 Feb 2021 22:55:15 +0000 (14:55 -0800)
commit61b4702a408834228c1c139b0e9af98616774db4
treec716ab52e6fefedd8cc4acd84c8601aec57748ec
parent60a55337e012b0c1739cad0f1b93fb35a7a0053f
[AArch64][GlobalISel] Fold constants into G_GLOBAL_VALUE

This is pretty much just ports `performGlobalAddressCombine` from
AArch64ISelLowering. (AArch64 doesn't use the generic DAG combine for this.)

This adds a pre-legalize combine which looks for this pattern:

```
  %g = G_GLOBAL_VALUE @x
  %ptr1 = G_PTR_ADD %g, cst1
  %ptr2 = G_PTR_ADD %g, cst2
  ...
  %ptrN = G_PTR_ADD %g, cstN
```

And then, if possible, transforms it like so:

```
  %g = G_GLOBAL_VALUE @x
  %offset_g = G_PTR_ADD %g, -min(cst)
  %ptr1 = G_PTR_ADD %offset_g, cst1
  %ptr2 = G_PTR_ADD %offset_g, cst2
  ...
  %ptrN = G_PTR_ADD %offset_g, cstN
```

Where min(cst) is the smallest out of the G_PTR_ADD constants.

This means we should save at least one G_PTR_ADD.

This also updates code in the legalizer + selector which assumes that
G_GLOBAL_VALUE will never have an offset and adds/updates relevant tests.

Differential Revision: https://reviews.llvm.org/D96624
12 files changed:
llvm/lib/Target/AArch64/AArch64Combine.td
llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp
llvm/lib/Target/AArch64/GISel/AArch64PreLegalizerCombiner.cpp
llvm/test/CodeGen/AArch64/GlobalISel/fold-global-offsets-target-features.mir [new file with mode: 0644]
llvm/test/CodeGen/AArch64/GlobalISel/fold-global-offsets.mir [new file with mode: 0644]
llvm/test/CodeGen/AArch64/GlobalISel/legalize-global-pic.mir
llvm/test/CodeGen/AArch64/GlobalISel/legalize-global.mir
llvm/test/CodeGen/AArch64/GlobalISel/select-add-low.mir [new file with mode: 0644]
llvm/test/CodeGen/AArch64/GlobalISel/select-gv-with-offset.mir [new file with mode: 0644]
llvm/test/CodeGen/AArch64/GlobalISel/select-store.mir
llvm/test/CodeGen/AArch64/fold-global-offsets.ll