xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Target/GlobalISel/Combine.td (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
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
14//===----------------------------------------------------------------------===//
15// Base Classes
16//
17// These are the core classes that the combiner backend relies on.
18//===----------------------------------------------------------------------===//
19
20/// All arguments of the defs operator must be subclasses of GIDefKind or
21/// sub-dags whose operator is GIDefKindWithArgs.
22class GIDefKind;
23class GIDefKindWithArgs;
24
25/// Declare a root node. There must be at least one of these in every combine
26/// rule.
27def root : GIDefKind;
28
29def defs;
30
31def pattern;
32def match;
33def apply;
34
35def wip_match_opcode;
36
37// Common base class for GICombineRule and GICombineGroup.
38class GICombine {
39  // See GICombineGroup. We only declare it here to make the tablegen pass
40  // simpler.
41  list<GICombine> Rules = ?;
42}
43
44// A group of combine rules that can be added to a GICombiner or another group.
45class GICombineGroup<list<GICombine> rules> : GICombine {
46  // The rules contained in this group. The rules in a group are flattened into
47  // a single list and sorted into whatever order is most efficient. However,
48  // they will never be re-ordered such that behaviour differs from the
49  // specified order. It is therefore possible to use the order of rules in this
50  // list to describe priorities.
51  let Rules = rules;
52}
53
54// Declares a combiner implementation class
55class GICombiner<string classname, list<GICombine> rules>
56    : GICombineGroup<rules> {
57  // The class name to use in the generated output.
58  string Classname = classname;
59  // Combiners can use this so they're free to define tryCombineAll themselves
60  // and do extra work before/after calling the TableGen-erated code.
61  string CombineAllMethodName = "tryCombineAll";
62}
63
64/// Declares data that is passed from the match stage to the apply stage.
65class GIDefMatchData<string type>  {
66  /// A C++ type name indicating the storage type.
67  string Type = type;
68}
69
70class GICombineRule<dag defs, dag match, dag apply> : GICombine {
71  /// Defines the external interface of the match rule. This includes:
72  /// * The names of the root nodes (requires at least one)
73  /// See GIDefKind for details.
74  dag Defs = defs;
75
76  /// Defines the things which must be true for the pattern to match
77  dag Match = match;
78
79  /// Defines the things which happen after the decision is made to apply a
80  /// combine rule.
81  dag Apply = apply;
82
83  /// Defines the predicates that are checked before the match function
84  /// is called. Targets can use this to, for instance, check Subtarget
85  /// features.
86  list<Predicate> Predicates = [];
87
88  // Maximum number of permutations of this rule that can be emitted.
89  // Set to -1 to disable the limit.
90  int MaxPermutations = 16;
91}
92
93def gi_mo;
94def gi_imm;
95
96// This is an equivalent of PatFrags but for MIR Patterns.
97//
98// GICombinePatFrags can be used in place of instructions for 'match' patterns.
99// Much like normal instructions, the defs (outs) come first, and the ins second
100//
101// Out operands can only be of type "root" or "gi_mo", and they must be defined
102// by an instruction pattern in all alternatives.
103//
104// In operands can be gi_imm or gi_mo. They cannot be redefined in any alternative
105// pattern and may only appear in the C++ code, or in the output operand of an
106// instruction pattern.
107class GICombinePatFrag<dag outs, dag ins, list<dag> alts> {
108  dag InOperands = ins;
109  dag OutOperands = outs;
110  list<dag> Alternatives = alts;
111}
112
113//===----------------------------------------------------------------------===//
114// Pattern Special Types
115//===----------------------------------------------------------------------===//
116
117class GISpecialType;
118
119// In an apply pattern, GITypeOf can be used to set the type of a new temporary
120// register to match the type of a matched register.
121//
122// This can only be used on temporary registers defined by the apply pattern.
123//
124// TODO: Make this work in matchers as well?
125//
126// FIXME: Syntax is very ugly.
127class GITypeOf<string opName> : GISpecialType {
128  string OpName = opName;
129}
130
131//===----------------------------------------------------------------------===//
132// Pattern Builtins
133//===----------------------------------------------------------------------===//
134
135// "Magic" Builtin instructions for MIR patterns.
136// The definitions that implement
137class GIBuiltinInst;
138
139// Replace all references to a register with another one.
140//
141// Usage:
142//    (apply (GIReplaceReg $old, $new))
143//
144// Operands:
145// - $old (out) register defined by a matched instruction
146// - $new (in)  register
147//
148// Semantics:
149// - Can only appear in an 'apply' pattern.
150// - If both old/new are operands of matched instructions,
151//   "canReplaceReg" is checked before applying the rule.
152def GIReplaceReg : GIBuiltinInst;
153
154// Apply action that erases the match root.
155//
156// Usage:
157//    (apply (GIEraseRoot))
158//
159// Semantics:
160// - Can only appear as the only pattern of an 'apply' pattern list.
161// - The root cannot have any output operands.
162// - The root must be a CodeGenInstruction
163//
164// TODO: Allow using this directly, like (apply GIEraseRoot)
165def GIEraseRoot : GIBuiltinInst;
166
167//===----------------------------------------------------------------------===//
168// Pattern MIFlags
169//===----------------------------------------------------------------------===//
170
171class MIFlagEnum<string enumName> {
172  string EnumName = "MachineInstr::" # enumName;
173}
174
175def FmNoNans    : MIFlagEnum<"FmNoNans">;
176def FmNoInfs    : MIFlagEnum<"FmNoInfs">;
177def FmNsz       : MIFlagEnum<"FmNsz">;
178def FmArcp      : MIFlagEnum<"FmArcp">;
179def FmContract  : MIFlagEnum<"FmContract">;
180def FmAfn       : MIFlagEnum<"FmAfn">;
181def FmReassoc   : MIFlagEnum<"FmReassoc">;
182def IsExact     : MIFlagEnum<"IsExact">;
183def NoSWrap     : MIFlagEnum<"NoSWrap">;
184def NoUWrap     : MIFlagEnum<"NoUWrap">;
185def NonNeg      : MIFlagEnum<"NonNeg">;
186
187def MIFlags;
188// def not; -> Already defined as a SDNode
189
190//===----------------------------------------------------------------------===//
191
192def extending_load_matchdata : GIDefMatchData<"PreferredTuple">;
193def indexed_load_store_matchdata : GIDefMatchData<"IndexedLoadStoreMatchInfo">;
194def instruction_steps_matchdata: GIDefMatchData<"InstructionStepsMatchInfo">;
195
196def register_matchinfo: GIDefMatchData<"Register">;
197def int64_matchinfo: GIDefMatchData<"int64_t">;
198def apint_matchinfo : GIDefMatchData<"APInt">;
199def constantfp_matchinfo : GIDefMatchData<"ConstantFP*">;
200def build_fn_matchinfo :
201GIDefMatchData<"std::function<void(MachineIRBuilder &)>">;
202def unsigned_matchinfo: GIDefMatchData<"unsigned">;
203
204def copy_prop : GICombineRule<
205  (defs root:$d),
206  (match (COPY $d, $s):$mi,
207         [{ return Helper.matchCombineCopy(*${mi}); }]),
208  (apply [{ Helper.applyCombineCopy(*${mi}); }])>;
209
210// idempotent operations
211// Fold (freeze (freeze x)) -> (freeze x).
212// Fold (fabs (fabs x)) -> (fabs x).
213// Fold (fcanonicalize (fcanonicalize x)) -> (fcanonicalize x).
214def idempotent_prop_frags : GICombinePatFrag<
215  (outs root:$dst, $src), (ins),
216  !foreach(op, [G_FREEZE, G_FABS, G_FCANONICALIZE],
217           (pattern (op $dst, $src), (op $src, $x)))>;
218
219def idempotent_prop : GICombineRule<
220   (defs root:$dst),
221   (match (idempotent_prop_frags $dst, $src)),
222   (apply (GIReplaceReg $dst, $src))>;
223
224// Convert freeze(Op(Op0, NonPoisonOps...)) to Op(freeze(Op0), NonPoisonOps...)
225// when Op0 is not guaranteed non-poison
226def push_freeze_to_prevent_poison_from_propagating : GICombineRule<
227  (defs root:$root, build_fn_matchinfo:$matchinfo),
228  (match (G_FREEZE $dst, $src):$root,
229         [{ return !isGuaranteedNotToBePoison(${src}.getReg(), MRI) && Helper.matchFreezeOfSingleMaybePoisonOperand(*${root}, ${matchinfo}); }]),
230  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
231
232def extending_loads : GICombineRule<
233  (defs root:$root, extending_load_matchdata:$matchinfo),
234  (match (wip_match_opcode G_LOAD, G_SEXTLOAD, G_ZEXTLOAD):$root,
235         [{ return Helper.matchCombineExtendingLoads(*${root}, ${matchinfo}); }]),
236  (apply [{ Helper.applyCombineExtendingLoads(*${root}, ${matchinfo}); }])>;
237
238def load_and_mask : GICombineRule<
239  (defs root:$root, build_fn_matchinfo:$matchinfo),
240  (match (wip_match_opcode G_AND):$root,
241        [{ return Helper.matchCombineLoadWithAndMask(*${root}, ${matchinfo}); }]),
242  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
243def combines_for_extload: GICombineGroup<[extending_loads, load_and_mask]>;
244
245def sext_trunc_sextload : GICombineRule<
246  (defs root:$d),
247  (match (wip_match_opcode G_SEXT_INREG):$d,
248         [{ return Helper.matchSextTruncSextLoad(*${d}); }]),
249  (apply [{ Helper.applySextTruncSextLoad(*${d}); }])>;
250
251def sext_inreg_of_load_matchdata : GIDefMatchData<"std::tuple<Register, unsigned>">;
252def sext_inreg_of_load : GICombineRule<
253  (defs root:$root, sext_inreg_of_load_matchdata:$matchinfo),
254  (match (wip_match_opcode G_SEXT_INREG):$root,
255         [{ return Helper.matchSextInRegOfLoad(*${root}, ${matchinfo}); }]),
256  (apply [{ Helper.applySextInRegOfLoad(*${root}, ${matchinfo}); }])>;
257
258def sext_inreg_to_zext_inreg : GICombineRule<
259  (defs root:$dst),
260  (match
261    (G_SEXT_INREG $dst, $src, $imm):$root,
262      [{
263        unsigned BitWidth = MRI.getType(${src}.getReg()).getScalarSizeInBits();
264        return Helper.getKnownBits()->maskedValueIsZero(${src}.getReg(),
265                 APInt::getOneBitSet(BitWidth, ${imm}.getImm() - 1)); }]),
266    (apply [{
267      Helper.getBuilder().setInstrAndDebugLoc(*${root});
268      Helper.getBuilder().buildZExtInReg(${dst}, ${src}, ${imm}.getImm());
269      ${root}->eraseFromParent();
270  }])
271>;
272
273def combine_extracted_vector_load : GICombineRule<
274  (defs root:$root, build_fn_matchinfo:$matchinfo),
275  (match (wip_match_opcode G_EXTRACT_VECTOR_ELT):$root,
276        [{ return Helper.matchCombineExtractedVectorLoad(*${root}, ${matchinfo}); }]),
277  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
278
279def combine_indexed_load_store : GICombineRule<
280  (defs root:$root, indexed_load_store_matchdata:$matchinfo),
281  (match (wip_match_opcode G_LOAD, G_SEXTLOAD, G_ZEXTLOAD, G_STORE):$root,
282         [{ return Helper.matchCombineIndexedLoadStore(*${root}, ${matchinfo}); }]),
283  (apply [{ Helper.applyCombineIndexedLoadStore(*${root}, ${matchinfo}); }])>;
284
285def opt_brcond_by_inverting_cond_matchdata : GIDefMatchData<"MachineInstr *">;
286def opt_brcond_by_inverting_cond : GICombineRule<
287  (defs root:$root, opt_brcond_by_inverting_cond_matchdata:$matchinfo),
288  (match (wip_match_opcode G_BR):$root,
289         [{ return Helper.matchOptBrCondByInvertingCond(*${root}, ${matchinfo}); }]),
290  (apply [{ Helper.applyOptBrCondByInvertingCond(*${root}, ${matchinfo}); }])>;
291
292def ptr_add_immed_matchdata : GIDefMatchData<"PtrAddChain">;
293def ptr_add_immed_chain : GICombineRule<
294  (defs root:$d, ptr_add_immed_matchdata:$matchinfo),
295  (match (wip_match_opcode G_PTR_ADD):$d,
296         [{ return Helper.matchPtrAddImmedChain(*${d}, ${matchinfo}); }]),
297  (apply [{ Helper.applyPtrAddImmedChain(*${d}, ${matchinfo}); }])>;
298
299def shifts_too_big : GICombineRule<
300  (defs root:$root),
301  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR):$root,
302         [{ return Helper.matchShiftsTooBig(*${root}); }]),
303  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
304
305// Fold shift (shift base x), y -> shift base, (x+y), if shifts are same
306def shift_immed_matchdata : GIDefMatchData<"RegisterImmPair">;
307def shift_immed_chain : GICombineRule<
308  (defs root:$d, shift_immed_matchdata:$matchinfo),
309  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR, G_SSHLSAT, G_USHLSAT):$d,
310         [{ return Helper.matchShiftImmedChain(*${d}, ${matchinfo}); }]),
311  (apply [{ Helper.applyShiftImmedChain(*${d}, ${matchinfo}); }])>;
312
313// Transform shift (logic (shift X, C0), Y), C1
314//        -> logic (shift X, (C0+C1)), (shift Y, C1), if shifts are same
315def shift_of_shifted_logic_matchdata : GIDefMatchData<"ShiftOfShiftedLogic">;
316def shift_of_shifted_logic_chain : GICombineRule<
317  (defs root:$d, shift_of_shifted_logic_matchdata:$matchinfo),
318  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR, G_USHLSAT, G_SSHLSAT):$d,
319         [{ return Helper.matchShiftOfShiftedLogic(*${d}, ${matchinfo}); }]),
320  (apply [{ Helper.applyShiftOfShiftedLogic(*${d}, ${matchinfo}); }])>;
321
322def mul_to_shl : GICombineRule<
323  (defs root:$d, unsigned_matchinfo:$matchinfo),
324  (match (G_MUL $d, $op1, $op2):$mi,
325         [{ return Helper.matchCombineMulToShl(*${mi}, ${matchinfo}); }]),
326  (apply [{ Helper.applyCombineMulToShl(*${mi}, ${matchinfo}); }])>;
327
328// shl ([asz]ext x), y => zext (shl x, y), if shift does not overflow int
329def reduce_shl_of_extend_matchdata : GIDefMatchData<"RegisterImmPair">;
330def reduce_shl_of_extend : GICombineRule<
331  (defs root:$dst, reduce_shl_of_extend_matchdata:$matchinfo),
332  (match (G_SHL $dst, $src0, $src1):$mi,
333         [{ return Helper.matchCombineShlOfExtend(*${mi}, ${matchinfo}); }]),
334  (apply [{ Helper.applyCombineShlOfExtend(*${mi}, ${matchinfo}); }])>;
335
336// Combine bitreverse(shl (bitreverse x), y)) -> (lshr x, y)
337def bitreverse_shl : GICombineRule<
338  (defs root:$d),
339  (match (G_BITREVERSE $rev, $val),
340         (G_SHL $src, $rev, $amt):$mi,
341         (G_BITREVERSE $d, $src),
342         [{ return Helper.isLegalOrBeforeLegalizer({TargetOpcode::G_LSHR,
343                                                   {MRI.getType(${val}.getReg()),
344                                                    MRI.getType(${amt}.getReg())}}); }]),
345  (apply (G_LSHR $d, $val, $amt))>;
346
347// Combine bitreverse(lshr (bitreverse x), y)) -> (shl x, y)
348def bitreverse_lshr : GICombineRule<
349  (defs root:$d),
350  (match (G_BITREVERSE $rev, $val),
351         (G_LSHR $src, $rev, $amt):$mi,
352         (G_BITREVERSE $d, $src),
353         [{ return Helper.isLegalOrBeforeLegalizer({TargetOpcode::G_SHL,
354                                                   {MRI.getType(${val}.getReg()),
355                                                    MRI.getType(${amt}.getReg())}}); }]),
356  (apply (G_SHL $d, $val, $amt))>;
357
358// Combine (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
359// Combine (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
360def commute_shift : GICombineRule<
361  (defs root:$d, build_fn_matchinfo:$matchinfo),
362  (match (wip_match_opcode G_SHL):$d,
363         [{ return Helper.matchCommuteShift(*${d}, ${matchinfo}); }]),
364  (apply [{ Helper.applyBuildFn(*${d}, ${matchinfo}); }])>;
365
366def narrow_binop_feeding_and : GICombineRule<
367  (defs root:$root, build_fn_matchinfo:$matchinfo),
368  (match (wip_match_opcode G_AND):$root,
369         [{ return Helper.matchNarrowBinopFeedingAnd(*${root}, ${matchinfo}); }]),
370  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
371
372// [us]itofp(undef) = 0, because the result value is bounded.
373def undef_to_fp_zero : GICombineRule<
374  (defs root:$root),
375  (match (wip_match_opcode G_UITOFP, G_SITOFP):$root,
376         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
377  (apply [{ Helper.replaceInstWithFConstant(*${root}, 0.0); }])>;
378
379def undef_to_int_zero: GICombineRule<
380  (defs root:$root),
381  (match (wip_match_opcode G_AND, G_MUL):$root,
382         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
383  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
384
385def undef_to_negative_one: GICombineRule<
386  (defs root:$root),
387  (match (wip_match_opcode G_OR):$root,
388         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
389  (apply [{ Helper.replaceInstWithConstant(*${root}, -1); }])>;
390
391def binop_left_undef_to_zero: GICombineRule<
392  (defs root:$root),
393  (match (wip_match_opcode G_SHL, G_UDIV, G_UREM):$root,
394         [{ return Helper.matchOperandIsUndef(*${root}, 1); }]),
395  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
396
397def binop_right_undef_to_undef: GICombineRule<
398  (defs root:$root),
399  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR):$root,
400         [{ return Helper.matchOperandIsUndef(*${root}, 2); }]),
401  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
402
403def unary_undef_to_zero: GICombineRule<
404  (defs root:$root),
405  (match (wip_match_opcode G_ABS):$root,
406         [{ return Helper.matchOperandIsUndef(*${root}, 1); }]),
407  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
408
409// Instructions where if any source operand is undef, the instruction can be
410// replaced with undef.
411def propagate_undef_any_op: GICombineRule<
412  (defs root:$root),
413  (match (wip_match_opcode G_ADD, G_FPTOSI, G_FPTOUI, G_SUB, G_XOR, G_TRUNC):$root,
414         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
415  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
416
417// Instructions where if all source operands are undef, the instruction can be
418// replaced with undef.
419def propagate_undef_all_ops: GICombineRule<
420  (defs root:$root),
421  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
422          [{ return Helper.matchAllExplicitUsesAreUndef(*${root}); }]),
423  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
424
425// Replace a G_SHUFFLE_VECTOR with an undef mask with a G_IMPLICIT_DEF.
426def propagate_undef_shuffle_mask: GICombineRule<
427  (defs root:$root),
428  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
429         [{ return Helper.matchUndefShuffleVectorMask(*${root}); }]),
430  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
431
432// Replace a G_SHUFFLE_VECTOR with a G_EXTRACT_VECTOR_ELT.
433def shuffle_to_extract: GICombineRule<
434  (defs root:$root),
435  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
436         [{ return Helper.matchShuffleToExtract(*${root}); }]),
437  (apply [{ Helper.applyShuffleToExtract(*${root}); }])>;
438
439  // Replace an insert/extract element of an out of bounds index with undef.
440  def insert_extract_vec_elt_out_of_bounds : GICombineRule<
441  (defs root:$root),
442  (match (wip_match_opcode G_INSERT_VECTOR_ELT, G_EXTRACT_VECTOR_ELT):$root,
443         [{ return Helper.matchInsertExtractVecEltOutOfBounds(*${root}); }]),
444  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
445
446// Fold (cond ? x : x) -> x
447def select_same_val: GICombineRule<
448  (defs root:$root),
449  (match (wip_match_opcode G_SELECT):$root,
450    [{ return Helper.matchSelectSameVal(*${root}); }]),
451  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 2); }])
452>;
453
454// Fold (undef ? x : y) -> y
455def select_undef_cmp: GICombineRule<
456  (defs root:$dst),
457  (match (G_IMPLICIT_DEF $undef),
458         (G_SELECT $dst, $undef, $x, $y)),
459  (apply (GIReplaceReg $dst, $y))
460>;
461
462// Fold (true ? x : y) -> x
463// Fold (false ? x : y) -> y
464def select_constant_cmp: GICombineRule<
465  (defs root:$root, unsigned_matchinfo:$matchinfo),
466  (match (wip_match_opcode G_SELECT):$root,
467    [{ return Helper.matchConstantSelectCmp(*${root}, ${matchinfo}); }]),
468  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, ${matchinfo}); }])
469>;
470
471// Fold (C op x) -> (x op C)
472// TODO: handle more isCommutable opcodes
473// TODO: handle compares (currently not marked as isCommutable)
474def commute_int_constant_to_rhs : GICombineRule<
475  (defs root:$root),
476  (match (wip_match_opcode G_ADD, G_MUL, G_AND, G_OR, G_XOR,
477                           G_SMIN, G_SMAX, G_UMIN, G_UMAX, G_UADDO, G_SADDO,
478                           G_UMULO, G_SMULO, G_UMULH, G_SMULH,
479                           G_UADDSAT, G_SADDSAT, G_SMULFIX, G_UMULFIX,
480                           G_SMULFIXSAT, G_UMULFIXSAT):$root,
481    [{ return Helper.matchCommuteConstantToRHS(*${root}); }]),
482  (apply [{ Helper.applyCommuteBinOpOperands(*${root}); }])
483>;
484
485def commute_fp_constant_to_rhs : GICombineRule<
486  (defs root:$root),
487  (match (wip_match_opcode G_FADD, G_FMUL, G_FMINNUM, G_FMAXNUM,
488                           G_FMINNUM_IEEE, G_FMAXNUM_IEEE,
489                           G_FMINIMUM, G_FMAXIMUM):$root,
490    [{ return Helper.matchCommuteFPConstantToRHS(*${root}); }]),
491  (apply [{ Helper.applyCommuteBinOpOperands(*${root}); }])
492>;
493
494def commute_constant_to_rhs : GICombineGroup<[
495  commute_int_constant_to_rhs,
496  commute_fp_constant_to_rhs
497]>;
498
499// Fold x op 0 -> x
500def right_identity_zero_frags : GICombinePatFrag<
501  (outs root:$dst), (ins $x),
502  !foreach(op,
503           [G_SUB, G_ADD, G_OR, G_XOR, G_SHL, G_ASHR,
504            G_LSHR, G_PTR_ADD, G_ROTL, G_ROTR],
505           (pattern (op $dst, $x, 0)))>;
506def right_identity_zero: GICombineRule<
507  (defs root:$dst),
508  (match (right_identity_zero_frags $dst, $lhs)),
509  (apply (GIReplaceReg $dst, $lhs))
510>;
511
512def right_identity_neg_zero_fp: GICombineRule<
513  (defs root:$dst),
514  (match (G_FADD $dst, $x, $y):$root,
515    [{ return Helper.matchConstantFPOp(${y}, -0.0); }]),
516  (apply (GIReplaceReg $dst, $x))
517>;
518
519// Fold x op 1 -> x
520def right_identity_one_int: GICombineRule<
521  (defs root:$dst),
522  (match (G_MUL $dst, $x, 1)),
523  (apply (GIReplaceReg $dst, $x))
524>;
525
526def right_identity_one_fp: GICombineRule<
527  (defs root:$dst),
528  (match (G_FMUL $dst, $x, $y):$root,
529    [{ return Helper.matchConstantFPOp(${y}, 1.0); }]),
530  (apply (GIReplaceReg $dst, $x))
531>;
532
533def right_identity_neg_one_fp: GICombineRule<
534  (defs root:$dst),
535  (match (G_FMUL $dst, $x, $y):$root,
536    [{ return Helper.matchConstantFPOp(${y}, -1.0); }]),
537  (apply (G_FNEG $dst, $x))
538>;
539
540def right_identity_one : GICombineGroup<[right_identity_one_int, right_identity_one_fp]>;
541
542// Fold (x op x) - > x
543def binop_same_val_frags : GICombinePatFrag<
544  (outs root:$dst), (ins $x),
545  [
546    (pattern (G_AND $dst, $x, $x)),
547    (pattern (G_OR $dst, $x, $x)),
548  ]
549>;
550def binop_same_val: GICombineRule<
551  (defs root:$dst),
552  (match (binop_same_val_frags $dst, $src)),
553  (apply (GIReplaceReg $dst, $src))
554>;
555
556// Fold (0 op x) - > 0
557def binop_left_to_zero: GICombineRule<
558  (defs root:$root),
559  (match (wip_match_opcode G_SDIV, G_UDIV, G_SREM, G_UREM):$root,
560    [{ return Helper.matchOperandIsZero(*${root}, 1); }]),
561  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
562>;
563
564def urem_pow2_to_mask : GICombineRule<
565  (defs root:$root),
566  (match (wip_match_opcode G_UREM):$root,
567    [{ return Helper.matchOperandIsKnownToBeAPowerOfTwo(*${root}, 2); }]),
568  (apply [{ Helper.applySimplifyURemByPow2(*${root}); }])
569>;
570
571// Push a binary operator through a select on constants.
572//
573// binop (select cond, K0, K1), K2 ->
574//   select cond, (binop K0, K2), (binop K1, K2)
575
576// Every binary operator that has constant folding. We currently do
577// not have constant folding for G_FPOW, G_FMAXNUM_IEEE or
578// G_FMINNUM_IEEE.
579def fold_binop_into_select : GICombineRule<
580  (defs root:$root, unsigned_matchinfo:$select_op_no),
581  (match (wip_match_opcode
582    G_ADD, G_SUB, G_PTR_ADD, G_AND, G_OR, G_XOR,
583    G_SDIV, G_SREM, G_UDIV, G_UREM, G_LSHR, G_ASHR, G_SHL,
584    G_SMIN, G_SMAX, G_UMIN, G_UMAX,
585    G_FMUL, G_FADD, G_FSUB, G_FDIV, G_FREM,
586    G_FMINNUM, G_FMAXNUM, G_FMINIMUM, G_FMAXIMUM):$root,
587    [{ return Helper.matchFoldBinOpIntoSelect(*${root}, ${select_op_no}); }]),
588  (apply [{ Helper.applyFoldBinOpIntoSelect(*${root}, ${select_op_no}); }])
589>;
590
591// Transform d = [su]div(x, y) and r = [su]rem(x, y) - > d, r = [su]divrem(x, y)
592def div_rem_to_divrem_matchdata : GIDefMatchData<"MachineInstr *">;
593def div_rem_to_divrem : GICombineRule<
594  (defs root:$root, div_rem_to_divrem_matchdata:$matchinfo),
595  (match (wip_match_opcode G_SDIV, G_UDIV, G_SREM, G_UREM):$root,
596    [{ return Helper.matchCombineDivRem(*${root}, ${matchinfo}); }]),
597  (apply [{ Helper.applyCombineDivRem(*${root}, ${matchinfo}); }])
598>;
599
600// Fold (x op 0) - > 0
601def binop_right_to_zero: GICombineRule<
602  (defs root:$dst),
603  (match (G_MUL $dst, $lhs, 0:$zero)),
604  (apply (GIReplaceReg $dst, $zero))
605>;
606
607// Erase stores of undef values.
608def erase_undef_store : GICombineRule<
609  (defs root:$root),
610  (match (wip_match_opcode G_STORE):$root,
611    [{ return Helper.matchUndefStore(*${root}); }]),
612  (apply [{ Helper.eraseInst(*${root}); }])
613>;
614
615def simplify_add_to_sub_matchinfo: GIDefMatchData<"std::tuple<Register, Register>">;
616def simplify_add_to_sub: GICombineRule <
617  (defs root:$root, simplify_add_to_sub_matchinfo:$info),
618  (match (wip_match_opcode G_ADD):$root,
619    [{ return Helper.matchSimplifyAddToSub(*${root}, ${info}); }]),
620  (apply [{ Helper.applySimplifyAddToSub(*${root}, ${info});}])
621>;
622
623// Fold fp_op(cst) to the constant result of the floating point operation.
624class constant_fold_unary_fp_op_rule<Instruction opcode> : GICombineRule <
625  (defs root:$dst),
626  (match (opcode $dst, $src0):$root, (G_FCONSTANT $src0, $cst)),
627  (apply [{ Helper.applyCombineConstantFoldFpUnary(*${root}, ${cst}.getFPImm()); }])
628>;
629
630def constant_fold_fneg : constant_fold_unary_fp_op_rule<G_FNEG>;
631def constant_fold_fabs : constant_fold_unary_fp_op_rule<G_FABS>;
632def constant_fold_fsqrt : constant_fold_unary_fp_op_rule<G_FSQRT>;
633def constant_fold_flog2 : constant_fold_unary_fp_op_rule<G_FLOG2>;
634def constant_fold_fptrunc : constant_fold_unary_fp_op_rule<G_FPTRUNC>;
635
636// Fold constant zero int to fp conversions.
637class itof_const_zero_fold_rule<Instruction opcode> : GICombineRule <
638  (defs root:$dst),
639  (match (opcode $dst, 0)),
640  // Can't use COPY $dst, 0 here because the 0 operand may be a smaller type
641  // than the destination for itofp.
642  (apply [{ Helper.replaceInstWithFConstant(*${dst}.getParent(), 0.0); }])
643>;
644def itof_const_zero_fold_si : itof_const_zero_fold_rule<G_SITOFP>;
645def itof_const_zero_fold_ui : itof_const_zero_fold_rule<G_UITOFP>;
646
647def constant_fold_fp_ops : GICombineGroup<[
648  constant_fold_fneg,
649  constant_fold_fabs,
650  constant_fold_fsqrt,
651  constant_fold_flog2,
652  constant_fold_fptrunc,
653  itof_const_zero_fold_si,
654  itof_const_zero_fold_ui
655]>;
656
657// Fold int2ptr(ptr2int(x)) -> x
658def p2i_to_i2p: GICombineRule<
659  (defs root:$root, register_matchinfo:$info),
660  (match (wip_match_opcode G_INTTOPTR):$root,
661    [{ return Helper.matchCombineI2PToP2I(*${root}, ${info}); }]),
662  (apply [{ Helper.applyCombineI2PToP2I(*${root}, ${info}); }])
663>;
664
665// Fold ptr2int(int2ptr(x)) -> x
666def i2p_to_p2i: GICombineRule<
667  (defs root:$dst, register_matchinfo:$info),
668  (match (G_INTTOPTR $t, $ptr),
669         (G_PTRTOINT $dst, $t):$mi,
670    [{ ${info} = ${ptr}.getReg(); return true; }]),
671  (apply [{ Helper.applyCombineP2IToI2P(*${mi}, ${info}); }])
672>;
673
674// Fold add ptrtoint(x), y -> ptrtoint (ptr_add x), y
675def add_p2i_to_ptradd_matchinfo : GIDefMatchData<"std::pair<Register, bool>">;
676def add_p2i_to_ptradd : GICombineRule<
677  (defs root:$root, add_p2i_to_ptradd_matchinfo:$info),
678  (match (wip_match_opcode G_ADD):$root,
679    [{ return Helper.matchCombineAddP2IToPtrAdd(*${root}, ${info}); }]),
680  (apply [{ Helper.applyCombineAddP2IToPtrAdd(*${root}, ${info}); }])
681>;
682
683// Fold (ptr_add (int2ptr C1), C2) -> C1 + C2
684def const_ptradd_to_i2p: GICombineRule<
685  (defs root:$root, apint_matchinfo:$info),
686  (match (wip_match_opcode G_PTR_ADD):$root,
687    [{ return Helper.matchCombineConstPtrAddToI2P(*${root}, ${info}); }]),
688  (apply [{ Helper.applyCombineConstPtrAddToI2P(*${root}, ${info}); }])
689>;
690
691// Simplify: (logic_op (op x...), (op y...)) -> (op (logic_op x, y))
692def hoist_logic_op_with_same_opcode_hands: GICombineRule <
693  (defs root:$root, instruction_steps_matchdata:$info),
694  (match (wip_match_opcode G_AND, G_OR, G_XOR):$root,
695    [{ return Helper.matchHoistLogicOpWithSameOpcodeHands(*${root}, ${info}); }]),
696  (apply [{ Helper.applyBuildInstructionSteps(*${root}, ${info});}])
697>;
698
699// Fold ashr (shl x, C), C -> sext_inreg (C)
700def shl_ashr_to_sext_inreg_matchinfo : GIDefMatchData<"std::tuple<Register, int64_t>">;
701def shl_ashr_to_sext_inreg : GICombineRule<
702  (defs root:$root, shl_ashr_to_sext_inreg_matchinfo:$info),
703  (match (wip_match_opcode G_ASHR): $root,
704    [{ return Helper.matchAshrShlToSextInreg(*${root}, ${info}); }]),
705  (apply [{ Helper.applyAshShlToSextInreg(*${root}, ${info});}])
706>;
707
708// Fold and(and(x, C1), C2) -> C1&C2 ? and(x, C1&C2) : 0
709def overlapping_and: GICombineRule <
710  (defs root:$root, build_fn_matchinfo:$info),
711  (match (wip_match_opcode G_AND):$root,
712         [{ return Helper.matchOverlappingAnd(*${root}, ${info}); }]),
713  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])
714>;
715
716// Fold (x & y) -> x or (x & y) -> y when (x & y) is known to equal x or equal y.
717def redundant_and: GICombineRule <
718  (defs root:$root, register_matchinfo:$matchinfo),
719  (match (wip_match_opcode G_AND):$root,
720         [{ return Helper.matchRedundantAnd(*${root}, ${matchinfo}); }]),
721  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
722>;
723
724// Fold (x | y) -> x or (x | y) -> y when (x | y) is known to equal x or equal y.
725def redundant_or: GICombineRule <
726  (defs root:$root, register_matchinfo:$matchinfo),
727  (match (wip_match_opcode G_OR):$root,
728         [{ return Helper.matchRedundantOr(*${root}, ${matchinfo}); }]),
729  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
730>;
731
732// If the input is already sign extended, just drop the extension.
733// sext_inreg x, K ->
734//   if computeNumSignBits(x) >= (x.getScalarSizeInBits() - K + 1)
735def redundant_sext_inreg: GICombineRule <
736  (defs root:$root),
737  (match (wip_match_opcode G_SEXT_INREG):$root,
738         [{ return Helper.matchRedundantSExtInReg(*${root}); }]),
739     (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
740>;
741
742// Fold (anyext (trunc x)) -> x if the source type is same as
743// the destination type.
744def anyext_trunc_fold: GICombineRule <
745  (defs root:$root, register_matchinfo:$matchinfo),
746  (match (wip_match_opcode G_ANYEXT):$root,
747         [{ return Helper.matchCombineAnyExtTrunc(*${root}, ${matchinfo}); }]),
748  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
749>;
750
751// Fold (zext (trunc x)) -> x if the source type is same as the destination type
752// and truncated bits are known to be zero.
753def zext_trunc_fold: GICombineRule <
754  (defs root:$root, register_matchinfo:$matchinfo),
755  (match (wip_match_opcode G_ZEXT):$root,
756         [{ return Helper.matchCombineZextTrunc(*${root}, ${matchinfo}); }]),
757  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
758>;
759
760// Fold ([asz]ext ([asz]ext x)) -> ([asz]ext x).
761def ext_ext_fold_matchinfo : GIDefMatchData<"std::tuple<Register, unsigned>">;
762def ext_ext_fold: GICombineRule <
763  (defs root:$root, ext_ext_fold_matchinfo:$matchinfo),
764  (match (wip_match_opcode G_ANYEXT, G_SEXT, G_ZEXT):$root,
765         [{ return Helper.matchCombineExtOfExt(*${root}, ${matchinfo}); }]),
766  (apply [{ Helper.applyCombineExtOfExt(*${root}, ${matchinfo}); }])
767>;
768
769def not_cmp_fold_matchinfo : GIDefMatchData<"SmallVector<Register, 4>">;
770def not_cmp_fold : GICombineRule<
771  (defs root:$d, not_cmp_fold_matchinfo:$info),
772  (match (wip_match_opcode G_XOR): $d,
773  [{ return Helper.matchNotCmp(*${d}, ${info}); }]),
774  (apply [{ Helper.applyNotCmp(*${d}, ${info}); }])
775>;
776
777// Fold (fneg (fneg x)) -> x.
778def fneg_fneg_fold: GICombineRule <
779  (defs root:$dst),
780  (match (G_FNEG $t, $src),
781         (G_FNEG $dst, $t)),
782  (apply (GIReplaceReg $dst, $src))
783>;
784
785// Fold (unmerge(merge x, y, z)) -> z, y, z.
786def unmerge_merge_matchinfo : GIDefMatchData<"SmallVector<Register, 8>">;
787def unmerge_merge : GICombineRule<
788  (defs root:$d, unmerge_merge_matchinfo:$info),
789  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
790  [{ return Helper.matchCombineUnmergeMergeToPlainValues(*${d}, ${info}); }]),
791  (apply [{ Helper.applyCombineUnmergeMergeToPlainValues(*${d}, ${info}); }])
792>;
793
794// Fold merge(unmerge).
795def merge_unmerge : GICombineRule<
796  (defs root:$d, register_matchinfo:$matchinfo),
797  (match (wip_match_opcode G_MERGE_VALUES):$d,
798  [{ return Helper.matchCombineMergeUnmerge(*${d}, ${matchinfo}); }]),
799  (apply [{ Helper.replaceSingleDefInstWithReg(*${d}, ${matchinfo}); }])
800>;
801
802// Fold (fabs (fneg x)) -> (fabs x).
803def fabs_fneg_fold: GICombineRule <
804  (defs root:$dst),
805  (match  (G_FNEG $tmp, $x),
806          (G_FABS $dst, $tmp)),
807  (apply (G_FABS $dst, $x))>;
808
809// Fold (unmerge cst) -> cst1, cst2, ...
810def unmerge_cst_matchinfo : GIDefMatchData<"SmallVector<APInt, 8>">;
811def unmerge_cst : GICombineRule<
812  (defs root:$d, unmerge_cst_matchinfo:$info),
813  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
814  [{ return Helper.matchCombineUnmergeConstant(*${d}, ${info}); }]),
815  (apply [{ Helper.applyCombineUnmergeConstant(*${d}, ${info}); }])
816>;
817
818// Fold (unmerge undef) -> undef, undef, ...
819def unmerge_undef : GICombineRule<
820  (defs root:$root, build_fn_matchinfo:$info),
821  (match (wip_match_opcode G_UNMERGE_VALUES): $root,
822         [{ return Helper.matchCombineUnmergeUndef(*${root}, ${info}); }]),
823  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])
824>;
825
826// Transform x,y<dead> = unmerge z -> x = trunc z.
827def unmerge_dead_to_trunc : GICombineRule<
828  (defs root:$d),
829  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
830  [{ return Helper.matchCombineUnmergeWithDeadLanesToTrunc(*${d}); }]),
831  (apply [{ Helper.applyCombineUnmergeWithDeadLanesToTrunc(*${d}); }])
832>;
833
834// Transform x,y = unmerge(zext(z)) -> x = zext z; y = 0.
835def unmerge_zext_to_zext : GICombineRule<
836  (defs root:$d),
837  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
838  [{ return Helper.matchCombineUnmergeZExtToZExt(*${d}); }]),
839  (apply [{ Helper.applyCombineUnmergeZExtToZExt(*${d}); }])
840>;
841
842// Fold trunc ([asz]ext x) -> x or ([asz]ext x) or (trunc x).
843def trunc_ext_fold_matchinfo : GIDefMatchData<"std::pair<Register, unsigned>">;
844def trunc_ext_fold: GICombineRule <
845  (defs root:$root, trunc_ext_fold_matchinfo:$matchinfo),
846  (match (wip_match_opcode G_TRUNC):$root,
847         [{ return Helper.matchCombineTruncOfExt(*${root}, ${matchinfo}); }]),
848  (apply [{ Helper.applyCombineTruncOfExt(*${root}, ${matchinfo}); }])
849>;
850
851// Under certain conditions, transform:
852//  trunc (shl x, K)     -> shl (trunc x), K//
853//  trunc ([al]shr x, K) -> (trunc ([al]shr (trunc x), K))
854def trunc_shift_matchinfo : GIDefMatchData<"std::pair<MachineInstr*, LLT>">;
855def trunc_shift: GICombineRule <
856  (defs root:$root, trunc_shift_matchinfo:$matchinfo),
857  (match (wip_match_opcode G_TRUNC):$root,
858         [{ return Helper.matchCombineTruncOfShift(*${root}, ${matchinfo}); }]),
859  (apply [{ Helper.applyCombineTruncOfShift(*${root}, ${matchinfo}); }])
860>;
861
862// Transform (mul x, -1) -> (sub 0, x)
863def mul_by_neg_one: GICombineRule <
864  (defs root:$dst),
865  (match (G_MUL $dst, $x, -1)),
866  (apply (G_SUB $dst, 0, $x))
867>;
868
869// Fold (xor (and x, y), y) -> (and (not x), y)
870def xor_of_and_with_same_reg_matchinfo :
871    GIDefMatchData<"std::pair<Register, Register>">;
872def xor_of_and_with_same_reg: GICombineRule <
873  (defs root:$root, xor_of_and_with_same_reg_matchinfo:$matchinfo),
874  (match (wip_match_opcode G_XOR):$root,
875         [{ return Helper.matchXorOfAndWithSameReg(*${root}, ${matchinfo}); }]),
876  (apply [{ Helper.applyXorOfAndWithSameReg(*${root}, ${matchinfo}); }])
877>;
878
879// Transform (ptr_add 0, x) -> (int_to_ptr x)
880def ptr_add_with_zero: GICombineRule<
881  (defs root:$root),
882  (match (wip_match_opcode G_PTR_ADD):$root,
883         [{ return Helper.matchPtrAddZero(*${root}); }]),
884  (apply [{ Helper.applyPtrAddZero(*${root}); }])>;
885
886def regs_small_vec : GIDefMatchData<"SmallVector<Register, 4>">;
887def combine_insert_vec_elts_build_vector : GICombineRule<
888  (defs root:$root, regs_small_vec:$info),
889  (match (wip_match_opcode G_INSERT_VECTOR_ELT):$root,
890    [{ return Helper.matchCombineInsertVecElts(*${root}, ${info}); }]),
891  (apply [{ Helper.applyCombineInsertVecElts(*${root}, ${info}); }])>;
892
893def load_or_combine : GICombineRule<
894  (defs root:$root, build_fn_matchinfo:$info),
895  (match (wip_match_opcode G_OR):$root,
896    [{ return Helper.matchLoadOrCombine(*${root}, ${info}); }]),
897  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
898
899def extend_through_phis_matchdata: GIDefMatchData<"MachineInstr*">;
900def extend_through_phis : GICombineRule<
901  (defs root:$root, extend_through_phis_matchdata:$matchinfo),
902  (match (wip_match_opcode G_PHI):$root,
903    [{ return Helper.matchExtendThroughPhis(*${root}, ${matchinfo}); }]),
904  (apply [{ Helper.applyExtendThroughPhis(*${root}, ${matchinfo}); }])>;
905
906// Currently only the one combine above.
907def insert_vec_elt_combines : GICombineGroup<
908                            [combine_insert_vec_elts_build_vector]>;
909
910def extract_vec_elt_build_vec : GICombineRule<
911  (defs root:$root, register_matchinfo:$matchinfo),
912  (match (wip_match_opcode G_EXTRACT_VECTOR_ELT):$root,
913    [{ return Helper.matchExtractVecEltBuildVec(*${root}, ${matchinfo}); }]),
914  (apply [{ Helper.applyExtractVecEltBuildVec(*${root}, ${matchinfo}); }])>;
915
916// Fold away full elt extracts from a build_vector.
917def extract_all_elts_from_build_vector_matchinfo :
918  GIDefMatchData<"SmallVector<std::pair<Register, MachineInstr*>>">;
919def extract_all_elts_from_build_vector : GICombineRule<
920  (defs root:$root, extract_all_elts_from_build_vector_matchinfo:$matchinfo),
921  (match (wip_match_opcode G_BUILD_VECTOR):$root,
922    [{ return Helper.matchExtractAllEltsFromBuildVector(*${root}, ${matchinfo}); }]),
923  (apply [{ Helper.applyExtractAllEltsFromBuildVector(*${root}, ${matchinfo}); }])>;
924
925def extract_vec_elt_combines : GICombineGroup<[
926  extract_vec_elt_build_vec,
927  extract_all_elts_from_build_vector]>;
928
929def funnel_shift_from_or_shift : GICombineRule<
930  (defs root:$root, build_fn_matchinfo:$info),
931  (match (wip_match_opcode G_OR):$root,
932    [{ return Helper.matchOrShiftToFunnelShift(*${root}, ${info}); }]),
933  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])
934>;
935
936def funnel_shift_to_rotate : GICombineRule<
937  (defs root:$root),
938  (match (wip_match_opcode G_FSHL, G_FSHR):$root,
939    [{ return Helper.matchFunnelShiftToRotate(*${root}); }]),
940  (apply [{ Helper.applyFunnelShiftToRotate(*${root}); }])
941>;
942
943// Fold fshr x, y, 0 -> y
944def funnel_shift_right_zero: GICombineRule<
945  (defs root:$root),
946  (match (G_FSHR $x, $y, $z, 0):$root),
947  (apply (COPY $x, $z))
948>;
949
950// Fold fshl x, y, 0 -> x
951def funnel_shift_left_zero: GICombineRule<
952  (defs root:$root),
953  (match (G_FSHL $x, $y, $z, 0):$root),
954  (apply (COPY $x, $y))
955>;
956
957// Fold fsh(l/r) x, y, C -> fsh(l/r) x, y, C % bw
958def funnel_shift_overshift: GICombineRule<
959  (defs root:$root),
960  (match (wip_match_opcode G_FSHL, G_FSHR):$root,
961    [{ return Helper.matchConstantLargerBitWidth(*${root}, 3); }]),
962  (apply [{ Helper.applyFunnelShiftConstantModulo(*${root}); }])
963>;
964
965def rotate_out_of_range : GICombineRule<
966  (defs root:$root),
967  (match (wip_match_opcode G_ROTR, G_ROTL):$root,
968    [{ return Helper.matchRotateOutOfRange(*${root}); }]),
969  (apply [{ Helper.applyRotateOutOfRange(*${root}); }])
970>;
971
972def icmp_to_true_false_known_bits : GICombineRule<
973  (defs root:$d, int64_matchinfo:$matchinfo),
974  (match (wip_match_opcode G_ICMP):$d,
975         [{ return Helper.matchICmpToTrueFalseKnownBits(*${d}, ${matchinfo}); }]),
976  (apply [{ Helper.replaceInstWithConstant(*${d}, ${matchinfo}); }])>;
977
978def icmp_to_lhs_known_bits : GICombineRule<
979  (defs root:$root, build_fn_matchinfo:$info),
980  (match (wip_match_opcode G_ICMP):$root,
981         [{ return Helper.matchICmpToLHSKnownBits(*${root}, ${info}); }]),
982  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
983
984def redundant_binop_in_equality : GICombineRule<
985  (defs root:$root, build_fn_matchinfo:$info),
986  (match (wip_match_opcode G_ICMP):$root,
987         [{ return Helper.matchRedundantBinOpInEquality(*${root}, ${info}); }]),
988  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
989
990// Transform: (X == 0 & Y == 0) -> (X | Y) == 0
991def double_icmp_zero_and_combine: GICombineRule<
992  (defs root:$root),
993  (match (G_ICMP $d1, $p, $s1, 0),
994         (G_ICMP $d2, $p, $s2, 0),
995         (G_AND $root, $d1, $d2),
996         [{ return ${p}.getPredicate() == CmpInst::ICMP_EQ &&
997                       !MRI.getType(${s1}.getReg()).getScalarType().isPointer() &&
998                       (MRI.getType(${s1}.getReg()) ==
999                           MRI.getType(${s2}.getReg())); }]),
1000  (apply (G_OR $ordst, $s1, $s2),
1001         (G_ICMP $root, $p, $ordst, 0))
1002>;
1003
1004// Transform: (X != 0 | Y != 0) -> (X | Y) != 0
1005def double_icmp_zero_or_combine: GICombineRule<
1006  (defs root:$root),
1007  (match (G_ICMP $d1, $p, $s1, 0),
1008         (G_ICMP $d2, $p, $s2, 0),
1009         (G_OR $root, $d1, $d2),
1010         [{ return ${p}.getPredicate() == CmpInst::ICMP_NE &&
1011                       !MRI.getType(${s1}.getReg()).getScalarType().isPointer() &&
1012                       (MRI.getType(${s1}.getReg()) ==
1013                           MRI.getType(${s2}.getReg())); }]),
1014  (apply (G_OR $ordst, $s1, $s2),
1015         (G_ICMP $root, $p, $ordst, 0))
1016>;
1017
1018def double_icmp_zero_and_or_combine : GICombineGroup<[double_icmp_zero_and_combine,
1019                                                      double_icmp_zero_or_combine]>;
1020
1021def and_or_disjoint_mask : GICombineRule<
1022  (defs root:$root, build_fn_matchinfo:$info),
1023  (match (wip_match_opcode G_AND):$root,
1024         [{ return Helper.matchAndOrDisjointMask(*${root}, ${info}); }]),
1025  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${info}); }])>;
1026
1027def bitfield_extract_from_and : GICombineRule<
1028  (defs root:$root, build_fn_matchinfo:$info),
1029  (match (G_CONSTANT $mask, $imm2),
1030         (G_CONSTANT $lsb, $imm1),
1031         (G_LSHR $shift, $x, $lsb),
1032         (G_AND $root, $shift, $mask):$root,
1033    [{ return Helper.matchBitfieldExtractFromAnd(*${root}, ${info}); }]),
1034  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1035
1036def funnel_shift_combines : GICombineGroup<[funnel_shift_from_or_shift,
1037                                            funnel_shift_to_rotate,
1038                                            funnel_shift_right_zero,
1039                                            funnel_shift_left_zero,
1040                                            funnel_shift_overshift]>;
1041
1042def bitfield_extract_from_sext_inreg : GICombineRule<
1043  (defs root:$root, build_fn_matchinfo:$info),
1044  (match (wip_match_opcode G_SEXT_INREG):$root,
1045    [{ return Helper.matchBitfieldExtractFromSExtInReg(*${root}, ${info}); }]),
1046  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1047
1048def bitfield_extract_from_shr : GICombineRule<
1049  (defs root:$root, build_fn_matchinfo:$info),
1050  (match (wip_match_opcode G_ASHR, G_LSHR):$root,
1051    [{ return Helper.matchBitfieldExtractFromShr(*${root}, ${info}); }]),
1052  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1053
1054def bitfield_extract_from_shr_and : GICombineRule<
1055  (defs root:$root, build_fn_matchinfo:$info),
1056  (match (wip_match_opcode G_ASHR, G_LSHR):$root,
1057    [{ return Helper.matchBitfieldExtractFromShrAnd(*${root}, ${info}); }]),
1058  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1059
1060def form_bitfield_extract : GICombineGroup<[bitfield_extract_from_sext_inreg,
1061                                            bitfield_extract_from_and,
1062                                            bitfield_extract_from_shr,
1063                                            bitfield_extract_from_shr_and]>;
1064
1065def udiv_by_const : GICombineRule<
1066  (defs root:$root),
1067  (match (wip_match_opcode G_UDIV):$root,
1068   [{ return Helper.matchUDivByConst(*${root}); }]),
1069  (apply [{ Helper.applyUDivByConst(*${root}); }])>;
1070
1071def sdiv_by_const : GICombineRule<
1072  (defs root:$root),
1073  (match (wip_match_opcode G_SDIV):$root,
1074   [{ return Helper.matchSDivByConst(*${root}); }]),
1075  (apply [{ Helper.applySDivByConst(*${root}); }])>;
1076
1077def sdiv_by_pow2 : GICombineRule<
1078  (defs root:$root),
1079  (match (G_SDIV $dst, $x, $y, (MIFlags (not IsExact))):$root,
1080   [{ return Helper.matchDivByPow2(*${root}, /*IsSigned=*/true); }]),
1081  (apply [{ Helper.applySDivByPow2(*${root}); }])>;
1082
1083def udiv_by_pow2 : GICombineRule<
1084  (defs root:$root),
1085  (match (G_UDIV $dst, $x, $y, (MIFlags (not IsExact))):$root,
1086   [{ return Helper.matchDivByPow2(*${root}, /*IsSigned=*/false); }]),
1087  (apply [{ Helper.applyUDivByPow2(*${root}); }])>;
1088
1089def intdiv_combines : GICombineGroup<[udiv_by_const, sdiv_by_const,
1090                                      sdiv_by_pow2, udiv_by_pow2]>;
1091
1092def reassoc_ptradd : GICombineRule<
1093  (defs root:$root, build_fn_matchinfo:$matchinfo),
1094  (match (wip_match_opcode G_PTR_ADD):$root,
1095    [{ return Helper.matchReassocPtrAdd(*${root}, ${matchinfo}); }]),
1096  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
1097
1098def reassoc_comm_binops : GICombineRule<
1099  (defs root:$root, build_fn_matchinfo:$matchinfo),
1100  (match (G_ADD $root, $src1, $src2):$root,
1101    [{ return Helper.matchReassocCommBinOp(*${root}, ${matchinfo}); }]),
1102  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1103
1104def reassocs : GICombineGroup<[reassoc_ptradd, reassoc_comm_binops]>;
1105
1106// Constant fold operations.
1107def constant_fold_binop : GICombineRule<
1108  (defs root:$d, apint_matchinfo:$matchinfo),
1109  (match (wip_match_opcode G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SHL, G_LSHR, G_ASHR):$d,
1110   [{ return Helper.matchConstantFoldBinOp(*${d}, ${matchinfo}); }]),
1111  (apply [{ Helper.replaceInstWithConstant(*${d}, ${matchinfo}); }])>;
1112
1113def constant_fold_fp_binop : GICombineRule<
1114  (defs root:$d, constantfp_matchinfo:$matchinfo),
1115  (match (wip_match_opcode G_FADD, G_FSUB, G_FMUL, G_FDIV):$d,
1116   [{ return Helper.matchConstantFoldFPBinOp(*${d}, ${matchinfo}); }]),
1117  (apply [{ Helper.replaceInstWithFConstant(*${d}, ${matchinfo}); }])>;
1118
1119
1120def constant_fold_fma : GICombineRule<
1121  (defs root:$d, constantfp_matchinfo:$matchinfo),
1122  (match (wip_match_opcode G_FMAD, G_FMA):$d,
1123   [{ return Helper.matchConstantFoldFMA(*${d}, ${matchinfo}); }]),
1124  (apply [{ Helper.replaceInstWithFConstant(*${d}, ${matchinfo}); }])>;
1125
1126def constant_fold_cast_op : GICombineRule<
1127  (defs root:$d, apint_matchinfo:$matchinfo),
1128  (match (wip_match_opcode G_ZEXT, G_SEXT, G_ANYEXT):$d,
1129   [{ return Helper.matchConstantFoldCastOp(*${d}, ${matchinfo}); }]),
1130  (apply [{ Helper.replaceInstWithConstant(*${d}, ${matchinfo}); }])>;
1131
1132def mulo_by_2: GICombineRule<
1133  (defs root:$root, build_fn_matchinfo:$matchinfo),
1134  (match (wip_match_opcode G_UMULO, G_SMULO):$root,
1135         [{ return Helper.matchMulOBy2(*${root}, ${matchinfo}); }]),
1136  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
1137
1138def mulo_by_0: GICombineRule<
1139  (defs root:$root, build_fn_matchinfo:$matchinfo),
1140  (match (wip_match_opcode G_UMULO, G_SMULO):$root,
1141         [{ return Helper.matchMulOBy0(*${root}, ${matchinfo}); }]),
1142  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1143
1144// Transform (uadde x, y, 0) -> (uaddo x, y)
1145//           (sadde x, y, 0) -> (saddo x, y)
1146//           (usube x, y, 0) -> (usubo x, y)
1147//           (ssube x, y, 0) -> (ssubo x, y)
1148def adde_to_addo: GICombineRule<
1149  (defs root:$root, build_fn_matchinfo:$matchinfo),
1150  (match (wip_match_opcode G_UADDE, G_SADDE, G_USUBE, G_SSUBE):$root,
1151         [{ return Helper.matchAddEToAddO(*${root}, ${matchinfo}); }]),
1152  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
1153
1154def mulh_to_lshr : GICombineRule<
1155  (defs root:$root),
1156  (match (wip_match_opcode G_UMULH):$root,
1157         [{ return Helper.matchUMulHToLShr(*${root}); }]),
1158  (apply [{ Helper.applyUMulHToLShr(*${root}); }])>;
1159
1160def mulh_combines : GICombineGroup<[mulh_to_lshr]>;
1161
1162def redundant_neg_operands: GICombineRule<
1163  (defs root:$root, build_fn_matchinfo:$matchinfo),
1164  (match (wip_match_opcode G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FMAD, G_FMA):$root,
1165    [{ return Helper.matchRedundantNegOperands(*${root}, ${matchinfo}); }]),
1166  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
1167
1168// Transform (fsub +-0.0, X) -> (fneg X)
1169def fsub_to_fneg: GICombineRule<
1170  (defs root:$root, register_matchinfo:$matchinfo),
1171  (match (wip_match_opcode G_FSUB):$root,
1172    [{ return Helper.matchFsubToFneg(*${root}, ${matchinfo}); }]),
1173  (apply [{ Helper.applyFsubToFneg(*${root}, ${matchinfo}); }])>;
1174
1175// Transform (fadd x, (fmul y, z)) -> (fma y, z, x)
1176//           (fadd x, (fmul y, z)) -> (fmad y, z, x)
1177// Transform (fadd (fmul x, y), z) -> (fma x, y, z)
1178//           (fadd (fmul x, y), z) -> (fmad x, y, z)
1179def combine_fadd_fmul_to_fmad_or_fma: GICombineRule<
1180  (defs root:$root, build_fn_matchinfo:$info),
1181  (match (wip_match_opcode G_FADD):$root,
1182         [{ return Helper.matchCombineFAddFMulToFMadOrFMA(*${root},
1183                                                          ${info}); }]),
1184  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1185
1186// Transform (fadd (fpext (fmul x, y)), z) -> (fma (fpext x), (fpext y), z)
1187//                                         -> (fmad (fpext x), (fpext y), z)
1188// Transform (fadd x, (fpext (fmul y, z))) -> (fma (fpext y), (fpext z), x)
1189//                                         -> (fmad (fpext y), (fpext z), x)
1190def combine_fadd_fpext_fmul_to_fmad_or_fma: GICombineRule<
1191  (defs root:$root, build_fn_matchinfo:$info),
1192  (match (wip_match_opcode G_FADD):$root,
1193         [{ return Helper.matchCombineFAddFpExtFMulToFMadOrFMA(*${root},
1194                                                               ${info}); }]),
1195  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1196
1197// Transform (fadd (fma x, y, (fmul z, u)), v)  -> (fma x, y, (fma z, u, v))
1198//           (fadd (fmad x, y, (fmul z, u)), v) -> (fmad x, y, (fmad z, u, v))
1199// Transform (fadd v, (fma x, y, (fmul z, u)))  -> (fma x, y, (fma z, u, v))
1200//           (fadd v, (fmad x, y, (fmul z, u))) -> (fmad x, y, (fmad z, u, v))
1201def combine_fadd_fma_fmul_to_fmad_or_fma: GICombineRule<
1202  (defs root:$root, build_fn_matchinfo:$info),
1203  (match (wip_match_opcode G_FADD):$root,
1204         [{ return Helper.matchCombineFAddFMAFMulToFMadOrFMA(*${root},
1205                                                             ${info}); }]),
1206  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1207
1208// Transform (fadd (fma x, y, (fpext (fmul u, v))), z) ->
1209//           (fma x, y, (fma (fpext u), (fpext v), z))
1210def combine_fadd_fpext_fma_fmul_to_fmad_or_fma: GICombineRule<
1211  (defs root:$root, build_fn_matchinfo:$info),
1212  (match (wip_match_opcode G_FADD):$root,
1213         [{ return Helper.matchCombineFAddFpExtFMulToFMadOrFMAAggressive(
1214                                                  *${root}, ${info}); }]),
1215  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1216
1217// Transform (fsub (fmul x, y), z) -> (fma x, y, -z)
1218//                                 -> (fmad x, y, -z)
1219def combine_fsub_fmul_to_fmad_or_fma: GICombineRule<
1220  (defs root:$root, build_fn_matchinfo:$info),
1221  (match (wip_match_opcode G_FSUB):$root,
1222         [{ return Helper.matchCombineFSubFMulToFMadOrFMA(*${root},
1223                                                          ${info}); }]),
1224  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1225
1226// Transform (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
1227//           (fsub x, (fneg (fmul, y, z))) -> (fma y, z, x)
1228def combine_fsub_fneg_fmul_to_fmad_or_fma: GICombineRule<
1229  (defs root:$root, build_fn_matchinfo:$info),
1230  (match (wip_match_opcode G_FSUB):$root,
1231         [{ return Helper.matchCombineFSubFNegFMulToFMadOrFMA(*${root},
1232                                                              ${info}); }]),
1233  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1234
1235// Transform (fsub (fpext (fmul x, y)), z) ->
1236//           (fma (fpext x), (fpext y), (fneg z))
1237def combine_fsub_fpext_fmul_to_fmad_or_fma: GICombineRule<
1238  (defs root:$root, build_fn_matchinfo:$info),
1239  (match (wip_match_opcode G_FSUB):$root,
1240         [{ return Helper.matchCombineFSubFpExtFMulToFMadOrFMA(*${root},
1241                                                               ${info}); }]),
1242  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1243
1244// Transform (fsub (fneg (fpext (fmul x, y))), z) ->
1245//           (fneg (fma (fpext x), (fpext y), z))
1246def combine_fsub_fpext_fneg_fmul_to_fmad_or_fma: GICombineRule<
1247  (defs root:$root, build_fn_matchinfo:$info),
1248  (match (wip_match_opcode G_FSUB):$root,
1249         [{ return Helper.matchCombineFSubFpExtFNegFMulToFMadOrFMA(
1250                                            *${root}, ${info}); }]),
1251  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1252
1253def combine_minmax_nan: GICombineRule<
1254  (defs root:$root, unsigned_matchinfo:$info),
1255  (match (wip_match_opcode G_FMINNUM, G_FMAXNUM, G_FMINIMUM, G_FMAXIMUM):$root,
1256         [{ return Helper.matchCombineFMinMaxNaN(*${root}, ${info}); }]),
1257  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, ${info}); }])>;
1258
1259// Transform (add x, (sub y, x)) -> y
1260// Transform (add (sub y, x), x) -> y
1261def add_sub_reg_frags : GICombinePatFrag<
1262  (outs root:$dst), (ins $src),
1263  [
1264    (pattern (G_ADD $dst, $x, $tmp), (G_SUB $tmp, $src, $x)),
1265    (pattern (G_ADD $dst, $tmp, $x), (G_SUB $tmp, $src, $x))
1266  ]>;
1267def add_sub_reg: GICombineRule <
1268  (defs root:$dst),
1269  (match (add_sub_reg_frags $dst, $src)),
1270  (apply (GIReplaceReg $dst, $src))>;
1271
1272def buildvector_identity_fold : GICombineRule<
1273  (defs root:$build_vector, register_matchinfo:$matchinfo),
1274  (match (wip_match_opcode G_BUILD_VECTOR_TRUNC, G_BUILD_VECTOR):$build_vector,
1275         [{ return Helper.matchBuildVectorIdentityFold(*${build_vector}, ${matchinfo}); }]),
1276  (apply [{ Helper.replaceSingleDefInstWithReg(*${build_vector}, ${matchinfo}); }])>;
1277
1278def trunc_buildvector_fold : GICombineRule<
1279  (defs root:$op, register_matchinfo:$matchinfo),
1280  (match (wip_match_opcode G_TRUNC):$op,
1281      [{ return Helper.matchTruncBuildVectorFold(*${op}, ${matchinfo}); }]),
1282  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${matchinfo}); }])>;
1283
1284def trunc_lshr_buildvector_fold : GICombineRule<
1285  (defs root:$op, register_matchinfo:$matchinfo),
1286  (match (wip_match_opcode G_TRUNC):$op,
1287      [{ return Helper.matchTruncLshrBuildVectorFold(*${op}, ${matchinfo}); }]),
1288  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${matchinfo}); }])>;
1289
1290// Transform:
1291//   (x + y) - y -> x
1292//   (x + y) - x -> y
1293//   x - (y + x) -> 0 - y
1294//   x - (x + z) -> 0 - z
1295def sub_add_reg: GICombineRule <
1296  (defs root:$root, build_fn_matchinfo:$matchinfo),
1297  (match (wip_match_opcode G_SUB):$root,
1298         [{ return Helper.matchSubAddSameReg(*${root}, ${matchinfo}); }]),
1299  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1300
1301def bitcast_bitcast_fold : GICombineRule<
1302  (defs root:$dst),
1303  (match (G_BITCAST $dst, $src1):$op, (G_BITCAST $src1, $src0),
1304      [{ return MRI.getType(${src0}.getReg()) == MRI.getType(${dst}.getReg()); }]),
1305  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${src0}.getReg()); }])>;
1306
1307
1308def fptrunc_fpext_fold : GICombineRule<
1309  (defs root:$dst),
1310  (match (G_FPTRUNC $dst, $src1):$op, (G_FPEXT $src1, $src0),
1311      [{ return MRI.getType(${src0}.getReg()) == MRI.getType(${dst}.getReg()); }]),
1312  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${src0}.getReg()); }])>;
1313
1314
1315def select_to_minmax: GICombineRule<
1316  (defs root:$root, build_fn_matchinfo:$info),
1317  (match (wip_match_opcode G_SELECT):$root,
1318         [{ return Helper.matchSimplifySelectToMinMax(*${root}, ${info}); }]),
1319  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1320
1321def select_to_iminmax: GICombineRule<
1322  (defs root:$root, build_fn_matchinfo:$info),
1323  (match (G_ICMP $tst, $tst1, $a, $b),
1324         (G_SELECT $root, $tst, $x, $y),
1325         [{ return Helper.matchSelectIMinMax(${root}, ${info}); }]),
1326  (apply [{ Helper.applyBuildFnMO(${root}, ${info}); }])>;
1327
1328def match_selects : GICombineRule<
1329  (defs root:$root, build_fn_matchinfo:$matchinfo),
1330  (match (wip_match_opcode G_SELECT):$root,
1331        [{ return Helper.matchSelect(*${root}, ${matchinfo}); }]),
1332  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1333
1334def match_ands : GICombineRule<
1335  (defs root:$root, build_fn_matchinfo:$matchinfo),
1336  (match (wip_match_opcode G_AND):$root,
1337        [{ return Helper.matchAnd(*${root}, ${matchinfo}); }]),
1338  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1339
1340def match_ors : GICombineRule<
1341  (defs root:$root, build_fn_matchinfo:$matchinfo),
1342  (match (wip_match_opcode G_OR):$root,
1343        [{ return Helper.matchOr(*${root}, ${matchinfo}); }]),
1344  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1345
1346def match_addos : GICombineRule<
1347  (defs root:$root, build_fn_matchinfo:$matchinfo),
1348  (match (wip_match_opcode G_SADDO, G_UADDO):$root,
1349        [{ return Helper.matchAddOverflow(*${root}, ${matchinfo}); }]),
1350  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1351
1352def match_extract_of_element_undef_vector: GICombineRule <
1353  (defs root:$root),
1354  (match (G_IMPLICIT_DEF $vector),
1355         (G_EXTRACT_VECTOR_ELT $root, $vector, $idx)),
1356  (apply (G_IMPLICIT_DEF $root))
1357>;
1358
1359def match_extract_of_element_undef_index: GICombineRule <
1360  (defs root:$root),
1361  (match (G_IMPLICIT_DEF $idx),
1362         (G_EXTRACT_VECTOR_ELT $root, $vector, $idx)),
1363  (apply (G_IMPLICIT_DEF $root))
1364>;
1365
1366def match_extract_of_element : GICombineRule<
1367  (defs root:$root, build_fn_matchinfo:$matchinfo),
1368  (match (wip_match_opcode G_EXTRACT_VECTOR_ELT):$root,
1369        [{ return Helper.matchExtractVectorElement(*${root}, ${matchinfo}); }]),
1370  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1371
1372def extract_vector_element_not_const : GICombineRule<
1373   (defs root:$root),
1374   (match (G_INSERT_VECTOR_ELT $src, $x, $value, $idx),
1375          (G_EXTRACT_VECTOR_ELT $root, $src, $idx)),
1376   (apply (GIReplaceReg $root, $value))>;
1377
1378def extract_vector_element_different_indices : GICombineRule<
1379   (defs root:$root, build_fn_matchinfo:$matchinfo),
1380   (match (G_INSERT_VECTOR_ELT $src, $x, $value, $idx2),
1381          (G_EXTRACT_VECTOR_ELT $root, $src, $idx1),
1382   [{ return Helper.matchExtractVectorElementWithDifferentIndices(${root}, ${matchinfo}); }]),
1383   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1384
1385def extract_vector_element_build_vector2 : GICombineRule<
1386   (defs root:$root, build_fn_matchinfo:$matchinfo),
1387   (match (G_BUILD_VECTOR $src, $x, $y),
1388          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1389   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1390   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1391
1392def extract_vector_element_build_vector3 : GICombineRule<
1393   (defs root:$root, build_fn_matchinfo:$matchinfo),
1394   (match (G_BUILD_VECTOR $src, $x, $y, $z),
1395          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1396   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1397   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1398
1399def extract_vector_element_build_vector4 : GICombineRule<
1400   (defs root:$root, build_fn_matchinfo:$matchinfo),
1401   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a),
1402          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1403   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1404   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1405
1406def extract_vector_element_build_vector5 : GICombineRule<
1407   (defs root:$root, build_fn_matchinfo:$matchinfo),
1408   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b),
1409          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1410   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1411   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1412
1413def extract_vector_element_build_vector6 : GICombineRule<
1414   (defs root:$root, build_fn_matchinfo:$matchinfo),
1415   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c),
1416          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1417   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1418   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1419
1420def extract_vector_element_build_vector7 : GICombineRule<
1421   (defs root:$root, build_fn_matchinfo:$matchinfo),
1422   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d),
1423          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1424   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1425   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1426
1427def extract_vector_element_build_vector8 : GICombineRule<
1428   (defs root:$root, build_fn_matchinfo:$matchinfo),
1429   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e),
1430          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1431   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1432   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1433
1434def extract_vector_element_build_vector9 : GICombineRule<
1435   (defs root:$root, build_fn_matchinfo:$matchinfo),
1436   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f),
1437          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1438   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1439   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1440
1441def extract_vector_element_build_vector10 : GICombineRule<
1442   (defs root:$root, build_fn_matchinfo:$matchinfo),
1443   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g),
1444          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1445   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1446   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1447
1448def extract_vector_element_build_vector11 : GICombineRule<
1449   (defs root:$root, build_fn_matchinfo:$matchinfo),
1450   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h),
1451          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1452   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1453   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1454
1455def extract_vector_element_build_vector12 : GICombineRule<
1456   (defs root:$root, build_fn_matchinfo:$matchinfo),
1457   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i),
1458          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1459   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1460   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1461
1462def extract_vector_element_build_vector13 : GICombineRule<
1463   (defs root:$root, build_fn_matchinfo:$matchinfo),
1464   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j),
1465          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1466   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1467   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1468
1469def extract_vector_element_build_vector14 : GICombineRule<
1470   (defs root:$root, build_fn_matchinfo:$matchinfo),
1471   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k),
1472          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1473   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1474   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1475
1476def extract_vector_element_build_vector15 : GICombineRule<
1477   (defs root:$root, build_fn_matchinfo:$matchinfo),
1478   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l),
1479          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1480   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1481   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1482
1483def extract_vector_element_build_vector16 : GICombineRule<
1484   (defs root:$root, build_fn_matchinfo:$matchinfo),
1485   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m),
1486          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1487   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1488   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1489
1490def extract_vector_element_build_vector_trunc2 : GICombineRule<
1491   (defs root:$root, build_fn_matchinfo:$matchinfo),
1492   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y),
1493          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1494   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1495   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1496
1497def extract_vector_element_build_vector_trunc3 : GICombineRule<
1498   (defs root:$root, build_fn_matchinfo:$matchinfo),
1499   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z),
1500          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1501   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1502   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1503
1504def extract_vector_element_build_vector_trunc4 : GICombineRule<
1505   (defs root:$root, build_fn_matchinfo:$matchinfo),
1506   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a),
1507          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1508   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1509   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1510
1511def extract_vector_element_build_vector_trunc5 : GICombineRule<
1512   (defs root:$root, build_fn_matchinfo:$matchinfo),
1513   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a, $b),
1514          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1515   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1516   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1517
1518def extract_vector_element_build_vector_trunc6 : GICombineRule<
1519   (defs root:$root, build_fn_matchinfo:$matchinfo),
1520   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a, $b, $c),
1521          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1522   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1523   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1524
1525def extract_vector_element_build_vector_trunc7 : GICombineRule<
1526   (defs root:$root, build_fn_matchinfo:$matchinfo),
1527   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a, $b, $c, $d),
1528          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1529   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1530   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1531
1532def extract_vector_element_build_vector_trunc8 : GICombineRule<
1533   (defs root:$root, build_fn_matchinfo:$matchinfo),
1534   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a, $b, $c, $d, $e),
1535          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1536   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1537   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1538
1539def sext_trunc : GICombineRule<
1540   (defs root:$root, build_fn_matchinfo:$matchinfo),
1541   (match (G_TRUNC $src, $x, (MIFlags NoSWrap)),
1542          (G_SEXT $root, $src),
1543   [{ return Helper.matchSextOfTrunc(${root}, ${matchinfo}); }]),
1544   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1545
1546def zext_trunc : GICombineRule<
1547   (defs root:$root, build_fn_matchinfo:$matchinfo),
1548   (match (G_TRUNC $src, $x, (MIFlags NoUWrap)),
1549          (G_ZEXT $root, $src),
1550   [{ return Helper.matchZextOfTrunc(${root}, ${matchinfo}); }]),
1551   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1552
1553def nneg_zext : GICombineRule<
1554   (defs root:$root, build_fn_matchinfo:$matchinfo),
1555   (match (G_ZEXT $root, $x, (MIFlags NonNeg)),
1556   [{ return Helper.matchNonNegZext(${root}, ${matchinfo}); }]),
1557   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1558
1559def extract_vector_element_shuffle_vector : GICombineRule<
1560   (defs root:$root, build_fn_matchinfo:$matchinfo),
1561   (match (G_SHUFFLE_VECTOR $src, $src1, $src2, $mask),
1562          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1563   [{ return Helper.matchExtractVectorElementWithShuffleVector(${root}, ${matchinfo}); }]),
1564   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1565
1566// Combines concat operations
1567def concat_matchinfo : GIDefMatchData<"SmallVector<Register>">;
1568def combine_concat_vector : GICombineRule<
1569  (defs root:$root, concat_matchinfo:$matchinfo),
1570  (match (wip_match_opcode G_CONCAT_VECTORS):$root,
1571        [{ return Helper.matchCombineConcatVectors(*${root}, ${matchinfo}); }]),
1572  (apply [{ Helper.applyCombineConcatVectors(*${root}, ${matchinfo}); }])>;
1573
1574// Combines Shuffles of Concats
1575// a = G_CONCAT_VECTORS x, y, undef, undef
1576// b = G_CONCAT_VECTORS z, undef, undef, undef
1577// c = G_SHUFFLE_VECTORS a, b, <0, 1, 4, undef>
1578// ===>
1579// c = G_CONCAT_VECTORS x, y, z, undef
1580def combine_shuffle_concat : GICombineRule<
1581  (defs root:$root, concat_matchinfo:$matchinfo),
1582  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
1583        [{ return Helper.matchCombineShuffleConcat(*${root}, ${matchinfo}); }]),
1584  (apply [{ Helper.applyCombineShuffleConcat(*${root}, ${matchinfo}); }])>;
1585
1586def insert_vector_element_idx_undef : GICombineRule<
1587   (defs root:$root),
1588   (match (G_IMPLICIT_DEF $idx),
1589          (G_INSERT_VECTOR_ELT $root, $src, $elt, $idx)),
1590   (apply (G_IMPLICIT_DEF $root))>;
1591
1592def insert_vector_element_elt_undef : GICombineRule<
1593   (defs root:$root),
1594   (match (G_IMPLICIT_DEF $elt),
1595          (G_INSERT_VECTOR_ELT $root, $src, $elt, $idx),
1596          [{ return isGuaranteedNotToBePoison(${src}.getReg(), MRI); }]),
1597   (apply (GIReplaceReg $root, $src))>;
1598
1599def insert_vector_element_extract_vector_element : GICombineRule<
1600   (defs root:$root),
1601   (match (G_EXTRACT_VECTOR_ELT $elt, $src, $idx),
1602          (G_INSERT_VECTOR_ELT $root, $src, $elt, $idx)),
1603   (apply (GIReplaceReg $root, $src))>;
1604
1605def insert_vector_elt_oob : GICombineRule<
1606  (defs root:$root, build_fn_matchinfo:$matchinfo),
1607  (match (wip_match_opcode G_INSERT_VECTOR_ELT):$root,
1608         [{ return Helper.matchInsertVectorElementOOB(*${root}, ${matchinfo}); }]),
1609  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1610
1611def add_of_vscale : GICombineRule<
1612   (defs root:$root, build_fn_matchinfo:$matchinfo),
1613   (match (G_VSCALE $left, $imm1),
1614          (G_VSCALE $right, $imm2),
1615          (G_ADD $root, $left, $right, (MIFlags NoSWrap)),
1616   [{ return Helper.matchAddOfVScale(${root}, ${matchinfo}); }]),
1617   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1618
1619def mul_of_vscale : GICombineRule<
1620   (defs root:$root, build_fn_matchinfo:$matchinfo),
1621   (match (G_VSCALE $left, $scale),
1622          (G_CONSTANT $x, $imm1),
1623          (G_MUL $root, $left, $x, (MIFlags NoSWrap)),
1624   [{ return Helper.matchMulOfVScale(${root}, ${matchinfo}); }]),
1625   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1626
1627def shl_of_vscale : GICombineRule<
1628   (defs root:$root, build_fn_matchinfo:$matchinfo),
1629   (match (G_VSCALE $left, $imm),
1630          (G_CONSTANT $x, $imm1),
1631          (G_SHL $root, $left, $x, (MIFlags NoSWrap)),
1632   [{ return Helper.matchShlOfVScale(${root}, ${matchinfo}); }]),
1633   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1634
1635def sub_of_vscale : GICombineRule<
1636   (defs root:$root, build_fn_matchinfo:$matchinfo),
1637   (match (G_VSCALE $right, $imm),
1638          (G_SUB $root, $x, $right, (MIFlags NoSWrap)),
1639   [{ return Helper.matchSubOfVScale(${root}, ${matchinfo}); }]),
1640   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1641
1642def expand_const_fpowi : GICombineRule<
1643   (defs root:$root),
1644   (match (G_CONSTANT $int, $imm),
1645          (G_FPOWI $dst, $float, $int):$root,
1646          [{ return Helper.matchFPowIExpansion(*${root}, ${imm}.getCImm()->getSExtValue()); }]),
1647   (apply [{ Helper.applyExpandFPowI(*${root}, ${imm}.getCImm()->getSExtValue()); }])>;
1648
1649// match_extract_of_element and insert_vector_elt_oob must be the first!
1650def vector_ops_combines: GICombineGroup<[
1651match_extract_of_element_undef_vector,
1652match_extract_of_element_undef_index,
1653insert_vector_element_idx_undef,
1654insert_vector_element_elt_undef,
1655match_extract_of_element,
1656insert_vector_elt_oob,
1657extract_vector_element_not_const,
1658extract_vector_element_different_indices,
1659extract_vector_element_build_vector2,
1660extract_vector_element_build_vector3,
1661extract_vector_element_build_vector4,
1662extract_vector_element_build_vector5,
1663extract_vector_element_build_vector7,
1664extract_vector_element_build_vector8,
1665extract_vector_element_build_vector9,
1666extract_vector_element_build_vector10,
1667extract_vector_element_build_vector11,
1668extract_vector_element_build_vector12,
1669extract_vector_element_build_vector13,
1670extract_vector_element_build_vector14,
1671extract_vector_element_build_vector15,
1672extract_vector_element_build_vector16,
1673extract_vector_element_build_vector_trunc2,
1674extract_vector_element_build_vector_trunc3,
1675extract_vector_element_build_vector_trunc4,
1676extract_vector_element_build_vector_trunc5,
1677extract_vector_element_build_vector_trunc6,
1678extract_vector_element_build_vector_trunc7,
1679extract_vector_element_build_vector_trunc8,
1680extract_vector_element_shuffle_vector,
1681insert_vector_element_extract_vector_element,
1682add_of_vscale,
1683mul_of_vscale,
1684shl_of_vscale,
1685sub_of_vscale,
1686]>;
1687
1688
1689// fold ((0-A) + B) -> B-A
1690def ZeroMinusAPlusB : GICombineRule<
1691   (defs root:$root),
1692   (match (G_SUB $sub, 0, $A),
1693          (G_ADD $root, $sub, $B)),
1694   (apply (G_SUB $root, $B, $A))>;
1695
1696// fold (A + (0-B)) -> A-B
1697def APlusZeroMinusB : GICombineRule<
1698   (defs root:$root),
1699   (match (G_SUB $sub, 0, $B),
1700          (G_ADD $root, $A, $sub)),
1701   (apply (G_SUB $root, $A, $B))>;
1702
1703 // fold (A+(B-A)) -> B
1704 def APlusBMinusB : GICombineRule<
1705   (defs root:$root),
1706   (match (G_SUB $sub, $B, $A),
1707          (G_ADD $root, $A, $sub)),
1708   (apply (GIReplaceReg $root, $B))>;
1709
1710// fold ((B-A)+A) -> B
1711 def BMinusAPlusA : GICombineRule<
1712   (defs root:$root),
1713   (match (G_SUB $sub, $B, $A),
1714          (G_ADD $root, $sub, $A)),
1715   (apply (GIReplaceReg $root, $B))>;
1716
1717// fold ((A-B)+(C-A)) -> (C-B)
1718def AMinusBPlusCMinusA : GICombineRule<
1719   (defs root:$root),
1720   (match (G_SUB $sub1, $A, $B),
1721          (G_SUB $sub2, $C, $A),
1722          (G_ADD $root, $sub1, $sub2)),
1723   (apply (G_SUB $root, $C, $B))>;
1724
1725// fold ((A-B)+(B-C)) -> (A-C)
1726def AMinusBPlusBMinusC : GICombineRule<
1727   (defs root:$root),
1728   (match (G_SUB $sub1, $A, $B),
1729          (G_SUB $sub2, $B, $C),
1730          (G_ADD $root, $sub1, $sub2)),
1731   (apply (G_SUB $root, $A, $C))>;
1732
1733// fold (A+(B-(A+C))) to (B-C)
1734def APlusBMinusAplusC : GICombineRule<
1735   (defs root:$root),
1736   (match (G_ADD $add1, $A, $C),
1737          (G_SUB $sub1, $B, $add1),
1738          (G_ADD $root, $A, $sub1)),
1739   (apply (G_SUB $root, $B, $C))>;
1740
1741// fold (A+(B-(C+A))) to (B-C)
1742def APlusBMinusCPlusA : GICombineRule<
1743   (defs root:$root),
1744   (match (G_ADD $add1, $C, $A),
1745          (G_SUB $sub1, $B, $add1),
1746          (G_ADD $root, $A, $sub1)),
1747   (apply (G_SUB $root, $B, $C))>;
1748
1749def integer_reassoc_combines: GICombineGroup<[
1750  ZeroMinusAPlusB,
1751  APlusZeroMinusB,
1752  APlusBMinusB,
1753  BMinusAPlusA,
1754  AMinusBPlusCMinusA,
1755  AMinusBPlusBMinusC,
1756  APlusBMinusAplusC,
1757  APlusBMinusCPlusA
1758]>;
1759
1760def freeze_of_non_undef_non_poison : GICombineRule<
1761   (defs root:$root),
1762   (match (G_FREEZE $root, $src),
1763          [{ return isGuaranteedNotToBeUndefOrPoison(${src}.getReg(), MRI); }]),
1764   (apply (GIReplaceReg $root, $src))>;
1765
1766def freeze_combines: GICombineGroup<[
1767  freeze_of_non_undef_non_poison,
1768  push_freeze_to_prevent_poison_from_propagating
1769]>;
1770
1771// FIXME: These should use the custom predicate feature once it lands.
1772def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero,
1773                                     undef_to_negative_one,
1774                                     binop_left_undef_to_zero,
1775                                     binop_right_undef_to_undef,
1776                                     unary_undef_to_zero,
1777                                     propagate_undef_any_op,
1778                                     propagate_undef_all_ops,
1779                                     propagate_undef_shuffle_mask,
1780                                     erase_undef_store,
1781                                     unmerge_undef,
1782                                     insert_extract_vec_elt_out_of_bounds]>;
1783
1784def identity_combines : GICombineGroup<[select_same_val, right_identity_zero,
1785                                        binop_same_val, binop_left_to_zero,
1786                                        binop_right_to_zero, p2i_to_i2p,
1787                                        i2p_to_p2i, anyext_trunc_fold,
1788                                        fneg_fneg_fold, right_identity_one,
1789                                        add_sub_reg, buildvector_identity_fold,
1790                                        trunc_buildvector_fold,
1791                                        trunc_lshr_buildvector_fold,
1792                                        bitcast_bitcast_fold, fptrunc_fpext_fold,
1793                                        right_identity_neg_zero_fp,
1794                                        right_identity_neg_one_fp]>;
1795
1796def const_combines : GICombineGroup<[constant_fold_fp_ops, const_ptradd_to_i2p,
1797                                     overlapping_and, mulo_by_2, mulo_by_0,
1798                                     adde_to_addo,
1799                                     combine_minmax_nan, expand_const_fpowi]>;
1800
1801def known_bits_simplifications : GICombineGroup<[
1802  redundant_and, redundant_sext_inreg, redundant_or, urem_pow2_to_mask,
1803  zext_trunc_fold, icmp_to_true_false_known_bits, icmp_to_lhs_known_bits,
1804  sext_inreg_to_zext_inreg]>;
1805
1806def width_reduction_combines : GICombineGroup<[reduce_shl_of_extend,
1807                                               narrow_binop_feeding_and]>;
1808
1809def phi_combines : GICombineGroup<[extend_through_phis]>;
1810
1811def bitreverse_shift : GICombineGroup<[bitreverse_shl, bitreverse_lshr]>;
1812
1813def select_combines : GICombineGroup<[select_undef_cmp, select_constant_cmp,
1814                                      select_to_iminmax, match_selects]>;
1815
1816def trivial_combines : GICombineGroup<[copy_prop, mul_to_shl, add_p2i_to_ptradd,
1817                                       mul_by_neg_one, idempotent_prop]>;
1818
1819def fma_combines : GICombineGroup<[combine_fadd_fmul_to_fmad_or_fma,
1820  combine_fadd_fpext_fmul_to_fmad_or_fma, combine_fadd_fma_fmul_to_fmad_or_fma,
1821  combine_fadd_fpext_fma_fmul_to_fmad_or_fma, combine_fsub_fmul_to_fmad_or_fma,
1822  combine_fsub_fneg_fmul_to_fmad_or_fma, combine_fsub_fpext_fmul_to_fmad_or_fma,
1823  combine_fsub_fpext_fneg_fmul_to_fmad_or_fma]>;
1824
1825def constant_fold_binops : GICombineGroup<[constant_fold_binop,
1826                                           constant_fold_fp_binop]>;
1827
1828def prefer_sign_combines : GICombineGroup<[nneg_zext]>;
1829
1830def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
1831    vector_ops_combines, freeze_combines,
1832    insert_vec_elt_combines, extract_vec_elt_combines, combines_for_extload,
1833    combine_extracted_vector_load,
1834    undef_combines, identity_combines, phi_combines,
1835    simplify_add_to_sub, hoist_logic_op_with_same_opcode_hands, shifts_too_big,
1836    reassocs, ptr_add_immed_chain,
1837    shl_ashr_to_sext_inreg, sext_inreg_of_load,
1838    width_reduction_combines, select_combines,
1839    known_bits_simplifications, ext_ext_fold,
1840    not_cmp_fold, opt_brcond_by_inverting_cond,
1841    unmerge_merge, unmerge_cst, unmerge_dead_to_trunc,
1842    unmerge_zext_to_zext, merge_unmerge, trunc_ext_fold, trunc_shift,
1843    const_combines, xor_of_and_with_same_reg, ptr_add_with_zero,
1844    shift_immed_chain, shift_of_shifted_logic_chain, load_or_combine,
1845    div_rem_to_divrem, funnel_shift_combines, bitreverse_shift, commute_shift,
1846    form_bitfield_extract, constant_fold_binops, constant_fold_fma,
1847    constant_fold_cast_op, fabs_fneg_fold,
1848    intdiv_combines, mulh_combines, redundant_neg_operands,
1849    and_or_disjoint_mask, fma_combines, fold_binop_into_select,
1850    sub_add_reg, select_to_minmax, redundant_binop_in_equality,
1851    fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
1852    combine_concat_vector, double_icmp_zero_and_or_combine, match_addos,
1853    sext_trunc, zext_trunc, prefer_sign_combines, combine_shuffle_concat]>;
1854
1855// A combine group used to for prelegalizer combiners at -O0. The combines in
1856// this group have been selected based on experiments to balance code size and
1857// compile time performance.
1858def optnone_combines : GICombineGroup<[trivial_combines,
1859    ptr_add_immed_chain, combines_for_extload,
1860    not_cmp_fold, opt_brcond_by_inverting_cond, combine_concat_vector]>;
1861