dc56f39240db7a836f27d052f6872d254b22bd87
[oota-llvm.git] / lib / Target / Hexagon / HexagonInstrInfo.td
1 //==- HexagonInstrInfo.td - Target Description for Hexagon -*- 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 //
10 // This file describes the Hexagon instructions in TableGen format.
11 //
12 //===----------------------------------------------------------------------===//
13
14 include "HexagonInstrFormats.td"
15 include "HexagonOperands.td"
16
17 // Pattern fragment that combines the value type and the register class
18 // into a single parameter.
19 // The pat frags in the definitions below need to have a named register,
20 // otherwise i32 will be assumed regardless of the register class. The
21 // name of the register does not matter.
22 def I1  : PatLeaf<(i1 PredRegs:$R)>;
23 def I32 : PatLeaf<(i32 IntRegs:$R)>;
24 def I64 : PatLeaf<(i64 DoubleRegs:$R)>;
25 def F32 : PatLeaf<(f32 IntRegs:$R)>;
26 def F64 : PatLeaf<(f64 DoubleRegs:$R)>;
27
28 // Pattern fragments to extract the low and high subregisters from a
29 // 64-bit value.
30 def LoReg: OutPatFrag<(ops node:$Rs),
31                       (EXTRACT_SUBREG (i64 $Rs), subreg_loreg)>;
32
33 //===----------------------------------------------------------------------===//
34
35 //===----------------------------------------------------------------------===//
36 // Compare
37 //===----------------------------------------------------------------------===//
38 let hasSideEffects = 0, isCompare = 1, InputType = "imm", isExtendable = 1,
39     opExtendable = 2 in
40 class T_CMP <string mnemonic, bits<2> MajOp, bit isNot, Operand ImmOp>
41   : ALU32Inst <(outs PredRegs:$dst),
42                (ins IntRegs:$src1, ImmOp:$src2),
43   "$dst = "#!if(isNot, "!","")#mnemonic#"($src1, #$src2)",
44   [], "",ALU32_2op_tc_2early_SLOT0123 >, ImmRegRel {
45     bits<2> dst;
46     bits<5> src1;
47     bits<10> src2;
48     let CextOpcode = mnemonic;
49     let opExtentBits  = !if(!eq(mnemonic, "cmp.gtu"), 9, 10);
50     let isExtentSigned = !if(!eq(mnemonic, "cmp.gtu"), 0, 1);
51
52     let IClass = 0b0111;
53
54     let Inst{27-24} = 0b0101;
55     let Inst{23-22} = MajOp;
56     let Inst{21}    = !if(!eq(mnemonic, "cmp.gtu"), 0, src2{9});
57     let Inst{20-16} = src1;
58     let Inst{13-5}  = src2{8-0};
59     let Inst{4}     = isNot;
60     let Inst{3-2}   = 0b00;
61     let Inst{1-0}   = dst;
62   }
63
64 def C2_cmpeqi   : T_CMP <"cmp.eq",  0b00, 0, s10Ext>;
65 def C2_cmpgti   : T_CMP <"cmp.gt",  0b01, 0, s10Ext>;
66 def C2_cmpgtui  : T_CMP <"cmp.gtu", 0b10, 0, u9Ext>;
67
68 class T_CMP_pat <InstHexagon MI, PatFrag OpNode, PatLeaf ImmPred>
69   : Pat<(i1 (OpNode (i32 IntRegs:$src1), ImmPred:$src2)),
70         (MI IntRegs:$src1, ImmPred:$src2)>;
71
72 def : T_CMP_pat <C2_cmpeqi,  seteq,  s10ImmPred>;
73 def : T_CMP_pat <C2_cmpgti,  setgt,  s10ImmPred>;
74 def : T_CMP_pat <C2_cmpgtui, setugt, u9ImmPred>;
75
76 //===----------------------------------------------------------------------===//
77 // ALU32/ALU +
78 //===----------------------------------------------------------------------===//
79 def SDTHexagonI64I32I32 : SDTypeProfile<1, 2,
80   [SDTCisVT<0, i64>, SDTCisVT<1, i32>, SDTCisSameAs<1, 2>]>;
81
82 def HexagonCOMBINE : SDNode<"HexagonISD::COMBINE", SDTHexagonI64I32I32>;
83
84 let hasSideEffects = 0, hasNewValue = 1, InputType = "reg" in
85 class T_ALU32_3op<string mnemonic, bits<3> MajOp, bits<3> MinOp, bit OpsRev,
86                   bit IsComm>
87   : ALU32_rr<(outs IntRegs:$Rd), (ins IntRegs:$Rs, IntRegs:$Rt),
88              "$Rd = "#mnemonic#"($Rs, $Rt)",
89              [], "", ALU32_3op_tc_1_SLOT0123>, ImmRegRel, PredRel {
90   let isCommutable = IsComm;
91   let BaseOpcode = mnemonic#_rr;
92   let CextOpcode = mnemonic;
93
94   bits<5> Rs;
95   bits<5> Rt;
96   bits<5> Rd;
97
98   let IClass = 0b1111;
99   let Inst{27} = 0b0;
100   let Inst{26-24} = MajOp;
101   let Inst{23-21} = MinOp;
102   let Inst{20-16} = !if(OpsRev,Rt,Rs);
103   let Inst{12-8} = !if(OpsRev,Rs,Rt);
104   let Inst{4-0} = Rd;
105 }
106
107 let hasSideEffects = 0, hasNewValue = 1 in
108 class T_ALU32_3op_pred<string mnemonic, bits<3> MajOp, bits<3> MinOp,
109                        bit OpsRev, bit PredNot, bit PredNew>
110   : ALU32_rr<(outs IntRegs:$Rd), (ins PredRegs:$Pu, IntRegs:$Rs, IntRegs:$Rt),
111              "if ("#!if(PredNot,"!","")#"$Pu"#!if(PredNew,".new","")#") "#
112              "$Rd = "#mnemonic#"($Rs, $Rt)",
113              [], "", ALU32_3op_tc_1_SLOT0123>, ImmRegRel, PredNewRel {
114   let isPredicated = 1;
115   let isPredicatedFalse = PredNot;
116   let isPredicatedNew = PredNew;
117   let BaseOpcode = mnemonic#_rr;
118   let CextOpcode = mnemonic;
119
120   bits<2> Pu;
121   bits<5> Rs;
122   bits<5> Rt;
123   bits<5> Rd;
124
125   let IClass = 0b1111;
126   let Inst{27} = 0b1;
127   let Inst{26-24} = MajOp;
128   let Inst{23-21} = MinOp;
129   let Inst{20-16} = !if(OpsRev,Rt,Rs);
130   let Inst{13} = PredNew;
131   let Inst{12-8} = !if(OpsRev,Rs,Rt);
132   let Inst{7} = PredNot;
133   let Inst{6-5} = Pu;
134   let Inst{4-0} = Rd;
135 }
136
137 class T_ALU32_combineh<string Op1, string Op2, bits<3> MajOp, bits<3> MinOp,
138                       bit OpsRev>
139   : T_ALU32_3op<"", MajOp, MinOp, OpsRev, 0> {
140   let AsmString = "$Rd = combine($Rs"#Op1#", $Rt"#Op2#")";
141 }
142
143 let isCodeGenOnly = 0 in {
144 def A2_combine_hh : T_ALU32_combineh<".h", ".h", 0b011, 0b100, 1>;
145 def A2_combine_hl : T_ALU32_combineh<".h", ".l", 0b011, 0b101, 1>;
146 def A2_combine_lh : T_ALU32_combineh<".l", ".h", 0b011, 0b110, 1>;
147 def A2_combine_ll : T_ALU32_combineh<".l", ".l", 0b011, 0b111, 1>;
148 }
149
150 class T_ALU32_3op_sfx<string mnemonic, string suffix, bits<3> MajOp,
151                       bits<3> MinOp, bit OpsRev, bit IsComm>
152   : T_ALU32_3op<"", MajOp, MinOp, OpsRev, IsComm> {
153   let AsmString = "$Rd = "#mnemonic#"($Rs, $Rt)"#suffix;
154 }
155
156 let Defs = [USR_OVF], Itinerary = ALU32_3op_tc_2_SLOT0123, 
157     isCodeGenOnly = 0 in {
158   def A2_addsat   : T_ALU32_3op_sfx<"add",    ":sat", 0b110, 0b010, 0, 1>;
159   def A2_subsat   : T_ALU32_3op_sfx<"sub",    ":sat", 0b110, 0b110, 1, 0>;
160 }
161
162 multiclass T_ALU32_3op_p<string mnemonic, bits<3> MajOp, bits<3> MinOp,
163                          bit OpsRev> {
164   def t    : T_ALU32_3op_pred<mnemonic, MajOp, MinOp, OpsRev, 0, 0>;
165   def f    : T_ALU32_3op_pred<mnemonic, MajOp, MinOp, OpsRev, 1, 0>;
166   def tnew : T_ALU32_3op_pred<mnemonic, MajOp, MinOp, OpsRev, 0, 1>;
167   def fnew : T_ALU32_3op_pred<mnemonic, MajOp, MinOp, OpsRev, 1, 1>;
168 }
169
170 multiclass T_ALU32_3op_A2<string mnemonic, bits<3> MajOp, bits<3> MinOp,
171                           bit OpsRev, bit IsComm> {
172   let isPredicable = 1 in
173   def  A2_#NAME  : T_ALU32_3op  <mnemonic, MajOp, MinOp, OpsRev, IsComm>;
174   defm A2_p#NAME : T_ALU32_3op_p<mnemonic, MajOp, MinOp, OpsRev>;
175 }
176
177 let isCodeGenOnly = 0 in {
178 defm add : T_ALU32_3op_A2<"add", 0b011, 0b000, 0, 1>;
179 defm and : T_ALU32_3op_A2<"and", 0b001, 0b000, 0, 1>;
180 defm or  : T_ALU32_3op_A2<"or",  0b001, 0b001, 0, 1>;
181 defm sub : T_ALU32_3op_A2<"sub", 0b011, 0b001, 1, 0>;
182 defm xor : T_ALU32_3op_A2<"xor", 0b001, 0b011, 0, 1>;
183 }
184
185 // Pats for instruction selection.
186 class BinOp32_pat<SDNode Op, InstHexagon MI, ValueType ResT>
187   : Pat<(ResT (Op (i32 IntRegs:$Rs), (i32 IntRegs:$Rt))),
188         (ResT (MI IntRegs:$Rs, IntRegs:$Rt))>;
189
190 def: BinOp32_pat<add, A2_add, i32>;
191 def: BinOp32_pat<and, A2_and, i32>;
192 def: BinOp32_pat<or,  A2_or,  i32>;
193 def: BinOp32_pat<sub, A2_sub, i32>;
194 def: BinOp32_pat<xor, A2_xor, i32>;
195
196 // A few special cases producing register pairs:
197 let OutOperandList = (outs DoubleRegs:$Rd), hasNewValue = 0,
198     isCodeGenOnly = 0 in {
199   def S2_packhl    : T_ALU32_3op  <"packhl",  0b101, 0b100, 0, 0>;
200
201   let isPredicable = 1 in
202     def A2_combinew  : T_ALU32_3op  <"combine", 0b101, 0b000, 0, 0>;
203
204   // Conditional combinew uses "newt/f" instead of "t/fnew".
205   def C2_ccombinewt    : T_ALU32_3op_pred<"combine", 0b101, 0b000, 0, 0, 0>;
206   def C2_ccombinewf    : T_ALU32_3op_pred<"combine", 0b101, 0b000, 0, 1, 0>;
207   def C2_ccombinewnewt : T_ALU32_3op_pred<"combine", 0b101, 0b000, 0, 0, 1>;
208   def C2_ccombinewnewf : T_ALU32_3op_pred<"combine", 0b101, 0b000, 0, 1, 1>;
209 }
210
211 let hasSideEffects = 0, hasNewValue = 1, isCompare = 1, InputType = "reg"  in
212 class T_ALU32_3op_cmp<string mnemonic, bits<2> MinOp, bit IsNeg, bit IsComm>
213   : ALU32_rr<(outs PredRegs:$Pd), (ins IntRegs:$Rs, IntRegs:$Rt),
214              "$Pd = "#mnemonic#"($Rs, $Rt)",
215              [], "", ALU32_3op_tc_1_SLOT0123>, ImmRegRel {
216   let CextOpcode = mnemonic;
217   let isCommutable = IsComm;
218   bits<5> Rs;
219   bits<5> Rt;
220   bits<2> Pd;
221
222   let IClass = 0b1111;
223   let Inst{27-24} = 0b0010;
224   let Inst{22-21} = MinOp;
225   let Inst{20-16} = Rs;
226   let Inst{12-8} = Rt;
227   let Inst{4} = IsNeg;
228   let Inst{3-2} = 0b00;
229   let Inst{1-0} = Pd;
230 }
231
232 let Itinerary = ALU32_3op_tc_2early_SLOT0123, isCodeGenOnly = 0 in {
233   def C2_cmpeq   : T_ALU32_3op_cmp< "cmp.eq",  0b00, 0, 1>;
234   def C2_cmpgt   : T_ALU32_3op_cmp< "cmp.gt",  0b10, 0, 0>;
235   def C2_cmpgtu  : T_ALU32_3op_cmp< "cmp.gtu", 0b11, 0, 0>;
236 }
237
238 // Patfrag to convert the usual comparison patfrags (e.g. setlt) to ones
239 // that reverse the order of the operands.
240 class RevCmp<PatFrag F> : PatFrag<(ops node:$rhs, node:$lhs), F.Fragment>;
241
242 // Pats for compares. They use PatFrags as operands, not SDNodes,
243 // since seteq/setgt/etc. are defined as ParFrags.
244 class T_cmp32_rr_pat<InstHexagon MI, PatFrag Op, ValueType VT>
245   : Pat<(VT (Op (i32 IntRegs:$Rs), (i32 IntRegs:$Rt))),
246         (VT (MI IntRegs:$Rs, IntRegs:$Rt))>;
247
248 def: T_cmp32_rr_pat<C2_cmpeq,  seteq, i1>;
249 def: T_cmp32_rr_pat<C2_cmpgt,  setgt, i1>;
250 def: T_cmp32_rr_pat<C2_cmpgtu, setugt, i1>;
251
252 def: T_cmp32_rr_pat<C2_cmpgt,  RevCmp<setlt>,  i1>;
253 def: T_cmp32_rr_pat<C2_cmpgtu, RevCmp<setult>, i1>;
254
255 let CextOpcode = "MUX", InputType = "reg", hasNewValue = 1,
256   isCodeGenOnly = 0 in
257 def C2_mux: ALU32_rr<(outs IntRegs:$Rd),
258                      (ins PredRegs:$Pu, IntRegs:$Rs, IntRegs:$Rt),
259       "$Rd = mux($Pu, $Rs, $Rt)", [], "", ALU32_3op_tc_1_SLOT0123>, ImmRegRel {
260   bits<5> Rd;
261   bits<2> Pu;
262   bits<5> Rs;
263   bits<5> Rt;
264
265   let CextOpcode = "mux";
266   let InputType = "reg";
267   let hasSideEffects = 0;
268   let IClass = 0b1111;
269
270   let Inst{27-24} = 0b0100;
271   let Inst{20-16} = Rs;
272   let Inst{12-8} = Rt;
273   let Inst{6-5} = Pu;
274   let Inst{4-0} = Rd;
275 }
276
277 def: Pat<(i32 (select (i1 PredRegs:$Pu), (i32 IntRegs:$Rs), (i32 IntRegs:$Rt))),
278          (C2_mux PredRegs:$Pu, IntRegs:$Rs, IntRegs:$Rt)>;
279
280 // Combines the two immediates into a double register.
281 // Increase complexity to make it greater than any complexity of a combine
282 // that involves a register.
283
284 let isReMaterializable = 1, isMoveImm = 1, isAsCheapAsAMove = 1,
285     isExtentSigned = 1, isExtendable = 1, opExtentBits = 8, opExtendable = 1,
286     AddedComplexity = 75, isCodeGenOnly = 0 in
287 def A2_combineii: ALU32Inst <(outs DoubleRegs:$Rdd), (ins s8Ext:$s8, s8Imm:$S8),
288   "$Rdd = combine(#$s8, #$S8)",
289   [(set (i64 DoubleRegs:$Rdd),
290         (i64 (HexagonCOMBINE(i32 s8ExtPred:$s8), (i32 s8ImmPred:$S8))))]> {
291     bits<5> Rdd;
292     bits<8> s8;
293     bits<8> S8;
294
295     let IClass = 0b0111;
296     let Inst{27-23} = 0b11000;
297     let Inst{22-16} = S8{7-1};
298     let Inst{13}    = S8{0};
299     let Inst{12-5}  = s8;
300     let Inst{4-0}   = Rdd;
301   }
302
303 //===----------------------------------------------------------------------===//
304 // Template class for predicated ADD of a reg and an Immediate value.
305 //===----------------------------------------------------------------------===//
306 let hasNewValue = 1 in
307 class T_Addri_Pred <bit PredNot, bit PredNew>
308   : ALU32_ri <(outs IntRegs:$Rd),
309               (ins PredRegs:$Pu, IntRegs:$Rs, s8Ext:$s8),
310   !if(PredNot, "if (!$Pu", "if ($Pu")#!if(PredNew,".new) $Rd = ",
311   ") $Rd = ")#"add($Rs, #$s8)"> {
312     bits<5> Rd;
313     bits<2> Pu;
314     bits<5> Rs;
315     bits<8> s8;
316
317     let isPredicatedNew = PredNew;
318     let IClass = 0b0111;
319
320     let Inst{27-24} = 0b0100;
321     let Inst{23}    = PredNot;
322     let Inst{22-21} = Pu;
323     let Inst{20-16} = Rs;
324     let Inst{13}    = PredNew;
325     let Inst{12-5}  = s8;
326     let Inst{4-0}   = Rd;
327   }
328
329 //===----------------------------------------------------------------------===//
330 // A2_addi: Add a signed immediate to a register.
331 //===----------------------------------------------------------------------===//
332 let hasNewValue = 1 in
333 class T_Addri <Operand immOp, list<dag> pattern = [] >
334   : ALU32_ri <(outs IntRegs:$Rd),
335               (ins IntRegs:$Rs, immOp:$s16),
336   "$Rd = add($Rs, #$s16)", pattern,
337   //[(set (i32 IntRegs:$Rd), (add (i32 IntRegs:$Rs), (s16ExtPred:$s16)))],
338   "", ALU32_ADDI_tc_1_SLOT0123> {
339     bits<5> Rd;
340     bits<5> Rs;
341     bits<16> s16;
342
343     let IClass = 0b1011;
344
345     let Inst{27-21} = s16{15-9};
346     let Inst{20-16} = Rs;
347     let Inst{13-5}  = s16{8-0};
348     let Inst{4-0}   = Rd;
349   }
350
351 //===----------------------------------------------------------------------===//
352 // Multiclass for ADD of a register and an immediate value.
353 //===----------------------------------------------------------------------===//
354 multiclass Addri_Pred<string mnemonic, bit PredNot> {
355   let isPredicatedFalse = PredNot in {
356     def _c#NAME : T_Addri_Pred<PredNot, 0>;
357     // Predicate new
358     def _cdn#NAME : T_Addri_Pred<PredNot, 1>;
359   }
360 }
361
362 let isExtendable = 1, InputType = "imm" in
363 multiclass Addri_base<string mnemonic, SDNode OpNode> {
364   let CextOpcode = mnemonic, BaseOpcode = mnemonic#_ri in {
365     let opExtendable = 2, isExtentSigned = 1, opExtentBits = 16,
366     isPredicable = 1 in
367     def NAME : T_Addri< s16Ext, // Rd=add(Rs,#s16)
368                         [(set (i32 IntRegs:$Rd),
369                               (add IntRegs:$Rs, s16ExtPred:$s16))]>;
370
371     let opExtendable = 3, isExtentSigned = 1, opExtentBits = 8,
372     hasSideEffects = 0, isPredicated = 1 in {
373       defm Pt : Addri_Pred<mnemonic, 0>;
374       defm NotPt : Addri_Pred<mnemonic, 1>;
375     }
376   }
377 }
378
379 let isCodeGenOnly = 0 in
380 defm ADD_ri : Addri_base<"add", add>, ImmRegRel, PredNewRel;
381
382 //===----------------------------------------------------------------------===//
383 // Template class used for the following ALU32 instructions.
384 // Rd=and(Rs,#s10)
385 // Rd=or(Rs,#s10)
386 //===----------------------------------------------------------------------===//
387 let isExtendable = 1, opExtendable = 2, isExtentSigned = 1, opExtentBits = 10,
388 InputType = "imm", hasNewValue = 1 in
389 class T_ALU32ri_logical <string mnemonic, SDNode OpNode, bits<2> MinOp>
390   : ALU32_ri <(outs IntRegs:$Rd),
391               (ins IntRegs:$Rs, s10Ext:$s10),
392   "$Rd = "#mnemonic#"($Rs, #$s10)" ,
393   [(set (i32 IntRegs:$Rd), (OpNode (i32 IntRegs:$Rs), s10ExtPred:$s10))]> {
394     bits<5> Rd;
395     bits<5> Rs;
396     bits<10> s10;
397     let CextOpcode = mnemonic;
398
399     let IClass = 0b0111;
400
401     let Inst{27-24} = 0b0110;
402     let Inst{23-22} = MinOp;
403     let Inst{21}    = s10{9};
404     let Inst{20-16} = Rs;
405     let Inst{13-5}  = s10{8-0};
406     let Inst{4-0}   = Rd;
407   }
408
409 let isCodeGenOnly = 0 in {
410 def OR_ri  : T_ALU32ri_logical<"or", or, 0b10>, ImmRegRel;
411 def AND_ri : T_ALU32ri_logical<"and", and, 0b00>, ImmRegRel;
412 }
413
414 // Subtract register from immediate
415 // Rd32=sub(#s10,Rs32)
416 let isExtendable = 1, opExtendable = 1, isExtentSigned = 1, opExtentBits = 10,
417 CextOpcode = "sub", InputType = "imm", hasNewValue = 1, isCodeGenOnly = 0 in
418 def SUB_ri: ALU32_ri <(outs IntRegs:$Rd), (ins s10Ext:$s10, IntRegs:$Rs),
419   "$Rd = sub(#$s10, $Rs)" ,
420   [(set IntRegs:$Rd, (sub s10ExtPred:$s10, IntRegs:$Rs))] > ,
421   ImmRegRel {
422     bits<5> Rd;
423     bits<10> s10;
424     bits<5> Rs;
425
426     let IClass = 0b0111;
427
428     let Inst{27-22} = 0b011001;
429     let Inst{21}    = s10{9};
430     let Inst{20-16} = Rs;
431     let Inst{13-5}  = s10{8-0};
432     let Inst{4-0}   = Rd;
433   }
434
435 // Nop.
436 let hasSideEffects = 0, isCodeGenOnly = 0 in
437 def A2_nop: ALU32Inst <(outs), (ins), "nop" > {
438   let IClass = 0b0111;
439   let Inst{27-24} = 0b1111;
440 }
441 // Rd = not(Rs) gets mapped to Rd=sub(#-1, Rs).
442 def : Pat<(not (i32 IntRegs:$src1)),
443           (SUB_ri -1, (i32 IntRegs:$src1))>;
444
445 let hasSideEffects = 0, hasNewValue = 1 in
446 class T_tfr16<bit isHi>
447   : ALU32Inst <(outs IntRegs:$Rx), (ins IntRegs:$src1, u16Imm:$u16),
448   "$Rx"#!if(isHi, ".h", ".l")#" = #$u16",
449   [], "$src1 = $Rx" > {
450     bits<5> Rx;
451     bits<16> u16;
452
453     let IClass = 0b0111;
454     let Inst{27-26} = 0b00;
455     let Inst{25-24} = !if(isHi, 0b10, 0b01);
456     let Inst{23-22} = u16{15-14};
457     let Inst{21}    = 0b1;
458     let Inst{20-16} = Rx;
459     let Inst{13-0}  = u16{13-0};
460   }
461
462 let isCodeGenOnly = 0 in {
463 def A2_tfril: T_tfr16<0>;
464 def A2_tfrih: T_tfr16<1>;
465 }
466
467 // Conditional transfer is an alias to conditional "Rd = add(Rs, #0)".
468 let isPredicated = 1, hasNewValue = 1, opNewValue = 0 in
469 class T_tfr_pred<bit isPredNot, bit isPredNew>
470   : ALU32Inst<(outs IntRegs:$dst),
471               (ins PredRegs:$src1, IntRegs:$src2),
472               "if ("#!if(isPredNot, "!", "")#
473               "$src1"#!if(isPredNew, ".new", "")#
474               ") $dst = $src2"> {
475     bits<5> dst;
476     bits<2> src1;
477     bits<5> src2;
478
479     let isPredicatedFalse = isPredNot;
480     let isPredicatedNew = isPredNew;
481     let IClass = 0b0111;
482
483     let Inst{27-24} = 0b0100;
484     let Inst{23} = isPredNot;
485     let Inst{13} = isPredNew;
486     let Inst{12-5} = 0;
487     let Inst{4-0} = dst;
488     let Inst{22-21} = src1;
489     let Inst{20-16} = src2;
490   }
491
492 let isPredicable = 1 in
493 class T_tfr : ALU32Inst<(outs IntRegs:$dst), (ins IntRegs:$src),
494               "$dst = $src"> {
495     bits<5> dst;
496     bits<5> src;
497
498     let IClass = 0b0111;
499
500     let Inst{27-21} = 0b0000011;
501     let Inst{20-16} = src;
502     let Inst{13}    = 0b0;
503     let Inst{4-0}   = dst;
504   }
505
506 let InputType = "reg", hasNewValue = 1, hasSideEffects = 0 in
507 multiclass tfr_base<string CextOp> {
508   let CextOpcode = CextOp, BaseOpcode = CextOp in {
509     def NAME : T_tfr;
510
511     // Predicate
512     def t : T_tfr_pred<0, 0>;
513     def f : T_tfr_pred<1, 0>;
514     // Predicate new
515     def tnew : T_tfr_pred<0, 1>;
516     def fnew : T_tfr_pred<1, 1>;
517   }
518 }
519
520 // Assembler mapped to C2_ccombinew[t|f|newt|newf].
521 // Please don't add bits to this instruction as it'll be converted into
522 // 'combine' before object code emission.
523 let isPredicated = 1 in
524 class T_tfrp_pred<bit PredNot, bit PredNew>
525   : ALU32_rr <(outs DoubleRegs:$dst),
526               (ins PredRegs:$src1, DoubleRegs:$src2),
527   "if ("#!if(PredNot, "!", "")#"$src1"
528         #!if(PredNew, ".new", "")#") $dst = $src2" > {
529     let isPredicatedFalse = PredNot;
530     let isPredicatedNew = PredNew;
531   }
532
533 // Assembler mapped to A2_combinew.
534 // Please don't add bits to this instruction as it'll be converted into
535 // 'combine' before object code emission.
536 class T_tfrp : ALU32Inst <(outs DoubleRegs:$dst),
537                (ins DoubleRegs:$src),
538     "$dst = $src">;
539
540 let hasSideEffects = 0 in
541 multiclass TFR64_base<string BaseName> {
542   let BaseOpcode = BaseName in {
543     let isPredicable = 1 in
544     def NAME : T_tfrp;
545     // Predicate
546     def t : T_tfrp_pred <0, 0>;
547     def f : T_tfrp_pred <1, 0>;
548     // Predicate new
549     def tnew : T_tfrp_pred <0, 1>;
550     def fnew : T_tfrp_pred <1, 1>;
551   }
552 }
553
554 let InputType = "imm", isExtendable = 1, isExtentSigned = 1, opExtentBits = 12,
555     isMoveImm = 1, opExtendable = 2, BaseOpcode = "TFRI", CextOpcode = "TFR",
556     hasSideEffects = 0, isPredicated = 1, hasNewValue = 1 in
557 class T_TFRI_Pred<bit PredNot, bit PredNew>
558   : ALU32_ri<(outs IntRegs:$Rd), (ins PredRegs:$Pu, s12Ext:$s12),
559     "if ("#!if(PredNot,"!","")#"$Pu"#!if(PredNew,".new","")#") $Rd = #$s12",
560     [], "", ALU32_2op_tc_1_SLOT0123>, ImmRegRel, PredNewRel {
561   let isPredicatedFalse = PredNot;
562   let isPredicatedNew = PredNew;
563
564   bits<5> Rd;
565   bits<2> Pu;
566   bits<12> s12;
567
568   let IClass = 0b0111;
569   let Inst{27-24} = 0b1110;
570   let Inst{23} = PredNot;
571   let Inst{22-21} = Pu;
572   let Inst{20} = 0b0;
573   let Inst{19-16,12-5} = s12;
574   let Inst{13} = PredNew;
575   let Inst{4-0} = Rd;
576 }
577
578 let isCodeGenOnly = 0 in {
579 def C2_cmoveit    : T_TFRI_Pred<0, 0>;
580 def C2_cmoveif    : T_TFRI_Pred<1, 0>;
581 def C2_cmovenewit : T_TFRI_Pred<0, 1>;
582 def C2_cmovenewif : T_TFRI_Pred<1, 1>;
583 }
584
585 let InputType = "imm", isExtendable = 1, isExtentSigned = 1,
586     CextOpcode = "TFR", BaseOpcode = "TFRI", hasNewValue = 1, opNewValue = 0,
587     isAsCheapAsAMove = 1 , opExtendable = 1, opExtentBits = 16, isMoveImm = 1,
588     isPredicated = 0, isPredicable = 1, isReMaterializable = 1,
589     isCodeGenOnly = 0 in
590 def A2_tfrsi : ALU32Inst<(outs IntRegs:$Rd), (ins s16Ext:$s16), "$Rd = #$s16",
591     [(set (i32 IntRegs:$Rd), s16ExtPred:$s16)], "", ALU32_2op_tc_1_SLOT0123>,
592     ImmRegRel, PredRel {
593   bits<5> Rd;
594   bits<16> s16;
595
596   let IClass = 0b0111;
597   let Inst{27-24} = 0b1000;
598   let Inst{23-22,20-16,13-5} = s16;
599   let Inst{4-0} = Rd;
600 }
601
602 let isCodeGenOnly = 0 in
603 defm A2_tfr  : tfr_base<"TFR">, ImmRegRel, PredNewRel;
604 defm A2_tfrp : TFR64_base<"TFR64">, PredNewRel;
605
606 // Assembler mapped
607 let isReMaterializable = 1, isMoveImm = 1, isAsCheapAsAMove = 1 in
608 def A2_tfrpi : ALU64_rr<(outs DoubleRegs:$dst), (ins s8Imm64:$src1),
609                       "$dst = #$src1",
610                       [(set (i64 DoubleRegs:$dst), s8Imm64Pred:$src1)]>;
611
612 // TODO: see if this instruction can be deleted..
613 let isExtendable = 1, opExtendable = 1, opExtentBits = 6 in
614 def TFRI64_V4 : ALU64_rr<(outs DoubleRegs:$dst), (ins u6Ext:$src1),
615                          "$dst = #$src1">;
616
617 //===----------------------------------------------------------------------===//
618 // ALU32/ALU -
619 //===----------------------------------------------------------------------===//
620
621
622 //===----------------------------------------------------------------------===//
623 // ALU32/PERM +
624 //===----------------------------------------------------------------------===//
625 // Scalar mux register immediate.
626 let hasSideEffects = 0, isExtentSigned = 1, CextOpcode = "MUX",
627     InputType = "imm", hasNewValue = 1, isExtendable = 1, opExtentBits = 8 in
628 class T_MUX1 <bit MajOp, dag ins, string AsmStr>
629       : ALU32Inst <(outs IntRegs:$Rd), ins, AsmStr>, ImmRegRel {
630   bits<5> Rd;
631   bits<2> Pu;
632   bits<8> s8;
633   bits<5> Rs;
634
635   let IClass = 0b0111;
636   let Inst{27-24} = 0b0011;
637   let Inst{23} = MajOp;
638   let Inst{22-21} = Pu;
639   let Inst{20-16} = Rs;
640   let Inst{13}    = 0b0;
641   let Inst{12-5}  = s8;
642   let Inst{4-0}   = Rd;
643 }
644
645 let opExtendable = 2, isCodeGenOnly = 0 in
646 def C2_muxri : T_MUX1<0b1, (ins PredRegs:$Pu, s8Ext:$s8, IntRegs:$Rs),
647                            "$Rd = mux($Pu, #$s8, $Rs)">;
648
649 let opExtendable = 3, isCodeGenOnly = 0 in
650 def C2_muxir : T_MUX1<0b0, (ins PredRegs:$Pu, IntRegs:$Rs, s8Ext:$s8),
651                            "$Rd = mux($Pu, $Rs, #$s8)">;
652
653 def : Pat<(i32 (select I1:$Pu, s8ExtPred:$s8, I32:$Rs)),
654           (C2_muxri I1:$Pu, s8ExtPred:$s8, I32:$Rs)>;
655
656 def : Pat<(i32 (select I1:$Pu, I32:$Rs, s8ExtPred:$s8)),
657           (C2_muxir I1:$Pu, I32:$Rs, s8ExtPred:$s8)>;
658
659 // C2_muxii: Scalar mux immediates.
660 let isExtentSigned = 1, hasNewValue = 1, isExtendable = 1,
661     opExtentBits = 8, opExtendable = 2, isCodeGenOnly = 0 in
662 def C2_muxii: ALU32Inst <(outs IntRegs:$Rd),
663                          (ins PredRegs:$Pu, s8Ext:$s8, s8Imm:$S8),
664   "$Rd = mux($Pu, #$s8, #$S8)" ,
665   [(set (i32 IntRegs:$Rd),
666         (i32 (select I1:$Pu, s8ExtPred:$s8, s8ImmPred:$S8)))] > {
667     bits<5> Rd;
668     bits<2> Pu;
669     bits<8> s8;
670     bits<8> S8;
671
672     let IClass = 0b0111;
673
674     let Inst{27-25} = 0b101;
675     let Inst{24-23} = Pu;
676     let Inst{22-16} = S8{7-1};
677     let Inst{13}    = S8{0};
678     let Inst{12-5}  = s8;
679     let Inst{4-0}   = Rd;
680   }
681
682 //===----------------------------------------------------------------------===//
683 // template class for non-predicated alu32_2op instructions
684 // - aslh, asrh, sxtb, sxth, zxth
685 //===----------------------------------------------------------------------===//
686 let hasNewValue = 1, opNewValue = 0 in
687 class T_ALU32_2op <string mnemonic, bits<3> minOp> :
688     ALU32Inst < (outs IntRegs:$Rd), (ins IntRegs:$Rs),
689     "$Rd = "#mnemonic#"($Rs)", [] > {
690   bits<5> Rd;
691   bits<5> Rs;
692
693   let IClass = 0b0111;
694
695   let Inst{27-24} = 0b0000;
696   let Inst{23-21} = minOp;
697   let Inst{13} = 0b0;
698   let Inst{4-0} = Rd;
699   let Inst{20-16} = Rs;
700 }
701
702 //===----------------------------------------------------------------------===//
703 // template class for predicated alu32_2op instructions
704 // - aslh, asrh, sxtb, sxth, zxtb, zxth
705 //===----------------------------------------------------------------------===//
706 let hasSideEffects = 0, validSubTargets = HasV4SubT,
707     hasNewValue = 1, opNewValue = 0 in
708 class T_ALU32_2op_Pred <string mnemonic, bits<3> minOp, bit isPredNot, 
709     bit isPredNew > :
710     ALU32Inst <(outs IntRegs:$Rd), (ins PredRegs:$Pu, IntRegs:$Rs),
711     !if(isPredNot, "if (!$Pu", "if ($Pu")
712     #!if(isPredNew, ".new) ",") ")#"$Rd = "#mnemonic#"($Rs)"> {
713   bits<5> Rd;
714   bits<2> Pu;
715   bits<5> Rs;
716
717   let IClass = 0b0111;
718
719   let Inst{27-24} = 0b0000;
720   let Inst{23-21} = minOp;
721   let Inst{13} = 0b1;
722   let Inst{11} = isPredNot;
723   let Inst{10} = isPredNew;
724   let Inst{4-0} = Rd;
725   let Inst{9-8} = Pu;
726   let Inst{20-16} = Rs;
727 }
728
729 multiclass ALU32_2op_Pred<string mnemonic, bits<3> minOp, bit PredNot> {
730   let isPredicatedFalse = PredNot in {
731     def NAME : T_ALU32_2op_Pred<mnemonic, minOp, PredNot, 0>;
732
733     // Predicate new
734     let isPredicatedNew = 1 in
735     def NAME#new : T_ALU32_2op_Pred<mnemonic, minOp, PredNot, 1>;
736   }
737 }
738
739 multiclass ALU32_2op_base<string mnemonic, bits<3> minOp> {
740   let BaseOpcode = mnemonic in {
741     let isPredicable = 1, hasSideEffects = 0 in
742     def A2_#NAME : T_ALU32_2op<mnemonic, minOp>;
743
744     let validSubTargets = HasV4SubT, isPredicated = 1, hasSideEffects = 0 in {
745       defm A4_p#NAME#t : ALU32_2op_Pred<mnemonic, minOp, 0>;
746       defm A4_p#NAME#f : ALU32_2op_Pred<mnemonic, minOp, 1>;
747     }
748   }
749 }
750
751 let isCodeGenOnly = 0 in {
752 defm aslh : ALU32_2op_base<"aslh", 0b000>, PredNewRel;
753 defm asrh : ALU32_2op_base<"asrh", 0b001>, PredNewRel;
754 defm sxtb : ALU32_2op_base<"sxtb", 0b101>, PredNewRel;
755 defm sxth : ALU32_2op_base<"sxth", 0b111>, PredNewRel;
756 defm zxth : ALU32_2op_base<"zxth", 0b110>, PredNewRel;
757 }
758
759 // Rd=zxtb(Rs): assembler mapped to Rd=and(Rs,#255).
760 // Compiler would want to generate 'zxtb' instead of 'and' becuase 'zxtb' has
761 // predicated forms while 'and' doesn't. Since integrated assembler can't
762 // handle 'mapped' instructions, we need to encode 'zxtb' same as 'and' where
763 // immediate operand is set to '255'.
764
765 let hasNewValue = 1, opNewValue = 0 in
766 class T_ZXTB: ALU32Inst < (outs IntRegs:$Rd), (ins IntRegs:$Rs),
767   "$Rd = zxtb($Rs)", [] > { // Rd = and(Rs,255)
768     bits<5> Rd;
769     bits<5> Rs;
770     bits<10> s10 = 255;
771
772     let IClass = 0b0111;
773
774     let Inst{27-22} = 0b011000;
775     let Inst{4-0} = Rd;
776     let Inst{20-16} = Rs;
777     let Inst{21} = s10{9};
778     let Inst{13-5} = s10{8-0};
779 }
780
781 //Rd=zxtb(Rs): assembler mapped to "Rd=and(Rs,#255)
782 multiclass ZXTB_base <string mnemonic, bits<3> minOp> {
783   let BaseOpcode = mnemonic in {
784     let isPredicable = 1, hasSideEffects = 0 in
785     def A2_#NAME : T_ZXTB;
786
787     let validSubTargets = HasV4SubT, isPredicated = 1, hasSideEffects = 0 in {
788       defm A4_p#NAME#t : ALU32_2op_Pred<mnemonic, minOp, 0>;
789       defm A4_p#NAME#f : ALU32_2op_Pred<mnemonic, minOp, 1>;
790     }
791   }
792 }
793
794 let isCodeGenOnly=0 in
795 defm zxtb : ZXTB_base<"zxtb",0b100>, PredNewRel;
796
797 def: Pat<(shl I32:$src1, (i32 16)),   (A2_aslh I32:$src1)>;
798 def: Pat<(sra I32:$src1, (i32 16)),   (A2_asrh I32:$src1)>;
799 def: Pat<(sext_inreg I32:$src1, i8),  (A2_sxtb I32:$src1)>;
800 def: Pat<(sext_inreg I32:$src1, i16), (A2_sxth I32:$src1)>;
801
802 // Mux.
803 def VMUX_prr64 : ALU64_rr<(outs DoubleRegs:$dst), (ins PredRegs:$src1,
804                                                    DoubleRegs:$src2,
805                                                    DoubleRegs:$src3),
806             "$dst = vmux($src1, $src2, $src3)",
807             []>;
808
809
810 //===----------------------------------------------------------------------===//
811 // ALU32/PERM -
812 //===----------------------------------------------------------------------===//
813
814
815 //===----------------------------------------------------------------------===//
816 // ALU32/PRED +
817 //===----------------------------------------------------------------------===//
818
819 // SDNode for converting immediate C to C-1.
820 def DEC_CONST_SIGNED : SDNodeXForm<imm, [{
821    // Return the byte immediate const-1 as an SDNode.
822    int32_t imm = N->getSExtValue();
823    return XformSToSM1Imm(imm);
824 }]>;
825
826 // SDNode for converting immediate C to C-1.
827 def DEC_CONST_UNSIGNED : SDNodeXForm<imm, [{
828    // Return the byte immediate const-1 as an SDNode.
829    uint32_t imm = N->getZExtValue();
830    return XformUToUM1Imm(imm);
831 }]>;
832
833 def CTLZ64_rr : SInst<(outs IntRegs:$dst), (ins DoubleRegs:$src1),
834     "$dst = cl0($src1)",
835     [(set (i32 IntRegs:$dst), (i32 (trunc (ctlz (i64 DoubleRegs:$src1)))))]>;
836
837 def CTTZ64_rr : SInst<(outs IntRegs:$dst), (ins DoubleRegs:$src1),
838     "$dst = ct0($src1)",
839     [(set (i32 IntRegs:$dst), (i32 (trunc (cttz (i64 DoubleRegs:$src1)))))]>;
840
841 //===----------------------------------------------------------------------===//
842 // ALU32/PRED -
843 //===----------------------------------------------------------------------===//
844
845
846 //===----------------------------------------------------------------------===//
847 // ALU64/ALU +
848 //===----------------------------------------------------------------------===//// Add.
849 //===----------------------------------------------------------------------===//
850 // Template Class
851 // Add/Subtract halfword
852 // Rd=add(Rt.L,Rs.[HL])[:sat]
853 // Rd=sub(Rt.L,Rs.[HL])[:sat]
854 // Rd=add(Rt.[LH],Rs.[HL])[:sat][:<16]
855 // Rd=sub(Rt.[LH],Rs.[HL])[:sat][:<16]
856 //===----------------------------------------------------------------------===//
857
858 let  hasNewValue = 1, opNewValue = 0 in
859 class T_XTYPE_ADD_SUB <bits<2> LHbits, bit isSat, bit hasShift, bit isSub>
860   : ALU64Inst <(outs IntRegs:$Rd), (ins IntRegs:$Rt, IntRegs:$Rs),
861   "$Rd = "#!if(isSub,"sub","add")#"($Rt."
862           #!if(hasShift, !if(LHbits{1},"h","l"),"l") #", $Rs."
863           #!if(hasShift, !if(LHbits{0},"h)","l)"), !if(LHbits{1},"h)","l)"))
864           #!if(isSat,":sat","")
865           #!if(hasShift,":<<16",""), [], "", ALU64_tc_1_SLOT23> {
866     bits<5> Rd;
867     bits<5> Rt;
868     bits<5> Rs;
869     let IClass = 0b1101;
870
871     let Inst{27-23} = 0b01010;
872     let Inst{22} = hasShift;
873     let Inst{21} = isSub;
874     let Inst{7} = isSat;
875     let Inst{6-5} = LHbits;
876     let Inst{4-0} = Rd;
877     let Inst{12-8} = Rt;
878     let Inst{20-16} = Rs;
879   }
880
881 //Rd=sub(Rt.L,Rs.[LH])
882 let isCodeGenOnly = 0 in {
883 def A2_subh_l16_ll : T_XTYPE_ADD_SUB <0b00, 0, 0, 1>;
884 def A2_subh_l16_hl : T_XTYPE_ADD_SUB <0b10, 0, 0, 1>;
885 }
886
887 let isCodeGenOnly = 0 in {
888 //Rd=add(Rt.L,Rs.[LH])
889 def A2_addh_l16_ll : T_XTYPE_ADD_SUB <0b00, 0, 0, 0>;
890 def A2_addh_l16_hl : T_XTYPE_ADD_SUB <0b10, 0, 0, 0>;
891 }
892
893 let Itinerary = ALU64_tc_2_SLOT23, Defs = [USR_OVF], isCodeGenOnly = 0 in {
894   //Rd=sub(Rt.L,Rs.[LH]):sat
895   def A2_subh_l16_sat_ll : T_XTYPE_ADD_SUB <0b00, 1, 0, 1>;
896   def A2_subh_l16_sat_hl : T_XTYPE_ADD_SUB <0b10, 1, 0, 1>;
897
898   //Rd=add(Rt.L,Rs.[LH]):sat
899   def A2_addh_l16_sat_ll : T_XTYPE_ADD_SUB <0b00, 1, 0, 0>;
900   def A2_addh_l16_sat_hl : T_XTYPE_ADD_SUB <0b10, 1, 0, 0>;
901 }
902
903 //Rd=sub(Rt.[LH],Rs.[LH]):<<16
904 let isCodeGenOnly = 0 in {
905 def A2_subh_h16_ll : T_XTYPE_ADD_SUB <0b00, 0, 1, 1>;
906 def A2_subh_h16_lh : T_XTYPE_ADD_SUB <0b01, 0, 1, 1>;
907 def A2_subh_h16_hl : T_XTYPE_ADD_SUB <0b10, 0, 1, 1>;
908 def A2_subh_h16_hh : T_XTYPE_ADD_SUB <0b11, 0, 1, 1>;
909 }
910
911 //Rd=add(Rt.[LH],Rs.[LH]):<<16
912 let isCodeGenOnly = 0 in {
913 def A2_addh_h16_ll : T_XTYPE_ADD_SUB <0b00, 0, 1, 0>;
914 def A2_addh_h16_lh : T_XTYPE_ADD_SUB <0b01, 0, 1, 0>;
915 def A2_addh_h16_hl : T_XTYPE_ADD_SUB <0b10, 0, 1, 0>;
916 def A2_addh_h16_hh : T_XTYPE_ADD_SUB <0b11, 0, 1, 0>;
917 }
918
919 let Itinerary = ALU64_tc_2_SLOT23, Defs = [USR_OVF], isCodeGenOnly = 0 in {
920   //Rd=sub(Rt.[LH],Rs.[LH]):sat:<<16
921   def A2_subh_h16_sat_ll : T_XTYPE_ADD_SUB <0b00, 1, 1, 1>;
922   def A2_subh_h16_sat_lh : T_XTYPE_ADD_SUB <0b01, 1, 1, 1>;
923   def A2_subh_h16_sat_hl : T_XTYPE_ADD_SUB <0b10, 1, 1, 1>;
924   def A2_subh_h16_sat_hh : T_XTYPE_ADD_SUB <0b11, 1, 1, 1>;
925
926   //Rd=add(Rt.[LH],Rs.[LH]):sat:<<16
927   def A2_addh_h16_sat_ll : T_XTYPE_ADD_SUB <0b00, 1, 1, 0>;
928   def A2_addh_h16_sat_lh : T_XTYPE_ADD_SUB <0b01, 1, 1, 0>;
929   def A2_addh_h16_sat_hl : T_XTYPE_ADD_SUB <0b10, 1, 1, 0>;
930   def A2_addh_h16_sat_hh : T_XTYPE_ADD_SUB <0b11, 1, 1, 0>;
931 }
932
933 // Add halfword.
934 def: Pat<(sext_inreg (add I32:$src1, I32:$src2), i16),
935          (A2_addh_l16_ll I32:$src1, I32:$src2)>;
936
937 def: Pat<(sra (add (shl I32:$src1, (i32 16)), I32:$src2), (i32 16)),
938          (A2_addh_l16_hl I32:$src1, I32:$src2)>;
939
940 def: Pat<(shl (add I32:$src1, I32:$src2), (i32 16)),
941          (A2_addh_h16_ll I32:$src1, I32:$src2)>;
942
943 // Subtract halfword.
944 def: Pat<(sext_inreg (sub I32:$src1, I32:$src2), i16),
945          (A2_subh_l16_ll I32:$src1, I32:$src2)>;
946
947 def: Pat<(shl (sub I32:$src1, I32:$src2), (i32 16)),
948          (A2_subh_h16_ll I32:$src1, I32:$src2)>;
949
950 let hasSideEffects = 0, hasNewValue = 1, isCodeGenOnly = 0 in
951 def S2_parityp: ALU64Inst<(outs IntRegs:$Rd),
952       (ins DoubleRegs:$Rs, DoubleRegs:$Rt),
953       "$Rd = parity($Rs, $Rt)", [], "", ALU64_tc_2_SLOT23> {
954   bits<5> Rd;
955   bits<5> Rs;
956   bits<5> Rt;
957
958   let IClass = 0b1101;
959   let Inst{27-24} = 0b0000;
960   let Inst{20-16} = Rs;
961   let Inst{12-8} = Rt;
962   let Inst{4-0} = Rd;
963 }
964
965 let hasNewValue = 1, opNewValue = 0, hasSideEffects = 0 in
966 class T_XTYPE_MIN_MAX < bit isMax, bit isUnsigned >
967   : ALU64Inst < (outs IntRegs:$Rd), (ins IntRegs:$Rt, IntRegs:$Rs),
968   "$Rd = "#!if(isMax,"max","min")#!if(isUnsigned,"u","")
969           #"($Rt, $Rs)", [], "", ALU64_tc_2_SLOT23> {
970     bits<5> Rd;
971     bits<5> Rt;
972     bits<5> Rs;
973
974     let IClass = 0b1101;
975
976     let Inst{27-23} = 0b01011;
977     let Inst{22-21} = !if(isMax, 0b10, 0b01);
978     let Inst{7} = isUnsigned;
979     let Inst{4-0} = Rd;
980     let Inst{12-8} = !if(isMax, Rs, Rt);
981     let Inst{20-16} = !if(isMax, Rt, Rs);
982   }
983
984 let isCodeGenOnly = 0 in {
985 def A2_min  : T_XTYPE_MIN_MAX < 0, 0 >;
986 def A2_minu : T_XTYPE_MIN_MAX < 0, 1 >;
987 def A2_max  : T_XTYPE_MIN_MAX < 1, 0 >;
988 def A2_maxu : T_XTYPE_MIN_MAX < 1, 1 >;
989 }
990
991 // Here, depending on  the operand being selected, we'll either generate a
992 // min or max instruction.
993 // Ex:
994 // (a>b)?a:b --> max(a,b) => Here check performed is '>' and the value selected
995 // is the larger of two. So, the corresponding HexagonInst is passed in 'Inst'.
996 // (a>b)?b:a --> min(a,b) => Here check performed is '>' but the smaller value
997 // is selected and the corresponding HexagonInst is passed in 'SwapInst'.
998
999 multiclass T_MinMax_pats <PatFrag Op, RegisterClass RC, ValueType VT,
1000                           InstHexagon Inst, InstHexagon SwapInst> {
1001   def: Pat<(select (i1 (Op (VT RC:$src1), (VT RC:$src2))),
1002                    (VT RC:$src1), (VT RC:$src2)),
1003            (Inst RC:$src1, RC:$src2)>;
1004   def: Pat<(select (i1 (Op (VT RC:$src1), (VT RC:$src2))),
1005                    (VT RC:$src2), (VT RC:$src1)),
1006            (SwapInst RC:$src1, RC:$src2)>;
1007 }
1008
1009
1010 multiclass MinMax_pats <PatFrag Op, InstHexagon Inst, InstHexagon SwapInst> {
1011   defm: T_MinMax_pats<Op, IntRegs, i32, Inst, SwapInst>;
1012
1013   def: Pat<(sext_inreg (i32 (select (i1 (Op (i32 PositiveHalfWord:$src1),
1014                                             (i32 PositiveHalfWord:$src2))),
1015                                     (i32 PositiveHalfWord:$src1),
1016                                     (i32 PositiveHalfWord:$src2))), i16),
1017            (Inst IntRegs:$src1, IntRegs:$src2)>;
1018
1019   def: Pat<(sext_inreg (i32 (select (i1 (Op (i32 PositiveHalfWord:$src1),
1020                                             (i32 PositiveHalfWord:$src2))),
1021                                     (i32 PositiveHalfWord:$src2),
1022                                     (i32 PositiveHalfWord:$src1))), i16),
1023            (SwapInst IntRegs:$src1, IntRegs:$src2)>;
1024 }
1025
1026 let AddedComplexity = 200 in {
1027   defm: MinMax_pats<setge,  A2_max,  A2_min>;
1028   defm: MinMax_pats<setgt,  A2_max,  A2_min>;
1029   defm: MinMax_pats<setle,  A2_min,  A2_max>;
1030   defm: MinMax_pats<setlt,  A2_min,  A2_max>;
1031   defm: MinMax_pats<setuge, A2_maxu, A2_minu>;
1032   defm: MinMax_pats<setugt, A2_maxu, A2_minu>;
1033   defm: MinMax_pats<setule, A2_minu, A2_maxu>;
1034   defm: MinMax_pats<setult, A2_minu, A2_maxu>;
1035 }
1036
1037 class T_cmp64_rr<string mnemonic, bits<3> MinOp, bit IsComm>
1038   : ALU64_rr<(outs PredRegs:$Pd), (ins DoubleRegs:$Rs, DoubleRegs:$Rt),
1039              "$Pd = "#mnemonic#"($Rs, $Rt)", [], "", ALU64_tc_2early_SLOT23> {
1040   let isCompare = 1;
1041   let isCommutable = IsComm;
1042   let hasSideEffects = 0;
1043
1044   bits<2> Pd;
1045   bits<5> Rs;
1046   bits<5> Rt;
1047
1048   let IClass = 0b1101;
1049   let Inst{27-21} = 0b0010100;
1050   let Inst{20-16} = Rs;
1051   let Inst{12-8} = Rt;
1052   let Inst{7-5} = MinOp;
1053   let Inst{1-0} = Pd;
1054 }
1055
1056 let isCodeGenOnly = 0 in {
1057 def C2_cmpeqp  : T_cmp64_rr<"cmp.eq",  0b000, 1>;
1058 def C2_cmpgtp  : T_cmp64_rr<"cmp.gt",  0b010, 0>;
1059 def C2_cmpgtup : T_cmp64_rr<"cmp.gtu", 0b100, 0>;
1060 }
1061
1062 class T_cmp64_rr_pat<InstHexagon MI, PatFrag CmpOp>
1063   : Pat<(i1 (CmpOp (i64 DoubleRegs:$Rs), (i64 DoubleRegs:$Rt))),
1064         (i1 (MI DoubleRegs:$Rs, DoubleRegs:$Rt))>;
1065
1066 def: T_cmp64_rr_pat<C2_cmpeqp,  seteq>;
1067 def: T_cmp64_rr_pat<C2_cmpgtp,  setgt>;
1068 def: T_cmp64_rr_pat<C2_cmpgtup, setugt>;
1069 def: T_cmp64_rr_pat<C2_cmpgtp,  RevCmp<setlt>>;
1070 def: T_cmp64_rr_pat<C2_cmpgtup, RevCmp<setult>>;
1071
1072 class T_ALU64_rr<string mnemonic, string suffix, bits<4> RegType,
1073                  bits<3> MajOp, bits<3> MinOp, bit OpsRev, bit IsComm,
1074                  string Op2Pfx>
1075   : ALU64_rr<(outs DoubleRegs:$Rd), (ins DoubleRegs:$Rs, DoubleRegs:$Rt),
1076              "$Rd = " #mnemonic# "($Rs, " #Op2Pfx# "$Rt)" #suffix, [],
1077              "", ALU64_tc_1_SLOT23> {
1078   let hasSideEffects = 0;
1079   let isCommutable = IsComm;
1080
1081   bits<5> Rs;
1082   bits<5> Rt;
1083   bits<5> Rd;
1084
1085   let IClass = 0b1101;
1086   let Inst{27-24} = RegType;
1087   let Inst{23-21} = MajOp;
1088   let Inst{20-16} = !if (OpsRev,Rt,Rs);
1089   let Inst{12-8} = !if (OpsRev,Rs,Rt);
1090   let Inst{7-5} = MinOp;
1091   let Inst{4-0} = Rd;
1092 }
1093
1094 class T_ALU64_arith<string mnemonic, bits<3> MajOp, bits<3> MinOp, bit IsSat,
1095                     bit OpsRev, bit IsComm>
1096   : T_ALU64_rr<mnemonic, !if(IsSat,":sat",""), 0b0011, MajOp, MinOp, OpsRev,
1097                IsComm, "">;
1098
1099 let isCodeGenOnly = 0 in {
1100 def A2_addp : T_ALU64_arith<"add", 0b000, 0b111, 0, 0, 1>;
1101 def A2_subp : T_ALU64_arith<"sub", 0b001, 0b111, 0, 1, 0>;
1102 }
1103
1104 def: Pat<(i64 (add I64:$Rs, I64:$Rt)), (A2_addp I64:$Rs, I64:$Rt)>;
1105 def: Pat<(i64 (sub I64:$Rs, I64:$Rt)), (A2_subp I64:$Rs, I64:$Rt)>;
1106
1107 class T_ALU64_logical<string mnemonic, bits<3> MinOp, bit OpsRev, bit IsComm,
1108                       bit IsNeg>
1109   : T_ALU64_rr<mnemonic, "", 0b0011, 0b111, MinOp, OpsRev, IsComm,
1110                !if(IsNeg,"~","")>;
1111
1112 let isCodeGenOnly = 0 in {
1113 def A2_andp : T_ALU64_logical<"and", 0b000, 0, 1, 0>;
1114 def A2_orp  : T_ALU64_logical<"or",  0b010, 0, 1, 0>;
1115 def A2_xorp : T_ALU64_logical<"xor", 0b100, 0, 1, 0>;
1116 }
1117
1118 def: Pat<(i64 (and I64:$Rs, I64:$Rt)), (A2_andp I64:$Rs, I64:$Rt)>;
1119 def: Pat<(i64 (or  I64:$Rs, I64:$Rt)), (A2_orp  I64:$Rs, I64:$Rt)>;
1120 def: Pat<(i64 (xor I64:$Rs, I64:$Rt)), (A2_xorp I64:$Rs, I64:$Rt)>;
1121
1122 //===----------------------------------------------------------------------===//
1123 // ALU64/ALU -
1124 //===----------------------------------------------------------------------===//
1125
1126 //===----------------------------------------------------------------------===//
1127 // ALU64/BIT +
1128 //===----------------------------------------------------------------------===//
1129 //
1130 //===----------------------------------------------------------------------===//
1131 // ALU64/BIT -
1132 //===----------------------------------------------------------------------===//
1133
1134 //===----------------------------------------------------------------------===//
1135 // ALU64/PERM +
1136 //===----------------------------------------------------------------------===//
1137 //
1138 //===----------------------------------------------------------------------===//
1139 // ALU64/PERM -
1140 //===----------------------------------------------------------------------===//
1141
1142 //===----------------------------------------------------------------------===//
1143 // CR +
1144 //===----------------------------------------------------------------------===//
1145 // Logical reductions on predicates.
1146
1147 // Looping instructions.
1148
1149 // Pipelined looping instructions.
1150
1151 // Logical operations on predicates.
1152 let hasSideEffects = 0 in
1153 class T_LOGICAL_1OP<string MnOp, bits<2> OpBits>
1154     : CRInst<(outs PredRegs:$Pd), (ins PredRegs:$Ps),
1155              "$Pd = " # MnOp # "($Ps)", [], "", CR_tc_2early_SLOT23> {
1156   bits<2> Pd;
1157   bits<2> Ps;
1158
1159   let IClass = 0b0110;
1160   let Inst{27-23} = 0b10111;
1161   let Inst{22-21} = OpBits;
1162   let Inst{20} = 0b0;
1163   let Inst{17-16} = Ps;
1164   let Inst{13} = 0b0;
1165   let Inst{1-0} = Pd;
1166 }
1167
1168 let isCodeGenOnly = 0 in {
1169 def C2_any8 : T_LOGICAL_1OP<"any8", 0b00>;
1170 def C2_all8 : T_LOGICAL_1OP<"all8", 0b01>;
1171 def C2_not  : T_LOGICAL_1OP<"not",  0b10>;
1172 }
1173
1174 def: Pat<(i1 (not (i1 PredRegs:$Ps))),
1175          (C2_not PredRegs:$Ps)>;
1176
1177 let hasSideEffects = 0 in
1178 class T_LOGICAL_2OP<string MnOp, bits<3> OpBits, bit IsNeg, bit Rev>
1179     : CRInst<(outs PredRegs:$Pd), (ins PredRegs:$Ps, PredRegs:$Pt),
1180              "$Pd = " # MnOp # "($Ps, " # !if (IsNeg,"!","") # "$Pt)",
1181              [], "", CR_tc_2early_SLOT23> {
1182   bits<2> Pd;
1183   bits<2> Ps;
1184   bits<2> Pt;
1185
1186   let IClass = 0b0110;
1187   let Inst{27-24} = 0b1011;
1188   let Inst{23-21} = OpBits;
1189   let Inst{20} = 0b0;
1190   let Inst{17-16} = !if(Rev,Pt,Ps);  // Rs and Rt are reversed for some
1191   let Inst{13} = 0b0;                // instructions.
1192   let Inst{9-8} = !if(Rev,Ps,Pt);
1193   let Inst{1-0} = Pd;
1194 }
1195
1196 let isCodeGenOnly = 0 in {
1197 def C2_and  : T_LOGICAL_2OP<"and", 0b000, 0, 1>;
1198 def C2_or   : T_LOGICAL_2OP<"or",  0b001, 0, 1>;
1199 def C2_xor  : T_LOGICAL_2OP<"xor", 0b010, 0, 0>;
1200 def C2_andn : T_LOGICAL_2OP<"and", 0b011, 1, 1>;
1201 def C2_orn  : T_LOGICAL_2OP<"or",  0b111, 1, 1>;
1202 }
1203
1204 def: Pat<(i1 (and I1:$Ps, I1:$Pt)),       (C2_and  I1:$Ps, I1:$Pt)>;
1205 def: Pat<(i1 (or  I1:$Ps, I1:$Pt)),       (C2_or   I1:$Ps, I1:$Pt)>;
1206 def: Pat<(i1 (xor I1:$Ps, I1:$Pt)),       (C2_xor  I1:$Ps, I1:$Pt)>;
1207 def: Pat<(i1 (and I1:$Ps, (not I1:$Pt))), (C2_andn I1:$Ps, I1:$Pt)>;
1208 def: Pat<(i1 (or  I1:$Ps, (not I1:$Pt))), (C2_orn  I1:$Ps, I1:$Pt)>;
1209
1210 let hasSideEffects = 0, hasNewValue = 1, isCodeGenOnly = 0 in
1211 def C2_vitpack : SInst<(outs IntRegs:$Rd), (ins PredRegs:$Ps, PredRegs:$Pt),
1212       "$Rd = vitpack($Ps, $Pt)", [], "", S_2op_tc_1_SLOT23> {
1213   bits<5> Rd;
1214   bits<2> Ps;
1215   bits<2> Pt;
1216
1217   let IClass = 0b1000;
1218   let Inst{27-24} = 0b1001;
1219   let Inst{22-21} = 0b00;
1220   let Inst{17-16} = Ps;
1221   let Inst{9-8} = Pt;
1222   let Inst{4-0} = Rd;
1223 }
1224
1225 let hasSideEffects = 0, isCodeGenOnly = 0 in
1226 def C2_mask : SInst<(outs DoubleRegs:$Rd), (ins PredRegs:$Pt),
1227       "$Rd = mask($Pt)", [], "", S_2op_tc_1_SLOT23> {
1228   bits<5> Rd;
1229   bits<2> Pt;
1230
1231   let IClass = 0b1000;
1232   let Inst{27-24} = 0b0110;
1233   let Inst{9-8} = Pt;
1234   let Inst{4-0} = Rd;
1235 }
1236
1237 def VALIGN_rrp : SInst<(outs DoubleRegs:$dst), (ins DoubleRegs:$src1,
1238                                                     DoubleRegs:$src2,
1239                                                     PredRegs:$src3),
1240              "$dst = valignb($src1, $src2, $src3)",
1241              []>;
1242
1243 def VSPLICE_rrp : SInst<(outs DoubleRegs:$dst), (ins DoubleRegs:$src1,
1244                                                      DoubleRegs:$src2,
1245                                                      PredRegs:$src3),
1246              "$dst = vspliceb($src1, $src2, $src3)",
1247              []>;
1248
1249 // User control register transfer.
1250 //===----------------------------------------------------------------------===//
1251 // CR -
1252 //===----------------------------------------------------------------------===//
1253
1254 //===----------------------------------------------------------------------===//
1255 // JR +
1256 //===----------------------------------------------------------------------===//
1257
1258 def retflag : SDNode<"HexagonISD::RET_FLAG", SDTNone,
1259                                [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
1260 def eh_return: SDNode<"HexagonISD::EH_RETURN", SDTNone, [SDNPHasChain]>;
1261
1262 def SDHexagonBR_JT: SDTypeProfile<0, 1, [SDTCisVT<0, i32>]>;
1263 def HexagonBR_JT: SDNode<"HexagonISD::BR_JT", SDHexagonBR_JT, [SDNPHasChain]>;
1264
1265 class CondStr<string CReg, bit True, bit New> {
1266   string S = "if (" # !if(True,"","!") # CReg # !if(New,".new","") # ") ";
1267 }
1268 class JumpOpcStr<string Mnemonic, bit New, bit Taken> {
1269   string S = Mnemonic # !if(New, !if(Taken,":t",":nt"), "");
1270 }
1271
1272 let isBranch = 1, isBarrier = 1, Defs = [PC], hasSideEffects = 0,
1273     isPredicable = 1,
1274     isExtendable = 1, opExtendable = 0, isExtentSigned = 1,
1275     opExtentBits = 24, opExtentAlign = 2, InputType = "imm" in
1276 class T_JMP<string ExtStr>
1277   : JInst<(outs), (ins brtarget:$dst),
1278       "jump " # ExtStr # "$dst",
1279       [], "", J_tc_2early_SLOT23> {
1280     bits<24> dst;
1281     let IClass = 0b0101;
1282
1283     let Inst{27-25} = 0b100;
1284     let Inst{24-16} = dst{23-15};
1285     let Inst{13-1} = dst{14-2};
1286 }
1287
1288 let isBranch = 1, Defs = [PC], hasSideEffects = 0, isPredicated = 1,
1289     isExtendable = 1, opExtendable = 1, isExtentSigned = 1,
1290     opExtentBits = 17, opExtentAlign = 2, InputType = "imm" in
1291 class T_JMP_c<bit PredNot, bit isPredNew, bit isTak, string ExtStr>
1292   : JInst<(outs), (ins PredRegs:$src, brtarget:$dst),
1293       CondStr<"$src", !if(PredNot,0,1), isPredNew>.S #
1294         JumpOpcStr<"jump", isPredNew, isTak>.S # " " #
1295         ExtStr # "$dst",
1296       [], "", J_tc_2early_SLOT23>, ImmRegRel {
1297     let isTaken = isTak;
1298     let isPredicatedFalse = PredNot;
1299     let isPredicatedNew = isPredNew;
1300     bits<2> src;
1301     bits<17> dst;
1302
1303     let IClass = 0b0101;
1304
1305     let Inst{27-24} = 0b1100;
1306     let Inst{21} = PredNot;
1307     let Inst{12} = !if(isPredNew, isTak, zero);
1308     let Inst{11} = isPredNew;
1309     let Inst{9-8} = src;
1310     let Inst{23-22} = dst{16-15};
1311     let Inst{20-16} = dst{14-10};
1312     let Inst{13} = dst{9};
1313     let Inst{7-1} = dst{8-2};
1314   }
1315
1316 multiclass JMP_Pred<bit PredNot, string ExtStr> {
1317   def NAME : T_JMP_c<PredNot, 0, 0, ExtStr>;
1318   // Predicate new
1319   def NAME#newpt : T_JMP_c<PredNot, 1, 1, ExtStr>; // taken
1320   def NAME#new   : T_JMP_c<PredNot, 1, 0, ExtStr>; // not taken
1321 }
1322
1323 multiclass JMP_base<string BaseOp, string ExtStr> {
1324   let BaseOpcode = BaseOp in {
1325     def NAME : T_JMP<ExtStr>;
1326     defm t : JMP_Pred<0, ExtStr>;
1327     defm f : JMP_Pred<1, ExtStr>;
1328   }
1329 }
1330
1331 // Jumps to address stored in a register, JUMPR_MISC
1332 // if ([[!]P[.new]]) jumpr[:t/nt] Rs
1333 let isBranch = 1, isIndirectBranch = 1, isBarrier = 1, Defs = [PC],
1334     isPredicable = 1, hasSideEffects = 0, InputType = "reg" in
1335 class T_JMPr
1336   : JRInst<(outs), (ins IntRegs:$dst),
1337       "jumpr $dst", [], "", J_tc_2early_SLOT2> {
1338     bits<5> dst;
1339
1340     let IClass = 0b0101;
1341     let Inst{27-21} = 0b0010100;
1342     let Inst{20-16} = dst;
1343 }
1344
1345 let isBranch = 1, isIndirectBranch = 1, Defs = [PC], isPredicated = 1,
1346     hasSideEffects = 0, InputType = "reg" in
1347 class T_JMPr_c <bit PredNot, bit isPredNew, bit isTak>
1348   : JRInst <(outs), (ins PredRegs:$src, IntRegs:$dst),
1349       CondStr<"$src", !if(PredNot,0,1), isPredNew>.S #
1350         JumpOpcStr<"jumpr", isPredNew, isTak>.S # " $dst", [],
1351       "", J_tc_2early_SLOT2> {
1352
1353     let isTaken = isTak;
1354     let isPredicatedFalse = PredNot;
1355     let isPredicatedNew = isPredNew;
1356     bits<2> src;
1357     bits<5> dst;
1358
1359     let IClass = 0b0101;
1360
1361     let Inst{27-22} = 0b001101;
1362     let Inst{21} = PredNot;
1363     let Inst{20-16} = dst;
1364     let Inst{12} = !if(isPredNew, isTak, zero);
1365     let Inst{11} = isPredNew;
1366     let Inst{9-8} = src;
1367 }
1368
1369 multiclass JMPR_Pred<bit PredNot> {
1370   def NAME: T_JMPr_c<PredNot, 0, 0>;
1371   // Predicate new
1372   def NAME#newpt  : T_JMPr_c<PredNot, 1, 1>; // taken
1373   def NAME#new    : T_JMPr_c<PredNot, 1, 0>; // not taken
1374 }
1375
1376 multiclass JMPR_base<string BaseOp> {
1377   let BaseOpcode = BaseOp in {
1378     def NAME : T_JMPr;
1379     defm t : JMPR_Pred<0>;
1380     defm f : JMPR_Pred<1>;
1381   }
1382 }
1383
1384 let isCall = 1, hasSideEffects = 1 in
1385 class JUMPR_MISC_CALLR<bit isPred, bit isPredNot,
1386                dag InputDag = (ins IntRegs:$Rs)>
1387   : JRInst<(outs), InputDag,
1388       !if(isPred, !if(isPredNot, "if (!$Pu) callr $Rs",
1389                                  "if ($Pu) callr $Rs"),
1390                                  "callr $Rs"),
1391       [], "", J_tc_2early_SLOT2> {
1392     bits<5> Rs;
1393     bits<2> Pu;
1394     let isPredicated = isPred;
1395     let isPredicatedFalse = isPredNot;
1396
1397     let IClass = 0b0101;
1398     let Inst{27-25} = 0b000;
1399     let Inst{24-23} = !if (isPred, 0b10, 0b01);
1400     let Inst{22} = 0;
1401     let Inst{21} = isPredNot;
1402     let Inst{9-8} = !if (isPred, Pu, 0b00);
1403     let Inst{20-16} = Rs;
1404
1405   }
1406
1407 let Defs = VolatileV3.Regs, isCodeGenOnly = 0 in {
1408   def J2_callrt : JUMPR_MISC_CALLR<1, 0, (ins PredRegs:$Pu, IntRegs:$Rs)>;
1409   def J2_callrf : JUMPR_MISC_CALLR<1, 1, (ins PredRegs:$Pu, IntRegs:$Rs)>;
1410 }
1411
1412 let isTerminator = 1, hasSideEffects = 0, isCodeGenOnly = 0 in {
1413   defm J2_jump : JMP_base<"JMP", "">, PredNewRel;
1414
1415   // Deal with explicit assembly
1416   //  - never extened a jump #,  always extend a jump ##
1417   let isAsmParserOnly = 1 in {
1418     defm J2_jump_ext   : JMP_base<"JMP", "##">;
1419     defm J2_jump_noext : JMP_base<"JMP", "#">;
1420   }
1421
1422   defm J2_jumpr : JMPR_base<"JMPr">, PredNewRel;
1423
1424   let isReturn = 1, isCodeGenOnly = 1 in
1425   defm JMPret : JMPR_base<"JMPret">, PredNewRel;
1426 }
1427
1428 def: Pat<(br bb:$dst),
1429          (J2_jump brtarget:$dst)>;
1430 def: Pat<(retflag),
1431          (JMPret (i32 R31))>;
1432 def: Pat<(brcond (i1 PredRegs:$src1), bb:$offset),
1433          (J2_jumpt PredRegs:$src1, bb:$offset)>;
1434
1435 // A return through builtin_eh_return.
1436 let isReturn = 1, isTerminator = 1, isBarrier = 1, hasSideEffects = 0,
1437     isCodeGenOnly = 1, Defs = [PC], Uses = [R28], isPredicable = 0 in
1438 def EH_RETURN_JMPR : T_JMPr;
1439
1440 def: Pat<(eh_return),
1441          (EH_RETURN_JMPR (i32 R31))>;
1442 def: Pat<(HexagonBR_JT (i32 IntRegs:$dst)),
1443          (J2_jumpr IntRegs:$dst)>;
1444 def: Pat<(brind (i32 IntRegs:$dst)),
1445          (J2_jumpr IntRegs:$dst)>;
1446
1447 //===----------------------------------------------------------------------===//
1448 // JR -
1449 //===----------------------------------------------------------------------===//
1450
1451 //===----------------------------------------------------------------------===//
1452 // LD +
1453 //===----------------------------------------------------------------------===//
1454 let isExtendable = 1, opExtendable = 2, isExtentSigned = 1, AddedComplexity = 20 in
1455 class T_load_io <string mnemonic, RegisterClass RC, bits<4> MajOp,
1456                  Operand ImmOp>
1457   : LDInst<(outs RC:$dst), (ins IntRegs:$src1, ImmOp:$offset),
1458   "$dst = "#mnemonic#"($src1 + #$offset)", []>, AddrModeRel {
1459     bits<4> name;
1460     bits<5> dst;
1461     bits<5> src1;
1462     bits<14> offset;
1463     bits<11> offsetBits;
1464
1465     string ImmOpStr = !cast<string>(ImmOp);
1466     let offsetBits = !if (!eq(ImmOpStr, "s11_3Ext"), offset{13-3},
1467                      !if (!eq(ImmOpStr, "s11_2Ext"), offset{12-2},
1468                      !if (!eq(ImmOpStr, "s11_1Ext"), offset{11-1},
1469                                       /* s11_0Ext */ offset{10-0})));
1470     let opExtentBits = !if (!eq(ImmOpStr, "s11_3Ext"), 14,
1471                        !if (!eq(ImmOpStr, "s11_2Ext"), 13,
1472                        !if (!eq(ImmOpStr, "s11_1Ext"), 12,
1473                                         /* s11_0Ext */ 11)));
1474     let hasNewValue = !if (!eq(ImmOpStr, "s11_3Ext"), 0, 1);
1475
1476     let IClass = 0b1001;
1477
1478     let Inst{27}    = 0b0;
1479     let Inst{26-25} = offsetBits{10-9};
1480     let Inst{24-21} = MajOp;
1481     let Inst{20-16} = src1;
1482     let Inst{13-5}  = offsetBits{8-0};
1483     let Inst{4-0}   = dst;
1484   }
1485
1486 let opExtendable = 3, isExtentSigned = 0, isPredicated = 1 in
1487 class T_pload_io <string mnemonic, RegisterClass RC, bits<4>MajOp,
1488                   Operand ImmOp, bit isNot, bit isPredNew>
1489   : LDInst<(outs RC:$dst),
1490            (ins PredRegs:$src1, IntRegs:$src2, ImmOp:$offset),
1491   "if ("#!if(isNot, "!$src1", "$src1")
1492        #!if(isPredNew, ".new", "")
1493        #") $dst = "#mnemonic#"($src2 + #$offset)",
1494   [],"", V2LDST_tc_ld_SLOT01> , AddrModeRel {
1495     bits<5> dst;
1496     bits<2> src1;
1497     bits<5> src2;
1498     bits<9> offset;
1499     bits<6> offsetBits;
1500     string ImmOpStr = !cast<string>(ImmOp);
1501
1502     let offsetBits = !if (!eq(ImmOpStr, "u6_3Ext"), offset{8-3},
1503                      !if (!eq(ImmOpStr, "u6_2Ext"), offset{7-2},
1504                      !if (!eq(ImmOpStr, "u6_1Ext"), offset{6-1},
1505                                       /* u6_0Ext */ offset{5-0})));
1506     let opExtentBits = !if (!eq(ImmOpStr, "u6_3Ext"), 9,
1507                        !if (!eq(ImmOpStr, "u6_2Ext"), 8,
1508                        !if (!eq(ImmOpStr, "u6_1Ext"), 7,
1509                                         /* u6_0Ext */ 6)));
1510     let hasNewValue = !if (!eq(ImmOpStr, "u6_3Ext"), 0, 1);
1511     let isPredicatedNew = isPredNew;
1512     let isPredicatedFalse = isNot;
1513
1514     let IClass = 0b0100;
1515
1516     let Inst{27}    = 0b0;
1517     let Inst{27}    = 0b0;
1518     let Inst{26}    = isNot;
1519     let Inst{25}    = isPredNew;
1520     let Inst{24-21} = MajOp;
1521     let Inst{20-16} = src2;
1522     let Inst{13}    = 0b0;
1523     let Inst{12-11} = src1;
1524     let Inst{10-5}  = offsetBits;
1525     let Inst{4-0}   = dst;
1526   }
1527
1528 let isExtendable = 1, hasSideEffects = 0, addrMode = BaseImmOffset in
1529 multiclass LD_Idxd<string mnemonic, string CextOp, RegisterClass RC,
1530                    Operand ImmOp, Operand predImmOp, bits<4>MajOp> {
1531   let CextOpcode = CextOp, BaseOpcode = CextOp#_indexed in {
1532     let isPredicable = 1 in
1533     def L2_#NAME#_io : T_load_io <mnemonic, RC, MajOp, ImmOp>;
1534
1535     // Predicated
1536     def L2_p#NAME#t_io : T_pload_io <mnemonic, RC, MajOp, predImmOp, 0, 0>;
1537     def L2_p#NAME#f_io : T_pload_io <mnemonic, RC, MajOp, predImmOp, 1, 0>;
1538
1539     // Predicated new
1540     def L2_p#NAME#tnew_io : T_pload_io <mnemonic, RC, MajOp, predImmOp, 0, 1>;
1541     def L2_p#NAME#fnew_io : T_pload_io <mnemonic, RC, MajOp, predImmOp, 1, 1>;
1542   }
1543 }
1544
1545 let accessSize = ByteAccess, isCodeGenOnly = 0 in {
1546   defm loadrb:  LD_Idxd <"memb", "LDrib", IntRegs, s11_0Ext, u6_0Ext, 0b1000>;
1547   defm loadrub: LD_Idxd <"memub", "LDriub", IntRegs, s11_0Ext, u6_0Ext, 0b1001>;
1548 }
1549
1550 let accessSize = HalfWordAccess, opExtentAlign = 1, isCodeGenOnly = 0 in {
1551   defm loadrh:  LD_Idxd <"memh", "LDrih", IntRegs, s11_1Ext, u6_1Ext, 0b1010>;
1552   defm loadruh: LD_Idxd <"memuh", "LDriuh", IntRegs, s11_1Ext, u6_1Ext, 0b1011>;
1553 }
1554
1555 let accessSize = WordAccess, opExtentAlign = 2, isCodeGenOnly = 0 in
1556 defm loadri: LD_Idxd <"memw", "LDriw", IntRegs, s11_2Ext, u6_2Ext, 0b1100>;
1557
1558 let accessSize = DoubleWordAccess, opExtentAlign = 3, isCodeGenOnly = 0 in
1559 defm loadrd: LD_Idxd <"memd", "LDrid", DoubleRegs, s11_3Ext, u6_3Ext, 0b1110>;
1560
1561 // Patterns to select load-indexed (i.e. load from base+offset).
1562 multiclass Loadx_pat<PatFrag Load, ValueType VT, PatLeaf ImmPred,
1563                      InstHexagon MI> {
1564   def: Pat<(VT (Load AddrFI:$fi)), (VT (MI AddrFI:$fi, 0))>;
1565   def: Pat<(VT (Load (add (i32 IntRegs:$Rs), ImmPred:$Off))),
1566            (VT (MI IntRegs:$Rs, imm:$Off))>;
1567   def: Pat<(VT (Load (i32 IntRegs:$Rs))), (VT (MI IntRegs:$Rs, 0))>;
1568 }
1569
1570 def : Pat < (i32 (sextloadi8 ADDRriS11_0:$addr)),
1571             (L2_loadrb_io AddrFI:$addr, 0) >;
1572
1573 def : Pat < (i32 (zextloadi8 ADDRriS11_0:$addr)),
1574             (L2_loadrub_io AddrFI:$addr, 0) >;
1575
1576 def : Pat < (i32 (sextloadi16 ADDRriS11_1:$addr)),
1577             (L2_loadrh_io AddrFI:$addr, 0) >;
1578
1579 def : Pat < (i32 (zextloadi16 ADDRriS11_1:$addr)),
1580             (L2_loadruh_io AddrFI:$addr, 0) >;
1581
1582 def : Pat < (i32 (load ADDRriS11_2:$addr)),
1583             (L2_loadri_io AddrFI:$addr, 0) >;
1584
1585 def : Pat < (i64 (load ADDRriS11_3:$addr)),
1586             (L2_loadrd_io AddrFI:$addr, 0) >;
1587
1588 let AddedComplexity = 20 in {
1589 def : Pat < (i32 (sextloadi8 (add IntRegs:$src1, s11_0ExtPred:$offset))),
1590             (L2_loadrb_io IntRegs:$src1, s11_0ExtPred:$offset) >;
1591
1592 def : Pat < (i32 (zextloadi8 (add IntRegs:$src1, s11_0ExtPred:$offset))),
1593             (L2_loadrub_io IntRegs:$src1, s11_0ExtPred:$offset) >;
1594
1595 def : Pat < (i32 (sextloadi16 (add IntRegs:$src1, s11_1ExtPred:$offset))),
1596             (L2_loadrh_io IntRegs:$src1, s11_1ExtPred:$offset) >;
1597
1598 def : Pat < (i32 (zextloadi16 (add IntRegs:$src1, s11_1ExtPred:$offset))),
1599             (L2_loadruh_io IntRegs:$src1, s11_1ExtPred:$offset) >;
1600
1601 def : Pat < (i32 (load (add IntRegs:$src1, s11_2ExtPred:$offset))),
1602             (L2_loadri_io IntRegs:$src1, s11_2ExtPred:$offset) >;
1603
1604 def : Pat < (i64 (load (add IntRegs:$src1, s11_3ExtPred:$offset))),
1605             (L2_loadrd_io IntRegs:$src1, s11_3ExtPred:$offset) >;
1606 }
1607
1608 //===----------------------------------------------------------------------===//
1609 // Post increment load
1610 //===----------------------------------------------------------------------===//
1611 //===----------------------------------------------------------------------===//
1612 // Template class for non-predicated post increment loads with immediate offset.
1613 //===----------------------------------------------------------------------===//
1614 let hasSideEffects = 0, addrMode = PostInc in
1615 class T_load_pi <string mnemonic, RegisterClass RC, Operand ImmOp,
1616                      bits<4> MajOp >
1617   : LDInstPI <(outs RC:$dst, IntRegs:$dst2),
1618   (ins IntRegs:$src1, ImmOp:$offset),
1619   "$dst = "#mnemonic#"($src1++#$offset)" ,
1620   [],
1621   "$src1 = $dst2" > ,
1622   PredNewRel {
1623     bits<5> dst;
1624     bits<5> src1;
1625     bits<7> offset;
1626     bits<4> offsetBits;
1627
1628     string ImmOpStr = !cast<string>(ImmOp);
1629     let offsetBits = !if (!eq(ImmOpStr, "s4_3Imm"), offset{6-3},
1630                      !if (!eq(ImmOpStr, "s4_2Imm"), offset{5-2},
1631                      !if (!eq(ImmOpStr, "s4_1Imm"), offset{4-1},
1632                                       /* s4_0Imm */ offset{3-0})));
1633     let hasNewValue = !if (!eq(ImmOpStr, "s4_3Imm"), 0, 1);
1634
1635     let IClass = 0b1001;
1636
1637     let Inst{27-25} = 0b101;
1638     let Inst{24-21} = MajOp;
1639     let Inst{20-16} = src1;
1640     let Inst{13-12} = 0b00;
1641     let Inst{8-5} = offsetBits;
1642     let Inst{4-0}   = dst;
1643   }
1644
1645 //===----------------------------------------------------------------------===//
1646 // Template class for predicated post increment loads with immediate offset.
1647 //===----------------------------------------------------------------------===//
1648 let isPredicated = 1, hasSideEffects = 0, addrMode = PostInc in
1649 class T_pload_pi <string mnemonic, RegisterClass RC, Operand ImmOp,
1650                           bits<4> MajOp, bit isPredNot, bit isPredNew >
1651   : LDInst <(outs RC:$dst, IntRegs:$dst2),
1652             (ins PredRegs:$src1, IntRegs:$src2, ImmOp:$offset),
1653   !if(isPredNot, "if (!$src1", "if ($src1")#!if(isPredNew, ".new) ",
1654   ") ")#"$dst = "#mnemonic#"($src2++#$offset)",
1655   [] ,
1656   "$src2 = $dst2" > ,
1657   PredNewRel {
1658     bits<5> dst;
1659     bits<2> src1;
1660     bits<5> src2;
1661     bits<7> offset;
1662     bits<4> offsetBits;
1663
1664     let isPredicatedNew = isPredNew;
1665     let isPredicatedFalse = isPredNot;
1666
1667     string ImmOpStr = !cast<string>(ImmOp);
1668     let offsetBits = !if (!eq(ImmOpStr, "s4_3Imm"), offset{6-3},
1669                      !if (!eq(ImmOpStr, "s4_2Imm"), offset{5-2},
1670                      !if (!eq(ImmOpStr, "s4_1Imm"), offset{4-1},
1671                                       /* s4_0Imm */ offset{3-0})));
1672     let hasNewValue = !if (!eq(ImmOpStr, "s4_3Imm"), 0, 1);
1673
1674     let IClass = 0b1001;
1675
1676     let Inst{27-25} = 0b101;
1677     let Inst{24-21} = MajOp;
1678     let Inst{20-16} = src2;
1679     let Inst{13} = 0b1;
1680     let Inst{12} = isPredNew;
1681     let Inst{11} = isPredNot;
1682     let Inst{10-9} = src1;
1683     let Inst{8-5}  = offsetBits;
1684     let Inst{4-0}  = dst;
1685   }
1686
1687 //===----------------------------------------------------------------------===//
1688 // Multiclass for post increment loads with immediate offset.
1689 //===----------------------------------------------------------------------===//
1690
1691 multiclass LD_PostInc <string mnemonic, string BaseOp, RegisterClass RC,
1692                        Operand ImmOp, bits<4> MajOp> {
1693   let BaseOpcode = "POST_"#BaseOp in {
1694     let isPredicable = 1 in
1695     def L2_#NAME#_pi : T_load_pi < mnemonic, RC, ImmOp, MajOp>;
1696
1697     // Predicated
1698     def L2_p#NAME#t_pi : T_pload_pi < mnemonic, RC, ImmOp, MajOp, 0, 0>;
1699     def L2_p#NAME#f_pi : T_pload_pi < mnemonic, RC, ImmOp, MajOp, 1, 0>;
1700
1701     // Predicated new
1702     def L2_p#NAME#tnew_pi : T_pload_pi < mnemonic, RC, ImmOp, MajOp, 0, 1>;
1703     def L2_p#NAME#fnew_pi : T_pload_pi < mnemonic, RC, ImmOp, MajOp, 1, 1>;
1704   }
1705 }
1706
1707 // post increment byte loads with immediate offset
1708 let accessSize = ByteAccess, isCodeGenOnly = 0 in {
1709   defm loadrb  : LD_PostInc <"memb",  "LDrib", IntRegs, s4_0Imm, 0b1000>;
1710   defm loadrub : LD_PostInc <"memub", "LDriub", IntRegs, s4_0Imm, 0b1001>;
1711 }
1712
1713 // post increment halfword loads with immediate offset
1714 let accessSize = HalfWordAccess, opExtentAlign = 1, isCodeGenOnly = 0 in {
1715   defm loadrh  : LD_PostInc <"memh",  "LDrih", IntRegs, s4_1Imm, 0b1010>;
1716   defm loadruh : LD_PostInc <"memuh", "LDriuh", IntRegs, s4_1Imm, 0b1011>;
1717 }
1718
1719 // post increment word loads with immediate offset
1720 let accessSize = WordAccess, opExtentAlign = 2, isCodeGenOnly = 0 in
1721 defm loadri : LD_PostInc <"memw", "LDriw", IntRegs, s4_2Imm, 0b1100>;
1722
1723 // post increment doubleword loads with immediate offset
1724 let accessSize = DoubleWordAccess, opExtentAlign = 3, isCodeGenOnly = 0 in
1725 defm loadrd : LD_PostInc <"memd", "LDrid", DoubleRegs, s4_3Imm, 0b1110>;
1726
1727 def : Pat< (i32 (extloadi1 ADDRriS11_0:$addr)),
1728            (i32 (L2_loadrb_io AddrFI:$addr, 0)) >;
1729
1730 // Load byte any-extend.
1731 def : Pat < (i32 (extloadi8 ADDRriS11_0:$addr)),
1732             (i32 (L2_loadrb_io AddrFI:$addr, 0)) >;
1733
1734 // Indexed load byte any-extend.
1735 let AddedComplexity = 20 in
1736 def : Pat < (i32 (extloadi8 (add IntRegs:$src1, s11_0ImmPred:$offset))),
1737             (i32 (L2_loadrb_io IntRegs:$src1, s11_0ImmPred:$offset)) >;
1738
1739 def : Pat < (i32 (extloadi16 ADDRriS11_1:$addr)),
1740             (i32 (L2_loadrh_io AddrFI:$addr, 0))>;
1741
1742 let AddedComplexity = 20 in
1743 def : Pat < (i32 (extloadi16 (add IntRegs:$src1, s11_1ImmPred:$offset))),
1744             (i32 (L2_loadrh_io IntRegs:$src1, s11_1ImmPred:$offset)) >;
1745
1746 let AddedComplexity = 10 in
1747 def : Pat < (i32 (zextloadi1 ADDRriS11_0:$addr)),
1748             (i32 (L2_loadrub_io AddrFI:$addr, 0))>;
1749
1750 let AddedComplexity = 20 in
1751 def : Pat < (i32 (zextloadi1 (add IntRegs:$src1, s11_0ImmPred:$offset))),
1752             (i32 (L2_loadrub_io IntRegs:$src1, s11_0ImmPred:$offset))>;
1753
1754 //===----------------------------------------------------------------------===//
1755 // Template class for post increment loads with register offset.
1756 //===----------------------------------------------------------------------===//
1757 let hasSideEffects = 0, addrMode = PostInc in
1758 class T_load_pr <string mnemonic, RegisterClass RC, bits<4> MajOp,
1759                        MemAccessSize AccessSz>
1760   : LDInstPI <(outs RC:$dst, IntRegs:$_dst_),
1761               (ins IntRegs:$src1, ModRegs:$src2),
1762   "$dst = "#mnemonic#"($src1++$src2)" ,
1763   [], "$src1 = $_dst_" > {
1764     bits<5> dst;
1765     bits<5> src1;
1766     bits<1> src2;
1767
1768     let accessSize = AccessSz;
1769     let IClass = 0b1001;
1770
1771     let Inst{27-25} = 0b110;
1772     let Inst{24-21} = MajOp;
1773     let Inst{20-16} = src1;
1774     let Inst{13}    = src2;
1775     let Inst{12}    = 0b0;
1776     let Inst{7}     = 0b0;
1777     let Inst{4-0}   = dst;
1778   }
1779
1780 let hasNewValue = 1, isCodeGenOnly = 0 in {
1781   def L2_loadrb_pr  : T_load_pr <"memb",  IntRegs, 0b1000, ByteAccess>;
1782   def L2_loadrub_pr : T_load_pr <"memub", IntRegs, 0b1001, ByteAccess>;
1783   def L2_loadrh_pr  : T_load_pr <"memh",  IntRegs, 0b1010, HalfWordAccess>;
1784   def L2_loadruh_pr : T_load_pr <"memuh", IntRegs, 0b1011, HalfWordAccess>;
1785   def L2_loadri_pr  : T_load_pr <"memw",  IntRegs, 0b1100, WordAccess>;
1786 }
1787
1788 let isCodeGenOnly = 0 in
1789 def L2_loadrd_pr   : T_load_pr <"memd", DoubleRegs, 0b1110, DoubleWordAccess>;
1790
1791 // Load predicate.
1792 let isExtendable = 1, opExtendable = 2, isExtentSigned = 1, opExtentBits = 13,
1793 isPseudo = 1, Defs = [R10,R11,D5], hasSideEffects = 0 in
1794 def LDriw_pred : LDInst2<(outs PredRegs:$dst),
1795             (ins MEMri:$addr),
1796             "Error; should not emit",
1797             []>;
1798
1799 let Defs = [R29, R30, R31], Uses = [R30], hasSideEffects = 0, isCodeGenOnly = 0 in
1800   def L2_deallocframe : LDInst<(outs), (ins),
1801                      "deallocframe",
1802                      []> {
1803     let IClass = 0b1001;
1804
1805     let Inst{27-16} = 0b000000011110;
1806     let Inst{13} = 0b0;
1807     let Inst{4-0} = 0b11110;
1808 }
1809
1810 // Load / Post increment circular addressing mode.
1811 let Uses = [CS], hasSideEffects = 0, hasNewValue = 1, opNewValue = 0 in
1812 class T_load_pcr<string mnemonic, RegisterClass RC, bits<4> MajOp>
1813   : LDInst <(outs RC:$dst, IntRegs:$_dst_),
1814             (ins IntRegs:$Rz, ModRegs:$Mu),
1815   "$dst = "#mnemonic#"($Rz ++ I:circ($Mu))", [],
1816   "$Rz = $_dst_" > {
1817     bits<5> dst;
1818     bits<5> Rz;
1819     bit Mu;
1820
1821     let IClass = 0b1001;
1822
1823     let Inst{27-25} = 0b100;
1824     let Inst{24-21} = MajOp;
1825     let Inst{20-16} = Rz;
1826     let Inst{13} = Mu;
1827     let Inst{12} = 0b0;
1828     let Inst{9} = 0b1;
1829     let Inst{7} = 0b0;
1830     let Inst{4-0} = dst;
1831  }
1832
1833 let accessSize = ByteAccess, isCodeGenOnly = 0 in {
1834   def L2_loadrb_pcr  : T_load_pcr <"memb",  IntRegs, 0b1000>;
1835   def L2_loadrub_pcr : T_load_pcr <"memub", IntRegs, 0b1001>;
1836 }
1837
1838 let accessSize = HalfWordAccess, isCodeGenOnly = 0 in {
1839   def L2_loadrh_pcr   : T_load_pcr <"memh",   IntRegs, 0b1010>;
1840   def L2_loadruh_pcr  : T_load_pcr <"memuh",  IntRegs, 0b1011>;
1841 }
1842
1843 let accessSize = WordAccess, isCodeGenOnly = 0 in {
1844   def  L2_loadri_pcr  : T_load_pcr <"memw", IntRegs, 0b1100>;
1845 }
1846
1847 let accessSize = DoubleWordAccess, isCodeGenOnly = 0 in
1848 def L2_loadrd_pcr  : T_load_pcr <"memd", DoubleRegs, 0b1110>;
1849
1850 //===----------------------------------------------------------------------===//
1851 // Circular loads with immediate offset.
1852 //===----------------------------------------------------------------------===//
1853 let Uses = [CS], mayLoad = 1, hasSideEffects = 0, hasNewValue = 1 in
1854 class T_load_pci <string mnemonic, RegisterClass RC,
1855                   Operand ImmOp, bits<4> MajOp>
1856   : LDInstPI<(outs RC:$dst, IntRegs:$_dst_),
1857              (ins IntRegs:$Rz, ImmOp:$offset, ModRegs:$Mu),
1858   "$dst = "#mnemonic#"($Rz ++ #$offset:circ($Mu))", [],
1859   "$Rz = $_dst_"> {
1860     bits<5> dst;
1861     bits<5> Rz;
1862     bits<1> Mu;
1863     bits<7> offset;
1864     bits<4> offsetBits;
1865
1866     string ImmOpStr = !cast<string>(ImmOp);
1867     let offsetBits = !if (!eq(ImmOpStr, "s4_3Imm"), offset{6-3},
1868                      !if (!eq(ImmOpStr, "s4_2Imm"), offset{5-2},
1869                      !if (!eq(ImmOpStr, "s4_1Imm"), offset{4-1},
1870                                       /* s4_0Imm */ offset{3-0})));
1871     let IClass      = 0b1001;
1872     let Inst{27-25} = 0b100;
1873     let Inst{24-21} = MajOp;
1874     let Inst{20-16} = Rz;
1875     let Inst{13}    = Mu;
1876     let Inst{12}    = 0b0;
1877     let Inst{9}     = 0b0;
1878     let Inst{8-5}   = offsetBits;
1879     let Inst{4-0}   = dst;
1880   }
1881
1882 // Byte variants of circ load
1883 let accessSize = ByteAccess, isCodeGenOnly = 0 in {
1884   def L2_loadrb_pci  : T_load_pci <"memb",  IntRegs, s4_0Imm, 0b1000>;
1885   def L2_loadrub_pci : T_load_pci <"memub", IntRegs, s4_0Imm, 0b1001>;
1886 }
1887
1888 // Half word variants of circ load
1889 let accessSize = HalfWordAccess, isCodeGenOnly = 0 in {
1890   def L2_loadrh_pci   : T_load_pci <"memh",   IntRegs, s4_1Imm, 0b1010>;
1891   def L2_loadruh_pci  : T_load_pci <"memuh",  IntRegs, s4_1Imm, 0b1011>;
1892 }
1893
1894 // Word variants of circ load
1895 let accessSize = WordAccess, isCodeGenOnly = 0 in
1896 def L2_loadri_pci   : T_load_pci <"memw",   IntRegs,    s4_2Imm, 0b1100>;
1897
1898 let accessSize = DoubleWordAccess, hasNewValue = 0, isCodeGenOnly = 0 in
1899 def L2_loadrd_pci : T_load_pci <"memd", DoubleRegs, s4_3Imm, 0b1110>;
1900
1901 // L[24]_load[wd]_locked: Load word/double with lock.
1902 let isSoloAX = 1 in
1903 class T_load_locked <string mnemonic, RegisterClass RC>
1904   : LD0Inst <(outs RC:$dst),
1905              (ins IntRegs:$src),
1906     "$dst = "#mnemonic#"($src)"> {
1907     bits<5> dst;
1908     bits<5> src;
1909     let IClass = 0b1001;
1910     let Inst{27-21} = 0b0010000;
1911     let Inst{20-16} = src;
1912     let Inst{13-12} = !if (!eq(mnemonic, "memd_locked"), 0b01, 0b00);
1913     let Inst{4-0} = dst;
1914 }
1915 let hasNewValue = 1, accessSize = WordAccess, opNewValue = 0, isCodeGenOnly = 0 in
1916   def L2_loadw_locked : T_load_locked <"memw_locked", IntRegs>;
1917 let accessSize = DoubleWordAccess, isCodeGenOnly = 0 in
1918   def L4_loadd_locked : T_load_locked <"memd_locked", DoubleRegs>;
1919 //===----------------------------------------------------------------------===//
1920 // Bit-reversed loads with auto-increment register
1921 //===----------------------------------------------------------------------===//
1922 let hasSideEffects = 0 in
1923 class T_load_pbr<string mnemonic, RegisterClass RC,
1924                             MemAccessSize addrSize, bits<4> majOp>
1925   : LDInst
1926     <(outs RC:$dst, IntRegs:$_dst_),
1927      (ins IntRegs:$Rz, ModRegs:$Mu),
1928      "$dst = "#mnemonic#"($Rz ++ $Mu:brev)" ,
1929       [] , "$Rz = $_dst_" > {
1930
1931       let accessSize = addrSize;
1932
1933       bits<5> dst;
1934       bits<5> Rz;
1935       bits<1> Mu;
1936
1937       let IClass = 0b1001;
1938
1939       let Inst{27-25} = 0b111;
1940       let Inst{24-21} = majOp;
1941       let Inst{20-16} = Rz;
1942       let Inst{13} = Mu;
1943       let Inst{12} = 0b0;
1944       let Inst{7} = 0b0;
1945       let Inst{4-0} = dst;
1946   }
1947
1948 let hasNewValue =1, opNewValue = 0, isCodeGenOnly = 0 in {
1949   def L2_loadrb_pbr   : T_load_pbr <"memb",  IntRegs, ByteAccess, 0b1000>;
1950   def L2_loadrub_pbr  : T_load_pbr <"memub", IntRegs, ByteAccess, 0b1001>;
1951   def L2_loadrh_pbr   : T_load_pbr <"memh",  IntRegs, HalfWordAccess, 0b1010>;
1952   def L2_loadruh_pbr  : T_load_pbr <"memuh", IntRegs, HalfWordAccess, 0b1011>;
1953   def L2_loadri_pbr : T_load_pbr <"memw", IntRegs, WordAccess, 0b1100>;
1954 }
1955
1956 let isCodeGenOnly = 0 in
1957 def L2_loadrd_pbr : T_load_pbr <"memd", DoubleRegs, DoubleWordAccess, 0b1110>;
1958
1959 //===----------------------------------------------------------------------===//
1960 // LD -
1961 //===----------------------------------------------------------------------===//
1962
1963 //===----------------------------------------------------------------------===//
1964 // MTYPE/ALU +
1965 //===----------------------------------------------------------------------===//
1966 //===----------------------------------------------------------------------===//
1967 // MTYPE/ALU -
1968 //===----------------------------------------------------------------------===//
1969
1970 //===----------------------------------------------------------------------===//
1971 // MTYPE/COMPLEX +
1972 //===----------------------------------------------------------------------===//
1973 //===----------------------------------------------------------------------===//
1974 // MTYPE/COMPLEX -
1975 //===----------------------------------------------------------------------===//
1976
1977 //===----------------------------------------------------------------------===//
1978 // MTYPE/MPYH +
1979 //===----------------------------------------------------------------------===//
1980
1981 //===----------------------------------------------------------------------===//
1982 // Template Class
1983 // MPYS / Multipy signed/unsigned halfwords
1984 //Rd=mpy[u](Rs.[H|L],Rt.[H|L])[:<<1][:rnd][:sat]
1985 //===----------------------------------------------------------------------===//
1986
1987 let hasNewValue = 1, opNewValue = 0 in
1988 class T_M2_mpy < bits<2> LHbits, bit isSat, bit isRnd,
1989                  bit hasShift, bit isUnsigned>
1990   : MInst < (outs IntRegs:$Rd), (ins IntRegs:$Rs, IntRegs:$Rt),
1991   "$Rd = "#!if(isUnsigned,"mpyu","mpy")#"($Rs."#!if(LHbits{1},"h","l")
1992                                        #", $Rt."#!if(LHbits{0},"h)","l)")
1993                                        #!if(hasShift,":<<1","")
1994                                        #!if(isRnd,":rnd","")
1995                                        #!if(isSat,":sat",""),
1996   [], "", M_tc_3x_SLOT23 > {
1997     bits<5> Rd;
1998     bits<5> Rs;
1999     bits<5> Rt;
2000
2001     let IClass = 0b1110;
2002
2003     let Inst{27-24} = 0b1100;
2004     let Inst{23} = hasShift;
2005     let Inst{22} = isUnsigned;
2006     let Inst{21} = isRnd;
2007     let Inst{7} = isSat;
2008     let Inst{6-5} = LHbits;
2009     let Inst{4-0} = Rd;
2010     let Inst{20-16} = Rs;
2011     let Inst{12-8} = Rt;
2012   }
2013
2014 //Rd=mpy(Rs.[H|L],Rt.[H|L])[:<<1]
2015 let isCodeGenOnly = 0 in {
2016 def M2_mpy_ll_s1: T_M2_mpy<0b00, 0, 0, 1, 0>;
2017 def M2_mpy_ll_s0: T_M2_mpy<0b00, 0, 0, 0, 0>;
2018 def M2_mpy_lh_s1: T_M2_mpy<0b01, 0, 0, 1, 0>;
2019 def M2_mpy_lh_s0: T_M2_mpy<0b01, 0, 0, 0, 0>;
2020 def M2_mpy_hl_s1: T_M2_mpy<0b10, 0, 0, 1, 0>;
2021 def M2_mpy_hl_s0: T_M2_mpy<0b10, 0, 0, 0, 0>;
2022 def M2_mpy_hh_s1: T_M2_mpy<0b11, 0, 0, 1, 0>;
2023 def M2_mpy_hh_s0: T_M2_mpy<0b11, 0, 0, 0, 0>;
2024 }
2025
2026 //Rd=mpyu(Rs.[H|L],Rt.[H|L])[:<<1]
2027 let isCodeGenOnly = 0 in {
2028 def M2_mpyu_ll_s1: T_M2_mpy<0b00, 0, 0, 1, 1>;
2029 def M2_mpyu_ll_s0: T_M2_mpy<0b00, 0, 0, 0, 1>;
2030 def M2_mpyu_lh_s1: T_M2_mpy<0b01, 0, 0, 1, 1>;
2031 def M2_mpyu_lh_s0: T_M2_mpy<0b01, 0, 0, 0, 1>;
2032 def M2_mpyu_hl_s1: T_M2_mpy<0b10, 0, 0, 1, 1>;
2033 def M2_mpyu_hl_s0: T_M2_mpy<0b10, 0, 0, 0, 1>;
2034 def M2_mpyu_hh_s1: T_M2_mpy<0b11, 0, 0, 1, 1>;
2035 def M2_mpyu_hh_s0: T_M2_mpy<0b11, 0, 0, 0, 1>;
2036 }
2037
2038 //Rd=mpy(Rs.[H|L],Rt.[H|L])[:<<1]:rnd
2039 let isCodeGenOnly = 0 in {
2040 def M2_mpy_rnd_ll_s1: T_M2_mpy <0b00, 0, 1, 1, 0>;
2041 def M2_mpy_rnd_ll_s0: T_M2_mpy <0b00, 0, 1, 0, 0>;
2042 def M2_mpy_rnd_lh_s1: T_M2_mpy <0b01, 0, 1, 1, 0>;
2043 def M2_mpy_rnd_lh_s0: T_M2_mpy <0b01, 0, 1, 0, 0>;
2044 def M2_mpy_rnd_hl_s1: T_M2_mpy <0b10, 0, 1, 1, 0>;
2045 def M2_mpy_rnd_hl_s0: T_M2_mpy <0b10, 0, 1, 0, 0>;
2046 def M2_mpy_rnd_hh_s1: T_M2_mpy <0b11, 0, 1, 1, 0>;
2047 def M2_mpy_rnd_hh_s0: T_M2_mpy <0b11, 0, 1, 0, 0>;
2048 }
2049
2050 //Rd=mpy(Rs.[H|L],Rt.[H|L])[:<<1][:sat]
2051 //Rd=mpy(Rs.[H|L],Rt.[H|L])[:<<1][:rnd][:sat]
2052 let Defs = [USR_OVF], isCodeGenOnly = 0 in {
2053   def M2_mpy_sat_ll_s1: T_M2_mpy <0b00, 1, 0, 1, 0>;
2054   def M2_mpy_sat_ll_s0: T_M2_mpy <0b00, 1, 0, 0, 0>;
2055   def M2_mpy_sat_lh_s1: T_M2_mpy <0b01, 1, 0, 1, 0>;
2056   def M2_mpy_sat_lh_s0: T_M2_mpy <0b01, 1, 0, 0, 0>;
2057   def M2_mpy_sat_hl_s1: T_M2_mpy <0b10, 1, 0, 1, 0>;
2058   def M2_mpy_sat_hl_s0: T_M2_mpy <0b10, 1, 0, 0, 0>;
2059   def M2_mpy_sat_hh_s1: T_M2_mpy <0b11, 1, 0, 1, 0>;
2060   def M2_mpy_sat_hh_s0: T_M2_mpy <0b11, 1, 0, 0, 0>;
2061
2062   def M2_mpy_sat_rnd_ll_s1: T_M2_mpy <0b00, 1, 1, 1, 0>;
2063   def M2_mpy_sat_rnd_ll_s0: T_M2_mpy <0b00, 1, 1, 0, 0>;
2064   def M2_mpy_sat_rnd_lh_s1: T_M2_mpy <0b01, 1, 1, 1, 0>;
2065   def M2_mpy_sat_rnd_lh_s0: T_M2_mpy <0b01, 1, 1, 0, 0>;
2066   def M2_mpy_sat_rnd_hl_s1: T_M2_mpy <0b10, 1, 1, 1, 0>;
2067   def M2_mpy_sat_rnd_hl_s0: T_M2_mpy <0b10, 1, 1, 0, 0>;
2068   def M2_mpy_sat_rnd_hh_s1: T_M2_mpy <0b11, 1, 1, 1, 0>;
2069   def M2_mpy_sat_rnd_hh_s0: T_M2_mpy <0b11, 1, 1, 0, 0>;
2070 }
2071
2072 //===----------------------------------------------------------------------===//
2073 // Template Class
2074 // MPYS / Multipy signed/unsigned halfwords and add/subtract the
2075 // result from the accumulator.
2076 //Rx [-+]= mpy[u](Rs.[H|L],Rt.[H|L])[:<<1][:sat]
2077 //===----------------------------------------------------------------------===//
2078
2079 let hasNewValue = 1, opNewValue = 0 in
2080 class T_M2_mpy_acc < bits<2> LHbits, bit isSat, bit isNac,
2081                  bit hasShift, bit isUnsigned >
2082   : MInst_acc<(outs IntRegs:$Rx), (ins IntRegs:$dst2, IntRegs:$Rs, IntRegs:$Rt),
2083   "$Rx "#!if(isNac,"-= ","+= ")#!if(isUnsigned,"mpyu","mpy")
2084                               #"($Rs."#!if(LHbits{1},"h","l")
2085                               #", $Rt."#!if(LHbits{0},"h)","l)")
2086                               #!if(hasShift,":<<1","")
2087                               #!if(isSat,":sat",""),
2088   [], "$dst2 = $Rx", M_tc_3x_SLOT23 > {
2089     bits<5> Rx;
2090     bits<5> Rs;
2091     bits<5> Rt;
2092
2093     let IClass = 0b1110;
2094     let Inst{27-24} = 0b1110;
2095     let Inst{23} = hasShift;
2096     let Inst{22} = isUnsigned;
2097     let Inst{21} = isNac;
2098     let Inst{7} = isSat;
2099     let Inst{6-5} = LHbits;
2100     let Inst{4-0} = Rx;
2101     let Inst{20-16} = Rs;
2102     let Inst{12-8} = Rt;
2103   }
2104
2105 //Rx += mpy(Rs.[H|L],Rt.[H|L])[:<<1]
2106 let isCodeGenOnly = 0 in {
2107 def M2_mpy_acc_ll_s1: T_M2_mpy_acc <0b00, 0, 0, 1, 0>;
2108 def M2_mpy_acc_ll_s0: T_M2_mpy_acc <0b00, 0, 0, 0, 0>;
2109 def M2_mpy_acc_lh_s1: T_M2_mpy_acc <0b01, 0, 0, 1, 0>;
2110 def M2_mpy_acc_lh_s0: T_M2_mpy_acc <0b01, 0, 0, 0, 0>;
2111 def M2_mpy_acc_hl_s1: T_M2_mpy_acc <0b10, 0, 0, 1, 0>;
2112 def M2_mpy_acc_hl_s0: T_M2_mpy_acc <0b10, 0, 0, 0, 0>;
2113 def M2_mpy_acc_hh_s1: T_M2_mpy_acc <0b11, 0, 0, 1, 0>;
2114 def M2_mpy_acc_hh_s0: T_M2_mpy_acc <0b11, 0, 0, 0, 0>;
2115 }
2116
2117 //Rx += mpyu(Rs.[H|L],Rt.[H|L])[:<<1]
2118 let isCodeGenOnly = 0 in {
2119 def M2_mpyu_acc_ll_s1: T_M2_mpy_acc <0b00, 0, 0, 1, 1>;
2120 def M2_mpyu_acc_ll_s0: T_M2_mpy_acc <0b00, 0, 0, 0, 1>;
2121 def M2_mpyu_acc_lh_s1: T_M2_mpy_acc <0b01, 0, 0, 1, 1>;
2122 def M2_mpyu_acc_lh_s0: T_M2_mpy_acc <0b01, 0, 0, 0, 1>;
2123 def M2_mpyu_acc_hl_s1: T_M2_mpy_acc <0b10, 0, 0, 1, 1>;
2124 def M2_mpyu_acc_hl_s0: T_M2_mpy_acc <0b10, 0, 0, 0, 1>;
2125 def M2_mpyu_acc_hh_s1: T_M2_mpy_acc <0b11, 0, 0, 1, 1>;
2126 def M2_mpyu_acc_hh_s0: T_M2_mpy_acc <0b11, 0, 0, 0, 1>;
2127 }
2128
2129 //Rx -= mpy(Rs.[H|L],Rt.[H|L])[:<<1]
2130 let isCodeGenOnly = 0 in {
2131 def M2_mpy_nac_ll_s1: T_M2_mpy_acc <0b00, 0, 1, 1, 0>;
2132 def M2_mpy_nac_ll_s0: T_M2_mpy_acc <0b00, 0, 1, 0, 0>;
2133 def M2_mpy_nac_lh_s1: T_M2_mpy_acc <0b01, 0, 1, 1, 0>;
2134 def M2_mpy_nac_lh_s0: T_M2_mpy_acc <0b01, 0, 1, 0, 0>;
2135 def M2_mpy_nac_hl_s1: T_M2_mpy_acc <0b10, 0, 1, 1, 0>;
2136 def M2_mpy_nac_hl_s0: T_M2_mpy_acc <0b10, 0, 1, 0, 0>;
2137 def M2_mpy_nac_hh_s1: T_M2_mpy_acc <0b11, 0, 1, 1, 0>;
2138 def M2_mpy_nac_hh_s0: T_M2_mpy_acc <0b11, 0, 1, 0, 0>;
2139 }
2140
2141 //Rx -= mpyu(Rs.[H|L],Rt.[H|L])[:<<1]
2142 let isCodeGenOnly = 0 in {
2143 def M2_mpyu_nac_ll_s1: T_M2_mpy_acc <0b00, 0, 1, 1, 1>;
2144 def M2_mpyu_nac_ll_s0: T_M2_mpy_acc <0b00, 0, 1, 0, 1>;
2145 def M2_mpyu_nac_lh_s1: T_M2_mpy_acc <0b01, 0, 1, 1, 1>;
2146 def M2_mpyu_nac_lh_s0: T_M2_mpy_acc <0b01, 0, 1, 0, 1>;
2147 def M2_mpyu_nac_hl_s1: T_M2_mpy_acc <0b10, 0, 1, 1, 1>;
2148 def M2_mpyu_nac_hl_s0: T_M2_mpy_acc <0b10, 0, 1, 0, 1>;
2149 def M2_mpyu_nac_hh_s1: T_M2_mpy_acc <0b11, 0, 1, 1, 1>;
2150 def M2_mpyu_nac_hh_s0: T_M2_mpy_acc <0b11, 0, 1, 0, 1>;
2151 }
2152
2153 //Rx += mpy(Rs.[H|L],Rt.[H|L])[:<<1]:sat
2154 let isCodeGenOnly = 0 in {
2155 def M2_mpy_acc_sat_ll_s1: T_M2_mpy_acc <0b00, 1, 0, 1, 0>;
2156 def M2_mpy_acc_sat_ll_s0: T_M2_mpy_acc <0b00, 1, 0, 0, 0>;
2157 def M2_mpy_acc_sat_lh_s1: T_M2_mpy_acc <0b01, 1, 0, 1, 0>;
2158 def M2_mpy_acc_sat_lh_s0: T_M2_mpy_acc <0b01, 1, 0, 0, 0>;
2159 def M2_mpy_acc_sat_hl_s1: T_M2_mpy_acc <0b10, 1, 0, 1, 0>;
2160 def M2_mpy_acc_sat_hl_s0: T_M2_mpy_acc <0b10, 1, 0, 0, 0>;
2161 def M2_mpy_acc_sat_hh_s1: T_M2_mpy_acc <0b11, 1, 0, 1, 0>;
2162 def M2_mpy_acc_sat_hh_s0: T_M2_mpy_acc <0b11, 1, 0, 0, 0>;
2163 }
2164
2165 //Rx -= mpy(Rs.[H|L],Rt.[H|L])[:<<1]:sat
2166 let isCodeGenOnly = 0 in {
2167 def M2_mpy_nac_sat_ll_s1: T_M2_mpy_acc <0b00, 1, 1, 1, 0>;
2168 def M2_mpy_nac_sat_ll_s0: T_M2_mpy_acc <0b00, 1, 1, 0, 0>;
2169 def M2_mpy_nac_sat_lh_s1: T_M2_mpy_acc <0b01, 1, 1, 1, 0>;
2170 def M2_mpy_nac_sat_lh_s0: T_M2_mpy_acc <0b01, 1, 1, 0, 0>;
2171 def M2_mpy_nac_sat_hl_s1: T_M2_mpy_acc <0b10, 1, 1, 1, 0>;
2172 def M2_mpy_nac_sat_hl_s0: T_M2_mpy_acc <0b10, 1, 1, 0, 0>;
2173 def M2_mpy_nac_sat_hh_s1: T_M2_mpy_acc <0b11, 1, 1, 1, 0>;
2174 def M2_mpy_nac_sat_hh_s0: T_M2_mpy_acc <0b11, 1, 1, 0, 0>;
2175 }
2176
2177 //===----------------------------------------------------------------------===//
2178 // Template Class
2179 // MPYS / Multipy signed/unsigned halfwords and add/subtract the
2180 // result from the 64-bit destination register.
2181 //Rxx [-+]= mpy[u](Rs.[H|L],Rt.[H|L])[:<<1][:sat]
2182 //===----------------------------------------------------------------------===//
2183
2184 class T_M2_mpyd_acc < bits<2> LHbits, bit isNac, bit hasShift, bit isUnsigned>
2185   : MInst_acc<(outs DoubleRegs:$Rxx),
2186               (ins DoubleRegs:$dst2, IntRegs:$Rs, IntRegs:$Rt),
2187   "$Rxx "#!if(isNac,"-= ","+= ")#!if(isUnsigned,"mpyu","mpy")
2188                                 #"($Rs."#!if(LHbits{1},"h","l")
2189                                 #", $Rt."#!if(LHbits{0},"h)","l)")
2190                                 #!if(hasShift,":<<1",""),
2191   [], "$dst2 = $Rxx", M_tc_3x_SLOT23 > {
2192     bits<5> Rxx;
2193     bits<5> Rs;
2194     bits<5> Rt;
2195
2196     let IClass = 0b1110;
2197
2198     let Inst{27-24} = 0b0110;
2199     let Inst{23} = hasShift;
2200     let Inst{22} = isUnsigned;
2201     let Inst{21} = isNac;
2202     let Inst{7} = 0;
2203     let Inst{6-5} = LHbits;
2204     let Inst{4-0} = Rxx;
2205     let Inst{20-16} = Rs;
2206     let Inst{12-8} = Rt;
2207   }
2208
2209 let isCodeGenOnly = 0 in {
2210 def M2_mpyd_acc_hh_s0: T_M2_mpyd_acc <0b11, 0, 0, 0>;
2211 def M2_mpyd_acc_hl_s0: T_M2_mpyd_acc <0b10, 0, 0, 0>;
2212 def M2_mpyd_acc_lh_s0: T_M2_mpyd_acc <0b01, 0, 0, 0>;
2213 def M2_mpyd_acc_ll_s0: T_M2_mpyd_acc <0b00, 0, 0, 0>;
2214
2215 def M2_mpyd_acc_hh_s1: T_M2_mpyd_acc <0b11, 0, 1, 0>;
2216 def M2_mpyd_acc_hl_s1: T_M2_mpyd_acc <0b10, 0, 1, 0>;
2217 def M2_mpyd_acc_lh_s1: T_M2_mpyd_acc <0b01, 0, 1, 0>;
2218 def M2_mpyd_acc_ll_s1: T_M2_mpyd_acc <0b00, 0, 1, 0>;
2219
2220 def M2_mpyd_nac_hh_s0: T_M2_mpyd_acc <0b11, 1, 0, 0>;
2221 def M2_mpyd_nac_hl_s0: T_M2_mpyd_acc <0b10, 1, 0, 0>;
2222 def M2_mpyd_nac_lh_s0: T_M2_mpyd_acc <0b01, 1, 0, 0>;
2223 def M2_mpyd_nac_ll_s0: T_M2_mpyd_acc <0b00, 1, 0, 0>;
2224
2225 def M2_mpyd_nac_hh_s1: T_M2_mpyd_acc <0b11, 1, 1, 0>;
2226 def M2_mpyd_nac_hl_s1: T_M2_mpyd_acc <0b10, 1, 1, 0>;
2227 def M2_mpyd_nac_lh_s1: T_M2_mpyd_acc <0b01, 1, 1, 0>;
2228 def M2_mpyd_nac_ll_s1: T_M2_mpyd_acc <0b00, 1, 1, 0>;
2229
2230 def M2_mpyud_acc_hh_s0: T_M2_mpyd_acc <0b11, 0, 0, 1>;
2231 def M2_mpyud_acc_hl_s0: T_M2_mpyd_acc <0b10, 0, 0, 1>;
2232 def M2_mpyud_acc_lh_s0: T_M2_mpyd_acc <0b01, 0, 0, 1>;
2233 def M2_mpyud_acc_ll_s0: T_M2_mpyd_acc <0b00, 0, 0, 1>;
2234
2235 def M2_mpyud_acc_hh_s1: T_M2_mpyd_acc <0b11, 0, 1, 1>;
2236 def M2_mpyud_acc_hl_s1: T_M2_mpyd_acc <0b10, 0, 1, 1>;
2237 def M2_mpyud_acc_lh_s1: T_M2_mpyd_acc <0b01, 0, 1, 1>;
2238 def M2_mpyud_acc_ll_s1: T_M2_mpyd_acc <0b00, 0, 1, 1>;
2239
2240 def M2_mpyud_nac_hh_s0: T_M2_mpyd_acc <0b11, 1, 0, 1>;
2241 def M2_mpyud_nac_hl_s0: T_M2_mpyd_acc <0b10, 1, 0, 1>;
2242 def M2_mpyud_nac_lh_s0: T_M2_mpyd_acc <0b01, 1, 0, 1>;
2243 def M2_mpyud_nac_ll_s0: T_M2_mpyd_acc <0b00, 1, 0, 1>;
2244
2245 def M2_mpyud_nac_hh_s1: T_M2_mpyd_acc <0b11, 1, 1, 1>;
2246 def M2_mpyud_nac_hl_s1: T_M2_mpyd_acc <0b10, 1, 1, 1>;
2247 def M2_mpyud_nac_lh_s1: T_M2_mpyd_acc <0b01, 1, 1, 1>;
2248 def M2_mpyud_nac_ll_s1: T_M2_mpyd_acc <0b00, 1, 1, 1>;
2249 }
2250
2251 let hasNewValue = 1, opNewValue = 0 in
2252 class T_MType_mpy <string mnemonic, bits<4> RegTyBits, RegisterClass RC,
2253                    bits<3> MajOp, bits<3> MinOp, bit isSat = 0, bit isRnd = 0,
2254                    string op2Suffix = "", bit isRaw = 0, bit isHi = 0 >
2255   : MInst <(outs IntRegs:$dst), (ins RC:$src1, RC:$src2),
2256   "$dst = "#mnemonic
2257            #"($src1, $src2"#op2Suffix#")"
2258            #!if(MajOp{2}, ":<<1", "")
2259            #!if(isRnd, ":rnd", "")
2260            #!if(isSat, ":sat", "")
2261            #!if(isRaw, !if(isHi, ":raw:hi", ":raw:lo"), ""), [] > {
2262     bits<5> dst;
2263     bits<5> src1;
2264     bits<5> src2;
2265
2266     let IClass = 0b1110;
2267
2268     let Inst{27-24} = RegTyBits;
2269     let Inst{23-21} = MajOp;
2270     let Inst{20-16} = src1;
2271     let Inst{13}    = 0b0;
2272     let Inst{12-8}  = src2;
2273     let Inst{7-5}   = MinOp;
2274     let Inst{4-0}   = dst;
2275   }
2276
2277 class T_MType_dd  <string mnemonic, bits<3> MajOp, bits<3> MinOp,
2278                    bit isSat = 0, bit isRnd = 0 >
2279   : T_MType_mpy <mnemonic, 0b1001, DoubleRegs, MajOp, MinOp, isSat, isRnd>;
2280
2281 class T_MType_rr1  <string mnemonic, bits<3> MajOp, bits<3> MinOp,
2282                     bit isSat = 0, bit isRnd = 0 >
2283   : T_MType_mpy<mnemonic, 0b1101, IntRegs, MajOp, MinOp, isSat, isRnd>;
2284
2285 class T_MType_rr2 <string mnemonic, bits<3> MajOp, bits<3> MinOp,
2286                    bit isSat = 0, bit isRnd = 0, string op2str = "" >
2287   : T_MType_mpy<mnemonic, 0b1101, IntRegs, MajOp, MinOp, isSat, isRnd, op2str>;
2288
2289 let CextOpcode = "mpyi", InputType = "reg", isCodeGenOnly = 0 in
2290 def M2_mpyi    : T_MType_rr1 <"mpyi", 0b000, 0b000>, ImmRegRel;
2291
2292 let isCodeGenOnly = 0 in {
2293 def M2_mpy_up  : T_MType_rr1 <"mpy",  0b000, 0b001>;
2294 def M2_mpyu_up : T_MType_rr1 <"mpyu", 0b010, 0b001>;
2295 }
2296
2297 let isCodeGenOnly = 0 in
2298 def M2_dpmpyss_rnd_s0 : T_MType_rr1 <"mpy", 0b001, 0b001, 0, 1>;
2299
2300 let isCodeGenOnly = 0 in {
2301 def M2_hmmpyh_rs1 : T_MType_rr2 <"mpy", 0b101, 0b100, 1, 1, ".h">;
2302 def M2_hmmpyl_rs1 : T_MType_rr2 <"mpy", 0b111, 0b100, 1, 1, ".l">;
2303 }
2304
2305 // V4 Instructions
2306 let isCodeGenOnly = 0 in {
2307 def M2_mpysu_up : T_MType_rr1 <"mpysu", 0b011, 0b001, 0>;
2308 def M2_mpy_up_s1_sat : T_MType_rr1 <"mpy", 0b111, 0b000, 1>;
2309
2310 def M2_hmmpyh_s1 : T_MType_rr2 <"mpy", 0b101, 0b000, 1, 0, ".h">;
2311 def M2_hmmpyl_s1 : T_MType_rr2 <"mpy", 0b101, 0b001, 1, 0, ".l">;
2312 }
2313
2314 def: Pat<(i32 (mul   I32:$src1, I32:$src2)), (M2_mpyi    I32:$src1, I32:$src2)>;
2315 def: Pat<(i32 (mulhs I32:$src1, I32:$src2)), (M2_mpy_up  I32:$src1, I32:$src2)>;
2316 def: Pat<(i32 (mulhu I32:$src1, I32:$src2)), (M2_mpyu_up I32:$src1, I32:$src2)>;
2317
2318 let hasNewValue = 1, opNewValue = 0 in
2319 class T_MType_mpy_ri <bit isNeg, Operand ImmOp, list<dag> pattern>
2320   : MInst < (outs IntRegs:$Rd), (ins IntRegs:$Rs, ImmOp:$u8),
2321   "$Rd ="#!if(isNeg, "- ", "+ ")#"mpyi($Rs, #$u8)" ,
2322    pattern, "", M_tc_3x_SLOT23> {
2323     bits<5> Rd;
2324     bits<5> Rs;
2325     bits<8> u8;
2326
2327     let IClass = 0b1110;
2328
2329     let Inst{27-24} = 0b0000;
2330     let Inst{23} = isNeg;
2331     let Inst{13} = 0b0;
2332     let Inst{4-0} = Rd;
2333     let Inst{20-16} = Rs;
2334     let Inst{12-5} = u8;
2335   }
2336
2337 let isExtendable = 1, opExtentBits = 8, opExtendable = 2, isCodeGenOnly = 0 in
2338 def M2_mpysip : T_MType_mpy_ri <0, u8Ext,
2339                 [(set (i32 IntRegs:$Rd), (mul IntRegs:$Rs, u8ExtPred:$u8))]>;
2340
2341 let isCodeGenOnly = 0 in
2342 def M2_mpysin :  T_MType_mpy_ri <1, u8Imm,
2343                 [(set (i32 IntRegs:$Rd), (ineg (mul IntRegs:$Rs,
2344                                                     u8ImmPred:$u8)))]>;
2345
2346 // Assember mapped to M2_mpyi
2347 let isAsmParserOnly = 1 in
2348 def M2_mpyui : MInst<(outs IntRegs:$dst),
2349                      (ins IntRegs:$src1, IntRegs:$src2),
2350   "$dst = mpyui($src1, $src2)">;
2351
2352 // Rd=mpyi(Rs,#m9)
2353 // s9 is NOT the same as m9 - but it works.. so far.
2354 // Assembler maps to either Rd=+mpyi(Rs,#u8) or Rd=-mpyi(Rs,#u8)
2355 // depending on the value of m9. See Arch Spec.
2356 let isExtendable = 1, opExtendable = 2, isExtentSigned = 1, opExtentBits = 9,
2357     CextOpcode = "mpyi", InputType = "imm", hasNewValue = 1 in
2358 def M2_mpysmi : MInst<(outs IntRegs:$dst), (ins IntRegs:$src1, s9Ext:$src2),
2359     "$dst = mpyi($src1, #$src2)",
2360     [(set (i32 IntRegs:$dst), (mul (i32 IntRegs:$src1),
2361                                    s9ExtPred:$src2))]>, ImmRegRel;
2362
2363 let hasNewValue = 1, isExtendable = 1,  opExtentBits = 8, opExtendable = 3,
2364     InputType = "imm" in
2365 class T_MType_acc_ri <string mnemonic, bits<3> MajOp, Operand ImmOp,
2366                       list<dag> pattern = []>
2367  : MInst < (outs IntRegs:$dst), (ins IntRegs:$src1, IntRegs:$src2, ImmOp:$src3),
2368   "$dst "#mnemonic#"($src2, #$src3)",
2369   pattern, "$src1 = $dst", M_tc_2_SLOT23> {
2370     bits<5> dst;
2371     bits<5> src2;
2372     bits<8> src3;
2373
2374     let IClass = 0b1110;
2375
2376     let Inst{27-26} = 0b00;
2377     let Inst{25-23} = MajOp;
2378     let Inst{20-16} = src2;
2379     let Inst{13} = 0b0;
2380     let Inst{12-5} = src3;
2381     let Inst{4-0} = dst;
2382   }
2383
2384 let InputType = "reg", hasNewValue = 1 in
2385 class T_MType_acc_rr <string mnemonic, bits<3> MajOp, bits<3> MinOp,
2386                       bit isSwap = 0, list<dag> pattern = [], bit hasNot = 0,
2387                       bit isSat = 0, bit isShift = 0>
2388   : MInst < (outs IntRegs:$dst),
2389             (ins IntRegs:$src1, IntRegs:$src2, IntRegs:$src3),
2390   "$dst "#mnemonic#"($src2, "#!if(hasNot, "~$src3)","$src3)")
2391                           #!if(isShift, ":<<1", "")
2392                           #!if(isSat, ":sat", ""),
2393   pattern, "$src1 = $dst", M_tc_2_SLOT23 > {
2394     bits<5> dst;
2395     bits<5> src2;
2396     bits<5> src3;
2397
2398     let IClass = 0b1110;
2399
2400     let Inst{27-24} = 0b1111;
2401     let Inst{23-21} = MajOp;
2402     let Inst{20-16} = !if(isSwap, src3, src2);
2403     let Inst{13} = 0b0;
2404     let Inst{12-8} = !if(isSwap, src2, src3);
2405     let Inst{7-5} = MinOp;
2406     let Inst{4-0} = dst;
2407   }
2408
2409 let CextOpcode = "MPYI_acc", Itinerary = M_tc_3x_SLOT23, isCodeGenOnly = 0 in {
2410   def M2_macsip : T_MType_acc_ri <"+= mpyi", 0b010, u8Ext,
2411                   [(set (i32 IntRegs:$dst),
2412                         (add (mul IntRegs:$src2, u8ExtPred:$src3),
2413                              IntRegs:$src1))]>, ImmRegRel;
2414
2415   def M2_maci   : T_MType_acc_rr <"+= mpyi", 0b000, 0b000, 0,
2416                  [(set (i32 IntRegs:$dst),
2417                        (add (mul IntRegs:$src2, IntRegs:$src3),
2418                             IntRegs:$src1))]>, ImmRegRel;
2419 }
2420
2421 let CextOpcode = "ADD_acc", isCodeGenOnly = 0 in {
2422   let isExtentSigned = 1 in
2423   def M2_accii : T_MType_acc_ri <"+= add", 0b100, s8Ext,
2424                  [(set (i32 IntRegs:$dst),
2425                        (add (add (i32 IntRegs:$src2), s8_16ExtPred:$src3),
2426                             (i32 IntRegs:$src1)))]>, ImmRegRel;
2427
2428   def M2_acci  : T_MType_acc_rr <"+= add",  0b000, 0b001, 0,
2429                  [(set (i32 IntRegs:$dst),
2430                        (add (add (i32 IntRegs:$src2), (i32 IntRegs:$src3)),
2431                             (i32 IntRegs:$src1)))]>, ImmRegRel;
2432 }
2433
2434 let CextOpcode = "SUB_acc", isCodeGenOnly = 0 in {
2435   let isExtentSigned = 1 in
2436   def M2_naccii : T_MType_acc_ri <"-= add", 0b101, s8Ext>, ImmRegRel;
2437
2438   def M2_nacci  : T_MType_acc_rr <"-= add",  0b100, 0b001, 0>, ImmRegRel;
2439 }
2440
2441 let Itinerary = M_tc_3x_SLOT23, isCodeGenOnly = 0 in
2442 def M2_macsin : T_MType_acc_ri <"-= mpyi", 0b011, u8Ext>;
2443
2444 let isCodeGenOnly = 0 in {
2445 def M2_xor_xacc : T_MType_acc_rr < "^= xor", 0b100, 0b011, 0>;
2446 def M2_subacc : T_MType_acc_rr <"+= sub",  0b000, 0b011, 1>;
2447 }
2448
2449 class T_MType_acc_pat1 <InstHexagon MI, SDNode firstOp, SDNode secOp,
2450                         PatLeaf ImmPred>
2451   : Pat <(secOp IntRegs:$src1, (firstOp IntRegs:$src2, ImmPred:$src3)),
2452          (MI IntRegs:$src1, IntRegs:$src2, ImmPred:$src3)>;
2453
2454 class T_MType_acc_pat2 <InstHexagon MI, SDNode firstOp, SDNode secOp>
2455   : Pat <(i32 (secOp IntRegs:$src1, (firstOp IntRegs:$src2, IntRegs:$src3))),
2456          (MI IntRegs:$src1, IntRegs:$src2, IntRegs:$src3)>;
2457
2458 def : T_MType_acc_pat2 <M2_xor_xacc, xor, xor>;
2459 def : T_MType_acc_pat1 <M2_macsin, mul, sub, u8ExtPred>;
2460
2461 def : T_MType_acc_pat1 <M2_naccii, add, sub, s8_16ExtPred>;
2462 def : T_MType_acc_pat2 <M2_nacci, add, sub>;
2463 //===----------------------------------------------------------------------===//
2464 // Template Class -- Multiply signed/unsigned halfwords with and without
2465 // saturation and rounding
2466 //===----------------------------------------------------------------------===//
2467 class T_M2_mpyd < bits<2> LHbits, bit isRnd, bit hasShift, bit isUnsigned >
2468   : MInst < (outs DoubleRegs:$Rdd), (ins IntRegs:$Rs, IntRegs:$Rt),
2469   "$Rdd = "#!if(isUnsigned,"mpyu","mpy")#"($Rs."#!if(LHbits{1},"h","l")
2470                                        #", $Rt."#!if(LHbits{0},"h)","l)")
2471                                        #!if(hasShift,":<<1","")
2472                                        #!if(isRnd,":rnd",""),
2473   [] > {
2474     bits<5> Rdd;
2475     bits<5> Rs;
2476     bits<5> Rt;
2477
2478     let IClass = 0b1110;
2479
2480     let Inst{27-24} = 0b0100;
2481     let Inst{23} = hasShift;
2482     let Inst{22} = isUnsigned;
2483     let Inst{21} = isRnd;
2484     let Inst{6-5} = LHbits;
2485     let Inst{4-0} = Rdd;
2486     let Inst{20-16} = Rs;
2487     let Inst{12-8} = Rt;
2488 }
2489
2490 let isCodeGenOnly = 0 in {
2491 def M2_mpyd_hh_s0: T_M2_mpyd<0b11, 0, 0, 0>;
2492 def M2_mpyd_hl_s0: T_M2_mpyd<0b10, 0, 0, 0>;
2493 def M2_mpyd_lh_s0: T_M2_mpyd<0b01, 0, 0, 0>;
2494 def M2_mpyd_ll_s0: T_M2_mpyd<0b00, 0, 0, 0>;
2495
2496 def M2_mpyd_hh_s1: T_M2_mpyd<0b11, 0, 1, 0>;
2497 def M2_mpyd_hl_s1: T_M2_mpyd<0b10, 0, 1, 0>;
2498 def M2_mpyd_lh_s1: T_M2_mpyd<0b01, 0, 1, 0>;
2499 def M2_mpyd_ll_s1: T_M2_mpyd<0b00, 0, 1, 0>;
2500
2501 def M2_mpyd_rnd_hh_s0: T_M2_mpyd<0b11, 1, 0, 0>;
2502 def M2_mpyd_rnd_hl_s0: T_M2_mpyd<0b10, 1, 0, 0>;
2503 def M2_mpyd_rnd_lh_s0: T_M2_mpyd<0b01, 1, 0, 0>;
2504 def M2_mpyd_rnd_ll_s0: T_M2_mpyd<0b00, 1, 0, 0>;
2505
2506 def M2_mpyd_rnd_hh_s1: T_M2_mpyd<0b11, 1, 1, 0>;
2507 def M2_mpyd_rnd_hl_s1: T_M2_mpyd<0b10, 1, 1, 0>;
2508 def M2_mpyd_rnd_lh_s1: T_M2_mpyd<0b01, 1, 1, 0>;
2509 def M2_mpyd_rnd_ll_s1: T_M2_mpyd<0b00, 1, 1, 0>;
2510
2511 //Rdd=mpyu(Rs.[HL],Rt.[HL])[:<<1]
2512 def M2_mpyud_hh_s0: T_M2_mpyd<0b11, 0, 0, 1>;
2513 def M2_mpyud_hl_s0: T_M2_mpyd<0b10, 0, 0, 1>;
2514 def M2_mpyud_lh_s0: T_M2_mpyd<0b01, 0, 0, 1>;
2515 def M2_mpyud_ll_s0: T_M2_mpyd<0b00, 0, 0, 1>;
2516
2517 def M2_mpyud_hh_s1: T_M2_mpyd<0b11, 0, 1, 1>;
2518 def M2_mpyud_hl_s1: T_M2_mpyd<0b10, 0, 1, 1>;
2519 def M2_mpyud_lh_s1: T_M2_mpyd<0b01, 0, 1, 1>;
2520 def M2_mpyud_ll_s1: T_M2_mpyd<0b00, 0, 1, 1>;
2521 }
2522 //===----------------------------------------------------------------------===//
2523 // Template Class for xtype mpy:
2524 // Vector multiply
2525 // Complex multiply
2526 // multiply 32X32 and use full result
2527 //===----------------------------------------------------------------------===//
2528 let hasSideEffects = 0 in
2529 class T_XTYPE_mpy64 <string mnemonic, bits<3> MajOp, bits<3> MinOp,
2530                      bit isSat, bit hasShift, bit isConj>
2531    : MInst <(outs DoubleRegs:$Rdd),
2532             (ins IntRegs:$Rs, IntRegs:$Rt),
2533   "$Rdd = "#mnemonic#"($Rs, $Rt"#!if(isConj,"*)",")")
2534                                 #!if(hasShift,":<<1","")
2535                                 #!if(isSat,":sat",""),
2536   [] > {
2537     bits<5> Rdd;
2538     bits<5> Rs;
2539     bits<5> Rt;
2540
2541     let IClass = 0b1110;
2542
2543     let Inst{27-24} = 0b0101;
2544     let Inst{23-21} = MajOp;
2545     let Inst{20-16} = Rs;
2546     let Inst{12-8} = Rt;
2547     let Inst{7-5} = MinOp;
2548     let Inst{4-0} = Rdd;
2549   }
2550
2551 //===----------------------------------------------------------------------===//
2552 // Template Class for xtype mpy with accumulation into 64-bit:
2553 // Vector multiply
2554 // Complex multiply
2555 // multiply 32X32 and use full result
2556 //===----------------------------------------------------------------------===//
2557 class T_XTYPE_mpy64_acc <string op1, string op2, bits<3> MajOp, bits<3> MinOp,
2558                          bit isSat, bit hasShift, bit isConj>
2559   : MInst <(outs DoubleRegs:$Rxx),
2560            (ins DoubleRegs:$dst2, IntRegs:$Rs, IntRegs:$Rt),
2561   "$Rxx "#op2#"= "#op1#"($Rs, $Rt"#!if(isConj,"*)",")")
2562                                    #!if(hasShift,":<<1","")
2563                                    #!if(isSat,":sat",""),
2564
2565   [] , "$dst2 = $Rxx" > {
2566     bits<5> Rxx;
2567     bits<5> Rs;
2568     bits<5> Rt;
2569
2570     let IClass = 0b1110;
2571
2572     let Inst{27-24} = 0b0111;
2573     let Inst{23-21} = MajOp;
2574     let Inst{20-16} = Rs;
2575     let Inst{12-8} = Rt;
2576     let Inst{7-5} = MinOp;
2577     let Inst{4-0} = Rxx;
2578   }
2579
2580 // MPY - Multiply and use full result
2581 // Rdd = mpy[u](Rs,Rt)
2582 let isCodeGenOnly = 0 in {
2583 def M2_dpmpyss_s0 : T_XTYPE_mpy64 < "mpy", 0b000, 0b000, 0, 0, 0>;
2584 def M2_dpmpyuu_s0 : T_XTYPE_mpy64 < "mpyu", 0b010, 0b000, 0, 0, 0>;
2585
2586 // Rxx[+-]= mpy[u](Rs,Rt)
2587 def M2_dpmpyss_acc_s0 : T_XTYPE_mpy64_acc < "mpy",  "+", 0b000, 0b000, 0, 0, 0>;
2588 def M2_dpmpyss_nac_s0 : T_XTYPE_mpy64_acc < "mpy",  "-", 0b001, 0b000, 0, 0, 0>;
2589 def M2_dpmpyuu_acc_s0 : T_XTYPE_mpy64_acc < "mpyu", "+", 0b010, 0b000, 0, 0, 0>;
2590 def M2_dpmpyuu_nac_s0 : T_XTYPE_mpy64_acc < "mpyu", "-", 0b011, 0b000, 0, 0, 0>;
2591 }
2592
2593 def: Pat<(i64 (mul (i64 (anyext (i32 IntRegs:$src1))),
2594                    (i64 (anyext (i32 IntRegs:$src2))))),
2595          (M2_dpmpyuu_s0 IntRegs:$src1, IntRegs:$src2)>;
2596
2597 def: Pat<(i64 (mul (i64 (sext (i32 IntRegs:$src1))),
2598                    (i64 (sext (i32 IntRegs:$src2))))),
2599          (M2_dpmpyss_s0 IntRegs:$src1, IntRegs:$src2)>;
2600
2601 def: Pat<(i64 (mul (is_sext_i32:$src1),
2602                    (is_sext_i32:$src2))),
2603          (M2_dpmpyss_s0 (LoReg DoubleRegs:$src1), (LoReg DoubleRegs:$src2))>;
2604
2605 // Multiply and accumulate, use full result.
2606 // Rxx[+-]=mpy(Rs,Rt)
2607
2608 def: Pat<(i64 (add (i64 DoubleRegs:$src1),
2609                    (mul (i64 (sext (i32 IntRegs:$src2))),
2610                         (i64 (sext (i32 IntRegs:$src3)))))),
2611          (M2_dpmpyss_acc_s0 DoubleRegs:$src1, IntRegs:$src2, IntRegs:$src3)>;
2612
2613 def: Pat<(i64 (sub (i64 DoubleRegs:$src1),
2614                    (mul (i64 (sext (i32 IntRegs:$src2))),
2615                         (i64 (sext (i32 IntRegs:$src3)))))),
2616          (M2_dpmpyss_nac_s0 DoubleRegs:$src1, IntRegs:$src2, IntRegs:$src3)>;
2617
2618 def: Pat<(i64 (add (i64 DoubleRegs:$src1),
2619                    (mul (i64 (anyext (i32 IntRegs:$src2))),
2620                         (i64 (anyext (i32 IntRegs:$src3)))))),
2621          (M2_dpmpyuu_acc_s0 DoubleRegs:$src1, IntRegs:$src2, IntRegs:$src3)>;
2622
2623 def: Pat<(i64 (add (i64 DoubleRegs:$src1),
2624                    (mul (i64 (zext (i32 IntRegs:$src2))),
2625                         (i64 (zext (i32 IntRegs:$src3)))))),
2626          (M2_dpmpyuu_acc_s0 DoubleRegs:$src1, IntRegs:$src2, IntRegs:$src3)>;
2627
2628 def: Pat<(i64 (sub (i64 DoubleRegs:$src1),
2629                    (mul (i64 (anyext (i32 IntRegs:$src2))),
2630                         (i64 (anyext (i32 IntRegs:$src3)))))),
2631          (M2_dpmpyuu_nac_s0 DoubleRegs:$src1, IntRegs:$src2, IntRegs:$src3)>;
2632
2633 def: Pat<(i64 (sub (i64 DoubleRegs:$src1),
2634                    (mul (i64 (zext (i32 IntRegs:$src2))),
2635                         (i64 (zext (i32 IntRegs:$src3)))))),
2636          (M2_dpmpyuu_nac_s0 DoubleRegs:$src1, IntRegs:$src2, IntRegs:$src3)>;
2637
2638 //===----------------------------------------------------------------------===//
2639 // MTYPE/MPYH -
2640 //===----------------------------------------------------------------------===//
2641
2642 //===----------------------------------------------------------------------===//
2643 // MTYPE/MPYS +
2644 //===----------------------------------------------------------------------===//
2645 //===----------------------------------------------------------------------===//
2646 // MTYPE/MPYS -
2647 //===----------------------------------------------------------------------===//
2648
2649 //===----------------------------------------------------------------------===//
2650 // MTYPE/VB +
2651 //===----------------------------------------------------------------------===//
2652 //===----------------------------------------------------------------------===//
2653 // MTYPE/VB -
2654 //===----------------------------------------------------------------------===//
2655
2656 //===----------------------------------------------------------------------===//
2657 // MTYPE/VH  +
2658 //===----------------------------------------------------------------------===//
2659 //===----------------------------------------------------------------------===//
2660 // MTYPE/VH  -
2661 //===----------------------------------------------------------------------===//
2662
2663 //===----------------------------------------------------------------------===//
2664 // ST +
2665 //===----------------------------------------------------------------------===//
2666 ///
2667 // Store doubleword.
2668 //===----------------------------------------------------------------------===//
2669 // Template class for non-predicated post increment stores with immediate offset
2670 //===----------------------------------------------------------------------===//
2671 let isPredicable = 1, hasSideEffects = 0, addrMode = PostInc in
2672 class T_store_pi <string mnemonic, RegisterClass RC, Operand ImmOp,
2673                  bits<4> MajOp, bit isHalf >
2674   : STInst <(outs IntRegs:$_dst_),
2675             (ins IntRegs:$src1, ImmOp:$offset, RC:$src2),
2676   mnemonic#"($src1++#$offset) = $src2"#!if(isHalf, ".h", ""),
2677   [], "$src1 = $_dst_" >,
2678   AddrModeRel {
2679     bits<5> src1;
2680     bits<5> src2;
2681     bits<7> offset;
2682     bits<4> offsetBits;
2683
2684     string ImmOpStr = !cast<string>(ImmOp);
2685     let offsetBits = !if (!eq(ImmOpStr, "s4_3Imm"), offset{6-3},
2686                      !if (!eq(ImmOpStr, "s4_2Imm"), offset{5-2},
2687                      !if (!eq(ImmOpStr, "s4_1Imm"), offset{4-1},
2688                                       /* s4_0Imm */ offset{3-0})));
2689     let isNVStorable = !if (!eq(ImmOpStr, "s4_3Imm"), 0, 1);
2690
2691     let IClass = 0b1010;
2692
2693     let Inst{27-25} = 0b101;
2694     let Inst{24-21} = MajOp;
2695     let Inst{20-16} = src1;
2696     let Inst{13}    = 0b0;
2697     let Inst{12-8}  = src2;
2698     let Inst{7}     = 0b0;
2699     let Inst{6-3}   = offsetBits;
2700     let Inst{1}     = 0b0;
2701   }
2702
2703 //===----------------------------------------------------------------------===//
2704 // Template class for predicated post increment stores with immediate offset
2705 //===----------------------------------------------------------------------===//
2706 let isPredicated = 1, hasSideEffects = 0, addrMode = PostInc in
2707 class T_pstore_pi <string mnemonic, RegisterClass RC, Operand ImmOp,
2708                       bits<4> MajOp, bit isHalf, bit isPredNot, bit isPredNew >
2709   : STInst <(outs IntRegs:$_dst_),
2710             (ins PredRegs:$src1, IntRegs:$src2, ImmOp:$offset, RC:$src3),
2711   !if(isPredNot, "if (!$src1", "if ($src1")#!if(isPredNew, ".new) ",
2712   ") ")#mnemonic#"($src2++#$offset) = $src3"#!if(isHalf, ".h", ""),
2713   [], "$src2 = $_dst_" >,
2714   AddrModeRel {
2715     bits<2> src1;
2716     bits<5> src2;
2717     bits<7> offset;
2718     bits<5> src3;
2719     bits<4> offsetBits;
2720
2721     string ImmOpStr = !cast<string>(ImmOp);
2722     let offsetBits = !if (!eq(ImmOpStr, "s4_3Imm"), offset{6-3},
2723                      !if (!eq(ImmOpStr, "s4_2Imm"), offset{5-2},
2724                      !if (!eq(ImmOpStr, "s4_1Imm"), offset{4-1},
2725                                       /* s4_0Imm */ offset{3-0})));
2726
2727     let isNVStorable = !if (!eq(ImmOpStr, "s4_3Imm"), 0, 1);
2728     let isPredicatedNew = isPredNew;
2729     let isPredicatedFalse = isPredNot;
2730
2731     let IClass = 0b1010;
2732
2733     let Inst{27-25} = 0b101;
2734     let Inst{24-21} = MajOp;
2735     let Inst{20-16} = src2;
2736     let Inst{13} = 0b1;
2737     let Inst{12-8} = src3;
2738     let Inst{7} = isPredNew;
2739     let Inst{6-3} = offsetBits;
2740     let Inst{2} = isPredNot;
2741     let Inst{1-0} = src1;
2742   }
2743
2744 multiclass ST_PostInc<string mnemonic, string BaseOp, RegisterClass RC,
2745                       Operand ImmOp, bits<4> MajOp, bit isHalf = 0 > {
2746
2747   let BaseOpcode = "POST_"#BaseOp in {
2748     def S2_#NAME#_pi : T_store_pi <mnemonic, RC, ImmOp, MajOp, isHalf>;
2749
2750     // Predicated
2751     def S2_p#NAME#t_pi : T_pstore_pi <mnemonic, RC, ImmOp, MajOp, isHalf, 0, 0>;
2752     def S2_p#NAME#f_pi : T_pstore_pi <mnemonic, RC, ImmOp, MajOp, isHalf, 1, 0>;
2753
2754     // Predicated new
2755     def S2_p#NAME#tnew_pi : T_pstore_pi <mnemonic, RC, ImmOp, MajOp,
2756                                           isHalf, 0, 1>;
2757     def S2_p#NAME#fnew_pi : T_pstore_pi <mnemonic, RC, ImmOp, MajOp,
2758                                           isHalf, 1, 1>;
2759   }
2760 }
2761
2762 let accessSize = ByteAccess, isCodeGenOnly = 0 in
2763 defm storerb: ST_PostInc <"memb", "STrib", IntRegs, s4_0Imm, 0b1000>;
2764
2765 let accessSize = HalfWordAccess, isCodeGenOnly = 0 in
2766 defm storerh: ST_PostInc <"memh", "STrih", IntRegs, s4_1Imm, 0b1010>;
2767
2768 let accessSize = WordAccess, isCodeGenOnly = 0 in
2769 defm storeri: ST_PostInc <"memw", "STriw", IntRegs, s4_2Imm, 0b1100>;
2770
2771 let accessSize = DoubleWordAccess, isCodeGenOnly = 0 in
2772 defm storerd: ST_PostInc <"memd", "STrid", DoubleRegs, s4_3Imm, 0b1110>;
2773
2774 let accessSize = HalfWordAccess, isNVStorable = 0, isCodeGenOnly = 0 in
2775 defm storerf: ST_PostInc <"memh", "STrih_H", IntRegs, s4_1Imm, 0b1011, 1>;
2776
2777 def : Pat<(post_truncsti8 (i32 IntRegs:$src1), IntRegs:$src2,
2778                            s4_3ImmPred:$offset),
2779           (S2_storerb_pi IntRegs:$src2, s4_0ImmPred:$offset, IntRegs:$src1)>;
2780
2781 def : Pat<(post_truncsti16 (i32 IntRegs:$src1), IntRegs:$src2,
2782                             s4_3ImmPred:$offset),
2783           (S2_storerh_pi IntRegs:$src2, s4_1ImmPred:$offset, IntRegs:$src1)>;
2784
2785 def : Pat<(post_store (i32 IntRegs:$src1), IntRegs:$src2, s4_2ImmPred:$offset),
2786           (S2_storeri_pi IntRegs:$src2, s4_1ImmPred:$offset, IntRegs:$src1)>;
2787
2788 def : Pat<(post_store (i64 DoubleRegs:$src1), IntRegs:$src2,
2789                        s4_3ImmPred:$offset),
2790           (S2_storerd_pi IntRegs:$src2, s4_3ImmPred:$offset, DoubleRegs:$src1)>;
2791
2792 //===----------------------------------------------------------------------===//
2793 // Template class for post increment stores with register offset.
2794 //===----------------------------------------------------------------------===//
2795 let isNVStorable = 1 in
2796 class T_store_pr <string mnemonic, RegisterClass RC, bits<3> MajOp,
2797                      MemAccessSize AccessSz, bit isHalf = 0>
2798   : STInst <(outs IntRegs:$_dst_),
2799             (ins IntRegs:$src1, ModRegs:$src2, RC:$src3),
2800   mnemonic#"($src1++$src2) = $src3"#!if(isHalf, ".h", ""),
2801   [], "$src1 = $_dst_" > {
2802     bits<5> src1;
2803     bits<1> src2;
2804     bits<5> src3;
2805     let accessSize = AccessSz;
2806
2807     let IClass = 0b1010;
2808
2809     let Inst{27-24} = 0b1101;
2810     let Inst{23-21} = MajOp;
2811     let Inst{20-16} = src1;
2812     let Inst{13} = src2;
2813     let Inst{12-8} = src3;
2814     let Inst{7} = 0b0;
2815   }
2816
2817 let isCodeGenOnly = 0 in {
2818 def S2_storerb_pr : T_store_pr<"memb", IntRegs, 0b000, ByteAccess>;
2819 def S2_storerh_pr : T_store_pr<"memh", IntRegs, 0b010, HalfWordAccess>;
2820 def S2_storeri_pr : T_store_pr<"memw", IntRegs, 0b100, WordAccess>;
2821 def S2_storerd_pr : T_store_pr<"memd", DoubleRegs, 0b110, DoubleWordAccess>;
2822
2823 def S2_storerf_pr : T_store_pr<"memh", IntRegs, 0b011, HalfWordAccess, 1>;
2824 }
2825 let opExtendable = 1, isExtentSigned = 1, isPredicable = 1 in
2826 class T_store_io <string mnemonic, RegisterClass RC, Operand ImmOp,
2827                  bits<3>MajOp, bit isH = 0>
2828   : STInst <(outs),
2829             (ins IntRegs:$src1, ImmOp:$src2, RC:$src3),
2830   mnemonic#"($src1+#$src2) = $src3"#!if(isH,".h","")>,
2831   AddrModeRel, ImmRegRel {
2832     bits<5> src1;
2833     bits<14> src2; // Actual address offset
2834     bits<5> src3;
2835     bits<11> offsetBits; // Represents offset encoding
2836
2837     string ImmOpStr = !cast<string>(ImmOp);
2838
2839     let opExtentBits = !if (!eq(ImmOpStr, "s11_3Ext"), 14,
2840                        !if (!eq(ImmOpStr, "s11_2Ext"), 13,
2841                        !if (!eq(ImmOpStr, "s11_1Ext"), 12,
2842                                         /* s11_0Ext */ 11)));
2843     let offsetBits = !if (!eq(ImmOpStr, "s11_3Ext"), src2{13-3},
2844                      !if (!eq(ImmOpStr, "s11_2Ext"), src2{12-2},
2845                      !if (!eq(ImmOpStr, "s11_1Ext"), src2{11-1},
2846                                       /* s11_0Ext */ src2{10-0})));
2847     let IClass = 0b1010;
2848
2849     let Inst{27} = 0b0;
2850     let Inst{26-25} = offsetBits{10-9};
2851     let Inst{24} = 0b1;
2852     let Inst{23-21} = MajOp;
2853     let Inst{20-16} = src1;
2854     let Inst{13} = offsetBits{8};
2855     let Inst{12-8} = src3;
2856     let Inst{7-0} = offsetBits{7-0};
2857   }
2858
2859 let opExtendable = 2, isPredicated = 1 in
2860 class T_pstore_io <string mnemonic, RegisterClass RC, Operand ImmOp,
2861                    bits<3>MajOp, bit PredNot, bit isPredNew, bit isH = 0>
2862   : STInst <(outs),
2863             (ins PredRegs:$src1, IntRegs:$src2, ImmOp:$src3, RC:$src4),
2864   !if(PredNot, "if (!$src1", "if ($src1")#!if(isPredNew, ".new) ",
2865   ") ")#mnemonic#"($src2+#$src3) = $src4"#!if(isH,".h",""),
2866   [],"",V2LDST_tc_st_SLOT01 >,
2867    AddrModeRel, ImmRegRel {
2868     bits<2> src1;
2869     bits<5> src2;
2870     bits<9> src3; // Actual address offset
2871     bits<5> src4;
2872     bits<6> offsetBits; // Represents offset encoding
2873
2874     let isPredicatedNew = isPredNew;
2875     let isPredicatedFalse = PredNot;
2876
2877     string ImmOpStr = !cast<string>(ImmOp);
2878     let opExtentBits = !if (!eq(ImmOpStr, "u6_3Ext"), 9,
2879                        !if (!eq(ImmOpStr, "u6_2Ext"), 8,
2880                        !if (!eq(ImmOpStr, "u6_1Ext"), 7,
2881                                         /* u6_0Ext */ 6)));
2882     let offsetBits = !if (!eq(ImmOpStr, "u6_3Ext"), src3{8-3},
2883                      !if (!eq(ImmOpStr, "u6_2Ext"), src3{7-2},
2884                      !if (!eq(ImmOpStr, "u6_1Ext"), src3{6-1},
2885                                       /* u6_0Ext */ src3{5-0})));
2886      let IClass = 0b0100;
2887
2888     let Inst{27} = 0b0;
2889     let Inst{26} = PredNot;
2890     let Inst{25} = isPredNew;
2891     let Inst{24} = 0b0;
2892     let Inst{23-21} = MajOp;
2893     let Inst{20-16} = src2;
2894     let Inst{13} = offsetBits{5};
2895     let Inst{12-8} = src4;
2896     let Inst{7-3} = offsetBits{4-0};
2897     let Inst{1-0} = src1;
2898   }
2899
2900 let isExtendable = 1, isNVStorable = 1, hasSideEffects = 0 in
2901 multiclass ST_Idxd<string mnemonic, string CextOp, RegisterClass RC,
2902                  Operand ImmOp, Operand predImmOp, bits<3> MajOp, bit isH = 0> {
2903   let CextOpcode = CextOp, BaseOpcode = CextOp#_indexed in {
2904     def S2_#NAME#_io : T_store_io <mnemonic, RC, ImmOp, MajOp, isH>;
2905
2906     // Predicated
2907     def S2_p#NAME#t_io : T_pstore_io<mnemonic, RC, predImmOp, MajOp, 0, 0, isH>;
2908     def S2_p#NAME#f_io : T_pstore_io<mnemonic, RC, predImmOp, MajOp, 1, 0, isH>;
2909
2910     // Predicated new
2911     def S4_p#NAME#tnew_io : T_pstore_io <mnemonic, RC, predImmOp,
2912                                          MajOp, 0, 1, isH>;
2913     def S4_p#NAME#fnew_io : T_pstore_io <mnemonic, RC, predImmOp,
2914                                          MajOp, 1, 1, isH>;
2915   }
2916 }
2917
2918 let addrMode = BaseImmOffset, InputType = "imm", isCodeGenOnly = 0 in {
2919   let accessSize = ByteAccess in
2920     defm storerb: ST_Idxd < "memb", "STrib", IntRegs, s11_0Ext, u6_0Ext, 0b000>;
2921
2922   let accessSize = HalfWordAccess, opExtentAlign = 1 in
2923     defm storerh: ST_Idxd < "memh", "STrih", IntRegs, s11_1Ext, u6_1Ext, 0b010>;
2924
2925   let accessSize = WordAccess, opExtentAlign = 2 in
2926     defm storeri: ST_Idxd < "memw", "STriw", IntRegs, s11_2Ext, u6_2Ext, 0b100>;
2927
2928   let accessSize = DoubleWordAccess, isNVStorable = 0, opExtentAlign = 3 in
2929     defm storerd: ST_Idxd < "memd", "STrid", DoubleRegs, s11_3Ext,
2930                             u6_3Ext, 0b110>;
2931
2932   let accessSize = HalfWordAccess, opExtentAlign = 1 in
2933     defm storerf: ST_Idxd < "memh", "STrif", IntRegs, s11_1Ext,
2934                             u6_1Ext, 0b011, 1>;
2935 }
2936
2937 def : Pat<(truncstorei8 (i32 IntRegs:$src1), ADDRriS11_0:$addr),
2938           (S2_storerb_io AddrFI:$addr, 0, (i32 IntRegs:$src1))>;
2939
2940 def : Pat<(truncstorei16 (i32 IntRegs:$src1), ADDRriS11_1:$addr),
2941           (S2_storerh_io AddrFI:$addr, 0, (i32 IntRegs:$src1))>;
2942
2943 def : Pat<(store (i32 IntRegs:$src1), ADDRriS11_2:$addr),
2944           (S2_storeri_io AddrFI:$addr, 0, (i32 IntRegs:$src1))>;
2945
2946 def : Pat<(store (i64 DoubleRegs:$src1), ADDRriS11_3:$addr),
2947           (S2_storerd_io AddrFI:$addr, 0, (i64 DoubleRegs:$src1))>;
2948
2949
2950 let AddedComplexity = 10 in {
2951 def : Pat<(truncstorei8 (i32 IntRegs:$src1), (add IntRegs:$src2,
2952                                                   s11_0ExtPred:$offset)),
2953           (S2_storerb_io IntRegs:$src2, s11_0ImmPred:$offset,
2954                          (i32 IntRegs:$src1))>;
2955
2956 def : Pat<(truncstorei16 (i32 IntRegs:$src1), (add IntRegs:$src2,
2957                                                    s11_1ExtPred:$offset)),
2958           (S2_storerh_io IntRegs:$src2, s11_1ImmPred:$offset,
2959                          (i32 IntRegs:$src1))>;
2960
2961 def : Pat<(store (i32 IntRegs:$src1), (add IntRegs:$src2,
2962                                            s11_2ExtPred:$offset)),
2963           (S2_storeri_io IntRegs:$src2, s11_2ImmPred:$offset,
2964                          (i32 IntRegs:$src1))>;
2965
2966 def : Pat<(store (i64 DoubleRegs:$src1), (add IntRegs:$src2,
2967                                               s11_3ExtPred:$offset)),
2968           (S2_storerd_io IntRegs:$src2, s11_3ImmPred:$offset,
2969                          (i64 DoubleRegs:$src1))>;
2970 }
2971
2972 // memh(Rx++#s4:1)=Rt.H
2973
2974 // Store predicate.
2975 let isExtendable = 1, opExtendable = 1, isExtentSigned = 1, opExtentBits = 13,
2976     isCodeGenOnly = 1, isPseudo = 1, hasSideEffects = 0 in
2977 def STriw_pred : STInst<(outs),
2978       (ins IntRegs:$addr, s11_2Ext:$off, PredRegs:$src1),
2979       ".error \"should not emit\"", []>;
2980
2981 // S2_allocframe: Allocate stack frame.
2982 let Defs = [R29, R30], Uses = [R29, R31, R30],
2983     hasSideEffects = 0, accessSize = DoubleWordAccess, isCodeGenOnly = 0 in
2984 def S2_allocframe: ST0Inst <
2985   (outs), (ins u11_3Imm:$u11_3),
2986   "allocframe(#$u11_3)" > {
2987     bits<14> u11_3;
2988
2989     let IClass = 0b1010;
2990     let Inst{27-16} = 0b000010011101;
2991     let Inst{13-11} = 0b000;
2992     let Inst{10-0} = u11_3{13-3};
2993   }
2994
2995 // S2_storer[bhwdf]_pci: Store byte/half/word/double.
2996 // S2_storer[bhwdf]_pci -> S2_storerbnew_pci
2997 let Uses = [CS], isNVStorable = 1 in
2998 class T_store_pci <string mnemonic, RegisterClass RC,
2999                          Operand Imm, bits<4>MajOp,
3000                          MemAccessSize AlignSize, string RegSrc = "Rt">
3001   : STInst <(outs IntRegs:$_dst_),
3002   (ins IntRegs:$Rz, Imm:$offset, ModRegs:$Mu, RC:$Rt),
3003   #mnemonic#"($Rz ++ #$offset:circ($Mu)) = $"#RegSrc#"",
3004   [] ,
3005   "$Rz = $_dst_" > {
3006     bits<5> Rz;
3007     bits<7> offset;
3008     bits<1> Mu;
3009     bits<5> Rt;
3010     let accessSize = AlignSize;
3011
3012     let IClass = 0b1010;
3013     let Inst{27-25} = 0b100;
3014     let Inst{24-21} = MajOp;
3015     let Inst{20-16} = Rz;
3016     let Inst{13} = Mu;
3017     let Inst{12-8} = Rt;
3018     let Inst{7} = 0b0;
3019     let Inst{6-3} =
3020       !if (!eq(!cast<string>(AlignSize), "DoubleWordAccess"), offset{6-3},
3021       !if (!eq(!cast<string>(AlignSize), "WordAccess"),       offset{5-2},
3022       !if (!eq(!cast<string>(AlignSize), "HalfWordAccess"),   offset{4-1},
3023                                        /* ByteAccess */       offset{3-0})));
3024     let Inst{1} = 0b0;
3025   }
3026
3027 let isCodeGenOnly = 0 in {
3028 def S2_storerb_pci : T_store_pci<"memb", IntRegs, s4_0Imm, 0b1000,
3029                                         ByteAccess>;
3030 def S2_storerh_pci : T_store_pci<"memh", IntRegs, s4_1Imm, 0b1010,
3031                                         HalfWordAccess>;
3032 def S2_storerf_pci : T_store_pci<"memh", IntRegs, s4_1Imm, 0b1011,
3033                                         HalfWordAccess, "Rt.h">;
3034 def S2_storeri_pci : T_store_pci<"memw", IntRegs, s4_2Imm, 0b1100,
3035                                         WordAccess>;
3036 def S2_storerd_pci : T_store_pci<"memd", DoubleRegs, s4_3Imm, 0b1110,
3037                                         DoubleWordAccess>;
3038 }
3039
3040 //===----------------------------------------------------------------------===//
3041 // Circular stores with auto-increment register
3042 //===----------------------------------------------------------------------===//
3043 let Uses = [CS], isNVStorable = 1, isCodeGenOnly = 0 in
3044 class T_store_pcr <string mnemonic, RegisterClass RC, bits<4>MajOp,
3045                                MemAccessSize AlignSize, string RegSrc = "Rt">
3046   : STInst <(outs IntRegs:$_dst_),
3047   (ins IntRegs:$Rz, ModRegs:$Mu, RC:$Rt),
3048   #mnemonic#"($Rz ++ I:circ($Mu)) = $"#RegSrc#"",
3049   [],
3050   "$Rz = $_dst_" > {
3051     bits<5> Rz;
3052     bits<1> Mu;
3053     bits<5> Rt;
3054
3055     let accessSize = AlignSize;
3056
3057     let IClass = 0b1010;
3058     let Inst{27-25} = 0b100;
3059     let Inst{24-21} = MajOp;
3060     let Inst{20-16} = Rz;
3061     let Inst{13} = Mu;
3062     let Inst{12-8} = Rt;
3063     let Inst{7} = 0b0;
3064     let Inst{1} = 0b1;
3065   }
3066
3067 let isCodeGenOnly = 0 in {
3068 def S2_storerb_pcr : T_store_pcr<"memb", IntRegs, 0b1000, ByteAccess>;
3069 def S2_storerh_pcr : T_store_pcr<"memh", IntRegs, 0b1010, HalfWordAccess>;
3070 def S2_storeri_pcr : T_store_pcr<"memw", IntRegs, 0b1100, WordAccess>;
3071 def S2_storerd_pcr : T_store_pcr<"memd", DoubleRegs, 0b1110, DoubleWordAccess>;
3072 def S2_storerf_pcr : T_store_pcr<"memh", IntRegs, 0b1011,
3073                                  HalfWordAccess, "Rt.h">;
3074 }
3075
3076 //===----------------------------------------------------------------------===//
3077 // Bit-reversed stores with auto-increment register
3078 //===----------------------------------------------------------------------===//
3079 let hasSideEffects = 0 in
3080 class T_store_pbr<string mnemonic, RegisterClass RC,
3081                             MemAccessSize addrSize, bits<3> majOp,
3082                             bit isHalf = 0>
3083   : STInst
3084     <(outs IntRegs:$_dst_),
3085      (ins IntRegs:$Rz, ModRegs:$Mu, RC:$src),
3086      #mnemonic#"($Rz ++ $Mu:brev) = $src"#!if (!eq(isHalf, 1), ".h", ""),
3087      [], "$Rz = $_dst_" > {
3088
3089       let accessSize = addrSize;
3090
3091       bits<5> Rz;
3092       bits<1> Mu;
3093       bits<5> src;
3094
3095       let IClass = 0b1010;
3096
3097       let Inst{27-24} = 0b1111;
3098       let Inst{23-21} = majOp;
3099       let Inst{7} = 0b0;
3100       let Inst{20-16} = Rz;
3101       let Inst{13} = Mu;
3102       let Inst{12-8} = src;
3103     }
3104
3105 let isNVStorable = 1, isCodeGenOnly = 0 in {
3106   let BaseOpcode = "S2_storerb_pbr" in
3107   def S2_storerb_pbr : T_store_pbr<"memb", IntRegs, ByteAccess,
3108                                              0b000>, NewValueRel;
3109   let BaseOpcode = "S2_storerh_pbr" in
3110   def S2_storerh_pbr : T_store_pbr<"memh", IntRegs, HalfWordAccess,
3111                                              0b010>, NewValueRel;
3112   let BaseOpcode = "S2_storeri_pbr" in
3113   def S2_storeri_pbr : T_store_pbr<"memw", IntRegs, WordAccess,
3114                                              0b100>, NewValueRel;
3115 }
3116 let isCodeGenOnly = 0 in {
3117 def S2_storerf_pbr : T_store_pbr<"memh", IntRegs, HalfWordAccess, 0b011, 1>;
3118 def S2_storerd_pbr : T_store_pbr<"memd", DoubleRegs, DoubleWordAccess, 0b110>;
3119 }
3120
3121 //===----------------------------------------------------------------------===//
3122 // ST -
3123 //===----------------------------------------------------------------------===//
3124
3125 //===----------------------------------------------------------------------===//
3126 // STYPE/ALU +
3127 //===----------------------------------------------------------------------===//
3128 // Logical NOT.
3129 def NOT_rr64 : ALU64_rr<(outs DoubleRegs:$dst), (ins DoubleRegs:$src1),
3130                "$dst = not($src1)",
3131                [(set (i64 DoubleRegs:$dst), (not (i64 DoubleRegs:$src1)))]>;
3132
3133
3134 //===----------------------------------------------------------------------===//
3135 // STYPE/ALU -
3136 //===----------------------------------------------------------------------===//
3137
3138 let hasSideEffects = 0 in
3139 class T_S2op_1 <string mnemonic, bits<4> RegTyBits, RegisterClass RCOut,
3140                 RegisterClass RCIn, bits<2> MajOp, bits<3> MinOp, bit isSat>
3141   : SInst <(outs RCOut:$dst), (ins RCIn:$src),
3142   "$dst = "#mnemonic#"($src)"#!if(isSat, ":sat", ""),
3143   [], "", S_2op_tc_1_SLOT23 > {
3144     bits<5> dst;
3145     bits<5> src;
3146
3147     let IClass = 0b1000;
3148
3149     let Inst{27-24} = RegTyBits;
3150     let Inst{23-22} = MajOp;
3151     let Inst{21} = 0b0;
3152     let Inst{20-16} = src;
3153     let Inst{7-5} = MinOp;
3154     let Inst{4-0} = dst;
3155   }
3156
3157 class T_S2op_1_di <string mnemonic, bits<2> MajOp, bits<3> MinOp>
3158   : T_S2op_1 <mnemonic, 0b0100, DoubleRegs, IntRegs, MajOp, MinOp, 0>;
3159
3160 let hasNewValue = 1 in
3161 class T_S2op_1_id <string mnemonic, bits<2> MajOp, bits<3> MinOp, bit isSat = 0>
3162   : T_S2op_1 <mnemonic, 0b1000, IntRegs, DoubleRegs, MajOp, MinOp, isSat>;
3163
3164 let hasNewValue = 1 in
3165 class T_S2op_1_ii <string mnemonic, bits<2> MajOp, bits<3> MinOp, bit isSat = 0>
3166   : T_S2op_1 <mnemonic, 0b1100, IntRegs, IntRegs, MajOp, MinOp, isSat>;
3167
3168 // Sign extend word to doubleword
3169 let isCodeGenOnly = 0 in
3170 def A2_sxtw   : T_S2op_1_di <"sxtw", 0b01, 0b000>;
3171
3172 def: Pat <(i64 (sext I32:$src)), (A2_sxtw I32:$src)>;
3173
3174 // Swizzle the bytes of a word
3175 let isCodeGenOnly = 0 in
3176 def A2_swiz : T_S2op_1_ii <"swiz", 0b10, 0b111>;
3177
3178 // Saturate
3179 let Defs = [USR_OVF], isCodeGenOnly = 0 in {
3180   def A2_sat   : T_S2op_1_id <"sat", 0b11, 0b000>;
3181   def A2_satb  : T_S2op_1_ii <"satb", 0b11, 0b111>;
3182   def A2_satub : T_S2op_1_ii <"satub", 0b11, 0b110>;
3183   def A2_sath  : T_S2op_1_ii <"sath", 0b11, 0b100>;
3184   def A2_satuh : T_S2op_1_ii <"satuh", 0b11, 0b101>;
3185 }
3186
3187 let Itinerary = S_2op_tc_2_SLOT23, isCodeGenOnly = 0 in {
3188   // Absolute value word
3189   def A2_abs    : T_S2op_1_ii <"abs", 0b10, 0b100>;
3190
3191   let Defs = [USR_OVF] in
3192   def A2_abssat : T_S2op_1_ii <"abs", 0b10, 0b101, 1>;
3193
3194   // Negate with saturation
3195   let Defs = [USR_OVF] in
3196   def A2_negsat : T_S2op_1_ii <"neg", 0b10, 0b110, 1>;
3197 }
3198
3199 def: Pat<(i32 (select (i1 (setlt (i32 IntRegs:$src), 0)),
3200                       (i32 (sub 0, (i32 IntRegs:$src))),
3201                       (i32 IntRegs:$src))),
3202          (A2_abs IntRegs:$src)>;
3203
3204 let AddedComplexity = 50 in
3205 def: Pat<(i32 (xor (add (sra (i32 IntRegs:$src), (i32 31)),
3206                         (i32 IntRegs:$src)),
3207                    (sra (i32 IntRegs:$src), (i32 31)))),
3208          (A2_abs IntRegs:$src)>;
3209
3210 class T_S2op_2 <string mnemonic, bits<4> RegTyBits, RegisterClass RCOut,
3211                 RegisterClass RCIn, bits<3> MajOp, bits<3> MinOp,
3212                 bit isSat, bit isRnd, list<dag> pattern = []>
3213   : SInst <(outs RCOut:$dst),
3214   (ins RCIn:$src, u5Imm:$u5),
3215   "$dst = "#mnemonic#"($src, #$u5)"#!if(isSat, ":sat", "")
3216                                    #!if(isRnd, ":rnd", ""),
3217   pattern, "", S_2op_tc_2_SLOT23> {
3218     bits<5> dst;
3219     bits<5> src;
3220     bits<5> u5;
3221
3222     let IClass = 0b1000;
3223
3224     let Inst{27-24} = RegTyBits;
3225     let Inst{23-21} = MajOp;
3226     let Inst{20-16} = src;
3227     let Inst{13} = 0b0;
3228     let Inst{12-8} = u5;
3229     let Inst{7-5} = MinOp;
3230     let Inst{4-0} = dst;
3231   }
3232
3233 class T_S2op_2_di <string mnemonic, bits<3> MajOp, bits<3> MinOp>
3234   : T_S2op_2 <mnemonic, 0b1000, DoubleRegs, IntRegs, MajOp, MinOp, 0, 0>;
3235
3236 let hasNewValue = 1 in
3237 class T_S2op_2_id <string mnemonic, bits<3> MajOp, bits<3> MinOp>
3238   : T_S2op_2 <mnemonic, 0b1000, IntRegs, DoubleRegs, MajOp, MinOp, 0, 0>;
3239   
3240 let hasNewValue = 1 in
3241 class T_S2op_2_ii <string mnemonic, bits<3> MajOp, bits<3> MinOp,
3242                    bit isSat = 0, bit isRnd = 0, list<dag> pattern = []>
3243   : T_S2op_2 <mnemonic, 0b1100, IntRegs, IntRegs, MajOp, MinOp,
3244               isSat, isRnd, pattern>;
3245
3246 class T_S2op_shift <string mnemonic, bits<3> MajOp, bits<3> MinOp, SDNode OpNd>
3247   : T_S2op_2_ii <mnemonic, MajOp, MinOp, 0, 0,
3248     [(set (i32 IntRegs:$dst), (OpNd (i32 IntRegs:$src),
3249                                     (u5ImmPred:$u5)))]>;
3250
3251 // Arithmetic/logical shift right/left by immediate
3252 let Itinerary = S_2op_tc_1_SLOT23, isCodeGenOnly = 0 in {
3253   def S2_asr_i_r : T_S2op_shift <"asr", 0b000, 0b000, sra>;
3254   def S2_lsr_i_r : T_S2op_shift <"lsr", 0b000, 0b001, srl>;
3255   def S2_asl_i_r : T_S2op_shift <"asl", 0b000, 0b010, shl>;
3256 }
3257
3258 // Shift left by immediate with saturation
3259 let Defs = [USR_OVF], isCodeGenOnly = 0 in
3260 def S2_asl_i_r_sat : T_S2op_2_ii <"asl", 0b010, 0b010, 1>;
3261
3262 // Shift right with round
3263 let isCodeGenOnly = 0 in
3264 def S2_asr_i_r_rnd : T_S2op_2_ii <"asr", 0b010, 0b000, 0, 1>;
3265
3266 def: Pat<(i32 (sra (i32 (add (i32 (sra I32:$src1, u5ImmPred:$src2)),
3267                              (i32 1))),
3268                    (i32 1))),
3269          (S2_asr_i_r_rnd IntRegs:$src1, u5ImmPred:$src2)>;
3270
3271 class T_S2op_3<string opc, bits<2>MajOp, bits<3>minOp, bits<1> sat = 0>
3272   : SInst<(outs DoubleRegs:$Rdd), (ins DoubleRegs:$Rss),
3273            "$Rdd = "#opc#"($Rss)"#!if(!eq(sat, 1),":sat","")> {
3274   bits<5> Rss;
3275   bits<5> Rdd;
3276   let IClass = 0b1000;
3277   let Inst{27-24} = 0;
3278   let Inst{23-22} = MajOp;
3279   let Inst{20-16} = Rss;
3280   let Inst{7-5} = minOp;
3281   let Inst{4-0} = Rdd;
3282 }
3283
3284 let isCodeGenOnly = 0 in {
3285 def A2_absp : T_S2op_3 <"abs", 0b10, 0b110>;
3286 def A2_negp : T_S2op_3 <"neg", 0b10, 0b101>;
3287 def A2_notp : T_S2op_3 <"not", 0b10, 0b100>;
3288 }
3289
3290 // Innterleave/deinterleave
3291 let isCodeGenOnly = 0 in {
3292 def S2_interleave   : T_S2op_3 <"interleave",   0b11, 0b101>;
3293 def S2_deinterleave : T_S2op_3 <"deinterleave", 0b11, 0b100>;
3294 }
3295
3296 //===----------------------------------------------------------------------===//
3297 // STYPE/BIT +
3298 //===----------------------------------------------------------------------===//
3299 // Bit count
3300
3301 let hasSideEffects = 0, hasNewValue = 1 in
3302 class T_COUNT_LEADING<string MnOp, bits<3> MajOp, bits<3> MinOp, bit Is32,
3303                 dag Out, dag Inp>
3304     : SInst<Out, Inp, "$Rd = "#MnOp#"($Rs)", [], "", S_2op_tc_1_SLOT23> {
3305   bits<5> Rs;
3306   bits<5> Rd;
3307   let IClass = 0b1000;
3308   let Inst{27} = 0b1;
3309   let Inst{26} = Is32;
3310   let Inst{25-24} = 0b00;
3311   let Inst{23-21} = MajOp;
3312   let Inst{20-16} = Rs;
3313   let Inst{7-5} = MinOp;
3314   let Inst{4-0} = Rd;
3315 }
3316
3317 class T_COUNT_LEADING_32<string MnOp, bits<3> MajOp, bits<3> MinOp>
3318     : T_COUNT_LEADING<MnOp, MajOp, MinOp, 0b1,
3319                       (outs IntRegs:$Rd), (ins IntRegs:$Rs)>;
3320
3321 class T_COUNT_LEADING_64<string MnOp, bits<3> MajOp, bits<3> MinOp>
3322     : T_COUNT_LEADING<MnOp, MajOp, MinOp, 0b0,
3323                       (outs IntRegs:$Rd), (ins DoubleRegs:$Rs)>;
3324
3325 let isCodeGenOnly = 0 in {
3326 def S2_cl0     : T_COUNT_LEADING_32<"cl0",     0b000, 0b101>;
3327 def S2_cl1     : T_COUNT_LEADING_32<"cl1",     0b000, 0b110>;
3328 def S2_ct0     : T_COUNT_LEADING_32<"ct0",     0b010, 0b100>;
3329 def S2_ct1     : T_COUNT_LEADING_32<"ct1",     0b010, 0b101>;
3330 def S2_cl0p    : T_COUNT_LEADING_64<"cl0",     0b010, 0b010>;
3331 def S2_cl1p    : T_COUNT_LEADING_64<"cl1",     0b010, 0b100>;
3332 def S2_clb     : T_COUNT_LEADING_32<"clb",     0b000, 0b100>;
3333 def S2_clbp    : T_COUNT_LEADING_64<"clb",     0b010, 0b000>;
3334 def S2_clbnorm : T_COUNT_LEADING_32<"normamt", 0b000, 0b111>;
3335 }
3336
3337 def: Pat<(i32 (ctlz I32:$Rs)),                (S2_cl0 I32:$Rs)>;
3338 def: Pat<(i32 (ctlz (not I32:$Rs))),          (S2_cl1 I32:$Rs)>;
3339 def: Pat<(i32 (cttz I32:$Rs)),                (S2_ct0 I32:$Rs)>;
3340 def: Pat<(i32 (cttz (not I32:$Rs))),          (S2_ct1 I32:$Rs)>;
3341 def: Pat<(i32 (trunc (ctlz I64:$Rss))),       (S2_cl0p I64:$Rss)>;
3342 def: Pat<(i32 (trunc (ctlz (not I64:$Rss)))), (S2_cl1p I64:$Rss)>;
3343
3344 // Bit set/clear/toggle
3345
3346 let hasSideEffects = 0, hasNewValue = 1 in
3347 class T_SCT_BIT_IMM<string MnOp, bits<3> MinOp>
3348     : SInst<(outs IntRegs:$Rd), (ins IntRegs:$Rs, u5Imm:$u5),
3349             "$Rd = "#MnOp#"($Rs, #$u5)", [], "", S_2op_tc_1_SLOT23> {
3350   bits<5> Rd;
3351   bits<5> Rs;
3352   bits<5> u5;
3353   let IClass = 0b1000;
3354   let Inst{27-21} = 0b1100110;
3355   let Inst{20-16} = Rs;
3356   let Inst{13} = 0b0;
3357   let Inst{12-8} = u5;
3358   let Inst{7-5} = MinOp;
3359   let Inst{4-0} = Rd;
3360 }
3361
3362 let hasSideEffects = 0, hasNewValue = 1 in
3363 class T_SCT_BIT_REG<string MnOp, bits<2> MinOp>
3364     : SInst<(outs IntRegs:$Rd), (ins IntRegs:$Rs, IntRegs:$Rt),
3365             "$Rd = "#MnOp#"($Rs, $Rt)", [], "", S_3op_tc_1_SLOT23> {
3366   bits<5> Rd;
3367   bits<5> Rs;
3368   bits<5> Rt;
3369   let IClass = 0b1100;
3370   let Inst{27-22} = 0b011010;
3371   let Inst{20-16} = Rs;
3372   let Inst{12-8} = Rt;
3373   let Inst{7-6} = MinOp;
3374   let Inst{4-0} = Rd;
3375 }
3376
3377 let isCodeGenOnly = 0 in {
3378 def S2_clrbit_i    : T_SCT_BIT_IMM<"clrbit",    0b001>;
3379 def S2_setbit_i    : T_SCT_BIT_IMM<"setbit",    0b000>;
3380 def S2_togglebit_i : T_SCT_BIT_IMM<"togglebit", 0b010>;
3381 def S2_clrbit_r    : T_SCT_BIT_REG<"clrbit",    0b01>;
3382 def S2_setbit_r    : T_SCT_BIT_REG<"setbit",    0b00>;
3383 def S2_togglebit_r : T_SCT_BIT_REG<"togglebit", 0b10>;
3384 }
3385
3386 def: Pat<(i32 (and (i32 IntRegs:$Rs), (not (shl 1, u5ImmPred:$u5)))),
3387          (S2_clrbit_i IntRegs:$Rs, u5ImmPred:$u5)>;
3388 def: Pat<(i32 (or (i32 IntRegs:$Rs), (shl 1, u5ImmPred:$u5))),
3389          (S2_setbit_i IntRegs:$Rs, u5ImmPred:$u5)>;
3390 def: Pat<(i32 (xor (i32 IntRegs:$Rs), (shl 1, u5ImmPred:$u5))),
3391          (S2_togglebit_i IntRegs:$Rs, u5ImmPred:$u5)>;
3392 def: Pat<(i32 (and (i32 IntRegs:$Rs), (not (shl 1, (i32 IntRegs:$Rt))))),
3393          (S2_clrbit_r IntRegs:$Rs, IntRegs:$Rt)>;
3394 def: Pat<(i32 (or (i32 IntRegs:$Rs), (shl 1, (i32 IntRegs:$Rt)))),
3395          (S2_setbit_r IntRegs:$Rs, IntRegs:$Rt)>;
3396 def: Pat<(i32 (xor (i32 IntRegs:$Rs), (shl 1, (i32 IntRegs:$Rt)))),
3397          (S2_togglebit_r IntRegs:$Rs, IntRegs:$Rt)>;
3398
3399 // Bit test
3400
3401 let hasSideEffects = 0 in
3402 class T_TEST_BIT_IMM<string MnOp, bits<3> MajOp>
3403     : SInst<(outs PredRegs:$Pd), (ins IntRegs:$Rs, u5Imm:$u5),
3404             "$Pd = "#MnOp#"($Rs, #$u5)",
3405             [], "", S_2op_tc_2early_SLOT23> {
3406   bits<2> Pd;
3407   bits<5> Rs;
3408   bits<5> u5;
3409   let IClass = 0b1000;
3410   let Inst{27-24} = 0b0101;
3411   let Inst{23-21} = MajOp;
3412   let Inst{20-16} = Rs;
3413   let Inst{13} = 0;
3414   let Inst{12-8} = u5;
3415   let Inst{1-0} = Pd;
3416 }
3417
3418 let hasSideEffects = 0 in
3419 class T_TEST_BIT_REG<string MnOp, bit IsNeg>
3420     : SInst<(outs PredRegs:$Pd), (ins IntRegs:$Rs, IntRegs:$Rt),
3421             "$Pd = "#MnOp#"($Rs, $Rt)",
3422             [], "", S_3op_tc_2early_SLOT23> {
3423   bits<2> Pd;
3424   bits<5> Rs;
3425   bits<5> Rt;
3426   let IClass = 0b1100;
3427   let Inst{27-22} = 0b011100;
3428   let Inst{21} = IsNeg;
3429   let Inst{20-16} = Rs;
3430   let Inst{12-8} = Rt;
3431   let Inst{1-0} = Pd;
3432 }
3433
3434 let isCodeGenOnly = 0 in {
3435 def S2_tstbit_i : T_TEST_BIT_IMM<"tstbit", 0b000>;
3436 def S2_tstbit_r : T_TEST_BIT_REG<"tstbit", 0>;
3437 }
3438
3439 let AddedComplexity = 20 in { // Complexity greater than cmp reg-imm.
3440   def: Pat<(i1 (setne (and (shl 1, u5ImmPred:$u5), (i32 IntRegs:$Rs)), 0)),
3441            (S2_tstbit_i IntRegs:$Rs, u5ImmPred:$u5)>;
3442   def: Pat<(i1 (setne (and (shl 1, (i32 IntRegs:$Rt)), (i32 IntRegs:$Rs)), 0)),
3443            (S2_tstbit_r IntRegs:$Rs, IntRegs:$Rt)>;
3444   def: Pat<(i1 (trunc (i32 IntRegs:$Rs))),
3445            (S2_tstbit_i IntRegs:$Rs, 0)>;
3446   def: Pat<(i1 (trunc (i64 DoubleRegs:$Rs))),
3447            (S2_tstbit_i (LoReg DoubleRegs:$Rs), 0)>;
3448 }
3449 let hasSideEffects = 0 in
3450 class T_TEST_BITS_IMM<string MnOp, bits<2> MajOp, bit IsNeg>
3451     : SInst<(outs PredRegs:$Pd), (ins IntRegs:$Rs, u6Imm:$u6),
3452             "$Pd = "#MnOp#"($Rs, #$u6)",
3453             [], "", S_2op_tc_2early_SLOT23> {
3454   bits<2> Pd;
3455   bits<5> Rs;
3456   bits<6> u6;
3457   let IClass = 0b1000;
3458   let Inst{27-24} = 0b0101;
3459   let Inst{23-22} = MajOp;
3460   let Inst{21} = IsNeg;
3461   let Inst{20-16} = Rs;
3462   let Inst{13-8} = u6;
3463   let Inst{1-0} = Pd;
3464 }
3465
3466 let hasSideEffects = 0 in
3467 class T_TEST_BITS_REG<string MnOp, bits<2> MajOp, bit IsNeg>
3468     : SInst<(outs PredRegs:$Pd), (ins IntRegs:$Rs, IntRegs:$Rt),
3469             "$Pd = "#MnOp#"($Rs, $Rt)",
3470             [], "", S_3op_tc_2early_SLOT23> {
3471   bits<2> Pd;
3472   bits<5> Rs;
3473   bits<5> Rt;
3474   let IClass = 0b1100;
3475   let Inst{27-24} = 0b0111;
3476   let Inst{23-22} = MajOp;
3477   let Inst{21} = IsNeg;
3478   let Inst{20-16} = Rs;
3479   let Inst{12-8} = Rt;
3480   let Inst{1-0} = Pd;
3481 }
3482
3483 let isCodeGenOnly = 0 in {
3484 def C2_bitsclri : T_TEST_BITS_IMM<"bitsclr", 0b10, 0>;
3485 def C2_bitsclr  : T_TEST_BITS_REG<"bitsclr", 0b10, 0>;
3486 def C2_bitsset  : T_TEST_BITS_REG<"bitsset", 0b01, 0>;
3487 }
3488
3489 let AddedComplexity = 20 in { // Complexity greater than compare reg-imm.
3490   def: Pat<(i1 (seteq (and (i32 IntRegs:$Rs), u6ImmPred:$u6), 0)),
3491            (C2_bitsclri IntRegs:$Rs, u6ImmPred:$u6)>;
3492   def: Pat<(i1 (seteq (and (i32 IntRegs:$Rs), (i32 IntRegs:$Rt)), 0)),
3493            (C2_bitsclr IntRegs:$Rs, IntRegs:$Rt)>;
3494 }
3495
3496 let AddedComplexity = 10 in   // Complexity greater than compare reg-reg.
3497 def: Pat<(i1 (seteq (and (i32 IntRegs:$Rs), (i32 IntRegs:$Rt)), IntRegs:$Rt)),
3498          (C2_bitsset IntRegs:$Rs, IntRegs:$Rt)>;
3499
3500 //===----------------------------------------------------------------------===//
3501 // STYPE/BIT -
3502 //===----------------------------------------------------------------------===//
3503
3504 //===----------------------------------------------------------------------===//
3505 // STYPE/COMPLEX +
3506 //===----------------------------------------------------------------------===//
3507 //===----------------------------------------------------------------------===//
3508 // STYPE/COMPLEX -
3509 //===----------------------------------------------------------------------===//
3510
3511 //===----------------------------------------------------------------------===//
3512 // XTYPE/PERM +
3513 //===----------------------------------------------------------------------===//
3514
3515 //===----------------------------------------------------------------------===//
3516 // XTYPE/PERM -
3517 //===----------------------------------------------------------------------===//
3518
3519 //===----------------------------------------------------------------------===//
3520 // STYPE/PRED +
3521 //===----------------------------------------------------------------------===//
3522
3523 // Predicate transfer.
3524 let hasSideEffects = 0, hasNewValue = 1, isCodeGenOnly = 0 in
3525 def C2_tfrpr : SInst<(outs IntRegs:$Rd), (ins PredRegs:$Ps),
3526       "$Rd = $Ps", [], "", S_2op_tc_1_SLOT23> {
3527   bits<5> Rd;
3528   bits<2> Ps;
3529
3530   let IClass = 0b1000;
3531   let Inst{27-24} = 0b1001;
3532   let Inst{22} = 0b1;
3533   let Inst{17-16} = Ps;
3534   let Inst{4-0} = Rd;
3535 }
3536
3537 // Transfer general register to predicate.
3538 let hasSideEffects = 0, isCodeGenOnly = 0 in
3539 def C2_tfrrp: SInst<(outs PredRegs:$Pd), (ins IntRegs:$Rs),
3540       "$Pd = $Rs", [], "", S_2op_tc_2early_SLOT23> {
3541   bits<2> Pd;
3542   bits<5> Rs;
3543
3544   let IClass = 0b1000;
3545   let Inst{27-21} = 0b0101010;
3546   let Inst{20-16} = Rs;
3547   let Inst{1-0} = Pd;
3548 }
3549
3550
3551 //===----------------------------------------------------------------------===//
3552 // STYPE/PRED -
3553 //===----------------------------------------------------------------------===//
3554
3555 //===----------------------------------------------------------------------===//
3556 // STYPE/SHIFT +
3557 //===----------------------------------------------------------------------===//
3558 class S_2OpInstImm<string Mnemonic, bits<3>MajOp, bits<3>MinOp,
3559                    Operand Imm, list<dag> pattern = [], bit isRnd = 0>
3560   : SInst<(outs DoubleRegs:$dst), (ins DoubleRegs:$src1, Imm:$src2),
3561            "$dst = "#Mnemonic#"($src1, #$src2)"#!if(isRnd, ":rnd", ""),
3562            pattern> {
3563   bits<5> src1;
3564   bits<5> dst;
3565   let IClass = 0b1000;
3566   let Inst{27-24} = 0;
3567   let Inst{23-21} = MajOp;
3568   let Inst{20-16} = src1;
3569   let Inst{7-5} = MinOp;
3570   let Inst{4-0} = dst;
3571 }
3572
3573 class S_2OpInstImmI6<string Mnemonic, SDNode OpNode, bits<3>MinOp>
3574   : S_2OpInstImm<Mnemonic, 0b000, MinOp, u6Imm,
3575   [(set (i64 DoubleRegs:$dst), (OpNode (i64 DoubleRegs:$src1),
3576                                         u6ImmPred:$src2))]> {
3577   bits<6> src2;
3578   let Inst{13-8} = src2;
3579 }
3580
3581 // Shift by immediate.
3582 let isCodeGenOnly = 0 in {
3583 def S2_asr_i_p : S_2OpInstImmI6<"asr", sra, 0b000>;
3584 def S2_asl_i_p : S_2OpInstImmI6<"asl", shl, 0b010>;
3585 def S2_lsr_i_p : S_2OpInstImmI6<"lsr", srl, 0b001>;
3586 }
3587
3588 // Shift left by small amount and add.
3589 let AddedComplexity = 100, hasNewValue = 1, hasSideEffects = 0,
3590     isCodeGenOnly = 0 in
3591 def S2_addasl_rrri: SInst <(outs IntRegs:$Rd),
3592                            (ins IntRegs:$Rt, IntRegs:$Rs, u3Imm:$u3),
3593   "$Rd = addasl($Rt, $Rs, #$u3)" ,
3594   [(set (i32 IntRegs:$Rd), (add (i32 IntRegs:$Rt),
3595                                 (shl (i32 IntRegs:$Rs), u3ImmPred:$u3)))],
3596   "", S_3op_tc_2_SLOT23> {
3597     bits<5> Rd;
3598     bits<5> Rt;
3599     bits<5> Rs;
3600     bits<3> u3;
3601
3602     let IClass = 0b1100;
3603
3604     let Inst{27-21} = 0b0100000;
3605     let Inst{20-16} = Rs;
3606     let Inst{13}    = 0b0;
3607     let Inst{12-8}  = Rt;
3608     let Inst{7-5}   = u3;
3609     let Inst{4-0}   = Rd;
3610   }
3611
3612 //===----------------------------------------------------------------------===//
3613 // STYPE/SHIFT -
3614 //===----------------------------------------------------------------------===//
3615
3616 //===----------------------------------------------------------------------===//
3617 // STYPE/VH +
3618 //===----------------------------------------------------------------------===//
3619 //===----------------------------------------------------------------------===//
3620 // STYPE/VH -
3621 //===----------------------------------------------------------------------===//
3622
3623 //===----------------------------------------------------------------------===//
3624 // STYPE/VW +
3625 //===----------------------------------------------------------------------===//
3626 //===----------------------------------------------------------------------===//
3627 // STYPE/VW -
3628 //===----------------------------------------------------------------------===//
3629
3630 //===----------------------------------------------------------------------===//
3631 // SYSTEM/SUPER +
3632 //===----------------------------------------------------------------------===//
3633
3634 //===----------------------------------------------------------------------===//
3635 // SYSTEM/USER +
3636 //===----------------------------------------------------------------------===//
3637 def HexagonBARRIER: SDNode<"HexagonISD::BARRIER", SDTNone, [SDNPHasChain]>;
3638
3639 let hasSideEffects = 1, isSoloAX = 1, isCodeGenOnly = 0 in
3640 def BARRIER : SYSInst<(outs), (ins),
3641                      "barrier",
3642                      [(HexagonBARRIER)],"",ST_tc_st_SLOT0> {
3643   let Inst{31-28} = 0b1010;
3644   let Inst{27-21} = 0b1000000;
3645 }
3646
3647 //===----------------------------------------------------------------------===//
3648 // SYSTEM/SUPER -
3649 //===----------------------------------------------------------------------===//
3650 //===----------------------------------------------------------------------===//
3651 // CRUSER - Type.
3652 //===----------------------------------------------------------------------===//
3653 // HW loop
3654 let isExtendable = 1, isExtentSigned = 1, opExtentBits = 9, opExtentAlign = 2,
3655     opExtendable = 0, hasSideEffects = 0 in
3656 class LOOP_iBase<string mnemonic, Operand brOp, bit mustExtend = 0>
3657          : CRInst<(outs), (ins brOp:$offset, u10Imm:$src2),
3658            #mnemonic#"($offset, #$src2)",
3659            [], "" , CR_tc_3x_SLOT3> {
3660     bits<9> offset;
3661     bits<10> src2;
3662
3663     let IClass = 0b0110;
3664
3665     let Inst{27-22} = 0b100100;
3666     let Inst{21} = !if (!eq(mnemonic, "loop0"), 0b0, 0b1);
3667     let Inst{20-16} = src2{9-5};
3668     let Inst{12-8} = offset{8-4};
3669     let Inst{7-5} = src2{4-2};
3670     let Inst{4-3} = offset{3-2};
3671     let Inst{1-0} = src2{1-0};
3672 }
3673
3674 let isExtendable = 1, isExtentSigned = 1, opExtentBits = 9, opExtentAlign = 2,
3675     opExtendable = 0, hasSideEffects = 0 in
3676 class LOOP_rBase<string mnemonic, Operand brOp, bit mustExtend = 0>
3677          : CRInst<(outs), (ins brOp:$offset, IntRegs:$src2),
3678            #mnemonic#"($offset, $src2)",
3679            [], "" ,CR_tc_3x_SLOT3> {
3680     bits<9> offset;
3681     bits<5> src2;
3682
3683     let IClass = 0b0110;
3684
3685     let Inst{27-22} = 0b000000;
3686     let Inst{21} = !if (!eq(mnemonic, "loop0"), 0b0, 0b1);
3687     let Inst{20-16} = src2;
3688     let Inst{12-8} = offset{8-4};
3689     let Inst{4-3} = offset{3-2};
3690   }
3691
3692 multiclass LOOP_ri<string mnemonic> {
3693   def i : LOOP_iBase<mnemonic, brtarget>;
3694   def r : LOOP_rBase<mnemonic, brtarget>;
3695 }
3696
3697
3698 let Defs = [SA0, LC0, USR], isCodeGenOnly = 0 in
3699 defm J2_loop0 : LOOP_ri<"loop0">;
3700
3701 // Interestingly only loop0's appear to set usr.lpcfg
3702 let Defs = [SA1, LC1], isCodeGenOnly = 0 in
3703 defm J2_loop1 : LOOP_ri<"loop1">;
3704
3705 let isBranch = 1, isTerminator = 1, hasSideEffects = 0,
3706     Defs = [PC, LC0], Uses = [SA0, LC0] in {
3707 def ENDLOOP0 : Endloop<(outs), (ins brtarget:$offset),
3708                        ":endloop0",
3709                        []>;
3710 }
3711
3712 let isBranch = 1, isTerminator = 1, hasSideEffects = 0,
3713     Defs = [PC, LC1], Uses = [SA1, LC1] in {
3714 def ENDLOOP1 : Endloop<(outs), (ins brtarget:$offset),
3715                        ":endloop1",
3716                        []>;
3717 }
3718
3719 // Pipelined loop instructions, sp[123]loop0
3720 let Defs = [LC0, SA0, P3, USR], hasSideEffects = 0,
3721     isExtentSigned = 1, isExtendable = 1, opExtentBits = 9, opExtentAlign = 2,
3722     opExtendable = 0, isPredicateLate = 1 in
3723 class SPLOOP_iBase<string SP, bits<2> op>
3724   : CRInst <(outs), (ins brtarget:$r7_2, u10Imm:$U10),
3725   "p3 = sp"#SP#"loop0($r7_2, #$U10)" > {
3726     bits<9> r7_2;
3727     bits<10> U10;
3728
3729     let IClass = 0b0110;
3730
3731     let Inst{22-21} = op;
3732     let Inst{27-23} = 0b10011;
3733     let Inst{20-16} = U10{9-5};
3734     let Inst{12-8} = r7_2{8-4};
3735     let Inst{7-5} = U10{4-2};
3736     let Inst{4-3} = r7_2{3-2};
3737     let Inst{1-0} = U10{1-0};
3738   }
3739
3740 let Defs = [LC0, SA0, P3, USR], hasSideEffects = 0,
3741     isExtentSigned = 1, isExtendable = 1, opExtentBits = 9, opExtentAlign = 2,
3742     opExtendable = 0, isPredicateLate = 1 in
3743 class SPLOOP_rBase<string SP, bits<2> op>
3744   : CRInst <(outs), (ins brtarget:$r7_2, IntRegs:$Rs),
3745   "p3 = sp"#SP#"loop0($r7_2, $Rs)" > {
3746     bits<9> r7_2;
3747     bits<5> Rs;
3748
3749     let IClass = 0b0110;
3750
3751     let Inst{22-21} = op;
3752     let Inst{27-23} = 0b00001;
3753     let Inst{20-16} = Rs;
3754     let Inst{12-8} = r7_2{8-4};
3755     let Inst{4-3} = r7_2{3-2};
3756   }
3757
3758 multiclass SPLOOP_ri<string mnemonic, bits<2> op> {
3759   def i : SPLOOP_iBase<mnemonic, op>;
3760   def r : SPLOOP_rBase<mnemonic, op>;
3761 }
3762
3763 let isCodeGenOnly = 0 in {
3764 defm J2_ploop1s : SPLOOP_ri<"1", 0b01>;
3765 defm J2_ploop2s : SPLOOP_ri<"2", 0b10>;
3766 defm J2_ploop3s : SPLOOP_ri<"3", 0b11>;
3767 }
3768
3769 // Transfer to/from Control/GPR Guest/GPR
3770 let hasSideEffects = 0 in
3771 class TFR_CR_RS_base<RegisterClass CTRC, RegisterClass RC, bit isDouble>
3772   : CRInst <(outs CTRC:$dst), (ins RC:$src),
3773   "$dst = $src", [], "", CR_tc_3x_SLOT3> {
3774     bits<5> dst;
3775     bits<5> src;
3776
3777     let IClass = 0b0110;
3778
3779     let Inst{27-25} = 0b001;
3780     let Inst{24} = isDouble;
3781     let Inst{23-21} = 0b001;
3782     let Inst{20-16} = src;
3783     let Inst{4-0} = dst;
3784   }
3785 let isCodeGenOnly = 0 in
3786 def A2_tfrrcr : TFR_CR_RS_base<CtrRegs, IntRegs, 0b0>;
3787 def : InstAlias<"m0 = $Rs", (A2_tfrrcr C6, IntRegs:$Rs)>;
3788 def : InstAlias<"m1 = $Rs", (A2_tfrrcr C7, IntRegs:$Rs)>;
3789
3790 let hasSideEffects = 0 in
3791 class TFR_RD_CR_base<RegisterClass RC, RegisterClass CTRC, bit isSingle>
3792   : CRInst <(outs RC:$dst), (ins CTRC:$src),
3793   "$dst = $src", [], "", CR_tc_3x_SLOT3> {
3794     bits<5> dst;
3795     bits<5> src;
3796
3797     let IClass = 0b0110;
3798
3799     let Inst{27-26} = 0b10;
3800     let Inst{25} = isSingle;
3801     let Inst{24-21} = 0b0000;
3802     let Inst{20-16} = src;
3803     let Inst{4-0} = dst;
3804   }
3805
3806 let hasNewValue = 1, opNewValue = 0, isCodeGenOnly = 0 in
3807 def A2_tfrcrr : TFR_RD_CR_base<IntRegs, CtrRegs, 1>;
3808 def : InstAlias<"$Rd = m0", (A2_tfrcrr IntRegs:$Rd, C6)>;
3809 def : InstAlias<"$Rd = m1", (A2_tfrcrr IntRegs:$Rd, C7)>;
3810
3811 // Y4_trace: Send value to etm trace.
3812 let isSoloAX = 1, hasSideEffects = 0, isCodeGenOnly = 0 in
3813 def Y4_trace: CRInst <(outs), (ins IntRegs:$Rs),
3814   "trace($Rs)"> {
3815     bits<5> Rs;
3816
3817     let IClass = 0b0110;
3818     let Inst{27-21} = 0b0010010;
3819     let Inst{20-16} = Rs;
3820   }
3821
3822 let AddedComplexity = 100, isPredicated = 1 in
3823 def TFR_condset_ri : ALU32_rr<(outs IntRegs:$dst),
3824             (ins PredRegs:$src1, IntRegs:$src2, s12Imm:$src3),
3825             "Error; should not emit",
3826             [(set (i32 IntRegs:$dst),
3827              (i32 (select (i1 PredRegs:$src1), (i32 IntRegs:$src2),
3828                           s12ImmPred:$src3)))]>;
3829
3830 let AddedComplexity = 100, isPredicated = 1 in
3831 def TFR_condset_ir : ALU32_rr<(outs IntRegs:$dst),
3832             (ins PredRegs:$src1, s12Imm:$src2, IntRegs:$src3),
3833             "Error; should not emit",
3834             [(set (i32 IntRegs:$dst),
3835              (i32 (select (i1 PredRegs:$src1), s12ImmPred:$src2,
3836                           (i32 IntRegs:$src3))))]>;
3837
3838 let AddedComplexity = 100, isPredicated = 1 in
3839 def TFR_condset_ii : ALU32_rr<(outs IntRegs:$dst),
3840                               (ins PredRegs:$src1, s12Imm:$src2, s12Imm:$src3),
3841                      "Error; should not emit",
3842                      [(set (i32 IntRegs:$dst),
3843                            (i32 (select (i1 PredRegs:$src1), s12ImmPred:$src2,
3844                                         s12ImmPred:$src3)))]>;
3845
3846 // Generate frameindex addresses.
3847 let isReMaterializable = 1 in
3848 def TFR_FI : ALU32_ri<(outs IntRegs:$dst), (ins FrameIndex:$src1),
3849              "$dst = add($src1)",
3850              [(set (i32 IntRegs:$dst), ADDRri:$src1)]>;
3851
3852 // Support for generating global address.
3853 // Taken from X86InstrInfo.td.
3854 def SDTHexagonCONST32 : SDTypeProfile<1, 1, [
3855                                             SDTCisVT<0, i32>,
3856                                             SDTCisVT<1, i32>,
3857                                             SDTCisPtrTy<0>]>;
3858 def HexagonCONST32 : SDNode<"HexagonISD::CONST32",     SDTHexagonCONST32>;
3859 def HexagonCONST32_GP : SDNode<"HexagonISD::CONST32_GP",     SDTHexagonCONST32>;
3860
3861 // HI/LO Instructions
3862 let isReMaterializable = 1, isMoveImm = 1, hasSideEffects = 0 in
3863 def LO : ALU32_ri<(outs IntRegs:$dst), (ins globaladdress:$global),
3864                   "$dst.l = #LO($global)",
3865                   []>;
3866
3867 let isReMaterializable = 1, isMoveImm = 1, hasSideEffects = 0 in
3868 def HI : ALU32_ri<(outs IntRegs:$dst), (ins globaladdress:$global),
3869                   "$dst.h = #HI($global)",
3870                   []>;
3871
3872 let isReMaterializable = 1, isMoveImm = 1, hasSideEffects = 0 in
3873 def LOi : ALU32_ri<(outs IntRegs:$dst), (ins i32imm:$imm_value),
3874                   "$dst.l = #LO($imm_value)",
3875                   []>;
3876
3877
3878 let isReMaterializable = 1, isMoveImm = 1, hasSideEffects = 0 in
3879 def HIi : ALU32_ri<(outs IntRegs:$dst), (ins i32imm:$imm_value),
3880                   "$dst.h = #HI($imm_value)",
3881                   []>;
3882
3883 let isReMaterializable = 1, isMoveImm = 1, hasSideEffects = 0 in
3884 def LO_jt : ALU32_ri<(outs IntRegs:$dst), (ins jumptablebase:$jt),
3885                   "$dst.l = #LO($jt)",
3886                   []>;
3887
3888 let isReMaterializable = 1, isMoveImm = 1, hasSideEffects = 0 in
3889 def HI_jt : ALU32_ri<(outs IntRegs:$dst), (ins jumptablebase:$jt),
3890                   "$dst.h = #HI($jt)",
3891                   []>;
3892
3893
3894 let isReMaterializable = 1, isMoveImm = 1, hasSideEffects = 0 in
3895 def LO_label : ALU32_ri<(outs IntRegs:$dst), (ins bblabel:$label),
3896                   "$dst.l = #LO($label)",
3897                   []>;
3898
3899 let isReMaterializable = 1, isMoveImm = 1 , hasSideEffects = 0 in
3900 def HI_label : ALU32_ri<(outs IntRegs:$dst), (ins bblabel:$label),
3901                   "$dst.h = #HI($label)",
3902                   []>;
3903
3904 // This pattern is incorrect. When we add small data, we should change
3905 // this pattern to use memw(#foo).
3906 // This is for sdata.
3907 let isMoveImm = 1 in
3908 def CONST32 : LDInst<(outs IntRegs:$dst), (ins globaladdress:$global),
3909               "$dst = CONST32(#$global)",
3910               [(set (i32 IntRegs:$dst),
3911                     (load (HexagonCONST32 tglobaltlsaddr:$global)))]>;
3912
3913 // This is for non-sdata.
3914 let isReMaterializable = 1, isMoveImm = 1 in
3915 def CONST32_set : LDInst2<(outs IntRegs:$dst), (ins globaladdress:$global),
3916                   "$dst = CONST32(#$global)",
3917                   [(set (i32 IntRegs:$dst),
3918                         (HexagonCONST32 tglobaladdr:$global))]>;
3919
3920 let isReMaterializable = 1, isMoveImm = 1 in
3921 def CONST32_set_jt : LDInst2<(outs IntRegs:$dst), (ins jumptablebase:$jt),
3922                      "$dst = CONST32(#$jt)",
3923                      [(set (i32 IntRegs:$dst),
3924                            (HexagonCONST32 tjumptable:$jt))]>;
3925
3926 let isReMaterializable = 1, isMoveImm = 1 in
3927 def CONST32GP_set : LDInst2<(outs IntRegs:$dst), (ins globaladdress:$global),
3928                     "$dst = CONST32(#$global)",
3929                     [(set (i32 IntRegs:$dst),
3930                           (HexagonCONST32_GP tglobaladdr:$global))]>;
3931
3932 let isReMaterializable = 1, isMoveImm = 1 in
3933 def CONST32_Int_Real : LDInst2<(outs IntRegs:$dst), (ins i32imm:$global),
3934                        "$dst = CONST32(#$global)",
3935                        [(set (i32 IntRegs:$dst), imm:$global) ]>;
3936
3937 // Map BlockAddress lowering to CONST32_Int_Real
3938 def : Pat<(HexagonCONST32_GP tblockaddress:$addr),
3939           (CONST32_Int_Real tblockaddress:$addr)>;
3940
3941 let isReMaterializable = 1, isMoveImm = 1 in
3942 def CONST32_Label : LDInst2<(outs IntRegs:$dst), (ins bblabel:$label),
3943                     "$dst = CONST32($label)",
3944                     [(set (i32 IntRegs:$dst), (HexagonCONST32 bbl:$label))]>;
3945
3946 let isReMaterializable = 1, isMoveImm = 1 in
3947 def CONST64_Int_Real : LDInst2<(outs DoubleRegs:$dst), (ins i64imm:$global),
3948                        "$dst = CONST64(#$global)",
3949                        [(set (i64 DoubleRegs:$dst), imm:$global) ]>;
3950
3951 def TFR_PdFalse : SInst<(outs PredRegs:$dst), (ins),
3952                   "$dst = xor($dst, $dst)",
3953                   [(set (i1 PredRegs:$dst), 0)]>;
3954
3955 def MPY_trsext : MInst<(outs IntRegs:$dst), (ins IntRegs:$src1, IntRegs:$src2),
3956        "$dst = mpy($src1, $src2)",
3957        [(set (i32 IntRegs:$dst),
3958              (trunc (i64 (srl (i64 (mul (i64 (sext (i32 IntRegs:$src1))),
3959                                         (i64 (sext (i32 IntRegs:$src2))))),
3960                               (i32 32)))))]>;
3961
3962 // Pseudo instructions.
3963 def SDT_SPCallSeqStart : SDCallSeqStart<[ SDTCisVT<0, i32> ]>;
3964
3965 def SDT_SPCallSeqEnd : SDCallSeqEnd<[ SDTCisVT<0, i32>,
3966                                         SDTCisVT<1, i32> ]>;
3967
3968 def callseq_end : SDNode<"ISD::CALLSEQ_END",   SDT_SPCallSeqEnd,
3969                   [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
3970
3971 def callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_SPCallSeqStart,
3972                     [SDNPHasChain, SDNPOutGlue]>;
3973
3974 def SDT_SPCall : SDTypeProfile<0, 1, [SDTCisVT<0, i32>]>;
3975
3976 def call : SDNode<"HexagonISD::CALL", SDT_SPCall,
3977            [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue, SDNPVariadic]>;
3978
3979 // For tailcalls a HexagonTCRet SDNode has 3 SDNode Properties - a chain,
3980 // Optional Flag and Variable Arguments.
3981 // Its 1 Operand has pointer type.
3982 def HexagonTCRet    : SDNode<"HexagonISD::TC_RETURN", SDT_SPCall,
3983                      [SDNPHasChain,  SDNPOptInGlue, SDNPVariadic]>;
3984
3985 let Defs = [R29, R30], Uses = [R31, R30, R29] in {
3986  def ADJCALLSTACKDOWN : Pseudo<(outs), (ins i32imm:$amt),
3987                         "Should never be emitted",
3988                         [(callseq_start timm:$amt)]>;
3989 }
3990
3991 let Defs = [R29, R30, R31], Uses = [R29] in {
3992  def ADJCALLSTACKUP : Pseudo<(outs), (ins i32imm:$amt1, i32imm:$amt2),
3993                       "Should never be emitted",
3994                       [(callseq_end timm:$amt1, timm:$amt2)]>;
3995 }
3996 // Call subroutine.
3997 let isCall = 1, hasSideEffects = 0,
3998   Defs = [D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10,
3999           R22, R23, R28, R31, P0, P1, P2, P3, LC0, LC1, SA0, SA1] in {
4000   def CALL : JInst<(outs), (ins calltarget:$dst),
4001              "call $dst", []>;
4002 }
4003
4004 // Call subroutine indirectly.
4005 let Defs = VolatileV3.Regs, isCodeGenOnly = 0 in
4006 def J2_callr : JUMPR_MISC_CALLR<0, 1>;
4007
4008 // Indirect tail-call.
4009 let isCodeGenOnly = 1, isCall = 1, isReturn = 1  in
4010 def TCRETURNR : T_JMPr;
4011
4012 // Direct tail-calls.
4013 let isCall = 1, isReturn = 1, isBarrier = 1, isPredicable = 0,
4014 isTerminator = 1, isCodeGenOnly = 1 in {
4015   def TCRETURNtg   : JInst<(outs), (ins calltarget:$dst), "jump $dst",
4016       [], "", J_tc_2early_SLOT23>;
4017   def TCRETURNtext : JInst<(outs), (ins calltarget:$dst), "jump $dst",
4018       [], "", J_tc_2early_SLOT23>;
4019 }
4020
4021 // Map call instruction.
4022 def : Pat<(call (i32 IntRegs:$dst)),
4023       (J2_callr (i32 IntRegs:$dst))>, Requires<[HasV2TOnly]>;
4024 def : Pat<(call tglobaladdr:$dst),
4025       (CALL tglobaladdr:$dst)>, Requires<[HasV2TOnly]>;
4026 def : Pat<(call texternalsym:$dst),
4027       (CALL texternalsym:$dst)>, Requires<[HasV2TOnly]>;
4028 //Tail calls.
4029 def : Pat<(HexagonTCRet tglobaladdr:$dst),
4030       (TCRETURNtg tglobaladdr:$dst)>;
4031 def : Pat<(HexagonTCRet texternalsym:$dst),
4032       (TCRETURNtext texternalsym:$dst)>;
4033 def : Pat<(HexagonTCRet (i32 IntRegs:$dst)),
4034       (TCRETURNR (i32 IntRegs:$dst))>;
4035
4036 // Atomic load and store support
4037 // 8 bit atomic load
4038 def : Pat<(atomic_load_8 ADDRriS11_0:$src1),
4039           (i32 (L2_loadrub_io AddrFI:$src1, 0))>;
4040
4041 def : Pat<(atomic_load_8 (add (i32 IntRegs:$src1), s11_0ImmPred:$offset)),
4042           (i32 (L2_loadrub_io (i32 IntRegs:$src1), s11_0ImmPred:$offset))>;
4043
4044 // 16 bit atomic load
4045 def : Pat<(atomic_load_16 ADDRriS11_1:$src1),
4046           (i32 (L2_loadruh_io AddrFI:$src1, 0))>;
4047
4048 def : Pat<(atomic_load_16 (add (i32 IntRegs:$src1), s11_1ImmPred:$offset)),
4049           (i32 (L2_loadruh_io (i32 IntRegs:$src1), s11_1ImmPred:$offset))>;
4050
4051 def : Pat<(atomic_load_32 ADDRriS11_2:$src1),
4052           (i32 (L2_loadri_io AddrFI:$src1, 0))>;
4053
4054 def : Pat<(atomic_load_32 (add (i32 IntRegs:$src1), s11_2ImmPred:$offset)),
4055           (i32 (L2_loadri_io (i32 IntRegs:$src1), s11_2ImmPred:$offset))>;
4056
4057 // 64 bit atomic load
4058 def : Pat<(atomic_load_64 ADDRriS11_3:$src1),
4059           (i64 (L2_loadrd_io AddrFI:$src1, 0))>;
4060
4061 def : Pat<(atomic_load_64 (add (i32 IntRegs:$src1), s11_3ImmPred:$offset)),
4062           (i64 (L2_loadrd_io (i32 IntRegs:$src1), s11_3ImmPred:$offset))>;
4063
4064
4065 def : Pat<(atomic_store_8 ADDRriS11_0:$src2, (i32 IntRegs:$src1)),
4066           (S2_storerb_io AddrFI:$src2, 0, (i32 IntRegs:$src1))>;
4067
4068 def : Pat<(atomic_store_8 (add (i32 IntRegs:$src2), s11_0ImmPred:$offset),
4069                           (i32 IntRegs:$src1)),
4070           (S2_storerb_io (i32 IntRegs:$src2), s11_0ImmPred:$offset,
4071                          (i32 IntRegs:$src1))>;
4072
4073
4074 def : Pat<(atomic_store_16 ADDRriS11_1:$src2, (i32 IntRegs:$src1)),
4075           (S2_storerh_io AddrFI:$src2, 0, (i32 IntRegs:$src1))>;
4076
4077 def : Pat<(atomic_store_16 (i32 IntRegs:$src1),
4078                           (add (i32 IntRegs:$src2), s11_1ImmPred:$offset)),
4079           (S2_storerh_io (i32 IntRegs:$src2), s11_1ImmPred:$offset,
4080                          (i32 IntRegs:$src1))>;
4081
4082 def : Pat<(atomic_store_32 ADDRriS11_2:$src2, (i32 IntRegs:$src1)),
4083           (S2_storeri_io AddrFI:$src2, 0, (i32 IntRegs:$src1))>;
4084
4085 def : Pat<(atomic_store_32 (add (i32 IntRegs:$src2), s11_2ImmPred:$offset),
4086                            (i32 IntRegs:$src1)),
4087           (S2_storeri_io (i32 IntRegs:$src2), s11_2ImmPred:$offset,
4088                          (i32 IntRegs:$src1))>;
4089
4090
4091
4092
4093 def : Pat<(atomic_store_64 ADDRriS11_3:$src2, (i64 DoubleRegs:$src1)),
4094           (S2_storerd_io AddrFI:$src2, 0, (i64 DoubleRegs:$src1))>;
4095
4096 def : Pat<(atomic_store_64 (add (i32 IntRegs:$src2), s11_3ImmPred:$offset),
4097                            (i64 DoubleRegs:$src1)),
4098           (S2_storerd_io (i32 IntRegs:$src2), s11_3ImmPred:$offset,
4099                          (i64 DoubleRegs:$src1))>;
4100
4101 // Map from r0 = and(r1, 65535) to r0 = zxth(r1)
4102 def : Pat <(and (i32 IntRegs:$src1), 65535),
4103       (A2_zxth (i32 IntRegs:$src1))>;
4104
4105 // Map from r0 = and(r1, 255) to r0 = zxtb(r1).
4106 def : Pat <(and (i32 IntRegs:$src1), 255),
4107       (A2_zxtb (i32 IntRegs:$src1))>;
4108
4109 // Map Add(p1, true) to p1 = not(p1).
4110 //     Add(p1, false) should never be produced,
4111 //     if it does, it got to be mapped to NOOP.
4112 def : Pat <(add (i1 PredRegs:$src1), -1),
4113       (C2_not (i1 PredRegs:$src1))>;
4114
4115 // Map from p0 = pnot(p0); r0 = mux(p0, #i, #j) => r0 = mux(p0, #j, #i).
4116 def : Pat <(select (not (i1 PredRegs:$src1)), s8ImmPred:$src2, s8ImmPred:$src3),
4117       (i32 (TFR_condset_ii (i1 PredRegs:$src1), s8ImmPred:$src3,
4118                            s8ImmPred:$src2))>;
4119
4120 // Map from p0 = pnot(p0); r0 = select(p0, #i, r1)
4121 // => r0 = TFR_condset_ri(p0, r1, #i)
4122 def : Pat <(select (not (i1 PredRegs:$src1)), s12ImmPred:$src2,
4123                    (i32 IntRegs:$src3)),
4124       (i32 (TFR_condset_ri (i1 PredRegs:$src1), (i32 IntRegs:$src3),
4125                            s12ImmPred:$src2))>;
4126
4127 // Map from p0 = pnot(p0); r0 = mux(p0, r1, #i)
4128 // => r0 = TFR_condset_ir(p0, #i, r1)
4129 def : Pat <(select (not (i1 PredRegs:$src1)), IntRegs:$src2, s12ImmPred:$src3),
4130       (i32 (TFR_condset_ir (i1 PredRegs:$src1), s12ImmPred:$src3,
4131                            (i32 IntRegs:$src2)))>;
4132
4133 // Map from p0 = pnot(p0); if (p0) jump => if (!p0) jump.
4134 def : Pat <(brcond (not (i1 PredRegs:$src1)), bb:$offset),
4135       (J2_jumpf (i1 PredRegs:$src1), bb:$offset)>;
4136
4137 // Map from p2 = pnot(p2); p1 = and(p0, p2) => p1 = and(p0, !p2).
4138 def : Pat <(and (i1 PredRegs:$src1), (not (i1 PredRegs:$src2))),
4139       (i1 (C2_andn (i1 PredRegs:$src1), (i1 PredRegs:$src2)))>;
4140
4141
4142 let AddedComplexity = 100 in
4143 def : Pat <(i64 (zextloadi1 (HexagonCONST32 tglobaladdr:$global))),
4144       (i64 (A2_combinew (A2_tfrsi 0),
4145                        (L2_loadrub_io (CONST32_set tglobaladdr:$global), 0)))>,
4146       Requires<[NoV4T]>;
4147
4148 // Map from i1 loads to 32 bits. This assumes that the i1* is byte aligned.
4149 let AddedComplexity = 10 in
4150 def : Pat <(i32 (zextloadi1 ADDRriS11_0:$addr)),
4151       (i32 (A2_and (i32 (L2_loadrb_io AddrFI:$addr, 0)), (A2_tfrsi 0x1)))>;
4152
4153 // Map from Rdd = sign_extend_inreg(Rss, i32) -> Rdd = A2_sxtw(Rss.lo).
4154 def : Pat <(i64 (sext_inreg (i64 DoubleRegs:$src1), i32)),
4155       (i64 (A2_sxtw (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1), subreg_loreg))))>;
4156
4157 // Map from Rdd = sign_extend_inreg(Rss, i16) -> Rdd = A2_sxtw(SXTH(Rss.lo)).
4158 def : Pat <(i64 (sext_inreg (i64 DoubleRegs:$src1), i16)),
4159       (i64 (A2_sxtw (i32 (A2_sxth (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1),
4160                                                  subreg_loreg))))))>;
4161
4162 // Map from Rdd = sign_extend_inreg(Rss, i8) -> Rdd = A2_sxtw(SXTB(Rss.lo)).
4163 def : Pat <(i64 (sext_inreg (i64 DoubleRegs:$src1), i8)),
4164       (i64 (A2_sxtw (i32 (A2_sxtb (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1),
4165                                                  subreg_loreg))))))>;
4166
4167 // We want to prevent emitting pnot's as much as possible.
4168 // Map brcond with an unsupported setcc to a J2_jumpf.
4169 def : Pat <(brcond (i1 (setne (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4170                         bb:$offset),
4171       (J2_jumpf (C2_cmpeq (i32 IntRegs:$src1), (i32 IntRegs:$src2)),
4172                 bb:$offset)>;
4173
4174 def : Pat <(brcond (i1 (setne (i32 IntRegs:$src1), s10ImmPred:$src2)),
4175                         bb:$offset),
4176       (J2_jumpf (C2_cmpeqi (i32 IntRegs:$src1), s10ImmPred:$src2), bb:$offset)>;
4177
4178 def : Pat <(brcond (i1 (setne (i1 PredRegs:$src1), (i1 -1))), bb:$offset),
4179       (J2_jumpf (i1 PredRegs:$src1), bb:$offset)>;
4180
4181 def : Pat <(brcond (i1 (setne (i1 PredRegs:$src1), (i1 0))), bb:$offset),
4182       (J2_jumpt (i1 PredRegs:$src1), bb:$offset)>;
4183
4184 // cmp.lt(Rs, Imm) -> !cmp.ge(Rs, Imm) -> !cmp.gt(Rs, Imm-1)
4185 def : Pat <(brcond (i1 (setlt (i32 IntRegs:$src1), s8ImmPred:$src2)),
4186                         bb:$offset),
4187       (J2_jumpf (C2_cmpgti (i32 IntRegs:$src1),
4188                 (DEC_CONST_SIGNED s8ImmPred:$src2)), bb:$offset)>;
4189
4190 // cmp.lt(r0, r1) -> cmp.gt(r1, r0)
4191 def : Pat <(brcond (i1 (setlt (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4192                         bb:$offset),
4193       (J2_jumpt (C2_cmpgt (i32 IntRegs:$src2), (i32 IntRegs:$src1)), bb:$offset)>;
4194
4195 def : Pat <(brcond (i1 (setuge (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4196                    bb:$offset),
4197       (J2_jumpf (C2_cmpgtup (i64 DoubleRegs:$src2), (i64 DoubleRegs:$src1)),
4198                    bb:$offset)>;
4199
4200 def : Pat <(brcond (i1 (setule (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4201                         bb:$offset),
4202       (J2_jumpf (C2_cmpgtu (i32 IntRegs:$src1), (i32 IntRegs:$src2)),
4203                 bb:$offset)>;
4204
4205 def : Pat <(brcond (i1 (setule (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4206                    bb:$offset),
4207       (J2_jumpf (C2_cmpgtup (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2)),
4208                 bb:$offset)>;
4209
4210 // Map from a 64-bit select to an emulated 64-bit mux.
4211 // Hexagon does not support 64-bit MUXes; so emulate with combines.
4212 def : Pat <(select (i1 PredRegs:$src1), (i64 DoubleRegs:$src2),
4213                    (i64 DoubleRegs:$src3)),
4214       (i64 (A2_combinew (i32 (C2_mux (i1 PredRegs:$src1),
4215                                     (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2),
4216                                                          subreg_hireg)),
4217                                     (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src3),
4218                                                          subreg_hireg)))),
4219                        (i32 (C2_mux (i1 PredRegs:$src1),
4220                                     (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2),
4221                                                          subreg_loreg)),
4222                                     (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src3),
4223                                                          subreg_loreg))))))>;
4224
4225 // Map from a 1-bit select to logical ops.
4226 // From LegalizeDAG.cpp: (B1 ? B2 : B3) <=> (B1 & B2)|(!B1&B3).
4227 def : Pat <(select (i1 PredRegs:$src1), (i1 PredRegs:$src2),
4228                    (i1 PredRegs:$src3)),
4229       (C2_or (C2_and (i1 PredRegs:$src1), (i1 PredRegs:$src2)),
4230              (C2_and (C2_not (i1 PredRegs:$src1)), (i1 PredRegs:$src3)))>;
4231
4232 // Map Pd = load(addr) -> Rs = load(addr); Pd = Rs.
4233 def : Pat<(i1 (load ADDRriS11_2:$addr)),
4234       (i1 (C2_tfrrp (i32 (L2_loadrb_io AddrFI:$addr, 0))))>;
4235
4236 // Map for truncating from 64 immediates to 32 bit immediates.
4237 def : Pat<(i32 (trunc (i64 DoubleRegs:$src))),
4238       (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src), subreg_loreg))>;
4239
4240 // Map for truncating from i64 immediates to i1 bit immediates.
4241 def :  Pat<(i1 (trunc (i64 DoubleRegs:$src))),
4242        (i1 (C2_tfrrp (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src),
4243                                           subreg_loreg))))>;
4244
4245 // Map memb(Rs) = Rdd -> memb(Rs) = Rt.
4246 def : Pat<(truncstorei8 (i64 DoubleRegs:$src), ADDRriS11_0:$addr),
4247       (S2_storerb_io AddrFI:$addr, 0, (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src),
4248                                                      subreg_loreg)))>;
4249
4250 // Map memh(Rs) = Rdd -> memh(Rs) = Rt.
4251 def : Pat<(truncstorei16 (i64 DoubleRegs:$src), ADDRriS11_0:$addr),
4252       (S2_storerh_io AddrFI:$addr, 0, (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src),
4253                                                      subreg_loreg)))>;
4254 // Map memw(Rs) = Rdd -> memw(Rs) = Rt
4255 def : Pat<(truncstorei32 (i64  DoubleRegs:$src), ADDRriS11_0:$addr),
4256       (S2_storeri_io AddrFI:$addr, 0, (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src),
4257                                                      subreg_loreg)))>;
4258
4259 // Map memw(Rs) = Rdd -> memw(Rs) = Rt.
4260 def : Pat<(truncstorei32 (i64 DoubleRegs:$src), ADDRriS11_0:$addr),
4261       (S2_storeri_io AddrFI:$addr, 0, (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src),
4262                                                      subreg_loreg)))>;
4263
4264 // Map from i1 = constant<-1>; memw(addr) = i1 -> r0 = 1; memw(addr) = r0.
4265 def : Pat<(store (i1 -1), ADDRriS11_2:$addr),
4266       (S2_storerb_io AddrFI:$addr, 0, (A2_tfrsi 1))>;
4267
4268
4269 // Map from i1 = constant<-1>; store i1 -> r0 = 1; store r0.
4270 def : Pat<(store (i1 -1), ADDRriS11_2:$addr),
4271       (S2_storerb_io AddrFI:$addr, 0, (A2_tfrsi 1))>;
4272
4273 // Map from memb(Rs) = Pd -> Rt = mux(Pd, #0, #1); store Rt.
4274 def : Pat<(store (i1 PredRegs:$src1), ADDRriS11_2:$addr),
4275       (S2_storerb_io AddrFI:$addr, 0, (i32 (C2_muxii (i1 PredRegs:$src1), 1, 0)) )>;
4276
4277 // Map Rdd = anyext(Rs) -> Rdd = A2_sxtw(Rs).
4278 // Hexagon_TODO: We can probably use combine but that will cost 2 instructions.
4279 // Better way to do this?
4280 def : Pat<(i64 (anyext (i32 IntRegs:$src1))),
4281       (i64 (A2_sxtw (i32 IntRegs:$src1)))>;
4282
4283 // Map cmple -> cmpgt.
4284 // rs <= rt -> !(rs > rt).
4285 def : Pat<(i1 (setle (i32 IntRegs:$src1), s10ExtPred:$src2)),
4286       (i1 (C2_not (C2_cmpgti (i32 IntRegs:$src1), s10ExtPred:$src2)))>;
4287
4288 // rs <= rt -> !(rs > rt).
4289 def : Pat<(i1 (setle (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4290       (i1 (C2_not (C2_cmpgt (i32 IntRegs:$src1), (i32 IntRegs:$src2))))>;
4291
4292 // Rss <= Rtt -> !(Rss > Rtt).
4293 def : Pat<(i1 (setle (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4294       (i1 (C2_not (C2_cmpgtp (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))))>;
4295
4296 // Map cmpne -> cmpeq.
4297 // Hexagon_TODO: We should improve on this.
4298 // rs != rt -> !(rs == rt).
4299 def : Pat <(i1 (setne (i32 IntRegs:$src1), s10ExtPred:$src2)),
4300       (i1 (C2_not(i1 (C2_cmpeqi (i32 IntRegs:$src1), s10ExtPred:$src2))))>;
4301
4302 // Map cmpne(Rs) -> !cmpeqe(Rs).
4303 // rs != rt -> !(rs == rt).
4304 def : Pat <(i1 (setne (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4305       (i1 (C2_not (i1 (C2_cmpeq (i32 IntRegs:$src1), (i32 IntRegs:$src2)))))>;
4306
4307 // Convert setne back to xor for hexagon since we compute w/ pred registers.
4308 def : Pat <(i1 (setne (i1 PredRegs:$src1), (i1 PredRegs:$src2))),
4309       (i1 (C2_xor (i1 PredRegs:$src1), (i1 PredRegs:$src2)))>;
4310
4311 // Map cmpne(Rss) -> !cmpew(Rss).
4312 // rs != rt -> !(rs == rt).
4313 def : Pat <(i1 (setne (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4314       (i1 (C2_not (i1 (C2_cmpeqp (i64 DoubleRegs:$src1),
4315                                      (i64 DoubleRegs:$src2)))))>;
4316
4317 // Map cmpge(Rs, Rt) -> !(cmpgt(Rs, Rt).
4318 // rs >= rt -> !(rt > rs).
4319 def : Pat <(i1 (setge (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4320       (i1 (C2_not (i1 (C2_cmpgt (i32 IntRegs:$src2), (i32 IntRegs:$src1)))))>;
4321
4322 // cmpge(Rs, Imm) -> cmpgt(Rs, Imm-1)
4323 def : Pat <(i1 (setge (i32 IntRegs:$src1), s8ExtPred:$src2)),
4324       (i1 (C2_cmpgti (i32 IntRegs:$src1), (DEC_CONST_SIGNED s8ExtPred:$src2)))>;
4325
4326 // Map cmpge(Rss, Rtt) -> !cmpgt(Rtt, Rss).
4327 // rss >= rtt -> !(rtt > rss).
4328 def : Pat <(i1 (setge (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4329       (i1 (C2_not (i1 (C2_cmpgtp (i64 DoubleRegs:$src2),
4330                                 (i64 DoubleRegs:$src1)))))>;
4331
4332 // Map cmplt(Rs, Imm) -> !cmpge(Rs, Imm).
4333 // !cmpge(Rs, Imm) -> !cmpgt(Rs, Imm-1).
4334 // rs < rt -> !(rs >= rt).
4335 def : Pat <(i1 (setlt (i32 IntRegs:$src1), s8ExtPred:$src2)),
4336       (i1 (C2_not (C2_cmpgti (i32 IntRegs:$src1), (DEC_CONST_SIGNED s8ExtPred:$src2))))>;
4337
4338 // Map cmplt(Rs, Rt) -> cmpgt(Rt, Rs).
4339 // rs < rt -> rt > rs.
4340 // We can let assembler map it, or we can do in the compiler itself.
4341 def : Pat <(i1 (setlt (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4342       (i1 (C2_cmpgt (i32 IntRegs:$src2), (i32 IntRegs:$src1)))>;
4343
4344 // Map cmplt(Rss, Rtt) -> cmpgt(Rtt, Rss).
4345 // rss < rtt -> (rtt > rss).
4346 def : Pat <(i1 (setlt (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4347       (i1 (C2_cmpgtp (i64 DoubleRegs:$src2), (i64 DoubleRegs:$src1)))>;
4348
4349 // Map from cmpltu(Rs, Rd) -> cmpgtu(Rd, Rs)
4350 // rs < rt -> rt > rs.
4351 // We can let assembler map it, or we can do in the compiler itself.
4352 def : Pat <(i1 (setult (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4353       (i1 (C2_cmpgtu (i32 IntRegs:$src2), (i32 IntRegs:$src1)))>;
4354
4355 // Map from cmpltu(Rss, Rdd) -> cmpgtu(Rdd, Rss).
4356 // rs < rt -> rt > rs.
4357 def : Pat <(i1 (setult (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4358       (i1 (C2_cmpgtup (i64 DoubleRegs:$src2), (i64 DoubleRegs:$src1)))>;
4359
4360 // Generate cmpgeu(Rs, #0) -> cmpeq(Rs, Rs)
4361 def : Pat <(i1 (setuge (i32 IntRegs:$src1), 0)),
4362       (i1 (C2_cmpeq (i32 IntRegs:$src1), (i32 IntRegs:$src1)))>;
4363
4364 // Generate cmpgeu(Rs, #u8) -> cmpgtu(Rs, #u8 -1)
4365 def : Pat <(i1 (setuge (i32 IntRegs:$src1), u8ExtPred:$src2)),
4366       (i1 (C2_cmpgtui (i32 IntRegs:$src1), (DEC_CONST_UNSIGNED u8ExtPred:$src2)))>;
4367
4368 // Generate cmpgtu(Rs, #u9)
4369 def : Pat <(i1 (setugt (i32 IntRegs:$src1), u9ExtPred:$src2)),
4370       (i1 (C2_cmpgtui (i32 IntRegs:$src1), u9ExtPred:$src2))>;
4371
4372 // Map from Rs >= Rt -> !(Rt > Rs).
4373 // rs >= rt -> !(rt > rs).
4374 def : Pat <(i1 (setuge (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4375       (i1 (C2_not (C2_cmpgtu (i32 IntRegs:$src2), (i32 IntRegs:$src1))))>;
4376
4377 // Map from Rs >= Rt -> !(Rt > Rs).
4378 // rs >= rt -> !(rt > rs).
4379 def : Pat <(i1 (setuge (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4380       (i1 (C2_not (C2_cmpgtup (i64 DoubleRegs:$src2), (i64 DoubleRegs:$src1))))>;
4381
4382 // Map from cmpleu(Rs, Rt) -> !cmpgtu(Rs, Rt).
4383 // Map from (Rs <= Rt) -> !(Rs > Rt).
4384 def : Pat <(i1 (setule (i32 IntRegs:$src1), (i32 IntRegs:$src2))),
4385       (i1 (C2_not (C2_cmpgtu (i32 IntRegs:$src1), (i32 IntRegs:$src2))))>;
4386
4387 // Map from cmpleu(Rss, Rtt) -> !cmpgtu(Rss, Rtt-1).
4388 // Map from (Rs <= Rt) -> !(Rs > Rt).
4389 def : Pat <(i1 (setule (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))),
4390       (i1 (C2_not (C2_cmpgtup (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2))))>;
4391
4392 // Sign extends.
4393 // i1 -> i32
4394 def : Pat <(i32 (sext (i1 PredRegs:$src1))),
4395       (i32 (C2_muxii (i1 PredRegs:$src1), -1, 0))>;
4396
4397 // i1 -> i64
4398 def : Pat <(i64 (sext (i1 PredRegs:$src1))),
4399       (i64 (A2_combinew (A2_tfrsi -1), (C2_muxii (i1 PredRegs:$src1), -1, 0)))>;
4400
4401 // Convert sign-extended load back to load and sign extend.
4402 // i8 -> i64
4403 def:  Pat <(i64 (sextloadi8 ADDRriS11_0:$src1)),
4404       (i64 (A2_sxtw (L2_loadrb_io AddrFI:$src1, 0)))>;
4405
4406 // Convert any-extended load back to load and sign extend.
4407 // i8 -> i64
4408 def:  Pat <(i64 (extloadi8 ADDRriS11_0:$src1)),
4409       (i64 (A2_sxtw (L2_loadrb_io AddrFI:$src1, 0)))>;
4410
4411 // Convert sign-extended load back to load and sign extend.
4412 // i16 -> i64
4413 def:  Pat <(i64 (sextloadi16 ADDRriS11_1:$src1)),
4414       (i64 (A2_sxtw (L2_loadrh_io AddrFI:$src1, 0)))>;
4415
4416 // Convert sign-extended load back to load and sign extend.
4417 // i32 -> i64
4418 def:  Pat <(i64 (sextloadi32 ADDRriS11_2:$src1)),
4419       (i64 (A2_sxtw (L2_loadri_io AddrFI:$src1, 0)))>;
4420
4421
4422 // Zero extends.
4423 // i1 -> i32
4424 def : Pat <(i32 (zext (i1 PredRegs:$src1))),
4425       (i32 (C2_muxii (i1 PredRegs:$src1), 1, 0))>;
4426
4427 // i1 -> i64
4428 def : Pat <(i64 (zext (i1 PredRegs:$src1))),
4429       (i64 (A2_combinew (A2_tfrsi 0), (C2_muxii (i1 PredRegs:$src1), 1, 0)))>,
4430       Requires<[NoV4T]>;
4431
4432 // i32 -> i64
4433 def : Pat <(i64 (zext (i32 IntRegs:$src1))),
4434       (i64 (A2_combinew (A2_tfrsi 0), (i32 IntRegs:$src1)))>,
4435       Requires<[NoV4T]>;
4436
4437 // i8 -> i64
4438 def:  Pat <(i64 (zextloadi8 ADDRriS11_0:$src1)),
4439       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadrub_io AddrFI:$src1, 0)))>,
4440       Requires<[NoV4T]>;
4441
4442 let AddedComplexity = 20 in
4443 def:  Pat <(i64 (zextloadi8 (add (i32 IntRegs:$src1),
4444                                 s11_0ExtPred:$offset))),
4445       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadrub_io IntRegs:$src1,
4446                                   s11_0ExtPred:$offset)))>,
4447       Requires<[NoV4T]>;
4448
4449 // i1 -> i64
4450 def:  Pat <(i64 (zextloadi1 ADDRriS11_0:$src1)),
4451       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadrub_io AddrFI:$src1, 0)))>,
4452       Requires<[NoV4T]>;
4453
4454 let AddedComplexity = 20 in
4455 def:  Pat <(i64 (zextloadi1 (add (i32 IntRegs:$src1),
4456                                 s11_0ExtPred:$offset))),
4457       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadrub_io IntRegs:$src1,
4458                                   s11_0ExtPred:$offset)))>,
4459       Requires<[NoV4T]>;
4460
4461 // i16 -> i64
4462 def:  Pat <(i64 (zextloadi16 ADDRriS11_1:$src1)),
4463       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadruh_io AddrFI:$src1, 0)))>,
4464       Requires<[NoV4T]>;
4465
4466 let AddedComplexity = 20 in
4467 def:  Pat <(i64 (zextloadi16 (add (i32 IntRegs:$src1),
4468                                   s11_1ExtPred:$offset))),
4469       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadruh_io IntRegs:$src1,
4470                                   s11_1ExtPred:$offset)))>,
4471       Requires<[NoV4T]>;
4472
4473 // i32 -> i64
4474 def:  Pat <(i64 (zextloadi32 ADDRriS11_2:$src1)),
4475       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadri_io AddrFI:$src1, 0)))>,
4476       Requires<[NoV4T]>;
4477
4478 let AddedComplexity = 100 in
4479 def:  Pat <(i64 (zextloadi32 (i32 (add IntRegs:$src1, s11_2ExtPred:$offset)))),
4480       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadri_io IntRegs:$src1,
4481                                   s11_2ExtPred:$offset)))>,
4482       Requires<[NoV4T]>;
4483
4484 let AddedComplexity = 10 in
4485 def:  Pat <(i32 (zextloadi1 ADDRriS11_0:$src1)),
4486       (i32 (L2_loadri_io AddrFI:$src1, 0))>;
4487
4488 // Map from Rs = Pd to Pd = mux(Pd, #1, #0)
4489 def : Pat <(i32 (zext (i1 PredRegs:$src1))),
4490       (i32 (C2_muxii (i1 PredRegs:$src1), 1, 0))>;
4491
4492 // Map from Rs = Pd to Pd = mux(Pd, #1, #0)
4493 def : Pat <(i32 (anyext (i1 PredRegs:$src1))),
4494       (i32 (C2_muxii (i1 PredRegs:$src1), 1, 0))>;
4495
4496 // Map from Rss = Pd to Rdd = A2_sxtw (mux(Pd, #1, #0))
4497 def : Pat <(i64 (anyext (i1 PredRegs:$src1))),
4498       (i64 (A2_sxtw (i32 (C2_muxii (i1 PredRegs:$src1), 1, 0))))>;
4499
4500
4501 let AddedComplexity = 100 in
4502 def: Pat<(i64 (or (i64 (shl (i64 DoubleRegs:$srcHigh),
4503                            (i32 32))),
4504                (i64 (zextloadi32 (i32 (add IntRegs:$src2,
4505                                          s11_2ExtPred:$offset2)))))),
4506         (i64 (A2_combinew (EXTRACT_SUBREG (i64 DoubleRegs:$srcHigh), subreg_loreg),
4507                         (L2_loadri_io IntRegs:$src2,
4508                                        s11_2ExtPred:$offset2)))>;
4509
4510 def: Pat<(i64 (or (i64 (shl (i64 DoubleRegs:$srcHigh),
4511                            (i32 32))),
4512                (i64 (zextloadi32 ADDRriS11_2:$srcLow)))),
4513         (i64 (A2_combinew (EXTRACT_SUBREG (i64 DoubleRegs:$srcHigh), subreg_loreg),
4514                         (L2_loadri_io AddrFI:$srcLow, 0)))>;
4515
4516 def: Pat<(i64 (or (i64 (shl (i64 DoubleRegs:$srcHigh),
4517                            (i32 32))),
4518                (i64 (zext (i32 IntRegs:$srcLow))))),
4519         (i64 (A2_combinew (EXTRACT_SUBREG (i64 DoubleRegs:$srcHigh), subreg_loreg),
4520                         IntRegs:$srcLow))>;
4521
4522 let AddedComplexity = 100 in
4523 def: Pat<(i64 (or (i64 (shl (i64 DoubleRegs:$srcHigh),
4524                            (i32 32))),
4525                (i64 (zextloadi32 (i32 (add IntRegs:$src2,
4526                                          s11_2ExtPred:$offset2)))))),
4527         (i64 (A2_combinew (EXTRACT_SUBREG (i64 DoubleRegs:$srcHigh), subreg_loreg),
4528                         (L2_loadri_io IntRegs:$src2,
4529                                        s11_2ExtPred:$offset2)))>;
4530
4531 def: Pat<(i64 (or (i64 (shl (i64 DoubleRegs:$srcHigh),
4532                            (i32 32))),
4533                (i64 (zextloadi32 ADDRriS11_2:$srcLow)))),
4534         (i64 (A2_combinew (EXTRACT_SUBREG (i64 DoubleRegs:$srcHigh), subreg_loreg),
4535                         (L2_loadri_io AddrFI:$srcLow, 0)))>;
4536
4537 def: Pat<(i64 (or (i64 (shl (i64 DoubleRegs:$srcHigh),
4538                            (i32 32))),
4539                (i64 (zext (i32 IntRegs:$srcLow))))),
4540         (i64 (A2_combinew (EXTRACT_SUBREG (i64 DoubleRegs:$srcHigh), subreg_loreg),
4541                         IntRegs:$srcLow))>;
4542
4543 // Any extended 64-bit load.
4544 // anyext i32 -> i64
4545 def:  Pat <(i64 (extloadi32 ADDRriS11_2:$src1)),
4546       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadri_io AddrFI:$src1, 0)))>,
4547       Requires<[NoV4T]>;
4548
4549 // When there is an offset we should prefer the pattern below over the pattern above.
4550 // The complexity of the above is 13 (gleaned from HexagonGenDAGIsel.inc)
4551 // So this complexity below is comfortably higher to allow for choosing the below.
4552 // If this is not done then we generate addresses such as
4553 // ********************************************
4554 //        r1 = add (r0, #4)
4555 //        r1 = memw(r1 + #0)
4556 //  instead of
4557 //        r1 = memw(r0 + #4)
4558 // ********************************************
4559 let AddedComplexity = 100 in
4560 def:  Pat <(i64 (extloadi32 (i32 (add IntRegs:$src1, s11_2ExtPred:$offset)))),
4561       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadri_io IntRegs:$src1,
4562                                   s11_2ExtPred:$offset)))>,
4563       Requires<[NoV4T]>;
4564
4565 // anyext i16 -> i64.
4566 def:  Pat <(i64 (extloadi16 ADDRriS11_2:$src1)),
4567       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadrh_io AddrFI:$src1, 0)))>,
4568       Requires<[NoV4T]>;
4569
4570 let AddedComplexity = 20 in
4571 def:  Pat <(i64 (extloadi16 (add (i32 IntRegs:$src1),
4572                                   s11_1ExtPred:$offset))),
4573       (i64 (A2_combinew (A2_tfrsi 0), (L2_loadrh_io IntRegs:$src1,
4574                                   s11_1ExtPred:$offset)))>,
4575       Requires<[NoV4T]>;
4576
4577 // Map from Rdd = zxtw(Rs) -> Rdd = combine(0, Rs).
4578 def : Pat<(i64 (zext (i32 IntRegs:$src1))),
4579       (i64 (A2_combinew (A2_tfrsi 0), (i32 IntRegs:$src1)))>,
4580       Requires<[NoV4T]>;
4581
4582 // Multiply 64-bit unsigned and use upper result.
4583 def : Pat <(mulhu (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2)),
4584       (i64
4585        (M2_dpmpyuu_acc_s0
4586         (i64
4587          (A2_combinew
4588           (A2_tfrsi 0),
4589            (i32
4590             (EXTRACT_SUBREG
4591              (i64
4592               (S2_lsr_i_p
4593                (i64
4594                 (M2_dpmpyuu_acc_s0
4595                  (i64
4596                   (M2_dpmpyuu_acc_s0
4597                    (i64
4598                     (A2_combinew (A2_tfrsi 0),
4599                      (i32
4600                       (EXTRACT_SUBREG
4601                        (i64
4602                         (S2_lsr_i_p
4603                          (i64
4604                           (M2_dpmpyuu_s0 
4605                             (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1),
4606                                                        subreg_loreg)),
4607                                   (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2),
4608                                                        subreg_loreg)))), 32)),
4609                        subreg_loreg)))),
4610                   (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1), subreg_hireg)),
4611                   (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2), subreg_loreg)))),
4612                  (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1), subreg_loreg)),
4613                  (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2), subreg_hireg)))),
4614                32)), subreg_loreg)))),
4615         (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1), subreg_hireg)),
4616         (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2), subreg_hireg))))>;
4617
4618 // Multiply 64-bit signed and use upper result.
4619 def : Pat <(mulhs (i64 DoubleRegs:$src1), (i64 DoubleRegs:$src2)),
4620       (i64
4621        (M2_dpmpyss_acc_s0
4622         (i64
4623          (A2_combinew (A2_tfrsi 0),
4624           (i32
4625            (EXTRACT_SUBREG
4626             (i64
4627              (S2_lsr_i_p
4628               (i64
4629                (M2_dpmpyss_acc_s0
4630                 (i64
4631                  (M2_dpmpyss_acc_s0
4632                   (i64
4633                    (A2_combinew (A2_tfrsi 0),
4634                     (i32
4635                      (EXTRACT_SUBREG
4636                       (i64
4637                        (S2_lsr_i_p
4638                         (i64
4639                          (M2_dpmpyuu_s0 
4640                            (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1),
4641                                                       subreg_loreg)),
4642                                  (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2),
4643                                                       subreg_loreg)))), 32)),
4644                       subreg_loreg)))),
4645                   (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1), subreg_hireg)),
4646                   (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2), subreg_loreg)))),
4647                 (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1), subreg_loreg)),
4648                 (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2), subreg_hireg)))),
4649               32)), subreg_loreg)))),
4650         (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src1), subreg_hireg)),
4651         (i32 (EXTRACT_SUBREG (i64 DoubleRegs:$src2), subreg_hireg))))>;
4652
4653 // Hexagon specific ISD nodes.
4654 //def SDTHexagonADJDYNALLOC : SDTypeProfile<1, 2, [SDTCisSameAs<0, 1>]>;
4655 def SDTHexagonADJDYNALLOC : SDTypeProfile<1, 2,
4656                                   [SDTCisVT<0, i32>, SDTCisVT<1, i32>]>;
4657 def Hexagon_ADJDYNALLOC : SDNode<"HexagonISD::ADJDYNALLOC",
4658                                   SDTHexagonADJDYNALLOC>;
4659 // Needed to tag these instructions for stack layout.
4660 let usesCustomInserter = 1 in
4661 def ADJDYNALLOC : ALU32_ri<(outs IntRegs:$dst), (ins IntRegs:$src1,
4662                                                      s16Imm:$src2),
4663                   "$dst = add($src1, #$src2)",
4664                   [(set (i32 IntRegs:$dst),
4665                         (Hexagon_ADJDYNALLOC (i32 IntRegs:$src1),
4666                                              s16ImmPred:$src2))]>;
4667
4668 def SDTHexagonARGEXTEND : SDTypeProfile<1, 1, [SDTCisVT<0, i32>]>;
4669 def Hexagon_ARGEXTEND : SDNode<"HexagonISD::ARGEXTEND", SDTHexagonARGEXTEND>;
4670 def ARGEXTEND : ALU32_rr <(outs IntRegs:$dst), (ins IntRegs:$src1),
4671                 "$dst = $src1",
4672                 [(set (i32 IntRegs:$dst),
4673                       (Hexagon_ARGEXTEND (i32 IntRegs:$src1)))]>;
4674
4675 let AddedComplexity = 100 in
4676 def : Pat<(i32 (sext_inreg (Hexagon_ARGEXTEND (i32 IntRegs:$src1)), i16)),
4677       (COPY (i32 IntRegs:$src1))>;
4678
4679 def HexagonWrapperJT: SDNode<"HexagonISD::WrapperJT", SDTIntUnaryOp>;
4680
4681 def : Pat<(HexagonWrapperJT tjumptable:$dst),
4682           (i32 (CONST32_set_jt tjumptable:$dst))>;
4683
4684 // XTYPE/SHIFT
4685 //
4686 //===----------------------------------------------------------------------===//
4687 // Template Class
4688 // Shift by immediate/register and accumulate/logical
4689 //===----------------------------------------------------------------------===//
4690
4691 // Rx[+-&|]=asr(Rs,#u5)
4692 // Rx[+-&|^]=lsr(Rs,#u5)
4693 // Rx[+-&|^]=asl(Rs,#u5)
4694
4695 let hasNewValue = 1, opNewValue = 0 in
4696 class T_shift_imm_acc_r <string opc1, string opc2, SDNode OpNode1,
4697                          SDNode OpNode2, bits<3> majOp, bits<2> minOp>
4698   : SInst_acc<(outs IntRegs:$Rx),
4699               (ins IntRegs:$src1, IntRegs:$Rs, u5Imm:$u5),
4700   "$Rx "#opc2#opc1#"($Rs, #$u5)",
4701   [(set (i32 IntRegs:$Rx),
4702          (OpNode2 (i32 IntRegs:$src1),
4703                   (OpNode1 (i32 IntRegs:$Rs), u5ImmPred:$u5)))],
4704   "$src1 = $Rx", S_2op_tc_2_SLOT23> {
4705     bits<5> Rx;
4706     bits<5> Rs;
4707     bits<5> u5;
4708
4709     let IClass = 0b1000;
4710
4711     let Inst{27-24} = 0b1110;
4712     let Inst{23-22} = majOp{2-1};
4713     let Inst{13} = 0b0;
4714     let Inst{7} = majOp{0};
4715     let Inst{6-5} = minOp;
4716     let Inst{4-0} = Rx;
4717     let Inst{20-16} = Rs;
4718     let Inst{12-8} = u5;
4719   }
4720
4721 // Rx[+-&|]=asr(Rs,Rt)
4722 // Rx[+-&|^]=lsr(Rs,Rt)
4723 // Rx[+-&|^]=asl(Rs,Rt)
4724
4725 let hasNewValue = 1, opNewValue = 0 in
4726 class T_shift_reg_acc_r <string opc1, string opc2, SDNode OpNode1,
4727                          SDNode OpNode2, bits<2> majOp, bits<2> minOp>
4728   : SInst_acc<(outs IntRegs:$Rx),
4729               (ins IntRegs:$src1, IntRegs:$Rs, IntRegs:$Rt),
4730   "$Rx "#opc2#opc1#"($Rs, $Rt)",
4731   [(set (i32 IntRegs:$Rx),
4732          (OpNode2 (i32 IntRegs:$src1),
4733                   (OpNode1 (i32 IntRegs:$Rs), (i32 IntRegs:$Rt))))],
4734   "$src1 = $Rx", S_3op_tc_2_SLOT23 > {
4735     bits<5> Rx;
4736     bits<5> Rs;
4737     bits<5> Rt;
4738
4739     let IClass = 0b1100;
4740
4741     let Inst{27-24} = 0b1100;
4742     let Inst{23-22} = majOp;
4743     let Inst{7-6} = minOp;
4744     let Inst{4-0} = Rx;
4745     let Inst{20-16} = Rs;
4746     let Inst{12-8} = Rt;
4747   }
4748
4749 // Rxx[+-&|]=asr(Rss,#u6)
4750 // Rxx[+-&|^]=lsr(Rss,#u6)
4751 // Rxx[+-&|^]=asl(Rss,#u6)
4752
4753 class T_shift_imm_acc_p <string opc1, string opc2, SDNode OpNode1,
4754                          SDNode OpNode2, bits<3> majOp, bits<2> minOp>
4755   : SInst_acc<(outs DoubleRegs:$Rxx),
4756               (ins DoubleRegs:$src1, DoubleRegs:$Rss, u6Imm:$u6),
4757   "$Rxx "#opc2#opc1#"($Rss, #$u6)",
4758   [(set (i64 DoubleRegs:$Rxx),
4759         (OpNode2 (i64 DoubleRegs:$src1),
4760                  (OpNode1 (i64 DoubleRegs:$Rss), u6ImmPred:$u6)))],
4761   "$src1 = $Rxx", S_2op_tc_2_SLOT23> {
4762     bits<5> Rxx;
4763     bits<5> Rss;
4764     bits<6> u6;
4765
4766     let IClass = 0b1000;
4767
4768     let Inst{27-24} = 0b0010;
4769     let Inst{23-22} = majOp{2-1};
4770     let Inst{7} = majOp{0};
4771     let Inst{6-5} = minOp;
4772     let Inst{4-0} = Rxx;
4773     let Inst{20-16} = Rss;
4774     let Inst{13-8} = u6;
4775   }
4776
4777
4778 // Rxx[+-&|]=asr(Rss,Rt)
4779 // Rxx[+-&|^]=lsr(Rss,Rt)
4780 // Rxx[+-&|^]=asl(Rss,Rt)
4781 // Rxx[+-&|^]=lsl(Rss,Rt)
4782
4783 class T_shift_reg_acc_p <string opc1, string opc2, SDNode OpNode1,
4784                          SDNode OpNode2, bits<3> majOp, bits<2> minOp>
4785   : SInst_acc<(outs DoubleRegs:$Rxx),
4786               (ins DoubleRegs:$src1, DoubleRegs:$Rss, IntRegs:$Rt),
4787   "$Rxx "#opc2#opc1#"($Rss, $Rt)",
4788   [(set (i64 DoubleRegs:$Rxx),
4789         (OpNode2 (i64 DoubleRegs:$src1),
4790                  (OpNode1 (i64 DoubleRegs:$Rss), (i32 IntRegs:$Rt))))],
4791   "$src1 = $Rxx", S_3op_tc_2_SLOT23> {
4792     bits<5> Rxx;
4793     bits<5> Rss;
4794     bits<5> Rt;
4795
4796     let IClass = 0b1100;
4797
4798     let Inst{27-24} = 0b1011;
4799     let Inst{23-21} = majOp;
4800     let Inst{20-16} = Rss;
4801     let Inst{12-8} = Rt;
4802     let Inst{7-6} = minOp;
4803     let Inst{4-0} = Rxx;
4804   }
4805
4806 //===----------------------------------------------------------------------===//
4807 // Multi-class for the shift instructions with logical/arithmetic operators.
4808 //===----------------------------------------------------------------------===//
4809
4810 multiclass xtype_imm_base<string OpcStr1, string OpcStr2, SDNode OpNode1,
4811                          SDNode OpNode2, bits<3> majOp, bits<2> minOp > {
4812   def _i_r#NAME : T_shift_imm_acc_r< OpcStr1, OpcStr2, OpNode1,
4813                                      OpNode2, majOp, minOp >;
4814   def _i_p#NAME : T_shift_imm_acc_p< OpcStr1, OpcStr2, OpNode1,
4815                                      OpNode2, majOp, minOp >;
4816 }
4817
4818 multiclass xtype_imm_acc<string opc1, SDNode OpNode, bits<2>minOp> {
4819   let AddedComplexity = 100 in
4820   defm _acc  : xtype_imm_base< opc1, "+= ", OpNode, add, 0b001, minOp>;
4821
4822   defm _nac  : xtype_imm_base< opc1, "-= ", OpNode, sub, 0b000, minOp>;
4823   defm _and  : xtype_imm_base< opc1, "&= ", OpNode, and, 0b010, minOp>;
4824   defm _or   : xtype_imm_base< opc1, "|= ", OpNode,  or, 0b011, minOp>;
4825 }
4826
4827 multiclass xtype_xor_imm_acc<string opc1, SDNode OpNode, bits<2>minOp> {
4828 let AddedComplexity = 100 in
4829   defm _xacc  : xtype_imm_base< opc1, "^= ", OpNode, xor, 0b100, minOp>;
4830 }
4831
4832 let isCodeGenOnly = 0 in {
4833 defm S2_asr : xtype_imm_acc<"asr", sra, 0b00>;
4834
4835 defm S2_lsr : xtype_imm_acc<"lsr", srl, 0b01>,
4836               xtype_xor_imm_acc<"lsr", srl, 0b01>;
4837
4838 defm S2_asl : xtype_imm_acc<"asl", shl, 0b10>,
4839               xtype_xor_imm_acc<"asl", shl, 0b10>;
4840 }
4841
4842 multiclass xtype_reg_acc_r<string opc1, SDNode OpNode, bits<2>minOp> {
4843   let AddedComplexity = 100 in
4844   def _acc : T_shift_reg_acc_r <opc1, "+= ", OpNode, add, 0b11, minOp>;
4845
4846   def _nac : T_shift_reg_acc_r <opc1, "-= ", OpNode, sub, 0b10, minOp>;
4847   def _and : T_shift_reg_acc_r <opc1, "&= ", OpNode, and, 0b01, minOp>;
4848   def _or  : T_shift_reg_acc_r <opc1, "|= ", OpNode,  or, 0b00, minOp>;
4849 }
4850
4851 multiclass xtype_reg_acc_p<string opc1, SDNode OpNode, bits<2>minOp> {
4852   let AddedComplexity = 100 in
4853   def _acc : T_shift_reg_acc_p <opc1, "+= ", OpNode, add, 0b110, minOp>;
4854
4855   def _nac : T_shift_reg_acc_p <opc1, "-= ", OpNode, sub, 0b100, minOp>;
4856   def _and : T_shift_reg_acc_p <opc1, "&= ", OpNode, and, 0b010, minOp>;
4857   def _or  : T_shift_reg_acc_p <opc1, "|= ", OpNode,  or, 0b000, minOp>;
4858   def _xor : T_shift_reg_acc_p <opc1, "^= ", OpNode, xor, 0b011, minOp>;
4859 }
4860
4861 multiclass xtype_reg_acc<string OpcStr, SDNode OpNode, bits<2> minOp > {
4862   defm _r_r : xtype_reg_acc_r <OpcStr, OpNode, minOp>;
4863   defm _r_p : xtype_reg_acc_p <OpcStr, OpNode, minOp>;
4864 }
4865
4866 let isCodeGenOnly = 0 in {
4867 defm S2_asl : xtype_reg_acc<"asl", shl, 0b10>;
4868 defm S2_asr : xtype_reg_acc<"asr", sra, 0b00>;
4869 defm S2_lsr : xtype_reg_acc<"lsr", srl, 0b01>;
4870 defm S2_lsl : xtype_reg_acc<"lsl", shl, 0b11>;
4871 }
4872
4873 //===----------------------------------------------------------------------===//
4874 let hasSideEffects = 0 in
4875 class T_S3op_1 <string mnemonic, RegisterClass RC, bits<2> MajOp, bits<3> MinOp,
4876                 bit SwapOps, bit isSat = 0, bit isRnd = 0, bit hasShift = 0>
4877   : SInst <(outs RC:$dst),
4878            (ins DoubleRegs:$src1, DoubleRegs:$src2),
4879   "$dst = "#mnemonic#"($src1, $src2)"#!if(isRnd, ":rnd", "")
4880                                      #!if(hasShift,":>>1","")
4881                                      #!if(isSat, ":sat", ""),
4882   [], "", S_3op_tc_2_SLOT23 > {
4883     bits<5> dst;
4884     bits<5> src1;
4885     bits<5> src2;
4886
4887     let IClass = 0b1100;
4888
4889     let Inst{27-24} = 0b0001;
4890     let Inst{23-22} = MajOp;
4891     let Inst{20-16} = !if (SwapOps, src2, src1);
4892     let Inst{12-8}  = !if (SwapOps, src1, src2);
4893     let Inst{7-5}   = MinOp;
4894     let Inst{4-0}   = dst;
4895   }
4896
4897 class T_S3op_64 <string mnemonic, bits<2> MajOp, bits<3> MinOp, bit SwapOps,
4898                  bit isSat = 0, bit isRnd = 0, bit hasShift = 0 >
4899   : T_S3op_1 <mnemonic, DoubleRegs, MajOp, MinOp, SwapOps,
4900               isSat, isRnd, hasShift>;
4901
4902 let isCodeGenOnly = 0 in
4903 def S2_lfsp : T_S3op_64 < "lfs", 0b10, 0b110, 0>;
4904
4905 //===----------------------------------------------------------------------===//
4906 // Template class used by vector shift, vector rotate, vector neg,
4907 // 32-bit shift, 64-bit shifts, etc.
4908 //===----------------------------------------------------------------------===//
4909
4910 let hasSideEffects = 0 in
4911 class T_S3op_3 <string mnemonic, RegisterClass RC, bits<2> MajOp,
4912                  bits<2> MinOp, bit isSat = 0, list<dag> pattern = [] >
4913   : SInst <(outs RC:$dst),
4914            (ins RC:$src1, IntRegs:$src2),
4915   "$dst = "#mnemonic#"($src1, $src2)"#!if(isSat, ":sat", ""),
4916   pattern, "", S_3op_tc_1_SLOT23> {
4917     bits<5> dst;
4918     bits<5> src1;
4919     bits<5> src2;
4920
4921     let IClass = 0b1100;
4922
4923     let Inst{27-24} = !if(!eq(!cast<string>(RC), "IntRegs"), 0b0110, 0b0011);
4924     let Inst{23-22} = MajOp;
4925     let Inst{20-16} = src1;
4926     let Inst{12-8} = src2;
4927     let Inst{7-6} = MinOp;
4928     let Inst{4-0} = dst;
4929   }
4930
4931 let hasNewValue = 1 in
4932 class T_S3op_shift32 <string mnemonic, SDNode OpNode, bits<2> MinOp>
4933   : T_S3op_3 <mnemonic, IntRegs, 0b01, MinOp, 0,
4934     [(set (i32 IntRegs:$dst), (OpNode (i32 IntRegs:$src1),
4935                                       (i32 IntRegs:$src2)))]>;
4936
4937 let hasNewValue = 1, Itinerary = S_3op_tc_2_SLOT23 in
4938 class T_S3op_shift32_Sat <string mnemonic, bits<2> MinOp>
4939   : T_S3op_3 <mnemonic, IntRegs, 0b00, MinOp, 1, []>;
4940
4941
4942 class T_S3op_shift64 <string mnemonic, SDNode OpNode, bits<2> MinOp>
4943   : T_S3op_3 <mnemonic, DoubleRegs, 0b10, MinOp, 0,
4944     [(set (i64 DoubleRegs:$dst), (OpNode (i64 DoubleRegs:$src1),
4945                                          (i32 IntRegs:$src2)))]>;
4946
4947
4948 class T_S3op_shiftVect <string mnemonic, bits<2> MajOp, bits<2> MinOp>
4949   : T_S3op_3 <mnemonic, DoubleRegs, MajOp, MinOp, 0, []>;
4950
4951
4952 // Shift by register
4953 // Rdd=[asr|lsr|asl|lsl](Rss,Rt)
4954
4955 let isCodeGenOnly = 0 in {
4956 def S2_asr_r_p : T_S3op_shift64 < "asr", sra, 0b00>;
4957 def S2_lsr_r_p : T_S3op_shift64 < "lsr", srl, 0b01>;
4958 def S2_asl_r_p : T_S3op_shift64 < "asl", shl, 0b10>;
4959 def S2_lsl_r_p : T_S3op_shift64 < "lsl", shl, 0b11>;
4960 }
4961
4962 // Rd=[asr|lsr|asl|lsl](Rs,Rt)
4963
4964 let isCodeGenOnly = 0 in {
4965 def S2_asr_r_r : T_S3op_shift32<"asr", sra, 0b00>;
4966 def S2_lsr_r_r : T_S3op_shift32<"lsr", srl, 0b01>;
4967 def S2_asl_r_r : T_S3op_shift32<"asl", shl, 0b10>;
4968 def S2_lsl_r_r : T_S3op_shift32<"lsl", shl, 0b11>;
4969 }
4970
4971 // Shift by register with saturation
4972 // Rd=asr(Rs,Rt):sat
4973 // Rd=asl(Rs,Rt):sat
4974
4975 let Defs = [USR_OVF], isCodeGenOnly = 0 in {
4976   def S2_asr_r_r_sat : T_S3op_shift32_Sat<"asr", 0b00>;
4977   def S2_asl_r_r_sat : T_S3op_shift32_Sat<"asl", 0b10>;
4978 }
4979
4980 //===----------------------------------------------------------------------===//
4981 // Template class for 'insert bitfield' instructions
4982 //===----------------------------------------------------------------------===//
4983 let hasSideEffects = 0 in
4984 class T_S3op_insert <string mnemonic, RegisterClass RC>
4985   : SInst <(outs RC:$dst),
4986            (ins RC:$src1, RC:$src2, DoubleRegs:$src3),
4987   "$dst = "#mnemonic#"($src2, $src3)" ,
4988   [], "$src1 = $dst", S_3op_tc_1_SLOT23 > {
4989     bits<5> dst;
4990     bits<5> src2;
4991     bits<5> src3;
4992
4993     let IClass = 0b1100;
4994
4995     let Inst{27-26} = 0b10;
4996     let Inst{25-24} = !if(!eq(!cast<string>(RC), "IntRegs"), 0b00, 0b10);
4997     let Inst{23}    = 0b0;
4998     let Inst{20-16} = src2;
4999     let Inst{12-8}  = src3;
5000     let Inst{4-0}   = dst;
5001   }
5002
5003 let hasSideEffects = 0 in
5004 class T_S2op_insert <bits<4> RegTyBits, RegisterClass RC, Operand ImmOp>
5005   : SInst <(outs RC:$dst), (ins RC:$dst2, RC:$src1, ImmOp:$src2, ImmOp:$src3),
5006   "$dst = insert($src1, #$src2, #$src3)",
5007   [], "$dst2 = $dst", S_2op_tc_2_SLOT23> {
5008     bits<5> dst;
5009     bits<5> src1;
5010     bits<6> src2;
5011     bits<6> src3;
5012     bit bit23;
5013     bit bit13;
5014     string ImmOpStr = !cast<string>(ImmOp);
5015
5016     let bit23 = !if (!eq(ImmOpStr, "u6Imm"), src3{5}, 0);
5017     let bit13 = !if (!eq(ImmOpStr, "u6Imm"), src2{5}, 0);
5018
5019     let IClass = 0b1000;
5020
5021     let Inst{27-24} = RegTyBits;
5022     let Inst{23}    = bit23;
5023     let Inst{22-21} = src3{4-3};
5024     let Inst{20-16} = src1;
5025     let Inst{13}    = bit13;
5026     let Inst{12-8}  = src2{4-0};
5027     let Inst{7-5}   = src3{2-0};
5028     let Inst{4-0}   = dst;
5029   }
5030
5031 // Rx=insert(Rs,Rtt)
5032 // Rx=insert(Rs,#u5,#U5)
5033 let hasNewValue = 1, isCodeGenOnly = 0 in {
5034   def S2_insert_rp : T_S3op_insert <"insert", IntRegs>;
5035   def S2_insert    : T_S2op_insert <0b1111, IntRegs, u5Imm>;
5036 }
5037
5038 // Rxx=insert(Rss,Rtt)
5039 // Rxx=insert(Rss,#u6,#U6)
5040 let isCodeGenOnly = 0 in {
5041 def S2_insertp_rp : T_S3op_insert<"insert", DoubleRegs>;
5042 def S2_insertp    : T_S2op_insert <0b0011, DoubleRegs, u6Imm>;
5043 }
5044
5045 //===----------------------------------------------------------------------===//
5046 // Template class for 'extract bitfield' instructions
5047 //===----------------------------------------------------------------------===//
5048 let hasNewValue = 1, hasSideEffects = 0 in
5049 class T_S3op_extract <string mnemonic, bits<2> MinOp>
5050   : SInst <(outs IntRegs:$Rd), (ins IntRegs:$Rs, DoubleRegs:$Rtt),
5051   "$Rd = "#mnemonic#"($Rs, $Rtt)",
5052   [], "", S_3op_tc_2_SLOT23 > {
5053     bits<5> Rd;
5054     bits<5> Rs;
5055     bits<5> Rtt;
5056
5057     let IClass = 0b1100;
5058
5059     let Inst{27-22} = 0b100100;
5060     let Inst{20-16} = Rs;
5061     let Inst{12-8}  = Rtt;
5062     let Inst{7-6}   = MinOp;
5063     let Inst{4-0}   = Rd;
5064   }
5065
5066 let hasSideEffects = 0 in
5067 class T_S2op_extract <string mnemonic, bits<4> RegTyBits,
5068                       RegisterClass RC, Operand ImmOp>
5069   : SInst <(outs RC:$dst), (ins RC:$src1, ImmOp:$src2, ImmOp:$src3),
5070   "$dst = "#mnemonic#"($src1, #$src2, #$src3)",
5071   [], "", S_2op_tc_2_SLOT23> {
5072     bits<5> dst;
5073     bits<5> src1;
5074     bits<6> src2;
5075     bits<6> src3;
5076     bit bit23;
5077     bit bit13;
5078     string ImmOpStr = !cast<string>(ImmOp);
5079
5080     let bit23 = !if (!eq(ImmOpStr, "u6Imm"), src3{5},
5081                 !if (!eq(mnemonic, "extractu"), 0, 1));
5082
5083     let bit13 = !if (!eq(ImmOpStr, "u6Imm"), src2{5}, 0);
5084
5085     let IClass = 0b1000;
5086
5087     let Inst{27-24} = RegTyBits;
5088     let Inst{23}    = bit23;
5089     let Inst{22-21} = src3{4-3};
5090     let Inst{20-16} = src1;
5091     let Inst{13}    = bit13;
5092     let Inst{12-8}  = src2{4-0};
5093     let Inst{7-5}   = src3{2-0};
5094     let Inst{4-0}   = dst;
5095   }
5096
5097 // Extract bitfield
5098
5099 // Rdd=extractu(Rss,Rtt)
5100 // Rdd=extractu(Rss,#u6,#U6)
5101 let isCodeGenOnly = 0 in {
5102 def S2_extractup_rp : T_S3op_64 < "extractu", 0b00, 0b000, 0>;
5103 def S2_extractup    : T_S2op_extract <"extractu", 0b0001, DoubleRegs, u6Imm>;
5104 }
5105
5106 // Rd=extractu(Rs,Rtt)
5107 // Rd=extractu(Rs,#u5,#U5)
5108 let hasNewValue = 1, isCodeGenOnly = 0 in {
5109   def S2_extractu_rp : T_S3op_extract<"extractu", 0b00>;
5110   def S2_extractu    : T_S2op_extract <"extractu", 0b1101, IntRegs, u5Imm>;
5111 }
5112
5113 //===----------------------------------------------------------------------===//
5114 // :raw for of tableindx[bdhw] insns
5115 //===----------------------------------------------------------------------===//
5116
5117 let hasSideEffects = 0, hasNewValue = 1, opNewValue = 0 in
5118 class tableidxRaw<string OpStr, bits<2>MinOp>
5119   : SInst <(outs IntRegs:$Rx),
5120            (ins IntRegs:$_dst_, IntRegs:$Rs, u4Imm:$u4, s6Imm:$S6),
5121            "$Rx = "#OpStr#"($Rs, #$u4, #$S6):raw",
5122     [], "$Rx = $_dst_" > {
5123     bits<5> Rx;
5124     bits<5> Rs;
5125     bits<4> u4;
5126     bits<6> S6;
5127
5128     let IClass = 0b1000;
5129
5130     let Inst{27-24} = 0b0111;
5131     let Inst{23-22} = MinOp;
5132     let Inst{21}    = u4{3};
5133     let Inst{20-16} = Rs;
5134     let Inst{13-8}  = S6;
5135     let Inst{7-5}   = u4{2-0};
5136     let Inst{4-0}   = Rx;
5137   }
5138
5139 let isCodeGenOnly = 0 in {
5140 def S2_tableidxb : tableidxRaw<"tableidxb", 0b00>;
5141 def S2_tableidxh : tableidxRaw<"tableidxh", 0b01>;
5142 def S2_tableidxw : tableidxRaw<"tableidxw", 0b10>;
5143 def S2_tableidxd : tableidxRaw<"tableidxd", 0b11>;
5144 }
5145
5146 // Change the sign of the immediate for Rd=-mpyi(Rs,#u8)
5147 def : Pat <(mul (i32 IntRegs:$src1), (ineg n8ImmPred:$src2)),
5148       (i32 (M2_mpysin (i32 IntRegs:$src1), u8ImmPred:$src2))>;
5149
5150 //===----------------------------------------------------------------------===//
5151 // V3 Instructions +
5152 //===----------------------------------------------------------------------===//
5153
5154 include "HexagonInstrInfoV3.td"
5155
5156 //===----------------------------------------------------------------------===//
5157 // V3 Instructions -
5158 //===----------------------------------------------------------------------===//
5159
5160 //===----------------------------------------------------------------------===//
5161 // V4 Instructions +
5162 //===----------------------------------------------------------------------===//
5163
5164 include "HexagonInstrInfoV4.td"
5165
5166 //===----------------------------------------------------------------------===//
5167 // V4 Instructions -
5168 //===----------------------------------------------------------------------===//
5169
5170 //===----------------------------------------------------------------------===//
5171 // V5 Instructions +
5172 //===----------------------------------------------------------------------===//
5173
5174 include "HexagonInstrInfoV5.td"
5175
5176 //===----------------------------------------------------------------------===//
5177 // V5 Instructions -
5178 //===----------------------------------------------------------------------===//