342 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; Test that the wcslen library call simplifier works correctly.3;4; RUN: opt < %s -passes=instcombine -S | FileCheck %s5 6target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"7 8declare i64 @wcslen(ptr)9 10!0 = !{i32 1, !"wchar_size", i32 4}11!llvm.module.flags = !{!0}12 13@hello = constant [6 x i32] [i32 104, i32 101, i32 108, i32 108, i32 111, i32 0]14@longer = constant [7 x i32] [i32 108, i32 111, i32 110, i32 103, i32 101, i32 114, i32 0]15@null = constant [1 x i32] zeroinitializer16@null_hello = constant [7 x i32] [i32 0, i32 104, i32 101, i32 108, i32 108, i32 111, i32 0]17@nullstring = constant i32 018@a = common global [32 x i32] zeroinitializer, align 119@null_hello_mid = constant [13 x i32] [i32 104, i32 101, i32 108, i32 108, i32 111, i32 32, i32 119, i32 111, i32 114, i32 0, i32 108, i32 100, i32 0]20 21define i64 @test_simplify1() {22; CHECK-LABEL: @test_simplify1(23; CHECK-NEXT: ret i64 524;25 %hello_l = call i64 @wcslen(ptr @hello)26 ret i64 %hello_l27}28 29define i64 @test_simplify2() {30; CHECK-LABEL: @test_simplify2(31; CHECK-NEXT: ret i64 032;33 %null_l = call i64 @wcslen(ptr @null)34 ret i64 %null_l35}36 37define i64 @test_simplify3() {38; CHECK-LABEL: @test_simplify3(39; CHECK-NEXT: ret i64 040;41 %null_hello_l = call i64 @wcslen(ptr @null_hello)42 ret i64 %null_hello_l43}44 45define i64 @test_simplify4() {46; CHECK-LABEL: @test_simplify4(47; CHECK-NEXT: ret i64 048;49 %len = tail call i64 @wcslen(ptr @nullstring) nounwind50 ret i64 %len51}52 53; Check wcslen(x) == 0 --> *x == 0.54 55define i1 @test_simplify5() {56; CHECK-LABEL: @test_simplify5(57; CHECK-NEXT: ret i1 false58;59 %hello_l = call i64 @wcslen(ptr @hello)60 %eq_hello = icmp eq i64 %hello_l, 061 ret i1 %eq_hello62}63 64define i1 @test_simplify6(ptr %str_p) {65; CHECK-LABEL: @test_simplify6(66; CHECK-NEXT: [[CHAR0:%.*]] = load i32, ptr [[STR_P:%.*]], align 467; CHECK-NEXT: [[EQ_NULL:%.*]] = icmp eq i32 [[CHAR0]], 068; CHECK-NEXT: ret i1 [[EQ_NULL]]69;70 %str_l = call i64 @wcslen(ptr %str_p)71 %eq_null = icmp eq i64 %str_l, 072 ret i1 %eq_null73}74 75; Check wcslen(x) != 0 --> *x != 0.76 77define i1 @test_simplify7() {78; CHECK-LABEL: @test_simplify7(79; CHECK-NEXT: ret i1 true80;81 %hello_l = call i64 @wcslen(ptr @hello)82 %ne_hello = icmp ne i64 %hello_l, 083 ret i1 %ne_hello84}85 86define i1 @test_simplify8(ptr %str_p) {87; CHECK-LABEL: @test_simplify8(88; CHECK-NEXT: [[CHAR0:%.*]] = load i32, ptr [[STR_P:%.*]], align 489; CHECK-NEXT: [[NE_NULL:%.*]] = icmp ne i32 [[CHAR0]], 090; CHECK-NEXT: ret i1 [[NE_NULL]]91;92 %str_l = call i64 @wcslen(ptr %str_p)93 %ne_null = icmp ne i64 %str_l, 094 ret i1 %ne_null95}96 97define i64 @test_simplify9(i1 %x) {98; CHECK-LABEL: @test_simplify9(99; CHECK-NEXT: [[L:%.*]] = select i1 [[X:%.*]], i64 5, i64 6100; CHECK-NEXT: ret i64 [[L]]101;102 %s = select i1 %x, ptr @hello, ptr @longer103 %l = call i64 @wcslen(ptr %s)104 ret i64 %l105}106 107; Check the case that should be simplified to a sub instruction.108; wcslen(@hello + x) --> 5 - x109 110define i64 @test_simplify10(i32 %x) {111; CHECK-LABEL: @test_simplify10(112; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64113; CHECK-NEXT: [[HELLO_L:%.*]] = sub nsw i64 5, [[TMP1]]114; CHECK-NEXT: ret i64 [[HELLO_L]]115;116 %hello_p = getelementptr inbounds [6 x i32], ptr @hello, i32 0, i32 %x117 %hello_l = call i64 @wcslen(ptr %hello_p)118 ret i64 %hello_l119}120 121define i64 @test_simplify10_gepi32(i64 %x) {122; CHECK-LABEL: @test_simplify10_gepi32(123; CHECK-NEXT: [[HELLO_L:%.*]] = sub i64 5, [[X:%.*]]124; CHECK-NEXT: ret i64 [[HELLO_L]]125;126 %hello_p = getelementptr inbounds i32, ptr @hello, i64 %x127 %hello_l = call i64 @wcslen(ptr %hello_p)128 ret i64 %hello_l129}130 131define i64 @test_simplify10_gepi64(i32 %x) {132; CHECK-LABEL: @test_simplify10_gepi64(133; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64134; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds i64, ptr @hello, i64 [[TMP1]]135; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])136; CHECK-NEXT: ret i64 [[HELLO_L]]137;138 %hello_p = getelementptr inbounds i64, ptr @hello, i32 %x139 %hello_l = call i64 @wcslen(ptr %hello_p)140 ret i64 %hello_l141}142 143define i64 @test_simplify10_gepi16(i64 %x) {144; CHECK-LABEL: @test_simplify10_gepi16(145; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds i16, ptr @hello, i64 [[X:%.*]]146; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])147; CHECK-NEXT: ret i64 [[HELLO_L]]148;149 %hello_p = getelementptr inbounds i16, ptr @hello, i64 %x150 %hello_l = call i64 @wcslen(ptr %hello_p)151 ret i64 %hello_l152}153 154define i64 @test_simplify10_gepi8(i64 %x) {155; CHECK-LABEL: @test_simplify10_gepi8(156; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds i8, ptr @hello, i64 [[TMP1:%.*]]157; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])158; CHECK-NEXT: ret i64 [[HELLO_L]]159;160 %hello_p = getelementptr inbounds i8, ptr @hello, i64 %x161 %hello_l = call i64 @wcslen(ptr %hello_p)162 ret i64 %hello_l163}164 165define i64 @test_simplify10_gepi8mul4(i64 %x) {166; CHECK-LABEL: @test_simplify10_gepi8mul4(167; CHECK-NEXT: [[Y:%.*]] = shl i64 [[X:%.*]], 2168; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds i8, ptr @hello, i64 [[Y]]169; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])170; CHECK-NEXT: ret i64 [[HELLO_L]]171;172 %y = mul i64 %x, 4173 %hello_p = getelementptr inbounds i8, ptr @hello, i64 %y174 %hello_l = call i64 @wcslen(ptr %hello_p)175 ret i64 %hello_l176}177 178; wcslen(@null_hello_mid + (x & 7)) --> 9 - (x & 7)179 180define i64 @test_simplify11(i32 %x) {181; CHECK-LABEL: @test_simplify11(182; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7183; CHECK-NEXT: [[NARROW:%.*]] = sub nuw nsw i32 9, [[AND]]184; CHECK-NEXT: [[HELLO_L:%.*]] = zext nneg i32 [[NARROW]] to i64185; CHECK-NEXT: ret i64 [[HELLO_L]]186;187 %and = and i32 %x, 7188 %hello_p = getelementptr inbounds [13 x i32], ptr @null_hello_mid, i32 0, i32 %and189 %hello_l = call i64 @wcslen(ptr %hello_p)190 ret i64 %hello_l191}192 193define i64 @test_simplify11_gepi32(i32 %x) {194; CHECK-LABEL: @test_simplify11_gepi32(195; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7196; CHECK-NEXT: [[NARROW:%.*]] = sub nuw nsw i32 9, [[AND]]197; CHECK-NEXT: [[HELLO_L:%.*]] = zext nneg i32 [[NARROW]] to i64198; CHECK-NEXT: ret i64 [[HELLO_L]]199;200 %and = and i32 %x, 7201 %hello_p = getelementptr inbounds i32, ptr @null_hello_mid, i32 %and202 %hello_l = call i64 @wcslen(ptr %hello_p)203 ret i64 %hello_l204}205 206define i64 @test_simplify11_gepi8(i32 %x) {207; CHECK-LABEL: @test_simplify11_gepi8(208; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7209; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i32 [[AND]] to i64210; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds nuw i8, ptr @null_hello_mid, i64 [[TMP1]]211; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])212; CHECK-NEXT: ret i64 [[HELLO_L]]213;214 %and = and i32 %x, 7215 %hello_p = getelementptr inbounds i8, ptr @null_hello_mid, i32 %and216 %hello_l = call i64 @wcslen(ptr %hello_p)217 ret i64 %hello_l218}219 220; Check cases that shouldn't be simplified.221 222define i64 @test_no_simplify1() {223; CHECK-LABEL: @test_no_simplify1(224; CHECK-NEXT: [[A_L:%.*]] = call i64 @wcslen(ptr nonnull @a)225; CHECK-NEXT: ret i64 [[A_L]]226;227 %a_l = call i64 @wcslen(ptr @a)228 ret i64 %a_l229}230 231; wcslen(@null_hello + x) should not be simplified to a sub instruction.232 233define i64 @test_no_simplify2(i32 %x) {234; CHECK-LABEL: @test_no_simplify2(235; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64236; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds i32, ptr @null_hello, i64 [[TMP1]]237; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])238; CHECK-NEXT: ret i64 [[HELLO_L]]239;240 %hello_p = getelementptr inbounds [7 x i32], ptr @null_hello, i32 0, i32 %x241 %hello_l = call i64 @wcslen(ptr %hello_p)242 ret i64 %hello_l243}244 245define i64 @test_no_simplify2_no_null_opt(i32 %x) #0 {246; CHECK-LABEL: @test_no_simplify2_no_null_opt(247; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64248; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds i32, ptr @null_hello, i64 [[TMP1]]249; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr [[HELLO_P]])250; CHECK-NEXT: ret i64 [[HELLO_L]]251;252 %hello_p = getelementptr inbounds [7 x i32], ptr @null_hello, i32 0, i32 %x253 %hello_l = call i64 @wcslen(ptr %hello_p)254 ret i64 %hello_l255}256 257; wcslen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.258 259define i64 @test_no_simplify3(i32 %x) {260; CHECK-LABEL: @test_no_simplify3(261; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15262; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i32 [[AND]] to i64263; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds nuw i32, ptr @null_hello_mid, i64 [[TMP1]]264; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])265; CHECK-NEXT: ret i64 [[HELLO_L]]266;267 %and = and i32 %x, 15268 %hello_p = getelementptr inbounds [13 x i32], ptr @null_hello_mid, i32 0, i32 %and269 %hello_l = call i64 @wcslen(ptr %hello_p)270 ret i64 %hello_l271}272 273define i64 @test_no_simplify3_no_null_opt(i32 %x) #0 {274; CHECK-LABEL: @test_no_simplify3_no_null_opt(275; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15276; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i32 [[AND]] to i64277; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds nuw i32, ptr @null_hello_mid, i64 [[TMP1]]278; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])279; CHECK-NEXT: ret i64 [[HELLO_L]]280;281 %and = and i32 %x, 15282 %hello_p = getelementptr inbounds [13 x i32], ptr @null_hello_mid, i32 0, i32 %and283 %hello_l = call i64 @wcslen(ptr %hello_p)284 ret i64 %hello_l285}286 287@str16 = constant [1 x i16] [i16 0]288 289; Fold the invalid call to zero. This is safer than letting the undefined290; library call take place even though it prevents sanitizers from detecting291; it.292 293define i64 @test_simplify12() {294; CHECK-LABEL: @test_simplify12(295; CHECK-NEXT: ret i64 0296;297 %l = call i64 @wcslen(ptr @str16)298 ret i64 %l299}300 301@ws = constant [10 x i32] [i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0]302 303; Fold wcslen(ws + 2) => 7.304define i64 @fold_wcslen_1() {305; CHECK-LABEL: @fold_wcslen_1(306; CHECK-NEXT: ret i64 7307;308 %p = getelementptr inbounds [10 x i32], ptr @ws, i64 0, i64 2309 %len = tail call i64 @wcslen(ptr %p)310 ret i64 %len311}312 313; Should not crash on this, and no optimization expected (idea is to get into314; llvm::getConstantDataArrayInfo looking for an array with 32-bit elements but315; with an offset that isn't a multiple of the element size).316define i64 @no_fold_wcslen_1() {317; CHECK-LABEL: @no_fold_wcslen_1(318; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull getelementptr inbounds nuw (i8, ptr @ws, i64 3))319; CHECK-NEXT: ret i64 [[LEN]]320;321 %p = getelementptr [15 x i8], ptr @ws, i64 0, i64 3322 %len = tail call i64 @wcslen(ptr %p)323 ret i64 %len324}325 326@s8 = constant [10 x i8] [i8 9, i8 8, i8 7, i8 6, i8 5, i8 4, i8 3, i8 2, i8 1, i8 0]327 328; Should not crash on this, and no optimization expected (idea is to get into329; llvm::getConstantDataArrayInfo looking for an array with 32-bit elements but330; with an offset that isn't a multiple of the element size).331define i64 @no_fold_wcslen_2() {332; CHECK-LABEL: @no_fold_wcslen_2(333; CHECK-NEXT: [[LEN:%.*]] = tail call i64 @wcslen(ptr nonnull getelementptr inbounds nuw (i8, ptr @s8, i64 3))334; CHECK-NEXT: ret i64 [[LEN]]335;336 %p = getelementptr [10 x i8], ptr @s8, i64 0, i64 3337 %len = tail call i64 @wcslen(ptr %p)338 ret i64 %len339}340 341attributes #0 = { null_pointer_is_valid }342