llvm/llvm/test/CodeGen/AArch64/GlobalISel/fold-global-offsets-target-features.mir

# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -mtriple aarch64-unknown-unknown -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=DEFAULT,CHECK
# RUN: llc -mtriple aarch64-apple-darwin -code-model=large -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=LARGE-MACHO,CHECK
# RUN: llc -mtriple aarch64-apple-darwin -code-model=small -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=SMALL-MACHO,CHECK
# RUN: llc -mtriple aarch64-linux-elf -code-model=large -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=LARGE-ELF,CHECK
# RUN: llc -mtriple aarch64-linux-elf -code-model=tiny -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=TINY,CHECK
# RUN: llc -mtriple aarch64-windows-coff -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=WINDOWS,CHECK

# Each of these tests has a trivial pattern for folding a G_PTR_ADD into a
# G_GLOBAL_VALUE.
#
# Check that given different code models/target features, we do/don't fold.

--- |
    @external_linkage = external hidden global i32
    @common_linkage = common local_unnamed_addr global i32 0, align 4
    @internal_linkage = internal unnamed_addr global i32 0, align 4
    @extern_weak_linkage = extern_weak hidden global i32
    @dll_import = external dllimport global i32

    define void @test_external_linkage() { ret void }
    define void @test_internal_linkage() { ret void }
    define void @test_common_linkage() { ret void }
    define void @test_extern_weak_linkage() { ret void }
    define void @never_fold_tagged_globals() #0 { ret void }
    define void @test_dll_import() { ret void }

    attributes #0 = { "target-features"="+tagged-globals" }
...
---
name:            test_external_linkage
alignment:       4
tracksRegLiveness: true
machineFunctionInfo: {}
body:             |
  bb.0:
    ; Large + Mach-O goes via GOT, so we can't fold.

    ; DEFAULT-LABEL: name: test_external_linkage
    ; DEFAULT: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @external_linkage + 1
    ; DEFAULT: $x0 = COPY [[GV]](p0)
    ; DEFAULT: RET_ReallyLR implicit $x0
    ; LARGE-MACHO-LABEL: name: test_external_linkage
    ; LARGE-MACHO: %global:_(p0) = G_GLOBAL_VALUE @external_linkage
    ; LARGE-MACHO: %imm:_(s64) = G_CONSTANT i64 1
    ; LARGE-MACHO: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; LARGE-MACHO: $x0 = COPY %ptr_add(p0)
    ; LARGE-MACHO: RET_ReallyLR implicit $x0
    ; SMALL-MACHO-LABEL: name: test_external_linkage
    ; SMALL-MACHO: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @external_linkage + 1
    ; SMALL-MACHO: $x0 = COPY [[GV]](p0)
    ; SMALL-MACHO: RET_ReallyLR implicit $x0
    ; LARGE-ELF-LABEL: name: test_external_linkage
    ; LARGE-ELF: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @external_linkage + 1
    ; LARGE-ELF: $x0 = COPY [[GV]](p0)
    ; LARGE-ELF: RET_ReallyLR implicit $x0
    ; TINY-LABEL: name: test_external_linkage
    ; TINY: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @external_linkage + 1
    ; TINY: $x0 = COPY [[GV]](p0)
    ; TINY: RET_ReallyLR implicit $x0
    ; WINDOWS-LABEL: name: test_external_linkage
    ; WINDOWS: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @external_linkage + 1
    ; WINDOWS: $x0 = COPY [[GV]](p0)
    ; WINDOWS: RET_ReallyLR implicit $x0
    %global:_(p0) = G_GLOBAL_VALUE @external_linkage
    %imm:_(s64) = G_CONSTANT i64 1
    %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    $x0 = COPY %ptr_add(p0)
    RET_ReallyLR implicit $x0

