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