blob: 31515788d2bf56e00b1c244d1878692464b0e7cc [file] [log] [blame]
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001//===-- HexagonISelDAGToDAGHVX.cpp ----------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Hexagon.h"
11#include "HexagonISelDAGToDAG.h"
12#include "HexagonISelLowering.h"
13#include "HexagonTargetMachine.h"
Krzysztof Parzyszeke156e9b2018-01-11 17:59:34 +000014#include "llvm/ADT/SetVector.h"
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +000015#include "llvm/CodeGen/MachineInstrBuilder.h"
16#include "llvm/CodeGen/SelectionDAGISel.h"
17#include "llvm/IR/Intrinsics.h"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/Debug.h"
20
21#include <deque>
22#include <map>
23#include <set>
24#include <utility>
25#include <vector>
26
27#define DEBUG_TYPE "hexagon-isel"
28
29using namespace llvm;
30
Benjamin Kramer802e6252017-12-24 12:46:22 +000031namespace {
32
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +000033// --------------------------------------------------------------------
34// Implementation of permutation networks.
35
36// Implementation of the node routing through butterfly networks:
37// - Forward delta.
38// - Reverse delta.
39// - Benes.
40//
41//
42// Forward delta network consists of log(N) steps, where N is the number
43// of inputs. In each step, an input can stay in place, or it can get
44// routed to another position[1]. The step after that consists of two
45// networks, each half in size in terms of the number of nodes. In those
46// terms, in the given step, an input can go to either the upper or the
47// lower network in the next step.
48//
49// [1] Hexagon's vdelta/vrdelta allow an element to be routed to both
50// positions as long as there is no conflict.
51
52// Here's a delta network for 8 inputs, only the switching routes are
53// shown:
54//
55// Steps:
56// |- 1 ---------------|- 2 -----|- 3 -|
57//
58// Inp[0] *** *** *** *** Out[0]
59// \ / \ / \ /
60// \ / \ / X
61// \ / \ / / \
62// Inp[1] *** \ / *** X *** *** Out[1]
63// \ \ / / \ / \ /
64// \ \ / / X X
65// \ \ / / / \ / \
66// Inp[2] *** \ \ / / *** X *** *** Out[2]
67// \ \ X / / / \ \ /
68// \ \ / \ / / / \ X
69// \ X X / / \ / \
70// Inp[3] *** \ / \ / \ / *** *** *** Out[3]
71// \ X X X /
72// \ / \ / \ / \ /
73// X X X X
74// / \ / \ / \ / \
75// / X X X \
76// Inp[4] *** / \ / \ / \ *** *** *** Out[4]
77// / X X \ \ / \ /
78// / / \ / \ \ \ / X
79// / / X \ \ \ / / \
80// Inp[5] *** / / \ \ *** X *** *** Out[5]
81// / / \ \ \ / \ /
82// / / \ \ X X
83// / / \ \ / \ / \
84// Inp[6] *** / \ *** X *** *** Out[6]
85// / \ / \ \ /
86// / \ / \ X
87// / \ / \ / \
88// Inp[7] *** *** *** *** Out[7]
89//
90//
91// Reverse delta network is same as delta network, with the steps in
92// the opposite order.
93//
94//
95// Benes network is a forward delta network immediately followed by
96// a reverse delta network.
97
Florian Hahn6a684b22018-01-12 20:35:45 +000098enum class ColorKind { None, Red, Black };
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +000099
100// Graph coloring utility used to partition nodes into two groups:
101// they will correspond to nodes routed to the upper and lower networks.
102struct Coloring {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000103 using Node = int;
Florian Hahn6a684b22018-01-12 20:35:45 +0000104 using MapType = std::map<Node, ColorKind>;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000105 static constexpr Node Ignore = Node(-1);
106
107 Coloring(ArrayRef<Node> Ord) : Order(Ord) {
108 build();
109 if (!color())
110 Colors.clear();
111 }
112
113 const MapType &colors() const {
114 return Colors;
115 }
116
Florian Hahn6a684b22018-01-12 20:35:45 +0000117 ColorKind other(ColorKind Color) {
118 if (Color == ColorKind::None)
119 return ColorKind::Red;
120 return Color == ColorKind::Red ? ColorKind::Black : ColorKind::Red;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000121 }
122
123 void dump() const;
124
125private:
126 ArrayRef<Node> Order;
127 MapType Colors;
128 std::set<Node> Needed;
129
130 using NodeSet = std::set<Node>;
131 std::map<Node,NodeSet> Edges;
132
133 Node conj(Node Pos) {
134 Node Num = Order.size();
135 return (Pos < Num/2) ? Pos + Num/2 : Pos - Num/2;
136 }
137
Florian Hahn6a684b22018-01-12 20:35:45 +0000138 ColorKind getColor(Node N) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000139 auto F = Colors.find(N);
Florian Hahn6a684b22018-01-12 20:35:45 +0000140 return F != Colors.end() ? F->second : ColorKind::None;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000141 }
142
Florian Hahn6a684b22018-01-12 20:35:45 +0000143 std::pair<bool, ColorKind> getUniqueColor(const NodeSet &Nodes);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000144
145 void build();
146 bool color();
147};
Benjamin Kramer802e6252017-12-24 12:46:22 +0000148} // namespace
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000149
Florian Hahn6a684b22018-01-12 20:35:45 +0000150std::pair<bool, ColorKind> Coloring::getUniqueColor(const NodeSet &Nodes) {
151 auto Color = ColorKind::None;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000152 for (Node N : Nodes) {
Florian Hahn6a684b22018-01-12 20:35:45 +0000153 ColorKind ColorN = getColor(N);
154 if (ColorN == ColorKind::None)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000155 continue;
Florian Hahn6a684b22018-01-12 20:35:45 +0000156 if (Color == ColorKind::None)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000157 Color = ColorN;
Florian Hahn6a684b22018-01-12 20:35:45 +0000158 else if (Color != ColorKind::None && Color != ColorN)
159 return { false, ColorKind::None };
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000160 }
161 return { true, Color };
162}
163
164void Coloring::build() {
165 // Add Order[P] and Order[conj(P)] to Edges.
166 for (unsigned P = 0; P != Order.size(); ++P) {
167 Node I = Order[P];
168 if (I != Ignore) {
169 Needed.insert(I);
170 Node PC = Order[conj(P)];
171 if (PC != Ignore && PC != I)
172 Edges[I].insert(PC);
173 }
174 }
175 // Add I and conj(I) to Edges.
176 for (unsigned I = 0; I != Order.size(); ++I) {
177 if (!Needed.count(I))
178 continue;
179 Node C = conj(I);
180 // This will create an entry in the edge table, even if I is not
181 // connected to any other node. This is necessary, because it still
182 // needs to be colored.
183 NodeSet &Is = Edges[I];
184 if (Needed.count(C))
185 Is.insert(C);
186 }
187}
188
189bool Coloring::color() {
190 SetVector<Node> FirstQ;
191 auto Enqueue = [this,&FirstQ] (Node N) {
192 SetVector<Node> Q;
193 Q.insert(N);
194 for (unsigned I = 0; I != Q.size(); ++I) {
195 NodeSet &Ns = Edges[Q[I]];
196 Q.insert(Ns.begin(), Ns.end());
197 }
198 FirstQ.insert(Q.begin(), Q.end());
199 };
200 for (Node N : Needed)
201 Enqueue(N);
202
203 for (Node N : FirstQ) {
204 if (Colors.count(N))
205 continue;
206 NodeSet &Ns = Edges[N];
207 auto P = getUniqueColor(Ns);
208 if (!P.first)
209 return false;
210 Colors[N] = other(P.second);
211 }
212
213 // First, color nodes that don't have any dups.
214 for (auto E : Edges) {
215 Node N = E.first;
216 if (!Needed.count(conj(N)) || Colors.count(N))
217 continue;
218 auto P = getUniqueColor(E.second);
219 if (!P.first)
220 return false;
221 Colors[N] = other(P.second);
222 }
223
224 // Now, nodes that are still uncolored. Since the graph can be modified
225 // in this step, create a work queue.
226 std::vector<Node> WorkQ;
227 for (auto E : Edges) {
228 Node N = E.first;
229 if (!Colors.count(N))
230 WorkQ.push_back(N);
231 }
232
233 for (unsigned I = 0; I < WorkQ.size(); ++I) {
234 Node N = WorkQ[I];
235 NodeSet &Ns = Edges[N];
236 auto P = getUniqueColor(Ns);
237 if (P.first) {
238 Colors[N] = other(P.second);
239 continue;
240 }
241
242 // Coloring failed. Split this node.
243 Node C = conj(N);
Florian Hahn6a684b22018-01-12 20:35:45 +0000244 ColorKind ColorN = other(ColorKind::None);
245 ColorKind ColorC = other(ColorN);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000246 NodeSet &Cs = Edges[C];
247 NodeSet CopyNs = Ns;
248 for (Node M : CopyNs) {
Florian Hahn6a684b22018-01-12 20:35:45 +0000249 ColorKind ColorM = getColor(M);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000250 if (ColorM == ColorC) {
251 // Connect M with C, disconnect M from N.
252 Cs.insert(M);
253 Edges[M].insert(C);
254 Ns.erase(M);
255 Edges[M].erase(N);
256 }
257 }
258 Colors[N] = ColorN;
259 Colors[C] = ColorC;
260 }
261
Fangrui Song956ee792018-03-30 22:22:31 +0000262 // Explicitly assign "None" to all uncolored nodes.
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000263 for (unsigned I = 0; I != Order.size(); ++I)
264 if (Colors.count(I) == 0)
Florian Hahn6a684b22018-01-12 20:35:45 +0000265 Colors[I] = ColorKind::None;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000266
267 return true;
268}
269
270LLVM_DUMP_METHOD
271void Coloring::dump() const {
272 dbgs() << "{ Order: {";
273 for (unsigned I = 0; I != Order.size(); ++I) {
274 Node P = Order[I];
275 if (P != Ignore)
276 dbgs() << ' ' << P;
277 else
278 dbgs() << " -";
279 }
280 dbgs() << " }\n";
281 dbgs() << " Needed: {";
282 for (Node N : Needed)
283 dbgs() << ' ' << N;
284 dbgs() << " }\n";
285
286 dbgs() << " Edges: {\n";
287 for (auto E : Edges) {
288 dbgs() << " " << E.first << " -> {";
289 for (auto N : E.second)
290 dbgs() << ' ' << N;
291 dbgs() << " }\n";
292 }
293 dbgs() << " }\n";
294
Florian Hahn6a684b22018-01-12 20:35:45 +0000295 auto ColorKindToName = [](ColorKind C) {
296 switch (C) {
297 case ColorKind::None:
298 return "None";
299 case ColorKind::Red:
300 return "Red";
301 case ColorKind::Black:
302 return "Black";
303 }
304 llvm_unreachable("all ColorKinds should be handled by the switch above");
305 };
306
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000307 dbgs() << " Colors: {\n";
308 for (auto C : Colors)
Florian Hahn6a684b22018-01-12 20:35:45 +0000309 dbgs() << " " << C.first << " -> " << ColorKindToName(C.second) << "\n";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000310 dbgs() << " }\n}\n";
311}
312
Benjamin Kramer802e6252017-12-24 12:46:22 +0000313namespace {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000314// Base class of for reordering networks. They don't strictly need to be
315// permutations, as outputs with repeated occurrences of an input element
316// are allowed.
317struct PermNetwork {
318 using Controls = std::vector<uint8_t>;
319 using ElemType = int;
320 static constexpr ElemType Ignore = ElemType(-1);
321
322 enum : uint8_t {
323 None,
324 Pass,
325 Switch
326 };
327 enum : uint8_t {
328 Forward,
329 Reverse
330 };
331
332 PermNetwork(ArrayRef<ElemType> Ord, unsigned Mult = 1) {
333 Order.assign(Ord.data(), Ord.data()+Ord.size());
334 Log = 0;
335
336 unsigned S = Order.size();
337 while (S >>= 1)
338 ++Log;
339
340 Table.resize(Order.size());
341 for (RowType &Row : Table)
342 Row.resize(Mult*Log, None);
343 }
344
345 void getControls(Controls &V, unsigned StartAt, uint8_t Dir) const {
346 unsigned Size = Order.size();
347 V.resize(Size);
348 for (unsigned I = 0; I != Size; ++I) {
349 unsigned W = 0;
350 for (unsigned L = 0; L != Log; ++L) {
351 unsigned C = ctl(I, StartAt+L) == Switch;
352 if (Dir == Forward)
353 W |= C << (Log-1-L);
354 else
355 W |= C << L;
356 }
357 assert(isUInt<8>(W));
358 V[I] = uint8_t(W);
359 }
360 }
361
362 uint8_t ctl(ElemType Pos, unsigned Step) const {
363 return Table[Pos][Step];
364 }
365 unsigned size() const {
366 return Order.size();
367 }
368 unsigned steps() const {
369 return Log;
370 }
371
372protected:
373 unsigned Log;
374 std::vector<ElemType> Order;
375 using RowType = std::vector<uint8_t>;
376 std::vector<RowType> Table;
377};
378
379struct ForwardDeltaNetwork : public PermNetwork {
380 ForwardDeltaNetwork(ArrayRef<ElemType> Ord) : PermNetwork(Ord) {}
381
382 bool run(Controls &V) {
383 if (!route(Order.data(), Table.data(), size(), 0))
384 return false;
385 getControls(V, 0, Forward);
386 return true;
387 }
388
389private:
390 bool route(ElemType *P, RowType *T, unsigned Size, unsigned Step);
391};
392
393struct ReverseDeltaNetwork : public PermNetwork {
394 ReverseDeltaNetwork(ArrayRef<ElemType> Ord) : PermNetwork(Ord) {}
395
396 bool run(Controls &V) {
397 if (!route(Order.data(), Table.data(), size(), 0))
398 return false;
399 getControls(V, 0, Reverse);
400 return true;
401 }
402
403private:
404 bool route(ElemType *P, RowType *T, unsigned Size, unsigned Step);
405};
406
407struct BenesNetwork : public PermNetwork {
408 BenesNetwork(ArrayRef<ElemType> Ord) : PermNetwork(Ord, 2) {}
409
410 bool run(Controls &F, Controls &R) {
411 if (!route(Order.data(), Table.data(), size(), 0))
412 return false;
413
414 getControls(F, 0, Forward);
415 getControls(R, Log, Reverse);
416 return true;
417 }
418
419private:
420 bool route(ElemType *P, RowType *T, unsigned Size, unsigned Step);
421};
Benjamin Kramer802e6252017-12-24 12:46:22 +0000422} // namespace
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000423
424bool ForwardDeltaNetwork::route(ElemType *P, RowType *T, unsigned Size,
425 unsigned Step) {
426 bool UseUp = false, UseDown = false;
427 ElemType Num = Size;
428
429 // Cannot use coloring here, because coloring is used to determine
430 // the "big" switch, i.e. the one that changes halves, and in a forward
431 // network, a color can be simultaneously routed to both halves in the
432 // step we're working on.
433 for (ElemType J = 0; J != Num; ++J) {
434 ElemType I = P[J];
435 // I is the position in the input,
436 // J is the position in the output.
437 if (I == Ignore)
438 continue;
439 uint8_t S;
440 if (I < Num/2)
441 S = (J < Num/2) ? Pass : Switch;
442 else
443 S = (J < Num/2) ? Switch : Pass;
444
445 // U is the element in the table that needs to be updated.
446 ElemType U = (S == Pass) ? I : (I < Num/2 ? I+Num/2 : I-Num/2);
447 if (U < Num/2)
448 UseUp = true;
449 else
450 UseDown = true;
451 if (T[U][Step] != S && T[U][Step] != None)
452 return false;
453 T[U][Step] = S;
454 }
455
456 for (ElemType J = 0; J != Num; ++J)
457 if (P[J] != Ignore && P[J] >= Num/2)
458 P[J] -= Num/2;
459
460 if (Step+1 < Log) {
461 if (UseUp && !route(P, T, Size/2, Step+1))
462 return false;
463 if (UseDown && !route(P+Size/2, T+Size/2, Size/2, Step+1))
464 return false;
465 }
466 return true;
467}
468
469bool ReverseDeltaNetwork::route(ElemType *P, RowType *T, unsigned Size,
470 unsigned Step) {
471 unsigned Pets = Log-1 - Step;
472 bool UseUp = false, UseDown = false;
473 ElemType Num = Size;
474
475 // In this step half-switching occurs, so coloring can be used.
476 Coloring G({P,Size});
477 const Coloring::MapType &M = G.colors();
478 if (M.empty())
479 return false;
480
Florian Hahn6a684b22018-01-12 20:35:45 +0000481 ColorKind ColorUp = ColorKind::None;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000482 for (ElemType J = 0; J != Num; ++J) {
483 ElemType I = P[J];
484 // I is the position in the input,
485 // J is the position in the output.
486 if (I == Ignore)
487 continue;
Florian Hahn6a684b22018-01-12 20:35:45 +0000488 ColorKind C = M.at(I);
489 if (C == ColorKind::None)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000490 continue;
491 // During "Step", inputs cannot switch halves, so if the "up" color
492 // is still unknown, make sure that it is selected in such a way that
493 // "I" will stay in the same half.
494 bool InpUp = I < Num/2;
Florian Hahn6a684b22018-01-12 20:35:45 +0000495 if (ColorUp == ColorKind::None)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000496 ColorUp = InpUp ? C : G.other(C);
497 if ((C == ColorUp) != InpUp) {
498 // If I should go to a different half than where is it now, give up.
499 return false;
500 }
501
502 uint8_t S;
503 if (InpUp) {
504 S = (J < Num/2) ? Pass : Switch;
505 UseUp = true;
506 } else {
507 S = (J < Num/2) ? Switch : Pass;
508 UseDown = true;
509 }
510 T[J][Pets] = S;
511 }
512
513 // Reorder the working permutation according to the computed switch table
514 // for the last step (i.e. Pets).
Simon Pilgrim3d0be4f2017-12-09 16:04:57 +0000515 for (ElemType J = 0, E = Size / 2; J != E; ++J) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000516 ElemType PJ = P[J]; // Current values of P[J]
517 ElemType PC = P[J+Size/2]; // and P[conj(J)]
518 ElemType QJ = PJ; // New values of P[J]
519 ElemType QC = PC; // and P[conj(J)]
520 if (T[J][Pets] == Switch)
521 QC = PJ;
522 if (T[J+Size/2][Pets] == Switch)
523 QJ = PC;
524 P[J] = QJ;
525 P[J+Size/2] = QC;
526 }
527
528 for (ElemType J = 0; J != Num; ++J)
529 if (P[J] != Ignore && P[J] >= Num/2)
530 P[J] -= Num/2;
531
532 if (Step+1 < Log) {
533 if (UseUp && !route(P, T, Size/2, Step+1))
534 return false;
535 if (UseDown && !route(P+Size/2, T+Size/2, Size/2, Step+1))
536 return false;
537 }
538 return true;
539}
540
541bool BenesNetwork::route(ElemType *P, RowType *T, unsigned Size,
542 unsigned Step) {
543 Coloring G({P,Size});
544 const Coloring::MapType &M = G.colors();
545 if (M.empty())
546 return false;
547 ElemType Num = Size;
548
549 unsigned Pets = 2*Log-1 - Step;
550 bool UseUp = false, UseDown = false;
551
552 // Both assignments, i.e. Red->Up and Red->Down are valid, but they will
553 // result in different controls. Let's pick the one where the first
554 // control will be "Pass".
Florian Hahn6a684b22018-01-12 20:35:45 +0000555 ColorKind ColorUp = ColorKind::None;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000556 for (ElemType J = 0; J != Num; ++J) {
557 ElemType I = P[J];
558 if (I == Ignore)
559 continue;
Florian Hahn6a684b22018-01-12 20:35:45 +0000560 ColorKind C = M.at(I);
561 if (C == ColorKind::None)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000562 continue;
Florian Hahn6a684b22018-01-12 20:35:45 +0000563 if (ColorUp == ColorKind::None) {
564 ColorUp = (I < Num / 2) ? ColorKind::Red : ColorKind::Black;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000565 }
566 unsigned CI = (I < Num/2) ? I+Num/2 : I-Num/2;
567 if (C == ColorUp) {
568 if (I < Num/2)
569 T[I][Step] = Pass;
570 else
571 T[CI][Step] = Switch;
572 T[J][Pets] = (J < Num/2) ? Pass : Switch;
573 UseUp = true;
574 } else { // Down
575 if (I < Num/2)
576 T[CI][Step] = Switch;
577 else
578 T[I][Step] = Pass;
579 T[J][Pets] = (J < Num/2) ? Switch : Pass;
580 UseDown = true;
581 }
582 }
583
584 // Reorder the working permutation according to the computed switch table
585 // for the last step (i.e. Pets).
586 for (ElemType J = 0; J != Num/2; ++J) {
587 ElemType PJ = P[J]; // Current values of P[J]
588 ElemType PC = P[J+Num/2]; // and P[conj(J)]
589 ElemType QJ = PJ; // New values of P[J]
590 ElemType QC = PC; // and P[conj(J)]
591 if (T[J][Pets] == Switch)
592 QC = PJ;
593 if (T[J+Num/2][Pets] == Switch)
594 QJ = PC;
595 P[J] = QJ;
596 P[J+Num/2] = QC;
597 }
598
599 for (ElemType J = 0; J != Num; ++J)
600 if (P[J] != Ignore && P[J] >= Num/2)
601 P[J] -= Num/2;
602
603 if (Step+1 < Log) {
604 if (UseUp && !route(P, T, Size/2, Step+1))
605 return false;
606 if (UseDown && !route(P+Size/2, T+Size/2, Size/2, Step+1))
607 return false;
608 }
609 return true;
610}
611
612// --------------------------------------------------------------------
613// Support for building selection results (output instructions that are
614// parts of the final selection).
615
Benjamin Kramer802e6252017-12-24 12:46:22 +0000616namespace {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000617struct OpRef {
618 OpRef(SDValue V) : OpV(V) {}
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); }
623
624 static OpRef lo(const OpRef &R) {
625 assert(!R.isValue());
626 return OpRef(R.OpN & (Undef | Index | LoHalf));
627 }
628 static OpRef hi(const OpRef &R) {
629 assert(!R.isValue());
630 return OpRef(R.OpN & (Undef | Index | HiHalf));
631 }
632 static OpRef undef(MVT Ty) { return OpRef(Undef | Ty.SimpleTy); }
633
634 // Direct value.
635 SDValue OpV = SDValue();
636
637 // Reference to the operand of the input node:
638 // If the 31st bit is 1, it's undef, otherwise, bits 28..0 are the
639 // operand index:
640 // If bit 30 is set, it's the high half of the operand.
641 // If bit 29 is set, it's the low half of the operand.
642 unsigned OpN = 0;
643
644 enum : unsigned {
645 Invalid = 0x10000000,
646 LoHalf = 0x20000000,
647 HiHalf = 0x40000000,
648 Whole = LoHalf | HiHalf,
649 Undef = 0x80000000,
650 Index = 0x0FFFFFFF, // Mask of the index value.
651 IndexBits = 28,
652 };
653
654 void print(raw_ostream &OS, const SelectionDAG &G) const;
655
656private:
657 OpRef(unsigned N) : OpN(N) {}
658};
659
660struct NodeTemplate {
661 NodeTemplate() = default;
662 unsigned Opc = 0;
663 MVT Ty = MVT::Other;
664 std::vector<OpRef> Ops;
665
666 void print(raw_ostream &OS, const SelectionDAG &G) const;
667};
668
669struct ResultStack {
670 ResultStack(SDNode *Inp)
671 : InpNode(Inp), InpTy(Inp->getValueType(0).getSimpleVT()) {}
672 SDNode *InpNode;
673 MVT InpTy;
674 unsigned push(const NodeTemplate &Res) {
675 List.push_back(Res);
676 return List.size()-1;
677 }
678 unsigned push(unsigned Opc, MVT Ty, std::vector<OpRef> &&Ops) {
679 NodeTemplate Res;
680 Res.Opc = Opc;
681 Res.Ty = Ty;
682 Res.Ops = Ops;
683 return push(Res);
684 }
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);
691 return NewTop;
692 }
693
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(); }
699
700 BaseType List;
701
702 void print(raw_ostream &OS, const SelectionDAG &G) const;
703};
Benjamin Kramer802e6252017-12-24 12:46:22 +0000704} // namespace
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000705
706void OpRef::print(raw_ostream &OS, const SelectionDAG &G) const {
707 if (isValue()) {
708 OpV.getNode()->print(OS, &G);
709 return;
710 }
711 if (OpN & Invalid) {
712 OS << "invalid";
713 return;
714 }
715 if (OpN & Undef) {
716 OS << "undef";
717 return;
718 }
719 if ((OpN & Whole) != Whole) {
720 assert((OpN & Whole) == LoHalf || (OpN & Whole) == HiHalf);
721 if (OpN & LoHalf)
722 OS << "lo ";
723 else
724 OS << "hi ";
725 }
726 OS << '#' << SignExtend32(OpN & Index, IndexBits);
727}
728
729void NodeTemplate::print(raw_ostream &OS, const SelectionDAG &G) const {
730 const TargetInstrInfo &TII = *G.getSubtarget().getInstrInfo();
731 OS << format("%8s", EVT(Ty).getEVTString().c_str()) << " "
732 << TII.getName(Opc);
733 bool Comma = false;
734 for (const auto &R : Ops) {
735 if (Comma)
736 OS << ',';
737 Comma = true;
738 OS << ' ';
739 R.print(OS, G);
740 }
741}
742
743void ResultStack::print(raw_ostream &OS, const SelectionDAG &G) const {
744 OS << "Input node:\n";
Davide Italiano9c60c7d2017-12-06 18:54:17 +0000745#ifndef NDEBUG
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000746 InpNode->dumpr(&G);
Davide Italiano9c60c7d2017-12-06 18:54:17 +0000747#endif
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000748 OS << "Result templates:\n";
749 for (unsigned I = 0, E = List.size(); I != E; ++I) {
750 OS << '[' << I << "] ";
751 List[I].print(OS, G);
752 OS << '\n';
753 }
754}
755
Benjamin Kramer802e6252017-12-24 12:46:22 +0000756namespace {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000757struct ShuffleMask {
758 ShuffleMask(ArrayRef<int> M) : Mask(M) {
759 for (unsigned I = 0, E = Mask.size(); I != E; ++I) {
760 int M = Mask[I];
761 if (M == -1)
762 continue;
763 MinSrc = (MinSrc == -1) ? M : std::min(MinSrc, M);
764 MaxSrc = (MaxSrc == -1) ? M : std::max(MaxSrc, M);
765 }
766 }
767
768 ArrayRef<int> Mask;
769 int MinSrc = -1, MaxSrc = -1;
770
771 ShuffleMask lo() const {
772 size_t H = Mask.size()/2;
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +0000773 return ShuffleMask(Mask.take_front(H));
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000774 }
775 ShuffleMask hi() const {
776 size_t H = Mask.size()/2;
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +0000777 return ShuffleMask(Mask.take_back(H));
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000778 }
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +0000779
780 void print(raw_ostream &OS) const {
781 OS << "MinSrc:" << MinSrc << ", MaxSrc:" << MaxSrc << " {";
782 for (int M : Mask)
783 OS << ' ' << M;
784 OS << " }";
785 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000786};
Benjamin Kramer802e6252017-12-24 12:46:22 +0000787} // namespace
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000788
789// --------------------------------------------------------------------
790// The HvxSelector class.
791
792static const HexagonTargetLowering &getHexagonLowering(SelectionDAG &G) {
793 return static_cast<const HexagonTargetLowering&>(G.getTargetLoweringInfo());
794}
795static const HexagonSubtarget &getHexagonSubtarget(SelectionDAG &G) {
796 return static_cast<const HexagonSubtarget&>(G.getSubtarget());
797}
798
799namespace llvm {
800 struct HvxSelector {
801 const HexagonTargetLowering &Lower;
802 HexagonDAGToDAGISel &ISel;
803 SelectionDAG &DAG;
804 const HexagonSubtarget &HST;
805 const unsigned HwLen;
806
807 HvxSelector(HexagonDAGToDAGISel &HS, SelectionDAG &G)
808 : Lower(getHexagonLowering(G)), ISel(HS), DAG(G),
809 HST(getHexagonSubtarget(G)), HwLen(HST.getVectorLength()) {}
810
811 MVT getSingleVT(MVT ElemTy) const {
812 unsigned NumElems = HwLen / (ElemTy.getSizeInBits()/8);
813 return MVT::getVectorVT(ElemTy, NumElems);
814 }
815
816 MVT getPairVT(MVT ElemTy) const {
817 unsigned NumElems = (2*HwLen) / (ElemTy.getSizeInBits()/8);
818 return MVT::getVectorVT(ElemTy, NumElems);
819 }
820
821 void selectShuffle(SDNode *N);
822 void selectRor(SDNode *N);
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000823 void selectVAlign(SDNode *N);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000824
825 private:
826 void materialize(const ResultStack &Results);
827
828 SDValue getVectorConstant(ArrayRef<uint8_t> Data, const SDLoc &dl);
829
830 enum : unsigned {
831 None,
832 PackMux,
833 };
834 OpRef concat(OpRef Va, OpRef Vb, ResultStack &Results);
835 OpRef packs(ShuffleMask SM, OpRef Va, OpRef Vb, ResultStack &Results,
836 MutableArrayRef<int> NewMask, unsigned Options = None);
837 OpRef packp(ShuffleMask SM, OpRef Va, OpRef Vb, ResultStack &Results,
838 MutableArrayRef<int> NewMask);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000839 OpRef vmuxs(ArrayRef<uint8_t> Bytes, OpRef Va, OpRef Vb,
840 ResultStack &Results);
841 OpRef vmuxp(ArrayRef<uint8_t> Bytes, OpRef Va, OpRef Vb,
842 ResultStack &Results);
843
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);
848
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);
853
854 bool selectVectorConstants(SDNode *N);
855 bool scalarizeShuffle(ArrayRef<int> Mask, const SDLoc &dl, MVT ResTy,
856 SDValue Va, SDValue Vb, SDNode *N);
857
858 };
859}
860
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000861static void splitMask(ArrayRef<int> Mask, MutableArrayRef<int> MaskL,
862 MutableArrayRef<int> MaskR) {
863 unsigned VecLen = Mask.size();
864 assert(MaskL.size() == VecLen && MaskR.size() == VecLen);
865 for (unsigned I = 0; I != VecLen; ++I) {
866 int M = Mask[I];
867 if (M < 0) {
868 MaskL[I] = MaskR[I] = -1;
869 } else if (unsigned(M) < VecLen) {
870 MaskL[I] = M;
871 MaskR[I] = -1;
872 } else {
873 MaskL[I] = -1;
874 MaskR[I] = M-VecLen;
875 }
876 }
877}
878
879static std::pair<int,unsigned> findStrip(ArrayRef<int> A, int Inc,
880 unsigned MaxLen) {
881 assert(A.size() > 0 && A.size() >= MaxLen);
882 int F = A[0];
883 int E = F;
884 for (unsigned I = 1; I != MaxLen; ++I) {
885 if (A[I] - E != Inc)
886 return { F, I };
887 E = A[I];
888 }
889 return { F, MaxLen };
890}
891
892static bool isUndef(ArrayRef<int> Mask) {
893 for (int Idx : Mask)
894 if (Idx != -1)
895 return false;
896 return true;
897}
898
899static bool isIdentity(ArrayRef<int> Mask) {
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +0000900 for (int I = 0, E = Mask.size(); I != E; ++I) {
901 int M = Mask[I];
902 if (M >= 0 && M != I)
903 return false;
904 }
905 return true;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000906}
907
908static bool isPermutation(ArrayRef<int> Mask) {
909 // Check by adding all numbers only works if there is no overflow.
910 assert(Mask.size() < 0x00007FFF && "Sanity failure");
911 int Sum = 0;
912 for (int Idx : Mask) {
913 if (Idx == -1)
914 return false;
915 Sum += Idx;
916 }
917 int N = Mask.size();
918 return 2*Sum == N*(N-1);
919}
920
921bool HvxSelector::selectVectorConstants(SDNode *N) {
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +0000922 // Constant vectors are generated as loads from constant pools or as
923 // splats of a constant value. Since they are generated during the
924 // selection process, the main selection algorithm is not aware of them.
925 // Select them directly here.
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000926 SmallVector<SDNode*,4> Nodes;
Krzysztof Parzyszeke156e9b2018-01-11 17:59:34 +0000927 SetVector<SDNode*> WorkQ;
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000928
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +0000929 // The one-use test for VSPLATW's operand may fail due to dead nodes
930 // left over in the DAG.
931 DAG.RemoveDeadNodes();
932
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000933 // The DAG can change (due to CSE) during selection, so cache all the
934 // unselected nodes first to avoid traversing a mutating DAG.
935
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000936 auto IsNodeToSelect = [] (SDNode *N) {
937 if (N->isMachineOpcode())
938 return false;
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +0000939 switch (N->getOpcode()) {
940 case HexagonISD::VZERO:
941 case HexagonISD::VSPLATW:
Krzysztof Parzyszek8cc636c2018-01-31 16:48:20 +0000942 return true;
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +0000943 case ISD::LOAD: {
944 SDValue Addr = cast<LoadSDNode>(N)->getBasePtr();
945 unsigned AddrOpc = Addr.getOpcode();
946 if (AddrOpc == HexagonISD::AT_PCREL || AddrOpc == HexagonISD::CP)
947 if (Addr.getOperand(0).getOpcode() == ISD::TargetConstantPool)
948 return true;
949 }
950 break;
Krzysztof Parzyszek8cc636c2018-01-31 16:48:20 +0000951 }
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +0000952 // Make sure to select the operand of VSPLATW.
953 bool IsSplatOp = N->hasOneUse() &&
954 N->use_begin()->getOpcode() == HexagonISD::VSPLATW;
955 return IsSplatOp;
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000956 };
957
Krzysztof Parzyszeke156e9b2018-01-11 17:59:34 +0000958 WorkQ.insert(N);
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000959 for (unsigned i = 0; i != WorkQ.size(); ++i) {
960 SDNode *W = WorkQ[i];
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000961 if (IsNodeToSelect(W))
962 Nodes.push_back(W);
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000963 for (unsigned j = 0, f = W->getNumOperands(); j != f; ++j)
Krzysztof Parzyszeke156e9b2018-01-11 17:59:34 +0000964 WorkQ.insert(W->getOperand(j).getNode());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000965 }
966
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000967 for (SDNode *L : Nodes)
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000968 ISel.Select(L);
969
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000970 return !Nodes.empty();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000971}
972
973void HvxSelector::materialize(const ResultStack &Results) {
974 DEBUG_WITH_TYPE("isel", {
975 dbgs() << "Materializing\n";
976 Results.print(dbgs(), DAG);
977 });
978 if (Results.empty())
979 return;
980 const SDLoc &dl(Results.InpNode);
981 std::vector<SDValue> Output;
982
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) {
987 assert(R.isValid());
988 if (R.isValue()) {
989 Ops.push_back(R.OpV);
990 continue;
991 }
992 if (R.OpN & OpRef::Undef) {
993 MVT::SimpleValueType SVT = MVT::SimpleValueType(R.OpN & OpRef::Index);
994 Ops.push_back(ISel.selectUndef(dl, MVT(SVT)));
995 continue;
996 }
997 // R is an index of a result.
998 unsigned Part = R.OpN & OpRef::Whole;
999 int Idx = SignExtend32(R.OpN & OpRef::Index, OpRef::IndexBits);
1000 if (Idx < 0)
1001 Idx += I;
1002 assert(Idx >= 0 && unsigned(Idx) < Output.size());
1003 SDValue Op = Output[Idx];
1004 MVT OpTy = Op.getValueType().getSimpleVT();
1005 if (Part != OpRef::Whole) {
1006 assert(Part == OpRef::LoHalf || Part == OpRef::HiHalf);
Krzysztof Parzyszek5aef4b52018-01-24 14:07:37 +00001007 MVT HalfTy = MVT::getVectorVT(OpTy.getVectorElementType(),
1008 OpTy.getVectorNumElements()/2);
1009 unsigned Sub = (Part == OpRef::LoHalf) ? Hexagon::vsub_lo
1010 : Hexagon::vsub_hi;
1011 Op = DAG.getTargetExtractSubreg(Sub, dl, HalfTy, Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001012 }
1013 Ops.push_back(Op);
1014 } // for (Node : Results)
1015
1016 assert(Node.Ty != MVT::Other);
1017 SDNode *ResN = (Node.Opc == TargetOpcode::COPY)
1018 ? Ops.front().getNode()
1019 : DAG.getMachineNode(Node.Opc, dl, Node.Ty, Ops);
1020 Output.push_back(SDValue(ResN, 0));
1021 }
1022
1023 SDNode *OutN = Output.back().getNode();
1024 SDNode *InpN = Results.InpNode;
1025 DEBUG_WITH_TYPE("isel", {
1026 dbgs() << "Generated node:\n";
1027 OutN->dumpr(&DAG);
1028 });
1029
1030 ISel.ReplaceNode(InpN, OutN);
1031 selectVectorConstants(OutN);
1032 DAG.RemoveDeadNodes();
1033}
1034
1035OpRef HvxSelector::concat(OpRef Lo, OpRef Hi, ResultStack &Results) {
1036 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1037 const SDLoc &dl(Results.InpNode);
1038 Results.push(TargetOpcode::REG_SEQUENCE, getPairVT(MVT::i8), {
1039 DAG.getTargetConstant(Hexagon::HvxWRRegClassID, dl, MVT::i32),
1040 Lo, DAG.getTargetConstant(Hexagon::vsub_lo, dl, MVT::i32),
1041 Hi, DAG.getTargetConstant(Hexagon::vsub_hi, dl, MVT::i32),
1042 });
1043 return OpRef::res(Results.top());
1044}
1045
1046// Va, Vb are single vectors, SM can be arbitrarily long.
1047OpRef HvxSelector::packs(ShuffleMask SM, OpRef Va, OpRef Vb,
1048 ResultStack &Results, MutableArrayRef<int> NewMask,
1049 unsigned Options) {
1050 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1051 if (!Va.isValid() || !Vb.isValid())
1052 return OpRef::fail();
1053
1054 int VecLen = SM.Mask.size();
1055 MVT Ty = getSingleVT(MVT::i8);
1056
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +00001057 auto IsSubvector = [] (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)
1061 return false;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001062 }
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +00001063 return true;
1064 };
1065
1066 if (SM.MaxSrc - SM.MinSrc < int(HwLen)) {
1067 if (SM.MinSrc == 0 || SM.MinSrc == int(HwLen) || !IsSubvector(SM)) {
Krzysztof Parzyszek95b07352018-05-25 14:53:14 +00001068 // If the mask picks elements from only one of the operands, return
1069 // that operand, and update the mask to use index 0 to refer to the
1070 // first element of that operand.
1071 // If the mask selects a subvector, it will be handled below, so
1072 // skip it here.
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +00001073 if (SM.MaxSrc < int(HwLen)) {
1074 memcpy(NewMask.data(), SM.Mask.data(), sizeof(int)*VecLen);
1075 return Va;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001076 }
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +00001077 if (SM.MinSrc >= int(HwLen)) {
1078 for (int I = 0; I != VecLen; ++I) {
1079 int M = SM.Mask[I];
1080 if (M != -1)
1081 M -= HwLen;
1082 NewMask[I] = M;
1083 }
1084 return Vb;
1085 }
1086 }
Krzysztof Parzyszek95b07352018-05-25 14:53:14 +00001087 int MinSrc = SM.MinSrc;
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +00001088 if (SM.MaxSrc < int(HwLen)) {
1089 Vb = Va;
1090 } else if (SM.MinSrc > int(HwLen)) {
1091 Va = Vb;
Krzysztof Parzyszek95b07352018-05-25 14:53:14 +00001092 MinSrc = SM.MinSrc - HwLen;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001093 }
1094 const SDLoc &dl(Results.InpNode);
Krzysztof Parzyszek95b07352018-05-25 14:53:14 +00001095 SDValue S = DAG.getTargetConstant(MinSrc, dl, MVT::i32);
1096 if (isUInt<3>(MinSrc)) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001097 Results.push(Hexagon::V6_valignbi, Ty, {Vb, Va, S});
1098 } else {
1099 Results.push(Hexagon::A2_tfrsi, MVT::i32, {S});
1100 unsigned Top = Results.top();
1101 Results.push(Hexagon::V6_valignb, Ty, {Vb, Va, OpRef::res(Top)});
1102 }
1103 for (int I = 0; I != VecLen; ++I) {
1104 int M = SM.Mask[I];
1105 if (M != -1)
1106 M -= SM.MinSrc;
1107 NewMask[I] = M;
1108 }
1109 return OpRef::res(Results.top());
1110 }
1111
1112 if (Options & PackMux) {
1113 // If elements picked from Va and Vb have all different (source) indexes
1114 // (relative to the start of the argument), do a mux, and update the mask.
1115 BitVector Picked(HwLen);
1116 SmallVector<uint8_t,128> MuxBytes(HwLen);
1117 bool CanMux = true;
1118 for (int I = 0; I != VecLen; ++I) {
1119 int M = SM.Mask[I];
1120 if (M == -1)
1121 continue;
1122 if (M >= int(HwLen))
1123 M -= HwLen;
1124 else
1125 MuxBytes[M] = 0xFF;
1126 if (Picked[M]) {
1127 CanMux = false;
1128 break;
1129 }
1130 NewMask[I] = M;
1131 }
1132 if (CanMux)
1133 return vmuxs(MuxBytes, Va, Vb, Results);
1134 }
1135
1136 return OpRef::fail();
1137}
1138
1139OpRef HvxSelector::packp(ShuffleMask SM, OpRef Va, OpRef Vb,
1140 ResultStack &Results, MutableArrayRef<int> NewMask) {
1141 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1142 unsigned HalfMask = 0;
1143 unsigned LogHw = Log2_32(HwLen);
1144 for (int M : SM.Mask) {
1145 if (M == -1)
1146 continue;
1147 HalfMask |= (1u << (M >> LogHw));
1148 }
1149
1150 if (HalfMask == 0)
1151 return OpRef::undef(getPairVT(MVT::i8));
1152
1153 // If more than two halves are used, bail.
1154 // TODO: be more aggressive here?
1155 if (countPopulation(HalfMask) > 2)
1156 return OpRef::fail();
1157
1158 MVT HalfTy = getSingleVT(MVT::i8);
1159
1160 OpRef Inp[2] = { Va, Vb };
1161 OpRef Out[2] = { OpRef::undef(HalfTy), OpRef::undef(HalfTy) };
1162
1163 uint8_t HalfIdx[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
1164 unsigned Idx = 0;
1165 for (unsigned I = 0; I != 4; ++I) {
1166 if ((HalfMask & (1u << I)) == 0)
1167 continue;
1168 assert(Idx < 2);
1169 OpRef Op = Inp[I/2];
1170 Out[Idx] = (I & 1) ? OpRef::hi(Op) : OpRef::lo(Op);
1171 HalfIdx[I] = Idx++;
1172 }
1173
1174 int VecLen = SM.Mask.size();
1175 for (int I = 0; I != VecLen; ++I) {
1176 int M = SM.Mask[I];
1177 if (M >= 0) {
1178 uint8_t Idx = HalfIdx[M >> LogHw];
1179 assert(Idx == 0 || Idx == 1);
1180 M = (M & (HwLen-1)) + HwLen*Idx;
1181 }
1182 NewMask[I] = M;
1183 }
1184
1185 return concat(Out[0], Out[1], Results);
1186}
1187
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001188OpRef HvxSelector::vmuxs(ArrayRef<uint8_t> Bytes, OpRef Va, OpRef Vb,
1189 ResultStack &Results) {
1190 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1191 MVT ByteTy = getSingleVT(MVT::i8);
1192 MVT BoolTy = MVT::getVectorVT(MVT::i1, 8*HwLen); // XXX
1193 const SDLoc &dl(Results.InpNode);
1194 SDValue B = getVectorConstant(Bytes, dl);
1195 Results.push(Hexagon::V6_vd0, ByteTy, {});
1196 Results.push(Hexagon::V6_veqb, BoolTy, {OpRef(B), OpRef::res(-1)});
Krzysztof Parzyszek40a605f2017-12-12 19:32:41 +00001197 Results.push(Hexagon::V6_vmux, ByteTy, {OpRef::res(-1), Vb, Va});
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001198 return OpRef::res(Results.top());
1199}
1200
1201OpRef HvxSelector::vmuxp(ArrayRef<uint8_t> Bytes, OpRef Va, OpRef Vb,
1202 ResultStack &Results) {
1203 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1204 size_t S = Bytes.size() / 2;
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001205 OpRef L = vmuxs(Bytes.take_front(S), OpRef::lo(Va), OpRef::lo(Vb), Results);
1206 OpRef H = vmuxs(Bytes.drop_front(S), OpRef::hi(Va), OpRef::hi(Vb), Results);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001207 return concat(L, H, Results);
1208}
1209
1210OpRef HvxSelector::shuffs1(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1211 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1212 unsigned VecLen = SM.Mask.size();
1213 assert(HwLen == VecLen);
Tim Shenb684b1a2017-12-06 19:33:42 +00001214 (void)VecLen;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001215 assert(all_of(SM.Mask, [this](int M) { return M == -1 || M < int(HwLen); }));
1216
1217 if (isIdentity(SM.Mask))
1218 return Va;
1219 if (isUndef(SM.Mask))
1220 return OpRef::undef(getSingleVT(MVT::i8));
1221
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001222 OpRef P = perfect(SM, Va, Results);
1223 if (P.isValid())
1224 return P;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001225 return butterfly(SM, Va, Results);
1226}
1227
1228OpRef HvxSelector::shuffs2(ShuffleMask SM, OpRef Va, OpRef Vb,
1229 ResultStack &Results) {
1230 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +00001231 if (isUndef(SM.Mask))
1232 return OpRef::undef(getSingleVT(MVT::i8));
1233
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001234 OpRef C = contracting(SM, Va, Vb, Results);
1235 if (C.isValid())
1236 return C;
1237
1238 int VecLen = SM.Mask.size();
1239 SmallVector<int,128> NewMask(VecLen);
1240 OpRef P = packs(SM, Va, Vb, Results, NewMask);
1241 if (P.isValid())
1242 return shuffs1(ShuffleMask(NewMask), P, Results);
1243
1244 SmallVector<int,128> MaskL(VecLen), MaskR(VecLen);
1245 splitMask(SM.Mask, MaskL, MaskR);
1246
1247 OpRef L = shuffs1(ShuffleMask(MaskL), Va, Results);
1248 OpRef R = shuffs1(ShuffleMask(MaskR), Vb, Results);
1249 if (!L.isValid() || !R.isValid())
1250 return OpRef::fail();
1251
1252 SmallVector<uint8_t,128> Bytes(VecLen);
1253 for (int I = 0; I != VecLen; ++I) {
1254 if (MaskL[I] != -1)
1255 Bytes[I] = 0xFF;
1256 }
1257 return vmuxs(Bytes, L, R, Results);
1258}
1259
1260OpRef HvxSelector::shuffp1(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1261 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1262 int VecLen = SM.Mask.size();
1263
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +00001264 if (isIdentity(SM.Mask))
1265 return Va;
1266 if (isUndef(SM.Mask))
1267 return OpRef::undef(getPairVT(MVT::i8));
1268
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001269 SmallVector<int,128> PackedMask(VecLen);
1270 OpRef P = packs(SM, OpRef::lo(Va), OpRef::hi(Va), Results, PackedMask);
1271 if (P.isValid()) {
1272 ShuffleMask PM(PackedMask);
1273 OpRef E = expanding(PM, P, Results);
1274 if (E.isValid())
1275 return E;
1276
1277 OpRef L = shuffs1(PM.lo(), P, Results);
1278 OpRef H = shuffs1(PM.hi(), P, Results);
1279 if (L.isValid() && H.isValid())
1280 return concat(L, H, Results);
1281 }
1282
1283 OpRef R = perfect(SM, Va, Results);
1284 if (R.isValid())
1285 return R;
1286 // TODO commute the mask and try the opposite order of the halves.
1287
1288 OpRef L = shuffs2(SM.lo(), OpRef::lo(Va), OpRef::hi(Va), Results);
1289 OpRef H = shuffs2(SM.hi(), OpRef::lo(Va), OpRef::hi(Va), Results);
1290 if (L.isValid() && H.isValid())
1291 return concat(L, H, Results);
1292
1293 return OpRef::fail();
1294}
1295
1296OpRef HvxSelector::shuffp2(ShuffleMask SM, OpRef Va, OpRef Vb,
1297 ResultStack &Results) {
1298 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +00001299 if (isUndef(SM.Mask))
1300 return OpRef::undef(getPairVT(MVT::i8));
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001301
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +00001302 int VecLen = SM.Mask.size();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001303 SmallVector<int,256> PackedMask(VecLen);
1304 OpRef P = packp(SM, Va, Vb, Results, PackedMask);
1305 if (P.isValid())
1306 return shuffp1(ShuffleMask(PackedMask), P, Results);
1307
1308 SmallVector<int,256> MaskL(VecLen), MaskR(VecLen);
Krzysztof Parzyszek67079be2018-02-05 15:46:41 +00001309 splitMask(SM.Mask, MaskL, MaskR);
1310
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001311 OpRef L = shuffp1(ShuffleMask(MaskL), Va, Results);
1312 OpRef R = shuffp1(ShuffleMask(MaskR), Vb, Results);
1313 if (!L.isValid() || !R.isValid())
1314 return OpRef::fail();
1315
1316 // Mux the results.
1317 SmallVector<uint8_t,256> Bytes(VecLen);
1318 for (int I = 0; I != VecLen; ++I) {
1319 if (MaskL[I] != -1)
1320 Bytes[I] = 0xFF;
1321 }
1322 return vmuxp(Bytes, L, R, Results);
1323}
1324
1325bool HvxSelector::scalarizeShuffle(ArrayRef<int> Mask, const SDLoc &dl,
1326 MVT ResTy, SDValue Va, SDValue Vb,
1327 SDNode *N) {
1328 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1329 MVT ElemTy = ResTy.getVectorElementType();
1330 assert(ElemTy == MVT::i8);
1331 unsigned VecLen = Mask.size();
1332 bool HavePairs = (2*HwLen == VecLen);
1333 MVT SingleTy = getSingleVT(MVT::i8);
1334
1335 SmallVector<SDValue,128> Ops;
1336 for (int I : Mask) {
1337 if (I < 0) {
1338 Ops.push_back(ISel.selectUndef(dl, ElemTy));
1339 continue;
1340 }
1341 SDValue Vec;
1342 unsigned M = I;
1343 if (M < VecLen) {
1344 Vec = Va;
1345 } else {
1346 Vec = Vb;
1347 M -= VecLen;
1348 }
1349 if (HavePairs) {
1350 if (M < HwLen) {
1351 Vec = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, Vec);
1352 } else {
1353 Vec = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, Vec);
1354 M -= HwLen;
1355 }
1356 }
1357 SDValue Idx = DAG.getConstant(M, dl, MVT::i32);
1358 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ElemTy, {Vec, Idx});
1359 SDValue L = Lower.LowerOperation(Ex, DAG);
1360 assert(L.getNode());
1361 Ops.push_back(L);
1362 }
1363
1364 SDValue LV;
1365 if (2*HwLen == VecLen) {
1366 SDValue B0 = DAG.getBuildVector(SingleTy, dl, {Ops.data(), HwLen});
1367 SDValue L0 = Lower.LowerOperation(B0, DAG);
1368 SDValue B1 = DAG.getBuildVector(SingleTy, dl, {Ops.data()+HwLen, HwLen});
1369 SDValue L1 = Lower.LowerOperation(B1, DAG);
1370 // XXX CONCAT_VECTORS is legal for HVX vectors. Legalizing (lowering)
1371 // functions may expect to be called only for illegal operations, so
1372 // make sure that they are not called for legal ones. Develop a better
1373 // mechanism for dealing with this.
1374 LV = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, {L0, L1});
1375 } else {
1376 SDValue BV = DAG.getBuildVector(ResTy, dl, Ops);
1377 LV = Lower.LowerOperation(BV, DAG);
1378 }
1379
1380 assert(!N->use_empty());
1381 ISel.ReplaceNode(N, LV.getNode());
1382 DAG.RemoveDeadNodes();
1383
1384 std::deque<SDNode*> SubNodes;
1385 SubNodes.push_back(LV.getNode());
1386 for (unsigned I = 0; I != SubNodes.size(); ++I) {
1387 for (SDValue Op : SubNodes[I]->ops())
1388 SubNodes.push_back(Op.getNode());
1389 }
1390 while (!SubNodes.empty()) {
1391 SDNode *S = SubNodes.front();
1392 SubNodes.pop_front();
1393 if (S->use_empty())
1394 continue;
1395 // This isn't great, but users need to be selected before any nodes that
1396 // they use. (The reason is to match larger patterns, and avoid nodes that
1397 // cannot be matched on their own, e.g. ValueType, TokenFactor, etc.).
1398 bool PendingUser = llvm::any_of(S->uses(), [&SubNodes](const SDNode *U) {
1399 return llvm::any_of(SubNodes, [U](const SDNode *T) {
1400 return T == U;
1401 });
1402 });
1403 if (PendingUser)
1404 SubNodes.push_back(S);
1405 else
1406 ISel.Select(S);
1407 }
1408
1409 DAG.RemoveDeadNodes();
1410 return true;
1411}
1412
1413OpRef HvxSelector::contracting(ShuffleMask SM, OpRef Va, OpRef Vb,
1414 ResultStack &Results) {
1415 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1416 if (!Va.isValid() || !Vb.isValid())
1417 return OpRef::fail();
1418
1419 // Contracting shuffles, i.e. instructions that always discard some bytes
1420 // from the operand vectors.
1421 //
1422 // V6_vshuff{e,o}b
1423 // V6_vdealb4w
1424 // V6_vpack{e,o}{b,h}
1425
1426 int VecLen = SM.Mask.size();
1427 std::pair<int,unsigned> Strip = findStrip(SM.Mask, 1, VecLen);
1428 MVT ResTy = getSingleVT(MVT::i8);
1429
1430 // The following shuffles only work for bytes and halfwords. This requires
1431 // the strip length to be 1 or 2.
1432 if (Strip.second != 1 && Strip.second != 2)
1433 return OpRef::fail();
1434
1435 // The patterns for the shuffles, in terms of the starting offsets of the
1436 // consecutive strips (L = length of the strip, N = VecLen):
1437 //
1438 // vpacke: 0, 2L, 4L ... N+0, N+2L, N+4L ... L = 1 or 2
1439 // vpacko: L, 3L, 5L ... N+L, N+3L, N+5L ... L = 1 or 2
1440 //
1441 // vshuffe: 0, N+0, 2L, N+2L, 4L ... L = 1 or 2
1442 // vshuffo: L, N+L, 3L, N+3L, 5L ... L = 1 or 2
1443 //
1444 // vdealb4w: 0, 4, 8 ... 2, 6, 10 ... N+0, N+4, N+8 ... N+2, N+6, N+10 ...
1445
1446 // The value of the element in the mask following the strip will decide
1447 // what kind of a shuffle this can be.
1448 int NextInMask = SM.Mask[Strip.second];
1449
1450 // Check if NextInMask could be 2L, 3L or 4, i.e. if it could be a mask
1451 // for vpack or vdealb4w. VecLen > 4, so NextInMask for vdealb4w would
1452 // satisfy this.
1453 if (NextInMask < VecLen) {
1454 // vpack{e,o} or vdealb4w
1455 if (Strip.first == 0 && Strip.second == 1 && NextInMask == 4) {
1456 int N = VecLen;
1457 // Check if this is vdealb4w (L=1).
1458 for (int I = 0; I != N/4; ++I)
1459 if (SM.Mask[I] != 4*I)
1460 return OpRef::fail();
1461 for (int I = 0; I != N/4; ++I)
1462 if (SM.Mask[I+N/4] != 2 + 4*I)
1463 return OpRef::fail();
1464 for (int I = 0; I != N/4; ++I)
1465 if (SM.Mask[I+N/2] != N + 4*I)
1466 return OpRef::fail();
1467 for (int I = 0; I != N/4; ++I)
1468 if (SM.Mask[I+3*N/4] != N+2 + 4*I)
1469 return OpRef::fail();
1470 // Matched mask for vdealb4w.
1471 Results.push(Hexagon::V6_vdealb4w, ResTy, {Vb, Va});
1472 return OpRef::res(Results.top());
1473 }
1474
1475 // Check if this is vpack{e,o}.
1476 int N = VecLen;
1477 int L = Strip.second;
1478 // Check if the first strip starts at 0 or at L.
1479 if (Strip.first != 0 && Strip.first != L)
1480 return OpRef::fail();
1481 // Examine the rest of the mask.
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001482 for (int I = L; I < N; I += L) {
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001483 auto S = findStrip(SM.Mask.drop_front(I), 1, N-I);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001484 // Check whether the mask element at the beginning of each strip
1485 // increases by 2L each time.
1486 if (S.first - Strip.first != 2*I)
1487 return OpRef::fail();
1488 // Check whether each strip is of the same length.
1489 if (S.second != unsigned(L))
1490 return OpRef::fail();
1491 }
1492
1493 // Strip.first == 0 => vpacke
1494 // Strip.first == L => vpacko
1495 assert(Strip.first == 0 || Strip.first == L);
1496 using namespace Hexagon;
1497 NodeTemplate Res;
1498 Res.Opc = Strip.second == 1 // Number of bytes.
1499 ? (Strip.first == 0 ? V6_vpackeb : V6_vpackob)
1500 : (Strip.first == 0 ? V6_vpackeh : V6_vpackoh);
1501 Res.Ty = ResTy;
1502 Res.Ops = { Vb, Va };
1503 Results.push(Res);
1504 return OpRef::res(Results.top());
1505 }
1506
1507 // Check if this is vshuff{e,o}.
1508 int N = VecLen;
1509 int L = Strip.second;
1510 std::pair<int,unsigned> PrevS = Strip;
1511 bool Flip = false;
1512 for (int I = L; I < N; I += L) {
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001513 auto S = findStrip(SM.Mask.drop_front(I), 1, N-I);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001514 if (S.second != PrevS.second)
1515 return OpRef::fail();
1516 int Diff = Flip ? PrevS.first - S.first + 2*L
1517 : S.first - PrevS.first;
1518 if (Diff != N)
1519 return OpRef::fail();
1520 Flip ^= true;
1521 PrevS = S;
1522 }
1523 // Strip.first == 0 => vshuffe
1524 // Strip.first == L => vshuffo
1525 assert(Strip.first == 0 || Strip.first == L);
1526 using namespace Hexagon;
1527 NodeTemplate Res;
1528 Res.Opc = Strip.second == 1 // Number of bytes.
1529 ? (Strip.first == 0 ? V6_vshuffeb : V6_vshuffob)
1530 : (Strip.first == 0 ? V6_vshufeh : V6_vshufoh);
1531 Res.Ty = ResTy;
1532 Res.Ops = { Vb, Va };
1533 Results.push(Res);
1534 return OpRef::res(Results.top());
1535}
1536
1537OpRef HvxSelector::expanding(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1538 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1539 // Expanding shuffles (using all elements and inserting into larger vector):
1540 //
1541 // V6_vunpacku{b,h} [*]
1542 //
1543 // [*] Only if the upper elements (filled with 0s) are "don't care" in Mask.
1544 //
1545 // Note: V6_vunpacko{b,h} are or-ing the high byte/half in the result, so
1546 // they are not shuffles.
1547 //
1548 // The argument is a single vector.
1549
1550 int VecLen = SM.Mask.size();
1551 assert(2*HwLen == unsigned(VecLen) && "Expecting vector-pair type");
1552
1553 std::pair<int,unsigned> Strip = findStrip(SM.Mask, 1, VecLen);
1554
1555 // The patterns for the unpacks, in terms of the starting offsets of the
1556 // consecutive strips (L = length of the strip, N = VecLen):
1557 //
1558 // vunpacku: 0, -1, L, -1, 2L, -1 ...
1559
1560 if (Strip.first != 0)
1561 return OpRef::fail();
1562
1563 // The vunpackus only handle byte and half-word.
1564 if (Strip.second != 1 && Strip.second != 2)
1565 return OpRef::fail();
1566
1567 int N = VecLen;
1568 int L = Strip.second;
1569
1570 // First, check the non-ignored strips.
1571 for (int I = 2*L; I < 2*N; I += 2*L) {
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001572 auto S = findStrip(SM.Mask.drop_front(I), 1, N-I);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001573 if (S.second != unsigned(L))
1574 return OpRef::fail();
1575 if (2*S.first != I)
1576 return OpRef::fail();
1577 }
1578 // Check the -1s.
1579 for (int I = L; I < 2*N; I += 2*L) {
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001580 auto S = findStrip(SM.Mask.drop_front(I), 0, N-I);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001581 if (S.first != -1 || S.second != unsigned(L))
1582 return OpRef::fail();
1583 }
1584
1585 unsigned Opc = Strip.second == 1 ? Hexagon::V6_vunpackub
1586 : Hexagon::V6_vunpackuh;
1587 Results.push(Opc, getPairVT(MVT::i8), {Va});
1588 return OpRef::res(Results.top());
1589}
1590
1591OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1592 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1593 // V6_vdeal{b,h}
1594 // V6_vshuff{b,h}
1595
1596 // V6_vshufoe{b,h} those are quivalent to vshuffvdd(..,{1,2})
1597 // V6_vshuffvdd (V6_vshuff)
1598 // V6_dealvdd (V6_vdeal)
1599
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001600 int VecLen = SM.Mask.size();
1601 assert(isPowerOf2_32(VecLen) && Log2_32(VecLen) <= 8);
1602 unsigned LogLen = Log2_32(VecLen);
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001603 unsigned HwLog = Log2_32(HwLen);
1604 // The result length must be the same as the length of a single vector,
1605 // or a vector pair.
1606 assert(LogLen == HwLog || LogLen == HwLog+1);
1607 bool Extend = (LogLen == HwLog);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001608
1609 if (!isPermutation(SM.Mask))
1610 return OpRef::fail();
1611
1612 SmallVector<unsigned,8> Perm(LogLen);
1613
1614 // Check if this could be a perfect shuffle, or a combination of perfect
1615 // shuffles.
1616 //
1617 // Consider this permutation (using hex digits to make the ASCII diagrams
1618 // easier to read):
1619 // { 0, 8, 1, 9, 2, A, 3, B, 4, C, 5, D, 6, E, 7, F }.
1620 // This is a "deal" operation: divide the input into two halves, and
1621 // create the output by picking elements by alternating between these two
1622 // halves:
1623 // 0 1 2 3 4 5 6 7 --> 0 8 1 9 2 A 3 B 4 C 5 D 6 E 7 F [*]
1624 // 8 9 A B C D E F
1625 //
1626 // Aside from a few special explicit cases (V6_vdealb, etc.), HVX provides
1627 // a somwehat different mechanism that could be used to perform shuffle/
1628 // deal operations: a 2x2 transpose.
1629 // Consider the halves of inputs again, they can be interpreted as a 2x8
1630 // matrix. A 2x8 matrix can be looked at four 2x2 matrices concatenated
1631 // together. Now, when considering 2 elements at a time, it will be a 2x4
1632 // matrix (with elements 01, 23, 45, etc.), or two 2x2 matrices:
1633 // 01 23 45 67
1634 // 89 AB CD EF
1635 // With groups of 4, this will become a single 2x2 matrix, and so on.
1636 //
1637 // The 2x2 transpose instruction works by transposing each of the 2x2
1638 // matrices (or "sub-matrices"), given a specific group size. For example,
1639 // if the group size is 1 (i.e. each element is its own group), there
1640 // will be four transposes of the four 2x2 matrices that form the 2x8.
1641 // For example, with the inputs as above, the result will be:
1642 // 0 8 2 A 4 C 6 E
1643 // 1 9 3 B 5 D 7 F
1644 // Now, this result can be tranposed again, but with the group size of 2:
1645 // 08 19 4C 5D
1646 // 2A 3B 6E 7F
1647 // If we then transpose that result, but with the group size of 4, we get:
1648 // 0819 2A3B
1649 // 4C5D 6E7F
1650 // If we concatenate these two rows, it will be
1651 // 0 8 1 9 2 A 3 B 4 C 5 D 6 E 7 F
1652 // which is the same as the "deal" [*] above.
1653 //
1654 // In general, a "deal" of individual elements is a series of 2x2 transposes,
1655 // with changing group size. HVX has two instructions:
1656 // Vdd = V6_vdealvdd Vu, Vv, Rt
1657 // Vdd = V6_shufvdd Vu, Vv, Rt
1658 // that perform exactly that. The register Rt controls which transposes are
1659 // going to happen: a bit at position n (counting from 0) indicates that a
1660 // transpose with a group size of 2^n will take place. If multiple bits are
1661 // set, multiple transposes will happen: vdealvdd will perform them starting
1662 // with the largest group size, vshuffvdd will do them in the reverse order.
1663 //
1664 // The main observation is that each 2x2 transpose corresponds to swapping
1665 // columns of bits in the binary representation of the values.
1666 //
1667 // The numbers {3,2,1,0} and the log2 of the number of contiguous 1 bits
1668 // in a given column. The * denote the columns that will be swapped.
1669 // The transpose with the group size 2^n corresponds to swapping columns
1670 // 3 (the highest log) and log2(n):
1671 //
1672 // 3 2 1 0 0 2 1 3 0 2 3 1
1673 // * * * * * *
1674 // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1675 // 1 0 0 0 1 8 1 0 0 0 8 1 0 0 0 8 1 0 0 0
1676 // 2 0 0 1 0 2 0 0 1 0 1 0 0 0 1 1 0 0 0 1
1677 // 3 0 0 1 1 A 1 0 1 0 9 1 0 0 1 9 1 0 0 1
1678 // 4 0 1 0 0 4 0 1 0 0 4 0 1 0 0 2 0 0 1 0
1679 // 5 0 1 0 1 C 1 1 0 0 C 1 1 0 0 A 1 0 1 0
1680 // 6 0 1 1 0 6 0 1 1 0 5 0 1 0 1 3 0 0 1 1
1681 // 7 0 1 1 1 E 1 1 1 0 D 1 1 0 1 B 1 0 1 1
1682 // 8 1 0 0 0 1 0 0 0 1 2 0 0 1 0 4 0 1 0 0
1683 // 9 1 0 0 1 9 1 0 0 1 A 1 0 1 0 C 1 1 0 0
1684 // A 1 0 1 0 3 0 0 1 1 3 0 0 1 1 5 0 1 0 1
1685 // B 1 0 1 1 B 1 0 1 1 B 1 0 1 1 D 1 1 0 1
1686 // C 1 1 0 0 5 0 1 0 1 6 0 1 1 0 6 0 1 1 0
1687 // D 1 1 0 1 D 1 1 0 1 E 1 1 1 0 E 1 1 1 0
1688 // E 1 1 1 0 7 0 1 1 1 7 0 1 1 1 7 0 1 1 1
1689 // F 1 1 1 1 F 1 1 1 1 F 1 1 1 1 F 1 1 1 1
1690
1691 auto XorPow2 = [] (ArrayRef<int> Mask, unsigned Num) {
1692 unsigned X = Mask[0] ^ Mask[Num/2];
1693 // Check that the first half has the X's bits clear.
1694 if ((Mask[0] & X) != 0)
1695 return 0u;
1696 for (unsigned I = 1; I != Num/2; ++I) {
1697 if (unsigned(Mask[I] ^ Mask[I+Num/2]) != X)
1698 return 0u;
1699 if ((Mask[I] & X) != 0)
1700 return 0u;
1701 }
1702 return X;
1703 };
1704
1705 // Create a vector of log2's for each column: Perm[i] corresponds to
1706 // the i-th bit (lsb is 0).
1707 assert(VecLen > 2);
1708 for (unsigned I = VecLen; I >= 2; I >>= 1) {
1709 // Examine the initial segment of Mask of size I.
1710 unsigned X = XorPow2(SM.Mask, I);
1711 if (!isPowerOf2_32(X))
1712 return OpRef::fail();
1713 // Check the other segments of Mask.
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001714 for (int J = I; J < VecLen; J += I) {
1715 if (XorPow2(SM.Mask.slice(J, I), I) != X)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001716 return OpRef::fail();
1717 }
1718 Perm[Log2_32(X)] = Log2_32(I)-1;
1719 }
1720
1721 // Once we have Perm, represent it as cycles. Denote the maximum log2
1722 // (equal to log2(VecLen)-1) as M. The cycle containing M can then be
1723 // written as (M a1 a2 a3 ... an). That cycle can be broken up into
1724 // simple swaps as (M a1)(M a2)(M a3)...(M an), with the composition
1725 // order being from left to right. Any (contiguous) segment where the
1726 // values ai, ai+1...aj are either all increasing or all decreasing,
1727 // can be implemented via a single vshuffvdd/vdealvdd respectively.
1728 //
1729 // If there is a cycle (a1 a2 ... an) that does not involve M, it can
1730 // be written as (M an)(a1 a2 ... an)(M a1). The first two cycles can
1731 // then be folded to get (M a1 a2 ... an)(M a1), and the above procedure
1732 // can be used to generate a sequence of vshuffvdd/vdealvdd.
1733 //
1734 // Example:
1735 // Assume M = 4 and consider a permutation (0 1)(2 3). It can be written
1736 // as (4 0 1)(4 0) composed with (4 2 3)(4 2), or simply
1737 // (4 0 1)(4 0)(4 2 3)(4 2).
1738 // It can then be expanded into swaps as
1739 // (4 0)(4 1)(4 0)(4 2)(4 3)(4 2),
1740 // and broken up into "increasing" segments as
1741 // [(4 0)(4 1)] [(4 0)(4 2)(4 3)] [(4 2)].
1742 // This is equivalent to
1743 // (4 0 1)(4 0 2 3)(4 2),
1744 // which can be implemented as 3 vshufvdd instructions.
1745
1746 using CycleType = SmallVector<unsigned,8>;
1747 std::set<CycleType> Cycles;
1748 std::set<unsigned> All;
1749
1750 for (unsigned I : Perm)
1751 All.insert(I);
1752
1753 // If the cycle contains LogLen-1, move it to the front of the cycle.
1754 // Otherwise, return the cycle unchanged.
1755 auto canonicalize = [LogLen](const CycleType &C) -> CycleType {
1756 unsigned LogPos, N = C.size();
1757 for (LogPos = 0; LogPos != N; ++LogPos)
1758 if (C[LogPos] == LogLen-1)
1759 break;
1760 if (LogPos == N)
1761 return C;
1762
1763 CycleType NewC(C.begin()+LogPos, C.end());
1764 NewC.append(C.begin(), C.begin()+LogPos);
1765 return NewC;
1766 };
1767
Krzysztof Parzyszekd2967862017-12-06 22:41:49 +00001768 auto pfs = [](const std::set<CycleType> &Cs, unsigned Len) {
1769 // Ordering: shuff: 5 0 1 2 3 4, deal: 5 4 3 2 1 0 (for Log=6),
1770 // for bytes zero is included, for halfwords is not.
1771 if (Cs.size() != 1)
1772 return 0u;
1773 const CycleType &C = *Cs.begin();
1774 if (C[0] != Len-1)
1775 return 0u;
1776 int D = Len - C.size();
1777 if (D != 0 && D != 1)
1778 return 0u;
1779
1780 bool IsDeal = true, IsShuff = true;
1781 for (unsigned I = 1; I != Len-D; ++I) {
1782 if (C[I] != Len-1-I)
1783 IsDeal = false;
1784 if (C[I] != I-(1-D)) // I-1, I
1785 IsShuff = false;
1786 }
1787 // At most one, IsDeal or IsShuff, can be non-zero.
1788 assert(!(IsDeal || IsShuff) || IsDeal != IsShuff);
1789 static unsigned Deals[] = { Hexagon::V6_vdealb, Hexagon::V6_vdealh };
1790 static unsigned Shufs[] = { Hexagon::V6_vshuffb, Hexagon::V6_vshuffh };
1791 return IsDeal ? Deals[D] : (IsShuff ? Shufs[D] : 0);
1792 };
1793
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001794 while (!All.empty()) {
1795 unsigned A = *All.begin();
1796 All.erase(A);
1797 CycleType C;
1798 C.push_back(A);
1799 for (unsigned B = Perm[A]; B != A; B = Perm[B]) {
1800 C.push_back(B);
1801 All.erase(B);
1802 }
1803 if (C.size() <= 1)
1804 continue;
1805 Cycles.insert(canonicalize(C));
1806 }
1807
Krzysztof Parzyszekd2967862017-12-06 22:41:49 +00001808 MVT SingleTy = getSingleVT(MVT::i8);
1809 MVT PairTy = getPairVT(MVT::i8);
1810
1811 // Recognize patterns for V6_vdeal{b,h} and V6_vshuff{b,h}.
1812 if (unsigned(VecLen) == HwLen) {
1813 if (unsigned SingleOpc = pfs(Cycles, LogLen)) {
1814 Results.push(SingleOpc, SingleTy, {Va});
1815 return OpRef::res(Results.top());
1816 }
1817 }
1818
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001819 SmallVector<unsigned,8> SwapElems;
1820 if (HwLen == unsigned(VecLen))
1821 SwapElems.push_back(LogLen-1);
1822
1823 for (const CycleType &C : Cycles) {
1824 unsigned First = (C[0] == LogLen-1) ? 1 : 0;
1825 SwapElems.append(C.begin()+First, C.end());
1826 if (First == 0)
1827 SwapElems.push_back(C[0]);
1828 }
1829
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001830 const SDLoc &dl(Results.InpNode);
1831 OpRef Arg = !Extend ? Va
1832 : concat(Va, OpRef::undef(SingleTy), Results);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001833
1834 for (unsigned I = 0, E = SwapElems.size(); I != E; ) {
1835 bool IsInc = I == E-1 || SwapElems[I] < SwapElems[I+1];
1836 unsigned S = (1u << SwapElems[I]);
1837 if (I < E-1) {
1838 while (++I < E-1 && IsInc == (SwapElems[I] < SwapElems[I+1]))
1839 S |= 1u << SwapElems[I];
1840 // The above loop will not add a bit for the final SwapElems[I+1],
1841 // so add it here.
1842 S |= 1u << SwapElems[I];
1843 }
1844 ++I;
1845
1846 NodeTemplate Res;
1847 Results.push(Hexagon::A2_tfrsi, MVT::i32,
1848 { DAG.getTargetConstant(S, dl, MVT::i32) });
1849 Res.Opc = IsInc ? Hexagon::V6_vshuffvdd : Hexagon::V6_vdealvdd;
1850 Res.Ty = PairTy;
1851 Res.Ops = { OpRef::hi(Arg), OpRef::lo(Arg), OpRef::res(-1) };
1852 Results.push(Res);
1853 Arg = OpRef::res(Results.top());
1854 }
1855
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001856 return !Extend ? Arg : OpRef::lo(Arg);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001857}
1858
1859OpRef HvxSelector::butterfly(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1860 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1861 // Butterfly shuffles.
1862 //
1863 // V6_vdelta
1864 // V6_vrdelta
1865 // V6_vror
1866
1867 // The assumption here is that all elements picked by Mask are in the
1868 // first operand to the vector_shuffle. This assumption is enforced
1869 // by the caller.
1870
1871 MVT ResTy = getSingleVT(MVT::i8);
1872 PermNetwork::Controls FC, RC;
1873 const SDLoc &dl(Results.InpNode);
1874 int VecLen = SM.Mask.size();
1875
1876 for (int M : SM.Mask) {
1877 if (M != -1 && M >= VecLen)
1878 return OpRef::fail();
1879 }
1880
1881 // Try the deltas/benes for both single vectors and vector pairs.
1882 ForwardDeltaNetwork FN(SM.Mask);
1883 if (FN.run(FC)) {
1884 SDValue Ctl = getVectorConstant(FC, dl);
1885 Results.push(Hexagon::V6_vdelta, ResTy, {Va, OpRef(Ctl)});
1886 return OpRef::res(Results.top());
1887 }
1888
1889 // Try reverse delta.
1890 ReverseDeltaNetwork RN(SM.Mask);
1891 if (RN.run(RC)) {
1892 SDValue Ctl = getVectorConstant(RC, dl);
1893 Results.push(Hexagon::V6_vrdelta, ResTy, {Va, OpRef(Ctl)});
1894 return OpRef::res(Results.top());
1895 }
1896
1897 // Do Benes.
1898 BenesNetwork BN(SM.Mask);
1899 if (BN.run(FC, RC)) {
1900 SDValue CtlF = getVectorConstant(FC, dl);
1901 SDValue CtlR = getVectorConstant(RC, dl);
1902 Results.push(Hexagon::V6_vdelta, ResTy, {Va, OpRef(CtlF)});
1903 Results.push(Hexagon::V6_vrdelta, ResTy,
1904 {OpRef::res(-1), OpRef(CtlR)});
1905 return OpRef::res(Results.top());
1906 }
1907
1908 return OpRef::fail();
1909}
1910
1911SDValue HvxSelector::getVectorConstant(ArrayRef<uint8_t> Data,
1912 const SDLoc &dl) {
1913 SmallVector<SDValue, 128> Elems;
1914 for (uint8_t C : Data)
1915 Elems.push_back(DAG.getConstant(C, dl, MVT::i8));
1916 MVT VecTy = MVT::getVectorVT(MVT::i8, Data.size());
1917 SDValue BV = DAG.getBuildVector(VecTy, dl, Elems);
1918 SDValue LV = Lower.LowerOperation(BV, DAG);
1919 DAG.RemoveDeadNode(BV.getNode());
1920 return LV;
1921}
1922
1923void HvxSelector::selectShuffle(SDNode *N) {
1924 DEBUG_WITH_TYPE("isel", {
1925 dbgs() << "Starting " << __func__ << " on node:\n";
1926 N->dump(&DAG);
1927 });
1928 MVT ResTy = N->getValueType(0).getSimpleVT();
1929 // Assume that vector shuffles operate on vectors of bytes.
1930 assert(ResTy.isVector() && ResTy.getVectorElementType() == MVT::i8);
1931
1932 auto *SN = cast<ShuffleVectorSDNode>(N);
1933 std::vector<int> Mask(SN->getMask().begin(), SN->getMask().end());
1934 // This shouldn't really be necessary. Is it?
1935 for (int &Idx : Mask)
1936 if (Idx != -1 && Idx < 0)
1937 Idx = -1;
1938
1939 unsigned VecLen = Mask.size();
1940 bool HavePairs = (2*HwLen == VecLen);
1941 assert(ResTy.getSizeInBits() / 8 == VecLen);
1942
1943 // Vd = vector_shuffle Va, Vb, Mask
1944 //
1945
1946 bool UseLeft = false, UseRight = false;
1947 for (unsigned I = 0; I != VecLen; ++I) {
1948 if (Mask[I] == -1)
1949 continue;
1950 unsigned Idx = Mask[I];
1951 assert(Idx < 2*VecLen);
1952 if (Idx < VecLen)
1953 UseLeft = true;
1954 else
1955 UseRight = true;
1956 }
1957
1958 DEBUG_WITH_TYPE("isel", {
1959 dbgs() << "VecLen=" << VecLen << " HwLen=" << HwLen << " UseLeft="
1960 << UseLeft << " UseRight=" << UseRight << " HavePairs="
1961 << HavePairs << '\n';
1962 });
1963 // If the mask is all -1's, generate "undef".
1964 if (!UseLeft && !UseRight) {
1965 ISel.ReplaceNode(N, ISel.selectUndef(SDLoc(SN), ResTy).getNode());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001966 return;
1967 }
1968
1969 SDValue Vec0 = N->getOperand(0);
1970 SDValue Vec1 = N->getOperand(1);
1971 ResultStack Results(SN);
1972 Results.push(TargetOpcode::COPY, ResTy, {Vec0});
1973 Results.push(TargetOpcode::COPY, ResTy, {Vec1});
1974 OpRef Va = OpRef::res(Results.top()-1);
1975 OpRef Vb = OpRef::res(Results.top());
1976
1977 OpRef Res = !HavePairs ? shuffs2(ShuffleMask(Mask), Va, Vb, Results)
1978 : shuffp2(ShuffleMask(Mask), Va, Vb, Results);
1979
1980 bool Done = Res.isValid();
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001981 if (Done) {
1982 // Make sure that Res is on the stack before materializing.
1983 Results.push(TargetOpcode::COPY, ResTy, {Res});
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001984 materialize(Results);
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001985 } else {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001986 Done = scalarizeShuffle(Mask, SDLoc(N), ResTy, Vec0, Vec1, N);
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001987 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001988
1989 if (!Done) {
1990#ifndef NDEBUG
1991 dbgs() << "Unhandled shuffle:\n";
1992 SN->dumpr(&DAG);
1993#endif
1994 llvm_unreachable("Failed to select vector shuffle");
1995 }
1996}
1997
1998void HvxSelector::selectRor(SDNode *N) {
1999 // If this is a rotation by less than 8, use V6_valignbi.
2000 MVT Ty = N->getValueType(0).getSimpleVT();
2001 const SDLoc &dl(N);
2002 SDValue VecV = N->getOperand(0);
2003 SDValue RotV = N->getOperand(1);
2004 SDNode *NewN = nullptr;
2005
2006 if (auto *CN = dyn_cast<ConstantSDNode>(RotV.getNode())) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002007 unsigned S = CN->getZExtValue() % HST.getVectorLength();
2008 if (S == 0) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002009 NewN = VecV.getNode();
2010 } else if (isUInt<3>(S)) {
2011 SDValue C = DAG.getTargetConstant(S, dl, MVT::i32);
2012 NewN = DAG.getMachineNode(Hexagon::V6_valignbi, dl, Ty,
2013 {VecV, VecV, C});
2014 }
2015 }
2016
2017 if (!NewN)
2018 NewN = DAG.getMachineNode(Hexagon::V6_vror, dl, Ty, {VecV, RotV});
2019
2020 ISel.ReplaceNode(N, NewN);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002021}
2022
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002023void HvxSelector::selectVAlign(SDNode *N) {
2024 SDValue Vv = N->getOperand(0);
2025 SDValue Vu = N->getOperand(1);
2026 SDValue Rt = N->getOperand(2);
2027 SDNode *NewN = DAG.getMachineNode(Hexagon::V6_valignb, SDLoc(N),
2028 N->getValueType(0), {Vv, Vu, Rt});
2029 ISel.ReplaceNode(N, NewN);
2030 DAG.RemoveDeadNode(N);
2031}
2032
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002033void HexagonDAGToDAGISel::SelectHvxShuffle(SDNode *N) {
2034 HvxSelector(*this, *CurDAG).selectShuffle(N);
2035}
2036
2037void HexagonDAGToDAGISel::SelectHvxRor(SDNode *N) {
2038 HvxSelector(*this, *CurDAG).selectRor(N);
2039}
2040
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002041void HexagonDAGToDAGISel::SelectHvxVAlign(SDNode *N) {
2042 HvxSelector(*this, *CurDAG).selectVAlign(N);
2043}
2044
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002045void HexagonDAGToDAGISel::SelectV65GatherPred(SDNode *N) {
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +00002046 if (!HST->usePackets()) {
2047 report_fatal_error("Support for gather requires packets, "
2048 "which are disabled");
2049 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002050 const SDLoc &dl(N);
2051 SDValue Chain = N->getOperand(0);
2052 SDValue Address = N->getOperand(2);
2053 SDValue Predicate = N->getOperand(3);
2054 SDValue Base = N->getOperand(4);
2055 SDValue Modifier = N->getOperand(5);
2056 SDValue Offset = N->getOperand(6);
2057
2058 unsigned Opcode;
2059 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2060 switch (IntNo) {
2061 default:
2062 llvm_unreachable("Unexpected HVX gather intrinsic.");
2063 case Intrinsic::hexagon_V6_vgathermhq:
2064 case Intrinsic::hexagon_V6_vgathermhq_128B:
2065 Opcode = Hexagon::V6_vgathermhq_pseudo;
2066 break;
2067 case Intrinsic::hexagon_V6_vgathermwq:
2068 case Intrinsic::hexagon_V6_vgathermwq_128B:
2069 Opcode = Hexagon::V6_vgathermwq_pseudo;
2070 break;
2071 case Intrinsic::hexagon_V6_vgathermhwq:
2072 case Intrinsic::hexagon_V6_vgathermhwq_128B:
2073 Opcode = Hexagon::V6_vgathermhwq_pseudo;
2074 break;
2075 }
2076
2077 SDVTList VTs = CurDAG->getVTList(MVT::Other);
2078 SDValue Ops[] = { Address, Predicate, Base, Modifier, Offset, Chain };
2079 SDNode *Result = CurDAG->getMachineNode(Opcode, dl, VTs, Ops);
2080
2081 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2082 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2083 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2084
Nirav Dave3264c1b2018-03-19 20:19:46 +00002085 ReplaceNode(N, Result);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002086}
2087
2088void HexagonDAGToDAGISel::SelectV65Gather(SDNode *N) {
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +00002089 if (!HST->usePackets()) {
2090 report_fatal_error("Support for gather requires packets, "
2091 "which are disabled");
2092 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002093 const SDLoc &dl(N);
2094 SDValue Chain = N->getOperand(0);
2095 SDValue Address = N->getOperand(2);
2096 SDValue Base = N->getOperand(3);
2097 SDValue Modifier = N->getOperand(4);
2098 SDValue Offset = N->getOperand(5);
2099
2100 unsigned Opcode;
2101 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2102 switch (IntNo) {
2103 default:
2104 llvm_unreachable("Unexpected HVX gather intrinsic.");
2105 case Intrinsic::hexagon_V6_vgathermh:
2106 case Intrinsic::hexagon_V6_vgathermh_128B:
2107 Opcode = Hexagon::V6_vgathermh_pseudo;
2108 break;
2109 case Intrinsic::hexagon_V6_vgathermw:
2110 case Intrinsic::hexagon_V6_vgathermw_128B:
2111 Opcode = Hexagon::V6_vgathermw_pseudo;
2112 break;
2113 case Intrinsic::hexagon_V6_vgathermhw:
2114 case Intrinsic::hexagon_V6_vgathermhw_128B:
2115 Opcode = Hexagon::V6_vgathermhw_pseudo;
2116 break;
2117 }
2118
2119 SDVTList VTs = CurDAG->getVTList(MVT::Other);
2120 SDValue Ops[] = { Address, Base, Modifier, Offset, Chain };
2121 SDNode *Result = CurDAG->getMachineNode(Opcode, dl, VTs, Ops);
2122
2123 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2124 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2125 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2126
Nirav Dave3264c1b2018-03-19 20:19:46 +00002127 ReplaceNode(N, Result);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002128}
2129
2130void HexagonDAGToDAGISel::SelectHVXDualOutput(SDNode *N) {
2131 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2132 SDNode *Result;
2133 switch (IID) {
2134 case Intrinsic::hexagon_V6_vaddcarry: {
2135 SmallVector<SDValue, 3> Ops = { N->getOperand(1), N->getOperand(2),
2136 N->getOperand(3) };
2137 SDVTList VTs = CurDAG->getVTList(MVT::v16i32, MVT::v512i1);
2138 Result = CurDAG->getMachineNode(Hexagon::V6_vaddcarry, SDLoc(N), VTs, Ops);
2139 break;
2140 }
2141 case Intrinsic::hexagon_V6_vaddcarry_128B: {
2142 SmallVector<SDValue, 3> Ops = { N->getOperand(1), N->getOperand(2),
2143 N->getOperand(3) };
2144 SDVTList VTs = CurDAG->getVTList(MVT::v32i32, MVT::v1024i1);
2145 Result = CurDAG->getMachineNode(Hexagon::V6_vaddcarry, SDLoc(N), VTs, Ops);
2146 break;
2147 }
2148 case Intrinsic::hexagon_V6_vsubcarry: {
2149 SmallVector<SDValue, 3> Ops = { N->getOperand(1), N->getOperand(2),
2150 N->getOperand(3) };
2151 SDVTList VTs = CurDAG->getVTList(MVT::v16i32, MVT::v512i1);
2152 Result = CurDAG->getMachineNode(Hexagon::V6_vsubcarry, SDLoc(N), VTs, Ops);
2153 break;
2154 }
2155 case Intrinsic::hexagon_V6_vsubcarry_128B: {
2156 SmallVector<SDValue, 3> Ops = { N->getOperand(1), N->getOperand(2),
2157 N->getOperand(3) };
2158 SDVTList VTs = CurDAG->getVTList(MVT::v32i32, MVT::v1024i1);
2159 Result = CurDAG->getMachineNode(Hexagon::V6_vsubcarry, SDLoc(N), VTs, Ops);
2160 break;
2161 }
2162 default:
2163 llvm_unreachable("Unexpected HVX dual output intrinsic.");
2164 }
2165 ReplaceUses(N, Result);
2166 ReplaceUses(SDValue(N, 0), SDValue(Result, 0));
2167 ReplaceUses(SDValue(N, 1), SDValue(Result, 1));
2168 CurDAG->RemoveDeadNode(N);
2169}