Skip to content

Commit 7020781

Browse files
committed
[InstCombine] add ptr difference tests; NFC
1 parent 01b3e16 commit 7020781

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

llvm/test/Transforms/InstCombine/sub-gep.ll

+54-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ define i64 @test_inbounds([0 x i32]* %base, i64 %idx) {
1414
ret i64 %d
1515
}
1616

17+
define i64 @test_partial_inbounds1([0 x i32]* %base, i64 %idx) {
18+
; CHECK-LABEL: @test_partial_inbounds1(
19+
; CHECK-NEXT: [[P2_IDX:%.*]] = shl i64 [[IDX:%.*]], 2
20+
; CHECK-NEXT: ret i64 [[P2_IDX]]
21+
;
22+
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
23+
%p2 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
24+
%i1 = ptrtoint i32* %p1 to i64
25+
%i2 = ptrtoint i32* %p2 to i64
26+
%d = sub i64 %i2, %i1
27+
ret i64 %d
28+
}
29+
30+
define i64 @test_partial_inbounds2([0 x i32]* %base, i64 %idx) {
31+
; CHECK-LABEL: @test_partial_inbounds2(
32+
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nsw i64 [[IDX:%.*]], 2
33+
; CHECK-NEXT: ret i64 [[P2_IDX]]
34+
;
35+
%p1 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 0
36+
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
37+
%i1 = ptrtoint i32* %p1 to i64
38+
%i2 = ptrtoint i32* %p2 to i64
39+
%d = sub i64 %i2, %i1
40+
ret i64 %d
41+
}
42+
1743
define i64 @test_inbounds_nuw([0 x i32]* %base, i64 %idx) {
1844
; CHECK-LABEL: @test_inbounds_nuw(
1945
; CHECK-NEXT: [[P2_IDX:%.*]] = shl nuw nsw i64 [[IDX:%.*]], 2
@@ -69,13 +95,39 @@ define i64 @test_inbounds_nuw_swapped([0 x i32]* %base, i64 %idx) {
6995
ret i64 %d
7096
}
7197

98+
define i64 @test_inbounds1_nuw_swapped([0 x i32]* %base, i64 %idx) {
99+
; CHECK-LABEL: @test_inbounds1_nuw_swapped(
100+
; CHECK-NEXT: [[P2_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
101+
; CHECK-NEXT: ret i64 [[P2_IDX_NEG]]
102+
;
103+
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 0
104+
%p2 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
105+
%i1 = ptrtoint i32* %p2 to i64
106+
%i2 = ptrtoint i32* %p1 to i64
107+
%d = sub nuw i64 %i2, %i1
108+
ret i64 %d
109+
}
110+
111+
define i64 @test_inbounds2_nuw_swapped([0 x i32]* %base, i64 %idx) {
112+
; CHECK-LABEL: @test_inbounds2_nuw_swapped(
113+
; CHECK-NEXT: [[P2_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
114+
; CHECK-NEXT: ret i64 [[P2_IDX_NEG]]
115+
;
116+
%p1 = getelementptr [0 x i32], [0 x i32]* %base, i64 0, i64 0
117+
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
118+
%i1 = ptrtoint i32* %p2 to i64
119+
%i2 = ptrtoint i32* %p1 to i64
120+
%d = sub nuw i64 %i2, %i1
121+
ret i64 %d
122+
}
123+
72124
; The sub and shl here could be nuw, but this is harder to handle.
73125
define i64 @test_inbounds_nuw_two_gep([0 x i32]* %base, i64 %idx, i64 %idx2) {
74126
; CHECK-LABEL: @test_inbounds_nuw_two_gep(
75127
; CHECK-NEXT: [[P1_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
76128
; CHECK-NEXT: [[P2_IDX_NEG_NEG:%.*]] = shl i64 [[IDX2:%.*]], 2
77-
; CHECK-NEXT: [[DOTNEG:%.*]] = add i64 [[P2_IDX_NEG_NEG]], [[P1_IDX_NEG]]
78-
; CHECK-NEXT: ret i64 [[DOTNEG]]
129+
; CHECK-NEXT: [[GEPDIFF_NEG:%.*]] = add i64 [[P2_IDX_NEG_NEG]], [[P1_IDX_NEG]]
130+
; CHECK-NEXT: ret i64 [[GEPDIFF_NEG]]
79131
;
80132
%p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
81133
%p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx2

0 commit comments

Comments
 (0)