Skip to content

Commit

Permalink
[InstCombine] remove trunc user restriction for match of bswap
Browse files Browse the repository at this point in the history
This does not appear to cause any problems, and it
fixes #50910

Extra tests with a trunc user were added with:
3a23937
...but they don't match either way, so there's an
opportunity to improve the matching further.
  • Loading branch information
rotateright committed Jan 5, 2022
1 parent 06cc2f2 commit e2165e0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 9 deletions.
6 changes: 1 addition & 5 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3147,11 +3147,6 @@ bool llvm::recognizeBSwapOrBitReverseIdiom(
if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() > 128)
return false; // Can't do integer/elements > 128 bits.

Type *DemandedTy = ITy;
if (I->hasOneUse())
if (auto *Trunc = dyn_cast<TruncInst>(I->user_back()))
DemandedTy = Trunc->getType();

// Try to find all the pieces corresponding to the bswap.
bool FoundRoot = false;
std::map<Value *, Optional<BitPart>> BPS;
Expand All @@ -3165,6 +3160,7 @@ bool llvm::recognizeBSwapOrBitReverseIdiom(
"Illegal bit provenance index");

// If the upper bits are zero, then attempt to perform as a truncated op.
Type *DemandedTy = ITy;
if (BitProvenance.back() == BitPart::Unset) {
while (!BitProvenance.empty() && BitProvenance.back() == BitPart::Unset)
BitProvenance = BitProvenance.drop_back();
Expand Down
5 changes: 1 addition & 4 deletions llvm/test/Transforms/InstCombine/bswap.ll
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,7 @@ declare i64 @llvm.bswap.i64(i64)

define i32 @PR50910(i64 %t0) {
; CHECK-LABEL: @PR50910(
; CHECK-NEXT: [[T2:%.*]] = and i64 [[T0:%.*]], 72057594037927935
; CHECK-NEXT: [[T3:%.*]] = call i64 @llvm.bswap.i64(i64 [[T2]])
; CHECK-NEXT: [[T4:%.*]] = lshr i64 [[T0]], 56
; CHECK-NEXT: [[T5:%.*]] = or i64 [[T3]], [[T4]]
; CHECK-NEXT: [[T5:%.*]] = call i64 @llvm.bswap.i64(i64 [[T0:%.*]])
; CHECK-NEXT: [[T6:%.*]] = trunc i64 [[T5]] to i32
; CHECK-NEXT: ret i32 [[T6]]
;
Expand Down

0 comments on commit e2165e0

Please sign in to comment.