1 ; RUN: opt < %s -nary-reassociate -early-cse -S | FileCheck %s
3 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
4 target triple = "nvptx64-unknown-unknown"
6 declare void @foo(float*)
14 define void @reassociate_gep(float* %a, i64 %i, i64 %j) {
15 ; CHECK-LABEL: @reassociate_gep(
17 %2 = getelementptr float, float* %a, i64 %i
18 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i64 %i
19 call void @foo(float* %2)
20 ; CHECK: call void @foo(float* [[t1]])
21 %3 = getelementptr float, float* %a, i64 %1
22 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 %j
23 call void @foo(float* %3)
24 ; CHECK: call void @foo(float* [[t2]])
29 ; foo(&a[sext(i +nsw j)]);
30 ; foo(&a[sext((i +nsw j) +nsw i)]);
36 ; t3 = t2 + sext(i); // sext(i) should be GVN'ed.
38 define void @reassociate_gep_nsw(float* %a, i32 %i, i32 %j) {
39 ; CHECK-LABEL: @reassociate_gep_nsw(
40 %idxprom.j = sext i32 %j to i64
41 %1 = getelementptr float, float* %a, i64 %idxprom.j
42 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i64 %idxprom.j
43 call void @foo(float* %1)
44 ; CHECK: call void @foo(float* [[t1]])
46 %2 = add nsw i32 %i, %j
47 %idxprom.2 = sext i32 %2 to i64
48 %3 = getelementptr float, float* %a, i64 %idxprom.2
49 ; CHECK: [[sexti:[^ ]+]] = sext i32 %i to i64
50 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 [[sexti]]
51 call void @foo(float* %3)
52 ; CHECK: call void @foo(float* [[t2]])
54 %4 = add nsw i32 %2, %i
55 %idxprom.4 = sext i32 %4 to i64
56 %5 = getelementptr float, float* %a, i64 %idxprom.4
57 ; CHECK: [[t3:[^ ]+]] = getelementptr float, float* [[t2]], i64 [[sexti]]
58 call void @foo(float* %5)
59 ; CHECK: call void @foo(float* [[t3]])
67 ; foo(&a[zext(i + j)]);
73 define void @reassociate_gep_assume(float* %a, i32 %i, i32 %j) {
74 ; CHECK-LABEL: @reassociate_gep_assume(
76 %cmp = icmp sgt i32 %j, -1
77 call void @llvm.assume(i1 %cmp)
79 %cmp2 = icmp sgt i32 %1, -1
80 call void @llvm.assume(i1 %cmp2)
82 %idxprom.j = zext i32 %j to i64
83 %2 = getelementptr float, float* %a, i64 %idxprom.j
84 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i64 %idxprom.j
85 call void @foo(float* %2)
86 ; CHECK: call void @foo(float* [[t1]])
88 %idxprom.1 = zext i32 %1 to i64
89 %3 = getelementptr float, float* %a, i64 %idxprom.1
90 ; CHECK: [[sexti:[^ ]+]] = sext i32 %i to i64
91 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 [[sexti]]
92 call void @foo(float* %3)
93 ; CHECK: call void @foo(float* [[t2]])
98 ; Do not split the second GEP because sext(i + j) != sext(i) + sext(j).
99 define void @reassociate_gep_no_nsw(float* %a, i32 %i, i32 %j) {
100 ; CHECK-LABEL: @reassociate_gep_no_nsw(
102 %2 = getelementptr float, float* %a, i32 %j
103 ; CHECK: getelementptr float, float* %a, i32 %j
104 call void @foo(float* %2)
105 %3 = getelementptr float, float* %a, i32 %1
106 ; CHECK: getelementptr float, float* %a, i32 %1
107 call void @foo(float* %3)
111 define void @reassociate_gep_128(float* %a, i128 %i, i128 %j) {
112 ; CHECK-LABEL: @reassociate_gep_128(
114 %2 = getelementptr float, float* %a, i128 %i
115 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i128 %i
116 call void @foo(float* %2)
117 ; CHECK: call void @foo(float* [[t1]])
118 %3 = getelementptr float, float* %a, i128 %1
119 ; CHECK: [[truncj:[^ ]+]] = trunc i128 %j to i64
120 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 [[truncj]]
121 call void @foo(float* %3)
122 ; CHECK: call void @foo(float* [[t2]])
126 %struct.complex = type { float, float }
128 declare void @bar(%struct.complex*)
130 define void @different_types(%struct.complex* %input, i64 %i) {
131 ; CHECK-LABEL: @different_types(
132 %t1 = getelementptr %struct.complex, %struct.complex* %input, i64 %i
133 call void @bar(%struct.complex* %t1)
135 %t2 = getelementptr %struct.complex, %struct.complex* %input, i64 %j, i32 0
136 ; CHECK: [[cast:[^ ]+]] = bitcast %struct.complex* %t1 to float*
137 ; CHECK-NEXT: %t2 = getelementptr float, float* [[cast]], i64 10
138 ; CHECK-NEXT: call void @foo(float* %t2)
139 call void @foo(float* %t2)
143 declare void @llvm.assume(i1)