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