1; RUN: opt < %s -instcombine -S | FileCheck %s 2 3target datalayout = "e-p:64:64" 4%intstruct = type { i32 } 5%pair = type { i32, i32 } 6%struct.B = type { double } 7%struct.A = type { %struct.B, i32, i32 } 8 9 10@Global = constant [10 x i8] c"helloworld" 11 12; Test noop elimination 13define i32* @test1(i32* %I) { 14 %A = getelementptr i32* %I, i64 0 15 ret i32* %A 16; CHECK: @test1 17; CHECK: ret i32* %I 18} 19 20; Test noop elimination 21define i32* @test2(i32* %I) { 22 %A = getelementptr i32* %I 23 ret i32* %A 24; CHECK: @test2 25; CHECK: ret i32* %I 26} 27 28; Test that two array indexing geps fold 29define i32* @test3(i32* %I) { 30 %A = getelementptr i32* %I, i64 17 31 %B = getelementptr i32* %A, i64 4 32 ret i32* %B 33; CHECK: @test3 34; CHECK: getelementptr i32* %I, i64 21 35} 36 37; Test that two getelementptr insts fold 38define i32* @test4({ i32 }* %I) { 39 %A = getelementptr { i32 }* %I, i64 1 40 %B = getelementptr { i32 }* %A, i64 0, i32 0 41 ret i32* %B 42; CHECK: @test4 43; CHECK: getelementptr { i32 }* %I, i64 1, i32 0 44} 45 46define void @test5(i8 %B) { 47 ; This should be turned into a constexpr instead of being an instruction 48 %A = getelementptr [10 x i8]* @Global, i64 0, i64 4 49 store i8 %B, i8* %A 50 ret void 51; CHECK: @test5 52; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8]* @Global, i64 0, i64 4) 53} 54 55 56define i32* @test7(i32* %I, i64 %C, i64 %D) { 57 %A = getelementptr i32* %I, i64 %C 58 %B = getelementptr i32* %A, i64 %D 59 ret i32* %B 60; CHECK: @test7 61; CHECK: %A.sum = add i64 %C, %D 62; CHECK: getelementptr i32* %I, i64 %A.sum 63} 64 65define i8* @test8([10 x i32]* %X) { 66 ;; Fold into the cast. 67 %A = getelementptr [10 x i32]* %X, i64 0, i64 0 68 %B = bitcast i32* %A to i8* 69 ret i8* %B 70; CHECK: @test8 71; CHECK: bitcast [10 x i32]* %X to i8* 72} 73 74define i32 @test9() { 75 %A = getelementptr { i32, double }* null, i32 0, i32 1 76 %B = ptrtoint double* %A to i32 77 ret i32 %B 78; CHECK: @test9 79; CHECK: ret i32 8 80} 81 82define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) { 83 %tmp.1 = getelementptr { i32, i32 }* %x, i32 0, i32 1 84 %tmp.3 = getelementptr { i32, i32 }* %y, i32 0, i32 1 85 ;; seteq x, y 86 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3 87 ret i1 %tmp.4 88; CHECK: @test10 89; CHECK: icmp eq { i32, i32 }* %x, %y 90} 91 92define i1 @test11({ i32, i32 }* %X) { 93 %P = getelementptr { i32, i32 }* %X, i32 0, i32 0 94 %Q = icmp eq i32* %P, null 95 ret i1 %Q 96; CHECK: @test11 97; CHECK: icmp eq { i32, i32 }* %X, null 98} 99 100 101; PR4748 102define i32 @test12(%struct.A* %a) { 103entry: 104 %g3 = getelementptr %struct.A* %a, i32 0, i32 1 105 store i32 10, i32* %g3, align 4 106 107 %g4 = getelementptr %struct.A* %a, i32 0, i32 0 108 109 %new_a = bitcast %struct.B* %g4 to %struct.A* 110 111 %g5 = getelementptr %struct.A* %new_a, i32 0, i32 1 112 %a_a = load i32* %g5, align 4 113 ret i32 %a_a 114; CHECK: @test12 115; CHECK: getelementptr %struct.A* %a, i64 0, i32 1 116; CHECK-NEXT: store i32 10, i32* %g3 117; CHECK-NEXT: ret i32 10 118} 119 120 121; PR2235 122%S = type { i32, [ 100 x i32] } 123define i1 @test13(i64 %X, %S* %P) { 124 %A = getelementptr inbounds %S* %P, i32 0, i32 1, i64 %X 125 %B = getelementptr inbounds %S* %P, i32 0, i32 0 126 %C = icmp eq i32* %A, %B 127 ret i1 %C 128; CHECK: @test13 129; CHECK: %C = icmp eq i64 %X, -1 130} 131 132 133@G = external global [3 x i8] 134define i8* @test14(i32 %Idx) { 135 %idx = zext i32 %Idx to i64 136 %tmp = getelementptr i8* getelementptr ([3 x i8]* @G, i32 0, i32 0), i64 %idx 137 ret i8* %tmp 138; CHECK: @test14 139; CHECK: getelementptr [3 x i8]* @G, i64 0, i64 %idx 140} 141 142 143; Test folding of constantexpr geps into normal geps. 144@Array = external global [40 x i32] 145define i32 *@test15(i64 %X) { 146 %A = getelementptr i32* getelementptr ([40 x i32]* @Array, i64 0, i64 0), i64 %X 147 ret i32* %A 148; CHECK: @test15 149; CHECK: getelementptr [40 x i32]* @Array, i64 0, i64 %X 150} 151 152 153define i32* @test16(i32* %X, i32 %Idx) { 154 %R = getelementptr i32* %X, i32 %Idx 155 ret i32* %R 156; CHECK: @test16 157; CHECK: sext i32 %Idx to i64 158} 159 160 161define i1 @test17(i16* %P, i32 %I, i32 %J) { 162 %X = getelementptr inbounds i16* %P, i32 %I 163 %Y = getelementptr inbounds i16* %P, i32 %J 164 %C = icmp ult i16* %X, %Y 165 ret i1 %C 166; CHECK: @test17 167; CHECK: %C = icmp slt i32 %I, %J 168} 169 170define i1 @test18(i16* %P, i32 %I) { 171 %X = getelementptr inbounds i16* %P, i32 %I 172 %C = icmp ult i16* %X, %P 173 ret i1 %C 174; CHECK: @test18 175; CHECK: %C = icmp slt i32 %I, 0 176} 177 178define i32 @test19(i32* %P, i32 %A, i32 %B) { 179 %tmp.4 = getelementptr inbounds i32* %P, i32 %A 180 %tmp.9 = getelementptr inbounds i32* %P, i32 %B 181 %tmp.10 = icmp eq i32* %tmp.4, %tmp.9 182 %tmp.11 = zext i1 %tmp.10 to i32 183 ret i32 %tmp.11 184; CHECK: @test19 185; CHECK: icmp eq i32 %A, %B 186} 187 188define i32 @test20(i32* %P, i32 %A, i32 %B) { 189 %tmp.4 = getelementptr inbounds i32* %P, i32 %A 190 %tmp.6 = icmp eq i32* %tmp.4, %P 191 %tmp.7 = zext i1 %tmp.6 to i32 192 ret i32 %tmp.7 193; CHECK: @test20 194; CHECK: icmp eq i32 %A, 0 195} 196 197 198define i32 @test21() { 199 %pbob1 = alloca %intstruct 200 %pbob2 = getelementptr %intstruct* %pbob1 201 %pbobel = getelementptr %intstruct* %pbob2, i64 0, i32 0 202 %rval = load i32* %pbobel 203 ret i32 %rval 204; CHECK: @test21 205; CHECK: getelementptr %intstruct* %pbob1, i64 0, i32 0 206} 207 208 209@A = global i32 1 ; <i32*> [#uses=1] 210@B = global i32 2 ; <i32*> [#uses=1] 211 212define i1 @test22() { 213 %C = icmp ult i32* getelementptr (i32* @A, i64 1), 214 getelementptr (i32* @B, i64 2) 215 ret i1 %C 216; CHECK: @test22 217; CHECK: icmp ult (i32* getelementptr inbounds (i32* @A, i64 1), i32* getelementptr (i32* @B, i64 2)) 218} 219 220 221%X = type { [10 x i32], float } 222 223define i1 @test23() { 224 %A = getelementptr %X* null, i64 0, i32 0, i64 0 ; <i32*> [#uses=1] 225 %B = icmp ne i32* %A, null ; <i1> [#uses=1] 226 ret i1 %B 227; CHECK: @test23 228; CHECK: ret i1 false 229} 230 231define void @test25() { 232entry: 233 %tmp = getelementptr { i64, i64, i64, i64 }* null, i32 0, i32 3 ; <i64*> [#uses=1] 234 %tmp.upgrd.1 = load i64* %tmp ; <i64> [#uses=1] 235 %tmp8.ui = load i64* null ; <i64> [#uses=1] 236 %tmp8 = bitcast i64 %tmp8.ui to i64 ; <i64> [#uses=1] 237 %tmp9 = and i64 %tmp8, %tmp.upgrd.1 ; <i64> [#uses=1] 238 %sext = trunc i64 %tmp9 to i32 ; <i32> [#uses=1] 239 %tmp27.i = sext i32 %sext to i64 ; <i64> [#uses=1] 240 tail call void @foo25( i32 0, i64 %tmp27.i ) 241 unreachable 242; CHECK: @test25 243} 244 245declare void @foo25(i32, i64) 246 247 248; PR1637 249define i1 @test26(i8* %arr) { 250 %X = getelementptr i8* %arr, i32 1 251 %Y = getelementptr i8* %arr, i32 1 252 %test = icmp uge i8* %X, %Y 253 ret i1 %test 254; CHECK: @test26 255; CHECK: ret i1 true 256} 257 258 %struct.__large_struct = type { [100 x i64] } 259 %struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } } 260 %struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } } 261 %struct.sigval_t = type { i8* } 262 263define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) { 264entry: 265 %from_addr = alloca %struct.siginfo_t* 266 %tmp344 = load %struct.siginfo_t** %from_addr, align 8 267 %tmp345 = getelementptr %struct.siginfo_t* %tmp344, i32 0, i32 3 268 %tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0 269 %tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }* 270 %tmp348 = getelementptr { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2 271 %tmp349 = getelementptr %struct.sigval_t* %tmp348, i32 0, i32 0 272 %tmp349350 = bitcast i8** %tmp349 to i32* 273 %tmp351 = load i32* %tmp349350, align 8 274 %tmp360 = call i32 asm sideeffect "...", 275 "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351, 276 %struct.__large_struct* null, i32 -14, i32 0 ) 277 unreachable 278; CHECK: @test27 279} 280 281; PR1978 282 %struct.x = type <{ i8 }> 283@.str = internal constant [6 x i8] c"Main!\00" 284@.str1 = internal constant [12 x i8] c"destroy %p\0A\00" 285 286define i32 @test28() nounwind { 287entry: 288 %orientations = alloca [1 x [1 x %struct.x]] 289 %tmp3 = call i32 @puts( i8* getelementptr ([6 x i8]* @.str, i32 0, i32 0) ) nounwind 290 %tmp45 = getelementptr inbounds [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0 291 %orientations62 = getelementptr [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0 292 br label %bb10 293 294bb10: 295 %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ] 296 %tmp.0.reg2mem.0.rec = mul i32 %indvar, -1 297 %tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1 298 %tmp12 = getelementptr inbounds %struct.x* %tmp45, i32 %tmp12.rec 299 %tmp16 = call i32 (i8*, ...)* @printf( i8* getelementptr ([12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind 300 %tmp84 = icmp eq %struct.x* %tmp12, %orientations62 301 %indvar.next = add i32 %indvar, 1 302 br i1 %tmp84, label %bb17, label %bb10 303 304bb17: 305 ret i32 0 306; CHECK: @test28 307; CHECK: icmp eq i32 %indvar, 0 308} 309 310declare i32 @puts(i8*) 311 312declare i32 @printf(i8*, ...) 313 314 315 316 317; rdar://6762290 318 %T = type <{ i64, i64, i64 }> 319define i32 @test29(i8* %start, i32 %X) nounwind { 320entry: 321 %tmp3 = load i64* null 322 %add.ptr = getelementptr i8* %start, i64 %tmp3 323 %tmp158 = load i32* null 324 %add.ptr159 = getelementptr %T* null, i32 %tmp158 325 %add.ptr209 = getelementptr i8* %start, i64 0 326 %add.ptr212 = getelementptr i8* %add.ptr209, i32 %X 327 %cmp214 = icmp ugt i8* %add.ptr212, %add.ptr 328 br i1 %cmp214, label %if.then216, label %if.end363 329 330if.then216: 331 ret i32 1 332 333if.end363: 334 ret i32 0 335; CHECK: @test29 336} 337 338 339; PR3694 340define i32 @test30(i32 %m, i32 %n) nounwind { 341entry: 342 %0 = alloca i32, i32 %n, align 4 343 %1 = bitcast i32* %0 to [0 x i32]* 344 call void @test30f(i32* %0) nounwind 345 %2 = getelementptr [0 x i32]* %1, i32 0, i32 %m 346 %3 = load i32* %2, align 4 347 ret i32 %3 348; CHECK: @test30 349; CHECK: getelementptr i32 350} 351 352declare void @test30f(i32*) 353 354 355 356define i1 @test31(i32* %A) { 357 %B = getelementptr i32* %A, i32 1 358 %C = getelementptr i32* %A, i64 1 359 %V = icmp eq i32* %B, %C 360 ret i1 %V 361; CHECK: @test31 362; CHECK: ret i1 true 363} 364 365 366; PR1345 367define i8* @test32(i8* %v) { 368 %A = alloca [4 x i8*], align 16 369 %B = getelementptr [4 x i8*]* %A, i32 0, i32 0 370 store i8* null, i8** %B 371 %C = bitcast [4 x i8*]* %A to { [16 x i8] }* 372 %D = getelementptr { [16 x i8] }* %C, i32 0, i32 0, i32 8 373 %E = bitcast i8* %D to i8** 374 store i8* %v, i8** %E 375 %F = getelementptr [4 x i8*]* %A, i32 0, i32 2 376 %G = load i8** %F 377 ret i8* %G 378; CHECK: @test32 379; CHECK: %D = getelementptr [4 x i8*]* %A, i64 0, i64 1 380; CHECK: %F = getelementptr [4 x i8*]* %A, i64 0, i64 2 381} 382 383; PR3290 384%struct.Key = type { { i32, i32 } } 385%struct.anon = type <{ i8, [3 x i8], i32 }> 386 387define i32 *@test33(%struct.Key *%A) { 388 %B = bitcast %struct.Key* %A to %struct.anon* 389 %C = getelementptr %struct.anon* %B, i32 0, i32 2 390 ret i32 *%C 391; CHECK: @test33 392; CHECK: getelementptr %struct.Key* %A, i64 0, i32 0, i32 1 393} 394 395 396 397 %T2 = type { i8*, i8 } 398define i8* @test34(i8* %Val, i64 %V) nounwind { 399entry: 400 %A = alloca %T2, align 8 401 %mrv_gep = bitcast %T2* %A to i64* 402 %B = getelementptr %T2* %A, i64 0, i32 0 403 404 store i64 %V, i64* %mrv_gep 405 %C = load i8** %B, align 8 406 ret i8* %C 407; CHECK: @test34 408; CHECK: %V.c = inttoptr i64 %V to i8* 409; CHECK: ret i8* %V.c 410} 411 412%t0 = type { i8*, [19 x i8] } 413%t1 = type { i8*, [0 x i8] } 414 415@array = external global [11 x i8] 416 417@s = external global %t0 418@"\01LC8" = external constant [17 x i8] 419 420; Instcombine should be able to fold this getelementptr. 421 422define i32 @test35() nounwind { 423 call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0), 424 i8* getelementptr (%t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind 425 ret i32 0 426; CHECK: @test35 427; CHECK: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([17 x i8]* @"\01LC8", i64 0, i64 0), i8* getelementptr inbounds (%t0* @s, i64 0, i32 1, i64 0)) nounwind 428} 429 430; Instcombine should constant-fold the GEP so that indices that have 431; static array extents are within bounds of those array extents. 432; In the below, -1 is not in the range [0,11). After the transformation, 433; the same address is computed, but 3 is in the range of [0,11). 434 435define i8* @test36() nounwind { 436 ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1) 437; CHECK: @test36 438; CHECK: ret i8* getelementptr ([11 x i8]* @array, i64 1676976733973595601, i64 4) 439} 440 441; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0). 442@A37 = external constant [1 x i8] 443define i1 @test37() nounwind { 444; CHECK: @test37 445; CHECK: ret i1 true 446 %t = icmp eq i8* getelementptr ([1 x i8]* @A37, i64 0, i64 1), 447 getelementptr ([1 x i8]* @A37, i64 1, i64 0) 448 ret i1 %t 449} 450 451; Test index promotion 452define i32* @test38(i32* %I, i32 %n) { 453 %A = getelementptr i32* %I, i32 %n 454 ret i32* %A 455; CHECK: @test38 456; CHECK: = sext i32 %n to i64 457; CHECK: %A = getelementptr i32* %I, i64 % 458} 459 460; Test that we don't duplicate work when the second gep is a "bitcast". 461%pr10322_t = type { i8* } 462declare void @pr10322_f2(%pr10322_t*) 463declare void @pr10322_f3(i8**) 464define void @pr10322_f1(%pr10322_t* %foo) { 465entry: 466 %arrayidx8 = getelementptr inbounds %pr10322_t* %foo, i64 2 467 call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind 468 %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0 469 call void @pr10322_f3(i8** %tmp2) nounwind 470 ret void 471 472; CHECK: @pr10322_f1 473; CHECK: %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0 474} 475 476; Test that we combine the last two geps in this sequence, before we 477; would wait for gep1 and gep2 to be combined and never combine 2 and 3. 478%three_gep_t = type {i32} 479%three_gep_t2 = type {%three_gep_t} 480 481define void @three_gep_f(%three_gep_t2* %x) { 482 %gep1 = getelementptr %three_gep_t2* %x, i64 2 483 call void @three_gep_h(%three_gep_t2* %gep1) 484 %gep2 = getelementptr %three_gep_t2* %gep1, i64 0, i32 0 485 %gep3 = getelementptr %three_gep_t* %gep2, i64 0, i32 0 486 call void @three_gep_g(i32* %gep3) 487 488; CHECK: @three_gep_f 489; CHECK: %gep3 = getelementptr %three_gep_t2* %gep1, i64 0, i32 0, i32 0 490 ret void 491} 492 493declare void @three_gep_g(i32*) 494declare void @three_gep_h(%three_gep_t2*) 495