...
---
name:            test_internal_linkage
alignment:       4
tracksRegLiveness: true
machineFunctionInfo: {}
body:             |
  bb.0:
    ; Large + Mach-O goes via GOT, so we can't fold.

    ; DEFAULT-LABEL: name: test_internal_linkage
    ; DEFAULT: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @internal_linkage + 1
    ; DEFAULT: $x0 = COPY [[GV]](p0)
    ; DEFAULT: RET_ReallyLR implicit $x0
    ; LARGE-MACHO-LABEL: name: test_internal_linkage
    ; LARGE-MACHO: %global:_(p0) = G_GLOBAL_VALUE @internal_linkage
    ; LARGE-MACHO: %imm:_(s64) = G_CONSTANT i64 1
    ; LARGE-MACHO: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; LARGE-MACHO: $x0 = COPY %ptr_add(p0)
    ; LARGE-MACHO: RET_ReallyLR implicit $x0
    ; SMALL-MACHO-LABEL: name: test_internal_linkage
    ; SMALL-MACHO: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @internal_linkage + 1
    ; SMALL-MACHO: $x0 = COPY [[GV]](p0)
    ; SMALL-MACHO: RET_ReallyLR implicit $x0
    ; LARGE-ELF-LABEL: name: test_internal_linkage
    ; LARGE-ELF: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @internal_linkage + 1
    ; LARGE-ELF: $x0 = COPY [[GV]](p0)
    ; LARGE-ELF: RET_ReallyLR implicit $x0
    ; TINY-LABEL: name: test_internal_linkage
    ; TINY: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @internal_linkage + 1
    ; TINY: $x0 = COPY [[GV]](p0)
    ; TINY: RET_ReallyLR implicit $x0
    ; WINDOWS-LABEL: name: test_internal_linkage
    ; WINDOWS: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @internal_linkage + 1
    ; WINDOWS: $x0 = COPY [[GV]](p0)
    ; WINDOWS: RET_ReallyLR implicit $x0
    %global:_(p0) = G_GLOBAL_VALUE @internal_linkage
    %imm:_(s64) = G_CONSTANT i64 1
    %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    $x0 = COPY %ptr_add(p0)
    RET_ReallyLR implicit $x0

...
---
name:            test_common_linkage
alignment:       4
tracksRegLiveness: true
machineFunctionInfo: {}
body:             |
  bb.0:
    ; DEFAULT-LABEL: name: test_common_linkage
    ; DEFAULT: %global:_(p0) = G_GLOBAL_VALUE @common_linkage
    ; DEFAULT: %imm:_(s64) = G_CONSTANT i64 1
    ; DEFAULT: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; DEFAULT: $x0 = COPY %ptr_add(p0)
    ; DEFAULT: RET_ReallyLR implicit $x0
    ; LARGE-MACHO-LABEL: name: test_common_linkage
    ; LARGE-MACHO: %global:_(p0) = G_GLOBAL_VALUE @common_linkage
    ; LARGE-MACHO: %imm:_(s64) = G_CONSTANT i64 1
    ; LARGE-MACHO: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; LARGE-MACHO: $x0 = COPY %ptr_add(p0)
    ; LARGE-MACHO: RET_ReallyLR implicit $x0
    ; SMALL-MACHO-LABEL: name: test_common_linkage
    ; SMALL-MACHO: %global:_(p0) = G_GLOBAL_VALUE @common_linkage
    ; SMALL-MACHO: %imm:_(s64) = G_CONSTANT i64 1
    ; SMALL-MACHO: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; SMALL-MACHO: $x0 = COPY %ptr_add(p0)
    ; SMALL-MACHO: RET_ReallyLR implicit $x0
    ; LARGE-ELF-LABEL: name: test_common_linkage
    ; LARGE-ELF: %global:_(p0) = G_GLOBAL_VALUE @common_linkage
    ; LARGE-ELF: %imm:_(s64) = G_CONSTANT i64 1
    ; LARGE-ELF: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; LARGE-ELF: $x0 = COPY %ptr_add(p0)
    ; LARGE-ELF: RET_ReallyLR implicit $x0
    ; TINY-LABEL: name: test_common_linkage
    ; TINY: %global:_(p0) = G_GLOBAL_VALUE @common_linkage
    ; TINY: %imm:_(s64) = G_CONSTANT i64 1
    ; TINY: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; TINY: $x0 = COPY %ptr_add(p0)
    ; TINY: RET_ReallyLR implicit $x0
    ; WINDOWS-LABEL: name: test_common_linkage
    ; WINDOWS: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @common_linkage + 1
    ; WINDOWS: $x0 = COPY [[GV]](p0)
    ; WINDOWS: RET_ReallyLR implicit $x0
    %global:_(p0) = G_GLOBAL_VALUE @common_linkage
    %imm:_(s64) = G_CONSTANT i64 1
    %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    $x0 = COPY %ptr_add(p0)
    RET_ReallyLR implicit $x0

