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] reduce expanded form of integer square-of-sums #61296

Closed
rotateright opened this issue Mar 9, 2023 · 2 comments
Closed

[InstCombine] reduce expanded form of integer square-of-sums #61296

rotateright opened this issue Mar 9, 2023 · 2 comments

Comments

@rotateright
Copy link
Contributor

; square-of-sum may be expanded in source/IR:
; x^2 + 2xy + y^2
; (x * x) + ((2 * x) + y) * y
define i6 @src(i6 %x, i6 %y) {
  %xx = mul i6 %x, %x
  %x2 = shl i6 %x, 1
  %x2y = add i6 %x2, %y
  %x2yy = mul i6 %x2y, %y
  %r = add i6 %x2yy, %xx
  ret i6 %r
}

; This can be reduced 
; (unless all of the intermediate values have extra uses?):
; (x + y)^2
define i6 @tgt(i6 %x, i6 %y) {
  %xy = add i6 %y, %x
  %r = mul i6 %xy, %xy
  ret i6 %r
}

https://alive2.llvm.org/ce/z/TkwEKJ

Note that no-wrap (nsw, nuw) can be propagated to the resulting math if (some subset of?) the original code has no-wrap. Also, there are probably 8 commuted variations of the last 3 math ops.

@rainerzufalldererste
Copy link
Contributor

https://reviews.llvm.org/D156026
Doesn't have any tests yet, but does the InstCombine for int & float

@rainerzufalldererste
Copy link
Contributor

Integer Version has been committed. Float: https://reviews.llvm.org/D158079

avillega pushed a commit to avillega/llvm-project that referenced this issue Sep 11, 2023
qihangkong pushed a commit to rvgpu/llvm that referenced this issue Apr 18, 2024
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