1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; Test that the memset library call simplifier works correctly. 3; 4; RUN: opt < %s -instcombine -S | FileCheck %s 5 6target 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" 7 8declare i8* @memset(i8*, i32, i32) 9declare void @llvm.memset.p0i8.i32(i8* nocapture writeonly, i8, i32, i32, i1) 10declare noalias i8* @malloc(i32) #1 11 12; Check memset(mem1, val, size) -> llvm.memset(mem1, val, size, 1). 13 14define 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]] 19; 20 %ret = call i8* @memset(i8* %mem, i32 %val, i32 %size) 21 ret i8* %ret 22} 23 24define i8* @pr25892_lite(i32 %size) #0 { 25; CHECK-LABEL: @pr25892_lite( 26; CHECK-NEXT: [[CALLOC:%.*]] = call i8* @calloc(i32 1, i32 [[SIZE:%.*]]) 27; CHECK-NEXT: ret i8* [[CALLOC]] 28; 29 %call1 = call i8* @malloc(i32 %size) #1 30 %call2 = call i8* @memset(i8* %call1, i32 0, i32 %size) #1 31 ret i8* %call2 32} 33 34; FIXME: A memset intrinsic should be handled similarly to a memset() libcall. 35 36define i8* @malloc_and_memset_intrinsic(i32 %n) #0 { 37; CHECK-LABEL: @malloc_and_memset_intrinsic( 38; CHECK-NEXT: [[CALL:%.*]] = call i8* @malloc(i32 [[N:%.*]]) 39; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[CALL]], i8 0, i32 [[N]], i1 false) 40; CHECK-NEXT: ret i8* [[CALL]] 41; 42 %call = call i8* @malloc(i32 %n) 43 call void @llvm.memset.p0i8.i32(i8* %call, i8 0, i32 %n, i32 1, i1 false) 44 ret i8* %call 45} 46 47; This should not create a calloc and should not crash the compiler. 48 49define i8* @notmalloc_memset(i32 %size, i8*(i32)* %notmalloc) { 50; CHECK-LABEL: @notmalloc_memset( 51; CHECK-NEXT: [[CALL1:%.*]] = call i8* [[NOTMALLOC:%.*]](i32 [[SIZE:%.*]]) #0 52; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[CALL1]], i8 0, i32 [[SIZE]], i1 false) #0 53; CHECK-NEXT: ret i8* [[CALL1]] 54; 55 %call1 = call i8* %notmalloc(i32 %size) #1 56 %call2 = call i8* @memset(i8* %call1, i32 0, i32 %size) #1 57 ret i8* %call2 58} 59 60; FIXME: memset(malloc(x), 0, x) -> calloc(1, x) 61; This doesn't fire currently because the malloc has more than one use. 62 63define float* @pr25892(i32 %size) #0 { 64; CHECK-LABEL: @pr25892( 65; CHECK-NEXT: entry: 66; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @malloc(i32 [[SIZE:%.*]]) #0 67; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8* [[CALL]], null 68; CHECK-NEXT: br i1 [[CMP]], label [[CLEANUP:%.*]], label [[IF_END:%.*]] 69; CHECK: if.end: 70; CHECK-NEXT: [[BC:%.*]] = bitcast i8* [[CALL]] to float* 71; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 [[CALL]], i8 0, i32 [[SIZE]], i1 false) #0 72; CHECK-NEXT: br label [[CLEANUP]] 73; CHECK: cleanup: 74; CHECK-NEXT: [[RETVAL_0:%.*]] = phi float* [ [[BC]], [[IF_END]] ], [ null, [[ENTRY:%.*]] ] 75; CHECK-NEXT: ret float* [[RETVAL_0]] 76; 77entry: 78 %call = tail call i8* @malloc(i32 %size) #1 79 %cmp = icmp eq i8* %call, null 80 br i1 %cmp, label %cleanup, label %if.end 81if.end: 82 %bc = bitcast i8* %call to float* 83 %call2 = tail call i8* @memset(i8* nonnull %call, i32 0, i32 %size) #1 84 br label %cleanup 85cleanup: 86 %retval.0 = phi float* [ %bc, %if.end ], [ null, %entry ] 87 ret float* %retval.0 88} 89 90; If there's a calloc transform, the store must also be eliminated. 91 92define i8* @buffer_is_modified_then_memset(i32 %size) { 93; CHECK-LABEL: @buffer_is_modified_then_memset( 94; CHECK-NEXT: [[PTR:%.*]] = tail call i8* @malloc(i32 [[SIZE:%.*]]) #0 95; CHECK-NEXT: store i8 1, i8* [[PTR]], align 1 96; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 [[PTR]], i8 0, i32 [[SIZE]], i1 false) #0 97; CHECK-NEXT: ret i8* [[PTR]] 98; 99 %ptr = tail call i8* @malloc(i32 %size) #1 100 store i8 1, i8* %ptr ;; fdata[0] = 1; 101 %memset = tail call i8* @memset(i8* nonnull %ptr, i32 0, i32 %size) #1 102 ret i8* %memset 103} 104 105define i8* @memset_size_select(i1 %b, i8* %ptr) { 106; CHECK-LABEL: @memset_size_select( 107; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[B:%.*]], i32 10, i32 50 108; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(10) [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) #0 109; CHECK-NEXT: ret i8* [[PTR]] 110; 111 %size = select i1 %b, i32 10, i32 50 112 %memset = tail call i8* @memset(i8* nonnull %ptr, i32 0, i32 %size) #1 113 ret i8* %memset 114} 115 116 117define i8* @memset_size_select2(i1 %b, i8* %ptr) { 118; CHECK-LABEL: @memset_size_select2( 119; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[B:%.*]], i32 10, i32 50 120; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(80) [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) #0 121; CHECK-NEXT: ret i8* [[PTR]] 122; 123 %size = select i1 %b, i32 10, i32 50 124 %memset = tail call i8* @memset(i8* nonnull dereferenceable(80) %ptr, i32 0, i32 %size) #1 125 ret i8* %memset 126} 127 128define i8* @memset_size_select3(i1 %b, i8* %ptr) { 129; CHECK-LABEL: @memset_size_select3( 130; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[B:%.*]], i32 10, i32 50 131; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(40) [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) 132; CHECK-NEXT: ret i8* [[PTR]] 133; 134 %size = select i1 %b, i32 10, i32 50 135 %memset = tail call i8* @memset(i8* dereferenceable_or_null(40) %ptr, i32 0, i32 %size) 136 ret i8* %memset 137} 138 139define i8* @memset_size_select4(i1 %b, i8* %ptr) { 140; CHECK-LABEL: @memset_size_select4( 141; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[B:%.*]], i32 10, i32 50 142; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(40) [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) #0 143; CHECK-NEXT: ret i8* [[PTR]] 144; 145 %size = select i1 %b, i32 10, i32 50 146 %memset = tail call i8* @memset(i8* nonnull dereferenceable_or_null(40) %ptr, i32 0, i32 %size) #1 147 ret i8* %memset 148} 149 150define i8* @memset_size_ashr(i1 %b, i8* %ptr, i32 %v) { 151; CHECK-LABEL: @memset_size_ashr( 152; CHECK-NEXT: [[SIZE:%.*]] = ashr i32 -2, [[V:%.*]] 153; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 [[PTR:%.*]], i8 0, i32 [[SIZE]], i1 false) #0 154; CHECK-NEXT: ret i8* [[PTR]] 155; 156 %size = ashr i32 -2, %v 157 %memset = tail call i8* @memset(i8* nonnull %ptr, i32 0, i32 %size) #1 158 ret i8* %memset 159} 160 161define i8* @memset_attrs1(i1 %b, i8* %ptr, i32 %size) { 162; CHECK-LABEL: @memset_attrs1( 163; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 dereferenceable_or_null(40) [[PTR:%.*]], i8 0, i32 [[SIZE:%.*]], i1 false) #0 164; CHECK-NEXT: ret i8* [[PTR]] 165; 166 %memset = tail call i8* @memset(i8* dereferenceable_or_null(40) %ptr, i32 0, i32 %size) #1 167 ret i8* %memset 168} 169 170; be sure to drop nonnull since size is unknown and can be 0 171; do not change dereferenceable attribute 172define i8* @memset_attrs2(i1 %b, i8* %ptr, i32 %size) { 173; CHECK-LABEL: @memset_attrs2( 174; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable(40) [[PTR:%.*]], i8 0, i32 [[SIZE:%.*]], i1 false) #0 175; CHECK-NEXT: ret i8* [[PTR]] 176; 177 %memset = tail call i8* @memset(i8* nonnull dereferenceable(40) %ptr, i32 0, i32 %size) #1 178 ret i8* %memset 179} 180 181; size is unknown, just copy attrs, no changes in attrs 182define i8* @memset_attrs3(i1 %b, i8* %ptr, i32 %size) { 183; CHECK-LABEL: @memset_attrs3( 184; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 dereferenceable_or_null(40) [[PTR:%.*]], i8 0, i32 [[SIZE:%.*]], i1 false) #0 185; CHECK-NEXT: ret i8* [[PTR]] 186; 187 %memset = tail call i8* @memset(i8* nonnull dereferenceable_or_null(40) %ptr, i32 0, i32 %size) #1 188 ret i8* %memset 189} 190 191; be sure to drop nonnull since size is unknown and can be 0 192define i8* @memset_attrs4(i1 %b, i8* %ptr, i32 %size) { 193; CHECK-LABEL: @memset_attrs4( 194; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 1 [[PTR:%.*]], i8 0, i32 [[SIZE:%.*]], i1 false) #0 195; CHECK-NEXT: ret i8* [[PTR]] 196; 197 %memset = tail call i8* @memset(i8* nonnull %ptr, i32 0, i32 %size) #1 198 ret i8* %memset 199} 200 201 202attributes #0 = { nounwind ssp uwtable } 203attributes #1 = { nounwind } 204attributes #2 = { nounwind readnone } 205 206