diff --git a/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h b/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h --- a/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h +++ b/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h @@ -272,6 +272,9 @@ /// Transform fneg(fneg(x)) to x. bool matchCombineFNegOfFNeg(MachineInstr &MI, Register &Reg); + /// Match fabs(fabs(x)) to fabs(x). + bool matchCombineFAbsOfFAbs(MachineInstr &MI, Register &Src); + /// Return true if any explicit use operand on \p MI is defined by a /// G_IMPLICIT_DEF. bool matchAnyExplicitUseIsUndef(MachineInstr &MI); @@ -302,6 +305,9 @@ /// Replace an instruction with a G_IMPLICIT_DEF. bool replaceInstWithUndef(MachineInstr &MI); + /// Replace an instruction with a copy from the provided \p Src. + bool replaceInstWithCopy(MachineInstr &MI, Register &Src); + /// Delete \p MI and replace all of its uses with its \p OpIdx-th operand. bool replaceSingleDefInstWithOperand(MachineInstr &MI, unsigned OpIdx); diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td --- a/llvm/include/llvm/Target/GlobalISel/Combine.td +++ b/llvm/include/llvm/Target/GlobalISel/Combine.td @@ -394,6 +394,15 @@ (apply [{ return Helper.replaceSingleDefInstWithReg(*${root}, ${matchinfo}); }]) >; +// Fold (fabs (fabs x)) -> (fabs x). +def fabs_fabs_fold_matchinfo : GIDefMatchData<"Register">; +def fabs_fabs_fold: GICombineRule< + (defs root:$root, fabs_fabs_fold_matchinfo:$matchinfo), + (match (wip_match_opcode G_FABS):$root, + [{ return Helper.matchCombineFAbsOfFAbs(*${root}, ${matchinfo}); }]), + (apply [{ return Helper.replaceInstWithCopy(*${root}, ${matchinfo}); }]) +>; + // FIXME: These should use the custom predicate feature once it lands. def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero, undef_to_negative_one, @@ -424,4 +433,5 @@ shl_ashr_to_sext_inreg, sext_inreg_of_load, width_reduction_combines, select_combines, known_bits_simplifications, ext_ext_fold, - not_cmp_fold, opt_brcond_by_inverting_cond]>; + not_cmp_fold, opt_brcond_by_inverting_cond, + fabs_fabs_fold]>; diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp --- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp +++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp @@ -1819,6 +1819,13 @@ return mi_match(SrcReg, MRI, m_GFNeg(m_Reg(Reg))); } +bool CombinerHelper::matchCombineFAbsOfFAbs(MachineInstr &MI, Register &Src) { + assert(MI.getOpcode() == TargetOpcode::G_FABS && "Expected a G_FABS"); + Src = MI.getOperand(1).getReg(); + Register AbsSrc; + return mi_match(Src, MRI, m_GFabs(m_Reg(AbsSrc))); +} + bool CombinerHelper::matchAnyExplicitUseIsUndef(MachineInstr &MI) { return any_of(MI.explicit_uses(), [this](const MachineOperand &MO) { return MO.isReg() && @@ -2017,6 +2024,14 @@ return true; } +bool CombinerHelper::replaceInstWithCopy(MachineInstr &MI, Register &Src) { + assert(MI.getNumDefs() == 1 && "Expected only one def?"); + Builder.setInstrAndDebugLoc(MI); + Builder.buildCopy(MI.getOperand(0), Src); + MI.eraseFromParent(); + return true; +} + bool CombinerHelper::matchSimplifyAddToSub( MachineInstr &MI, std::tuple &MatchInfo) { Register LHS = MI.getOperand(1).getReg(); diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-fabs.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-fabs.mir new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-fabs.mir @@ -0,0 +1,32 @@ +# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py +# RUN: llc -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs -mtriple aarch64-unknown-unknown %s -o - | FileCheck %s +# RUN: llc -debugify-and-strip-all-safe -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs -mtriple aarch64-unknown-unknown %s -o - | FileCheck %s + +--- +name: test_combine_fabs_fabs +body: | + bb.1: + liveins: $w0 + ; CHECK-LABEL: name: test_combine_fabs_fabs + ; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $w0 + ; CHECK: [[FABS:%[0-9]+]]:_(s32) = G_FABS [[COPY]] + ; CHECK: $w0 = COPY [[FABS]](s32) + %0:_(s32) = COPY $w0 + %1:_(s32) = G_FABS %0(s32) + %2:_(s32) = G_FABS %1(s32) + $w0 = COPY %2(s32) +... +--- +name: test_combine_fabs_fabs_vec +body: | + bb.1: + liveins: $x0 + ; CHECK-LABEL: name: test_combine_fabs_fabs_vec + ; CHECK: [[COPY:%[0-9]+]]:_(<2 x s32>) = COPY $x0 + ; CHECK: [[FABS:%[0-9]+]]:_(<2 x s32>) = G_FABS [[COPY]] + ; CHECK: $x0 = COPY [[FABS]](<2 x s32>) + %0:_(<2 x s32>) = COPY $x0 + %1:_(<2 x s32>) = G_FABS %0(<2 x s32>) + %2:_(<2 x s32>) = G_FABS %1(<2 x s32>) + $x0 = COPY %2(<2 x s32>) +...