Skip to content

[Value] Look through inttoptr (add ..) in accumulateConstantOffsets #124981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions llvm/include/llvm/IR/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@ class Value {
/// For example, for a value \p ExternalAnalysis might try to calculate a
/// lower bound. If \p ExternalAnalysis is successful, it should return true.
///
/// If \p LookThroughIntToPtr is true then this method also looks through
/// IntToPtr and PtrToInt constant expressions. The returned pointer may not
/// have the same provenance as this value.
///
/// If this is called on a non-pointer value, it returns 'this' and the
/// \p Offset is not modified.
///
Expand All @@ -722,17 +726,19 @@ class Value {
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
bool AllowInvariantGroup = false,
function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis =
nullptr) const;
nullptr,
bool LookThroughIntToPtr = false) const;

Value *stripAndAccumulateConstantOffsets(
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
bool AllowInvariantGroup = false,
function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis =
nullptr) {
nullptr,
bool LookThroughIntToPtr = false) {
return const_cast<Value *>(
static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets(
DL, Offset, AllowNonInbounds, AllowInvariantGroup,
ExternalAnalysis));
DL, Offset, AllowNonInbounds, AllowInvariantGroup, ExternalAnalysis,
LookThroughIntToPtr));
}

/// This is a wrapper around stripAndAccumulateConstantOffsets with the
Expand Down
13 changes: 9 additions & 4 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,11 +1258,16 @@ Constant *llvm::ConstantFoldCompareInstOperands(
if (Ops0->getType()->isPointerTy() && !ICmpInst::isSigned(Predicate)) {
unsigned IndexWidth = DL.getIndexTypeSizeInBits(Ops0->getType());
APInt Offset0(IndexWidth, 0);
Value *Stripped0 =
Ops0->stripAndAccumulateInBoundsConstantOffsets(DL, Offset0);
bool IsEqPred = ICmpInst::isEquality(Predicate);
Value *Stripped0 = Ops0->stripAndAccumulateConstantOffsets(
DL, Offset0, /*AllowNonInbounds=*/IsEqPred,
/*AllowInvariantGroup=*/false, /*ExternalAnalysis=*/nullptr,
/*LookThroughIntToPtr=*/IsEqPred);
APInt Offset1(IndexWidth, 0);
Value *Stripped1 =
Ops1->stripAndAccumulateInBoundsConstantOffsets(DL, Offset1);
Value *Stripped1 = Ops1->stripAndAccumulateConstantOffsets(
DL, Offset1, /*AllowNonInbounds=*/IsEqPred,
/*AllowInvariantGroup=*/false, /*ExternalAnalysis=*/nullptr,
/*LookThroughIntToPtr=*/IsEqPred);
if (Stripped0 == Stripped1)
return ConstantInt::getBool(
Ops0->getContext(),
Expand Down
21 changes: 20 additions & 1 deletion llvm/lib/IR/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,8 @@ const Value *Value::stripPointerCastsForAliasAnalysis() const {
const Value *Value::stripAndAccumulateConstantOffsets(
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
bool AllowInvariantGroup,
function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
function_ref<bool(Value &, APInt &)> ExternalAnalysis,
bool LookThroughIntToPtr) const {
if (!getType()->isPtrOrPtrVectorTy())
return this;

Expand Down Expand Up @@ -775,6 +776,24 @@ const Value *Value::stripAndAccumulateConstantOffsets(
V = RV;
if (AllowInvariantGroup && Call->isLaunderOrStripInvariantGroup())
V = Call->getArgOperand(0);
} else if (auto *Int2Ptr = dyn_cast<Operator>(V)) {
// Try to accumulate across (inttoptr (add (ptrtoint p), off)).
if (!AllowNonInbounds || !LookThroughIntToPtr || !Int2Ptr ||
Int2Ptr->getOpcode() != Instruction::IntToPtr ||
Int2Ptr->getOperand(0)->getType()->getScalarSizeInBits() != BitWidth)
return V;

auto *Add = dyn_cast<AddOperator>(Int2Ptr->getOperand(0));
if (!Add)
return V;

auto *Ptr2Int = dyn_cast<PtrToIntOperator>(Add->getOperand(0));
auto *CI = dyn_cast<ConstantInt>(Add->getOperand(1));
if (!Ptr2Int || !CI)
return V;

Offset += CI->getValue();
V = Ptr2Int->getOperand(0);
}
assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
} while (Visited.insert(V).second);
Expand Down
33 changes: 23 additions & 10 deletions llvm/test/Transforms/InstSimplify/constant-fold-inttoptr-add.ll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should either use -p instsimplify or be in the InstCombine directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I had it in instcombine, then moved but. not updated the run-line should be fixed now thanks

Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -p instcombine -S %s | FileCheck %s
; RUN: opt -p instsimplify -S %s | FileCheck %s

@glob = external global [314 x i64]

define i1 @known_constexpr_add_eq() {
; CHECK-LABEL: define i1 @known_constexpr_add_eq() {
; 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)
; CHECK-NEXT: ret i1 [[COND]]
; CHECK-NEXT: ret i1 false
;
%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)
ret i1 %cond
}

define i1 @known_constexpr_add_eq_ops_swapped() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get what the difference between this test and the previous is. What's swapped here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted the name but not the code... fixed now, thanks

; CHECK-LABEL: define i1 @known_constexpr_add_eq_ops_swapped() {
; 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)
; CHECK-NEXT: ret i1 [[COND]]
; CHECK-NEXT: ret i1 false
;
%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)
%cond = icmp eq ptr inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 -80) to ptr), getelementptr inbounds nuw (i8, ptr @glob, i64 80)
ret i1 %cond
}

define i1 @known_constexpr_add_ne() {
; CHECK-LABEL: define i1 @known_constexpr_add_ne() {
; 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)
; CHECK-NEXT: ret i1 [[COND]]
; CHECK-NEXT: ret i1 true
;
%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)
ret i1 %cond
Expand All @@ -41,8 +38,7 @@ define i1 @wrap_positive_to_negate() {
; 9223372036854775808 = 2^63
define i1 @wrap_positive_to_zero() {
; CHECK-LABEL: define i1 @wrap_positive_to_zero() {
; 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)
; CHECK-NEXT: ret i1 [[COND]]
; CHECK-NEXT: ret i1 true
;
%cond = icmp eq ptr @glob, inttoptr (i64 add (i64 ptrtoint (ptr getelementptr nuw (i8, ptr @glob, i64 9223372036854775808)to i64), i64 9223372036854775808) to ptr)
ret i1 %cond
Expand Down Expand Up @@ -99,3 +95,20 @@ define ptr @return_inttoptr() {
;
ret ptr inttoptr (i64 add (i64 ptrtoint (ptr @glob to i64), i64 8) to ptr)
}

define i1 @known_constexpr_add_nested_1() {
; CHECK-LABEL: define i1 @known_constexpr_add_nested_1() {
; CHECK-NEXT: ret i1 true
;
%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)
ret i1 %cond
}

define i1 @known_constexpr_add_nested_2() {
; CHECK-LABEL: define i1 @known_constexpr_add_nested_2() {
; CHECK-NEXT: ret i1 true
;
;%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)
%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)
ret i1 %cond
}