Skip to content

Commit a2c39d8

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 56ad542 commit a2c39d8

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
@@ -5,8 +5,7 @@
55

66
define i1 @known_constexpr_add_eq() {
77
; CHECK-LABEL: define i1 @known_constexpr_add_eq() {
8-
; 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)
9-
; CHECK-NEXT: ret i1 [[COND]]
8+
; CHECK-NEXT: ret i1 false
109
;
1110
%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)
1211
ret i1 %cond
@@ -23,8 +22,7 @@ define i1 @known_constexpr_add_eq_ops_swapped() {
2322

2423
define i1 @known_constexpr_add_ne() {
2524
; CHECK-LABEL: define i1 @known_constexpr_add_ne() {
26-
; 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)
27-
; CHECK-NEXT: ret i1 [[COND]]
25+
; CHECK-NEXT: ret i1 true
2826
;
2927
%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)
3028
ret i1 %cond
@@ -41,8 +39,7 @@ define i1 @wrap_positive_to_negate() {
4139
; 9223372036854775808 = 2^63
4240
define i1 @wrap_positive_to_zero() {
4341
; CHECK-LABEL: define i1 @wrap_positive_to_zero() {
44-
; 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)
45-
; CHECK-NEXT: ret i1 [[COND]]
42+
; CHECK-NEXT: ret i1 true
4643
;
4744
%cond = icmp eq ptr @glob, inttoptr (i64 add (i64 ptrtoint (ptr getelementptr nuw (i8, ptr @glob, i64 9223372036854775808)to i64), i64 9223372036854775808) to ptr)
4845
ret i1 %cond

0 commit comments

Comments
 (0)