3ba7d551bea2249703297861b02ad97a6d7b0a14
[oota-llvm.git] / lib / Target / Hexagon / HexagonInstrInfoV4.td
1 //=- HexagonInstrInfoV4.td - Target Desc. for Hexagon Target -*- 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 V4 instructions in TableGen format.
11 //
12 //===----------------------------------------------------------------------===//
13
14 def addrga: PatLeaf<(i32 AddrGA:$Addr)>;
15 def addrgp: PatLeaf<(i32 AddrGP:$Addr)>;
16
17 let hasSideEffects = 0 in
18 class T_Immext<Operand ImmType>
19   : EXTENDERInst<(outs), (ins ImmType:$imm),
20                  "immext(#$imm)", []> {
21     bits<32> imm;
22     let IClass = 0b0000;
23
24     let Inst{27-16} = imm{31-20};
25     let Inst{13-0} = imm{19-6};
26   }
27
28 def A4_ext : T_Immext<u26_6Imm>;
29 let isCodeGenOnly = 1 in {
30   let isBranch = 1 in
31     def A4_ext_b : T_Immext<brtarget>;
32   let isCall = 1 in
33     def A4_ext_c : T_Immext<calltarget>;
34   def A4_ext_g : T_Immext<globaladdress>;
35 }
36
37 def BITPOS32 : SDNodeXForm<imm, [{
38    // Return the bit position we will set [0-31].
39    // As an SDNode.
40    int32_t imm = N->getSExtValue();
41    return XformMskToBitPosU5Imm(imm);
42 }]>;
43
44 // Hexagon V4 Architecture spec defines 8 instruction classes:
45 // LD ST ALU32 XTYPE J JR MEMOP NV CR SYSTEM(system is not implemented in the
46 // compiler)
47
48 // LD Instructions:
49 // ========================================
50 // Loads (8/16/32/64 bit)
51 // Deallocframe
52
53 // ST Instructions:
54 // ========================================
55 // Stores (8/16/32/64 bit)
56 // Allocframe
57
58 // ALU32 Instructions:
59 // ========================================
60 // Arithmetic / Logical (32 bit)
61 // Vector Halfword
62
63 // XTYPE Instructions (32/64 bit):
64 // ========================================
65 // Arithmetic, Logical, Bit Manipulation
66 // Multiply (Integer, Fractional, Complex)
67 // Permute / Vector Permute Operations
68 // Predicate Operations
69 // Shift / Shift with Add/Sub/Logical
70 // Vector Byte ALU
71 // Vector Halfword (ALU, Shift, Multiply)
72 // Vector Word (ALU, Shift)
73
74 // J Instructions:
75 // ========================================
76 // Jump/Call PC-relative
77
78 // JR Instructions:
79 // ========================================
80 // Jump/Call Register
81
82 // MEMOP Instructions:
83 // ========================================
84 // Operation on memory (8/16/32 bit)
85
86 // NV Instructions:
87 // ========================================
88 // New-value Jumps
89 // New-value Stores
90
91 // CR Instructions:
92 // ========================================
93 // Control-Register Transfers
94 // Hardware Loop Setup
95 // Predicate Logicals & Reductions
96
97 // SYSTEM Instructions (not implemented in the compiler):
98 // ========================================
99 // Prefetch
100 // Cache Maintenance
101 // Bus Operations
102
103
104 //===----------------------------------------------------------------------===//
105 // ALU32 +
106 //===----------------------------------------------------------------------===//
107
108 class T_ALU32_3op_not<string mnemonic, bits<3> MajOp, bits<3> MinOp,
109                       bit OpsRev>
110   : T_ALU32_3op<mnemonic, MajOp, MinOp, OpsRev, 0> {
111   let AsmString = "$Rd = "#mnemonic#"($Rs, ~$Rt)";
112 }
113
114 let BaseOpcode = "andn_rr", CextOpcode = "andn" in
115 def A4_andn    : T_ALU32_3op_not<"and", 0b001, 0b100, 1>;
116 let BaseOpcode = "orn_rr", CextOpcode = "orn" in
117 def A4_orn     : T_ALU32_3op_not<"or",  0b001, 0b101, 1>;
118
119 let CextOpcode = "rcmp.eq" in
120 def A4_rcmpeq  : T_ALU32_3op<"cmp.eq",  0b011, 0b010, 0, 1>;
121 let CextOpcode = "!rcmp.eq" in
122 def A4_rcmpneq : T_ALU32_3op<"!cmp.eq", 0b011, 0b011, 0, 1>;
123
124 def C4_cmpneq  : T_ALU32_3op_cmp<"!cmp.eq",  0b00, 1, 1>;
125 def C4_cmplte  : T_ALU32_3op_cmp<"!cmp.gt",  0b10, 1, 0>;
126 def C4_cmplteu : T_ALU32_3op_cmp<"!cmp.gtu", 0b11, 1, 0>;
127
128 // Pats for instruction selection.
129
130 // A class to embed the usual comparison patfrags within a zext to i32.
131 // The seteq/setne frags use "lhs" and "rhs" as operands, so use the same
132 // names, or else the frag's "body" won't match the operands.
133 class CmpInReg<PatFrag Op>
134   : PatFrag<(ops node:$lhs, node:$rhs),(i32 (zext (i1 Op.Fragment)))>;
135
136 def: T_cmp32_rr_pat<A4_rcmpeq,  CmpInReg<seteq>, i32>;
137 def: T_cmp32_rr_pat<A4_rcmpneq, CmpInReg<setne>, i32>;
138
139 def: T_cmp32_rr_pat<C4_cmpneq,  setne,  i1>;
140 def: T_cmp32_rr_pat<C4_cmplteu, setule, i1>;
141
142 def: T_cmp32_rr_pat<C4_cmplteu, RevCmp<setuge>, i1>;
143
144 class T_CMP_rrbh<string mnemonic, bits<3> MinOp, bit IsComm>
145   : SInst<(outs PredRegs:$Pd), (ins IntRegs:$Rs, IntRegs:$Rt),
146     "$Pd = "#mnemonic#"($Rs, $Rt)", [], "", S_3op_tc_2early_SLOT23>,
147     ImmRegRel {
148   let InputType = "reg";
149   let CextOpcode = mnemonic;
150   let isCompare = 1;
151   let isCommutable = IsComm;
152   let hasSideEffects = 0;
153
154   bits<2> Pd;
155   bits<5> Rs;
156   bits<5> Rt;
157
158   let IClass = 0b1100;
159   let Inst{27-21} = 0b0111110;
160   let Inst{20-16} = Rs;
161   let Inst{12-8} = Rt;
162   let Inst{7-5} = MinOp;
163   let Inst{1-0} = Pd;
164 }
165
166 def A4_cmpbeq  : T_CMP_rrbh<"cmpb.eq",  0b110, 1>;
167 def A4_cmpbgt  : T_CMP_rrbh<"cmpb.gt",  0b010, 0>;
168 def A4_cmpbgtu : T_CMP_rrbh<"cmpb.gtu", 0b111, 0>;
169 def A4_cmpheq  : T_CMP_rrbh<"cmph.eq",  0b011, 1>;
170 def A4_cmphgt  : T_CMP_rrbh<"cmph.gt",  0b100, 0>;
171 def A4_cmphgtu : T_CMP_rrbh<"cmph.gtu", 0b101, 0>;
172
173 let AddedComplexity = 100 in {
174   def: Pat<(i1 (seteq (and (xor (i32 IntRegs:$Rs), (i32 IntRegs:$Rt)),
175                        255), 0)),
176            (A4_cmpbeq IntRegs:$Rs, IntRegs:$Rt)>;
177   def: Pat<(i1 (setne (and (xor (i32 IntRegs:$Rs), (i32 IntRegs:$Rt)),
178                        255), 0)),
179            (C2_not (A4_cmpbeq IntRegs:$Rs, IntRegs:$Rt))>;
180   def: Pat<(i1 (seteq (and (xor (i32 IntRegs:$Rs), (i32 IntRegs:$Rt)),
181                            65535), 0)),
182            (A4_cmpheq IntRegs:$Rs, IntRegs:$Rt)>;
183   def: Pat<(i1 (setne (and (xor (i32 IntRegs:$Rs), (i32 IntRegs:$Rt)),
184                            65535), 0)),
185            (C2_not (A4_cmpheq IntRegs:$Rs, IntRegs:$Rt))>;
186 }
187
188 class T_CMP_ribh<string mnemonic, bits<2> MajOp, bit IsHalf, bit IsComm,
189                  Operand ImmType, bit IsImmExt, bit IsImmSigned, int ImmBits>
190   : ALU64Inst<(outs PredRegs:$Pd), (ins IntRegs:$Rs, ImmType:$Imm),
191     "$Pd = "#mnemonic#"($Rs, #$Imm)", [], "", ALU64_tc_2early_SLOT23>,
192     ImmRegRel {
193   let InputType = "imm";
194   let CextOpcode = mnemonic;
195   let isCompare = 1;
196   let isCommutable = IsComm;
197   let hasSideEffects = 0;
198   let isExtendable = IsImmExt;
199   let opExtendable = !if (IsImmExt, 2, 0);
200   let isExtentSigned = IsImmSigned;
201   let opExtentBits = ImmBits;
202
203   bits<2> Pd;
204   bits<5> Rs;
205   bits<8> Imm;
206
207   let IClass = 0b1101;
208   let Inst{27-24} = 0b1101;
209   let Inst{22-21} = MajOp;
210   let Inst{20-16} = Rs;
211   let Inst{12-5} = Imm;
212   let Inst{4} = 0b0;
213   let Inst{3} = IsHalf;
214   let Inst{1-0} = Pd;
215 }
216
217 def A4_cmpbeqi  : T_CMP_ribh<"cmpb.eq",  0b00, 0, 1, u8Imm, 0, 0, 8>;
218 def A4_cmpbgti  : T_CMP_ribh<"cmpb.gt",  0b01, 0, 0, s8Imm, 0, 1, 8>;
219 def A4_cmpbgtui : T_CMP_ribh<"cmpb.gtu", 0b10, 0, 0, u7Ext, 1, 0, 7>;
220 def A4_cmpheqi  : T_CMP_ribh<"cmph.eq",  0b00, 1, 1, s8Ext, 1, 1, 8>;
221 def A4_cmphgti  : T_CMP_ribh<"cmph.gt",  0b01, 1, 0, s8Ext, 1, 1, 8>;
222 def A4_cmphgtui : T_CMP_ribh<"cmph.gtu", 0b10, 1, 0, u7Ext, 1, 0, 7>;
223
224 class T_RCMP_EQ_ri<string mnemonic, bit IsNeg>
225   : ALU32_ri<(outs IntRegs:$Rd), (ins IntRegs:$Rs, s8Ext:$s8),
226     "$Rd = "#mnemonic#"($Rs, #$s8)", [], "", ALU32_2op_tc_1_SLOT0123>,
227     ImmRegRel {
228   let InputType = "imm";
229   let CextOpcode = !if (IsNeg, "!rcmp.eq", "rcmp.eq");
230   let isExtendable = 1;
231   let opExtendable = 2;
232   let isExtentSigned = 1;
233   let opExtentBits = 8;
234   let hasNewValue = 1;
235
236   bits<5> Rd;
237   bits<5> Rs;
238   bits<8> s8;
239
240   let IClass = 0b0111;
241   let Inst{27-24} = 0b0011;
242   let Inst{22} = 0b1;
243   let Inst{21} = IsNeg;
244   let Inst{20-16} = Rs;
245   let Inst{13} = 0b1;
246   let Inst{12-5} = s8;
247   let Inst{4-0} = Rd;
248 }
249
250 def A4_rcmpeqi  : T_RCMP_EQ_ri<"cmp.eq",  0>;
251 def A4_rcmpneqi : T_RCMP_EQ_ri<"!cmp.eq", 1>;
252
253 def: Pat<(i32 (zext (i1 (seteq (i32 IntRegs:$Rs), s8ExtPred:$s8)))),
254          (A4_rcmpeqi IntRegs:$Rs, s8ExtPred:$s8)>;
255 def: Pat<(i32 (zext (i1 (setne (i32 IntRegs:$Rs), s8ExtPred:$s8)))),
256          (A4_rcmpneqi IntRegs:$Rs, s8ExtPred:$s8)>;
257
258 // Preserve the S2_tstbit_r generation
259 def: Pat<(i32 (zext (i1 (setne (i32 (and (i32 (shl 1, (i32 IntRegs:$src2))),
260                                          (i32 IntRegs:$src1))), 0)))),
261          (C2_muxii (S2_tstbit_r IntRegs:$src1, IntRegs:$src2), 1, 0)>;
262
263 //===----------------------------------------------------------------------===//
264 // ALU32 -
265 //===----------------------------------------------------------------------===//
266
267
268 //===----------------------------------------------------------------------===//
269 // ALU32/PERM +
270 //===----------------------------------------------------------------------===//
271
272 // Combine a word and an immediate into a register pair.
273 let hasSideEffects = 0, isExtentSigned = 1, isExtendable = 1,
274     opExtentBits = 8 in
275 class T_Combine1 <bits<2> MajOp, dag ins, string AsmStr>
276   : ALU32Inst <(outs DoubleRegs:$Rdd), ins, AsmStr> {
277     bits<5> Rdd;
278     bits<5> Rs;
279     bits<8> s8;
280
281     let IClass      = 0b0111;
282     let Inst{27-24} = 0b0011;
283     let Inst{22-21} = MajOp;
284     let Inst{20-16} = Rs;
285     let Inst{13}    = 0b1;
286     let Inst{12-5}  = s8;
287     let Inst{4-0}   = Rdd;
288   }
289
290 let opExtendable = 2 in
291 def A4_combineri : T_Combine1<0b00, (ins IntRegs:$Rs, s8Ext:$s8),
292                                     "$Rdd = combine($Rs, #$s8)">;
293
294 let opExtendable = 1 in
295 def A4_combineir : T_Combine1<0b01, (ins s8Ext:$s8, IntRegs:$Rs),
296                                     "$Rdd = combine(#$s8, $Rs)">;
297
298 def HexagonWrapperCombineRI_V4 :
299   SDNode<"HexagonISD::WrapperCombineRI_V4", SDTHexagonI64I32I32>;
300 def HexagonWrapperCombineIR_V4 :
301   SDNode<"HexagonISD::WrapperCombineIR_V4", SDTHexagonI64I32I32>;
302
303 def : Pat <(HexagonWrapperCombineRI_V4 IntRegs:$r, s8ExtPred:$i),
304            (A4_combineri IntRegs:$r, s8ExtPred:$i)>;
305
306 def : Pat <(HexagonWrapperCombineIR_V4 s8ExtPred:$i, IntRegs:$r),
307            (A4_combineir s8ExtPred:$i, IntRegs:$r)>;
308
309 // A4_combineii: Set two small immediates.
310 let hasSideEffects = 0, isExtendable = 1, opExtentBits = 6, opExtendable = 2 in
311 def A4_combineii: ALU32Inst<(outs DoubleRegs:$Rdd), (ins s8Imm:$s8, u6Ext:$U6),
312   "$Rdd = combine(#$s8, #$U6)"> {
313     bits<5> Rdd;
314     bits<8> s8;
315     bits<6> U6;
316
317     let IClass = 0b0111;
318     let Inst{27-23} = 0b11001;
319     let Inst{20-16} = U6{5-1};
320     let Inst{13}    = U6{0};
321     let Inst{12-5}  = s8;
322     let Inst{4-0}   = Rdd;
323   }
324
325 // The complexity of the combine with two immediates should be greater than
326 // the complexity of a combine involving a register.
327 let AddedComplexity = 75 in
328 def: Pat<(HexagonCOMBINE s8ImmPred:$s8, u6ExtPred:$u6),
329          (A4_combineii imm:$s8, imm:$u6)>;
330
331 //===----------------------------------------------------------------------===//
332 // ALU32/PERM -
333 //===----------------------------------------------------------------------===//
334
335 //===----------------------------------------------------------------------===//
336 // LD +
337 //===----------------------------------------------------------------------===//
338
339 def Zext64: OutPatFrag<(ops node:$Rs),
340   (i64 (A4_combineir 0, (i32 $Rs)))>;
341 def Sext64: OutPatFrag<(ops node:$Rs),
342   (i64 (A2_sxtw (i32 $Rs)))>;
343
344 // Patterns to generate indexed loads with different forms of the address:
345 // - frameindex,
346 // - base + offset,
347 // - base (without offset).
348 multiclass Loadxm_pat<PatFrag Load, ValueType VT, PatFrag ValueMod,
349                       PatLeaf ImmPred, InstHexagon MI> {
350   def: Pat<(VT (Load AddrFI:$fi)),
351            (VT (ValueMod (MI AddrFI:$fi, 0)))>;
352   def: Pat<(VT (Load (add IntRegs:$Rs, ImmPred:$Off))),
353            (VT (ValueMod (MI IntRegs:$Rs, imm:$Off)))>;
354   def: Pat<(VT (Load (i32 IntRegs:$Rs))),
355            (VT (ValueMod (MI IntRegs:$Rs, 0)))>;
356 }
357
358 defm: Loadxm_pat<extloadi1,   i64, Zext64, s11_0ExtPred, L2_loadrub_io>;
359 defm: Loadxm_pat<extloadi8,   i64, Zext64, s11_0ExtPred, L2_loadrub_io>;
360 defm: Loadxm_pat<extloadi16,  i64, Zext64, s11_1ExtPred, L2_loadruh_io>;
361 defm: Loadxm_pat<zextloadi1,  i64, Zext64, s11_0ExtPred, L2_loadrub_io>;
362 defm: Loadxm_pat<zextloadi8,  i64, Zext64, s11_0ExtPred, L2_loadrub_io>;
363 defm: Loadxm_pat<zextloadi16, i64, Zext64, s11_1ExtPred, L2_loadruh_io>;
364 defm: Loadxm_pat<sextloadi8,  i64, Sext64, s11_0ExtPred, L2_loadrb_io>;
365 defm: Loadxm_pat<sextloadi16, i64, Sext64, s11_1ExtPred, L2_loadrh_io>;
366
367 // Map Rdd = anyext(Rs) -> Rdd = combine(#0, Rs).
368 def: Pat<(i64 (anyext (i32 IntRegs:$src1))), (Zext64 IntRegs:$src1)>;
369
370 //===----------------------------------------------------------------------===//
371 // Template class for load instructions with Absolute set addressing mode.
372 //===----------------------------------------------------------------------===//
373 let isExtended = 1, opExtendable = 2, opExtentBits = 6, addrMode = AbsoluteSet,
374     hasSideEffects = 0 in
375 class T_LD_abs_set<string mnemonic, RegisterClass RC, bits<4>MajOp>:
376             LDInst<(outs RC:$dst1, IntRegs:$dst2),
377             (ins u6Ext:$addr),
378             "$dst1 = "#mnemonic#"($dst2 = #$addr)",
379             []> {
380   bits<7> name;
381   bits<5> dst1;
382   bits<5> dst2;
383   bits<6> addr;
384
385   let IClass = 0b1001;
386   let Inst{27-25} = 0b101;
387   let Inst{24-21} = MajOp;
388   let Inst{13-12} = 0b01;
389   let Inst{4-0}   = dst1;
390   let Inst{20-16} = dst2;
391   let Inst{11-8}  = addr{5-2};
392   let Inst{6-5}   = addr{1-0};
393 }
394
395 let accessSize = ByteAccess, hasNewValue = 1 in {
396   def L4_loadrb_ap   : T_LD_abs_set <"memb",   IntRegs, 0b1000>;
397   def L4_loadrub_ap  : T_LD_abs_set <"memub",  IntRegs, 0b1001>;
398 }
399
400 let accessSize = HalfWordAccess, hasNewValue = 1 in {
401   def L4_loadrh_ap  : T_LD_abs_set <"memh",  IntRegs, 0b1010>;
402   def L4_loadruh_ap : T_LD_abs_set <"memuh", IntRegs, 0b1011>;
403   def L4_loadbsw2_ap : T_LD_abs_set <"membh",  IntRegs, 0b0001>;
404   def L4_loadbzw2_ap : T_LD_abs_set <"memubh", IntRegs, 0b0011>;
405 }
406
407 let accessSize = WordAccess, hasNewValue = 1 in
408   def L4_loadri_ap : T_LD_abs_set <"memw", IntRegs, 0b1100>;
409
410 let accessSize = WordAccess in {
411   def L4_loadbzw4_ap : T_LD_abs_set <"memubh", DoubleRegs, 0b0101>;
412   def L4_loadbsw4_ap : T_LD_abs_set <"membh",  DoubleRegs, 0b0111>;
413 }
414
415 let accessSize = DoubleWordAccess in
416 def L4_loadrd_ap : T_LD_abs_set <"memd", DoubleRegs, 0b1110>;
417
418 let accessSize = ByteAccess in
419   def L4_loadalignb_ap : T_LD_abs_set <"memb_fifo", DoubleRegs, 0b0100>;
420
421 let accessSize = HalfWordAccess in
422 def L4_loadalignh_ap : T_LD_abs_set <"memh_fifo", DoubleRegs, 0b0010>;
423
424 // Load - Indirect with long offset
425 let InputType = "imm", addrMode = BaseLongOffset, isExtended = 1,
426 opExtentBits = 6, opExtendable = 3 in
427 class T_LoadAbsReg <string mnemonic, string CextOp, RegisterClass RC,
428                     bits<4> MajOp>
429   : LDInst <(outs RC:$dst), (ins IntRegs:$src1, u2Imm:$src2, u6Ext:$src3),
430   "$dst = "#mnemonic#"($src1<<#$src2 + #$src3)",
431   [] >, ImmRegShl {
432     bits<5> dst;
433     bits<5> src1;
434     bits<2> src2;
435     bits<6> src3;
436     let CextOpcode = CextOp;
437     let hasNewValue = !if (!eq(!cast<string>(RC), "DoubleRegs"), 0, 1);
438
439     let IClass = 0b1001;
440     let Inst{27-25} = 0b110;
441     let Inst{24-21} = MajOp;
442     let Inst{20-16} = src1;
443     let Inst{13}    = src2{1};
444     let Inst{12}    = 0b1;
445     let Inst{11-8}  = src3{5-2};
446     let Inst{7}     = src2{0};
447     let Inst{6-5}   = src3{1-0};
448     let Inst{4-0}   = dst;
449   }
450
451 let accessSize = ByteAccess in {
452   def L4_loadrb_ur  : T_LoadAbsReg<"memb",  "LDrib", IntRegs, 0b1000>;
453   def L4_loadrub_ur : T_LoadAbsReg<"memub", "LDriub", IntRegs, 0b1001>;
454   def L4_loadalignb_ur : T_LoadAbsReg<"memb_fifo", "LDrib_fifo",
455                                       DoubleRegs, 0b0100>;
456 }
457
458 let accessSize = HalfWordAccess in {
459   def L4_loadrh_ur   : T_LoadAbsReg<"memh",   "LDrih",    IntRegs, 0b1010>;
460   def L4_loadruh_ur  : T_LoadAbsReg<"memuh",  "LDriuh",   IntRegs, 0b1011>;
461   def L4_loadbsw2_ur : T_LoadAbsReg<"membh",  "LDribh2",  IntRegs, 0b0001>;
462   def L4_loadbzw2_ur : T_LoadAbsReg<"memubh", "LDriubh2", IntRegs, 0b0011>;
463   def L4_loadalignh_ur : T_LoadAbsReg<"memh_fifo", "LDrih_fifo",
464                                       DoubleRegs, 0b0010>;
465 }
466
467 let accessSize = WordAccess in {
468   def L4_loadri_ur   : T_LoadAbsReg<"memw", "LDriw", IntRegs, 0b1100>;
469   def L4_loadbsw4_ur : T_LoadAbsReg<"membh", "LDribh4", DoubleRegs, 0b0111>;
470   def L4_loadbzw4_ur : T_LoadAbsReg<"memubh", "LDriubh4", DoubleRegs, 0b0101>;
471 }
472
473 let accessSize = DoubleWordAccess in
474 def L4_loadrd_ur  : T_LoadAbsReg<"memd", "LDrid", DoubleRegs, 0b1110>;
475
476
477 multiclass T_LoadAbsReg_Pat <PatFrag ldOp, InstHexagon MI, ValueType VT = i32> {
478   def  : Pat <(VT (ldOp (add (shl IntRegs:$src1, u2ImmPred:$src2),
479                              (HexagonCONST32 tglobaladdr:$src3)))),
480               (MI IntRegs:$src1, u2ImmPred:$src2, tglobaladdr:$src3)>;
481
482   def  : Pat <(VT (ldOp (add IntRegs:$src1,
483                              (HexagonCONST32 tglobaladdr:$src2)))),
484               (MI IntRegs:$src1, 0, tglobaladdr:$src2)>;
485 }
486
487 let AddedComplexity  = 60 in {
488 defm : T_LoadAbsReg_Pat <sextloadi8, L4_loadrb_ur>;
489 defm : T_LoadAbsReg_Pat <zextloadi8, L4_loadrub_ur>;
490 defm : T_LoadAbsReg_Pat <extloadi8,  L4_loadrub_ur>;
491
492 defm : T_LoadAbsReg_Pat <sextloadi16, L4_loadrh_ur>;
493 defm : T_LoadAbsReg_Pat <zextloadi16, L4_loadruh_ur>;
494 defm : T_LoadAbsReg_Pat <extloadi16,  L4_loadruh_ur>;
495
496 defm : T_LoadAbsReg_Pat <load, L4_loadri_ur>;
497 defm : T_LoadAbsReg_Pat <load, L4_loadrd_ur, i64>;
498 }
499
500 //===----------------------------------------------------------------------===//
501 // Template classes for the non-predicated load instructions with
502 // base + register offset addressing mode
503 //===----------------------------------------------------------------------===//
504 class T_load_rr <string mnemonic, RegisterClass RC, bits<3> MajOp>:
505    LDInst<(outs RC:$dst), (ins IntRegs:$src1, IntRegs:$src2, u2Imm:$u2),
506   "$dst = "#mnemonic#"($src1 + $src2<<#$u2)",
507   [], "", V4LDST_tc_ld_SLOT01>, ImmRegShl, AddrModeRel {
508     bits<5> dst;
509     bits<5> src1;
510     bits<5> src2;
511     bits<2> u2;
512
513     let IClass = 0b0011;
514
515     let Inst{27-24} = 0b1010;
516     let Inst{23-21} = MajOp;
517     let Inst{20-16} = src1;
518     let Inst{12-8}  = src2;
519     let Inst{13}    = u2{1};
520     let Inst{7}     = u2{0};
521     let Inst{4-0}   = dst;
522   }
523
524 //===----------------------------------------------------------------------===//
525 // Template classes for the predicated load instructions with
526 // base + register offset addressing mode
527 //===----------------------------------------------------------------------===//
528 let isPredicated =  1 in
529 class T_pload_rr <string mnemonic, RegisterClass RC, bits<3> MajOp,
530                   bit isNot, bit isPredNew>:
531    LDInst <(outs RC:$dst),
532            (ins PredRegs:$src1, IntRegs:$src2, IntRegs:$src3, u2Imm:$u2),
533   !if(isNot, "if (!$src1", "if ($src1")#!if(isPredNew, ".new) ",
534   ") ")#"$dst = "#mnemonic#"($src2+$src3<<#$u2)",
535   [], "", V4LDST_tc_ld_SLOT01>, AddrModeRel {
536     bits<5> dst;
537     bits<2> src1;
538     bits<5> src2;
539     bits<5> src3;
540     bits<2> u2;
541
542     let isPredicatedFalse = isNot;
543     let isPredicatedNew = isPredNew;
544
545     let IClass = 0b0011;
546
547     let Inst{27-26} = 0b00;
548     let Inst{25}    = isPredNew;
549     let Inst{24}    = isNot;
550     let Inst{23-21} = MajOp;
551     let Inst{20-16} = src2;
552     let Inst{12-8}  = src3;
553     let Inst{13}    = u2{1};
554     let Inst{7}     = u2{0};
555     let Inst{6-5}   = src1;
556     let Inst{4-0}   = dst;
557   }
558
559 //===----------------------------------------------------------------------===//
560 // multiclass for load instructions with base + register offset
561 // addressing mode
562 //===----------------------------------------------------------------------===//
563 let hasSideEffects = 0, addrMode = BaseRegOffset in
564 multiclass ld_idxd_shl <string mnemonic, string CextOp, RegisterClass RC,
565                         bits<3> MajOp > {
566   let CextOpcode = CextOp, BaseOpcode = CextOp#_indexed_shl,
567       InputType = "reg" in {
568     let isPredicable = 1 in
569     def L4_#NAME#_rr : T_load_rr <mnemonic, RC, MajOp>;
570
571     // Predicated
572     def L4_p#NAME#t_rr : T_pload_rr <mnemonic, RC, MajOp, 0, 0>;
573     def L4_p#NAME#f_rr : T_pload_rr <mnemonic, RC, MajOp, 1, 0>;
574
575     // Predicated new
576     def L4_p#NAME#tnew_rr : T_pload_rr <mnemonic, RC, MajOp, 0, 1>;
577     def L4_p#NAME#fnew_rr : T_pload_rr <mnemonic, RC, MajOp, 1, 1>;
578   }
579 }
580
581 let hasNewValue = 1, accessSize = ByteAccess in {
582   defm loadrb  : ld_idxd_shl<"memb", "LDrib", IntRegs, 0b000>;
583   defm loadrub : ld_idxd_shl<"memub", "LDriub", IntRegs, 0b001>;
584 }
585
586 let hasNewValue = 1, accessSize = HalfWordAccess in {
587   defm loadrh  : ld_idxd_shl<"memh", "LDrih", IntRegs, 0b010>;
588   defm loadruh : ld_idxd_shl<"memuh", "LDriuh", IntRegs, 0b011>;
589 }
590
591 let hasNewValue = 1, accessSize = WordAccess in
592 defm loadri : ld_idxd_shl<"memw", "LDriw", IntRegs, 0b100>;
593
594 let accessSize = DoubleWordAccess in
595 defm loadrd  : ld_idxd_shl<"memd", "LDrid", DoubleRegs, 0b110>;
596
597 // 'def pats' for load instructions with base + register offset and non-zero
598 // immediate value. Immediate value is used to left-shift the second
599 // register operand.
600 class Loadxs_pat<PatFrag Load, ValueType VT, InstHexagon MI>
601   : Pat<(VT (Load (add (i32 IntRegs:$Rs),
602                        (i32 (shl (i32 IntRegs:$Rt), u2ImmPred:$u2))))),
603         (VT (MI IntRegs:$Rs, IntRegs:$Rt, imm:$u2))>;
604
605 let AddedComplexity = 40 in {
606   def: Loadxs_pat<extloadi8,   i32, L4_loadrub_rr>;
607   def: Loadxs_pat<zextloadi8,  i32, L4_loadrub_rr>;
608   def: Loadxs_pat<sextloadi8,  i32, L4_loadrb_rr>;
609   def: Loadxs_pat<extloadi16,  i32, L4_loadruh_rr>;
610   def: Loadxs_pat<zextloadi16, i32, L4_loadruh_rr>;
611   def: Loadxs_pat<sextloadi16, i32, L4_loadrh_rr>;
612   def: Loadxs_pat<load,        i32, L4_loadri_rr>;
613   def: Loadxs_pat<load,        i64, L4_loadrd_rr>;
614 }
615
616 // 'def pats' for load instruction base + register offset and
617 // zero immediate value.
618 class Loadxs_simple_pat<PatFrag Load, ValueType VT, InstHexagon MI>
619   : Pat<(VT (Load (add (i32 IntRegs:$Rs), (i32 IntRegs:$Rt)))),
620         (VT (MI IntRegs:$Rs, IntRegs:$Rt, 0))>;
621
622 let AddedComplexity = 20 in {
623   def: Loadxs_simple_pat<extloadi8,   i32, L4_loadrub_rr>;
624   def: Loadxs_simple_pat<zextloadi8,  i32, L4_loadrub_rr>;
625   def: Loadxs_simple_pat<sextloadi8,  i32, L4_loadrb_rr>;
626   def: Loadxs_simple_pat<extloadi16,  i32, L4_loadruh_rr>;
627   def: Loadxs_simple_pat<zextloadi16, i32, L4_loadruh_rr>;
628   def: Loadxs_simple_pat<sextloadi16, i32, L4_loadrh_rr>;
629   def: Loadxs_simple_pat<load,        i32, L4_loadri_rr>;
630   def: Loadxs_simple_pat<load,        i64, L4_loadrd_rr>;
631 }
632
633 // zext i1->i64
634 def: Pat<(i64 (zext (i1 PredRegs:$src1))),
635          (Zext64 (C2_muxii PredRegs:$src1, 1, 0))>;
636
637 // zext i32->i64
638 def: Pat<(i64 (zext (i32 IntRegs:$src1))),
639          (Zext64 IntRegs:$src1)>;
640
641 // zext i32->i64
642 def:  Pat <(i64 (zextloadi32 ADDRriS11_2:$src1)),
643       (i64 (A4_combineir 0, (L2_loadri_io AddrFI:$src1, 0)))>;
644
645 let AddedComplexity = 100 in
646 def:  Pat <(i64 (zextloadi32 (i32 (add IntRegs:$src1, s11_2ExtPred:$offset)))),
647       (i64 (A4_combineir 0, (L2_loadri_io IntRegs:$src1,
648                                   s11_2ExtPred:$offset)))>;
649
650 // anyext i32->i64
651 def:  Pat <(i64 (extloadi32 ADDRriS11_2:$src1)),
652       (i64 (A4_combineir 0, (L2_loadri_io AddrFI:$src1, 0)))>;
653
654 //===----------------------------------------------------------------------===//
655 // LD -
656 //===----------------------------------------------------------------------===//
657
658 //===----------------------------------------------------------------------===//
659 // ST +
660 //===----------------------------------------------------------------------===//
661 ///
662 //===----------------------------------------------------------------------===//
663 // Template class for store instructions with Absolute set addressing mode.
664 //===----------------------------------------------------------------------===//
665 let isExtended = 1, opExtendable = 1, opExtentBits = 6,
666     addrMode = AbsoluteSet, isNVStorable = 1 in
667 class T_ST_absset <string mnemonic, string BaseOp, RegisterClass RC,
668                    bits<3> MajOp, MemAccessSize AccessSz, bit isHalf = 0>
669   : STInst<(outs IntRegs:$dst),
670            (ins u6Ext:$addr, RC:$src),
671     mnemonic#"($dst = #$addr) = $src"#!if(isHalf, ".h","")>, NewValueRel {
672     bits<5> dst;
673     bits<6> addr;
674     bits<5> src;
675     let accessSize = AccessSz;
676     let BaseOpcode = BaseOp#"_AbsSet";
677
678     let IClass = 0b1010;
679
680     let Inst{27-24} = 0b1011;
681     let Inst{23-21} = MajOp;
682     let Inst{20-16} = dst;
683     let Inst{13}    = 0b0;
684     let Inst{12-8}  = src;
685     let Inst{7}     = 0b1;
686     let Inst{5-0}   = addr;
687   }
688
689 def S4_storerb_ap : T_ST_absset <"memb", "STrib", IntRegs, 0b000, ByteAccess>;
690 def S4_storerh_ap : T_ST_absset <"memh", "STrih", IntRegs, 0b010,
691                                  HalfWordAccess>;
692 def S4_storeri_ap : T_ST_absset <"memw", "STriw", IntRegs, 0b100, WordAccess>;
693
694 let isNVStorable = 0 in {
695   def S4_storerf_ap : T_ST_absset <"memh", "STrif", IntRegs,
696                                    0b011, HalfWordAccess, 1>;
697   def S4_storerd_ap : T_ST_absset <"memd", "STrid", DoubleRegs,
698                                    0b110, DoubleWordAccess>;
699 }
700
701 let opExtendable = 1, isNewValue = 1, isNVStore = 1, opNewValue = 2,
702 isExtended = 1, opExtentBits= 6 in
703 class T_ST_absset_nv <string mnemonic, string BaseOp, bits<2> MajOp,
704                       MemAccessSize AccessSz >
705   : NVInst <(outs IntRegs:$dst),
706             (ins u6Ext:$addr, IntRegs:$src),
707     mnemonic#"($dst = #$addr) = $src.new">, NewValueRel {
708     bits<5> dst;
709     bits<6> addr;
710     bits<3> src;
711     let accessSize = AccessSz;
712     let BaseOpcode = BaseOp#"_AbsSet";
713
714     let IClass = 0b1010;
715
716     let Inst{27-21} = 0b1011101;
717     let Inst{20-16} = dst;
718     let Inst{13-11} = 0b000;
719     let Inst{12-11} = MajOp;
720     let Inst{10-8}  = src;
721     let Inst{7}     = 0b1;
722     let Inst{5-0}   = addr;
723   }
724
725 let mayStore = 1, addrMode = AbsoluteSet in {
726   def S4_storerbnew_ap : T_ST_absset_nv <"memb", "STrib", 0b00, ByteAccess>;
727   def S4_storerhnew_ap : T_ST_absset_nv <"memh", "STrih", 0b01, HalfWordAccess>;
728   def S4_storerinew_ap : T_ST_absset_nv <"memw", "STriw", 0b10, WordAccess>;
729 }
730
731 let isExtended = 1, opExtendable = 2, opExtentBits = 6, InputType = "imm",
732 addrMode = BaseLongOffset, AddedComplexity = 40 in
733 class T_StoreAbsReg <string mnemonic, string CextOp, RegisterClass RC,
734                      bits<3> MajOp, MemAccessSize AccessSz, bit isHalf = 0>
735   : STInst<(outs),
736            (ins IntRegs:$src1, u2Imm:$src2, u6Ext:$src3, RC:$src4),
737    mnemonic#"($src1<<#$src2 + #$src3) = $src4"#!if(isHalf, ".h",""),
738    []>, ImmRegShl, NewValueRel {
739
740     bits<5> src1;
741     bits<2> src2;
742     bits<6> src3;
743     bits<5> src4;
744
745     let accessSize = AccessSz;
746     let CextOpcode = CextOp;
747     let BaseOpcode = CextOp#"_shl";
748     let IClass = 0b1010;
749
750     let Inst{27-24} =0b1101;
751     let Inst{23-21} = MajOp;
752     let Inst{20-16} = src1;
753     let Inst{13}    = src2{1};
754     let Inst{12-8}  = src4;
755     let Inst{7}     = 0b1;
756     let Inst{6}     = src2{0};
757     let Inst{5-0}   = src3;
758 }
759
760 def S4_storerb_ur : T_StoreAbsReg <"memb", "STrib", IntRegs, 0b000, ByteAccess>;
761 def S4_storerh_ur : T_StoreAbsReg <"memh", "STrih", IntRegs, 0b010,
762                                    HalfWordAccess>;
763 def S4_storerf_ur : T_StoreAbsReg <"memh", "STrif", IntRegs, 0b011,
764                                    HalfWordAccess, 1>;
765 def S4_storeri_ur : T_StoreAbsReg <"memw", "STriw", IntRegs, 0b100, WordAccess>;
766 def S4_storerd_ur : T_StoreAbsReg <"memd", "STrid", DoubleRegs, 0b110,
767                                    DoubleWordAccess>;
768
769 let AddedComplexity = 40 in
770 multiclass T_StoreAbsReg_Pats <InstHexagon MI, RegisterClass RC, ValueType VT,
771                            PatFrag stOp> {
772  def : Pat<(stOp (VT RC:$src4),
773                  (add (shl (i32 IntRegs:$src1), u2ImmPred:$src2),
774                       u0AlwaysExtPred:$src3)),
775           (MI IntRegs:$src1, u2ImmPred:$src2, u0AlwaysExtPred:$src3, RC:$src4)>;
776
777  def : Pat<(stOp (VT RC:$src4),
778                  (add (shl IntRegs:$src1, u2ImmPred:$src2),
779                       (HexagonCONST32 tglobaladdr:$src3))),
780            (MI IntRegs:$src1, u2ImmPred:$src2, tglobaladdr:$src3, RC:$src4)>;
781
782  def : Pat<(stOp (VT RC:$src4),
783                  (add IntRegs:$src1, (HexagonCONST32 tglobaladdr:$src3))),
784            (MI IntRegs:$src1, 0, tglobaladdr:$src3, RC:$src4)>;
785 }
786
787 defm : T_StoreAbsReg_Pats <S4_storerd_ur, DoubleRegs, i64, store>;
788 defm : T_StoreAbsReg_Pats <S4_storeri_ur, IntRegs, i32, store>;
789 defm : T_StoreAbsReg_Pats <S4_storerb_ur, IntRegs, i32, truncstorei8>;
790 defm : T_StoreAbsReg_Pats <S4_storerh_ur, IntRegs, i32, truncstorei16>;
791
792 let mayStore = 1, isNVStore = 1, isExtended = 1, addrMode = BaseLongOffset,
793     opExtentBits = 6, isNewValue = 1, opNewValue = 3, opExtendable = 2 in
794 class T_StoreAbsRegNV <string mnemonic, string CextOp, bits<2> MajOp,
795                        MemAccessSize AccessSz>
796   : NVInst <(outs ),
797             (ins IntRegs:$src1, u2Imm:$src2, u6Ext:$src3, IntRegs:$src4),
798   mnemonic#"($src1<<#$src2 + #$src3) = $src4.new">, NewValueRel {
799     bits<5> src1;
800     bits<2> src2;
801     bits<6> src3;
802     bits<3> src4;
803
804     let CextOpcode  = CextOp;
805     let BaseOpcode  = CextOp#"_shl";
806     let IClass      = 0b1010;
807
808     let Inst{27-21} = 0b1101101;
809     let Inst{12-11} = 0b00;
810     let Inst{7}     = 0b1;
811     let Inst{20-16} = src1;
812     let Inst{13}    = src2{1};
813     let Inst{12-11} = MajOp;
814     let Inst{10-8}  = src4;
815     let Inst{6}     = src2{0};
816     let Inst{5-0}   = src3;
817   }
818
819 def S4_storerbnew_ur : T_StoreAbsRegNV <"memb", "STrib", 0b00, ByteAccess>;
820 def S4_storerhnew_ur : T_StoreAbsRegNV <"memh", "STrih", 0b01, HalfWordAccess>;
821 def S4_storerinew_ur : T_StoreAbsRegNV <"memw", "STriw", 0b10, WordAccess>;
822
823 //===----------------------------------------------------------------------===//
824 // Template classes for the non-predicated store instructions with
825 // base + register offset addressing mode
826 //===----------------------------------------------------------------------===//
827 let isPredicable = 1 in
828 class T_store_rr <string mnemonic, RegisterClass RC, bits<3> MajOp, bit isH>
829   : STInst < (outs ), (ins IntRegs:$Rs, IntRegs:$Ru, u2Imm:$u2, RC:$Rt),
830   mnemonic#"($Rs + $Ru<<#$u2) = $Rt"#!if(isH, ".h",""),
831   [],"",V4LDST_tc_st_SLOT01>, ImmRegShl, AddrModeRel {
832
833     bits<5> Rs;
834     bits<5> Ru;
835     bits<2> u2;
836     bits<5> Rt;
837
838     let IClass = 0b0011;
839
840     let Inst{27-24} = 0b1011;
841     let Inst{23-21} = MajOp;
842     let Inst{20-16} = Rs;
843     let Inst{12-8}  = Ru;
844     let Inst{13}    = u2{1};
845     let Inst{7}     = u2{0};
846     let Inst{4-0}   = Rt;
847   }
848
849 //===----------------------------------------------------------------------===//
850 // Template classes for the predicated store instructions with
851 // base + register offset addressing mode
852 //===----------------------------------------------------------------------===//
853 let isPredicated = 1 in
854 class T_pstore_rr <string mnemonic, RegisterClass RC, bits<3> MajOp,
855                    bit isNot, bit isPredNew, bit isH>
856   : STInst <(outs),
857             (ins PredRegs:$Pv, IntRegs:$Rs, IntRegs:$Ru, u2Imm:$u2, RC:$Rt),
858
859   !if(isNot, "if (!$Pv", "if ($Pv")#!if(isPredNew, ".new) ",
860   ") ")#mnemonic#"($Rs+$Ru<<#$u2) = $Rt"#!if(isH, ".h",""),
861   [], "", V4LDST_tc_st_SLOT01> , AddrModeRel{
862     bits<2> Pv;
863     bits<5> Rs;
864     bits<5> Ru;
865     bits<2> u2;
866     bits<5> Rt;
867
868     let isPredicatedFalse = isNot;
869     let isPredicatedNew = isPredNew;
870
871     let IClass = 0b0011;
872
873     let Inst{27-26} = 0b01;
874     let Inst{25}    = isPredNew;
875     let Inst{24}    = isNot;
876     let Inst{23-21} = MajOp;
877     let Inst{20-16} = Rs;
878     let Inst{12-8}  = Ru;
879     let Inst{13}    = u2{1};
880     let Inst{7}     = u2{0};
881     let Inst{6-5}   = Pv;
882     let Inst{4-0}   = Rt;
883   }
884
885 //===----------------------------------------------------------------------===//
886 // Template classes for the new-value store instructions with
887 // base + register offset addressing mode
888 //===----------------------------------------------------------------------===//
889 let isPredicable = 1, isNewValue = 1, opNewValue = 3 in
890 class T_store_new_rr <string mnemonic, bits<2> MajOp> :
891   NVInst < (outs ), (ins IntRegs:$Rs, IntRegs:$Ru, u2Imm:$u2, IntRegs:$Nt),
892   mnemonic#"($Rs + $Ru<<#$u2) = $Nt.new",
893   [],"",V4LDST_tc_st_SLOT0>, ImmRegShl, AddrModeRel {
894
895     bits<5> Rs;
896     bits<5> Ru;
897     bits<2> u2;
898     bits<3> Nt;
899
900     let IClass = 0b0011;
901
902     let Inst{27-21} = 0b1011101;
903     let Inst{20-16} = Rs;
904     let Inst{12-8}  = Ru;
905     let Inst{13}    = u2{1};
906     let Inst{7}     = u2{0};
907     let Inst{4-3}   = MajOp;
908     let Inst{2-0}   = Nt;
909   }
910
911 //===----------------------------------------------------------------------===//
912 // Template classes for the predicated new-value store instructions with
913 // base + register offset addressing mode
914 //===----------------------------------------------------------------------===//
915 let isPredicated = 1, isNewValue = 1, opNewValue = 4 in
916 class T_pstore_new_rr <string mnemonic, bits<2> MajOp, bit isNot, bit isPredNew>
917   : NVInst<(outs),
918            (ins PredRegs:$Pv, IntRegs:$Rs, IntRegs:$Ru, u2Imm:$u2, IntRegs:$Nt),
919    !if(isNot, "if (!$Pv", "if ($Pv")#!if(isPredNew, ".new) ",
920    ") ")#mnemonic#"($Rs+$Ru<<#$u2) = $Nt.new",
921    [], "", V4LDST_tc_st_SLOT0>, AddrModeRel {
922     bits<2> Pv;
923     bits<5> Rs;
924     bits<5> Ru;
925     bits<2> u2;
926     bits<3> Nt;
927
928     let isPredicatedFalse = isNot;
929     let isPredicatedNew = isPredNew;
930
931     let IClass = 0b0011;
932     let Inst{27-26} = 0b01;
933     let Inst{25}    = isPredNew;
934     let Inst{24}    = isNot;
935     let Inst{23-21} = 0b101;
936     let Inst{20-16} = Rs;
937     let Inst{12-8}  = Ru;
938     let Inst{13}    = u2{1};
939     let Inst{7}     = u2{0};
940     let Inst{6-5}   = Pv;
941     let Inst{4-3}   = MajOp;
942     let Inst{2-0}   = Nt;
943   }
944
945 //===----------------------------------------------------------------------===//
946 // multiclass for store instructions with base + register offset addressing
947 // mode
948 //===----------------------------------------------------------------------===//
949 let isNVStorable = 1 in
950 multiclass ST_Idxd_shl<string mnemonic, string CextOp, RegisterClass RC,
951                        bits<3> MajOp, bit isH = 0> {
952   let CextOpcode = CextOp, BaseOpcode = CextOp#_indexed_shl in {
953     def S4_#NAME#_rr : T_store_rr <mnemonic, RC, MajOp, isH>;
954
955     // Predicated
956     def S4_p#NAME#t_rr : T_pstore_rr <mnemonic, RC, MajOp, 0, 0, isH>;
957     def S4_p#NAME#f_rr : T_pstore_rr <mnemonic, RC, MajOp, 1, 0, isH>;
958
959     // Predicated new
960     def S4_p#NAME#tnew_rr : T_pstore_rr <mnemonic, RC, MajOp, 0, 1, isH>;
961     def S4_p#NAME#fnew_rr : T_pstore_rr <mnemonic, RC, MajOp, 1, 1, isH>;
962   }
963 }
964
965 //===----------------------------------------------------------------------===//
966 // multiclass for new-value store instructions with base + register offset
967 // addressing mode.
968 //===----------------------------------------------------------------------===//
969 let mayStore = 1, isNVStore = 1 in
970 multiclass ST_Idxd_shl_nv <string mnemonic, string CextOp, RegisterClass RC,
971                            bits<2> MajOp> {
972   let CextOpcode = CextOp, BaseOpcode = CextOp#_indexed_shl in {
973     def S4_#NAME#new_rr : T_store_new_rr<mnemonic, MajOp>;
974
975     // Predicated
976     def S4_p#NAME#newt_rr : T_pstore_new_rr <mnemonic, MajOp, 0, 0>;
977     def S4_p#NAME#newf_rr : T_pstore_new_rr <mnemonic, MajOp, 1, 0>;
978
979     // Predicated new
980     def S4_p#NAME#newtnew_rr : T_pstore_new_rr <mnemonic, MajOp, 0, 1>;
981     def S4_p#NAME#newfnew_rr : T_pstore_new_rr <mnemonic, MajOp, 1, 1>;
982   }
983 }
984
985 let addrMode = BaseRegOffset, InputType = "reg", hasSideEffects = 0 in {
986   let accessSize = ByteAccess in
987   defm storerb: ST_Idxd_shl<"memb", "STrib", IntRegs, 0b000>,
988                 ST_Idxd_shl_nv<"memb", "STrib", IntRegs, 0b00>;
989
990   let accessSize = HalfWordAccess in
991   defm storerh: ST_Idxd_shl<"memh", "STrih", IntRegs, 0b010>,
992                 ST_Idxd_shl_nv<"memh", "STrih", IntRegs, 0b01>;
993
994   let accessSize = WordAccess in
995   defm storeri: ST_Idxd_shl<"memw", "STriw", IntRegs, 0b100>,
996                 ST_Idxd_shl_nv<"memw", "STriw", IntRegs, 0b10>;
997
998   let isNVStorable = 0, accessSize = DoubleWordAccess in
999   defm storerd: ST_Idxd_shl<"memd", "STrid", DoubleRegs, 0b110>;
1000
1001   let isNVStorable = 0, accessSize = HalfWordAccess in
1002   defm storerf: ST_Idxd_shl<"memh", "STrif", IntRegs, 0b011, 1>;
1003 }
1004
1005 class Storexs_pat<PatFrag Store, PatFrag Value, InstHexagon MI>
1006   : Pat<(Store Value:$Ru, (add (i32 IntRegs:$Rs),
1007                                (i32 (shl (i32 IntRegs:$Rt), u2ImmPred:$u2)))),
1008         (MI IntRegs:$Rs, IntRegs:$Rt, imm:$u2, Value:$Ru)>;
1009
1010 let AddedComplexity = 40 in {
1011   def: Storexs_pat<truncstorei8,  I32, S4_storerb_rr>;
1012   def: Storexs_pat<truncstorei16, I32, S4_storerh_rr>;
1013   def: Storexs_pat<store,         I32, S4_storeri_rr>;
1014   def: Storexs_pat<store,         I64, S4_storerd_rr>;
1015 }
1016
1017 // memd(Rx++#s4:3)=Rtt
1018 // memd(Rx++#s4:3:circ(Mu))=Rtt
1019 // memd(Rx++I:circ(Mu))=Rtt
1020 // memd(Rx++Mu)=Rtt
1021 // memd(Rx++Mu:brev)=Rtt
1022 // memd(gp+#u16:3)=Rtt
1023
1024 // Store doubleword conditionally.
1025 // if ([!]Pv[.new]) memd(#u6)=Rtt
1026 // TODO: needs to be implemented.
1027
1028 //===----------------------------------------------------------------------===//
1029 // Template class
1030 //===----------------------------------------------------------------------===//
1031 let isPredicable = 1, isExtendable = 1, isExtentSigned = 1, opExtentBits = 8,
1032     opExtendable = 2 in
1033 class T_StoreImm <string mnemonic, Operand OffsetOp, bits<2> MajOp >
1034   : STInst <(outs ), (ins IntRegs:$Rs, OffsetOp:$offset, s8Ext:$S8),
1035   mnemonic#"($Rs+#$offset)=#$S8",
1036   [], "", V4LDST_tc_st_SLOT01>,
1037   ImmRegRel, PredNewRel {
1038     bits<5> Rs;
1039     bits<8> S8;
1040     bits<8> offset;
1041     bits<6> offsetBits;
1042
1043     string OffsetOpStr = !cast<string>(OffsetOp);
1044     let offsetBits = !if (!eq(OffsetOpStr, "u6_2Imm"), offset{7-2},
1045                      !if (!eq(OffsetOpStr, "u6_1Imm"), offset{6-1},
1046                                          /* u6_0Imm */ offset{5-0}));
1047
1048     let IClass = 0b0011;
1049
1050     let Inst{27-25} = 0b110;
1051     let Inst{22-21} = MajOp;
1052     let Inst{20-16} = Rs;
1053     let Inst{12-7}  = offsetBits;
1054     let Inst{13}    = S8{7};
1055     let Inst{6-0}   = S8{6-0};
1056   }
1057
1058 let isPredicated = 1, isExtendable = 1, isExtentSigned = 1, opExtentBits = 6,
1059     opExtendable = 3 in
1060 class T_StoreImm_pred <string mnemonic, Operand OffsetOp, bits<2> MajOp,
1061                        bit isPredNot, bit isPredNew >
1062   : STInst <(outs ),
1063             (ins PredRegs:$Pv, IntRegs:$Rs, OffsetOp:$offset, s6Ext:$S6),
1064   !if(isPredNot, "if (!$Pv", "if ($Pv")#!if(isPredNew, ".new) ",
1065   ") ")#mnemonic#"($Rs+#$offset)=#$S6",
1066   [], "", V4LDST_tc_st_SLOT01>,
1067   ImmRegRel, PredNewRel {
1068     bits<2> Pv;
1069     bits<5> Rs;
1070     bits<6> S6;
1071     bits<8> offset;
1072     bits<6> offsetBits;
1073
1074     string OffsetOpStr = !cast<string>(OffsetOp);
1075     let offsetBits = !if (!eq(OffsetOpStr, "u6_2Imm"), offset{7-2},
1076                      !if (!eq(OffsetOpStr, "u6_1Imm"), offset{6-1},
1077                                          /* u6_0Imm */ offset{5-0}));
1078     let isPredicatedNew = isPredNew;
1079     let isPredicatedFalse = isPredNot;
1080
1081     let IClass = 0b0011;
1082
1083     let Inst{27-25} = 0b100;
1084     let Inst{24}    = isPredNew;
1085     let Inst{23}    = isPredNot;
1086     let Inst{22-21} = MajOp;
1087     let Inst{20-16} = Rs;
1088     let Inst{13}    = S6{5};
1089     let Inst{12-7}  = offsetBits;
1090     let Inst{6-5}   = Pv;
1091     let Inst{4-0}   = S6{4-0};
1092   }
1093
1094
1095 //===----------------------------------------------------------------------===//
1096 // multiclass for store instructions with base + immediate offset
1097 // addressing mode and immediate stored value.
1098 // mem[bhw](Rx++#s4:3)=#s8
1099 // if ([!]Pv[.new]) mem[bhw](Rx++#s4:3)=#s6
1100 //===----------------------------------------------------------------------===//
1101
1102 multiclass ST_Imm_Pred <string mnemonic, Operand OffsetOp, bits<2> MajOp,
1103                         bit PredNot> {
1104   def _io    : T_StoreImm_pred <mnemonic, OffsetOp, MajOp, PredNot, 0>;
1105   // Predicate new
1106   def new_io : T_StoreImm_pred <mnemonic, OffsetOp, MajOp, PredNot, 1>;
1107 }
1108
1109 multiclass ST_Imm <string mnemonic, string CextOp, Operand OffsetOp,
1110                    bits<2> MajOp> {
1111   let CextOpcode = CextOp, BaseOpcode = CextOp#_imm in {
1112     def _io : T_StoreImm <mnemonic, OffsetOp, MajOp>;
1113
1114     defm t : ST_Imm_Pred <mnemonic, OffsetOp, MajOp, 0>;
1115     defm f : ST_Imm_Pred <mnemonic, OffsetOp, MajOp, 1>;
1116   }
1117 }
1118
1119 let hasSideEffects = 0, addrMode = BaseImmOffset,
1120     InputType = "imm" in {
1121   let accessSize = ByteAccess in
1122   defm S4_storeirb : ST_Imm<"memb", "STrib", u6_0Imm, 0b00>;
1123
1124   let accessSize = HalfWordAccess in
1125   defm S4_storeirh : ST_Imm<"memh", "STrih", u6_1Imm, 0b01>;
1126
1127   let accessSize = WordAccess in
1128   defm S4_storeiri : ST_Imm<"memw", "STriw", u6_2Imm, 0b10>;
1129 }
1130
1131 def IMM_BYTE : SDNodeXForm<imm, [{
1132   // -1 etc is  represented as 255 etc
1133   // assigning to a byte restores our desired signed value.
1134   int8_t imm = N->getSExtValue();
1135   return CurDAG->getTargetConstant(imm, MVT::i32);
1136 }]>;
1137
1138 def IMM_HALF : SDNodeXForm<imm, [{
1139   // -1 etc is  represented as 65535 etc
1140   // assigning to a short restores our desired signed value.
1141   int16_t imm = N->getSExtValue();
1142   return CurDAG->getTargetConstant(imm, MVT::i32);
1143 }]>;
1144
1145 def IMM_WORD : SDNodeXForm<imm, [{
1146   // -1 etc can be represented as 4294967295 etc
1147   // Currently, it's not doing this. But some optimization
1148   // might convert -1 to a large +ve number.
1149   // assigning to a word restores our desired signed value.
1150   int32_t imm = N->getSExtValue();
1151   return CurDAG->getTargetConstant(imm, MVT::i32);
1152 }]>;
1153
1154 def ToImmByte : OutPatFrag<(ops node:$R), (IMM_BYTE $R)>;
1155 def ToImmHalf : OutPatFrag<(ops node:$R), (IMM_HALF $R)>;
1156 def ToImmWord : OutPatFrag<(ops node:$R), (IMM_WORD $R)>;
1157
1158 let AddedComplexity = 40 in {
1159   // Not using frameindex patterns for these stores, because the offset
1160   // is not extendable. This could cause problems during removing the frame
1161   // indices, since the offset with respect to R29/R30 may not fit in the
1162   // u6 field.
1163   def: Storexm_add_pat<truncstorei8, s8ExtPred, u6_0ImmPred, ToImmByte,
1164                        S4_storeirb_io>;
1165   def: Storexm_add_pat<truncstorei16, s8ExtPred, u6_1ImmPred, ToImmHalf,
1166                        S4_storeirh_io>;
1167   def: Storexm_add_pat<store, s8ExtPred, u6_2ImmPred, ToImmWord,
1168                        S4_storeiri_io>;
1169 }
1170
1171 def: Storexm_simple_pat<truncstorei8,  s8ExtPred, ToImmByte, S4_storeirb_io>;
1172 def: Storexm_simple_pat<truncstorei16, s8ExtPred, ToImmHalf, S4_storeirh_io>;
1173 def: Storexm_simple_pat<store,         s8ExtPred, ToImmWord, S4_storeiri_io>;
1174
1175 // memb(Rx++#s4:0:circ(Mu))=Rt
1176 // memb(Rx++I:circ(Mu))=Rt
1177 // memb(Rx++Mu)=Rt
1178 // memb(Rx++Mu:brev)=Rt
1179 // memb(gp+#u16:0)=Rt
1180
1181 // Store halfword.
1182 // TODO: needs to be implemented
1183 // memh(Re=#U6)=Rt.H
1184 // memh(Rs+#s11:1)=Rt.H
1185 // memh(Rs+Ru<<#u2)=Rt.H
1186 // TODO: needs to be implemented.
1187
1188 // memh(Ru<<#u2+#U6)=Rt.H
1189 // memh(Rx++#s4:1:circ(Mu))=Rt.H
1190 // memh(Rx++#s4:1:circ(Mu))=Rt
1191 // memh(Rx++I:circ(Mu))=Rt.H
1192 // memh(Rx++I:circ(Mu))=Rt
1193 // memh(Rx++Mu)=Rt.H
1194 // memh(Rx++Mu)=Rt
1195 // memh(Rx++Mu:brev)=Rt.H
1196 // memh(Rx++Mu:brev)=Rt
1197 // memh(gp+#u16:1)=Rt
1198 // if ([!]Pv[.new]) memh(#u6)=Rt.H
1199 // if ([!]Pv[.new]) memh(#u6)=Rt
1200
1201 // if ([!]Pv[.new]) memh(Rs+#u6:1)=Rt.H
1202 // TODO: needs to be implemented.
1203
1204 // if ([!]Pv[.new]) memh(Rx++#s4:1)=Rt.H
1205 // TODO: Needs to be implemented.
1206
1207 // Store word.
1208 // memw(Re=#U6)=Rt
1209 // TODO: Needs to be implemented.
1210 // memw(Rx++#s4:2)=Rt
1211 // memw(Rx++#s4:2:circ(Mu))=Rt
1212 // memw(Rx++I:circ(Mu))=Rt
1213 // memw(Rx++Mu)=Rt
1214 // memw(Rx++Mu:brev)=Rt
1215
1216 //===----------------------------------------------------------------------===
1217 // ST -
1218 //===----------------------------------------------------------------------===
1219
1220
1221 //===----------------------------------------------------------------------===//
1222 // NV/ST +
1223 //===----------------------------------------------------------------------===//
1224
1225 let opNewValue = 2, opExtendable = 1, isExtentSigned = 1, isPredicable = 1 in
1226 class T_store_io_nv <string mnemonic, RegisterClass RC,
1227                     Operand ImmOp, bits<2>MajOp>
1228   : NVInst_V4 <(outs),
1229                (ins IntRegs:$src1, ImmOp:$src2, RC:$src3),
1230   mnemonic#"($src1+#$src2) = $src3.new",
1231   [],"",ST_tc_st_SLOT0> {
1232     bits<5> src1;
1233     bits<13> src2; // Actual address offset
1234     bits<3> src3;
1235     bits<11> offsetBits; // Represents offset encoding
1236
1237     let opExtentBits = !if (!eq(mnemonic, "memb"), 11,
1238                        !if (!eq(mnemonic, "memh"), 12,
1239                        !if (!eq(mnemonic, "memw"), 13, 0)));
1240
1241     let opExtentAlign = !if (!eq(mnemonic, "memb"), 0,
1242                         !if (!eq(mnemonic, "memh"), 1,
1243                         !if (!eq(mnemonic, "memw"), 2, 0)));
1244
1245     let offsetBits = !if (!eq(mnemonic, "memb"),  src2{10-0},
1246                      !if (!eq(mnemonic, "memh"),  src2{11-1},
1247                      !if (!eq(mnemonic, "memw"),  src2{12-2}, 0)));
1248
1249     let IClass = 0b1010;
1250
1251     let Inst{27} = 0b0;
1252     let Inst{26-25} = offsetBits{10-9};
1253     let Inst{24-21} = 0b1101;
1254     let Inst{20-16} = src1;
1255     let Inst{13} = offsetBits{8};
1256     let Inst{12-11} = MajOp;
1257     let Inst{10-8} = src3;
1258     let Inst{7-0} = offsetBits{7-0};
1259   }
1260
1261 let opExtendable = 2, opNewValue = 3, isPredicated = 1 in
1262 class T_pstore_io_nv <string mnemonic, RegisterClass RC, Operand predImmOp,
1263                          bits<2>MajOp, bit PredNot, bit isPredNew>
1264   : NVInst_V4 <(outs),
1265                (ins PredRegs:$src1, IntRegs:$src2, predImmOp:$src3, RC:$src4),
1266   !if(PredNot, "if (!$src1", "if ($src1")#!if(isPredNew, ".new) ",
1267   ") ")#mnemonic#"($src2+#$src3) = $src4.new",
1268   [],"",V2LDST_tc_st_SLOT0> {
1269     bits<2> src1;
1270     bits<5> src2;
1271     bits<9> src3;
1272     bits<3> src4;
1273     bits<6> offsetBits; // Represents offset encoding
1274
1275     let isPredicatedNew = isPredNew;
1276     let isPredicatedFalse = PredNot;
1277     let opExtentBits = !if (!eq(mnemonic, "memb"), 6,
1278                        !if (!eq(mnemonic, "memh"), 7,
1279                        !if (!eq(mnemonic, "memw"), 8, 0)));
1280
1281     let opExtentAlign = !if (!eq(mnemonic, "memb"), 0,
1282                         !if (!eq(mnemonic, "memh"), 1,
1283                         !if (!eq(mnemonic, "memw"), 2, 0)));
1284
1285     let offsetBits = !if (!eq(mnemonic, "memb"), src3{5-0},
1286                      !if (!eq(mnemonic, "memh"), src3{6-1},
1287                      !if (!eq(mnemonic, "memw"), src3{7-2}, 0)));
1288
1289     let IClass = 0b0100;
1290
1291     let Inst{27}    = 0b0;
1292     let Inst{26}    = PredNot;
1293     let Inst{25}    = isPredNew;
1294     let Inst{24-21} = 0b0101;
1295     let Inst{20-16} = src2;
1296     let Inst{13}    = offsetBits{5};
1297     let Inst{12-11} = MajOp;
1298     let Inst{10-8}  = src4;
1299     let Inst{7-3}   = offsetBits{4-0};
1300     let Inst{2}     = 0b0;
1301     let Inst{1-0}   = src1;
1302   }
1303
1304 // multiclass for new-value store instructions with base + immediate offset.
1305 //
1306 let mayStore = 1, isNVStore = 1, isNewValue = 1, hasSideEffects = 0,
1307     isExtendable = 1 in
1308 multiclass ST_Idxd_nv<string mnemonic, string CextOp, RegisterClass RC,
1309                    Operand ImmOp, Operand predImmOp, bits<2> MajOp> {
1310
1311   let CextOpcode = CextOp, BaseOpcode = CextOp#_indexed in {
1312     def S2_#NAME#new_io : T_store_io_nv <mnemonic, RC, ImmOp, MajOp>;
1313     // Predicated
1314     def S2_p#NAME#newt_io :T_pstore_io_nv <mnemonic, RC, predImmOp, MajOp, 0, 0>;
1315     def S2_p#NAME#newf_io :T_pstore_io_nv <mnemonic, RC, predImmOp, MajOp, 1, 0>;
1316     // Predicated new
1317     def S4_p#NAME#newtnew_io :T_pstore_io_nv <mnemonic, RC, predImmOp,
1318                                               MajOp, 0, 1>;
1319     def S4_p#NAME#newfnew_io :T_pstore_io_nv <mnemonic, RC, predImmOp,
1320                                               MajOp, 1, 1>;
1321   }
1322 }
1323
1324 let addrMode = BaseImmOffset, InputType = "imm" in {
1325   let accessSize = ByteAccess in
1326   defm storerb: ST_Idxd_nv<"memb", "STrib", IntRegs, s11_0Ext,
1327                            u6_0Ext, 0b00>, AddrModeRel;
1328
1329   let accessSize = HalfWordAccess, opExtentAlign = 1 in
1330   defm storerh: ST_Idxd_nv<"memh", "STrih", IntRegs, s11_1Ext,
1331                            u6_1Ext, 0b01>, AddrModeRel;
1332
1333   let accessSize = WordAccess, opExtentAlign = 2 in
1334   defm storeri: ST_Idxd_nv<"memw", "STriw", IntRegs, s11_2Ext,
1335                            u6_2Ext, 0b10>, AddrModeRel;
1336 }
1337
1338 //===----------------------------------------------------------------------===//
1339 // Post increment loads with register offset.
1340 //===----------------------------------------------------------------------===//
1341
1342 let hasNewValue = 1 in
1343 def L2_loadbsw2_pr : T_load_pr <"membh", IntRegs, 0b0001, HalfWordAccess>;
1344
1345 def L2_loadbsw4_pr : T_load_pr <"membh", DoubleRegs, 0b0111, WordAccess>;
1346
1347 let hasSideEffects = 0, addrMode = PostInc in
1348 class T_loadalign_pr <string mnemonic, bits<4> MajOp, MemAccessSize AccessSz>
1349   : LDInstPI <(outs DoubleRegs:$dst, IntRegs:$_dst_),
1350               (ins DoubleRegs:$src1, IntRegs:$src2, ModRegs:$src3),
1351   "$dst = "#mnemonic#"($src2++$src3)", [],
1352   "$src1 = $dst, $src2 = $_dst_"> {
1353     bits<5> dst;
1354     bits<5> src2;
1355     bits<1> src3;
1356
1357     let accessSize = AccessSz;
1358     let IClass = 0b1001;
1359
1360     let Inst{27-25} = 0b110;
1361     let Inst{24-21} = MajOp;
1362     let Inst{20-16} = src2;
1363     let Inst{13}    = src3;
1364     let Inst{12}    = 0b0;
1365     let Inst{7}     = 0b0;
1366     let Inst{4-0}   = dst;
1367   }
1368
1369 def L2_loadalignb_pr : T_loadalign_pr <"memb_fifo", 0b0100, ByteAccess>;
1370 def L2_loadalignh_pr : T_loadalign_pr <"memh_fifo", 0b0010, HalfWordAccess>;
1371
1372 //===----------------------------------------------------------------------===//
1373 // Template class for non-predicated post increment .new stores
1374 // mem[bhwd](Rx++#s4:[0123])=Nt.new
1375 //===----------------------------------------------------------------------===//
1376 let isPredicable = 1, hasSideEffects = 0, addrMode = PostInc, isNVStore = 1,
1377     isNewValue = 1, opNewValue = 3 in
1378 class T_StorePI_nv <string mnemonic, Operand ImmOp, bits<2> MajOp >
1379   : NVInstPI_V4 <(outs IntRegs:$_dst_),
1380                  (ins IntRegs:$src1, ImmOp:$offset, IntRegs:$src2),
1381   mnemonic#"($src1++#$offset) = $src2.new",
1382   [], "$src1 = $_dst_">,
1383   AddrModeRel {
1384     bits<5> src1;
1385     bits<3> src2;
1386     bits<7> offset;
1387     bits<4> offsetBits;
1388
1389     string ImmOpStr = !cast<string>(ImmOp);
1390     let offsetBits = !if (!eq(ImmOpStr, "s4_2Imm"), offset{5-2},
1391                      !if (!eq(ImmOpStr, "s4_1Imm"), offset{4-1},
1392                                       /* s4_0Imm */ offset{3-0}));
1393     let IClass = 0b1010;
1394
1395     let Inst{27-21} = 0b1011101;
1396     let Inst{20-16} = src1;
1397     let Inst{13} = 0b0;
1398     let Inst{12-11} = MajOp;
1399     let Inst{10-8} = src2;
1400     let Inst{7} = 0b0;
1401     let Inst{6-3} = offsetBits;
1402     let Inst{1} = 0b0;
1403   }
1404
1405 //===----------------------------------------------------------------------===//
1406 // Template class for predicated post increment .new stores
1407 // if([!]Pv[.new]) mem[bhwd](Rx++#s4:[0123])=Nt.new
1408 //===----------------------------------------------------------------------===//
1409 let isPredicated = 1, hasSideEffects = 0, addrMode = PostInc, isNVStore = 1,
1410     isNewValue = 1, opNewValue = 4 in
1411 class T_StorePI_nv_pred <string mnemonic, Operand ImmOp,
1412                          bits<2> MajOp, bit isPredNot, bit isPredNew >
1413   : NVInstPI_V4 <(outs IntRegs:$_dst_),
1414                  (ins PredRegs:$src1, IntRegs:$src2,
1415                       ImmOp:$offset, IntRegs:$src3),
1416   !if(isPredNot, "if (!$src1", "if ($src1")#!if(isPredNew, ".new) ",
1417   ") ")#mnemonic#"($src2++#$offset) = $src3.new",
1418   [], "$src2 = $_dst_">,
1419   AddrModeRel {
1420     bits<2> src1;
1421     bits<5> src2;
1422     bits<3> src3;
1423     bits<7> offset;
1424     bits<4> offsetBits;
1425
1426     string ImmOpStr = !cast<string>(ImmOp);
1427     let offsetBits = !if (!eq(ImmOpStr, "s4_2Imm"), offset{5-2},
1428                      !if (!eq(ImmOpStr, "s4_1Imm"), offset{4-1},
1429                                       /* s4_0Imm */ offset{3-0}));
1430     let isPredicatedNew = isPredNew;
1431     let isPredicatedFalse = isPredNot;
1432
1433     let IClass = 0b1010;
1434
1435     let Inst{27-21} = 0b1011101;
1436     let Inst{20-16} = src2;
1437     let Inst{13} = 0b1;
1438     let Inst{12-11} = MajOp;
1439     let Inst{10-8} = src3;
1440     let Inst{7} = isPredNew;
1441     let Inst{6-3} = offsetBits;
1442     let Inst{2} = isPredNot;
1443     let Inst{1-0} = src1;
1444   }
1445
1446 multiclass ST_PostInc_Pred_nv<string mnemonic, Operand ImmOp,
1447                               bits<2> MajOp, bit PredNot> {
1448   def _pi : T_StorePI_nv_pred <mnemonic, ImmOp, MajOp, PredNot, 0>;
1449
1450   // Predicate new
1451   def new_pi : T_StorePI_nv_pred <mnemonic, ImmOp, MajOp, PredNot, 1>;
1452 }
1453
1454 multiclass ST_PostInc_nv<string mnemonic, string BaseOp, Operand ImmOp,
1455                          bits<2> MajOp> {
1456   let BaseOpcode = "POST_"#BaseOp in {
1457     def S2_#NAME#_pi : T_StorePI_nv <mnemonic, ImmOp, MajOp>;
1458
1459     // Predicated
1460     defm S2_p#NAME#t : ST_PostInc_Pred_nv <mnemonic, ImmOp, MajOp, 0>;
1461     defm S2_p#NAME#f : ST_PostInc_Pred_nv <mnemonic, ImmOp, MajOp, 1>;
1462   }
1463 }
1464
1465 let accessSize = ByteAccess in
1466 defm storerbnew: ST_PostInc_nv <"memb", "STrib", s4_0Imm, 0b00>;
1467
1468 let accessSize = HalfWordAccess in
1469 defm storerhnew: ST_PostInc_nv <"memh", "STrih", s4_1Imm, 0b01>;
1470
1471 let accessSize = WordAccess in
1472 defm storerinew: ST_PostInc_nv <"memw", "STriw", s4_2Imm, 0b10>;
1473
1474 //===----------------------------------------------------------------------===//
1475 // Template class for post increment .new stores with register offset
1476 //===----------------------------------------------------------------------===//
1477 let isNewValue = 1, mayStore = 1, isNVStore = 1, opNewValue = 3 in
1478 class T_StorePI_RegNV <string mnemonic, bits<2> MajOp, MemAccessSize AccessSz>
1479   : NVInstPI_V4 <(outs IntRegs:$_dst_),
1480                  (ins IntRegs:$src1, ModRegs:$src2, IntRegs:$src3),
1481   #mnemonic#"($src1++$src2) = $src3.new",
1482   [], "$src1 = $_dst_"> {
1483     bits<5> src1;
1484     bits<1> src2;
1485     bits<3> src3;
1486     let accessSize = AccessSz;
1487
1488     let IClass = 0b1010;
1489
1490     let Inst{27-21} = 0b1101101;
1491     let Inst{20-16} = src1;
1492     let Inst{13}    = src2;
1493     let Inst{12-11} = MajOp;
1494     let Inst{10-8}  = src3;
1495     let Inst{7}     = 0b0;
1496   }
1497
1498 def S2_storerbnew_pr : T_StorePI_RegNV<"memb", 0b00, ByteAccess>;
1499 def S2_storerhnew_pr : T_StorePI_RegNV<"memh", 0b01, HalfWordAccess>;
1500 def S2_storerinew_pr : T_StorePI_RegNV<"memw", 0b10, WordAccess>;
1501
1502 // memb(Rx++#s4:0:circ(Mu))=Nt.new
1503 // memb(Rx++I:circ(Mu))=Nt.new
1504 // memb(Rx++Mu:brev)=Nt.new
1505 // memh(Rx++#s4:1:circ(Mu))=Nt.new
1506 // memh(Rx++I:circ(Mu))=Nt.new
1507 // memh(Rx++Mu)=Nt.new
1508 // memh(Rx++Mu:brev)=Nt.new
1509
1510 // memw(Rx++#s4:2:circ(Mu))=Nt.new
1511 // memw(Rx++I:circ(Mu))=Nt.new
1512 // memw(Rx++Mu)=Nt.new
1513 // memw(Rx++Mu:brev)=Nt.new
1514
1515 //===----------------------------------------------------------------------===//
1516 // NV/ST -
1517 //===----------------------------------------------------------------------===//
1518
1519 //===----------------------------------------------------------------------===//
1520 // NV/J +
1521 //===----------------------------------------------------------------------===//
1522
1523 //===----------------------------------------------------------------------===//
1524 // multiclass/template class for the new-value compare jumps with the register
1525 // operands.
1526 //===----------------------------------------------------------------------===//
1527
1528 let isExtendable = 1, opExtendable = 2, isExtentSigned = 1, opExtentBits = 11,
1529     opExtentAlign = 2 in
1530 class NVJrr_template<string mnemonic, bits<3> majOp, bit NvOpNum,
1531                       bit isNegCond, bit isTak>
1532   : NVInst_V4<(outs),
1533     (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset),
1534     "if ("#!if(isNegCond, "!","")#mnemonic#
1535     "($src1"#!if(!eq(NvOpNum, 0),".new, ",", ")#
1536     "$src2"#!if(!eq(NvOpNum, 1),".new))","))")#" jump:"
1537     #!if(isTak, "t","nt")#" $offset", []> {
1538
1539       bits<5> src1;
1540       bits<5> src2;
1541       bits<3> Ns;    // New-Value Operand
1542       bits<5> RegOp; // Non-New-Value Operand
1543       bits<11> offset;
1544
1545       let isTaken = isTak;
1546       let isPredicatedFalse = isNegCond;
1547       let opNewValue{0} = NvOpNum;
1548
1549       let Ns = !if(!eq(NvOpNum, 0), src1{2-0}, src2{2-0});
1550       let RegOp = !if(!eq(NvOpNum, 0), src2, src1);
1551
1552       let IClass = 0b0010;
1553       let Inst{27-26} = 0b00;
1554       let Inst{25-23} = majOp;
1555       let Inst{22} = isNegCond;
1556       let Inst{18-16} = Ns;
1557       let Inst{13} = isTak;
1558       let Inst{12-8} = RegOp;
1559       let Inst{21-20} = offset{10-9};
1560       let Inst{7-1} = offset{8-2};
1561 }
1562
1563
1564 multiclass NVJrr_cond<string mnemonic, bits<3> majOp, bit NvOpNum,
1565                        bit isNegCond> {
1566   // Branch not taken:
1567   def _nt: NVJrr_template<mnemonic, majOp, NvOpNum, isNegCond, 0>;
1568   // Branch taken:
1569   def _t : NVJrr_template<mnemonic, majOp, NvOpNum, isNegCond, 1>;
1570 }
1571
1572 // NvOpNum = 0 -> First Operand is a new-value Register
1573 // NvOpNum = 1 -> Second Operand is a new-value Register
1574
1575 multiclass NVJrr_base<string mnemonic, string BaseOp, bits<3> majOp,
1576                        bit NvOpNum> {
1577   let BaseOpcode = BaseOp#_NVJ in {
1578     defm _t_jumpnv : NVJrr_cond<mnemonic, majOp, NvOpNum, 0>; // True cond
1579     defm _f_jumpnv : NVJrr_cond<mnemonic, majOp, NvOpNum, 1>; // False cond
1580   }
1581 }
1582
1583 // if ([!]cmp.eq(Ns.new,Rt)) jump:[n]t #r9:2
1584 // if ([!]cmp.gt(Ns.new,Rt)) jump:[n]t #r9:2
1585 // if ([!]cmp.gtu(Ns.new,Rt)) jump:[n]t #r9:2
1586 // if ([!]cmp.gt(Rt,Ns.new)) jump:[n]t #r9:2
1587 // if ([!]cmp.gtu(Rt,Ns.new)) jump:[n]t #r9:2
1588
1589 let isPredicated = 1, isBranch = 1, isNewValue = 1, isTerminator = 1,
1590     Defs = [PC], hasSideEffects = 0 in {
1591   defm J4_cmpeq  : NVJrr_base<"cmp.eq",  "CMPEQ",  0b000, 0>, PredRel;
1592   defm J4_cmpgt  : NVJrr_base<"cmp.gt",  "CMPGT",  0b001, 0>, PredRel;
1593   defm J4_cmpgtu : NVJrr_base<"cmp.gtu", "CMPGTU", 0b010, 0>, PredRel;
1594   defm J4_cmplt  : NVJrr_base<"cmp.gt",  "CMPLT",  0b011, 1>, PredRel;
1595   defm J4_cmpltu : NVJrr_base<"cmp.gtu", "CMPLTU", 0b100, 1>, PredRel;
1596 }
1597
1598 //===----------------------------------------------------------------------===//
1599 // multiclass/template class for the new-value compare jumps instruction
1600 // with a register and an unsigned immediate (U5) operand.
1601 //===----------------------------------------------------------------------===//
1602
1603 let isExtendable = 1, opExtendable = 2, isExtentSigned = 1, opExtentBits = 11,
1604     opExtentAlign = 2 in
1605 class NVJri_template<string mnemonic, bits<3> majOp, bit isNegCond,
1606                          bit isTak>
1607   : NVInst_V4<(outs),
1608     (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset),
1609     "if ("#!if(isNegCond, "!","")#mnemonic#"($src1.new, #$src2)) jump:"
1610     #!if(isTak, "t","nt")#" $offset", []> {
1611
1612       let isTaken = isTak;
1613       let isPredicatedFalse = isNegCond;
1614       let isTaken = isTak;
1615
1616       bits<3> src1;
1617       bits<5> src2;
1618       bits<11> offset;
1619
1620       let IClass = 0b0010;
1621       let Inst{26} = 0b1;
1622       let Inst{25-23} = majOp;
1623       let Inst{22} = isNegCond;
1624       let Inst{18-16} = src1;
1625       let Inst{13} = isTak;
1626       let Inst{12-8} = src2;
1627       let Inst{21-20} = offset{10-9};
1628       let Inst{7-1} = offset{8-2};
1629 }
1630
1631 multiclass NVJri_cond<string mnemonic, bits<3> majOp, bit isNegCond> {
1632   // Branch not taken:
1633   def _nt: NVJri_template<mnemonic, majOp, isNegCond, 0>;
1634   // Branch taken:
1635   def _t : NVJri_template<mnemonic, majOp, isNegCond, 1>;
1636 }
1637
1638 multiclass NVJri_base<string mnemonic, string BaseOp, bits<3> majOp> {
1639   let BaseOpcode = BaseOp#_NVJri in {
1640     defm _t_jumpnv : NVJri_cond<mnemonic, majOp, 0>; // True Cond
1641     defm _f_jumpnv : NVJri_cond<mnemonic, majOp, 1>; // False cond
1642   }
1643 }
1644
1645 // if ([!]cmp.eq(Ns.new,#U5)) jump:[n]t #r9:2
1646 // if ([!]cmp.gt(Ns.new,#U5)) jump:[n]t #r9:2
1647 // if ([!]cmp.gtu(Ns.new,#U5)) jump:[n]t #r9:2
1648
1649 let isPredicated = 1, isBranch = 1, isNewValue = 1, isTerminator = 1,
1650     Defs = [PC], hasSideEffects = 0 in {
1651   defm J4_cmpeqi  : NVJri_base<"cmp.eq", "CMPEQ", 0b000>, PredRel;
1652   defm J4_cmpgti  : NVJri_base<"cmp.gt", "CMPGT", 0b001>, PredRel;
1653   defm J4_cmpgtui : NVJri_base<"cmp.gtu", "CMPGTU", 0b010>, PredRel;
1654 }
1655
1656 //===----------------------------------------------------------------------===//
1657 // multiclass/template class for the new-value compare jumps instruction
1658 // with a register and an hardcoded 0/-1 immediate value.
1659 //===----------------------------------------------------------------------===//
1660
1661 let isExtendable = 1, opExtendable = 1, isExtentSigned = 1, opExtentBits = 11,
1662     opExtentAlign = 2 in
1663 class NVJ_ConstImm_template<string mnemonic, bits<3> majOp, string ImmVal,
1664                             bit isNegCond, bit isTak>
1665   : NVInst_V4<(outs),
1666     (ins IntRegs:$src1, brtarget:$offset),
1667     "if ("#!if(isNegCond, "!","")#mnemonic
1668     #"($src1.new, #"#ImmVal#")) jump:"
1669     #!if(isTak, "t","nt")#" $offset", []> {
1670
1671       let isTaken = isTak;
1672       let isPredicatedFalse = isNegCond;
1673       let isTaken = isTak;
1674
1675       bits<3> src1;
1676       bits<11> offset;
1677       let IClass = 0b0010;
1678       let Inst{26} = 0b1;
1679       let Inst{25-23} = majOp;
1680       let Inst{22} = isNegCond;
1681       let Inst{18-16} = src1;
1682       let Inst{13} = isTak;
1683       let Inst{21-20} = offset{10-9};
1684       let Inst{7-1} = offset{8-2};
1685 }
1686
1687 multiclass NVJ_ConstImm_cond<string mnemonic, bits<3> majOp, string ImmVal,
1688                              bit isNegCond> {
1689   // Branch not taken:
1690   def _nt: NVJ_ConstImm_template<mnemonic, majOp, ImmVal, isNegCond, 0>;
1691   // Branch taken:
1692   def _t : NVJ_ConstImm_template<mnemonic, majOp, ImmVal, isNegCond, 1>;
1693 }
1694
1695 multiclass NVJ_ConstImm_base<string mnemonic, string BaseOp, bits<3> majOp,
1696                              string ImmVal> {
1697   let BaseOpcode = BaseOp#_NVJ_ConstImm in {
1698     defm _t_jumpnv : NVJ_ConstImm_cond<mnemonic, majOp, ImmVal, 0>; // True
1699     defm _f_jumpnv : NVJ_ConstImm_cond<mnemonic, majOp, ImmVal, 1>; // False
1700   }
1701 }
1702
1703 // if ([!]tstbit(Ns.new,#0)) jump:[n]t #r9:2
1704 // if ([!]cmp.eq(Ns.new,#-1)) jump:[n]t #r9:2
1705 // if ([!]cmp.gt(Ns.new,#-1)) jump:[n]t #r9:2
1706
1707 let isPredicated = 1, isBranch = 1, isNewValue = 1, isTerminator=1,
1708     Defs = [PC], hasSideEffects = 0 in {
1709   defm J4_tstbit0 : NVJ_ConstImm_base<"tstbit", "TSTBIT", 0b011, "0">, PredRel;
1710   defm J4_cmpeqn1 : NVJ_ConstImm_base<"cmp.eq", "CMPEQ",  0b100, "-1">, PredRel;
1711   defm J4_cmpgtn1 : NVJ_ConstImm_base<"cmp.gt", "CMPGT",  0b101, "-1">, PredRel;
1712 }
1713
1714 // J4_hintjumpr: Hint indirect conditional jump.
1715 let isBranch = 1, isIndirectBranch = 1, hasSideEffects = 0 in
1716 def J4_hintjumpr: JRInst <
1717   (outs),
1718   (ins IntRegs:$Rs),
1719   "hintjr($Rs)"> {
1720     bits<5> Rs;
1721     let IClass = 0b0101;
1722     let Inst{27-21} = 0b0010101;
1723     let Inst{20-16} = Rs;
1724   }
1725
1726 //===----------------------------------------------------------------------===//
1727 // NV/J -
1728 //===----------------------------------------------------------------------===//
1729
1730 //===----------------------------------------------------------------------===//
1731 // CR +
1732 //===----------------------------------------------------------------------===//
1733
1734 // PC-relative add
1735 let hasNewValue = 1, isExtendable = 1, opExtendable = 1,
1736     isExtentSigned = 0, opExtentBits = 6, hasSideEffects = 0, Uses = [PC] in
1737 def C4_addipc : CRInst <(outs IntRegs:$Rd), (ins u6Ext:$u6),
1738   "$Rd = add(pc, #$u6)", [], "", CR_tc_2_SLOT3 > {
1739     bits<5> Rd;
1740     bits<6> u6;
1741
1742     let IClass = 0b0110;
1743     let Inst{27-16} = 0b101001001001;
1744     let Inst{12-7} = u6;
1745     let Inst{4-0} = Rd;
1746   }
1747
1748
1749
1750 let hasSideEffects = 0 in
1751 class T_LOGICAL_3OP<string MnOp1, string MnOp2, bits<2> OpBits, bit IsNeg>
1752     : CRInst<(outs PredRegs:$Pd),
1753              (ins PredRegs:$Ps, PredRegs:$Pt, PredRegs:$Pu),
1754              "$Pd = " # MnOp1 # "($Ps, " # MnOp2 # "($Pt, " #
1755                    !if (IsNeg,"!","") # "$Pu))",
1756              [], "", CR_tc_2early_SLOT23> {
1757   bits<2> Pd;
1758   bits<2> Ps;
1759   bits<2> Pt;
1760   bits<2> Pu;
1761
1762   let IClass = 0b0110;
1763   let Inst{27-24} = 0b1011;
1764   let Inst{23} = IsNeg;
1765   let Inst{22-21} = OpBits;
1766   let Inst{20} = 0b1;
1767   let Inst{17-16} = Ps;
1768   let Inst{13} = 0b0;
1769   let Inst{9-8} = Pt;
1770   let Inst{7-6} = Pu;
1771   let Inst{1-0} = Pd;
1772 }
1773
1774 def C4_and_and  : T_LOGICAL_3OP<"and", "and", 0b00, 0>;
1775 def C4_and_or   : T_LOGICAL_3OP<"and", "or",  0b01, 0>;
1776 def C4_or_and   : T_LOGICAL_3OP<"or",  "and", 0b10, 0>;
1777 def C4_or_or    : T_LOGICAL_3OP<"or",  "or",  0b11, 0>;
1778 def C4_and_andn : T_LOGICAL_3OP<"and", "and", 0b00, 1>;
1779 def C4_and_orn  : T_LOGICAL_3OP<"and", "or",  0b01, 1>;
1780 def C4_or_andn  : T_LOGICAL_3OP<"or",  "and", 0b10, 1>;
1781 def C4_or_orn   : T_LOGICAL_3OP<"or",  "or",  0b11, 1>;
1782
1783 // op(Ps, op(Pt, Pu))
1784 class LogLog_pat<SDNode Op1, SDNode Op2, InstHexagon MI>
1785   : Pat<(i1 (Op1 I1:$Ps, (Op2 I1:$Pt, I1:$Pu))),
1786         (MI I1:$Ps, I1:$Pt, I1:$Pu)>;
1787
1788 // op(Ps, op(Pt, ~Pu))
1789 class LogLogNot_pat<SDNode Op1, SDNode Op2, InstHexagon MI>
1790   : Pat<(i1 (Op1 I1:$Ps, (Op2 I1:$Pt, (not I1:$Pu)))),
1791         (MI I1:$Ps, I1:$Pt, I1:$Pu)>;
1792
1793 def: LogLog_pat<and, and, C4_and_and>;
1794 def: LogLog_pat<and, or,  C4_and_or>;
1795 def: LogLog_pat<or,  and, C4_or_and>;
1796 def: LogLog_pat<or,  or,  C4_or_or>;
1797
1798 def: LogLogNot_pat<and, and, C4_and_andn>;
1799 def: LogLogNot_pat<and, or,  C4_and_orn>;
1800 def: LogLogNot_pat<or,  and, C4_or_andn>;
1801 def: LogLogNot_pat<or,  or,  C4_or_orn>;
1802
1803 //===----------------------------------------------------------------------===//
1804 // CR -
1805 //===----------------------------------------------------------------------===//
1806
1807 //===----------------------------------------------------------------------===//
1808 // XTYPE/ALU +
1809 //===----------------------------------------------------------------------===//
1810
1811 // Logical with-not instructions.
1812 def A4_andnp : T_ALU64_logical<"and", 0b001, 1, 0, 1>;
1813 def A4_ornp  : T_ALU64_logical<"or",  0b011, 1, 0, 1>;
1814
1815 def: Pat<(i64 (and (i64 DoubleRegs:$Rs), (i64 (not (i64 DoubleRegs:$Rt))))),
1816          (A4_andnp DoubleRegs:$Rs, DoubleRegs:$Rt)>;
1817 def: Pat<(i64 (or  (i64 DoubleRegs:$Rs), (i64 (not (i64 DoubleRegs:$Rt))))),
1818          (A4_ornp DoubleRegs:$Rs, DoubleRegs:$Rt)>;
1819
1820 let hasNewValue = 1, hasSideEffects = 0 in
1821 def S4_parity: ALU64Inst<(outs IntRegs:$Rd), (ins IntRegs:$Rs, IntRegs:$Rt),
1822       "$Rd = parity($Rs, $Rt)", [], "", ALU64_tc_2_SLOT23> {
1823   bits<5> Rd;
1824   bits<5> Rs;
1825   bits<5> Rt;
1826
1827   let IClass = 0b1101;
1828   let Inst{27-21} = 0b0101111;
1829   let Inst{20-16} = Rs;
1830   let Inst{12-8} = Rt;
1831   let Inst{4-0} = Rd;
1832 }
1833
1834 //  Add and accumulate.
1835 //  Rd=add(Rs,add(Ru,#s6))
1836 let isExtentSigned = 1, hasNewValue = 1, isExtendable = 1, opExtentBits = 6,
1837     opExtendable = 3 in
1838 def S4_addaddi : ALU64Inst <(outs IntRegs:$Rd),
1839                             (ins IntRegs:$Rs, IntRegs:$Ru, s6Ext:$s6),
1840   "$Rd = add($Rs, add($Ru, #$s6))" ,
1841   [(set (i32 IntRegs:$Rd), (add (i32 IntRegs:$Rs),
1842                            (add (i32 IntRegs:$Ru), s6_16ExtPred:$s6)))],
1843   "", ALU64_tc_2_SLOT23> {
1844     bits<5> Rd;
1845     bits<5> Rs;
1846     bits<5> Ru;
1847     bits<6> s6;
1848
1849     let IClass = 0b1101;
1850
1851     let Inst{27-23} = 0b10110;
1852     let Inst{22-21} = s6{5-4};
1853     let Inst{20-16} = Rs;
1854     let Inst{13}    = s6{3};
1855     let Inst{12-8}  = Rd;
1856     let Inst{7-5}   = s6{2-0};
1857     let Inst{4-0}   = Ru;
1858   }
1859
1860 let isExtentSigned = 1, hasSideEffects = 0, hasNewValue = 1, isExtendable = 1,
1861     opExtentBits = 6, opExtendable = 2 in
1862 def S4_subaddi: ALU64Inst <(outs IntRegs:$Rd),
1863                            (ins IntRegs:$Rs, s6Ext:$s6, IntRegs:$Ru),
1864   "$Rd = add($Rs, sub(#$s6, $Ru))",
1865   [], "", ALU64_tc_2_SLOT23> {
1866     bits<5> Rd;
1867     bits<5> Rs;
1868     bits<6> s6;
1869     bits<5> Ru;
1870
1871     let IClass = 0b1101;
1872
1873     let Inst{27-23} = 0b10111;
1874     let Inst{22-21} = s6{5-4};
1875     let Inst{20-16} = Rs;
1876     let Inst{13}    = s6{3};
1877     let Inst{12-8}  = Rd;
1878     let Inst{7-5}   = s6{2-0};
1879     let Inst{4-0}   = Ru;
1880   }
1881
1882 // Rd=add(Rs,sub(#s6,Ru))
1883 def: Pat<(add (i32 IntRegs:$src1), (sub s6_10ExtPred:$src2,
1884                                         (i32 IntRegs:$src3))),
1885          (S4_subaddi IntRegs:$src1, s6_10ExtPred:$src2, IntRegs:$src3)>;
1886
1887 // Rd=sub(add(Rs,#s6),Ru)
1888 def: Pat<(sub (add (i32 IntRegs:$src1), s6_10ExtPred:$src2),
1889                    (i32 IntRegs:$src3)),
1890          (S4_subaddi IntRegs:$src1, s6_10ExtPred:$src2, IntRegs:$src3)>;
1891
1892 // Rd=add(sub(Rs,Ru),#s6)
1893 def: Pat<(add (sub (i32 IntRegs:$src1), (i32 IntRegs:$src3)),
1894                    (s6_10ExtPred:$src2)),
1895          (S4_subaddi IntRegs:$src1, s6_10ExtPred:$src2, IntRegs:$src3)>;
1896
1897
1898 //  Add or subtract doublewords with carry.
1899 //TODO:
1900 //  Rdd=add(Rss,Rtt,Px):carry
1901 //TODO:
1902 //  Rdd=sub(Rss,Rtt,Px):carry
1903
1904 // Extract bitfield
1905 // Rdd=extract(Rss,#u6,#U6)
1906 // Rdd=extract(Rss,Rtt)
1907 // Rd=extract(Rs,Rtt)
1908 // Rd=extract(Rs,#u5,#U5)
1909
1910 def S4_extractp_rp : T_S3op_64 < "extract",  0b11, 0b100, 0>;
1911 def S4_extractp    : T_S2op_extract <"extract",  0b1010, DoubleRegs, u6Imm>;
1912
1913 let hasNewValue = 1 in {
1914   def S4_extract_rp : T_S3op_extract<"extract",  0b01>;
1915   def S4_extract    : T_S2op_extract <"extract",  0b1101, IntRegs, u5Imm>;
1916 }
1917
1918 // Complex add/sub halfwords/words
1919 let Defs = [USR_OVF] in {
1920   def S4_vxaddsubh : T_S3op_64 < "vxaddsubh", 0b01, 0b100, 0, 1>;
1921   def S4_vxaddsubw : T_S3op_64 < "vxaddsubw", 0b01, 0b000, 0, 1>;
1922   def S4_vxsubaddh : T_S3op_64 < "vxsubaddh", 0b01, 0b110, 0, 1>;
1923   def S4_vxsubaddw : T_S3op_64 < "vxsubaddw", 0b01, 0b010, 0, 1>;
1924 }
1925
1926 let Defs = [USR_OVF] in {
1927   def S4_vxaddsubhr : T_S3op_64 < "vxaddsubh", 0b11, 0b000, 0, 1, 1, 1>;
1928   def S4_vxsubaddhr : T_S3op_64 < "vxsubaddh", 0b11, 0b010, 0, 1, 1, 1>;
1929 }
1930
1931 let Itinerary = M_tc_3x_SLOT23, Defs = [USR_OVF] in {
1932   def M4_mac_up_s1_sat: T_MType_acc_rr<"+= mpy", 0b011, 0b000, 0, [], 0, 1, 1>;
1933   def M4_nac_up_s1_sat: T_MType_acc_rr<"-= mpy", 0b011, 0b001, 0, [], 0, 1, 1>;
1934 }
1935
1936 // Logical xor with xor accumulation.
1937 // Rxx^=xor(Rss,Rtt)
1938 let hasSideEffects = 0 in
1939 def M4_xor_xacc
1940   : SInst <(outs DoubleRegs:$Rxx),
1941            (ins DoubleRegs:$dst2, DoubleRegs:$Rss, DoubleRegs:$Rtt),
1942   "$Rxx ^= xor($Rss, $Rtt)",
1943   [(set (i64 DoubleRegs:$Rxx),
1944    (xor (i64 DoubleRegs:$dst2), (xor (i64 DoubleRegs:$Rss),
1945                                      (i64 DoubleRegs:$Rtt))))],
1946   "$dst2 = $Rxx", S_3op_tc_1_SLOT23> {
1947     bits<5> Rxx;
1948     bits<5> Rss;
1949     bits<5> Rtt;
1950
1951     let IClass = 0b1100;
1952
1953     let Inst{27-22} = 0b101010;
1954     let Inst{20-16} = Rss;
1955     let Inst{12-8}  = Rtt;
1956     let Inst{7-5}   = 0b000;
1957     let Inst{4-0}   = Rxx;
1958   }
1959
1960 // Rotate and reduce bytes
1961 // Rdd=vrcrotate(Rss,Rt,#u2)
1962 let hasSideEffects = 0 in
1963 def S4_vrcrotate
1964   : SInst <(outs DoubleRegs:$Rdd),
1965            (ins DoubleRegs:$Rss, IntRegs:$Rt, u2Imm:$u2),
1966   "$Rdd = vrcrotate($Rss, $Rt, #$u2)",
1967   [], "", S_3op_tc_3x_SLOT23> {
1968     bits<5> Rdd;
1969     bits<5> Rss;
1970     bits<5> Rt;
1971     bits<2> u2;
1972
1973     let IClass = 0b1100;
1974
1975     let Inst{27-22} = 0b001111;
1976     let Inst{20-16} = Rss;
1977     let Inst{13}    = u2{1};
1978     let Inst{12-8}  = Rt;
1979     let Inst{7-6}   = 0b11;
1980     let Inst{5}     = u2{0};
1981     let Inst{4-0}   = Rdd;
1982   }
1983
1984 // Rotate and reduce bytes with accumulation
1985 // Rxx+=vrcrotate(Rss,Rt,#u2)
1986 let hasSideEffects = 0 in
1987 def S4_vrcrotate_acc
1988   : SInst <(outs DoubleRegs:$Rxx),
1989            (ins DoubleRegs:$dst2, DoubleRegs:$Rss, IntRegs:$Rt, u2Imm:$u2),
1990   "$Rxx += vrcrotate($Rss, $Rt, #$u2)", [],
1991   "$dst2 = $Rxx", S_3op_tc_3x_SLOT23> {
1992     bits<5> Rxx;
1993     bits<5> Rss;
1994     bits<5> Rt;
1995     bits<2> u2;
1996
1997     let IClass = 0b1100;
1998
1999     let Inst{27-21} = 0b1011101;
2000     let Inst{20-16} = Rss;
2001     let Inst{13}    = u2{1};
2002     let Inst{12-8}  = Rt;
2003     let Inst{5}     = u2{0};
2004     let Inst{4-0}   = Rxx;
2005   }
2006
2007 // Vector reduce conditional negate halfwords
2008 let hasSideEffects = 0 in
2009 def S2_vrcnegh
2010   : SInst <(outs DoubleRegs:$Rxx),
2011            (ins DoubleRegs:$dst2, DoubleRegs:$Rss, IntRegs:$Rt),
2012   "$Rxx += vrcnegh($Rss, $Rt)", [],
2013   "$dst2 = $Rxx", S_3op_tc_3x_SLOT23> {
2014     bits<5> Rxx;
2015     bits<5> Rss;
2016     bits<5> Rt;
2017
2018     let IClass = 0b1100;
2019
2020     let Inst{27-21} = 0b1011001;
2021     let Inst{20-16} = Rss;
2022     let Inst{13}    = 0b1;
2023     let Inst{12-8}  = Rt;
2024     let Inst{7-5}   = 0b111;
2025     let Inst{4-0}   = Rxx;
2026   }
2027
2028 // Split bitfield
2029 def A4_bitspliti : T_S2op_2_di <"bitsplit", 0b110, 0b100>;
2030
2031 // Arithmetic/Convergent round
2032 def A4_cround_ri : T_S2op_2_ii <"cround", 0b111, 0b000>;
2033
2034 def A4_round_ri  : T_S2op_2_ii <"round", 0b111, 0b100>;
2035
2036 let Defs = [USR_OVF] in
2037 def A4_round_ri_sat : T_S2op_2_ii <"round", 0b111, 0b110, 1>;
2038
2039 // Logical-logical words.
2040 // Compound or-and -- Rx=or(Ru,and(Rx,#s10))
2041 let isExtentSigned = 1, hasNewValue = 1, isExtendable = 1, opExtentBits = 10,
2042     opExtendable = 3 in
2043 def S4_or_andix:
2044   ALU64Inst<(outs IntRegs:$Rx),
2045             (ins IntRegs:$Ru, IntRegs:$_src_, s10Ext:$s10),
2046   "$Rx = or($Ru, and($_src_, #$s10))" ,
2047   [(set (i32 IntRegs:$Rx),
2048         (or (i32 IntRegs:$Ru), (and (i32 IntRegs:$_src_), s10ExtPred:$s10)))] ,
2049   "$_src_ = $Rx", ALU64_tc_2_SLOT23> {
2050     bits<5> Rx;
2051     bits<5> Ru;
2052     bits<10> s10;
2053
2054     let IClass = 0b1101;
2055
2056     let Inst{27-22} = 0b101001;
2057     let Inst{20-16} = Rx;
2058     let Inst{21}    = s10{9};
2059     let Inst{13-5}  = s10{8-0};
2060     let Inst{4-0}   = Ru;
2061   }
2062
2063 // Miscellaneous ALU64 instructions.
2064 //
2065 let hasNewValue = 1, hasSideEffects = 0 in
2066 def A4_modwrapu: ALU64Inst<(outs IntRegs:$Rd), (ins IntRegs:$Rs, IntRegs:$Rt),
2067       "$Rd = modwrap($Rs, $Rt)", [], "", ALU64_tc_2_SLOT23> {
2068   bits<5> Rd;
2069   bits<5> Rs;
2070   bits<5> Rt;
2071
2072   let IClass = 0b1101;
2073   let Inst{27-21} = 0b0011111;
2074   let Inst{20-16} = Rs;
2075   let Inst{12-8} = Rt;
2076   let Inst{7-5} = 0b111;
2077   let Inst{4-0} = Rd;
2078 }
2079
2080 let hasSideEffects = 0 in
2081 def A4_bitsplit: ALU64Inst<(outs DoubleRegs:$Rd),
2082       (ins IntRegs:$Rs, IntRegs:$Rt),
2083       "$Rd = bitsplit($Rs, $Rt)", [], "", ALU64_tc_1_SLOT23> {
2084   bits<5> Rd;
2085   bits<5> Rs;
2086   bits<5> Rt;
2087
2088   let IClass = 0b1101;
2089   let Inst{27-24} = 0b0100;
2090   let Inst{21} = 0b1;
2091   let Inst{20-16} = Rs;
2092   let Inst{12-8} = Rt;
2093   let Inst{4-0} = Rd;
2094 }
2095
2096 let hasSideEffects = 0 in
2097 def dep_S2_packhl: ALU64Inst<(outs DoubleRegs:$Rd),
2098       (ins IntRegs:$Rs, IntRegs:$Rt),
2099       "$Rd = packhl($Rs, $Rt):deprecated", [], "", ALU64_tc_1_SLOT23> {
2100   bits<5> Rd;
2101   bits<5> Rs;
2102   bits<5> Rt;
2103
2104   let IClass = 0b1101;
2105   let Inst{27-24} = 0b0100;
2106   let Inst{21} = 0b0;
2107   let Inst{20-16} = Rs;
2108   let Inst{12-8} = Rt;
2109   let Inst{4-0} = Rd;
2110 }
2111
2112 let hasNewValue = 1, hasSideEffects = 0 in
2113 def dep_A2_addsat: ALU64Inst<(outs IntRegs:$Rd),
2114       (ins IntRegs:$Rs, IntRegs:$Rt),
2115       "$Rd = add($Rs, $Rt):sat:deprecated", [], "", ALU64_tc_2_SLOT23> {
2116   bits<5> Rd;
2117   bits<5> Rs;
2118   bits<5> Rt;
2119
2120   let IClass = 0b1101;
2121   let Inst{27-21} = 0b0101100;
2122   let Inst{20-16} = Rs;
2123   let Inst{12-8} = Rt;
2124   let Inst{7} = 0b0;
2125   let Inst{4-0} = Rd;
2126 }
2127
2128 let hasNewValue = 1, hasSideEffects = 0 in
2129 def dep_A2_subsat: ALU64Inst<(outs IntRegs:$Rd),
2130       (ins IntRegs:$Rs, IntRegs:$Rt),
2131       "$Rd = sub($Rs, $Rt):sat:deprecated", [], "", ALU64_tc_2_SLOT23> {
2132   bits<5> Rd;
2133   bits<5> Rs;
2134   bits<5> Rt;
2135
2136   let IClass = 0b1101;
2137   let Inst{27-21} = 0b0101100;
2138   let Inst{20-16} = Rt;
2139   let Inst{12-8} = Rs;
2140   let Inst{7} = 0b1;
2141   let Inst{4-0} = Rd;
2142 }
2143
2144 // Rx[&|]=xor(Rs,Rt)
2145 def M4_or_xor   : T_MType_acc_rr < "|= xor", 0b110, 0b001, 0>;
2146 def M4_and_xor  : T_MType_acc_rr < "&= xor", 0b010, 0b010, 0>;
2147
2148 // Rx[&|^]=or(Rs,Rt)
2149 def M4_xor_or   : T_MType_acc_rr < "^= or",  0b110, 0b011, 0>;
2150
2151 let CextOpcode = "ORr_ORr" in
2152 def M4_or_or    : T_MType_acc_rr < "|= or",  0b110, 0b000, 0>;
2153 def M4_and_or   : T_MType_acc_rr < "&= or",  0b010, 0b001, 0>;
2154
2155 // Rx[&|^]=and(Rs,Rt)
2156 def M4_xor_and  : T_MType_acc_rr < "^= and", 0b110, 0b010, 0>;
2157
2158 let CextOpcode = "ORr_ANDr" in
2159 def M4_or_and   : T_MType_acc_rr < "|= and", 0b010, 0b011, 0>;
2160 def M4_and_and  : T_MType_acc_rr < "&= and", 0b010, 0b000, 0>;
2161
2162 // Rx[&|^]=and(Rs,~Rt)
2163 def M4_xor_andn : T_MType_acc_rr < "^= and", 0b001, 0b010, 0, [], 1>;
2164 def M4_or_andn  : T_MType_acc_rr < "|= and", 0b001, 0b000, 0, [], 1>;
2165 def M4_and_andn : T_MType_acc_rr < "&= and", 0b001, 0b001, 0, [], 1>;
2166
2167 def: T_MType_acc_pat2 <M4_or_xor, xor, or>;
2168 def: T_MType_acc_pat2 <M4_and_xor, xor, and>;
2169 def: T_MType_acc_pat2 <M4_or_and, and, or>;
2170 def: T_MType_acc_pat2 <M4_and_and, and, and>;
2171 def: T_MType_acc_pat2 <M4_xor_and, and, xor>;
2172 def: T_MType_acc_pat2 <M4_or_or, or, or>;
2173 def: T_MType_acc_pat2 <M4_and_or, or, and>;
2174 def: T_MType_acc_pat2 <M4_xor_or, or, xor>;
2175
2176 class T_MType_acc_pat3 <InstHexagon MI, SDNode firstOp, SDNode secOp>
2177   : Pat <(i32 (secOp IntRegs:$src1, (firstOp IntRegs:$src2,
2178                                               (not IntRegs:$src3)))),
2179          (i32 (MI IntRegs:$src1, IntRegs:$src2, IntRegs:$src3))>;
2180
2181 def: T_MType_acc_pat3 <M4_or_andn, and, or>;
2182 def: T_MType_acc_pat3 <M4_and_andn, and, and>;
2183 def: T_MType_acc_pat3 <M4_xor_andn, and, xor>;
2184
2185 // Compound or-or and or-and
2186 let isExtentSigned = 1, InputType = "imm", hasNewValue = 1, isExtendable = 1,
2187     opExtentBits = 10, opExtendable = 3 in
2188 class T_CompOR <string mnemonic, bits<2> MajOp, SDNode OpNode>
2189   : MInst_acc <(outs IntRegs:$Rx),
2190                (ins IntRegs:$src1, IntRegs:$Rs, s10Ext:$s10),
2191   "$Rx |= "#mnemonic#"($Rs, #$s10)",
2192   [(set (i32 IntRegs:$Rx), (or (i32 IntRegs:$src1),
2193                            (OpNode (i32 IntRegs:$Rs), s10ExtPred:$s10)))],
2194   "$src1 = $Rx", ALU64_tc_2_SLOT23>, ImmRegRel {
2195     bits<5> Rx;
2196     bits<5> Rs;
2197     bits<10> s10;
2198
2199     let IClass = 0b1101;
2200
2201     let Inst{27-24} = 0b1010;
2202     let Inst{23-22} = MajOp;
2203     let Inst{20-16} = Rs;
2204     let Inst{21}    = s10{9};
2205     let Inst{13-5}  = s10{8-0};
2206     let Inst{4-0}   = Rx;
2207   }
2208
2209 let CextOpcode = "ORr_ANDr" in
2210 def S4_or_andi : T_CompOR <"and", 0b00, and>;
2211
2212 let CextOpcode = "ORr_ORr" in
2213 def S4_or_ori : T_CompOR <"or", 0b10, or>;
2214
2215 //    Modulo wrap
2216 //        Rd=modwrap(Rs,Rt)
2217 //    Round
2218 //        Rd=cround(Rs,#u5)
2219 //        Rd=cround(Rs,Rt)
2220 //        Rd=round(Rs,#u5)[:sat]
2221 //        Rd=round(Rs,Rt)[:sat]
2222 //    Vector reduce add unsigned halfwords
2223 //        Rd=vraddh(Rss,Rtt)
2224 //    Vector add bytes
2225 //        Rdd=vaddb(Rss,Rtt)
2226 //    Vector conditional negate
2227 //        Rdd=vcnegh(Rss,Rt)
2228 //        Rxx+=vrcnegh(Rss,Rt)
2229 //    Vector maximum bytes
2230 //        Rdd=vmaxb(Rtt,Rss)
2231 //    Vector reduce maximum halfwords
2232 //        Rxx=vrmaxh(Rss,Ru)
2233 //        Rxx=vrmaxuh(Rss,Ru)
2234 //    Vector reduce maximum words
2235 //        Rxx=vrmaxuw(Rss,Ru)
2236 //        Rxx=vrmaxw(Rss,Ru)
2237 //    Vector minimum bytes
2238 //        Rdd=vminb(Rtt,Rss)
2239 //    Vector reduce minimum halfwords
2240 //        Rxx=vrminh(Rss,Ru)
2241 //        Rxx=vrminuh(Rss,Ru)
2242 //    Vector reduce minimum words
2243 //        Rxx=vrminuw(Rss,Ru)
2244 //        Rxx=vrminw(Rss,Ru)
2245 //    Vector subtract bytes
2246 //        Rdd=vsubb(Rss,Rtt)
2247
2248 //===----------------------------------------------------------------------===//
2249 // XTYPE/ALU -
2250 //===----------------------------------------------------------------------===//
2251
2252 //===----------------------------------------------------------------------===//
2253 // XTYPE/BIT +
2254 //===----------------------------------------------------------------------===//
2255
2256 // Bit reverse
2257 def S2_brevp : T_S2op_3 <"brev", 0b11, 0b110>;
2258
2259 // Bit count
2260 def S2_ct0p : T_COUNT_LEADING_64<"ct0", 0b111, 0b010>;
2261 def S2_ct1p : T_COUNT_LEADING_64<"ct1", 0b111, 0b100>;
2262 def S4_clbpnorm : T_COUNT_LEADING_64<"normamt", 0b011, 0b000>;
2263
2264 def: Pat<(i32 (trunc (cttz (i64 DoubleRegs:$Rss)))),
2265          (S2_ct0p (i64 DoubleRegs:$Rss))>;
2266 def: Pat<(i32 (trunc (cttz (not (i64 DoubleRegs:$Rss))))),
2267          (S2_ct1p (i64 DoubleRegs:$Rss))>;
2268
2269 let hasSideEffects = 0, hasNewValue = 1 in
2270 def S4_clbaddi : SInst<(outs IntRegs:$Rd), (ins IntRegs:$Rs, s6Imm:$s6),
2271     "$Rd = add(clb($Rs), #$s6)", [], "", S_2op_tc_2_SLOT23> {
2272   bits<5> Rs;
2273   bits<5> Rd;
2274   bits<6> s6;
2275   let IClass = 0b1000;
2276   let Inst{27-24} = 0b1100;
2277   let Inst{23-21} = 0b001;
2278   let Inst{20-16} = Rs;
2279   let Inst{13-8} = s6;
2280   let Inst{7-5} = 0b000;
2281   let Inst{4-0} = Rd;
2282 }
2283
2284 let hasSideEffects = 0, hasNewValue = 1 in
2285 def S4_clbpaddi : SInst<(outs IntRegs:$Rd), (ins DoubleRegs:$Rs, s6Imm:$s6),
2286     "$Rd = add(clb($Rs), #$s6)", [], "", S_2op_tc_2_SLOT23> {
2287   bits<5> Rs;
2288   bits<5> Rd;
2289   bits<6> s6;
2290   let IClass = 0b1000;
2291   let Inst{27-24} = 0b1000;
2292   let Inst{23-21} = 0b011;
2293   let Inst{20-16} = Rs;
2294   let Inst{13-8} = s6;
2295   let Inst{7-5} = 0b010;
2296   let Inst{4-0} = Rd;
2297 }
2298
2299
2300 // Bit test/set/clear
2301 def S4_ntstbit_i : T_TEST_BIT_IMM<"!tstbit", 0b001>;
2302 def S4_ntstbit_r : T_TEST_BIT_REG<"!tstbit", 1>;
2303
2304 let AddedComplexity = 20 in {   // Complexity greater than cmp reg-imm.
2305   def: Pat<(i1 (seteq (and (shl 1, u5ImmPred:$u5), (i32 IntRegs:$Rs)), 0)),
2306            (S4_ntstbit_i (i32 IntRegs:$Rs), u5ImmPred:$u5)>;
2307   def: Pat<(i1 (seteq (and (shl 1, (i32 IntRegs:$Rt)), (i32 IntRegs:$Rs)), 0)),
2308            (S4_ntstbit_r (i32 IntRegs:$Rs), (i32 IntRegs:$Rt))>;
2309 }
2310
2311 // Add extra complexity to prefer these instructions over bitsset/bitsclr.
2312 // The reason is that tstbit/ntstbit can be folded into a compound instruction:
2313 //   if ([!]tstbit(...)) jump ...
2314 let AddedComplexity = 100 in
2315 def: Pat<(i1 (setne (and (i32 IntRegs:$Rs), (i32 Set5ImmPred:$u5)), (i32 0))),
2316          (S2_tstbit_i (i32 IntRegs:$Rs), (BITPOS32 Set5ImmPred:$u5))>;
2317
2318 let AddedComplexity = 100 in
2319 def: Pat<(i1 (seteq (and (i32 IntRegs:$Rs), (i32 Set5ImmPred:$u5)), (i32 0))),
2320          (S4_ntstbit_i (i32 IntRegs:$Rs), (BITPOS32 Set5ImmPred:$u5))>;
2321
2322 def C4_nbitsset  : T_TEST_BITS_REG<"!bitsset", 0b01, 1>;
2323 def C4_nbitsclr  : T_TEST_BITS_REG<"!bitsclr", 0b10, 1>;
2324 def C4_nbitsclri : T_TEST_BITS_IMM<"!bitsclr", 0b10, 1>;
2325
2326 // Do not increase complexity of these patterns. In the DAG, "cmp i8" may be
2327 // represented as a compare against "value & 0xFF", which is an exact match
2328 // for cmpb (same for cmph). The patterns below do not contain any additional
2329 // complexity that would make them preferable, and if they were actually used
2330 // instead of cmpb/cmph, they would result in a compare against register that
2331 // is loaded with the byte/half mask (i.e. 0xFF or 0xFFFF).
2332 def: Pat<(i1 (setne (and I32:$Rs, u6ImmPred:$u6), 0)),
2333          (C4_nbitsclri I32:$Rs, u6ImmPred:$u6)>;
2334 def: Pat<(i1 (setne (and I32:$Rs, I32:$Rt), 0)),
2335          (C4_nbitsclr I32:$Rs, I32:$Rt)>;
2336 def: Pat<(i1 (setne (and I32:$Rs, I32:$Rt), I32:$Rt)),
2337          (C4_nbitsset I32:$Rs, I32:$Rt)>;
2338
2339 //===----------------------------------------------------------------------===//
2340 // XTYPE/BIT -
2341 //===----------------------------------------------------------------------===//
2342
2343 //===----------------------------------------------------------------------===//
2344 // XTYPE/MPY +
2345 //===----------------------------------------------------------------------===//
2346
2347 // Rd=add(#u6,mpyi(Rs,#U6)) -- Multiply by immed and add immed.
2348
2349 let hasNewValue = 1, isExtendable = 1, opExtentBits = 6, opExtendable = 1 in
2350 def M4_mpyri_addi : MInst<(outs IntRegs:$Rd),
2351   (ins u6Ext:$u6, IntRegs:$Rs, u6Imm:$U6),
2352   "$Rd = add(#$u6, mpyi($Rs, #$U6))" ,
2353   [(set (i32 IntRegs:$Rd),
2354         (add (mul (i32 IntRegs:$Rs), u6ImmPred:$U6),
2355              u6ExtPred:$u6))] ,"",ALU64_tc_3x_SLOT23> {
2356     bits<5> Rd;
2357     bits<6> u6;
2358     bits<5> Rs;
2359     bits<6> U6;
2360
2361     let IClass = 0b1101;
2362
2363     let Inst{27-24} = 0b1000;
2364     let Inst{23}    = U6{5};
2365     let Inst{22-21} = u6{5-4};
2366     let Inst{20-16} = Rs;
2367     let Inst{13}    = u6{3};
2368     let Inst{12-8}  = Rd;
2369     let Inst{7-5}   = u6{2-0};
2370     let Inst{4-0}   = U6{4-0};
2371   }
2372
2373 // Rd=add(#u6,mpyi(Rs,Rt))
2374 let CextOpcode = "ADD_MPY", InputType = "imm", hasNewValue = 1,
2375     isExtendable = 1, opExtentBits = 6, opExtendable = 1 in
2376 def M4_mpyrr_addi : MInst <(outs IntRegs:$Rd),
2377   (ins u6Ext:$u6, IntRegs:$Rs, IntRegs:$Rt),
2378   "$Rd = add(#$u6, mpyi($Rs, $Rt))" ,
2379   [(set (i32 IntRegs:$Rd),
2380         (add (mul (i32 IntRegs:$Rs), (i32 IntRegs:$Rt)), u6ExtPred:$u6))],
2381   "", ALU64_tc_3x_SLOT23>, ImmRegRel {
2382     bits<5> Rd;
2383     bits<6> u6;
2384     bits<5> Rs;
2385     bits<5> Rt;
2386
2387     let IClass = 0b1101;
2388
2389     let Inst{27-23} = 0b01110;
2390     let Inst{22-21} = u6{5-4};
2391     let Inst{20-16} = Rs;
2392     let Inst{13}    = u6{3};
2393     let Inst{12-8}  = Rt;
2394     let Inst{7-5}   = u6{2-0};
2395     let Inst{4-0}   = Rd;
2396   }
2397
2398 let hasNewValue = 1 in
2399 class T_AddMpy <bit MajOp, PatLeaf ImmPred, dag ins>
2400   : ALU64Inst <(outs IntRegs:$dst), ins,
2401   "$dst = add($src1, mpyi("#!if(MajOp,"$src3, #$src2))",
2402                                       "#$src2, $src3))"),
2403   [(set (i32 IntRegs:$dst),
2404         (add (i32 IntRegs:$src1), (mul (i32 IntRegs:$src3), ImmPred:$src2)))],
2405   "", ALU64_tc_3x_SLOT23> {
2406     bits<5> dst;
2407     bits<5> src1;
2408     bits<8> src2;
2409     bits<5> src3;
2410
2411     let IClass = 0b1101;
2412
2413     bits<6> ImmValue = !if(MajOp, src2{5-0}, src2{7-2});
2414
2415     let Inst{27-24} = 0b1111;
2416     let Inst{23}    = MajOp;
2417     let Inst{22-21} = ImmValue{5-4};
2418     let Inst{20-16} = src3;
2419     let Inst{13}    = ImmValue{3};
2420     let Inst{12-8}  = dst;
2421     let Inst{7-5}   = ImmValue{2-0};
2422     let Inst{4-0}   = src1;
2423   }
2424
2425 def M4_mpyri_addr_u2 : T_AddMpy<0b0, u6_2ImmPred,
2426                        (ins IntRegs:$src1, u6_2Imm:$src2, IntRegs:$src3)>;
2427
2428 let isExtendable = 1, opExtentBits = 6, opExtendable = 3,
2429     CextOpcode = "ADD_MPY", InputType = "imm" in
2430 def M4_mpyri_addr : T_AddMpy<0b1, u6ExtPred,
2431                     (ins IntRegs:$src1, IntRegs:$src3, u6Ext:$src2)>, ImmRegRel;
2432
2433 // Rx=add(Ru,mpyi(Rx,Rs))
2434 let CextOpcode = "ADD_MPY", InputType = "reg", hasNewValue = 1 in
2435 def M4_mpyrr_addr: MInst_acc <(outs IntRegs:$Rx),
2436                               (ins IntRegs:$Ru, IntRegs:$_src_, IntRegs:$Rs),
2437   "$Rx = add($Ru, mpyi($_src_, $Rs))",
2438   [(set (i32 IntRegs:$Rx), (add (i32 IntRegs:$Ru),
2439                            (mul (i32 IntRegs:$_src_), (i32 IntRegs:$Rs))))],
2440   "$_src_ = $Rx", M_tc_3x_SLOT23>, ImmRegRel {
2441     bits<5> Rx;
2442     bits<5> Ru;
2443     bits<5> Rs;
2444
2445     let IClass = 0b1110;
2446
2447     let Inst{27-21} = 0b0011000;
2448     let Inst{12-8} = Rx;
2449     let Inst{4-0} = Ru;
2450     let Inst{20-16} = Rs;
2451   }
2452
2453 // Rd=add(##,mpyi(Rs,#U6))
2454 def : Pat <(add (mul (i32 IntRegs:$src2), u6ImmPred:$src3),
2455                      (HexagonCONST32 tglobaladdr:$src1)),
2456            (i32 (M4_mpyri_addi tglobaladdr:$src1, IntRegs:$src2,
2457                                u6ImmPred:$src3))>;
2458
2459 // Rd=add(##,mpyi(Rs,Rt))
2460 def : Pat <(add (mul (i32 IntRegs:$src2), (i32 IntRegs:$src3)),
2461                      (HexagonCONST32 tglobaladdr:$src1)),
2462            (i32 (M4_mpyrr_addi tglobaladdr:$src1, IntRegs:$src2,
2463                                IntRegs:$src3))>;
2464
2465 // Vector reduce multiply word by signed half (32x16)
2466 //Rdd=vrmpyweh(Rss,Rtt)[:<<1]
2467 def M4_vrmpyeh_s0 : T_M2_vmpy<"vrmpyweh", 0b010, 0b100, 0, 0, 0>;
2468 def M4_vrmpyeh_s1 : T_M2_vmpy<"vrmpyweh", 0b110, 0b100, 1, 0, 0>;
2469
2470 //Rdd=vrmpywoh(Rss,Rtt)[:<<1]
2471 def M4_vrmpyoh_s0 : T_M2_vmpy<"vrmpywoh", 0b001, 0b010, 0, 0, 0>;
2472 def M4_vrmpyoh_s1 : T_M2_vmpy<"vrmpywoh", 0b101, 0b010, 1, 0, 0>;
2473
2474 //Rdd+=vrmpyweh(Rss,Rtt)[:<<1]
2475 def M4_vrmpyeh_acc_s0: T_M2_vmpy_acc<"vrmpyweh", 0b001, 0b110, 0, 0>;
2476 def M4_vrmpyeh_acc_s1: T_M2_vmpy_acc<"vrmpyweh", 0b101, 0b110, 1, 0>;
2477
2478 //Rdd=vrmpywoh(Rss,Rtt)[:<<1]
2479 def M4_vrmpyoh_acc_s0: T_M2_vmpy_acc<"vrmpywoh", 0b011, 0b110, 0, 0>;
2480 def M4_vrmpyoh_acc_s1: T_M2_vmpy_acc<"vrmpywoh", 0b111, 0b110, 1, 0>;
2481
2482 // Vector multiply halfwords, signed by unsigned
2483 // Rdd=vmpyhsu(Rs,Rt)[:<<]:sat
2484 def M2_vmpy2su_s0 : T_XTYPE_mpy64 < "vmpyhsu", 0b000, 0b111, 1, 0, 0>;
2485 def M2_vmpy2su_s1 : T_XTYPE_mpy64 < "vmpyhsu", 0b100, 0b111, 1, 1, 0>;
2486
2487 // Rxx+=vmpyhsu(Rs,Rt)[:<<1]:sat
2488 def M2_vmac2su_s0 : T_XTYPE_mpy64_acc < "vmpyhsu", "+", 0b011, 0b101, 1, 0, 0>;
2489 def M2_vmac2su_s1 : T_XTYPE_mpy64_acc < "vmpyhsu", "+", 0b111, 0b101, 1, 1, 0>;
2490
2491 // Vector polynomial multiply halfwords
2492 // Rdd=vpmpyh(Rs,Rt)
2493 def M4_vpmpyh : T_XTYPE_mpy64 < "vpmpyh", 0b110, 0b111, 0, 0, 0>;
2494
2495 // Rxx^=vpmpyh(Rs,Rt)
2496 def M4_vpmpyh_acc : T_XTYPE_mpy64_acc < "vpmpyh", "^", 0b101, 0b111, 0, 0, 0>;
2497
2498 // Polynomial multiply words
2499 // Rdd=pmpyw(Rs,Rt)
2500 def M4_pmpyw : T_XTYPE_mpy64 < "pmpyw", 0b010, 0b111, 0, 0, 0>;
2501
2502 // Rxx^=pmpyw(Rs,Rt)
2503 def M4_pmpyw_acc  : T_XTYPE_mpy64_acc < "pmpyw", "^", 0b001, 0b111, 0, 0, 0>;
2504
2505 //===----------------------------------------------------------------------===//
2506 // XTYPE/MPY -
2507 //===----------------------------------------------------------------------===//
2508
2509 //===----------------------------------------------------------------------===//
2510 // ALU64/Vector compare
2511 //===----------------------------------------------------------------------===//
2512 //===----------------------------------------------------------------------===//
2513 // Template class for vector compare
2514 //===----------------------------------------------------------------------===//
2515
2516 let hasSideEffects = 0 in
2517 class T_vcmpImm <string Str, bits<2> cmpOp, bits<2> minOp, Operand ImmOprnd>
2518   : ALU64_rr <(outs PredRegs:$Pd),
2519               (ins DoubleRegs:$Rss, ImmOprnd:$Imm),
2520   "$Pd = "#Str#"($Rss, #$Imm)",
2521   [], "", ALU64_tc_2early_SLOT23> {
2522     bits<2> Pd;
2523     bits<5> Rss;
2524     bits<32> Imm;
2525     bits<8> ImmBits;
2526     let ImmBits{6-0} = Imm{6-0};
2527     let ImmBits{7} = !if (!eq(cmpOp,0b10), 0b0, Imm{7}); // 0 for vcmp[bhw].gtu
2528
2529     let IClass = 0b1101;
2530
2531     let Inst{27-24} = 0b1100;
2532     let Inst{22-21} = cmpOp;
2533     let Inst{20-16} = Rss;
2534     let Inst{12-5} = ImmBits;
2535     let Inst{4-3} = minOp;
2536     let Inst{1-0} = Pd;
2537   }
2538
2539 // Vector compare bytes
2540 def A4_vcmpbgt   : T_vcmp <"vcmpb.gt", 0b1010>;
2541 def: T_vcmp_pat<A4_vcmpbgt, setgt, v8i8>;
2542
2543 let AsmString = "$Pd = any8(vcmpb.eq($Rss, $Rtt))" in
2544 def A4_vcmpbeq_any : T_vcmp <"any8(vcmpb.gt", 0b1000>;
2545
2546 def A4_vcmpbeqi  : T_vcmpImm <"vcmpb.eq",  0b00, 0b00, u8Imm>;
2547 def A4_vcmpbgti  : T_vcmpImm <"vcmpb.gt",  0b01, 0b00, s8Imm>;
2548 def A4_vcmpbgtui : T_vcmpImm <"vcmpb.gtu", 0b10, 0b00, u7Imm>;
2549
2550 // Vector compare halfwords
2551 def A4_vcmpheqi  : T_vcmpImm <"vcmph.eq",  0b00, 0b01, s8Imm>;
2552 def A4_vcmphgti  : T_vcmpImm <"vcmph.gt",  0b01, 0b01, s8Imm>;
2553 def A4_vcmphgtui : T_vcmpImm <"vcmph.gtu", 0b10, 0b01, u7Imm>;
2554
2555 // Vector compare words
2556 def A4_vcmpweqi  : T_vcmpImm <"vcmpw.eq",  0b00, 0b10, s8Imm>;
2557 def A4_vcmpwgti  : T_vcmpImm <"vcmpw.gt",  0b01, 0b10, s8Imm>;
2558 def A4_vcmpwgtui : T_vcmpImm <"vcmpw.gtu", 0b10, 0b10, u7Imm>;
2559
2560 //===----------------------------------------------------------------------===//
2561 // XTYPE/SHIFT +
2562 //===----------------------------------------------------------------------===//
2563 // Shift by immediate and accumulate/logical.
2564 // Rx=add(#u8,asl(Rx,#U5))  Rx=add(#u8,lsr(Rx,#U5))
2565 // Rx=sub(#u8,asl(Rx,#U5))  Rx=sub(#u8,lsr(Rx,#U5))
2566 // Rx=and(#u8,asl(Rx,#U5))  Rx=and(#u8,lsr(Rx,#U5))
2567 // Rx=or(#u8,asl(Rx,#U5))   Rx=or(#u8,lsr(Rx,#U5))
2568 let isExtendable = 1, opExtendable = 1, isExtentSigned = 0, opExtentBits = 8,
2569     hasNewValue = 1, opNewValue = 0 in
2570 class T_S4_ShiftOperate<string MnOp, string MnSh, SDNode Op, SDNode Sh,
2571                         bit asl_lsr, bits<2> MajOp, InstrItinClass Itin>
2572   : MInst_acc<(outs IntRegs:$Rd), (ins u8Ext:$u8, IntRegs:$Rx, u5Imm:$U5),
2573       "$Rd = "#MnOp#"(#$u8, "#MnSh#"($Rx, #$U5))",
2574       [(set (i32 IntRegs:$Rd),
2575             (Op (Sh I32:$Rx, u5ImmPred:$U5), u8ExtPred:$u8))],
2576       "$Rd = $Rx", Itin> {
2577
2578   bits<5> Rd;
2579   bits<8> u8;
2580   bits<5> Rx;
2581   bits<5> U5;
2582
2583   let IClass = 0b1101;
2584   let Inst{27-24} = 0b1110;
2585   let Inst{23-21} = u8{7-5};
2586   let Inst{20-16} = Rd;
2587   let Inst{13} = u8{4};
2588   let Inst{12-8} = U5;
2589   let Inst{7-5} = u8{3-1};
2590   let Inst{4} = asl_lsr;
2591   let Inst{3} = u8{0};
2592   let Inst{2-1} = MajOp;
2593 }
2594
2595 multiclass T_ShiftOperate<string mnemonic, SDNode Op, bits<2> MajOp,
2596                           InstrItinClass Itin> {
2597   def _asl_ri : T_S4_ShiftOperate<mnemonic, "asl", Op, shl, 0, MajOp, Itin>;
2598   def _lsr_ri : T_S4_ShiftOperate<mnemonic, "lsr", Op, srl, 1, MajOp, Itin>;
2599 }
2600
2601 let AddedComplexity = 200 in {
2602   defm S4_addi : T_ShiftOperate<"add", add, 0b10, ALU64_tc_2_SLOT23>;
2603   defm S4_andi : T_ShiftOperate<"and", and, 0b00, ALU64_tc_2_SLOT23>;
2604 }
2605
2606 let AddedComplexity = 30 in
2607 defm S4_ori  : T_ShiftOperate<"or",  or,  0b01, ALU64_tc_1_SLOT23>;
2608
2609 defm S4_subi : T_ShiftOperate<"sub", sub, 0b11, ALU64_tc_1_SLOT23>;
2610
2611 let AddedComplexity = 200 in {
2612   def: Pat<(add addrga:$addr, (shl I32:$src2, u5ImmPred:$src3)),
2613            (S4_addi_asl_ri addrga:$addr, IntRegs:$src2, u5ImmPred:$src3)>;
2614   def: Pat<(add addrga:$addr, (srl I32:$src2, u5ImmPred:$src3)),
2615            (S4_addi_lsr_ri addrga:$addr, IntRegs:$src2, u5ImmPred:$src3)>;
2616   def: Pat<(sub addrga:$addr, (shl I32:$src2, u5ImmPred:$src3)),
2617            (S4_subi_asl_ri addrga:$addr, IntRegs:$src2, u5ImmPred:$src3)>;
2618   def: Pat<(sub addrga:$addr, (srl I32:$src2, u5ImmPred:$src3)),
2619            (S4_subi_lsr_ri addrga:$addr, IntRegs:$src2, u5ImmPred:$src3)>;
2620 }
2621
2622 // Vector conditional negate
2623 // Rdd=vcnegh(Rss,Rt)
2624 let Defs = [USR_OVF], Itinerary = S_3op_tc_2_SLOT23 in
2625 def S2_vcnegh   : T_S3op_shiftVect < "vcnegh",   0b11, 0b01>;
2626
2627 // Rd=[cround|round](Rs,Rt)
2628 let hasNewValue = 1, Itinerary = S_3op_tc_2_SLOT23 in {
2629   def A4_cround_rr    : T_S3op_3 < "cround", IntRegs, 0b11, 0b00>;
2630   def A4_round_rr     : T_S3op_3 < "round", IntRegs, 0b11, 0b10>;
2631 }
2632
2633 // Rd=round(Rs,Rt):sat
2634 let hasNewValue = 1, Defs = [USR_OVF], Itinerary = S_3op_tc_2_SLOT23 in
2635 def A4_round_rr_sat : T_S3op_3 < "round", IntRegs, 0b11, 0b11, 1>;
2636
2637 // Rd=[cmpyiwh|cmpyrwh](Rss,Rt):<<1:rnd:sat
2638 let Defs = [USR_OVF], Itinerary = S_3op_tc_3x_SLOT23 in {
2639   def M4_cmpyi_wh     : T_S3op_8<"cmpyiwh", 0b100, 1, 1, 1>;
2640   def M4_cmpyr_wh     : T_S3op_8<"cmpyrwh", 0b110, 1, 1, 1>;
2641 }
2642
2643 // Rdd=[add|sub](Rss,Rtt,Px):carry
2644 let isPredicateLate = 1, hasSideEffects = 0 in
2645 class T_S3op_carry <string mnemonic, bits<3> MajOp>
2646   : SInst < (outs DoubleRegs:$Rdd, PredRegs:$Px),
2647             (ins DoubleRegs:$Rss, DoubleRegs:$Rtt, PredRegs:$Pu),
2648   "$Rdd = "#mnemonic#"($Rss, $Rtt, $Pu):carry",
2649   [], "$Px = $Pu", S_3op_tc_1_SLOT23 > {
2650     bits<5> Rdd;
2651     bits<5> Rss;
2652     bits<5> Rtt;
2653     bits<2> Pu;
2654
2655     let IClass = 0b1100;
2656
2657     let Inst{27-24} = 0b0010;
2658     let Inst{23-21} = MajOp;
2659     let Inst{20-16} = Rss;
2660     let Inst{12-8}  = Rtt;
2661     let Inst{6-5}   = Pu;
2662     let Inst{4-0}   = Rdd;
2663   }
2664
2665 def A4_addp_c : T_S3op_carry < "add", 0b110 >;
2666 def A4_subp_c : T_S3op_carry < "sub", 0b111 >;
2667
2668 let Itinerary = S_3op_tc_3_SLOT23, hasSideEffects = 0 in
2669 class T_S3op_6 <string mnemonic, bits<3> MinOp, bit isUnsigned>
2670   : SInst <(outs DoubleRegs:$Rxx),
2671            (ins DoubleRegs:$dst2, DoubleRegs:$Rss, IntRegs:$Ru),
2672   "$Rxx = "#mnemonic#"($Rss, $Ru)" ,
2673   [] , "$dst2 = $Rxx"> {
2674     bits<5> Rxx;
2675     bits<5> Rss;
2676     bits<5> Ru;
2677
2678     let IClass = 0b1100;
2679
2680     let Inst{27-21} = 0b1011001;
2681     let Inst{20-16} = Rss;
2682     let Inst{13}    = isUnsigned;
2683     let Inst{12-8}  = Rxx;
2684     let Inst{7-5}   = MinOp;
2685     let Inst{4-0}   = Ru;
2686   }
2687
2688 // Vector reduce maximum halfwords
2689 // Rxx=vrmax[u]h(Rss,Ru)
2690 def A4_vrmaxh  : T_S3op_6 < "vrmaxh",  0b001, 0>;
2691 def A4_vrmaxuh : T_S3op_6 < "vrmaxuh", 0b001, 1>;
2692
2693 // Vector reduce maximum words
2694 // Rxx=vrmax[u]w(Rss,Ru)
2695 def A4_vrmaxw  : T_S3op_6 < "vrmaxw",  0b010, 0>;
2696 def A4_vrmaxuw : T_S3op_6 < "vrmaxuw", 0b010, 1>;
2697
2698 // Vector reduce minimum halfwords
2699 // Rxx=vrmin[u]h(Rss,Ru)
2700 def A4_vrminh  : T_S3op_6 < "vrminh",  0b101, 0>;
2701 def A4_vrminuh : T_S3op_6 < "vrminuh", 0b101, 1>;
2702
2703 // Vector reduce minimum words
2704 // Rxx=vrmin[u]w(Rss,Ru)
2705 def A4_vrminw  : T_S3op_6 < "vrminw",  0b110, 0>;
2706 def A4_vrminuw : T_S3op_6 < "vrminuw", 0b110, 1>;
2707
2708 // Shift an immediate left by register amount.
2709 let hasNewValue = 1, hasSideEffects = 0 in
2710 def S4_lsli: SInst <(outs IntRegs:$Rd), (ins s6Imm:$s6, IntRegs:$Rt),
2711   "$Rd = lsl(#$s6, $Rt)" ,
2712   [(set (i32 IntRegs:$Rd), (shl s6ImmPred:$s6,
2713                                  (i32 IntRegs:$Rt)))],
2714   "", S_3op_tc_1_SLOT23> {
2715     bits<5> Rd;
2716     bits<6> s6;
2717     bits<5> Rt;
2718
2719     let IClass = 0b1100;
2720
2721     let Inst{27-22} = 0b011010;
2722     let Inst{20-16} = s6{5-1};
2723     let Inst{12-8}  = Rt;
2724     let Inst{7-6}   = 0b11;
2725     let Inst{4-0}   = Rd;
2726     let Inst{5}     = s6{0};
2727   }
2728
2729 //===----------------------------------------------------------------------===//
2730 // XTYPE/SHIFT -
2731 //===----------------------------------------------------------------------===//
2732
2733 //===----------------------------------------------------------------------===//
2734 // MEMOP: Word, Half, Byte
2735 //===----------------------------------------------------------------------===//
2736
2737 def MEMOPIMM : SDNodeXForm<imm, [{
2738   // Call the transformation function XformM5ToU5Imm to get the negative
2739   // immediate's positive counterpart.
2740   int32_t imm = N->getSExtValue();
2741   return XformM5ToU5Imm(imm);
2742 }]>;
2743
2744 def MEMOPIMM_HALF : SDNodeXForm<imm, [{
2745   // -1 .. -31 represented as 65535..65515
2746   // assigning to a short restores our desired signed value.
2747   // Call the transformation function XformM5ToU5Imm to get the negative
2748   // immediate's positive counterpart.
2749   int16_t imm = N->getSExtValue();
2750   return XformM5ToU5Imm(imm);
2751 }]>;
2752
2753 def MEMOPIMM_BYTE : SDNodeXForm<imm, [{
2754   // -1 .. -31 represented as 255..235
2755   // assigning to a char restores our desired signed value.
2756   // Call the transformation function XformM5ToU5Imm to get the negative
2757   // immediate's positive counterpart.
2758   int8_t imm = N->getSExtValue();
2759   return XformM5ToU5Imm(imm);
2760 }]>;
2761
2762 def SETMEMIMM : SDNodeXForm<imm, [{
2763    // Return the bit position we will set [0-31].
2764    // As an SDNode.
2765    int32_t imm = N->getSExtValue();
2766    return XformMskToBitPosU5Imm(imm);
2767 }]>;
2768
2769 def CLRMEMIMM : SDNodeXForm<imm, [{
2770    // Return the bit position we will clear [0-31].
2771    // As an SDNode.
2772    // we bit negate the value first
2773    int32_t imm = ~(N->getSExtValue());
2774    return XformMskToBitPosU5Imm(imm);
2775 }]>;
2776
2777 def SETMEMIMM_SHORT : SDNodeXForm<imm, [{
2778    // Return the bit position we will set [0-15].
2779    // As an SDNode.
2780    int16_t imm = N->getSExtValue();
2781    return XformMskToBitPosU4Imm(imm);
2782 }]>;
2783
2784 def CLRMEMIMM_SHORT : SDNodeXForm<imm, [{
2785    // Return the bit position we will clear [0-15].
2786    // As an SDNode.
2787    // we bit negate the value first
2788    int16_t imm = ~(N->getSExtValue());
2789    return XformMskToBitPosU4Imm(imm);
2790 }]>;
2791
2792 def SETMEMIMM_BYTE : SDNodeXForm<imm, [{
2793    // Return the bit position we will set [0-7].
2794    // As an SDNode.
2795    int8_t imm =  N->getSExtValue();
2796    return XformMskToBitPosU3Imm(imm);
2797 }]>;
2798
2799 def CLRMEMIMM_BYTE : SDNodeXForm<imm, [{
2800    // Return the bit position we will clear [0-7].
2801    // As an SDNode.
2802    // we bit negate the value first
2803    int8_t imm = ~(N->getSExtValue());
2804    return XformMskToBitPosU3Imm(imm);
2805 }]>;
2806
2807 //===----------------------------------------------------------------------===//
2808 // Template class for MemOp instructions with the register value.
2809 //===----------------------------------------------------------------------===//
2810 class MemOp_rr_base <string opc, bits<2> opcBits, Operand ImmOp,
2811                      string memOp, bits<2> memOpBits> :
2812       MEMInst_V4<(outs),
2813                  (ins IntRegs:$base, ImmOp:$offset, IntRegs:$delta),
2814                  opc#"($base+#$offset)"#memOp#"$delta",
2815                  []>,
2816                  Requires<[UseMEMOP]> {
2817
2818     bits<5> base;
2819     bits<5> delta;
2820     bits<32> offset;
2821     bits<6> offsetBits; // memb - u6:0 , memh - u6:1, memw - u6:2
2822
2823     let offsetBits = !if (!eq(opcBits, 0b00), offset{5-0},
2824                      !if (!eq(opcBits, 0b01), offset{6-1},
2825                      !if (!eq(opcBits, 0b10), offset{7-2},0)));
2826
2827     let opExtentAlign = opcBits;
2828     let IClass = 0b0011;
2829     let Inst{27-24} = 0b1110;
2830     let Inst{22-21} = opcBits;
2831     let Inst{20-16} = base;
2832     let Inst{13} = 0b0;
2833     let Inst{12-7} = offsetBits;
2834     let Inst{6-5} = memOpBits;
2835     let Inst{4-0} = delta;
2836 }
2837
2838 //===----------------------------------------------------------------------===//
2839 // Template class for MemOp instructions with the immediate value.
2840 //===----------------------------------------------------------------------===//
2841 class MemOp_ri_base <string opc, bits<2> opcBits, Operand ImmOp,
2842                      string memOp, bits<2> memOpBits> :
2843       MEMInst_V4 <(outs),
2844                   (ins IntRegs:$base, ImmOp:$offset, u5Imm:$delta),
2845                   opc#"($base+#$offset)"#memOp#"#$delta"
2846                   #!if(memOpBits{1},")", ""), // clrbit, setbit - include ')'
2847                   []>,
2848                   Requires<[UseMEMOP]> {
2849
2850     bits<5> base;
2851     bits<5> delta;
2852     bits<32> offset;
2853     bits<6> offsetBits; // memb - u6:0 , memh - u6:1, memw - u6:2
2854
2855     let offsetBits = !if (!eq(opcBits, 0b00), offset{5-0},
2856                      !if (!eq(opcBits, 0b01), offset{6-1},
2857                      !if (!eq(opcBits, 0b10), offset{7-2},0)));
2858
2859     let opExtentAlign = opcBits;
2860     let IClass = 0b0011;
2861     let Inst{27-24} = 0b1111;
2862     let Inst{22-21} = opcBits;
2863     let Inst{20-16} = base;
2864     let Inst{13} = 0b0;
2865     let Inst{12-7} = offsetBits;
2866     let Inst{6-5} = memOpBits;
2867     let Inst{4-0} = delta;
2868 }
2869
2870 // multiclass to define MemOp instructions with register operand.
2871 multiclass MemOp_rr<string opc, bits<2> opcBits, Operand ImmOp> {
2872   def L4_add#NAME : MemOp_rr_base <opc, opcBits, ImmOp, " += ", 0b00>; // add
2873   def L4_sub#NAME : MemOp_rr_base <opc, opcBits, ImmOp, " -= ", 0b01>; // sub
2874   def L4_and#NAME : MemOp_rr_base <opc, opcBits, ImmOp, " &= ", 0b10>; // and
2875   def L4_or#NAME  : MemOp_rr_base <opc, opcBits, ImmOp, " |= ", 0b11>; // or
2876 }
2877
2878 // multiclass to define MemOp instructions with immediate Operand.
2879 multiclass MemOp_ri<string opc, bits<2> opcBits, Operand ImmOp> {
2880   def L4_iadd#NAME : MemOp_ri_base <opc, opcBits, ImmOp, " += ", 0b00 >;
2881   def L4_isub#NAME : MemOp_ri_base <opc, opcBits, ImmOp, " -= ", 0b01 >;
2882   def L4_iand#NAME : MemOp_ri_base<opc, opcBits, ImmOp, " = clrbit(", 0b10>;
2883   def L4_ior#NAME : MemOp_ri_base<opc, opcBits, ImmOp, " = setbit(", 0b11>;
2884 }
2885
2886 multiclass MemOp_base <string opc, bits<2> opcBits, Operand ImmOp> {
2887   defm _#NAME : MemOp_rr <opc, opcBits, ImmOp>;
2888   defm _#NAME : MemOp_ri <opc, opcBits, ImmOp>;
2889 }
2890
2891 // Define MemOp instructions.
2892 let isExtendable = 1, opExtendable = 1, isExtentSigned = 0 in {
2893   let opExtentBits = 6, accessSize = ByteAccess in
2894   defm memopb_io : MemOp_base <"memb", 0b00, u6_0Ext>;
2895
2896   let opExtentBits = 7, accessSize = HalfWordAccess in
2897   defm memoph_io : MemOp_base <"memh", 0b01, u6_1Ext>;
2898
2899   let opExtentBits = 8, accessSize = WordAccess in
2900   defm memopw_io : MemOp_base <"memw", 0b10, u6_2Ext>;
2901 }
2902
2903 //===----------------------------------------------------------------------===//
2904 // Multiclass to define 'Def Pats' for ALU operations on the memory
2905 // Here value used for the ALU operation is an immediate value.
2906 // mem[bh](Rs+#0) += #U5
2907 // mem[bh](Rs+#u6) += #U5
2908 //===----------------------------------------------------------------------===//
2909
2910 multiclass MemOpi_u5Pats <PatFrag ldOp, PatFrag stOp, PatLeaf ExtPred,
2911                           InstHexagon MI, SDNode OpNode> {
2912   let AddedComplexity = 180 in
2913   def: Pat<(stOp (OpNode (ldOp IntRegs:$addr), u5ImmPred:$addend),
2914                   IntRegs:$addr),
2915             (MI IntRegs:$addr, 0, u5ImmPred:$addend)>;
2916
2917   let AddedComplexity = 190 in
2918   def: Pat<(stOp (OpNode (ldOp (add IntRegs:$base, ExtPred:$offset)),
2919                   u5ImmPred:$addend),
2920             (add IntRegs:$base, ExtPred:$offset)),
2921             (MI IntRegs:$base, ExtPred:$offset, u5ImmPred:$addend)>;
2922 }
2923
2924 multiclass MemOpi_u5ALUOp<PatFrag ldOp, PatFrag stOp, PatLeaf ExtPred,
2925                           InstHexagon addMI, InstHexagon subMI> {
2926   defm: MemOpi_u5Pats<ldOp, stOp, ExtPred, addMI, add>;
2927   defm: MemOpi_u5Pats<ldOp, stOp, ExtPred, subMI, sub>;
2928 }
2929
2930 multiclass MemOpi_u5ExtType<PatFrag ldOpByte, PatFrag ldOpHalf > {
2931   // Half Word
2932   defm: MemOpi_u5ALUOp <ldOpHalf, truncstorei16, u6_1ExtPred,
2933                         L4_iadd_memoph_io, L4_isub_memoph_io>;
2934   // Byte
2935   defm: MemOpi_u5ALUOp <ldOpByte, truncstorei8, u6ExtPred,
2936                         L4_iadd_memopb_io, L4_isub_memopb_io>;
2937 }
2938
2939 let Predicates = [UseMEMOP] in {
2940   defm: MemOpi_u5ExtType<zextloadi8, zextloadi16>; // zero extend
2941   defm: MemOpi_u5ExtType<sextloadi8, sextloadi16>; // sign extend
2942   defm: MemOpi_u5ExtType<extloadi8,  extloadi16>;  // any extend
2943
2944   // Word
2945   defm: MemOpi_u5ALUOp <load, store, u6_2ExtPred, L4_iadd_memopw_io,
2946                         L4_isub_memopw_io>;
2947 }
2948
2949 //===----------------------------------------------------------------------===//
2950 // multiclass to define 'Def Pats' for ALU operations on the memory.
2951 // Here value used for the ALU operation is a negative value.
2952 // mem[bh](Rs+#0) += #m5
2953 // mem[bh](Rs+#u6) += #m5
2954 //===----------------------------------------------------------------------===//
2955
2956 multiclass MemOpi_m5Pats <PatFrag ldOp, PatFrag stOp, PatLeaf extPred,
2957                           PatLeaf immPred, SDNodeXForm xformFunc,
2958                           InstHexagon MI> {
2959   let AddedComplexity = 190 in
2960   def: Pat<(stOp (add (ldOp IntRegs:$addr), immPred:$subend), IntRegs:$addr),
2961            (MI IntRegs:$addr, 0, (xformFunc immPred:$subend))>;
2962
2963   let AddedComplexity = 195 in
2964   def: Pat<(stOp (add (ldOp (add IntRegs:$base, extPred:$offset)),
2965                   immPred:$subend),
2966            (add IntRegs:$base, extPred:$offset)),
2967            (MI IntRegs:$base, extPred:$offset, (xformFunc immPred:$subend))>;
2968 }
2969
2970 multiclass MemOpi_m5ExtType<PatFrag ldOpByte, PatFrag ldOpHalf > {
2971   // Half Word
2972   defm: MemOpi_m5Pats <ldOpHalf, truncstorei16, u6_1ExtPred, m5HImmPred,
2973                        MEMOPIMM_HALF, L4_isub_memoph_io>;
2974   // Byte
2975   defm: MemOpi_m5Pats <ldOpByte, truncstorei8, u6ExtPred, m5BImmPred,
2976                        MEMOPIMM_BYTE, L4_isub_memopb_io>;
2977 }
2978
2979 let Predicates = [UseMEMOP] in {
2980   defm: MemOpi_m5ExtType<zextloadi8, zextloadi16>; // zero extend
2981   defm: MemOpi_m5ExtType<sextloadi8, sextloadi16>; // sign extend
2982   defm: MemOpi_m5ExtType<extloadi8,  extloadi16>;  // any extend
2983
2984   // Word
2985   defm: MemOpi_m5Pats <load, store, u6_2ExtPred, m5ImmPred,
2986                        MEMOPIMM, L4_isub_memopw_io>;
2987 }
2988
2989 //===----------------------------------------------------------------------===//
2990 // Multiclass to define 'def Pats' for bit operations on the memory.
2991 // mem[bhw](Rs+#0) = [clrbit|setbit](#U5)
2992 // mem[bhw](Rs+#u6) = [clrbit|setbit](#U5)
2993 //===----------------------------------------------------------------------===//
2994
2995 multiclass MemOpi_bitPats <PatFrag ldOp, PatFrag stOp, PatLeaf immPred,
2996                      PatLeaf extPred, SDNodeXForm xformFunc, InstHexagon MI,
2997                      SDNode OpNode> {
2998
2999   // mem[bhw](Rs+#u6:[012]) = [clrbit|setbit](#U5)
3000   let AddedComplexity = 250 in
3001   def: Pat<(stOp (OpNode (ldOp (add IntRegs:$base, extPred:$offset)),
3002                   immPred:$bitend),
3003            (add IntRegs:$base, extPred:$offset)),
3004            (MI IntRegs:$base, extPred:$offset, (xformFunc immPred:$bitend))>;
3005
3006   // mem[bhw](Rs+#0) = [clrbit|setbit](#U5)
3007   let AddedComplexity = 225 in
3008   def: Pat<(stOp (OpNode (ldOp IntRegs:$addr), immPred:$bitend), IntRegs:$addr),
3009            (MI IntRegs:$addr, 0, (xformFunc immPred:$bitend))>;
3010 }
3011
3012 multiclass MemOpi_bitExtType<PatFrag ldOpByte, PatFrag ldOpHalf> {
3013   // Byte - clrbit
3014   defm: MemOpi_bitPats<ldOpByte, truncstorei8, Clr3ImmPred, u6ExtPred,
3015                        CLRMEMIMM_BYTE, L4_iand_memopb_io, and>;
3016   // Byte - setbit
3017   defm: MemOpi_bitPats<ldOpByte, truncstorei8, Set3ImmPred, u6ExtPred,
3018                        SETMEMIMM_BYTE, L4_ior_memopb_io, or>;
3019   // Half Word - clrbit
3020   defm: MemOpi_bitPats<ldOpHalf, truncstorei16, Clr4ImmPred, u6_1ExtPred,
3021                        CLRMEMIMM_SHORT, L4_iand_memoph_io, and>;
3022   // Half Word - setbit
3023   defm: MemOpi_bitPats<ldOpHalf, truncstorei16, Set4ImmPred, u6_1ExtPred,
3024                        SETMEMIMM_SHORT, L4_ior_memoph_io, or>;
3025 }
3026
3027 let Predicates = [UseMEMOP] in {
3028   // mem[bh](Rs+#0) = [clrbit|setbit](#U5)
3029   // mem[bh](Rs+#u6:[01]) = [clrbit|setbit](#U5)
3030   defm: MemOpi_bitExtType<zextloadi8, zextloadi16>; // zero extend
3031   defm: MemOpi_bitExtType<sextloadi8, sextloadi16>; // sign extend
3032   defm: MemOpi_bitExtType<extloadi8,  extloadi16>;  // any extend
3033
3034   // memw(Rs+#0) = [clrbit|setbit](#U5)
3035   // memw(Rs+#u6:2) = [clrbit|setbit](#U5)
3036   defm: MemOpi_bitPats<load, store, Clr5ImmPred, u6_2ExtPred, CLRMEMIMM,
3037                        L4_iand_memopw_io, and>;
3038   defm: MemOpi_bitPats<load, store, Set5ImmPred, u6_2ExtPred, SETMEMIMM,
3039                        L4_ior_memopw_io, or>;
3040 }
3041
3042 //===----------------------------------------------------------------------===//
3043 // Multiclass to define 'def Pats' for ALU operations on the memory
3044 // where addend is a register.
3045 // mem[bhw](Rs+#0) [+-&|]= Rt
3046 // mem[bhw](Rs+#U6:[012]) [+-&|]= Rt
3047 //===----------------------------------------------------------------------===//
3048
3049 multiclass MemOpr_Pats <PatFrag ldOp, PatFrag stOp, PatLeaf extPred,
3050                         InstHexagon MI, SDNode OpNode> {
3051   let AddedComplexity = 141 in
3052   // mem[bhw](Rs+#0) [+-&|]= Rt
3053   def: Pat<(stOp (OpNode (ldOp IntRegs:$addr), (i32 IntRegs:$addend)),
3054                  IntRegs:$addr),
3055            (MI IntRegs:$addr, 0, (i32 IntRegs:$addend))>;
3056
3057   // mem[bhw](Rs+#U6:[012]) [+-&|]= Rt
3058   let AddedComplexity = 150 in
3059   def: Pat<(stOp (OpNode (ldOp (add IntRegs:$base, extPred:$offset)),
3060                   (i32 IntRegs:$orend)),
3061            (add IntRegs:$base, extPred:$offset)),
3062            (MI IntRegs:$base, extPred:$offset, (i32 IntRegs:$orend))>;
3063 }
3064
3065 multiclass MemOPr_ALUOp<PatFrag ldOp, PatFrag stOp, PatLeaf extPred,
3066                         InstHexagon addMI, InstHexagon subMI,
3067                         InstHexagon andMI, InstHexagon orMI> {
3068   defm: MemOpr_Pats <ldOp, stOp, extPred, addMI, add>;
3069   defm: MemOpr_Pats <ldOp, stOp, extPred, subMI, sub>;
3070   defm: MemOpr_Pats <ldOp, stOp, extPred, andMI, and>;
3071   defm: MemOpr_Pats <ldOp, stOp, extPred, orMI,  or>;
3072 }
3073
3074 multiclass MemOPr_ExtType<PatFrag ldOpByte, PatFrag ldOpHalf > {
3075   // Half Word
3076   defm: MemOPr_ALUOp <ldOpHalf, truncstorei16, u6_1ExtPred,
3077                       L4_add_memoph_io, L4_sub_memoph_io,
3078                       L4_and_memoph_io, L4_or_memoph_io>;
3079   // Byte
3080   defm: MemOPr_ALUOp <ldOpByte, truncstorei8, u6ExtPred,
3081                       L4_add_memopb_io, L4_sub_memopb_io,
3082                       L4_and_memopb_io, L4_or_memopb_io>;
3083 }
3084
3085 // Define 'def Pats' for MemOps with register addend.
3086 let Predicates = [UseMEMOP] in {
3087   // Byte, Half Word
3088   defm: MemOPr_ExtType<zextloadi8, zextloadi16>; // zero extend
3089   defm: MemOPr_ExtType<sextloadi8, sextloadi16>; // sign extend
3090   defm: MemOPr_ExtType<extloadi8,  extloadi16>;  // any extend
3091   // Word
3092   defm: MemOPr_ALUOp <load, store, u6_2ExtPred, L4_add_memopw_io,
3093                       L4_sub_memopw_io, L4_and_memopw_io, L4_or_memopw_io>;
3094 }
3095
3096 //===----------------------------------------------------------------------===//
3097 // XTYPE/PRED +
3098 //===----------------------------------------------------------------------===//
3099
3100 // Hexagon V4 only supports these flavors of byte/half compare instructions:
3101 // EQ/GT/GTU. Other flavors like GE/GEU/LT/LTU/LE/LEU are not supported by
3102 // hardware. However, compiler can still implement these patterns through
3103 // appropriate patterns combinations based on current implemented patterns.
3104 // The implemented patterns are: EQ/GT/GTU.
3105 // Missing patterns are: GE/GEU/LT/LTU/LE/LEU.
3106
3107 // Following instruction is not being extended as it results into the
3108 // incorrect code for negative numbers.
3109 // Pd=cmpb.eq(Rs,#u8)
3110
3111 // p=!cmp.eq(r1,#s10)
3112 def C4_cmpneqi  : T_CMP <"cmp.eq",  0b00, 1, s10Ext>;
3113 def C4_cmpltei  : T_CMP <"cmp.gt",  0b01, 1, s10Ext>;
3114 def C4_cmplteui : T_CMP <"cmp.gtu", 0b10, 1, u9Ext>;
3115
3116 def : T_CMP_pat <C4_cmpneqi,  setne,  s10ExtPred>;
3117 def : T_CMP_pat <C4_cmpltei,  setle,  s10ExtPred>;
3118 def : T_CMP_pat <C4_cmplteui, setule, u9ImmPred>;
3119
3120 // rs <= rt -> !(rs > rt).
3121 /*
3122 def: Pat<(i1 (setle (i32 IntRegs:$src1), s10ExtPred:$src2)),
3123          (C2_not (C2_cmpgti IntRegs:$src1, s10ExtPred:$src2))>;
3124 //         (C4_cmpltei IntRegs:$src1, s10ExtPred:$src2)>;
3125 */
3126 // Map cmplt(Rs, Imm) -> !cmpgt(Rs, Imm-1).
3127 def: Pat<(i1 (setlt (i32 IntRegs:$src1), s8ExtPred:$src2)),
3128          (C4_cmpltei IntRegs:$src1, (DEC_CONST_SIGNED s8ExtPred:$src2))>;
3129
3130 // rs != rt -> !(rs == rt).
3131 def: Pat<(i1 (setne (i32 IntRegs:$src1), s10ExtPred:$src2)),
3132          (C4_cmpneqi IntRegs:$src1, s10ExtPred:$src2)>;
3133
3134 // SDNode for converting immediate C to C-1.
3135 def DEC_CONST_BYTE : SDNodeXForm<imm, [{
3136    // Return the byte immediate const-1 as an SDNode.
3137    int32_t imm = N->getSExtValue();
3138    return XformU7ToU7M1Imm(imm);
3139 }]>;
3140
3141 // For the sequence
3142 //   zext( setult ( and(Rs, 255), u8))
3143 // Use the isdigit transformation below
3144
3145 // Generate code of the form 'C2_muxii(cmpbgtui(Rdd, C-1),0,1)'
3146 // for C code of the form r = ((c>='0') & (c<='9')) ? 1 : 0;.
3147 // The isdigit transformation relies on two 'clever' aspects:
3148 // 1) The data type is unsigned which allows us to eliminate a zero test after
3149 //    biasing the expression by 48. We are depending on the representation of
3150 //    the unsigned types, and semantics.
3151 // 2) The front end has converted <= 9 into < 10 on entry to LLVM
3152 //
3153 // For the C code:
3154 //   retval = ((c>='0') & (c<='9')) ? 1 : 0;
3155 // The code is transformed upstream of llvm into
3156 //   retval = (c-48) < 10 ? 1 : 0;
3157 let AddedComplexity = 139 in
3158 def: Pat<(i32 (zext (i1 (setult (i32 (and (i32 IntRegs:$src1), 255)),
3159                          u7StrictPosImmPred:$src2)))),
3160          (C2_muxii (A4_cmpbgtui IntRegs:$src1,
3161                     (DEC_CONST_BYTE u7StrictPosImmPred:$src2)),
3162           0, 1)>;
3163
3164 //===----------------------------------------------------------------------===//
3165 // XTYPE/PRED -
3166 //===----------------------------------------------------------------------===//
3167
3168 //===----------------------------------------------------------------------===//
3169 // Multiclass for DeallocReturn
3170 //===----------------------------------------------------------------------===//
3171 class L4_RETURN<string mnemonic, bit isNot, bit isPredNew, bit isTak>
3172   : LD0Inst<(outs), (ins PredRegs:$src),
3173   !if(isNot, "if (!$src", "if ($src")#
3174   !if(isPredNew, ".new) ", ") ")#mnemonic#
3175   !if(isPredNew, #!if(isTak,":t", ":nt"),""),
3176   [], "", LD_tc_3or4stall_SLOT0> {
3177
3178     bits<2> src;
3179     let BaseOpcode = "L4_RETURN";
3180     let isPredicatedFalse = isNot;
3181     let isPredicatedNew = isPredNew;
3182     let isTaken = isTak;
3183     let IClass = 0b1001;
3184
3185     let Inst{27-16} = 0b011000011110;
3186
3187     let Inst{13} = isNot;
3188     let Inst{12} = isTak;
3189     let Inst{11} = isPredNew;
3190     let Inst{10} = 0b0;
3191     let Inst{9-8} = src;
3192     let Inst{4-0} = 0b11110;
3193   }
3194
3195 // Produce all predicated forms, p, !p, p.new, !p.new, :t, :nt
3196 multiclass L4_RETURN_PRED<string mnemonic, bit PredNot> {
3197   let isPredicated = 1 in {
3198     def _#NAME# : L4_RETURN <mnemonic, PredNot, 0, 1>;
3199     def _#NAME#new_pnt : L4_RETURN <mnemonic, PredNot, 1, 0>;
3200     def _#NAME#new_pt : L4_RETURN <mnemonic, PredNot, 1, 1>;
3201   }
3202 }
3203
3204 multiclass LD_MISC_L4_RETURN<string mnemonic> {
3205   let isBarrier = 1, isPredicable = 1 in
3206     def NAME : LD0Inst <(outs), (ins), mnemonic, [], "",
3207                         LD_tc_3or4stall_SLOT0> {
3208       let BaseOpcode = "L4_RETURN";
3209       let IClass = 0b1001;
3210       let Inst{27-16} = 0b011000011110;
3211       let Inst{13-10} = 0b0000;
3212       let Inst{4-0} = 0b11110;
3213     }
3214   defm t : L4_RETURN_PRED<mnemonic, 0 >;
3215   defm f : L4_RETURN_PRED<mnemonic, 1 >;
3216 }
3217
3218 let isReturn = 1, isTerminator = 1,
3219     Defs = [R29, R30, R31, PC], Uses = [R30], hasSideEffects = 0 in
3220 defm L4_return: LD_MISC_L4_RETURN <"dealloc_return">, PredNewRel;
3221
3222 // Restore registers and dealloc return function call.
3223 let isCall = 1, isBarrier = 1, isReturn = 1, isTerminator = 1,
3224     Defs = [R29, R30, R31, PC], isPredicable = 0, isAsmParserOnly = 1 in {
3225   def RESTORE_DEALLOC_RET_JMP_V4 : JInst<(outs),
3226                                    (ins calltarget:$dst),
3227              "jump $dst",
3228              []>;
3229 }
3230
3231 // Restore registers and dealloc frame before a tail call.
3232 let isCall = 1, Defs = [R29, R30, R31, PC], isAsmParserOnly = 1 in {
3233   def RESTORE_DEALLOC_BEFORE_TAILCALL_V4 : JInst<(outs),
3234                                            (ins calltarget:$dst),
3235              "call $dst",
3236              []>;
3237 }
3238
3239 // Save registers function call.
3240 let isCall = 1, Uses = [R29, R31], isAsmParserOnly = 1 in {
3241   def SAVE_REGISTERS_CALL_V4 : JInst<(outs),
3242                                (ins calltarget:$dst),
3243              "call $dst // Save_calle_saved_registers",
3244              []>;
3245 }
3246
3247 //===----------------------------------------------------------------------===//
3248 // Template class for non predicated store instructions with
3249 // GP-Relative or absolute addressing.
3250 //===----------------------------------------------------------------------===//
3251 let hasSideEffects = 0, isPredicable = 1, isNVStorable = 1 in
3252 class T_StoreAbsGP <string mnemonic, RegisterClass RC, Operand ImmOp,
3253                     bits<2>MajOp, Operand AddrOp, bit isAbs, bit isHalf>
3254   : STInst<(outs), (ins AddrOp:$addr, RC:$src),
3255   mnemonic # !if(isAbs, "(##", "(#")#"$addr) = $src"#!if(isHalf, ".h",""),
3256   [], "", V2LDST_tc_st_SLOT01> {
3257     bits<19> addr;
3258     bits<5> src;
3259     bits<16> offsetBits;
3260
3261     string ImmOpStr = !cast<string>(ImmOp);
3262     let offsetBits = !if (!eq(ImmOpStr, "u16_3Imm"), addr{18-3},
3263                      !if (!eq(ImmOpStr, "u16_2Imm"), addr{17-2},
3264                      !if (!eq(ImmOpStr, "u16_1Imm"), addr{16-1},
3265                                       /* u16_0Imm */ addr{15-0})));
3266     let IClass = 0b0100;
3267     let Inst{27} = 1;
3268     let Inst{26-25} = offsetBits{15-14};
3269     let Inst{24}    = 0b0;
3270     let Inst{23-22} = MajOp;
3271     let Inst{21}    = isHalf;
3272     let Inst{20-16} = offsetBits{13-9};
3273     let Inst{13}    = offsetBits{8};
3274     let Inst{12-8}  = src;
3275     let Inst{7-0}   = offsetBits{7-0};
3276   }
3277
3278 //===----------------------------------------------------------------------===//
3279 // Template class for predicated store instructions with
3280 // GP-Relative or absolute addressing.
3281 //===----------------------------------------------------------------------===//
3282 let hasSideEffects = 0, isPredicated = 1, isNVStorable = 1, opExtentBits = 6,
3283     opExtendable = 1 in
3284 class T_StoreAbs_Pred <string mnemonic, RegisterClass RC, bits<2> MajOp,
3285                        bit isHalf, bit isNot, bit isNew>
3286   : STInst<(outs), (ins PredRegs:$src1, u6Ext:$absaddr, RC: $src2),
3287   !if(isNot, "if (!$src1", "if ($src1")#!if(isNew, ".new) ",
3288   ") ")#mnemonic#"(#$absaddr) = $src2"#!if(isHalf, ".h",""),
3289   [], "", ST_tc_st_SLOT01>, AddrModeRel {
3290     bits<2> src1;
3291     bits<6> absaddr;
3292     bits<5> src2;
3293
3294     let isPredicatedNew = isNew;
3295     let isPredicatedFalse = isNot;
3296
3297     let IClass = 0b1010;
3298
3299     let Inst{27-24} = 0b1111;
3300     let Inst{23-22} = MajOp;
3301     let Inst{21}    = isHalf;
3302     let Inst{17-16} = absaddr{5-4};
3303     let Inst{13}    = isNew;
3304     let Inst{12-8}  = src2;
3305     let Inst{7}     = 0b1;
3306     let Inst{6-3}   = absaddr{3-0};
3307     let Inst{2}     = isNot;
3308     let Inst{1-0}   = src1;
3309   }
3310
3311 //===----------------------------------------------------------------------===//
3312 // Template class for predicated store instructions with absolute addressing.
3313 //===----------------------------------------------------------------------===//
3314 class T_StoreAbs <string mnemonic, RegisterClass RC, Operand ImmOp,
3315                  bits<2> MajOp, bit isHalf>
3316   : T_StoreAbsGP <mnemonic, RC, ImmOp, MajOp, u0AlwaysExt, 1, isHalf>,
3317                   AddrModeRel {
3318   string ImmOpStr = !cast<string>(ImmOp);
3319   let opExtentBits = !if (!eq(ImmOpStr, "u16_3Imm"), 19,
3320                      !if (!eq(ImmOpStr, "u16_2Imm"), 18,
3321                      !if (!eq(ImmOpStr, "u16_1Imm"), 17,
3322                                       /* u16_0Imm */ 16)));
3323
3324   let opExtentAlign = !if (!eq(ImmOpStr, "u16_3Imm"), 3,
3325                       !if (!eq(ImmOpStr, "u16_2Imm"), 2,
3326                       !if (!eq(ImmOpStr, "u16_1Imm"), 1,
3327                                        /* u16_0Imm */ 0)));
3328 }
3329
3330 //===----------------------------------------------------------------------===//
3331 // Multiclass for store instructions with absolute addressing.
3332 //===----------------------------------------------------------------------===//
3333 let addrMode = Absolute, isExtended = 1 in
3334 multiclass ST_Abs<string mnemonic, string CextOp, RegisterClass RC,
3335                   Operand ImmOp, bits<2> MajOp, bit isHalf = 0> {
3336   let CextOpcode = CextOp, BaseOpcode = CextOp#_abs in {
3337     let opExtendable = 0, isPredicable = 1 in
3338     def S2_#NAME#abs : T_StoreAbs <mnemonic, RC, ImmOp, MajOp, isHalf>;
3339
3340     // Predicated
3341     def S4_p#NAME#t_abs : T_StoreAbs_Pred<mnemonic, RC, MajOp, isHalf, 0, 0>;
3342     def S4_p#NAME#f_abs : T_StoreAbs_Pred<mnemonic, RC, MajOp, isHalf, 1, 0>;
3343
3344     // .new Predicated
3345     def S4_p#NAME#tnew_abs : T_StoreAbs_Pred<mnemonic, RC, MajOp, isHalf, 0, 1>;
3346     def S4_p#NAME#fnew_abs : T_StoreAbs_Pred<mnemonic, RC, MajOp, isHalf, 1, 1>;
3347   }
3348 }
3349
3350 //===----------------------------------------------------------------------===//
3351 // Template class for non predicated new-value store instructions with
3352 // GP-Relative or absolute addressing.
3353 //===----------------------------------------------------------------------===//
3354 let hasSideEffects = 0, isPredicable = 1, mayStore = 1, isNVStore = 1,
3355     isNewValue = 1, opNewValue = 1 in
3356 class T_StoreAbsGP_NV <string mnemonic, Operand ImmOp, bits<2>MajOp, bit isAbs>
3357   : NVInst_V4<(outs), (ins u0AlwaysExt:$addr, IntRegs:$src),
3358   mnemonic # !if(isAbs, "(##", "(#")#"$addr) = $src.new",
3359   [], "", V2LDST_tc_st_SLOT0> {
3360     bits<19> addr;
3361     bits<3> src;
3362     bits<16> offsetBits;
3363
3364     string ImmOpStr = !cast<string>(ImmOp);
3365     let offsetBits = !if (!eq(ImmOpStr, "u16_3Imm"), addr{18-3},
3366                      !if (!eq(ImmOpStr, "u16_2Imm"), addr{17-2},
3367                      !if (!eq(ImmOpStr, "u16_1Imm"), addr{16-1},
3368                                       /* u16_0Imm */ addr{15-0})));
3369     let IClass = 0b0100;
3370
3371     let Inst{27} = 1;
3372     let Inst{26-25} = offsetBits{15-14};
3373     let Inst{24-21} = 0b0101;
3374     let Inst{20-16} = offsetBits{13-9};
3375     let Inst{13}    = offsetBits{8};
3376     let Inst{12-11} = MajOp;
3377     let Inst{10-8}  = src;
3378     let Inst{7-0}   = offsetBits{7-0};
3379   }
3380
3381 //===----------------------------------------------------------------------===//
3382 // Template class for predicated new-value store instructions with
3383 // absolute addressing.
3384 //===----------------------------------------------------------------------===//
3385 let hasSideEffects = 0, isPredicated = 1, mayStore = 1, isNVStore = 1,
3386     isNewValue = 1, opNewValue = 2, opExtentBits = 6, opExtendable = 1 in
3387 class T_StoreAbs_NV_Pred <string mnemonic, bits<2> MajOp, bit isNot, bit isNew>
3388   : NVInst_V4<(outs), (ins PredRegs:$src1, u6Ext:$absaddr, IntRegs:$src2),
3389   !if(isNot, "if (!$src1", "if ($src1")#!if(isNew, ".new) ",
3390   ") ")#mnemonic#"(#$absaddr) = $src2.new",
3391   [], "", ST_tc_st_SLOT0>, AddrModeRel {
3392     bits<2> src1;
3393     bits<6> absaddr;
3394     bits<3> src2;
3395
3396     let isPredicatedNew = isNew;
3397     let isPredicatedFalse = isNot;
3398
3399     let IClass = 0b1010;
3400
3401     let Inst{27-24} = 0b1111;
3402     let Inst{23-21} = 0b101;
3403     let Inst{17-16} = absaddr{5-4};
3404     let Inst{13}    = isNew;
3405     let Inst{12-11} = MajOp;
3406     let Inst{10-8}  = src2;
3407     let Inst{7}     = 0b1;
3408     let Inst{6-3}   = absaddr{3-0};
3409     let Inst{2}     = isNot;
3410     let Inst{1-0}   = src1;
3411 }
3412
3413 //===----------------------------------------------------------------------===//
3414 // Template class for non-predicated new-value store instructions with
3415 // absolute addressing.
3416 //===----------------------------------------------------------------------===//
3417 class T_StoreAbs_NV <string mnemonic, Operand ImmOp, bits<2> MajOp>
3418   : T_StoreAbsGP_NV <mnemonic, ImmOp, MajOp, 1>, AddrModeRel {
3419
3420   string ImmOpStr = !cast<string>(ImmOp);
3421   let opExtentBits = !if (!eq(ImmOpStr, "u16_3Imm"), 19,
3422                      !if (!eq(ImmOpStr, "u16_2Imm"), 18,
3423                      !if (!eq(ImmOpStr, "u16_1Imm"), 17,
3424                                       /* u16_0Imm */ 16)));
3425
3426   let opExtentAlign = !if (!eq(ImmOpStr, "u16_3Imm"), 3,
3427                       !if (!eq(ImmOpStr, "u16_2Imm"), 2,
3428                       !if (!eq(ImmOpStr, "u16_1Imm"), 1,
3429                                        /* u16_0Imm */ 0)));
3430 }
3431
3432 //===----------------------------------------------------------------------===//
3433 // Multiclass for new-value store instructions with absolute addressing.
3434 //===----------------------------------------------------------------------===//
3435 let addrMode = Absolute, isExtended = 1  in
3436 multiclass ST_Abs_NV <string mnemonic, string CextOp, Operand ImmOp,
3437                    bits<2> MajOp> {
3438   let CextOpcode = CextOp, BaseOpcode = CextOp#_abs in {
3439     let opExtendable = 0, isPredicable = 1 in
3440     def S2_#NAME#newabs : T_StoreAbs_NV <mnemonic, ImmOp, MajOp>;
3441
3442     // Predicated
3443     def S4_p#NAME#newt_abs  : T_StoreAbs_NV_Pred <mnemonic, MajOp, 0, 0>;
3444     def S4_p#NAME#newf_abs  : T_StoreAbs_NV_Pred <mnemonic, MajOp, 1, 0>;
3445
3446     // .new Predicated
3447     def S4_p#NAME#newtnew_abs : T_StoreAbs_NV_Pred <mnemonic, MajOp, 0, 1>;
3448     def S4_p#NAME#newfnew_abs : T_StoreAbs_NV_Pred <mnemonic, MajOp, 1, 1>;
3449   }
3450 }
3451
3452 //===----------------------------------------------------------------------===//
3453 // Stores with absolute addressing
3454 //===----------------------------------------------------------------------===//
3455 let accessSize = ByteAccess in
3456 defm storerb : ST_Abs    <"memb", "STrib", IntRegs, u16_0Imm, 0b00>,
3457                ST_Abs_NV <"memb", "STrib", u16_0Imm, 0b00>;
3458
3459 let accessSize = HalfWordAccess in
3460 defm storerh : ST_Abs    <"memh", "STrih", IntRegs, u16_1Imm, 0b01>,
3461                ST_Abs_NV <"memh", "STrih", u16_1Imm, 0b01>;
3462
3463 let accessSize = WordAccess in
3464 defm storeri : ST_Abs    <"memw", "STriw", IntRegs, u16_2Imm, 0b10>,
3465                ST_Abs_NV <"memw", "STriw", u16_2Imm, 0b10>;
3466
3467 let isNVStorable = 0, accessSize = DoubleWordAccess in
3468 defm storerd : ST_Abs <"memd", "STrid", DoubleRegs, u16_3Imm, 0b11>;
3469
3470 let isNVStorable = 0, accessSize = HalfWordAccess in
3471 defm storerf : ST_Abs <"memh", "STrif", IntRegs, u16_1Imm, 0b01, 1>;
3472
3473 //===----------------------------------------------------------------------===//
3474 // GP-relative stores.
3475 // mem[bhwd](#global)=Rt
3476 // Once predicated, these instructions map to absolute addressing mode.
3477 // if ([!]Pv[.new]) mem[bhwd](##global)=Rt
3478 //===----------------------------------------------------------------------===//
3479
3480 let isAsmParserOnly = 1 in
3481 class T_StoreGP <string mnemonic, string BaseOp, RegisterClass RC,
3482                  Operand ImmOp, bits<2> MajOp, bit isHalf = 0>
3483   : T_StoreAbsGP <mnemonic, RC, ImmOp, MajOp, globaladdress, 0, isHalf> {
3484     // Set BaseOpcode same as absolute addressing instructions so that
3485     // non-predicated GP-Rel instructions can have relate with predicated
3486     // Absolute instruction.
3487     let BaseOpcode = BaseOp#_abs;
3488   }
3489
3490 let isAsmParserOnly = 1 in
3491 multiclass ST_GP <string mnemonic, string BaseOp, Operand ImmOp,
3492                   bits<2> MajOp, bit isHalf = 0> {
3493   // Set BaseOpcode same as absolute addressing instructions so that
3494   // non-predicated GP-Rel instructions can have relate with predicated
3495   // Absolute instruction.
3496   let BaseOpcode = BaseOp#_abs in {
3497     def NAME#gp : T_StoreAbsGP <mnemonic, IntRegs, ImmOp, MajOp,
3498                                 globaladdress, 0, isHalf>;
3499     // New-value store
3500     def NAME#newgp : T_StoreAbsGP_NV <mnemonic, ImmOp, MajOp, 0> ;
3501   }
3502 }
3503
3504 let accessSize = ByteAccess in
3505 defm S2_storerb : ST_GP<"memb", "STrib", u16_0Imm, 0b00>, NewValueRel;
3506
3507 let accessSize = HalfWordAccess in
3508 defm S2_storerh : ST_GP<"memh", "STrih", u16_1Imm, 0b01>, NewValueRel;
3509
3510 let accessSize = WordAccess in
3511 defm S2_storeri : ST_GP<"memw", "STriw", u16_2Imm, 0b10>, NewValueRel;
3512
3513 let isNVStorable = 0, accessSize = DoubleWordAccess in
3514 def S2_storerdgp : T_StoreGP <"memd", "STrid", DoubleRegs,
3515                               u16_3Imm, 0b11>, PredNewRel;
3516
3517 let isNVStorable = 0, accessSize = HalfWordAccess in
3518 def S2_storerfgp : T_StoreGP <"memh", "STrif", IntRegs,
3519                               u16_1Imm, 0b01, 1>, PredNewRel;
3520
3521 class Loada_pat<PatFrag Load, ValueType VT, PatFrag Addr, InstHexagon MI>
3522   : Pat<(VT (Load Addr:$addr)), (MI Addr:$addr)>;
3523
3524 class Loadam_pat<PatFrag Load, ValueType VT, PatFrag Addr, PatFrag ValueMod,
3525                  InstHexagon MI>
3526   : Pat<(VT (Load Addr:$addr)), (ValueMod (MI Addr:$addr))>;
3527
3528 class Storea_pat<PatFrag Store, PatFrag Value, PatFrag Addr, InstHexagon MI>
3529   : Pat<(Store Value:$val, Addr:$addr), (MI Addr:$addr, Value:$val)>;
3530
3531 class Stoream_pat<PatFrag Store, PatFrag Value, PatFrag Addr, PatFrag ValueMod,
3532                   InstHexagon MI>
3533   : Pat<(Store Value:$val, Addr:$addr),
3534         (MI Addr:$addr, (ValueMod Value:$val))>;
3535
3536 def: Storea_pat<SwapSt<atomic_store_8>,  I32, addrgp, S2_storerbgp>;
3537 def: Storea_pat<SwapSt<atomic_store_16>, I32, addrgp, S2_storerhgp>;
3538 def: Storea_pat<SwapSt<atomic_store_32>, I32, addrgp, S2_storerigp>;
3539 def: Storea_pat<SwapSt<atomic_store_64>, I64, addrgp, S2_storerdgp>;
3540
3541 let AddedComplexity = 100 in {
3542   def: Storea_pat<truncstorei8,  I32, addrgp, S2_storerbgp>;
3543   def: Storea_pat<truncstorei16, I32, addrgp, S2_storerhgp>;
3544   def: Storea_pat<store,         I32, addrgp, S2_storerigp>;
3545   def: Storea_pat<store,         I64, addrgp, S2_storerdgp>;
3546
3547   // Map from "i1 = constant<-1>; memw(CONST32(#foo)) = i1"
3548   //       to "r0 = 1; memw(#foo) = r0"
3549   let AddedComplexity = 100 in
3550   def: Pat<(store (i1 -1), (HexagonCONST32_GP tglobaladdr:$global)),
3551            (S2_storerbgp tglobaladdr:$global, (A2_tfrsi 1))>;
3552 }
3553
3554 //===----------------------------------------------------------------------===//
3555 // Template class for non predicated load instructions with
3556 // absolute addressing mode.
3557 //===----------------------------------------------------------------------===//
3558 let isPredicable = 1, hasSideEffects = 0 in
3559 class T_LoadAbsGP <string mnemonic, RegisterClass RC, Operand ImmOp,
3560                    bits<3> MajOp, Operand AddrOp, bit isAbs>
3561   : LDInst <(outs RC:$dst), (ins AddrOp:$addr),
3562   "$dst = "#mnemonic# !if(isAbs, "(##", "(#")#"$addr)",
3563   [], "", V2LDST_tc_ld_SLOT01> {
3564     bits<5> dst;
3565     bits<19> addr;
3566     bits<16> offsetBits;
3567
3568     string ImmOpStr = !cast<string>(ImmOp);
3569     let offsetBits = !if (!eq(ImmOpStr, "u16_3Imm"), addr{18-3},
3570                      !if (!eq(ImmOpStr, "u16_2Imm"), addr{17-2},
3571                      !if (!eq(ImmOpStr, "u16_1Imm"), addr{16-1},
3572                                       /* u16_0Imm */ addr{15-0})));
3573
3574     let IClass = 0b0100;
3575
3576     let Inst{27}    = 0b1;
3577     let Inst{26-25} = offsetBits{15-14};
3578     let Inst{24}    = 0b1;
3579     let Inst{23-21} = MajOp;
3580     let Inst{20-16} = offsetBits{13-9};
3581     let Inst{13-5}  = offsetBits{8-0};
3582     let Inst{4-0}   = dst;
3583   }
3584
3585 class T_LoadAbs <string mnemonic, RegisterClass RC, Operand ImmOp,
3586                  bits<3> MajOp>
3587   : T_LoadAbsGP <mnemonic, RC, ImmOp, MajOp, u0AlwaysExt, 1>, AddrModeRel {
3588
3589     string ImmOpStr = !cast<string>(ImmOp);
3590     let opExtentBits = !if (!eq(ImmOpStr, "u16_3Imm"), 19,
3591                        !if (!eq(ImmOpStr, "u16_2Imm"), 18,
3592                        !if (!eq(ImmOpStr, "u16_1Imm"), 17,
3593                                         /* u16_0Imm */ 16)));
3594
3595     let opExtentAlign = !if (!eq(ImmOpStr, "u16_3Imm"), 3,
3596                         !if (!eq(ImmOpStr, "u16_2Imm"), 2,
3597                         !if (!eq(ImmOpStr, "u16_1Imm"), 1,
3598                                         /* u16_0Imm */ 0)));
3599   }
3600
3601 //===----------------------------------------------------------------------===//
3602 // Template class for predicated load instructions with
3603 // absolute addressing mode.
3604 //===----------------------------------------------------------------------===//
3605 let isPredicated = 1, opExtentBits = 6, opExtendable = 2 in
3606 class T_LoadAbs_Pred <string mnemonic, RegisterClass RC, bits<3> MajOp,
3607                       bit isPredNot, bit isPredNew>
3608   : LDInst <(outs RC:$dst), (ins PredRegs:$src1, u6Ext:$absaddr),
3609   !if(isPredNot, "if (!$src1", "if ($src1")#!if(isPredNew, ".new) ",
3610   ") ")#"$dst = "#mnemonic#"(#$absaddr)">, AddrModeRel {
3611     bits<5> dst;
3612     bits<2> src1;
3613     bits<6> absaddr;
3614
3615     let isPredicatedNew = isPredNew;
3616     let isPredicatedFalse = isPredNot;
3617     let hasNewValue = !if (!eq(!cast<string>(RC), "DoubleRegs"), 0, 1);
3618
3619     let IClass = 0b1001;
3620
3621     let Inst{27-24} = 0b1111;
3622     let Inst{23-21} = MajOp;
3623     let Inst{20-16} = absaddr{5-1};
3624     let Inst{13} = 0b1;
3625     let Inst{12} = isPredNew;
3626     let Inst{11} = isPredNot;
3627     let Inst{10-9} = src1;
3628     let Inst{8} = absaddr{0};
3629     let Inst{7} = 0b1;
3630     let Inst{4-0} = dst;
3631   }
3632
3633 //===----------------------------------------------------------------------===//
3634 // Multiclass for the load instructions with absolute addressing mode.
3635 //===----------------------------------------------------------------------===//
3636 multiclass LD_Abs_Pred<string mnemonic, RegisterClass RC, bits<3> MajOp,
3637                        bit PredNot> {
3638   def _abs : T_LoadAbs_Pred <mnemonic, RC, MajOp, PredNot, 0>;
3639   // Predicate new
3640   def new_abs : T_LoadAbs_Pred <mnemonic, RC, MajOp, PredNot, 1>;
3641 }
3642
3643 let addrMode = Absolute, isExtended = 1 in
3644 multiclass LD_Abs<string mnemonic, string CextOp, RegisterClass RC,
3645                   Operand ImmOp, bits<3> MajOp> {
3646   let CextOpcode = CextOp, BaseOpcode = CextOp#_abs in {
3647     let opExtendable = 1, isPredicable = 1 in
3648     def L4_#NAME#_abs: T_LoadAbs <mnemonic, RC, ImmOp, MajOp>;
3649
3650     // Predicated
3651     defm L4_p#NAME#t : LD_Abs_Pred<mnemonic, RC, MajOp, 0>;
3652     defm L4_p#NAME#f : LD_Abs_Pred<mnemonic, RC, MajOp, 1>;
3653   }
3654 }
3655
3656 let accessSize = ByteAccess, hasNewValue = 1 in {
3657   defm loadrb  : LD_Abs<"memb",  "LDrib",  IntRegs, u16_0Imm, 0b000>;
3658   defm loadrub : LD_Abs<"memub", "LDriub", IntRegs, u16_0Imm, 0b001>;
3659 }
3660
3661 let accessSize = HalfWordAccess, hasNewValue = 1 in {
3662   defm loadrh  : LD_Abs<"memh",  "LDrih",  IntRegs, u16_1Imm, 0b010>;
3663   defm loadruh : LD_Abs<"memuh", "LDriuh", IntRegs, u16_1Imm, 0b011>;
3664 }
3665
3666 let accessSize = WordAccess, hasNewValue = 1 in
3667 defm loadri  : LD_Abs<"memw",  "LDriw",  IntRegs, u16_2Imm, 0b100>;
3668
3669 let accessSize = DoubleWordAccess in
3670 defm loadrd  : LD_Abs<"memd",  "LDrid", DoubleRegs, u16_3Imm, 0b110>;
3671
3672 //===----------------------------------------------------------------------===//
3673 // multiclass for load instructions with GP-relative addressing mode.
3674 // Rx=mem[bhwd](##global)
3675 // Once predicated, these instructions map to absolute addressing mode.
3676 // if ([!]Pv[.new]) Rx=mem[bhwd](##global)
3677 //===----------------------------------------------------------------------===//
3678
3679 let isAsmParserOnly = 1 in
3680 class T_LoadGP <string mnemonic, string BaseOp, RegisterClass RC, Operand ImmOp,
3681                 bits<3> MajOp>
3682   : T_LoadAbsGP <mnemonic, RC, ImmOp, MajOp, globaladdress, 0>, PredNewRel {
3683     let BaseOpcode = BaseOp#_abs;
3684   }
3685
3686 let accessSize = ByteAccess, hasNewValue = 1 in {
3687   def L2_loadrbgp  : T_LoadGP<"memb",  "LDrib",  IntRegs, u16_0Imm, 0b000>;
3688   def L2_loadrubgp : T_LoadGP<"memub", "LDriub", IntRegs, u16_0Imm, 0b001>;
3689 }
3690
3691 let accessSize = HalfWordAccess, hasNewValue = 1 in {
3692   def L2_loadrhgp  : T_LoadGP<"memh",  "LDrih",  IntRegs, u16_1Imm, 0b010>;
3693   def L2_loadruhgp : T_LoadGP<"memuh", "LDriuh", IntRegs, u16_1Imm, 0b011>;
3694 }
3695
3696 let accessSize = WordAccess, hasNewValue = 1 in
3697 def L2_loadrigp  : T_LoadGP<"memw",  "LDriw",  IntRegs, u16_2Imm, 0b100>;
3698
3699 let accessSize = DoubleWordAccess in
3700 def L2_loadrdgp  : T_LoadGP<"memd", "LDrid", DoubleRegs, u16_3Imm, 0b110>;
3701
3702 def: Loada_pat<atomic_load_8,  i32, addrgp, L2_loadrubgp>;
3703 def: Loada_pat<atomic_load_16, i32, addrgp, L2_loadruhgp>;
3704 def: Loada_pat<atomic_load_32, i32, addrgp, L2_loadrigp>;
3705 def: Loada_pat<atomic_load_64, i64, addrgp, L2_loadrdgp>;
3706
3707 // Map from Pd = load(globaladdress) -> Rd = memb(globaladdress), Pd = Rd
3708 def: Loadam_pat<load, i1, addrga, I32toI1, L4_loadrub_abs>;
3709 def: Loadam_pat<load, i1, addrgp, I32toI1, L2_loadrubgp>;
3710
3711 def: Stoream_pat<store, I1, addrga, I1toI32, S2_storerbabs>;
3712 def: Stoream_pat<store, I1, addrgp, I1toI32, S2_storerbgp>;
3713
3714 // Map from load(globaladdress) -> mem[u][bhwd](#foo)
3715 class LoadGP_pats <PatFrag ldOp, InstHexagon MI, ValueType VT = i32>
3716   : Pat <(VT (ldOp (HexagonCONST32_GP tglobaladdr:$global))),
3717          (VT (MI tglobaladdr:$global))>;
3718
3719 let AddedComplexity = 100 in {
3720   def: LoadGP_pats <extloadi8, L2_loadrbgp>;
3721   def: LoadGP_pats <sextloadi8, L2_loadrbgp>;
3722   def: LoadGP_pats <zextloadi8, L2_loadrubgp>;
3723   def: LoadGP_pats <extloadi16, L2_loadrhgp>;
3724   def: LoadGP_pats <sextloadi16, L2_loadrhgp>;
3725   def: LoadGP_pats <zextloadi16, L2_loadruhgp>;
3726   def: LoadGP_pats <load, L2_loadrigp>;
3727   def: LoadGP_pats <load, L2_loadrdgp, i64>;
3728 }
3729
3730 // When the Interprocedural Global Variable optimizer realizes that a certain
3731 // global variable takes only two constant values, it shrinks the global to
3732 // a boolean. Catch those loads here in the following 3 patterns.
3733 let AddedComplexity = 100 in {
3734   def: LoadGP_pats <extloadi1, L2_loadrubgp>;
3735   def: LoadGP_pats <zextloadi1, L2_loadrubgp>;
3736 }
3737
3738 // Transfer global address into a register
3739 def: Pat<(HexagonCONST32 tglobaladdr:$Rs),      (A2_tfrsi s16Ext:$Rs)>;
3740 def: Pat<(HexagonCONST32_GP tblockaddress:$Rs), (A2_tfrsi s16Ext:$Rs)>;
3741 def: Pat<(HexagonCONST32_GP tglobaladdr:$Rs),   (A2_tfrsi s16Ext:$Rs)>;
3742
3743 def: Pat<(i64 (ctlz I64:$src1)), (Zext64 (S2_cl0p I64:$src1))>;
3744 def: Pat<(i64 (cttz I64:$src1)), (Zext64 (S2_ct0p I64:$src1))>;
3745
3746 let AddedComplexity  = 30 in {
3747   def: Storea_pat<truncstorei8,  I32, u0AlwaysExtPred, S2_storerbabs>;
3748   def: Storea_pat<truncstorei16, I32, u0AlwaysExtPred, S2_storerhabs>;
3749   def: Storea_pat<store,         I32, u0AlwaysExtPred, S2_storeriabs>;
3750 }
3751
3752 let AddedComplexity  = 30 in {
3753   def: Loada_pat<load,        i32, u0AlwaysExtPred, L4_loadri_abs>;
3754   def: Loada_pat<sextloadi8,  i32, u0AlwaysExtPred, L4_loadrb_abs>;
3755   def: Loada_pat<zextloadi8,  i32, u0AlwaysExtPred, L4_loadrub_abs>;
3756   def: Loada_pat<sextloadi16, i32, u0AlwaysExtPred, L4_loadrh_abs>;
3757   def: Loada_pat<zextloadi16, i32, u0AlwaysExtPred, L4_loadruh_abs>;
3758 }
3759
3760 // Indexed store word - global address.
3761 // memw(Rs+#u6:2)=#S8
3762 let AddedComplexity = 100 in
3763 def: Storex_add_pat<store, addrga, u6_2ImmPred, S4_storeiri_io>;
3764
3765 // Load from a global address that has only one use in the current basic block.
3766 let AddedComplexity = 100 in {
3767   def: Loada_pat<extloadi8,   i32, addrga, L4_loadrub_abs>;
3768   def: Loada_pat<sextloadi8,  i32, addrga, L4_loadrb_abs>;
3769   def: Loada_pat<zextloadi8,  i32, addrga, L4_loadrub_abs>;
3770
3771   def: Loada_pat<extloadi16,  i32, addrga, L4_loadruh_abs>;
3772   def: Loada_pat<sextloadi16, i32, addrga, L4_loadrh_abs>;
3773   def: Loada_pat<zextloadi16, i32, addrga, L4_loadruh_abs>;
3774
3775   def: Loada_pat<load,        i32, addrga, L4_loadri_abs>;
3776   def: Loada_pat<load,        i64, addrga, L4_loadrd_abs>;
3777 }
3778
3779 // Store to a global address that has only one use in the current basic block.
3780 let AddedComplexity = 100 in {
3781   def: Storea_pat<truncstorei8,  I32, addrga, S2_storerbabs>;
3782   def: Storea_pat<truncstorei16, I32, addrga, S2_storerhabs>;
3783   def: Storea_pat<store,         I32, addrga, S2_storeriabs>;
3784   def: Storea_pat<store,         I64, addrga, S2_storerdabs>;
3785
3786   def: Stoream_pat<truncstorei32, I64, addrga, LoReg, S2_storeriabs>;
3787 }
3788
3789 // Map from Pd = load(globaladdress) -> Rd = memb(globaladdress), Pd = Rd
3790 let AddedComplexity = 100 in
3791 def : Pat <(i1 (load (HexagonCONST32_GP tglobaladdr:$global))),
3792            (i1 (C2_tfrrp (i32 (L2_loadrbgp tglobaladdr:$global))))>;
3793
3794 // Transfer global address into a register
3795 let isExtended = 1, opExtendable = 1, AddedComplexity=50, isMoveImm = 1,
3796 isAsCheapAsAMove = 1, isReMaterializable = 1, isCodeGenOnly = 1 in
3797 def TFRI_V4 : ALU32_ri<(outs IntRegs:$dst), (ins s16Ext:$src1),
3798            "$dst = #$src1",
3799            [(set IntRegs:$dst, (HexagonCONST32 tglobaladdr:$src1))]>;
3800
3801 // Transfer a block address into a register
3802 def : Pat<(HexagonCONST32_GP tblockaddress:$src1),
3803           (TFRI_V4 tblockaddress:$src1)>;
3804
3805 let AddedComplexity = 50 in
3806 def : Pat<(HexagonCONST32_GP tglobaladdr:$src1),
3807            (TFRI_V4 tglobaladdr:$src1)>;
3808
3809 // i8/i16/i32 -> i64 loads
3810 // We need a complexity of 120 here to override preceding handling of
3811 // zextload.
3812 let AddedComplexity = 120 in {
3813   def: Loadam_pat<extloadi8,   i64, addrga, Zext64, L4_loadrub_abs>;
3814   def: Loadam_pat<sextloadi8,  i64, addrga, Sext64, L4_loadrb_abs>;
3815   def: Loadam_pat<zextloadi8,  i64, addrga, Zext64, L4_loadrub_abs>;
3816
3817   def: Loadam_pat<extloadi16,  i64, addrga, Zext64, L4_loadruh_abs>;
3818   def: Loadam_pat<sextloadi16, i64, addrga, Sext64, L4_loadrh_abs>;
3819   def: Loadam_pat<zextloadi16, i64, addrga, Zext64, L4_loadruh_abs>;
3820
3821   def: Loadam_pat<extloadi32,  i64, addrga, Zext64, L4_loadri_abs>;
3822   def: Loadam_pat<sextloadi32, i64, addrga, Sext64, L4_loadri_abs>;
3823   def: Loadam_pat<zextloadi32, i64, addrga, Zext64, L4_loadri_abs>;
3824 }
3825
3826 let AddedComplexity = 100 in {
3827   def: Loada_pat<extloadi8,   i32, addrgp, L4_loadrub_abs>;
3828   def: Loada_pat<sextloadi8,  i32, addrgp, L4_loadrb_abs>;
3829   def: Loada_pat<zextloadi8,  i32, addrgp, L4_loadrub_abs>;
3830
3831   def: Loada_pat<extloadi16,  i32, addrgp, L4_loadruh_abs>;
3832   def: Loada_pat<sextloadi16, i32, addrgp, L4_loadrh_abs>;
3833   def: Loada_pat<zextloadi16, i32, addrgp, L4_loadruh_abs>;
3834
3835   def: Loada_pat<load,        i32, addrgp, L4_loadri_abs>;
3836   def: Loada_pat<load,        i64, addrgp, L4_loadrd_abs>;
3837 }
3838
3839 let AddedComplexity = 100 in {
3840   def: Storea_pat<truncstorei8,  I32, addrgp, S2_storerbabs>;
3841   def: Storea_pat<truncstorei16, I32, addrgp, S2_storerhabs>;
3842   def: Storea_pat<store,         I32, addrgp, S2_storeriabs>;
3843   def: Storea_pat<store,         I64, addrgp, S2_storerdabs>;
3844 }
3845
3846 def: Loada_pat<atomic_load_8,  i32, addrgp, L4_loadrub_abs>;
3847 def: Loada_pat<atomic_load_16, i32, addrgp, L4_loadruh_abs>;
3848 def: Loada_pat<atomic_load_32, i32, addrgp, L4_loadri_abs>;
3849 def: Loada_pat<atomic_load_64, i64, addrgp, L4_loadrd_abs>;
3850
3851 def: Storea_pat<SwapSt<atomic_store_8>,  I32, addrgp, S2_storerbabs>;
3852 def: Storea_pat<SwapSt<atomic_store_16>, I32, addrgp, S2_storerhabs>;
3853 def: Storea_pat<SwapSt<atomic_store_32>, I32, addrgp, S2_storeriabs>;
3854 def: Storea_pat<SwapSt<atomic_store_64>, I64, addrgp, S2_storerdabs>;
3855
3856 //===----------------------------------------------------------------------===//
3857 // :raw for of boundscheck:hi:lo insns
3858 //===----------------------------------------------------------------------===//
3859
3860 // A4_boundscheck_lo: Detect if a register is within bounds.
3861 let hasSideEffects = 0 in
3862 def A4_boundscheck_lo: ALU64Inst <
3863   (outs PredRegs:$Pd),
3864   (ins DoubleRegs:$Rss, DoubleRegs:$Rtt),
3865   "$Pd = boundscheck($Rss, $Rtt):raw:lo"> {
3866     bits<2> Pd;
3867     bits<5> Rss;
3868     bits<5> Rtt;
3869
3870     let IClass = 0b1101;
3871
3872     let Inst{27-23} = 0b00100;
3873     let Inst{13} = 0b1;
3874     let Inst{7-5} = 0b100;
3875     let Inst{1-0} = Pd;
3876     let Inst{20-16} = Rss;
3877     let Inst{12-8} = Rtt;
3878   }
3879
3880 // A4_boundscheck_hi: Detect if a register is within bounds.
3881 let hasSideEffects = 0 in
3882 def A4_boundscheck_hi: ALU64Inst <
3883   (outs PredRegs:$Pd),
3884   (ins DoubleRegs:$Rss, DoubleRegs:$Rtt),
3885   "$Pd = boundscheck($Rss, $Rtt):raw:hi"> {
3886     bits<2> Pd;
3887     bits<5> Rss;
3888     bits<5> Rtt;
3889
3890     let IClass = 0b1101;
3891
3892     let Inst{27-23} = 0b00100;
3893     let Inst{13} = 0b1;
3894     let Inst{7-5} = 0b101;
3895     let Inst{1-0} = Pd;
3896     let Inst{20-16} = Rss;
3897     let Inst{12-8} = Rtt;
3898   }
3899
3900 let hasSideEffects = 0, isAsmParserOnly = 1 in
3901 def A4_boundscheck : MInst <
3902   (outs PredRegs:$Pd), (ins IntRegs:$Rs, DoubleRegs:$Rtt),
3903   "$Pd=boundscheck($Rs,$Rtt)">;
3904
3905 // A4_tlbmatch: Detect if a VA/ASID matches a TLB entry.
3906 let isPredicateLate = 1, hasSideEffects = 0 in
3907 def A4_tlbmatch : ALU64Inst<(outs PredRegs:$Pd),
3908   (ins DoubleRegs:$Rs, IntRegs:$Rt),
3909   "$Pd = tlbmatch($Rs, $Rt)",
3910   [], "", ALU64_tc_2early_SLOT23> {
3911     bits<2> Pd;
3912     bits<5> Rs;
3913     bits<5> Rt;
3914
3915     let IClass = 0b1101;
3916     let Inst{27-23} = 0b00100;
3917     let Inst{20-16} = Rs;
3918     let Inst{13} = 0b1;
3919     let Inst{12-8} = Rt;
3920     let Inst{7-5} = 0b011;
3921     let Inst{1-0} = Pd;
3922   }
3923
3924 // We need custom lowering of ISD::PREFETCH into HexagonISD::DCFETCH
3925 // because the SDNode ISD::PREFETCH has properties MayLoad and MayStore.
3926 // We don't really want either one here.
3927 def SDTHexagonDCFETCH : SDTypeProfile<0, 2, [SDTCisPtrTy<0>,SDTCisInt<1>]>;
3928 def HexagonDCFETCH : SDNode<"HexagonISD::DCFETCH", SDTHexagonDCFETCH,
3929                             [SDNPHasChain]>;
3930
3931 // Use LD0Inst for dcfetch, but set "mayLoad" to 0 because this doesn't
3932 // really do a load.
3933 let hasSideEffects = 1, mayLoad = 0 in
3934 def Y2_dcfetchbo : LD0Inst<(outs), (ins IntRegs:$Rs, u11_3Imm:$u11_3),
3935       "dcfetch($Rs + #$u11_3)",
3936       [(HexagonDCFETCH IntRegs:$Rs, u11_3ImmPred:$u11_3)],
3937       "", LD_tc_ld_SLOT0> {
3938   bits<5> Rs;
3939   bits<14> u11_3;
3940
3941   let IClass = 0b1001;
3942   let Inst{27-21} = 0b0100000;
3943   let Inst{20-16} = Rs;
3944   let Inst{13} = 0b0;
3945   let Inst{10-0} = u11_3{13-3};
3946 }
3947
3948 //===----------------------------------------------------------------------===//
3949 // Compound instructions
3950 //===----------------------------------------------------------------------===//
3951
3952 let isBranch = 1, hasSideEffects = 0, isExtentSigned = 1,
3953     isPredicated = 1, isPredicatedNew = 1, isExtendable = 1,
3954     opExtentBits = 11, opExtentAlign = 2, opExtendable = 1,
3955     isTerminator = 1 in
3956 class CJInst_tstbit_R0<string px, bit np, string tnt>
3957   : InstHexagon<(outs), (ins IntRegs:$Rs, brtarget:$r9_2),
3958   ""#px#" = tstbit($Rs, #0); if ("
3959     #!if(np, "!","")#""#px#".new) jump:"#tnt#" $r9_2",
3960   [], "", COMPOUND, TypeCOMPOUND> {
3961   bits<4> Rs;
3962   bits<11> r9_2;
3963
3964   // np: !p[01]
3965   let isPredicatedFalse = np;
3966   // tnt: Taken/Not Taken
3967   let isBrTaken = !if (!eq(tnt, "t"), "true", "false");
3968   let isTaken   = !if (!eq(tnt, "t"), 1, 0);
3969
3970   let IClass = 0b0001;
3971   let Inst{27-26} = 0b00;
3972   let Inst{25} = !if (!eq(px, "!p1"), 1,
3973                  !if (!eq(px,  "p1"), 1, 0));
3974   let Inst{24-23} = 0b11;
3975   let Inst{22} = np;
3976   let Inst{21-20} = r9_2{10-9};
3977   let Inst{19-16} = Rs;
3978   let Inst{13} = !if (!eq(tnt, "t"), 1, 0);
3979   let Inst{9-8} = 0b11;
3980   let Inst{7-1} = r9_2{8-2};
3981 }
3982
3983 let Defs = [PC, P0], Uses = [P0] in {
3984   def J4_tstbit0_tp0_jump_nt : CJInst_tstbit_R0<"p0", 0, "nt">;
3985   def J4_tstbit0_tp0_jump_t : CJInst_tstbit_R0<"p0", 0, "t">;
3986   def J4_tstbit0_fp0_jump_nt : CJInst_tstbit_R0<"p0", 1, "nt">;
3987   def J4_tstbit0_fp0_jump_t : CJInst_tstbit_R0<"p0", 1, "t">;
3988 }
3989
3990 let Defs = [PC, P1], Uses = [P1] in {
3991   def J4_tstbit0_tp1_jump_nt : CJInst_tstbit_R0<"p1", 0, "nt">;
3992   def J4_tstbit0_tp1_jump_t : CJInst_tstbit_R0<"p1", 0, "t">;
3993   def J4_tstbit0_fp1_jump_nt : CJInst_tstbit_R0<"p1", 1, "nt">;
3994   def J4_tstbit0_fp1_jump_t : CJInst_tstbit_R0<"p1", 1, "t">;
3995 }
3996
3997
3998 let isBranch = 1, hasSideEffects = 0,
3999     isExtentSigned = 1, isPredicated = 1, isPredicatedNew = 1,
4000     isExtendable = 1, opExtentBits = 11, opExtentAlign = 2,
4001     opExtendable = 2, isTerminator = 1 in
4002 class CJInst_RR<string px, string op, bit np, string tnt>
4003   : InstHexagon<(outs), (ins IntRegs:$Rs, IntRegs:$Rt, brtarget:$r9_2),
4004   ""#px#" = cmp."#op#"($Rs, $Rt); if ("
4005    #!if(np, "!","")#""#px#".new) jump:"#tnt#" $r9_2",
4006   [], "", COMPOUND, TypeCOMPOUND> {
4007   bits<4> Rs;
4008   bits<4> Rt;
4009   bits<11> r9_2;
4010
4011   // np: !p[01]
4012   let isPredicatedFalse = np;
4013   // tnt: Taken/Not Taken
4014   let isBrTaken = !if (!eq(tnt, "t"), "true", "false");
4015   let isTaken   = !if (!eq(tnt, "t"), 1, 0);
4016
4017   let IClass = 0b0001;
4018   let Inst{27-23} = !if (!eq(op, "eq"),  0b01000,
4019                     !if (!eq(op, "gt"),  0b01001,
4020                     !if (!eq(op, "gtu"), 0b01010, 0)));
4021   let Inst{22} = np;
4022   let Inst{21-20} = r9_2{10-9};
4023   let Inst{19-16} = Rs;
4024   let Inst{13} = !if (!eq(tnt, "t"), 1, 0);
4025   // px: Predicate reg 0/1
4026   let Inst{12} = !if (!eq(px, "!p1"), 1,
4027                  !if (!eq(px,  "p1"), 1, 0));
4028   let Inst{11-8} = Rt;
4029   let Inst{7-1} = r9_2{8-2};
4030 }
4031
4032 // P[10] taken/not taken.
4033 multiclass T_tnt_CJInst_RR<string op, bit np> {
4034   let Defs = [PC, P0], Uses = [P0] in {
4035     def NAME#p0_jump_nt : CJInst_RR<"p0", op, np, "nt">;
4036     def NAME#p0_jump_t : CJInst_RR<"p0", op, np, "t">;
4037   }
4038   let Defs = [PC, P1], Uses = [P1] in {
4039     def NAME#p1_jump_nt : CJInst_RR<"p1", op, np, "nt">;
4040     def NAME#p1_jump_t : CJInst_RR<"p1", op, np, "t">;
4041   }
4042 }
4043 // Predicate / !Predicate
4044 multiclass T_pnp_CJInst_RR<string op>{
4045   defm J4_cmp#NAME#_t : T_tnt_CJInst_RR<op, 0>;
4046   defm J4_cmp#NAME#_f : T_tnt_CJInst_RR<op, 1>;
4047 }
4048 // TypeCJ Instructions compare RR and jump
4049 defm eq : T_pnp_CJInst_RR<"eq">;
4050 defm gt : T_pnp_CJInst_RR<"gt">;
4051 defm gtu : T_pnp_CJInst_RR<"gtu">;
4052
4053 let isBranch = 1, hasSideEffects = 0, isExtentSigned = 1,
4054     isPredicated = 1, isPredicatedNew = 1, isExtendable = 1, opExtentBits = 11,
4055     opExtentAlign = 2, opExtendable = 2, isTerminator = 1 in
4056 class CJInst_RU5<string px, string op, bit np, string tnt>
4057   : InstHexagon<(outs), (ins IntRegs:$Rs, u5Imm:$U5, brtarget:$r9_2),
4058   ""#px#" = cmp."#op#"($Rs, #$U5); if ("
4059     #!if(np, "!","")#""#px#".new) jump:"#tnt#" $r9_2",
4060   [], "", COMPOUND, TypeCOMPOUND> {
4061   bits<4> Rs;
4062   bits<5> U5;
4063   bits<11> r9_2;
4064
4065   // np: !p[01]
4066   let isPredicatedFalse = np;
4067   // tnt: Taken/Not Taken
4068   let isBrTaken = !if (!eq(tnt, "t"), "true", "false");
4069   let isTaken   = !if (!eq(tnt, "t"), 1, 0);
4070
4071   let IClass = 0b0001;
4072   let Inst{27-26} = 0b00;
4073   // px: Predicate reg 0/1
4074   let Inst{25} = !if (!eq(px, "!p1"), 1,
4075                  !if (!eq(px,  "p1"), 1, 0));
4076   let Inst{24-23} = !if (!eq(op, "eq"),  0b00,
4077                     !if (!eq(op, "gt"),  0b01,
4078                     !if (!eq(op, "gtu"), 0b10, 0)));
4079   let Inst{22} = np;
4080   let Inst{21-20} = r9_2{10-9};
4081   let Inst{19-16} = Rs;
4082   let Inst{13} = !if (!eq(tnt, "t"), 1, 0);
4083   let Inst{12-8} = U5;
4084   let Inst{7-1} = r9_2{8-2};
4085 }
4086 // P[10] taken/not taken.
4087 multiclass T_tnt_CJInst_RU5<string op, bit np> {
4088   let Defs = [PC, P0], Uses = [P0] in {
4089     def NAME#p0_jump_nt : CJInst_RU5<"p0", op, np, "nt">;
4090     def NAME#p0_jump_t : CJInst_RU5<"p0", op, np, "t">;
4091   }
4092   let Defs = [PC, P1], Uses = [P1] in {
4093     def NAME#p1_jump_nt : CJInst_RU5<"p1", op, np, "nt">;
4094     def NAME#p1_jump_t : CJInst_RU5<"p1", op, np, "t">;
4095   }
4096 }
4097 // Predicate / !Predicate
4098 multiclass T_pnp_CJInst_RU5<string op>{
4099   defm J4_cmp#NAME#i_t : T_tnt_CJInst_RU5<op, 0>;
4100   defm J4_cmp#NAME#i_f : T_tnt_CJInst_RU5<op, 1>;
4101 }
4102 // TypeCJ Instructions compare RI and jump
4103 defm eq : T_pnp_CJInst_RU5<"eq">;
4104 defm gt : T_pnp_CJInst_RU5<"gt">;
4105 defm gtu : T_pnp_CJInst_RU5<"gtu">;
4106
4107 let isBranch = 1, hasSideEffects = 0, isExtentSigned = 1,
4108     isPredicated = 1, isPredicatedFalse = 1, isPredicatedNew = 1,
4109     isExtendable = 1, opExtentBits = 11, opExtentAlign = 2, opExtendable = 1,
4110     isTerminator = 1 in
4111 class CJInst_Rn1<string px, string op, bit np, string tnt>
4112   : InstHexagon<(outs), (ins IntRegs:$Rs, brtarget:$r9_2),
4113   ""#px#" = cmp."#op#"($Rs,#-1); if ("
4114   #!if(np, "!","")#""#px#".new) jump:"#tnt#" $r9_2",
4115   [], "", COMPOUND, TypeCOMPOUND> {
4116   bits<4> Rs;
4117   bits<11> r9_2;
4118
4119   // np: !p[01]
4120   let isPredicatedFalse = np;
4121   // tnt: Taken/Not Taken
4122   let isBrTaken = !if (!eq(tnt, "t"), "true", "false");
4123   let isTaken   = !if (!eq(tnt, "t"), 1, 0);
4124
4125   let IClass = 0b0001;
4126   let Inst{27-26} = 0b00;
4127   let Inst{25} = !if (!eq(px, "!p1"), 1,
4128                  !if (!eq(px,  "p1"), 1, 0));
4129
4130   let Inst{24-23} = 0b11;
4131   let Inst{22} = np;
4132   let Inst{21-20} = r9_2{10-9};
4133   let Inst{19-16} = Rs;
4134   let Inst{13} = !if (!eq(tnt, "t"), 1, 0);
4135   let Inst{9-8} = !if (!eq(op, "eq"),  0b00,
4136                   !if (!eq(op, "gt"),  0b01, 0));
4137   let Inst{7-1} = r9_2{8-2};
4138 }
4139
4140 // P[10] taken/not taken.
4141 multiclass T_tnt_CJInst_Rn1<string op, bit np> {
4142   let Defs = [PC, P0], Uses = [P0] in {
4143     def NAME#p0_jump_nt : CJInst_Rn1<"p0", op, np, "nt">;
4144     def NAME#p0_jump_t : CJInst_Rn1<"p0", op, np, "t">;
4145   }
4146   let Defs = [PC, P1], Uses = [P1] in {
4147     def NAME#p1_jump_nt : CJInst_Rn1<"p1", op, np, "nt">;
4148     def NAME#p1_jump_t : CJInst_Rn1<"p1", op, np, "t">;
4149   }
4150 }
4151 // Predicate / !Predicate
4152 multiclass T_pnp_CJInst_Rn1<string op>{
4153   defm J4_cmp#NAME#n1_t : T_tnt_CJInst_Rn1<op, 0>;
4154   defm J4_cmp#NAME#n1_f : T_tnt_CJInst_Rn1<op, 1>;
4155 }
4156 // TypeCJ Instructions compare -1 and jump
4157 defm eq : T_pnp_CJInst_Rn1<"eq">;
4158 defm gt : T_pnp_CJInst_Rn1<"gt">;
4159
4160 // J4_jumpseti: Direct unconditional jump and set register to immediate.
4161 let Defs = [PC], isBranch = 1, hasSideEffects = 0, hasNewValue = 1,
4162     isExtentSigned = 1, opNewValue = 0, isExtendable = 1, opExtentBits = 11,
4163     opExtentAlign = 2, opExtendable = 2 in
4164 def J4_jumpseti: CJInst <
4165   (outs IntRegs:$Rd),
4166   (ins u6Imm:$U6, brtarget:$r9_2),
4167   "$Rd = #$U6 ; jump $r9_2"> {
4168     bits<4> Rd;
4169     bits<6> U6;
4170     bits<11> r9_2;
4171
4172     let IClass = 0b0001;
4173     let Inst{27-24} = 0b0110;
4174     let Inst{21-20} = r9_2{10-9};
4175     let Inst{19-16} = Rd;
4176     let Inst{13-8} = U6;
4177     let Inst{7-1} = r9_2{8-2};
4178   }
4179
4180 // J4_jumpsetr: Direct unconditional jump and transfer register.
4181 let Defs = [PC], isBranch = 1, hasSideEffects = 0, hasNewValue = 1,
4182     isExtentSigned = 1, opNewValue = 0, isExtendable = 1, opExtentBits = 11,
4183     opExtentAlign = 2, opExtendable = 2 in
4184 def J4_jumpsetr: CJInst <
4185   (outs IntRegs:$Rd),
4186   (ins IntRegs:$Rs, brtarget:$r9_2),
4187   "$Rd = $Rs ; jump $r9_2"> {
4188     bits<4> Rd;
4189     bits<4> Rs;
4190     bits<11> r9_2;
4191
4192     let IClass = 0b0001;
4193     let Inst{27-24} = 0b0111;
4194     let Inst{21-20} = r9_2{10-9};
4195     let Inst{11-8} = Rd;
4196     let Inst{19-16} = Rs;
4197     let Inst{7-1} = r9_2{8-2};
4198   }