Skip to content

Commit a2300b4

Browse files
committed
[InstCombine] Do not simplify lshr/shl arg if it is part of fshl rotate pattern.
The fshl/fshr having first two arguments as same gets lowered to targets specific rotate. But based on the uses, one of the arguments can get simplified resulting in different arguments performing equivalent operation. This patch prevents the simplification of the arguments of lshr/shl if they are part of fshl pattern.
1 parent b896ff5 commit a2300b4

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,19 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
610610
DemandedMask, Known))
611611
return R;
612612

613+
// Do not simplify if shl is part of funnel-shift pattern
614+
if (I->hasOneUse()) {
615+
auto *Inst = dyn_cast<Instruction>(I->user_back());
616+
if (Inst && Inst->getOpcode() == BinaryOperator::Or) {
617+
if (auto Opt = convertShlOrLShrToFShlOrFShr(*Inst)) {
618+
auto [IID, FShiftArgs] = *Opt;
619+
if ((IID == Intrinsic::fshl || IID == Intrinsic::fshr) &&
620+
FShiftArgs[0] == FShiftArgs[1])
621+
return nullptr;
622+
}
623+
}
624+
}
625+
613626
// TODO: If we only want bits that already match the signbit then we don't
614627
// need to shift.
615628

@@ -670,6 +683,19 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
670683
if (match(I->getOperand(1), m_APInt(SA))) {
671684
uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
672685

686+
// Do not simplify if lshr is part of funnel-shift pattern
687+
if (I->hasOneUse()) {
688+
auto *Inst = dyn_cast<Instruction>(I->user_back());
689+
if (Inst && Inst->getOpcode() == BinaryOperator::Or) {
690+
if (auto Opt = convertShlOrLShrToFShlOrFShr(*Inst)) {
691+
auto [IID, FShiftArgs] = *Opt;
692+
if ((IID == Intrinsic::fshl || IID == Intrinsic::fshr) &&
693+
FShiftArgs[0] == FShiftArgs[1])
694+
return nullptr;
695+
}
696+
}
697+
}
698+
673699
// If we are just demanding the shifted sign bit and below, then this can
674700
// be treated as an ASHR in disguise.
675701
if (DemandedMask.countl_zero() >= ShiftAmt) {

llvm/test/Transforms/InstCombine/fsh.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ define i32 @fsh_rotate_5(i8 %x, i32 %y) {
726726
; CHECK-LABEL: @fsh_rotate_5(
727727
; CHECK-NEXT: [[T1:%.*]] = zext i8 [[X:%.*]] to i32
728728
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[T1]], [[Y:%.*]]
729-
; CHECK-NEXT: [[OR2:%.*]] = call i32 @llvm.fshl.i32(i32 [[OR1]], i32 [[Y]], i32 5)
729+
; CHECK-NEXT: [[OR2:%.*]] = call i32 @llvm.fshl.i32(i32 [[OR1]], i32 [[OR1]], i32 5)
730730
; CHECK-NEXT: ret i32 [[OR2]]
731731
;
732732

@@ -742,7 +742,7 @@ define i32 @fsh_rotate_18(i8 %x, i32 %y) {
742742
; CHECK-LABEL: @fsh_rotate_18(
743743
; CHECK-NEXT: [[T1:%.*]] = zext i8 [[X:%.*]] to i32
744744
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[T1]], [[Y:%.*]]
745-
; CHECK-NEXT: [[OR2:%.*]] = call i32 @llvm.fshl.i32(i32 [[OR1]], i32 [[Y]], i32 18)
745+
; CHECK-NEXT: [[OR2:%.*]] = call i32 @llvm.fshl.i32(i32 [[OR1]], i32 [[OR1]], i32 18)
746746
; CHECK-NEXT: ret i32 [[OR2]]
747747
;
748748

@@ -769,12 +769,12 @@ define i32 @fsh_load_rotate_12(ptr %data) {
769769
; CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr [[ARRAYIDX4]], align 1
770770
; CHECK-NEXT: [[CONV5:%.*]] = zext i8 [[TMP2]] to i32
771771
; CHECK-NEXT: [[SHL6:%.*]] = shl nuw nsw i32 [[CONV5]], 8
772+
; CHECK-NEXT: [[OR7:%.*]] = or i32 [[OR]], [[SHL6]]
772773
; CHECK-NEXT: [[ARRAYIDX8:%.*]] = getelementptr inbounds i8, ptr [[DATA]], i64 3
773774
; CHECK-NEXT: [[TMP3:%.*]] = load i8, ptr [[ARRAYIDX8]], align 1
774775
; CHECK-NEXT: [[CONV9:%.*]] = zext i8 [[TMP3]] to i32
775-
; CHECK-NEXT: [[TMP4:%.*]] = or i32 [[SHL6]], [[CONV9]]
776-
; CHECK-NEXT: [[OR10:%.*]] = or i32 [[TMP4]], [[SHL3]]
777-
; CHECK-NEXT: [[OR15:%.*]] = call i32 @llvm.fshl.i32(i32 [[OR10]], i32 [[OR]], i32 12)
776+
; CHECK-NEXT: [[OR10:%.*]] = or i32 [[OR7]], [[CONV9]]
777+
; CHECK-NEXT: [[OR15:%.*]] = call i32 @llvm.fshl.i32(i32 [[OR10]], i32 [[OR10]], i32 12)
778778
; CHECK-NEXT: ret i32 [[OR15]]
779779
;
780780

@@ -822,7 +822,7 @@ define i32 @fsh_load_rotate_25(ptr %data) {
822822
; CHECK-NEXT: [[TMP3:%.*]] = load i8, ptr [[ARRAYIDX8]], align 1
823823
; CHECK-NEXT: [[CONV9:%.*]] = zext i8 [[TMP3]] to i32
824824
; CHECK-NEXT: [[OR10:%.*]] = or i32 [[OR7]], [[CONV9]]
825-
; CHECK-NEXT: [[OR15:%.*]] = call i32 @llvm.fshl.i32(i32 [[CONV9]], i32 [[OR10]], i32 25)
825+
; CHECK-NEXT: [[OR15:%.*]] = call i32 @llvm.fshl.i32(i32 [[OR10]], i32 [[OR10]], i32 25)
826826
; CHECK-NEXT: ret i32 [[OR15]]
827827
;
828828

0 commit comments

Comments
 (0)