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 50575 - [InstCombine] remove unnecessary cast op
Summary: [InstCombine] remove unnecessary cast op
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: PC All
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-06-03 11:05 PDT by Sanjay Patel
Modified: 2021-06-04 04:23 PDT (History)
3 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sanjay Patel 2021-06-03 11:05:19 PDT
Forking this off from bug 49543 - we can eliminate an intermediate cast in a sequence like this by using 'ashr' to replicate the signbit: 

define i64 @src(i32 %a0) {
  %a = lshr i32 %a0, 24
  %b = trunc i32 %a to i8
  %c = sext i8 %b to i64
  ret i64 %c
}

define i64 @tgt(i32 %a0) {
  %a = ashr i32 %a0, 24
  %c = sext i32 %a to i64
  ret i64 %c
}
Comment 1 Sanjay Patel 2021-06-03 11:23:36 PDT
We should also handle the case where the initial width is bigger than the destination:

define i26 @src(i32 %a0) {
  %a = lshr i32 %a0, 24
  %b = trunc i32 %a to i8
  %c = sext i8 %b to i26
  ret i26 %c
}

Instead of a sext, we need to trunc:

define i26 @tgt(i32 %a0) {
  %a = ashr i32 %a0, 24
  %c = trunc i32 %a to i26
  ret i26 %c
}

We already handle the pattern where initial width == dest width by converting to shifts (lshr cancels out shl in that case).
Comment 2 Roman Lebedev 2021-06-03 11:36:26 PDT
(In reply to Sanjay Patel from comment #1)
> <...>

Also see https://reviews.llvm.org/rG41b71f718b94c6f12bbaa670e97cabb070308ed2
Comment 3 Sanjay Patel 2021-06-04 04:23:51 PDT
(In reply to Roman Lebedev from comment #2)
> Also see https://reviews.llvm.org/rG41b71f718b94c6f12bbaa670e97cabb070308ed2

Thanks! That goes a bit further than what I had drafted, so I pushed extra tests and a smaller patch as a first step:
https://reviews.llvm.org/rG8937450e8581
https://reviews.llvm.org/rG23a116c8c446

Hopefully, the basic cases here are fixed now.

I left TODO comments on the tests for the remaining part.