http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57755
Bug ID: 57755 Summary: Improve fold_binary_op_with_conditional_arg Product: gcc Version: 4.9.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org Hello, fold_binary_op_with_conditional_arg performs the following: Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'. Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. It gives up in this first case (arg is 'a' above): if (!TREE_CONSTANT (arg) && (TREE_SIDE_EFFECTS (arg) || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value))) return NULL_TREE; and after folding both branches: if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs)) return NULL_TREE; This seems suboptimal. On the one hand, for ((a<b)?a:c)*3/2+1, it distributes the operations to a < b ? (a * 3) / 2 + 1 : (c * 3) / 2 + 1 (we can add as many operations with constants as we want) and this isn't completely undone later (partially with -Os, not at all with -O3). On the other hand, for ((a<2)?-1u:0)&b, it gives up instead of producing (a<2)?b:0. We must be careful with recursions (PR55219) and with folders performing the reverse transformations, but I think we should be able to optimize: (((a<2)?-1:0)&((b<1)?-1:0))!=0 (obviously, the title doesn't prevent from moving this functionality to gimple)