[InstCombine] Strip bitcasts in GEP diff fold

Bitcasts were stripped in one case, but not the other. Of course,
this no longer really matters with opaque pointers, but as I went
through the trouble of tracking this down, we may as well remove
one typed vs opaque pointer optimization discrepancy.
This commit is contained in:
Nikita Popov 2022-05-24 16:08:21 +02:00
parent 3eab6c5911
commit e6e0eb3bc8
3 changed files with 6 additions and 12 deletions

View File

@ -1688,7 +1688,8 @@ Value *InstCombinerImpl::OptimizePointerDifference(Value *LHS, Value *RHS,
// Require at least one GEP with a common base pointer on both sides.
if (auto *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
// (gep X, ...) - X
if (LHSGEP->getOperand(0) == RHS) {
if (LHSGEP->getOperand(0)->stripPointerCasts() ==
RHS->stripPointerCasts()) {
GEP1 = LHSGEP;
} else if (auto *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
// (gep X, ...) - (gep X, ...)

View File

@ -552,7 +552,7 @@ define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
; CHECK-NEXT: [[T349:%.*]] = getelementptr [[STRUCT_SIGINFO_T:%.*]], %struct.siginfo_t* [[T344]], i64 0, i32 3, i32 0, i32 3, i32 0
; CHECK-NEXT: [[T349350:%.*]] = bitcast i8** [[T349]] to i32*
; CHECK-NEXT: [[T351:%.*]] = load i32, i32* [[T349350]], align 8
; CHECK-NEXT: [[T360:%.*]] = call i32 asm sideeffect "...", "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"(i32 [[T351]], %struct.__large_struct* elementtype(%struct.__large_struct) null, i32 -14, i32 0) #[[ATTR0:[0-9]+]]
; CHECK-NEXT: [[T360:%.*]] = call i32 asm sideeffect "...", "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"(i32 [[T351]], %struct.__large_struct* elementtype([[STRUCT___LARGE_STRUCT:%.*]]) null, i32 -14, i32 0) #[[ATTR0:[0-9]+]]
; CHECK-NEXT: unreachable
;
entry:
@ -1300,9 +1300,8 @@ define i8* @gep_null_inbounds_different_type(i64 %idx1, i64 %idx2) {
define i8* @D98588(i8* %c1, i64 %offset) {
; CHECK-LABEL: @D98588(
; CHECK-NEXT: [[C2:%.*]] = bitcast i8* [[C1:%.*]] to i64*
; CHECK-NEXT: [[C2_NEXT:%.*]] = getelementptr inbounds i64, i64* [[C2]], i64 [[OFFSET:%.*]]
; CHECK-NEXT: [[GEP:%.*]] = bitcast i64* [[C2_NEXT]] to i8*
; CHECK-NEXT: [[C2_NEXT_IDX:%.*]] = shl nsw i64 [[OFFSET:%.*]], 3
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* [[C1:%.*]], i64 [[C2_NEXT_IDX]]
; CHECK-NEXT: ret i8* [[GEP]]
;
%c2 = bitcast i8* %c1 to i64*

View File

@ -372,13 +372,7 @@ define i64 @gep_diff_second_inbounds(i8* %foo, i64 %i, i64 %j) {
define i64 @gep_diff_with_bitcast(i64* %p, i64 %idx) {
; CHECK-LABEL: @gep_diff_with_bitcast(
; CHECK-NEXT: [[I0:%.*]] = bitcast i64* [[P:%.*]] to [4 x i64]*
; CHECK-NEXT: [[I1:%.*]] = getelementptr inbounds [4 x i64], [4 x i64]* [[I0]], i64 [[IDX:%.*]]
; CHECK-NEXT: [[I3:%.*]] = ptrtoint [4 x i64]* [[I1]] to i64
; CHECK-NEXT: [[I4:%.*]] = ptrtoint i64* [[P]] to i64
; CHECK-NEXT: [[I5:%.*]] = sub nuw i64 [[I3]], [[I4]]
; CHECK-NEXT: [[I6:%.*]] = lshr i64 [[I5]], 5
; CHECK-NEXT: ret i64 [[I6]]
; CHECK-NEXT: ret i64 [[IDX:%.*]]
;
%i0 = bitcast i64* %p to [4 x i64]*
%i1 = getelementptr inbounds [4 x i64], [4 x i64]* %i0, i64 %idx