1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2 ; RUN: opt -S -passes=globalopt < %s | FileCheck %s
4 ; Test behavior of trying to fold access of global variables
5 ; initialized one time through addrspacecast.
7 @as0_ptr = internal global ptr null
8 @as1_ptr = internal global ptr addrspace(1) null
9 @as2_ptr = internal global ptr addrspace(2) null
10 @as1_null_valid_ptr = internal global ptr addrspace(1) null
11 @as0_null_valid_ptr = internal global ptr null
13 @Actual.as0 = internal global i32 1234
14 @Actual.as1 = internal addrspace(1) global i32 5678
15 @Actual.as2 = internal addrspace(2) global i32 91011
16 @Actual.as1.null.valid = internal addrspace(1) global i32 91011
17 @Actual.as0.null.valid = internal global i32 9999
19 ; Initialize each global once.
21 ; CHECK-LABEL: define void @init() local_unnamed_addr {
22 ; CHECK-NEXT: store ptr addrspace(1) @Actual.as1, ptr @as1_ptr, align 8
23 ; CHECK-NEXT: store ptr addrspace(2) @Actual.as2, ptr @as2_ptr, align 8
24 ; CHECK-NEXT: store ptr addrspace(1) @Actual.as1.null.valid, ptr @as1_null_valid_ptr, align 8
25 ; CHECK-NEXT: store ptr @Actual.as0.null.valid, ptr @as0_null_valid_ptr, align 8
26 ; CHECK-NEXT: ret void
28 store ptr @Actual.as0, ptr @as0_ptr
29 store ptr addrspace(1) @Actual.as1, ptr @as1_ptr
30 store ptr addrspace(2) @Actual.as2, ptr @as2_ptr
31 store ptr addrspace(1) @Actual.as1.null.valid, ptr @as1_null_valid_ptr
32 store ptr @Actual.as0.null.valid, ptr @as0_null_valid_ptr
36 ; Access stored value is null in the default address space, which we
37 ; can assume cannot alias a valid object in another address space.
38 define i32 @load_from_as0_ptr_in_as1() {
39 ; CHECK-LABEL: define i32 @load_from_as0_ptr_in_as1() local_unnamed_addr {
40 ; CHECK-NEXT: ret i32 1234
42 %ptr = load ptr, ptr @as0_ptr
43 %cast = addrspacecast ptr %ptr to ptr addrspace(1)
44 %load = load i32, ptr addrspace(1) %cast
48 ; Cast is to default address space and access is done through default
49 ; address space in a function where null is valid.
50 define i32 @load_from_as0_ptr_in_as1_null_is_valid() null_pointer_is_valid {
51 ; CHECK-LABEL: define i32 @load_from_as0_ptr_in_as1_null_is_valid
52 ; CHECK-SAME: () local_unnamed_addr #[[ATTR0:[0-9]+]] {
53 ; CHECK-NEXT: [[PTR:%.*]] = load ptr addrspace(1), ptr @as1_null_valid_ptr, align 8
54 ; CHECK-NEXT: [[CAST:%.*]] = addrspacecast ptr addrspace(1) [[PTR]] to ptr
55 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[CAST]], align 4
56 ; CHECK-NEXT: ret i32 [[LOAD]]
58 %ptr = load ptr addrspace(1), ptr @as1_null_valid_ptr
59 %cast = addrspacecast ptr addrspace(1) %ptr to ptr
60 %load = load i32, ptr %cast
64 ; Original addrspace(0) null was valid, and we also have to assume the
65 ; access is valid in addrspace(1)
66 define i32 @load_from_as1_ptr_in_as0_null_is_valid() null_pointer_is_valid {
67 ; CHECK-LABEL: define i32 @load_from_as1_ptr_in_as0_null_is_valid
68 ; CHECK-SAME: () local_unnamed_addr #[[ATTR0]] {
69 ; CHECK-NEXT: [[PTR:%.*]] = load ptr, ptr @as0_null_valid_ptr, align 8
70 ; CHECK-NEXT: [[CAST:%.*]] = addrspacecast ptr [[PTR]] to ptr addrspace(1)
71 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr addrspace(1) [[CAST]], align 4
72 ; CHECK-NEXT: ret i32 [[LOAD]]
74 %ptr = load ptr, ptr @as0_null_valid_ptr
75 %cast = addrspacecast ptr %ptr to ptr addrspace(1)
76 %load = load i32, ptr addrspace(1) %cast
80 ; Underlying pointer could be valid, can't fold this out.
81 define i32 @load_from_as1_ptr_in_as0() {
82 ; CHECK-LABEL: define i32 @load_from_as1_ptr_in_as0() local_unnamed_addr {
83 ; CHECK-NEXT: [[PTR:%.*]] = load ptr addrspace(1), ptr @as1_ptr, align 8
84 ; CHECK-NEXT: [[CAST:%.*]] = addrspacecast ptr addrspace(1) [[PTR]] to ptr
85 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[CAST]], align 4
86 ; CHECK-NEXT: ret i32 [[LOAD]]
88 %ptr = load ptr addrspace(1), ptr @as1_ptr
89 %cast = addrspacecast ptr addrspace(1) %ptr to ptr
90 %load = load i32, ptr %cast
94 ; Two non-default address spaces, can't fold.
95 define i32 @load_from_as2_ptr_in_as1() {
96 ; CHECK-LABEL: define i32 @load_from_as2_ptr_in_as1() local_unnamed_addr {
97 ; CHECK-NEXT: [[PTR:%.*]] = load ptr addrspace(2), ptr @as2_ptr, align 8
98 ; CHECK-NEXT: [[CAST:%.*]] = addrspacecast ptr addrspace(2) [[PTR]] to ptr addrspace(1)
99 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr addrspace(1) [[CAST]], align 4
100 ; CHECK-NEXT: ret i32 [[LOAD]]
102 %ptr = load ptr addrspace(2), ptr @as2_ptr
103 %cast = addrspacecast ptr addrspace(2) %ptr to ptr addrspace(1)
104 %load = load i32, ptr addrspace(1) %cast