1; Test that the memset library call simplifier works correctly.
2;
3; RUN: opt < %s -instcombine -S | FileCheck %s
4
5target 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"
6
7declare i8* @memset(i8*, i32, i32)
8
9; Check memset(mem1, val, size) -> llvm.memset(mem1, val, size, 1).
10
11define i8* @test_simplify1(i8* %mem, i32 %val, i32 %size) {
12; CHECK-LABEL: @test_simplify1(
13  %ret = call i8* @memset(i8* %mem, i32 %val, i32 %size)
14; CHECK: call void @llvm.memset
15  ret i8* %ret
16; CHECK: ret i8* %mem
17}
18
19define i8* @pr25892_lite(i32 %size) #0 {
20  %call1 = call i8* @malloc(i32 %size) #1
21  %call2 = call i8* @memset(i8* %call1, i32 0, i32 %size) #1
22  ret i8* %call2
23
24; CHECK-LABEL: @pr25892_lite(
25; CHECK-NEXT:  %calloc = call i8* @calloc(i32 1, i32 %size)
26; CHECK-NEXT:  ret i8* %calloc
27}
28
29; FIXME: memset(malloc(x), 0, x) -> calloc(1, x)
30; This doesn't fire currently because the malloc has more than one use.
31
32define float* @pr25892(i32 %size) #0 {
33entry:
34  %call = tail call i8* @malloc(i32 %size) #1
35  %cmp = icmp eq i8* %call, null
36  br i1 %cmp, label %cleanup, label %if.end
37if.end:
38  %bc = bitcast i8* %call to float*
39  %call2 = tail call i8* @memset(i8* nonnull %call, i32 0, i32 %size) #1
40  br label %cleanup
41cleanup:
42  %retval.0 = phi float* [ %bc, %if.end ], [ null, %entry ]
43  ret float* %retval.0
44
45; CHECK-LABEL: @pr25892(
46; CHECK:       entry:
47; CHECK-NEXT:    %call = tail call i8* @malloc(i32 %size) #1
48; CHECK-NEXT:    %cmp = icmp eq i8* %call, null
49; CHECK-NEXT:    br i1 %cmp, label %cleanup, label %if.end
50; CHECK:       if.end:
51; CHECK-NEXT:    %bc = bitcast i8* %call to float*
52; CHECK-NEXT:    call void @llvm.memset.p0i8.i32(i8* nonnull %call, i8 0, i32 %size, i32 1, i1 false)
53; CHECK-NEXT:    br label %cleanup
54; CHECK:       cleanup:
55; CHECK-NEXT:    %retval.0 = phi float* [ %bc, %if.end ], [ null, %entry ]
56; CHECK-NEXT:    ret float* %retval.0
57}
58
59declare noalias i8* @malloc(i32) #1
60declare i64 @llvm.objectsize.i64.p0i8(i8*, i1) #2
61
62attributes #0 = { nounwind ssp uwtable }
63attributes #1 = { nounwind }
64attributes #2 = { nounwind readnone }
65
66