1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test that the memset library call simplifier works correctly.
4 ; RUN: opt < %s -instcombine -S | FileCheck %s
6 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
8 declare i8* @memset(i8*, i32, i32)
9 declare void @llvm.memset.p0i8.i32(i8* nocapture writeonly, i8, i32, i32, i1)
10 declare noalias i8* @malloc(i32) #1
12 ; Check memset(mem1, val, size) -> llvm.memset(mem1, val, size, 1).
14 define i8* @test_simplify1(i8* %mem, i32 %val, i32 %size) {
15 ; CHECK-LABEL: @test_simplify1(
16 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[VAL:%.*]] to i8
17 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[MEM:%.*]], i8 [[TMP1]], i32 [[SIZE:%.*]], i1 false)
18 ; CHECK-NEXT: ret i8* [[MEM]]
20 %ret = call i8* @memset(i8* %mem, i32 %val, i32 %size)
24 ; Malloc + memset pattern is now handled by DSE in a more general way.
26 define i8* @pr25892_lite(i32 %size) #0 {
27 ; CHECK-LABEL: @pr25892_lite(
28 ; CHECK-NEXT: [[CALL:%.*]] = call i8* @malloc(i32 [[SIZE:%.*]])
29 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[CALL]], i8 0, i32 [[SIZE]], i1 false)
31 %call1 = call i8* @malloc(i32 %size) #1
32 %call2 = call i8* @memset(i8* %call1, i32 0, i32 %size) #1
36 ; A memset intrinsic should be handled similarly to a memset() libcall.
37 ; Notice that malloc + memset pattern is now handled by DSE in a more general way.
39 define i8* @malloc_and_memset_intrinsic(i32 %n) #0 {
40 ; CHECK-LABEL: @malloc_and_memset_intrinsic(
41 ; CHECK-NEXT: [[CALL:%.*]] = call i8* @malloc(i32 [[N:%.*]])
42 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[CALL]], i8 0, i32 [[N]], i1 false)
43 ; CHECK-NEXT: ret i8* [[CALL]]
45 %call = call i8* @malloc(i32 %n)
46 call void @llvm.memset.p0i8.i32(i8* %call, i8 0, i32 %n, i32 1, i1 false)
50 ; This should not create a calloc and should not crash the compiler.
51 ; Notice that malloc + memset pattern is now handled by DSE in a more general way.
53 define i8* @notmalloc_memset(i32 %size, i8*(i32)* %notmalloc) {
54 ; CHECK-LABEL: @notmalloc_memset(
55 ; CHECK-NEXT: [[CALL1:%.*]] = call i8* [[NOTMALLOC:%.*]](i32 [[SIZE:%.*]]) [[ATTR0:#.*]]
56 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[CALL1]], i8 0, i32 [[SIZE]], i1 false) [[ATTR0]]
57 ; CHECK-NEXT: ret i8* [[CALL1]]
59 %call1 = call i8* %notmalloc(i32 %size) #1
60 %call2 = call i8* @memset(i8* %call1, i32 0, i32 %size) #1
64 ; This doesn't fire currently because the malloc has more than one use.
65 ; Notice that malloc + memset pattern is now handled by DSE in a more general way.
67 define float* @pr25892(i32 %size) #0 {
68 ; CHECK-LABEL: @pr25892(
70 ; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @malloc(i32 [[SIZE:%.*]]) [[ATTR0]]
71 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8* [[CALL]], null
72 ; CHECK-NEXT: br i1 [[CMP]], label [[CLEANUP:%.*]], label [[IF_END:%.*]]
74 ; CHECK-NEXT: [[BC:%.*]] = bitcast i8* [[CALL]] to float*
75 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 [[CALL]], i8 0, i32 [[SIZE]], i1 false) [[ATTR0]]
76 ; CHECK-NEXT: br label [[CLEANUP]]
78 ; CHECK-NEXT: [[RETVAL_0:%.*]] = phi float* [ [[BC]], [[IF_END]] ], [ null, [[ENTRY:%.*]] ]
79 ; CHECK-NEXT: ret float* [[RETVAL_0]]
82 %call = tail call i8* @malloc(i32 %size) #1
83 %cmp = icmp eq i8* %call, null
84 br i1 %cmp, label %cleanup, label %if.end
86 %bc = bitcast i8* %call to float*
87 %call2 = tail call i8* @memset(i8* nonnull %call, i32 0, i32 %size) #1
90 %retval.0 = phi float* [ %bc, %if.end ], [ null, %entry ]
94 ; If there's a calloc transform, the store must also be eliminated.
96 define i8* @buffer_is_modified_then_memset(i32 %size) {
97 ; CHECK-LABEL: @buffer_is_modified_then_memset(
98 ; CHECK-NEXT: [[PTR:%.*]] = tail call i8* @malloc(i32 [[SIZE:%.*]]) [[ATTR0]]
99 ; CHECK-NEXT: store i8 1, i8* [[PTR]], align 1
100 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 [[PTR]], i8 0, i32 [[SIZE]], i1 false) [[ATTR0]]
101 ; CHECK-NEXT: ret i8* [[PTR]]
103 %ptr = tail call i8* @malloc(i32 %size) #1
104 store i8 1, i8* %ptr ;; fdata[0] = 1;
105 %memset = tail call i8* @memset(i8* nonnull %ptr, i32 0, i32 %size) #1
109 define i8* @memset_size_select(i1 %b, i8* %ptr) {
110 ; CHECK-LABEL: @memset_size_select(
111 ; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[B:%.*]], i32 10, i32 50
112 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(10) [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) [[ATTR0]]
113 ; CHECK-NEXT: ret i8* [[PTR]]
115 %size = select i1 %b, i32 10, i32 50
116 %memset = tail call i8* @memset(i8* nonnull %ptr, i32 0, i32 %size) #1
121 define i8* @memset_size_select2(i1 %b, i8* %ptr) {
122 ; CHECK-LABEL: @memset_size_select2(
123 ; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[B:%.*]], i32 10, i32 50
124 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(80) [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) [[ATTR0]]
125 ; CHECK-NEXT: ret i8* [[PTR]]
127 %size = select i1 %b, i32 10, i32 50
128 %memset = tail call i8* @memset(i8* nonnull dereferenceable(80) %ptr, i32 0, i32 %size) #1
132 define i8* @memset_size_select3(i1 %b, i8* %ptr) {
133 ; CHECK-LABEL: @memset_size_select3(
134 ; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[B:%.*]], i32 10, i32 50
135 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(40) [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false)
136 ; CHECK-NEXT: ret i8* [[PTR]]
138 %size = select i1 %b, i32 10, i32 50
139 %memset = tail call i8* @memset(i8* dereferenceable_or_null(40) %ptr, i32 0, i32 %size)
143 define i8* @memset_size_select4(i1 %b, i8* %ptr) {
144 ; CHECK-LABEL: @memset_size_select4(
145 ; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[B:%.*]], i32 10, i32 50
146 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(40) [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) [[ATTR0]]
147 ; CHECK-NEXT: ret i8* [[PTR]]
149 %size = select i1 %b, i32 10, i32 50
150 %memset = tail call i8* @memset(i8* nonnull dereferenceable_or_null(40) %ptr, i32 0, i32 %size) #1
154 define i8* @memset_size_ashr(i1 %b, i8* %ptr, i32 %v) {
155 ; CHECK-LABEL: @memset_size_ashr(
156 ; CHECK-NEXT: [[SIZE:%.*]] = ashr i32 -2, [[V:%.*]]
157 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noundef nonnull align 1 [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) [[ATTR0]]
158 ; CHECK-NEXT: ret i8* [[PTR]]
160 %size = ashr i32 -2, %v
161 %memset = tail call i8* @memset(i8* nonnull %ptr, i32 0, i32 %size) #1
165 define i8* @memset_attrs1(i1 %b, i8* %ptr, i32 %size) {
166 ; CHECK-LABEL: @memset_attrs1(
167 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 dereferenceable_or_null(40) [[PTR:%.*]], i8 0, i32 [[SIZE:%.*]], i1 false) [[ATTR0]]
168 ; CHECK-NEXT: ret i8* [[PTR]]
170 %memset = tail call i8* @memset(i8* dereferenceable_or_null(40) %ptr, i32 0, i32 %size) #1
174 ; be sure to drop nonnull since size is unknown and can be 0
175 ; do not change dereferenceable attribute
176 define i8* @memset_attrs2(i1 %b, i8* %ptr, i32 %size) {
177 ; CHECK-LABEL: @memset_attrs2(
178 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(40) [[PTR:%.*]], i8 0, i32 [[SIZE:%.*]], i1 false) [[ATTR0]]
179 ; CHECK-NEXT: ret i8* [[PTR]]
181 %memset = tail call i8* @memset(i8* nonnull dereferenceable(40) %ptr, i32 0, i32 %size) #1
185 ; size is unknown, just copy attrs, no changes in attrs
186 define i8* @memset_attrs3(i1 %b, i8* %ptr, i32 %size) {
187 ; CHECK-LABEL: @memset_attrs3(
188 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable_or_null(40) [[PTR:%.*]], i8 0, i32 [[SIZE:%.*]], i1 false) [[ATTR0]]
189 ; CHECK-NEXT: ret i8* [[PTR]]
191 %memset = tail call i8* @memset(i8* nonnull dereferenceable_or_null(40) %ptr, i32 0, i32 %size) #1
195 ; be sure to drop nonnull since size is unknown and can be 0
196 define i8* @memset_attrs4(i1 %b, i8* %ptr, i32 %size) {
197 ; CHECK-LABEL: @memset_attrs4(
198 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 [[PTR:%.*]], i8 0, i32 [[SIZE:%.*]], i1 false) [[ATTR0]]
199 ; CHECK-NEXT: ret i8* [[PTR]]
201 %memset = tail call i8* @memset(i8* nonnull %ptr, i32 0, i32 %size) #1
205 define i8* @test_no_incompatible_attr(i8* %mem, i32 %val, i32 %size) {
206 ; CHECK-LABEL: @test_no_incompatible_attr(
207 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[VAL:%.*]] to i8
208 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[MEM:%.*]], i8 [[TMP1]], i32 [[SIZE:%.*]], i1 false)
209 ; CHECK-NEXT: ret i8* [[MEM]]
211 %ret = call dereferenceable(1) i8* @memset(i8* %mem, i32 %val, i32 %size)
215 attributes #0 = { nounwind ssp uwtable }
216 attributes #1 = { nounwind }
217 attributes #2 = { nounwind readnone }