2834a1eb8d9c7bb748da3a616b46d88947df6c97
[oota-llvm.git] / lib / Target / CellSPU / SPUInstrInfo.td
1 //==- SPUInstrInfo.td - Describe the Cell SPU Instructions -*- tablegen -*-==//
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 // Cell SPU Instructions:
10 //===----------------------------------------------------------------------===//
11
12 //===----------------------------------------------------------------------===//
13 // TODO Items (not urgent today, but would be nice, low priority)
14 //
15 // ANDBI, ORBI: SPU constructs a 4-byte constant for these instructions by
16 // concatenating the byte argument b as "bbbb". Could recognize this bit pattern
17 // in 16-bit and 32-bit constants and reduce instruction count.
18 //===----------------------------------------------------------------------===//
19
20 //===----------------------------------------------------------------------===//
21 // Pseudo instructions:
22 //===----------------------------------------------------------------------===//
23
24 let hasCtrlDep = 1, Defs = [R1], Uses = [R1] in {
25   def ADJCALLSTACKDOWN : Pseudo<(outs), (ins u16imm_i32:$amt),
26                                 "${:comment} ADJCALLSTACKDOWN",
27                                 [(callseq_start timm:$amt)]>;
28   def ADJCALLSTACKUP   : Pseudo<(outs), (ins u16imm_i32:$amt),
29                                 "${:comment} ADJCALLSTACKUP",
30                                 [(callseq_end timm:$amt)]>;
31 }
32
33 //===----------------------------------------------------------------------===//
34 // DWARF debugging Pseudo Instructions
35 //===----------------------------------------------------------------------===//
36
37 def DWARF_LOC        : Pseudo<(outs), (ins i32imm:$line, i32imm:$col, i32imm:$file),
38                               "${:comment} .loc $file, $line, $col",
39                       [(dwarf_loc (i32 imm:$line), (i32 imm:$col),
40                                   (i32 imm:$file))]>;
41
42 //===----------------------------------------------------------------------===//
43 // Loads:
44 // NB: The ordering is actually important, since the instruction selection
45 // will try each of the instructions in sequence, i.e., the D-form first with
46 // the 10-bit displacement, then the A-form with the 16 bit displacement, and
47 // finally the X-form with the register-register.
48 //===----------------------------------------------------------------------===//
49
50 let canFoldAsLoad = 1 in {
51   class LoadDFormVec<ValueType vectype>
52     : RI10Form<0b00101100, (outs VECREG:$rT), (ins dformaddr:$src),
53                "lqd\t$rT, $src",
54                LoadStore,
55                [(set (vectype VECREG:$rT), (load dform_addr:$src))]>
56   { }
57
58   class LoadDForm<RegisterClass rclass>
59     : RI10Form<0b00101100, (outs rclass:$rT), (ins dformaddr:$src),
60                "lqd\t$rT, $src",
61                LoadStore,
62                [(set rclass:$rT, (load dform_addr:$src))]>
63   { }
64
65   multiclass LoadDForms
66   {
67     def v16i8: LoadDFormVec<v16i8>;
68     def v8i16: LoadDFormVec<v8i16>;
69     def v4i32: LoadDFormVec<v4i32>;
70     def v2i64: LoadDFormVec<v2i64>;
71     def v4f32: LoadDFormVec<v4f32>;
72     def v2f64: LoadDFormVec<v2f64>;
73
74     def v2i32: LoadDFormVec<v2i32>;
75
76     def r128:  LoadDForm<GPRC>;
77     def r64:   LoadDForm<R64C>;
78     def r32:   LoadDForm<R32C>;
79     def f32:   LoadDForm<R32FP>;
80     def f64:   LoadDForm<R64FP>;
81     def r16:   LoadDForm<R16C>;
82     def r8:    LoadDForm<R8C>;
83   }
84
85   class LoadAFormVec<ValueType vectype>
86     : RI16Form<0b100001100, (outs VECREG:$rT), (ins addr256k:$src),
87                "lqa\t$rT, $src",
88                LoadStore,
89                [(set (vectype VECREG:$rT), (load aform_addr:$src))]>
90   { }
91
92   class LoadAForm<RegisterClass rclass>
93     : RI16Form<0b100001100, (outs rclass:$rT), (ins addr256k:$src),
94                "lqa\t$rT, $src",
95                LoadStore,
96                [(set rclass:$rT, (load aform_addr:$src))]>
97   { }
98
99   multiclass LoadAForms
100   {
101     def v16i8: LoadAFormVec<v16i8>;
102     def v8i16: LoadAFormVec<v8i16>;
103     def v4i32: LoadAFormVec<v4i32>;
104     def v2i64: LoadAFormVec<v2i64>;
105     def v4f32: LoadAFormVec<v4f32>;
106     def v2f64: LoadAFormVec<v2f64>;
107
108     def v2i32: LoadAFormVec<v2i32>;
109
110     def r128:  LoadAForm<GPRC>;
111     def r64:   LoadAForm<R64C>;
112     def r32:   LoadAForm<R32C>;
113     def f32:   LoadAForm<R32FP>;
114     def f64:   LoadAForm<R64FP>;
115     def r16:   LoadAForm<R16C>;
116     def r8:    LoadAForm<R8C>;
117   }
118
119   class LoadXFormVec<ValueType vectype>
120     : RRForm<0b00100011100, (outs VECREG:$rT), (ins memrr:$src),
121              "lqx\t$rT, $src",
122              LoadStore,
123              [(set (vectype VECREG:$rT), (load xform_addr:$src))]>
124   { }
125
126   class LoadXForm<RegisterClass rclass>
127     : RRForm<0b00100011100, (outs rclass:$rT), (ins memrr:$src),
128              "lqx\t$rT, $src",
129              LoadStore,
130              [(set rclass:$rT, (load xform_addr:$src))]>
131   { }
132
133   multiclass LoadXForms
134   {
135     def v16i8: LoadXFormVec<v16i8>;
136     def v8i16: LoadXFormVec<v8i16>;
137     def v4i32: LoadXFormVec<v4i32>;
138     def v2i64: LoadXFormVec<v2i64>;
139     def v4f32: LoadXFormVec<v4f32>;
140     def v2f64: LoadXFormVec<v2f64>;
141
142     def v2i32: LoadXFormVec<v2i32>;
143
144     def r128:  LoadXForm<GPRC>;
145     def r64:   LoadXForm<R64C>;
146     def r32:   LoadXForm<R32C>;
147     def f32:   LoadXForm<R32FP>;
148     def f64:   LoadXForm<R64FP>;
149     def r16:   LoadXForm<R16C>;
150     def r8:    LoadXForm<R8C>;
151   }
152
153   defm LQA : LoadAForms;
154   defm LQD : LoadDForms;
155   defm LQX : LoadXForms;
156
157 /* Load quadword, PC relative: Not much use at this point in time.
158    Might be of use later for relocatable code. It's effectively the
159    same as LQA, but uses PC-relative addressing.
160   def LQR : RI16Form<0b111001100, (outs VECREG:$rT), (ins s16imm:$disp),
161                      "lqr\t$rT, $disp", LoadStore,
162                      [(set VECREG:$rT, (load iaddr:$disp))]>;
163  */
164 }
165
166 //===----------------------------------------------------------------------===//
167 // Stores:
168 //===----------------------------------------------------------------------===//
169 class StoreDFormVec<ValueType vectype>
170   : RI10Form<0b00100100, (outs), (ins VECREG:$rT, dformaddr:$src),
171              "stqd\t$rT, $src",
172              LoadStore,
173              [(store (vectype VECREG:$rT), dform_addr:$src)]>
174 { }
175
176 class StoreDForm<RegisterClass rclass>
177   : RI10Form<0b00100100, (outs), (ins rclass:$rT, dformaddr:$src),
178              "stqd\t$rT, $src",
179              LoadStore,
180              [(store rclass:$rT, dform_addr:$src)]>
181 { }
182
183 multiclass StoreDForms
184 {
185   def v16i8: StoreDFormVec<v16i8>;
186   def v8i16: StoreDFormVec<v8i16>;
187   def v4i32: StoreDFormVec<v4i32>;
188   def v2i64: StoreDFormVec<v2i64>;
189   def v4f32: StoreDFormVec<v4f32>;
190   def v2f64: StoreDFormVec<v2f64>;
191
192   def v2i32: StoreDFormVec<v2i32>;
193
194   def r128:  StoreDForm<GPRC>;
195   def r64:   StoreDForm<R64C>;
196   def r32:   StoreDForm<R32C>;
197   def f32:   StoreDForm<R32FP>;
198   def f64:   StoreDForm<R64FP>;
199   def r16:   StoreDForm<R16C>;
200   def r8:    StoreDForm<R8C>;
201 }
202
203 class StoreAFormVec<ValueType vectype>
204   : RI16Form<0b0010010, (outs), (ins VECREG:$rT, addr256k:$src),
205              "stqa\t$rT, $src",
206              LoadStore,
207              [(store (vectype VECREG:$rT), aform_addr:$src)]>;
208
209 class StoreAForm<RegisterClass rclass>
210   : RI16Form<0b001001, (outs), (ins rclass:$rT, addr256k:$src),
211              "stqa\t$rT, $src",
212              LoadStore,
213              [(store rclass:$rT, aform_addr:$src)]>;
214
215 multiclass StoreAForms
216 {
217   def v16i8: StoreAFormVec<v16i8>;
218   def v8i16: StoreAFormVec<v8i16>;
219   def v4i32: StoreAFormVec<v4i32>;
220   def v2i64: StoreAFormVec<v2i64>;
221   def v4f32: StoreAFormVec<v4f32>;
222   def v2f64: StoreAFormVec<v2f64>;
223
224   def v2i32: StoreAFormVec<v2i32>;
225
226   def r128:  StoreAForm<GPRC>;
227   def r64:   StoreAForm<R64C>;
228   def r32:   StoreAForm<R32C>;
229   def f32:   StoreAForm<R32FP>;
230   def f64:   StoreAForm<R64FP>;
231   def r16:   StoreAForm<R16C>;
232   def r8:    StoreAForm<R8C>;
233 }
234
235 class StoreXFormVec<ValueType vectype>
236   : RRForm<0b00100100, (outs), (ins VECREG:$rT, memrr:$src),
237            "stqx\t$rT, $src",
238            LoadStore,
239            [(store (vectype VECREG:$rT), xform_addr:$src)]>
240 { }
241
242 class StoreXForm<RegisterClass rclass>
243   : RRForm<0b00100100, (outs), (ins rclass:$rT, memrr:$src),
244            "stqx\t$rT, $src",
245            LoadStore,
246            [(store rclass:$rT, xform_addr:$src)]>
247 { }
248
249 multiclass StoreXForms
250 {
251   def v16i8: StoreXFormVec<v16i8>;
252   def v8i16: StoreXFormVec<v8i16>;
253   def v4i32: StoreXFormVec<v4i32>;
254   def v2i64: StoreXFormVec<v2i64>;
255   def v4f32: StoreXFormVec<v4f32>;
256   def v2f64: StoreXFormVec<v2f64>;
257
258   def v2i32: StoreXFormVec<v2i32>;
259
260   def r128:  StoreXForm<GPRC>;
261   def r64:   StoreXForm<R64C>;
262   def r32:   StoreXForm<R32C>;
263   def f32:   StoreXForm<R32FP>;
264   def f64:   StoreXForm<R64FP>;
265   def r16:   StoreXForm<R16C>;
266   def r8:    StoreXForm<R8C>;
267 }
268
269 defm STQD : StoreDForms;
270 defm STQA : StoreAForms;
271 defm STQX : StoreXForms;
272
273 /* Store quadword, PC relative: Not much use at this point in time. Might
274    be useful for relocatable code.
275 def STQR : RI16Form<0b111000100, (outs), (ins VECREG:$rT, s16imm:$disp),
276                    "stqr\t$rT, $disp", LoadStore,
277                    [(store VECREG:$rT, iaddr:$disp)]>;
278 */
279
280 //===----------------------------------------------------------------------===//
281 // Generate Controls for Insertion:
282 //===----------------------------------------------------------------------===//
283
284 def CBD: RI7Form<0b10101111100, (outs VECREG:$rT), (ins shufaddr:$src),
285     "cbd\t$rT, $src", ShuffleOp,
286     [(set (v16i8 VECREG:$rT), (SPUshufmask dform2_addr:$src))]>;
287
288 def CBX: RRForm<0b00101011100, (outs VECREG:$rT), (ins memrr:$src),
289     "cbx\t$rT, $src", ShuffleOp,
290     [(set (v16i8 VECREG:$rT), (SPUshufmask xform_addr:$src))]>;
291
292 def CHD: RI7Form<0b10101111100, (outs VECREG:$rT), (ins shufaddr:$src),
293     "chd\t$rT, $src", ShuffleOp,
294     [(set (v8i16 VECREG:$rT), (SPUshufmask dform2_addr:$src))]>;
295
296 def CHX: RRForm<0b10101011100, (outs VECREG:$rT), (ins memrr:$src),
297     "chx\t$rT, $src", ShuffleOp,
298     [(set (v8i16 VECREG:$rT), (SPUshufmask xform_addr:$src))]>;
299
300 def CWD: RI7Form<0b01101111100, (outs VECREG:$rT), (ins shufaddr:$src),
301     "cwd\t$rT, $src", ShuffleOp,
302     [(set (v4i32 VECREG:$rT), (SPUshufmask dform2_addr:$src))]>;
303
304 def CWX: RRForm<0b01101011100, (outs VECREG:$rT), (ins memrr:$src),
305     "cwx\t$rT, $src", ShuffleOp,
306     [(set (v4i32 VECREG:$rT), (SPUshufmask xform_addr:$src))]>;
307
308 def CWDf32: RI7Form<0b01101111100, (outs VECREG:$rT), (ins shufaddr:$src),
309     "cwd\t$rT, $src", ShuffleOp,
310     [(set (v4f32 VECREG:$rT), (SPUshufmask dform2_addr:$src))]>;
311
312 def CWXf32: RRForm<0b01101011100, (outs VECREG:$rT), (ins memrr:$src),
313     "cwx\t$rT, $src", ShuffleOp,
314     [(set (v4f32 VECREG:$rT), (SPUshufmask xform_addr:$src))]>;
315
316 def CDD: RI7Form<0b11101111100, (outs VECREG:$rT), (ins shufaddr:$src),
317     "cdd\t$rT, $src", ShuffleOp,
318     [(set (v2i64 VECREG:$rT), (SPUshufmask dform2_addr:$src))]>;
319
320 def CDX: RRForm<0b11101011100, (outs VECREG:$rT), (ins memrr:$src),
321     "cdx\t$rT, $src", ShuffleOp,
322     [(set (v2i64 VECREG:$rT), (SPUshufmask xform_addr:$src))]>;
323
324 def CDDf64: RI7Form<0b11101111100, (outs VECREG:$rT), (ins shufaddr:$src),
325     "cdd\t$rT, $src", ShuffleOp,
326     [(set (v2f64 VECREG:$rT), (SPUshufmask dform2_addr:$src))]>;
327
328 def CDXf64: RRForm<0b11101011100, (outs VECREG:$rT), (ins memrr:$src),
329     "cdx\t$rT, $src", ShuffleOp,
330     [(set (v2f64 VECREG:$rT), (SPUshufmask xform_addr:$src))]>;
331
332 //===----------------------------------------------------------------------===//
333 // Constant formation:
334 //===----------------------------------------------------------------------===//
335
336 def ILHv8i16:
337   RI16Form<0b110000010, (outs VECREG:$rT), (ins s16imm:$val),
338     "ilh\t$rT, $val", ImmLoad,
339     [(set (v8i16 VECREG:$rT), (v8i16 v8i16SExt16Imm:$val))]>;
340
341 def ILHr16:
342   RI16Form<0b110000010, (outs R16C:$rT), (ins s16imm:$val),
343     "ilh\t$rT, $val", ImmLoad,
344     [(set R16C:$rT, immSExt16:$val)]>;
345
346 // Cell SPU doesn't have a native 8-bit immediate load, but ILH works ("with
347 // the right constant")
348 def ILHr8:
349   RI16Form<0b110000010, (outs R8C:$rT), (ins s16imm_i8:$val),
350     "ilh\t$rT, $val", ImmLoad,
351     [(set R8C:$rT, immSExt8:$val)]>;
352
353 // IL does sign extension!
354
355 class ILInst<dag OOL, dag IOL, list<dag> pattern>:
356   RI16Form<0b100000010, OOL, IOL, "il\t$rT, $val",
357            ImmLoad, pattern>;
358
359 class ILVecInst<ValueType vectype, Operand immtype, PatLeaf xform>:
360   ILInst<(outs VECREG:$rT), (ins immtype:$val),
361          [(set (vectype VECREG:$rT), (vectype xform:$val))]>;
362
363 class ILRegInst<RegisterClass rclass, Operand immtype, PatLeaf xform>:
364   ILInst<(outs rclass:$rT), (ins immtype:$val),
365          [(set rclass:$rT, xform:$val)]>;
366
367 multiclass ImmediateLoad
368 {
369   def v2i64: ILVecInst<v2i64, s16imm_i64, v2i64SExt16Imm>;
370   def v4i32: ILVecInst<v4i32, s16imm_i32, v4i32SExt16Imm>;
371
372   // TODO: Need v2f64, v4f32
373
374   def r64: ILRegInst<R64C, s16imm_i64, immSExt16>;
375   def r32: ILRegInst<R32C, s16imm_i32, immSExt16>;
376   def f32: ILRegInst<R32FP, s16imm_f32, fpimmSExt16>;
377   def f64: ILRegInst<R64FP, s16imm_f64, fpimmSExt16>;
378 }
379
380 defm IL : ImmediateLoad;
381
382 class ILHUInst<dag OOL, dag IOL, list<dag> pattern>:
383   RI16Form<0b010000010, OOL, IOL, "ilhu\t$rT, $val",
384            ImmLoad, pattern>;
385
386 class ILHUVecInst<ValueType vectype, Operand immtype, PatLeaf xform>:
387   ILHUInst<(outs VECREG:$rT), (ins immtype:$val),
388            [(set (vectype VECREG:$rT), (vectype xform:$val))]>;
389
390 class ILHURegInst<RegisterClass rclass, Operand immtype, PatLeaf xform>:
391   ILHUInst<(outs rclass:$rT), (ins immtype:$val),
392            [(set rclass:$rT, xform:$val)]>;
393
394 multiclass ImmLoadHalfwordUpper
395 {
396   def v2i64: ILHUVecInst<v2i64, u16imm_i64, immILHUvec_i64>;
397   def v4i32: ILHUVecInst<v4i32, u16imm_i32, immILHUvec>;
398
399   def r64: ILHURegInst<R64C, u16imm_i64, hi16>;
400   def r32: ILHURegInst<R32C, u16imm_i32, hi16>;
401
402   // Loads the high portion of an address
403   def hi: ILHURegInst<R32C, symbolHi, hi16>;
404
405   // Used in custom lowering constant SFP loads:
406   def f32: ILHURegInst<R32FP, f16imm, hi16_f32>;
407 }
408
409 defm ILHU : ImmLoadHalfwordUpper;
410
411 // Immediate load address (can also be used to load 18-bit unsigned constants,
412 // see the zext 16->32 pattern)
413
414 class ILAInst<dag OOL, dag IOL, list<dag> pattern>:
415   RI18Form<0b1000010, OOL, IOL, "ila\t$rT, $val",
416            LoadNOP, pattern>;
417
418 class ILAVecInst<ValueType vectype, Operand immtype, PatLeaf xform>:
419   ILAInst<(outs VECREG:$rT), (ins immtype:$val),
420           [(set (vectype VECREG:$rT), (vectype xform:$val))]>;
421
422 class ILARegInst<RegisterClass rclass, Operand immtype, PatLeaf xform>:
423   ILAInst<(outs rclass:$rT), (ins immtype:$val),
424           [(set rclass:$rT, xform:$val)]>;
425
426 multiclass ImmLoadAddress
427 {
428   def v2i64: ILAVecInst<v2i64, u18imm, v2i64Uns18Imm>;
429   def v4i32: ILAVecInst<v4i32, u18imm, v4i32Uns18Imm>;
430
431   def r64: ILARegInst<R64C, u18imm_i64, imm18>;
432   def r32: ILARegInst<R32C, u18imm, imm18>;
433   def f32: ILARegInst<R32FP, f18imm, fpimm18>;
434   def f64: ILARegInst<R64FP, f18imm_f64, fpimm18>;
435
436   def hi: ILARegInst<R32C, symbolHi, imm18>;
437   def lo: ILARegInst<R32C, symbolLo, imm18>;
438
439   def lsa: ILAInst<(outs R32C:$rT), (ins symbolLSA:$val),
440                    [/* no pattern */]>;
441 }
442
443 defm ILA : ImmLoadAddress;
444
445 // Immediate OR, Halfword Lower: The "other" part of loading large constants
446 // into 32-bit registers. See the anonymous pattern Pat<(i32 imm:$imm), ...>
447 // Note that these are really two operand instructions, but they're encoded
448 // as three operands with the first two arguments tied-to each other.
449
450 class IOHLInst<dag OOL, dag IOL, list<dag> pattern>:
451   RI16Form<0b100000110, OOL, IOL, "iohl\t$rT, $val",
452            ImmLoad, pattern>,
453   RegConstraint<"$rS = $rT">,
454   NoEncode<"$rS">;
455
456 class IOHLVecInst<ValueType vectype, Operand immtype /* , PatLeaf xform */>:
457   IOHLInst<(outs VECREG:$rT), (ins VECREG:$rS, immtype:$val),
458            [/* no pattern */]>;
459
460 class IOHLRegInst<RegisterClass rclass, Operand immtype /* , PatLeaf xform */>:
461   IOHLInst<(outs rclass:$rT), (ins rclass:$rS, immtype:$val),
462            [/* no pattern */]>;
463
464 multiclass ImmOrHalfwordLower
465 {
466   def v2i64: IOHLVecInst<v2i64, u16imm_i64>;
467   def v4i32: IOHLVecInst<v4i32, u16imm_i32>;
468
469   def r32: IOHLRegInst<R32C, i32imm>;
470   def f32: IOHLRegInst<R32FP, f32imm>;
471
472   def lo: IOHLRegInst<R32C, symbolLo>;
473 }
474
475 defm IOHL: ImmOrHalfwordLower;
476
477 // Form select mask for bytes using immediate, used in conjunction with the
478 // SELB instruction:
479
480 class FSMBIVec<ValueType vectype>:
481   RI16Form<0b101001100, (outs VECREG:$rT), (ins u16imm:$val),
482           "fsmbi\t$rT, $val",
483           SelectOp,
484           [(set (vectype VECREG:$rT), (SPUselmask (i16 immU16:$val)))]>;
485
486 multiclass FormSelectMaskBytesImm
487 {
488   def v16i8: FSMBIVec<v16i8>;
489   def v8i16: FSMBIVec<v8i16>;
490   def v4i32: FSMBIVec<v4i32>;
491   def v2i64: FSMBIVec<v2i64>;
492 }
493
494 defm FSMBI : FormSelectMaskBytesImm;
495
496 // fsmb: Form select mask for bytes. N.B. Input operand, $rA, is 16-bits
497 class FSMBInst<dag OOL, dag IOL, list<dag> pattern>:
498     RRForm_1<0b01101101100, OOL, IOL, "fsmb\t$rT, $rA", SelectOp,
499              pattern>;
500
501 class FSMBRegInst<RegisterClass rclass, ValueType vectype>:
502     FSMBInst<(outs VECREG:$rT), (ins rclass:$rA),
503              [(set (vectype VECREG:$rT), (SPUselmask rclass:$rA))]>;
504
505 class FSMBVecInst<ValueType vectype>:
506     FSMBInst<(outs VECREG:$rT), (ins VECREG:$rA),
507              [(set (vectype VECREG:$rT),
508                    (SPUselmask (vectype VECREG:$rA)))]>;
509
510 multiclass FormSelectMaskBits {
511   def v16i8_r16: FSMBRegInst<R16C, v16i8>;
512   def v16i8:     FSMBVecInst<v16i8>;
513 }
514
515 defm FSMB: FormSelectMaskBits;
516
517 // fsmh: Form select mask for halfwords. N.B., Input operand, $rA, is
518 // only 8-bits wide (even though it's input as 16-bits here)
519
520 class FSMHInst<dag OOL, dag IOL, list<dag> pattern>:
521     RRForm_1<0b10101101100, OOL, IOL, "fsmh\t$rT, $rA", SelectOp,
522              pattern>;
523
524 class FSMHRegInst<RegisterClass rclass, ValueType vectype>:
525     FSMHInst<(outs VECREG:$rT), (ins rclass:$rA),
526              [(set (vectype VECREG:$rT), (SPUselmask rclass:$rA))]>;
527
528 class FSMHVecInst<ValueType vectype>:
529     FSMHInst<(outs VECREG:$rT), (ins VECREG:$rA),
530              [(set (vectype VECREG:$rT),
531                    (SPUselmask (vectype VECREG:$rA)))]>;
532
533 multiclass FormSelectMaskHalfword {
534   def v8i16_r16: FSMHRegInst<R16C, v8i16>;
535   def v8i16:     FSMHVecInst<v8i16>;
536 }
537
538 defm FSMH: FormSelectMaskHalfword;
539
540 // fsm: Form select mask for words. Like the other fsm* instructions,
541 // only the lower 4 bits of $rA are significant.
542
543 class FSMInst<dag OOL, dag IOL, list<dag> pattern>:
544     RRForm_1<0b00101101100, OOL, IOL, "fsm\t$rT, $rA", SelectOp,
545              pattern>;
546
547 class FSMRegInst<ValueType vectype, RegisterClass rclass>:
548     FSMInst<(outs VECREG:$rT), (ins rclass:$rA),
549             [(set (vectype VECREG:$rT), (SPUselmask rclass:$rA))]>;
550
551 class FSMVecInst<ValueType vectype>:
552     FSMInst<(outs VECREG:$rT), (ins VECREG:$rA),
553             [(set (vectype VECREG:$rT), (SPUselmask (vectype VECREG:$rA)))]>;
554
555 multiclass FormSelectMaskWord {
556   def v4i32: FSMVecInst<v4i32>;
557
558   def r32 :  FSMRegInst<v4i32, R32C>;
559   def r16 :  FSMRegInst<v4i32, R16C>;
560 }
561
562 defm FSM : FormSelectMaskWord;
563
564 // Special case when used for i64 math operations
565 multiclass FormSelectMaskWord64 {
566   def r32 : FSMRegInst<v2i64, R32C>;
567   def r16 : FSMRegInst<v2i64, R16C>;
568 }
569
570 defm FSM64 : FormSelectMaskWord64;
571
572 //===----------------------------------------------------------------------===//
573 // Integer and Logical Operations:
574 //===----------------------------------------------------------------------===//
575
576 def AHv8i16:
577   RRForm<0b00010011000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
578     "ah\t$rT, $rA, $rB", IntegerOp,
579     [(set (v8i16 VECREG:$rT), (int_spu_si_ah VECREG:$rA, VECREG:$rB))]>;
580
581 def : Pat<(add (v8i16 VECREG:$rA), (v8i16 VECREG:$rB)),
582           (AHv8i16 VECREG:$rA, VECREG:$rB)>;
583
584 def AHr16:
585   RRForm<0b00010011000, (outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
586     "ah\t$rT, $rA, $rB", IntegerOp,
587     [(set R16C:$rT, (add R16C:$rA, R16C:$rB))]>;
588
589 def AHIvec:
590     RI10Form<0b10111000, (outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
591       "ahi\t$rT, $rA, $val", IntegerOp,
592       [(set (v8i16 VECREG:$rT), (add (v8i16 VECREG:$rA),
593                                      v8i16SExt10Imm:$val))]>;
594
595 def AHIr16:
596   RI10Form<0b10111000, (outs R16C:$rT), (ins R16C:$rA, s10imm:$val),
597     "ahi\t$rT, $rA, $val", IntegerOp,
598     [(set R16C:$rT, (add R16C:$rA, i16ImmSExt10:$val))]>;
599
600 // v4i32, i32 add instruction:
601
602 class AInst<dag OOL, dag IOL, list<dag> pattern>:
603   RRForm<0b00000011000, OOL, IOL,
604          "a\t$rT, $rA, $rB", IntegerOp,
605          pattern>;
606
607 class AVecInst<ValueType vectype>:
608   AInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
609         [(set (vectype VECREG:$rT), (add (vectype VECREG:$rA),
610                                          (vectype VECREG:$rB)))]>;
611
612 class ARegInst<RegisterClass rclass>:
613   AInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
614         [(set rclass:$rT, (add rclass:$rA, rclass:$rB))]>;
615         
616 multiclass AddInstruction {
617   def v4i32: AVecInst<v4i32>;
618   def v16i8: AVecInst<v16i8>;
619   
620   def r32:   ARegInst<R32C>;
621 }
622
623 defm A : AddInstruction;
624
625 class AIInst<dag OOL, dag IOL, list<dag> pattern>:
626     RI10Form<0b00111000, OOL, IOL,
627              "ai\t$rT, $rA, $val", IntegerOp,
628              pattern>;
629
630 class AIVecInst<ValueType vectype, PatLeaf immpred>:
631     AIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
632             [(set (vectype VECREG:$rT), (add (vectype VECREG:$rA), immpred:$val))]>;
633
634 class AIFPVecInst<ValueType vectype, PatLeaf immpred>:
635     AIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
636             [/* no pattern */]>;
637
638 class AIRegInst<RegisterClass rclass, PatLeaf immpred>:
639     AIInst<(outs rclass:$rT), (ins rclass:$rA, s10imm_i32:$val),
640            [(set rclass:$rT, (add rclass:$rA, immpred:$val))]>;
641
642 // This is used to add epsilons to floating point numbers in the f32 fdiv code:
643 class AIFPInst<RegisterClass rclass, PatLeaf immpred>:
644     AIInst<(outs rclass:$rT), (ins rclass:$rA, s10imm_i32:$val),
645            [/* no pattern */]>;
646
647 multiclass AddImmediate {
648   def v4i32: AIVecInst<v4i32, v4i32SExt10Imm>;
649
650   def r32: AIRegInst<R32C, i32ImmSExt10>;
651
652   def v4f32: AIFPVecInst<v4f32, v4i32SExt10Imm>;
653   def f32: AIFPInst<R32FP, i32ImmSExt10>;
654 }
655
656 defm AI : AddImmediate;
657
658 def SFHvec:
659     RRForm<0b00010010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
660       "sfh\t$rT, $rA, $rB", IntegerOp,
661       [(set (v8i16 VECREG:$rT), (sub (v8i16 VECREG:$rA),
662                                      (v8i16 VECREG:$rB)))]>;
663
664 def SFHr16:
665     RRForm<0b00010010000, (outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
666       "sfh\t$rT, $rA, $rB", IntegerOp,
667       [(set R16C:$rT, (sub R16C:$rA, R16C:$rB))]>;
668
669 def SFHIvec:
670     RI10Form<0b10110000, (outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
671       "sfhi\t$rT, $rA, $val", IntegerOp,
672       [(set (v8i16 VECREG:$rT), (sub v8i16SExt10Imm:$val,
673                                      (v8i16 VECREG:$rA)))]>;
674
675 def SFHIr16 : RI10Form<0b10110000, (outs R16C:$rT), (ins R16C:$rA, s10imm:$val),
676   "sfhi\t$rT, $rA, $val", IntegerOp,
677   [(set R16C:$rT, (sub i16ImmSExt10:$val, R16C:$rA))]>;
678
679 def SFvec : RRForm<0b00000010000, (outs VECREG:$rT),
680                                   (ins VECREG:$rA, VECREG:$rB),
681   "sf\t$rT, $rA, $rB", IntegerOp,
682   [(set (v4i32 VECREG:$rT), (sub (v4i32 VECREG:$rA), (v4i32 VECREG:$rB)))]>;
683
684 def SFr32 : RRForm<0b00000010000, (outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
685   "sf\t$rT, $rA, $rB", IntegerOp,
686   [(set R32C:$rT, (sub R32C:$rA, R32C:$rB))]>;
687
688 def SFIvec:
689     RI10Form<0b00110000, (outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
690       "sfi\t$rT, $rA, $val", IntegerOp,
691       [(set (v4i32 VECREG:$rT), (sub v4i32SExt10Imm:$val,
692                                      (v4i32 VECREG:$rA)))]>;
693
694 def SFIr32 : RI10Form<0b00110000, (outs R32C:$rT),
695                                   (ins R32C:$rA, s10imm_i32:$val),
696   "sfi\t$rT, $rA, $val", IntegerOp,
697   [(set R32C:$rT, (sub i32ImmSExt10:$val, R32C:$rA))]>;
698
699 // ADDX: only available in vector form, doesn't match a pattern.
700 class ADDXInst<dag OOL, dag IOL, list<dag> pattern>:
701     RRForm<0b00000010110, OOL, IOL,
702       "addx\t$rT, $rA, $rB",
703       IntegerOp, pattern>;
704
705 class ADDXVecInst<ValueType vectype>:
706     ADDXInst<(outs VECREG:$rT),
707              (ins VECREG:$rA, VECREG:$rB, VECREG:$rCarry),
708              [/* no pattern */]>,
709     RegConstraint<"$rCarry = $rT">,
710     NoEncode<"$rCarry">;
711
712 class ADDXRegInst<RegisterClass rclass>:
713     ADDXInst<(outs rclass:$rT),
714              (ins rclass:$rA, rclass:$rB, rclass:$rCarry),
715              [/* no pattern */]>,
716     RegConstraint<"$rCarry = $rT">,
717     NoEncode<"$rCarry">;
718
719 multiclass AddExtended {
720   def v2i64 : ADDXVecInst<v2i64>;
721   def v4i32 : ADDXVecInst<v4i32>;
722   def r64 : ADDXRegInst<R64C>;
723   def r32 : ADDXRegInst<R32C>;
724 }
725
726 defm ADDX : AddExtended;
727
728 // CG: Generate carry for add
729 class CGInst<dag OOL, dag IOL, list<dag> pattern>:
730     RRForm<0b01000011000, OOL, IOL,
731       "cg\t$rT, $rA, $rB",
732       IntegerOp, pattern>;
733
734 class CGVecInst<ValueType vectype>:
735     CGInst<(outs VECREG:$rT),
736            (ins VECREG:$rA, VECREG:$rB),
737            [/* no pattern */]>;
738
739 class CGRegInst<RegisterClass rclass>:
740     CGInst<(outs rclass:$rT),
741            (ins rclass:$rA, rclass:$rB),
742            [/* no pattern */]>;
743
744 multiclass CarryGenerate {
745   def v2i64 : CGVecInst<v2i64>;
746   def v4i32 : CGVecInst<v4i32>;
747   def r64 : CGRegInst<R64C>;
748   def r32 : CGRegInst<R32C>;
749 }
750
751 defm CG : CarryGenerate;
752
753 // SFX: Subract from, extended. This is used in conjunction with BG to subtract
754 // with carry (borrow, in this case)
755 class SFXInst<dag OOL, dag IOL, list<dag> pattern>:
756     RRForm<0b10000010110, OOL, IOL,
757       "sfx\t$rT, $rA, $rB",
758       IntegerOp, pattern>;
759
760 class SFXVecInst<ValueType vectype>:
761     SFXInst<(outs VECREG:$rT),
762             (ins VECREG:$rA, VECREG:$rB, VECREG:$rCarry),
763              [/* no pattern */]>,
764     RegConstraint<"$rCarry = $rT">,
765     NoEncode<"$rCarry">;
766
767 class SFXRegInst<RegisterClass rclass>:
768     SFXInst<(outs rclass:$rT),
769             (ins rclass:$rA, rclass:$rB, rclass:$rCarry),
770              [/* no pattern */]>,
771     RegConstraint<"$rCarry = $rT">,
772     NoEncode<"$rCarry">;
773
774 multiclass SubtractExtended {
775   def v2i64 : SFXVecInst<v2i64>;
776   def v4i32 : SFXVecInst<v4i32>;
777   def r64 : SFXRegInst<R64C>;
778   def r32 : SFXRegInst<R32C>;
779 }
780
781 defm SFX : SubtractExtended;
782
783 // BG: only available in vector form, doesn't match a pattern.
784 class BGInst<dag OOL, dag IOL, list<dag> pattern>:
785     RRForm<0b01000010000, OOL, IOL,
786       "bg\t$rT, $rA, $rB",
787       IntegerOp, pattern>;
788
789 class BGVecInst<ValueType vectype>:
790     BGInst<(outs VECREG:$rT),
791            (ins VECREG:$rA, VECREG:$rB),
792            [/* no pattern */]>;
793
794 class BGRegInst<RegisterClass rclass>:
795     BGInst<(outs rclass:$rT),
796            (ins rclass:$rA, rclass:$rB),
797            [/* no pattern */]>;
798
799 multiclass BorrowGenerate {
800   def v4i32 : BGVecInst<v4i32>;
801   def v2i64 : BGVecInst<v2i64>;
802   def r64 : BGRegInst<R64C>;
803   def r32 : BGRegInst<R32C>;
804 }
805
806 defm BG : BorrowGenerate;
807
808 // BGX: Borrow generate, extended.
809 def BGXvec:
810     RRForm<0b11000010110, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB,
811                                 VECREG:$rCarry),
812       "bgx\t$rT, $rA, $rB", IntegerOp,
813       []>,
814     RegConstraint<"$rCarry = $rT">,
815     NoEncode<"$rCarry">;
816
817 // Halfword multiply variants:
818 // N.B: These can be used to build up larger quantities (16x16 -> 32)
819
820 def MPYv8i16:
821   RRForm<0b00100011110, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
822     "mpy\t$rT, $rA, $rB", IntegerMulDiv,
823     [/* no pattern */]>;
824
825 def MPYr16:
826   RRForm<0b00100011110, (outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
827     "mpy\t$rT, $rA, $rB", IntegerMulDiv,
828     [(set R16C:$rT, (mul R16C:$rA, R16C:$rB))]>;
829
830 // Unsigned 16-bit multiply:
831
832 class MPYUInst<dag OOL, dag IOL, list<dag> pattern>:
833     RRForm<0b00110011110, OOL, IOL,
834       "mpyu\t$rT, $rA, $rB", IntegerMulDiv,
835       pattern>;
836
837 def MPYUv4i32:
838   MPYUInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
839            [/* no pattern */]>;
840
841 def MPYUr16:
842   MPYUInst<(outs R32C:$rT), (ins R16C:$rA, R16C:$rB),
843            [(set R32C:$rT, (mul (zext R16C:$rA), (zext R16C:$rB)))]>;
844
845 def MPYUr32:
846   MPYUInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
847            [/* no pattern */]>;
848
849 // mpyi: multiply 16 x s10imm -> 32 result.
850
851 class MPYIInst<dag OOL, dag IOL, list<dag> pattern>:
852   RI10Form<0b00101110, OOL, IOL,
853     "mpyi\t$rT, $rA, $val", IntegerMulDiv,
854     pattern>;
855
856 def MPYIvec:
857   MPYIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
858            [(set (v8i16 VECREG:$rT),
859                  (mul (v8i16 VECREG:$rA), v8i16SExt10Imm:$val))]>;
860
861 def MPYIr16:
862   MPYIInst<(outs R16C:$rT), (ins R16C:$rA, s10imm:$val),
863            [(set R16C:$rT, (mul R16C:$rA, i16ImmSExt10:$val))]>;
864
865 // mpyui: same issues as other multiplies, plus, this doesn't match a
866 // pattern... but may be used during target DAG selection or lowering
867
868 class MPYUIInst<dag OOL, dag IOL, list<dag> pattern>:
869   RI10Form<0b10101110, OOL, IOL,
870            "mpyui\t$rT, $rA, $val", IntegerMulDiv,
871            pattern>;
872     
873 def MPYUIvec:
874   MPYUIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
875             []>;
876
877 def MPYUIr16:
878   MPYUIInst<(outs R16C:$rT), (ins R16C:$rA, s10imm:$val),
879             []>;
880
881 // mpya: 16 x 16 + 16 -> 32 bit result
882 class MPYAInst<dag OOL, dag IOL, list<dag> pattern>:
883   RRRForm<0b0011, OOL, IOL,
884           "mpya\t$rT, $rA, $rB, $rC", IntegerMulDiv,
885           pattern>;
886           
887 def MPYAv4i32:
888   MPYAInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
889            [(set (v4i32 VECREG:$rT),
890                  (add (v4i32 (bitconvert (mul (v8i16 VECREG:$rA),
891                                               (v8i16 VECREG:$rB)))),
892                       (v4i32 VECREG:$rC)))]>;
893
894 def MPYAr32:
895   MPYAInst<(outs R32C:$rT), (ins R16C:$rA, R16C:$rB, R32C:$rC),
896            [(set R32C:$rT, (add (sext (mul R16C:$rA, R16C:$rB)),
897                                 R32C:$rC))]>;
898                                 
899 def MPYAr32_sext:
900   MPYAInst<(outs R32C:$rT), (ins R16C:$rA, R16C:$rB, R32C:$rC),
901            [(set R32C:$rT, (add (mul (sext R16C:$rA), (sext R16C:$rB)),
902                                 R32C:$rC))]>;
903
904 def MPYAr32_sextinreg:
905   MPYAInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB, R32C:$rC),
906            [(set R32C:$rT, (add (mul (sext_inreg R32C:$rA, i16),
907                                      (sext_inreg R32C:$rB, i16)),
908                                 R32C:$rC))]>;
909
910 // mpyh: multiply high, used to synthesize 32-bit multiplies
911 class MPYHInst<dag OOL, dag IOL, list<dag> pattern>:
912   RRForm<0b10100011110, OOL, IOL,
913          "mpyh\t$rT, $rA, $rB", IntegerMulDiv,
914          pattern>;
915          
916 def MPYHv4i32:
917     MPYHInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
918              [/* no pattern */]>;
919
920 def MPYHr32:
921     MPYHInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
922              [/* no pattern */]>;
923
924 // mpys: multiply high and shift right (returns the top half of
925 // a 16-bit multiply, sign extended to 32 bits.)
926
927 class MPYSInst<dag OOL, dag IOL>:
928     RRForm<0b11100011110, OOL, IOL, 
929       "mpys\t$rT, $rA, $rB", IntegerMulDiv,
930       [/* no pattern */]>;
931
932 def MPYSv4i32:
933     MPYSInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB)>;
934     
935 def MPYSr16:
936     MPYSInst<(outs R32C:$rT), (ins R16C:$rA, R16C:$rB)>;
937
938 // mpyhh: multiply high-high (returns the 32-bit result from multiplying
939 // the top 16 bits of the $rA, $rB)
940
941 class MPYHHInst<dag OOL, dag IOL>:
942   RRForm<0b01100011110, OOL, IOL,
943         "mpyhh\t$rT, $rA, $rB", IntegerMulDiv,
944         [/* no pattern */]>;
945         
946 def MPYHHv8i16:
947     MPYHHInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB)>;
948
949 def MPYHHr32:
950     MPYHHInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB)>;
951
952 // mpyhha: Multiply high-high, add to $rT:
953
954 class MPYHHAInst<dag OOL, dag IOL>:
955     RRForm<0b01100010110, OOL, IOL,
956       "mpyhha\t$rT, $rA, $rB", IntegerMulDiv,
957       [/* no pattern */]>;
958
959 def MPYHHAvec:
960     MPYHHAInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB)>;
961     
962 def MPYHHAr32:
963     MPYHHAInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB)>;
964
965 // mpyhhu: Multiply high-high, unsigned, e.g.:
966 //
967 // +-------+-------+   +-------+-------+   +---------+
968 // |  a0   .  a1   | x |  b0   .  b1   | = | a0 x b0 |
969 // +-------+-------+   +-------+-------+   +---------+
970 //
971 // where a0, b0 are the upper 16 bits of the 32-bit word
972
973 class MPYHHUInst<dag OOL, dag IOL>:
974     RRForm<0b01110011110, OOL, IOL,
975       "mpyhhu\t$rT, $rA, $rB", IntegerMulDiv,
976       [/* no pattern */]>;
977
978 def MPYHHUv4i32:
979     MPYHHUInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB)>;
980     
981 def MPYHHUr32:
982     MPYHHUInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB)>;
983
984 // mpyhhau: Multiply high-high, unsigned
985
986 class MPYHHAUInst<dag OOL, dag IOL>:
987     RRForm<0b01110010110, OOL, IOL,
988       "mpyhhau\t$rT, $rA, $rB", IntegerMulDiv,
989       [/* no pattern */]>;
990
991 def MPYHHAUvec:
992     MPYHHAUInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB)>;
993     
994 def MPYHHAUr32:
995     MPYHHAUInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB)>;
996
997 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
998 // clz: Count leading zeroes
999 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
1000 class CLZInst<dag OOL, dag IOL, list<dag> pattern>:
1001     RRForm_1<0b10100101010, OOL, IOL, "clz\t$rT, $rA",
1002              IntegerOp, pattern>;
1003
1004 class CLZRegInst<RegisterClass rclass>:
1005     CLZInst<(outs rclass:$rT), (ins rclass:$rA),
1006             [(set rclass:$rT, (ctlz rclass:$rA))]>;
1007
1008 class CLZVecInst<ValueType vectype>:
1009     CLZInst<(outs VECREG:$rT), (ins VECREG:$rA),
1010             [(set (vectype VECREG:$rT), (ctlz (vectype VECREG:$rA)))]>;
1011
1012 multiclass CountLeadingZeroes {
1013   def v4i32 : CLZVecInst<v4i32>;
1014   def r32   : CLZRegInst<R32C>;
1015 }
1016
1017 defm CLZ : CountLeadingZeroes;
1018
1019 // cntb: Count ones in bytes (aka "population count")
1020 //
1021 // NOTE: This instruction is really a vector instruction, but the custom
1022 // lowering code uses it in unorthodox ways to support CTPOP for other
1023 // data types!
1024
1025 def CNTBv16i8:
1026     RRForm_1<0b00101101010, (outs VECREG:$rT), (ins VECREG:$rA),
1027       "cntb\t$rT, $rA", IntegerOp,
1028       [(set (v16i8 VECREG:$rT), (SPUcntb (v16i8 VECREG:$rA)))]>;
1029
1030 def CNTBv8i16 :
1031     RRForm_1<0b00101101010, (outs VECREG:$rT), (ins VECREG:$rA),
1032       "cntb\t$rT, $rA", IntegerOp,
1033       [(set (v8i16 VECREG:$rT), (SPUcntb (v8i16 VECREG:$rA)))]>;
1034
1035 def CNTBv4i32 :
1036     RRForm_1<0b00101101010, (outs VECREG:$rT), (ins VECREG:$rA),
1037       "cntb\t$rT, $rA", IntegerOp,
1038       [(set (v4i32 VECREG:$rT), (SPUcntb (v4i32 VECREG:$rA)))]>;
1039
1040 // gbb: Gather the low order bits from each byte in $rA into a single 16-bit
1041 // quantity stored into $rT's slot 0, upper 16 bits are zeroed, as are
1042 // slots 1-3.
1043 //
1044 // Note: This instruction "pairs" with the fsmb instruction for all of the
1045 // various types defined here.
1046 //
1047 // Note 2: The "VecInst" and "RegInst" forms refer to the result being either
1048 // a vector or register.
1049
1050 class GBBInst<dag OOL, dag IOL, list<dag> pattern>:
1051   RRForm_1<0b01001101100, OOL, IOL, "gbb\t$rT, $rA", GatherOp, pattern>;
1052
1053 class GBBRegInst<RegisterClass rclass, ValueType vectype>:
1054   GBBInst<(outs rclass:$rT), (ins VECREG:$rA),
1055           [/* no pattern */]>;
1056
1057 class GBBVecInst<ValueType vectype>:
1058   GBBInst<(outs VECREG:$rT), (ins VECREG:$rA),
1059           [/* no pattern */]>;
1060
1061 multiclass GatherBitsFromBytes {
1062   def v16i8_r32: GBBRegInst<R32C, v16i8>;
1063   def v16i8_r16: GBBRegInst<R16C, v16i8>;
1064   def v16i8:     GBBVecInst<v16i8>;
1065 }
1066
1067 defm GBB: GatherBitsFromBytes;
1068
1069 // gbh: Gather all low order bits from each halfword in $rA into a single
1070 // 8-bit quantity stored in $rT's slot 0, with the upper bits of $rT set to 0
1071 // and slots 1-3 also set to 0.
1072 //
1073 // See notes for GBBInst, above.
1074
1075 class GBHInst<dag OOL, dag IOL, list<dag> pattern>:
1076     RRForm_1<0b10001101100, OOL, IOL, "gbh\t$rT, $rA", GatherOp,
1077              pattern>;
1078
1079 class GBHRegInst<RegisterClass rclass, ValueType vectype>:
1080     GBHInst<(outs rclass:$rT), (ins VECREG:$rA),
1081             [/* no pattern */]>;
1082
1083 class GBHVecInst<ValueType vectype>:
1084     GBHInst<(outs VECREG:$rT), (ins VECREG:$rA),
1085             [/* no pattern */]>;
1086
1087 multiclass GatherBitsHalfword {
1088   def v8i16_r32: GBHRegInst<R32C, v8i16>;
1089   def v8i16_r16: GBHRegInst<R16C, v8i16>;
1090   def v8i16:     GBHVecInst<v8i16>;
1091 }
1092
1093 defm GBH: GatherBitsHalfword;
1094
1095 // gb: Gather all low order bits from each word in $rA into a single
1096 // 4-bit quantity stored in $rT's slot 0, upper bits in $rT set to 0,
1097 // as well as slots 1-3.
1098 //
1099 // See notes for gbb, above.
1100
1101 class GBInst<dag OOL, dag IOL, list<dag> pattern>:
1102     RRForm_1<0b00001101100, OOL, IOL, "gb\t$rT, $rA", GatherOp,
1103              pattern>;
1104
1105 class GBRegInst<RegisterClass rclass, ValueType vectype>:
1106     GBInst<(outs rclass:$rT), (ins VECREG:$rA),
1107            [/* no pattern */]>;
1108
1109 class GBVecInst<ValueType vectype>:
1110     GBInst<(outs VECREG:$rT), (ins VECREG:$rA),
1111            [/* no pattern */]>;
1112
1113 multiclass GatherBitsWord {
1114   def v4i32_r32: GBRegInst<R32C, v4i32>;
1115   def v4i32_r16: GBRegInst<R16C, v4i32>;
1116   def v4i32:     GBVecInst<v4i32>;
1117 }
1118
1119 defm GB: GatherBitsWord;
1120
1121 // avgb: average bytes
1122 def AVGB:
1123     RRForm<0b11001011000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1124       "avgb\t$rT, $rA, $rB", ByteOp,
1125       []>;
1126
1127 // absdb: absolute difference of bytes
1128 def ABSDB:
1129     RRForm<0b11001010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1130       "absdb\t$rT, $rA, $rB", ByteOp,
1131       []>;
1132
1133 // sumb: sum bytes into halfwords
1134 def SUMB:
1135     RRForm<0b11001010010, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1136       "sumb\t$rT, $rA, $rB", ByteOp,
1137       []>;
1138
1139 // Sign extension operations:
1140 class XSBHInst<dag OOL, dag IOL, list<dag> pattern>:
1141     RRForm_1<0b01101101010, OOL, IOL,
1142       "xsbh\t$rDst, $rSrc",
1143       IntegerOp, pattern>;
1144
1145 class XSBHVecInst<ValueType vectype>:
1146     XSBHInst<(outs VECREG:$rDst), (ins VECREG:$rSrc),
1147       [(set (v8i16 VECREG:$rDst), (sext (vectype VECREG:$rSrc)))]>;
1148
1149 class XSBHInRegInst<RegisterClass rclass, list<dag> pattern>:
1150     XSBHInst<(outs rclass:$rDst), (ins rclass:$rSrc),
1151              pattern>;
1152
1153 multiclass ExtendByteHalfword {
1154   def v16i8:     XSBHVecInst<v8i16>;
1155   def r8:        XSBHInst<(outs R16C:$rDst), (ins R8C:$rSrc),
1156                           [(set R16C:$rDst, (sext R8C:$rSrc))]>;
1157   def r16:       XSBHInRegInst<R16C,
1158                                [(set R16C:$rDst, (sext_inreg R16C:$rSrc, i8))]>;
1159
1160   // 32-bit form for XSBH: used to sign extend 8-bit quantities to 16-bit
1161   // quantities to 32-bit quantities via a 32-bit register (see the sext 8->32
1162   // pattern below). Intentionally doesn't match a pattern because we want the
1163   // sext 8->32 pattern to do the work for us, namely because we need the extra
1164   // XSHWr32.
1165   def r32:   XSBHInRegInst<R32C, [/* no pattern */]>;
1166   
1167   // Same as the 32-bit version, but for i64
1168   def r64:   XSBHInRegInst<R64C, [/* no pattern */]>;
1169 }
1170
1171 defm XSBH : ExtendByteHalfword;
1172
1173 // Sign extend halfwords to words:
1174
1175 class XSHWInst<dag OOL, dag IOL, list<dag> pattern>:
1176     RRForm_1<0b01101101010, OOL, IOL, "xshw\t$rDest, $rSrc",
1177             IntegerOp, pattern>;
1178
1179 class XSHWVecInst<ValueType in_vectype, ValueType out_vectype>:
1180     XSHWInst<(outs VECREG:$rDest), (ins VECREG:$rSrc),
1181              [(set (out_vectype VECREG:$rDest),
1182                    (sext (in_vectype VECREG:$rSrc)))]>;
1183
1184 class XSHWInRegInst<RegisterClass rclass, list<dag> pattern>:
1185     XSHWInst<(outs rclass:$rDest), (ins rclass:$rSrc),
1186              pattern>;
1187              
1188 class XSHWRegInst<RegisterClass rclass>:
1189     XSHWInst<(outs rclass:$rDest), (ins R16C:$rSrc),
1190              [(set rclass:$rDest, (sext R16C:$rSrc))]>;
1191
1192 multiclass ExtendHalfwordWord {
1193   def v4i32: XSHWVecInst<v4i32, v8i16>;
1194   
1195   def r16:   XSHWRegInst<R32C>;
1196   
1197   def r32:   XSHWInRegInst<R32C,
1198                           [(set R32C:$rDest, (sext_inreg R32C:$rSrc, i16))]>;
1199   def r64:   XSHWInRegInst<R64C, [/* no pattern */]>;
1200 }
1201
1202 defm XSHW : ExtendHalfwordWord;
1203
1204 // Sign-extend words to doublewords (32->64 bits)
1205
1206 class XSWDInst<dag OOL, dag IOL, list<dag> pattern>:
1207     RRForm_1<0b01100101010, OOL, IOL, "xswd\t$rDst, $rSrc",
1208               IntegerOp, pattern>;
1209       
1210 class XSWDVecInst<ValueType in_vectype, ValueType out_vectype>:
1211     XSWDInst<(outs VECREG:$rDst), (ins VECREG:$rSrc),
1212              [(set (out_vectype VECREG:$rDst),
1213                    (sext (out_vectype VECREG:$rSrc)))]>;
1214       
1215 class XSWDRegInst<RegisterClass in_rclass, RegisterClass out_rclass>:
1216     XSWDInst<(outs out_rclass:$rDst), (ins in_rclass:$rSrc),
1217              [(set out_rclass:$rDst, (sext in_rclass:$rSrc))]>;
1218              
1219 multiclass ExtendWordToDoubleWord {
1220   def v2i64: XSWDVecInst<v4i32, v2i64>;
1221   def r64:   XSWDRegInst<R32C, R64C>;
1222   
1223   def r64_inreg: XSWDInst<(outs R64C:$rDst), (ins R64C:$rSrc),
1224                           [(set R64C:$rDst, (sext_inreg R64C:$rSrc, i32))]>;
1225 }
1226
1227 defm XSWD : ExtendWordToDoubleWord;
1228
1229 // AND operations
1230
1231 class ANDInst<dag OOL, dag IOL, list<dag> pattern> :
1232     RRForm<0b10000011000, OOL, IOL, "and\t$rT, $rA, $rB",
1233            IntegerOp, pattern>;
1234
1235 class ANDVecInst<ValueType vectype>:
1236     ANDInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1237              [(set (vectype VECREG:$rT), (and (vectype VECREG:$rA),
1238                                               (vectype VECREG:$rB)))]>;
1239
1240 class ANDRegInst<RegisterClass rclass>:
1241     ANDInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
1242              [(set rclass:$rT, (and rclass:$rA, rclass:$rB))]>;
1243
1244 multiclass BitwiseAnd
1245 {
1246   def v16i8: ANDVecInst<v16i8>;
1247   def v8i16: ANDVecInst<v8i16>;
1248   def v4i32: ANDVecInst<v4i32>;
1249   def v2i64: ANDVecInst<v2i64>;
1250
1251   def r128:  ANDRegInst<GPRC>;
1252   def r64:   ANDRegInst<R64C>;
1253   def r32:   ANDRegInst<R32C>;
1254   def r16:   ANDRegInst<R16C>;
1255   def r8:    ANDRegInst<R8C>;
1256
1257   //===---------------------------------------------
1258   // Special instructions to perform the fabs instruction
1259   def fabs32: ANDInst<(outs R32FP:$rT), (ins R32FP:$rA, R32C:$rB),
1260                       [/* Intentionally does not match a pattern */]>;
1261
1262   // Could use v4i32, but won't for clarity
1263   def fabsvec: ANDInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1264                        [/* Intentionally does not match a pattern */]>;
1265
1266   //===---------------------------------------------
1267
1268   // Hacked form of AND to zero-extend 16-bit quantities to 32-bit
1269   // quantities -- see 16->32 zext pattern.
1270   //
1271   // This pattern is somewhat artificial, since it might match some
1272   // compiler generated pattern but it is unlikely to do so.
1273
1274   def i16i32: ANDInst<(outs R32C:$rT), (ins R16C:$rA, R32C:$rB),
1275                       [(set R32C:$rT, (and (zext R16C:$rA), R32C:$rB))]>;
1276 }
1277
1278 defm AND : BitwiseAnd;
1279
1280 // N.B.: vnot_conv is one of those special target selection pattern fragments,
1281 // in which we expect there to be a bit_convert on the constant. Bear in mind
1282 // that llvm translates "not <reg>" to "xor <reg>, -1" (or in this case, a
1283 // constant -1 vector.)
1284
1285 class ANDCInst<dag OOL, dag IOL, list<dag> pattern>:
1286     RRForm<0b10000011010, OOL, IOL, "andc\t$rT, $rA, $rB",
1287            IntegerOp, pattern>;
1288
1289 class ANDCVecInst<ValueType vectype>:
1290     ANDCInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1291              [(set (vectype VECREG:$rT), (and (vectype VECREG:$rA),
1292                                               (vnot (vectype VECREG:$rB))))]>;
1293
1294 class ANDCRegInst<RegisterClass rclass>:
1295     ANDCInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
1296              [(set rclass:$rT, (and rclass:$rA, (not rclass:$rB)))]>;
1297
1298 multiclass AndComplement
1299 {
1300   def v16i8: ANDCVecInst<v16i8>;
1301   def v8i16: ANDCVecInst<v8i16>;
1302   def v4i32: ANDCVecInst<v4i32>;
1303   def v2i64: ANDCVecInst<v2i64>;
1304
1305   def r128: ANDCRegInst<GPRC>;
1306   def r64:  ANDCRegInst<R64C>;
1307   def r32:  ANDCRegInst<R32C>;
1308   def r16:  ANDCRegInst<R16C>;
1309   def r8:   ANDCRegInst<R8C>;
1310 }
1311
1312 defm ANDC : AndComplement;
1313
1314 class ANDBIInst<dag OOL, dag IOL, list<dag> pattern>:
1315     RI10Form<0b01101000, OOL, IOL, "andbi\t$rT, $rA, $val",
1316              ByteOp, pattern>;
1317
1318 multiclass AndByteImm
1319 {
1320   def v16i8: ANDBIInst<(outs VECREG:$rT), (ins VECREG:$rA, u10imm:$val),
1321                        [(set (v16i8 VECREG:$rT),
1322                              (and (v16i8 VECREG:$rA),
1323                                   (v16i8 v16i8U8Imm:$val)))]>;
1324
1325   def r8: ANDBIInst<(outs R8C:$rT), (ins R8C:$rA, u10imm_i8:$val),
1326                     [(set R8C:$rT, (and R8C:$rA, immU8:$val))]>;
1327 }
1328
1329 defm ANDBI : AndByteImm;
1330
1331 class ANDHIInst<dag OOL, dag IOL, list<dag> pattern> :
1332     RI10Form<0b10101000, OOL, IOL, "andhi\t$rT, $rA, $val",
1333              ByteOp, pattern>;
1334
1335 multiclass AndHalfwordImm
1336 {
1337   def v8i16: ANDHIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
1338                        [(set (v8i16 VECREG:$rT),
1339                              (and (v8i16 VECREG:$rA), v8i16SExt10Imm:$val))]>;
1340
1341   def r16: ANDHIInst<(outs R16C:$rT), (ins R16C:$rA, u10imm:$val),
1342                      [(set R16C:$rT, (and R16C:$rA, i16ImmUns10:$val))]>;
1343
1344   // Zero-extend i8 to i16:
1345   def i8i16: ANDHIInst<(outs R16C:$rT), (ins R8C:$rA, u10imm:$val),
1346                       [(set R16C:$rT, (and (zext R8C:$rA), i16ImmUns10:$val))]>;
1347 }
1348
1349 defm ANDHI : AndHalfwordImm;
1350
1351 class ANDIInst<dag OOL, dag IOL, list<dag> pattern> :
1352     RI10Form<0b00101000, OOL, IOL, "andi\t$rT, $rA, $val",
1353              IntegerOp, pattern>;
1354
1355 multiclass AndWordImm
1356 {
1357   def v4i32: ANDIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
1358                       [(set (v4i32 VECREG:$rT),
1359                             (and (v4i32 VECREG:$rA), v4i32SExt10Imm:$val))]>;
1360
1361   def r32: ANDIInst<(outs R32C:$rT), (ins R32C:$rA, s10imm_i32:$val),
1362                     [(set R32C:$rT, (and R32C:$rA, i32ImmSExt10:$val))]>;
1363
1364   // Hacked form of ANDI to zero-extend i8 quantities to i32. See the zext 8->32
1365   // pattern below.
1366   def i8i32: ANDIInst<(outs R32C:$rT), (ins R8C:$rA, s10imm_i32:$val),
1367                       [(set R32C:$rT,
1368                             (and (zext R8C:$rA), i32ImmSExt10:$val))]>;
1369
1370   // Hacked form of ANDI to zero-extend i16 quantities to i32. See the
1371   // zext 16->32 pattern below.
1372   //
1373   // Note that this pattern is somewhat artificial, since it might match
1374   // something the compiler generates but is unlikely to occur in practice.
1375   def i16i32: ANDIInst<(outs R32C:$rT), (ins R16C:$rA, s10imm_i32:$val),
1376                        [(set R32C:$rT,
1377                              (and (zext R16C:$rA), i32ImmSExt10:$val))]>;
1378 }
1379
1380 defm ANDI : AndWordImm;
1381
1382 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
1383 // Bitwise OR group:
1384 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
1385
1386 // Bitwise "or" (N.B.: These are also register-register copy instructions...)
1387 class ORInst<dag OOL, dag IOL, list<dag> pattern>:
1388     RRForm<0b10000010000, OOL, IOL, "or\t$rT, $rA, $rB",
1389            IntegerOp, pattern>;
1390
1391 class ORVecInst<ValueType vectype>:
1392     ORInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1393            [(set (vectype VECREG:$rT), (or (vectype VECREG:$rA),
1394                                            (vectype VECREG:$rB)))]>;
1395
1396 class ORRegInst<RegisterClass rclass>:
1397     ORInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
1398            [(set rclass:$rT, (or rclass:$rA, rclass:$rB))]>;
1399
1400 // ORCvtForm: OR conversion form
1401 //
1402 // This is used to "convert" the preferred slot to its vector equivalent, as
1403 // well as convert a vector back to its preferred slot.
1404 //
1405 // These are effectively no-ops, but need to exist for proper type conversion
1406 // and type coercion.
1407
1408 class ORCvtForm<dag OOL, dag IOL, list<dag> pattern = [/* no pattern */]>
1409           : SPUInstr<OOL, IOL, "or\t$rT, $rA, $rA", IntegerOp> {
1410   bits<7> RA;
1411   bits<7> RT;
1412
1413   let Pattern = pattern;
1414
1415   let Inst{0-10} = 0b10000010000;
1416   let Inst{11-17} = RA;
1417   let Inst{18-24} = RA;
1418   let Inst{25-31} = RT;
1419 }
1420
1421 class ORPromoteScalar<RegisterClass rclass>:
1422     ORCvtForm<(outs VECREG:$rT), (ins rclass:$rA)>;
1423
1424 class ORExtractElt<RegisterClass rclass>:
1425     ORCvtForm<(outs rclass:$rT), (ins VECREG:$rA)>;
1426
1427 /* class ORCvtRegGPRC<RegisterClass rclass>:
1428     ORCvtForm<(outs GPRC:$rT), (ins rclass:$rA)>; */
1429
1430 /* class ORCvtVecGPRC:
1431     ORCvtForm<(outs GPRC:$rT), (ins VECREG:$rA)>; */
1432
1433 /* class ORCvtGPRCReg<RegisterClass rclass>:
1434     ORCvtForm<(outs rclass:$rT), (ins GPRC:$rA)>; */
1435     
1436 class ORCvtFormR32Reg<RegisterClass rclass, list<dag> pattern = [ ]>:
1437     ORCvtForm<(outs rclass:$rT), (ins R32C:$rA), pattern>;
1438     
1439 class ORCvtFormRegR32<RegisterClass rclass, list<dag> pattern = [ ]>:
1440     ORCvtForm<(outs R32C:$rT), (ins rclass:$rA), pattern>;
1441
1442 class ORCvtFormR64Reg<RegisterClass rclass, list<dag> pattern = [ ]>:
1443     ORCvtForm<(outs rclass:$rT), (ins R64C:$rA), pattern>;
1444     
1445 class ORCvtFormRegR64<RegisterClass rclass, list<dag> pattern = [ ]>:
1446     ORCvtForm<(outs R64C:$rT), (ins rclass:$rA), pattern>;
1447
1448 /* class ORCvtGPRCVec:
1449     ORCvtForm<(outs VECREG:$rT), (ins GPRC:$rA)>; */
1450
1451 multiclass BitwiseOr
1452 {
1453   def v16i8: ORVecInst<v16i8>;
1454   def v8i16: ORVecInst<v8i16>;
1455   def v4i32: ORVecInst<v4i32>;
1456   def v2i64: ORVecInst<v2i64>;
1457
1458   def v4f32: ORInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1459                     [(set (v4f32 VECREG:$rT),
1460                           (v4f32 (bitconvert (or (v4i32 VECREG:$rA),
1461                                                  (v4i32 VECREG:$rB)))))]>;
1462
1463   def v2f64: ORInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1464                     [(set (v2f64 VECREG:$rT),
1465                           (v2f64 (bitconvert (or (v2i64 VECREG:$rA),
1466                                                  (v2i64 VECREG:$rB)))))]>;
1467
1468   def r128: ORRegInst<GPRC>;
1469   def r64:  ORRegInst<R64C>;
1470   def r32:  ORRegInst<R32C>;
1471   def r16:  ORRegInst<R16C>;
1472   def r8:   ORRegInst<R8C>;
1473
1474   // OR instructions used to copy f32 and f64 registers.
1475   def f32: ORInst<(outs R32FP:$rT), (ins R32FP:$rA, R32FP:$rB),
1476                   [/* no pattern */]>;
1477
1478   def f64: ORInst<(outs R64FP:$rT), (ins R64FP:$rA, R64FP:$rB),
1479                   [/* no pattern */]>;
1480
1481   // scalar->vector promotion, prefslot2vec:
1482   def v16i8_i8:  ORPromoteScalar<R8C>;
1483   def v8i16_i16: ORPromoteScalar<R16C>;
1484   def v4i32_i32: ORPromoteScalar<R32C>;
1485   def v2i64_i64: ORPromoteScalar<R64C>;
1486   def v4f32_f32: ORPromoteScalar<R32FP>;
1487   def v2f64_f64: ORPromoteScalar<R64FP>;
1488
1489   // vector->scalar demotion, vec2prefslot:
1490   def i8_v16i8:  ORExtractElt<R8C>;
1491   def i16_v8i16: ORExtractElt<R16C>;
1492   def i32_v4i32: ORExtractElt<R32C>;
1493   def i64_v2i64: ORExtractElt<R64C>;
1494   def f32_v4f32: ORExtractElt<R32FP>;
1495   def f64_v2f64: ORExtractElt<R64FP>;
1496
1497 /*
1498   // Conversion from GPRC to register
1499   def i128_r64:  ORCvtRegGPRC<R64C>;
1500   def i128_f64:  ORCvtRegGPRC<R64FP>;
1501   def i128_r32:  ORCvtRegGPRC<R32C>;
1502   def i128_f32:  ORCvtRegGPRC<R32FP>;
1503   def i128_r16:  ORCvtRegGPRC<R16C>;
1504   def i128_r8:   ORCvtRegGPRC<R8C>;
1505
1506   // Conversion from GPRC to vector
1507   def i128_vec:  ORCvtVecGPRC;
1508
1509   // Conversion from register to GPRC
1510   def r64_i128:  ORCvtGPRCReg<R64C>;
1511   def f64_i128:  ORCvtGPRCReg<R64FP>;
1512   def r32_i128:  ORCvtGPRCReg<R32C>;
1513   def f32_i128:  ORCvtGPRCReg<R32FP>;
1514   def r16_i128:  ORCvtGPRCReg<R16C>;
1515   def r8_i128:   ORCvtGPRCReg<R8C>;
1516
1517   // Conversion from vector to GPRC
1518   def vec_i128:  ORCvtGPRCVec;
1519 */
1520 /*
1521   // Conversion from register to R32C:
1522   def r16_r32:   ORCvtFormRegR32<R16C>;
1523   def r8_r32:    ORCvtFormRegR32<R8C>;
1524   
1525   // Conversion from R32C to register
1526   def r32_r16:   ORCvtFormR32Reg<R16C>;
1527   def r32_r8:    ORCvtFormR32Reg<R8C>;
1528   
1529   // Conversion from register to R64C:
1530   def r32_r64:   ORCvtFormR64Reg<R32C>;
1531   def r16_r64:   ORCvtFormR64Reg<R16C>;
1532   def r8_r64:    ORCvtFormR64Reg<R8C>;
1533   
1534   // Conversion from R64C to register
1535   def r64_r32:   ORCvtFormRegR64<R32C>;
1536   def r64_r16:   ORCvtFormRegR64<R16C>;
1537   def r64_r8:    ORCvtFormRegR64<R8C>;
1538 */
1539
1540   // bitconvert patterns:
1541   def r32_f32:   ORCvtFormR32Reg<R32FP,
1542                                  [(set R32FP:$rT, (bitconvert R32C:$rA))]>;
1543   def f32_r32:   ORCvtFormRegR32<R32FP,
1544                                  [(set R32C:$rT, (bitconvert R32FP:$rA))]>;
1545
1546   def r64_f64:   ORCvtFormR64Reg<R64FP,
1547                                  [(set R64FP:$rT, (bitconvert R64C:$rA))]>;
1548   def f64_r64:   ORCvtFormRegR64<R64FP,
1549                                  [(set R64C:$rT, (bitconvert R64FP:$rA))]>;
1550 }
1551
1552 defm OR : BitwiseOr;
1553
1554 // scalar->vector promotion patterns (preferred slot to vector):
1555 def : Pat<(v16i8 (SPUprefslot2vec R8C:$rA)),
1556           (ORv16i8_i8 R8C:$rA)>;
1557
1558 def : Pat<(v8i16 (SPUprefslot2vec R16C:$rA)),
1559           (ORv8i16_i16 R16C:$rA)>;
1560
1561 def : Pat<(v4i32 (SPUprefslot2vec R32C:$rA)),
1562           (ORv4i32_i32 R32C:$rA)>;
1563
1564 def : Pat<(v2i64 (SPUprefslot2vec R64C:$rA)),
1565           (ORv2i64_i64 R64C:$rA)>;
1566
1567 def : Pat<(v4f32 (SPUprefslot2vec R32FP:$rA)),
1568           (ORv4f32_f32 R32FP:$rA)>;
1569
1570 def : Pat<(v2f64 (SPUprefslot2vec R64FP:$rA)),
1571           (ORv2f64_f64 R64FP:$rA)>;
1572
1573 // ORi*_v*: Used to extract vector element 0 (the preferred slot), otherwise
1574 // known as converting the vector back to its preferred slot
1575
1576 def : Pat<(SPUvec2prefslot (v16i8 VECREG:$rA)),
1577           (ORi8_v16i8 VECREG:$rA)>;
1578
1579 def : Pat<(SPUvec2prefslot (v8i16 VECREG:$rA)),
1580           (ORi16_v8i16 VECREG:$rA)>;
1581
1582 def : Pat<(SPUvec2prefslot (v4i32 VECREG:$rA)),
1583           (ORi32_v4i32 VECREG:$rA)>;
1584
1585 def : Pat<(SPUvec2prefslot (v2i64 VECREG:$rA)),
1586           (ORi64_v2i64 VECREG:$rA)>;
1587
1588 def : Pat<(SPUvec2prefslot (v4f32 VECREG:$rA)),
1589           (ORf32_v4f32 VECREG:$rA)>;
1590
1591 def : Pat<(SPUvec2prefslot (v2f64 VECREG:$rA)),
1592           (ORf64_v2f64 VECREG:$rA)>;
1593
1594 // Load Register: This is an assembler alias for a bitwise OR of a register
1595 // against itself. It's here because it brings some clarity to assembly
1596 // language output.
1597
1598 let hasCtrlDep = 1 in {
1599     class LRInst<dag OOL, dag IOL>
1600               : SPUInstr<OOL, IOL, "lr\t$rT, $rA", IntegerOp> {
1601       bits<7> RA;
1602       bits<7> RT;
1603
1604       let Pattern = [/*no pattern*/];
1605
1606       let Inst{0-10} = 0b10000010000;   /* It's an OR operation */
1607       let Inst{11-17} = RA;
1608       let Inst{18-24} = RA;
1609       let Inst{25-31} = RT;
1610     }
1611
1612     class LRVecInst<ValueType vectype>:
1613         LRInst<(outs VECREG:$rT), (ins VECREG:$rA)>;
1614
1615     class LRRegInst<RegisterClass rclass>:
1616         LRInst<(outs rclass:$rT), (ins rclass:$rA)>;
1617
1618     multiclass LoadRegister {
1619       def v2i64: LRVecInst<v2i64>;
1620       def v2f64: LRVecInst<v2f64>;
1621       def v4i32: LRVecInst<v4i32>;
1622       def v4f32: LRVecInst<v4f32>;
1623       def v8i16: LRVecInst<v8i16>;
1624       def v16i8: LRVecInst<v16i8>;
1625
1626       def r128:  LRRegInst<GPRC>;
1627       def r64:   LRRegInst<R64C>;
1628       def f64:   LRRegInst<R64FP>;
1629       def r32:   LRRegInst<R32C>;
1630       def f32:   LRRegInst<R32FP>;
1631       def r16:   LRRegInst<R16C>;
1632       def r8:    LRRegInst<R8C>;
1633     }
1634
1635     defm LR: LoadRegister;
1636 }
1637
1638 // ORC: Bitwise "or" with complement (c = a | ~b)
1639
1640 class ORCInst<dag OOL, dag IOL, list<dag> pattern>:
1641     RRForm<0b10010010000, OOL, IOL, "orc\t$rT, $rA, $rB",
1642            IntegerOp, pattern>;
1643
1644 class ORCVecInst<ValueType vectype>:
1645     ORCInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1646             [(set (vectype VECREG:$rT), (or (vectype VECREG:$rA),
1647                                             (vnot (vectype VECREG:$rB))))]>;
1648
1649 class ORCRegInst<RegisterClass rclass>:
1650   ORCInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
1651           [(set rclass:$rT, (or rclass:$rA, (not rclass:$rB)))]>;
1652
1653 multiclass BitwiseOrComplement
1654 {
1655   def v16i8: ORCVecInst<v16i8>;
1656   def v8i16: ORCVecInst<v8i16>;
1657   def v4i32: ORCVecInst<v4i32>;
1658   def v2i64: ORCVecInst<v2i64>;
1659
1660   def r64:   ORCRegInst<R64C>;
1661   def r32:   ORCRegInst<R32C>;
1662   def r16:   ORCRegInst<R16C>;
1663   def r8:    ORCRegInst<R8C>;
1664 }
1665
1666 defm ORC : BitwiseOrComplement;
1667
1668 // OR byte immediate
1669 class ORBIInst<dag OOL, dag IOL, list<dag> pattern>:
1670     RI10Form<0b01100000, OOL, IOL, "orbi\t$rT, $rA, $val",
1671              IntegerOp, pattern>;
1672
1673 class ORBIVecInst<ValueType vectype, PatLeaf immpred>:
1674     ORBIInst<(outs VECREG:$rT), (ins VECREG:$rA, u10imm:$val),
1675              [(set (v16i8 VECREG:$rT), (or (vectype VECREG:$rA),
1676                                            (vectype immpred:$val)))]>;
1677
1678 multiclass BitwiseOrByteImm
1679 {
1680   def v16i8: ORBIVecInst<v16i8, v16i8U8Imm>;
1681
1682   def r8: ORBIInst<(outs R8C:$rT), (ins R8C:$rA, u10imm_i8:$val),
1683                    [(set R8C:$rT, (or R8C:$rA, immU8:$val))]>;
1684 }
1685
1686 defm ORBI : BitwiseOrByteImm;
1687
1688 // OR halfword immediate
1689 class ORHIInst<dag OOL, dag IOL, list<dag> pattern>:
1690     RI10Form<0b10100000, OOL, IOL, "orhi\t$rT, $rA, $val",
1691              IntegerOp, pattern>;
1692
1693 class ORHIVecInst<ValueType vectype, PatLeaf immpred>:
1694     ORHIInst<(outs VECREG:$rT), (ins VECREG:$rA, u10imm:$val),
1695               [(set (vectype VECREG:$rT), (or (vectype VECREG:$rA),
1696                                               immpred:$val))]>;
1697
1698 multiclass BitwiseOrHalfwordImm
1699 {
1700   def v8i16: ORHIVecInst<v8i16, v8i16Uns10Imm>;
1701
1702   def r16: ORHIInst<(outs R16C:$rT), (ins R16C:$rA, u10imm:$val),
1703                     [(set R16C:$rT, (or R16C:$rA, i16ImmUns10:$val))]>;
1704
1705   // Specialized ORHI form used to promote 8-bit registers to 16-bit
1706   def i8i16: ORHIInst<(outs R16C:$rT), (ins R8C:$rA, s10imm:$val),
1707                       [(set R16C:$rT, (or (anyext R8C:$rA),
1708                                           i16ImmSExt10:$val))]>;
1709 }
1710
1711 defm ORHI : BitwiseOrHalfwordImm;
1712
1713 class ORIInst<dag OOL, dag IOL, list<dag> pattern>:
1714     RI10Form<0b00100000, OOL, IOL, "ori\t$rT, $rA, $val",
1715              IntegerOp, pattern>;
1716
1717 class ORIVecInst<ValueType vectype, PatLeaf immpred>:
1718     ORIInst<(outs VECREG:$rT), (ins VECREG:$rA, u10imm:$val),
1719             [(set (vectype VECREG:$rT), (or (vectype VECREG:$rA),
1720                                             immpred:$val))]>;
1721
1722 // Bitwise "or" with immediate
1723 multiclass BitwiseOrImm
1724 {
1725   def v4i32: ORIVecInst<v4i32, v4i32Uns10Imm>;
1726
1727   def r32: ORIInst<(outs R32C:$rT), (ins R32C:$rA, u10imm_i32:$val),
1728                    [(set R32C:$rT, (or R32C:$rA, i32ImmUns10:$val))]>;
1729
1730   // i16i32: hacked version of the ori instruction to extend 16-bit quantities
1731   // to 32-bit quantities. used exclusively to match "anyext" conversions (vide
1732   // infra "anyext 16->32" pattern.)
1733   def i16i32: ORIInst<(outs R32C:$rT), (ins R16C:$rA, s10imm_i32:$val),
1734                       [(set R32C:$rT, (or (anyext R16C:$rA),
1735                                           i32ImmSExt10:$val))]>;
1736
1737   // i8i32: Hacked version of the ORI instruction to extend 16-bit quantities
1738   // to 32-bit quantities. Used exclusively to match "anyext" conversions (vide
1739   // infra "anyext 16->32" pattern.)
1740   def i8i32: ORIInst<(outs R32C:$rT), (ins R8C:$rA, s10imm_i32:$val),
1741                      [(set R32C:$rT, (or (anyext R8C:$rA),
1742                                          i32ImmSExt10:$val))]>;
1743 }
1744
1745 defm ORI : BitwiseOrImm;
1746
1747 // ORX: "or" across the vector: or's $rA's word slots leaving the result in
1748 // $rT[0], slots 1-3 are zeroed.
1749 //
1750 // FIXME: Needs to match an intrinsic pattern.
1751 def ORXv4i32:
1752     RRForm<0b10010010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1753       "orx\t$rT, $rA, $rB", IntegerOp,
1754       []>;
1755
1756 // XOR:
1757
1758 class XORInst<dag OOL, dag IOL, list<dag> pattern> :
1759     RRForm<0b10010010000, OOL, IOL, "xor\t$rT, $rA, $rB",
1760            IntegerOp, pattern>;
1761
1762 class XORVecInst<ValueType vectype>:
1763     XORInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1764              [(set (vectype VECREG:$rT), (xor (vectype VECREG:$rA),
1765                                               (vectype VECREG:$rB)))]>;
1766
1767 class XORRegInst<RegisterClass rclass>:
1768     XORInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
1769              [(set rclass:$rT, (xor rclass:$rA, rclass:$rB))]>;
1770
1771 multiclass BitwiseExclusiveOr
1772 {
1773   def v16i8: XORVecInst<v16i8>;
1774   def v8i16: XORVecInst<v8i16>;
1775   def v4i32: XORVecInst<v4i32>;
1776   def v2i64: XORVecInst<v2i64>;
1777
1778   def r128:  XORRegInst<GPRC>;
1779   def r64:   XORRegInst<R64C>;
1780   def r32:   XORRegInst<R32C>;
1781   def r16:   XORRegInst<R16C>;
1782   def r8:    XORRegInst<R8C>;
1783
1784   // Special forms for floating point instructions.
1785   // fneg and fabs require bitwise logical ops to manipulate the sign bit.
1786
1787   def fneg32: XORInst<(outs R32FP:$rT), (ins R32FP:$rA, R32C:$rB),
1788                       [/* no pattern */]>;
1789
1790   def fneg64: XORInst<(outs R64FP:$rT), (ins R64FP:$rA, VECREG:$rB),
1791                       [/* no pattern */]>;
1792
1793   def fnegvec: XORInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1794                        [/* no pattern, see fneg{32,64} */]>;
1795 }
1796
1797 defm XOR : BitwiseExclusiveOr;
1798
1799 //==----------------------------------------------------------
1800
1801 class XORBIInst<dag OOL, dag IOL, list<dag> pattern>:
1802     RI10Form<0b01100000, OOL, IOL, "xorbi\t$rT, $rA, $val",
1803              IntegerOp, pattern>;
1804
1805 multiclass XorByteImm
1806 {
1807   def v16i8:
1808     XORBIInst<(outs VECREG:$rT), (ins VECREG:$rA, u10imm:$val),
1809               [(set (v16i8 VECREG:$rT), (xor (v16i8 VECREG:$rA), v16i8U8Imm:$val))]>;
1810
1811   def r8:
1812     XORBIInst<(outs R8C:$rT), (ins R8C:$rA, u10imm_i8:$val),
1813               [(set R8C:$rT, (xor R8C:$rA, immU8:$val))]>;
1814 }
1815
1816 defm XORBI : XorByteImm;
1817
1818 def XORHIv8i16:
1819     RI10Form<0b10100000, (outs VECREG:$rT), (ins VECREG:$rA, u10imm:$val),
1820       "xorhi\t$rT, $rA, $val", IntegerOp,
1821       [(set (v8i16 VECREG:$rT), (xor (v8i16 VECREG:$rA),
1822                                       v8i16SExt10Imm:$val))]>;
1823
1824 def XORHIr16:
1825     RI10Form<0b10100000, (outs R16C:$rT), (ins R16C:$rA, s10imm:$val),
1826       "xorhi\t$rT, $rA, $val", IntegerOp,
1827       [(set R16C:$rT, (xor R16C:$rA, i16ImmSExt10:$val))]>;
1828
1829 def XORIv4i32:
1830     RI10Form<0b00100000, (outs VECREG:$rT), (ins VECREG:$rA, s10imm_i32:$val),
1831       "xori\t$rT, $rA, $val", IntegerOp,
1832       [(set (v4i32 VECREG:$rT), (xor (v4i32 VECREG:$rA),
1833                                      v4i32SExt10Imm:$val))]>;
1834
1835 def XORIr32:
1836     RI10Form<0b00100000, (outs R32C:$rT), (ins R32C:$rA, s10imm_i32:$val),
1837       "xori\t$rT, $rA, $val", IntegerOp,
1838       [(set R32C:$rT, (xor R32C:$rA, i32ImmSExt10:$val))]>;
1839
1840 // NAND:
1841 def NANDv16i8:
1842     RRForm<0b10010010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1843       "nand\t$rT, $rA, $rB", IntegerOp,
1844       [(set (v16i8 VECREG:$rT), (vnot (and (v16i8 VECREG:$rA),
1845                                            (v16i8 VECREG:$rB))))]>;
1846
1847 def NANDv8i16:
1848     RRForm<0b10010010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1849       "nand\t$rT, $rA, $rB", IntegerOp,
1850       [(set (v8i16 VECREG:$rT), (vnot (and (v8i16 VECREG:$rA),
1851                                            (v8i16 VECREG:$rB))))]>;
1852
1853 def NANDv4i32:
1854     RRForm<0b10010010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1855       "nand\t$rT, $rA, $rB", IntegerOp,
1856       [(set (v4i32 VECREG:$rT), (vnot (and (v4i32 VECREG:$rA),
1857                                            (v4i32 VECREG:$rB))))]>;
1858
1859 def NANDr32:
1860     RRForm<0b10010010000, (outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
1861       "nand\t$rT, $rA, $rB", IntegerOp,
1862       [(set R32C:$rT, (not (and R32C:$rA, R32C:$rB)))]>;
1863
1864 def NANDr16:
1865     RRForm<0b10010010000, (outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
1866       "nand\t$rT, $rA, $rB", IntegerOp,
1867       [(set R16C:$rT, (not (and R16C:$rA, R16C:$rB)))]>;
1868
1869 def NANDr8:
1870     RRForm<0b10010010000, (outs R8C:$rT), (ins R8C:$rA, R8C:$rB),
1871       "nand\t$rT, $rA, $rB", IntegerOp,
1872       [(set R8C:$rT, (not (and R8C:$rA, R8C:$rB)))]>;
1873
1874 // NOR:
1875 def NORv16i8:
1876     RRForm<0b10010010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1877       "nor\t$rT, $rA, $rB", IntegerOp,
1878       [(set (v16i8 VECREG:$rT), (vnot (or (v16i8 VECREG:$rA),
1879                                           (v16i8 VECREG:$rB))))]>;
1880
1881 def NORv8i16:
1882     RRForm<0b10010010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1883       "nor\t$rT, $rA, $rB", IntegerOp,
1884       [(set (v8i16 VECREG:$rT), (vnot (or (v8i16 VECREG:$rA),
1885                                           (v8i16 VECREG:$rB))))]>;
1886
1887 def NORv4i32:
1888     RRForm<0b10010010000, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
1889       "nor\t$rT, $rA, $rB", IntegerOp,
1890       [(set (v4i32 VECREG:$rT), (vnot (or (v4i32 VECREG:$rA),
1891                                           (v4i32 VECREG:$rB))))]>;
1892
1893 def NORr32:
1894     RRForm<0b10010010000, (outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
1895       "nor\t$rT, $rA, $rB", IntegerOp,
1896       [(set R32C:$rT, (not (or R32C:$rA, R32C:$rB)))]>;
1897
1898 def NORr16:
1899     RRForm<0b10010010000, (outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
1900       "nor\t$rT, $rA, $rB", IntegerOp,
1901       [(set R16C:$rT, (not (or R16C:$rA, R16C:$rB)))]>;
1902
1903 def NORr8:
1904     RRForm<0b10010010000, (outs R8C:$rT), (ins R8C:$rA, R8C:$rB),
1905       "nor\t$rT, $rA, $rB", IntegerOp,
1906       [(set R8C:$rT, (not (or R8C:$rA, R8C:$rB)))]>;
1907
1908 // Select bits:
1909 class SELBInst<dag OOL, dag IOL, list<dag> pattern>:
1910     RRRForm<0b1000, OOL, IOL, "selb\t$rT, $rA, $rB, $rC",
1911             IntegerOp, pattern>;
1912
1913 class SELBVecInst<ValueType vectype>:
1914   SELBInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
1915            [(set (vectype VECREG:$rT),
1916                  (or (and (vectype VECREG:$rC), (vectype VECREG:$rB)),
1917                      (and (vnot (vectype VECREG:$rC)),
1918                           (vectype VECREG:$rA))))]>;
1919
1920 class SELBVecVCondInst<ValueType vectype>:
1921   SELBInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
1922            [(set (vectype VECREG:$rT),
1923                  (select (vectype VECREG:$rC),
1924                          (vectype VECREG:$rB),
1925                          (vectype VECREG:$rA)))]>;
1926
1927 class SELBVecCondInst<ValueType vectype>:
1928   SELBInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, R32C:$rC),
1929            [(set (vectype VECREG:$rT),
1930                  (select R32C:$rC,
1931                          (vectype VECREG:$rB),
1932                          (vectype VECREG:$rA)))]>;
1933
1934 class SELBRegInst<RegisterClass rclass>:
1935   SELBInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB, rclass:$rC),
1936            [(set rclass:$rT,
1937                  (or (and rclass:$rB, rclass:$rC),
1938                      (and rclass:$rA, (not rclass:$rC))))]>;
1939
1940 class SELBRegCondInst<RegisterClass rcond, RegisterClass rclass>:
1941   SELBInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB, rcond:$rC),
1942            [(set rclass:$rT,
1943                  (select rcond:$rC, rclass:$rB, rclass:$rA))]>;
1944
1945 multiclass SelectBits
1946 {
1947   def v16i8: SELBVecInst<v16i8>;
1948   def v8i16: SELBVecInst<v8i16>;
1949   def v4i32: SELBVecInst<v4i32>;
1950   def v2i64: SELBVecInst<v2i64>;
1951
1952   def r128:  SELBRegInst<GPRC>;
1953   def r64:   SELBRegInst<R64C>;
1954   def r32:   SELBRegInst<R32C>;
1955   def r16:   SELBRegInst<R16C>;
1956   def r8:    SELBRegInst<R8C>;
1957
1958   def v16i8_cond: SELBVecCondInst<v16i8>;
1959   def v8i16_cond: SELBVecCondInst<v8i16>;
1960   def v4i32_cond: SELBVecCondInst<v4i32>;
1961   def v2i64_cond: SELBVecCondInst<v2i64>;
1962
1963   def v16i8_vcond: SELBVecCondInst<v16i8>;
1964   def v8i16_vcond: SELBVecCondInst<v8i16>;
1965   def v4i32_vcond: SELBVecCondInst<v4i32>;
1966   def v2i64_vcond: SELBVecCondInst<v2i64>;
1967
1968   def v4f32_cond:
1969         SELBInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
1970                  [(set (v4f32 VECREG:$rT),
1971                        (select (v4i32 VECREG:$rC),
1972                                (v4f32 VECREG:$rB),
1973                                (v4f32 VECREG:$rA)))]>;
1974
1975   // SELBr64_cond is defined in SPU64InstrInfo.td
1976   def r32_cond:   SELBRegCondInst<R32C, R32C>;
1977   def f32_cond:   SELBRegCondInst<R32C, R32FP>;
1978   def r16_cond:   SELBRegCondInst<R16C, R16C>;
1979   def r8_cond:    SELBRegCondInst<R8C,  R8C>;
1980 }
1981
1982 defm SELB : SelectBits;
1983
1984 class SPUselbPatVec<ValueType vectype, SPUInstr inst>:
1985    Pat<(SPUselb (vectype VECREG:$rA), (vectype VECREG:$rB), (vectype VECREG:$rC)),
1986        (inst VECREG:$rA, VECREG:$rB, VECREG:$rC)>;
1987
1988 def : SPUselbPatVec<v16i8, SELBv16i8>;
1989 def : SPUselbPatVec<v8i16, SELBv8i16>;
1990 def : SPUselbPatVec<v4i32, SELBv4i32>;
1991 def : SPUselbPatVec<v2i64, SELBv2i64>;
1992
1993 class SPUselbPatReg<RegisterClass rclass, SPUInstr inst>:
1994    Pat<(SPUselb rclass:$rA, rclass:$rB, rclass:$rC),
1995        (inst rclass:$rA, rclass:$rB, rclass:$rC)>;
1996
1997 def : SPUselbPatReg<R8C,   SELBr8>;
1998 def : SPUselbPatReg<R16C,  SELBr16>;
1999 def : SPUselbPatReg<R32C,  SELBr32>;
2000 def : SPUselbPatReg<R64C,  SELBr64>;
2001
2002 // EQV: Equivalence (1 for each same bit, otherwise 0)
2003 //
2004 // Note: There are a lot of ways to match this bit operator and these patterns
2005 // attempt to be as exhaustive as possible.
2006
2007 class EQVInst<dag OOL, dag IOL, list<dag> pattern>:
2008     RRForm<0b10010010000, OOL, IOL, "eqv\t$rT, $rA, $rB",
2009            IntegerOp, pattern>;
2010
2011 class EQVVecInst<ValueType vectype>:
2012     EQVInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
2013             [(set (vectype VECREG:$rT),
2014                   (or (and (vectype VECREG:$rA), (vectype VECREG:$rB)),
2015                       (and (vnot (vectype VECREG:$rA)),
2016                            (vnot (vectype VECREG:$rB)))))]>;
2017
2018 class EQVRegInst<RegisterClass rclass>:
2019     EQVInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
2020             [(set rclass:$rT, (or (and rclass:$rA, rclass:$rB),
2021                                   (and (not rclass:$rA), (not rclass:$rB))))]>;
2022
2023 class EQVVecPattern1<ValueType vectype>:
2024   EQVInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
2025           [(set (vectype VECREG:$rT),
2026                 (xor (vectype VECREG:$rA), (vnot (vectype VECREG:$rB))))]>;
2027
2028 class EQVRegPattern1<RegisterClass rclass>:
2029   EQVInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
2030           [(set rclass:$rT, (xor rclass:$rA, (not rclass:$rB)))]>;
2031
2032 class EQVVecPattern2<ValueType vectype>:
2033   EQVInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
2034           [(set (vectype VECREG:$rT),
2035                 (or (and (vectype VECREG:$rA), (vectype VECREG:$rB)),
2036                     (vnot (or (vectype VECREG:$rA), (vectype VECREG:$rB)))))]>;
2037
2038 class EQVRegPattern2<RegisterClass rclass>:
2039   EQVInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
2040           [(set rclass:$rT,
2041                 (or (and rclass:$rA, rclass:$rB),
2042                     (not (or rclass:$rA, rclass:$rB))))]>;
2043
2044 class EQVVecPattern3<ValueType vectype>:
2045   EQVInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
2046           [(set (vectype VECREG:$rT),
2047                 (not (xor (vectype VECREG:$rA), (vectype VECREG:$rB))))]>;
2048
2049 class EQVRegPattern3<RegisterClass rclass>:
2050   EQVInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
2051           [(set rclass:$rT, (not (xor rclass:$rA, rclass:$rB)))]>;
2052
2053 multiclass BitEquivalence
2054 {
2055   def v16i8: EQVVecInst<v16i8>;
2056   def v8i16: EQVVecInst<v8i16>;
2057   def v4i32: EQVVecInst<v4i32>;
2058   def v2i64: EQVVecInst<v2i64>;
2059
2060   def v16i8_1: EQVVecPattern1<v16i8>;
2061   def v8i16_1: EQVVecPattern1<v8i16>;
2062   def v4i32_1: EQVVecPattern1<v4i32>;
2063   def v2i64_1: EQVVecPattern1<v2i64>;
2064
2065   def v16i8_2: EQVVecPattern2<v16i8>;
2066   def v8i16_2: EQVVecPattern2<v8i16>;
2067   def v4i32_2: EQVVecPattern2<v4i32>;
2068   def v2i64_2: EQVVecPattern2<v2i64>;
2069
2070   def v16i8_3: EQVVecPattern3<v16i8>;
2071   def v8i16_3: EQVVecPattern3<v8i16>;
2072   def v4i32_3: EQVVecPattern3<v4i32>;
2073   def v2i64_3: EQVVecPattern3<v2i64>;
2074
2075   def r128:  EQVRegInst<GPRC>;
2076   def r64:   EQVRegInst<R64C>;
2077   def r32:   EQVRegInst<R32C>;
2078   def r16:   EQVRegInst<R16C>;
2079   def r8:    EQVRegInst<R8C>;
2080
2081   def r128_1: EQVRegPattern1<GPRC>;
2082   def r64_1:  EQVRegPattern1<R64C>;
2083   def r32_1:  EQVRegPattern1<R32C>;
2084   def r16_1:  EQVRegPattern1<R16C>;
2085   def r8_1:   EQVRegPattern1<R8C>;
2086
2087   def r128_2: EQVRegPattern2<GPRC>;
2088   def r64_2:  EQVRegPattern2<R64C>;
2089   def r32_2:  EQVRegPattern2<R32C>;
2090   def r16_2:  EQVRegPattern2<R16C>;
2091   def r8_2:   EQVRegPattern2<R8C>;
2092
2093   def r128_3: EQVRegPattern3<GPRC>;
2094   def r64_3:  EQVRegPattern3<R64C>;
2095   def r32_3:  EQVRegPattern3<R32C>;
2096   def r16_3:  EQVRegPattern3<R16C>;
2097   def r8_3:   EQVRegPattern3<R8C>;
2098 }
2099
2100 defm EQV: BitEquivalence;
2101
2102 //===----------------------------------------------------------------------===//
2103 // Vector shuffle...
2104 //===----------------------------------------------------------------------===//
2105 // SPUshuffle is generated in LowerVECTOR_SHUFFLE and gets replaced with SHUFB.
2106 // See the SPUshuffle SDNode operand above, which sets up the DAG pattern
2107 // matcher to emit something when the LowerVECTOR_SHUFFLE generates a node with
2108 // the SPUISD::SHUFB opcode.
2109 //===----------------------------------------------------------------------===//
2110
2111 class SHUFBInst<dag OOL, dag IOL, list<dag> pattern>:
2112     RRRForm<0b1000, OOL, IOL, "shufb\t$rT, $rA, $rB, $rC",
2113             IntegerOp, pattern>;
2114
2115 class SHUFBVecInst<ValueType resultvec, ValueType maskvec>:
2116     SHUFBInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
2117               [(set (resultvec VECREG:$rT),
2118                     (SPUshuffle (resultvec VECREG:$rA),
2119                                 (resultvec VECREG:$rB),
2120                                 (maskvec VECREG:$rC)))]>;
2121
2122 class SHUFBGPRCInst:
2123     SHUFBInst<(outs VECREG:$rT), (ins GPRC:$rA, GPRC:$rB, VECREG:$rC),
2124               [/* no pattern */]>;
2125
2126 multiclass ShuffleBytes
2127 {
2128   def v16i8     : SHUFBVecInst<v16i8, v16i8>;
2129   def v16i8_m32 : SHUFBVecInst<v16i8, v4i32>;
2130   def v8i16     : SHUFBVecInst<v8i16, v16i8>;
2131   def v8i16_m32 : SHUFBVecInst<v8i16, v4i32>;
2132   def v4i32     : SHUFBVecInst<v4i32, v16i8>;
2133   def v4i32_m32 : SHUFBVecInst<v4i32, v4i32>;
2134   def v2i64     : SHUFBVecInst<v2i64, v16i8>;
2135   def v2i64_m32 : SHUFBVecInst<v2i64, v4i32>;
2136
2137   def v4f32     : SHUFBVecInst<v4f32, v16i8>;
2138   def v4f32_m32 : SHUFBVecInst<v4f32, v4i32>;
2139
2140   def v2f64     : SHUFBVecInst<v2f64, v16i8>;
2141   def v2f64_m32 : SHUFBVecInst<v2f64, v4i32>;
2142
2143   def gprc      : SHUFBGPRCInst;
2144 }
2145
2146 defm SHUFB : ShuffleBytes;
2147
2148 //===----------------------------------------------------------------------===//
2149 // Shift and rotate group:
2150 //===----------------------------------------------------------------------===//
2151
2152 class SHLHInst<dag OOL, dag IOL, list<dag> pattern>:
2153     RRForm<0b11111010000, OOL, IOL, "shlh\t$rT, $rA, $rB",
2154            RotateShift, pattern>;
2155
2156 class SHLHVecInst<ValueType vectype>:
2157     SHLHInst<(outs VECREG:$rT), (ins VECREG:$rA, R16C:$rB),
2158              [(set (vectype VECREG:$rT),
2159                    (SPUvec_shl (vectype VECREG:$rA), R16C:$rB))]>;
2160
2161 multiclass ShiftLeftHalfword
2162 {
2163   def v8i16: SHLHVecInst<v8i16>;
2164   def r16:   SHLHInst<(outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
2165                       [(set R16C:$rT, (shl R16C:$rA, R16C:$rB))]>;
2166   def r16_r32: SHLHInst<(outs R16C:$rT), (ins R16C:$rA, R32C:$rB),
2167                         [(set R16C:$rT, (shl R16C:$rA, R32C:$rB))]>;
2168 }
2169
2170 defm SHLH : ShiftLeftHalfword;
2171
2172 //===----------------------------------------------------------------------===//
2173
2174 class SHLHIInst<dag OOL, dag IOL, list<dag> pattern>:
2175     RI7Form<0b11111010000, OOL, IOL, "shlhi\t$rT, $rA, $val",
2176             RotateShift, pattern>;
2177
2178 class SHLHIVecInst<ValueType vectype>:
2179     SHLHIInst<(outs VECREG:$rT), (ins VECREG:$rA, u7imm:$val),
2180               [(set (vectype VECREG:$rT),
2181                     (SPUvec_shl (vectype VECREG:$rA), (i16 uimm7:$val)))]>;
2182
2183 multiclass ShiftLeftHalfwordImm
2184 {
2185   def v8i16: SHLHIVecInst<v8i16>;
2186   def r16: SHLHIInst<(outs R16C:$rT), (ins R16C:$rA, u7imm:$val),
2187                      [(set R16C:$rT, (shl R16C:$rA, (i16 uimm7:$val)))]>;
2188 }
2189
2190 defm SHLHI : ShiftLeftHalfwordImm;
2191
2192 def : Pat<(SPUvec_shl (v8i16 VECREG:$rA), (i32 uimm7:$val)),
2193           (SHLHIv8i16 VECREG:$rA, uimm7:$val)>;
2194
2195 def : Pat<(shl R16C:$rA, (i32 uimm7:$val)),
2196           (SHLHIr16 R16C:$rA, uimm7:$val)>;
2197
2198 //===----------------------------------------------------------------------===//
2199
2200 class SHLInst<dag OOL, dag IOL, list<dag> pattern>:
2201     RRForm<0b11111010000, OOL, IOL, "shl\t$rT, $rA, $rB",
2202            RotateShift, pattern>;
2203
2204 multiclass ShiftLeftWord
2205 {
2206   def v4i32:
2207       SHLInst<(outs VECREG:$rT), (ins VECREG:$rA, R16C:$rB),
2208               [(set (v4i32 VECREG:$rT),
2209                     (SPUvec_shl (v4i32 VECREG:$rA), R16C:$rB))]>;
2210   def r32:
2211       SHLInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
2212               [(set R32C:$rT, (shl R32C:$rA, R32C:$rB))]>;
2213 }
2214
2215 defm SHL: ShiftLeftWord;
2216
2217 //===----------------------------------------------------------------------===//
2218
2219 class SHLIInst<dag OOL, dag IOL, list<dag> pattern>:
2220     RI7Form<0b11111010000, OOL, IOL, "shli\t$rT, $rA, $val",
2221             RotateShift, pattern>;
2222
2223 multiclass ShiftLeftWordImm
2224 {
2225   def v4i32:
2226     SHLIInst<(outs VECREG:$rT), (ins VECREG:$rA, u7imm_i32:$val),
2227              [(set (v4i32 VECREG:$rT),
2228                    (SPUvec_shl (v4i32 VECREG:$rA), (i32 uimm7:$val)))]>;
2229
2230   def r32:
2231     SHLIInst<(outs R32C:$rT), (ins R32C:$rA, u7imm_i32:$val),
2232              [(set R32C:$rT, (shl R32C:$rA, (i32 uimm7:$val)))]>;
2233 }
2234
2235 defm SHLI : ShiftLeftWordImm;
2236
2237 //===----------------------------------------------------------------------===//
2238 // SHLQBI vec form: Note that this will shift the entire vector (the 128-bit
2239 // register) to the left. Vector form is here to ensure type correctness.
2240 //
2241 // The shift count is in the lowest 3 bits (29-31) of $rB, so only a bit shift
2242 // of 7 bits is actually possible.
2243 //
2244 // Note also that SHLQBI/SHLQBII are used in conjunction with SHLQBY/SHLQBYI
2245 // to shift i64 and i128. SHLQBI is the residual left over after shifting by
2246 // bytes with SHLQBY.
2247
2248 class SHLQBIInst<dag OOL, dag IOL, list<dag> pattern>:
2249     RRForm<0b11011011100, OOL, IOL, "shlqbi\t$rT, $rA, $rB",
2250            RotateShift, pattern>;
2251
2252 class SHLQBIVecInst<ValueType vectype>:
2253     SHLQBIInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2254                [(set (vectype VECREG:$rT),
2255                      (SPUshlquad_l_bits (vectype VECREG:$rA), R32C:$rB))]>;
2256
2257 class SHLQBIRegInst<RegisterClass rclass>:
2258     SHLQBIInst<(outs rclass:$rT), (ins rclass:$rA, R32C:$rB),
2259                [/* no pattern */]>;
2260
2261 multiclass ShiftLeftQuadByBits
2262 {
2263   def v16i8: SHLQBIVecInst<v16i8>;
2264   def v8i16: SHLQBIVecInst<v8i16>;
2265   def v4i32: SHLQBIVecInst<v4i32>;
2266   def v4f32: SHLQBIVecInst<v4f32>;
2267   def v2i64: SHLQBIVecInst<v2i64>;
2268   def v2f64: SHLQBIVecInst<v2f64>;
2269
2270   def r128:  SHLQBIRegInst<GPRC>;
2271 }
2272
2273 defm SHLQBI : ShiftLeftQuadByBits;
2274
2275 // See note above on SHLQBI. In this case, the predicate actually does then
2276 // enforcement, whereas with SHLQBI, we have to "take it on faith."
2277 class SHLQBIIInst<dag OOL, dag IOL, list<dag> pattern>:
2278     RI7Form<0b11011111100, OOL, IOL, "shlqbii\t$rT, $rA, $val",
2279             RotateShift, pattern>;
2280
2281 class SHLQBIIVecInst<ValueType vectype>:
2282     SHLQBIIInst<(outs VECREG:$rT), (ins VECREG:$rA, u7imm_i32:$val),
2283                 [(set (vectype VECREG:$rT),
2284                       (SPUshlquad_l_bits (vectype VECREG:$rA), (i32 bitshift:$val)))]>;
2285
2286 multiclass ShiftLeftQuadByBitsImm
2287 {
2288   def v16i8 : SHLQBIIVecInst<v16i8>;
2289   def v8i16 : SHLQBIIVecInst<v8i16>;
2290   def v4i32 : SHLQBIIVecInst<v4i32>;
2291   def v4f32 : SHLQBIIVecInst<v4f32>;
2292   def v2i64 : SHLQBIIVecInst<v2i64>;
2293   def v2f64 : SHLQBIIVecInst<v2f64>;
2294 }
2295
2296 defm SHLQBII : ShiftLeftQuadByBitsImm;
2297
2298 // SHLQBY, SHLQBYI vector forms: Shift the entire vector to the left by bytes,
2299 // not by bits. See notes above on SHLQBI.
2300
2301 class SHLQBYInst<dag OOL, dag IOL, list<dag> pattern>:
2302     RI7Form<0b11111011100, OOL, IOL, "shlqby\t$rT, $rA, $rB",
2303             RotateShift, pattern>;
2304
2305 class SHLQBYVecInst<ValueType vectype>:
2306     SHLQBYInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2307                [(set (vectype VECREG:$rT),
2308                      (SPUshlquad_l_bytes (vectype VECREG:$rA), R32C:$rB))]>;
2309
2310 multiclass ShiftLeftQuadBytes
2311 {
2312   def v16i8: SHLQBYVecInst<v16i8>;
2313   def v8i16: SHLQBYVecInst<v8i16>;
2314   def v4i32: SHLQBYVecInst<v4i32>;
2315   def v4f32: SHLQBYVecInst<v4f32>;
2316   def v2i64: SHLQBYVecInst<v2i64>;
2317   def v2f64: SHLQBYVecInst<v2f64>;
2318   def r128: SHLQBYInst<(outs GPRC:$rT), (ins GPRC:$rA, R32C:$rB),
2319                        [(set GPRC:$rT, (SPUshlquad_l_bytes GPRC:$rA, R32C:$rB))]>;
2320 }
2321
2322 defm SHLQBY: ShiftLeftQuadBytes;
2323
2324 class SHLQBYIInst<dag OOL, dag IOL, list<dag> pattern>:
2325     RI7Form<0b11111111100, OOL, IOL, "shlqbyi\t$rT, $rA, $val",
2326             RotateShift, pattern>;
2327
2328 class SHLQBYIVecInst<ValueType vectype>:
2329     SHLQBYIInst<(outs VECREG:$rT), (ins VECREG:$rA, u7imm_i32:$val),
2330                 [(set (vectype VECREG:$rT),
2331                       (SPUshlquad_l_bytes (vectype VECREG:$rA), (i32 uimm7:$val)))]>;
2332
2333 multiclass ShiftLeftQuadBytesImm
2334 {
2335   def v16i8: SHLQBYIVecInst<v16i8>;
2336   def v8i16: SHLQBYIVecInst<v8i16>;
2337   def v4i32: SHLQBYIVecInst<v4i32>;
2338   def v4f32: SHLQBYIVecInst<v4f32>;
2339   def v2i64: SHLQBYIVecInst<v2i64>;
2340   def v2f64: SHLQBYIVecInst<v2f64>;
2341   def r128:  SHLQBYIInst<(outs GPRC:$rT), (ins GPRC:$rA, u7imm_i32:$val),
2342                          [(set GPRC:$rT,
2343                                (SPUshlquad_l_bytes GPRC:$rA, (i32 uimm7:$val)))]>;
2344 }
2345
2346 defm SHLQBYI : ShiftLeftQuadBytesImm;
2347
2348 class SHLQBYBIInst<dag OOL, dag IOL, list<dag> pattern>:
2349     RRForm<0b00111001111, OOL, IOL, "shlqbybi\t$rT, $rA, $rB",
2350            RotateShift, pattern>;
2351
2352 class SHLQBYBIVecInst<ValueType vectype>:
2353     SHLQBYBIInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2354                 [/* no pattern */]>;
2355
2356 class SHLQBYBIRegInst<RegisterClass rclass>:
2357     SHLQBYBIInst<(outs rclass:$rT), (ins rclass:$rA, R32C:$rB),
2358                  [/* no pattern */]>;
2359
2360 multiclass ShiftLeftQuadBytesBitCount
2361 {
2362   def v16i8: SHLQBYBIVecInst<v16i8>;
2363   def v8i16: SHLQBYBIVecInst<v8i16>;
2364   def v4i32: SHLQBYBIVecInst<v4i32>;
2365   def v4f32: SHLQBYBIVecInst<v4f32>;
2366   def v2i64: SHLQBYBIVecInst<v2i64>;
2367   def v2f64: SHLQBYBIVecInst<v2f64>;
2368
2369   def r128:  SHLQBYBIRegInst<GPRC>;
2370 }
2371
2372 defm SHLQBYBI : ShiftLeftQuadBytesBitCount;
2373
2374 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2375 // Rotate halfword:
2376 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2377 class ROTHInst<dag OOL, dag IOL, list<dag> pattern>:
2378     RRForm<0b00111010000, OOL, IOL, "roth\t$rT, $rA, $rB",
2379            RotateShift, pattern>;
2380
2381 class ROTHVecInst<ValueType vectype>:
2382     ROTHInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
2383              [(set (vectype VECREG:$rT),
2384                    (SPUvec_rotl VECREG:$rA, VECREG:$rB))]>;
2385
2386 class ROTHRegInst<RegisterClass rclass>:
2387     ROTHInst<(outs rclass:$rT), (ins rclass:$rA, rclass:$rB),
2388              [(set rclass:$rT, (rotl rclass:$rA, rclass:$rB))]>;
2389
2390 multiclass RotateLeftHalfword
2391 {
2392   def v8i16: ROTHVecInst<v8i16>;
2393   def r16: ROTHRegInst<R16C>;
2394 }
2395
2396 defm ROTH: RotateLeftHalfword;
2397
2398 def ROTHr16_r32: ROTHInst<(outs R16C:$rT), (ins R16C:$rA, R32C:$rB),
2399                           [(set R16C:$rT, (rotl R16C:$rA, R32C:$rB))]>;
2400
2401 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2402 // Rotate halfword, immediate:
2403 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2404 class ROTHIInst<dag OOL, dag IOL, list<dag> pattern>:
2405     RI7Form<0b00111110000, OOL, IOL, "rothi\t$rT, $rA, $val",
2406             RotateShift, pattern>;
2407
2408 class ROTHIVecInst<ValueType vectype>:
2409     ROTHIInst<(outs VECREG:$rT), (ins VECREG:$rA, u7imm:$val),
2410               [(set (vectype VECREG:$rT),
2411                     (SPUvec_rotl VECREG:$rA, (i16 uimm7:$val)))]>;
2412
2413 multiclass RotateLeftHalfwordImm
2414 {
2415   def v8i16: ROTHIVecInst<v8i16>;
2416   def r16: ROTHIInst<(outs R16C:$rT), (ins R16C:$rA, u7imm:$val),
2417                      [(set R16C:$rT, (rotl R16C:$rA, (i16 uimm7:$val)))]>;
2418   def r16_r32: ROTHIInst<(outs R16C:$rT), (ins R16C:$rA, u7imm_i32:$val),
2419                          [(set R16C:$rT, (rotl R16C:$rA, (i32 uimm7:$val)))]>;
2420 }
2421
2422 defm ROTHI: RotateLeftHalfwordImm;
2423
2424 def : Pat<(SPUvec_rotl VECREG:$rA, (i32 uimm7:$val)),
2425           (ROTHIv8i16 VECREG:$rA, imm:$val)>;
2426
2427 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2428 // Rotate word:
2429 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2430
2431 class ROTInst<dag OOL, dag IOL, list<dag> pattern>:
2432     RRForm<0b00011010000, OOL, IOL, "rot\t$rT, $rA, $rB",
2433            RotateShift, pattern>;
2434
2435 class ROTVecInst<ValueType vectype>:
2436     ROTInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2437             [(set (vectype VECREG:$rT),
2438                   (SPUvec_rotl (vectype VECREG:$rA), R32C:$rB))]>;
2439
2440 class ROTRegInst<RegisterClass rclass>:
2441     ROTInst<(outs rclass:$rT), (ins rclass:$rA, R32C:$rB),
2442             [(set rclass:$rT,
2443                   (rotl rclass:$rA, R32C:$rB))]>;
2444
2445 multiclass RotateLeftWord
2446 {
2447   def v4i32: ROTVecInst<v4i32>;
2448   def r32:   ROTRegInst<R32C>;
2449 }
2450
2451 defm ROT: RotateLeftWord;
2452
2453 // The rotate amount is in the same bits whether we've got an 8-bit, 16-bit or
2454 // 32-bit register
2455 def ROTr32_r16_anyext:
2456     ROTInst<(outs R32C:$rT), (ins R32C:$rA, R16C:$rB),
2457             [(set R32C:$rT, (rotl R32C:$rA, (i32 (anyext R16C:$rB))))]>;
2458
2459 def : Pat<(rotl R32C:$rA, (i32 (zext R16C:$rB))),
2460           (ROTr32_r16_anyext R32C:$rA, R16C:$rB)>;
2461
2462 def : Pat<(rotl R32C:$rA, (i32 (sext R16C:$rB))),
2463           (ROTr32_r16_anyext R32C:$rA, R16C:$rB)>;
2464
2465 def ROTr32_r8_anyext:
2466     ROTInst<(outs R32C:$rT), (ins R32C:$rA, R8C:$rB),
2467             [(set R32C:$rT, (rotl R32C:$rA, (i32 (anyext R8C:$rB))))]>;
2468
2469 def : Pat<(rotl R32C:$rA, (i32 (zext R8C:$rB))),
2470           (ROTr32_r8_anyext R32C:$rA, R8C:$rB)>;
2471
2472 def : Pat<(rotl R32C:$rA, (i32 (sext R8C:$rB))),
2473           (ROTr32_r8_anyext R32C:$rA, R8C:$rB)>;
2474
2475 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2476 // Rotate word, immediate
2477 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2478
2479 class ROTIInst<dag OOL, dag IOL, list<dag> pattern>:
2480     RI7Form<0b00011110000, OOL, IOL, "roti\t$rT, $rA, $val",
2481             RotateShift, pattern>;
2482
2483 class ROTIVecInst<ValueType vectype, Operand optype, ValueType inttype, PatLeaf pred>:
2484     ROTIInst<(outs VECREG:$rT), (ins VECREG:$rA, optype:$val),
2485              [(set (vectype VECREG:$rT),
2486                    (SPUvec_rotl (vectype VECREG:$rA), (inttype pred:$val)))]>;
2487
2488 class ROTIRegInst<RegisterClass rclass, Operand optype, ValueType inttype, PatLeaf pred>:
2489     ROTIInst<(outs rclass:$rT), (ins rclass:$rA, optype:$val),
2490              [(set rclass:$rT, (rotl rclass:$rA, (inttype pred:$val)))]>;
2491
2492 multiclass RotateLeftWordImm
2493 {
2494   def v4i32: ROTIVecInst<v4i32, u7imm_i32, i32, uimm7>;
2495   def v4i32_i16: ROTIVecInst<v4i32, u7imm, i16, uimm7>;
2496   def v4i32_i8:  ROTIVecInst<v4i32, u7imm_i8, i8, uimm7>;
2497
2498   def r32:       ROTIRegInst<R32C, u7imm_i32, i32, uimm7>;
2499   def r32_i16:   ROTIRegInst<R32C, u7imm, i16, uimm7>;
2500   def r32_i8:    ROTIRegInst<R32C, u7imm_i8, i8, uimm7>;
2501 }
2502
2503 defm ROTI : RotateLeftWordImm;
2504
2505 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2506 // Rotate quad by byte (count)
2507 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2508
2509 class ROTQBYInst<dag OOL, dag IOL, list<dag> pattern>:
2510     RRForm<0b00111011100, OOL, IOL, "rotqby\t$rT, $rA, $rB",
2511            RotateShift, pattern>;
2512
2513 class ROTQBYVecInst<ValueType vectype>:
2514     ROTQBYInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2515                [(set (vectype VECREG:$rT),
2516                      (SPUrotbytes_left (vectype VECREG:$rA), R32C:$rB))]>;
2517
2518 multiclass RotateQuadLeftByBytes
2519 {
2520   def v16i8: ROTQBYVecInst<v16i8>;
2521   def v8i16: ROTQBYVecInst<v8i16>;
2522   def v4i32: ROTQBYVecInst<v4i32>;
2523   def v4f32: ROTQBYVecInst<v4f32>;
2524   def v2i64: ROTQBYVecInst<v2i64>;
2525   def v2f64: ROTQBYVecInst<v2f64>;
2526 }
2527
2528 defm ROTQBY: RotateQuadLeftByBytes;
2529
2530 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2531 // Rotate quad by byte (count), immediate
2532 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2533
2534 class ROTQBYIInst<dag OOL, dag IOL, list<dag> pattern>:
2535     RI7Form<0b00111111100, OOL, IOL, "rotqbyi\t$rT, $rA, $val",
2536             RotateShift, pattern>;
2537
2538 class ROTQBYIVecInst<ValueType vectype>:
2539     ROTQBYIInst<(outs VECREG:$rT), (ins VECREG:$rA, u7imm:$val),
2540                 [(set (vectype VECREG:$rT),
2541                       (SPUrotbytes_left (vectype VECREG:$rA), (i16 uimm7:$val)))]>;
2542
2543 multiclass RotateQuadByBytesImm
2544 {
2545   def v16i8: ROTQBYIVecInst<v16i8>;
2546   def v8i16: ROTQBYIVecInst<v8i16>;
2547   def v4i32: ROTQBYIVecInst<v4i32>;
2548   def v4f32: ROTQBYIVecInst<v4f32>;
2549   def v2i64: ROTQBYIVecInst<v2i64>;
2550   def vfi64: ROTQBYIVecInst<v2f64>;
2551 }
2552
2553 defm ROTQBYI: RotateQuadByBytesImm;
2554
2555 // See ROTQBY note above.
2556 class ROTQBYBIInst<dag OOL, dag IOL, list<dag> pattern>:
2557     RI7Form<0b00110011100, OOL, IOL,
2558       "rotqbybi\t$rT, $rA, $shift",
2559       RotateShift, pattern>;
2560
2561 class ROTQBYBIVecInst<ValueType vectype, RegisterClass rclass>:
2562     ROTQBYBIInst<(outs VECREG:$rT), (ins VECREG:$rA, rclass:$shift),
2563       [(set (vectype VECREG:$rT),
2564             (SPUrotbytes_left_bits (vectype VECREG:$rA), rclass:$shift))]>;
2565
2566 multiclass RotateQuadByBytesByBitshift {
2567   def v16i8_r32: ROTQBYBIVecInst<v16i8, R32C>;
2568   def v8i16_r32: ROTQBYBIVecInst<v8i16, R32C>;
2569   def v4i32_r32: ROTQBYBIVecInst<v4i32, R32C>;
2570   def v2i64_r32: ROTQBYBIVecInst<v2i64, R32C>;
2571 }
2572
2573 defm ROTQBYBI : RotateQuadByBytesByBitshift;
2574
2575 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2576 // See ROTQBY note above.
2577 //
2578 // Assume that the user of this instruction knows to shift the rotate count
2579 // into bit 29
2580 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2581
2582 class ROTQBIInst<dag OOL, dag IOL, list<dag> pattern>:
2583     RRForm<0b00011011100, OOL, IOL, "rotqbi\t$rT, $rA, $rB",
2584            RotateShift, pattern>;
2585
2586 class ROTQBIVecInst<ValueType vectype>:
2587     ROTQBIInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2588                [/* no pattern yet */]>;
2589
2590 class ROTQBIRegInst<RegisterClass rclass>:
2591     ROTQBIInst<(outs rclass:$rT), (ins rclass:$rA, R32C:$rB),
2592                [/* no pattern yet */]>;
2593
2594 multiclass RotateQuadByBitCount
2595 {
2596   def v16i8: ROTQBIVecInst<v16i8>;
2597   def v8i16: ROTQBIVecInst<v8i16>;
2598   def v4i32: ROTQBIVecInst<v4i32>;
2599   def v2i64: ROTQBIVecInst<v2i64>;
2600
2601   def r128:  ROTQBIRegInst<GPRC>;
2602   def r64:   ROTQBIRegInst<R64C>;
2603 }
2604
2605 defm ROTQBI: RotateQuadByBitCount;
2606
2607 class ROTQBIIInst<dag OOL, dag IOL, list<dag> pattern>:
2608     RI7Form<0b00011111100, OOL, IOL, "rotqbii\t$rT, $rA, $val",
2609             RotateShift, pattern>;
2610
2611 class ROTQBIIVecInst<ValueType vectype, Operand optype, ValueType inttype,
2612                      PatLeaf pred>:
2613     ROTQBIIInst<(outs VECREG:$rT), (ins VECREG:$rA, optype:$val),
2614                 [/* no pattern yet */]>;
2615
2616 class ROTQBIIRegInst<RegisterClass rclass, Operand optype, ValueType inttype,
2617                      PatLeaf pred>:
2618     ROTQBIIInst<(outs rclass:$rT), (ins rclass:$rA, optype:$val),
2619                 [/* no pattern yet */]>;
2620
2621 multiclass RotateQuadByBitCountImm
2622 {
2623   def v16i8: ROTQBIIVecInst<v16i8, u7imm_i32, i32, uimm7>;
2624   def v8i16: ROTQBIIVecInst<v8i16, u7imm_i32, i32, uimm7>;
2625   def v4i32: ROTQBIIVecInst<v4i32, u7imm_i32, i32, uimm7>;
2626   def v2i64: ROTQBIIVecInst<v2i64, u7imm_i32, i32, uimm7>;
2627
2628   def r128:  ROTQBIIRegInst<GPRC, u7imm_i32, i32, uimm7>;
2629   def r64:   ROTQBIIRegInst<R64C, u7imm_i32, i32, uimm7>;
2630 }
2631
2632 defm ROTQBII : RotateQuadByBitCountImm;
2633
2634 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2635 // ROTHM v8i16 form:
2636 // NOTE(1): No vector rotate is generated by the C/C++ frontend (today),
2637 //          so this only matches a synthetically generated/lowered code
2638 //          fragment.
2639 // NOTE(2): $rB must be negated before the right rotate!
2640 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2641
2642 class ROTHMInst<dag OOL, dag IOL, list<dag> pattern>:
2643     RRForm<0b10111010000, OOL, IOL, "rothm\t$rT, $rA, $rB",
2644            RotateShift, pattern>;
2645
2646 def ROTHMv8i16:
2647     ROTHMInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2648               [/* see patterns below - $rB must be negated */]>;
2649
2650 def : Pat<(SPUvec_srl (v8i16 VECREG:$rA), R32C:$rB),
2651           (ROTHMv8i16 VECREG:$rA, (SFIr32 R32C:$rB, 0))>;
2652
2653 def : Pat<(SPUvec_srl (v8i16 VECREG:$rA), R16C:$rB),
2654           (ROTHMv8i16 VECREG:$rA,
2655                       (SFIr32 (XSHWr16 R16C:$rB), 0))>;
2656
2657 def : Pat<(SPUvec_srl (v8i16 VECREG:$rA), R8C:$rB),
2658           (ROTHMv8i16 VECREG:$rA,
2659                       (SFIr32 (XSHWr16 (XSBHr8 R8C:$rB) ), 0))>;
2660
2661 // ROTHM r16 form: Rotate 16-bit quantity to right, zero fill at the left
2662 // Note: This instruction doesn't match a pattern because rB must be negated
2663 // for the instruction to work. Thus, the pattern below the instruction!
2664
2665 def ROTHMr16:
2666     ROTHMInst<(outs R16C:$rT), (ins R16C:$rA, R32C:$rB),
2667               [/* see patterns below - $rB must be negated! */]>;
2668
2669 def : Pat<(srl R16C:$rA, R32C:$rB),
2670           (ROTHMr16 R16C:$rA, (SFIr32 R32C:$rB, 0))>;
2671
2672 def : Pat<(srl R16C:$rA, R16C:$rB),
2673           (ROTHMr16 R16C:$rA,
2674                     (SFIr32 (XSHWr16 R16C:$rB), 0))>;
2675
2676 def : Pat<(srl R16C:$rA, R8C:$rB),
2677           (ROTHMr16 R16C:$rA,
2678                     (SFIr32 (XSHWr16 (XSBHr8 R8C:$rB) ), 0))>;
2679
2680 // ROTHMI v8i16 form: See the comment for ROTHM v8i16. The difference here is
2681 // that the immediate can be complemented, so that the user doesn't have to
2682 // worry about it.
2683
2684 class ROTHMIInst<dag OOL, dag IOL, list<dag> pattern>:
2685     RI7Form<0b10111110000, OOL, IOL, "rothmi\t$rT, $rA, $val",
2686             RotateShift, pattern>;
2687
2688 def ROTHMIv8i16:
2689     ROTHMIInst<(outs VECREG:$rT), (ins VECREG:$rA, rothNeg7imm:$val),
2690                [/* no pattern */]>;
2691
2692 def : Pat<(SPUvec_srl (v8i16 VECREG:$rA), (i32 imm:$val)),
2693           (ROTHMIv8i16 VECREG:$rA, imm:$val)>;
2694
2695 def: Pat<(SPUvec_srl (v8i16 VECREG:$rA), (i16 imm:$val)),
2696          (ROTHMIv8i16 VECREG:$rA, imm:$val)>;
2697
2698 def: Pat<(SPUvec_srl (v8i16 VECREG:$rA), (i8 imm:$val)),
2699          (ROTHMIv8i16 VECREG:$rA, imm:$val)>;
2700
2701 def ROTHMIr16:
2702     ROTHMIInst<(outs R16C:$rT), (ins R16C:$rA, rothNeg7imm:$val),
2703                [/* no pattern */]>;
2704
2705 def: Pat<(srl R16C:$rA, (i32 uimm7:$val)),
2706          (ROTHMIr16 R16C:$rA, uimm7:$val)>;
2707
2708 def: Pat<(srl R16C:$rA, (i16 uimm7:$val)),
2709          (ROTHMIr16 R16C:$rA, uimm7:$val)>;
2710
2711 def: Pat<(srl R16C:$rA, (i8 uimm7:$val)),
2712          (ROTHMIr16 R16C:$rA, uimm7:$val)>;
2713
2714 // ROTM v4i32 form: See the ROTHM v8i16 comments.
2715 class ROTMInst<dag OOL, dag IOL, list<dag> pattern>:
2716     RRForm<0b10011010000, OOL, IOL, "rotm\t$rT, $rA, $rB",
2717            RotateShift, pattern>;
2718
2719 def ROTMv4i32:
2720     ROTMInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2721              [/* see patterns below - $rB must be negated */]>;
2722
2723 def : Pat<(SPUvec_srl VECREG:$rA, R32C:$rB),
2724           (ROTMv4i32 VECREG:$rA, (SFIr32 R32C:$rB, 0))>;
2725
2726 def : Pat<(SPUvec_srl VECREG:$rA, R16C:$rB),
2727           (ROTMv4i32 VECREG:$rA,
2728                      (SFIr32 (XSHWr16 R16C:$rB), 0))>;
2729
2730 def : Pat<(SPUvec_srl VECREG:$rA, R8C:$rB),
2731           (ROTMv4i32 VECREG:$rA,
2732                      (SFIr32 (XSHWr16 (XSBHr8 R8C:$rB)), 0))>;
2733
2734 def ROTMr32:
2735     ROTMInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
2736              [/* see patterns below - $rB must be negated */]>;
2737
2738 def : Pat<(srl R32C:$rA, R32C:$rB),
2739           (ROTMr32 R32C:$rA, (SFIr32 R32C:$rB, 0))>;
2740
2741 def : Pat<(srl R32C:$rA, R16C:$rB),
2742           (ROTMr32 R32C:$rA,
2743                    (SFIr32 (XSHWr16 R16C:$rB), 0))>;
2744
2745 def : Pat<(srl R32C:$rA, R8C:$rB),
2746           (ROTMr32 R32C:$rA,
2747                    (SFIr32 (XSHWr16 (XSBHr8 R8C:$rB)), 0))>;
2748
2749 // ROTMI v4i32 form: See the comment for ROTHM v8i16.
2750 def ROTMIv4i32:
2751     RI7Form<0b10011110000, (outs VECREG:$rT), (ins VECREG:$rA, rotNeg7imm:$val),
2752       "rotmi\t$rT, $rA, $val", RotateShift,
2753       [(set (v4i32 VECREG:$rT),
2754             (SPUvec_srl VECREG:$rA, (i32 uimm7:$val)))]>;
2755
2756 def : Pat<(SPUvec_srl VECREG:$rA, (i16 uimm7:$val)),
2757           (ROTMIv4i32 VECREG:$rA, uimm7:$val)>;
2758
2759 def : Pat<(SPUvec_srl VECREG:$rA, (i8 uimm7:$val)),
2760           (ROTMIv4i32 VECREG:$rA, uimm7:$val)>;
2761
2762 // ROTMI r32 form: know how to complement the immediate value.
2763 def ROTMIr32:
2764     RI7Form<0b10011110000, (outs R32C:$rT), (ins R32C:$rA, rotNeg7imm:$val),
2765       "rotmi\t$rT, $rA, $val", RotateShift,
2766       [(set R32C:$rT, (srl R32C:$rA, (i32 uimm7:$val)))]>;
2767
2768 def : Pat<(srl R32C:$rA, (i16 imm:$val)),
2769           (ROTMIr32 R32C:$rA, uimm7:$val)>;
2770
2771 def : Pat<(srl R32C:$rA, (i8 imm:$val)),
2772           (ROTMIr32 R32C:$rA, uimm7:$val)>;
2773
2774 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2775 // ROTQMBY: This is a vector form merely so that when used in an
2776 // instruction pattern, type checking will succeed. This instruction assumes
2777 // that the user knew to negate $rB.
2778 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2779
2780 class ROTQMBYInst<dag OOL, dag IOL, list<dag> pattern>:
2781     RRForm<0b10111011100, OOL, IOL, "rotqmby\t$rT, $rA, $rB",
2782            RotateShift, pattern>;
2783
2784 class ROTQMBYVecInst<ValueType vectype>:
2785     ROTQMBYInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2786                 [/* no pattern, $rB must be negated */]>;
2787
2788 class ROTQMBYRegInst<RegisterClass rclass>:
2789     ROTQMBYInst<(outs rclass:$rT), (ins rclass:$rA, R32C:$rB),
2790                 [/* no pattern */]>;
2791
2792 multiclass RotateQuadBytes
2793 {
2794   def v16i8: ROTQMBYVecInst<v16i8>;
2795   def v8i16: ROTQMBYVecInst<v8i16>;
2796   def v4i32: ROTQMBYVecInst<v4i32>;
2797   def v2i64: ROTQMBYVecInst<v2i64>;
2798
2799   def r128: ROTQMBYRegInst<GPRC>;
2800   def r64:  ROTQMBYRegInst<R64C>;
2801 }
2802
2803 defm ROTQMBY : RotateQuadBytes;
2804
2805 class ROTQMBYIInst<dag OOL, dag IOL, list<dag> pattern>:
2806     RI7Form<0b10111111100, OOL, IOL, "rotqmbyi\t$rT, $rA, $val",
2807             RotateShift, pattern>;
2808
2809 class ROTQMBYIVecInst<ValueType vectype>:
2810     ROTQMBYIInst<(outs VECREG:$rT), (ins VECREG:$rA, rotNeg7imm:$val),
2811                  [/* no pattern */]>;
2812
2813 class ROTQMBYIRegInst<RegisterClass rclass, Operand optype, ValueType inttype,
2814                       PatLeaf pred>:
2815     ROTQMBYIInst<(outs rclass:$rT), (ins rclass:$rA, optype:$val),
2816                  [/* no pattern */]>;
2817
2818 // 128-bit zero extension form:
2819 class ROTQMBYIZExtInst<RegisterClass rclass, Operand optype, PatLeaf pred>:
2820     ROTQMBYIInst<(outs GPRC:$rT), (ins rclass:$rA, optype:$val),
2821                  [/* no pattern */]>;
2822
2823 multiclass RotateQuadBytesImm
2824 {
2825   def v16i8: ROTQMBYIVecInst<v16i8>;
2826   def v8i16: ROTQMBYIVecInst<v8i16>;
2827   def v4i32: ROTQMBYIVecInst<v4i32>;
2828   def v2i64: ROTQMBYIVecInst<v2i64>;
2829
2830   def r128:  ROTQMBYIRegInst<GPRC, rotNeg7imm, i32, uimm7>;
2831   def r64:   ROTQMBYIRegInst<R64C, rotNeg7imm, i32, uimm7>;
2832   
2833   def r128_zext_r8:  ROTQMBYIZExtInst<R8C, rotNeg7imm, uimm7>;
2834   def r128_zext_r16: ROTQMBYIZExtInst<R16C, rotNeg7imm, uimm7>;
2835   def r128_zext_r32: ROTQMBYIZExtInst<R32C, rotNeg7imm, uimm7>;
2836   def r128_zext_r64: ROTQMBYIZExtInst<R64C, rotNeg7imm, uimm7>;
2837 }
2838
2839 defm ROTQMBYI : RotateQuadBytesImm;
2840
2841 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2842 // Rotate right and mask by bit count
2843 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2844
2845 class ROTQMBYBIInst<dag OOL, dag IOL, list<dag> pattern>:
2846     RRForm<0b10110011100, OOL, IOL, "rotqmbybi\t$rT, $rA, $rB",
2847            RotateShift, pattern>;
2848
2849 class ROTQMBYBIVecInst<ValueType vectype>:
2850     ROTQMBYBIInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2851                   [/* no pattern, */]>;
2852
2853 multiclass RotateMaskQuadByBitCount
2854 {
2855   def v16i8: ROTQMBYBIVecInst<v16i8>;
2856   def v8i16: ROTQMBYBIVecInst<v8i16>;
2857   def v4i32: ROTQMBYBIVecInst<v4i32>;
2858   def v2i64: ROTQMBYBIVecInst<v2i64>;
2859 }
2860
2861 defm ROTQMBYBI: RotateMaskQuadByBitCount;
2862
2863 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2864 // Rotate quad and mask by bits
2865 // Note that the rotate amount has to be negated
2866 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2867
2868 class ROTQMBIInst<dag OOL, dag IOL, list<dag> pattern>:
2869     RRForm<0b10011011100, OOL, IOL, "rotqmbi\t$rT, $rA, $rB",
2870            RotateShift, pattern>;
2871
2872 class ROTQMBIVecInst<ValueType vectype>:
2873     ROTQMBIInst<(outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2874                 [/* no pattern */]>;
2875
2876 class ROTQMBIRegInst<RegisterClass rclass>:
2877     ROTQMBIInst<(outs rclass:$rT), (ins rclass:$rA, R32C:$rB),
2878                 [/* no pattern */]>;
2879
2880 multiclass RotateMaskQuadByBits
2881 {
2882   def v16i8: ROTQMBIVecInst<v16i8>;
2883   def v8i16: ROTQMBIVecInst<v8i16>;
2884   def v4i32: ROTQMBIVecInst<v4i32>;
2885   def v2i64: ROTQMBIVecInst<v2i64>;
2886
2887   def r128:  ROTQMBIRegInst<GPRC>;
2888   def r64:   ROTQMBIRegInst<R64C>;
2889 }
2890
2891 defm ROTQMBI: RotateMaskQuadByBits;
2892
2893 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2894 // Rotate quad and mask by bits, immediate
2895 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2896
2897 class ROTQMBIIInst<dag OOL, dag IOL, list<dag> pattern>:
2898     RI7Form<0b10011111100, OOL, IOL, "rotqmbii\t$rT, $rA, $val",
2899             RotateShift, pattern>;
2900
2901 class ROTQMBIIVecInst<ValueType vectype>:
2902    ROTQMBIIInst<(outs VECREG:$rT), (ins VECREG:$rA, rotNeg7imm:$val),
2903                  [/* no pattern */]>;
2904
2905 class ROTQMBIIRegInst<RegisterClass rclass>:
2906    ROTQMBIIInst<(outs rclass:$rT), (ins rclass:$rA, rotNeg7imm:$val),
2907                  [/* no pattern */]>;
2908
2909 multiclass RotateMaskQuadByBitsImm
2910 {
2911   def v16i8: ROTQMBIIVecInst<v16i8>;
2912   def v8i16: ROTQMBIIVecInst<v8i16>;
2913   def v4i32: ROTQMBIIVecInst<v4i32>;
2914   def v2i64: ROTQMBIIVecInst<v2i64>;
2915
2916   def r128:  ROTQMBIIRegInst<GPRC>;
2917   def r64:   ROTQMBIIRegInst<R64C>;
2918 }
2919
2920 defm ROTQMBII: RotateMaskQuadByBitsImm;
2921
2922 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2923 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2924
2925 def ROTMAHv8i16:
2926     RRForm<0b01111010000, (outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2927       "rotmah\t$rT, $rA, $rB", RotateShift,
2928       [/* see patterns below - $rB must be negated */]>;
2929
2930 def : Pat<(SPUvec_sra VECREG:$rA, R32C:$rB),
2931           (ROTMAHv8i16 VECREG:$rA, (SFIr32 R32C:$rB, 0))>;
2932
2933 def : Pat<(SPUvec_sra VECREG:$rA, R16C:$rB),
2934           (ROTMAHv8i16 VECREG:$rA,
2935                        (SFIr32 (XSHWr16 R16C:$rB), 0))>;
2936
2937 def : Pat<(SPUvec_sra VECREG:$rA, R8C:$rB),
2938           (ROTMAHv8i16 VECREG:$rA,
2939                        (SFIr32 (XSHWr16 (XSBHr8 R8C:$rB)), 0))>;
2940
2941 def ROTMAHr16:
2942     RRForm<0b01111010000, (outs R16C:$rT), (ins R16C:$rA, R32C:$rB),
2943       "rotmah\t$rT, $rA, $rB", RotateShift,
2944       [/* see patterns below - $rB must be negated */]>;
2945
2946 def : Pat<(sra R16C:$rA, R32C:$rB),
2947           (ROTMAHr16 R16C:$rA, (SFIr32 R32C:$rB, 0))>;
2948
2949 def : Pat<(sra R16C:$rA, R16C:$rB),
2950           (ROTMAHr16 R16C:$rA,
2951                      (SFIr32 (XSHWr16 R16C:$rB), 0))>;
2952
2953 def : Pat<(sra R16C:$rA, R8C:$rB),
2954           (ROTMAHr16 R16C:$rA,
2955                      (SFIr32 (XSHWr16 (XSBHr8 R8C:$rB)), 0))>;
2956
2957 def ROTMAHIv8i16:
2958     RRForm<0b01111110000, (outs VECREG:$rT), (ins VECREG:$rA, rothNeg7imm:$val),
2959       "rotmahi\t$rT, $rA, $val", RotateShift,
2960       [(set (v8i16 VECREG:$rT),
2961             (SPUvec_sra (v8i16 VECREG:$rA), (i32 uimm7:$val)))]>;
2962
2963 def : Pat<(SPUvec_sra (v8i16 VECREG:$rA), (i16 uimm7:$val)),
2964           (ROTMAHIv8i16 (v8i16 VECREG:$rA), (i32 uimm7:$val))>;
2965
2966 def : Pat<(SPUvec_sra (v8i16 VECREG:$rA), (i8 uimm7:$val)),
2967           (ROTMAHIv8i16 (v8i16 VECREG:$rA), (i32 uimm7:$val))>;
2968
2969 def ROTMAHIr16:
2970     RRForm<0b01111110000, (outs R16C:$rT), (ins R16C:$rA, rothNeg7imm_i16:$val),
2971       "rotmahi\t$rT, $rA, $val", RotateShift,
2972       [(set R16C:$rT, (sra R16C:$rA, (i16 uimm7:$val)))]>;
2973
2974 def : Pat<(sra R16C:$rA, (i32 imm:$val)),
2975           (ROTMAHIr16 R16C:$rA, uimm7:$val)>;
2976
2977 def : Pat<(sra R16C:$rA, (i8 imm:$val)),
2978           (ROTMAHIr16 R16C:$rA, uimm7:$val)>;
2979
2980 def ROTMAv4i32:
2981     RRForm<0b01011010000, (outs VECREG:$rT), (ins VECREG:$rA, R32C:$rB),
2982       "rotma\t$rT, $rA, $rB", RotateShift,
2983       [/* see patterns below - $rB must be negated */]>;
2984
2985 def : Pat<(SPUvec_sra VECREG:$rA, R32C:$rB),
2986           (ROTMAv4i32 (v4i32 VECREG:$rA), (SFIr32 R32C:$rB, 0))>;
2987
2988 def : Pat<(SPUvec_sra VECREG:$rA, R16C:$rB),
2989           (ROTMAv4i32 (v4i32 VECREG:$rA),
2990                       (SFIr32 (XSHWr16 R16C:$rB), 0))>;
2991
2992 def : Pat<(SPUvec_sra VECREG:$rA, R8C:$rB),
2993           (ROTMAv4i32 (v4i32 VECREG:$rA),
2994                       (SFIr32 (XSHWr16 (XSBHr8 R8C:$rB)), 0))>;
2995
2996 def ROTMAr32:
2997     RRForm<0b01011010000, (outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
2998       "rotma\t$rT, $rA, $rB", RotateShift,
2999       [/* see patterns below - $rB must be negated */]>;
3000
3001 def : Pat<(sra R32C:$rA, R32C:$rB),
3002           (ROTMAr32 R32C:$rA, (SFIr32 R32C:$rB, 0))>;
3003
3004 def : Pat<(sra R32C:$rA, R16C:$rB),
3005           (ROTMAr32 R32C:$rA,
3006                     (SFIr32 (XSHWr16 R16C:$rB), 0))>;
3007
3008 def : Pat<(sra R32C:$rA, R8C:$rB),
3009           (ROTMAr32 R32C:$rA,
3010                     (SFIr32 (XSHWr16 (XSBHr8 R8C:$rB)), 0))>;
3011
3012 class ROTMAIInst<dag OOL, dag IOL, list<dag> pattern>:
3013     RRForm<0b01011110000, OOL, IOL,
3014       "rotmai\t$rT, $rA, $val",
3015       RotateShift, pattern>;
3016
3017 class ROTMAIVecInst<ValueType vectype, Operand intop, ValueType inttype>:
3018     ROTMAIInst<(outs VECREG:$rT), (ins VECREG:$rA, intop:$val),
3019       [(set (vectype VECREG:$rT),
3020             (SPUvec_sra VECREG:$rA, (inttype uimm7:$val)))]>;
3021
3022 class ROTMAIRegInst<RegisterClass rclass, Operand intop, ValueType inttype>:
3023     ROTMAIInst<(outs rclass:$rT), (ins rclass:$rA, intop:$val),
3024       [(set rclass:$rT, (sra rclass:$rA, (inttype uimm7:$val)))]>;
3025
3026 multiclass RotateMaskAlgebraicImm {
3027   def v2i64_i32 : ROTMAIVecInst<v2i64, rotNeg7imm, i32>;
3028   def v4i32_i32 : ROTMAIVecInst<v4i32, rotNeg7imm, i32>;
3029   def r64_i32 : ROTMAIRegInst<R64C, rotNeg7imm, i32>;
3030   def r32_i32 : ROTMAIRegInst<R32C, rotNeg7imm, i32>;
3031 }
3032
3033 defm ROTMAI : RotateMaskAlgebraicImm;
3034
3035 //===----------------------------------------------------------------------===//
3036 // Branch and conditionals:
3037 //===----------------------------------------------------------------------===//
3038
3039 let isTerminator = 1, isBarrier = 1 in {
3040   // Halt If Equal (r32 preferred slot only, no vector form)
3041   def HEQr32:
3042     RRForm_3<0b00011011110, (outs), (ins R32C:$rA, R32C:$rB),
3043       "heq\t$rA, $rB", BranchResolv,
3044       [/* no pattern to match */]>;
3045
3046   def HEQIr32 :
3047     RI10Form_2<0b11111110, (outs), (ins R32C:$rA, s10imm:$val),
3048       "heqi\t$rA, $val", BranchResolv,
3049       [/* no pattern to match */]>;
3050
3051   // HGT/HGTI: These instructions use signed arithmetic for the comparison,
3052   // contrasting with HLGT/HLGTI, which use unsigned comparison:
3053   def HGTr32:
3054     RRForm_3<0b00011010010, (outs), (ins R32C:$rA, R32C:$rB),
3055       "hgt\t$rA, $rB", BranchResolv,
3056       [/* no pattern to match */]>;
3057
3058   def HGTIr32:
3059     RI10Form_2<0b11110010, (outs), (ins R32C:$rA, s10imm:$val),
3060       "hgti\t$rA, $val", BranchResolv,
3061       [/* no pattern to match */]>;
3062
3063   def HLGTr32:
3064     RRForm_3<0b00011011010, (outs), (ins R32C:$rA, R32C:$rB),
3065       "hlgt\t$rA, $rB", BranchResolv,
3066       [/* no pattern to match */]>;
3067
3068   def HLGTIr32:
3069     RI10Form_2<0b11111010, (outs), (ins R32C:$rA, s10imm:$val),
3070       "hlgti\t$rA, $val", BranchResolv,
3071       [/* no pattern to match */]>;
3072 }
3073
3074 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
3075 // Comparison operators for i8, i16 and i32:
3076 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
3077
3078 class CEQBInst<dag OOL, dag IOL, list<dag> pattern> :
3079   RRForm<0b00001011110, OOL, IOL, "ceqb\t$rT, $rA, $rB",
3080          ByteOp, pattern>;
3081
3082 multiclass CmpEqualByte
3083 {
3084   def v16i8 :
3085     CEQBInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3086       [(set (v16i8 VECREG:$rT), (seteq (v8i16 VECREG:$rA),
3087                                        (v8i16 VECREG:$rB)))]>;
3088
3089   def r8 :
3090     CEQBInst<(outs R8C:$rT), (ins R8C:$rA, R8C:$rB),
3091              [(set R8C:$rT, (seteq R8C:$rA, R8C:$rB))]>;
3092 }
3093
3094 class CEQBIInst<dag OOL, dag IOL, list<dag> pattern> :
3095   RI10Form<0b01111110, OOL, IOL, "ceqbi\t$rT, $rA, $val",
3096            ByteOp, pattern>;
3097
3098 multiclass CmpEqualByteImm
3099 {
3100   def v16i8 :
3101     CEQBIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm_i8:$val),
3102               [(set (v16i8 VECREG:$rT), (seteq (v16i8 VECREG:$rA),
3103                                                v16i8SExt8Imm:$val))]>;
3104   def r8:
3105     CEQBIInst<(outs R8C:$rT), (ins R8C:$rA, s10imm_i8:$val),
3106              [(set R8C:$rT, (seteq R8C:$rA, immSExt8:$val))]>;
3107 }
3108
3109 class CEQHInst<dag OOL, dag IOL, list<dag> pattern> :
3110   RRForm<0b00010011110, OOL, IOL, "ceqh\t$rT, $rA, $rB",
3111          ByteOp, pattern>;
3112
3113 multiclass CmpEqualHalfword
3114 {
3115   def v8i16 : CEQHInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3116                        [(set (v8i16 VECREG:$rT), (seteq (v8i16 VECREG:$rA),
3117                                                         (v8i16 VECREG:$rB)))]>;
3118
3119   def r16 : CEQHInst<(outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
3120                      [(set R16C:$rT, (seteq R16C:$rA, R16C:$rB))]>;
3121 }
3122
3123 class CEQHIInst<dag OOL, dag IOL, list<dag> pattern> :
3124   RI10Form<0b10111110, OOL, IOL, "ceqhi\t$rT, $rA, $val",
3125            ByteOp, pattern>;
3126
3127 multiclass CmpEqualHalfwordImm
3128 {
3129   def v8i16 : CEQHIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
3130                         [(set (v8i16 VECREG:$rT),
3131                               (seteq (v8i16 VECREG:$rA),
3132                                      (v8i16 v8i16SExt10Imm:$val)))]>;
3133   def r16 : CEQHIInst<(outs R16C:$rT), (ins R16C:$rA, s10imm:$val),
3134                       [(set R16C:$rT, (seteq R16C:$rA, i16ImmSExt10:$val))]>;
3135 }
3136
3137 class CEQInst<dag OOL, dag IOL, list<dag> pattern> :
3138   RRForm<0b00000011110, OOL, IOL, "ceq\t$rT, $rA, $rB",
3139          ByteOp, pattern>;
3140
3141 multiclass CmpEqualWord
3142 {
3143   def v4i32 : CEQInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3144                       [(set (v4i32 VECREG:$rT),
3145                             (seteq (v4i32 VECREG:$rA), (v4i32 VECREG:$rB)))]>;
3146
3147   def r32 : CEQInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
3148                     [(set R32C:$rT, (seteq R32C:$rA, R32C:$rB))]>;
3149 }
3150
3151 class CEQIInst<dag OOL, dag IOL, list<dag> pattern> :
3152   RI10Form<0b00111110, OOL, IOL, "ceqi\t$rT, $rA, $val",
3153            ByteOp, pattern>;
3154
3155 multiclass CmpEqualWordImm
3156 {
3157   def v4i32 : CEQIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
3158                        [(set (v4i32 VECREG:$rT),
3159                              (seteq (v4i32 VECREG:$rA),
3160                                     (v4i32 v4i32SExt16Imm:$val)))]>;
3161
3162   def r32: CEQIInst<(outs R32C:$rT), (ins R32C:$rA, s10imm_i32:$val),
3163                     [(set R32C:$rT, (seteq R32C:$rA, i32ImmSExt10:$val))]>;
3164 }
3165
3166 class CGTBInst<dag OOL, dag IOL, list<dag> pattern> :
3167   RRForm<0b00001010010, OOL, IOL, "cgtb\t$rT, $rA, $rB",
3168          ByteOp, pattern>;
3169
3170 multiclass CmpGtrByte
3171 {
3172   def v16i8 :
3173     CGTBInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3174       [(set (v16i8 VECREG:$rT), (setgt (v8i16 VECREG:$rA),
3175                                        (v8i16 VECREG:$rB)))]>;
3176
3177   def r8 :
3178     CGTBInst<(outs R8C:$rT), (ins R8C:$rA, R8C:$rB),
3179              [(set R8C:$rT, (setgt R8C:$rA, R8C:$rB))]>;
3180 }
3181
3182 class CGTBIInst<dag OOL, dag IOL, list<dag> pattern> :
3183   RI10Form<0b01110010, OOL, IOL, "cgtbi\t$rT, $rA, $val",
3184            ByteOp, pattern>;
3185
3186 multiclass CmpGtrByteImm
3187 {
3188   def v16i8 :
3189     CGTBIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm_i8:$val),
3190               [(set (v16i8 VECREG:$rT), (setgt (v16i8 VECREG:$rA),
3191                                                v16i8SExt8Imm:$val))]>;
3192   def r8:
3193     CGTBIInst<(outs R8C:$rT), (ins R8C:$rA, s10imm_i8:$val),
3194               [(set R8C:$rT, (setgt R8C:$rA, immSExt8:$val))]>;
3195 }
3196
3197 class CGTHInst<dag OOL, dag IOL, list<dag> pattern> :
3198   RRForm<0b00010010010, OOL, IOL, "cgth\t$rT, $rA, $rB",
3199          ByteOp, pattern>;
3200
3201 multiclass CmpGtrHalfword
3202 {
3203   def v8i16 : CGTHInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3204                        [(set (v8i16 VECREG:$rT), (setgt (v8i16 VECREG:$rA),
3205                                                         (v8i16 VECREG:$rB)))]>;
3206
3207   def r16 : CGTHInst<(outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
3208                      [(set R16C:$rT, (setgt R16C:$rA, R16C:$rB))]>;
3209 }
3210
3211 class CGTHIInst<dag OOL, dag IOL, list<dag> pattern> :
3212   RI10Form<0b10110010, OOL, IOL, "cgthi\t$rT, $rA, $val",
3213            ByteOp, pattern>;
3214
3215 multiclass CmpGtrHalfwordImm
3216 {
3217   def v8i16 : CGTHIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
3218                         [(set (v8i16 VECREG:$rT),
3219                               (setgt (v8i16 VECREG:$rA),
3220                                      (v8i16 v8i16SExt10Imm:$val)))]>;
3221   def r16 : CGTHIInst<(outs R16C:$rT), (ins R16C:$rA, s10imm:$val),
3222                       [(set R16C:$rT, (setgt R16C:$rA, i16ImmSExt10:$val))]>;
3223 }
3224
3225 class CGTInst<dag OOL, dag IOL, list<dag> pattern> :
3226   RRForm<0b00000010010, OOL, IOL, "cgt\t$rT, $rA, $rB",
3227          ByteOp, pattern>;
3228
3229 multiclass CmpGtrWord
3230 {
3231   def v4i32 : CGTInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3232                       [(set (v4i32 VECREG:$rT),
3233                             (setgt (v4i32 VECREG:$rA), (v4i32 VECREG:$rB)))]>;
3234
3235   def r32 : CGTInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
3236                     [(set R32C:$rT, (setgt R32C:$rA, R32C:$rB))]>;
3237 }
3238
3239 class CGTIInst<dag OOL, dag IOL, list<dag> pattern> :
3240   RI10Form<0b00110010, OOL, IOL, "cgti\t$rT, $rA, $val",
3241            ByteOp, pattern>;
3242
3243 multiclass CmpGtrWordImm
3244 {
3245   def v4i32 : CGTIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
3246                        [(set (v4i32 VECREG:$rT),
3247                              (setgt (v4i32 VECREG:$rA),
3248                                     (v4i32 v4i32SExt16Imm:$val)))]>;
3249
3250   def r32: CGTIInst<(outs R32C:$rT), (ins R32C:$rA, s10imm_i32:$val),
3251                     [(set R32C:$rT, (setgt R32C:$rA, i32ImmSExt10:$val))]>;
3252
3253   // CGTIv4f32, CGTIf32: These are used in the f32 fdiv instruction sequence:
3254   def v4f32: CGTIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
3255                        [(set (v4i32 VECREG:$rT),
3256                              (setgt (v4i32 (bitconvert (v4f32 VECREG:$rA))),
3257                                     (v4i32 v4i32SExt16Imm:$val)))]>;
3258
3259   def f32:   CGTIInst<(outs R32C:$rT), (ins R32FP:$rA, s10imm_i32:$val),
3260                       [/* no pattern */]>;
3261 }
3262
3263 class CLGTBInst<dag OOL, dag IOL, list<dag> pattern> :
3264   RRForm<0b00001011010, OOL, IOL, "clgtb\t$rT, $rA, $rB",
3265          ByteOp, pattern>;
3266
3267 multiclass CmpLGtrByte
3268 {
3269   def v16i8 :
3270     CLGTBInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3271       [(set (v16i8 VECREG:$rT), (setugt (v8i16 VECREG:$rA),
3272                                        (v8i16 VECREG:$rB)))]>;
3273
3274   def r8 :
3275     CLGTBInst<(outs R8C:$rT), (ins R8C:$rA, R8C:$rB),
3276              [(set R8C:$rT, (setugt R8C:$rA, R8C:$rB))]>;
3277 }
3278
3279 class CLGTBIInst<dag OOL, dag IOL, list<dag> pattern> :
3280   RI10Form<0b01111010, OOL, IOL, "clgtbi\t$rT, $rA, $val",
3281            ByteOp, pattern>;
3282
3283 multiclass CmpLGtrByteImm
3284 {
3285   def v16i8 :
3286     CLGTBIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm_i8:$val),
3287               [(set (v16i8 VECREG:$rT), (setugt (v16i8 VECREG:$rA),
3288                                                v16i8SExt8Imm:$val))]>;
3289   def r8:
3290     CLGTBIInst<(outs R8C:$rT), (ins R8C:$rA, s10imm_i8:$val),
3291              [(set R8C:$rT, (setugt R8C:$rA, immSExt8:$val))]>;
3292 }
3293
3294 class CLGTHInst<dag OOL, dag IOL, list<dag> pattern> :
3295   RRForm<0b00010011010, OOL, IOL, "clgth\t$rT, $rA, $rB",
3296          ByteOp, pattern>;
3297
3298 multiclass CmpLGtrHalfword
3299 {
3300   def v8i16 : CLGTHInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3301                        [(set (v8i16 VECREG:$rT), (setugt (v8i16 VECREG:$rA),
3302                                                         (v8i16 VECREG:$rB)))]>;
3303
3304   def r16 : CLGTHInst<(outs R16C:$rT), (ins R16C:$rA, R16C:$rB),
3305                      [(set R16C:$rT, (setugt R16C:$rA, R16C:$rB))]>;
3306 }
3307
3308 class CLGTHIInst<dag OOL, dag IOL, list<dag> pattern> :
3309   RI10Form<0b10111010, OOL, IOL, "clgthi\t$rT, $rA, $val",
3310            ByteOp, pattern>;
3311
3312 multiclass CmpLGtrHalfwordImm
3313 {
3314   def v8i16 : CLGTHIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
3315                          [(set (v8i16 VECREG:$rT),
3316                                (setugt (v8i16 VECREG:$rA),
3317                                        (v8i16 v8i16SExt10Imm:$val)))]>;
3318   def r16 : CLGTHIInst<(outs R16C:$rT), (ins R16C:$rA, s10imm:$val),
3319                        [(set R16C:$rT, (setugt R16C:$rA, i16ImmSExt10:$val))]>;
3320 }
3321
3322 class CLGTInst<dag OOL, dag IOL, list<dag> pattern> :
3323   RRForm<0b00000011010, OOL, IOL, "clgt\t$rT, $rA, $rB",
3324          ByteOp, pattern>;
3325
3326 multiclass CmpLGtrWord
3327 {
3328   def v4i32 : CLGTInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3329                       [(set (v4i32 VECREG:$rT),
3330                             (setugt (v4i32 VECREG:$rA), (v4i32 VECREG:$rB)))]>;
3331
3332   def r32 : CLGTInst<(outs R32C:$rT), (ins R32C:$rA, R32C:$rB),
3333                      [(set R32C:$rT, (setugt R32C:$rA, R32C:$rB))]>;
3334 }
3335
3336 class CLGTIInst<dag OOL, dag IOL, list<dag> pattern> :
3337   RI10Form<0b00111010, OOL, IOL, "clgti\t$rT, $rA, $val",
3338            ByteOp, pattern>;
3339
3340 multiclass CmpLGtrWordImm
3341 {
3342   def v4i32 : CLGTIInst<(outs VECREG:$rT), (ins VECREG:$rA, s10imm:$val),
3343                        [(set (v4i32 VECREG:$rT),
3344                              (setugt (v4i32 VECREG:$rA),
3345                                     (v4i32 v4i32SExt16Imm:$val)))]>;
3346
3347   def r32: CLGTIInst<(outs R32C:$rT), (ins R32C:$rA, s10imm_i32:$val),
3348                      [(set R32C:$rT, (setugt R32C:$rA, i32ImmSExt10:$val))]>;
3349 }
3350
3351 defm CEQB   : CmpEqualByte;
3352 defm CEQBI  : CmpEqualByteImm;
3353 defm CEQH   : CmpEqualHalfword;
3354 defm CEQHI  : CmpEqualHalfwordImm;
3355 defm CEQ    : CmpEqualWord;
3356 defm CEQI   : CmpEqualWordImm;
3357 defm CGTB   : CmpGtrByte;
3358 defm CGTBI  : CmpGtrByteImm;
3359 defm CGTH   : CmpGtrHalfword;
3360 defm CGTHI  : CmpGtrHalfwordImm;
3361 defm CGT    : CmpGtrWord;
3362 defm CGTI   : CmpGtrWordImm;
3363 defm CLGTB  : CmpLGtrByte;
3364 defm CLGTBI : CmpLGtrByteImm;
3365 defm CLGTH  : CmpLGtrHalfword;
3366 defm CLGTHI : CmpLGtrHalfwordImm;
3367 defm CLGT   : CmpLGtrWord;
3368 defm CLGTI  : CmpLGtrWordImm;
3369
3370 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
3371 // For SETCC primitives not supported above (setlt, setle, setge, etc.)
3372 // define a pattern to generate the right code, as a binary operator
3373 // (in a manner of speaking.)
3374 //
3375 // Notes:
3376 // 1. This only matches the setcc set of conditionals. Special pattern
3377 //    matching is used for select conditionals.
3378 //
3379 // 2. The "DAG" versions of these classes is almost exclusively used for
3380 //    i64 comparisons. See the tblgen fundamentals documentation for what
3381 //    ".ResultInstrs[0]" means; see TargetSelectionDAG.td and the Pattern
3382 //    class for where ResultInstrs originates.
3383 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
3384
3385 class SETCCNegCondReg<PatFrag cond, RegisterClass rclass, ValueType inttype,
3386                       SPUInstr xorinst, SPUInstr cmpare>:
3387   Pat<(cond rclass:$rA, rclass:$rB),
3388       (xorinst (cmpare rclass:$rA, rclass:$rB), (inttype -1))>;
3389
3390 class SETCCNegCondImm<PatFrag cond, RegisterClass rclass, ValueType inttype,
3391                       PatLeaf immpred, SPUInstr xorinst, SPUInstr cmpare>:
3392   Pat<(cond rclass:$rA, (inttype immpred:$imm)),
3393       (xorinst (cmpare rclass:$rA, (inttype immpred:$imm)), (inttype -1))>;
3394
3395 def : SETCCNegCondReg<setne, R8C, i8, XORBIr8,  CEQBr8>;
3396 def : SETCCNegCondImm<setne, R8C, i8, immSExt8, XORBIr8, CEQBIr8>;
3397
3398 def : SETCCNegCondReg<setne, R16C, i16, XORHIr16,     CEQHr16>;
3399 def : SETCCNegCondImm<setne, R16C, i16, i16ImmSExt10, XORHIr16, CEQHIr16>;
3400
3401 def : SETCCNegCondReg<setne, R32C, i32, XORIr32, CEQr32>;
3402 def : SETCCNegCondImm<setne, R32C, i32, i32ImmSExt10, XORIr32, CEQIr32>;
3403
3404 class SETCCBinOpReg<PatFrag cond, RegisterClass rclass,
3405                     SPUInstr binop, SPUInstr cmpOp1, SPUInstr cmpOp2>:
3406     Pat<(cond rclass:$rA, rclass:$rB),
3407         (binop (cmpOp1 rclass:$rA, rclass:$rB),
3408                (cmpOp2 rclass:$rA, rclass:$rB))>;
3409
3410 class SETCCBinOpImm<PatFrag cond, RegisterClass rclass, PatLeaf immpred,
3411                     ValueType immtype,
3412                     SPUInstr binop, SPUInstr cmpOp1, SPUInstr cmpOp2>:
3413     Pat<(cond rclass:$rA, (immtype immpred:$imm)),
3414         (binop (cmpOp1 rclass:$rA, (immtype immpred:$imm)),
3415                (cmpOp2 rclass:$rA, (immtype immpred:$imm)))>;
3416
3417 def : SETCCBinOpReg<setge, R8C, ORr8, CGTBr8, CEQBr8>;
3418 def : SETCCBinOpImm<setge, R8C, immSExt8, i8, ORr8, CGTBIr8, CEQBIr8>;
3419 def : SETCCBinOpReg<setlt, R8C, NORr8, CGTBr8, CEQBr8>;
3420 def : SETCCBinOpImm<setlt, R8C, immSExt8, i8, NORr8, CGTBIr8, CEQBIr8>;
3421 def : Pat<(setle R8C:$rA, R8C:$rB),
3422           (XORBIr8 (CGTBr8 R8C:$rA, R8C:$rB), 0xff)>;
3423 def :  Pat<(setle R8C:$rA, immU8:$imm),
3424            (XORBIr8 (CGTBIr8 R8C:$rA, immU8:$imm), 0xff)>;
3425
3426 def : SETCCBinOpReg<setge, R16C, ORr16, CGTHr16, CEQHr16>;
3427 def : SETCCBinOpImm<setge, R16C, i16ImmSExt10, i16,
3428                     ORr16, CGTHIr16, CEQHIr16>;
3429 def : SETCCBinOpReg<setlt, R16C, NORr16, CGTHr16, CEQHr16>;
3430 def : SETCCBinOpImm<setlt, R16C, i16ImmSExt10, i16, NORr16, CGTHIr16, CEQHIr16>;
3431 def : Pat<(setle R16C:$rA, R16C:$rB),
3432           (XORHIr16 (CGTHr16 R16C:$rA, R16C:$rB), 0xffff)>;
3433 def : Pat<(setle R16C:$rA, i16ImmSExt10:$imm),
3434           (XORHIr16 (CGTHIr16 R16C:$rA, i16ImmSExt10:$imm), 0xffff)>;
3435
3436 def : SETCCBinOpReg<setge, R32C, ORr32, CGTr32, CEQr32>;
3437 def : SETCCBinOpImm<setge, R32C, i32ImmSExt10, i32,
3438                     ORr32, CGTIr32, CEQIr32>;
3439 def : SETCCBinOpReg<setlt, R32C, NORr32, CGTr32, CEQr32>;
3440 def : SETCCBinOpImm<setlt, R32C, i32ImmSExt10, i32, NORr32, CGTIr32, CEQIr32>;
3441 def : Pat<(setle R32C:$rA, R32C:$rB),
3442           (XORIr32 (CGTr32 R32C:$rA, R32C:$rB), 0xffffffff)>;
3443 def : Pat<(setle R32C:$rA, i32ImmSExt10:$imm),
3444           (XORIr32 (CGTIr32 R32C:$rA, i32ImmSExt10:$imm), 0xffffffff)>;
3445
3446 def : SETCCBinOpReg<setuge, R8C, ORr8, CLGTBr8, CEQBr8>;
3447 def : SETCCBinOpImm<setuge, R8C, immSExt8, i8, ORr8, CLGTBIr8, CEQBIr8>;
3448 def : SETCCBinOpReg<setult, R8C, NORr8, CLGTBr8, CEQBr8>;
3449 def : SETCCBinOpImm<setult, R8C, immSExt8, i8, NORr8, CLGTBIr8, CEQBIr8>;
3450 def : Pat<(setule R8C:$rA, R8C:$rB),
3451           (XORBIr8 (CLGTBr8 R8C:$rA, R8C:$rB), 0xff)>;
3452 def :  Pat<(setule R8C:$rA, immU8:$imm),
3453            (XORBIr8 (CLGTBIr8 R8C:$rA, immU8:$imm), 0xff)>;
3454
3455 def : SETCCBinOpReg<setuge, R16C, ORr16, CLGTHr16, CEQHr16>;
3456 def : SETCCBinOpImm<setuge, R16C, i16ImmSExt10, i16,
3457                     ORr16, CLGTHIr16, CEQHIr16>;
3458 def : SETCCBinOpReg<setult, R16C, NORr16, CLGTHr16, CEQHr16>;
3459 def : SETCCBinOpImm<setult, R16C, i16ImmSExt10, i16, NORr16,
3460                     CLGTHIr16, CEQHIr16>;
3461 def : Pat<(setule R16C:$rA, R16C:$rB),
3462           (XORHIr16 (CLGTHr16 R16C:$rA, R16C:$rB), 0xffff)>;
3463 def :  Pat<(setule R16C:$rA, i16ImmSExt10:$imm),
3464            (XORHIr16 (CLGTHIr16 R16C:$rA, i16ImmSExt10:$imm), 0xffff)>;
3465
3466 def : SETCCBinOpReg<setuge, R32C, ORr32, CLGTr32, CEQr32>;
3467 def : SETCCBinOpImm<setuge, R32C, i32ImmSExt10, i32,
3468                     ORr32, CLGTIr32, CEQIr32>;
3469 def : SETCCBinOpReg<setult, R32C, NORr32, CLGTr32, CEQr32>;
3470 def : SETCCBinOpImm<setult, R32C, i32ImmSExt10, i32, NORr32, CLGTIr32, CEQIr32>;
3471 def : Pat<(setule R32C:$rA, R32C:$rB),
3472           (XORIr32 (CLGTr32 R32C:$rA, R32C:$rB), 0xffffffff)>;
3473 def : Pat<(setule R32C:$rA, i32ImmSExt10:$imm),
3474           (XORIr32 (CLGTIr32 R32C:$rA, i32ImmSExt10:$imm), 0xffffffff)>;
3475
3476 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
3477 // select conditional patterns:
3478 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
3479
3480 class SELECTNegCondReg<PatFrag cond, RegisterClass rclass, ValueType inttype,
3481                        SPUInstr selinstr, SPUInstr cmpare>:
3482   Pat<(select (inttype (cond rclass:$rA, rclass:$rB)),
3483               rclass:$rTrue, rclass:$rFalse),
3484       (selinstr rclass:$rTrue, rclass:$rFalse,
3485                 (cmpare rclass:$rA, rclass:$rB))>;
3486
3487 class SELECTNegCondImm<PatFrag cond, RegisterClass rclass, ValueType inttype,
3488                        PatLeaf immpred, SPUInstr selinstr, SPUInstr cmpare>:
3489   Pat<(select (inttype (cond rclass:$rA, immpred:$imm)),
3490               rclass:$rTrue, rclass:$rFalse),
3491       (selinstr rclass:$rTrue, rclass:$rFalse,
3492                 (cmpare rclass:$rA, immpred:$imm))>;
3493
3494 def : SELECTNegCondReg<setne, R8C, i8, SELBr8, CEQBr8>;
3495 def : SELECTNegCondImm<setne, R8C, i8, immSExt8, SELBr8, CEQBIr8>;
3496 def : SELECTNegCondReg<setle, R8C, i8, SELBr8, CGTBr8>;
3497 def : SELECTNegCondImm<setle, R8C, i8, immSExt8, SELBr8, CGTBr8>;
3498 def : SELECTNegCondReg<setule, R8C, i8, SELBr8, CLGTBr8>;
3499 def : SELECTNegCondImm<setule, R8C, i8, immU8, SELBr8, CLGTBIr8>;
3500
3501 def : SELECTNegCondReg<setne, R16C, i16, SELBr16, CEQHr16>;
3502 def : SELECTNegCondImm<setne, R16C, i16, i16ImmSExt10, SELBr16, CEQHIr16>;
3503 def : SELECTNegCondReg<setle, R16C, i16, SELBr16, CGTHr16>;
3504 def : SELECTNegCondImm<setle, R16C, i16, i16ImmSExt10, SELBr16, CGTHIr16>;
3505 def : SELECTNegCondReg<setule, R16C, i16, SELBr16, CLGTHr16>;
3506 def : SELECTNegCondImm<setule, R16C, i16, i16ImmSExt10, SELBr16, CLGTHIr16>;
3507
3508 def : SELECTNegCondReg<setne, R32C, i32, SELBr32, CEQr32>;
3509 def : SELECTNegCondImm<setne, R32C, i32, i32ImmSExt10, SELBr32, CEQIr32>;
3510 def : SELECTNegCondReg<setle, R32C, i32, SELBr32, CGTr32>;
3511 def : SELECTNegCondImm<setle, R32C, i32, i32ImmSExt10, SELBr32, CGTIr32>;
3512 def : SELECTNegCondReg<setule, R32C, i32, SELBr32, CLGTr32>;
3513 def : SELECTNegCondImm<setule, R32C, i32, i32ImmSExt10, SELBr32, CLGTIr32>;
3514
3515 class SELECTBinOpReg<PatFrag cond, RegisterClass rclass, ValueType inttype,
3516                      SPUInstr selinstr, SPUInstr binop, SPUInstr cmpOp1,
3517                      SPUInstr cmpOp2>:
3518   Pat<(select (inttype (cond rclass:$rA, rclass:$rB)),
3519               rclass:$rTrue, rclass:$rFalse),
3520       (selinstr rclass:$rFalse, rclass:$rTrue,
3521                 (binop (cmpOp1 rclass:$rA, rclass:$rB),
3522                        (cmpOp2 rclass:$rA, rclass:$rB)))>;
3523
3524 class SELECTBinOpImm<PatFrag cond, RegisterClass rclass, PatLeaf immpred,
3525                      ValueType inttype,
3526                      SPUInstr selinstr, SPUInstr binop, SPUInstr cmpOp1,
3527                      SPUInstr cmpOp2>:
3528     Pat<(select (inttype (cond rclass:$rA, (inttype immpred:$imm))),
3529                 rclass:$rTrue, rclass:$rFalse),
3530         (selinstr rclass:$rFalse, rclass:$rTrue,
3531                   (binop (cmpOp1 rclass:$rA, (inttype immpred:$imm)),
3532                          (cmpOp2 rclass:$rA, (inttype immpred:$imm))))>;
3533
3534 def : SELECTBinOpReg<setge, R8C, i8, SELBr8, ORr8, CGTBr8, CEQBr8>;
3535 def : SELECTBinOpImm<setge, R8C, immSExt8, i8,
3536                      SELBr8, ORr8, CGTBIr8, CEQBIr8>;
3537
3538 def : SELECTBinOpReg<setge, R16C, i16, SELBr16, ORr16, CGTHr16, CEQHr16>;
3539 def : SELECTBinOpImm<setge, R16C, i16ImmSExt10, i16,
3540                      SELBr16, ORr16, CGTHIr16, CEQHIr16>;
3541
3542 def : SELECTBinOpReg<setge, R32C, i32, SELBr32, ORr32, CGTr32, CEQr32>;
3543 def : SELECTBinOpImm<setge, R32C, i32ImmSExt10, i32,
3544                      SELBr32, ORr32, CGTIr32, CEQIr32>;
3545
3546 def : SELECTBinOpReg<setuge, R8C, i8, SELBr8, ORr8, CLGTBr8, CEQBr8>;
3547 def : SELECTBinOpImm<setuge, R8C, immSExt8, i8,
3548                      SELBr8, ORr8, CLGTBIr8, CEQBIr8>;
3549
3550 def : SELECTBinOpReg<setuge, R16C, i16, SELBr16, ORr16, CLGTHr16, CEQHr16>;
3551 def : SELECTBinOpImm<setuge, R16C, i16ImmUns10, i16,
3552                      SELBr16, ORr16, CLGTHIr16, CEQHIr16>;
3553
3554 def : SELECTBinOpReg<setuge, R32C, i32, SELBr32, ORr32, CLGTr32, CEQr32>;
3555 def : SELECTBinOpImm<setuge, R32C, i32ImmUns10, i32,
3556                      SELBr32, ORr32, CLGTIr32, CEQIr32>;
3557
3558 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
3559
3560 let isCall = 1,
3561   // All calls clobber the non-callee-saved registers:
3562   Defs = [R0, R1, R2, R3, R4, R5, R6, R7, R8, R9,
3563           R10,R11,R12,R13,R14,R15,R16,R17,R18,R19,
3564           R20,R21,R22,R23,R24,R25,R26,R27,R28,R29,
3565           R30,R31,R32,R33,R34,R35,R36,R37,R38,R39,
3566           R40,R41,R42,R43,R44,R45,R46,R47,R48,R49,
3567           R50,R51,R52,R53,R54,R55,R56,R57,R58,R59,
3568           R60,R61,R62,R63,R64,R65,R66,R67,R68,R69,
3569           R70,R71,R72,R73,R74,R75,R76,R77,R78,R79],
3570   // All of these instructions use $lr (aka $0)
3571   Uses = [R0]  in {
3572   // Branch relative and set link: Used if we actually know that the target
3573   // is within [-32768, 32767] bytes of the target
3574   def BRSL:
3575     BranchSetLink<0b011001100, (outs), (ins relcalltarget:$func, variable_ops),
3576       "brsl\t$$lr, $func",
3577       [(SPUcall (SPUpcrel tglobaladdr:$func, 0))]>;
3578
3579   // Branch absolute and set link: Used if we actually know that the target
3580   // is an absolute address
3581   def BRASL:
3582     BranchSetLink<0b011001100, (outs), (ins calltarget:$func, variable_ops),
3583       "brasl\t$$lr, $func",
3584       [(SPUcall (SPUaform tglobaladdr:$func, 0))]>;
3585
3586   // Branch indirect and set link if external data. These instructions are not
3587   // actually generated, matched by an intrinsic:
3588   def BISLED_00: BISLEDForm<0b11, "bisled\t$$lr, $func", [/* empty pattern */]>;
3589   def BISLED_E0: BISLEDForm<0b10, "bisled\t$$lr, $func", [/* empty pattern */]>;
3590   def BISLED_0D: BISLEDForm<0b01, "bisled\t$$lr, $func", [/* empty pattern */]>;
3591   def BISLED_ED: BISLEDForm<0b00, "bisled\t$$lr, $func", [/* empty pattern */]>;
3592
3593   // Branch indirect and set link. This is the "X-form" address version of a
3594   // function call
3595   def BISL:
3596     BIForm<0b10010101100, "bisl\t$$lr, $func", [(SPUcall R32C:$func)]>;
3597 }
3598
3599 // Support calls to external symbols:      
3600 def : Pat<(SPUcall (SPUpcrel texternalsym:$func, 0)),
3601           (BRSL texternalsym:$func)>;
3602       
3603 def : Pat<(SPUcall (SPUaform texternalsym:$func, 0)),
3604           (BRASL texternalsym:$func)>;
3605
3606 // Unconditional branches:
3607 let isBranch = 1, isTerminator = 1, hasCtrlDep = 1, isBarrier = 1 in {
3608   def BR :
3609     UncondBranch<0b001001100, (outs), (ins brtarget:$dest),
3610       "br\t$dest",
3611       [(br bb:$dest)]>;
3612
3613   // Unconditional, absolute address branch
3614   def BRA:
3615     UncondBranch<0b001100000, (outs), (ins brtarget:$dest),
3616       "bra\t$dest",
3617       [/* no pattern */]>;
3618
3619   // Indirect branch
3620   def BI:
3621     BIForm<0b00010101100, "bi\t$func", [(brind R32C:$func)]>;
3622
3623   // Conditional branches:
3624   class BRNZInst<dag IOL, list<dag> pattern>:
3625     RI16Form<0b010000100, (outs), IOL, "brnz\t$rCond,$dest",
3626              BranchResolv, pattern>;
3627
3628   class BRNZRegInst<RegisterClass rclass>:
3629     BRNZInst<(ins rclass:$rCond, brtarget:$dest),
3630              [(brcond rclass:$rCond, bb:$dest)]>;
3631
3632   class BRNZVecInst<ValueType vectype>:
3633     BRNZInst<(ins VECREG:$rCond, brtarget:$dest),
3634              [(brcond (vectype VECREG:$rCond), bb:$dest)]>;
3635
3636   multiclass BranchNotZero {
3637     def v4i32 : BRNZVecInst<v4i32>;
3638     def r32   : BRNZRegInst<R32C>;
3639   }
3640
3641   defm BRNZ : BranchNotZero;
3642
3643   class BRZInst<dag IOL, list<dag> pattern>:
3644     RI16Form<0b000000100, (outs), IOL, "brz\t$rT,$dest",
3645              BranchResolv, pattern>;
3646
3647   class BRZRegInst<RegisterClass rclass>:
3648     BRZInst<(ins rclass:$rT, brtarget:$dest), [/* no pattern */]>;
3649
3650   class BRZVecInst<ValueType vectype>:
3651     BRZInst<(ins VECREG:$rT, brtarget:$dest), [/* no pattern */]>;
3652
3653   multiclass BranchZero {
3654     def v4i32: BRZVecInst<v4i32>;
3655     def r32:   BRZRegInst<R32C>;
3656   }
3657
3658   defm BRZ: BranchZero;
3659
3660   // Note: LLVM doesn't do branch conditional, indirect. Otherwise these would
3661   // be useful:
3662   /*
3663   class BINZInst<dag IOL, list<dag> pattern>:
3664    BICondForm<0b10010100100, (outs), IOL, "binz\t$rA, $dest", pattern>;
3665
3666   class BINZRegInst<RegisterClass rclass>:
3667     BINZInst<(ins rclass:$rA, brtarget:$dest),
3668              [(brcond rclass:$rA, R32C:$dest)]>;
3669
3670   class BINZVecInst<ValueType vectype>:
3671     BINZInst<(ins VECREG:$rA, R32C:$dest),
3672              [(brcond (vectype VECREG:$rA), R32C:$dest)]>;
3673
3674   multiclass BranchNotZeroIndirect {
3675     def v4i32: BINZVecInst<v4i32>;
3676     def r32:   BINZRegInst<R32C>;
3677   }
3678
3679   defm BINZ: BranchNotZeroIndirect;
3680
3681   class BIZInst<dag IOL, list<dag> pattern>:
3682     BICondForm<0b00010100100, (outs), IOL, "biz\t$rA, $func", pattern>;
3683
3684   class BIZRegInst<RegisterClass rclass>:
3685     BIZInst<(ins rclass:$rA, R32C:$func), [/* no pattern */]>;
3686
3687   class BIZVecInst<ValueType vectype>:
3688     BIZInst<(ins VECREG:$rA, R32C:$func), [/* no pattern */]>;
3689
3690   multiclass BranchZeroIndirect {
3691     def v4i32: BIZVecInst<v4i32>;
3692     def r32:   BIZRegInst<R32C>;
3693   }
3694
3695   defm BIZ: BranchZeroIndirect;
3696   */
3697
3698   class BRHNZInst<dag IOL, list<dag> pattern>:
3699     RI16Form<0b011000100, (outs), IOL, "brhnz\t$rCond,$dest", BranchResolv,
3700              pattern>;
3701
3702   class BRHNZRegInst<RegisterClass rclass>:
3703     BRHNZInst<(ins rclass:$rCond, brtarget:$dest),
3704               [(brcond rclass:$rCond, bb:$dest)]>;
3705
3706   class BRHNZVecInst<ValueType vectype>:
3707     BRHNZInst<(ins VECREG:$rCond, brtarget:$dest), [/* no pattern */]>;
3708
3709   multiclass BranchNotZeroHalfword {
3710     def v8i16: BRHNZVecInst<v8i16>;
3711     def r16:   BRHNZRegInst<R16C>;
3712   }
3713
3714   defm BRHNZ: BranchNotZeroHalfword;
3715
3716   class BRHZInst<dag IOL, list<dag> pattern>:
3717     RI16Form<0b001000100, (outs), IOL, "brhz\t$rT,$dest", BranchResolv,
3718              pattern>;
3719
3720   class BRHZRegInst<RegisterClass rclass>:
3721     BRHZInst<(ins rclass:$rT, brtarget:$dest), [/* no pattern */]>;
3722
3723   class BRHZVecInst<ValueType vectype>:
3724     BRHZInst<(ins VECREG:$rT, brtarget:$dest), [/* no pattern */]>;
3725
3726   multiclass BranchZeroHalfword {
3727     def v8i16: BRHZVecInst<v8i16>;
3728     def r16:   BRHZRegInst<R16C>;
3729   }
3730
3731   defm BRHZ: BranchZeroHalfword;
3732 }
3733
3734 //===----------------------------------------------------------------------===//
3735 // setcc and brcond patterns:
3736 //===----------------------------------------------------------------------===//
3737
3738 def : Pat<(brcond (i16 (seteq R16C:$rA, 0)), bb:$dest),
3739           (BRHZr16 R16C:$rA, bb:$dest)>;
3740 def : Pat<(brcond (i16 (setne R16C:$rA, 0)), bb:$dest),
3741           (BRHNZr16 R16C:$rA, bb:$dest)>;
3742
3743 def : Pat<(brcond (i32 (seteq R32C:$rA, 0)), bb:$dest),
3744           (BRZr32 R32C:$rA, bb:$dest)>;
3745 def : Pat<(brcond (i32 (setne R32C:$rA, 0)), bb:$dest),
3746           (BRNZr32 R32C:$rA, bb:$dest)>;
3747
3748 multiclass BranchCondEQ<PatFrag cond, SPUInstr brinst16, SPUInstr brinst32>
3749 {
3750   def r16imm: Pat<(brcond (i16 (cond R16C:$rA, i16ImmSExt10:$val)), bb:$dest),
3751                   (brinst16 (CEQHIr16 R16C:$rA, i16ImmSExt10:$val), bb:$dest)>;
3752
3753   def r16 : Pat<(brcond (i16 (cond R16C:$rA, R16C:$rB)), bb:$dest),
3754                 (brinst16 (CEQHr16 R16C:$rA, R16:$rB), bb:$dest)>;
3755
3756   def r32imm : Pat<(brcond (i32 (cond R32C:$rA, i32ImmSExt10:$val)), bb:$dest),
3757                    (brinst32 (CEQIr32 R32C:$rA, i32ImmSExt10:$val), bb:$dest)>;
3758
3759   def r32 : Pat<(brcond (i32 (cond R32C:$rA, R32C:$rB)), bb:$dest),
3760                 (brinst32 (CEQr32 R32C:$rA, R32C:$rB), bb:$dest)>;
3761 }
3762
3763 defm BRCONDeq : BranchCondEQ<seteq, BRHNZr16, BRNZr32>;
3764 defm BRCONDne : BranchCondEQ<setne, BRHZr16, BRZr32>;
3765
3766 multiclass BranchCondLGT<PatFrag cond, SPUInstr brinst16, SPUInstr brinst32>
3767 {
3768   def r16imm : Pat<(brcond (i16 (cond R16C:$rA, i16ImmSExt10:$val)), bb:$dest),
3769                    (brinst16 (CLGTHIr16 R16C:$rA, i16ImmSExt10:$val), bb:$dest)>;
3770
3771   def r16 : Pat<(brcond (i16 (cond R16C:$rA, R16C:$rB)), bb:$dest),
3772                 (brinst16 (CLGTHr16 R16C:$rA, R16:$rB), bb:$dest)>;
3773
3774   def r32imm : Pat<(brcond (i32 (cond R32C:$rA, i32ImmSExt10:$val)), bb:$dest),
3775                    (brinst32 (CLGTIr32 R32C:$rA, i32ImmSExt10:$val), bb:$dest)>;
3776
3777   def r32 : Pat<(brcond (i32 (cond R32C:$rA, R32C:$rB)), bb:$dest),
3778                 (brinst32 (CLGTr32 R32C:$rA, R32C:$rB), bb:$dest)>;
3779 }
3780
3781 defm BRCONDugt : BranchCondLGT<setugt, BRHNZr16, BRNZr32>;
3782 defm BRCONDule : BranchCondLGT<setule, BRHZr16, BRZr32>;
3783
3784 multiclass BranchCondLGTEQ<PatFrag cond, SPUInstr orinst16, SPUInstr brinst16,
3785                            SPUInstr orinst32, SPUInstr brinst32>
3786 {
3787   def r16imm: Pat<(brcond (i16 (cond R16C:$rA, i16ImmSExt10:$val)), bb:$dest),
3788                   (brinst16 (orinst16 (CLGTHIr16 R16C:$rA, i16ImmSExt10:$val),
3789                                       (CEQHIr16 R16C:$rA, i16ImmSExt10:$val)),
3790                             bb:$dest)>;
3791
3792   def r16: Pat<(brcond (i16 (cond R16C:$rA, R16C:$rB)), bb:$dest),
3793                (brinst16 (orinst16 (CLGTHr16 R16C:$rA, R16:$rB),
3794                                    (CEQHr16 R16C:$rA, R16:$rB)),
3795                          bb:$dest)>;
3796
3797   def r32imm : Pat<(brcond (i32 (cond R32C:$rA, i32ImmSExt10:$val)), bb:$dest),
3798                    (brinst32 (orinst32 (CLGTIr32 R32C:$rA, i32ImmSExt10:$val),
3799                                        (CEQIr32 R32C:$rA, i32ImmSExt10:$val)),
3800                              bb:$dest)>;
3801
3802   def r32 : Pat<(brcond (i32 (cond R32C:$rA, R32C:$rB)), bb:$dest),
3803                 (brinst32 (orinst32 (CLGTr32 R32C:$rA, R32C:$rB),
3804                                     (CEQr32 R32C:$rA, R32C:$rB)),
3805                           bb:$dest)>;
3806 }
3807
3808 defm BRCONDuge : BranchCondLGTEQ<setuge, ORr16, BRHNZr16, ORr32, BRNZr32>;
3809 defm BRCONDult : BranchCondLGTEQ<setult, ORr16, BRHZr16, ORr32, BRZr32>;
3810
3811 multiclass BranchCondGT<PatFrag cond, SPUInstr brinst16, SPUInstr brinst32>
3812 {
3813   def r16imm : Pat<(brcond (i16 (cond R16C:$rA, i16ImmSExt10:$val)), bb:$dest),
3814                    (brinst16 (CGTHIr16 R16C:$rA, i16ImmSExt10:$val), bb:$dest)>;
3815
3816   def r16 : Pat<(brcond (i16 (cond R16C:$rA, R16C:$rB)), bb:$dest),
3817                 (brinst16 (CGTHr16 R16C:$rA, R16:$rB), bb:$dest)>;
3818
3819   def r32imm : Pat<(brcond (i32 (cond R32C:$rA, i32ImmSExt10:$val)), bb:$dest),
3820                    (brinst32 (CGTIr32 R32C:$rA, i32ImmSExt10:$val), bb:$dest)>;
3821
3822   def r32 : Pat<(brcond (i32 (cond R32C:$rA, R32C:$rB)), bb:$dest),
3823                 (brinst32 (CGTr32 R32C:$rA, R32C:$rB), bb:$dest)>;
3824 }
3825
3826 defm BRCONDgt : BranchCondGT<setgt, BRHNZr16, BRNZr32>;
3827 defm BRCONDle : BranchCondGT<setle, BRHZr16, BRZr32>;
3828
3829 multiclass BranchCondGTEQ<PatFrag cond, SPUInstr orinst16, SPUInstr brinst16,
3830                           SPUInstr orinst32, SPUInstr brinst32>
3831 {
3832   def r16imm: Pat<(brcond (i16 (cond R16C:$rA, i16ImmSExt10:$val)), bb:$dest),
3833                   (brinst16 (orinst16 (CGTHIr16 R16C:$rA, i16ImmSExt10:$val),
3834                                       (CEQHIr16 R16C:$rA, i16ImmSExt10:$val)),
3835                             bb:$dest)>;
3836
3837   def r16: Pat<(brcond (i16 (cond R16C:$rA, R16C:$rB)), bb:$dest),
3838                (brinst16 (orinst16 (CGTHr16 R16C:$rA, R16:$rB),
3839                                    (CEQHr16 R16C:$rA, R16:$rB)),
3840                          bb:$dest)>;
3841
3842   def r32imm : Pat<(brcond (i32 (cond R32C:$rA, i32ImmSExt10:$val)), bb:$dest),
3843                    (brinst32 (orinst32 (CGTIr32 R32C:$rA, i32ImmSExt10:$val),
3844                                        (CEQIr32 R32C:$rA, i32ImmSExt10:$val)),
3845                              bb:$dest)>;
3846
3847   def r32 : Pat<(brcond (i32 (cond R32C:$rA, R32C:$rB)), bb:$dest),
3848                 (brinst32 (orinst32 (CGTr32 R32C:$rA, R32C:$rB),
3849                                     (CEQr32 R32C:$rA, R32C:$rB)),
3850                           bb:$dest)>;
3851 }
3852
3853 defm BRCONDge : BranchCondGTEQ<setge, ORr16, BRHNZr16, ORr32, BRNZr32>;
3854 defm BRCONDlt : BranchCondGTEQ<setlt, ORr16, BRHZr16, ORr32, BRZr32>;
3855
3856 let isTerminator = 1, isBarrier = 1 in {
3857   let isReturn = 1 in {
3858     def RET:
3859         RETForm<"bi\t$$lr", [(retflag)]>;
3860   }
3861 }
3862
3863 //===----------------------------------------------------------------------===//
3864 // Single precision floating point instructions
3865 //===----------------------------------------------------------------------===//
3866
3867 class FAInst<dag OOL, dag IOL, list<dag> pattern>:
3868     RRForm<0b01011000100, OOL, IOL, "fa\t$rT, $rA, $rB",
3869            SPrecFP, pattern>;
3870
3871 class FAVecInst<ValueType vectype>:
3872     FAInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3873              [(set (vectype VECREG:$rT),
3874                    (fadd (vectype VECREG:$rA), (vectype VECREG:$rB)))]>;
3875
3876 multiclass SFPAdd
3877 {
3878   def v4f32: FAVecInst<v4f32>;
3879   def f32:   FAInst<(outs R32FP:$rT), (ins R32FP:$rA, R32FP:$rB),
3880                     [(set R32FP:$rT, (fadd R32FP:$rA, R32FP:$rB))]>;
3881 }
3882
3883 defm FA : SFPAdd;
3884
3885 class FSInst<dag OOL, dag IOL, list<dag> pattern>:
3886     RRForm<0b01011000100, OOL, IOL, "fs\t$rT, $rA, $rB",
3887            SPrecFP, pattern>;
3888
3889 class FSVecInst<ValueType vectype>:
3890     FSInst<(outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3891            [(set (vectype VECREG:$rT),
3892                  (fsub (vectype VECREG:$rA), (vectype VECREG:$rB)))]>;
3893
3894 multiclass SFPSub
3895 {
3896   def v4f32: FSVecInst<v4f32>;
3897   def f32:   FSInst<(outs R32FP:$rT), (ins R32FP:$rA, R32FP:$rB),
3898                     [(set R32FP:$rT, (fsub R32FP:$rA, R32FP:$rB))]>;
3899 }
3900
3901 defm FS : SFPSub;
3902
3903 // Floating point reciprocal estimate
3904
3905 class FRESTInst<dag OOL, dag IOL>:
3906   RRForm_1<0b00110111000, OOL, IOL,
3907            "frest\t$rT, $rA", SPrecFP,
3908            [/* no pattern */]>;
3909
3910 def FRESTv4f32 :
3911     FRESTInst<(outs VECREG:$rT), (ins VECREG:$rA)>;
3912
3913 def FRESTf32 :
3914     FRESTInst<(outs R32FP:$rT), (ins R32FP:$rA)>;
3915
3916 // Floating point interpolate (used in conjunction with reciprocal estimate)
3917 def FIv4f32 :
3918     RRForm<0b00101011110, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
3919       "fi\t$rT, $rA, $rB", SPrecFP,
3920       [/* no pattern */]>;
3921
3922 def FIf32 :
3923     RRForm<0b00101011110, (outs R32FP:$rT), (ins R32FP:$rA, R32FP:$rB),
3924       "fi\t$rT, $rA, $rB", SPrecFP,
3925       [/* no pattern */]>;
3926
3927 //--------------------------------------------------------------------------
3928 // Basic single precision floating point comparisons:
3929 //
3930 // Note: There is no support on SPU for single precision NaN. Consequently,
3931 // ordered and unordered comparisons are the same.
3932 //--------------------------------------------------------------------------
3933
3934 def FCEQf32 :
3935     RRForm<0b01000011110, (outs R32C:$rT), (ins R32FP:$rA, R32FP:$rB),
3936       "fceq\t$rT, $rA, $rB", SPrecFP,
3937       [(set R32C:$rT, (setueq R32FP:$rA, R32FP:$rB))]>;
3938
3939 def : Pat<(setoeq R32FP:$rA, R32FP:$rB),
3940           (FCEQf32 R32FP:$rA, R32FP:$rB)>;
3941
3942 def FCMEQf32 :
3943     RRForm<0b01010011110, (outs R32C:$rT), (ins R32FP:$rA, R32FP:$rB),
3944       "fcmeq\t$rT, $rA, $rB", SPrecFP,
3945       [(set R32C:$rT, (setueq (fabs R32FP:$rA), (fabs R32FP:$rB)))]>;
3946
3947 def : Pat<(setoeq (fabs R32FP:$rA), (fabs R32FP:$rB)),
3948           (FCMEQf32 R32FP:$rA, R32FP:$rB)>;
3949
3950 def FCGTf32 :
3951     RRForm<0b01000011010, (outs R32C:$rT), (ins R32FP:$rA, R32FP:$rB),
3952       "fcgt\t$rT, $rA, $rB", SPrecFP,
3953       [(set R32C:$rT, (setugt R32FP:$rA, R32FP:$rB))]>;
3954
3955 def : Pat<(setugt R32FP:$rA, R32FP:$rB),
3956           (FCGTf32 R32FP:$rA, R32FP:$rB)>;
3957
3958 def FCMGTf32 :
3959     RRForm<0b01010011010, (outs R32C:$rT), (ins R32FP:$rA, R32FP:$rB),
3960       "fcmgt\t$rT, $rA, $rB", SPrecFP,
3961       [(set R32C:$rT, (setugt (fabs R32FP:$rA), (fabs R32FP:$rB)))]>;
3962
3963 def : Pat<(setugt (fabs R32FP:$rA), (fabs R32FP:$rB)),
3964           (FCMGTf32 R32FP:$rA, R32FP:$rB)>;
3965
3966 //--------------------------------------------------------------------------
3967 // Single precision floating point comparisons and SETCC equivalents:
3968 //--------------------------------------------------------------------------
3969
3970 def : SETCCNegCondReg<setune, R32FP, i32, XORIr32, FCEQf32>;
3971 def : SETCCNegCondReg<setone, R32FP, i32, XORIr32, FCEQf32>;
3972
3973 def : SETCCBinOpReg<setuge, R32FP, ORr32, FCGTf32, FCEQf32>;
3974 def : SETCCBinOpReg<setoge, R32FP, ORr32, FCGTf32, FCEQf32>;
3975
3976 def : SETCCBinOpReg<setult, R32FP, NORr32, FCGTf32, FCEQf32>;
3977 def : SETCCBinOpReg<setolt, R32FP, NORr32, FCGTf32, FCEQf32>;
3978
3979 def : Pat<(setule R32FP:$rA, R32FP:$rB),
3980           (XORIr32 (FCGTf32 R32FP:$rA, R32FP:$rB), 0xffffffff)>;
3981 def : Pat<(setole R32FP:$rA, R32FP:$rB),
3982           (XORIr32 (FCGTf32 R32FP:$rA, R32FP:$rB), 0xffffffff)>;
3983
3984 // FP Status and Control Register Write
3985 // Why isn't rT a don't care in the ISA?
3986 // Should we create a special RRForm_3 for this guy and zero out the rT?
3987 def FSCRWf32 :
3988     RRForm_1<0b01011101110, (outs R32FP:$rT), (ins R32FP:$rA),
3989       "fscrwr\t$rA", SPrecFP,
3990       [/* This instruction requires an intrinsic. Note: rT is unused. */]>;
3991
3992 // FP Status and Control Register Read
3993 def FSCRRf32 :
3994     RRForm_2<0b01011101110, (outs R32FP:$rT), (ins),
3995       "fscrrd\t$rT", SPrecFP,
3996       [/* This instruction requires an intrinsic */]>;
3997
3998 // llvm instruction space
3999 // How do these map onto cell instructions?
4000 // fdiv rA rB
4001 //   frest rC rB        # c = 1/b (both lines)
4002 //   fi rC rB rC
4003 //   fm rD rA rC        # d = a * 1/b
4004 //   fnms rB rD rB rA # b = - (d * b - a) --should == 0 in a perfect world
4005 //   fma rB rB rC rD            # b = b * c + d
4006 //                              = -(d *b -a) * c + d
4007 //                              = a * c - c ( a *b *c - a)
4008
4009 // fcopysign (???)
4010
4011 // Library calls:
4012 // These llvm instructions will actually map to library calls.
4013 // All that's needed, then, is to check that the appropriate library is
4014 // imported and do a brsl to the proper function name.
4015 // frem # fmod(x, y): x - (x/y) * y
4016 // (Note: fmod(double, double), fmodf(float,float)
4017 // fsqrt?
4018 // fsin?
4019 // fcos?
4020 // Unimplemented SPU instruction space
4021 // floating reciprocal absolute square root estimate (frsqest)
4022
4023 // The following are probably just intrinsics
4024 // status and control register write
4025 // status and control register read
4026
4027 //--------------------------------------
4028 // Floating point multiply instructions
4029 //--------------------------------------
4030
4031 def FMv4f32:
4032     RRForm<0b00100011010, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
4033       "fm\t$rT, $rA, $rB", SPrecFP,
4034       [(set (v4f32 VECREG:$rT), (fmul (v4f32 VECREG:$rA),
4035                                       (v4f32 VECREG:$rB)))]>;
4036
4037 def FMf32 :
4038     RRForm<0b01100011010, (outs R32FP:$rT), (ins R32FP:$rA, R32FP:$rB),
4039       "fm\t$rT, $rA, $rB", SPrecFP,
4040       [(set R32FP:$rT, (fmul R32FP:$rA, R32FP:$rB))]>;
4041
4042 // Floating point multiply and add
4043 // e.g. d = c + (a * b)
4044 def FMAv4f32:
4045     RRRForm<0b0111, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
4046       "fma\t$rT, $rA, $rB, $rC", SPrecFP,
4047       [(set (v4f32 VECREG:$rT),
4048             (fadd (v4f32 VECREG:$rC),
4049                   (fmul (v4f32 VECREG:$rA), (v4f32 VECREG:$rB))))]>;
4050
4051 def FMAf32:
4052     RRRForm<0b0111, (outs R32FP:$rT), (ins R32FP:$rA, R32FP:$rB, R32FP:$rC),
4053       "fma\t$rT, $rA, $rB, $rC", SPrecFP,
4054       [(set R32FP:$rT, (fadd R32FP:$rC, (fmul R32FP:$rA, R32FP:$rB)))]>;
4055
4056 // FP multiply and subtract
4057 // Subtracts value in rC from product
4058 // res = a * b - c
4059 def FMSv4f32 :
4060     RRRForm<0b0111, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
4061       "fms\t$rT, $rA, $rB, $rC", SPrecFP,
4062       [(set (v4f32 VECREG:$rT),
4063             (fsub (fmul (v4f32 VECREG:$rA), (v4f32 VECREG:$rB)),
4064                   (v4f32 VECREG:$rC)))]>;
4065
4066 def FMSf32 :
4067     RRRForm<0b0111, (outs R32FP:$rT), (ins R32FP:$rA, R32FP:$rB, R32FP:$rC),
4068       "fms\t$rT, $rA, $rB, $rC", SPrecFP,
4069       [(set R32FP:$rT,
4070             (fsub (fmul R32FP:$rA, R32FP:$rB), R32FP:$rC))]>;
4071
4072 // Floating Negative Mulitply and Subtract
4073 // Subtracts product from value in rC
4074 // res = fneg(fms a b c)
4075 //     = - (a * b - c)
4076 //     = c - a * b
4077 // NOTE: subtraction order
4078 // fsub a b = a - b
4079 // fs a b = b - a?
4080 def FNMSf32 :
4081     RRRForm<0b1101, (outs R32FP:$rT), (ins R32FP:$rA, R32FP:$rB, R32FP:$rC),
4082       "fnms\t$rT, $rA, $rB, $rC", SPrecFP,
4083       [(set R32FP:$rT, (fsub R32FP:$rC, (fmul R32FP:$rA, R32FP:$rB)))]>;
4084
4085 def FNMSv4f32 :
4086     RRRForm<0b1101, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
4087       "fnms\t$rT, $rA, $rB, $rC", SPrecFP,
4088       [(set (v4f32 VECREG:$rT),
4089             (fsub (v4f32 VECREG:$rC),
4090                   (fmul (v4f32 VECREG:$rA),
4091                         (v4f32 VECREG:$rB))))]>;
4092
4093 //--------------------------------------
4094 // Floating Point Conversions
4095 // Signed conversions:
4096 def CSiFv4f32:
4097     CVTIntFPForm<0b0101101110, (outs VECREG:$rT), (ins VECREG:$rA),
4098       "csflt\t$rT, $rA, 0", SPrecFP,
4099       [(set (v4f32 VECREG:$rT), (sint_to_fp (v4i32 VECREG:$rA)))]>;
4100
4101 // Convert signed integer to floating point
4102 def CSiFf32 :
4103     CVTIntFPForm<0b0101101110, (outs R32FP:$rT), (ins R32C:$rA),
4104       "csflt\t$rT, $rA, 0", SPrecFP,
4105       [(set R32FP:$rT, (sint_to_fp R32C:$rA))]>;
4106
4107 // Convert unsigned into to float
4108 def CUiFv4f32 :
4109     CVTIntFPForm<0b1101101110, (outs VECREG:$rT), (ins VECREG:$rA),
4110       "cuflt\t$rT, $rA, 0", SPrecFP,
4111       [(set (v4f32 VECREG:$rT), (uint_to_fp (v4i32 VECREG:$rA)))]>;
4112
4113 def CUiFf32 :
4114     CVTIntFPForm<0b1101101110, (outs R32FP:$rT), (ins R32C:$rA),
4115       "cuflt\t$rT, $rA, 0", SPrecFP,
4116       [(set R32FP:$rT, (uint_to_fp R32C:$rA))]>;
4117
4118 // Convert float to unsigned int
4119 // Assume that scale = 0
4120
4121 def CFUiv4f32 :
4122     CVTIntFPForm<0b1101101110, (outs VECREG:$rT), (ins VECREG:$rA),
4123       "cfltu\t$rT, $rA, 0", SPrecFP,
4124       [(set (v4i32 VECREG:$rT), (fp_to_uint (v4f32 VECREG:$rA)))]>;
4125
4126 def CFUif32 :
4127     CVTIntFPForm<0b1101101110, (outs R32C:$rT), (ins R32FP:$rA),
4128       "cfltu\t$rT, $rA, 0", SPrecFP,
4129       [(set R32C:$rT, (fp_to_uint R32FP:$rA))]>;
4130
4131 // Convert float to signed int
4132 // Assume that scale = 0
4133
4134 def CFSiv4f32 :
4135     CVTIntFPForm<0b1101101110, (outs VECREG:$rT), (ins VECREG:$rA),
4136       "cflts\t$rT, $rA, 0", SPrecFP,
4137       [(set (v4i32 VECREG:$rT), (fp_to_sint (v4f32 VECREG:$rA)))]>;
4138
4139 def CFSif32 :
4140     CVTIntFPForm<0b1101101110, (outs R32C:$rT), (ins R32FP:$rA),
4141       "cflts\t$rT, $rA, 0", SPrecFP,
4142       [(set R32C:$rT, (fp_to_sint R32FP:$rA))]>;
4143
4144 //===----------------------------------------------------------------------==//
4145 // Single<->Double precision conversions
4146 //===----------------------------------------------------------------------==//
4147
4148 // NOTE: We use "vec" name suffix here to avoid confusion (e.g. input is a
4149 // v4f32, output is v2f64--which goes in the name?)
4150
4151 // Floating point extend single to double
4152 // NOTE: Not sure if passing in v4f32 to FESDvec is correct since it
4153 // operates on two double-word slots (i.e. 1st and 3rd fp numbers
4154 // are ignored).
4155 def FESDvec :
4156     RRForm_1<0b00011101110, (outs VECREG:$rT), (ins VECREG:$rA),
4157       "fesd\t$rT, $rA", SPrecFP,
4158       [(set (v2f64 VECREG:$rT), (fextend (v4f32 VECREG:$rA)))]>;
4159
4160 def FESDf32 :
4161     RRForm_1<0b00011101110, (outs R64FP:$rT), (ins R32FP:$rA),
4162       "fesd\t$rT, $rA", SPrecFP,
4163       [(set R64FP:$rT, (fextend R32FP:$rA))]>;
4164
4165 // Floating point round double to single
4166 //def FRDSvec :
4167 //    RRForm_1<0b10011101110, (outs VECREG:$rT), (ins VECREG:$rA),
4168 //      "frds\t$rT, $rA,", SPrecFP,
4169 //      [(set (v4f32 R32FP:$rT), (fround (v2f64 R64FP:$rA)))]>;
4170
4171 def FRDSf64 :
4172     RRForm_1<0b10011101110, (outs R32FP:$rT), (ins R64FP:$rA),
4173       "frds\t$rT, $rA", SPrecFP,
4174       [(set R32FP:$rT, (fround R64FP:$rA))]>;
4175
4176 //ToDo include anyextend?
4177
4178 //===----------------------------------------------------------------------==//
4179 // Double precision floating point instructions
4180 //===----------------------------------------------------------------------==//
4181 def FAf64 :
4182     RRForm<0b00110011010, (outs R64FP:$rT), (ins R64FP:$rA, R64FP:$rB),
4183       "dfa\t$rT, $rA, $rB", DPrecFP,
4184       [(set R64FP:$rT, (fadd R64FP:$rA, R64FP:$rB))]>;
4185
4186 def FAv2f64 :
4187     RRForm<0b00110011010, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
4188       "dfa\t$rT, $rA, $rB", DPrecFP,
4189       [(set (v2f64 VECREG:$rT), (fadd (v2f64 VECREG:$rA), (v2f64 VECREG:$rB)))]>;
4190
4191 def FSf64 :
4192     RRForm<0b10100011010, (outs R64FP:$rT), (ins R64FP:$rA, R64FP:$rB),
4193       "dfs\t$rT, $rA, $rB", DPrecFP,
4194       [(set R64FP:$rT, (fsub R64FP:$rA, R64FP:$rB))]>;
4195
4196 def FSv2f64 :
4197     RRForm<0b10100011010, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
4198       "dfs\t$rT, $rA, $rB", DPrecFP,
4199       [(set (v2f64 VECREG:$rT),
4200             (fsub (v2f64 VECREG:$rA), (v2f64 VECREG:$rB)))]>;
4201
4202 def FMf64 :
4203     RRForm<0b01100011010, (outs R64FP:$rT), (ins R64FP:$rA, R64FP:$rB),
4204       "dfm\t$rT, $rA, $rB", DPrecFP,
4205       [(set R64FP:$rT, (fmul R64FP:$rA, R64FP:$rB))]>;
4206
4207 def FMv2f64:
4208     RRForm<0b00100011010, (outs VECREG:$rT), (ins VECREG:$rA, VECREG:$rB),
4209       "dfm\t$rT, $rA, $rB", DPrecFP,
4210       [(set (v2f64 VECREG:$rT),
4211             (fmul (v2f64 VECREG:$rA), (v2f64 VECREG:$rB)))]>;
4212
4213 def FMAf64:
4214     RRForm<0b00111010110, (outs R64FP:$rT),
4215                           (ins R64FP:$rA, R64FP:$rB, R64FP:$rC),
4216       "dfma\t$rT, $rA, $rB", DPrecFP,
4217       [(set R64FP:$rT, (fadd R64FP:$rC, (fmul R64FP:$rA, R64FP:$rB)))]>,
4218     RegConstraint<"$rC = $rT">,
4219     NoEncode<"$rC">;
4220
4221 def FMAv2f64:
4222     RRForm<0b00111010110, (outs VECREG:$rT),
4223                           (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
4224       "dfma\t$rT, $rA, $rB", DPrecFP,
4225       [(set (v2f64 VECREG:$rT),
4226             (fadd (v2f64 VECREG:$rC),
4227                   (fmul (v2f64 VECREG:$rA), (v2f64 VECREG:$rB))))]>,
4228     RegConstraint<"$rC = $rT">,
4229     NoEncode<"$rC">;
4230
4231 def FMSf64 :
4232     RRForm<0b10111010110, (outs R64FP:$rT),
4233                           (ins R64FP:$rA, R64FP:$rB, R64FP:$rC),
4234       "dfms\t$rT, $rA, $rB", DPrecFP,
4235       [(set R64FP:$rT, (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))]>,
4236     RegConstraint<"$rC = $rT">,
4237     NoEncode<"$rC">;
4238
4239 def FMSv2f64 :
4240     RRForm<0b10111010110, (outs VECREG:$rT),
4241                           (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
4242       "dfms\t$rT, $rA, $rB", DPrecFP,
4243       [(set (v2f64 VECREG:$rT),
4244             (fsub (fmul (v2f64 VECREG:$rA), (v2f64 VECREG:$rB)),
4245                   (v2f64 VECREG:$rC)))]>;
4246
4247 // FNMS: - (a * b - c)
4248 // - (a * b) + c => c - (a * b)
4249 def FNMSf64 :
4250     RRForm<0b01111010110, (outs R64FP:$rT),
4251                           (ins R64FP:$rA, R64FP:$rB, R64FP:$rC),
4252       "dfnms\t$rT, $rA, $rB", DPrecFP,
4253       [(set R64FP:$rT, (fsub R64FP:$rC, (fmul R64FP:$rA, R64FP:$rB)))]>,
4254     RegConstraint<"$rC = $rT">,
4255     NoEncode<"$rC">;
4256
4257 def : Pat<(fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC)),
4258           (FNMSf64 R64FP:$rA, R64FP:$rB, R64FP:$rC)>;
4259
4260 def FNMSv2f64 :
4261     RRForm<0b01111010110, (outs VECREG:$rT),
4262                           (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
4263       "dfnms\t$rT, $rA, $rB", DPrecFP,
4264       [(set (v2f64 VECREG:$rT),
4265             (fsub (v2f64 VECREG:$rC),
4266                   (fmul (v2f64 VECREG:$rA),
4267                         (v2f64 VECREG:$rB))))]>,
4268     RegConstraint<"$rC = $rT">,
4269     NoEncode<"$rC">;
4270
4271 def : Pat<(fneg (fsub (fmul (v2f64 VECREG:$rA), (v2f64 VECREG:$rB)),
4272                 (v2f64 VECREG:$rC))),
4273           (FNMSv2f64 VECREG:$rA, VECREG:$rB, VECREG:$rC)>;
4274
4275 // - (a * b + c)
4276 // - (a * b) - c
4277 def FNMAf64 :
4278     RRForm<0b11111010110, (outs R64FP:$rT),
4279                           (ins R64FP:$rA, R64FP:$rB, R64FP:$rC),
4280       "dfnma\t$rT, $rA, $rB", DPrecFP,
4281       [(set R64FP:$rT, (fneg (fadd R64FP:$rC, (fmul R64FP:$rA, R64FP:$rB))))]>,
4282     RegConstraint<"$rC = $rT">,
4283     NoEncode<"$rC">;
4284
4285 def FNMAv2f64 :
4286     RRForm<0b11111010110, (outs VECREG:$rT),
4287                           (ins VECREG:$rA, VECREG:$rB, VECREG:$rC),
4288       "dfnma\t$rT, $rA, $rB", DPrecFP,
4289       [(set (v2f64 VECREG:$rT),
4290             (fneg (fadd (v2f64 VECREG:$rC),
4291                         (fmul (v2f64 VECREG:$rA),
4292                               (v2f64 VECREG:$rB)))))]>,
4293     RegConstraint<"$rC = $rT">,
4294     NoEncode<"$rC">;
4295
4296 //===----------------------------------------------------------------------==//
4297 // Floating point negation and absolute value
4298 //===----------------------------------------------------------------------==//
4299
4300 def : Pat<(fneg (v4f32 VECREG:$rA)),
4301           (XORfnegvec (v4f32 VECREG:$rA),
4302                       (v4f32 (ILHUv4i32 0x8000)))>;
4303
4304 def : Pat<(fneg R32FP:$rA),
4305           (XORfneg32 R32FP:$rA, (ILHUr32 0x8000))>;
4306
4307 def : Pat<(fneg (v2f64 VECREG:$rA)),
4308           (XORfnegvec (v2f64 VECREG:$rA),
4309                       (v2f64 (ANDBIv16i8 (FSMBIv16i8 0x8080), 0x80)))>;
4310
4311 def : Pat<(fneg R64FP:$rA),
4312           (XORfneg64 R64FP:$rA,
4313                      (ANDBIv16i8 (FSMBIv16i8 0x8080), 0x80))>;
4314
4315 // Floating point absolute value
4316
4317 def : Pat<(fabs R32FP:$rA),
4318           (ANDfabs32 R32FP:$rA, (IOHLr32 (ILHUr32 0x7fff), 0xffff))>;
4319
4320 def : Pat<(fabs (v4f32 VECREG:$rA)),
4321           (ANDfabsvec (v4f32 VECREG:$rA),
4322                       (v4f32 (ANDBIv16i8 (FSMBIv16i8 0xffff), 0x7f)))>;
4323
4324 //===----------------------------------------------------------------------===//
4325 // Hint for branch instructions:
4326 //===----------------------------------------------------------------------===//
4327
4328 /* def HBR : SPUInstr<(outs), (ins), "hbr\t" */
4329
4330 //===----------------------------------------------------------------------===//
4331 // Execution, Load NOP (execute NOPs belong in even pipeline, load NOPs belong
4332 // in the odd pipeline)
4333 //===----------------------------------------------------------------------===//
4334
4335 def ENOP : SPUInstr<(outs), (ins), "enop", ExecNOP> {
4336   let Pattern = [];
4337
4338   let Inst{0-10} = 0b10000000010;
4339   let Inst{11-17} = 0;
4340   let Inst{18-24} = 0;
4341   let Inst{25-31} = 0;
4342 }
4343
4344 def LNOP : SPUInstr<(outs), (ins), "lnop", LoadNOP> {
4345   let Pattern = [];
4346
4347   let Inst{0-10} = 0b10000000000;
4348   let Inst{11-17} = 0;
4349   let Inst{18-24} = 0;
4350   let Inst{25-31} = 0;
4351 }
4352
4353 //===----------------------------------------------------------------------===//
4354 // Bit conversions (type conversions between vector/packed types)
4355 // NOTE: Promotions are handled using the XS* instructions. Truncation
4356 // is not handled.
4357 //===----------------------------------------------------------------------===//
4358 def : Pat<(v16i8 (bitconvert (v8i16 VECREG:$src))), (v16i8 VECREG:$src)>;
4359 def : Pat<(v16i8 (bitconvert (v4i32 VECREG:$src))), (v16i8 VECREG:$src)>;
4360 def : Pat<(v16i8 (bitconvert (v2i64 VECREG:$src))), (v16i8 VECREG:$src)>;
4361 def : Pat<(v16i8 (bitconvert (v4f32 VECREG:$src))), (v16i8 VECREG:$src)>;
4362 def : Pat<(v16i8 (bitconvert (v2f64 VECREG:$src))), (v16i8 VECREG:$src)>;
4363
4364 def : Pat<(v8i16 (bitconvert (v16i8 VECREG:$src))), (v8i16 VECREG:$src)>;
4365 def : Pat<(v8i16 (bitconvert (v4i32 VECREG:$src))), (v8i16 VECREG:$src)>;
4366 def : Pat<(v8i16 (bitconvert (v2i64 VECREG:$src))), (v8i16 VECREG:$src)>;
4367 def : Pat<(v8i16 (bitconvert (v4f32 VECREG:$src))), (v8i16 VECREG:$src)>;
4368 def : Pat<(v8i16 (bitconvert (v2f64 VECREG:$src))), (v8i16 VECREG:$src)>;
4369
4370 def : Pat<(v4i32 (bitconvert (v16i8 VECREG:$src))), (v4i32 VECREG:$src)>;
4371 def : Pat<(v4i32 (bitconvert (v8i16 VECREG:$src))), (v4i32 VECREG:$src)>;
4372 def : Pat<(v4i32 (bitconvert (v2i64 VECREG:$src))), (v4i32 VECREG:$src)>;
4373 def : Pat<(v4i32 (bitconvert (v4f32 VECREG:$src))), (v4i32 VECREG:$src)>;
4374 def : Pat<(v4i32 (bitconvert (v2f64 VECREG:$src))), (v4i32 VECREG:$src)>;
4375
4376 def : Pat<(v2i64 (bitconvert (v16i8 VECREG:$src))), (v2i64 VECREG:$src)>;
4377 def : Pat<(v2i64 (bitconvert (v8i16 VECREG:$src))), (v2i64 VECREG:$src)>;
4378 def : Pat<(v2i64 (bitconvert (v4i32 VECREG:$src))), (v2i64 VECREG:$src)>;
4379 def : Pat<(v2i64 (bitconvert (v4f32 VECREG:$src))), (v2i64 VECREG:$src)>;
4380 def : Pat<(v2i64 (bitconvert (v2f64 VECREG:$src))), (v2i64 VECREG:$src)>;
4381
4382 def : Pat<(v4f32 (bitconvert (v16i8 VECREG:$src))), (v4f32 VECREG:$src)>;
4383 def : Pat<(v4f32 (bitconvert (v8i16 VECREG:$src))), (v4f32 VECREG:$src)>;
4384 def : Pat<(v4f32 (bitconvert (v2i64 VECREG:$src))), (v4f32 VECREG:$src)>;
4385 def : Pat<(v4f32 (bitconvert (v4i32 VECREG:$src))), (v4f32 VECREG:$src)>;
4386 def : Pat<(v4f32 (bitconvert (v2f64 VECREG:$src))), (v4f32 VECREG:$src)>;
4387
4388 def : Pat<(v2f64 (bitconvert (v16i8 VECREG:$src))), (v2f64 VECREG:$src)>;
4389 def : Pat<(v2f64 (bitconvert (v8i16 VECREG:$src))), (v2f64 VECREG:$src)>;
4390 def : Pat<(v2f64 (bitconvert (v4i32 VECREG:$src))), (v2f64 VECREG:$src)>;
4391 def : Pat<(v2f64 (bitconvert (v2i64 VECREG:$src))), (v2f64 VECREG:$src)>;
4392 def : Pat<(v2f64 (bitconvert (v2f64 VECREG:$src))), (v2f64 VECREG:$src)>;
4393
4394 def : Pat<(f32 (bitconvert (i32 R32C:$src))), (f32 R32FP:$src)>;
4395 def : Pat<(f64 (bitconvert (i64 R64C:$src))), (f64 R64FP:$src)>;
4396
4397 //===----------------------------------------------------------------------===//
4398 // Instruction patterns:
4399 //===----------------------------------------------------------------------===//
4400
4401 // General 32-bit constants:
4402 def : Pat<(i32 imm:$imm),
4403           (IOHLr32 (ILHUr32 (HI16 imm:$imm)), (LO16 imm:$imm))>;
4404
4405 // Single precision float constants:
4406 def : Pat<(f32 fpimm:$imm),
4407           (IOHLf32 (ILHUf32 (HI16_f32 fpimm:$imm)), (LO16_f32 fpimm:$imm))>;
4408
4409 // General constant 32-bit vectors
4410 def : Pat<(v4i32 v4i32Imm:$imm),
4411           (IOHLv4i32 (v4i32 (ILHUv4i32 (HI16_vec v4i32Imm:$imm))),
4412                      (LO16_vec v4i32Imm:$imm))>;
4413
4414 // 8-bit constants
4415 def : Pat<(i8 imm:$imm),
4416           (ILHr8 imm:$imm)>;
4417
4418 //===----------------------------------------------------------------------===//
4419 // Call instruction patterns:
4420 //===----------------------------------------------------------------------===//
4421 // Return void
4422 def : Pat<(ret),
4423           (RET)>;
4424
4425 //===----------------------------------------------------------------------===//
4426 // Zero/Any/Sign extensions
4427 //===----------------------------------------------------------------------===//
4428
4429 // sext 8->32: Sign extend bytes to words
4430 def : Pat<(sext_inreg R32C:$rSrc, i8),
4431           (XSHWr32 (XSBHr32 R32C:$rSrc))>;
4432
4433 def : Pat<(i32 (sext R8C:$rSrc)),
4434           (XSHWr16 (XSBHr8 R8C:$rSrc))>;
4435
4436 // sext 8->64: Sign extend bytes to double word
4437 def : Pat<(sext_inreg R64C:$rSrc, i8),
4438           (XSWDr64_inreg (XSHWr64 (XSBHr64 R64C:$rSrc)))>;
4439           
4440 def : Pat<(i64 (sext R8C:$rSrc)),
4441           (XSWDr64 (XSHWr16 (XSBHr8 R8C:$rSrc)))>;
4442
4443 // zext 8->16: Zero extend bytes to halfwords
4444 def : Pat<(i16 (zext R8C:$rSrc)),
4445           (ANDHIi8i16 R8C:$rSrc, 0xff)>;
4446
4447 // zext 8->32: Zero extend bytes to words
4448 def : Pat<(i32 (zext R8C:$rSrc)),
4449           (ANDIi8i32 R8C:$rSrc, 0xff)>;
4450
4451 // zext 8->64: Zero extend bytes to double words
4452 def : Pat<(i64 (zext R8C:$rSrc)),
4453           (ORi64_v2i64 (SELBv4i32 (ROTQMBYv4i32
4454                                     (ORv4i32_i32 (ANDIi8i32 R8C:$rSrc, 0xff)),
4455                                     0x4),
4456                                   (ILv4i32 0x0),
4457                                   (FSMBIv4i32 0x0f0f)))>;
4458
4459 // anyext 8->16: Extend 8->16 bits, irrespective of sign, preserves high bits
4460 def : Pat<(i16 (anyext R8C:$rSrc)),
4461           (ORHIi8i16 R8C:$rSrc, 0)>;
4462
4463 // anyext 8->32: Extend 8->32 bits, irrespective of sign, preserves high bits
4464 def : Pat<(i32 (anyext R8C:$rSrc)),
4465           (ORIi8i32 R8C:$rSrc, 0)>;
4466
4467 // sext 16->64: Sign extend halfword to double word
4468 def : Pat<(sext_inreg R64C:$rSrc, i16),
4469           (XSWDr64_inreg (XSHWr64 R64C:$rSrc))>;
4470           
4471 def : Pat<(sext R16C:$rSrc),
4472           (XSWDr64 (XSHWr16 R16C:$rSrc))>;
4473
4474 // zext 16->32: Zero extend halfwords to words
4475 def : Pat<(i32 (zext R16C:$rSrc)),
4476           (ANDi16i32 R16C:$rSrc, (ILAr32 0xffff))>;
4477
4478 def : Pat<(i32 (zext (and R16C:$rSrc, 0xf))),
4479           (ANDIi16i32 R16C:$rSrc, 0xf)>;
4480
4481 def : Pat<(i32 (zext (and R16C:$rSrc, 0xff))),
4482           (ANDIi16i32 R16C:$rSrc, 0xff)>;
4483
4484 def : Pat<(i32 (zext (and R16C:$rSrc, 0xfff))),
4485           (ANDIi16i32 R16C:$rSrc, 0xfff)>;
4486
4487 // anyext 16->32: Extend 16->32 bits, irrespective of sign
4488 def : Pat<(i32 (anyext R16C:$rSrc)),
4489           (ORIi16i32 R16C:$rSrc, 0)>;
4490
4491 //===----------------------------------------------------------------------===//
4492 // Truncates:
4493 // These truncates are for the SPU's supported types (i8, i16, i32). i64 and
4494 // above are custom lowered.
4495 //===----------------------------------------------------------------------===//
4496
4497 def : Pat<(i8 (trunc GPRC:$src)),
4498           (ORi8_v16i8
4499             (SHUFBgprc GPRC:$src, GPRC:$src,
4500                        (IOHLv4i32 (ILHUv4i32 0x0f0f), 0x0f0f)))>;
4501
4502 def : Pat<(i8 (trunc R64C:$src)),
4503           (ORi8_v16i8
4504             (SHUFBv2i64_m32
4505               (ORv2i64_i64 R64C:$src),
4506               (ORv2i64_i64 R64C:$src),
4507               (IOHLv4i32 (ILHUv4i32 0x0707), 0x0707)))>;
4508
4509 def : Pat<(i8 (trunc R32C:$src)),
4510           (ORi8_v16i8
4511             (SHUFBv4i32_m32
4512                (ORv4i32_i32 R32C:$src),
4513                (ORv4i32_i32 R32C:$src),
4514                (IOHLv4i32 (ILHUv4i32 0x0303), 0x0303)))>;
4515
4516 def : Pat<(i8 (trunc R16C:$src)),
4517           (ORi8_v16i8
4518             (SHUFBv4i32_m32
4519                (ORv8i16_i16 R16C:$src),
4520                (ORv8i16_i16 R16C:$src),
4521                (IOHLv4i32 (ILHUv4i32 0x0303), 0x0303)))>;
4522
4523 def : Pat<(i16 (trunc GPRC:$src)),
4524           (ORi16_v8i16
4525             (SHUFBgprc GPRC:$src, GPRC:$src,
4526                        (IOHLv4i32 (ILHUv4i32 0x0e0f), 0x0e0f)))>;
4527
4528 def : Pat<(i16 (trunc R64C:$src)),
4529           (ORi16_v8i16
4530             (SHUFBv2i64_m32
4531               (ORv2i64_i64 R64C:$src),
4532               (ORv2i64_i64 R64C:$src),
4533               (IOHLv4i32 (ILHUv4i32 0x0607), 0x0607)))>;
4534
4535 def : Pat<(i16 (trunc R32C:$src)),
4536           (ORi16_v8i16
4537             (SHUFBv4i32_m32
4538                (ORv4i32_i32 R32C:$src),
4539                (ORv4i32_i32 R32C:$src),
4540                (IOHLv4i32 (ILHUv4i32 0x0203), 0x0203)))>;
4541
4542 def : Pat<(i32 (trunc GPRC:$src)),
4543           (ORi32_v4i32
4544             (SHUFBgprc GPRC:$src, GPRC:$src,
4545                        (IOHLv4i32 (ILHUv4i32 0x0c0d), 0x0e0f)))>;
4546
4547 def : Pat<(i32 (trunc R64C:$src)),
4548           (ORi32_v4i32
4549             (SHUFBv2i64_m32
4550               (ORv2i64_i64 R64C:$src),
4551               (ORv2i64_i64 R64C:$src),
4552               (IOHLv4i32 (ILHUv4i32 0x0405), 0x0607)))>;
4553
4554 //===----------------------------------------------------------------------===//
4555 // Address generation: SPU, like PPC, has to split addresses into high and
4556 // low parts in order to load them into a register.
4557 //===----------------------------------------------------------------------===//
4558
4559 def : Pat<(SPUaform tglobaladdr:$in, 0),  (ILAlsa tglobaladdr:$in)>;
4560 def : Pat<(SPUaform texternalsym:$in, 0), (ILAlsa texternalsym:$in)>;
4561 def : Pat<(SPUaform tjumptable:$in, 0),   (ILAlsa tjumptable:$in)>;
4562 def : Pat<(SPUaform tconstpool:$in, 0),   (ILAlsa  tconstpool:$in)>;
4563
4564 def : Pat<(SPUindirect (SPUhi tglobaladdr:$in, 0),
4565                        (SPUlo tglobaladdr:$in, 0)),
4566           (IOHLlo (ILHUhi tglobaladdr:$in), tglobaladdr:$in)>;
4567
4568 def : Pat<(SPUindirect (SPUhi texternalsym:$in, 0),
4569                        (SPUlo texternalsym:$in, 0)),
4570           (IOHLlo (ILHUhi texternalsym:$in), texternalsym:$in)>;
4571
4572 def : Pat<(SPUindirect (SPUhi tjumptable:$in, 0),
4573                        (SPUlo tjumptable:$in, 0)),
4574           (IOHLlo (ILHUhi tjumptable:$in), tjumptable:$in)>;
4575
4576 def : Pat<(SPUindirect (SPUhi tconstpool:$in, 0),
4577                        (SPUlo tconstpool:$in, 0)),
4578           (IOHLlo (ILHUhi tconstpool:$in), tconstpool:$in)>;
4579
4580 def : Pat<(add (SPUhi tglobaladdr:$in, 0), (SPUlo tglobaladdr:$in, 0)),
4581           (IOHLlo (ILHUhi tglobaladdr:$in), tglobaladdr:$in)>;
4582
4583 def : Pat<(add (SPUhi texternalsym:$in, 0), (SPUlo texternalsym:$in, 0)),
4584           (IOHLlo (ILHUhi texternalsym:$in), texternalsym:$in)>;
4585
4586 def : Pat<(add (SPUhi tjumptable:$in, 0), (SPUlo tjumptable:$in, 0)),
4587           (IOHLlo (ILHUhi tjumptable:$in), tjumptable:$in)>;
4588
4589 def : Pat<(add (SPUhi tconstpool:$in, 0), (SPUlo tconstpool:$in, 0)),
4590           (IOHLlo (ILHUhi tconstpool:$in), tconstpool:$in)>;
4591
4592 // Instrinsics:
4593 include "CellSDKIntrinsics.td"
4594 // Various math operator instruction sequences
4595 include "SPUMathInstr.td"
4596 // 64-bit "instructions"/support
4597 include "SPU64InstrInfo.td"
4598 // 128-bit "instructions"/support
4599 include "SPU128InstrInfo.td"