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