Skip to content

Commit 75d36dc

Browse files
rohitaggarwal007Rohit AggarwalRKSimon
authored
[X86][SelectionDAG] Fix the Gather's base and index by modifying the Scale value (#137813)
Fix the Gather's base and index for one use or multiple uses of Index Node. Using the approach to update the Scale if SHL Opcode and followed by truncate. --------- Co-authored-by: Rohit Aggarwal <[email protected]> Co-authored-by: Simon Pilgrim <[email protected]>
1 parent 8aaac80 commit 75d36dc

File tree

2 files changed

+117
-170
lines changed

2 files changed

+117
-170
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56712,6 +56712,34 @@ static SDValue combineGatherScatter(SDNode *N, SelectionDAG &DAG,
5671256712

5671356713
if (DCI.isBeforeLegalize()) {
5671456714
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+
}
5671556743

5671656744
// Shrink indices if they are larger than 32-bits.
5671756745
// Only do this before legalize types since v2i64 could become v2i32.
@@ -56722,8 +56750,8 @@ static SDValue combineGatherScatter(SDNode *N, SelectionDAG &DAG,
5672256750

5672356751
// FIXME: We could support more than just constant fold, but we need to
5672456752
// 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.
5672756755
if (SDValue TruncIndex =
5672856756
DAG.FoldConstantArithmetic(ISD::TRUNCATE, DL, NewVT, Index))
5672956757
return rebuildGatherScatter(GorS, TruncIndex, Base, Scale, DAG);
@@ -56737,6 +56765,12 @@ static SDValue combineGatherScatter(SDNode *N, SelectionDAG &DAG,
5673756765
Index = DAG.getNode(ISD::TRUNCATE, DL, NewVT, Index);
5673856766
return rebuildGatherScatter(GorS, Index, Base, Scale, DAG);
5673956767
}
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+
}
5674056774
}
5674156775
}
5674256776

0 commit comments

Comments
 (0)