1 ; RUN: opt < %s -instcombine -S | FileCheck %s 2 3 target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64" 4 5 %intstruct = type { i32 } 6 %pair = type { i32, i32 } 7 %struct.B = type { double } 8 %struct.A = type { %struct.B, i32, i32 } 9 %struct.C = type { [7 x i8] } 10 11 12 @Global = constant [10 x i8] c"helloworld" 13 @Global_as1 = addrspace(1) constant [10 x i8] c"helloworld" 14 15 ; Test noop elimination 16 define i32* @test1(i32* %I) { 17 %A = getelementptr i32, i32* %I, i64 0 18 ret i32* %A 19 ; CHECK-LABEL: @test1( 20 ; CHECK: ret i32* %I 21 } 22 23 define i32 addrspace(1)* @test1_as1(i32 addrspace(1)* %I) { 24 %A = getelementptr i32, i32 addrspace(1)* %I, i64 0 25 ret i32 addrspace(1)* %A 26 ; CHECK-LABEL: @test1_as1( 27 ; CHECK: ret i32 addrspace(1)* %I 28 } 29 30 ; Test noop elimination 31 define i32* @test2(i32* %I) { 32 %A = getelementptr i32, i32* %I 33 ret i32* %A 34 ; CHECK-LABEL: @test2( 35 ; CHECK: ret i32* %I 36 } 37 38 ; Test that two array indexing geps fold 39 define i32* @test3(i32* %I) { 40 %A = getelementptr i32, i32* %I, i64 17 41 %B = getelementptr i32, i32* %A, i64 4 42 ret i32* %B 43 ; CHECK-LABEL: @test3( 44 ; CHECK: getelementptr i32, i32* %I, i64 21 45 } 46 47 ; Test that two getelementptr insts fold 48 define i32* @test4({ i32 }* %I) { 49 %A = getelementptr { i32 }, { i32 }* %I, i64 1 50 %B = getelementptr { i32 }, { i32 }* %A, i64 0, i32 0 51 ret i32* %B 52 ; CHECK-LABEL: @test4( 53 ; CHECK: getelementptr { i32 }, { i32 }* %I, i64 1, i32 0 54 } 55 56 define void @test5(i8 %B) { 57 ; This should be turned into a constexpr instead of being an instruction 58 %A = getelementptr [10 x i8], [10 x i8]* @Global, i64 0, i64 4 59 store i8 %B, i8* %A 60 ret void 61 ; CHECK-LABEL: @test5( 62 ; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @Global, i64 0, i64 4) 63 } 64 65 define void @test5_as1(i8 %B) { 66 ; This should be turned into a constexpr instead of being an instruction 67 %A = getelementptr [10 x i8], [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4 68 store i8 %B, i8 addrspace(1)* %A 69 ret void 70 ; CHECK-LABEL: @test5_as1( 71 ; CHECK: store i8 %B, i8 addrspace(1)* getelementptr inbounds ([10 x i8], [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4) 72 } 73 74 %as1_ptr_struct = type { i32 addrspace(1)* } 75 %as2_ptr_struct = type { i32 addrspace(2)* } 76 77 @global_as2 = addrspace(2) global i32 zeroinitializer 78 @global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 } 79 80 ; This should be turned into a constexpr instead of being an instruction 81 define void @test_evaluate_gep_nested_as_ptrs(i32 addrspace(2)* %B) { 82 ; CHECK-LABEL: @test_evaluate_gep_nested_as_ptrs( 83 ; CHECK-NEXT: store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* getelementptr inbounds (%as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0), align 8 84 ; CHECK-NEXT: ret void 85 %A = getelementptr %as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0 86 store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* %A 87 ret void 88 } 89 90 @arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer 91 92 define void @test_evaluate_gep_as_ptrs_array(i8 addrspace(2)* %B) { 93 ; CHECK-LABEL: @test_evaluate_gep_as_ptrs_array( 94 ; CHECK-NEXT: store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* getelementptr inbounds ([4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2), align 4 95 96 ; CHECK-NEXT: ret void 97 %A = getelementptr [4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2 98 store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* %A 99 ret void 100 } 101 102 define i32* @test7(i32* %I, i64 %C, i64 %D) { 103 %A = getelementptr i32, i32* %I, i64 %C 104 %B = getelementptr i32, i32* %A, i64 %D 105 ret i32* %B 106 ; CHECK-LABEL: @test7( 107 ; CHECK: %A = getelementptr i32, i32* %I, i64 %C 108 ; CHECK: %B = getelementptr i32, i32* %A, i64 %D 109 } 110 111 define i8* @test8([10 x i32]* %X) { 112 ;; Fold into the cast. 113 %A = getelementptr [10 x i32], [10 x i32]* %X, i64 0, i64 0 114 %B = bitcast i32* %A to i8* 115 ret i8* %B 116 ; CHECK-LABEL: @test8( 117 ; CHECK: bitcast [10 x i32]* %X to i8* 118 } 119 120 define i32 @test9() { 121 %A = getelementptr { i32, double }, { i32, double }* null, i32 0, i32 1 122 %B = ptrtoint double* %A to i32 123 ret i32 %B 124 ; CHECK-LABEL: @test9( 125 ; CHECK: ret i32 8 126 } 127 128 define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) { 129 %tmp.1 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1 130 %tmp.3 = getelementptr { i32, i32 }, { i32, i32 }* %y, i32 0, i32 1 131 ;; seteq x, y 132 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3 133 ret i1 %tmp.4 134 ; CHECK-LABEL: @test10( 135 ; CHECK: icmp eq { i32, i32 }* %x, %y 136 } 137 138 define i1 @test11({ i32, i32 }* %X) { 139 %P = getelementptr { i32, i32 }, { i32, i32 }* %X, i32 0, i32 0 140 %Q = icmp eq i32* %P, null 141 ret i1 %Q 142 ; CHECK-LABEL: @test11( 143 ; CHECK: icmp eq { i32, i32 }* %X, null 144 } 145 146 147 ; PR4748 148 define i32 @test12(%struct.A* %a) { 149 entry: 150 %g3 = getelementptr %struct.A, %struct.A* %a, i32 0, i32 1 151 store i32 10, i32* %g3, align 4 152 153 %g4 = getelementptr %struct.A, %struct.A* %a, i32 0, i32 0 154 155 %new_a = bitcast %struct.B* %g4 to %struct.A* 156 157 %g5 = getelementptr %struct.A, %struct.A* %new_a, i32 0, i32 1 158 %a_a = load i32, i32* %g5, align 4 159 ret i32 %a_a 160 ; CHECK-LABEL: @test12( 161 ; CHECK: getelementptr %struct.A, %struct.A* %a, i64 0, i32 1 162 ; CHECK-NEXT: store i32 10, i32* %g3 163 ; CHECK-NEXT: ret i32 10 164 } 165 166 167 ; PR2235 168 %S = type { i32, [ 100 x i32] } 169 define i1 @test13(i64 %X, %S* %P) { 170 %A = getelementptr inbounds %S, %S* %P, i32 0, i32 1, i64 %X 171 %B = getelementptr inbounds %S, %S* %P, i32 0, i32 0 172 %C = icmp eq i32* %A, %B 173 ret i1 %C 174 ; CHECK-LABEL: @test13( 175 ; CHECK: %C = icmp eq i64 %X, -1 176 } 177 178 define <2 x i1> @test13_vector(<2 x i64> %X, <2 x %S*> %P) nounwind { 179 ; CHECK-LABEL: @test13_vector( 180 ; CHECK-NEXT: shl nuw <2 x i64> %X, <i64 2, i64 2> 181 ; CHECK-NEXT: add <2 x i64> %A.idx, <i64 4, i64 4> 182 ; CHECK-NEXT: icmp eq <2 x i64> %A.offs, zeroinitializer 183 %A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, <2 x i64> %X 184 %B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 0, i32 0> 185 %C = icmp eq <2 x i32*> %A, %B 186 ret <2 x i1> %C 187 } 188 189 define i1 @test13_as1(i16 %X, %S addrspace(1)* %P) { 190 ; CHECK-LABEL: @test13_as1( 191 ; CHECK-NEXT: %C = icmp eq i16 %X, -1 192 ; CHECK-NEXT: ret i1 %C 193 %A = getelementptr inbounds %S, %S addrspace(1)* %P, i16 0, i32 1, i16 %X 194 %B = getelementptr inbounds %S, %S addrspace(1)* %P, i16 0, i32 0 195 %C = icmp eq i32 addrspace(1)* %A, %B 196 ret i1 %C 197 } 198 199 define <2 x i1> @test13_vector_as1(<2 x i16> %X, <2 x %S addrspace(1)*> %P) { 200 ; CHECK-LABEL: @test13_vector_as1( 201 ; CHECK-NEXT: shl nuw <2 x i16> %X, <i16 2, i16 2> 202 ; CHECK-NEXT: add <2 x i16> %A.idx, <i16 4, i16 4> 203 ; CHECK-NEXT: icmp eq <2 x i16> %A.offs, zeroinitializer 204 ; CHECK-NEXT: ret <2 x i1> 205 %A = getelementptr inbounds %S, <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 1, i32 1>, <2 x i16> %X 206 %B = getelementptr inbounds %S, <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 0, i32 0> 207 %C = icmp eq <2 x i32 addrspace(1)*> %A, %B 208 ret <2 x i1> %C 209 } 210 211 define i1 @test13_i32(i32 %X, %S* %P) { 212 ; CHECK-LABEL: @test13_i32( 213 ; CHECK: %C = icmp eq i32 %X, -1 214 %A = getelementptr inbounds %S, %S* %P, i32 0, i32 1, i32 %X 215 %B = getelementptr inbounds %S, %S* %P, i32 0, i32 0 216 %C = icmp eq i32* %A, %B 217 ret i1 %C 218 } 219 220 define i1 @test13_i16(i16 %X, %S* %P) { 221 ; CHECK-LABEL: @test13_i16( 222 ; CHECK: %C = icmp eq i16 %X, -1 223 %A = getelementptr inbounds %S, %S* %P, i16 0, i32 1, i16 %X 224 %B = getelementptr inbounds %S, %S* %P, i16 0, i32 0 225 %C = icmp eq i32* %A, %B 226 ret i1 %C 227 } 228 229 define i1 @test13_i128(i128 %X, %S* %P) { 230 ; CHECK-LABEL: @test13_i128( 231 ; CHECK: %C = icmp eq i64 %1, -1 232 %A = getelementptr inbounds %S, %S* %P, i128 0, i32 1, i128 %X 233 %B = getelementptr inbounds %S, %S* %P, i128 0, i32 0 234 %C = icmp eq i32* %A, %B 235 ret i1 %C 236 } 237 238 239 @G = external global [3 x i8] 240 define i8* @test14(i32 %Idx) { 241 %idx = zext i32 %Idx to i64 242 %tmp = getelementptr i8, i8* getelementptr ([3 x i8], [3 x i8]* @G, i32 0, i32 0), i64 %idx 243 ret i8* %tmp 244 ; CHECK-LABEL: @test14( 245 ; CHECK: getelementptr [3 x i8], [3 x i8]* @G, i64 0, i64 %idx 246 } 247 248 249 ; Test folding of constantexpr geps into normal geps. 250 @Array = external global [40 x i32] 251 define i32 *@test15(i64 %X) { 252 %A = getelementptr i32, i32* getelementptr ([40 x i32], [40 x i32]* @Array, i64 0, i64 0), i64 %X 253 ret i32* %A 254 ; CHECK-LABEL: @test15( 255 ; CHECK: getelementptr [40 x i32], [40 x i32]* @Array, i64 0, i64 %X 256 } 257 258 259 define i32* @test16(i32* %X, i32 %Idx) { 260 %R = getelementptr i32, i32* %X, i32 %Idx 261 ret i32* %R 262 ; CHECK-LABEL: @test16( 263 ; CHECK: sext i32 %Idx to i64 264 } 265 266 267 define i1 @test17(i16* %P, i32 %I, i32 %J) { 268 %X = getelementptr inbounds i16, i16* %P, i32 %I 269 %Y = getelementptr inbounds i16, i16* %P, i32 %J 270 %C = icmp ult i16* %X, %Y 271 ret i1 %C 272 ; CHECK-LABEL: @test17( 273 ; CHECK: %C = icmp slt i32 %I, %J 274 } 275 276 define i1 @test18(i16* %P, i32 %I) { 277 %X = getelementptr inbounds i16, i16* %P, i32 %I 278 %C = icmp ult i16* %X, %P 279 ret i1 %C 280 ; CHECK-LABEL: @test18( 281 ; CHECK: %C = icmp slt i32 %I, 0 282 } 283 284 ; Larger than the pointer size for a non-zero address space 285 define i1 @test18_as1(i16 addrspace(1)* %P, i32 %I) { 286 ; CHECK-LABEL: @test18_as1( 287 ; CHECK-NEXT: %1 = trunc i32 %I to i16 288 ; CHECK-NEXT: %C = icmp slt i16 %1, 0 289 ; CHECK-NEXT: ret i1 %C 290 %X = getelementptr inbounds i16, i16 addrspace(1)* %P, i32 %I 291 %C = icmp ult i16 addrspace(1)* %X, %P 292 ret i1 %C 293 } 294 295 ; Smaller than the pointer size for a non-zero address space 296 define i1 @test18_as1_i32(i16 addrspace(1)* %P, i32 %I) { 297 ; CHECK-LABEL: @test18_as1_i32( 298 ; CHECK-NEXT: %1 = trunc i32 %I to i16 299 ; CHECK-NEXT: %C = icmp slt i16 %1, 0 300 ; CHECK-NEXT: ret i1 %C 301 %X = getelementptr inbounds i16, i16 addrspace(1)* %P, i32 %I 302 %C = icmp ult i16 addrspace(1)* %X, %P 303 ret i1 %C 304 } 305 306 ; Smaller than pointer size 307 define i1 @test18_i16(i16* %P, i16 %I) { 308 ; CHECK-LABEL: @test18_i16( 309 ; CHECK: %C = icmp slt i16 %I, 0 310 %X = getelementptr inbounds i16, i16* %P, i16 %I 311 %C = icmp ult i16* %X, %P 312 ret i1 %C 313 } 314 315 ; Same as pointer size 316 define i1 @test18_i64(i16* %P, i64 %I) { 317 ; CHECK-LABEL: @test18_i64( 318 ; CHECK: %C = icmp slt i64 %I, 0 319 %X = getelementptr inbounds i16, i16* %P, i64 %I 320 %C = icmp ult i16* %X, %P 321 ret i1 %C 322 } 323 324 ; Larger than the pointer size 325 define i1 @test18_i128(i16* %P, i128 %I) { 326 ; CHECK-LABEL: @test18_i128( 327 ; CHECK: %C = icmp slt i64 %1, 0 328 %X = getelementptr inbounds i16, i16* %P, i128 %I 329 %C = icmp ult i16* %X, %P 330 ret i1 %C 331 } 332 333 define i32 @test19(i32* %P, i32 %A, i32 %B) { 334 %tmp.4 = getelementptr inbounds i32, i32* %P, i32 %A 335 %tmp.9 = getelementptr inbounds i32, i32* %P, i32 %B 336 %tmp.10 = icmp eq i32* %tmp.4, %tmp.9 337 %tmp.11 = zext i1 %tmp.10 to i32 338 ret i32 %tmp.11 339 ; CHECK-LABEL: @test19( 340 ; CHECK: icmp eq i32 %A, %B 341 } 342 343 define i32 @test20(i32* %P, i32 %A, i32 %B) { 344 %tmp.4 = getelementptr inbounds i32, i32* %P, i32 %A 345 %tmp.6 = icmp eq i32* %tmp.4, %P 346 %tmp.7 = zext i1 %tmp.6 to i32 347 ret i32 %tmp.7 348 ; CHECK-LABEL: @test20( 349 ; CHECK: icmp eq i32 %A, 0 350 } 351 352 define i32 @test20_as1(i32 addrspace(1)* %P, i32 %A, i32 %B) { 353 %tmp.4 = getelementptr inbounds i32, i32 addrspace(1)* %P, i32 %A 354 %tmp.6 = icmp eq i32 addrspace(1)* %tmp.4, %P 355 %tmp.7 = zext i1 %tmp.6 to i32 356 ret i32 %tmp.7 357 ; CHECK-LABEL: @test20_as1( 358 ; CHECK: icmp eq i16 %1, 0 359 } 360 361 362 define i32 @test21() { 363 %pbob1 = alloca %intstruct 364 %pbob2 = getelementptr %intstruct, %intstruct* %pbob1 365 %pbobel = getelementptr %intstruct, %intstruct* %pbob2, i64 0, i32 0 366 %rval = load i32, i32* %pbobel 367 ret i32 %rval 368 ; CHECK-LABEL: @test21( 369 ; CHECK: getelementptr %intstruct, %intstruct* %pbob1, i64 0, i32 0 370 } 371 372 373 @A = global i32 1 ; <i32*> [#uses=1] 374 @B = global i32 2 ; <i32*> [#uses=1] 375 376 define i1 @test22() { 377 %C = icmp ult i32* getelementptr (i32, i32* @A, i64 1), 378 getelementptr (i32, i32* @B, i64 2) 379 ret i1 %C 380 ; CHECK-LABEL: @test22( 381 ; CHECK: icmp ult (i32* getelementptr inbounds (i32, i32* @A, i64 1), i32* getelementptr (i32, i32* @B, i64 2)) 382 } 383 384 385 %X = type { [10 x i32], float } 386 387 define i1 @test23() { 388 %A = getelementptr %X, %X* null, i64 0, i32 0, i64 0 ; <i32*> [#uses=1] 389 %B = icmp ne i32* %A, null ; <i1> [#uses=1] 390 ret i1 %B 391 ; CHECK-LABEL: @test23( 392 ; CHECK: ret i1 false 393 } 394 395 define void @test25() { 396 entry: 397 %tmp = getelementptr { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* null, i32 0, i32 3 ; <i64*> [#uses=1] 398 %tmp.upgrd.1 = load i64, i64* %tmp ; <i64> [#uses=1] 399 %tmp8.ui = load i64, i64* null ; <i64> [#uses=1] 400 %tmp8 = bitcast i64 %tmp8.ui to i64 ; <i64> [#uses=1] 401 %tmp9 = and i64 %tmp8, %tmp.upgrd.1 ; <i64> [#uses=1] 402 %sext = trunc i64 %tmp9 to i32 ; <i32> [#uses=1] 403 %tmp27.i = sext i32 %sext to i64 ; <i64> [#uses=1] 404 tail call void @foo25( i32 0, i64 %tmp27.i ) 405 unreachable 406 ; CHECK-LABEL: @test25( 407 } 408 409 declare void @foo25(i32, i64) 410 411 412 ; PR1637 413 define i1 @test26(i8* %arr) { 414 %X = getelementptr i8, i8* %arr, i32 1 415 %Y = getelementptr i8, i8* %arr, i32 1 416 %test = icmp uge i8* %X, %Y 417 ret i1 %test 418 ; CHECK-LABEL: @test26( 419 ; CHECK: ret i1 true 420 } 421 422 %struct.__large_struct = type { [100 x i64] } 423 %struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } } 424 %struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } } 425 %struct.sigval_t = type { i8* } 426 427 define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) { 428 entry: 429 %from_addr = alloca %struct.siginfo_t* 430 %tmp344 = load %struct.siginfo_t*, %struct.siginfo_t** %from_addr, align 8 431 %tmp345 = getelementptr %struct.siginfo_t, %struct.siginfo_t* %tmp344, i32 0, i32 3 432 %tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0 433 %tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }* 434 %tmp348 = getelementptr { i32, i32, %struct.sigval_t }, { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2 435 %tmp349 = getelementptr %struct.sigval_t, %struct.sigval_t* %tmp348, i32 0, i32 0 436 %tmp349350 = bitcast i8** %tmp349 to i32* 437 %tmp351 = load i32, i32* %tmp349350, align 8 438 %tmp360 = call i32 asm sideeffect "...", 439 "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351, 440 %struct.__large_struct* null, i32 -14, i32 0 ) 441 unreachable 442 ; CHECK-LABEL: @test27( 443 } 444 445 ; PR1978 446 %struct.x = type <{ i8 }> 447 @.str = internal constant [6 x i8] c"Main!\00" 448 @.str1 = internal constant [12 x i8] c"destroy %p\0A\00" 449 450 define i32 @test28() nounwind { 451 entry: 452 %orientations = alloca [1 x [1 x %struct.x]] 453 %tmp3 = call i32 @puts( i8* getelementptr ([6 x i8], [6 x i8]* @.str, i32 0, i32 0) ) nounwind 454 %tmp45 = getelementptr inbounds [1 x [1 x %struct.x]], [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0 455 %orientations62 = getelementptr [1 x [1 x %struct.x]], [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0 456 br label %bb10 457 458 bb10: 459 %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ] 460 %tmp.0.reg2mem.0.rec = mul i32 %indvar, -1 461 %tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1 462 %tmp12 = getelementptr inbounds %struct.x, %struct.x* %tmp45, i32 %tmp12.rec 463 %tmp16 = call i32 (i8*, ...) @printf( i8* getelementptr ([12 x i8], [12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind 464 %tmp84 = icmp eq %struct.x* %tmp12, %orientations62 465 %indvar.next = add i32 %indvar, 1 466 br i1 %tmp84, label %bb17, label %bb10 467 468 bb17: 469 ret i32 0 470 ; CHECK-LABEL: @test28( 471 ; CHECK: icmp eq i32 %indvar, 0 472 } 473 474 declare i32 @puts(i8*) 475 476 declare i32 @printf(i8*, ...) 477 478 479 480 481 ; rdar://6762290 482 %T = type <{ i64, i64, i64 }> 483 define i32 @test29(i8* %start, i32 %X) nounwind { 484 entry: 485 %tmp3 = load i64, i64* null 486 %add.ptr = getelementptr i8, i8* %start, i64 %tmp3 487 %tmp158 = load i32, i32* null 488 %add.ptr159 = getelementptr %T, %T* null, i32 %tmp158 489 %add.ptr209 = getelementptr i8, i8* %start, i64 0 490 %add.ptr212 = getelementptr i8, i8* %add.ptr209, i32 %X 491 %cmp214 = icmp ugt i8* %add.ptr212, %add.ptr 492 br i1 %cmp214, label %if.then216, label %if.end363 493 494 if.then216: 495 ret i32 1 496 497 if.end363: 498 ret i32 0 499 ; CHECK-LABEL: @test29( 500 } 501 502 503 ; PR3694 504 define i32 @test30(i32 %m, i32 %n) nounwind { 505 entry: 506 %0 = alloca i32, i32 %n, align 4 507 %1 = bitcast i32* %0 to [0 x i32]* 508 call void @test30f(i32* %0) nounwind 509 %2 = getelementptr [0 x i32], [0 x i32]* %1, i32 0, i32 %m 510 %3 = load i32, i32* %2, align 4 511 ret i32 %3 512 ; CHECK-LABEL: @test30( 513 ; CHECK: getelementptr i32 514 } 515 516 declare void @test30f(i32*) 517 518 519 520 define i1 @test31(i32* %A) { 521 %B = getelementptr i32, i32* %A, i32 1 522 %C = getelementptr i32, i32* %A, i64 1 523 %V = icmp eq i32* %B, %C 524 ret i1 %V 525 ; CHECK-LABEL: @test31( 526 ; CHECK: ret i1 true 527 } 528 529 530 ; PR1345 531 define i8* @test32(i8* %v) { 532 %A = alloca [4 x i8*], align 16 533 %B = getelementptr [4 x i8*], [4 x i8*]* %A, i32 0, i32 0 534 store i8* null, i8** %B 535 %C = bitcast [4 x i8*]* %A to { [16 x i8] }* 536 %D = getelementptr { [16 x i8] }, { [16 x i8] }* %C, i32 0, i32 0, i32 8 537 %E = bitcast i8* %D to i8** 538 store i8* %v, i8** %E 539 %F = getelementptr [4 x i8*], [4 x i8*]* %A, i32 0, i32 2 540 %G = load i8*, i8** %F 541 ret i8* %G 542 ; CHECK-LABEL: @test32( 543 ; CHECK: %D = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 1 544 ; CHECK: %F = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 2 545 } 546 547 ; PR3290 548 %struct.Key = type { { i32, i32 } } 549 %struct.anon = type <{ i8, [3 x i8], i32 }> 550 551 define i32* @test33(%struct.Key* %A) { 552 ; CHECK-LABEL: @test33( 553 ; CHECK: getelementptr %struct.Key, %struct.Key* %A, i64 0, i32 0, i32 1 554 %B = bitcast %struct.Key* %A to %struct.anon* 555 %C = getelementptr %struct.anon, %struct.anon* %B, i32 0, i32 2 556 ret i32* %C 557 } 558 559 define i32 addrspace(1)* @test33_as1(%struct.Key addrspace(1)* %A) { 560 ; CHECK-LABEL: @test33_as1( 561 ; CHECK: getelementptr %struct.Key, %struct.Key addrspace(1)* %A, i16 0, i32 0, i32 1 562 %B = bitcast %struct.Key addrspace(1)* %A to %struct.anon addrspace(1)* 563 %C = getelementptr %struct.anon, %struct.anon addrspace(1)* %B, i32 0, i32 2 564 ret i32 addrspace(1)* %C 565 } 566 567 define i32 addrspace(1)* @test33_array_as1([10 x i32] addrspace(1)* %A) { 568 ; CHECK-LABEL: @test33_array_as1( 569 ; CHECK: getelementptr [10 x i32], [10 x i32] addrspace(1)* %A, i16 0, i16 2 570 %B = bitcast [10 x i32] addrspace(1)* %A to [5 x i32] addrspace(1)* 571 %C = getelementptr [5 x i32], [5 x i32] addrspace(1)* %B, i32 0, i32 2 572 ret i32 addrspace(1)* %C 573 } 574 575 ; Make sure the GEP indices use the right pointer sized integer 576 define i32 addrspace(1)* @test33_array_struct_as1([10 x %struct.Key] addrspace(1)* %A) { 577 ; CHECK-LABEL: @test33_array_struct_as1( 578 ; CHECK: getelementptr [10 x %struct.Key], [10 x %struct.Key] addrspace(1)* %A, i16 0, i16 1, i32 0, i32 0 579 %B = bitcast [10 x %struct.Key] addrspace(1)* %A to [20 x i32] addrspace(1)* 580 %C = getelementptr [20 x i32], [20 x i32] addrspace(1)* %B, i32 0, i32 2 581 ret i32 addrspace(1)* %C 582 } 583 584 define i32 addrspace(1)* @test33_addrspacecast(%struct.Key* %A) { 585 ; CHECK-LABEL: @test33_addrspacecast( 586 ; CHECK: %C = getelementptr %struct.Key, %struct.Key* %A, i64 0, i32 0, i32 1 587 ; CHECK-NEXT: addrspacecast i32* %C to i32 addrspace(1)* 588 ; CHECK-NEXT: ret 589 %B = addrspacecast %struct.Key* %A to %struct.anon addrspace(1)* 590 %C = getelementptr %struct.anon, %struct.anon addrspace(1)* %B, i32 0, i32 2 591 ret i32 addrspace(1)* %C 592 } 593 594 %T2 = type { i8*, i8 } 595 define i8* @test34(i8* %Val, i64 %V) nounwind { 596 entry: 597 %A = alloca %T2, align 8 598 %mrv_gep = bitcast %T2* %A to i64* 599 %B = getelementptr %T2, %T2* %A, i64 0, i32 0 600 601 store i64 %V, i64* %mrv_gep 602 %C = load i8*, i8** %B, align 8 603 ret i8* %C 604 ; CHECK-LABEL: @test34( 605 ; CHECK: %[[C:.*]] = inttoptr i64 %V to i8* 606 ; CHECK: ret i8* %[[C]] 607 } 608 609 %t0 = type { i8*, [19 x i8] } 610 %t1 = type { i8*, [0 x i8] } 611 612 @array = external global [11 x i8] 613 614 @s = external global %t0 615 @"\01LC8" = external constant [17 x i8] 616 617 ; Instcombine should be able to fold this getelementptr. 618 619 define i32 @test35() nounwind { 620 call i32 (i8*, ...) @printf(i8* getelementptr ([17 x i8], [17 x i8]* @"\01LC8", i32 0, i32 0), 621 i8* getelementptr (%t1, %t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind 622 ret i32 0 623 ; CHECK-LABEL: @test35( 624 ; CHECK: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @"\01LC8", i64 0, i64 0), i8* getelementptr inbounds (%t0, %t0* @s, i64 0, i32 1, i64 0)) [[NUW:#[0-9]+]] 625 } 626 627 ; Instcombine should constant-fold the GEP so that indices that have 628 ; static array extents are within bounds of those array extents. 629 ; In the below, -1 is not in the range [0,11). After the transformation, 630 ; the same address is computed, but 3 is in the range of [0,11). 631 632 define i8* @test36() nounwind { 633 ret i8* getelementptr ([11 x i8], [11 x i8]* @array, i32 0, i64 -1) 634 ; CHECK-LABEL: @test36( 635 ; CHECK: ret i8* getelementptr ([11 x i8], [11 x i8]* @array, i64 1676976733973595601, i64 4) 636 } 637 638 ; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0). 639 @A37 = external constant [1 x i8] 640 define i1 @test37() nounwind { 641 ; CHECK-LABEL: @test37( 642 ; CHECK: ret i1 true 643 %t = icmp eq i8* getelementptr ([1 x i8], [1 x i8]* @A37, i64 0, i64 1), 644 getelementptr ([1 x i8], [1 x i8]* @A37, i64 1, i64 0) 645 ret i1 %t 646 } 647 648 ; Test index promotion 649 define i32* @test38(i32* %I, i32 %n) { 650 %A = getelementptr i32, i32* %I, i32 %n 651 ret i32* %A 652 ; CHECK-LABEL: @test38( 653 ; CHECK: = sext i32 %n to i64 654 ; CHECK: %A = getelementptr i32, i32* %I, i64 % 655 } 656 657 ; Test that we don't duplicate work when the second gep is a "bitcast". 658 %pr10322_t = type { i8* } 659 declare void @pr10322_f2(%pr10322_t*) 660 declare void @pr10322_f3(i8**) 661 define void @pr10322_f1(%pr10322_t* %foo) { 662 entry: 663 %arrayidx8 = getelementptr inbounds %pr10322_t, %pr10322_t* %foo, i64 2 664 call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind 665 %tmp2 = getelementptr inbounds %pr10322_t, %pr10322_t* %arrayidx8, i64 0, i32 0 666 call void @pr10322_f3(i8** %tmp2) nounwind 667 ret void 668 669 ; CHECK-LABEL: @pr10322_f1( 670 ; CHECK: %tmp2 = getelementptr inbounds %pr10322_t, %pr10322_t* %arrayidx8, i64 0, i32 0 671 } 672 673 ; Test that we combine the last two geps in this sequence, before we 674 ; would wait for gep1 and gep2 to be combined and never combine 2 and 3. 675 %three_gep_t = type {i32} 676 %three_gep_t2 = type {%three_gep_t} 677 678 define void @three_gep_f(%three_gep_t2* %x) { 679 %gep1 = getelementptr %three_gep_t2, %three_gep_t2* %x, i64 2 680 call void @three_gep_h(%three_gep_t2* %gep1) 681 %gep2 = getelementptr %three_gep_t2, %three_gep_t2* %gep1, i64 0, i32 0 682 %gep3 = getelementptr %three_gep_t, %three_gep_t* %gep2, i64 0, i32 0 683 call void @three_gep_g(i32* %gep3) 684 685 ; CHECK-LABEL: @three_gep_f( 686 ; CHECK: %gep3 = getelementptr %three_gep_t2, %three_gep_t2* %gep1, i64 0, i32 0, i32 0 687 ret void 688 } 689 690 declare void @three_gep_g(i32*) 691 declare void @three_gep_h(%three_gep_t2*) 692 693 %struct.ham = type { i32, %struct.zot*, %struct.zot*, %struct.zot* } 694 %struct.zot = type { i64, i8 } 695 696 define void @test39(%struct.ham* %arg, i8 %arg1) nounwind { 697 %tmp = getelementptr inbounds %struct.ham, %struct.ham* %arg, i64 0, i32 2 698 %tmp2 = load %struct.zot*, %struct.zot** %tmp, align 8 699 %tmp3 = bitcast %struct.zot* %tmp2 to i8* 700 %tmp4 = getelementptr inbounds i8, i8* %tmp3, i64 -8 701 store i8 %arg1, i8* %tmp4, align 8 702 ret void 703 704 ; CHECK-LABEL: @test39( 705 ; CHECK: getelementptr inbounds %struct.ham, %struct.ham* %arg, i64 0, i32 2 706 ; CHECK: getelementptr inbounds i8, i8* %{{.+}}, i64 -8 707 } 708 709 define i1 @pr16483([1 x i8]* %a, [1 x i8]* %b) { 710 %c = getelementptr [1 x i8], [1 x i8]* %a, i32 0, i32 0 711 %d = getelementptr [1 x i8], [1 x i8]* %b, i32 0, i32 0 712 %cmp = icmp ult i8* %c, %d 713 ret i1 %cmp 714 715 ; CHECK-LABEL: @pr16483( 716 ; CHECK-NEXT: icmp ult [1 x i8]* %a, %b 717 } 718 719 define i8 @test_gep_bitcast_as1(i32 addrspace(1)* %arr, i16 %N) { 720 ; CHECK-LABEL: @test_gep_bitcast_as1( 721 ; CHECK: getelementptr i32, i32 addrspace(1)* %arr, i16 %N 722 ; CHECK: bitcast 723 %cast = bitcast i32 addrspace(1)* %arr to i8 addrspace(1)* 724 %V = mul i16 %N, 4 725 %t = getelementptr i8, i8 addrspace(1)* %cast, i16 %V 726 %x = load i8, i8 addrspace(1)* %t 727 ret i8 %x 728 } 729 730 ; The element size of the array matches the element size of the pointer 731 define i64 @test_gep_bitcast_array_same_size_element([100 x double]* %arr, i64 %N) { 732 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element( 733 ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %V 734 ; CHECK: bitcast 735 %cast = bitcast [100 x double]* %arr to i64* 736 %V = mul i64 %N, 8 737 %t = getelementptr i64, i64* %cast, i64 %V 738 %x = load i64, i64* %t 739 ret i64 %x 740 } 741 742 ; gep should be done in the original address space. 743 define i64 @test_gep_bitcast_array_same_size_element_addrspacecast([100 x double]* %arr, i64 %N) { 744 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_addrspacecast( 745 ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %V 746 ; CHECK-NEXT: bitcast double* 747 ; CHECK-NEXT: %t = addrspacecast i64* 748 ; CHECK: load i64, i64 addrspace(3)* %t 749 %cast = addrspacecast [100 x double]* %arr to i64 addrspace(3)* 750 %V = mul i64 %N, 8 751 %t = getelementptr i64, i64 addrspace(3)* %cast, i64 %V 752 %x = load i64, i64 addrspace(3)* %t 753 ret i64 %x 754 } 755 756 ; The element size of the array is different the element size of the pointer 757 define i8 @test_gep_bitcast_array_different_size_element([100 x double]* %arr, i64 %N) { 758 ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element( 759 ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %N 760 ; CHECK: bitcast 761 %cast = bitcast [100 x double]* %arr to i8* 762 %V = mul i64 %N, 8 763 %t = getelementptr i8, i8* %cast, i64 %V 764 %x = load i8, i8* %t 765 ret i8 %x 766 } 767 768 define i64 @test_gep_bitcast_array_same_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) { 769 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_as1( 770 ; CHECK: getelementptr [100 x double], [100 x double] addrspace(1)* %arr, i16 0, i16 %V 771 ; CHECK: bitcast 772 %cast = bitcast [100 x double] addrspace(1)* %arr to i64 addrspace(1)* 773 %V = mul i16 %N, 8 774 %t = getelementptr i64, i64 addrspace(1)* %cast, i16 %V 775 %x = load i64, i64 addrspace(1)* %t 776 ret i64 %x 777 } 778 779 define i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) { 780 ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element_as1( 781 ; CHECK: getelementptr [100 x double], [100 x double] addrspace(1)* %arr, i16 0, i16 %N 782 ; CHECK: bitcast 783 %cast = bitcast [100 x double] addrspace(1)* %arr to i8 addrspace(1)* 784 %V = mul i16 %N, 8 785 %t = getelementptr i8, i8 addrspace(1)* %cast, i16 %V 786 %x = load i8, i8 addrspace(1)* %t 787 ret i8 %x 788 } 789 790 define i64 @test40() { 791 %array = alloca [3 x i32], align 4 792 %gep = getelementptr inbounds [3 x i32], [3 x i32]* %array, i64 0, i64 2 793 %gepi8 = bitcast i32* %gep to i8* 794 %p = ptrtoint [3 x i32]* %array to i64 795 %np = sub i64 0, %p 796 %gep2 = getelementptr i8, i8* %gepi8, i64 %np 797 %ret = ptrtoint i8* %gep2 to i64 798 ret i64 %ret 799 800 ; CHECK-LABEL: @test40 801 ; CHECK-NEXT: ret i64 8 802 } 803 804 define i16 @test41([3 x i32] addrspace(1)* %array) { 805 %gep = getelementptr inbounds [3 x i32], [3 x i32] addrspace(1)* %array, i16 0, i16 2 806 %gepi8 = bitcast i32 addrspace(1)* %gep to i8 addrspace(1)* 807 %p = ptrtoint [3 x i32] addrspace(1)* %array to i16 808 %np = sub i16 0, %p 809 %gep2 = getelementptr i8, i8 addrspace(1)* %gepi8, i16 %np 810 %ret = ptrtoint i8 addrspace(1)* %gep2 to i16 811 ret i16 %ret 812 813 ; CHECK-LABEL: @test41( 814 ; CHECK-NEXT: ret i16 8 815 } 816 817 define i8* @test42(i8* %c1, i8* %c2) { 818 %ptrtoint = ptrtoint i8* %c1 to i64 819 %sub = sub i64 0, %ptrtoint 820 %gep = getelementptr inbounds i8, i8* %c2, i64 %sub 821 ret i8* %gep 822 823 ; CHECK-LABEL: @test42( 824 ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint i8* %c1 to i64 825 ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint i8* %c2 to i64 826 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]] 827 ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i8* 828 ; CHECK-NEXT: ret i8* [[INTTOPTR]] 829 } 830 831 define i16* @test43(i16* %c1, i16* %c2) { 832 %ptrtoint = ptrtoint i16* %c1 to i64 833 %sub = sub i64 0, %ptrtoint 834 %shr = ashr i64 %sub, 1 835 %gep = getelementptr inbounds i16, i16* %c2, i64 %shr 836 ret i16* %gep 837 838 ; CHECK-LABEL: @test43( 839 ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint i16* %c1 to i64 840 ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint i16* %c2 to i64 841 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]] 842 ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i16* 843 ; CHECK-NEXT: ret i16* [[INTTOPTR]] 844 } 845 846 define %struct.C* @test44(%struct.C* %c1, %struct.C* %c2) { 847 %ptrtoint = ptrtoint %struct.C* %c1 to i64 848 %sub = sub i64 0, %ptrtoint 849 %shr = sdiv i64 %sub, 7 850 %gep = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %shr 851 ret %struct.C* %gep 852 853 ; CHECK-LABEL: @test44( 854 ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint %struct.C* %c1 to i64 855 ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint %struct.C* %c2 to i64 856 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]] 857 ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to %struct.C* 858 ; CHECK-NEXT: ret %struct.C* [[INTTOPTR]] 859 } 860 861 define %struct.C* @test45(%struct.C* %c1, %struct.C** %c2) { 862 %ptrtoint1 = ptrtoint %struct.C* %c1 to i64 863 %ptrtoint2 = ptrtoint %struct.C** %c2 to i64 864 %sub = sub i64 %ptrtoint2, %ptrtoint1 ; C2 - C1 865 %shr = sdiv i64 %sub, 7 866 %gep = getelementptr inbounds %struct.C, %struct.C* %c1, i64 %shr ; C1 + (C2 - C1) 867 ret %struct.C* %gep 868 869 ; CHECK-LABEL: @test45( 870 ; CHECK-NEXT: [[BITCAST:%.*]] = bitcast %struct.C** %c2 to %struct.C* 871 ; CHECK-NEXT: ret %struct.C* [[BITCAST]] 872 } 873 874 define %struct.C* @test46(%struct.C* %c1, %struct.C* %c2, i64 %N) { 875 %ptrtoint = ptrtoint %struct.C* %c1 to i64 876 %sub = sub i64 0, %ptrtoint 877 %sdiv = sdiv i64 %sub, %N 878 %gep = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %sdiv 879 ret %struct.C* %gep 880 881 ; CHECK-LABEL: @test46( 882 ; CHECK-NEXT: [[PTRTOINT:%.*]] = ptrtoint %struct.C* %c1 to i64 883 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[PTRTOINT]] 884 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i64 [[SUB]], %N 885 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %sdiv 886 ; CHECK-NEXT: ret %struct.C* [[GEP]] 887 } 888 889 define i32 addrspace(1)* @ascast_0_gep(i32* %p) nounwind { 890 ; CHECK-LABEL: @ascast_0_gep( 891 ; CHECK-NOT: getelementptr 892 ; CHECK: ret 893 %gep = getelementptr i32, i32* %p, i32 0 894 %x = addrspacecast i32* %gep to i32 addrspace(1)* 895 ret i32 addrspace(1)* %x 896 } 897 898 ; Do not merge the GEP and the addrspacecast, because it would undo the 899 ; addrspacecast canonicalization. 900 define i32 addrspace(1)* @ascast_0_0_gep([128 x i32]* %p) nounwind { 901 ; CHECK-LABEL: @ascast_0_0_gep( 902 ; CHECK-NEXT: getelementptr [128 x i32] 903 ; CHECK-NEXT: addrspacecast i32* 904 ; CHECK-NEXT: ret i32 addrspace(1)* 905 %gep = getelementptr [128 x i32], [128 x i32]* %p, i32 0, i32 0 906 %x = addrspacecast i32* %gep to i32 addrspace(1)* 907 ret i32 addrspace(1)* %x 908 } 909 910 ; CHECK: attributes [[NUW]] = { nounwind } 911