Skip to content

Commit 69edef1

Browse files
committed
[DAG] Simplify control flow in SelectionDAGBuilder::visitShuffleVector [NFC]
If we've handled ==, and < above, the only case left can be >. We don't need to branch on this, and can instead assert and reduce indentation, and simplify reasoning about the fallthrough path.
1 parent d6a1501 commit 69edef1

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

+47-47
Original file line numberDiff line numberDiff line change
@@ -4119,61 +4119,61 @@ void SelectionDAGBuilder::visitShuffleVector(const User &I) {
41194119
return;
41204120
}
41214121

4122-
if (SrcNumElts > MaskNumElts) {
4123-
// Analyze the access pattern of the vector to see if we can extract
4124-
// two subvectors and do the shuffle.
4125-
int StartIdx[2] = { -1, -1 }; // StartIdx to extract from
4126-
bool CanExtract = true;
4127-
for (int Idx : Mask) {
4128-
unsigned Input = 0;
4129-
if (Idx < 0)
4130-
continue;
4122+
assert(SrcNumElts > MaskNumElts);
41314123

4132-
if (Idx >= (int)SrcNumElts) {
4133-
Input = 1;
4134-
Idx -= SrcNumElts;
4135-
}
4124+
// Analyze the access pattern of the vector to see if we can extract
4125+
// two subvectors and do the shuffle.
4126+
int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4127+
bool CanExtract = true;
4128+
for (int Idx : Mask) {
4129+
unsigned Input = 0;
4130+
if (Idx < 0)
4131+
continue;
41364132

4137-
// If all the indices come from the same MaskNumElts sized portion of
4138-
// the sources we can use extract. Also make sure the extract wouldn't
4139-
// extract past the end of the source.
4140-
int NewStartIdx = alignDown(Idx, MaskNumElts);
4141-
if (NewStartIdx + MaskNumElts > SrcNumElts ||
4142-
(StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4143-
CanExtract = false;
4144-
// Make sure we always update StartIdx as we use it to track if all
4145-
// elements are undef.
4146-
StartIdx[Input] = NewStartIdx;
4133+
if (Idx >= (int)SrcNumElts) {
4134+
Input = 1;
4135+
Idx -= SrcNumElts;
41474136
}
41484137

4149-
if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4150-
setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4151-
return;
4152-
}
4153-
if (CanExtract) {
4154-
// Extract appropriate subvector and generate a vector shuffle
4155-
for (unsigned Input = 0; Input < 2; ++Input) {
4156-
SDValue &Src = Input == 0 ? Src1 : Src2;
4157-
if (StartIdx[Input] < 0)
4158-
Src = DAG.getUNDEF(VT);
4159-
else {
4160-
Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4161-
DAG.getVectorIdxConstant(StartIdx[Input], DL));
4162-
}
4163-
}
4138+
// If all the indices come from the same MaskNumElts sized portion of
4139+
// the sources we can use extract. Also make sure the extract wouldn't
4140+
// extract past the end of the source.
4141+
int NewStartIdx = alignDown(Idx, MaskNumElts);
4142+
if (NewStartIdx + MaskNumElts > SrcNumElts ||
4143+
(StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4144+
CanExtract = false;
4145+
// Make sure we always update StartIdx as we use it to track if all
4146+
// elements are undef.
4147+
StartIdx[Input] = NewStartIdx;
4148+
}
41644149

4165-
// Calculate new mask.
4166-
SmallVector<int, 8> MappedOps(Mask);
4167-
for (int &Idx : MappedOps) {
4168-
if (Idx >= (int)SrcNumElts)
4169-
Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4170-
else if (Idx >= 0)
4171-
Idx -= StartIdx[0];
4150+
if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4151+
setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4152+
return;
4153+
}
4154+
if (CanExtract) {
4155+
// Extract appropriate subvector and generate a vector shuffle
4156+
for (unsigned Input = 0; Input < 2; ++Input) {
4157+
SDValue &Src = Input == 0 ? Src1 : Src2;
4158+
if (StartIdx[Input] < 0)
4159+
Src = DAG.getUNDEF(VT);
4160+
else {
4161+
Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4162+
DAG.getVectorIdxConstant(StartIdx[Input], DL));
41724163
}
4164+
}
41734165

4174-
setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4175-
return;
4166+
// Calculate new mask.
4167+
SmallVector<int, 8> MappedOps(Mask);
4168+
for (int &Idx : MappedOps) {
4169+
if (Idx >= (int)SrcNumElts)
4170+
Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4171+
else if (Idx >= 0)
4172+
Idx -= StartIdx[0];
41764173
}
4174+
4175+
setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4176+
return;
41774177
}
41784178

41794179
// We can't use either concat vectors or extract subvectors so fall back to

0 commit comments

Comments
 (0)