d08c6f60c041aef66da9bc8a8f2a8296afa67212
[oota-llvm.git] / test / Transforms / NaryReassociate / NVPTX / nary-gep.ll
1 ; RUN: opt < %s -nary-reassociate -early-cse -S | FileCheck %s
2
3 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
4 target triple = "nvptx64-unknown-unknown"
5
6 declare 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);
14 define 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);
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]])
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 ; Do not split the second GEP because sext(i + j) != sext(i) + sext(j).
65 define void @reassociate_gep_no_nsw(float* %a, i32 %i, i32 %j) {
66 ; CHECK-LABEL: @reassociate_gep_no_nsw(
67   %1 = add i32 %i, %j
68   %2 = getelementptr float, float* %a, i32 %j
69 ; CHECK: getelementptr float, float* %a, i32 %j
70   call void @foo(float* %2)
71   %3 = getelementptr float, float* %a, i32 %1
72 ; CHECK: getelementptr float, float* %a, i32 %1
73   call void @foo(float* %3)
74   ret void
75 }
76
77 define void @reassociate_gep_128(float* %a, i128 %i, i128 %j) {
78 ; CHECK-LABEL: @reassociate_gep_128(
79   %1 = add i128 %i, %j
80   %2 = getelementptr float, float* %a, i128 %i
81 ; CHECK: [[t1:[^ ]+]] = getelementptr float, float* %a, i128 %i
82   call void @foo(float* %2)
83 ; CHECK: call void @foo(float* [[t1]])
84   %3 = getelementptr float, float* %a, i128 %1
85 ; CHECK: [[truncj:[^ ]+]] = trunc i128 %j to i64
86 ; CHECK: [[t2:[^ ]+]] = getelementptr float, float* [[t1]], i64 [[truncj]]
87   call void @foo(float* %3)
88 ; CHECK: call void @foo(float* [[t2]])
89   ret void
90 }