Skip to content

Commit 766ac5d

Browse files
committed
!fixup add LookThroughIntToPtr flag
1 parent a2c39d8 commit 766ac5d

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

llvm/include/llvm/IR/Value.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,17 +722,19 @@ class Value {
722722
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
723723
bool AllowInvariantGroup = false,
724724
function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis =
725-
nullptr) const;
725+
nullptr,
726+
bool LookThroughIntToPtr = false) const;
726727

727728
Value *stripAndAccumulateConstantOffsets(
728729
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
729730
bool AllowInvariantGroup = false,
730731
function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis =
731-
nullptr) {
732+
nullptr,
733+
bool LookThroughIntToPtr = false) {
732734
return const_cast<Value *>(
733735
static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets(
734-
DL, Offset, AllowNonInbounds, AllowInvariantGroup,
735-
ExternalAnalysis));
736+
DL, Offset, AllowNonInbounds, AllowInvariantGroup, ExternalAnalysis,
737+
LookThroughIntToPtr));
736738
}
737739

738740
/// This is a wrapper around stripAndAccumulateConstantOffsets with the

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,12 +1258,16 @@ Constant *llvm::ConstantFoldCompareInstOperands(
12581258
if (Ops0->getType()->isPointerTy() && !ICmpInst::isSigned(Predicate)) {
12591259
unsigned IndexWidth = DL.getIndexTypeSizeInBits(Ops0->getType());
12601260
APInt Offset0(IndexWidth, 0);
1261-
bool AllowNonInbounds = ICmpInst::isEquality(Predicate);
1261+
bool IsEqPred = ICmpInst::isEquality(Predicate);
12621262
Value *Stripped0 = Ops0->stripAndAccumulateConstantOffsets(
1263-
DL, Offset0, AllowNonInbounds);
1263+
DL, Offset0, /*AllowNonInbounds=*/IsEqPred,
1264+
/*AllowInvariantGroup=*/false, /*ExternalAnalysis=*/nullptr,
1265+
/*LookThroughIntToPtr*/ IsEqPred);
12641266
APInt Offset1(IndexWidth, 0);
12651267
Value *Stripped1 = Ops1->stripAndAccumulateConstantOffsets(
1266-
DL, Offset1, AllowNonInbounds);
1268+
DL, Offset1, /*AllowNonInbounds=*/IsEqPred,
1269+
/*AllowInvariantGroup=*/false, /*ExternalAnalysis=*/nullptr,
1270+
/*LookThroughIntToPtr*/ IsEqPred);
12671271
if (Stripped0 == Stripped1)
12681272
return ConstantInt::getBool(
12691273
Ops0->getContext(),

llvm/lib/IR/Value.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ const Value *Value::stripPointerCastsForAliasAnalysis() const {
714714
const Value *Value::stripAndAccumulateConstantOffsets(
715715
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
716716
bool AllowInvariantGroup,
717-
function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
717+
function_ref<bool(Value &, APInt &)> ExternalAnalysis,
718+
bool LookThroughIntToPtr) const {
718719
if (!getType()->isPtrOrPtrVectorTy())
719720
return this;
720721

@@ -777,22 +778,22 @@ const Value *Value::stripAndAccumulateConstantOffsets(
777778
V = Call->getArgOperand(0);
778779
} else if (auto *Int2Ptr = dyn_cast<Operator>(V)) {
779780
// Try to accumulate across (inttoptr (add (ptrtoint p), off)).
780-
if (!Int2Ptr || Int2Ptr->getOpcode() != Instruction::IntToPtr ||
781+
if (!AllowNonInbounds || !LookThroughIntToPtr || !Int2Ptr ||
782+
Int2Ptr->getOpcode() != Instruction::IntToPtr ||
781783
Int2Ptr->getOperand(0)->getType()->getScalarSizeInBits() != BitWidth)
782784
return V;
785+
783786
auto *Add = dyn_cast<AddOperator>(Int2Ptr->getOperand(0));
784-
if (!AllowNonInbounds || !Add)
787+
if (!Add)
785788
return V;
789+
786790
auto *Ptr2Int = dyn_cast<PtrToIntOperator>(Add->getOperand(0));
787791
auto *CI = dyn_cast<ConstantInt>(Add->getOperand(1));
788792
if (!Ptr2Int || !CI)
789793
return V;
790794

791-
APInt AddOffset = CI->getValue();
792-
if (AddOffset.getSignificantBits() > BitWidth)
793-
return V;
794-
795-
Offset = AddOffset.sextOrTrunc(BitWidth);
795+
const APInt &AddOffset = CI->getValue();
796+
Offset += AddOffset;
796797
V = Ptr2Int->getOperand(0);
797798
}
798799
assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ define i1 @known_constexpr_add_eq() {
1313

1414
define i1 @known_constexpr_add_eq_ops_swapped() {
1515
; CHECK-LABEL: define i1 @known_constexpr_add_eq_ops_swapped() {
16-
; 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)
17-
; CHECK-NEXT: ret i1 [[COND]]
16+
; CHECK-NEXT: ret i1 false
1817
;
1918
%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)
2019
ret i1 %cond
@@ -96,3 +95,20 @@ define ptr @return_inttoptr() {
9695
;
9796
ret ptr inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 8) to ptr)
9897
}
98+
99+
define i1 @known_constexpr_add_nested_1() {
100+
; CHECK-LABEL: define i1 @known_constexpr_add_nested_1() {
101+
; CHECK-NEXT: ret i1 true
102+
;
103+
%cond = icmp eq ptr @glob, inttoptr (i64 add (i64 ptrtoint (ptr getelementptr inbounds nuw (i8, ptr @glob, i64 80) to i64), i64 -80) to ptr)
104+
ret i1 %cond
105+
}
106+
107+
define i1 @known_constexpr_add_nested_2() {
108+
; CHECK-LABEL: define i1 @known_constexpr_add_nested_2() {
109+
; CHECK-NEXT: ret i1 true
110+
;
111+
;%cond = icmp eq ptr @glob, ptr getelementptr inbounds nuw (i8, ptr inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 -80) to ptr), i64 80)
112+
%cond = icmp eq ptr @glob, getelementptr inbounds nuw (i8, ptr inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 -80) to ptr), i64 80)
113+
ret i1 %cond
114+
}

0 commit comments

Comments
 (0)