Skip to content

Commit 46957a1

Browse files
committed
[InstCombine] Fix incorrect fshr to fshl transform
This transform is only valid if the (modular) shift amount is not zero. Proof: https://alive2.llvm.org/ce/z/WBxn-x Fixes #89338.
1 parent ab1d988 commit 46957a1

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,10 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
19881988
// is not entirely arbitrary. For historical reasons, the backend may
19891989
// recognize rotate left patterns but miss rotate right patterns.
19901990
if (IID == Intrinsic::fshr) {
1991-
// fshr X, Y, C --> fshl X, Y, (BitWidth - C)
1991+
// fshr X, Y, C --> fshl X, Y, (BitWidth - C) if C is not zero.
1992+
if (!isKnownNonZero(ShAmtC, SQ.getWithInstruction(II)))
1993+
return nullptr;
1994+
19921995
Constant *LeftShiftC = ConstantExpr::getSub(WidthC, ShAmtC);
19931996
Module *Mod = II->getModule();
19941997
Function *Fshl = Intrinsic::getDeclaration(Mod, Intrinsic::fshl, Ty);

llvm/test/Transforms/InstCombine/fsh.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,10 +1002,9 @@ define <2 x i32> @fsh_unary_shuffle_ops_partial_widening(<3 x i32> %x, <2 x i32>
10021002
ret <2 x i32> %r
10031003
}
10041004

1005-
; FIXME: This is a miscompile.
10061005
define <2 x i32> @fshr_vec_zero_elem(<2 x i32> %x, <2 x i32> %y) {
10071006
; CHECK-LABEL: @fshr_vec_zero_elem(
1008-
; CHECK-NEXT: [[FSH:%.*]] = call <2 x i32> @llvm.fshl.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]], <2 x i32> <i32 30, i32 0>)
1007+
; CHECK-NEXT: [[FSH:%.*]] = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]], <2 x i32> <i32 2, i32 0>)
10091008
; CHECK-NEXT: ret <2 x i32> [[FSH]]
10101009
;
10111010
%fsh = call <2 x i32> @llvm.fshr.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 2, i32 0>)

0 commit comments

Comments
 (0)