44 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; RUN: opt < %s -passes=instcombine -S | FileCheck %s3 4target datalayout = "p:32:32"5 6%S = type { [2 x i32] }7 8define i1 @test(ptr %p, i32 %n) {9; CHECK-LABEL: @test(10; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[N:%.*]], 111; CHECK-NEXT: ret i1 [[CMP]]12;13 %end = getelementptr inbounds [0 x %S], ptr %p, i32 0, i32 %n, i32 0, i32 014 %last = getelementptr inbounds %S, ptr %end, i32 -115 %cmp = icmp eq ptr %last, %p16 ret i1 %cmp17}18 19; Same test using 64-bit indices.20define i1 @test64(ptr %p, i64 %n) {21; CHECK-LABEL: @test64(22; CHECK-NEXT: [[TMP1:%.*]] = trunc nsw i64 [[N:%.*]] to i3223; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 124; CHECK-NEXT: ret i1 [[CMP]]25;26 %end = getelementptr inbounds [0 x %S], ptr %p, i64 0, i64 %n, i32 0, i64 027 %last = getelementptr inbounds %S, ptr %end, i64 -128 %cmp = icmp eq ptr %last, %p29 ret i1 %cmp30}31 32; Here the offset overflows and is treated modulo 2^32. This is UB.33define i1 @test64_overflow(ptr %p, i64 %n) {34; CHECK-LABEL: @test64_overflow(35; CHECK-NEXT: [[TMP1:%.*]] = trunc nsw i64 [[N:%.*]] to i3236; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 137; CHECK-NEXT: ret i1 [[CMP]]38;39 %end = getelementptr inbounds [0 x %S], ptr %p, i64 0, i64 %n, i32 0, i64 858993459240 %last = getelementptr inbounds %S, ptr %end, i64 -141 %cmp = icmp eq ptr %last, %p42 ret i1 %cmp43}44