• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt < %s -nary-reassociate -early-cse -S | FileCheck %s
2
3target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
4target triple = "nvptx64-unknown-unknown"
5
6declare void @foo(float*)
7
8; foo(&a[i]);
9; foo(&a[i + j]);
10;   =>
11; t = &a[i];
12; foo(t);
13; foo(t + j);
14define void @reassociate_gep(float* %a, i64 %i, i64 %j) {
15; CHECK-LABEL: @reassociate_gep(
16  %1 = add i64 %i, %j
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]])
25  ret void
26}
27
28; foo(&a[sext(j)]);
29; foo(&a[sext(i +nsw j)]);
30; foo(&a[sext((i +nsw j) +nsw i)]);
31;   =>
32; t1 = &a[sext(j)];
33; foo(t1);
34; t2 = t1 + sext(i);
35; foo(t2);
36; t3 = t2 + sext(i); // sext(i) should be GVN'ed.
37; foo(t3);
38define 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]])
45
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]])
53
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]])
60
61  ret void
62}
63
64; assume(j >= 0);
65; foo(&a[zext(j)]);
66; assume(i + j >= 0);
67; foo(&a[zext(i + j)]);
68;   =>
69; t1 = &a[zext(j)];
70; foo(t1);
71; t2 = t1 + sext(i);
72; foo(t2);
73define void @reassociate_gep_assume(float* %a, i32 %i, i32 %j) {
74; CHECK-LABEL: @reassociate_gep_assume(
75  ; assume(j >= 0)
76  %cmp = icmp sgt i32 %j, -1
77  call void @llvm.assume(i1 %cmp)
78  %1 = add i32 %i, %j
79  %cmp2 = icmp sgt i32 %1, -1
80  call void @llvm.assume(i1 %cmp2)
81
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]])
87
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]])
94
95  ret void
96}
97
98; Do not split the second GEP because sext(i + j) != sext(i) + sext(j).
99define void @reassociate_gep_no_nsw(float* %a, i32 %i, i32 %j) {
100; CHECK-LABEL: @reassociate_gep_no_nsw(
101  %1 = add i32 %i, %j
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)
108  ret void
109}
110
111define void @reassociate_gep_128(float* %a, i128 %i, i128 %j) {
112; CHECK-LABEL: @reassociate_gep_128(
113  %1 = add i128 %i, %j
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]])
123  ret void
124}
125
126%struct.complex = type { float, float }
127
128declare void @bar(%struct.complex*)
129
130define 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)
134  %j = add i64 %i, 5
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)
140  ret void
141}
142
143declare void @llvm.assume(i1)
144