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