@@ -682,39 +682,32 @@ bool RISCVDAGToDAGISel::trySignedBitfieldInsertInMask(SDNode *Node) {
682682 if (!Subtarget->hasVendorXqcibm ())
683683 return false ;
684684
685- auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand (1 ));
686- if (!N1C)
687- return false ;
685+ using namespace SDPatternMatch ;
688686
689- int32_t C1 = N1C->getSExtValue ();
690- if (!isShiftedMask_32 (C1) || isInt<12 >(C1))
687+ SDValue X;
688+ APInt MaskImm;
689+ if (!sd_match (Node, m_Or (m_OneUse (m_Value (X)), m_ConstInt (MaskImm))))
691690 return false ;
692691
693- // INSBI will clobber the input register in N0. Bail out if we need a copy to
694- // preserve this value.
695- SDValue N0 = Node->getOperand (0 );
696- if (!N0.hasOneUse ())
692+ unsigned ShAmt, Width;
693+ if (!MaskImm.isShiftedMask (ShAmt, Width) || MaskImm.isSignedIntN (12 ))
697694 return false ;
698695
699- // If C1 is a shifted mask (but can't be formed as an ORI),
700- // use a bitfield insert of -1.
701- // Transform (or x, C1)
702- // -> (qc.insbi x, -1, width, shift)
703- const unsigned Leading = llvm::countl_zero ((uint32_t )C1);
704- const unsigned Trailing = llvm::countr_zero ((uint32_t )C1);
705- const unsigned Width = 32 - Leading - Trailing;
706-
707696 // If Zbs is enabled and it is a single bit set we can use BSETI which
708697 // can be compressed to C_BSETI when Xqcibm in enabled.
709698 if (Width == 1 && Subtarget->hasStdExtZbs ())
710699 return false ;
711700
701+ // If C1 is a shifted mask (but can't be formed as an ORI),
702+ // use a bitfield insert of -1.
703+ // Transform (or x, C1)
704+ // -> (qc.insbi x, -1, width, shift)
712705 SDLoc DL (Node);
713706 MVT VT = Node->getSimpleValueType (0 );
714707
715- SDValue Ops[] = {N0 , CurDAG->getSignedTargetConstant (-1 , DL, VT),
708+ SDValue Ops[] = {X , CurDAG->getSignedTargetConstant (-1 , DL, VT),
716709 CurDAG->getTargetConstant (Width, DL, VT),
717- CurDAG->getTargetConstant (Trailing , DL, VT)};
710+ CurDAG->getTargetConstant (ShAmt , DL, VT)};
718711 SDNode *BitIns = CurDAG->getMachineNode (RISCV::QC_INSBI, DL, VT, Ops);
719712 ReplaceNode (Node, BitIns);
720713 return true ;
0 commit comments