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