1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test lib call simplification of __memset_chk calls with various values
5 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
8 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
10 %struct.T = type { [100 x i32], [100 x i32], [1024 x i8] }
11 @t = common global %struct.T zeroinitializer
13 ; Check cases where dstlen >= len.
15 define ptr @test_simplify1() {
16 ; CHECK-LABEL: @test_simplify1(
17 ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1824) @t, i8 0, i64 1824, i1 false)
18 ; CHECK-NEXT: ret ptr @t
21 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 1824)
25 define ptr @test_simplify2() {
26 ; CHECK-LABEL: @test_simplify2(
27 ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1824) @t, i8 0, i64 1824, i1 false)
28 ; CHECK-NEXT: ret ptr @t
31 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 3648)
35 define ptr @test_simplify3() {
36 ; CHECK-LABEL: @test_simplify3(
37 ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1824) @t, i8 0, i64 1824, i1 false)
38 ; CHECK-NEXT: ret ptr @t
41 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 -1)
45 ; Same as @test_simplify1 with tail call.
46 define ptr @test_simplify4() {
47 ; CHECK-LABEL: @test_simplify4(
48 ; CHECK-NEXT: tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1824) @t, i8 0, i64 1824, i1 false)
49 ; CHECK-NEXT: ret ptr @t
52 %ret = tail call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 1824)
56 ; Check cases where dstlen < len.
58 define ptr @test_no_simplify1() {
59 ; CHECK-LABEL: @test_no_simplify1(
60 ; CHECK-NEXT: [[RET:%.*]] = call ptr @__memset_chk(ptr nonnull @t, i32 0, i64 1824, i64 400)
61 ; CHECK-NEXT: ret ptr [[RET]]
64 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 400)
68 define ptr @test_no_simplify2() {
69 ; CHECK-LABEL: @test_no_simplify2(
70 ; CHECK-NEXT: [[RET:%.*]] = call ptr @__memset_chk(ptr nonnull @t, i32 0, i64 1824, i64 0)
71 ; CHECK-NEXT: ret ptr [[RET]]
74 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 0)
78 define ptr @test_no_simplify3(ptr %dst, i32 %a, i64 %b, i64 %c) {
79 ; CHECK-LABEL: @test_no_simplify3(
80 ; CHECK-NEXT: [[RET:%.*]] = musttail call ptr @__memset_chk(ptr [[DST:%.*]], i32 0, i64 1824, i64 1824)
81 ; CHECK-NEXT: ret ptr [[RET]]
83 %ret = musttail call ptr @__memset_chk(ptr %dst, i32 0, i64 1824, i64 1824)
88 ; Test that RAUW in SimplifyLibCalls for __memset_chk generates valid IR
89 define i32 @test_rauw(ptr %a, ptr %b, ptr %c) {
90 ; CHECK-LABEL: @test_rauw(
92 ; CHECK-NEXT: [[CALL49:%.*]] = call i64 @strlen(ptr noundef nonnull dereferenceable(1) [[A:%.*]])
93 ; CHECK-NEXT: [[ADD180:%.*]] = add i64 [[CALL49]], 1
94 ; CHECK-NEXT: [[YO107:%.*]] = call i64 @llvm.objectsize.i64.p0(ptr [[B:%.*]], i1 false, i1 false, i1 false)
95 ; CHECK-NEXT: [[CALL50:%.*]] = call ptr @__memmove_chk(ptr [[B]], ptr [[A]], i64 [[ADD180]], i64 [[YO107]])
96 ; CHECK-NEXT: [[STRLEN:%.*]] = call i64 @strlen(ptr noundef nonnull dereferenceable(1) [[B]])
97 ; CHECK-NEXT: [[STRCHR1:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[STRLEN]]
98 ; CHECK-NEXT: [[D:%.*]] = load ptr, ptr [[C:%.*]], align 8
99 ; CHECK-NEXT: [[SUB182:%.*]] = ptrtoint ptr [[D]] to i64
100 ; CHECK-NEXT: [[SUB183:%.*]] = ptrtoint ptr [[B]] to i64
101 ; CHECK-NEXT: [[SUB184:%.*]] = sub i64 [[SUB182]], [[SUB183]]
102 ; CHECK-NEXT: [[ADD52_I_I:%.*]] = add nsw i64 [[SUB184]], 1
103 ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 1 [[STRCHR1]], i8 0, i64 [[ADD52_I_I]], i1 false)
104 ; CHECK-NEXT: ret i32 4
107 %call49 = call i64 @strlen(ptr %a)
108 %add180 = add i64 %call49, 1
109 %yo107 = call i64 @llvm.objectsize.i64.p0(ptr %b, i1 false, i1 false, i1 false)
110 %call50 = call ptr @__memmove_chk(ptr %b, ptr %a, i64 %add180, i64 %yo107)
111 %call51i = call ptr @strrchr(ptr %b, i32 0)
112 %d = load ptr, ptr %c, align 8
113 %sub182 = ptrtoint ptr %d to i64
114 %sub183 = ptrtoint ptr %b to i64
115 %sub184 = sub i64 %sub182, %sub183
116 %add52.i.i = add nsw i64 %sub184, 1
117 %call185 = call ptr @__memset_chk(ptr %call51i, i32 0, i64 %add52.i.i, i64 -1)
121 declare ptr @__memmove_chk(ptr, ptr, i64, i64)
122 declare ptr @strrchr(ptr, i32)
123 declare i64 @strlen(ptr nocapture)
124 declare i64 @llvm.objectsize.i64.p0(ptr, i1, i1, i1)
126 declare ptr @__memset_chk(ptr, i32, i64, i64)
128 ; FIXME: memset(malloc(x), 0, x) -> calloc(1, x)
130 define ptr @pr25892(i64 %size) #0 {
131 ; CHECK-LABEL: @pr25892(
133 ; CHECK-NEXT: [[CALL:%.*]] = tail call ptr @malloc(i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]]
134 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[CALL]], null
135 ; CHECK-NEXT: br i1 [[CMP]], label [[CLEANUP:%.*]], label [[IF_END:%.*]]
137 ; CHECK-NEXT: [[CALL2:%.*]] = tail call i64 @llvm.objectsize.i64.p0(ptr nonnull [[CALL]], i1 false, i1 false, i1 false)
138 ; CHECK-NEXT: [[CALL3:%.*]] = tail call ptr @__memset_chk(ptr nonnull [[CALL]], i32 0, i64 [[SIZE]], i64 [[CALL2]]) #[[ATTR3]]
139 ; CHECK-NEXT: br label [[CLEANUP]]
141 ; CHECK-NEXT: [[RETVAL_0:%.*]] = phi ptr [ [[CALL]], [[IF_END]] ], [ null, [[ENTRY:%.*]] ]
142 ; CHECK-NEXT: ret ptr [[RETVAL_0]]
145 %call = tail call ptr @malloc(i64 %size) #1
146 %cmp = icmp eq ptr %call, null
147 br i1 %cmp, label %cleanup, label %if.end
149 %call2 = tail call i64 @llvm.objectsize.i64.p0(ptr nonnull %call, i1 false, i1 false, i1 false)
150 %call3 = tail call ptr @__memset_chk(ptr nonnull %call, i32 0, i64 %size, i64 %call2) #1
153 %retval.0 = phi ptr [ %call, %if.end ], [ null, %entry ]
158 define ptr @test_no_incompatible_attr(ptr %mem, i32 %val, i32 %size) {
159 ; CHECK-LABEL: @test_no_incompatible_attr(
160 ; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1824) @t, i8 0, i64 1824, i1 false)
161 ; CHECK-NEXT: ret ptr @t
164 %ret = call dereferenceable(1) ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 1824)
168 declare noalias ptr @malloc(i64) #1
170 attributes #0 = { nounwind ssp uwtable }
171 attributes #1 = { nounwind }
172 attributes #2 = { nounwind readnone }