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

Failure to optimize __builtin_mul_overflow pattern #48113

Closed
GabrielRavier opened this issue Jan 16, 2021 · 6 comments
Closed

Failure to optimize __builtin_mul_overflow pattern #48113

GabrielRavier opened this issue Jan 16, 2021 · 6 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla

Comments

@GabrielRavier
Copy link
Contributor

Bugzilla Link 48769
Resolution FIXED
Resolved on Sep 07, 2021 11:04
Version trunk
OS Linux
Depends On #25679
CC @LebedevRI,@RKSimon,@nickdesaulniers,@nikic,@rotateright
Fixed by commit(s) 13ec913, 35fa7b8

Extended Description

int f1(unsigned x, unsigned y)
{
unsigned int r = x * y;
return x && ((int)r / (int)x) != (int)y;
}

This can be optimized to :

int f2(unsigned x, unsigned y)
{
int res;
return __builtin_mul_overflow((int)x, (int)y, &res);
}

This optimization is done by GCC, but not by LLVM.

See also comparison here : https://godbolt.org/z/f9Yv9c

@GabrielRavier
Copy link
Contributor Author

assigned to @LebedevRI

@RKSimon
Copy link
Collaborator

RKSimon commented Apr 14, 2021

Alive2: https://alive2.llvm.org/ce/z/gi2rzw


define i1 @​src(i8 %0, i8 %1) {
%2:
%3 = mul i8 %1, %0
%4 = sdiv i8 %3, %0
%5 = icmp ne i8 %4, %1
ret i1 %5
}
=>
define i1 @​tgt(i8 %0, i8 %1) {
%2:
%3 = smul_overflow {i8, i1} %0, %1
%4 = extractvalue {i8, i1} %3, 1
ret i1 %4
}
Transformation seems to be correct!


define i1 @​src(i8 %0, i8 %1) {
%2:
%3 = mul i8 %1, %0
%4 = udiv i8 %3, %0
%5 = icmp ne i8 %4, %1
ret i1 %5
}
=>
define i1 @​tgt(i8 %0, i8 %1) {
%2:
%3 = umul_overflow {i8, i1} %0, %1
%4 = extractvalue {i8, i1} %3, 1
ret i1 %4
}
Transformation seems to be correct!

@LebedevRI
Copy link
Member

Fixed in 13ec913, thanks.

@RKSimon
Copy link
Collaborator

RKSimon commented May 9, 2021

Fixed in 13ec913, thanks.

Reverted at 91f7a4f

@nickdesaulniers
Copy link
Member

Does https://reviews.llvm.org/D108928 help this get re-landed?

@LebedevRI
Copy link
Member

Relanded in 35fa7b8.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla
Projects
None yet
Development

No branches or pull requests

4 participants