1//===- Combine.td - Combine rule definitions ---------------*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Declare GlobalISel combine rules and provide mechanisms to opt-out.
10//
11//===----------------------------------------------------------------------===//
12
13// Common base class for GICombineRule and GICombineGroup.
14class GICombine {
15  // See GICombineGroup. We only declare it here to make the tablegen pass
16  // simpler.
17  list<GICombine> Rules = ?;
18}
19
20// A group of combine rules that can be added to a GICombiner or another group.
21class GICombineGroup<list<GICombine> rules> : GICombine {
22  // The rules contained in this group. The rules in a group are flattened into
23  // a single list and sorted into whatever order is most efficient. However,
24  // they will never be re-ordered such that behaviour differs from the
25  // specified order. It is therefore possible to use the order of rules in this
26  // list to describe priorities.
27  let Rules = rules;
28}
29
30class GICombinerHelperArg<string type, string name> {
31  string Type = type;
32  string Name = name;
33}
34
35// Declares a combiner helper class
36class GICombinerHelper<string classname, list<GICombine> rules>
37    : GICombineGroup<rules> {
38  // The class name to use in the generated output.
39  string Classname = classname;
40  // The name of a run-time compiler option that will be generated to disable
41  // specific rules within this combiner.
42  string DisableRuleOption = ?;
43  // The state class to inherit from (if any). The generated helper will inherit
44  // from this class and will forward arguments to its constructors.
45  string StateClass = "";
46  // Any additional arguments that should be appended to the tryCombine*().
47  list<GICombinerHelperArg> AdditionalArguments =
48      [GICombinerHelperArg<"CombinerHelper &", "Helper">];
49}
50class GICombineRule<dag defs, dag match, dag apply> : GICombine {
51  /// Defines the external interface of the match rule. This includes:
52  /// * The names of the root nodes (requires at least one)
53  /// See GIDefKind for details.
54  dag Defs = defs;
55
56  /// Defines the things which must be true for the pattern to match
57  /// See GIMatchKind for details.
58  dag Match = match;
59
60  /// Defines the things which happen after the decision is made to apply a
61  /// combine rule.
62  /// See GIApplyKind for details.
63  dag Apply = apply;
64
65  /// Defines the predicates that are checked before the match function
66  /// is called. Targets can use this to, for instance, check Subtarget
67  /// features.
68  list<Predicate> Predicates = [];
69}
70
71/// The operator at the root of a GICombineRule.Defs dag.
72def defs;
73
74/// All arguments of the defs operator must be subclasses of GIDefKind or
75/// sub-dags whose operator is GIDefKindWithArgs.
76class GIDefKind;
77class GIDefKindWithArgs;
78/// Declare a root node. There must be at least one of these in every combine
79/// rule.
80/// TODO: The plan is to elide `root` definitions and determine it from the DAG
81///       itself with an overide for situations where the usual determination
82///       is incorrect.
83def root : GIDefKind;
84
85/// Declares data that is passed from the match stage to the apply stage.
86class GIDefMatchData<string type> : GIDefKind {
87  /// A C++ type name indicating the storage type.
88  string Type = type;
89}
90
91def extending_load_matchdata : GIDefMatchData<"PreferredTuple">;
92def indexed_load_store_matchdata : GIDefMatchData<"IndexedLoadStoreMatchInfo">;
93def instruction_steps_matchdata: GIDefMatchData<"InstructionStepsMatchInfo">;
94
95/// The operator at the root of a GICombineRule.Match dag.
96def match;
97/// All arguments of the match operator must be either:
98/// * A subclass of GIMatchKind
99/// * A subclass of GIMatchKindWithArgs
100/// * A subclass of Instruction
101/// * A MIR code block (deprecated)
102/// The GIMatchKind and GIMatchKindWithArgs cases are described in more detail
103/// in their definitions below.
104/// For the Instruction case, these are collected into a DAG where operand names
105/// that occur multiple times introduce edges.
106class GIMatchKind;
107class GIMatchKindWithArgs;
108
109/// In lieu of having proper macro support. Trivial one-off opcode checks can be
110/// performed with this.
111def wip_match_opcode : GIMatchKindWithArgs;
112
113/// The operator at the root of a GICombineRule.Apply dag.
114def apply;
115/// All arguments of the apply operator must be subclasses of GIApplyKind, or
116/// sub-dags whose operator is GIApplyKindWithArgs, or an MIR block
117/// (deprecated).
118class GIApplyKind;
119class GIApplyKindWithArgs;
120
121def register_matchinfo: GIDefMatchData<"Register">;
122def int64_matchinfo: GIDefMatchData<"int64_t">;
123def apint_matchinfo : GIDefMatchData<"APInt">;
124def build_fn_matchinfo :
125GIDefMatchData<"std::function<void(MachineIRBuilder &)>">;
126def unsigned_matchinfo: GIDefMatchData<"unsigned">;
127
128def copy_prop : GICombineRule<
129  (defs root:$d),
130  (match (COPY $d, $s):$mi,
131         [{ return Helper.matchCombineCopy(*${mi}); }]),
132  (apply [{ Helper.applyCombineCopy(*${mi}); }])>;
133
134// idempotent operations
135// Fold (freeze (freeze x)) -> (freeze x).
136// Fold (fabs (fabs x)) -> (fabs x).
137// Fold (fcanonicalize (fcanonicalize x)) -> (fcanonicalize x).
138def idempotent_prop : GICombineRule<
139   (defs root:$mi),
140   (match (wip_match_opcode G_FREEZE, G_FABS, G_FCANONICALIZE):$mi,
141          [{ return MRI.getVRegDef(${mi}->getOperand(1).getReg())->getOpcode() ==
142                    ${mi}->getOpcode(); }]),
143   (apply [{ Helper.replaceSingleDefInstWithOperand(*${mi}, 1); }])>;
144
145
146def extending_loads : GICombineRule<
147  (defs root:$root, extending_load_matchdata:$matchinfo),
148  (match (wip_match_opcode G_LOAD, G_SEXTLOAD, G_ZEXTLOAD):$root,
149         [{ return Helper.matchCombineExtendingLoads(*${root}, ${matchinfo}); }]),
150  (apply [{ Helper.applyCombineExtendingLoads(*${root}, ${matchinfo}); }])>;
151
152def load_and_mask : GICombineRule<
153  (defs root:$root, build_fn_matchinfo:$matchinfo),
154  (match (wip_match_opcode G_AND):$root,
155        [{ return Helper.matchCombineLoadWithAndMask(*${root}, ${matchinfo}); }]),
156  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
157def combines_for_extload: GICombineGroup<[extending_loads, load_and_mask]>;
158
159def sext_trunc_sextload : GICombineRule<
160  (defs root:$d),
161  (match (wip_match_opcode G_SEXT_INREG):$d,
162         [{ return Helper.matchSextTruncSextLoad(*${d}); }]),
163  (apply [{ Helper.applySextTruncSextLoad(*${d}); }])>;
164
165def sext_inreg_of_load_matchdata : GIDefMatchData<"std::tuple<Register, unsigned>">;
166def sext_inreg_of_load : GICombineRule<
167  (defs root:$root, sext_inreg_of_load_matchdata:$matchinfo),
168  (match (wip_match_opcode G_SEXT_INREG):$root,
169         [{ return Helper.matchSextInRegOfLoad(*${root}, ${matchinfo}); }]),
170  (apply [{ Helper.applySextInRegOfLoad(*${root}, ${matchinfo}); }])>;
171
172def sext_inreg_to_zext_inreg : GICombineRule<
173  (defs root:$dst),
174  (match
175    (G_SEXT_INREG $dst, $src, $imm):$root,
176      [{
177        unsigned BitWidth = MRI.getType(${src}.getReg()).getScalarSizeInBits();
178        return Helper.getKnownBits()->maskedValueIsZero(${src}.getReg(),
179                 APInt::getOneBitSet(BitWidth, ${imm}.getImm() - 1)); }]),
180    (apply [{
181      Helper.getBuilder().setInstrAndDebugLoc(*${root});
182      Helper.getBuilder().buildZExtInReg(${dst}, ${src}, ${imm}.getImm());
183      ${root}->eraseFromParent();
184  }])
185>;
186
187def combine_indexed_load_store : GICombineRule<
188  (defs root:$root, indexed_load_store_matchdata:$matchinfo),
189  (match (wip_match_opcode G_LOAD, G_SEXTLOAD, G_ZEXTLOAD, G_STORE):$root,
190         [{ return Helper.matchCombineIndexedLoadStore(*${root}, ${matchinfo}); }]),
191  (apply [{ Helper.applyCombineIndexedLoadStore(*${root}, ${matchinfo}); }])>;
192
193def opt_brcond_by_inverting_cond_matchdata : GIDefMatchData<"MachineInstr *">;
194def opt_brcond_by_inverting_cond : GICombineRule<
195  (defs root:$root, opt_brcond_by_inverting_cond_matchdata:$matchinfo),
196  (match (wip_match_opcode G_BR):$root,
197         [{ return Helper.matchOptBrCondByInvertingCond(*${root}, ${matchinfo}); }]),
198  (apply [{ Helper.applyOptBrCondByInvertingCond(*${root}, ${matchinfo}); }])>;
199
200def ptr_add_immed_matchdata : GIDefMatchData<"PtrAddChain">;
201def ptr_add_immed_chain : GICombineRule<
202  (defs root:$d, ptr_add_immed_matchdata:$matchinfo),
203  (match (wip_match_opcode G_PTR_ADD):$d,
204         [{ return Helper.matchPtrAddImmedChain(*${d}, ${matchinfo}); }]),
205  (apply [{ Helper.applyPtrAddImmedChain(*${d}, ${matchinfo}); }])>;
206
207def shifts_too_big : GICombineRule<
208  (defs root:$root),
209  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR):$root,
210         [{ return Helper.matchShiftsTooBig(*${root}); }]),
211  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
212
213// Fold shift (shift base x), y -> shift base, (x+y), if shifts are same
214def shift_immed_matchdata : GIDefMatchData<"RegisterImmPair">;
215def shift_immed_chain : GICombineRule<
216  (defs root:$d, shift_immed_matchdata:$matchinfo),
217  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR, G_SSHLSAT, G_USHLSAT):$d,
218         [{ return Helper.matchShiftImmedChain(*${d}, ${matchinfo}); }]),
219  (apply [{ Helper.applyShiftImmedChain(*${d}, ${matchinfo}); }])>;
220
221// Transform shift (logic (shift X, C0), Y), C1
222//        -> logic (shift X, (C0+C1)), (shift Y, C1), if shifts are same
223def shift_of_shifted_logic_matchdata : GIDefMatchData<"ShiftOfShiftedLogic">;
224def shift_of_shifted_logic_chain : GICombineRule<
225  (defs root:$d, shift_of_shifted_logic_matchdata:$matchinfo),
226  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR, G_USHLSAT, G_SSHLSAT):$d,
227         [{ return Helper.matchShiftOfShiftedLogic(*${d}, ${matchinfo}); }]),
228  (apply [{ Helper.applyShiftOfShiftedLogic(*${d}, ${matchinfo}); }])>;
229
230def mul_to_shl_matchdata : GIDefMatchData<"unsigned">;
231def mul_to_shl : GICombineRule<
232  (defs root:$d, mul_to_shl_matchdata:$matchinfo),
233  (match (G_MUL $d, $op1, $op2):$mi,
234         [{ return Helper.matchCombineMulToShl(*${mi}, ${matchinfo}); }]),
235  (apply [{ Helper.applyCombineMulToShl(*${mi}, ${matchinfo}); }])>;
236
237// shl ([asz]ext x), y => zext (shl x, y), if shift does not overflow int
238def reduce_shl_of_extend_matchdata : GIDefMatchData<"RegisterImmPair">;
239def reduce_shl_of_extend : GICombineRule<
240  (defs root:$dst, reduce_shl_of_extend_matchdata:$matchinfo),
241  (match (G_SHL $dst, $src0, $src1):$mi,
242         [{ return Helper.matchCombineShlOfExtend(*${mi}, ${matchinfo}); }]),
243  (apply [{ Helper.applyCombineShlOfExtend(*${mi}, ${matchinfo}); }])>;
244
245// Combine (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
246// Combine (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
247def commute_shift : GICombineRule<
248  (defs root:$d, build_fn_matchinfo:$matchinfo),
249  (match (wip_match_opcode G_SHL):$d,
250         [{ return Helper.matchCommuteShift(*${d}, ${matchinfo}); }]),
251  (apply [{ Helper.applyBuildFn(*${d}, ${matchinfo}); }])>;
252
253def narrow_binop_feeding_and : GICombineRule<
254  (defs root:$root, build_fn_matchinfo:$matchinfo),
255  (match (wip_match_opcode G_AND):$root,
256         [{ return Helper.matchNarrowBinopFeedingAnd(*${root}, ${matchinfo}); }]),
257  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
258
259// [us]itofp(undef) = 0, because the result value is bounded.
260def undef_to_fp_zero : GICombineRule<
261  (defs root:$root),
262  (match (wip_match_opcode G_UITOFP, G_SITOFP):$root,
263         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
264  (apply [{ Helper.replaceInstWithFConstant(*${root}, 0.0); }])>;
265
266def undef_to_int_zero: GICombineRule<
267  (defs root:$root),
268  (match (wip_match_opcode G_AND, G_MUL):$root,
269         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
270  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
271
272def undef_to_negative_one: GICombineRule<
273  (defs root:$root),
274  (match (wip_match_opcode G_OR):$root,
275         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
276  (apply [{ Helper.replaceInstWithConstant(*${root}, -1); }])>;
277
278def binop_left_undef_to_zero: GICombineRule<
279  (defs root:$root),
280  (match (wip_match_opcode G_SHL, G_UDIV, G_UREM):$root,
281         [{ return Helper.matchOperandIsUndef(*${root}, 1); }]),
282  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
283
284def binop_right_undef_to_undef: GICombineRule<
285  (defs root:$root),
286  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR):$root,
287         [{ return Helper.matchOperandIsUndef(*${root}, 2); }]),
288  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
289
290def unary_undef_to_zero: GICombineRule<
291  (defs root:$root),
292  (match (wip_match_opcode G_ABS):$root,
293         [{ return Helper.matchOperandIsUndef(*${root}, 1); }]),
294  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
295
296// Instructions where if any source operand is undef, the instruction can be
297// replaced with undef.
298def propagate_undef_any_op: GICombineRule<
299  (defs root:$root),
300  (match (wip_match_opcode G_ADD, G_FPTOSI, G_FPTOUI, G_SUB, G_XOR, G_TRUNC):$root,
301         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
302  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
303
304// Instructions where if all source operands are undef, the instruction can be
305// replaced with undef.
306def propagate_undef_all_ops: GICombineRule<
307  (defs root:$root),
308  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
309          [{ return Helper.matchAllExplicitUsesAreUndef(*${root}); }]),
310  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
311
312// Replace a G_SHUFFLE_VECTOR with an undef mask with a G_IMPLICIT_DEF.
313def propagate_undef_shuffle_mask: GICombineRule<
314  (defs root:$root),
315  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
316         [{ return Helper.matchUndefShuffleVectorMask(*${root}); }]),
317  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
318
319  // Replace an insert/extract element of an out of bounds index with undef.
320  def insert_extract_vec_elt_out_of_bounds : GICombineRule<
321  (defs root:$root),
322  (match (wip_match_opcode G_INSERT_VECTOR_ELT, G_EXTRACT_VECTOR_ELT):$root,
323         [{ return Helper.matchInsertExtractVecEltOutOfBounds(*${root}); }]),
324  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
325
326// Fold (cond ? x : x) -> x
327def select_same_val: GICombineRule<
328  (defs root:$root),
329  (match (wip_match_opcode G_SELECT):$root,
330    [{ return Helper.matchSelectSameVal(*${root}); }]),
331  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 2); }])
332>;
333
334// Fold (undef ? x : y) -> y
335def select_undef_cmp: GICombineRule<
336  (defs root:$root),
337  (match (wip_match_opcode G_SELECT):$root,
338    [{ return Helper.matchUndefSelectCmp(*${root}); }]),
339  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 2); }])
340>;
341
342// Fold (true ? x : y) -> x
343// Fold (false ? x : y) -> y
344def select_constant_cmp_matchdata : GIDefMatchData<"unsigned">;
345def select_constant_cmp: GICombineRule<
346  (defs root:$root, select_constant_cmp_matchdata:$matchinfo),
347  (match (wip_match_opcode G_SELECT):$root,
348    [{ return Helper.matchConstantSelectCmp(*${root}, ${matchinfo}); }]),
349  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, ${matchinfo}); }])
350>;
351
352def select_to_logical : GICombineRule<
353  (defs root:$root, build_fn_matchinfo:$matchinfo),
354  (match (wip_match_opcode G_SELECT):$root,
355    [{ return Helper.matchSelectToLogical(*${root}, ${matchinfo}); }]),
356  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])
357>;
358
359// Fold (C op x) -> (x op C)
360// TODO: handle more isCommutable opcodes
361// TODO: handle compares (currently not marked as isCommutable)
362def commute_constant_to_rhs : GICombineRule<
363  (defs root:$root),
364  (match (wip_match_opcode G_ADD, G_MUL, G_AND, G_OR, G_XOR):$root, [{
365    return getIConstantVRegVal(${root}->getOperand(1).getReg(), MRI).has_value();
366  }]),
367  (apply [{
368    Observer.changingInstr(*${root});
369    Register LHSReg = ${root}->getOperand(1).getReg();
370    Register RHSReg = ${root}->getOperand(2).getReg();
371    ${root}->getOperand(1).setReg(RHSReg);
372    ${root}->getOperand(2).setReg(LHSReg);
373    Observer.changedInstr(*${root});
374  }])
375>;
376
377// Fold x op 0 -> x
378def right_identity_zero: GICombineRule<
379  (defs root:$root),
380  (match (wip_match_opcode G_SUB, G_ADD, G_OR, G_XOR, G_SHL, G_ASHR, G_LSHR,
381                           G_PTR_ADD, G_ROTL, G_ROTR):$root,
382    [{ return Helper.matchConstantOp(${root}->getOperand(2), 0); }]),
383  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
384>;
385
386// Fold x op 1 -> x
387def right_identity_one: GICombineRule<
388  (defs root:$root),
389  (match (wip_match_opcode G_MUL):$root,
390    [{ return Helper.matchConstantOp(${root}->getOperand(2), 1); }]),
391  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
392>;
393
394// Fold (x op x) - > x
395def binop_same_val: GICombineRule<
396  (defs root:$root),
397  (match (wip_match_opcode G_AND, G_OR):$root,
398    [{ return Helper.matchBinOpSameVal(*${root}); }]),
399  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
400>;
401
402// Fold (0 op x) - > 0
403def binop_left_to_zero: GICombineRule<
404  (defs root:$root),
405  (match (wip_match_opcode G_SDIV, G_UDIV, G_SREM, G_UREM):$root,
406    [{ return Helper.matchOperandIsZero(*${root}, 1); }]),
407  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
408>;
409
410def urem_pow2_to_mask : GICombineRule<
411  (defs root:$root),
412  (match (wip_match_opcode G_UREM):$root,
413    [{ return Helper.matchOperandIsKnownToBeAPowerOfTwo(*${root}, 2); }]),
414  (apply [{ Helper.applySimplifyURemByPow2(*${root}); }])
415>;
416
417// Push a binary operator through a select on constants.
418//
419// binop (select cond, K0, K1), K2 ->
420//   select cond, (binop K0, K2), (binop K1, K2)
421
422// Every binary operator that has constant folding. We currently do
423// not have constant folding for G_FPOW, G_FMAXNUM_IEEE or
424// G_FMINNUM_IEEE.
425def fold_binop_into_select : GICombineRule<
426  (defs root:$root, unsigned_matchinfo:$select_op_no),
427  (match (wip_match_opcode
428    G_ADD, G_SUB, G_PTR_ADD, G_AND, G_OR, G_XOR,
429    G_SDIV, G_SREM, G_UDIV, G_UREM, G_LSHR, G_ASHR, G_SHL,
430    G_SMIN, G_SMAX, G_UMIN, G_UMAX,
431    G_FMUL, G_FADD, G_FSUB, G_FDIV, G_FREM,
432    G_FMINNUM, G_FMAXNUM, G_FMINIMUM, G_FMAXIMUM):$root,
433    [{ return Helper.matchFoldBinOpIntoSelect(*${root}, ${select_op_no}); }]),
434  (apply [{ Helper.applyFoldBinOpIntoSelect(*${root}, ${select_op_no}); }])
435>;
436
437// Transform d = [su]div(x, y) and r = [su]rem(x, y) - > d, r = [su]divrem(x, y)
438def div_rem_to_divrem_matchdata : GIDefMatchData<"MachineInstr *">;
439def div_rem_to_divrem : GICombineRule<
440  (defs root:$root, div_rem_to_divrem_matchdata:$matchinfo),
441  (match (wip_match_opcode G_SDIV, G_UDIV, G_SREM, G_UREM):$root,
442    [{ return Helper.matchCombineDivRem(*${root}, ${matchinfo}); }]),
443  (apply [{ Helper.applyCombineDivRem(*${root}, ${matchinfo}); }])
444>;
445
446// Fold (x op 0) - > 0
447def binop_right_to_zero: GICombineRule<
448  (defs root:$root),
449  (match (wip_match_opcode G_MUL):$root,
450    [{ return Helper.matchOperandIsZero(*${root}, 2); }]),
451  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 2); }])
452>;
453
454// Erase stores of undef values.
455def erase_undef_store : GICombineRule<
456  (defs root:$root),
457  (match (wip_match_opcode G_STORE):$root,
458    [{ return Helper.matchUndefStore(*${root}); }]),
459  (apply [{ Helper.eraseInst(*${root}); }])
460>;
461
462def simplify_add_to_sub_matchinfo: GIDefMatchData<"std::tuple<Register, Register>">;
463def simplify_add_to_sub: GICombineRule <
464  (defs root:$root, simplify_add_to_sub_matchinfo:$info),
465  (match (wip_match_opcode G_ADD):$root,
466    [{ return Helper.matchSimplifyAddToSub(*${root}, ${info}); }]),
467  (apply [{ Helper.applySimplifyAddToSub(*${root}, ${info});}])
468>;
469
470// Fold fp_op(cst) to the constant result of the floating point operation.
471class constant_fold_unary_fp_op_rule<Instruction opcode> : GICombineRule <
472  (defs root:$dst),
473  (match (opcode $dst, $src0):$root, (G_FCONSTANT $src0, $cst)),
474  (apply [{ Helper.applyCombineConstantFoldFpUnary(*${root}, ${cst}.getFPImm()); }])
475>;
476
477def constant_fold_fneg : constant_fold_unary_fp_op_rule<G_FNEG>;
478def constant_fold_fabs : constant_fold_unary_fp_op_rule<G_FABS>;
479def constant_fold_fsqrt : constant_fold_unary_fp_op_rule<G_FSQRT>;
480def constant_fold_flog2 : constant_fold_unary_fp_op_rule<G_FLOG2>;
481def constant_fold_fptrunc : constant_fold_unary_fp_op_rule<G_FPTRUNC>;
482
483def constant_fold_fp_ops : GICombineGroup<[
484  constant_fold_fneg,
485  constant_fold_fabs,
486  constant_fold_fsqrt,
487  constant_fold_flog2,
488  constant_fold_fptrunc
489]>;
490
491// Fold int2ptr(ptr2int(x)) -> x
492def p2i_to_i2p: GICombineRule<
493  (defs root:$root, register_matchinfo:$info),
494  (match (wip_match_opcode G_INTTOPTR):$root,
495    [{ return Helper.matchCombineI2PToP2I(*${root}, ${info}); }]),
496  (apply [{ Helper.applyCombineI2PToP2I(*${root}, ${info}); }])
497>;
498
499// Fold ptr2int(int2ptr(x)) -> x
500def i2p_to_p2i: GICombineRule<
501  (defs root:$dst, register_matchinfo:$info),
502  (match (G_INTTOPTR $t, $ptr),
503         (G_PTRTOINT $dst, $t):$mi,
504    [{ ${info} = ${ptr}.getReg(); return true; }]),
505  (apply [{ Helper.applyCombineP2IToI2P(*${mi}, ${info}); }])
506>;
507
508// Fold add ptrtoint(x), y -> ptrtoint (ptr_add x), y
509def add_p2i_to_ptradd_matchinfo : GIDefMatchData<"std::pair<Register, bool>">;
510def add_p2i_to_ptradd : GICombineRule<
511  (defs root:$root, add_p2i_to_ptradd_matchinfo:$info),
512  (match (wip_match_opcode G_ADD):$root,
513    [{ return Helper.matchCombineAddP2IToPtrAdd(*${root}, ${info}); }]),
514  (apply [{ Helper.applyCombineAddP2IToPtrAdd(*${root}, ${info}); }])
515>;
516
517// Fold (ptr_add (int2ptr C1), C2) -> C1 + C2
518def const_ptradd_to_i2p_matchinfo : GIDefMatchData<"APInt">;
519def const_ptradd_to_i2p: GICombineRule<
520  (defs root:$root, const_ptradd_to_i2p_matchinfo:$info),
521  (match (wip_match_opcode G_PTR_ADD):$root,
522    [{ return Helper.matchCombineConstPtrAddToI2P(*${root}, ${info}); }]),
523  (apply [{ Helper.applyCombineConstPtrAddToI2P(*${root}, ${info}); }])
524>;
525
526// Simplify: (logic_op (op x...), (op y...)) -> (op (logic_op x, y))
527def hoist_logic_op_with_same_opcode_hands: GICombineRule <
528  (defs root:$root, instruction_steps_matchdata:$info),
529  (match (wip_match_opcode G_AND, G_OR, G_XOR):$root,
530    [{ return Helper.matchHoistLogicOpWithSameOpcodeHands(*${root}, ${info}); }]),
531  (apply [{ Helper.applyBuildInstructionSteps(*${root}, ${info});}])
532>;
533
534// Fold ashr (shl x, C), C -> sext_inreg (C)
535def shl_ashr_to_sext_inreg_matchinfo : GIDefMatchData<"std::tuple<Register, int64_t>">;
536def shl_ashr_to_sext_inreg : GICombineRule<
537  (defs root:$root, shl_ashr_to_sext_inreg_matchinfo:$info),
538  (match (wip_match_opcode G_ASHR): $root,
539    [{ return Helper.matchAshrShlToSextInreg(*${root}, ${info}); }]),
540  (apply [{ Helper.applyAshShlToSextInreg(*${root}, ${info});}])
541>;
542
543// Fold and(and(x, C1), C2) -> C1&C2 ? and(x, C1&C2) : 0
544def overlapping_and: GICombineRule <
545  (defs root:$root, build_fn_matchinfo:$info),
546  (match (wip_match_opcode G_AND):$root,
547         [{ return Helper.matchOverlappingAnd(*${root}, ${info}); }]),
548  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])
549>;
550
551// Fold (x & y) -> x or (x & y) -> y when (x & y) is known to equal x or equal y.
552def redundant_and: GICombineRule <
553  (defs root:$root, register_matchinfo:$matchinfo),
554  (match (wip_match_opcode G_AND):$root,
555         [{ return Helper.matchRedundantAnd(*${root}, ${matchinfo}); }]),
556  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
557>;
558
559// Fold (x | y) -> x or (x | y) -> y when (x | y) is known to equal x or equal y.
560def redundant_or: GICombineRule <
561  (defs root:$root, register_matchinfo:$matchinfo),
562  (match (wip_match_opcode G_OR):$root,
563         [{ return Helper.matchRedundantOr(*${root}, ${matchinfo}); }]),
564  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
565>;
566
567// If the input is already sign extended, just drop the extension.
568// sext_inreg x, K ->
569//   if computeNumSignBits(x) >= (x.getScalarSizeInBits() - K + 1)
570def redundant_sext_inreg: GICombineRule <
571  (defs root:$root),
572  (match (wip_match_opcode G_SEXT_INREG):$root,
573         [{ return Helper.matchRedundantSExtInReg(*${root}); }]),
574     (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
575>;
576
577// Fold (anyext (trunc x)) -> x if the source type is same as
578// the destination type.
579def anyext_trunc_fold: GICombineRule <
580  (defs root:$root, register_matchinfo:$matchinfo),
581  (match (wip_match_opcode G_ANYEXT):$root,
582         [{ return Helper.matchCombineAnyExtTrunc(*${root}, ${matchinfo}); }]),
583  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
584>;
585
586// Fold (zext (trunc x)) -> x if the source type is same as the destination type
587// and truncated bits are known to be zero.
588def zext_trunc_fold_matchinfo : GIDefMatchData<"Register">;
589def zext_trunc_fold: GICombineRule <
590  (defs root:$root, zext_trunc_fold_matchinfo:$matchinfo),
591  (match (wip_match_opcode G_ZEXT):$root,
592         [{ return Helper.matchCombineZextTrunc(*${root}, ${matchinfo}); }]),
593  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
594>;
595
596// Fold ([asz]ext ([asz]ext x)) -> ([asz]ext x).
597def ext_ext_fold_matchinfo : GIDefMatchData<"std::tuple<Register, unsigned>">;
598def ext_ext_fold: GICombineRule <
599  (defs root:$root, ext_ext_fold_matchinfo:$matchinfo),
600  (match (wip_match_opcode G_ANYEXT, G_SEXT, G_ZEXT):$root,
601         [{ return Helper.matchCombineExtOfExt(*${root}, ${matchinfo}); }]),
602  (apply [{ Helper.applyCombineExtOfExt(*${root}, ${matchinfo}); }])
603>;
604
605def not_cmp_fold_matchinfo : GIDefMatchData<"SmallVector<Register, 4>">;
606def not_cmp_fold : GICombineRule<
607  (defs root:$d, not_cmp_fold_matchinfo:$info),
608  (match (wip_match_opcode G_XOR): $d,
609  [{ return Helper.matchNotCmp(*${d}, ${info}); }]),
610  (apply [{ Helper.applyNotCmp(*${d}, ${info}); }])
611>;
612
613// Fold (fneg (fneg x)) -> x.
614def fneg_fneg_fold: GICombineRule <
615  (defs root:$dst, register_matchinfo:$matchinfo),
616  (match (G_FNEG $t, $src),
617         (G_FNEG $dst, $t):$mi,
618         [{ ${matchinfo} = ${src}.getReg(); return true; }]),
619  (apply [{ Helper.replaceSingleDefInstWithReg(*${mi}, ${matchinfo}); }])
620>;
621
622// Fold (unmerge(merge x, y, z)) -> z, y, z.
623def unmerge_merge_matchinfo : GIDefMatchData<"SmallVector<Register, 8>">;
624def unmerge_merge : GICombineRule<
625  (defs root:$d, unmerge_merge_matchinfo:$info),
626  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
627  [{ return Helper.matchCombineUnmergeMergeToPlainValues(*${d}, ${info}); }]),
628  (apply [{ Helper.applyCombineUnmergeMergeToPlainValues(*${d}, ${info}); }])
629>;
630
631// Fold merge(unmerge).
632def merge_unmerge : GICombineRule<
633  (defs root:$d, register_matchinfo:$matchinfo),
634  (match (wip_match_opcode G_MERGE_VALUES):$d,
635  [{ return Helper.matchCombineMergeUnmerge(*${d}, ${matchinfo}); }]),
636  (apply [{ Helper.replaceSingleDefInstWithReg(*${d}, ${matchinfo}); }])
637>;
638
639// Fold (fabs (fneg x)) -> (fabs x).
640def fabs_fneg_fold: GICombineRule <
641  (defs root:$root, build_fn_matchinfo:$matchinfo),
642  (match (wip_match_opcode G_FABS):$root,
643         [{ return Helper.matchCombineFAbsOfFNeg(*${root}, ${matchinfo}); }]),
644  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
645
646// Fold (unmerge cst) -> cst1, cst2, ...
647def unmerge_cst_matchinfo : GIDefMatchData<"SmallVector<APInt, 8>">;
648def unmerge_cst : GICombineRule<
649  (defs root:$d, unmerge_cst_matchinfo:$info),
650  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
651  [{ return Helper.matchCombineUnmergeConstant(*${d}, ${info}); }]),
652  (apply [{ Helper.applyCombineUnmergeConstant(*${d}, ${info}); }])
653>;
654
655// Fold (unmerge undef) -> undef, undef, ...
656def unmerge_undef : GICombineRule<
657  (defs root:$root, build_fn_matchinfo:$info),
658  (match (wip_match_opcode G_UNMERGE_VALUES): $root,
659         [{ return Helper.matchCombineUnmergeUndef(*${root}, ${info}); }]),
660  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])
661>;
662
663// Transform x,y<dead> = unmerge z -> x = trunc z.
664def unmerge_dead_to_trunc : GICombineRule<
665  (defs root:$d),
666  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
667  [{ return Helper.matchCombineUnmergeWithDeadLanesToTrunc(*${d}); }]),
668  (apply [{ Helper.applyCombineUnmergeWithDeadLanesToTrunc(*${d}); }])
669>;
670
671// Transform x,y = unmerge(zext(z)) -> x = zext z; y = 0.
672def unmerge_zext_to_zext : GICombineRule<
673  (defs root:$d),
674  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
675  [{ return Helper.matchCombineUnmergeZExtToZExt(*${d}); }]),
676  (apply [{ Helper.applyCombineUnmergeZExtToZExt(*${d}); }])
677>;
678
679// Fold trunc ([asz]ext x) -> x or ([asz]ext x) or (trunc x).
680def trunc_ext_fold_matchinfo : GIDefMatchData<"std::pair<Register, unsigned>">;
681def trunc_ext_fold: GICombineRule <
682  (defs root:$root, trunc_ext_fold_matchinfo:$matchinfo),
683  (match (wip_match_opcode G_TRUNC):$root,
684         [{ return Helper.matchCombineTruncOfExt(*${root}, ${matchinfo}); }]),
685  (apply [{ Helper.applyCombineTruncOfExt(*${root}, ${matchinfo}); }])
686>;
687
688// Under certain conditions, transform:
689//  trunc (shl x, K)     -> shl (trunc x), K//
690//  trunc ([al]shr x, K) -> (trunc ([al]shr (trunc x), K))
691def trunc_shift_matchinfo : GIDefMatchData<"std::pair<MachineInstr*, LLT>">;
692def trunc_shift: GICombineRule <
693  (defs root:$root, trunc_shift_matchinfo:$matchinfo),
694  (match (wip_match_opcode G_TRUNC):$root,
695         [{ return Helper.matchCombineTruncOfShift(*${root}, ${matchinfo}); }]),
696  (apply [{ Helper.applyCombineTruncOfShift(*${root}, ${matchinfo}); }])
697>;
698
699// Transform (mul x, -1) -> (sub 0, x)
700def mul_by_neg_one: GICombineRule <
701  (defs root:$root),
702  (match (wip_match_opcode G_MUL):$root,
703         [{ return Helper.matchConstantOp(${root}->getOperand(2), -1); }]),
704  (apply [{ Helper.applyCombineMulByNegativeOne(*${root}); }])
705>;
706
707// Fold (xor (and x, y), y) -> (and (not x), y)
708def xor_of_and_with_same_reg_matchinfo :
709    GIDefMatchData<"std::pair<Register, Register>">;
710def xor_of_and_with_same_reg: GICombineRule <
711  (defs root:$root, xor_of_and_with_same_reg_matchinfo:$matchinfo),
712  (match (wip_match_opcode G_XOR):$root,
713         [{ return Helper.matchXorOfAndWithSameReg(*${root}, ${matchinfo}); }]),
714  (apply [{ Helper.applyXorOfAndWithSameReg(*${root}, ${matchinfo}); }])
715>;
716
717// Transform (ptr_add 0, x) -> (int_to_ptr x)
718def ptr_add_with_zero: GICombineRule<
719  (defs root:$root),
720  (match (wip_match_opcode G_PTR_ADD):$root,
721         [{ return Helper.matchPtrAddZero(*${root}); }]),
722  (apply [{ Helper.applyPtrAddZero(*${root}); }])>;
723
724def regs_small_vec : GIDefMatchData<"SmallVector<Register, 4>">;
725def combine_insert_vec_elts_build_vector : GICombineRule<
726  (defs root:$root, regs_small_vec:$info),
727  (match (wip_match_opcode G_INSERT_VECTOR_ELT):$root,
728    [{ return Helper.matchCombineInsertVecElts(*${root}, ${info}); }]),
729  (apply [{ Helper.applyCombineInsertVecElts(*${root}, ${info}); }])>;
730
731def load_or_combine : GICombineRule<
732  (defs root:$root, build_fn_matchinfo:$info),
733  (match (wip_match_opcode G_OR):$root,
734    [{ return Helper.matchLoadOrCombine(*${root}, ${info}); }]),
735  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
736
737def extend_through_phis_matchdata: GIDefMatchData<"MachineInstr*">;
738def extend_through_phis : GICombineRule<
739  (defs root:$root, extend_through_phis_matchdata:$matchinfo),
740  (match (wip_match_opcode G_PHI):$root,
741    [{ return Helper.matchExtendThroughPhis(*${root}, ${matchinfo}); }]),
742  (apply [{ Helper.applyExtendThroughPhis(*${root}, ${matchinfo}); }])>;
743
744// Currently only the one combine above.
745def insert_vec_elt_combines : GICombineGroup<
746                            [combine_insert_vec_elts_build_vector]>;
747
748def extract_vec_elt_build_vec : GICombineRule<
749  (defs root:$root, register_matchinfo:$matchinfo),
750  (match (wip_match_opcode G_EXTRACT_VECTOR_ELT):$root,
751    [{ return Helper.matchExtractVecEltBuildVec(*${root}, ${matchinfo}); }]),
752  (apply [{ Helper.applyExtractVecEltBuildVec(*${root}, ${matchinfo}); }])>;
753
754// Fold away full elt extracts from a build_vector.
755def extract_all_elts_from_build_vector_matchinfo :
756  GIDefMatchData<"SmallVector<std::pair<Register, MachineInstr*>>">;
757def extract_all_elts_from_build_vector : GICombineRule<
758  (defs root:$root, extract_all_elts_from_build_vector_matchinfo:$matchinfo),
759  (match (wip_match_opcode G_BUILD_VECTOR):$root,
760    [{ return Helper.matchExtractAllEltsFromBuildVector(*${root}, ${matchinfo}); }]),
761  (apply [{ Helper.applyExtractAllEltsFromBuildVector(*${root}, ${matchinfo}); }])>;
762
763def extract_vec_elt_combines : GICombineGroup<[
764  extract_vec_elt_build_vec,
765  extract_all_elts_from_build_vector]>;
766
767def funnel_shift_from_or_shift : GICombineRule<
768  (defs root:$root, build_fn_matchinfo:$info),
769  (match (wip_match_opcode G_OR):$root,
770    [{ return Helper.matchOrShiftToFunnelShift(*${root}, ${info}); }]),
771  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])
772>;
773
774def funnel_shift_to_rotate : GICombineRule<
775  (defs root:$root),
776  (match (wip_match_opcode G_FSHL, G_FSHR):$root,
777    [{ return Helper.matchFunnelShiftToRotate(*${root}); }]),
778  (apply [{ Helper.applyFunnelShiftToRotate(*${root}); }])
779>;
780
781def rotate_out_of_range : GICombineRule<
782  (defs root:$root),
783  (match (wip_match_opcode G_ROTR, G_ROTL):$root,
784    [{ return Helper.matchRotateOutOfRange(*${root}); }]),
785  (apply [{ Helper.applyRotateOutOfRange(*${root}); }])
786>;
787
788def icmp_to_true_false_known_bits : GICombineRule<
789  (defs root:$d, int64_matchinfo:$matchinfo),
790  (match (wip_match_opcode G_ICMP):$d,
791         [{ return Helper.matchICmpToTrueFalseKnownBits(*${d}, ${matchinfo}); }]),
792  (apply [{ Helper.replaceInstWithConstant(*${d}, ${matchinfo}); }])>;
793
794def icmp_to_lhs_known_bits : GICombineRule<
795  (defs root:$root, build_fn_matchinfo:$info),
796  (match (wip_match_opcode G_ICMP):$root,
797         [{ return Helper.matchICmpToLHSKnownBits(*${root}, ${info}); }]),
798  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
799
800def redundant_binop_in_equality : GICombineRule<
801  (defs root:$root, build_fn_matchinfo:$info),
802  (match (wip_match_opcode G_ICMP):$root,
803         [{ return Helper.matchRedundantBinOpInEquality(*${root}, ${info}); }]),
804  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
805
806def and_or_disjoint_mask : GICombineRule<
807  (defs root:$root, build_fn_matchinfo:$info),
808  (match (wip_match_opcode G_AND):$root,
809         [{ return Helper.matchAndOrDisjointMask(*${root}, ${info}); }]),
810  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${info}); }])>;
811
812def bitfield_extract_from_and : GICombineRule<
813  (defs root:$root, build_fn_matchinfo:$info),
814  (match (wip_match_opcode G_AND):$root,
815    [{ return Helper.matchBitfieldExtractFromAnd(*${root}, ${info}); }]),
816  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
817
818def funnel_shift_combines : GICombineGroup<[funnel_shift_from_or_shift,
819                                            funnel_shift_to_rotate]>;
820
821def bitfield_extract_from_sext_inreg : GICombineRule<
822  (defs root:$root, build_fn_matchinfo:$info),
823  (match (wip_match_opcode G_SEXT_INREG):$root,
824    [{ return Helper.matchBitfieldExtractFromSExtInReg(*${root}, ${info}); }]),
825  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
826
827def bitfield_extract_from_shr : GICombineRule<
828  (defs root:$root, build_fn_matchinfo:$info),
829  (match (wip_match_opcode G_ASHR, G_LSHR):$root,
830    [{ return Helper.matchBitfieldExtractFromShr(*${root}, ${info}); }]),
831  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
832
833def bitfield_extract_from_shr_and : GICombineRule<
834  (defs root:$root, build_fn_matchinfo:$info),
835  (match (wip_match_opcode G_ASHR, G_LSHR):$root,
836    [{ return Helper.matchBitfieldExtractFromShrAnd(*${root}, ${info}); }]),
837  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
838
839def form_bitfield_extract : GICombineGroup<[bitfield_extract_from_sext_inreg,
840                                            bitfield_extract_from_and,
841                                            bitfield_extract_from_shr,
842                                            bitfield_extract_from_shr_and]>;
843
844def udiv_by_const : GICombineRule<
845  (defs root:$root),
846  (match (wip_match_opcode G_UDIV):$root,
847   [{ return Helper.matchUDivByConst(*${root}); }]),
848  (apply [{ Helper.applyUDivByConst(*${root}); }])>;
849
850def sdiv_by_const : GICombineRule<
851  (defs root:$root),
852  (match (wip_match_opcode G_SDIV):$root,
853   [{ return Helper.matchSDivByConst(*${root}); }]),
854  (apply [{ Helper.applySDivByConst(*${root}); }])>;
855
856def intdiv_combines : GICombineGroup<[udiv_by_const, sdiv_by_const]>;
857
858def reassoc_ptradd : GICombineRule<
859  (defs root:$root, build_fn_matchinfo:$matchinfo),
860  (match (wip_match_opcode G_PTR_ADD):$root,
861    [{ return Helper.matchReassocPtrAdd(*${root}, ${matchinfo}); }]),
862  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
863
864def reassoc_comm_binops : GICombineRule<
865  (defs root:$root, build_fn_matchinfo:$matchinfo),
866  (match (G_ADD $root, $src1, $src2):$root,
867    [{ return Helper.matchReassocCommBinOp(*${root}, ${matchinfo}); }]),
868  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
869
870def reassocs : GICombineGroup<[reassoc_ptradd, reassoc_comm_binops]>;
871
872// Constant fold operations.
873def constant_fold : GICombineRule<
874  (defs root:$d, apint_matchinfo:$matchinfo),
875  (match (wip_match_opcode G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR):$d,
876   [{ return Helper.matchConstantFold(*${d}, ${matchinfo}); }]),
877  (apply [{ Helper.replaceInstWithConstant(*${d}, ${matchinfo}); }])>;
878
879def mulo_by_2: GICombineRule<
880  (defs root:$root, build_fn_matchinfo:$matchinfo),
881  (match (wip_match_opcode G_UMULO, G_SMULO):$root,
882         [{ return Helper.matchMulOBy2(*${root}, ${matchinfo}); }]),
883  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
884
885def mulo_by_0: GICombineRule<
886  (defs root:$root, build_fn_matchinfo:$matchinfo),
887  (match (wip_match_opcode G_UMULO, G_SMULO):$root,
888         [{ return Helper.matchMulOBy0(*${root}, ${matchinfo}); }]),
889  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
890
891def addo_by_0: GICombineRule<
892  (defs root:$root, build_fn_matchinfo:$matchinfo),
893  (match (wip_match_opcode G_UADDO, G_SADDO):$root,
894         [{ return Helper.matchAddOBy0(*${root}, ${matchinfo}); }]),
895  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
896
897// Transform (uadde x, y, 0) -> (uaddo x, y)
898//           (sadde x, y, 0) -> (saddo x, y)
899//           (usube x, y, 0) -> (usubo x, y)
900//           (ssube x, y, 0) -> (ssubo x, y)
901def adde_to_addo: GICombineRule<
902  (defs root:$root, build_fn_matchinfo:$matchinfo),
903  (match (wip_match_opcode G_UADDE, G_SADDE, G_USUBE, G_SSUBE):$root,
904         [{ return Helper.matchAddEToAddO(*${root}, ${matchinfo}); }]),
905  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
906
907def mulh_to_lshr : GICombineRule<
908  (defs root:$root),
909  (match (wip_match_opcode G_UMULH):$root,
910         [{ return Helper.matchUMulHToLShr(*${root}); }]),
911  (apply [{ Helper.applyUMulHToLShr(*${root}); }])>;
912
913def mulh_combines : GICombineGroup<[mulh_to_lshr]>;
914
915def redundant_neg_operands: GICombineRule<
916  (defs root:$root, build_fn_matchinfo:$matchinfo),
917  (match (wip_match_opcode G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FMAD, G_FMA):$root,
918    [{ return Helper.matchRedundantNegOperands(*${root}, ${matchinfo}); }]),
919  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
920
921// Transform (fsub +-0.0, X) -> (fneg X)
922def fsub_to_fneg: GICombineRule<
923  (defs root:$root, register_matchinfo:$matchinfo),
924  (match (wip_match_opcode G_FSUB):$root,
925    [{ return Helper.matchFsubToFneg(*${root}, ${matchinfo}); }]),
926  (apply [{ Helper.applyFsubToFneg(*${root}, ${matchinfo}); }])>;
927
928// Transform (fadd x, (fmul y, z)) -> (fma y, z, x)
929//           (fadd x, (fmul y, z)) -> (fmad y, z, x)
930// Transform (fadd (fmul x, y), z) -> (fma x, y, z)
931//           (fadd (fmul x, y), z) -> (fmad x, y, z)
932def combine_fadd_fmul_to_fmad_or_fma: GICombineRule<
933  (defs root:$root, build_fn_matchinfo:$info),
934  (match (wip_match_opcode G_FADD):$root,
935         [{ return Helper.matchCombineFAddFMulToFMadOrFMA(*${root},
936                                                          ${info}); }]),
937  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
938
939// Transform (fadd (fpext (fmul x, y)), z) -> (fma (fpext x), (fpext y), z)
940//                                         -> (fmad (fpext x), (fpext y), z)
941// Transform (fadd x, (fpext (fmul y, z))) -> (fma (fpext y), (fpext z), x)
942//                                         -> (fmad (fpext y), (fpext z), x)
943def combine_fadd_fpext_fmul_to_fmad_or_fma: GICombineRule<
944  (defs root:$root, build_fn_matchinfo:$info),
945  (match (wip_match_opcode G_FADD):$root,
946         [{ return Helper.matchCombineFAddFpExtFMulToFMadOrFMA(*${root},
947                                                               ${info}); }]),
948  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
949
950// Transform (fadd (fma x, y, (fmul z, u)), v)  -> (fma x, y, (fma z, u, v))
951//           (fadd (fmad x, y, (fmul z, u)), v) -> (fmad x, y, (fmad z, u, v))
952// Transform (fadd v, (fma x, y, (fmul z, u)))  -> (fma x, y, (fma z, u, v))
953//           (fadd v, (fmad x, y, (fmul z, u))) -> (fmad x, y, (fmad z, u, v))
954def combine_fadd_fma_fmul_to_fmad_or_fma: GICombineRule<
955  (defs root:$root, build_fn_matchinfo:$info),
956  (match (wip_match_opcode G_FADD):$root,
957         [{ return Helper.matchCombineFAddFMAFMulToFMadOrFMA(*${root},
958                                                             ${info}); }]),
959  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
960
961// Transform (fadd (fma x, y, (fpext (fmul u, v))), z) ->
962//           (fma x, y, (fma (fpext u), (fpext v), z))
963def combine_fadd_fpext_fma_fmul_to_fmad_or_fma: GICombineRule<
964  (defs root:$root, build_fn_matchinfo:$info),
965  (match (wip_match_opcode G_FADD):$root,
966         [{ return Helper.matchCombineFAddFpExtFMulToFMadOrFMAAggressive(
967                                                  *${root}, ${info}); }]),
968  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
969
970// Transform (fsub (fmul x, y), z) -> (fma x, y, -z)
971//                                 -> (fmad x, y, -z)
972def combine_fsub_fmul_to_fmad_or_fma: GICombineRule<
973  (defs root:$root, build_fn_matchinfo:$info),
974  (match (wip_match_opcode G_FSUB):$root,
975         [{ return Helper.matchCombineFSubFMulToFMadOrFMA(*${root},
976                                                          ${info}); }]),
977  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
978
979// Transform (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
980//           (fsub x, (fneg (fmul, y, z))) -> (fma y, z, x)
981def combine_fsub_fneg_fmul_to_fmad_or_fma: GICombineRule<
982  (defs root:$root, build_fn_matchinfo:$info),
983  (match (wip_match_opcode G_FSUB):$root,
984         [{ return Helper.matchCombineFSubFNegFMulToFMadOrFMA(*${root},
985                                                              ${info}); }]),
986  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
987
988// Transform (fsub (fpext (fmul x, y)), z) ->
989//           (fma (fpext x), (fpext y), (fneg z))
990def combine_fsub_fpext_fmul_to_fmad_or_fma: GICombineRule<
991  (defs root:$root, build_fn_matchinfo:$info),
992  (match (wip_match_opcode G_FSUB):$root,
993         [{ return Helper.matchCombineFSubFpExtFMulToFMadOrFMA(*${root},
994                                                               ${info}); }]),
995  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
996
997// Transform (fsub (fneg (fpext (fmul x, y))), z) ->
998//           (fneg (fma (fpext x), (fpext y), z))
999def combine_fsub_fpext_fneg_fmul_to_fmad_or_fma: GICombineRule<
1000  (defs root:$root, build_fn_matchinfo:$info),
1001  (match (wip_match_opcode G_FSUB):$root,
1002         [{ return Helper.matchCombineFSubFpExtFNegFMulToFMadOrFMA(
1003                                            *${root}, ${info}); }]),
1004  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1005
1006def combine_minmax_nan: GICombineRule<
1007  (defs root:$root, unsigned_matchinfo:$info),
1008  (match (wip_match_opcode G_FMINNUM, G_FMAXNUM, G_FMINIMUM, G_FMAXIMUM):$root,
1009         [{ return Helper.matchCombineFMinMaxNaN(*${root}, ${info}); }]),
1010  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, ${info}); }])>;
1011
1012// Transform (add x, (sub y, x)) -> y
1013// Transform (add (sub y, x), x) -> y
1014def add_sub_reg: GICombineRule <
1015  (defs root:$root, register_matchinfo:$matchinfo),
1016  (match (wip_match_opcode G_ADD):$root,
1017         [{ return Helper.matchAddSubSameReg(*${root}, ${matchinfo}); }]),
1018  (apply [{ Helper.replaceSingleDefInstWithReg(*${root},
1019                                                      ${matchinfo}); }])>;
1020
1021def buildvector_identity_fold : GICombineRule<
1022  (defs root:$build_vector, register_matchinfo:$matchinfo),
1023  (match (wip_match_opcode G_BUILD_VECTOR_TRUNC, G_BUILD_VECTOR):$build_vector,
1024         [{ return Helper.matchBuildVectorIdentityFold(*${build_vector}, ${matchinfo}); }]),
1025  (apply [{ Helper.replaceSingleDefInstWithReg(*${build_vector}, ${matchinfo}); }])>;
1026
1027def trunc_buildvector_fold : GICombineRule<
1028  (defs root:$op, register_matchinfo:$matchinfo),
1029  (match (wip_match_opcode G_TRUNC):$op,
1030      [{ return Helper.matchTruncBuildVectorFold(*${op}, ${matchinfo}); }]),
1031  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${matchinfo}); }])>;
1032
1033def trunc_lshr_buildvector_fold : GICombineRule<
1034  (defs root:$op, register_matchinfo:$matchinfo),
1035  (match (wip_match_opcode G_TRUNC):$op,
1036      [{ return Helper.matchTruncLshrBuildVectorFold(*${op}, ${matchinfo}); }]),
1037  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${matchinfo}); }])>;
1038
1039// Transform:
1040//   (x + y) - y -> x
1041//   (x + y) - x -> y
1042//   x - (y + x) -> 0 - y
1043//   x - (x + z) -> 0 - z
1044def sub_add_reg: GICombineRule <
1045  (defs root:$root, build_fn_matchinfo:$matchinfo),
1046  (match (wip_match_opcode G_SUB):$root,
1047         [{ return Helper.matchSubAddSameReg(*${root}, ${matchinfo}); }]),
1048  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1049
1050def bitcast_bitcast_fold : GICombineRule<
1051  (defs root:$dst),
1052  (match (G_BITCAST $dst, $src1):$op, (G_BITCAST $src1, $src0),
1053      [{ return MRI.getType(${src0}.getReg()) == MRI.getType(${dst}.getReg()); }]),
1054  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${src0}.getReg()); }])>;
1055
1056
1057def fptrunc_fpext_fold : GICombineRule<
1058  (defs root:$dst),
1059  (match (G_FPTRUNC $dst, $src1):$op, (G_FPEXT $src1, $src0),
1060      [{ return MRI.getType(${src0}.getReg()) == MRI.getType(${dst}.getReg()); }]),
1061  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${src0}.getReg()); }])>;
1062
1063
1064def select_to_minmax: GICombineRule<
1065  (defs root:$root, build_fn_matchinfo:$info),
1066  (match (wip_match_opcode G_SELECT):$root,
1067         [{ return Helper.matchSimplifySelectToMinMax(*${root}, ${info}); }]),
1068  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1069
1070// FIXME: These should use the custom predicate feature once it lands.
1071def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero,
1072                                     undef_to_negative_one,
1073                                     binop_left_undef_to_zero,
1074                                     binop_right_undef_to_undef,
1075                                     unary_undef_to_zero,
1076                                     propagate_undef_any_op,
1077                                     propagate_undef_all_ops,
1078                                     propagate_undef_shuffle_mask,
1079                                     erase_undef_store,
1080                                     unmerge_undef,
1081                                     insert_extract_vec_elt_out_of_bounds]>;
1082
1083def identity_combines : GICombineGroup<[select_same_val, right_identity_zero,
1084                                        binop_same_val, binop_left_to_zero,
1085                                        binop_right_to_zero, p2i_to_i2p,
1086                                        i2p_to_p2i, anyext_trunc_fold,
1087                                        fneg_fneg_fold, right_identity_one,
1088                                        add_sub_reg, buildvector_identity_fold,
1089                                        trunc_buildvector_fold,
1090                                        trunc_lshr_buildvector_fold,
1091                                        bitcast_bitcast_fold, fptrunc_fpext_fold]>;
1092
1093def const_combines : GICombineGroup<[constant_fold_fp_ops, const_ptradd_to_i2p,
1094                                     overlapping_and, mulo_by_2, mulo_by_0,
1095                                     addo_by_0, adde_to_addo,
1096                                     combine_minmax_nan]>;
1097
1098def known_bits_simplifications : GICombineGroup<[
1099  redundant_and, redundant_sext_inreg, redundant_or, urem_pow2_to_mask,
1100  zext_trunc_fold, icmp_to_true_false_known_bits, icmp_to_lhs_known_bits,
1101  sext_inreg_to_zext_inreg]>;
1102
1103def width_reduction_combines : GICombineGroup<[reduce_shl_of_extend,
1104                                               narrow_binop_feeding_and]>;
1105
1106def phi_combines : GICombineGroup<[extend_through_phis]>;
1107
1108def select_combines : GICombineGroup<[select_undef_cmp, select_constant_cmp,
1109                                      select_to_logical]>;
1110
1111def trivial_combines : GICombineGroup<[copy_prop, mul_to_shl, add_p2i_to_ptradd,
1112                                       mul_by_neg_one, idempotent_prop]>;
1113
1114def fma_combines : GICombineGroup<[combine_fadd_fmul_to_fmad_or_fma,
1115  combine_fadd_fpext_fmul_to_fmad_or_fma, combine_fadd_fma_fmul_to_fmad_or_fma,
1116  combine_fadd_fpext_fma_fmul_to_fmad_or_fma, combine_fsub_fmul_to_fmad_or_fma,
1117  combine_fsub_fneg_fmul_to_fmad_or_fma, combine_fsub_fpext_fmul_to_fmad_or_fma,
1118  combine_fsub_fpext_fneg_fmul_to_fmad_or_fma]>;
1119
1120def all_combines : GICombineGroup<[trivial_combines, insert_vec_elt_combines,
1121    extract_vec_elt_combines, combines_for_extload,
1122    combine_indexed_load_store, undef_combines, identity_combines, phi_combines,
1123    simplify_add_to_sub, hoist_logic_op_with_same_opcode_hands, shifts_too_big,
1124    reassocs, ptr_add_immed_chain,
1125    shl_ashr_to_sext_inreg, sext_inreg_of_load,
1126    width_reduction_combines, select_combines,
1127    known_bits_simplifications, ext_ext_fold,
1128    not_cmp_fold, opt_brcond_by_inverting_cond,
1129    unmerge_merge, unmerge_cst, unmerge_dead_to_trunc,
1130    unmerge_zext_to_zext, merge_unmerge, trunc_ext_fold, trunc_shift,
1131    const_combines, xor_of_and_with_same_reg, ptr_add_with_zero,
1132    shift_immed_chain, shift_of_shifted_logic_chain, load_or_combine,
1133    div_rem_to_divrem, funnel_shift_combines, commute_shift,
1134    form_bitfield_extract, constant_fold, fabs_fneg_fold,
1135    intdiv_combines, mulh_combines, redundant_neg_operands,
1136    and_or_disjoint_mask, fma_combines, fold_binop_into_select,
1137    sub_add_reg, select_to_minmax, redundant_binop_in_equality,
1138    fsub_to_fneg, commute_constant_to_rhs]>;
1139
1140// A combine group used to for prelegalizer combiners at -O0. The combines in
1141// this group have been selected based on experiments to balance code size and
1142// compile time performance.
1143def optnone_combines : GICombineGroup<[trivial_combines,
1144    ptr_add_immed_chain, combines_for_extload,
1145    not_cmp_fold, opt_brcond_by_inverting_cond]>;
1146