Skip to content

Commit 0982e76

Browse files
committed
[Value] Look through inttoptr (add ..) in accumulateConstantOffsets
Look through inttoptr (add (ptrtoint P), C) when accumulating offsets. Adds a missing fold after llvm#123518 Alive2 for the tests with changes: https://alive2.llvm.org/ce/z/VvPrzv
1 parent 4359360 commit 0982e76

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,11 +1258,12 @@ Constant *llvm::ConstantFoldCompareInstOperands(
12581258
if (Ops0->getType()->isPointerTy() && !ICmpInst::isSigned(Predicate)) {
12591259
unsigned IndexWidth = DL.getIndexTypeSizeInBits(Ops0->getType());
12601260
APInt Offset0(IndexWidth, 0);
1261-
Value *Stripped0 =
1262-
Ops0->stripAndAccumulateInBoundsConstantOffsets(DL, Offset0);
1261+
bool AllowNonInbounds = ICmpInst::isEquality(Predicate);
1262+
Value *Stripped0 = Ops0->stripAndAccumulateConstantOffsets(
1263+
DL, Offset0, AllowNonInbounds);
12631264
APInt Offset1(IndexWidth, 0);
1264-
Value *Stripped1 =
1265-
Ops1->stripAndAccumulateInBoundsConstantOffsets(DL, Offset1);
1265+
Value *Stripped1 = Ops1->stripAndAccumulateConstantOffsets(
1266+
DL, Offset1, AllowNonInbounds);
12661267
if (Stripped0 == Stripped1)
12671268
return ConstantInt::getBool(
12681269
Ops0->getContext(),

llvm/lib/IR/Value.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,25 @@ const Value *Value::stripAndAccumulateConstantOffsets(
775775
V = RV;
776776
if (AllowInvariantGroup && Call->isLaunderOrStripInvariantGroup())
777777
V = Call->getArgOperand(0);
778+
} else if (auto *Int2Ptr = dyn_cast<Operator>(V)) {
779+
// Try to accumulate across (inttoptr (add (ptrtoint p), off)).
780+
if (!Int2Ptr || Int2Ptr->getOpcode() != Instruction::IntToPtr ||
781+
Int2Ptr->getOperand(0)->getType()->getScalarSizeInBits() != BitWidth)
782+
return V;
783+
auto *Add = dyn_cast<AddOperator>(Int2Ptr->getOperand(0));
784+
if (!AllowNonInbounds || !Add)
785+
return V;
786+
auto *Ptr2Int = dyn_cast<PtrToIntOperator>(Add->getOperand(0));
787+
auto *CI = dyn_cast<ConstantInt>(Add->getOperand(1));
788+
if (!Ptr2Int || !CI)
789+
return V;
790+
791+
APInt AddOffset = CI->getValue();
792+
if (AddOffset.getSignificantBits() > BitWidth)
793+
return V;
794+
795+
Offset = AddOffset.sextOrTrunc(BitWidth);
796+
V = Ptr2Int->getOperand(0);
778797
}
779798
assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
780799
} while (Visited.insert(V).second);

llvm/test/Transforms/InstSimplify/constant-fold-inttoptr-add.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:
77

88
define i1 @known_constexpr_add_eq() {
99
; CHECK-LABEL: define i1 @known_constexpr_add_eq() {
10-
; CHECK-NEXT: [[COND:%.*]] = icmp eq ptr getelementptr inbounds nuw (i8, ptr @glob, i64 80), inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 -80) to ptr)
11-
; CHECK-NEXT: ret i1 [[COND]]
10+
; CHECK-NEXT: ret i1 false
1211
;
1312
%cond = icmp eq ptr getelementptr inbounds nuw (i8, ptr @glob, i64 80), inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 -80) to ptr)
1413
ret i1 %cond
1514
}
1615

1716
define i1 @known_constexpr_add_ne() {
1817
; CHECK-LABEL: define i1 @known_constexpr_add_ne() {
19-
; CHECK-NEXT: [[COND:%.*]] = icmp ne ptr getelementptr inbounds nuw (i8, ptr @glob, i64 80), inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 -80) to ptr)
20-
; CHECK-NEXT: ret i1 [[COND]]
18+
; CHECK-NEXT: ret i1 true
2119
;
2220
%cond = icmp ne ptr getelementptr inbounds nuw (i8, ptr @glob, i64 80), inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 -80) to ptr)
2321
ret i1 %cond
@@ -34,8 +32,7 @@ define i1 @wrap_positive_to_negate() {
3432
; 9223372036854775808 = 2^63
3533
define i1 @wrap_positive_to_zero() {
3634
; CHECK-LABEL: define i1 @wrap_positive_to_zero() {
37-
; CHECK-NEXT: [[COND:%.*]] = icmp eq ptr @glob, inttoptr (i64 add (i64 ptrtoint (ptr getelementptr nuw (i8, ptr @glob, i64 -9223372036854775808) to i64), i64 -9223372036854775808) to ptr)
38-
; CHECK-NEXT: ret i1 [[COND]]
35+
; CHECK-NEXT: ret i1 true
3936
;
4037
%cond = icmp eq ptr @glob, inttoptr (i64 add (i64 ptrtoint (ptr getelementptr nuw (i8, ptr @glob, i64 9223372036854775808)to i64), i64 9223372036854775808) to ptr)
4138
ret i1 %cond

0 commit comments

Comments
 (0)