Skip to content

[IPSCCP] Intersect attribute info for interprocedural args #106397

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 1 commit into from
Aug 29, 2024
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
26 changes: 15 additions & 11 deletions llvm/lib/Transforms/Utils/SCCPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,19 +820,21 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
markOverdefined(ValueState[V], V);
}

void trackValueOfArgument(Argument *A) {
ValueLatticeElement getArgAttributeVL(Argument *A) {
if (A->getType()->isIntOrIntVectorTy()) {
if (std::optional<ConstantRange> Range = A->getRange()) {
markConstantRange(ValueState[A], A, *Range);
return;
}
}
if (A->hasNonNullAttr()) {
markNotNull(ValueState[A], A);
return;
if (std::optional<ConstantRange> Range = A->getRange())
return ValueLatticeElement::getRange(*Range);
}
if (A->hasNonNullAttr())
return ValueLatticeElement::getNot(Constant::getNullValue(A->getType()));
// Assume nothing about the incoming arguments without attributes.
markOverdefined(A);
return ValueLatticeElement::getOverdefined();
}

void trackValueOfArgument(Argument *A) {
if (A->getType()->isStructTy())
return (void)markOverdefined(A);
mergeInValue(A, getArgAttributeVL(A));
}

bool isStructLatticeConstant(Function *F, StructType *STy);
Expand Down Expand Up @@ -1800,7 +1802,9 @@ void SCCPInstVisitor::handleCallArguments(CallBase &CB) {
getMaxWidenStepsOpts());
}
} else
mergeInValue(&*AI, getValueState(*CAI), getMaxWidenStepsOpts());
mergeInValue(&*AI,
getValueState(*CAI).intersect(getArgAttributeVL(&*AI)),
getMaxWidenStepsOpts());
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions llvm/test/Transforms/SCCP/pointer-nonnull.ll
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,22 @@ define internal i1 @ip_test_nonnull_callee(ptr nonnull %p) {
;
; IPSCCP-LABEL: define internal i1 @ip_test_nonnull_callee(
; IPSCCP-SAME: ptr nonnull [[P:%.*]]) {
; IPSCCP-NEXT: [[CMP:%.*]] = icmp ne ptr [[P]], null
; IPSCCP-NEXT: ret i1 [[CMP]]
; IPSCCP-NEXT: ret i1 poison
Copy link
Member

Choose a reason for hiding this comment

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

Is it correct? Shouldn't it be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because IPSCCP replaces the the use of the return value in the caller, so the return value in the callee becomes unused and is replaced with poison.

Copy link
Contributor

Choose a reason for hiding this comment

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

For IPSCCP, returned value isn't used any longer (at call sites we replaced by true)

;
%cmp = icmp ne ptr %p, null
ret i1 %cmp
}

define i1 @ip_test_nonnull_caller(ptr %p) {
; CHECK-LABEL: define i1 @ip_test_nonnull_caller(
; CHECK-SAME: ptr [[P:%.*]]) {
; CHECK-NEXT: [[RES:%.*]] = call i1 @ip_test_nonnull_callee(ptr [[P]])
; CHECK-NEXT: ret i1 [[RES]]
; SCCP-LABEL: define i1 @ip_test_nonnull_caller(
; SCCP-SAME: ptr [[P:%.*]]) {
; SCCP-NEXT: [[RES:%.*]] = call i1 @ip_test_nonnull_callee(ptr [[P]])
; SCCP-NEXT: ret i1 [[RES]]
;
; IPSCCP-LABEL: define i1 @ip_test_nonnull_caller(
; IPSCCP-SAME: ptr [[P:%.*]]) {
; IPSCCP-NEXT: [[RES:%.*]] = call i1 @ip_test_nonnull_callee(ptr [[P]])
; IPSCCP-NEXT: ret i1 true
;
%res = call i1 @ip_test_nonnull_callee(ptr %p)
ret i1 %res
Expand Down
27 changes: 16 additions & 11 deletions llvm/test/Transforms/SCCP/range-attribute.ll
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ define i1 @ip_range_attribute_constant() {

define internal i1 @ip_cmp_attribute_overdefined_callee(i32 range(i32 0, 10) %x) {
; IPSCCP-LABEL: @ip_cmp_attribute_overdefined_callee(
; IPSCCP-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 10
; IPSCCP-NEXT: ret i1 [[CMP]]
; IPSCCP-NEXT: ret i1 poison
;
; SCCP-LABEL: @ip_cmp_attribute_overdefined_callee(
; SCCP-NEXT: ret i1 true
Expand All @@ -204,19 +203,21 @@ define internal i1 @ip_cmp_attribute_overdefined_callee(i32 range(i32 0, 10) %x)
}

define i1 @ip_cmp_attribute_overdefined_caller(i32 %x) {
; CHECK-LABEL: @ip_cmp_attribute_overdefined_caller(
; CHECK-NEXT: [[RES:%.*]] = call i1 @ip_cmp_attribute_overdefined_callee(i32 [[X:%.*]])
; CHECK-NEXT: ret i1 [[RES]]
; IPSCCP-LABEL: @ip_cmp_attribute_overdefined_caller(
; IPSCCP-NEXT: [[RES:%.*]] = call i1 @ip_cmp_attribute_overdefined_callee(i32 [[X:%.*]])
; IPSCCP-NEXT: ret i1 true
;
; SCCP-LABEL: @ip_cmp_attribute_overdefined_caller(
; SCCP-NEXT: [[RES:%.*]] = call i1 @ip_cmp_attribute_overdefined_callee(i32 [[X:%.*]])
; SCCP-NEXT: ret i1 [[RES]]
;
%res = call i1 @ip_cmp_attribute_overdefined_callee(i32 %x)
ret i1 %res
}

define internal i1 @ip_cmp_attribute_intersect_callee(i32 range(i32 0, 10) %x) {
; IPSCCP-LABEL: @ip_cmp_attribute_intersect_callee(
; IPSCCP-NEXT: [[CMP1:%.*]] = icmp ult i32 [[X:%.*]], 10
; IPSCCP-NEXT: [[AND:%.*]] = and i1 [[CMP1]], true
; IPSCCP-NEXT: ret i1 [[AND]]
; IPSCCP-NEXT: ret i1 poison
;
; SCCP-LABEL: @ip_cmp_attribute_intersect_callee(
; SCCP-NEXT: [[CMP2:%.*]] = icmp uge i32 [[X:%.*]], 5
Expand All @@ -230,9 +231,13 @@ define internal i1 @ip_cmp_attribute_intersect_callee(i32 range(i32 0, 10) %x) {
}

define i1 @ip_cmp_attribute_intersect_caller(i32 range(i32 5, 15) %x) {
; CHECK-LABEL: @ip_cmp_attribute_intersect_caller(
; CHECK-NEXT: [[RES:%.*]] = call i1 @ip_cmp_attribute_intersect_callee(i32 [[X:%.*]])
; CHECK-NEXT: ret i1 [[RES]]
; IPSCCP-LABEL: @ip_cmp_attribute_intersect_caller(
; IPSCCP-NEXT: [[RES:%.*]] = call i1 @ip_cmp_attribute_intersect_callee(i32 [[X:%.*]])
; IPSCCP-NEXT: ret i1 true
;
; SCCP-LABEL: @ip_cmp_attribute_intersect_caller(
; SCCP-NEXT: [[RES:%.*]] = call i1 @ip_cmp_attribute_intersect_callee(i32 [[X:%.*]])
; SCCP-NEXT: ret i1 [[RES]]
;
%res = call i1 @ip_cmp_attribute_intersect_callee(i32 %x)
ret i1 %res
Expand Down
Loading