-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[X86][DAGCombiner][SelectionDAG] - Fold Zext Build Vector to Bitcast of widen Build Vector #135010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
e5c1914
483c273
a48660c
ff669c6
e776464
9dc5117
dd28865
321b52e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14195,6 +14195,66 @@ static SDValue widenAbs(SDNode *Extend, SelectionDAG &DAG) { | |
return DAG.getZExtOrTrunc(NewAbs, SDLoc(Extend), VT); | ||
} | ||
|
||
// Try to widen the build vector and bitcast it to the type of zext. | ||
// This is a special case for the 128-bit vector types. Intention is to remove | ||
// the zext and replace it with a bitcast the wider type. While lowering | ||
// the bitcast is removed and extra commutation due to zext is avoided. | ||
static SDValue widenBuildVec(SDNode *Extend, SelectionDAG &DAG) { | ||
|
||
assert(Extend->getOpcode() == ISD::ZERO_EXTEND && "Expected zero extend."); | ||
|
||
EVT ExtendVT = Extend->getValueType(0); | ||
|
||
SDValue BV = Extend->getOperand(0); | ||
if (BV.getOpcode() != ISD::BUILD_VECTOR || !BV.hasOneUse()) | ||
return SDValue(); | ||
|
||
SDLoc dl(BV); | ||
EVT VT = BV.getValueType(); | ||
EVT EltVT = BV.getOperand(0).getValueType(); | ||
unsigned NumElts = VT.getVectorNumElements(); | ||
|
||
const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | ||
|
||
if (TLI.getTypeAction(*DAG.getContext(), VT) != | ||
TargetLowering::TypeWidenVector) | ||
return SDValue(); | ||
|
||
EVT WidenVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); | ||
unsigned WidenNumElts = WidenVT.getVectorNumElements(); | ||
|
||
SmallVector<SDValue, 16> NewOps(BV->op_begin(), BV->op_end()); | ||
assert(WidenNumElts >= NumElts && "Shrinking vector instead of widening!"); | ||
// Fill the new elements with Zero. | ||
NewOps.append(WidenNumElts - NumElts, DAG.getConstant(0, dl, EltVT)); | ||
// Compute the step to place the elements in the right place and control the | ||
// iteration. | ||
unsigned step = WidenNumElts / NumElts; | ||
if (WidenVT.is128BitVector()) { | ||
if (step > 1 && Extend->getValueSizeInBits(0) == WidenVT.getSizeInBits()) { | ||
for (int i = NumElts - 1, j = WidenNumElts - step; i > 0; | ||
i--, j -= step) { | ||
SDValue temp = NewOps[i]; | ||
NewOps[i] = NewOps[j]; | ||
NewOps[j] = temp; | ||
} | ||
// Create new build vector with WidenVT and NewOps | ||
SDValue NewBV = DAG.getBuildVector(WidenVT, dl, NewOps); | ||
// Replace the old build vector with the new one. Bitcast the | ||
// new build vector to the type of the zext. | ||
SDValue NewBVBitcast = DAG.getBitcast(ExtendVT, NewBV); | ||
DAG.ReplaceAllUsesOfValueWith(SDValue(Extend, 0), NewBVBitcast); | ||
LLVM_DEBUG( | ||
dbgs() << DAG.getMachineFunction().getFunction().getName() | ||
<< " - Widening buildvector and replace zext with bitcast\n"; | ||
BV.dump(); Extend->dump(); dbgs() << " to \n"; | ||
NewBV.getNode()->dump(); NewBVBitcast->dump();); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove this whole debug print. You should see the transform triggered in the generic dag combine case. You aslo don't need all of the manual dumping, you can directly use operator<< There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I will fix it |
||
return NewBV; | ||
} | ||
} | ||
return SDValue(); | ||
} | ||
|
||
SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) { | ||
SDValue N0 = N->getOperand(0); | ||
EVT VT = N->getValueType(0); | ||
|
@@ -14521,6 +14581,9 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) { | |
return SDValue(CSENode, 0); | ||
} | ||
|
||
if (SDValue V = widenBuildVec(N, DAG)) | ||
return V; | ||
|
||
return SDValue(); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DAG format comment of the matched pattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arsenm Add such comment like zext(build_vec) -> bitcast(build_vec). which visualize the transformation, right?