Skip to content

[GlobalISel] Combine (X == 0) & (Y == 0) -> (X | Y) == 0 #71949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion llvm/include/llvm/Target/GlobalISel/Combine.td
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,37 @@ def redundant_binop_in_equality : GICombineRule<
[{ return Helper.matchRedundantBinOpInEquality(*${root}, ${info}); }]),
(apply [{ Helper.applyBuildFn(*${root}, ${info}); }])>;

// Transform: (X == 0 & Y == 0) -> (X | Y) == 0
def double_icmp_zero_and_combine: GICombineRule<
(defs root:$root),
(match (G_ICMP $d1, $p, $s1, 0),
(G_ICMP $d2, $p, $s2, 0),
(G_AND $root, $d1, $d2),
[{ return ${p}.getPredicate() == CmpInst::ICMP_EQ &&
!MRI.getType(${s1}.getReg()).getScalarType().isPointer() &&
(MRI.getType(${s1}.getReg()) ==
MRI.getType(${s2}.getReg())); }]),
Comment on lines +963 to +964
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to match this with GITypeOf<"$s1"> instead of using the manual predicate.

@Pierre-vh is there a nicer way to filter out pointers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this error when using it
error: GISpecialType is not supported in 'match' patterns

Maybe its only usable in the apply part atm.

(apply (G_OR $ordst, $s1, $s2),
(G_ICMP $root, $p, $ordst, 0))
>;

// Transform: (X != 0 | Y != 0) -> (X | Y) != 0
def double_icmp_zero_or_combine: GICombineRule<
(defs root:$root),
(match (G_ICMP $d1, $p, $s1, 0),
(G_ICMP $d2, $p, $s2, 0),
(G_OR $root, $d1, $d2),
[{ return ${p}.getPredicate() == CmpInst::ICMP_NE &&
!MRI.getType(${s1}.getReg()).getScalarType().isPointer() &&
(MRI.getType(${s1}.getReg()) ==
MRI.getType(${s2}.getReg())); }]),
(apply (G_OR $ordst, $s1, $s2),
(G_ICMP $root, $p, $ordst, 0))
>;

def double_icmp_zero_and_or_combine : GICombineGroup<[double_icmp_zero_and_combine,
double_icmp_zero_or_combine]>;

def and_or_disjoint_mask : GICombineRule<
(defs root:$root, build_fn_matchinfo:$info),
(match (wip_match_opcode G_AND):$root,
Expand Down Expand Up @@ -1343,7 +1374,7 @@ def all_combines : GICombineGroup<[trivial_combines, insert_vec_elt_combines,
and_or_disjoint_mask, fma_combines, fold_binop_into_select,
sub_add_reg, select_to_minmax, redundant_binop_in_equality,
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
combine_concat_vector]>;
combine_concat_vector, double_icmp_zero_and_or_combine]>;

// A combine group used to for prelegalizer combiners at -O0. The combines in
// this group have been selected based on experiments to balance code size and
Expand Down
Loading