Skip to content

Commit fa83e47

Browse files
committed
Addressed comments
1 parent a2b3e03 commit fa83e47

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,14 +2631,16 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
26312631
/* IsNUW */ false))
26322632
return replaceInstUsesWith(I, Res);
26332633

2634-
if (match(Op0, m_ZExt(m_PtrToInt(m_Value(LHSOp)))) &&
2634+
if (match(Op0, m_ZExt(m_PtrToIntSameSize(DL, m_Value(LHSOp)))) &&
26352635
match(Op1, m_ZExtOrSelf(m_PtrToInt(m_Value(RHSOp))))) {
26362636
if (auto *GEP = dyn_cast<GEPOperator>(LHSOp)) {
26372637
if (GEP->getPointerOperand() == RHSOp) {
26382638
if (GEP->hasNoUnsignedWrap() || GEP->hasNoUnsignedSignedWrap()) {
26392639
Value *Offset = EmitGEPOffset(GEP);
26402640
Value *Res = GEP->hasNoUnsignedWrap()
2641-
? Builder.CreateZExt(Offset, I.getType())
2641+
? Builder.CreateZExt(
2642+
Offset, I.getType(), "",
2643+
/*IsNonNeg=*/GEP->hasNoUnsignedSignedWrap())
26422644
: Builder.CreateSExt(Offset, I.getType());
26432645
return replaceInstUsesWith(I, Res);
26442646
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,18 @@ define i64 @zext_ptrtoint_sub_ptrtoint_as2_nuw(i32 %offset) {
340340
ret i64 %D
341341
}
342342

343+
define i64 @zext_ptrtoint_sub_ptrtoint_as2_nusw_nuw(i32 %offset) {
344+
; CHECK-LABEL: @zext_ptrtoint_sub_ptrtoint_as2_nusw_nuw(
345+
; CHECK-NEXT: %A.idx = shl nuw nsw i32 %offset, 1
346+
; CHECK-NEXT: %D = zext nneg i32 %A.idx to i64
347+
; CHECK-NEXT: ret i64 %D
348+
%A = getelementptr nusw nuw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
349+
%B = ptrtoint ptr addrspace(2) %A to i32
350+
%C = zext i32 %B to i64
351+
%D = sub i64 %C, ptrtoint (ptr addrspace(2) @Arr_as2 to i64)
352+
ret i64 %D
353+
}
354+
343355
define i64 @zext_ptrtoint_sub_zext_ptrtoint_as2(i32 %offset) {
344356
; CHECK-LABEL: @zext_ptrtoint_sub_zext_ptrtoint_as2(
345357
; CHECK-NEXT: %A.idx = shl nsw i32 %offset, 1
@@ -366,6 +378,21 @@ define i64 @zext_ptrtoint_sub_zext_ptrtoint_as2_nuw(i32 %offset) {
366378
ret i64 %E
367379
}
368380

381+
define i64 @negative_zext_ptrtoint_sub_ptrtoint_as2_nuw(i32 %offset) {
382+
; CHECK-LABEL: @negative_zext_ptrtoint_sub_ptrtoint_as2_nuw(
383+
; CHECK-NEXT: %A = getelementptr nuw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
384+
; CHECK-NEXT: %1 = ptrtoint ptr addrspace(2) %A to i32
385+
; CHECK-NEXT: %B.mask = and i32 %1, 65534
386+
; CHECK-NEXT: %C = zext nneg i32 %B.mask to i64
387+
; CHECK-NEXT: %D = sub nsw i64 %C, ptrtoint (ptr addrspace(2) @Arr_as2 to i64)
388+
; CHECK-NEXT: ret i64 %D
389+
%A = getelementptr nuw bfloat, ptr addrspace(2) @Arr_as2, i32 %offset
390+
%B = ptrtoint ptr addrspace(2) %A to i16
391+
%C = zext i16 %B to i64
392+
%D = sub i64 %C, ptrtoint (ptr addrspace(2) @Arr_as2 to i64)
393+
ret i64 %D
394+
}
395+
369396
define i64 @ptrtoint_sub_zext_ptrtoint_as2(i32 %offset) {
370397
; CHECK-LABEL: @ptrtoint_sub_zext_ptrtoint_as2(
371398
; CHECK-NEXT: %A = getelementptr inbounds bfloat, ptr addrspace(2) @Arr_as2, i32 %offset

0 commit comments

Comments
 (0)