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

nuw being lost when optimizing sub to add leads to worse codegen #53377

Open
JakobDegen opened this issue Jan 24, 2022 · 1 comment
Open

nuw being lost when optimizing sub to add leads to worse codegen #53377

JakobDegen opened this issue Jan 24, 2022 · 1 comment

Comments

@JakobDegen
Copy link

JakobDegen commented Jan 24, 2022

See the problem on godbolt. In this code:

define i64 @fast(i8* align 1) unnamed_addr #0 {
  %2 = load i8, i8* %0, align 1
  %3 = sub nuw i8 %2, 10
  %4 = zext i8 %3 to i64
  ret i64 %4
}

running through opt causes the sub to be switched out for

%3 = add i8 %2, -10

Note that the nuw flag was lost. This causes the compiled assembly to include an extra instruction. Originally found in rust-lang/rust#91449 .

Alive2 actually indicates that the nuw flag would be incorrect here, which makes sense since -10 is secretly just 246. Is there another way to address this? I can close this issue if the only reasonable response is "yeah, canonicalization requires us to do this, and sometimes that causes problems. Sorry"

@JakobDegen JakobDegen changed the title nuw flag is lost when optimizing sub to add nuw being lost when optimizing sub to add leads to worse codegen Jan 24, 2022
@nikic
Copy link
Contributor

nikic commented Jan 24, 2022

Yeah, this is a known problem. I've hit this issue many times.

The only generic way to address this that I see is to add a new snuw (or so) flag that basically say that it's nuw after converting add x, y into sub x, -y.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants