27 #define DEBUG_TYPE "hexagon-isel" 104 using MapType = std::map<Node, ColorKind>;
105 static constexpr Node Ignore = Node(-1);
113 const MapType &colors()
const {
119 return ColorKind::Red;
120 return Color == ColorKind::Red ? ColorKind::Black : ColorKind::Red;
128 std::set<Node> Needed;
130 using NodeSet = std::set<Node>;
131 std::map<Node,NodeSet> Edges;
133 Node conj(Node Pos) {
134 Node Num = Order.
size();
135 return (Pos < Num/2) ? Pos + Num/2 : Pos - Num/2;
139 auto F = Colors.find(N);
143 std::pair<bool, ColorKind> getUniqueColor(
const NodeSet &Nodes);
150 std::pair<bool, ColorKind> Coloring::getUniqueColor(
const NodeSet &Nodes) {
152 for (Node
N : Nodes) {
161 return {
true,
Color };
166 for (
unsigned P = 0;
P != Order.size(); ++
P) {
170 Node PC = Order[conj(
P)];
171 if (PC != Ignore && PC != I)
176 for (
unsigned I = 0;
I != Order.size(); ++
I) {
177 if (!Needed.count(
I))
189 bool Coloring::color() {
191 auto Enqueue = [
this,&FirstQ] (Node
N) {
194 for (
unsigned I = 0;
I != Q.
size(); ++
I) {
196 Q.insert(Ns.begin(), Ns.end());
200 for (Node
N : Needed)
203 for (Node
N : FirstQ) {
207 auto P = getUniqueColor(Ns);
210 Colors[
N] = other(
P.second);
214 for (
auto E : Edges) {
216 if (!Needed.count(conj(N)) || Colors.count(N))
218 auto P = getUniqueColor(
E.second);
221 Colors[
N] = other(
P.second);
226 std::vector<Node> WorkQ;
227 for (
auto E : Edges) {
229 if (!Colors.count(N))
233 for (
unsigned I = 0;
I < WorkQ.size(); ++
I) {
236 auto P = getUniqueColor(Ns);
238 Colors[
N] = other(
P.second);
248 for (Node M : CopyNs) {
250 if (ColorM == ColorC) {
263 for (
unsigned I = 0;
I != Order.size(); ++
I)
264 if (Colors.count(
I) == 0)
272 dbgs() <<
"{ Order: {";
273 for (
unsigned I = 0;
I != Order.size(); ++
I) {
281 dbgs() <<
" Needed: {";
282 for (Node
N : Needed)
286 dbgs() <<
" Edges: {\n";
287 for (
auto E : Edges) {
288 dbgs() <<
" " <<
E.first <<
" -> {";
289 for (
auto N :
E.second)
301 case ColorKind::Black:
307 dbgs() <<
" Colors: {\n";
308 for (
auto C : Colors)
309 dbgs() <<
" " <<
C.first <<
" -> " << ColorKindToName(
C.second) <<
"\n";
318 using Controls = std::vector<uint8_t>;
319 using ElemType = int;
320 static constexpr ElemType Ignore = ElemType(-1);
336 unsigned S = Order.size();
340 Table.resize(Order.size());
341 for (RowType &Row : Table)
345 void getControls(Controls &V,
unsigned StartAt, uint8_t Dir)
const {
346 unsigned Size = Order.size();
348 for (
unsigned I = 0;
I !=
Size; ++
I) {
350 for (
unsigned L = 0; L != Log; ++L) {
351 unsigned C = ctl(
I, StartAt+L) == Switch;
362 uint8_t ctl(ElemType Pos,
unsigned Step)
const {
363 return Table[Pos][Step];
365 unsigned size()
const {
368 unsigned steps()
const {
374 std::vector<ElemType> Order;
375 using RowType = std::vector<uint8_t>;
376 std::vector<RowType> Table;
379 struct ForwardDeltaNetwork :
public PermNetwork {
382 bool run(Controls &V) {
383 if (!route(Order.data(), Table.data(),
size(), 0))
385 getControls(V, 0, Forward);
390 bool route(ElemType *
P, RowType *
T,
unsigned Size,
unsigned Step);
393 struct ReverseDeltaNetwork :
public PermNetwork {
396 bool run(Controls &V) {
397 if (!route(Order.data(), Table.data(),
size(), 0))
399 getControls(V, 0, Reverse);
404 bool route(ElemType *
P, RowType *
T,
unsigned Size,
unsigned Step);
407 struct BenesNetwork :
public PermNetwork {
410 bool run(Controls &
F, Controls &R) {
411 if (!route(Order.data(), Table.data(),
size(), 0))
414 getControls(F, 0, Forward);
415 getControls(R, Log, Reverse);
420 bool route(ElemType *
P, RowType *
T,
unsigned Size,
unsigned Step);
424 bool ForwardDeltaNetwork::route(ElemType *
P, RowType *
T,
unsigned Size,
426 bool UseUp =
false, UseDown =
false;
433 for (ElemType J = 0; J != Num; ++J) {
441 S = (J < Num/2) ?
Pass : Switch;
443 S = (J < Num/2) ? Switch :
Pass;
446 ElemType U = (S ==
Pass) ? I : (I < Num/2 ? I+Num/2 : I-Num/2);
451 if (
T[U][Step] != S &&
T[U][Step] !=
None)
456 for (ElemType J = 0; J != Num; ++J)
457 if (
P[J] != Ignore &&
P[J] >= Num/2)
461 if (UseUp && !route(
P,
T, Size/2, Step+1))
463 if (UseDown && !route(
P+Size/2,
T+Size/2, Size/2, Step+1))
469 bool ReverseDeltaNetwork::route(ElemType *
P, RowType *
T,
unsigned Size,
471 unsigned Pets = Log-1 - Step;
472 bool UseUp =
false, UseDown =
false;
477 const Coloring::MapType &M =
G.colors();
482 for (ElemType J = 0; J != Num; ++J) {
494 bool InpUp = I < Num/2;
496 ColorUp = InpUp ?
C :
G.other(C);
497 if ((C == ColorUp) != InpUp) {
504 S = (J < Num/2) ?
Pass : Switch;
507 S = (J < Num/2) ? Switch :
Pass;
515 for (ElemType J = 0,
E = Size / 2; J !=
E; ++J) {
517 ElemType PC =
P[J+Size/2];
520 if (
T[J][Pets] == Switch)
522 if (
T[J+Size/2][Pets] == Switch)
528 for (ElemType J = 0; J != Num; ++J)
529 if (
P[J] != Ignore &&
P[J] >= Num/2)
533 if (UseUp && !route(
P,
T, Size/2, Step+1))
535 if (UseDown && !route(
P+Size/2,
T+Size/2, Size/2, Step+1))
541 bool BenesNetwork::route(ElemType *
P, RowType *
T,
unsigned Size,
544 const Coloring::MapType &M =
G.colors();
549 unsigned Pets = 2*Log-1 - Step;
550 bool UseUp =
false, UseDown =
false;
556 for (ElemType J = 0; J != Num; ++J) {
564 ColorUp = (I < Num / 2) ? ColorKind::Red : ColorKind::Black;
566 unsigned CI = (I < Num/2) ? I+Num/2 : I-Num/2;
571 T[CI][Step] = Switch;
572 T[J][Pets] = (J < Num/2) ?
Pass : Switch;
576 T[CI][Step] = Switch;
579 T[J][Pets] = (J < Num/2) ? Switch :
Pass;
586 for (ElemType J = 0; J != Num/2; ++J) {
588 ElemType PC =
P[J+Num/2];
591 if (
T[J][Pets] == Switch)
593 if (
T[J+Num/2][Pets] == Switch)
599 for (ElemType J = 0; J != Num; ++J)
600 if (
P[J] != Ignore &&
P[J] >= Num/2)
604 if (UseUp && !route(
P,
T, Size/2, Step+1))
606 if (UseDown && !route(
P+Size/2,
T+Size/2, Size/2, Step+1))
619 bool isValue()
const {
return OpV.getNode() !=
nullptr; }
620 bool isValid()
const {
return isValue() || !(OpN & Invalid); }
621 static OpRef res(
int N) {
return OpRef(Whole | (N &
Index)); }
622 static OpRef
fail() {
return OpRef(Invalid); }
624 static OpRef lo(
const OpRef &R) {
626 return OpRef(R.OpN & (
Undef |
Index | LoHalf));
628 static OpRef hi(
const OpRef &R) {
630 return OpRef(R.OpN & (
Undef |
Index | HiHalf));
645 Invalid = 0x10000000,
648 Whole = LoHalf | HiHalf,
657 OpRef(
unsigned N) : OpN(N) {}
660 struct NodeTemplate {
661 NodeTemplate() =
default;
664 std::vector<OpRef> Ops;
674 unsigned push(
const NodeTemplate &Res) {
676 return List.size()-1;
678 unsigned push(
unsigned Opc,
MVT Ty, std::vector<OpRef> &&Ops) {
685 bool empty()
const {
return List.empty(); }
686 unsigned size()
const {
return List.size(); }
687 unsigned top()
const {
return size()-1; }
688 const NodeTemplate &operator[](
unsigned I)
const {
return List[
I]; }
689 unsigned reset(
unsigned NewTop) {
690 List.resize(NewTop+1);
694 using BaseType = std::vector<NodeTemplate>;
695 BaseType::iterator
begin() {
return List.begin(); }
696 BaseType::iterator
end() {
return List.end(); }
697 BaseType::const_iterator
begin()
const {
return List.begin(); }
698 BaseType::const_iterator
end()
const {
return List.end(); }
708 OpV.getNode()->print(OS, &G);
719 if ((OpN & Whole) != Whole) {
720 assert((OpN & Whole) == LoHalf || (OpN & Whole) == HiHalf);
734 for (
const auto &R : Ops) {
744 OS <<
"Input node:\n";
748 OS <<
"Result templates:\n";
749 for (
unsigned I = 0,
E =
List.size();
I !=
E; ++
I) {
750 OS <<
'[' <<
I <<
"] ";
751 List[
I].print(OS, G);
759 for (
unsigned I = 0,
E =
Mask.size();
I !=
E; ++
I) {
763 MinSrc = (MinSrc == -1) ? M : std::min(MinSrc, M);
764 MaxSrc = (MaxSrc == -1) ? M :
std::max(MaxSrc, M);
769 int MinSrc = -1, MaxSrc = -1;
771 ShuffleMask lo()
const {
772 size_t H = Mask.
size()/2;
775 ShuffleMask hi()
const {
776 size_t H = Mask.
size()/2;
781 OS <<
"MinSrc:" << MinSrc <<
", MaxSrc:" << MaxSrc <<
" {";
826 void materialize(
const ResultStack &
Results);
834 OpRef
concat(OpRef Va, OpRef Vb, ResultStack &Results);
835 OpRef packs(ShuffleMask SM, OpRef Va, OpRef Vb, ResultStack &Results,
837 OpRef packp(ShuffleMask SM, OpRef Va, OpRef Vb, ResultStack &Results,
840 ResultStack &Results);
842 ResultStack &Results);
844 OpRef shuffs1(ShuffleMask SM, OpRef Va, ResultStack &Results);
845 OpRef shuffs2(ShuffleMask SM, OpRef Va, OpRef Vb, ResultStack &Results);
846 OpRef shuffp1(ShuffleMask SM, OpRef Va, ResultStack &Results);
847 OpRef shuffp2(ShuffleMask SM, OpRef Va, OpRef Vb, ResultStack &Results);
849 OpRef butterfly(ShuffleMask SM, OpRef Va, ResultStack &Results);
850 OpRef contracting(ShuffleMask SM, OpRef Va, OpRef Vb, ResultStack &Results);
851 OpRef expanding(ShuffleMask SM, OpRef Va, ResultStack &Results);
852 OpRef perfect(ShuffleMask SM, OpRef Va, ResultStack &Results);
854 bool selectVectorConstants(
SDNode *N);
863 unsigned VecLen = Mask.
size();
865 for (
unsigned I = 0;
I != VecLen; ++
I) {
868 MaskL[
I] = MaskR[
I] = -1;
869 }
else if (
unsigned(M) < VecLen) {
884 for (
unsigned I = 1;
I != MaxLen; ++
I) {
889 return {
F, MaxLen };
900 for (
int I = 0,
E = Mask.
size();
I !=
E; ++
I) {
902 if (M >= 0 && M !=
I)
910 assert(Mask.
size() < 0x00007FFF &&
"Sanity failure");
912 for (
int Idx : Mask) {
918 return 2*Sum == N*(N-1);
921 bool HvxSelector::selectVectorConstants(
SDNode *
N) {
936 auto IsNodeToSelect = [] (
SDNode *
N) {
944 SDValue Addr = cast<LoadSDNode>(
N)->getBasePtr();
959 for (
unsigned i = 0; i != WorkQ.
size(); ++i) {
961 if (IsNodeToSelect(W))
970 return !Nodes.empty();
973 void HvxSelector::materialize(
const ResultStack &
Results) {
975 dbgs() <<
"Materializing\n";
980 const SDLoc &dl(Results.InpNode);
981 std::vector<SDValue> Output;
983 for (
unsigned I = 0,
E = Results.size();
I !=
E; ++
I) {
984 const NodeTemplate &Node = Results[
I];
985 std::vector<SDValue> Ops;
986 for (
const OpRef &R : Node.Ops) {
989 Ops.push_back(R.OpV);
994 Ops.push_back(
ISel.selectUndef(dl,
MVT(SVT)));
998 unsigned Part = R.OpN & OpRef::Whole;
1002 assert(Idx >= 0 &&
unsigned(Idx) < Output.size());
1004 MVT OpTy = Op.getValueType().getSimpleVT();
1005 if (Part != OpRef::Whole) {
1006 assert(Part == OpRef::LoHalf || Part == OpRef::HiHalf);
1009 unsigned Sub = (Part == OpRef::LoHalf) ? Hexagon::vsub_lo
1017 SDNode *ResN = (Node.Opc == TargetOpcode::COPY)
1018 ? Ops.front().getNode()
1020 Output.push_back(
SDValue(ResN, 0));
1023 SDNode *OutN = Output.back().getNode();
1024 SDNode *InpN = Results.InpNode;
1026 dbgs() <<
"Generated node:\n";
1031 selectVectorConstants(OutN);
1035 OpRef HvxSelector::concat(OpRef
Lo, OpRef
Hi, ResultStack &Results) {
1037 const SDLoc &dl(Results.InpNode);
1043 return OpRef::res(Results.top());
1047 OpRef HvxSelector::packs(ShuffleMask SM, OpRef Va, OpRef Vb,
1051 if (!Va.isValid() || !Vb.isValid())
1054 int VecLen = SM.Mask.size();
1057 auto IsExtSubvector = [] (ShuffleMask M) {
1058 assert(M.MinSrc >= 0 && M.MaxSrc >= 0);
1059 for (
int I = 0,
E = M.Mask.size();
I !=
E; ++
I) {
1060 if (M.Mask[
I] >= 0 && M.Mask[
I]-
I != M.MinSrc)
1066 if (SM.MaxSrc - SM.MinSrc <
int(
HwLen)) {
1067 if (SM.MinSrc == 0 || SM.MinSrc ==
int(
HwLen) || !IsExtSubvector(SM)) {
1073 if (SM.MaxSrc <
int(
HwLen)) {
1074 memcpy(NewMask.
data(), SM.Mask.data(),
sizeof(int)*VecLen);
1077 if (SM.MinSrc >=
int(
HwLen)) {
1078 for (
int I = 0;
I != VecLen; ++
I) {
1087 int MinSrc = SM.MinSrc;
1088 if (SM.MaxSrc <
int(
HwLen)) {
1090 }
else if (SM.MinSrc >
int(
HwLen)) {
1092 MinSrc = SM.MinSrc -
HwLen;
1094 const SDLoc &dl(Results.InpNode);
1095 if (isUInt<3>(MinSrc) || isUInt<3>(
HwLen-MinSrc)) {
1096 bool IsRight = isUInt<3>(MinSrc);
1099 unsigned Opc = IsRight ? Hexagon::V6_valignbi
1100 : Hexagon::V6_vlalignbi;
1101 Results.push(Opc, Ty, {Vb, Va, S});
1104 Results.push(Hexagon::A2_tfrsi,
MVT::i32, {S});
1105 unsigned Top = Results.top();
1106 Results.push(Hexagon::V6_valignb, Ty, {Vb, Va, OpRef::res(Top)});
1108 for (
int I = 0;
I != VecLen; ++
I) {
1114 return OpRef::res(Results.top());
1117 if (Options & PackMux) {
1123 for (
int I = 0;
I != VecLen; ++
I) {
1127 if (M >=
int(
HwLen))
1138 return vmuxs(MuxBytes, Va, Vb, Results);
1144 OpRef HvxSelector::packp(ShuffleMask SM, OpRef Va, OpRef Vb,
1147 unsigned HalfMask = 0;
1149 for (
int M : SM.Mask) {
1152 HalfMask |= (1u << (M >> LogHw));
1165 OpRef Inp[2] = { Va, Vb };
1166 OpRef Out[2] = { OpRef::undef(HalfTy), OpRef::undef(HalfTy) };
1168 uint8_t HalfIdx[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
1170 for (
unsigned I = 0;
I != 4; ++
I) {
1171 if ((HalfMask & (1u <<
I)) == 0)
1174 OpRef
Op = Inp[
I/2];
1175 Out[Idx] = (
I & 1) ? OpRef::hi(Op) : OpRef::lo(Op);
1179 int VecLen = SM.Mask.size();
1180 for (
int I = 0;
I != VecLen; ++
I) {
1183 uint8_t Idx = HalfIdx[M >> LogHw];
1184 assert(Idx == 0 || Idx == 1);
1190 return concat(Out[0], Out[1], Results);
1194 ResultStack &Results) {
1198 const SDLoc &dl(Results.InpNode);
1199 SDValue B = getVectorConstant(Bytes, dl);
1200 Results.push(Hexagon::V6_vd0, ByteTy, {});
1201 Results.push(Hexagon::V6_veqb, BoolTy, {OpRef(B), OpRef::res(-1)});
1202 Results.push(Hexagon::V6_vmux, ByteTy, {OpRef::res(-1), Vb, Va});
1203 return OpRef::res(Results.top());
1207 ResultStack &Results) {
1209 size_t S = Bytes.
size() / 2;
1212 return concat(L, H, Results);
1215 OpRef HvxSelector::shuffs1(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1217 unsigned VecLen = SM.Mask.size();
1220 assert(
all_of(SM.Mask, [
this](
int M) { return M == -1 || M < int(HwLen); }));
1227 OpRef
P = perfect(SM, Va, Results);
1230 return butterfly(SM, Va, Results);
1233 OpRef HvxSelector::shuffs2(ShuffleMask SM, OpRef Va, OpRef Vb,
1234 ResultStack &Results) {
1239 OpRef
C = contracting(SM, Va, Vb, Results);
1243 int VecLen = SM.Mask.size();
1245 OpRef
P = packs(SM, Va, Vb, Results, NewMask);
1247 return shuffs1(ShuffleMask(NewMask),
P,
Results);
1252 OpRef L = shuffs1(ShuffleMask(MaskL), Va, Results);
1253 OpRef R = shuffs1(ShuffleMask(MaskR), Vb, Results);
1254 if (!L.isValid() || !R.isValid())
1258 for (
int I = 0;
I != VecLen; ++
I) {
1262 return vmuxs(Bytes, L, R, Results);
1265 OpRef HvxSelector::shuffp1(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1267 int VecLen = SM.Mask.size();
1275 OpRef
P = packs(SM, OpRef::lo(Va), OpRef::hi(Va), Results, PackedMask);
1277 ShuffleMask PM(PackedMask);
1278 OpRef
E = expanding(PM, P, Results);
1282 OpRef L = shuffs1(PM.lo(),
P,
Results);
1284 if (L.isValid() && H.isValid())
1285 return concat(L, H, Results);
1288 OpRef R = perfect(SM, Va, Results);
1293 OpRef L = shuffs2(SM.lo(), OpRef::lo(Va), OpRef::hi(Va),
Results);
1294 OpRef
H = shuffs2(SM.hi(), OpRef::lo(Va), OpRef::hi(Va),
Results);
1295 if (L.isValid() && H.isValid())
1296 return concat(L, H, Results);
1301 OpRef HvxSelector::shuffp2(ShuffleMask SM, OpRef Va, OpRef Vb,
1302 ResultStack &Results) {
1307 int VecLen = SM.Mask.size();
1309 OpRef
P = packp(SM, Va, Vb, Results, PackedMask);
1311 return shuffp1(ShuffleMask(PackedMask),
P,
Results);
1316 OpRef L = shuffp1(ShuffleMask(MaskL), Va, Results);
1317 OpRef R = shuffp1(ShuffleMask(MaskR), Vb, Results);
1318 if (!L.isValid() || !R.isValid())
1323 for (
int I = 0;
I != VecLen; ++
I) {
1327 return vmuxp(Bytes, L, R, Results);
1336 unsigned VecLen = Mask.
size();
1337 bool HavePairs = (2*
HwLen == VecLen);
1341 for (
int I : Mask) {
1370 if (2*
HwLen == VecLen) {
1389 std::deque<SDNode*> SubNodes;
1390 SubNodes.push_back(LV.
getNode());
1391 for (
unsigned I = 0;
I != SubNodes.size(); ++
I) {
1393 SubNodes.push_back(
Op.getNode());
1395 while (!SubNodes.empty()) {
1396 SDNode *S = SubNodes.front();
1397 SubNodes.pop_front();
1409 SubNodes.push_back(S);
1418 OpRef HvxSelector::contracting(ShuffleMask SM, OpRef Va, OpRef Vb,
1419 ResultStack &Results) {
1421 if (!Va.isValid() || !Vb.isValid())
1431 int VecLen = SM.Mask.size();
1432 std::pair<int,unsigned> Strip =
findStrip(SM.Mask, 1, VecLen);
1437 if (Strip.second != 1 && Strip.second != 2)
1453 int NextInMask = SM.Mask[Strip.second];
1458 if (NextInMask < VecLen) {
1460 if (Strip.first == 0 && Strip.second == 1 && NextInMask == 4) {
1463 for (
int I = 0;
I != N/4; ++
I)
1464 if (SM.Mask[
I] != 4*
I)
1466 for (
int I = 0;
I != N/4; ++
I)
1467 if (SM.Mask[
I+N/4] != 2 + 4*
I)
1469 for (
int I = 0;
I != N/4; ++
I)
1470 if (SM.Mask[
I+N/2] != N + 4*
I)
1472 for (
int I = 0;
I != N/4; ++
I)
1473 if (SM.Mask[
I+3*N/4] != N+2 + 4*
I)
1476 Results.push(Hexagon::V6_vdealb4w, ResTy, {Vb, Va});
1477 return OpRef::res(Results.top());
1482 int L = Strip.second;
1484 if (Strip.first != 0 && Strip.first != L)
1487 for (
int I = L;
I <
N;
I += L) {
1488 auto S =
findStrip(SM.Mask.drop_front(
I), 1, N-
I);
1491 if (S.first - Strip.first != 2*
I)
1494 if (S.second !=
unsigned(L))
1500 assert(Strip.first == 0 || Strip.first == L);
1501 using namespace Hexagon;
1503 Res.Opc = Strip.second == 1
1504 ? (Strip.first == 0 ? V6_vpackeb : V6_vpackob)
1505 : (Strip.first == 0 ? V6_vpackeh : V6_vpackoh);
1507 Res.Ops = { Vb, Va };
1509 return OpRef::res(Results.top());
1514 int L = Strip.second;
1515 std::pair<int,unsigned> PrevS = Strip;
1517 for (
int I = L;
I <
N;
I += L) {
1518 auto S =
findStrip(SM.Mask.drop_front(
I), 1, N-
I);
1519 if (S.second != PrevS.second)
1521 int Diff = Flip ? PrevS.first - S.first + 2*L
1522 : S.first - PrevS.first;
1530 assert(Strip.first == 0 || Strip.first == L);
1531 using namespace Hexagon;
1533 Res.Opc = Strip.second == 1
1534 ? (Strip.first == 0 ? V6_vshuffeb : V6_vshuffob)
1535 : (Strip.first == 0 ? V6_vshufeh : V6_vshufoh);
1537 Res.Ops = { Vb, Va };
1539 return OpRef::res(Results.top());
1542 OpRef HvxSelector::expanding(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1555 int VecLen = SM.Mask.size();
1556 assert(2*
HwLen ==
unsigned(VecLen) &&
"Expecting vector-pair type");
1558 std::pair<int,unsigned> Strip =
findStrip(SM.Mask, 1, VecLen);
1565 if (Strip.first != 0)
1569 if (Strip.second != 1 && Strip.second != 2)
1573 int L = Strip.second;
1576 for (
int I = 2*L;
I < 2*
N;
I += 2*L) {
1577 auto S =
findStrip(SM.Mask.drop_front(
I), 1, N-
I);
1578 if (S.second !=
unsigned(L))
1584 for (
int I = L;
I < 2*
N;
I += 2*L) {
1585 auto S =
findStrip(SM.Mask.drop_front(
I), 0, N-
I);
1586 if (S.first != -1 || S.second !=
unsigned(L))
1590 unsigned Opc = Strip.second == 1 ? Hexagon::V6_vunpackub
1591 : Hexagon::V6_vunpackuh;
1593 return OpRef::res(Results.top());
1596 OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1605 int VecLen = SM.Mask.size();
1607 unsigned LogLen =
Log2_32(VecLen);
1611 assert(LogLen == HwLog || LogLen == HwLog+1);
1612 bool Extend = (LogLen == HwLog);
1697 unsigned X = Mask[0] ^ Mask[Num/2];
1699 if ((Mask[0] & X) != 0)
1701 for (
unsigned I = 1;
I != Num/2; ++
I) {
1702 if (
unsigned(Mask[
I] ^ Mask[
I+Num/2]) !=
X)
1704 if ((Mask[
I] & X) != 0)
1713 for (
unsigned I = VecLen;
I >= 2;
I >>= 1) {
1715 unsigned X = XorPow2(SM.Mask,
I);
1719 for (
int J =
I; J < VecLen; J +=
I) {
1720 if (XorPow2(SM.Mask.slice(J,
I),
I) != X)
1752 std::set<CycleType> Cycles;
1753 std::set<unsigned> All;
1755 for (
unsigned I : Perm)
1760 auto canonicalize = [LogLen](
const CycleType &
C) -> CycleType {
1761 unsigned LogPos, N =
C.size();
1762 for (LogPos = 0; LogPos !=
N; ++LogPos)
1763 if (
C[LogPos] == LogLen-1)
1768 CycleType NewC(
C.begin()+LogPos,
C.end());
1769 NewC.append(
C.begin(),
C.begin()+LogPos);
1773 auto pfs = [](
const std::set<CycleType> &Cs,
unsigned Len) {
1778 const CycleType &
C = *Cs.begin();
1781 int D = Len - C.size();
1782 if (D != 0 && D != 1)
1785 bool IsDeal =
true, IsShuff =
true;
1786 for (
unsigned I = 1;
I != Len-
D; ++
I) {
1787 if (C[
I] != Len-1-
I)
1789 if (C[
I] !=
I-(1-D))
1793 assert(!(IsDeal || IsShuff) || IsDeal != IsShuff);
1794 static unsigned Deals[] = { Hexagon::V6_vdealb, Hexagon::V6_vdealh };
1795 static unsigned Shufs[] = { Hexagon::V6_vshuffb, Hexagon::V6_vshuffh };
1796 return IsDeal ? Deals[
D] : (IsShuff ? Shufs[
D] : 0);
1799 while (!All.empty()) {
1800 unsigned A = *All.begin();
1804 for (
unsigned B = Perm[A];
B != A;
B = Perm[
B]) {
1810 Cycles.insert(canonicalize(C));
1817 if (
unsigned(VecLen) ==
HwLen) {
1818 if (
unsigned SingleOpc = pfs(Cycles, LogLen)) {
1819 Results.push(SingleOpc, SingleTy, {Va});
1820 return OpRef::res(Results.top());
1825 if (
HwLen ==
unsigned(VecLen))
1828 for (
const CycleType &
C : Cycles) {
1829 unsigned First = (
C[0] == LogLen-1) ? 1 : 0;
1830 SwapElems.append(
C.begin()+First,
C.end());
1832 SwapElems.push_back(
C[0]);
1835 const SDLoc &dl(Results.InpNode);
1836 OpRef
Arg = !Extend ? Va
1837 :
concat(Va, OpRef::undef(SingleTy), Results);
1839 for (
unsigned I = 0,
E = SwapElems.size();
I !=
E; ) {
1840 bool IsInc =
I ==
E-1 || SwapElems[
I] < SwapElems[
I+1];
1841 unsigned S = (1u << SwapElems[
I]);
1843 while (++
I <
E-1 && IsInc == (SwapElems[
I] < SwapElems[
I+1]))
1844 S |= 1u << SwapElems[
I];
1847 S |= 1u << SwapElems[
I];
1852 Results.push(Hexagon::A2_tfrsi,
MVT::i32,
1854 Res.Opc = IsInc ? Hexagon::V6_vshuffvdd : Hexagon::V6_vdealvdd;
1856 Res.Ops = { OpRef::hi(Arg), OpRef::lo(Arg), OpRef::res(-1) };
1858 Arg = OpRef::res(Results.top());
1861 return !Extend ?
Arg : OpRef::lo(Arg);
1864 OpRef HvxSelector::butterfly(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1877 PermNetwork::Controls
FC, RC;
1878 const SDLoc &dl(Results.InpNode);
1879 int VecLen = SM.Mask.size();
1881 for (
int M : SM.Mask) {
1882 if (M != -1 && M >= VecLen)
1887 ForwardDeltaNetwork FN(SM.Mask);
1889 SDValue Ctl = getVectorConstant(FC, dl);
1890 Results.push(Hexagon::V6_vdelta, ResTy, {Va, OpRef(Ctl)});
1891 return OpRef::res(Results.top());
1895 ReverseDeltaNetwork
RN(SM.Mask);
1897 SDValue Ctl = getVectorConstant(RC, dl);
1898 Results.push(Hexagon::V6_vrdelta, ResTy, {Va, OpRef(Ctl)});
1899 return OpRef::res(Results.top());
1903 BenesNetwork BN(SM.Mask);
1904 if (BN.run(FC, RC)) {
1905 SDValue CtlF = getVectorConstant(FC, dl);
1906 SDValue CtlR = getVectorConstant(RC, dl);
1907 Results.push(Hexagon::V6_vdelta, ResTy, {Va, OpRef(CtlF)});
1908 Results.push(Hexagon::V6_vrdelta, ResTy,
1909 {OpRef::res(-1), OpRef(CtlR)});
1910 return OpRef::res(Results.top());
1919 for (uint8_t
C : Data)
1930 dbgs() <<
"Starting " << __func__ <<
" on node:\n";
1935 assert(ResTy.isVector() && ResTy.getVectorElementType() ==
MVT::i8);
1937 auto *SN = cast<ShuffleVectorSDNode>(
N);
1938 std::vector<int>
Mask(SN->getMask().begin(), SN->getMask().end());
1940 for (
int &Idx : Mask)
1941 if (Idx != -1 && Idx < 0)
1944 unsigned VecLen = Mask.size();
1945 bool HavePairs = (2*
HwLen == VecLen);
1946 assert(ResTy.getSizeInBits() / 8 == VecLen);
1951 bool UseLeft =
false, UseRight =
false;
1952 for (
unsigned I = 0;
I != VecLen; ++
I) {
1955 unsigned Idx = Mask[
I];
1964 dbgs() <<
"VecLen=" << VecLen <<
" HwLen=" <<
HwLen <<
" UseLeft=" 1965 << UseLeft <<
" UseRight=" << UseRight <<
" HavePairs=" 1966 << HavePairs <<
'\n';
1969 if (!UseLeft && !UseRight) {
1977 Results.push(TargetOpcode::COPY, ResTy, {Vec0});
1978 Results.push(TargetOpcode::COPY, ResTy, {Vec1});
1979 OpRef Va = OpRef::res(Results.top()-1);
1980 OpRef Vb = OpRef::res(Results.top());
1982 OpRef Res = !HavePairs ? shuffs2(ShuffleMask(Mask), Va, Vb, Results)
1983 : shuffp2(ShuffleMask(Mask), Va, Vb, Results);
1985 bool Done = Res.isValid();
1988 Results.push(TargetOpcode::COPY, ResTy, {Res});
1989 materialize(Results);
1991 Done = scalarizeShuffle(Mask,
SDLoc(N), ResTy, Vec0, Vec1, N);
1996 dbgs() <<
"Unhandled shuffle:\n";
2011 if (
auto *CN = dyn_cast<ConstantSDNode>(RotV.
getNode())) {
2015 }
else if (isUInt<3>(S)) {
2038 void HexagonDAGToDAGISel::SelectHvxShuffle(
SDNode *N) {
2042 void HexagonDAGToDAGISel::SelectHvxRor(
SDNode *N) {
2046 void HexagonDAGToDAGISel::SelectHvxVAlign(
SDNode *N) {
2053 "which are disabled");
2064 unsigned IntNo = cast<ConstantSDNode>(N->
getOperand(1))->getZExtValue();
2068 case Intrinsic::hexagon_V6_vgathermhq:
2069 case Intrinsic::hexagon_V6_vgathermhq_128B:
2070 Opcode = Hexagon::V6_vgathermhq_pseudo;
2072 case Intrinsic::hexagon_V6_vgathermwq:
2073 case Intrinsic::hexagon_V6_vgathermwq_128B:
2074 Opcode = Hexagon::V6_vgathermwq_pseudo;
2076 case Intrinsic::hexagon_V6_vgathermhwq:
2077 case Intrinsic::hexagon_V6_vgathermhwq_128B:
2078 Opcode = Hexagon::V6_vgathermhwq_pseudo;
2084 SDNode *
Result = CurDAG->getMachineNode(Opcode, dl, VTs, Ops);
2087 MemOp[0] = cast<MemIntrinsicSDNode>(
N)->getMemOperand();
2088 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2090 ReplaceNode(N, Result);
2096 "which are disabled");
2106 unsigned IntNo = cast<ConstantSDNode>(N->
getOperand(1))->getZExtValue();
2110 case Intrinsic::hexagon_V6_vgathermh:
2111 case Intrinsic::hexagon_V6_vgathermh_128B:
2112 Opcode = Hexagon::V6_vgathermh_pseudo;
2114 case Intrinsic::hexagon_V6_vgathermw:
2115 case Intrinsic::hexagon_V6_vgathermw_128B:
2116 Opcode = Hexagon::V6_vgathermw_pseudo;
2118 case Intrinsic::hexagon_V6_vgathermhw:
2119 case Intrinsic::hexagon_V6_vgathermhw_128B:
2120 Opcode = Hexagon::V6_vgathermhw_pseudo;
2126 SDNode *
Result = CurDAG->getMachineNode(Opcode, dl, VTs, Ops);
2129 MemOp[0] = cast<MemIntrinsicSDNode>(
N)->getMemOperand();
2130 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2132 ReplaceNode(N, Result);
2136 unsigned IID = cast<ConstantSDNode>(N->
getOperand(0))->getZExtValue();
2139 case Intrinsic::hexagon_V6_vaddcarry: {
2143 Result = CurDAG->getMachineNode(Hexagon::V6_vaddcarry,
SDLoc(N), VTs, Ops);
2146 case Intrinsic::hexagon_V6_vaddcarry_128B: {
2150 Result = CurDAG->getMachineNode(Hexagon::V6_vaddcarry,
SDLoc(N), VTs, Ops);
2153 case Intrinsic::hexagon_V6_vsubcarry: {
2157 Result = CurDAG->getMachineNode(Hexagon::V6_vsubcarry,
SDLoc(N), VTs, Ops);
2160 case Intrinsic::hexagon_V6_vsubcarry_128B: {
2164 Result = CurDAG->getMachineNode(Hexagon::V6_vsubcarry,
SDLoc(N), VTs, Ops);
2170 ReplaceUses(N, Result);
2173 CurDAG->RemoveDeadNode(N);
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&... Ranges)
Concatenated range across two or more ranges.
Pass interface - Implemented by all 'passes'.
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
const HexagonTargetLowering & Lower
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
size_type size() const
Determine the number of elements in the SetVector.
static MVT getVectorVT(MVT VT, unsigned NumElements)
HexagonDAGToDAGISel & ISel
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
void selectVAlign(SDNode *N)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
unsigned getVectorNumElements() const
Function Alias Analysis Results
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
static void dump(StringRef Title, SpillInfo const &Spills)
static bool isPermutation(ArrayRef< int > Mask)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
iterator end()
Get an iterator to the end of the SetVector.
std::pair< MCSymbol *, MachineModuleInfoImpl::StubValueTy > PairTy
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
static void splitMask(ArrayRef< int > Mask, MutableArrayRef< int > MaskL, MutableArrayRef< int > MaskR)
bool hasOneUse() const
Return true if there is exactly one use of this node.
A description of a memory reference used in the backend.
const HexagonInstrInfo * TII
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s), MachineInstr opcode, and operands.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned getSizeInBits() const
MVT getSingleVT(MVT ElemTy) const
bool insert(const value_type &X)
Insert a new element into the SetVector.
iterator begin()
Get an iterator to the beginning of the SetVector.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
virtual const TargetInstrInfo * getInstrInfo() const
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
MVT getVectorElementType() const
TargetInstrInfo - Interface to description of machine instruction set.
Error build(ArrayRef< Module *> Mods, SmallVector< char, 0 > &Symtab, StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc)
Fills in Symtab and StrtabBuilder with a valid symbol and string table for Mods.
static const HexagonTargetLowering & getHexagonLowering(SelectionDAG &G)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
constexpr bool isUInt< 8 >(uint64_t x)
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const SDValue & getOperand(unsigned Num) const
static const HexagonSubtarget & getHexagonSubtarget(SelectionDAG &G)
static std::pair< int, unsigned > findStrip(ArrayRef< int > A, int Inc, unsigned MaxLen)
void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
void selectRor(SDNode *N)
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
unsigned getNumOperands() const
Return the number of values used by this operation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const HexagonSubtarget & HST
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
This callback is invoked for operations that are unsupported by the target, which are registered to u...
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
bool use_empty() const
Return true if there are no uses of this node.
static bool isUndef(ArrayRef< int > Mask)
void dump() const
Dump this node, for debugging.
const TargetLowering & getTargetLoweringInfo() const
print lazy value Lazy Value Info Printer Pass
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Color
A "color", which is either even or odd.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Represents one node in the SelectionDAG.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
iterator_range< use_iterator > uses()
iterator insert(iterator I, T &&Elt)
amdgpu Simplify well known AMD library false Value Value * Arg
std::set< NodeId > NodeSet
HvxSelector(HexagonDAGToDAGISel &HS, SelectionDAG &G)
ArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
unsigned getVectorLength() const
pointer data()
Return a pointer to the vector's buffer, even if empty().
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
const TargetSubtargetInfo & getSubtarget() const
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
static bool isIdentity(ArrayRef< int > Mask)
void SelectV65Gather(SDNode *N)
unsigned getOpcode() const
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
void selectShuffle(SDNode *N)
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void dumpr() const
Dump (recursively) this node and its use-def subgraph.
MVT getPairVT(MVT ElemTy) const
A vector that has set insertion semantics.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
This class implements an extremely fast bulk output stream that can only output to a stream...
void ReplaceNode(SDNode *F, SDNode *T)
Replace all uses of F with T, then remove F from the DAG.
void SelectHVXDualOutput(SDNode *N)
const SDValue & getOperand(unsigned i) const
void Select(SDNode *N) override
Main hook for targets to transform nodes into machine nodes.
void SelectV65GatherPred(SDNode *N)
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
static void fail(const SDLoc &DL, SelectionDAG &DAG, const Twine &Msg)