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