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 -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 i8* @test_simplify1() {
16 ; CHECK-LABEL: @test_simplify1(
17 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 4 dereferenceable(1824) bitcast (%struct.T* @t to i8*), i8 0, i64 1824, i1 false)
18 ; CHECK-NEXT: ret i8* bitcast (%struct.T* @t to i8*)
20 %dst = bitcast %struct.T* @t to i8*
22 %ret = call i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 1824)
26 define i8* @test_simplify2() {
27 ; CHECK-LABEL: @test_simplify2(
28 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 4 dereferenceable(1824) bitcast (%struct.T* @t to i8*), i8 0, i64 1824, i1 false)
29 ; CHECK-NEXT: ret i8* bitcast (%struct.T* @t to i8*)
31 %dst = bitcast %struct.T* @t to i8*
33 %ret = call i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 3648)
37 define i8* @test_simplify3() {
38 ; CHECK-LABEL: @test_simplify3(
39 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 4 dereferenceable(1824) bitcast (%struct.T* @t to i8*), i8 0, i64 1824, i1 false)
40 ; CHECK-NEXT: ret i8* bitcast (%struct.T* @t to i8*)
42 %dst = bitcast %struct.T* @t to i8*
44 %ret = call i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 -1)
48 ; Same as @test_simplify1 with tail call.
49 define i8* @test_simplify4() {
50 ; CHECK-LABEL: @test_simplify4(
51 ; CHECK-NEXT: tail call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 4 dereferenceable(1824) bitcast (%struct.T* @t to i8*), i8 0, i64 1824, i1 false)
52 ; CHECK-NEXT: ret i8* bitcast (%struct.T* @t to i8*)
54 %dst = bitcast %struct.T* @t to i8*
56 %ret = tail call i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 1824)
60 ; Check cases where dstlen < len.
62 define i8* @test_no_simplify1() {
63 ; CHECK-LABEL: @test_no_simplify1(
64 ; CHECK-NEXT: [[RET:%.*]] = call i8* @__memset_chk(i8* bitcast (%struct.T* @t to i8*), i32 0, i64 1824, i64 400)
65 ; CHECK-NEXT: ret i8* [[RET]]
67 %dst = bitcast %struct.T* @t to i8*
69 %ret = call i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 400)
73 define i8* @test_no_simplify2() {
74 ; CHECK-LABEL: @test_no_simplify2(
75 ; CHECK-NEXT: [[RET:%.*]] = call i8* @__memset_chk(i8* bitcast (%struct.T* @t to i8*), i32 0, i64 1824, i64 0)
76 ; CHECK-NEXT: ret i8* [[RET]]
78 %dst = bitcast %struct.T* @t to i8*
80 %ret = call i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 0)
84 define i8* @test_no_simplify3(i8* %dst, i32 %a, i64 %b, i64 %c) {
85 ; CHECK-LABEL: @test_no_simplify3(
86 ; CHECK-NEXT: %ret = musttail call i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 1824)
87 ; CHECK-NEXT: ret i8* %ret
89 %ret = musttail call i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 1824)
94 ; Test that RAUW in SimplifyLibCalls for __memset_chk generates valid IR
95 define i32 @test_rauw(i8* %a, i8* %b, i8** %c) {
96 ; CHECK-LABEL: @test_rauw(
98 ; CHECK-NEXT: [[CALL49:%.*]] = call i64 @strlen(i8* noundef nonnull dereferenceable(1) [[A:%.*]])
99 ; CHECK-NEXT: [[ADD180:%.*]] = add i64 [[CALL49]], 1
100 ; CHECK-NEXT: [[YO107:%.*]] = call i64 @llvm.objectsize.i64.p0i8(i8* [[B:%.*]], i1 false, i1 false, i1 false)
101 ; CHECK-NEXT: [[CALL50:%.*]] = call i8* @__memmove_chk(i8* [[B]], i8* [[A]], i64 [[ADD180]], i64 [[YO107]])
102 ; CHECK-NEXT: [[STRLEN:%.*]] = call i64 @strlen(i8* noundef nonnull dereferenceable(1) [[B]])
103 ; CHECK-NEXT: [[STRCHR1:%.*]] = getelementptr i8, i8* [[B]], i64 [[STRLEN]]
104 ; CHECK-NEXT: [[D:%.*]] = load i8*, i8** [[C:%.*]], align 8
105 ; CHECK-NEXT: [[SUB182:%.*]] = ptrtoint i8* [[D]] to i64
106 ; CHECK-NEXT: [[SUB183:%.*]] = ptrtoint i8* [[B]] to i64
107 ; CHECK-NEXT: [[SUB184:%.*]] = sub i64 [[SUB182]], [[SUB183]]
108 ; CHECK-NEXT: [[ADD52_I_I:%.*]] = add nsw i64 [[SUB184]], 1
109 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[STRCHR1]], i8 0, i64 [[ADD52_I_I]], i1 false)
110 ; CHECK-NEXT: ret i32 4
113 %call49 = call i64 @strlen(i8* %a)
114 %add180 = add i64 %call49, 1
115 %yo107 = call i64 @llvm.objectsize.i64.p0i8(i8* %b, i1 false, i1 false, i1 false)
116 %call50 = call i8* @__memmove_chk(i8* %b, i8* %a, i64 %add180, i64 %yo107)
117 %call51i = call i8* @strrchr(i8* %b, i32 0)
118 %d = load i8*, i8** %c, align 8
119 %sub182 = ptrtoint i8* %d to i64
120 %sub183 = ptrtoint i8* %b to i64
121 %sub184 = sub i64 %sub182, %sub183
122 %add52.i.i = add nsw i64 %sub184, 1
123 %call185 = call i8* @__memset_chk(i8* %call51i, i32 0, i64 %add52.i.i, i64 -1)
127 declare i8* @__memmove_chk(i8*, i8*, i64, i64)
128 declare i8* @strrchr(i8*, i32)
129 declare i64 @strlen(i8* nocapture)
130 declare i64 @llvm.objectsize.i64.p0i8(i8*, i1, i1, i1)
132 declare i8* @__memset_chk(i8*, i32, i64, i64)
134 ; FIXME: memset(malloc(x), 0, x) -> calloc(1, x)
136 define float* @pr25892(i64 %size) #0 {
137 ; CHECK-LABEL: @pr25892(
139 ; CHECK-NEXT: [[CALL:%.*]] = tail call i8* @malloc(i64 [[SIZE:%.*]]) [[ATTR3:#.*]]
140 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8* [[CALL]], null
141 ; CHECK-NEXT: br i1 [[CMP]], label [[CLEANUP:%.*]], label [[IF_END:%.*]]
143 ; CHECK-NEXT: [[BC:%.*]] = bitcast i8* [[CALL]] to float*
144 ; CHECK-NEXT: [[CALL2:%.*]] = tail call i64 @llvm.objectsize.i64.p0i8(i8* nonnull [[CALL]], i1 false, i1 false, i1 false)
145 ; CHECK-NEXT: [[CALL3:%.*]] = tail call i8* @__memset_chk(i8* nonnull [[CALL]], i32 0, i64 [[SIZE]], i64 [[CALL2]]) [[ATTR3]]
146 ; CHECK-NEXT: br label [[CLEANUP]]
148 ; CHECK-NEXT: [[RETVAL_0:%.*]] = phi float* [ [[BC]], [[IF_END]] ], [ null, [[ENTRY:%.*]] ]
149 ; CHECK-NEXT: ret float* [[RETVAL_0]]
152 %call = tail call i8* @malloc(i64 %size) #1
153 %cmp = icmp eq i8* %call, null
154 br i1 %cmp, label %cleanup, label %if.end
156 %bc = bitcast i8* %call to float*
157 %call2 = tail call i64 @llvm.objectsize.i64.p0i8(i8* nonnull %call, i1 false, i1 false, i1 false)
158 %call3 = tail call i8* @__memset_chk(i8* nonnull %call, i32 0, i64 %size, i64 %call2) #1
161 %retval.0 = phi float* [ %bc, %if.end ], [ null, %entry ]
166 define i8* @test_no_incompatible_attr(i8* %mem, i32 %val, i32 %size) {
167 ; CHECK-LABEL: @test_no_incompatible_attr(
168 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 4 dereferenceable(1824) bitcast (%struct.T* @t to i8*), i8 0, i64 1824, i1 false)
169 ; CHECK-NEXT: ret i8* bitcast (%struct.T* @t to i8*)
171 %dst = bitcast %struct.T* @t to i8*
173 %ret = call dereferenceable(1) i8* @__memset_chk(i8* %dst, i32 0, i64 1824, i64 1824)
177 declare noalias i8* @malloc(i64) #1
179 attributes #0 = { nounwind ssp uwtable }
180 attributes #1 = { nounwind }
181 attributes #2 = { nounwind readnone }