@@ -56712,6 +56712,34 @@ static SDValue combineGatherScatter(SDNode *N, SelectionDAG &DAG,
56712
56712
56713
56713
if (DCI.isBeforeLegalize()) {
56714
56714
unsigned IndexWidth = Index.getScalarValueSizeInBits();
56715
+ // Attempt to move shifted index into the address scale, allows further
56716
+ // index truncation below.
56717
+ if (Index.getOpcode() == ISD::SHL && isa<ConstantSDNode>(Scale)) {
56718
+ unsigned ScaleAmt = Scale->getAsZExtVal();
56719
+ assert(isPowerOf2_32(ScaleAmt) && "Scale must be a power of 2");
56720
+ unsigned Log2ScaleAmt = Log2_32(ScaleAmt);
56721
+ unsigned MaskBits = IndexWidth - Log2ScaleAmt;
56722
+ APInt DemandedBits = APInt::getLowBitsSet(IndexWidth, MaskBits);
56723
+ if (TLI.SimplifyDemandedBits(Index, DemandedBits, DCI)) {
56724
+ if (N->getOpcode() != ISD::DELETED_NODE)
56725
+ DCI.AddToWorklist(N);
56726
+ return SDValue(N, 0);
56727
+ }
56728
+ if (auto MinShAmt = DAG.getValidMinimumShiftAmount(Index)) {
56729
+ if (*MinShAmt >= 1 && (*MinShAmt + Log2ScaleAmt) < 4 &&
56730
+ DAG.ComputeNumSignBits(Index.getOperand(0)) > 1) {
56731
+ SDValue ShAmt = Index.getOperand(1);
56732
+ SDValue NewShAmt =
56733
+ DAG.getNode(ISD::SUB, DL, ShAmt.getValueType(), ShAmt,
56734
+ DAG.getConstant(1, DL, ShAmt.getValueType()));
56735
+ SDValue NewIndex = DAG.getNode(ISD::SHL, DL, Index.getValueType(),
56736
+ Index.getOperand(0), NewShAmt);
56737
+ SDValue NewScale =
56738
+ DAG.getConstant(ScaleAmt * 2, DL, Scale.getValueType());
56739
+ return rebuildGatherScatter(GorS, NewIndex, Base, NewScale, DAG);
56740
+ }
56741
+ }
56742
+ }
56715
56743
56716
56744
// Shrink indices if they are larger than 32-bits.
56717
56745
// Only do this before legalize types since v2i64 could become v2i32.
@@ -56722,8 +56750,8 @@ static SDValue combineGatherScatter(SDNode *N, SelectionDAG &DAG,
56722
56750
56723
56751
// FIXME: We could support more than just constant fold, but we need to
56724
56752
// careful with costing. A truncate that can be optimized out would be
56725
- // fine. Otherwise we might only want to create a truncate if it avoids a
56726
- // split.
56753
+ // fine. Otherwise we might only want to create a truncate if it avoids
56754
+ // a split.
56727
56755
if (SDValue TruncIndex =
56728
56756
DAG.FoldConstantArithmetic(ISD::TRUNCATE, DL, NewVT, Index))
56729
56757
return rebuildGatherScatter(GorS, TruncIndex, Base, Scale, DAG);
@@ -56737,6 +56765,12 @@ static SDValue combineGatherScatter(SDNode *N, SelectionDAG &DAG,
56737
56765
Index = DAG.getNode(ISD::TRUNCATE, DL, NewVT, Index);
56738
56766
return rebuildGatherScatter(GorS, Index, Base, Scale, DAG);
56739
56767
}
56768
+
56769
+ // Shrink if we remove an illegal type.
56770
+ if (!TLI.isTypeLegal(Index.getValueType()) && TLI.isTypeLegal(NewVT)) {
56771
+ Index = DAG.getNode(ISD::TRUNCATE, DL, NewVT, Index);
56772
+ return rebuildGatherScatter(GorS, Index, Base, Scale, DAG);
56773
+ }
56740
56774
}
56741
56775
}
56742
56776
0 commit comments