Skip to content

Commit

Permalink
[InstSimplify] Restrict a GEP transform to avoid provenance changes
Browse files Browse the repository at this point in the history
This is a follow-up to D98588, and fixes the inline `FIXME` about a GEP-related simplification not
preserving the provenance.

https://alive2.llvm.org/ce/z/qbQoAY

Additional tests were added in {rGf125f28afdb59eba29d2491dac0dfc0a7bf1b60b}

Depends on D98672

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D98611
  • Loading branch information
nagisa committed Mar 16, 2021
1 parent 264f101 commit 6513995
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 3 additions & 5 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4349,11 +4349,9 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
// doesn't truncate the pointers.
if (Ops[1]->getType()->getScalarSizeInBits() ==
Q.DL.getPointerSizeInBits(AS)) {
auto CanSimplify = [GEPTy, &P]() -> bool {
// FIXME: The following transforms are only legal if P and V have the
// same provenance (PR44403). Check whether getUnderlyingObject() is
// the same?
return P->getType() == GEPTy;
auto CanSimplify = [GEPTy, &P, V = Ops[0]]() -> bool {
return P->getType() == GEPTy &&
getUnderlyingObject(P) == getUnderlyingObject(V);
};
// getelementptr V, (sub P, V) -> P if P points to a type of size 1.
if (TyAllocSize == 1 &&
Expand Down
20 changes: 17 additions & 3 deletions llvm/test/Transforms/InstSimplify/gep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"

define %struct.A* @test1(%struct.A* %b, %struct.A* %e) {
; CHECK-LABEL: @test1(
; CHECK-NEXT: ret %struct.A* [[E:%.*]]
; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint %struct.A* [[E:%.*]] to i64
; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint %struct.A* [[B:%.*]] to i64
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
; CHECK-NEXT: [[SDIV:%.*]] = sdiv exact i64 [[SUB]], 7
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_A:%.*]], %struct.A* [[B]], i64 [[SDIV]]
; CHECK-NEXT: ret %struct.A* [[GEP]]
;
%e_ptr = ptrtoint %struct.A* %e to i64
%b_ptr = ptrtoint %struct.A* %b to i64
Expand All @@ -19,7 +24,11 @@ define %struct.A* @test1(%struct.A* %b, %struct.A* %e) {

define i8* @test2(i8* %b, i8* %e) {
; CHECK-LABEL: @test2(
; CHECK-NEXT: ret i8* [[E:%.*]]
; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint i8* [[E:%.*]] to i64
; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i8* [[B:%.*]] to i64
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* [[B]], i64 [[SUB]]
; CHECK-NEXT: ret i8* [[GEP]]
;
%e_ptr = ptrtoint i8* %e to i64
%b_ptr = ptrtoint i8* %b to i64
Expand All @@ -30,7 +39,12 @@ define i8* @test2(i8* %b, i8* %e) {

define i64* @test3(i64* %b, i64* %e) {
; CHECK-LABEL: @test3(
; CHECK-NEXT: ret i64* [[E:%.*]]
; CHECK-NEXT: [[E_PTR:%.*]] = ptrtoint i64* [[E:%.*]] to i64
; CHECK-NEXT: [[B_PTR:%.*]] = ptrtoint i64* [[B:%.*]] to i64
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[E_PTR]], [[B_PTR]]
; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i64 [[SUB]], 3
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i64, i64* [[B]], i64 [[ASHR]]
; CHECK-NEXT: ret i64* [[GEP]]
;
%e_ptr = ptrtoint i64* %e to i64
%b_ptr = ptrtoint i64* %b to i64
Expand Down

0 comments on commit 6513995

Please sign in to comment.