Skip to content

Commit d9a2554

Browse files
committed
GlobalISel: neg (and x, 1) --> SIGN_EXTEND_INREG x, 1
1 parent 3fcd921 commit d9a2554

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

llvm/include/llvm/Target/GlobalISel/Combine.td

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,16 @@ def shl_ashr_to_sext_inreg : GICombineRule<
747747
(apply [{ Helper.applyAshShlToSextInreg(*${root}, ${info});}])
748748
>;
749749

750+
// Fold sub 0, (and x, 1) -> sext_inreg x, 1
751+
def neg_and_one_to_sext_inreg : GICombineRule<
752+
(defs root:$dst),
753+
(match (G_AND $and, $x, 1),
754+
(G_SUB $dst, 0, $and),
755+
[{ return Helper.isLegalOrBeforeLegalizer(
756+
{TargetOpcode::G_SEXT_INREG, {MRI.getType(${x}.getReg())}}); }]),
757+
(apply (G_SEXT_INREG $dst, $x, 1))
758+
>;
759+
750760
// Fold and(and(x, C1), C2) -> C1&C2 ? and(x, C1&C2) : 0
751761
def overlapping_and: GICombineRule <
752762
(defs root:$root, build_fn_matchinfo:$info),
@@ -2013,7 +2023,7 @@ def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
20132023
undef_combines, identity_combines, phi_combines,
20142024
simplify_add_to_sub, hoist_logic_op_with_same_opcode_hands, shifts_too_big,
20152025
reassocs, ptr_add_immed_chain, cmp_combines,
2016-
shl_ashr_to_sext_inreg, sext_inreg_of_load,
2026+
shl_ashr_to_sext_inreg, neg_and_one_to_sext_inreg, sext_inreg_of_load,
20172027
width_reduction_combines, select_combines,
20182028
known_bits_simplifications, trunc_shift,
20192029
not_cmp_fold, opt_brcond_by_inverting_cond,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -o - -mtriple aarch64-- -run-pass=aarch64-prelegalizer-combiner %s | FileCheck %s
3+
---
4+
name: test_combine_neg_and_one_to_sext_inreg
5+
body: |
6+
bb.1:
7+
liveins: $w0
8+
; CHECK-LABEL: name: test_combine_neg_and_one_to_sext_inreg
9+
; CHECK: liveins: $w0
10+
; CHECK-NEXT: {{ $}}
11+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $w0
12+
; CHECK-NEXT: [[SEXT_INREG:%[0-9]+]]:_(s32) = G_SEXT_INREG [[COPY]], 1
13+
; CHECK-NEXT: $w0 = COPY [[SEXT_INREG]](s32)
14+
%0:_(s32) = COPY $w0
15+
%1:_(s32) = G_CONSTANT i32 1
16+
%3:_(s32) = G_CONSTANT i32 0
17+
%2:_(s32) = G_AND %0:_, %1:_
18+
%4:_(s32) = G_SUB %3:_, %2:_
19+
$w0 = COPY %4:_(s32)
20+
...

0 commit comments

Comments
 (0)