blob: 803aecea9659b090b657d07696fa807f06f026f9 [file] [log] [blame]
Chris Lattner4ad53bd2006-04-17 00:35:34 +00001//===-- PerfectShuffle.cpp - Perfect Shuffle Generator --------------------===//
Chris Lattner27e98aa2006-04-17 00:30:41 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file computes an optimal sequence of instructions for doing all shuffles
11// of two 4-element vectors. With a release build and when configured to emit
12// an altivec instruction table, this takes about 30s to run on a 2.7Ghz
13// PowerPC G5.
14//
15//===----------------------------------------------------------------------===//
16
17#include <iostream>
18#include <vector>
Chris Lattner309db812006-04-18 00:21:25 +000019#include <cassert>
Chris Lattner27e98aa2006-04-17 00:30:41 +000020
21struct Operator;
22
23// Masks are 4-nibble hex numbers. Values 0-7 in any nibble means that it takes
24// an element from that value of the input vectors. A value of 8 means the
25// entry is undefined.
26
27// Mask manipulation functions.
28static inline unsigned short MakeMask(unsigned V0, unsigned V1,
29 unsigned V2, unsigned V3) {
30 return (V0 << (3*4)) | (V1 << (2*4)) | (V2 << (1*4)) | (V3 << (0*4));
31}
32
33/// getMaskElt - Return element N of the specified mask.
34static unsigned getMaskElt(unsigned Mask, unsigned Elt) {
35 return (Mask >> ((3-Elt)*4)) & 0xF;
36}
37
38static unsigned setMaskElt(unsigned Mask, unsigned Elt, unsigned NewVal) {
39 unsigned FieldShift = ((3-Elt)*4);
40 return (Mask & ~(0xF << FieldShift)) | (NewVal << FieldShift);
41}
42
43// Reject elements where the values are 9-15.
44static bool isValidMask(unsigned short Mask) {
45 unsigned short UndefBits = Mask & 0x8888;
46 return (Mask & ((UndefBits >> 1)|(UndefBits>>2)|(UndefBits>>3))) == 0;
47}
48
49/// hasUndefElements - Return true if any of the elements in the mask are undefs
50///
51static bool hasUndefElements(unsigned short Mask) {
52 return (Mask & 0x8888) != 0;
53}
54
55/// isOnlyLHSMask - Return true if this mask only refers to its LHS, not
56/// including undef values..
57static bool isOnlyLHSMask(unsigned short Mask) {
58 return (Mask & 0x4444) == 0;
59}
60
61/// getLHSOnlyMask - Given a mask that refers to its LHS and RHS, modify it to
62/// refer to the LHS only (for when one argument value is passed into the same
63/// function twice).
64static unsigned short getLHSOnlyMask(unsigned short Mask) {
65 return Mask & 0xBBBB; // Keep only LHS and Undefs.
66}
67
68/// getCompressedMask - Turn a 16-bit uncompressed mask (where each elt uses 4
69/// bits) into a compressed 13-bit mask, where each elt is multiplied by 9.
70static unsigned getCompressedMask(unsigned short Mask) {
71 return getMaskElt(Mask, 0)*9*9*9 + getMaskElt(Mask, 1)*9*9 +
72 getMaskElt(Mask, 2)*9 + getMaskElt(Mask, 3);
73}
74
75static void PrintMask(unsigned i, std::ostream &OS) {
76 OS << "<" << (char)(getMaskElt(i, 0) == 8 ? 'u' : ('0'+getMaskElt(i, 0)))
77 << "," << (char)(getMaskElt(i, 1) == 8 ? 'u' : ('0'+getMaskElt(i, 1)))
78 << "," << (char)(getMaskElt(i, 2) == 8 ? 'u' : ('0'+getMaskElt(i, 2)))
79 << "," << (char)(getMaskElt(i, 3) == 8 ? 'u' : ('0'+getMaskElt(i, 3)))
80 << ">";
81}
82
83/// ShuffleVal - This represents a shufflevector operation.
84struct ShuffleVal {
85 unsigned Cost; // Number of instrs used to generate this value.
86 Operator *Op; // The Operation used to generate this value.
87 unsigned short Arg0, Arg1; // Input operands for this value.
88
89 ShuffleVal() : Cost(1000000) {}
90};
91
92
93/// ShufTab - This is the actual shuffle table that we are trying to generate.
94///
95static ShuffleVal ShufTab[65536];
96
97/// TheOperators - All of the operators that this target supports.
98static std::vector<Operator*> TheOperators;
99
100/// Operator - This is a vector operation that is available for use.
101struct Operator {
102 unsigned short ShuffleMask;
103 unsigned short OpNum;
104 const char *Name;
105
Chris Lattnercf1f6442006-04-17 00:47:18 +0000106 Operator(unsigned short shufflemask, const char *name, unsigned opnum)
107 : ShuffleMask(shufflemask), OpNum(opnum), Name(name) {
Chris Lattner27e98aa2006-04-17 00:30:41 +0000108 TheOperators.push_back(this);
109 }
110 ~Operator() {
111 assert(TheOperators.back() == this);
112 TheOperators.pop_back();
113 }
114
115 bool isOnlyLHSOperator() const {
116 return isOnlyLHSMask(ShuffleMask);
117 }
118
119 const char *getName() const { return Name; }
120
121 unsigned short getTransformedMask(unsigned short LHSMask, unsigned RHSMask) {
122 // Extract the elements from LHSMask and RHSMask, as appropriate.
123 unsigned Result = 0;
124 for (unsigned i = 0; i != 4; ++i) {
125 unsigned SrcElt = (ShuffleMask >> (4*i)) & 0xF;
126 unsigned ResElt;
127 if (SrcElt < 4)
128 ResElt = getMaskElt(LHSMask, SrcElt);
129 else if (SrcElt < 8)
130 ResElt = getMaskElt(RHSMask, SrcElt-4);
131 else {
132 assert(SrcElt == 8 && "Bad src elt!");
133 ResElt = 8;
134 }
135 Result |= ResElt << (4*i);
136 }
137 return Result;
138 }
139};
140
141static const char *getZeroCostOpName(unsigned short Op) {
142 if (ShufTab[Op].Arg0 == 0x0123)
143 return "LHS";
144 else if (ShufTab[Op].Arg0 == 0x4567)
145 return "RHS";
146 else {
147 assert(0 && "bad zero cost operation");
148 abort();
149 }
150}
151
152static void PrintOperation(unsigned ValNo, unsigned short Vals[]) {
153 unsigned short ThisOp = Vals[ValNo];
154 std::cerr << "t" << ValNo;
155 PrintMask(ThisOp, std::cerr);
156 std::cerr << " = " << ShufTab[ThisOp].Op->getName() << "(";
157
158 if (ShufTab[ShufTab[ThisOp].Arg0].Cost == 0) {
159 std::cerr << getZeroCostOpName(ShufTab[ThisOp].Arg0);
160 PrintMask(ShufTab[ThisOp].Arg0, std::cerr);
161 } else {
162 // Figure out what tmp # it is.
163 for (unsigned i = 0; ; ++i)
164 if (Vals[i] == ShufTab[ThisOp].Arg0) {
165 std::cerr << "t" << i;
166 break;
167 }
168 }
169
170 if (!ShufTab[Vals[ValNo]].Op->isOnlyLHSOperator()) {
171 std::cerr << ", ";
172 if (ShufTab[ShufTab[ThisOp].Arg1].Cost == 0) {
173 std::cerr << getZeroCostOpName(ShufTab[ThisOp].Arg1);
174 PrintMask(ShufTab[ThisOp].Arg1, std::cerr);
175 } else {
176 // Figure out what tmp # it is.
177 for (unsigned i = 0; ; ++i)
178 if (Vals[i] == ShufTab[ThisOp].Arg1) {
179 std::cerr << "t" << i;
180 break;
181 }
182 }
183 }
184 std::cerr << ") ";
185}
186
187static unsigned getNumEntered() {
188 unsigned Count = 0;
189 for (unsigned i = 0; i != 65536; ++i)
190 Count += ShufTab[i].Cost < 100;
191 return Count;
192}
193
194static void EvaluateOps(unsigned short Elt, unsigned short Vals[],
195 unsigned &NumVals) {
196 if (ShufTab[Elt].Cost == 0) return;
197
198 // If this value has already been evaluated, it is free. FIXME: match undefs.
199 for (unsigned i = 0, e = NumVals; i != e; ++i)
200 if (Vals[i] == Elt) return;
201
202 // Otherwise, get the operands of the value, then add it.
203 unsigned Arg0 = ShufTab[Elt].Arg0, Arg1 = ShufTab[Elt].Arg1;
204 if (ShufTab[Arg0].Cost)
205 EvaluateOps(Arg0, Vals, NumVals);
206 if (Arg0 != Arg1 && ShufTab[Arg1].Cost)
207 EvaluateOps(Arg1, Vals, NumVals);
208
209 Vals[NumVals++] = Elt;
210}
211
212
213int main() {
214 // Seed the table with accesses to the LHS and RHS.
215 ShufTab[0x0123].Cost = 0;
216 ShufTab[0x0123].Op = 0;
217 ShufTab[0x0123].Arg0 = 0x0123;
218 ShufTab[0x4567].Cost = 0;
219 ShufTab[0x4567].Op = 0;
220 ShufTab[0x4567].Arg0 = 0x4567;
221
222 // Seed the first-level of shuffles, shuffles whose inputs are the input to
223 // the vectorshuffle operation.
224 bool MadeChange = true;
225 unsigned OpCount = 0;
226 while (MadeChange) {
227 MadeChange = false;
228 ++OpCount;
229 std::cerr << "Starting iteration #" << OpCount << " with "
230 << getNumEntered() << " entries established.\n";
231
232 // Scan the table for two reasons: First, compute the maximum cost of any
233 // operation left in the table. Second, make sure that values with undefs
234 // have the cheapest alternative that they match.
235 unsigned MaxCost = ShufTab[0].Cost;
236 for (unsigned i = 1; i != 0x8889; ++i) {
237 if (!isValidMask(i)) continue;
238 if (ShufTab[i].Cost > MaxCost)
239 MaxCost = ShufTab[i].Cost;
240
241 // If this value has an undef, make it be computed the cheapest possible
242 // way of any of the things that it matches.
243 if (hasUndefElements(i)) {
244 // This code is a little bit tricky, so here's the idea: consider some
245 // permutation, like 7u4u. To compute the lowest cost for 7u4u, we
246 // need to take the minimum cost of all of 7[0-8]4[0-8], 81 entries. If
247 // there are 3 undefs, the number rises to 729 entries we have to scan,
248 // and for the 4 undef case, we have to scan the whole table.
249 //
250 // Instead of doing this huge amount of scanning, we process the table
251 // entries *in order*, and use the fact that 'u' is 8, larger than any
252 // valid index. Given an entry like 7u4u then, we only need to scan
253 // 7[0-7]4u - 8 entries. We can get away with this, because we already
254 // know that each of 704u, 714u, 724u, etc contain the minimum value of
255 // all of the 704[0-8], 714[0-8] and 724[0-8] entries respectively.
256 unsigned UndefIdx;
257 if (i & 0x8000)
258 UndefIdx = 0;
259 else if (i & 0x0800)
260 UndefIdx = 1;
261 else if (i & 0x0080)
262 UndefIdx = 2;
263 else if (i & 0x0008)
264 UndefIdx = 3;
265 else
266 abort();
267
268 unsigned MinVal = i;
269 unsigned MinCost = ShufTab[i].Cost;
270
271 // Scan the 8 entries.
272 for (unsigned j = 0; j != 8; ++j) {
273 unsigned NewElt = setMaskElt(i, UndefIdx, j);
274 if (ShufTab[NewElt].Cost < MinCost) {
275 MinCost = ShufTab[NewElt].Cost;
276 MinVal = NewElt;
277 }
278 }
279
280 // If we found something cheaper than what was here before, use it.
281 if (i != MinVal) {
282 MadeChange = true;
283 ShufTab[i] = ShufTab[MinVal];
284 }
285 }
286 }
287
288 for (unsigned LHS = 0; LHS != 0x8889; ++LHS) {
289 if (!isValidMask(LHS)) continue;
290 if (ShufTab[LHS].Cost > 1000) continue;
291
292 // If nothing involving this operand could possibly be cheaper than what
293 // we already have, don't consider it.
294 if (ShufTab[LHS].Cost + 1 >= MaxCost)
295 continue;
296
297 for (unsigned opnum = 0, e = TheOperators.size(); opnum != e; ++opnum) {
298 Operator *Op = TheOperators[opnum];
299 unsigned short Mask = Op->ShuffleMask;
300
301 // Evaluate op(LHS,LHS)
302 unsigned ResultMask = Op->getTransformedMask(LHS, LHS);
303
304 unsigned Cost = ShufTab[LHS].Cost + 1;
305 if (Cost < ShufTab[ResultMask].Cost) {
306 ShufTab[ResultMask].Cost = Cost;
307 ShufTab[ResultMask].Op = Op;
308 ShufTab[ResultMask].Arg0 = LHS;
309 ShufTab[ResultMask].Arg1 = LHS;
310 MadeChange = true;
311 }
312
313 // If this is a two input instruction, include the op(x,y) cases. If
314 // this is a one input instruction, skip this.
315 if (Op->isOnlyLHSOperator()) continue;
316
317 for (unsigned RHS = 0; RHS != 0x8889; ++RHS) {
318 if (!isValidMask(RHS)) continue;
319 if (ShufTab[RHS].Cost > 1000) continue;
320
321 // If nothing involving this operand could possibly be cheaper than
322 // what we already have, don't consider it.
323 if (ShufTab[RHS].Cost + 1 >= MaxCost)
324 continue;
325
326
327 // Evaluate op(LHS,RHS)
328 unsigned ResultMask = Op->getTransformedMask(LHS, RHS);
329
330 if (ShufTab[ResultMask].Cost <= OpCount ||
331 ShufTab[ResultMask].Cost <= ShufTab[LHS].Cost ||
332 ShufTab[ResultMask].Cost <= ShufTab[RHS].Cost)
333 continue;
334
335 // Figure out the cost to evaluate this, knowing that CSE's only need
336 // to be evaluated once.
337 unsigned short Vals[30];
338 unsigned NumVals = 0;
339 EvaluateOps(LHS, Vals, NumVals);
340 EvaluateOps(RHS, Vals, NumVals);
341
342 unsigned Cost = NumVals + 1;
343 if (Cost < ShufTab[ResultMask].Cost) {
344 ShufTab[ResultMask].Cost = Cost;
345 ShufTab[ResultMask].Op = Op;
346 ShufTab[ResultMask].Arg0 = LHS;
347 ShufTab[ResultMask].Arg1 = RHS;
348 MadeChange = true;
349 }
350 }
351 }
352 }
353 }
354
355 std::cerr << "Finished Table has " << getNumEntered()
356 << " entries established.\n";
357
358 unsigned CostArray[10] = { 0 };
359
360 // Compute a cost histogram.
361 for (unsigned i = 0; i != 65536; ++i) {
362 if (!isValidMask(i)) continue;
363 if (ShufTab[i].Cost > 9)
364 ++CostArray[9];
365 else
366 ++CostArray[ShufTab[i].Cost];
367 }
368
369 for (unsigned i = 0; i != 9; ++i)
370 if (CostArray[i])
371 std::cout << "// " << CostArray[i] << " entries have cost " << i << "\n";
372 if (CostArray[9])
373 std::cout << "// " << CostArray[9] << " entries have higher cost!\n";
374
375
376 // Build up the table to emit.
377 std::cout << "\n// This table is 6561*4 = 26244 bytes in size.\n";
Chris Lattner3033d4d2006-04-17 00:33:35 +0000378 std::cout << "static const unsigned PerfectShuffleTable[6561+1] = {\n";
Chris Lattner27e98aa2006-04-17 00:30:41 +0000379
380 for (unsigned i = 0; i != 0x8889; ++i) {
381 if (!isValidMask(i)) continue;
382
383 // CostSat - The cost of this operation saturated to two bits.
384 unsigned CostSat = ShufTab[i].Cost;
Chris Lattner8c3e8472006-04-17 05:25:16 +0000385 if (CostSat > 4) CostSat = 4;
386 if (CostSat == 0) CostSat = 1;
387 --CostSat; // Cost is now between 0-3.
Chris Lattner27e98aa2006-04-17 00:30:41 +0000388
389 unsigned OpNum = ShufTab[i].Op ? ShufTab[i].Op->OpNum : 0;
390 assert(OpNum < 16 && "Too few bits to encode operation!");
391
392 unsigned LHS = getCompressedMask(ShufTab[i].Arg0);
393 unsigned RHS = getCompressedMask(ShufTab[i].Arg1);
394
395 // Encode this as 2 bits of saturated cost, 4 bits of opcodes, 13 bits of
396 // LHS, and 13 bits of RHS = 32 bits.
Chris Lattner195d8ad2006-04-17 05:05:52 +0000397 unsigned Val = (CostSat << 30) | (OpNum << 26) | (LHS << 13) | RHS;
Chris Lattner27e98aa2006-04-17 00:30:41 +0000398
399 std::cout << " " << Val << "U,\t// ";
400 PrintMask(i, std::cout);
401 std::cout << ": Cost " << ShufTab[i].Cost;
402 std::cout << " " << (ShufTab[i].Op ? ShufTab[i].Op->getName() : "copy");
403 std::cout << " ";
404 if (ShufTab[ShufTab[i].Arg0].Cost == 0) {
405 std::cout << getZeroCostOpName(ShufTab[i].Arg0);
406 } else {
407 PrintMask(ShufTab[i].Arg0, std::cout);
408 }
409
410 if (ShufTab[i].Op && !ShufTab[i].Op->isOnlyLHSOperator()) {
411 std::cout << ", ";
412 if (ShufTab[ShufTab[i].Arg1].Cost == 0) {
413 std::cout << getZeroCostOpName(ShufTab[i].Arg1);
414 } else {
415 PrintMask(ShufTab[i].Arg1, std::cout);
416 }
417 }
418 std::cout << "\n";
419 }
420 std::cout << " 0\n};\n";
421
422 if (0) {
423 // Print out the table.
424 for (unsigned i = 0; i != 0x8889; ++i) {
425 if (!isValidMask(i)) continue;
426 if (ShufTab[i].Cost < 1000) {
427 PrintMask(i, std::cerr);
428 std::cerr << " - Cost " << ShufTab[i].Cost << " - ";
429
430 unsigned short Vals[30];
431 unsigned NumVals = 0;
432 EvaluateOps(i, Vals, NumVals);
433
434 for (unsigned j = 0, e = NumVals; j != e; ++j)
435 PrintOperation(j, Vals);
436 std::cerr << "\n";
437 }
438 }
439 }
440}
441
442
Chris Lattnercf1f6442006-04-17 00:47:18 +0000443#define GENERATE_ALTIVEC
444
445#ifdef GENERATE_ALTIVEC
Chris Lattner27e98aa2006-04-17 00:30:41 +0000446
447///===---------------------------------------------------------------------===//
448/// The altivec instruction definitions. This is the altivec-specific part of
449/// this file.
450///===---------------------------------------------------------------------===//
451
Chris Lattnercf1f6442006-04-17 00:47:18 +0000452// Note that the opcode numbers here must match those in the PPC backend.
453enum {
454 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
455 OP_VMRGHW,
456 OP_VMRGLW,
457 OP_VSPLTISW0,
458 OP_VSPLTISW1,
459 OP_VSPLTISW2,
460 OP_VSPLTISW3,
461 OP_VSLDOI4,
462 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000463 OP_VSLDOI12
Chris Lattnercf1f6442006-04-17 00:47:18 +0000464};
465
Chris Lattner27e98aa2006-04-17 00:30:41 +0000466struct vmrghw : public Operator {
Chris Lattnercf1f6442006-04-17 00:47:18 +0000467 vmrghw() : Operator(0x0415, "vmrghw", OP_VMRGHW) {}
Chris Lattner27e98aa2006-04-17 00:30:41 +0000468} the_vmrghw;
469
470struct vmrglw : public Operator {
Chris Lattnercf1f6442006-04-17 00:47:18 +0000471 vmrglw() : Operator(0x2637, "vmrglw", OP_VMRGLW) {}
Chris Lattner27e98aa2006-04-17 00:30:41 +0000472} the_vmrglw;
473
474template<unsigned Elt>
475struct vspltisw : public Operator {
Chris Lattnercf1f6442006-04-17 00:47:18 +0000476 vspltisw(const char *N, unsigned Opc)
477 : Operator(MakeMask(Elt, Elt, Elt, Elt), N, Opc) {}
Chris Lattner27e98aa2006-04-17 00:30:41 +0000478};
479
Chris Lattnercf1f6442006-04-17 00:47:18 +0000480vspltisw<0> the_vspltisw0("vspltisw0", OP_VSPLTISW0);
481vspltisw<1> the_vspltisw1("vspltisw1", OP_VSPLTISW1);
482vspltisw<2> the_vspltisw2("vspltisw2", OP_VSPLTISW2);
483vspltisw<3> the_vspltisw3("vspltisw3", OP_VSPLTISW3);
Chris Lattner27e98aa2006-04-17 00:30:41 +0000484
485template<unsigned N>
486struct vsldoi : public Operator {
Chris Lattnercf1f6442006-04-17 00:47:18 +0000487 vsldoi(const char *Name, unsigned Opc)
488 : Operator(MakeMask(N&7, (N+1)&7, (N+2)&7, (N+3)&7), Name, Opc) {
Chris Lattner27e98aa2006-04-17 00:30:41 +0000489 }
490};
491
Chris Lattnercf1f6442006-04-17 00:47:18 +0000492vsldoi<1> the_vsldoi1("vsldoi4" , OP_VSLDOI4);
493vsldoi<2> the_vsldoi2("vsldoi8" , OP_VSLDOI8);
494vsldoi<3> the_vsldoi3("vsldoi12", OP_VSLDOI12);
Chris Lattner27e98aa2006-04-17 00:30:41 +0000495
Chris Lattnercf1f6442006-04-17 00:47:18 +0000496#endif