@@ -17504,8 +17504,46 @@ bool RISCVTargetLowering::isDesirableToCommuteWithShift(
1750417504 // (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
1750517505 SDValue N0 = N->getOperand(0);
1750617506 EVT Ty = N0.getValueType();
17507+
17508+ // LD/ST will optimize constant Offset extraction, so when AddNode is used by
17509+ // LD/ST, it can still complete the folding optimization operation performed
17510+ // above.
17511+ auto isLDST = [&]() {
17512+ bool canOptAwlays = false;
17513+ if (N0->getOpcode() == ISD::ADD && !N0->hasOneUse()) {
17514+ for (SDNode *Use : N0->uses()) {
17515+ // This use is the one we're on right now. Skip it
17516+ if (Use == N || Use->getOpcode() == ISD::SELECT)
17517+ continue;
17518+ if (!isa<StoreSDNode>(Use) && !isa<LoadSDNode>(Use)) {
17519+ canOptAwlays = false;
17520+ break;
17521+ }
17522+ canOptAwlays = true;
17523+ }
17524+ }
17525+
17526+ if (N0->getOpcode() == ISD::SIGN_EXTEND &&
17527+ !N0->getOperand(0)->hasOneUse()) {
17528+ for (SDNode *Use : N0->getOperand(0)->uses()) {
17529+ // This use is the one we're on right now. Skip it
17530+ if (Use == N0.getNode() || Use->getOpcode() == ISD::SELECT)
17531+ continue;
17532+ if (!isa<StoreSDNode>(Use) && !isa<LoadSDNode>(Use)) {
17533+ canOptAwlays = false;
17534+ break;
17535+ }
17536+ canOptAwlays = true;
17537+ }
17538+ }
17539+ return canOptAwlays;
17540+ };
17541+
1750717542 if (Ty.isScalarInteger() &&
1750817543 (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::OR)) {
17544+ if (N0.getOpcode() == ISD::ADD && !N0->hasOneUse()) {
17545+ return isLDST();
17546+ }
1750917547 auto *C1 = dyn_cast<ConstantSDNode>(N0->getOperand(1));
1751017548 auto *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1));
1751117549 if (C1 && C2) {
@@ -17540,6 +17578,16 @@ bool RISCVTargetLowering::isDesirableToCommuteWithShift(
1754017578 return false;
1754117579 }
1754217580 }
17581+
17582+ if ((N0->getOpcode() == ISD::ADD || N0->getOpcode() == ISD::OR) &&
17583+ !N0->hasOneUse())
17584+ return false;
17585+
17586+ if (N0->getOpcode() == ISD::SIGN_EXTEND &&
17587+ N0->getOperand(0)->getOpcode() == ISD::ADD &&
17588+ !(N0->hasOneUse() && N0->getOperand(0)->hasOneUse()))
17589+ return isLDST();
17590+
1754317591 return true;
1754417592}
1754517593
0 commit comments