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