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