xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZPatterns.td (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric//===-- SystemZPatterns.td - SystemZ-specific pattern rules ---*- tblgen-*-===//
2*0b57cec5SDimitry Andric//
3*0b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric//
7*0b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric// Record that INSN performs a 64-bit version of unary operator OPERATOR
10*0b57cec5SDimitry Andric// in which the operand is sign-extended from 32 to 64 bits.
11*0b57cec5SDimitry Andricmulticlass SXU<SDPatternOperator operator, Instruction insn> {
12*0b57cec5SDimitry Andric  def : Pat<(operator (sext (i32 GR32:$src))),
13*0b57cec5SDimitry Andric            (insn GR32:$src)>;
14*0b57cec5SDimitry Andric  def : Pat<(operator (sext_inreg GR64:$src, i32)),
15*0b57cec5SDimitry Andric            (insn (EXTRACT_SUBREG GR64:$src, subreg_l32))>;
16*0b57cec5SDimitry Andric}
17*0b57cec5SDimitry Andric
18*0b57cec5SDimitry Andric// Record that INSN performs a 64-bit version of binary operator OPERATOR
19*0b57cec5SDimitry Andric// in which the first operand has class CLS and which the second operand
20*0b57cec5SDimitry Andric// is sign-extended from a 32-bit register.
21*0b57cec5SDimitry Andricmulticlass SXB<SDPatternOperator operator, RegisterOperand cls,
22*0b57cec5SDimitry Andric               Instruction insn> {
23*0b57cec5SDimitry Andric  def : Pat<(operator cls:$src1, (sext GR32:$src2)),
24*0b57cec5SDimitry Andric            (insn cls:$src1, GR32:$src2)>;
25*0b57cec5SDimitry Andric  def : Pat<(operator cls:$src1, (sext_inreg GR64:$src2, i32)),
26*0b57cec5SDimitry Andric            (insn cls:$src1, (EXTRACT_SUBREG GR64:$src2, subreg_l32))>;
27*0b57cec5SDimitry Andric}
28*0b57cec5SDimitry Andric
29*0b57cec5SDimitry Andric// Like SXB, but for zero extension.
30*0b57cec5SDimitry Andricmulticlass ZXB<SDPatternOperator operator, RegisterOperand cls,
31*0b57cec5SDimitry Andric               Instruction insn> {
32*0b57cec5SDimitry Andric  def : Pat<(operator cls:$src1, (zext GR32:$src2)),
33*0b57cec5SDimitry Andric            (insn cls:$src1, GR32:$src2)>;
34*0b57cec5SDimitry Andric  def : Pat<(operator cls:$src1, (and GR64:$src2, 0xffffffff)),
35*0b57cec5SDimitry Andric            (insn cls:$src1, (EXTRACT_SUBREG GR64:$src2, subreg_l32))>;
36*0b57cec5SDimitry Andric}
37*0b57cec5SDimitry Andric
38*0b57cec5SDimitry Andric// Record that INSN performs a binary read-modify-write operation,
39*0b57cec5SDimitry Andric// with LOAD, OPERATOR and STORE being the read, modify and write
40*0b57cec5SDimitry Andric// respectively.  MODE is the addressing mode and IMM is the type
41*0b57cec5SDimitry Andric// of the second operand.
42*0b57cec5SDimitry Andricclass RMWI<SDPatternOperator load, SDPatternOperator operator,
43*0b57cec5SDimitry Andric           SDPatternOperator store, AddressingMode mode,
44*0b57cec5SDimitry Andric           PatFrag imm, Instruction insn>
45*0b57cec5SDimitry Andric  : Pat<(store (operator (load mode:$addr), imm:$src), mode:$addr),
46*0b57cec5SDimitry Andric        (insn mode:$addr, (UIMM8 imm:$src))>;
47*0b57cec5SDimitry Andric
48*0b57cec5SDimitry Andric// Record that INSN performs binary operation OPERATION on a byte
49*0b57cec5SDimitry Andric// memory location.  IMM is the type of the second operand.
50*0b57cec5SDimitry Andricmulticlass RMWIByte<SDPatternOperator operator, AddressingMode mode,
51*0b57cec5SDimitry Andric                    Instruction insn> {
52*0b57cec5SDimitry Andric  def : RMWI<anyextloadi8, operator, truncstorei8, mode, imm32, insn>;
53*0b57cec5SDimitry Andric  def : RMWI<anyextloadi8, operator, truncstorei8, mode, imm64, insn>;
54*0b57cec5SDimitry Andric}
55*0b57cec5SDimitry Andric
56*0b57cec5SDimitry Andric// Record that INSN performs insertion TYPE into a register of class CLS.
57*0b57cec5SDimitry Andric// The inserted operand is loaded using LOAD from an address of mode MODE.
58*0b57cec5SDimitry Andricmulticlass InsertMem<string type, Instruction insn, RegisterOperand cls,
59*0b57cec5SDimitry Andric                     SDPatternOperator load, AddressingMode mode> {
60*0b57cec5SDimitry Andric  def : Pat<(!cast<SDPatternOperator>("or_as_"##type)
61*0b57cec5SDimitry Andric              cls:$src1, (load mode:$src2)),
62*0b57cec5SDimitry Andric            (insn cls:$src1, mode:$src2)>;
63*0b57cec5SDimitry Andric  def : Pat<(!cast<SDPatternOperator>("or_as_rev"##type)
64*0b57cec5SDimitry Andric              (load mode:$src2), cls:$src1),
65*0b57cec5SDimitry Andric            (insn cls:$src1, mode:$src2)>;
66*0b57cec5SDimitry Andric}
67*0b57cec5SDimitry Andric
68*0b57cec5SDimitry Andric// INSN stores the low 32 bits of a GPR to a memory with addressing mode MODE.
69*0b57cec5SDimitry Andric// Record that it is equivalent to using OPERATOR to store a GR64.
70*0b57cec5SDimitry Andricclass StoreGR64<Instruction insn, SDPatternOperator operator,
71*0b57cec5SDimitry Andric                AddressingMode mode>
72*0b57cec5SDimitry Andric  : Pat<(operator GR64:$R1, mode:$XBD2),
73*0b57cec5SDimitry Andric        (insn (EXTRACT_SUBREG GR64:$R1, subreg_l32), mode:$XBD2)>;
74*0b57cec5SDimitry Andric
75*0b57cec5SDimitry Andric// INSN and INSNY are an RX/RXY pair of instructions that store the low
76*0b57cec5SDimitry Andric// 32 bits of a GPR to memory.  Record that they are equivalent to using
77*0b57cec5SDimitry Andric// OPERATOR to store a GR64.
78*0b57cec5SDimitry Andricmulticlass StoreGR64Pair<Instruction insn, Instruction insny,
79*0b57cec5SDimitry Andric                         SDPatternOperator operator> {
80*0b57cec5SDimitry Andric  def : StoreGR64<insn, operator, bdxaddr12pair>;
81*0b57cec5SDimitry Andric  def : StoreGR64<insny, operator, bdxaddr20pair>;
82*0b57cec5SDimitry Andric}
83*0b57cec5SDimitry Andric
84*0b57cec5SDimitry Andric// INSN stores the low 32 bits of a GPR using PC-relative addressing.
85*0b57cec5SDimitry Andric// Record that it is equivalent to using OPERATOR to store a GR64.
86*0b57cec5SDimitry Andricclass StoreGR64PC<Instruction insn, SDPatternOperator operator>
87*0b57cec5SDimitry Andric  : Pat<(operator GR64:$R1, pcrel32:$XBD2),
88*0b57cec5SDimitry Andric        (insn (EXTRACT_SUBREG GR64:$R1, subreg_l32), pcrel32:$XBD2)> {
89*0b57cec5SDimitry Andric  // We want PC-relative addresses to be tried ahead of BD and BDX addresses.
90*0b57cec5SDimitry Andric  // However, BDXs have two extra operands and are therefore 6 units more
91*0b57cec5SDimitry Andric  // complex.
92*0b57cec5SDimitry Andric  let AddedComplexity = 7;
93*0b57cec5SDimitry Andric}
94*0b57cec5SDimitry Andric
95*0b57cec5SDimitry Andric// INSN and INSNINV conditionally store the low 32 bits of a GPR to memory,
96*0b57cec5SDimitry Andric// with INSN storing when the condition is true and INSNINV storing when the
97*0b57cec5SDimitry Andric// condition is false.  Record that they are equivalent to a LOAD/select/STORE
98*0b57cec5SDimitry Andric// sequence for GR64s.
99*0b57cec5SDimitry Andricmulticlass CondStores64<Instruction insn, Instruction insninv,
100*0b57cec5SDimitry Andric                        SDPatternOperator store, SDPatternOperator load,
101*0b57cec5SDimitry Andric                        AddressingMode mode> {
102*0b57cec5SDimitry Andric  def : Pat<(store (z_select_ccmask GR64:$new, (load mode:$addr),
103*0b57cec5SDimitry Andric                                    imm32zx4:$valid, imm32zx4:$cc),
104*0b57cec5SDimitry Andric                   mode:$addr),
105*0b57cec5SDimitry Andric            (insn (EXTRACT_SUBREG GR64:$new, subreg_l32), mode:$addr,
106*0b57cec5SDimitry Andric                  imm32zx4:$valid, imm32zx4:$cc)>;
107*0b57cec5SDimitry Andric  def : Pat<(store (z_select_ccmask (load mode:$addr), GR64:$new,
108*0b57cec5SDimitry Andric                                    imm32zx4:$valid, imm32zx4:$cc),
109*0b57cec5SDimitry Andric                   mode:$addr),
110*0b57cec5SDimitry Andric            (insninv (EXTRACT_SUBREG GR64:$new, subreg_l32), mode:$addr,
111*0b57cec5SDimitry Andric                     imm32zx4:$valid, imm32zx4:$cc)>;
112*0b57cec5SDimitry Andric}
113*0b57cec5SDimitry Andric
114*0b57cec5SDimitry Andric// Try to use MVC instruction INSN for a load of type LOAD followed by a store
115*0b57cec5SDimitry Andric// of the same size.  VT is the type of the intermediate (legalized) value and
116*0b57cec5SDimitry Andric// LENGTH is the number of bytes loaded by LOAD.
117*0b57cec5SDimitry Andricmulticlass MVCLoadStore<SDPatternOperator load, ValueType vt, Instruction insn,
118*0b57cec5SDimitry Andric                        bits<5> length> {
119*0b57cec5SDimitry Andric  def : Pat<(mvc_store (vt (load bdaddr12only:$src)), bdaddr12only:$dest),
120*0b57cec5SDimitry Andric            (insn bdaddr12only:$dest, bdaddr12only:$src, length)>;
121*0b57cec5SDimitry Andric}
122*0b57cec5SDimitry Andric
123*0b57cec5SDimitry Andric// Use NC-like instruction INSN for block_op operation OPERATOR.
124*0b57cec5SDimitry Andric// The other operand is a load of type LOAD, which accesses LENGTH bytes.
125*0b57cec5SDimitry Andric// VT is the intermediate legalized type in which the binary operation
126*0b57cec5SDimitry Andric// is actually done.
127*0b57cec5SDimitry Andricmulticlass BinaryLoadStore<SDPatternOperator operator, SDPatternOperator load,
128*0b57cec5SDimitry Andric                           ValueType vt, Instruction insn, bits<5> length> {
129*0b57cec5SDimitry Andric  def : Pat<(operator (vt (load bdaddr12only:$src)), bdaddr12only:$dest),
130*0b57cec5SDimitry Andric            (insn bdaddr12only:$dest, bdaddr12only:$src, length)>;
131*0b57cec5SDimitry Andric}
132*0b57cec5SDimitry Andric
133*0b57cec5SDimitry Andric// A convenient way of generating all block peepholes for a particular
134*0b57cec5SDimitry Andric// LOAD/VT/LENGTH combination.
135*0b57cec5SDimitry Andricmulticlass BlockLoadStore<SDPatternOperator load, ValueType vt,
136*0b57cec5SDimitry Andric                          Instruction mvc, Instruction nc, Instruction oc,
137*0b57cec5SDimitry Andric                          Instruction xc, bits<5> length> {
138*0b57cec5SDimitry Andric  defm : MVCLoadStore<load, vt, mvc, length>;
139*0b57cec5SDimitry Andric  defm : BinaryLoadStore<block_and1, load, vt, nc, length>;
140*0b57cec5SDimitry Andric  defm : BinaryLoadStore<block_and2, load, vt, nc, length>;
141*0b57cec5SDimitry Andric  defm : BinaryLoadStore<block_or1,  load, vt, oc, length>;
142*0b57cec5SDimitry Andric  defm : BinaryLoadStore<block_or2,  load, vt, oc, length>;
143*0b57cec5SDimitry Andric  defm : BinaryLoadStore<block_xor1, load, vt, xc, length>;
144*0b57cec5SDimitry Andric  defm : BinaryLoadStore<block_xor2, load, vt, xc, length>;
145*0b57cec5SDimitry Andric}
146*0b57cec5SDimitry Andric
147*0b57cec5SDimitry Andric// Record that INSN is a LOAD AND TEST that can be used to compare
148*0b57cec5SDimitry Andric// registers in CLS against zero.  The instruction has separate R1 and R2
149*0b57cec5SDimitry Andric// operands, but they must be the same when the instruction is used like this.
150*0b57cec5SDimitry Andricmulticlass CompareZeroFP<Instruction insn, RegisterOperand cls> {
151*0b57cec5SDimitry Andric  def : Pat<(z_fcmp cls:$reg, (fpimm0)), (insn cls:$reg, cls:$reg)>;
152*0b57cec5SDimitry Andric  // The sign of the zero makes no difference.
153*0b57cec5SDimitry Andric  def : Pat<(z_fcmp cls:$reg, (fpimmneg0)), (insn cls:$reg, cls:$reg)>;
154*0b57cec5SDimitry Andric}
155*0b57cec5SDimitry Andric
156*0b57cec5SDimitry Andric// Use INSN for performing binary operation OPERATION of type VT
157*0b57cec5SDimitry Andric// on registers of class CLS.
158*0b57cec5SDimitry Andricclass BinaryRRWithType<Instruction insn, RegisterOperand cls,
159*0b57cec5SDimitry Andric                       SDPatternOperator operator, ValueType vt>
160*0b57cec5SDimitry Andric  : Pat<(vt (operator cls:$x, cls:$y)), (insn cls:$x, cls:$y)>;
161*0b57cec5SDimitry Andric
162*0b57cec5SDimitry Andric// Use INSN to perform conversion operation OPERATOR, with the input being
163*0b57cec5SDimitry Andric// TR2 and the output being TR1.  SUPPRESS is 4 to suppress inexact conditions
164*0b57cec5SDimitry Andric// and 0 to allow them.  MODE is the rounding mode to use.
165*0b57cec5SDimitry Andricclass FPConversion<Instruction insn, SDPatternOperator operator, TypedReg tr1,
166*0b57cec5SDimitry Andric                   TypedReg tr2, bits<3> suppress, bits<4> mode>
167*0b57cec5SDimitry Andric  : Pat<(tr1.vt (operator (tr2.vt tr2.op:$vec))),
168*0b57cec5SDimitry Andric        (insn tr2.op:$vec, suppress, mode)>;
169*0b57cec5SDimitry Andric
170*0b57cec5SDimitry Andric// Use INSN to perform mininum/maximum operation OPERATOR on type TR.
171*0b57cec5SDimitry Andric// FUNCTION is the type of minimum/maximum function to perform.
172*0b57cec5SDimitry Andricclass FPMinMax<Instruction insn, SDPatternOperator operator, TypedReg tr,
173*0b57cec5SDimitry Andric               bits<4> function>
174*0b57cec5SDimitry Andric  : Pat<(tr.vt (operator (tr.vt tr.op:$vec1), (tr.vt tr.op:$vec2))),
175*0b57cec5SDimitry Andric        (insn tr.op:$vec1, tr.op:$vec2, function)>;
176