llvm/test/Transforms/InstCombine/memset-1.ll
Sanjay Patel 569647fbec [LibCallSimplifier] fold memset(malloc(x), 0, x) --> calloc(1, x)
This is a step towards solving PR25892:
https://llvm.org/bugs/show_bug.cgi?id=25892

It won't handle the reported case. As noted by the 'TODO' comments in the patch, 
we need to relax the hasOneUse() constraint and also match patterns that include
memset_chk() and the llvm.memset() intrinsic in addition to memset().

Differential Revision: http://reviews.llvm.org/D16337



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@258816 91177308-0d34-0410-b5e6-96231b3b80d8
2016-01-26 16:17:24 +00:00

66 lines
2.1 KiB
LLVM

; Test that the memset library call simplifier works correctly.
;
; RUN: opt < %s -instcombine -S | FileCheck %s
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"
declare i8* @memset(i8*, i32, i32)
; Check memset(mem1, val, size) -> llvm.memset(mem1, val, size, 1).
define i8* @test_simplify1(i8* %mem, i32 %val, i32 %size) {
; CHECK-LABEL: @test_simplify1(
%ret = call i8* @memset(i8* %mem, i32 %val, i32 %size)
; CHECK: call void @llvm.memset
ret i8* %ret
; CHECK: ret i8* %mem
}
define i8* @pr25892_lite(i32 %size) #0 {
%call1 = call i8* @malloc(i32 %size) #1
%call2 = call i8* @memset(i8* %call1, i32 0, i32 %size) #1
ret i8* %call2
; CHECK-LABEL: @pr25892_lite(
; CHECK-NEXT: %calloc = call i8* @calloc(i32 1, i32 %size)
; CHECK-NEXT: ret i8* %calloc
}
; FIXME: memset(malloc(x), 0, x) -> calloc(1, x)
; This doesn't fire currently because the malloc has more than one use.
define float* @pr25892(i32 %size) #0 {
entry:
%call = tail call i8* @malloc(i32 %size) #1
%cmp = icmp eq i8* %call, null
br i1 %cmp, label %cleanup, label %if.end
if.end:
%bc = bitcast i8* %call to float*
%call2 = tail call i8* @memset(i8* nonnull %call, i32 0, i32 %size) #1
br label %cleanup
cleanup:
%retval.0 = phi float* [ %bc, %if.end ], [ null, %entry ]
ret float* %retval.0
; CHECK-LABEL: @pr25892(
; CHECK: entry:
; CHECK-NEXT: %call = tail call i8* @malloc(i32 %size) #1
; CHECK-NEXT: %cmp = icmp eq i8* %call, null
; CHECK-NEXT: br i1 %cmp, label %cleanup, label %if.end
; CHECK: if.end:
; CHECK-NEXT: %bc = bitcast i8* %call to float*
; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull %call, i8 0, i32 %size, i32 1, i1 false)
; CHECK-NEXT: br label %cleanup
; CHECK: cleanup:
; CHECK-NEXT: %retval.0 = phi float* [ %bc, %if.end ], [ null, %entry ]
; CHECK-NEXT: ret float* %retval.0
}
declare noalias i8* @malloc(i32) #1
declare i64 @llvm.objectsize.i64.p0i8(i8*, i1) #2
attributes #0 = { nounwind ssp uwtable }
attributes #1 = { nounwind }
attributes #2 = { nounwind readnone }