LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 49543 - [InstCombine] Failure to fold sext(trunc(ashr(x,bw-1)) -> sext(ashr(x,bw-1)
Summary: [InstCombine] Failure to fold sext(trunc(ashr(x,bw-1)) -> sext(ashr(x,bw-1)
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: PC Windows NT
: P enhancement
Assignee: Sanjay Patel
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-03-11 10:17 PST by Simon Pilgrim
Modified: 2021-06-04 00:33 PDT (History)
4 users (show)

See Also:
Fixed By Commit(s): b865eead76577b031c6fae5e2490e7be0073f201


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Pilgrim 2021-03-11 10:17:46 PST
Alive2: https://alive2.llvm.org/ce/z/A3FMem

define i64 @src(i32 %a0) {
  %a = ashr i32 %a0, 16 ; all valid 16 -> 31
  %b = trunc i32 %a to i16
  %c = sext i16 %b to i64
  ret i64 %c
}

define i64 @tgt(i32 %a0) {
  %a = ashr i32 %a0, 16
  %c = sext i32 %a to i64
  ret i64 %c
}

https://c.godbolt.org/z/zh7ffM
Comment 1 Roman Lebedev 2021-03-11 10:27:56 PST
Same is true for lshr:
https://alive2.llvm.org/ce/z/NFowLr
Comment 2 Roman Lebedev 2021-03-11 10:33:49 PST
Err, submitted too soon

Same is true for lshr:
https://alive2.llvm.org/ce/z/NFowLr
https://c.godbolt.org/z/7q545n

I think we could use ComputeNumSignBits() here.
I'll try to take a look..


Same for zext, but we get that already: https://c.godbolt.org/z/6GqxTM
https://alive2.llvm.org/ce/z/289tfg
https://alive2.llvm.org/ce/z/GWh-uX
Comment 3 Simon Pilgrim 2021-04-13 04:32:57 PDT
@lebedev.ri Are you still looking at this?
Comment 4 Roman Lebedev 2021-04-13 10:18:56 PDT
(In reply to Simon Pilgrim from comment #3)
> @lebedev.ri Are you still looking at this?

Not really. If someone wants to pick this up, feel free to.
There is a number of patches needed here:
* general computenumsignbits fold
* fold for that specific IR, to handle non-splat vectors
* fold for the case where the first op is lshr
* ???
* do we need something similar for zero-extension?
Comment 5 Sanjay Patel 2021-06-01 13:10:11 PDT
Looking at the ComputeNumSignBits pattern - tests:
https://reviews.llvm.org/rG337854270023
Comment 6 Sanjay Patel 2021-06-03 07:36:46 PDT
https://reviews.llvm.org/D103617
Comment 7 Sanjay Patel 2021-06-03 11:27:28 PDT
Filed the 'lshr' pattern as bug 50575.
The example pasted in the description is transformed into that (ashr->lshr). 

But the bug title and the example in the godbolt link in the description are different and should be fixed with:
https://reviews.llvm.org/rGb865eead7657

So I think it's ok to close this one, but feel free to reopen or file new bugs to track other items.
Comment 8 Simon Pilgrim 2021-06-04 00:33:13 PDT
Cheers Sanjay!