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