174 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; Test lib call simplification of __memset_chk calls with various values3; for dstlen and len.4;5; RUN: opt < %s -passes=instcombine -S | FileCheck %s6; rdar://77190857 8target 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"9 10%struct.T = type { [100 x i32], [100 x i32], [1024 x i8] }11@t = common global %struct.T zeroinitializer12 13; Check cases where dstlen >= len.14 15define 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 @t19;20 21 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 1824)22 ret ptr %ret23}24 25define 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 @t29;30 31 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 3648)32 ret ptr %ret33}34 35define 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 @t39;40 41 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 -1)42 ret ptr %ret43}44 45; Same as @test_simplify1 with tail call.46define 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 @t50;51 52 %ret = tail call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 1824)53 ret ptr %ret54}55 56; Check cases where dstlen < len.57 58define 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]]62;63 64 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 400)65 ret ptr %ret66}67 68define 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]]72;73 74 %ret = call ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 0)75 ret ptr %ret76}77 78define 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]]82;83 %ret = musttail call ptr @__memset_chk(ptr %dst, i32 0, i64 1824, i64 1824)84 ret ptr %ret85}86 87 88; Test that RAUW in SimplifyLibCalls for __memset_chk generates valid IR89define i32 @test_rauw(ptr %a, ptr %b, ptr %c) {90; CHECK-LABEL: @test_rauw(91; CHECK-NEXT: entry:92; CHECK-NEXT: [[CALL49:%.*]] = call i64 @strlen(ptr noundef nonnull dereferenceable(1) [[A:%.*]])93; CHECK-NEXT: [[ADD180:%.*]] = add i64 [[CALL49]], 194; 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 nonnull [[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 899; CHECK-NEXT: [[SUB182:%.*]] = ptrtoint ptr [[D]] to i64100; CHECK-NEXT: [[SUB183:%.*]] = ptrtoint ptr [[B]] to i64101; CHECK-NEXT: [[SUB184:%.*]] = sub i64 [[SUB182]], [[SUB183]]102; CHECK-NEXT: [[ADD52_I_I:%.*]] = add nsw i64 [[SUB184]], 1103; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr nonnull align 1 [[STRCHR1]], i8 0, i64 [[ADD52_I_I]], i1 false)104; CHECK-NEXT: ret i32 4105;106entry:107 %call49 = call i64 @strlen(ptr %a)108 %add180 = add i64 %call49, 1109 %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 8113 %sub182 = ptrtoint ptr %d to i64114 %sub183 = ptrtoint ptr %b to i64115 %sub184 = sub i64 %sub182, %sub183116 %add52.i.i = add nsw i64 %sub184, 1117 %call185 = call ptr @__memset_chk(ptr %call51i, i32 0, i64 %add52.i.i, i64 -1)118 ret i32 4119}120 121declare ptr @__memmove_chk(ptr, ptr, i64, i64)122declare ptr @strrchr(ptr, i32)123declare i64 @strlen(ptr nocapture)124declare i64 @llvm.objectsize.i64.p0(ptr, i1, i1, i1)125 126declare ptr @__memset_chk(ptr, i32, i64, i64)127 128; FIXME: memset(malloc(x), 0, x) -> calloc(1, x)129 130define ptr @pr25892(i64 %size) #0 {131; CHECK-LABEL: @pr25892(132; CHECK-NEXT: entry:133; CHECK-NEXT: [[CALL:%.*]] = tail call ptr @malloc(i64 [[SIZE:%.*]]) #[[ATTR3:[0-9]+]]134; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[CALL]], null135; CHECK-NEXT: br i1 [[CMP]], label [[CLEANUP:%.*]], label [[IF_END:%.*]]136; CHECK: 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]]140; CHECK: cleanup:141; CHECK-NEXT: [[RETVAL_0:%.*]] = phi ptr [ [[CALL]], [[IF_END]] ], [ null, [[ENTRY:%.*]] ]142; CHECK-NEXT: ret ptr [[RETVAL_0]]143;144entry:145 %call = tail call ptr @malloc(i64 %size) #1146 %cmp = icmp eq ptr %call, null147 br i1 %cmp, label %cleanup, label %if.end148if.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) #1151 br label %cleanup152cleanup:153 %retval.0 = phi ptr [ %call, %if.end ], [ null, %entry ]154 ret ptr %retval.0155 156}157 158define 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 @t162;163 164 %ret = call dereferenceable(1) ptr @__memset_chk(ptr @t, i32 0, i64 1824, i64 1824)165 ret ptr %ret166}167 168declare noalias ptr @malloc(i64) #1169 170attributes #0 = { nounwind ssp uwtable }171attributes #1 = { nounwind }172attributes #2 = { nounwind readnone }173 174