1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; Test that the strlen library call simplifier works correctly. 3; 4; RUN: opt < %s -passes=instcombine -S | FileCheck %s 5 6target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" 7 8@hello = constant [6 x i8] c"hello\00" 9@longer = constant [7 x i8] c"longer\00" 10@null = constant [1 x i8] zeroinitializer 11@null_hello = constant [7 x i8] c"\00hello\00" 12@nullstring = constant i8 0 13@a = common global [32 x i8] zeroinitializer, align 1 14@null_hello_mid = constant [13 x i8] c"hello wor\00ld\00" 15 16declare i32 @strlen(ptr) 17 18; Check strlen(string constant) -> integer constant. 19 20define i32 @test_simplify1() { 21; CHECK-LABEL: @test_simplify1( 22; CHECK-NEXT: ret i32 5 23; 24 %hello_l = call i32 @strlen(ptr @hello) 25 ret i32 %hello_l 26} 27 28define i32 @test_simplify2() { 29; CHECK-LABEL: @test_simplify2( 30; CHECK-NEXT: ret i32 0 31; 32 %null_l = call i32 @strlen(ptr @null) 33 ret i32 %null_l 34} 35 36define i32 @test_simplify3() { 37; CHECK-LABEL: @test_simplify3( 38; CHECK-NEXT: ret i32 0 39; 40 %null_hello_l = call i32 @strlen(ptr @null_hello) 41 ret i32 %null_hello_l 42} 43 44define i32 @test_simplify4() { 45; CHECK-LABEL: @test_simplify4( 46; CHECK-NEXT: ret i32 0 47; 48 %len = tail call i32 @strlen(ptr @nullstring) nounwind 49 ret i32 %len 50} 51 52; Check strlen(x) == 0 --> *x == 0. 53 54define i1 @test_simplify5() { 55; CHECK-LABEL: @test_simplify5( 56; CHECK-NEXT: ret i1 false 57; 58 %hello_l = call i32 @strlen(ptr @hello) 59 %eq_hello = icmp eq i32 %hello_l, 0 60 ret i1 %eq_hello 61} 62 63define i1 @test_simplify6(ptr %str_p) { 64; CHECK-LABEL: @test_simplify6( 65; CHECK-NEXT: [[CHAR0:%.*]] = load i8, ptr [[STR_P:%.*]], align 1 66; CHECK-NEXT: [[EQ_NULL:%.*]] = icmp eq i8 [[CHAR0]], 0 67; CHECK-NEXT: ret i1 [[EQ_NULL]] 68; 69 %str_l = call i32 @strlen(ptr %str_p) 70 %eq_null = icmp eq i32 %str_l, 0 71 ret i1 %eq_null 72} 73 74; Check strlen(x) != 0 --> *x != 0. 75 76define i1 @test_simplify7() { 77; CHECK-LABEL: @test_simplify7( 78; CHECK-NEXT: ret i1 true 79; 80 %hello_l = call i32 @strlen(ptr @hello) 81 %ne_hello = icmp ne i32 %hello_l, 0 82 ret i1 %ne_hello 83} 84 85define i1 @test_simplify8(ptr %str_p) { 86; CHECK-LABEL: @test_simplify8( 87; CHECK-NEXT: [[CHAR0:%.*]] = load i8, ptr [[STR_P:%.*]], align 1 88; CHECK-NEXT: [[NE_NULL:%.*]] = icmp ne i8 [[CHAR0]], 0 89; CHECK-NEXT: ret i1 [[NE_NULL]] 90; 91 %str_l = call i32 @strlen(ptr %str_p) 92 %ne_null = icmp ne i32 %str_l, 0 93 ret i1 %ne_null 94} 95 96define i32 @test_simplify9(i1 %x) { 97; CHECK-LABEL: @test_simplify9( 98; CHECK-NEXT: [[L:%.*]] = select i1 [[X:%.*]], i32 5, i32 6 99; CHECK-NEXT: ret i32 [[L]] 100; 101 %s = select i1 %x, ptr @hello, ptr @longer 102 %l = call i32 @strlen(ptr %s) 103 ret i32 %l 104} 105 106; Check the case that should be simplified to a sub instruction. 107; strlen(@hello + x) --> 5 - x 108 109define i32 @test_simplify10_inbounds(i32 %x) { 110; CHECK-LABEL: @test_simplify10_inbounds( 111; CHECK-NEXT: [[HELLO_L:%.*]] = sub i32 5, [[X:%.*]] 112; CHECK-NEXT: ret i32 [[HELLO_L]] 113; 114 %hello_p = getelementptr inbounds [6 x i8], ptr @hello, i32 0, i32 %x 115 %hello_l = call i32 @strlen(ptr %hello_p) 116 ret i32 %hello_l 117} 118 119define i32 @test_simplify10_no_inbounds(i32 %x) { 120; CHECK-LABEL: @test_simplify10_no_inbounds( 121; CHECK-NEXT: [[HELLO_L:%.*]] = sub i32 5, [[X:%.*]] 122; CHECK-NEXT: ret i32 [[HELLO_L]] 123; 124 %hello_p = getelementptr [6 x i8], ptr @hello, i32 0, i32 %x 125 %hello_l = call i32 @strlen(ptr %hello_p) 126 ret i32 %hello_l 127} 128 129; strlen(@null_hello_mid + (x & 7)) --> 9 - (x & 7) 130 131define i32 @test_simplify11(i32 %x) { 132; CHECK-LABEL: @test_simplify11( 133; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 7 134; CHECK-NEXT: [[HELLO_L:%.*]] = sub nuw nsw i32 9, [[AND]] 135; CHECK-NEXT: ret i32 [[HELLO_L]] 136; 137 %and = and i32 %x, 7 138 %hello_p = getelementptr inbounds [13 x i8], ptr @null_hello_mid, i32 0, i32 %and 139 %hello_l = call i32 @strlen(ptr %hello_p) 140 ret i32 %hello_l 141} 142 143; Check cases that shouldn't be simplified. 144 145define i32 @test_no_simplify1() { 146; CHECK-LABEL: @test_no_simplify1( 147; CHECK-NEXT: [[A_L:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) @a) 148; CHECK-NEXT: ret i32 [[A_L]] 149; 150 %a_l = call i32 @strlen(ptr @a) 151 ret i32 %a_l 152} 153 154; strlen(@null_hello + x) should not be simplified to a sub instruction. 155 156define i32 @test_no_simplify2(i32 %x) { 157; CHECK-LABEL: @test_no_simplify2( 158; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], ptr @null_hello, i32 0, i32 [[X:%.*]] 159; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[HELLO_P]]) 160; CHECK-NEXT: ret i32 [[HELLO_L]] 161; 162 %hello_p = getelementptr inbounds [7 x i8], ptr @null_hello, i32 0, i32 %x 163 %hello_l = call i32 @strlen(ptr %hello_p) 164 ret i32 %hello_l 165} 166 167define i32 @test_no_simplify2_no_null_opt(i32 %x) #0 { 168; CHECK-LABEL: @test_no_simplify2_no_null_opt( 169; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], ptr @null_hello, i32 0, i32 [[X:%.*]] 170; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(ptr noundef [[HELLO_P]]) 171; CHECK-NEXT: ret i32 [[HELLO_L]] 172; 173 %hello_p = getelementptr inbounds [7 x i8], ptr @null_hello, i32 0, i32 %x 174 %hello_l = call i32 @strlen(ptr %hello_p) 175 ret i32 %hello_l 176} 177 178; strlen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction. 179 180define i32 @test_no_simplify3(i32 %x) { 181; CHECK-LABEL: @test_no_simplify3( 182; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15 183; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds nuw [13 x i8], ptr @null_hello_mid, i32 0, i32 [[AND]] 184; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[HELLO_P]]) 185; CHECK-NEXT: ret i32 [[HELLO_L]] 186; 187 %and = and i32 %x, 15 188 %hello_p = getelementptr inbounds [13 x i8], ptr @null_hello_mid, i32 0, i32 %and 189 %hello_l = call i32 @strlen(ptr %hello_p) 190 ret i32 %hello_l 191} 192 193define i32 @test_no_simplify3_on_null_opt(i32 %x) #0 { 194; CHECK-LABEL: @test_no_simplify3_on_null_opt( 195; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15 196; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds nuw [13 x i8], ptr @null_hello_mid, i32 0, i32 [[AND]] 197; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[HELLO_P]]) 198; CHECK-NEXT: ret i32 [[HELLO_L]] 199; 200 %and = and i32 %x, 15 201 %hello_p = getelementptr inbounds [13 x i8], ptr @null_hello_mid, i32 0, i32 %and 202 %hello_l = call i32 @strlen(ptr %hello_p) 203 ret i32 %hello_l 204} 205 206define i32 @test1(ptr %str) { 207; CHECK-LABEL: @test1( 208; CHECK-NEXT: [[LEN:%.*]] = tail call i32 @strlen(ptr noundef nonnull dereferenceable(1) [[STR:%.*]]) #[[ATTR1:[0-9]+]] 209; CHECK-NEXT: ret i32 [[LEN]] 210; 211 %len = tail call i32 @strlen(ptr %str) nounwind 212 ret i32 %len 213} 214 215define i32 @test2(ptr %str) #0 { 216; CHECK-LABEL: @test2( 217; CHECK-NEXT: [[LEN:%.*]] = tail call i32 @strlen(ptr noundef [[STR:%.*]]) #[[ATTR1]] 218; CHECK-NEXT: ret i32 [[LEN]] 219; 220 %len = tail call i32 @strlen(ptr %str) nounwind 221 ret i32 %len 222} 223 224; Test cases for PR47149. 225define i1 @strlen0_after_write_to_first_byte_global() { 226; CHECK-LABEL: @strlen0_after_write_to_first_byte_global( 227; CHECK-NEXT: store i8 49, ptr @a, align 16 228; CHECK-NEXT: ret i1 false 229; 230 store i8 49, ptr @a, align 16 231 %len = tail call i32 @strlen(ptr nonnull dereferenceable(1) @a) 232 %cmp = icmp eq i32 %len, 0 233 ret i1 %cmp 234} 235 236define i1 @strlen0_after_write_to_second_byte_global() { 237; CHECK-LABEL: @strlen0_after_write_to_second_byte_global( 238; CHECK-NEXT: store i8 49, ptr getelementptr inbounds nuw (i8, ptr @a, i32 1), align 16 239; CHECK-NEXT: [[CHAR0:%.*]] = load i8, ptr @a, align 1 240; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[CHAR0]], 0 241; CHECK-NEXT: ret i1 [[CMP]] 242; 243 store i8 49, ptr getelementptr inbounds ([32 x i8], ptr @a, i64 0, i64 1), align 16 244 %len = tail call i32 @strlen(ptr nonnull dereferenceable(1) @a) 245 %cmp = icmp eq i32 %len, 0 246 ret i1 %cmp 247} 248 249define i1 @strlen0_after_write_to_first_byte(ptr %ptr) { 250; CHECK-LABEL: @strlen0_after_write_to_first_byte( 251; CHECK-NEXT: store i8 49, ptr [[PTR:%.*]], align 1 252; CHECK-NEXT: ret i1 false 253; 254 store i8 49, ptr %ptr 255 %len = tail call i32 @strlen(ptr nonnull dereferenceable(1) %ptr) 256 %cmp = icmp eq i32 %len, 0 257 ret i1 %cmp 258} 259 260define i1 @strlen0_after_write_to_second_byte(ptr %ptr) { 261; CHECK-LABEL: @strlen0_after_write_to_second_byte( 262; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[PTR:%.*]], i32 1 263; CHECK-NEXT: store i8 49, ptr [[GEP]], align 1 264; CHECK-NEXT: [[CHAR0:%.*]] = load i8, ptr [[PTR]], align 1 265; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[CHAR0]], 0 266; CHECK-NEXT: ret i1 [[CMP]] 267; 268 %gep = getelementptr i8, ptr %ptr, i64 1 269 store i8 49, ptr %gep 270 %len = tail call i32 @strlen(ptr nonnull dereferenceable(1) %ptr) 271 %cmp = icmp eq i32 %len, 0 272 ret i1 %cmp 273} 274 275attributes #0 = { null_pointer_is_valid } 276