189 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 8; Test behavior for wchar_size==29!llvm.module.flags = !{!0}10!0 = !{i32 1, !"wchar_size", i32 2}11 12declare i64 @wcslen(ptr)13 14@hello = constant [6 x i16] [i16 104, i16 101, i16 108, i16 108, i16 111, i16 0]15@longer = constant [7 x i16] [i16 108, i16 111, i16 110, i16 103, i16 101, i16 114, i16 0]16@null = constant [1 x i16] zeroinitializer17@null_hello = constant [7 x i16] [i16 0, i16 104, i16 101, i16 108, i16 108, i16 111, i16 0]18@nullstring = constant i16 019@a = common global [32 x i16] zeroinitializer, align 120@null_hello_mid = constant [13 x i16] [i16 104, i16 101, i16 108, i16 108, i16 111, i16 32, i16 119, i16 111, i16 114, i16 0, i16 108, i16 100, i16 0]21 22define i64 @test_simplify1() {23; CHECK-LABEL: @test_simplify1(24; CHECK-NEXT: ret i64 525;26 %hello_l = call i64 @wcslen(ptr @hello)27 ret i64 %hello_l28}29 30define i64 @test_simplify2() {31; CHECK-LABEL: @test_simplify2(32; CHECK-NEXT: ret i64 033;34 %null_l = call i64 @wcslen(ptr @null)35 ret i64 %null_l36}37 38define i64 @test_simplify3() {39; CHECK-LABEL: @test_simplify3(40; CHECK-NEXT: ret i64 041;42 %null_hello_l = call i64 @wcslen(ptr @null_hello)43 ret i64 %null_hello_l44}45 46define i64 @test_simplify4() {47; CHECK-LABEL: @test_simplify4(48; CHECK-NEXT: ret i64 049;50 %len = tail call i64 @wcslen(ptr @nullstring) nounwind51 ret i64 %len52}53 54; Check wcslen(x) == 0 --> *x == 0.55 56define i1 @test_simplify5() {57; CHECK-LABEL: @test_simplify5(58; CHECK-NEXT: ret i1 false59;60 %hello_l = call i64 @wcslen(ptr @hello)61 %eq_hello = icmp eq i64 %hello_l, 062 ret i1 %eq_hello63}64 65define i1 @test_simplify6(ptr %str_p) {66; CHECK-LABEL: @test_simplify6(67; CHECK-NEXT: [[CHAR0:%.*]] = load i16, ptr [[STR_P:%.*]], align 268; CHECK-NEXT: [[EQ_NULL:%.*]] = icmp eq i16 [[CHAR0]], 069; CHECK-NEXT: ret i1 [[EQ_NULL]]70;71 %str_l = call i64 @wcslen(ptr %str_p)72 %eq_null = icmp eq i64 %str_l, 073 ret i1 %eq_null74}75 76; Check wcslen(x) != 0 --> *x != 0.77 78define i1 @test_simplify7() {79; CHECK-LABEL: @test_simplify7(80; CHECK-NEXT: ret i1 true81;82 %hello_l = call i64 @wcslen(ptr @hello)83 %ne_hello = icmp ne i64 %hello_l, 084 ret i1 %ne_hello85}86 87define i1 @test_simplify8(ptr %str_p) {88; CHECK-LABEL: @test_simplify8(89; CHECK-NEXT: [[CHAR0:%.*]] = load i16, ptr [[STR_P:%.*]], align 290; CHECK-NEXT: [[NE_NULL:%.*]] = icmp ne i16 [[CHAR0]], 091; CHECK-NEXT: ret i1 [[NE_NULL]]92;93 %str_l = call i64 @wcslen(ptr %str_p)94 %ne_null = icmp ne i64 %str_l, 095 ret i1 %ne_null96}97 98define i64 @test_simplify9(i1 %x) {99; CHECK-LABEL: @test_simplify9(100; CHECK-NEXT: [[L:%.*]] = select i1 [[X:%.*]], i64 5, i64 6101; CHECK-NEXT: ret i64 [[L]]102;103 %s = select i1 %x, ptr @hello, ptr @longer104 %l = call i64 @wcslen(ptr %s)105 ret i64 %l106}107 108; Check the case that should be simplified to a sub instruction.109; wcslen(@hello + x) --> 5 - x110 111define i64 @test_simplify10(i16 %x) {112; CHECK-LABEL: @test_simplify10(113; CHECK-NEXT: [[TMP1:%.*]] = sext i16 [[X:%.*]] to i64114; CHECK-NEXT: [[HELLO_L:%.*]] = sub nsw i64 5, [[TMP1]]115; CHECK-NEXT: ret i64 [[HELLO_L]]116;117 %hello_p = getelementptr inbounds [6 x i16], ptr @hello, i16 0, i16 %x118 %hello_l = call i64 @wcslen(ptr %hello_p)119 ret i64 %hello_l120}121 122; wcslen(@null_hello_mid + (x & 7)) --> 9 - (x & 7)123 124define i64 @test_simplify11(i16 %x) {125; CHECK-LABEL: @test_simplify11(126; CHECK-NEXT: [[AND:%.*]] = and i16 [[X:%.*]], 7127; CHECK-NEXT: [[NARROW:%.*]] = sub nuw nsw i16 9, [[AND]]128; CHECK-NEXT: [[HELLO_L:%.*]] = zext nneg i16 [[NARROW]] to i64129; CHECK-NEXT: ret i64 [[HELLO_L]]130;131 %and = and i16 %x, 7132 %hello_p = getelementptr inbounds [13 x i16], ptr @null_hello_mid, i16 0, i16 %and133 %hello_l = call i64 @wcslen(ptr %hello_p)134 ret i64 %hello_l135}136 137; Check cases that shouldn't be simplified.138 139define i64 @test_no_simplify1() {140; CHECK-LABEL: @test_no_simplify1(141; CHECK-NEXT: [[A_L:%.*]] = call i64 @wcslen(ptr nonnull @a)142; CHECK-NEXT: ret i64 [[A_L]]143;144 %a_l = call i64 @wcslen(ptr @a)145 ret i64 %a_l146}147 148; wcslen(@null_hello + x) should not be simplified to a sub instruction.149 150define i64 @test_no_simplify2(i16 %x) {151; CHECK-LABEL: @test_no_simplify2(152; CHECK-NEXT: [[TMP1:%.*]] = sext i16 [[X:%.*]] to i64153; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds i16, ptr @null_hello, i64 [[TMP1]]154; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])155; CHECK-NEXT: ret i64 [[HELLO_L]]156;157 %hello_p = getelementptr inbounds [7 x i16], ptr @null_hello, i16 0, i16 %x158 %hello_l = call i64 @wcslen(ptr %hello_p)159 ret i64 %hello_l160}161 162; wcslen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.163 164define i64 @test_no_simplify3(i16 %x) {165; CHECK-LABEL: @test_no_simplify3(166; CHECK-NEXT: [[AND:%.*]] = and i16 [[X:%.*]], 15167; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i16 [[AND]] to i64168; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds nuw i16, ptr @null_hello_mid, i64 [[TMP1]]169; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(ptr nonnull [[HELLO_P]])170; CHECK-NEXT: ret i64 [[HELLO_L]]171;172 %and = and i16 %x, 15173 %hello_p = getelementptr inbounds [13 x i16], ptr @null_hello_mid, i16 0, i16 %and174 %hello_l = call i64 @wcslen(ptr %hello_p)175 ret i64 %hello_l176}177 178@str32 = constant [1 x i32] [i32 0]179 180; This is safe to simplify despite the type mismatch.181 182define i64 @test_no_simplify4() {183; CHECK-LABEL: @test_no_simplify4(184; CHECK-NEXT: ret i64 0185;186 %l = call i64 @wcslen(ptr @str32)187 ret i64 %l188}189