1 ; RUN: opt < %s -nary-reassociate -early-cse -earlycse-debug-hash -S | FileCheck %s
2 ; RUN: opt < %s -passes='nary-reassociate' -S | opt -early-cse -S | FileCheck %s
4 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
5 target triple = "nvptx64-unknown-unknown"
7 declare void @foo(float*)
15 define void @reassociate_gep(float* %a, i64 %i, i64 %j) {
16 ; CHECK-LABEL: @reassociate_gep(
18 %2 = getelementptr float, float* %a, i64 %i
19 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i64 %i
20 call void @foo(float* %2)
21 ; CHECK: call void @foo(float* [[t1]])
22 %3 = getelementptr float, float* %a, i64 %1
23 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 %j
24 call void @foo(float* %3)
25 ; CHECK: call void @foo(float* [[t2]])
30 ; foo(&a[sext(i +nsw j)]);
31 ; foo(&a[sext((i +nsw j) +nsw i)]);
37 ; t3 = t2 + sext(i); // sext(i) should be GVN'ed.
39 define void @reassociate_gep_nsw(float* %a, i32 %i, i32 %j) {
40 ; CHECK-LABEL: @reassociate_gep_nsw(
41 %idxprom.j = sext i32 %j to i64
42 %1 = getelementptr float, float* %a, i64 %idxprom.j
43 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i64 %idxprom.j
44 call void @foo(float* %1)
45 ; CHECK: call void @foo(float* [[t1]])
47 %2 = add nsw i32 %i, %j
48 %idxprom.2 = sext i32 %2 to i64
49 %3 = getelementptr float, float* %a, i64 %idxprom.2
50 ; CHECK: [[sexti:[^ ]+]] = sext i32 %i to i64
51 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 [[sexti]]
52 call void @foo(float* %3)
53 ; CHECK: call void @foo(float* [[t2]])
55 %4 = add nsw i32 %2, %i
56 %idxprom.4 = sext i32 %4 to i64
57 %5 = getelementptr float, float* %a, i64 %idxprom.4
58 ; CHECK: [[t3:[^ ]+]] = getelementptr float, float* [[t2]], i64 [[sexti]]
59 call void @foo(float* %5)
60 ; CHECK: call void @foo(float* [[t3]])
68 ; foo(&a[zext(i + j)]);
74 define void @reassociate_gep_assume(float* %a, i32 %i, i32 %j) {
75 ; CHECK-LABEL: @reassociate_gep_assume(
77 %cmp = icmp sgt i32 %j, -1
78 call void @llvm.assume(i1 %cmp)
80 %cmp2 = icmp sgt i32 %1, -1
81 call void @llvm.assume(i1 %cmp2)
83 %idxprom.j = zext i32 %j to i64
84 %2 = getelementptr float, float* %a, i64 %idxprom.j
85 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i64 %idxprom.j
86 call void @foo(float* %2)
87 ; CHECK: call void @foo(float* [[t1]])
89 %idxprom.1 = zext i32 %1 to i64
90 %3 = getelementptr float, float* %a, i64 %idxprom.1
91 ; CHECK: [[sexti:[^ ]+]] = sext i32 %i to i64
92 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 [[sexti]]
93 call void @foo(float* %3)
94 ; CHECK: call void @foo(float* [[t2]])
99 ; Do not split the second GEP because sext(i + j) != sext(i) + sext(j).
100 define void @reassociate_gep_no_nsw(float* %a, i32 %i, i32 %j) {
101 ; CHECK-LABEL: @reassociate_gep_no_nsw(
103 %2 = getelementptr float, float* %a, i32 %j
104 ; CHECK: getelementptr float, float* %a, i32 %j
105 call void @foo(float* %2)
106 %3 = getelementptr float, float* %a, i32 %1
107 ; CHECK: getelementptr float, float* %a, i32 %1
108 call void @foo(float* %3)
112 define void @reassociate_gep_128(float* %a, i128 %i, i128 %j) {
113 ; CHECK-LABEL: @reassociate_gep_128(
115 %2 = getelementptr float, float* %a, i128 %i
116 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i128 %i
117 call void @foo(float* %2)
118 ; CHECK: call void @foo(float* [[t1]])
119 %3 = getelementptr float, float* %a, i128 %1
120 ; CHECK: [[truncj:[^ ]+]] = trunc i128 %j to i64
121 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 [[truncj]]
122 call void @foo(float* %3)
123 ; CHECK: call void @foo(float* [[t2]])
127 %struct.complex = type { float, float }
129 declare void @bar(%struct.complex*)
131 define void @different_types(%struct.complex* %input, i64 %i) {
132 ; CHECK-LABEL: @different_types(
133 %t1 = getelementptr %struct.complex, %struct.complex* %input, i64 %i
134 call void @bar(%struct.complex* %t1)
136 %t2 = getelementptr %struct.complex, %struct.complex* %input, i64 %j, i32 0
137 ; CHECK: [[cast:[^ ]+]] = bitcast %struct.complex* %t1 to float*
138 ; CHECK-NEXT: %t2 = getelementptr float, float* [[cast]], i64 10
139 ; CHECK-NEXT: call void @foo(float* %t2)
140 call void @foo(float* %t2)
144 declare void @llvm.assume(i1)