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