834 changes: 536 additions & 298 deletions llvm/test/Transforms/InstCombine/getelementptr.ll

Large diffs are not rendered by default.

31 changes: 23 additions & 8 deletions llvm/test/Transforms/InstCombine/pr26992.ll
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -instcombine -S < %s | FileCheck %s
target triple = "x86_64-pc-windows-msvc"

define i1 @test1(i8* %p) personality i32 (...)* @__CxxFrameHandler3 {
; CHECK-LABEL: @test1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = getelementptr i8, i8* [[P:%.*]], i64 1
; CHECK-NEXT: invoke void @may_throw()
; CHECK-NEXT: to label [[INVOKE_CONT:%.*]] unwind label [[CATCH_DISPATCH:%.*]]
; CHECK: invoke.cont:
; CHECK-NEXT: [[B:%.*]] = getelementptr inbounds i8, i8* [[P]], i64 2
; CHECK-NEXT: invoke void @may_throw()
; CHECK-NEXT: to label [[EXIT:%.*]] unwind label [[CATCH_DISPATCH]]
; CHECK: catch.dispatch:
; CHECK-NEXT: [[C:%.*]] = phi i8* [ [[B]], [[INVOKE_CONT]] ], [ [[A]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[TMP1:%.*]] = catchswitch within none [label %catch] unwind to caller
; CHECK: catch:
; CHECK-NEXT: [[TMP2:%.*]] = catchpad within [[TMP1]] [i8* null, i32 64, i8* null]
; CHECK-NEXT: catchret from [[TMP2]] to label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[D:%.*]] = phi i8* [ [[A]], [[INVOKE_CONT]] ], [ [[C]], [[CATCH:%.*]] ]
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8* [[D]], [[A]]
; CHECK-NEXT: ret i1 [[CMP]]
;
entry:
%a = getelementptr i8, i8* %p, i64 1
invoke void @may_throw()
to label %invoke.cont unwind label %catch.dispatch
to label %invoke.cont unwind label %catch.dispatch

invoke.cont:
%b = getelementptr inbounds i8, i8* %a, i64 1
invoke void @may_throw()
to label %exit unwind label %catch.dispatch
to label %exit unwind label %catch.dispatch

catch.dispatch:
%c = phi i8* [ %b, %invoke.cont ], [ %a, %entry ]
Expand All @@ -26,12 +47,6 @@ exit:
ret i1 %cmp
}

; CHECK-LABEL: define i1 @test1(
; CHECK: %[[gep_a:.*]] = getelementptr i8, i8* %p, i64 1
; CHECK: %[[gep_b:.*]] = getelementptr inbounds i8, i8* %p, i64 2
; CHECK: phi i8* [ %[[gep_b]], {{.*}} ], [ %[[gep_a]], {{.*}} ]
; CHECK: %tmp1 = catchswitch within none [label %catch] unwind to caller

declare void @may_throw()

declare i32 @__CxxFrameHandler3(...)
101 changes: 101 additions & 0 deletions llvm/test/Transforms/InstCombine/sub-gep.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -instcombine < %s | FileCheck %s

define i64 @test_inbounds([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nsw i64 [[IDX:%.*]], 2
; CHECK-NEXT: ret i64 [[P2_IDX]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub i64 %i2, %i1
ret i64 %d
}

define i64 @test_inbounds_nuw([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds_nuw(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nsw i64 [[IDX:%.*]], 2
; CHECK-NEXT: ret i64 [[P2_IDX]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}

define i64 @test_nuw([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_nuw(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl i64 [[IDX:%.*]], 2
; CHECK-NEXT: ret i64 [[P2_IDX]]
;
%p1 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}

define i32 @test_inbounds_nuw_trunc([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds_nuw_trunc(
; CHECK-NEXT: [[IDX_TR:%.*]] = trunc i64 [[IDX:%.*]] to i32
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[IDX_TR]], 2
; CHECK-NEXT: ret i32 [[TMP1]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%t1 = trunc i64 %i1 to i32
%t2 = trunc i64 %i2 to i32
%d = sub nuw i32 %t2, %t1
ret i32 %d
}

define i64 @test_inbounds_nuw_swapped([0 x i32]* %base, i64 %idx) {
; CHECK-LABEL: @test_inbounds_nuw_swapped(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nsw i64 [[IDX:%.*]], 2
; CHECK-NEXT: [[DIFF_NEG:%.*]] = sub i64 0, [[P2_IDX]]
; CHECK-NEXT: ret i64 [[DIFF_NEG]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%i1 = ptrtoint i32* %p2 to i64
%i2 = ptrtoint i32* %p1 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}

; The sub and shl here could be nuw, but this is harder to handle.
define i64 @test_inbounds_nuw_two_gep([0 x i32]* %base, i64 %idx, i64 %idx2) {
; CHECK-LABEL: @test_inbounds_nuw_two_gep(
; CHECK-NEXT: [[P2_IDX1:%.*]] = sub i64 [[IDX2:%.*]], [[IDX:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = shl i64 [[P2_IDX1]], 2
; CHECK-NEXT: ret i64 [[TMP1]]
;
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx2
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}

define i64 @test_inbounds_nuw_multi_index([0 x [2 x i32]]* %base, i64 %idx, i64 %idx2) {
; CHECK-LABEL: @test_inbounds_nuw_multi_index(
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nsw i64 [[IDX:%.*]], 3
; CHECK-NEXT: [[P2_IDX1:%.*]] = shl nsw i64 [[IDX2:%.*]], 2
; CHECK-NEXT: [[P2_OFFS2:%.*]] = add i64 [[P2_IDX]], [[P2_IDX1]]
; CHECK-NEXT: ret i64 [[P2_OFFS2]]
;
%p1 = getelementptr inbounds [0 x [2 x i32]], [0 x [2 x i32]]* %base, i64 0, i64 0, i64 0
%p2 = getelementptr inbounds [0 x [2 x i32]], [0 x [2 x i32]]* %base, i64 0, i64 %idx, i64 %idx2
%i1 = ptrtoint i32* %p1 to i64
%i2 = ptrtoint i32* %p2 to i64
%d = sub nuw i64 %i2, %i1
ret i64 %d
}