1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; Test a pile of objectsize bounds checking. 3; RUN: opt < %s -instcombine -S | FileCheck %s 4; We need target data to get the sizes of the arrays and structures. 5target 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" 6 7@a = private global [60 x i8] zeroinitializer, align 1 ; <[60 x i8]*> 8@.str = private constant [8 x i8] c"abcdefg\00" ; <[8 x i8]*> 9define i32 @foo() nounwind { 10; CHECK-LABEL: @foo( 11; CHECK-NEXT: ret i32 60 12; 13 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i1 false, i1 false, i1 false) 14 ret i32 %1 15} 16 17define i8* @bar() nounwind { 18; CHECK-LABEL: @bar( 19; CHECK-NEXT: entry: 20; CHECK-NEXT: [[RETVAL:%.*]] = alloca i8*, align 4 21; CHECK-NEXT: br i1 true, label [[COND_TRUE:%.*]], label [[COND_FALSE:%.*]] 22; CHECK: cond.true: 23; CHECK-NEXT: [[TMP0:%.*]] = load i8*, i8** [[RETVAL]], align 4 24; CHECK-NEXT: ret i8* [[TMP0]] 25; CHECK: cond.false: 26; CHECK-NEXT: ret i8* undef 27; 28entry: 29 %retval = alloca i8* 30 %0 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i1 false, i1 false, i1 false) 31 %cmp = icmp ne i32 %0, -1 32 br i1 %cmp, label %cond.true, label %cond.false 33 34cond.true: 35 %1 = load i8*, i8** %retval 36 ret i8* %1 37 38cond.false: 39 %2 = load i8*, i8** %retval 40 ret i8* %2 41} 42 43define i32 @f() nounwind { 44; CHECK-LABEL: @f( 45; CHECK-NEXT: ret i32 0 46; 47 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr ([60 x i8], [60 x i8]* @a, i32 1, i32 0), i1 false, i1 false, i1 false) 48 ret i32 %1 49} 50 51@window = external global [0 x i8] 52 53define i1 @baz() nounwind { 54; CHECK-LABEL: @baz( 55; CHECK-NEXT: [[TMP1:%.*]] = tail call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 0), i1 false, i1 false, i1 false) 56; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], -1 57; CHECK-NEXT: ret i1 [[TMP2]] 58; 59 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 0), i1 false, i1 false, i1 false) 60 %2 = icmp eq i32 %1, -1 61 ret i1 %2 62} 63 64define void @test1(i8* %q, i32 %x) nounwind noinline { 65; CHECK-LABEL: @test1( 66; CHECK-NEXT: entry: 67; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 10), i1 false, i1 false, i1 false) 68; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[TMP0]], -1 69; CHECK-NEXT: br i1 [[TMP1]], label %"47", label %"46" 70; CHECK: "46": 71; CHECK-NEXT: unreachable 72; CHECK: "47": 73; CHECK-NEXT: unreachable 74; 75entry: 76 %0 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 10), i1 false, i1 false, i1 false) ; <i64> [#uses=1] 77 %1 = icmp eq i32 %0, -1 ; <i1> [#uses=1] 78 br i1 %1, label %"47", label %"46" 79 80"46": ; preds = %entry 81 unreachable 82 83"47": ; preds = %entry 84 unreachable 85} 86 87@.str5 = private constant [9 x i32] [i32 97, i32 98, i32 99, i32 100, i32 0, i32 88 101, i32 102, i32 103, i32 0], align 4 89define i32 @test2() nounwind { 90; CHECK-LABEL: @test2( 91; CHECK-NEXT: ret i32 34 92; 93 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr (i8, i8* bitcast ([9 x i32]* @.str5 to i8*), i32 2), i1 false, i1 false, i1 false) 94 ret i32 %1 95} 96 97; rdar://7674946 98@array = internal global [480 x float] zeroinitializer ; <[480 x float]*> [#uses=1] 99 100declare i8* @__memcpy_chk(i8*, i8*, i32, i32) nounwind 101 102declare i32 @llvm.objectsize.i32.p0i8(i8*, i1, i1, i1) nounwind readonly 103 104declare i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)*, i1, i1, i1) nounwind readonly 105 106declare i8* @__inline_memcpy_chk(i8*, i8*, i32) nounwind inlinehint 107 108define void @test3() nounwind { 109; CHECK-LABEL: @test3( 110; CHECK-NEXT: entry: 111; CHECK-NEXT: br i1 undef, label [[BB11:%.*]], label [[BB12:%.*]] 112; CHECK: bb11: 113; CHECK-NEXT: unreachable 114; CHECK: bb12: 115; CHECK-NEXT: [[TMP0:%.*]] = call i8* @__inline_memcpy_chk(i8* bitcast (float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 1) to i8*), i8* undef, i32 512) [[ATTR3:#.*]] 116; CHECK-NEXT: unreachable 117; 118entry: 119 br i1 undef, label %bb11, label %bb12 120 121bb11: 122 %0 = getelementptr inbounds float, float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 128), i32 -127 ; <float*> [#uses=1] 123 %1 = bitcast float* %0 to i8* ; <i8*> [#uses=1] 124 %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false, i1 false, i1 false) ; <i32> [#uses=1] 125 %3 = call i8* @__memcpy_chk(i8* undef, i8* undef, i32 512, i32 %2) nounwind ; <i8*> [#uses=0] 126 unreachable 127 128bb12: 129 %4 = getelementptr inbounds float, float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 128), i32 -127 ; <float*> [#uses=1] 130 %5 = bitcast float* %4 to i8* ; <i8*> [#uses=1] 131 %6 = call i8* @__inline_memcpy_chk(i8* %5, i8* undef, i32 512) nounwind inlinehint ; <i8*> [#uses=0] 132 unreachable 133} 134 135; rdar://7718857 136 137%struct.data = type { [100 x i32], [100 x i32], [1024 x i8] } 138 139define i32 @test4(i8** %esc) nounwind ssp { 140; CHECK-LABEL: @test4( 141; CHECK-NEXT: entry: 142; CHECK-NEXT: [[TMP0:%.*]] = alloca [[STRUCT_DATA:%.*]], align 8 143; CHECK-NEXT: [[TMP1:%.*]] = bitcast %struct.data* [[TMP0]] to i8* 144; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* nonnull align 8 dereferenceable(1824) [[TMP1]], i8 0, i32 1824, i1 false) [[ATTR0:#.*]] 145; CHECK-NEXT: [[TMP2:%.*]] = bitcast i8** [[ESC:%.*]] to %struct.data** 146; CHECK-NEXT: store %struct.data* [[TMP0]], %struct.data** [[TMP2]], align 4 147; CHECK-NEXT: ret i32 0 148; 149entry: 150 %0 = alloca %struct.data, align 8 151 %1 = bitcast %struct.data* %0 to i8* 152 %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false, i1 false, i1 false) nounwind 153 %3 = call i8* @__memset_chk(i8* %1, i32 0, i32 1824, i32 %2) nounwind 154 store i8* %1, i8** %esc 155 ret i32 0 156} 157 158; rdar://7782496 159@s = external global i8* 160 161define i8* @test5(i32 %n) nounwind ssp { 162; CHECK-LABEL: @test5( 163; CHECK-NEXT: entry: 164; CHECK-NEXT: [[TMP0:%.*]] = tail call noalias dereferenceable_or_null(20) i8* @malloc(i32 20) [[ATTR0]] 165; CHECK-NEXT: [[TMP1:%.*]] = load i8*, i8** @s, align 8 166; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(10) [[TMP0]], i8* nonnull align 1 dereferenceable(10) [[TMP1]], i32 10, i1 false) [[ATTR0]] 167; CHECK-NEXT: ret i8* [[TMP0]] 168; 169entry: 170 %0 = tail call noalias i8* @malloc(i32 20) nounwind 171 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false, i1 false, i1 false) 172 %2 = load i8*, i8** @s, align 8 173 %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 10, i32 %1) nounwind 174 ret i8* %0 175} 176 177define void @test6(i32 %n) nounwind ssp { 178; CHECK-LABEL: @test6( 179; CHECK-NEXT: entry: 180; CHECK-NEXT: [[TMP0:%.*]] = tail call noalias dereferenceable_or_null(20) i8* @malloc(i32 20) [[ATTR0]] 181; CHECK-NEXT: [[TMP1:%.*]] = load i8*, i8** @s, align 8 182; CHECK-NEXT: [[TMP2:%.*]] = tail call i8* @__memcpy_chk(i8* [[TMP0]], i8* [[TMP1]], i32 30, i32 20) [[ATTR0]] 183; CHECK-NEXT: ret void 184; 185entry: 186 %0 = tail call noalias i8* @malloc(i32 20) nounwind 187 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false, i1 false, i1 false) 188 %2 = load i8*, i8** @s, align 8 189 %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 30, i32 %1) nounwind 190 ret void 191} 192 193declare i8* @__memset_chk(i8*, i32, i32, i32) nounwind 194 195declare noalias i8* @malloc(i32) nounwind 196 197define i32 @test7(i8** %esc) { 198; CHECK-LABEL: @test7( 199; CHECK-NEXT: [[ALLOC:%.*]] = call noalias dereferenceable_or_null(48) i8* @malloc(i32 48) [[ATTR0]] 200; CHECK-NEXT: store i8* [[ALLOC]], i8** [[ESC:%.*]], align 4 201; CHECK-NEXT: ret i32 32 202; 203 %alloc = call noalias i8* @malloc(i32 48) nounwind 204 store i8* %alloc, i8** %esc 205 %gep = getelementptr inbounds i8, i8* %alloc, i32 16 206 %objsize = call i32 @llvm.objectsize.i32.p0i8(i8* %gep, i1 false, i1 false, i1 false) nounwind readonly 207 ret i32 %objsize 208} 209 210declare noalias i8* @calloc(i32, i32) nounwind 211 212define i32 @test8(i8** %esc) { 213; CHECK-LABEL: @test8( 214; CHECK-NEXT: [[ALLOC:%.*]] = call noalias dereferenceable_or_null(35) i8* @calloc(i32 5, i32 7) [[ATTR0]] 215; CHECK-NEXT: store i8* [[ALLOC]], i8** [[ESC:%.*]], align 4 216; CHECK-NEXT: ret i32 30 217; 218 %alloc = call noalias i8* @calloc(i32 5, i32 7) nounwind 219 store i8* %alloc, i8** %esc 220 %gep = getelementptr inbounds i8, i8* %alloc, i32 5 221 %objsize = call i32 @llvm.objectsize.i32.p0i8(i8* %gep, i1 false, i1 false, i1 false) nounwind readonly 222 ret i32 %objsize 223} 224 225declare noalias i8* @strdup(i8* nocapture) nounwind 226declare noalias i8* @strndup(i8* nocapture, i32) nounwind 227 228define i32 @test9(i8** %esc) { 229; CHECK-LABEL: @test9( 230; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0)) [[ATTR0]] 231; CHECK-NEXT: store i8* [[CALL]], i8** [[ESC:%.*]], align 8 232; CHECK-NEXT: ret i32 8 233; 234 %call = tail call i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0)) nounwind 235 store i8* %call, i8** %esc, align 8 236 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false) 237 ret i32 %1 238} 239 240define i32 @test10(i8** %esc) { 241; CHECK-LABEL: @test10( 242; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(4) i8* @strndup(i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i32 3) [[ATTR0]] 243; CHECK-NEXT: store i8* [[CALL]], i8** [[ESC:%.*]], align 8 244; CHECK-NEXT: ret i32 4 245; 246 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 3) nounwind 247 store i8* %call, i8** %esc, align 8 248 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false) 249 ret i32 %1 250} 251 252define i32 @test11(i8** %esc) { 253; CHECK-LABEL: @test11( 254; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0)) 255; CHECK-NEXT: store i8* [[STRDUP]], i8** [[ESC:%.*]], align 8 256; CHECK-NEXT: ret i32 8 257; 258 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7) nounwind 259 store i8* %call, i8** %esc, align 8 260 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false) 261 ret i32 %1 262} 263 264define i32 @test12(i8** %esc) { 265; CHECK-LABEL: @test12( 266; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0)) 267; CHECK-NEXT: store i8* [[STRDUP]], i8** [[ESC:%.*]], align 8 268; CHECK-NEXT: ret i32 8 269; 270 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 8) nounwind 271 store i8* %call, i8** %esc, align 8 272 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false) 273 ret i32 %1 274} 275 276define i32 @test13(i8** %esc) { 277; CHECK-LABEL: @test13( 278; CHECK-NEXT: [[STRDUP:%.*]] = call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0)) 279; CHECK-NEXT: store i8* [[STRDUP]], i8** [[ESC:%.*]], align 8 280; CHECK-NEXT: ret i32 8 281; 282 %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 57) nounwind 283 store i8* %call, i8** %esc, align 8 284 %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true, i1 false, i1 false) 285 ret i32 %1 286} 287 288@globalalias = internal alias [60 x i8], [60 x i8]* @a 289 290define i32 @test18() { 291; CHECK-LABEL: @test18( 292; CHECK-NEXT: ret i32 60 293; 294 %bc = bitcast [60 x i8]* @globalalias to i8* 295 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %bc, i1 false, i1 false, i1 false) 296 ret i32 %1 297} 298 299@globalalias2 = weak alias [60 x i8], [60 x i8]* @a 300 301define i32 @test19() { 302; CHECK-LABEL: @test19( 303; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @globalalias2, i32 0, i32 0), i1 false, i1 false, i1 false) 304; CHECK-NEXT: ret i32 [[TMP1]] 305; 306 %bc = bitcast [60 x i8]* @globalalias2 to i8* 307 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %bc, i1 false, i1 false, i1 false) 308 ret i32 %1 309} 310 311define i32 @test20() { 312; CHECK-LABEL: @test20( 313; CHECK-NEXT: ret i32 0 314; 315 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 false, i1 false, i1 false) 316 ret i32 %1 317} 318 319define i32 @test21() { 320; CHECK-LABEL: @test21( 321; CHECK-NEXT: ret i32 0 322; 323 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 true, i1 false, i1 false) 324 ret i32 %1 325} 326 327define i32 @test22() { 328; CHECK-LABEL: @test22( 329; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 false, i1 true, i1 false) 330; CHECK-NEXT: ret i32 [[TMP1]] 331; 332 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 false, i1 true, i1 false) 333 ret i32 %1 334} 335 336define i32 @test23() { 337; CHECK-LABEL: @test23( 338; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 true, i1 true, i1 false) 339; CHECK-NEXT: ret i32 [[TMP1]] 340; 341 %1 = call i32 @llvm.objectsize.i32.p0i8(i8* null, i1 true, i1 true, i1 false) 342 ret i32 %1 343} 344 345; 1 is an arbitrary non-zero address space. 346define i32 @test24() { 347; CHECK-LABEL: @test24( 348; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false, i1 false, i1 false) 349; CHECK-NEXT: ret i32 [[TMP1]] 350; 351 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false, 352 i1 false, i1 false) 353 ret i32 %1 354} 355 356define i32 @test25() { 357; CHECK-LABEL: @test25( 358; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true, i1 false, i1 false) 359; CHECK-NEXT: ret i32 [[TMP1]] 360; 361 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true, 362 i1 false, i1 false) 363 ret i32 %1 364} 365 366define i32 @test26() { 367; CHECK-LABEL: @test26( 368; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false, i1 true, i1 false) 369; CHECK-NEXT: ret i32 [[TMP1]] 370; 371 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false, 372 i1 true, i1 false) 373 ret i32 %1 374} 375 376define i32 @test27() { 377; CHECK-LABEL: @test27( 378; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true, i1 true, i1 false) 379; CHECK-NEXT: ret i32 [[TMP1]] 380; 381 %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true, 382 i1 true, i1 false) 383 ret i32 %1 384} 385