Skip to content

Commit 7d33824

Browse files
nikicjrbyrnes
authored andcommitted
[SeparateConstOffsetFromGEP] Check correct index for non-negativity
We were checking the index of GEP twice, instead of checking both GEP and PtrGEP. Change-Id: I0ce71f4af95fb6b8d0a70fc8baa850c8e2fa5d60
1 parent e3e0483 commit 7d33824

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
10061006
auto KnownGEPIdx = computeKnownBits(GEPIdx->get(), *DL);
10071007
IsChainInBounds &= KnownGEPIdx.isNonNegative();
10081008
if (IsChainInBounds) {
1009-
auto PtrGEPIdx = GEP->indices().begin();
1009+
auto PtrGEPIdx = PtrGEP->indices().begin();
10101010
auto KnownPtrGEPIdx = computeKnownBits(PtrGEPIdx->get(), *DL);
10111011
IsChainInBounds &= KnownPtrGEPIdx.isNonNegative();
10121012
}

llvm/test/Transforms/SeparateConstOffsetFromGEP/NVPTX/lower-gep-reorder.ll

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,44 @@ end:
6363
call void asm sideeffect "; use $0", "v"(ptr %idx3)
6464
ret void
6565
}
66+
67+
define void @inboundsPossiblyNegative1(ptr %in.ptr, i64 %in.idx1) {
68+
; CHECK-LABEL: define void @inboundsPossiblyNegative1(
69+
; CHECK-SAME: ptr [[IN_PTR:%.*]], i64 [[IN_IDX1:%.*]]) {
70+
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr <2 x i8>, ptr [[IN_PTR]], i64 [[IN_IDX1]]
71+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr <2 x i8>, ptr [[TMP0]], i64 1
72+
; CHECK-NEXT: ret void
73+
;
74+
%const1 = getelementptr inbounds <2 x i8>, ptr %in.ptr, i64 1
75+
%idx1 = getelementptr inbounds <2 x i8>, ptr %const1, i64 %in.idx1
76+
ret void
77+
}
78+
79+
define void @inboundsPossiblyNegative2(ptr %in.ptr, i64 %in.idx1) {
80+
; CHECK-LABEL: define void @inboundsPossiblyNegative2(
81+
; CHECK-SAME: ptr [[IN_PTR:%.*]], i64 [[IN_IDX1:%.*]]) {
82+
; CHECK-NEXT: [[IN_IDX1_NNEG:%.*]] = and i64 [[IN_IDX1]], 9223372036854775807
83+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr <2 x i8>, ptr [[IN_PTR]], i64 [[IN_IDX1_NNEG]]
84+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr <2 x i8>, ptr [[TMP1]], i64 -1
85+
; CHECK-NEXT: ret void
86+
;
87+
%in.idx1.nneg = and i64 %in.idx1, 9223372036854775807
88+
%const1 = getelementptr inbounds <2 x i8>, ptr %in.ptr, i64 -1
89+
%idx1 = getelementptr inbounds <2 x i8>, ptr %const1, i64 %in.idx1.nneg
90+
ret void
91+
}
92+
93+
define void @inboundsNonNegative(ptr %in.ptr, i64 %in.idx1) {
94+
; CHECK-LABEL: define void @inboundsNonNegative(
95+
; CHECK-SAME: ptr [[IN_PTR:%.*]], i64 [[IN_IDX1:%.*]]) {
96+
; CHECK-NEXT: [[IDXPROM:%.*]] = and i64 [[IN_IDX1]], 9223372036854775807
97+
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds <2 x i8>, ptr [[IN_PTR]], i64 [[IDXPROM]]
98+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds <2 x i8>, ptr [[TMP0]], i64 1
99+
; CHECK-NEXT: ret void
100+
;
101+
%in.idx1.nneg = and i64 %in.idx1, 9223372036854775807
102+
%const1 = getelementptr inbounds <2 x i8>, ptr %in.ptr, i64 1
103+
%idx1 = getelementptr inbounds <2 x i8>, ptr %const1, i64 %in.idx1.nneg
104+
ret void
105+
}
106+

0 commit comments

Comments
 (0)