1 ; RUN: llc < %s -march=nvptx64 -mcpu=sm_35 -O0 | FileCheck %s --check-prefix PTX
2 ; RUN: opt < %s -S -nvptx-lower-aggr-copies | FileCheck %s --check-prefix IR
3 ; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_35 -O0 | %ptxas-verify %}
5 ; Verify that the NVPTXLowerAggrCopies pass works as expected - calls to
6 ; llvm.mem* intrinsics get lowered to loops.
8 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
9 target triple = "nvptx64-unknown-unknown"
11 declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture readonly, i64, i1) #1
12 declare void @llvm.memmove.p0.p0.i64(ptr nocapture, ptr nocapture readonly, i64, i1) #1
13 declare void @llvm.memset.p0.i64(ptr nocapture, i8, i64, i1) #1
15 define ptr @memcpy_caller(ptr %dst, ptr %src, i64 %n) #0 {
17 tail call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 %n, i1 false)
20 ; IR-LABEL: @memcpy_caller
22 ; IR: [[Cond:%[0-9]+]] = icmp ne i64 %n, 0
23 ; IR: br i1 [[Cond]], label %loop-memcpy-expansion, label %post-loop-memcpy-expansion
25 ; IR: loop-memcpy-expansion:
26 ; IR: %loop-index = phi i64 [ 0, %entry ], [ [[IndexInc:%[0-9]+]], %loop-memcpy-expansion ]
27 ; IR: [[SrcGep:%[0-9]+]] = getelementptr inbounds i8, ptr %src, i64 %loop-index
28 ; IR: [[Load:%[0-9]+]] = load i8, ptr [[SrcGep]]
29 ; IR: [[DstGep:%[0-9]+]] = getelementptr inbounds i8, ptr %dst, i64 %loop-index
30 ; IR: store i8 [[Load]], ptr [[DstGep]]
31 ; IR: [[IndexInc]] = add i64 %loop-index, 1
32 ; IR: [[Cond2:%[0-9]+]] = icmp ult i64 [[IndexInc]], %n
33 ; IR: br i1 [[Cond2]], label %loop-memcpy-expansion, label %post-loop-memcpy-expansion
35 ; IR-LABEL: post-loop-memcpy-expansion:
38 ; PTX-LABEL: .visible .func (.param .b64 func_retval0) memcpy_caller
39 ; PTX: $L__BB[[LABEL:[_0-9]+]]:
40 ; PTX: ld.u8 %rs[[REG:[0-9]+]]
41 ; PTX: st.u8 [%rd{{[0-9]+}}], %rs[[REG]]
42 ; PTX: add.s64 %rd[[COUNTER:[0-9]+]], %rd{{[0-9]+}}, 1
43 ; PTX: setp.lt.u64 %p[[PRED:[0-9]+]], %rd[[COUNTER]], %rd
44 ; PTX: @%p[[PRED]] bra $L__BB[[LABEL]]
48 define ptr @memcpy_volatile_caller(ptr %dst, ptr %src, i64 %n) #0 {
50 tail call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 %n, i1 true)
53 ; IR-LABEL: @memcpy_volatile_caller
55 ; IR: [[Cond:%[0-9]+]] = icmp ne i64 %n, 0
56 ; IR: br i1 [[Cond]], label %loop-memcpy-expansion, label %post-loop-memcpy-expansion
58 ; IR: loop-memcpy-expansion:
59 ; IR: %loop-index = phi i64 [ 0, %entry ], [ [[IndexInc:%[0-9]+]], %loop-memcpy-expansion ]
60 ; IR: [[SrcGep:%[0-9]+]] = getelementptr inbounds i8, ptr %src, i64 %loop-index
61 ; IR: [[Load:%[0-9]+]] = load volatile i8, ptr [[SrcGep]]
62 ; IR: [[DstGep:%[0-9]+]] = getelementptr inbounds i8, ptr %dst, i64 %loop-index
63 ; IR: store volatile i8 [[Load]], ptr [[DstGep]]
64 ; IR: [[IndexInc]] = add i64 %loop-index, 1
65 ; IR: [[Cond2:%[0-9]+]] = icmp ult i64 [[IndexInc]], %n
66 ; IR: br i1 [[Cond2]], label %loop-memcpy-expansion, label %post-loop-memcpy-expansion
68 ; IR-LABEL: post-loop-memcpy-expansion:
72 ; PTX-LABEL: .visible .func (.param .b64 func_retval0) memcpy_volatile_caller
73 ; PTX: $L__BB[[LABEL:[_0-9]+]]:
74 ; PTX: ld.volatile.u8 %rs[[REG:[0-9]+]]
75 ; PTX: st.volatile.u8 [%rd{{[0-9]+}}], %rs[[REG]]
76 ; PTX: add.s64 %rd[[COUNTER:[0-9]+]], %rd{{[0-9]+}}, 1
77 ; PTX: setp.lt.u64 %p[[PRED:[0-9]+]], %rd[[COUNTER]], %rd
78 ; PTX: @%p[[PRED]] bra $L__BB[[LABEL]]
81 define ptr @memcpy_casting_caller(ptr %dst, ptr %src, i64 %n) #0 {
83 tail call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 %n, i1 false)
86 ; Check that casts in calls to memcpy are handled properly
87 ; IR-LABEL: @memcpy_casting_caller
88 ; IR: getelementptr inbounds i8, ptr %src
89 ; IR: getelementptr inbounds i8, ptr %dst
92 define ptr @memcpy_known_size(ptr %dst, ptr %src) {
94 tail call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 144, i1 false)
97 ; Check that calls with compile-time constant size are handled correctly
98 ; IR-LABEL: @memcpy_known_size
100 ; IR: br label %load-store-loop
101 ; IR: load-store-loop:
102 ; IR: %loop-index = phi i64 [ 0, %entry ], [ [[IndexInc:%[0-9]+]], %load-store-loop ]
103 ; IR: [[SrcGep:%[0-9]+]] = getelementptr inbounds i8, ptr %src, i64 %loop-index
104 ; IR: [[Load:%[0-9]+]] = load i8, ptr [[SrcGep]]
105 ; IR: [[DstGep:%[0-9]+]] = getelementptr inbounds i8, ptr %dst, i64 %loop-index
106 ; IR: store i8 [[Load]], ptr [[DstGep]]
107 ; IR: [[IndexInc]] = add i64 %loop-index, 1
108 ; IR: [[Cond:%[0-9]+]] = icmp ult i64 %3, 144
109 ; IR: br i1 [[Cond]], label %load-store-loop, label %memcpy-split
112 define ptr @memset_caller(ptr %dst, i32 %c, i64 %n) #0 {
114 %0 = trunc i32 %c to i8
115 tail call void @llvm.memset.p0.i64(ptr %dst, i8 %0, i64 %n, i1 false)
118 ; IR-LABEL: @memset_caller
119 ; IR: [[VAL:%[0-9]+]] = trunc i32 %c to i8
120 ; IR: [[CMPREG:%[0-9]+]] = icmp eq i64 0, %n
121 ; IR: br i1 [[CMPREG]], label %split, label %loadstoreloop
123 ; IR: [[STOREPTR:%[0-9]+]] = getelementptr inbounds i8, ptr %dst, i64
124 ; IR-NEXT: store i8 [[VAL]], ptr [[STOREPTR]]
126 ; PTX-LABEL: .visible .func (.param .b64 func_retval0) memset_caller(
127 ; PTX: ld.param.u32 %r[[C:[0-9]+]]
128 ; PTX: cvt.u16.u32 %rs[[REG:[0-9]+]], %r[[C]];
129 ; PTX: $L__BB[[LABEL:[_0-9]+]]:
130 ; PTX: st.u8 [%rd{{[0-9]+}}], %rs[[REG]]
131 ; PTX: add.s64 %rd[[COUNTER:[0-9]+]], %rd{{[0-9]+}}, 1
132 ; PTX: setp.lt.u64 %p[[PRED:[0-9]+]], %rd[[COUNTER]], %rd
133 ; PTX: @%p[[PRED]] bra $L__BB[[LABEL]]
136 define ptr @volatile_memset_caller(ptr %dst, i32 %c, i64 %n) #0 {
138 %0 = trunc i32 %c to i8
139 tail call void @llvm.memset.p0.i64(ptr %dst, i8 %0, i64 %n, i1 true)
142 ; IR-LABEL: @volatile_memset_caller
143 ; IR: [[VAL:%[0-9]+]] = trunc i32 %c to i8
145 ; IR: [[STOREPTR:%[0-9]+]] = getelementptr inbounds i8, ptr %dst, i64
146 ; IR-NEXT: store volatile i8 [[VAL]], ptr [[STOREPTR]]
149 define ptr @memmove_caller(ptr %dst, ptr %src, i64 %n) #0 {
151 tail call void @llvm.memmove.p0.p0.i64(ptr %dst, ptr %src, i64 %n, i1 false)
154 ; IR-LABEL: @memmove_caller
155 ; IR: icmp ult ptr %src, %dst
156 ; IR: [[PHIVAL:%[0-9a-zA-Z_]+]] = phi i64
157 ; IR-NEXT: %bwd_main_index = sub i64 [[PHIVAL]], 1
158 ; IR: [[FWDPHIVAL:%[0-9a-zA-Z_]+]] = phi i64
159 ; IR: {{%[0-9a-zA-Z_]+}} = add i64 [[FWDPHIVAL]], 1
161 ; PTX-LABEL: .visible .func (.param .b64 func_retval0) memmove_caller(
162 ; PTX: ld.param.u64 %rd[[N:[0-9]+]]
163 ; PTX-DAG: setp.eq.s64 %p[[NEQ0:[0-9]+]], %rd[[N]], 0
164 ; PTX-DAG: setp.ge.u64 %p[[SRC_GT_THAN_DST:[0-9]+]], %rd{{[0-9]+}}, %rd{{[0-9]+}}
165 ; PTX-NEXT: @%p[[SRC_GT_THAN_DST]] bra $L__BB[[FORWARD_BB:[0-9_]+]]
166 ; -- this is the backwards copying BB
167 ; PTX: @%p[[NEQ0]] bra $L__BB[[EXIT:[0-9_]+]]
168 ; PTX: add.s64 %rd{{[0-9]}}, %rd{{[0-9]}}, -1
169 ; PTX: ld.u8 %rs[[ELEMENT:[0-9]+]]
170 ; PTX: st.u8 [%rd{{[0-9]+}}], %rs[[ELEMENT]]
171 ; -- this is the forwards copying BB
172 ; PTX: $L__BB[[FORWARD_BB]]:
173 ; PTX: @%p[[NEQ0]] bra $L__BB[[EXIT]]
174 ; PTX: ld.u8 %rs[[ELEMENT2:[0-9]+]]
175 ; PTX: st.u8 [%rd{{[0-9]+}}], %rs[[ELEMENT2]]
176 ; PTX: add.s64 %rd{{[0-9]+}}, %rd{{[0-9]+}}, 1
178 ; PTX: $L__BB[[EXIT]]:
179 ; PTX-NEXT: st.param.b64 [func_retval0