From 698e0ec89bc0960e074d2222208bffe47f5addd9 Mon Sep 17 00:00:00 2001 From: Andrew Pinski Date: Mon, 30 Sep 2024 16:44:44 +0000 Subject: phi-opt: Improve factor heurstic with constants and conversions from bool [PR116890] Take: ``` if (t_3(D) != 0) goto ; else goto ; _8 = c_4(D) != 0; _9 = (int) _8; # e_2 = PHI <_9(3), 0(2)> ``` We should factor out the conversion here as that will allow a simplfication to `(t_3 != 0) & (c_4 != 0)`. Unlike most other types; `a ? b : CST` will simplify for boolean result type to either `a | b` or `a & b` so allowing this conversion for all operations will be always profitable. Bootstrapped and tested on x86_64-linux-gnu with no regressions. Note on the phi-opt-7.c testcase change, we are now able to optimize this and remove the if due to the factoring out now so this is an improvement. PR tree-optimization/116890 gcc/ChangeLog: * tree-ssa-phiopt.cc (factor_out_conditional_operation): Conversions from bool is also should be considered as wanting to happen. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-7.c: Update testcase for no ifs left. * gcc.dg/tree-ssa/phi-opt-42.c: New test. * gcc.dg/tree-ssa/phi-opt-43.c: New test. Signed-off-by: Andrew Pinski --- gcc/tree-ssa-phiopt.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'gcc/tree-ssa-phiopt.cc') diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index d43832b..bd7f960 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -345,9 +345,17 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, if (gassign *assign = dyn_cast (stmt)) { tree lhs = gimple_assign_lhs (assign); + tree lhst = TREE_TYPE (lhs); enum tree_code ass_code = gimple_assign_rhs_code (assign); - if (ass_code != MAX_EXPR && ass_code != MIN_EXPR) + if (ass_code != MAX_EXPR && ass_code != MIN_EXPR + /* Conversions from boolean like types is ok + as `a?1:b` and `a?0:b` will always simplify + to `a & b` or `a | b`. + See PR 116890. */ + && !(INTEGRAL_TYPE_P (lhst) + && TYPE_UNSIGNED (lhst) + && TYPE_PRECISION (lhst) == 1)) return NULL; if (lhs != gimple_assign_rhs1 (arg0_def_stmt)) return NULL; -- cgit v1.1