Skip to content

redundant second cast to integer after already casted int value in asm.js? #2951

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
MaxGraey opened this issue Jul 10, 2020 · 1 comment · May be fixed by #4078
Open

redundant second cast to integer after already casted int value in asm.js? #2951

MaxGraey opened this issue Jul 10, 2020 · 1 comment · May be fixed by #4078

Comments

@MaxGraey
Copy link
Contributor

MaxGraey commented Jul 10, 2020

https://webassembly.studio/?f=oeg02m22gmq

export function add_div(x: i32): i32 {
  return u32(x) / 100 + x / -100;
}

output:

 function main_add_div($0) {
  $0 = $0 | 0;
  return (($0 | 0) / (-100 | 0) | 0) + (($0 >>> 0) / (100 >>> 0) | 0) | 0 | 0;
 }

but I'm expecting single | 0 at the end:

 function main_add_div($0) {
  $0 = $0 | 0;
  return (($0 | 0) / (-100 | 0) | 0) + (($0 >>> 0) / (100 >>> 0) | 0) | 0;
 }

similar here
but after extra optimizations it normalized to expected. But I guess it could be done earlier without extra opts?

also since asm2js is more like js code and less than asm.js could we simplify some conversion? Like this one:

function i32_trunc_u_f32($0) {
  $0 = Math_fround($0);
  return ~~$0 >>> 0 | 0;
}

could be just:

function i32_trunc_u_f32($0) {
  $0 = Math_fround($0);
  return $0 | 0;
}

And Closure Compiler can't do such optimizations btw

@kripken
Copy link
Member

kripken commented Jul 10, 2020

Could be a missing optimization. Those are written in src/tools/wasm2js.cpp:optimizeJS().

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

Successfully merging a pull request may close this issue.

2 participants