Skip to content
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

[InstCombine] Remove transformation on call instruction where return value need void to non-void conversion #98536

Merged
merged 6 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 9 additions & 17 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4071,9 +4071,7 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
if (Callee->isDeclaration())
return false; // Cannot transform this return value.

if (!Caller->use_empty() &&
// void -> non-void is handled specially
!NewRetTy->isVoidTy())
if (!Caller->use_empty())
return false; // Cannot transform this return value.
}

Expand Down Expand Up @@ -4233,9 +4231,6 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {

AttributeSet FnAttrs = CallerPAL.getFnAttrs();

if (NewRetTy->isVoidTy())
Caller->setName(""); // Void type should not have a name.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this bit might still be needed? Can you please add another function to your test that does %res = call i32 @foo() but does not use the result? Just want to make sure this doesn't hit some void name assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this would necessary now after we have tightened the condition above to skip transformation only when return value has uses.


assert((ArgAttrs.size() == FT->getNumParams() || FT->isVarArg()) &&
"missing argument attributes");
AttributeList NewCallerPAL = AttributeList::get(
Expand Down Expand Up @@ -4264,17 +4259,14 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
Instruction *NC = NewCall;
Value *NV = NC;
if (OldRetTy != NV->getType() && !Caller->use_empty()) {
if (!NV->getType()->isVoidTy()) {
NV = NC = CastInst::CreateBitOrPointerCast(NC, OldRetTy);
NC->setDebugLoc(Caller->getDebugLoc());

auto OptInsertPt = NewCall->getInsertionPointAfterDef();
assert(OptInsertPt && "No place to insert cast");
InsertNewInstBefore(NC, *OptInsertPt);
Worklist.pushUsersToWorkList(*Caller);
} else {
NV = PoisonValue::get(Caller->getType());
}
assert(!NV->getType()->isVoidTy());
NV = NC = CastInst::CreateBitOrPointerCast(NC, OldRetTy);
NC->setDebugLoc(Caller->getDebugLoc());

auto OptInsertPt = NewCall->getInsertionPointAfterDef();
assert(OptInsertPt && "No place to insert cast");
InsertNewInstBefore(NC, *OptInsertPt);
Worklist.pushUsersToWorkList(*Caller);
}

if (!Caller->use_empty())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt --passes=instcombine -S < %s | FileCheck %s

define dso_local void @foo() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Drop dso_local.

; CHECK-LABEL: define dso_local void @foo() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: ret void
;
entry:
ret void
}

define dso_local i32 @bar() {
; CHECK-LABEL: define dso_local i32 @bar() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[TMP0:%.*]] = tail call i32 @foo()
; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[TMP0]], 1
; CHECK-NEXT: ret i32 [[TMP1]]
;
entry:
%1 = tail call i32 @foo()
%2 = add i32 %1, 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Can drop the add too :)

ret i32 %2
}
Loading