15f613dfdSUlrich Weigand//===-- SystemZPatterns.td - SystemZ-specific pattern rules ---*- tblgen-*-===//
25f613dfdSUlrich Weigand//
32946cd70SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f613dfdSUlrich Weigand//
75f613dfdSUlrich Weigand//===----------------------------------------------------------------------===//
85f613dfdSUlrich Weigand
95f613dfdSUlrich Weigand// Record that INSN performs a 64-bit version of unary operator OPERATOR
105f613dfdSUlrich Weigand// in which the operand is sign-extended from 32 to 64 bits.
115f613dfdSUlrich Weigandmulticlass SXU<SDPatternOperator operator, Instruction insn> {
125f613dfdSUlrich Weigand  def : Pat<(operator (sext (i32 GR32:$src))),
135f613dfdSUlrich Weigand            (insn GR32:$src)>;
145f613dfdSUlrich Weigand  def : Pat<(operator (sext_inreg GR64:$src, i32)),
1587a44364SRichard Sandiford            (insn (EXTRACT_SUBREG GR64:$src, subreg_l32))>;
165f613dfdSUlrich Weigand}
175f613dfdSUlrich Weigand
185f613dfdSUlrich Weigand// Record that INSN performs a 64-bit version of binary operator OPERATOR
195f613dfdSUlrich Weigand// in which the first operand has class CLS and which the second operand
205f613dfdSUlrich Weigand// is sign-extended from a 32-bit register.
215f613dfdSUlrich Weigandmulticlass SXB<SDPatternOperator operator, RegisterOperand cls,
225f613dfdSUlrich Weigand               Instruction insn> {
235f613dfdSUlrich Weigand  def : Pat<(operator cls:$src1, (sext GR32:$src2)),
245f613dfdSUlrich Weigand            (insn cls:$src1, GR32:$src2)>;
255f613dfdSUlrich Weigand  def : Pat<(operator cls:$src1, (sext_inreg GR64:$src2, i32)),
2687a44364SRichard Sandiford            (insn cls:$src1, (EXTRACT_SUBREG GR64:$src2, subreg_l32))>;
275f613dfdSUlrich Weigand}
285f613dfdSUlrich Weigand
295f613dfdSUlrich Weigand// Like SXB, but for zero extension.
305f613dfdSUlrich Weigandmulticlass ZXB<SDPatternOperator operator, RegisterOperand cls,
315f613dfdSUlrich Weigand               Instruction insn> {
325f613dfdSUlrich Weigand  def : Pat<(operator cls:$src1, (zext GR32:$src2)),
335f613dfdSUlrich Weigand            (insn cls:$src1, GR32:$src2)>;
345f613dfdSUlrich Weigand  def : Pat<(operator cls:$src1, (and GR64:$src2, 0xffffffff)),
3587a44364SRichard Sandiford            (insn cls:$src1, (EXTRACT_SUBREG GR64:$src2, subreg_l32))>;
365f613dfdSUlrich Weigand}
375f613dfdSUlrich Weigand
385f613dfdSUlrich Weigand// Record that INSN performs a binary read-modify-write operation,
395f613dfdSUlrich Weigand// with LOAD, OPERATOR and STORE being the read, modify and write
405f613dfdSUlrich Weigand// respectively.  MODE is the addressing mode and IMM is the type
415f613dfdSUlrich Weigand// of the second operand.
425f613dfdSUlrich Weigandclass RMWI<SDPatternOperator load, SDPatternOperator operator,
435f613dfdSUlrich Weigand           SDPatternOperator store, AddressingMode mode,
445f613dfdSUlrich Weigand           PatFrag imm, Instruction insn>
455f613dfdSUlrich Weigand  : Pat<(store (operator (load mode:$addr), imm:$src), mode:$addr),
465f613dfdSUlrich Weigand        (insn mode:$addr, (UIMM8 imm:$src))>;
475f613dfdSUlrich Weigand
485f613dfdSUlrich Weigand// Record that INSN performs binary operation OPERATION on a byte
495f613dfdSUlrich Weigand// memory location.  IMM is the type of the second operand.
505f613dfdSUlrich Weigandmulticlass RMWIByte<SDPatternOperator operator, AddressingMode mode,
515f613dfdSUlrich Weigand                    Instruction insn> {
52b86a8348SRichard Sandiford  def : RMWI<anyextloadi8, operator, truncstorei8, mode, imm32, insn>;
53b86a8348SRichard Sandiford  def : RMWI<anyextloadi8, operator, truncstorei8, mode, imm64, insn>;
545f613dfdSUlrich Weigand}
555f613dfdSUlrich Weigand
565f613dfdSUlrich Weigand// Record that INSN performs insertion TYPE into a register of class CLS.
575f613dfdSUlrich Weigand// The inserted operand is loaded using LOAD from an address of mode MODE.
585f613dfdSUlrich Weigandmulticlass InsertMem<string type, Instruction insn, RegisterOperand cls,
595f613dfdSUlrich Weigand                     SDPatternOperator load, AddressingMode mode> {
60*2cb48d62SFangrui Song  def : Pat<(!cast<SDPatternOperator>("or_as_"#type)
615f613dfdSUlrich Weigand              cls:$src1, (load mode:$src2)),
625f613dfdSUlrich Weigand            (insn cls:$src1, mode:$src2)>;
63*2cb48d62SFangrui Song  def : Pat<(!cast<SDPatternOperator>("or_as_rev"#type)
645f613dfdSUlrich Weigand              (load mode:$src2), cls:$src1),
655f613dfdSUlrich Weigand            (insn cls:$src1, mode:$src2)>;
665f613dfdSUlrich Weigand}
6797846491SRichard Sandiford
686cbd7f0cSRichard Sandiford// INSN stores the low 32 bits of a GPR to a memory with addressing mode MODE.
696cbd7f0cSRichard Sandiford// Record that it is equivalent to using OPERATOR to store a GR64.
706cbd7f0cSRichard Sandifordclass StoreGR64<Instruction insn, SDPatternOperator operator,
716cbd7f0cSRichard Sandiford                AddressingMode mode>
726cbd7f0cSRichard Sandiford  : Pat<(operator GR64:$R1, mode:$XBD2),
7387a44364SRichard Sandiford        (insn (EXTRACT_SUBREG GR64:$R1, subreg_l32), mode:$XBD2)>;
746cbd7f0cSRichard Sandiford
756cbd7f0cSRichard Sandiford// INSN and INSNY are an RX/RXY pair of instructions that store the low
766cbd7f0cSRichard Sandiford// 32 bits of a GPR to memory.  Record that they are equivalent to using
776cbd7f0cSRichard Sandiford// OPERATOR to store a GR64.
786cbd7f0cSRichard Sandifordmulticlass StoreGR64Pair<Instruction insn, Instruction insny,
796cbd7f0cSRichard Sandiford                         SDPatternOperator operator> {
806cbd7f0cSRichard Sandiford  def : StoreGR64<insn, operator, bdxaddr12pair>;
816cbd7f0cSRichard Sandiford  def : StoreGR64<insny, operator, bdxaddr20pair>;
826cbd7f0cSRichard Sandiford}
836cbd7f0cSRichard Sandiford
846cbd7f0cSRichard Sandiford// INSN stores the low 32 bits of a GPR using PC-relative addressing.
856cbd7f0cSRichard Sandiford// Record that it is equivalent to using OPERATOR to store a GR64.
866cbd7f0cSRichard Sandifordclass StoreGR64PC<Instruction insn, SDPatternOperator operator>
876cbd7f0cSRichard Sandiford  : Pat<(operator GR64:$R1, pcrel32:$XBD2),
8887a44364SRichard Sandiford        (insn (EXTRACT_SUBREG GR64:$R1, subreg_l32), pcrel32:$XBD2)> {
896cbd7f0cSRichard Sandiford  // We want PC-relative addresses to be tried ahead of BD and BDX addresses.
906cbd7f0cSRichard Sandiford  // However, BDXs have two extra operands and are therefore 6 units more
916cbd7f0cSRichard Sandiford  // complex.
926cbd7f0cSRichard Sandiford  let AddedComplexity = 7;
936cbd7f0cSRichard Sandiford}
946cbd7f0cSRichard Sandiford
956cbd7f0cSRichard Sandiford// INSN and INSNINV conditionally store the low 32 bits of a GPR to memory,
966cbd7f0cSRichard Sandiford// with INSN storing when the condition is true and INSNINV storing when the
976cbd7f0cSRichard Sandiford// condition is false.  Record that they are equivalent to a LOAD/select/STORE
986cbd7f0cSRichard Sandiford// sequence for GR64s.
996cbd7f0cSRichard Sandifordmulticlass CondStores64<Instruction insn, Instruction insninv,
1006cbd7f0cSRichard Sandiford                        SDPatternOperator store, SDPatternOperator load,
1016cbd7f0cSRichard Sandiford                        AddressingMode mode> {
1026cbd7f0cSRichard Sandiford  def : Pat<(store (z_select_ccmask GR64:$new, (load mode:$addr),
1033ecab8e4SMatt Arsenault                                    imm32zx4_timm:$valid, imm32zx4_timm:$cc),
1046cbd7f0cSRichard Sandiford                   mode:$addr),
10587a44364SRichard Sandiford            (insn (EXTRACT_SUBREG GR64:$new, subreg_l32), mode:$addr,
106ca44614aSRichard Sandiford                  imm32zx4:$valid, imm32zx4:$cc)>;
1076cbd7f0cSRichard Sandiford  def : Pat<(store (z_select_ccmask (load mode:$addr), GR64:$new,
1083ecab8e4SMatt Arsenault                                    imm32zx4_timm:$valid, imm32zx4_timm:$cc),
1096cbd7f0cSRichard Sandiford                   mode:$addr),
11087a44364SRichard Sandiford            (insninv (EXTRACT_SUBREG GR64:$new, subreg_l32), mode:$addr,
111ca44614aSRichard Sandiford                     imm32zx4:$valid, imm32zx4:$cc)>;
1126cbd7f0cSRichard Sandiford}
1136cbd7f0cSRichard Sandiford
114178273a1SRichard Sandiford// Try to use MVC instruction INSN for a load of type LOAD followed by a store
115178273a1SRichard Sandiford// of the same size.  VT is the type of the intermediate (legalized) value and
116178273a1SRichard Sandiford// LENGTH is the number of bytes loaded by LOAD.
117178273a1SRichard Sandifordmulticlass MVCLoadStore<SDPatternOperator load, ValueType vt, Instruction insn,
118178273a1SRichard Sandiford                        bits<5> length> {
119178273a1SRichard Sandiford  def : Pat<(mvc_store (vt (load bdaddr12only:$src)), bdaddr12only:$dest),
12097846491SRichard Sandiford            (insn bdaddr12only:$dest, bdaddr12only:$src, length)>;
12197846491SRichard Sandiford}
1229f11bc19SRichard Sandiford
123178273a1SRichard Sandiford// Use NC-like instruction INSN for block_op operation OPERATOR.
124178273a1SRichard Sandiford// The other operand is a load of type LOAD, which accesses LENGTH bytes.
125178273a1SRichard Sandiford// VT is the intermediate legalized type in which the binary operation
126178273a1SRichard Sandiford// is actually done.
127178273a1SRichard Sandifordmulticlass BinaryLoadStore<SDPatternOperator operator, SDPatternOperator load,
128178273a1SRichard Sandiford                           ValueType vt, Instruction insn, bits<5> length> {
129178273a1SRichard Sandiford  def : Pat<(operator (vt (load bdaddr12only:$src)), bdaddr12only:$dest),
130178273a1SRichard Sandiford            (insn bdaddr12only:$dest, bdaddr12only:$src, length)>;
131178273a1SRichard Sandiford}
132178273a1SRichard Sandiford
133178273a1SRichard Sandiford// A convenient way of generating all block peepholes for a particular
134178273a1SRichard Sandiford// LOAD/VT/LENGTH combination.
135178273a1SRichard Sandifordmulticlass BlockLoadStore<SDPatternOperator load, ValueType vt,
136178273a1SRichard Sandiford                          Instruction mvc, Instruction nc, Instruction oc,
137178273a1SRichard Sandiford                          Instruction xc, bits<5> length> {
138178273a1SRichard Sandiford  defm : MVCLoadStore<load, vt, mvc, length>;
139178273a1SRichard Sandiford  defm : BinaryLoadStore<block_and1, load, vt, nc, length>;
140178273a1SRichard Sandiford  defm : BinaryLoadStore<block_and2, load, vt, nc, length>;
141178273a1SRichard Sandiford  defm : BinaryLoadStore<block_or1,  load, vt, oc, length>;
142178273a1SRichard Sandiford  defm : BinaryLoadStore<block_or2,  load, vt, oc, length>;
143178273a1SRichard Sandiford  defm : BinaryLoadStore<block_xor1, load, vt, xc, length>;
144178273a1SRichard Sandiford  defm : BinaryLoadStore<block_xor2, load, vt, xc, length>;
145178273a1SRichard Sandiford}
146178273a1SRichard Sandiford
1479f11bc19SRichard Sandiford// Record that INSN is a LOAD AND TEST that can be used to compare
1489f11bc19SRichard Sandiford// registers in CLS against zero.  The instruction has separate R1 and R2
1499f11bc19SRichard Sandiford// operands, but they must be the same when the instruction is used like this.
150198ddf83SRichard Sandifordmulticlass CompareZeroFP<Instruction insn, RegisterOperand cls> {
1519db13b5aSUlrich Weigand  def : Pat<(z_any_fcmp cls:$reg, (fpimm0)), (insn cls:$reg, cls:$reg)>;
152198ddf83SRichard Sandiford  // The sign of the zero makes no difference.
1539db13b5aSUlrich Weigand  def : Pat<(z_any_fcmp cls:$reg, (fpimmneg0)), (insn cls:$reg, cls:$reg)>;
154198ddf83SRichard Sandiford}
155cd808237SUlrich Weigand
156cd808237SUlrich Weigand// Use INSN for performing binary operation OPERATION of type VT
157cd808237SUlrich Weigand// on registers of class CLS.
158cd808237SUlrich Weigandclass BinaryRRWithType<Instruction insn, RegisterOperand cls,
159cd808237SUlrich Weigand                       SDPatternOperator operator, ValueType vt>
160cd808237SUlrich Weigand  : Pat<(vt (operator cls:$x, cls:$y)), (insn cls:$x, cls:$y)>;
161cd808237SUlrich Weigand
162cd808237SUlrich Weigand// Use INSN to perform conversion operation OPERATOR, with the input being
163cd808237SUlrich Weigand// TR2 and the output being TR1.  SUPPRESS is 4 to suppress inexact conditions
164cd808237SUlrich Weigand// and 0 to allow them.  MODE is the rounding mode to use.
165cd808237SUlrich Weigandclass FPConversion<Instruction insn, SDPatternOperator operator, TypedReg tr1,
166cd808237SUlrich Weigand                   TypedReg tr2, bits<3> suppress, bits<4> mode>
167cd808237SUlrich Weigand  : Pat<(tr1.vt (operator (tr2.vt tr2.op:$vec))),
168cd808237SUlrich Weigand        (insn tr2.op:$vec, suppress, mode)>;
1692b3482feSUlrich Weigand
1700312b9f5SKazuaki Ishizaki// Use INSN to perform minimum/maximum operation OPERATOR on type TR.
1712b3482feSUlrich Weigand// FUNCTION is the type of minimum/maximum function to perform.
1722b3482feSUlrich Weigandclass FPMinMax<Instruction insn, SDPatternOperator operator, TypedReg tr,
1732b3482feSUlrich Weigand               bits<4> function>
1742b3482feSUlrich Weigand  : Pat<(tr.vt (operator (tr.vt tr.op:$vec1), (tr.vt tr.op:$vec2))),
1752b3482feSUlrich Weigand        (insn tr.op:$vec1, tr.op:$vec2, function)>;
176