On Tue, Apr 05, 2022 at 03:07:53PM +0200, Richard Biener wrote: > The following avoids expanding definitions in regions conditionally > executed under the condition A when simplifying A && B or A || B. > This is done by passing down the basic-block of the outer condition > to maybe_fold_{and,or}_comparisons, through the various helpers > in gimple-fold.cc that might call back to maybe_fold_{and,or}_comparisons > and ultimatively to maybe_fold_comparisons_from_match_pd where the > fix is to provide a custom valueization hook to > gimple_match_op::resimplify that avoids looking at definitions > that do not dominate the outer block. > > For the testcase this avoids combining a stmt that invokes undefined > integer overflow when the outer condition is false but it also > aovids combining stmts with range information that is derived from > the outer condition. > > The new parameter to maybe_fold_{and,or}_comparisons is defaulted > to nullptr and I only adjusted the if-combine to pass down the > outer block. I think other callers like tree-if-conv have the > same issue but it's not straight-forward as to what to do there. > > Bootstrapped and tested on x86_64-unknown-linux-gnu. > > Any opinions? (I'm not 100% there's not other code besides > maybe_fold_comparisons_from_match_pd in the call chains that > need to check against the BB - some pieces do look at the > definition of a stmt. Though I think the proper TLC here > is to move what's missing to match.pd and get rid of the > "manual duplication" there) > > Thanks, > Richard. > > 2022-04-05 Richard Biener <rguent...@suse.de> > > PR tree-optimization/105142 > * gimple-fold.h (maybe_fold_and_comparisons): Add defaulted > basic-block parameter. > (maybe_fold_or_comparisons): Likewise. > * gimple-fold.cc (follow_outer_ssa_edges): New. > (maybe_fold_comparisons_from_match_pd): Use follow_outer_ssa_edges > when an outer condition basic-block is specified. > (and_comparisons_1, and_var_with_comparison, > and_var_with_comparison_1, or_comparisons_1, > or_var_with_comparison, or_var_with_comparison_1): Receive and pass > down the outer condition basic-block. > * tree-ssa-ifcombine.cc (ifcombine_ifandif): Pass down the > basic-block of the outer condition. > > * g++.dg/torture/pr105142.C: New testcase.
LGTM. Jakub