@@ -1068,6 +1068,8 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
10681068 setTargetDAGCombine (ISD::AND);
10691069 setTargetDAGCombine (ISD::OR);
10701070 setTargetDAGCombine (ISD::XOR);
1071+ setTargetDAGCombine (ISD::ROTL);
1072+ setTargetDAGCombine (ISD::ROTR);
10711073 setTargetDAGCombine (ISD::ANY_EXTEND);
10721074 if (Subtarget.hasStdExtF ()) {
10731075 setTargetDAGCombine (ISD::ZERO_EXTEND);
@@ -7269,6 +7271,40 @@ static SDValue transformAddShlImm(SDNode *N, SelectionDAG &DAG,
72697271 return DAG.getNode (ISD::SHL, DL, VT, NA1, DAG.getConstant (Bits, DL, VT));
72707272}
72717273
7274+ // Combine
7275+ // ROTR ((GREV x, 24), 16) -> (GREVI x, 8)
7276+ // ROTL ((GREV x, 24), 16) -> (GREVI x, 8)
7277+ // RORW ((GREVW x, 24), 16) -> (GREVIW x, 8)
7278+ // ROLW ((GREVW x, 24), 16) -> (GREVIW x, 8)
7279+ static SDValue combineROTR_ROTL_RORW_ROLW (SDNode *N, SelectionDAG &DAG) {
7280+ SDValue Src = N->getOperand (0 );
7281+ SDLoc DL (N);
7282+ unsigned Opc;
7283+
7284+ if ((N->getOpcode () == ISD::ROTR || N->getOpcode () == ISD::ROTL) &&
7285+ Src.getOpcode () == RISCVISD::GREV)
7286+ Opc = RISCVISD::GREV;
7287+ else if ((N->getOpcode () == RISCVISD::RORW ||
7288+ N->getOpcode () == RISCVISD::ROLW) &&
7289+ Src.getOpcode () == RISCVISD::GREVW)
7290+ Opc = RISCVISD::GREVW;
7291+ else
7292+ return SDValue ();
7293+
7294+ if (!isa<ConstantSDNode>(N->getOperand (1 )) ||
7295+ !isa<ConstantSDNode>(Src.getOperand (1 )))
7296+ return SDValue ();
7297+
7298+ unsigned ShAmt1 = N->getConstantOperandVal (1 );
7299+ unsigned ShAmt2 = Src.getConstantOperandVal (1 );
7300+ if (ShAmt1 != 16 && ShAmt2 != 24 )
7301+ return SDValue ();
7302+
7303+ Src = Src.getOperand (0 );
7304+ return DAG.getNode (Opc, DL, N->getValueType (0 ), Src,
7305+ DAG.getConstant (8 , DL, N->getOperand (1 ).getValueType ()));
7306+ }
7307+
72727308// Combine (GREVI (GREVI x, C2), C1) -> (GREVI x, C1^C2) when C1^C2 is
72737309// non-zero, and to x when it is. Any repeated GREVI stage undoes itself.
72747310// Combine (GORCI (GORCI x, C2), C1) -> (GORCI x, C1|C2). Repeated stage does
@@ -7973,8 +8009,12 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
79738009 if (SimplifyDemandedLowBitsHelper (0 , 32 ) ||
79748010 SimplifyDemandedLowBitsHelper (1 , 5 ))
79758011 return SDValue (N, 0 );
7976- break ;
8012+
8013+ return combineROTR_ROTL_RORW_ROLW (N, DAG);
79778014 }
8015+ case ISD::ROTR:
8016+ case ISD::ROTL:
8017+ return combineROTR_ROTL_RORW_ROLW (N, DAG);
79788018 case RISCVISD::CLZW:
79798019 case RISCVISD::CTZW: {
79808020 // Only the lower 32 bits of the first operand are read
0 commit comments