Skip to content

[InlineCost] Simplify extractvalue across callsite #145054

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions llvm/lib/Analysis/InlineCost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2316,9 +2316,18 @@ bool CallAnalyzer::visitStore(StoreInst &I) {
}

bool CallAnalyzer::visitExtractValue(ExtractValueInst &I) {
// Constant folding for extract value is trivial.
if (simplifyInstruction(I))
return true;
Value *Op = I.getAggregateOperand();

// Special handling, because we want to simplify extractvalue with a
// potential insertvalue from the caller.
if (Value *SimpleOp = getSimplifiedValueUnchecked(Op)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to split off the change from constant -> constant and then improve extract value handling in a separate pqtch

SimplifyQuery SQ(DL);
Value *SimpleV = simplifyExtractValueInst(SimpleOp, I.getIndices(), SQ);
if (SimpleV) {
SimplifiedValues[&I] = SimpleV;
return true;
}
}

// SROA can't look through these, but they may be free.
return Base::visitExtractValue(I);
Expand Down
53 changes: 53 additions & 0 deletions llvm/test/Transforms/Inline/simplify-crosscallsite.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 also passes without your change. You could use a recursive call to prove that the recursive code path is eliminated. Alternative could check debug output.

Copy link
Contributor Author

@tobias-stadler tobias-stadler Jun 23, 2025

Choose a reason for hiding this comment

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

Oops, x86 has lower cost than AArch64 here. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please also add a negative test where extractvalue does not simplify (e.g. same as current but swap 0 and 1).

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -S -passes=inline | FileCheck %s

define i32 @callee([2 x i32] %agg) {
; CHECK-LABEL: define i32 @callee(
; CHECK-SAME: [2 x i32] [[AGG:%.*]]) {
; CHECK-NEXT: [[V:%.*]] = extractvalue [2 x i32] [[AGG]], 0
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[V]], 0
; CHECK-NEXT: br i1 [[C]], label %[[IS_NULL:.*]], label %[[NON_NULL:.*]]
; CHECK: [[IS_NULL]]:
; CHECK-NEXT: ret i32 0
; CHECK: [[NON_NULL]]:
; CHECK-NEXT: [[R:%.*]] = call i32 @callee([2 x i32] [[AGG]])
; CHECK-NEXT: ret i32 [[R]]
;
%v = extractvalue [2 x i32] %agg, 0
%c = icmp eq i32 %v, 0
br i1 %c, label %is_null, label %non_null

is_null:
ret i32 0

non_null:
%r = call i32 @callee([2 x i32] %agg)
ret i32 %r
}

define i32 @caller_simplified(i32 %arg) {
; CHECK-LABEL: define i32 @caller_simplified(
; CHECK-SAME: i32 [[ARG:%.*]]) {
; CHECK-NEXT: [[AGG0:%.*]] = insertvalue [2 x i32] poison, i32 0, 0
; CHECK-NEXT: [[AGG1:%.*]] = insertvalue [2 x i32] [[AGG0]], i32 [[ARG]], 1
; CHECK-NEXT: ret i32 0
;
%agg0 = insertvalue [2 x i32] poison, i32 0, 0
%agg1 = insertvalue [2 x i32] %agg0, i32 %arg, 1
%v = call i32 @callee([2 x i32] %agg1)
ret i32 %v
}

define i32 @caller_not_simplified(i32 %arg) {
; CHECK-LABEL: define i32 @caller_not_simplified(
; CHECK-SAME: i32 [[ARG:%.*]]) {
; CHECK-NEXT: [[AGG0:%.*]] = insertvalue [2 x i32] poison, i32 1, 0
; CHECK-NEXT: [[AGG1:%.*]] = insertvalue [2 x i32] [[AGG0]], i32 [[ARG]], 1
; CHECK-NEXT: [[V:%.*]] = call i32 @callee([2 x i32] [[AGG1]])
; CHECK-NEXT: ret i32 [[V]]
;
%agg0 = insertvalue [2 x i32] poison, i32 1, 0
%agg1 = insertvalue [2 x i32] %agg0, i32 %arg, 1
%v = call i32 @callee([2 x i32] %agg1)
ret i32 %v
}
Loading