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