...
---
name:            test_extern_weak_linkage
alignment:       4
tracksRegLiveness: true
machineFunctionInfo: {}
body:             |
  bb.0:
    ; DEFAULT-LABEL: name: test_extern_weak_linkage
    ; DEFAULT: %global:_(p0) = G_GLOBAL_VALUE @extern_weak_linkage
    ; DEFAULT: %imm:_(s64) = G_CONSTANT i64 1
    ; DEFAULT: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; DEFAULT: $x0 = COPY %ptr_add(p0)
    ; DEFAULT: RET_ReallyLR implicit $x0
    ; LARGE-MACHO-LABEL: name: test_extern_weak_linkage
    ; LARGE-MACHO: %global:_(p0) = G_GLOBAL_VALUE @extern_weak_linkage
    ; LARGE-MACHO: %imm:_(s64) = G_CONSTANT i64 1
    ; LARGE-MACHO: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; LARGE-MACHO: $x0 = COPY %ptr_add(p0)
    ; LARGE-MACHO: RET_ReallyLR implicit $x0
    ; SMALL-MACHO-LABEL: name: test_extern_weak_linkage
    ; SMALL-MACHO: %global:_(p0) = G_GLOBAL_VALUE @extern_weak_linkage
    ; SMALL-MACHO: %imm:_(s64) = G_CONSTANT i64 1
    ; SMALL-MACHO: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; SMALL-MACHO: $x0 = COPY %ptr_add(p0)
    ; SMALL-MACHO: RET_ReallyLR implicit $x0
    ; LARGE-ELF-LABEL: name: test_extern_weak_linkage
    ; LARGE-ELF: %global:_(p0) = G_GLOBAL_VALUE @extern_weak_linkage
    ; LARGE-ELF: %imm:_(s64) = G_CONSTANT i64 1
    ; LARGE-ELF: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; LARGE-ELF: $x0 = COPY %ptr_add(p0)
    ; LARGE-ELF: RET_ReallyLR implicit $x0
    ; TINY-LABEL: name: test_extern_weak_linkage
    ; TINY: %global:_(p0) = G_GLOBAL_VALUE @extern_weak_linkage
    ; TINY: %imm:_(s64) = G_CONSTANT i64 1
    ; TINY: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; TINY: $x0 = COPY %ptr_add(p0)
    ; TINY: RET_ReallyLR implicit $x0
    ; WINDOWS-LABEL: name: test_extern_weak_linkage
    ; WINDOWS: %global:_(p0) = G_GLOBAL_VALUE @extern_weak_linkage
    ; WINDOWS: %imm:_(s64) = G_CONSTANT i64 1
    ; WINDOWS: %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    ; WINDOWS: $x0 = COPY %ptr_add(p0)
    ; WINDOWS: RET_ReallyLR implicit $x0
    %global:_(p0) = G_GLOBAL_VALUE @extern_weak_linkage
    %imm:_(s64) = G_CONSTANT i64 1
    %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    $x0 = COPY %ptr_add(p0)
    RET_ReallyLR implicit $x0

...
---
name:            never_fold_tagged_globals
alignment:       4
tracksRegLiveness: true
machineFunctionInfo: {}
body:             |
  bb.0:
    ; CHECK-LABEL: name: never_fold_tagged_globals
    ; CHECK-NOT: %global:_(p0) = G_GLOBAL_VALUE @external_linkage + 1
    %global:_(p0) = G_GLOBAL_VALUE @external_linkage
    %imm:_(s64) = G_CONSTANT i64 1
    %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    $x0 = COPY %ptr_add(p0)
    RET_ReallyLR implicit $x0

...
---
name:            test_dll_import
alignment:       4
tracksRegLiveness: true
machineFunctionInfo: {}
body:             |
  bb.0:
    ; CHECK-LABEL: name: test_dll_import
    ; CHECK-NOT: %global:_(p0) = G_GLOBAL_VALUE @dll_import + 1
    %global:_(p0) = G_GLOBAL_VALUE @dll_import
    %imm:_(s64) = G_CONSTANT i64 1
    %ptr_add:_(p0) = G_PTR_ADD %global, %imm(s64)
    $x0 = COPY %ptr_add(p0)
    RET_ReallyLR implicit $x0