• 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//===----------------------------------------------------------------------===//
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">;
185
186def MIFlags;
187// def not; -> Already defined as a SDNode
188
189//===----------------------------------------------------------------------===//
190
191def extending_load_matchdata : GIDefMatchData<"PreferredTuple">;
192def indexed_load_store_matchdata : GIDefMatchData<"IndexedLoadStoreMatchInfo">;
193def instruction_steps_matchdata: GIDefMatchData<"InstructionStepsMatchInfo">;
194
195def register_matchinfo: GIDefMatchData<"Register">;
196def int64_matchinfo: GIDefMatchData<"int64_t">;
197def apint_matchinfo : GIDefMatchData<"APInt">;
198def constantfp_matchinfo : GIDefMatchData<"ConstantFP*">;
199def build_fn_matchinfo :
200GIDefMatchData<"std::function<void(MachineIRBuilder &)>">;
201def unsigned_matchinfo: GIDefMatchData<"unsigned">;
202
203def copy_prop : GICombineRule<
204  (defs root:$d),
205  (match (COPY $d, $s):$mi,
206         [{ return Helper.matchCombineCopy(*${mi}); }]),
207  (apply [{ Helper.applyCombineCopy(*${mi}); }])>;
208
209// idempotent operations
210// Fold (freeze (freeze x)) -> (freeze x).
211// Fold (fabs (fabs x)) -> (fabs x).
212// Fold (fcanonicalize (fcanonicalize x)) -> (fcanonicalize x).
213def idempotent_prop_frags : GICombinePatFrag<
214  (outs root:$dst, $src), (ins),
215  !foreach(op, [G_FREEZE, G_FABS, G_FCANONICALIZE],
216           (pattern (op $dst, $src), (op $src, $x)))>;
217
218def idempotent_prop : GICombineRule<
219   (defs root:$dst),
220   (match (idempotent_prop_frags $dst, $src)),
221   (apply (GIReplaceReg $dst, $src))>;
222
223
224def extending_loads : GICombineRule<
225  (defs root:$root, extending_load_matchdata:$matchinfo),
226  (match (wip_match_opcode G_LOAD, G_SEXTLOAD, G_ZEXTLOAD):$root,
227         [{ return Helper.matchCombineExtendingLoads(*${root}, ${matchinfo}); }]),
228  (apply [{ Helper.applyCombineExtendingLoads(*${root}, ${matchinfo}); }])>;
229
230def load_and_mask : GICombineRule<
231  (defs root:$root, build_fn_matchinfo:$matchinfo),
232  (match (wip_match_opcode G_AND):$root,
233        [{ return Helper.matchCombineLoadWithAndMask(*${root}, ${matchinfo}); }]),
234  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
235def combines_for_extload: GICombineGroup<[extending_loads, load_and_mask]>;
236
237def sext_trunc_sextload : GICombineRule<
238  (defs root:$d),
239  (match (wip_match_opcode G_SEXT_INREG):$d,
240         [{ return Helper.matchSextTruncSextLoad(*${d}); }]),
241  (apply [{ Helper.applySextTruncSextLoad(*${d}); }])>;
242
243def sext_inreg_of_load_matchdata : GIDefMatchData<"std::tuple<Register, unsigned>">;
244def sext_inreg_of_load : GICombineRule<
245  (defs root:$root, sext_inreg_of_load_matchdata:$matchinfo),
246  (match (wip_match_opcode G_SEXT_INREG):$root,
247         [{ return Helper.matchSextInRegOfLoad(*${root}, ${matchinfo}); }]),
248  (apply [{ Helper.applySextInRegOfLoad(*${root}, ${matchinfo}); }])>;
249
250def sext_inreg_to_zext_inreg : GICombineRule<
251  (defs root:$dst),
252  (match
253    (G_SEXT_INREG $dst, $src, $imm):$root,
254      [{
255        unsigned BitWidth = MRI.getType(${src}.getReg()).getScalarSizeInBits();
256        return Helper.getKnownBits()->maskedValueIsZero(${src}.getReg(),
257                 APInt::getOneBitSet(BitWidth, ${imm}.getImm() - 1)); }]),
258    (apply [{
259      Helper.getBuilder().setInstrAndDebugLoc(*${root});
260      Helper.getBuilder().buildZExtInReg(${dst}, ${src}, ${imm}.getImm());
261      ${root}->eraseFromParent();
262  }])
263>;
264
265def combine_extracted_vector_load : GICombineRule<
266  (defs root:$root, build_fn_matchinfo:$matchinfo),
267  (match (wip_match_opcode G_EXTRACT_VECTOR_ELT):$root,
268        [{ return Helper.matchCombineExtractedVectorLoad(*${root}, ${matchinfo}); }]),
269  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
270
271def combine_indexed_load_store : GICombineRule<
272  (defs root:$root, indexed_load_store_matchdata:$matchinfo),
273  (match (wip_match_opcode G_LOAD, G_SEXTLOAD, G_ZEXTLOAD, G_STORE):$root,
274         [{ return Helper.matchCombineIndexedLoadStore(*${root}, ${matchinfo}); }]),
275  (apply [{ Helper.applyCombineIndexedLoadStore(*${root}, ${matchinfo}); }])>;
276
277def opt_brcond_by_inverting_cond_matchdata : GIDefMatchData<"MachineInstr *">;
278def opt_brcond_by_inverting_cond : GICombineRule<
279  (defs root:$root, opt_brcond_by_inverting_cond_matchdata:$matchinfo),
280  (match (wip_match_opcode G_BR):$root,
281         [{ return Helper.matchOptBrCondByInvertingCond(*${root}, ${matchinfo}); }]),
282  (apply [{ Helper.applyOptBrCondByInvertingCond(*${root}, ${matchinfo}); }])>;
283
284def ptr_add_immed_matchdata : GIDefMatchData<"PtrAddChain">;
285def ptr_add_immed_chain : GICombineRule<
286  (defs root:$d, ptr_add_immed_matchdata:$matchinfo),
287  (match (wip_match_opcode G_PTR_ADD):$d,
288         [{ return Helper.matchPtrAddImmedChain(*${d}, ${matchinfo}); }]),
289  (apply [{ Helper.applyPtrAddImmedChain(*${d}, ${matchinfo}); }])>;
290
291def shifts_too_big : GICombineRule<
292  (defs root:$root),
293  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR):$root,
294         [{ return Helper.matchShiftsTooBig(*${root}); }]),
295  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
296
297// Fold shift (shift base x), y -> shift base, (x+y), if shifts are same
298def shift_immed_matchdata : GIDefMatchData<"RegisterImmPair">;
299def shift_immed_chain : GICombineRule<
300  (defs root:$d, shift_immed_matchdata:$matchinfo),
301  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR, G_SSHLSAT, G_USHLSAT):$d,
302         [{ return Helper.matchShiftImmedChain(*${d}, ${matchinfo}); }]),
303  (apply [{ Helper.applyShiftImmedChain(*${d}, ${matchinfo}); }])>;
304
305// Transform shift (logic (shift X, C0), Y), C1
306//        -> logic (shift X, (C0+C1)), (shift Y, C1), if shifts are same
307def shift_of_shifted_logic_matchdata : GIDefMatchData<"ShiftOfShiftedLogic">;
308def shift_of_shifted_logic_chain : GICombineRule<
309  (defs root:$d, shift_of_shifted_logic_matchdata:$matchinfo),
310  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR, G_USHLSAT, G_SSHLSAT):$d,
311         [{ return Helper.matchShiftOfShiftedLogic(*${d}, ${matchinfo}); }]),
312  (apply [{ Helper.applyShiftOfShiftedLogic(*${d}, ${matchinfo}); }])>;
313
314def mul_to_shl : GICombineRule<
315  (defs root:$d, unsigned_matchinfo:$matchinfo),
316  (match (G_MUL $d, $op1, $op2):$mi,
317         [{ return Helper.matchCombineMulToShl(*${mi}, ${matchinfo}); }]),
318  (apply [{ Helper.applyCombineMulToShl(*${mi}, ${matchinfo}); }])>;
319
320// shl ([asz]ext x), y => zext (shl x, y), if shift does not overflow int
321def reduce_shl_of_extend_matchdata : GIDefMatchData<"RegisterImmPair">;
322def reduce_shl_of_extend : GICombineRule<
323  (defs root:$dst, reduce_shl_of_extend_matchdata:$matchinfo),
324  (match (G_SHL $dst, $src0, $src1):$mi,
325         [{ return Helper.matchCombineShlOfExtend(*${mi}, ${matchinfo}); }]),
326  (apply [{ Helper.applyCombineShlOfExtend(*${mi}, ${matchinfo}); }])>;
327
328// Combine bitreverse(shl (bitreverse x), y)) -> (lshr x, y)
329def bitreverse_shl : GICombineRule<
330  (defs root:$d),
331  (match (G_BITREVERSE $rev, $val),
332         (G_SHL $src, $rev, $amt):$mi,
333         (G_BITREVERSE $d, $src),
334         [{ return Helper.isLegalOrBeforeLegalizer({TargetOpcode::G_LSHR,
335                                                   {MRI.getType(${val}.getReg()),
336                                                    MRI.getType(${amt}.getReg())}}); }]),
337  (apply (G_LSHR $d, $val, $amt))>;
338
339// Combine bitreverse(lshr (bitreverse x), y)) -> (shl x, y)
340def bitreverse_lshr : GICombineRule<
341  (defs root:$d, build_fn_matchinfo:$matchinfo),
342  (match (G_BITREVERSE $rev, $val),
343         (G_LSHR $src, $rev, $amt):$mi,
344         (G_BITREVERSE $d, $src),
345         [{ return Helper.isLegalOrBeforeLegalizer({TargetOpcode::G_SHL,
346                                                   {MRI.getType(${val}.getReg()),
347                                                    MRI.getType(${amt}.getReg())}}); }]),
348  (apply (G_SHL $d, $val, $amt))>;
349
350// Combine (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
351// Combine (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
352def commute_shift : GICombineRule<
353  (defs root:$d, build_fn_matchinfo:$matchinfo),
354  (match (wip_match_opcode G_SHL):$d,
355         [{ return Helper.matchCommuteShift(*${d}, ${matchinfo}); }]),
356  (apply [{ Helper.applyBuildFn(*${d}, ${matchinfo}); }])>;
357
358def narrow_binop_feeding_and : GICombineRule<
359  (defs root:$root, build_fn_matchinfo:$matchinfo),
360  (match (wip_match_opcode G_AND):$root,
361         [{ return Helper.matchNarrowBinopFeedingAnd(*${root}, ${matchinfo}); }]),
362  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
363
364// [us]itofp(undef) = 0, because the result value is bounded.
365def undef_to_fp_zero : GICombineRule<
366  (defs root:$root),
367  (match (wip_match_opcode G_UITOFP, G_SITOFP):$root,
368         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
369  (apply [{ Helper.replaceInstWithFConstant(*${root}, 0.0); }])>;
370
371def undef_to_int_zero: GICombineRule<
372  (defs root:$root),
373  (match (wip_match_opcode G_AND, G_MUL):$root,
374         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
375  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
376
377def undef_to_negative_one: GICombineRule<
378  (defs root:$root),
379  (match (wip_match_opcode G_OR):$root,
380         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
381  (apply [{ Helper.replaceInstWithConstant(*${root}, -1); }])>;
382
383def binop_left_undef_to_zero: GICombineRule<
384  (defs root:$root),
385  (match (wip_match_opcode G_SHL, G_UDIV, G_UREM):$root,
386         [{ return Helper.matchOperandIsUndef(*${root}, 1); }]),
387  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
388
389def binop_right_undef_to_undef: GICombineRule<
390  (defs root:$root),
391  (match (wip_match_opcode G_SHL, G_ASHR, G_LSHR):$root,
392         [{ return Helper.matchOperandIsUndef(*${root}, 2); }]),
393  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
394
395def unary_undef_to_zero: GICombineRule<
396  (defs root:$root),
397  (match (wip_match_opcode G_ABS):$root,
398         [{ return Helper.matchOperandIsUndef(*${root}, 1); }]),
399  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
400
401// Instructions where if any source operand is undef, the instruction can be
402// replaced with undef.
403def propagate_undef_any_op: GICombineRule<
404  (defs root:$root),
405  (match (wip_match_opcode G_ADD, G_FPTOSI, G_FPTOUI, G_SUB, G_XOR, G_TRUNC):$root,
406         [{ return Helper.matchAnyExplicitUseIsUndef(*${root}); }]),
407  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
408
409// Instructions where if all source operands are undef, the instruction can be
410// replaced with undef.
411def propagate_undef_all_ops: GICombineRule<
412  (defs root:$root),
413  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
414          [{ return Helper.matchAllExplicitUsesAreUndef(*${root}); }]),
415  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
416
417// Replace a G_SHUFFLE_VECTOR with an undef mask with a G_IMPLICIT_DEF.
418def propagate_undef_shuffle_mask: GICombineRule<
419  (defs root:$root),
420  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
421         [{ return Helper.matchUndefShuffleVectorMask(*${root}); }]),
422  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
423
424// Replace a G_SHUFFLE_VECTOR with a G_EXTRACT_VECTOR_ELT.
425def shuffle_to_extract: GICombineRule<
426  (defs root:$root),
427  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
428         [{ return Helper.matchShuffleToExtract(*${root}); }]),
429  (apply [{ Helper.applyShuffleToExtract(*${root}); }])>;
430
431  // Replace an insert/extract element of an out of bounds index with undef.
432  def insert_extract_vec_elt_out_of_bounds : GICombineRule<
433  (defs root:$root),
434  (match (wip_match_opcode G_INSERT_VECTOR_ELT, G_EXTRACT_VECTOR_ELT):$root,
435         [{ return Helper.matchInsertExtractVecEltOutOfBounds(*${root}); }]),
436  (apply [{ Helper.replaceInstWithUndef(*${root}); }])>;
437
438// Fold (cond ? x : x) -> x
439def select_same_val: GICombineRule<
440  (defs root:$root),
441  (match (wip_match_opcode G_SELECT):$root,
442    [{ return Helper.matchSelectSameVal(*${root}); }]),
443  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 2); }])
444>;
445
446// Fold (undef ? x : y) -> y
447def select_undef_cmp: GICombineRule<
448  (defs root:$dst),
449  (match (G_IMPLICIT_DEF $undef),
450         (G_SELECT $dst, $undef, $x, $y)),
451  (apply (GIReplaceReg $dst, $y))
452>;
453
454// Fold (true ? x : y) -> x
455// Fold (false ? x : y) -> y
456def select_constant_cmp: GICombineRule<
457  (defs root:$root, unsigned_matchinfo:$matchinfo),
458  (match (wip_match_opcode G_SELECT):$root,
459    [{ return Helper.matchConstantSelectCmp(*${root}, ${matchinfo}); }]),
460  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, ${matchinfo}); }])
461>;
462
463// Fold (C op x) -> (x op C)
464// TODO: handle more isCommutable opcodes
465// TODO: handle compares (currently not marked as isCommutable)
466def commute_int_constant_to_rhs : GICombineRule<
467  (defs root:$root),
468  (match (wip_match_opcode G_ADD, G_MUL, G_AND, G_OR, G_XOR,
469                           G_SMIN, G_SMAX, G_UMIN, G_UMAX, G_UADDO, G_SADDO,
470                           G_UMULO, G_SMULO, G_UMULH, G_SMULH,
471                           G_UADDSAT, G_SADDSAT, G_SMULFIX, G_UMULFIX,
472                           G_SMULFIXSAT, G_UMULFIXSAT):$root,
473    [{ return Helper.matchCommuteConstantToRHS(*${root}); }]),
474  (apply [{ Helper.applyCommuteBinOpOperands(*${root}); }])
475>;
476
477def commute_fp_constant_to_rhs : GICombineRule<
478  (defs root:$root),
479  (match (wip_match_opcode G_FADD, G_FMUL, G_FMINNUM, G_FMAXNUM,
480                           G_FMINNUM_IEEE, G_FMAXNUM_IEEE,
481                           G_FMINIMUM, G_FMAXIMUM):$root,
482    [{ return Helper.matchCommuteFPConstantToRHS(*${root}); }]),
483  (apply [{ Helper.applyCommuteBinOpOperands(*${root}); }])
484>;
485
486def commute_constant_to_rhs : GICombineGroup<[
487  commute_int_constant_to_rhs,
488  commute_fp_constant_to_rhs
489]>;
490
491// Fold x op 0 -> x
492def right_identity_zero_frags : GICombinePatFrag<
493  (outs root:$dst), (ins $x),
494  !foreach(op,
495           [G_SUB, G_ADD, G_OR, G_XOR, G_SHL, G_ASHR,
496            G_LSHR, G_PTR_ADD, G_ROTL, G_ROTR],
497           (pattern (op $dst, $x, 0)))>;
498def right_identity_zero: GICombineRule<
499  (defs root:$dst),
500  (match (right_identity_zero_frags $dst, $lhs)),
501  (apply (GIReplaceReg $dst, $lhs))
502>;
503
504def right_identity_neg_zero_fp: GICombineRule<
505  (defs root:$dst),
506  (match (G_FADD $dst, $x, $y):$root,
507    [{ return Helper.matchConstantFPOp(${y}, -0.0); }]),
508  (apply (GIReplaceReg $dst, $x))
509>;
510
511// Fold x op 1 -> x
512def right_identity_one_int: GICombineRule<
513  (defs root:$dst),
514  (match (G_MUL $dst, $x, 1)),
515  (apply (GIReplaceReg $dst, $x))
516>;
517
518def right_identity_one_fp: GICombineRule<
519  (defs root:$dst),
520  (match (G_FMUL $dst, $x, $y):$root,
521    [{ return Helper.matchConstantFPOp(${y}, 1.0); }]),
522  (apply (GIReplaceReg $dst, $x))
523>;
524
525def right_identity_neg_one_fp: GICombineRule<
526  (defs root:$dst),
527  (match (G_FMUL $dst, $x, $y):$root,
528    [{ return Helper.matchConstantFPOp(${y}, -1.0); }]),
529  (apply (G_FNEG $dst, $x))
530>;
531
532def right_identity_one : GICombineGroup<[right_identity_one_int, right_identity_one_fp]>;
533
534// Fold (x op x) - > x
535def binop_same_val_frags : GICombinePatFrag<
536  (outs root:$dst), (ins $x),
537  [
538    (pattern (G_AND $dst, $x, $x)),
539    (pattern (G_OR $dst, $x, $x)),
540  ]
541>;
542def binop_same_val: GICombineRule<
543  (defs root:$dst),
544  (match (binop_same_val_frags $dst, $src)),
545  (apply (GIReplaceReg $dst, $src))
546>;
547
548// Fold (0 op x) - > 0
549def binop_left_to_zero: GICombineRule<
550  (defs root:$root),
551  (match (wip_match_opcode G_SDIV, G_UDIV, G_SREM, G_UREM):$root,
552    [{ return Helper.matchOperandIsZero(*${root}, 1); }]),
553  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
554>;
555
556def urem_pow2_to_mask : GICombineRule<
557  (defs root:$root),
558  (match (wip_match_opcode G_UREM):$root,
559    [{ return Helper.matchOperandIsKnownToBeAPowerOfTwo(*${root}, 2); }]),
560  (apply [{ Helper.applySimplifyURemByPow2(*${root}); }])
561>;
562
563// Push a binary operator through a select on constants.
564//
565// binop (select cond, K0, K1), K2 ->
566//   select cond, (binop K0, K2), (binop K1, K2)
567
568// Every binary operator that has constant folding. We currently do
569// not have constant folding for G_FPOW, G_FMAXNUM_IEEE or
570// G_FMINNUM_IEEE.
571def fold_binop_into_select : GICombineRule<
572  (defs root:$root, unsigned_matchinfo:$select_op_no),
573  (match (wip_match_opcode
574    G_ADD, G_SUB, G_PTR_ADD, G_AND, G_OR, G_XOR,
575    G_SDIV, G_SREM, G_UDIV, G_UREM, G_LSHR, G_ASHR, G_SHL,
576    G_SMIN, G_SMAX, G_UMIN, G_UMAX,
577    G_FMUL, G_FADD, G_FSUB, G_FDIV, G_FREM,
578    G_FMINNUM, G_FMAXNUM, G_FMINIMUM, G_FMAXIMUM):$root,
579    [{ return Helper.matchFoldBinOpIntoSelect(*${root}, ${select_op_no}); }]),
580  (apply [{ Helper.applyFoldBinOpIntoSelect(*${root}, ${select_op_no}); }])
581>;
582
583// Transform d = [su]div(x, y) and r = [su]rem(x, y) - > d, r = [su]divrem(x, y)
584def div_rem_to_divrem_matchdata : GIDefMatchData<"MachineInstr *">;
585def div_rem_to_divrem : GICombineRule<
586  (defs root:$root, div_rem_to_divrem_matchdata:$matchinfo),
587  (match (wip_match_opcode G_SDIV, G_UDIV, G_SREM, G_UREM):$root,
588    [{ return Helper.matchCombineDivRem(*${root}, ${matchinfo}); }]),
589  (apply [{ Helper.applyCombineDivRem(*${root}, ${matchinfo}); }])
590>;
591
592// Fold (x op 0) - > 0
593def binop_right_to_zero: GICombineRule<
594  (defs root:$dst),
595  (match (G_MUL $dst, $lhs, 0:$zero)),
596  (apply (GIReplaceReg $dst, $zero))
597>;
598
599// Erase stores of undef values.
600def erase_undef_store : GICombineRule<
601  (defs root:$root),
602  (match (wip_match_opcode G_STORE):$root,
603    [{ return Helper.matchUndefStore(*${root}); }]),
604  (apply [{ Helper.eraseInst(*${root}); }])
605>;
606
607def simplify_add_to_sub_matchinfo: GIDefMatchData<"std::tuple<Register, Register>">;
608def simplify_add_to_sub: GICombineRule <
609  (defs root:$root, simplify_add_to_sub_matchinfo:$info),
610  (match (wip_match_opcode G_ADD):$root,
611    [{ return Helper.matchSimplifyAddToSub(*${root}, ${info}); }]),
612  (apply [{ Helper.applySimplifyAddToSub(*${root}, ${info});}])
613>;
614
615// Fold fp_op(cst) to the constant result of the floating point operation.
616class constant_fold_unary_fp_op_rule<Instruction opcode> : GICombineRule <
617  (defs root:$dst),
618  (match (opcode $dst, $src0):$root, (G_FCONSTANT $src0, $cst)),
619  (apply [{ Helper.applyCombineConstantFoldFpUnary(*${root}, ${cst}.getFPImm()); }])
620>;
621
622def constant_fold_fneg : constant_fold_unary_fp_op_rule<G_FNEG>;
623def constant_fold_fabs : constant_fold_unary_fp_op_rule<G_FABS>;
624def constant_fold_fsqrt : constant_fold_unary_fp_op_rule<G_FSQRT>;
625def constant_fold_flog2 : constant_fold_unary_fp_op_rule<G_FLOG2>;
626def constant_fold_fptrunc : constant_fold_unary_fp_op_rule<G_FPTRUNC>;
627
628// Fold constant zero int to fp conversions.
629class itof_const_zero_fold_rule<Instruction opcode> : GICombineRule <
630  (defs root:$dst),
631  (match (opcode $dst, 0)),
632  // Can't use COPY $dst, 0 here because the 0 operand may be a smaller type
633  // than the destination for itofp.
634  (apply [{ Helper.replaceInstWithFConstant(*${dst}.getParent(), 0.0); }])
635>;
636def itof_const_zero_fold_si : itof_const_zero_fold_rule<G_SITOFP>;
637def itof_const_zero_fold_ui : itof_const_zero_fold_rule<G_UITOFP>;
638
639def constant_fold_fp_ops : GICombineGroup<[
640  constant_fold_fneg,
641  constant_fold_fabs,
642  constant_fold_fsqrt,
643  constant_fold_flog2,
644  constant_fold_fptrunc,
645  itof_const_zero_fold_si,
646  itof_const_zero_fold_ui
647]>;
648
649// Fold int2ptr(ptr2int(x)) -> x
650def p2i_to_i2p: GICombineRule<
651  (defs root:$root, register_matchinfo:$info),
652  (match (wip_match_opcode G_INTTOPTR):$root,
653    [{ return Helper.matchCombineI2PToP2I(*${root}, ${info}); }]),
654  (apply [{ Helper.applyCombineI2PToP2I(*${root}, ${info}); }])
655>;
656
657// Fold ptr2int(int2ptr(x)) -> x
658def i2p_to_p2i: GICombineRule<
659  (defs root:$dst, register_matchinfo:$info),
660  (match (G_INTTOPTR $t, $ptr),
661         (G_PTRTOINT $dst, $t):$mi,
662    [{ ${info} = ${ptr}.getReg(); return true; }]),
663  (apply [{ Helper.applyCombineP2IToI2P(*${mi}, ${info}); }])
664>;
665
666// Fold add ptrtoint(x), y -> ptrtoint (ptr_add x), y
667def add_p2i_to_ptradd_matchinfo : GIDefMatchData<"std::pair<Register, bool>">;
668def add_p2i_to_ptradd : GICombineRule<
669  (defs root:$root, add_p2i_to_ptradd_matchinfo:$info),
670  (match (wip_match_opcode G_ADD):$root,
671    [{ return Helper.matchCombineAddP2IToPtrAdd(*${root}, ${info}); }]),
672  (apply [{ Helper.applyCombineAddP2IToPtrAdd(*${root}, ${info}); }])
673>;
674
675// Fold (ptr_add (int2ptr C1), C2) -> C1 + C2
676def const_ptradd_to_i2p: GICombineRule<
677  (defs root:$root, apint_matchinfo:$info),
678  (match (wip_match_opcode G_PTR_ADD):$root,
679    [{ return Helper.matchCombineConstPtrAddToI2P(*${root}, ${info}); }]),
680  (apply [{ Helper.applyCombineConstPtrAddToI2P(*${root}, ${info}); }])
681>;
682
683// Simplify: (logic_op (op x...), (op y...)) -> (op (logic_op x, y))
684def hoist_logic_op_with_same_opcode_hands: GICombineRule <
685  (defs root:$root, instruction_steps_matchdata:$info),
686  (match (wip_match_opcode G_AND, G_OR, G_XOR):$root,
687    [{ return Helper.matchHoistLogicOpWithSameOpcodeHands(*${root}, ${info}); }]),
688  (apply [{ Helper.applyBuildInstructionSteps(*${root}, ${info});}])
689>;
690
691// Fold ashr (shl x, C), C -> sext_inreg (C)
692def shl_ashr_to_sext_inreg_matchinfo : GIDefMatchData<"std::tuple<Register, int64_t>">;
693def shl_ashr_to_sext_inreg : GICombineRule<
694  (defs root:$root, shl_ashr_to_sext_inreg_matchinfo:$info),
695  (match (wip_match_opcode G_ASHR): $root,
696    [{ return Helper.matchAshrShlToSextInreg(*${root}, ${info}); }]),
697  (apply [{ Helper.applyAshShlToSextInreg(*${root}, ${info});}])
698>;
699
700// Fold and(and(x, C1), C2) -> C1&C2 ? and(x, C1&C2) : 0
701def overlapping_and: GICombineRule <
702  (defs root:$root, build_fn_matchinfo:$info),
703  (match (wip_match_opcode G_AND):$root,
704         [{ return Helper.matchOverlappingAnd(*${root}, ${info}); }]),
705  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])
706>;
707
708// Fold (x & y) -> x or (x & y) -> y when (x & y) is known to equal x or equal y.
709def redundant_and: GICombineRule <
710  (defs root:$root, register_matchinfo:$matchinfo),
711  (match (wip_match_opcode G_AND):$root,
712         [{ return Helper.matchRedundantAnd(*${root}, ${matchinfo}); }]),
713  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
714>;
715
716// Fold (x | y) -> x or (x | y) -> y when (x | y) is known to equal x or equal y.
717def redundant_or: GICombineRule <
718  (defs root:$root, register_matchinfo:$matchinfo),
719  (match (wip_match_opcode G_OR):$root,
720         [{ return Helper.matchRedundantOr(*${root}, ${matchinfo}); }]),
721  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
722>;
723
724// If the input is already sign extended, just drop the extension.
725// sext_inreg x, K ->
726//   if computeNumSignBits(x) >= (x.getScalarSizeInBits() - K + 1)
727def redundant_sext_inreg: GICombineRule <
728  (defs root:$root),
729  (match (wip_match_opcode G_SEXT_INREG):$root,
730         [{ return Helper.matchRedundantSExtInReg(*${root}); }]),
731     (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
732>;
733
734// Fold (anyext (trunc x)) -> x if the source type is same as
735// the destination type.
736def anyext_trunc_fold: GICombineRule <
737  (defs root:$root, register_matchinfo:$matchinfo),
738  (match (wip_match_opcode G_ANYEXT):$root,
739         [{ return Helper.matchCombineAnyExtTrunc(*${root}, ${matchinfo}); }]),
740  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
741>;
742
743// Fold (zext (trunc x)) -> x if the source type is same as the destination type
744// and truncated bits are known to be zero.
745def zext_trunc_fold: GICombineRule <
746  (defs root:$root, register_matchinfo:$matchinfo),
747  (match (wip_match_opcode G_ZEXT):$root,
748         [{ return Helper.matchCombineZextTrunc(*${root}, ${matchinfo}); }]),
749  (apply [{ Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }])
750>;
751
752// Fold ([asz]ext ([asz]ext x)) -> ([asz]ext x).
753def ext_ext_fold_matchinfo : GIDefMatchData<"std::tuple<Register, unsigned>">;
754def ext_ext_fold: GICombineRule <
755  (defs root:$root, ext_ext_fold_matchinfo:$matchinfo),
756  (match (wip_match_opcode G_ANYEXT, G_SEXT, G_ZEXT):$root,
757         [{ return Helper.matchCombineExtOfExt(*${root}, ${matchinfo}); }]),
758  (apply [{ Helper.applyCombineExtOfExt(*${root}, ${matchinfo}); }])
759>;
760
761def not_cmp_fold_matchinfo : GIDefMatchData<"SmallVector<Register, 4>">;
762def not_cmp_fold : GICombineRule<
763  (defs root:$d, not_cmp_fold_matchinfo:$info),
764  (match (wip_match_opcode G_XOR): $d,
765  [{ return Helper.matchNotCmp(*${d}, ${info}); }]),
766  (apply [{ Helper.applyNotCmp(*${d}, ${info}); }])
767>;
768
769// Fold (fneg (fneg x)) -> x.
770def fneg_fneg_fold: GICombineRule <
771  (defs root:$dst),
772  (match (G_FNEG $t, $src),
773         (G_FNEG $dst, $t)),
774  (apply (GIReplaceReg $dst, $src))
775>;
776
777// Fold (unmerge(merge x, y, z)) -> z, y, z.
778def unmerge_merge_matchinfo : GIDefMatchData<"SmallVector<Register, 8>">;
779def unmerge_merge : GICombineRule<
780  (defs root:$d, unmerge_merge_matchinfo:$info),
781  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
782  [{ return Helper.matchCombineUnmergeMergeToPlainValues(*${d}, ${info}); }]),
783  (apply [{ Helper.applyCombineUnmergeMergeToPlainValues(*${d}, ${info}); }])
784>;
785
786// Fold merge(unmerge).
787def merge_unmerge : GICombineRule<
788  (defs root:$d, register_matchinfo:$matchinfo),
789  (match (wip_match_opcode G_MERGE_VALUES):$d,
790  [{ return Helper.matchCombineMergeUnmerge(*${d}, ${matchinfo}); }]),
791  (apply [{ Helper.replaceSingleDefInstWithReg(*${d}, ${matchinfo}); }])
792>;
793
794// Fold (fabs (fneg x)) -> (fabs x).
795def fabs_fneg_fold: GICombineRule <
796  (defs root:$dst),
797  (match  (G_FNEG $tmp, $x),
798          (G_FABS $dst, $tmp)),
799  (apply (G_FABS $dst, $x))>;
800
801// Fold (unmerge cst) -> cst1, cst2, ...
802def unmerge_cst_matchinfo : GIDefMatchData<"SmallVector<APInt, 8>">;
803def unmerge_cst : GICombineRule<
804  (defs root:$d, unmerge_cst_matchinfo:$info),
805  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
806  [{ return Helper.matchCombineUnmergeConstant(*${d}, ${info}); }]),
807  (apply [{ Helper.applyCombineUnmergeConstant(*${d}, ${info}); }])
808>;
809
810// Fold (unmerge undef) -> undef, undef, ...
811def unmerge_undef : GICombineRule<
812  (defs root:$root, build_fn_matchinfo:$info),
813  (match (wip_match_opcode G_UNMERGE_VALUES): $root,
814         [{ return Helper.matchCombineUnmergeUndef(*${root}, ${info}); }]),
815  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])
816>;
817
818// Transform x,y<dead> = unmerge z -> x = trunc z.
819def unmerge_dead_to_trunc : GICombineRule<
820  (defs root:$d),
821  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
822  [{ return Helper.matchCombineUnmergeWithDeadLanesToTrunc(*${d}); }]),
823  (apply [{ Helper.applyCombineUnmergeWithDeadLanesToTrunc(*${d}); }])
824>;
825
826// Transform x,y = unmerge(zext(z)) -> x = zext z; y = 0.
827def unmerge_zext_to_zext : GICombineRule<
828  (defs root:$d),
829  (match (wip_match_opcode G_UNMERGE_VALUES): $d,
830  [{ return Helper.matchCombineUnmergeZExtToZExt(*${d}); }]),
831  (apply [{ Helper.applyCombineUnmergeZExtToZExt(*${d}); }])
832>;
833
834// Fold trunc ([asz]ext x) -> x or ([asz]ext x) or (trunc x).
835def trunc_ext_fold_matchinfo : GIDefMatchData<"std::pair<Register, unsigned>">;
836def trunc_ext_fold: GICombineRule <
837  (defs root:$root, trunc_ext_fold_matchinfo:$matchinfo),
838  (match (wip_match_opcode G_TRUNC):$root,
839         [{ return Helper.matchCombineTruncOfExt(*${root}, ${matchinfo}); }]),
840  (apply [{ Helper.applyCombineTruncOfExt(*${root}, ${matchinfo}); }])
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 (wip_match_opcode G_AND):$root,
1022    [{ return Helper.matchBitfieldExtractFromAnd(*${root}, ${info}); }]),
1023  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1024
1025def funnel_shift_combines : GICombineGroup<[funnel_shift_from_or_shift,
1026                                            funnel_shift_to_rotate,
1027                                            funnel_shift_right_zero,
1028                                            funnel_shift_left_zero,
1029                                            funnel_shift_overshift]>;
1030
1031def bitfield_extract_from_sext_inreg : GICombineRule<
1032  (defs root:$root, build_fn_matchinfo:$info),
1033  (match (wip_match_opcode G_SEXT_INREG):$root,
1034    [{ return Helper.matchBitfieldExtractFromSExtInReg(*${root}, ${info}); }]),
1035  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1036
1037def bitfield_extract_from_shr : GICombineRule<
1038  (defs root:$root, build_fn_matchinfo:$info),
1039  (match (wip_match_opcode G_ASHR, G_LSHR):$root,
1040    [{ return Helper.matchBitfieldExtractFromShr(*${root}, ${info}); }]),
1041  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1042
1043def bitfield_extract_from_shr_and : GICombineRule<
1044  (defs root:$root, build_fn_matchinfo:$info),
1045  (match (wip_match_opcode G_ASHR, G_LSHR):$root,
1046    [{ return Helper.matchBitfieldExtractFromShrAnd(*${root}, ${info}); }]),
1047  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1048
1049def form_bitfield_extract : GICombineGroup<[bitfield_extract_from_sext_inreg,
1050                                            bitfield_extract_from_and,
1051                                            bitfield_extract_from_shr,
1052                                            bitfield_extract_from_shr_and]>;
1053
1054def udiv_by_const : GICombineRule<
1055  (defs root:$root),
1056  (match (wip_match_opcode G_UDIV):$root,
1057   [{ return Helper.matchUDivByConst(*${root}); }]),
1058  (apply [{ Helper.applyUDivByConst(*${root}); }])>;
1059
1060def sdiv_by_const : GICombineRule<
1061  (defs root:$root),
1062  (match (wip_match_opcode G_SDIV):$root,
1063   [{ return Helper.matchSDivByConst(*${root}); }]),
1064  (apply [{ Helper.applySDivByConst(*${root}); }])>;
1065
1066def sdiv_by_pow2 : GICombineRule<
1067  (defs root:$root),
1068  (match (G_SDIV $dst, $x, $y, (MIFlags (not IsExact))):$root,
1069   [{ return Helper.matchDivByPow2(*${root}, /*IsSigned=*/true); }]),
1070  (apply [{ Helper.applySDivByPow2(*${root}); }])>;
1071
1072def udiv_by_pow2 : GICombineRule<
1073  (defs root:$root),
1074  (match (G_UDIV $dst, $x, $y, (MIFlags (not IsExact))):$root,
1075   [{ return Helper.matchDivByPow2(*${root}, /*IsSigned=*/false); }]),
1076  (apply [{ Helper.applyUDivByPow2(*${root}); }])>;
1077
1078def intdiv_combines : GICombineGroup<[udiv_by_const, sdiv_by_const,
1079                                      sdiv_by_pow2, udiv_by_pow2]>;
1080
1081def reassoc_ptradd : GICombineRule<
1082  (defs root:$root, build_fn_matchinfo:$matchinfo),
1083  (match (wip_match_opcode G_PTR_ADD):$root,
1084    [{ return Helper.matchReassocPtrAdd(*${root}, ${matchinfo}); }]),
1085  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
1086
1087def reassoc_comm_binops : GICombineRule<
1088  (defs root:$root, build_fn_matchinfo:$matchinfo),
1089  (match (G_ADD $root, $src1, $src2):$root,
1090    [{ return Helper.matchReassocCommBinOp(*${root}, ${matchinfo}); }]),
1091  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1092
1093def reassocs : GICombineGroup<[reassoc_ptradd, reassoc_comm_binops]>;
1094
1095// Constant fold operations.
1096def constant_fold_binop : GICombineRule<
1097  (defs root:$d, apint_matchinfo:$matchinfo),
1098  (match (wip_match_opcode G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SHL, G_LSHR, G_ASHR):$d,
1099   [{ return Helper.matchConstantFoldBinOp(*${d}, ${matchinfo}); }]),
1100  (apply [{ Helper.replaceInstWithConstant(*${d}, ${matchinfo}); }])>;
1101
1102def constant_fold_fp_binop : GICombineRule<
1103  (defs root:$d, constantfp_matchinfo:$matchinfo),
1104  (match (wip_match_opcode G_FADD, G_FSUB, G_FMUL, G_FDIV):$d,
1105   [{ return Helper.matchConstantFoldFPBinOp(*${d}, ${matchinfo}); }]),
1106  (apply [{ Helper.replaceInstWithFConstant(*${d}, ${matchinfo}); }])>;
1107
1108
1109def constant_fold_fma : GICombineRule<
1110  (defs root:$d, constantfp_matchinfo:$matchinfo),
1111  (match (wip_match_opcode G_FMAD, G_FMA):$d,
1112   [{ return Helper.matchConstantFoldFMA(*${d}, ${matchinfo}); }]),
1113  (apply [{ Helper.replaceInstWithFConstant(*${d}, ${matchinfo}); }])>;
1114
1115def constant_fold_cast_op : GICombineRule<
1116  (defs root:$d, apint_matchinfo:$matchinfo),
1117  (match (wip_match_opcode G_ZEXT, G_SEXT, G_ANYEXT):$d,
1118   [{ return Helper.matchConstantFoldCastOp(*${d}, ${matchinfo}); }]),
1119  (apply [{ Helper.replaceInstWithConstant(*${d}, ${matchinfo}); }])>;
1120
1121def mulo_by_2: GICombineRule<
1122  (defs root:$root, build_fn_matchinfo:$matchinfo),
1123  (match (wip_match_opcode G_UMULO, G_SMULO):$root,
1124         [{ return Helper.matchMulOBy2(*${root}, ${matchinfo}); }]),
1125  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
1126
1127def mulo_by_0: GICombineRule<
1128  (defs root:$root, build_fn_matchinfo:$matchinfo),
1129  (match (wip_match_opcode G_UMULO, G_SMULO):$root,
1130         [{ return Helper.matchMulOBy0(*${root}, ${matchinfo}); }]),
1131  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1132
1133// Transform (uadde x, y, 0) -> (uaddo x, y)
1134//           (sadde x, y, 0) -> (saddo x, y)
1135//           (usube x, y, 0) -> (usubo x, y)
1136//           (ssube x, y, 0) -> (ssubo x, y)
1137def adde_to_addo: GICombineRule<
1138  (defs root:$root, build_fn_matchinfo:$matchinfo),
1139  (match (wip_match_opcode G_UADDE, G_SADDE, G_USUBE, G_SSUBE):$root,
1140         [{ return Helper.matchAddEToAddO(*${root}, ${matchinfo}); }]),
1141  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
1142
1143def mulh_to_lshr : GICombineRule<
1144  (defs root:$root),
1145  (match (wip_match_opcode G_UMULH):$root,
1146         [{ return Helper.matchUMulHToLShr(*${root}); }]),
1147  (apply [{ Helper.applyUMulHToLShr(*${root}); }])>;
1148
1149def mulh_combines : GICombineGroup<[mulh_to_lshr]>;
1150
1151def redundant_neg_operands: GICombineRule<
1152  (defs root:$root, build_fn_matchinfo:$matchinfo),
1153  (match (wip_match_opcode G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FMAD, G_FMA):$root,
1154    [{ return Helper.matchRedundantNegOperands(*${root}, ${matchinfo}); }]),
1155  (apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])>;
1156
1157// Transform (fsub +-0.0, X) -> (fneg X)
1158def fsub_to_fneg: GICombineRule<
1159  (defs root:$root, register_matchinfo:$matchinfo),
1160  (match (wip_match_opcode G_FSUB):$root,
1161    [{ return Helper.matchFsubToFneg(*${root}, ${matchinfo}); }]),
1162  (apply [{ Helper.applyFsubToFneg(*${root}, ${matchinfo}); }])>;
1163
1164// Transform (fadd x, (fmul y, z)) -> (fma y, z, x)
1165//           (fadd x, (fmul y, z)) -> (fmad y, z, x)
1166// Transform (fadd (fmul x, y), z) -> (fma x, y, z)
1167//           (fadd (fmul x, y), z) -> (fmad x, y, z)
1168def combine_fadd_fmul_to_fmad_or_fma: GICombineRule<
1169  (defs root:$root, build_fn_matchinfo:$info),
1170  (match (wip_match_opcode G_FADD):$root,
1171         [{ return Helper.matchCombineFAddFMulToFMadOrFMA(*${root},
1172                                                          ${info}); }]),
1173  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1174
1175// Transform (fadd (fpext (fmul x, y)), z) -> (fma (fpext x), (fpext y), z)
1176//                                         -> (fmad (fpext x), (fpext y), z)
1177// Transform (fadd x, (fpext (fmul y, z))) -> (fma (fpext y), (fpext z), x)
1178//                                         -> (fmad (fpext y), (fpext z), x)
1179def combine_fadd_fpext_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.matchCombineFAddFpExtFMulToFMadOrFMA(*${root},
1183                                                               ${info}); }]),
1184  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1185
1186// Transform (fadd (fma x, y, (fmul z, u)), v)  -> (fma x, y, (fma z, u, v))
1187//           (fadd (fmad x, y, (fmul z, u)), v) -> (fmad x, y, (fmad z, u, v))
1188// Transform (fadd v, (fma x, y, (fmul z, u)))  -> (fma x, y, (fma z, u, v))
1189//           (fadd v, (fmad x, y, (fmul z, u))) -> (fmad x, y, (fmad z, u, v))
1190def combine_fadd_fma_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.matchCombineFAddFMAFMulToFMadOrFMA(*${root},
1194                                                             ${info}); }]),
1195  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1196
1197// Transform (fadd (fma x, y, (fpext (fmul u, v))), z) ->
1198//           (fma x, y, (fma (fpext u), (fpext v), z))
1199def combine_fadd_fpext_fma_fmul_to_fmad_or_fma: GICombineRule<
1200  (defs root:$root, build_fn_matchinfo:$info),
1201  (match (wip_match_opcode G_FADD):$root,
1202         [{ return Helper.matchCombineFAddFpExtFMulToFMadOrFMAAggressive(
1203                                                  *${root}, ${info}); }]),
1204  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1205
1206// Transform (fsub (fmul x, y), z) -> (fma x, y, -z)
1207//                                 -> (fmad x, y, -z)
1208def combine_fsub_fmul_to_fmad_or_fma: GICombineRule<
1209  (defs root:$root, build_fn_matchinfo:$info),
1210  (match (wip_match_opcode G_FSUB):$root,
1211         [{ return Helper.matchCombineFSubFMulToFMadOrFMA(*${root},
1212                                                          ${info}); }]),
1213  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1214
1215// Transform (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
1216//           (fsub x, (fneg (fmul, y, z))) -> (fma y, z, x)
1217def combine_fsub_fneg_fmul_to_fmad_or_fma: GICombineRule<
1218  (defs root:$root, build_fn_matchinfo:$info),
1219  (match (wip_match_opcode G_FSUB):$root,
1220         [{ return Helper.matchCombineFSubFNegFMulToFMadOrFMA(*${root},
1221                                                              ${info}); }]),
1222  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1223
1224// Transform (fsub (fpext (fmul x, y)), z) ->
1225//           (fma (fpext x), (fpext y), (fneg z))
1226def combine_fsub_fpext_fmul_to_fmad_or_fma: GICombineRule<
1227  (defs root:$root, build_fn_matchinfo:$info),
1228  (match (wip_match_opcode G_FSUB):$root,
1229         [{ return Helper.matchCombineFSubFpExtFMulToFMadOrFMA(*${root},
1230                                                               ${info}); }]),
1231  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1232
1233// Transform (fsub (fneg (fpext (fmul x, y))), z) ->
1234//           (fneg (fma (fpext x), (fpext y), z))
1235def combine_fsub_fpext_fneg_fmul_to_fmad_or_fma: GICombineRule<
1236  (defs root:$root, build_fn_matchinfo:$info),
1237  (match (wip_match_opcode G_FSUB):$root,
1238         [{ return Helper.matchCombineFSubFpExtFNegFMulToFMadOrFMA(
1239                                            *${root}, ${info}); }]),
1240  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1241
1242def combine_minmax_nan: GICombineRule<
1243  (defs root:$root, unsigned_matchinfo:$info),
1244  (match (wip_match_opcode G_FMINNUM, G_FMAXNUM, G_FMINIMUM, G_FMAXIMUM):$root,
1245         [{ return Helper.matchCombineFMinMaxNaN(*${root}, ${info}); }]),
1246  (apply [{ Helper.replaceSingleDefInstWithOperand(*${root}, ${info}); }])>;
1247
1248// Transform (add x, (sub y, x)) -> y
1249// Transform (add (sub y, x), x) -> y
1250def add_sub_reg_frags : GICombinePatFrag<
1251  (outs root:$dst), (ins $src),
1252  [
1253    (pattern (G_ADD $dst, $x, $tmp), (G_SUB $tmp, $src, $x)),
1254    (pattern (G_ADD $dst, $tmp, $x), (G_SUB $tmp, $src, $x))
1255  ]>;
1256def add_sub_reg: GICombineRule <
1257  (defs root:$dst),
1258  (match (add_sub_reg_frags $dst, $src)),
1259  (apply (GIReplaceReg $dst, $src))>;
1260
1261def buildvector_identity_fold : GICombineRule<
1262  (defs root:$build_vector, register_matchinfo:$matchinfo),
1263  (match (wip_match_opcode G_BUILD_VECTOR_TRUNC, G_BUILD_VECTOR):$build_vector,
1264         [{ return Helper.matchBuildVectorIdentityFold(*${build_vector}, ${matchinfo}); }]),
1265  (apply [{ Helper.replaceSingleDefInstWithReg(*${build_vector}, ${matchinfo}); }])>;
1266
1267def trunc_buildvector_fold : GICombineRule<
1268  (defs root:$op, register_matchinfo:$matchinfo),
1269  (match (wip_match_opcode G_TRUNC):$op,
1270      [{ return Helper.matchTruncBuildVectorFold(*${op}, ${matchinfo}); }]),
1271  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${matchinfo}); }])>;
1272
1273def trunc_lshr_buildvector_fold : GICombineRule<
1274  (defs root:$op, register_matchinfo:$matchinfo),
1275  (match (wip_match_opcode G_TRUNC):$op,
1276      [{ return Helper.matchTruncLshrBuildVectorFold(*${op}, ${matchinfo}); }]),
1277  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${matchinfo}); }])>;
1278
1279// Transform:
1280//   (x + y) - y -> x
1281//   (x + y) - x -> y
1282//   x - (y + x) -> 0 - y
1283//   x - (x + z) -> 0 - z
1284def sub_add_reg: GICombineRule <
1285  (defs root:$root, build_fn_matchinfo:$matchinfo),
1286  (match (wip_match_opcode G_SUB):$root,
1287         [{ return Helper.matchSubAddSameReg(*${root}, ${matchinfo}); }]),
1288  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1289
1290def bitcast_bitcast_fold : GICombineRule<
1291  (defs root:$dst),
1292  (match (G_BITCAST $dst, $src1):$op, (G_BITCAST $src1, $src0),
1293      [{ return MRI.getType(${src0}.getReg()) == MRI.getType(${dst}.getReg()); }]),
1294  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${src0}.getReg()); }])>;
1295
1296
1297def fptrunc_fpext_fold : GICombineRule<
1298  (defs root:$dst),
1299  (match (G_FPTRUNC $dst, $src1):$op, (G_FPEXT $src1, $src0),
1300      [{ return MRI.getType(${src0}.getReg()) == MRI.getType(${dst}.getReg()); }]),
1301  (apply [{ Helper.replaceSingleDefInstWithReg(*${op}, ${src0}.getReg()); }])>;
1302
1303
1304def select_to_minmax: GICombineRule<
1305  (defs root:$root, build_fn_matchinfo:$info),
1306  (match (wip_match_opcode G_SELECT):$root,
1307         [{ return Helper.matchSimplifySelectToMinMax(*${root}, ${info}); }]),
1308  (apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;
1309
1310def match_selects : GICombineRule<
1311  (defs root:$root, build_fn_matchinfo:$matchinfo),
1312  (match (wip_match_opcode G_SELECT):$root,
1313        [{ return Helper.matchSelect(*${root}, ${matchinfo}); }]),
1314  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1315
1316def match_ands : GICombineRule<
1317  (defs root:$root, build_fn_matchinfo:$matchinfo),
1318  (match (wip_match_opcode G_AND):$root,
1319        [{ return Helper.matchAnd(*${root}, ${matchinfo}); }]),
1320  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1321
1322def match_ors : GICombineRule<
1323  (defs root:$root, build_fn_matchinfo:$matchinfo),
1324  (match (wip_match_opcode G_OR):$root,
1325        [{ return Helper.matchOr(*${root}, ${matchinfo}); }]),
1326  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1327
1328def match_addos : GICombineRule<
1329  (defs root:$root, build_fn_matchinfo:$matchinfo),
1330  (match (wip_match_opcode G_SADDO, G_UADDO):$root,
1331        [{ return Helper.matchAddOverflow(*${root}, ${matchinfo}); }]),
1332  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1333
1334def match_extract_of_element_undef_vector: GICombineRule <
1335  (defs root:$root),
1336  (match (G_IMPLICIT_DEF $vector),
1337         (G_EXTRACT_VECTOR_ELT $root, $vector, $idx)),
1338  (apply (G_IMPLICIT_DEF $root))
1339>;
1340
1341def match_extract_of_element_undef_index: GICombineRule <
1342  (defs root:$root),
1343  (match (G_IMPLICIT_DEF $idx),
1344         (G_EXTRACT_VECTOR_ELT $root, $vector, $idx)),
1345  (apply (G_IMPLICIT_DEF $root))
1346>;
1347
1348def match_extract_of_element : GICombineRule<
1349  (defs root:$root, build_fn_matchinfo:$matchinfo),
1350  (match (wip_match_opcode G_EXTRACT_VECTOR_ELT):$root,
1351        [{ return Helper.matchExtractVectorElement(*${root}, ${matchinfo}); }]),
1352  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1353
1354def extract_vector_element_not_const : GICombineRule<
1355   (defs root:$root, build_fn_matchinfo:$matchinfo),
1356   (match (G_INSERT_VECTOR_ELT $src, $x, $value, $idx),
1357          (G_EXTRACT_VECTOR_ELT $root, $src, $idx)),
1358   (apply (GIReplaceReg $root, $value))>;
1359
1360def extract_vector_element_different_indices : GICombineRule<
1361   (defs root:$root, build_fn_matchinfo:$matchinfo),
1362   (match (G_INSERT_VECTOR_ELT $src, $x, $value, $idx2),
1363          (G_EXTRACT_VECTOR_ELT $root, $src, $idx1),
1364   [{ return Helper.matchExtractVectorElementWithDifferentIndices(${root}, ${matchinfo}); }]),
1365   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1366
1367def extract_vector_element_build_vector2 : GICombineRule<
1368   (defs root:$root, build_fn_matchinfo:$matchinfo),
1369   (match (G_BUILD_VECTOR $src, $x, $y),
1370          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1371   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1372   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1373
1374def extract_vector_element_build_vector3 : GICombineRule<
1375   (defs root:$root, build_fn_matchinfo:$matchinfo),
1376   (match (G_BUILD_VECTOR $src, $x, $y, $z),
1377          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1378   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1379   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1380
1381def extract_vector_element_build_vector4 : GICombineRule<
1382   (defs root:$root, build_fn_matchinfo:$matchinfo),
1383   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a),
1384          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1385   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1386   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1387
1388def extract_vector_element_build_vector5 : GICombineRule<
1389   (defs root:$root, build_fn_matchinfo:$matchinfo),
1390   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b),
1391          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1392   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1393   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1394
1395def extract_vector_element_build_vector6 : GICombineRule<
1396   (defs root:$root, build_fn_matchinfo:$matchinfo),
1397   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c),
1398          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1399   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1400   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1401
1402def extract_vector_element_build_vector7 : GICombineRule<
1403   (defs root:$root, build_fn_matchinfo:$matchinfo),
1404   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d),
1405          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1406   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1407   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1408
1409def extract_vector_element_build_vector8 : GICombineRule<
1410   (defs root:$root, build_fn_matchinfo:$matchinfo),
1411   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e),
1412          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1413   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1414   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1415
1416def extract_vector_element_build_vector9 : GICombineRule<
1417   (defs root:$root, build_fn_matchinfo:$matchinfo),
1418   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f),
1419          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1420   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1421   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1422
1423def extract_vector_element_build_vector10 : GICombineRule<
1424   (defs root:$root, build_fn_matchinfo:$matchinfo),
1425   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g),
1426          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1427   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1428   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1429
1430def extract_vector_element_build_vector11 : GICombineRule<
1431   (defs root:$root, build_fn_matchinfo:$matchinfo),
1432   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h),
1433          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1434   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1435   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1436
1437def extract_vector_element_build_vector12 : GICombineRule<
1438   (defs root:$root, build_fn_matchinfo:$matchinfo),
1439   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i),
1440          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1441   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1442   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1443
1444def extract_vector_element_build_vector13 : GICombineRule<
1445   (defs root:$root, build_fn_matchinfo:$matchinfo),
1446   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j),
1447          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1448   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1449   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1450
1451def extract_vector_element_build_vector14 : GICombineRule<
1452   (defs root:$root, build_fn_matchinfo:$matchinfo),
1453   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k),
1454          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1455   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1456   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1457
1458def extract_vector_element_build_vector15 : GICombineRule<
1459   (defs root:$root, build_fn_matchinfo:$matchinfo),
1460   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l),
1461          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1462   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1463   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1464
1465def extract_vector_element_build_vector16 : GICombineRule<
1466   (defs root:$root, build_fn_matchinfo:$matchinfo),
1467   (match (G_BUILD_VECTOR $src, $x, $y, $z, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m),
1468          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1469   [{ return Helper.matchExtractVectorElementWithBuildVector(${root}, ${matchinfo}); }]),
1470   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1471
1472def extract_vector_element_build_vector_trunc2 : GICombineRule<
1473   (defs root:$root, build_fn_matchinfo:$matchinfo),
1474   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y),
1475          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1476   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1477   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1478
1479def extract_vector_element_build_vector_trunc3 : GICombineRule<
1480   (defs root:$root, build_fn_matchinfo:$matchinfo),
1481   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z),
1482          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1483   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1484   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1485
1486def extract_vector_element_build_vector_trunc4 : GICombineRule<
1487   (defs root:$root, build_fn_matchinfo:$matchinfo),
1488   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a),
1489          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1490   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1491   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1492
1493def extract_vector_element_build_vector_trunc5 : GICombineRule<
1494   (defs root:$root, build_fn_matchinfo:$matchinfo),
1495   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a, $b),
1496          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1497   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1498   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1499
1500def extract_vector_element_build_vector_trunc6 : GICombineRule<
1501   (defs root:$root, build_fn_matchinfo:$matchinfo),
1502   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a, $b, $c),
1503          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1504   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1505   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1506
1507def extract_vector_element_build_vector_trunc7 : GICombineRule<
1508   (defs root:$root, build_fn_matchinfo:$matchinfo),
1509   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a, $b, $c, $d),
1510          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1511   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1512   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1513
1514def extract_vector_element_build_vector_trunc8 : GICombineRule<
1515   (defs root:$root, build_fn_matchinfo:$matchinfo),
1516   (match (G_BUILD_VECTOR_TRUNC $src, $x, $y, $z, $a, $b, $c, $d, $e),
1517          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1518   [{ return Helper.matchExtractVectorElementWithBuildVectorTrunc(${root}, ${matchinfo}); }]),
1519   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1520
1521def extract_vector_element_freeze : GICombineRule<
1522   (defs root:$root, build_fn_matchinfo:$matchinfo),
1523   (match (G_FREEZE $src, $input),
1524          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1525   [{ return Helper.matchExtractVectorElementWithFreeze(${root}, ${matchinfo}); }]),
1526   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1527
1528def sext_trunc : GICombineRule<
1529   (defs root:$root, build_fn_matchinfo:$matchinfo),
1530   (match (G_TRUNC $src, $x, (MIFlags NoSWrap)),
1531          (G_SEXT $root, $src),
1532   [{ return Helper.matchSextOfTrunc(${root}, ${matchinfo}); }]),
1533   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1534
1535def zext_trunc : GICombineRule<
1536   (defs root:$root, build_fn_matchinfo:$matchinfo),
1537   (match (G_TRUNC $src, $x, (MIFlags NoUWrap)),
1538          (G_ZEXT $root, $src),
1539   [{ return Helper.matchZextOfTrunc(${root}, ${matchinfo}); }]),
1540   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1541
1542def extract_vector_element_shuffle_vector : GICombineRule<
1543   (defs root:$root, build_fn_matchinfo:$matchinfo),
1544   (match (G_SHUFFLE_VECTOR $src, $src1, $src2, $mask),
1545          (G_EXTRACT_VECTOR_ELT $root, $src, $idx),
1546   [{ return Helper.matchExtractVectorElementWithShuffleVector(${root}, ${matchinfo}); }]),
1547   (apply [{ Helper.applyBuildFnMO(${root}, ${matchinfo}); }])>;
1548
1549// Combines concat operations
1550def concat_matchinfo : GIDefMatchData<"SmallVector<Register>">;
1551def combine_concat_vector : GICombineRule<
1552  (defs root:$root, concat_matchinfo:$matchinfo),
1553  (match (wip_match_opcode G_CONCAT_VECTORS):$root,
1554        [{ return Helper.matchCombineConcatVectors(*${root}, ${matchinfo}); }]),
1555  (apply [{ Helper.applyCombineConcatVectors(*${root}, ${matchinfo}); }])>;
1556
1557// Combines Shuffles of Concats
1558// a = G_CONCAT_VECTORS x, y, undef, undef
1559// b = G_CONCAT_VECTORS z, undef, undef, undef
1560// c = G_SHUFFLE_VECTORS a, b, <0, 1, 4, undef>
1561// ===>
1562// c = G_CONCAT_VECTORS x, y, z, undef
1563def combine_shuffle_concat : GICombineRule<
1564  (defs root:$root, concat_matchinfo:$matchinfo),
1565  (match (wip_match_opcode G_SHUFFLE_VECTOR):$root,
1566        [{ return Helper.matchCombineShuffleConcat(*${root}, ${matchinfo}); }]),
1567  (apply [{ Helper.applyCombineShuffleConcat(*${root}, ${matchinfo}); }])>;
1568
1569def insert_vector_element_idx_undef : GICombineRule<
1570   (defs root:$root, build_fn_matchinfo:$matchinfo),
1571   (match (G_IMPLICIT_DEF $idx),
1572          (G_INSERT_VECTOR_ELT $root, $src, $elt, $idx)),
1573   (apply (G_IMPLICIT_DEF $root))>;
1574
1575def insert_vector_element_elt_undef : GICombineRule<
1576   (defs root:$root, build_fn_matchinfo:$matchinfo),
1577   (match (G_IMPLICIT_DEF $elt),
1578          (G_INSERT_VECTOR_ELT $root, $src, $elt, $idx),
1579          [{ return isGuaranteedNotToBePoison(${src}.getReg(), MRI); }]),
1580   (apply (GIReplaceReg $root, $src))>;
1581
1582def insert_vector_element_extract_vector_element : GICombineRule<
1583   (defs root:$root, build_fn_matchinfo:$matchinfo),
1584   (match (G_EXTRACT_VECTOR_ELT $elt, $src, $idx),
1585          (G_INSERT_VECTOR_ELT $root, $src, $elt, $idx)),
1586   (apply (GIReplaceReg $root, $src))>;
1587
1588def insert_vector_elt_oob : GICombineRule<
1589  (defs root:$root, build_fn_matchinfo:$matchinfo),
1590  (match (wip_match_opcode G_INSERT_VECTOR_ELT):$root,
1591         [{ return Helper.matchInsertVectorElementOOB(*${root}, ${matchinfo}); }]),
1592  (apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
1593
1594// match_extract_of_element and insert_vector_elt_oob must be the first!
1595def vector_ops_combines: GICombineGroup<[
1596match_extract_of_element_undef_vector,
1597match_extract_of_element_undef_index,
1598insert_vector_element_idx_undef,
1599insert_vector_element_elt_undef,
1600match_extract_of_element,
1601insert_vector_elt_oob,
1602extract_vector_element_not_const,
1603extract_vector_element_different_indices,
1604extract_vector_element_build_vector2,
1605extract_vector_element_build_vector3,
1606extract_vector_element_build_vector4,
1607extract_vector_element_build_vector5,
1608extract_vector_element_build_vector7,
1609extract_vector_element_build_vector8,
1610extract_vector_element_build_vector9,
1611extract_vector_element_build_vector10,
1612extract_vector_element_build_vector11,
1613extract_vector_element_build_vector12,
1614extract_vector_element_build_vector13,
1615extract_vector_element_build_vector14,
1616extract_vector_element_build_vector15,
1617extract_vector_element_build_vector16,
1618extract_vector_element_build_vector_trunc2,
1619extract_vector_element_build_vector_trunc3,
1620extract_vector_element_build_vector_trunc4,
1621extract_vector_element_build_vector_trunc5,
1622extract_vector_element_build_vector_trunc6,
1623extract_vector_element_build_vector_trunc7,
1624extract_vector_element_build_vector_trunc8,
1625extract_vector_element_freeze,
1626extract_vector_element_shuffle_vector,
1627insert_vector_element_extract_vector_element
1628]>;
1629
1630// FIXME: These should use the custom predicate feature once it lands.
1631def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero,
1632                                     undef_to_negative_one,
1633                                     binop_left_undef_to_zero,
1634                                     binop_right_undef_to_undef,
1635                                     unary_undef_to_zero,
1636                                     propagate_undef_any_op,
1637                                     propagate_undef_all_ops,
1638                                     propagate_undef_shuffle_mask,
1639                                     erase_undef_store,
1640                                     unmerge_undef,
1641                                     insert_extract_vec_elt_out_of_bounds]>;
1642
1643def identity_combines : GICombineGroup<[select_same_val, right_identity_zero,
1644                                        binop_same_val, binop_left_to_zero,
1645                                        binop_right_to_zero, p2i_to_i2p,
1646                                        i2p_to_p2i, anyext_trunc_fold,
1647                                        fneg_fneg_fold, right_identity_one,
1648                                        add_sub_reg, buildvector_identity_fold,
1649                                        trunc_buildvector_fold,
1650                                        trunc_lshr_buildvector_fold,
1651                                        bitcast_bitcast_fold, fptrunc_fpext_fold,
1652                                        right_identity_neg_zero_fp,
1653                                        right_identity_neg_one_fp]>;
1654
1655def const_combines : GICombineGroup<[constant_fold_fp_ops, const_ptradd_to_i2p,
1656                                     overlapping_and, mulo_by_2, mulo_by_0,
1657                                     adde_to_addo,
1658                                     combine_minmax_nan]>;
1659
1660def known_bits_simplifications : GICombineGroup<[
1661  redundant_and, redundant_sext_inreg, redundant_or, urem_pow2_to_mask,
1662  zext_trunc_fold, icmp_to_true_false_known_bits, icmp_to_lhs_known_bits,
1663  sext_inreg_to_zext_inreg]>;
1664
1665def width_reduction_combines : GICombineGroup<[reduce_shl_of_extend,
1666                                               narrow_binop_feeding_and]>;
1667
1668def phi_combines : GICombineGroup<[extend_through_phis]>;
1669
1670def bitreverse_shift : GICombineGroup<[bitreverse_shl, bitreverse_lshr]>;
1671
1672def select_combines : GICombineGroup<[select_undef_cmp, select_constant_cmp,
1673                                      match_selects]>;
1674
1675def trivial_combines : GICombineGroup<[copy_prop, mul_to_shl, add_p2i_to_ptradd,
1676                                       mul_by_neg_one, idempotent_prop]>;
1677
1678def fma_combines : GICombineGroup<[combine_fadd_fmul_to_fmad_or_fma,
1679  combine_fadd_fpext_fmul_to_fmad_or_fma, combine_fadd_fma_fmul_to_fmad_or_fma,
1680  combine_fadd_fpext_fma_fmul_to_fmad_or_fma, combine_fsub_fmul_to_fmad_or_fma,
1681  combine_fsub_fneg_fmul_to_fmad_or_fma, combine_fsub_fpext_fmul_to_fmad_or_fma,
1682  combine_fsub_fpext_fneg_fmul_to_fmad_or_fma]>;
1683
1684def constant_fold_binops : GICombineGroup<[constant_fold_binop,
1685                                           constant_fold_fp_binop]>;
1686
1687def all_combines : GICombineGroup<[trivial_combines, vector_ops_combines,
1688    insert_vec_elt_combines, extract_vec_elt_combines, combines_for_extload,
1689    combine_extracted_vector_load,
1690    undef_combines, identity_combines, phi_combines,
1691    simplify_add_to_sub, hoist_logic_op_with_same_opcode_hands, shifts_too_big,
1692    reassocs, ptr_add_immed_chain,
1693    shl_ashr_to_sext_inreg, sext_inreg_of_load,
1694    width_reduction_combines, select_combines,
1695    known_bits_simplifications, ext_ext_fold,
1696    not_cmp_fold, opt_brcond_by_inverting_cond,
1697    unmerge_merge, unmerge_cst, unmerge_dead_to_trunc,
1698    unmerge_zext_to_zext, merge_unmerge, trunc_ext_fold, trunc_shift,
1699    const_combines, xor_of_and_with_same_reg, ptr_add_with_zero,
1700    shift_immed_chain, shift_of_shifted_logic_chain, load_or_combine,
1701    div_rem_to_divrem, funnel_shift_combines, bitreverse_shift, commute_shift,
1702    form_bitfield_extract, constant_fold_binops, constant_fold_fma,
1703    constant_fold_cast_op, fabs_fneg_fold,
1704    intdiv_combines, mulh_combines, redundant_neg_operands,
1705    and_or_disjoint_mask, fma_combines, fold_binop_into_select,
1706    sub_add_reg, select_to_minmax, redundant_binop_in_equality,
1707    fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
1708    combine_concat_vector, double_icmp_zero_and_or_combine, match_addos,
1709    sext_trunc, zext_trunc, combine_shuffle_concat]>;
1710
1711// A combine group used to for prelegalizer combiners at -O0. The combines in
1712// this group have been selected based on experiments to balance code size and
1713// compile time performance.
1714def optnone_combines : GICombineGroup<[trivial_combines,
1715    ptr_add_immed_chain, combines_for_extload,
1716    not_cmp_fold, opt_brcond_by_inverting_cond, combine_concat_vector]>;
1717