blob: 3758362599ae0d823611a50048c64b88574e8f43 [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
262 // Explicitly assign "None" all all uncolored nodes.
263 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 Parzyszek90ca4e82018-01-26 21:54:56 +0000922 // Constant vectors are generated as loads from constant pools or
923 // as VSPLATs of a constant value.
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000924 // Since they are generated during the selection process, the main
925 // selection algorithm is not aware of them. Select them directly
926 // here.
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000927 SmallVector<SDNode*,4> Nodes;
Krzysztof Parzyszeke156e9b2018-01-11 17:59:34 +0000928 SetVector<SDNode*> WorkQ;
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000929
930 // The DAG can change (due to CSE) during selection, so cache all the
931 // unselected nodes first to avoid traversing a mutating DAG.
932
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000933 auto IsNodeToSelect = [] (SDNode *N) {
934 if (N->isMachineOpcode())
935 return false;
936 unsigned Opc = N->getOpcode();
Krzysztof Parzyszekef204472018-02-05 15:52:54 +0000937 if (Opc == HexagonISD::VSPLAT || Opc == HexagonISD::VZERO)
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000938 return true;
Krzysztof Parzyszek8cc636c2018-01-31 16:48:20 +0000939 if (Opc == ISD::BITCAST) {
940 // Only select bitcasts of VSPLATs.
941 if (N->getOperand(0).getOpcode() == HexagonISD::VSPLAT)
942 return true;
943 }
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000944 if (Opc == ISD::LOAD) {
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000945 SDValue Addr = cast<LoadSDNode>(N)->getBasePtr();
946 unsigned AddrOpc = Addr.getOpcode();
947 if (AddrOpc == HexagonISD::AT_PCREL || AddrOpc == HexagonISD::CP)
948 if (Addr.getOperand(0).getOpcode() == ISD::TargetConstantPool)
949 return true;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000950 }
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000951 return false;
952 };
953
Krzysztof Parzyszeke156e9b2018-01-11 17:59:34 +0000954 WorkQ.insert(N);
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000955 for (unsigned i = 0; i != WorkQ.size(); ++i) {
956 SDNode *W = WorkQ[i];
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000957 if (IsNodeToSelect(W))
958 Nodes.push_back(W);
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000959 for (unsigned j = 0, f = W->getNumOperands(); j != f; ++j)
Krzysztof Parzyszeke156e9b2018-01-11 17:59:34 +0000960 WorkQ.insert(W->getOperand(j).getNode());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000961 }
962
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000963 for (SDNode *L : Nodes)
Krzysztof Parzyszeke7045832017-12-18 23:13:27 +0000964 ISel.Select(L);
965
Krzysztof Parzyszek90ca4e82018-01-26 21:54:56 +0000966 return !Nodes.empty();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +0000967}
968
969void HvxSelector::materialize(const ResultStack &Results) {
970 DEBUG_WITH_TYPE("isel", {
971 dbgs() << "Materializing\n";
972 Results.print(dbgs(), DAG);
973 });
974 if (Results.empty())
975 return;
976 const SDLoc &dl(Results.InpNode);
977 std::vector<SDValue> Output;
978
979 for (unsigned I = 0, E = Results.size(); I != E; ++I) {
980 const NodeTemplate &Node = Results[I];
981 std::vector<SDValue> Ops;
982 for (const OpRef &R : Node.Ops) {
983 assert(R.isValid());
984 if (R.isValue()) {
985 Ops.push_back(R.OpV);
986 continue;
987 }
988 if (R.OpN & OpRef::Undef) {
989 MVT::SimpleValueType SVT = MVT::SimpleValueType(R.OpN & OpRef::Index);
990 Ops.push_back(ISel.selectUndef(dl, MVT(SVT)));
991 continue;
992 }
993 // R is an index of a result.
994 unsigned Part = R.OpN & OpRef::Whole;
995 int Idx = SignExtend32(R.OpN & OpRef::Index, OpRef::IndexBits);
996 if (Idx < 0)
997 Idx += I;
998 assert(Idx >= 0 && unsigned(Idx) < Output.size());
999 SDValue Op = Output[Idx];
1000 MVT OpTy = Op.getValueType().getSimpleVT();
1001 if (Part != OpRef::Whole) {
1002 assert(Part == OpRef::LoHalf || Part == OpRef::HiHalf);
Krzysztof Parzyszek5aef4b52018-01-24 14:07:37 +00001003 MVT HalfTy = MVT::getVectorVT(OpTy.getVectorElementType(),
1004 OpTy.getVectorNumElements()/2);
1005 unsigned Sub = (Part == OpRef::LoHalf) ? Hexagon::vsub_lo
1006 : Hexagon::vsub_hi;
1007 Op = DAG.getTargetExtractSubreg(Sub, dl, HalfTy, Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001008 }
1009 Ops.push_back(Op);
1010 } // for (Node : Results)
1011
1012 assert(Node.Ty != MVT::Other);
1013 SDNode *ResN = (Node.Opc == TargetOpcode::COPY)
1014 ? Ops.front().getNode()
1015 : DAG.getMachineNode(Node.Opc, dl, Node.Ty, Ops);
1016 Output.push_back(SDValue(ResN, 0));
1017 }
1018
1019 SDNode *OutN = Output.back().getNode();
1020 SDNode *InpN = Results.InpNode;
1021 DEBUG_WITH_TYPE("isel", {
1022 dbgs() << "Generated node:\n";
1023 OutN->dumpr(&DAG);
1024 });
1025
1026 ISel.ReplaceNode(InpN, OutN);
1027 selectVectorConstants(OutN);
1028 DAG.RemoveDeadNodes();
1029}
1030
1031OpRef HvxSelector::concat(OpRef Lo, OpRef Hi, ResultStack &Results) {
1032 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1033 const SDLoc &dl(Results.InpNode);
1034 Results.push(TargetOpcode::REG_SEQUENCE, getPairVT(MVT::i8), {
1035 DAG.getTargetConstant(Hexagon::HvxWRRegClassID, dl, MVT::i32),
1036 Lo, DAG.getTargetConstant(Hexagon::vsub_lo, dl, MVT::i32),
1037 Hi, DAG.getTargetConstant(Hexagon::vsub_hi, dl, MVT::i32),
1038 });
1039 return OpRef::res(Results.top());
1040}
1041
1042// Va, Vb are single vectors, SM can be arbitrarily long.
1043OpRef HvxSelector::packs(ShuffleMask SM, OpRef Va, OpRef Vb,
1044 ResultStack &Results, MutableArrayRef<int> NewMask,
1045 unsigned Options) {
1046 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1047 if (!Va.isValid() || !Vb.isValid())
1048 return OpRef::fail();
1049
1050 int VecLen = SM.Mask.size();
1051 MVT Ty = getSingleVT(MVT::i8);
1052
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +00001053 auto IsSubvector = [] (ShuffleMask M) {
1054 assert(M.MinSrc >= 0 && M.MaxSrc >= 0);
1055 for (int I = 0, E = M.Mask.size(); I != E; ++I) {
1056 if (M.Mask[I] >= 0 && M.Mask[I]-I != M.MinSrc)
1057 return false;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001058 }
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +00001059 return true;
1060 };
1061
1062 if (SM.MaxSrc - SM.MinSrc < int(HwLen)) {
1063 if (SM.MinSrc == 0 || SM.MinSrc == int(HwLen) || !IsSubvector(SM)) {
1064 if (SM.MaxSrc < int(HwLen)) {
1065 memcpy(NewMask.data(), SM.Mask.data(), sizeof(int)*VecLen);
1066 return Va;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001067 }
Krzysztof Parzyszeke3e96322018-03-02 22:22:19 +00001068 if (SM.MinSrc >= int(HwLen)) {
1069 for (int I = 0; I != VecLen; ++I) {
1070 int M = SM.Mask[I];
1071 if (M != -1)
1072 M -= HwLen;
1073 NewMask[I] = M;
1074 }
1075 return Vb;
1076 }
1077 }
1078 if (SM.MaxSrc < int(HwLen)) {
1079 Vb = Va;
1080 } else if (SM.MinSrc > int(HwLen)) {
1081 Va = Vb;
1082 SM.MinSrc -= HwLen;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001083 }
1084 const SDLoc &dl(Results.InpNode);
1085 SDValue S = DAG.getTargetConstant(SM.MinSrc, dl, MVT::i32);
1086 if (isUInt<3>(SM.MinSrc)) {
1087 Results.push(Hexagon::V6_valignbi, Ty, {Vb, Va, S});
1088 } else {
1089 Results.push(Hexagon::A2_tfrsi, MVT::i32, {S});
1090 unsigned Top = Results.top();
1091 Results.push(Hexagon::V6_valignb, Ty, {Vb, Va, OpRef::res(Top)});
1092 }
1093 for (int I = 0; I != VecLen; ++I) {
1094 int M = SM.Mask[I];
1095 if (M != -1)
1096 M -= SM.MinSrc;
1097 NewMask[I] = M;
1098 }
1099 return OpRef::res(Results.top());
1100 }
1101
1102 if (Options & PackMux) {
1103 // If elements picked from Va and Vb have all different (source) indexes
1104 // (relative to the start of the argument), do a mux, and update the mask.
1105 BitVector Picked(HwLen);
1106 SmallVector<uint8_t,128> MuxBytes(HwLen);
1107 bool CanMux = true;
1108 for (int I = 0; I != VecLen; ++I) {
1109 int M = SM.Mask[I];
1110 if (M == -1)
1111 continue;
1112 if (M >= int(HwLen))
1113 M -= HwLen;
1114 else
1115 MuxBytes[M] = 0xFF;
1116 if (Picked[M]) {
1117 CanMux = false;
1118 break;
1119 }
1120 NewMask[I] = M;
1121 }
1122 if (CanMux)
1123 return vmuxs(MuxBytes, Va, Vb, Results);
1124 }
1125
1126 return OpRef::fail();
1127}
1128
1129OpRef HvxSelector::packp(ShuffleMask SM, OpRef Va, OpRef Vb,
1130 ResultStack &Results, MutableArrayRef<int> NewMask) {
1131 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1132 unsigned HalfMask = 0;
1133 unsigned LogHw = Log2_32(HwLen);
1134 for (int M : SM.Mask) {
1135 if (M == -1)
1136 continue;
1137 HalfMask |= (1u << (M >> LogHw));
1138 }
1139
1140 if (HalfMask == 0)
1141 return OpRef::undef(getPairVT(MVT::i8));
1142
1143 // If more than two halves are used, bail.
1144 // TODO: be more aggressive here?
1145 if (countPopulation(HalfMask) > 2)
1146 return OpRef::fail();
1147
1148 MVT HalfTy = getSingleVT(MVT::i8);
1149
1150 OpRef Inp[2] = { Va, Vb };
1151 OpRef Out[2] = { OpRef::undef(HalfTy), OpRef::undef(HalfTy) };
1152
1153 uint8_t HalfIdx[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
1154 unsigned Idx = 0;
1155 for (unsigned I = 0; I != 4; ++I) {
1156 if ((HalfMask & (1u << I)) == 0)
1157 continue;
1158 assert(Idx < 2);
1159 OpRef Op = Inp[I/2];
1160 Out[Idx] = (I & 1) ? OpRef::hi(Op) : OpRef::lo(Op);
1161 HalfIdx[I] = Idx++;
1162 }
1163
1164 int VecLen = SM.Mask.size();
1165 for (int I = 0; I != VecLen; ++I) {
1166 int M = SM.Mask[I];
1167 if (M >= 0) {
1168 uint8_t Idx = HalfIdx[M >> LogHw];
1169 assert(Idx == 0 || Idx == 1);
1170 M = (M & (HwLen-1)) + HwLen*Idx;
1171 }
1172 NewMask[I] = M;
1173 }
1174
1175 return concat(Out[0], Out[1], Results);
1176}
1177
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001178OpRef HvxSelector::vmuxs(ArrayRef<uint8_t> Bytes, OpRef Va, OpRef Vb,
1179 ResultStack &Results) {
1180 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1181 MVT ByteTy = getSingleVT(MVT::i8);
1182 MVT BoolTy = MVT::getVectorVT(MVT::i1, 8*HwLen); // XXX
1183 const SDLoc &dl(Results.InpNode);
1184 SDValue B = getVectorConstant(Bytes, dl);
1185 Results.push(Hexagon::V6_vd0, ByteTy, {});
1186 Results.push(Hexagon::V6_veqb, BoolTy, {OpRef(B), OpRef::res(-1)});
Krzysztof Parzyszek40a605f2017-12-12 19:32:41 +00001187 Results.push(Hexagon::V6_vmux, ByteTy, {OpRef::res(-1), Vb, Va});
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001188 return OpRef::res(Results.top());
1189}
1190
1191OpRef HvxSelector::vmuxp(ArrayRef<uint8_t> Bytes, OpRef Va, OpRef Vb,
1192 ResultStack &Results) {
1193 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1194 size_t S = Bytes.size() / 2;
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001195 OpRef L = vmuxs(Bytes.take_front(S), OpRef::lo(Va), OpRef::lo(Vb), Results);
1196 OpRef H = vmuxs(Bytes.drop_front(S), OpRef::hi(Va), OpRef::hi(Vb), Results);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001197 return concat(L, H, Results);
1198}
1199
1200OpRef HvxSelector::shuffs1(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1201 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1202 unsigned VecLen = SM.Mask.size();
1203 assert(HwLen == VecLen);
Tim Shenb684b1a2017-12-06 19:33:42 +00001204 (void)VecLen;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001205 assert(all_of(SM.Mask, [this](int M) { return M == -1 || M < int(HwLen); }));
1206
1207 if (isIdentity(SM.Mask))
1208 return Va;
1209 if (isUndef(SM.Mask))
1210 return OpRef::undef(getSingleVT(MVT::i8));
1211
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001212 OpRef P = perfect(SM, Va, Results);
1213 if (P.isValid())
1214 return P;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001215 return butterfly(SM, Va, Results);
1216}
1217
1218OpRef HvxSelector::shuffs2(ShuffleMask SM, OpRef Va, OpRef Vb,
1219 ResultStack &Results) {
1220 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +00001221 if (isUndef(SM.Mask))
1222 return OpRef::undef(getSingleVT(MVT::i8));
1223
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001224 OpRef C = contracting(SM, Va, Vb, Results);
1225 if (C.isValid())
1226 return C;
1227
1228 int VecLen = SM.Mask.size();
1229 SmallVector<int,128> NewMask(VecLen);
1230 OpRef P = packs(SM, Va, Vb, Results, NewMask);
1231 if (P.isValid())
1232 return shuffs1(ShuffleMask(NewMask), P, Results);
1233
1234 SmallVector<int,128> MaskL(VecLen), MaskR(VecLen);
1235 splitMask(SM.Mask, MaskL, MaskR);
1236
1237 OpRef L = shuffs1(ShuffleMask(MaskL), Va, Results);
1238 OpRef R = shuffs1(ShuffleMask(MaskR), Vb, Results);
1239 if (!L.isValid() || !R.isValid())
1240 return OpRef::fail();
1241
1242 SmallVector<uint8_t,128> Bytes(VecLen);
1243 for (int I = 0; I != VecLen; ++I) {
1244 if (MaskL[I] != -1)
1245 Bytes[I] = 0xFF;
1246 }
1247 return vmuxs(Bytes, L, R, Results);
1248}
1249
1250OpRef HvxSelector::shuffp1(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1251 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1252 int VecLen = SM.Mask.size();
1253
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +00001254 if (isIdentity(SM.Mask))
1255 return Va;
1256 if (isUndef(SM.Mask))
1257 return OpRef::undef(getPairVT(MVT::i8));
1258
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001259 SmallVector<int,128> PackedMask(VecLen);
1260 OpRef P = packs(SM, OpRef::lo(Va), OpRef::hi(Va), Results, PackedMask);
1261 if (P.isValid()) {
1262 ShuffleMask PM(PackedMask);
1263 OpRef E = expanding(PM, P, Results);
1264 if (E.isValid())
1265 return E;
1266
1267 OpRef L = shuffs1(PM.lo(), P, Results);
1268 OpRef H = shuffs1(PM.hi(), P, Results);
1269 if (L.isValid() && H.isValid())
1270 return concat(L, H, Results);
1271 }
1272
1273 OpRef R = perfect(SM, Va, Results);
1274 if (R.isValid())
1275 return R;
1276 // TODO commute the mask and try the opposite order of the halves.
1277
1278 OpRef L = shuffs2(SM.lo(), OpRef::lo(Va), OpRef::hi(Va), Results);
1279 OpRef H = shuffs2(SM.hi(), OpRef::lo(Va), OpRef::hi(Va), Results);
1280 if (L.isValid() && H.isValid())
1281 return concat(L, H, Results);
1282
1283 return OpRef::fail();
1284}
1285
1286OpRef HvxSelector::shuffp2(ShuffleMask SM, OpRef Va, OpRef Vb,
1287 ResultStack &Results) {
1288 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +00001289 if (isUndef(SM.Mask))
1290 return OpRef::undef(getPairVT(MVT::i8));
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001291
Krzysztof Parzyszekedcd9dc2017-12-12 20:23:12 +00001292 int VecLen = SM.Mask.size();
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001293 SmallVector<int,256> PackedMask(VecLen);
1294 OpRef P = packp(SM, Va, Vb, Results, PackedMask);
1295 if (P.isValid())
1296 return shuffp1(ShuffleMask(PackedMask), P, Results);
1297
1298 SmallVector<int,256> MaskL(VecLen), MaskR(VecLen);
Krzysztof Parzyszek67079be2018-02-05 15:46:41 +00001299 splitMask(SM.Mask, MaskL, MaskR);
1300
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001301 OpRef L = shuffp1(ShuffleMask(MaskL), Va, Results);
1302 OpRef R = shuffp1(ShuffleMask(MaskR), Vb, Results);
1303 if (!L.isValid() || !R.isValid())
1304 return OpRef::fail();
1305
1306 // Mux the results.
1307 SmallVector<uint8_t,256> Bytes(VecLen);
1308 for (int I = 0; I != VecLen; ++I) {
1309 if (MaskL[I] != -1)
1310 Bytes[I] = 0xFF;
1311 }
1312 return vmuxp(Bytes, L, R, Results);
1313}
1314
1315bool HvxSelector::scalarizeShuffle(ArrayRef<int> Mask, const SDLoc &dl,
1316 MVT ResTy, SDValue Va, SDValue Vb,
1317 SDNode *N) {
1318 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1319 MVT ElemTy = ResTy.getVectorElementType();
1320 assert(ElemTy == MVT::i8);
1321 unsigned VecLen = Mask.size();
1322 bool HavePairs = (2*HwLen == VecLen);
1323 MVT SingleTy = getSingleVT(MVT::i8);
1324
1325 SmallVector<SDValue,128> Ops;
1326 for (int I : Mask) {
1327 if (I < 0) {
1328 Ops.push_back(ISel.selectUndef(dl, ElemTy));
1329 continue;
1330 }
1331 SDValue Vec;
1332 unsigned M = I;
1333 if (M < VecLen) {
1334 Vec = Va;
1335 } else {
1336 Vec = Vb;
1337 M -= VecLen;
1338 }
1339 if (HavePairs) {
1340 if (M < HwLen) {
1341 Vec = DAG.getTargetExtractSubreg(Hexagon::vsub_lo, dl, SingleTy, Vec);
1342 } else {
1343 Vec = DAG.getTargetExtractSubreg(Hexagon::vsub_hi, dl, SingleTy, Vec);
1344 M -= HwLen;
1345 }
1346 }
1347 SDValue Idx = DAG.getConstant(M, dl, MVT::i32);
1348 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ElemTy, {Vec, Idx});
1349 SDValue L = Lower.LowerOperation(Ex, DAG);
1350 assert(L.getNode());
1351 Ops.push_back(L);
1352 }
1353
1354 SDValue LV;
1355 if (2*HwLen == VecLen) {
1356 SDValue B0 = DAG.getBuildVector(SingleTy, dl, {Ops.data(), HwLen});
1357 SDValue L0 = Lower.LowerOperation(B0, DAG);
1358 SDValue B1 = DAG.getBuildVector(SingleTy, dl, {Ops.data()+HwLen, HwLen});
1359 SDValue L1 = Lower.LowerOperation(B1, DAG);
1360 // XXX CONCAT_VECTORS is legal for HVX vectors. Legalizing (lowering)
1361 // functions may expect to be called only for illegal operations, so
1362 // make sure that they are not called for legal ones. Develop a better
1363 // mechanism for dealing with this.
1364 LV = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResTy, {L0, L1});
1365 } else {
1366 SDValue BV = DAG.getBuildVector(ResTy, dl, Ops);
1367 LV = Lower.LowerOperation(BV, DAG);
1368 }
1369
1370 assert(!N->use_empty());
1371 ISel.ReplaceNode(N, LV.getNode());
1372 DAG.RemoveDeadNodes();
1373
1374 std::deque<SDNode*> SubNodes;
1375 SubNodes.push_back(LV.getNode());
1376 for (unsigned I = 0; I != SubNodes.size(); ++I) {
1377 for (SDValue Op : SubNodes[I]->ops())
1378 SubNodes.push_back(Op.getNode());
1379 }
1380 while (!SubNodes.empty()) {
1381 SDNode *S = SubNodes.front();
1382 SubNodes.pop_front();
1383 if (S->use_empty())
1384 continue;
1385 // This isn't great, but users need to be selected before any nodes that
1386 // they use. (The reason is to match larger patterns, and avoid nodes that
1387 // cannot be matched on their own, e.g. ValueType, TokenFactor, etc.).
1388 bool PendingUser = llvm::any_of(S->uses(), [&SubNodes](const SDNode *U) {
1389 return llvm::any_of(SubNodes, [U](const SDNode *T) {
1390 return T == U;
1391 });
1392 });
1393 if (PendingUser)
1394 SubNodes.push_back(S);
1395 else
1396 ISel.Select(S);
1397 }
1398
1399 DAG.RemoveDeadNodes();
1400 return true;
1401}
1402
1403OpRef HvxSelector::contracting(ShuffleMask SM, OpRef Va, OpRef Vb,
1404 ResultStack &Results) {
1405 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1406 if (!Va.isValid() || !Vb.isValid())
1407 return OpRef::fail();
1408
1409 // Contracting shuffles, i.e. instructions that always discard some bytes
1410 // from the operand vectors.
1411 //
1412 // V6_vshuff{e,o}b
1413 // V6_vdealb4w
1414 // V6_vpack{e,o}{b,h}
1415
1416 int VecLen = SM.Mask.size();
1417 std::pair<int,unsigned> Strip = findStrip(SM.Mask, 1, VecLen);
1418 MVT ResTy = getSingleVT(MVT::i8);
1419
1420 // The following shuffles only work for bytes and halfwords. This requires
1421 // the strip length to be 1 or 2.
1422 if (Strip.second != 1 && Strip.second != 2)
1423 return OpRef::fail();
1424
1425 // The patterns for the shuffles, in terms of the starting offsets of the
1426 // consecutive strips (L = length of the strip, N = VecLen):
1427 //
1428 // vpacke: 0, 2L, 4L ... N+0, N+2L, N+4L ... L = 1 or 2
1429 // vpacko: L, 3L, 5L ... N+L, N+3L, N+5L ... L = 1 or 2
1430 //
1431 // vshuffe: 0, N+0, 2L, N+2L, 4L ... L = 1 or 2
1432 // vshuffo: L, N+L, 3L, N+3L, 5L ... L = 1 or 2
1433 //
1434 // vdealb4w: 0, 4, 8 ... 2, 6, 10 ... N+0, N+4, N+8 ... N+2, N+6, N+10 ...
1435
1436 // The value of the element in the mask following the strip will decide
1437 // what kind of a shuffle this can be.
1438 int NextInMask = SM.Mask[Strip.second];
1439
1440 // Check if NextInMask could be 2L, 3L or 4, i.e. if it could be a mask
1441 // for vpack or vdealb4w. VecLen > 4, so NextInMask for vdealb4w would
1442 // satisfy this.
1443 if (NextInMask < VecLen) {
1444 // vpack{e,o} or vdealb4w
1445 if (Strip.first == 0 && Strip.second == 1 && NextInMask == 4) {
1446 int N = VecLen;
1447 // Check if this is vdealb4w (L=1).
1448 for (int I = 0; I != N/4; ++I)
1449 if (SM.Mask[I] != 4*I)
1450 return OpRef::fail();
1451 for (int I = 0; I != N/4; ++I)
1452 if (SM.Mask[I+N/4] != 2 + 4*I)
1453 return OpRef::fail();
1454 for (int I = 0; I != N/4; ++I)
1455 if (SM.Mask[I+N/2] != N + 4*I)
1456 return OpRef::fail();
1457 for (int I = 0; I != N/4; ++I)
1458 if (SM.Mask[I+3*N/4] != N+2 + 4*I)
1459 return OpRef::fail();
1460 // Matched mask for vdealb4w.
1461 Results.push(Hexagon::V6_vdealb4w, ResTy, {Vb, Va});
1462 return OpRef::res(Results.top());
1463 }
1464
1465 // Check if this is vpack{e,o}.
1466 int N = VecLen;
1467 int L = Strip.second;
1468 // Check if the first strip starts at 0 or at L.
1469 if (Strip.first != 0 && Strip.first != L)
1470 return OpRef::fail();
1471 // Examine the rest of the mask.
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001472 for (int I = L; I < N; I += L) {
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001473 auto S = findStrip(SM.Mask.drop_front(I), 1, N-I);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001474 // Check whether the mask element at the beginning of each strip
1475 // increases by 2L each time.
1476 if (S.first - Strip.first != 2*I)
1477 return OpRef::fail();
1478 // Check whether each strip is of the same length.
1479 if (S.second != unsigned(L))
1480 return OpRef::fail();
1481 }
1482
1483 // Strip.first == 0 => vpacke
1484 // Strip.first == L => vpacko
1485 assert(Strip.first == 0 || Strip.first == L);
1486 using namespace Hexagon;
1487 NodeTemplate Res;
1488 Res.Opc = Strip.second == 1 // Number of bytes.
1489 ? (Strip.first == 0 ? V6_vpackeb : V6_vpackob)
1490 : (Strip.first == 0 ? V6_vpackeh : V6_vpackoh);
1491 Res.Ty = ResTy;
1492 Res.Ops = { Vb, Va };
1493 Results.push(Res);
1494 return OpRef::res(Results.top());
1495 }
1496
1497 // Check if this is vshuff{e,o}.
1498 int N = VecLen;
1499 int L = Strip.second;
1500 std::pair<int,unsigned> PrevS = Strip;
1501 bool Flip = false;
1502 for (int I = L; I < N; I += L) {
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001503 auto S = findStrip(SM.Mask.drop_front(I), 1, N-I);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001504 if (S.second != PrevS.second)
1505 return OpRef::fail();
1506 int Diff = Flip ? PrevS.first - S.first + 2*L
1507 : S.first - PrevS.first;
1508 if (Diff != N)
1509 return OpRef::fail();
1510 Flip ^= true;
1511 PrevS = S;
1512 }
1513 // Strip.first == 0 => vshuffe
1514 // Strip.first == L => vshuffo
1515 assert(Strip.first == 0 || Strip.first == L);
1516 using namespace Hexagon;
1517 NodeTemplate Res;
1518 Res.Opc = Strip.second == 1 // Number of bytes.
1519 ? (Strip.first == 0 ? V6_vshuffeb : V6_vshuffob)
1520 : (Strip.first == 0 ? V6_vshufeh : V6_vshufoh);
1521 Res.Ty = ResTy;
1522 Res.Ops = { Vb, Va };
1523 Results.push(Res);
1524 return OpRef::res(Results.top());
1525}
1526
1527OpRef HvxSelector::expanding(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1528 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1529 // Expanding shuffles (using all elements and inserting into larger vector):
1530 //
1531 // V6_vunpacku{b,h} [*]
1532 //
1533 // [*] Only if the upper elements (filled with 0s) are "don't care" in Mask.
1534 //
1535 // Note: V6_vunpacko{b,h} are or-ing the high byte/half in the result, so
1536 // they are not shuffles.
1537 //
1538 // The argument is a single vector.
1539
1540 int VecLen = SM.Mask.size();
1541 assert(2*HwLen == unsigned(VecLen) && "Expecting vector-pair type");
1542
1543 std::pair<int,unsigned> Strip = findStrip(SM.Mask, 1, VecLen);
1544
1545 // The patterns for the unpacks, in terms of the starting offsets of the
1546 // consecutive strips (L = length of the strip, N = VecLen):
1547 //
1548 // vunpacku: 0, -1, L, -1, 2L, -1 ...
1549
1550 if (Strip.first != 0)
1551 return OpRef::fail();
1552
1553 // The vunpackus only handle byte and half-word.
1554 if (Strip.second != 1 && Strip.second != 2)
1555 return OpRef::fail();
1556
1557 int N = VecLen;
1558 int L = Strip.second;
1559
1560 // First, check the non-ignored strips.
1561 for (int I = 2*L; I < 2*N; I += 2*L) {
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001562 auto S = findStrip(SM.Mask.drop_front(I), 1, N-I);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001563 if (S.second != unsigned(L))
1564 return OpRef::fail();
1565 if (2*S.first != I)
1566 return OpRef::fail();
1567 }
1568 // Check the -1s.
1569 for (int I = L; I < 2*N; I += 2*L) {
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001570 auto S = findStrip(SM.Mask.drop_front(I), 0, N-I);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001571 if (S.first != -1 || S.second != unsigned(L))
1572 return OpRef::fail();
1573 }
1574
1575 unsigned Opc = Strip.second == 1 ? Hexagon::V6_vunpackub
1576 : Hexagon::V6_vunpackuh;
1577 Results.push(Opc, getPairVT(MVT::i8), {Va});
1578 return OpRef::res(Results.top());
1579}
1580
1581OpRef HvxSelector::perfect(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1582 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1583 // V6_vdeal{b,h}
1584 // V6_vshuff{b,h}
1585
1586 // V6_vshufoe{b,h} those are quivalent to vshuffvdd(..,{1,2})
1587 // V6_vshuffvdd (V6_vshuff)
1588 // V6_dealvdd (V6_vdeal)
1589
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001590 int VecLen = SM.Mask.size();
1591 assert(isPowerOf2_32(VecLen) && Log2_32(VecLen) <= 8);
1592 unsigned LogLen = Log2_32(VecLen);
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001593 unsigned HwLog = Log2_32(HwLen);
1594 // The result length must be the same as the length of a single vector,
1595 // or a vector pair.
1596 assert(LogLen == HwLog || LogLen == HwLog+1);
1597 bool Extend = (LogLen == HwLog);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001598
1599 if (!isPermutation(SM.Mask))
1600 return OpRef::fail();
1601
1602 SmallVector<unsigned,8> Perm(LogLen);
1603
1604 // Check if this could be a perfect shuffle, or a combination of perfect
1605 // shuffles.
1606 //
1607 // Consider this permutation (using hex digits to make the ASCII diagrams
1608 // easier to read):
1609 // { 0, 8, 1, 9, 2, A, 3, B, 4, C, 5, D, 6, E, 7, F }.
1610 // This is a "deal" operation: divide the input into two halves, and
1611 // create the output by picking elements by alternating between these two
1612 // halves:
1613 // 0 1 2 3 4 5 6 7 --> 0 8 1 9 2 A 3 B 4 C 5 D 6 E 7 F [*]
1614 // 8 9 A B C D E F
1615 //
1616 // Aside from a few special explicit cases (V6_vdealb, etc.), HVX provides
1617 // a somwehat different mechanism that could be used to perform shuffle/
1618 // deal operations: a 2x2 transpose.
1619 // Consider the halves of inputs again, they can be interpreted as a 2x8
1620 // matrix. A 2x8 matrix can be looked at four 2x2 matrices concatenated
1621 // together. Now, when considering 2 elements at a time, it will be a 2x4
1622 // matrix (with elements 01, 23, 45, etc.), or two 2x2 matrices:
1623 // 01 23 45 67
1624 // 89 AB CD EF
1625 // With groups of 4, this will become a single 2x2 matrix, and so on.
1626 //
1627 // The 2x2 transpose instruction works by transposing each of the 2x2
1628 // matrices (or "sub-matrices"), given a specific group size. For example,
1629 // if the group size is 1 (i.e. each element is its own group), there
1630 // will be four transposes of the four 2x2 matrices that form the 2x8.
1631 // For example, with the inputs as above, the result will be:
1632 // 0 8 2 A 4 C 6 E
1633 // 1 9 3 B 5 D 7 F
1634 // Now, this result can be tranposed again, but with the group size of 2:
1635 // 08 19 4C 5D
1636 // 2A 3B 6E 7F
1637 // If we then transpose that result, but with the group size of 4, we get:
1638 // 0819 2A3B
1639 // 4C5D 6E7F
1640 // If we concatenate these two rows, it will be
1641 // 0 8 1 9 2 A 3 B 4 C 5 D 6 E 7 F
1642 // which is the same as the "deal" [*] above.
1643 //
1644 // In general, a "deal" of individual elements is a series of 2x2 transposes,
1645 // with changing group size. HVX has two instructions:
1646 // Vdd = V6_vdealvdd Vu, Vv, Rt
1647 // Vdd = V6_shufvdd Vu, Vv, Rt
1648 // that perform exactly that. The register Rt controls which transposes are
1649 // going to happen: a bit at position n (counting from 0) indicates that a
1650 // transpose with a group size of 2^n will take place. If multiple bits are
1651 // set, multiple transposes will happen: vdealvdd will perform them starting
1652 // with the largest group size, vshuffvdd will do them in the reverse order.
1653 //
1654 // The main observation is that each 2x2 transpose corresponds to swapping
1655 // columns of bits in the binary representation of the values.
1656 //
1657 // The numbers {3,2,1,0} and the log2 of the number of contiguous 1 bits
1658 // in a given column. The * denote the columns that will be swapped.
1659 // The transpose with the group size 2^n corresponds to swapping columns
1660 // 3 (the highest log) and log2(n):
1661 //
1662 // 3 2 1 0 0 2 1 3 0 2 3 1
1663 // * * * * * *
1664 // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1665 // 1 0 0 0 1 8 1 0 0 0 8 1 0 0 0 8 1 0 0 0
1666 // 2 0 0 1 0 2 0 0 1 0 1 0 0 0 1 1 0 0 0 1
1667 // 3 0 0 1 1 A 1 0 1 0 9 1 0 0 1 9 1 0 0 1
1668 // 4 0 1 0 0 4 0 1 0 0 4 0 1 0 0 2 0 0 1 0
1669 // 5 0 1 0 1 C 1 1 0 0 C 1 1 0 0 A 1 0 1 0
1670 // 6 0 1 1 0 6 0 1 1 0 5 0 1 0 1 3 0 0 1 1
1671 // 7 0 1 1 1 E 1 1 1 0 D 1 1 0 1 B 1 0 1 1
1672 // 8 1 0 0 0 1 0 0 0 1 2 0 0 1 0 4 0 1 0 0
1673 // 9 1 0 0 1 9 1 0 0 1 A 1 0 1 0 C 1 1 0 0
1674 // A 1 0 1 0 3 0 0 1 1 3 0 0 1 1 5 0 1 0 1
1675 // B 1 0 1 1 B 1 0 1 1 B 1 0 1 1 D 1 1 0 1
1676 // C 1 1 0 0 5 0 1 0 1 6 0 1 1 0 6 0 1 1 0
1677 // D 1 1 0 1 D 1 1 0 1 E 1 1 1 0 E 1 1 1 0
1678 // E 1 1 1 0 7 0 1 1 1 7 0 1 1 1 7 0 1 1 1
1679 // F 1 1 1 1 F 1 1 1 1 F 1 1 1 1 F 1 1 1 1
1680
1681 auto XorPow2 = [] (ArrayRef<int> Mask, unsigned Num) {
1682 unsigned X = Mask[0] ^ Mask[Num/2];
1683 // Check that the first half has the X's bits clear.
1684 if ((Mask[0] & X) != 0)
1685 return 0u;
1686 for (unsigned I = 1; I != Num/2; ++I) {
1687 if (unsigned(Mask[I] ^ Mask[I+Num/2]) != X)
1688 return 0u;
1689 if ((Mask[I] & X) != 0)
1690 return 0u;
1691 }
1692 return X;
1693 };
1694
1695 // Create a vector of log2's for each column: Perm[i] corresponds to
1696 // the i-th bit (lsb is 0).
1697 assert(VecLen > 2);
1698 for (unsigned I = VecLen; I >= 2; I >>= 1) {
1699 // Examine the initial segment of Mask of size I.
1700 unsigned X = XorPow2(SM.Mask, I);
1701 if (!isPowerOf2_32(X))
1702 return OpRef::fail();
1703 // Check the other segments of Mask.
Krzysztof Parzyszek3f84c0f2017-12-20 20:54:13 +00001704 for (int J = I; J < VecLen; J += I) {
1705 if (XorPow2(SM.Mask.slice(J, I), I) != X)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001706 return OpRef::fail();
1707 }
1708 Perm[Log2_32(X)] = Log2_32(I)-1;
1709 }
1710
1711 // Once we have Perm, represent it as cycles. Denote the maximum log2
1712 // (equal to log2(VecLen)-1) as M. The cycle containing M can then be
1713 // written as (M a1 a2 a3 ... an). That cycle can be broken up into
1714 // simple swaps as (M a1)(M a2)(M a3)...(M an), with the composition
1715 // order being from left to right. Any (contiguous) segment where the
1716 // values ai, ai+1...aj are either all increasing or all decreasing,
1717 // can be implemented via a single vshuffvdd/vdealvdd respectively.
1718 //
1719 // If there is a cycle (a1 a2 ... an) that does not involve M, it can
1720 // be written as (M an)(a1 a2 ... an)(M a1). The first two cycles can
1721 // then be folded to get (M a1 a2 ... an)(M a1), and the above procedure
1722 // can be used to generate a sequence of vshuffvdd/vdealvdd.
1723 //
1724 // Example:
1725 // Assume M = 4 and consider a permutation (0 1)(2 3). It can be written
1726 // as (4 0 1)(4 0) composed with (4 2 3)(4 2), or simply
1727 // (4 0 1)(4 0)(4 2 3)(4 2).
1728 // It can then be expanded into swaps as
1729 // (4 0)(4 1)(4 0)(4 2)(4 3)(4 2),
1730 // and broken up into "increasing" segments as
1731 // [(4 0)(4 1)] [(4 0)(4 2)(4 3)] [(4 2)].
1732 // This is equivalent to
1733 // (4 0 1)(4 0 2 3)(4 2),
1734 // which can be implemented as 3 vshufvdd instructions.
1735
1736 using CycleType = SmallVector<unsigned,8>;
1737 std::set<CycleType> Cycles;
1738 std::set<unsigned> All;
1739
1740 for (unsigned I : Perm)
1741 All.insert(I);
1742
1743 // If the cycle contains LogLen-1, move it to the front of the cycle.
1744 // Otherwise, return the cycle unchanged.
1745 auto canonicalize = [LogLen](const CycleType &C) -> CycleType {
1746 unsigned LogPos, N = C.size();
1747 for (LogPos = 0; LogPos != N; ++LogPos)
1748 if (C[LogPos] == LogLen-1)
1749 break;
1750 if (LogPos == N)
1751 return C;
1752
1753 CycleType NewC(C.begin()+LogPos, C.end());
1754 NewC.append(C.begin(), C.begin()+LogPos);
1755 return NewC;
1756 };
1757
Krzysztof Parzyszekd2967862017-12-06 22:41:49 +00001758 auto pfs = [](const std::set<CycleType> &Cs, unsigned Len) {
1759 // Ordering: shuff: 5 0 1 2 3 4, deal: 5 4 3 2 1 0 (for Log=6),
1760 // for bytes zero is included, for halfwords is not.
1761 if (Cs.size() != 1)
1762 return 0u;
1763 const CycleType &C = *Cs.begin();
1764 if (C[0] != Len-1)
1765 return 0u;
1766 int D = Len - C.size();
1767 if (D != 0 && D != 1)
1768 return 0u;
1769
1770 bool IsDeal = true, IsShuff = true;
1771 for (unsigned I = 1; I != Len-D; ++I) {
1772 if (C[I] != Len-1-I)
1773 IsDeal = false;
1774 if (C[I] != I-(1-D)) // I-1, I
1775 IsShuff = false;
1776 }
1777 // At most one, IsDeal or IsShuff, can be non-zero.
1778 assert(!(IsDeal || IsShuff) || IsDeal != IsShuff);
1779 static unsigned Deals[] = { Hexagon::V6_vdealb, Hexagon::V6_vdealh };
1780 static unsigned Shufs[] = { Hexagon::V6_vshuffb, Hexagon::V6_vshuffh };
1781 return IsDeal ? Deals[D] : (IsShuff ? Shufs[D] : 0);
1782 };
1783
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001784 while (!All.empty()) {
1785 unsigned A = *All.begin();
1786 All.erase(A);
1787 CycleType C;
1788 C.push_back(A);
1789 for (unsigned B = Perm[A]; B != A; B = Perm[B]) {
1790 C.push_back(B);
1791 All.erase(B);
1792 }
1793 if (C.size() <= 1)
1794 continue;
1795 Cycles.insert(canonicalize(C));
1796 }
1797
Krzysztof Parzyszekd2967862017-12-06 22:41:49 +00001798 MVT SingleTy = getSingleVT(MVT::i8);
1799 MVT PairTy = getPairVT(MVT::i8);
1800
1801 // Recognize patterns for V6_vdeal{b,h} and V6_vshuff{b,h}.
1802 if (unsigned(VecLen) == HwLen) {
1803 if (unsigned SingleOpc = pfs(Cycles, LogLen)) {
1804 Results.push(SingleOpc, SingleTy, {Va});
1805 return OpRef::res(Results.top());
1806 }
1807 }
1808
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001809 SmallVector<unsigned,8> SwapElems;
1810 if (HwLen == unsigned(VecLen))
1811 SwapElems.push_back(LogLen-1);
1812
1813 for (const CycleType &C : Cycles) {
1814 unsigned First = (C[0] == LogLen-1) ? 1 : 0;
1815 SwapElems.append(C.begin()+First, C.end());
1816 if (First == 0)
1817 SwapElems.push_back(C[0]);
1818 }
1819
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001820 const SDLoc &dl(Results.InpNode);
1821 OpRef Arg = !Extend ? Va
1822 : concat(Va, OpRef::undef(SingleTy), Results);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001823
1824 for (unsigned I = 0, E = SwapElems.size(); I != E; ) {
1825 bool IsInc = I == E-1 || SwapElems[I] < SwapElems[I+1];
1826 unsigned S = (1u << SwapElems[I]);
1827 if (I < E-1) {
1828 while (++I < E-1 && IsInc == (SwapElems[I] < SwapElems[I+1]))
1829 S |= 1u << SwapElems[I];
1830 // The above loop will not add a bit for the final SwapElems[I+1],
1831 // so add it here.
1832 S |= 1u << SwapElems[I];
1833 }
1834 ++I;
1835
1836 NodeTemplate Res;
1837 Results.push(Hexagon::A2_tfrsi, MVT::i32,
1838 { DAG.getTargetConstant(S, dl, MVT::i32) });
1839 Res.Opc = IsInc ? Hexagon::V6_vshuffvdd : Hexagon::V6_vdealvdd;
1840 Res.Ty = PairTy;
1841 Res.Ops = { OpRef::hi(Arg), OpRef::lo(Arg), OpRef::res(-1) };
1842 Results.push(Res);
1843 Arg = OpRef::res(Results.top());
1844 }
1845
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001846 return !Extend ? Arg : OpRef::lo(Arg);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001847}
1848
1849OpRef HvxSelector::butterfly(ShuffleMask SM, OpRef Va, ResultStack &Results) {
1850 DEBUG_WITH_TYPE("isel", {dbgs() << __func__ << '\n';});
1851 // Butterfly shuffles.
1852 //
1853 // V6_vdelta
1854 // V6_vrdelta
1855 // V6_vror
1856
1857 // The assumption here is that all elements picked by Mask are in the
1858 // first operand to the vector_shuffle. This assumption is enforced
1859 // by the caller.
1860
1861 MVT ResTy = getSingleVT(MVT::i8);
1862 PermNetwork::Controls FC, RC;
1863 const SDLoc &dl(Results.InpNode);
1864 int VecLen = SM.Mask.size();
1865
1866 for (int M : SM.Mask) {
1867 if (M != -1 && M >= VecLen)
1868 return OpRef::fail();
1869 }
1870
1871 // Try the deltas/benes for both single vectors and vector pairs.
1872 ForwardDeltaNetwork FN(SM.Mask);
1873 if (FN.run(FC)) {
1874 SDValue Ctl = getVectorConstant(FC, dl);
1875 Results.push(Hexagon::V6_vdelta, ResTy, {Va, OpRef(Ctl)});
1876 return OpRef::res(Results.top());
1877 }
1878
1879 // Try reverse delta.
1880 ReverseDeltaNetwork RN(SM.Mask);
1881 if (RN.run(RC)) {
1882 SDValue Ctl = getVectorConstant(RC, dl);
1883 Results.push(Hexagon::V6_vrdelta, ResTy, {Va, OpRef(Ctl)});
1884 return OpRef::res(Results.top());
1885 }
1886
1887 // Do Benes.
1888 BenesNetwork BN(SM.Mask);
1889 if (BN.run(FC, RC)) {
1890 SDValue CtlF = getVectorConstant(FC, dl);
1891 SDValue CtlR = getVectorConstant(RC, dl);
1892 Results.push(Hexagon::V6_vdelta, ResTy, {Va, OpRef(CtlF)});
1893 Results.push(Hexagon::V6_vrdelta, ResTy,
1894 {OpRef::res(-1), OpRef(CtlR)});
1895 return OpRef::res(Results.top());
1896 }
1897
1898 return OpRef::fail();
1899}
1900
1901SDValue HvxSelector::getVectorConstant(ArrayRef<uint8_t> Data,
1902 const SDLoc &dl) {
1903 SmallVector<SDValue, 128> Elems;
1904 for (uint8_t C : Data)
1905 Elems.push_back(DAG.getConstant(C, dl, MVT::i8));
1906 MVT VecTy = MVT::getVectorVT(MVT::i8, Data.size());
1907 SDValue BV = DAG.getBuildVector(VecTy, dl, Elems);
1908 SDValue LV = Lower.LowerOperation(BV, DAG);
1909 DAG.RemoveDeadNode(BV.getNode());
1910 return LV;
1911}
1912
1913void HvxSelector::selectShuffle(SDNode *N) {
1914 DEBUG_WITH_TYPE("isel", {
1915 dbgs() << "Starting " << __func__ << " on node:\n";
1916 N->dump(&DAG);
1917 });
1918 MVT ResTy = N->getValueType(0).getSimpleVT();
1919 // Assume that vector shuffles operate on vectors of bytes.
1920 assert(ResTy.isVector() && ResTy.getVectorElementType() == MVT::i8);
1921
1922 auto *SN = cast<ShuffleVectorSDNode>(N);
1923 std::vector<int> Mask(SN->getMask().begin(), SN->getMask().end());
1924 // This shouldn't really be necessary. Is it?
1925 for (int &Idx : Mask)
1926 if (Idx != -1 && Idx < 0)
1927 Idx = -1;
1928
1929 unsigned VecLen = Mask.size();
1930 bool HavePairs = (2*HwLen == VecLen);
1931 assert(ResTy.getSizeInBits() / 8 == VecLen);
1932
1933 // Vd = vector_shuffle Va, Vb, Mask
1934 //
1935
1936 bool UseLeft = false, UseRight = false;
1937 for (unsigned I = 0; I != VecLen; ++I) {
1938 if (Mask[I] == -1)
1939 continue;
1940 unsigned Idx = Mask[I];
1941 assert(Idx < 2*VecLen);
1942 if (Idx < VecLen)
1943 UseLeft = true;
1944 else
1945 UseRight = true;
1946 }
1947
1948 DEBUG_WITH_TYPE("isel", {
1949 dbgs() << "VecLen=" << VecLen << " HwLen=" << HwLen << " UseLeft="
1950 << UseLeft << " UseRight=" << UseRight << " HavePairs="
1951 << HavePairs << '\n';
1952 });
1953 // If the mask is all -1's, generate "undef".
1954 if (!UseLeft && !UseRight) {
1955 ISel.ReplaceNode(N, ISel.selectUndef(SDLoc(SN), ResTy).getNode());
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001956 return;
1957 }
1958
1959 SDValue Vec0 = N->getOperand(0);
1960 SDValue Vec1 = N->getOperand(1);
1961 ResultStack Results(SN);
1962 Results.push(TargetOpcode::COPY, ResTy, {Vec0});
1963 Results.push(TargetOpcode::COPY, ResTy, {Vec1});
1964 OpRef Va = OpRef::res(Results.top()-1);
1965 OpRef Vb = OpRef::res(Results.top());
1966
1967 OpRef Res = !HavePairs ? shuffs2(ShuffleMask(Mask), Va, Vb, Results)
1968 : shuffp2(ShuffleMask(Mask), Va, Vb, Results);
1969
1970 bool Done = Res.isValid();
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001971 if (Done) {
1972 // Make sure that Res is on the stack before materializing.
1973 Results.push(TargetOpcode::COPY, ResTy, {Res});
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001974 materialize(Results);
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001975 } else {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001976 Done = scalarizeShuffle(Mask, SDLoc(N), ResTy, Vec0, Vec1, N);
Krzysztof Parzyszek64533cf2017-12-06 21:25:03 +00001977 }
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001978
1979 if (!Done) {
1980#ifndef NDEBUG
1981 dbgs() << "Unhandled shuffle:\n";
1982 SN->dumpr(&DAG);
1983#endif
1984 llvm_unreachable("Failed to select vector shuffle");
1985 }
1986}
1987
1988void HvxSelector::selectRor(SDNode *N) {
1989 // If this is a rotation by less than 8, use V6_valignbi.
1990 MVT Ty = N->getValueType(0).getSimpleVT();
1991 const SDLoc &dl(N);
1992 SDValue VecV = N->getOperand(0);
1993 SDValue RotV = N->getOperand(1);
1994 SDNode *NewN = nullptr;
1995
1996 if (auto *CN = dyn_cast<ConstantSDNode>(RotV.getNode())) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001997 unsigned S = CN->getZExtValue() % HST.getVectorLength();
1998 if (S == 0) {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001999 NewN = VecV.getNode();
2000 } else if (isUInt<3>(S)) {
2001 SDValue C = DAG.getTargetConstant(S, dl, MVT::i32);
2002 NewN = DAG.getMachineNode(Hexagon::V6_valignbi, dl, Ty,
2003 {VecV, VecV, C});
2004 }
2005 }
2006
2007 if (!NewN)
2008 NewN = DAG.getMachineNode(Hexagon::V6_vror, dl, Ty, {VecV, RotV});
2009
2010 ISel.ReplaceNode(N, NewN);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002011}
2012
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002013void HvxSelector::selectVAlign(SDNode *N) {
2014 SDValue Vv = N->getOperand(0);
2015 SDValue Vu = N->getOperand(1);
2016 SDValue Rt = N->getOperand(2);
2017 SDNode *NewN = DAG.getMachineNode(Hexagon::V6_valignb, SDLoc(N),
2018 N->getValueType(0), {Vv, Vu, Rt});
2019 ISel.ReplaceNode(N, NewN);
2020 DAG.RemoveDeadNode(N);
2021}
2022
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002023void HexagonDAGToDAGISel::SelectHvxShuffle(SDNode *N) {
2024 HvxSelector(*this, *CurDAG).selectShuffle(N);
2025}
2026
2027void HexagonDAGToDAGISel::SelectHvxRor(SDNode *N) {
2028 HvxSelector(*this, *CurDAG).selectRor(N);
2029}
2030
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002031void HexagonDAGToDAGISel::SelectHvxVAlign(SDNode *N) {
2032 HvxSelector(*this, *CurDAG).selectVAlign(N);
2033}
2034
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002035void HexagonDAGToDAGISel::SelectV65GatherPred(SDNode *N) {
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +00002036 if (!HST->usePackets()) {
2037 report_fatal_error("Support for gather requires packets, "
2038 "which are disabled");
2039 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002040 const SDLoc &dl(N);
2041 SDValue Chain = N->getOperand(0);
2042 SDValue Address = N->getOperand(2);
2043 SDValue Predicate = N->getOperand(3);
2044 SDValue Base = N->getOperand(4);
2045 SDValue Modifier = N->getOperand(5);
2046 SDValue Offset = N->getOperand(6);
2047
2048 unsigned Opcode;
2049 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2050 switch (IntNo) {
2051 default:
2052 llvm_unreachable("Unexpected HVX gather intrinsic.");
2053 case Intrinsic::hexagon_V6_vgathermhq:
2054 case Intrinsic::hexagon_V6_vgathermhq_128B:
2055 Opcode = Hexagon::V6_vgathermhq_pseudo;
2056 break;
2057 case Intrinsic::hexagon_V6_vgathermwq:
2058 case Intrinsic::hexagon_V6_vgathermwq_128B:
2059 Opcode = Hexagon::V6_vgathermwq_pseudo;
2060 break;
2061 case Intrinsic::hexagon_V6_vgathermhwq:
2062 case Intrinsic::hexagon_V6_vgathermhwq_128B:
2063 Opcode = Hexagon::V6_vgathermhwq_pseudo;
2064 break;
2065 }
2066
2067 SDVTList VTs = CurDAG->getVTList(MVT::Other);
2068 SDValue Ops[] = { Address, Predicate, Base, Modifier, Offset, Chain };
2069 SDNode *Result = CurDAG->getMachineNode(Opcode, dl, VTs, Ops);
2070
2071 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2072 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2073 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2074
Nirav Dave3264c1b2018-03-19 20:19:46 +00002075 ReplaceNode(N, Result);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002076}
2077
2078void HexagonDAGToDAGISel::SelectV65Gather(SDNode *N) {
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +00002079 if (!HST->usePackets()) {
2080 report_fatal_error("Support for gather requires packets, "
2081 "which are disabled");
2082 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002083 const SDLoc &dl(N);
2084 SDValue Chain = N->getOperand(0);
2085 SDValue Address = N->getOperand(2);
2086 SDValue Base = N->getOperand(3);
2087 SDValue Modifier = N->getOperand(4);
2088 SDValue Offset = N->getOperand(5);
2089
2090 unsigned Opcode;
2091 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2092 switch (IntNo) {
2093 default:
2094 llvm_unreachable("Unexpected HVX gather intrinsic.");
2095 case Intrinsic::hexagon_V6_vgathermh:
2096 case Intrinsic::hexagon_V6_vgathermh_128B:
2097 Opcode = Hexagon::V6_vgathermh_pseudo;
2098 break;
2099 case Intrinsic::hexagon_V6_vgathermw:
2100 case Intrinsic::hexagon_V6_vgathermw_128B:
2101 Opcode = Hexagon::V6_vgathermw_pseudo;
2102 break;
2103 case Intrinsic::hexagon_V6_vgathermhw:
2104 case Intrinsic::hexagon_V6_vgathermhw_128B:
2105 Opcode = Hexagon::V6_vgathermhw_pseudo;
2106 break;
2107 }
2108
2109 SDVTList VTs = CurDAG->getVTList(MVT::Other);
2110 SDValue Ops[] = { Address, Base, Modifier, Offset, Chain };
2111 SDNode *Result = CurDAG->getMachineNode(Opcode, dl, VTs, Ops);
2112
2113 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2114 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2115 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2116
Nirav Dave3264c1b2018-03-19 20:19:46 +00002117 ReplaceNode(N, Result);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002118}
2119
2120void HexagonDAGToDAGISel::SelectHVXDualOutput(SDNode *N) {
2121 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2122 SDNode *Result;
2123 switch (IID) {
2124 case Intrinsic::hexagon_V6_vaddcarry: {
2125 SmallVector<SDValue, 3> Ops = { N->getOperand(1), N->getOperand(2),
2126 N->getOperand(3) };
2127 SDVTList VTs = CurDAG->getVTList(MVT::v16i32, MVT::v512i1);
2128 Result = CurDAG->getMachineNode(Hexagon::V6_vaddcarry, SDLoc(N), VTs, Ops);
2129 break;
2130 }
2131 case Intrinsic::hexagon_V6_vaddcarry_128B: {
2132 SmallVector<SDValue, 3> Ops = { N->getOperand(1), N->getOperand(2),
2133 N->getOperand(3) };
2134 SDVTList VTs = CurDAG->getVTList(MVT::v32i32, MVT::v1024i1);
2135 Result = CurDAG->getMachineNode(Hexagon::V6_vaddcarry, SDLoc(N), VTs, Ops);
2136 break;
2137 }
2138 case Intrinsic::hexagon_V6_vsubcarry: {
2139 SmallVector<SDValue, 3> Ops = { N->getOperand(1), N->getOperand(2),
2140 N->getOperand(3) };
2141 SDVTList VTs = CurDAG->getVTList(MVT::v16i32, MVT::v512i1);
2142 Result = CurDAG->getMachineNode(Hexagon::V6_vsubcarry, SDLoc(N), VTs, Ops);
2143 break;
2144 }
2145 case Intrinsic::hexagon_V6_vsubcarry_128B: {
2146 SmallVector<SDValue, 3> Ops = { N->getOperand(1), N->getOperand(2),
2147 N->getOperand(3) };
2148 SDVTList VTs = CurDAG->getVTList(MVT::v32i32, MVT::v1024i1);
2149 Result = CurDAG->getMachineNode(Hexagon::V6_vsubcarry, SDLoc(N), VTs, Ops);
2150 break;
2151 }
2152 default:
2153 llvm_unreachable("Unexpected HVX dual output intrinsic.");
2154 }
2155 ReplaceUses(N, Result);
2156 ReplaceUses(SDValue(N, 0), SDValue(Result, 0));
2157 ReplaceUses(SDValue(N, 1), SDValue(Result, 1));
2158 CurDAG->RemoveDeadNode(N);
2159}