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