On Sun, Sep 21, 2025 at 1:09 PM Jeff Law <[email protected]> wrote: > > > > On 9/19/25 13:45, Andrew Pinski wrote: > > Since optimize_unreachable does not directly remove the bb, we can still > > remove > > the condition that goes to a block containing a forced label. This is a > > small cleanup > > from the original patch which added optimize_unreachable. > > > > The review of the original patch missed that the bb was not being removed > > by the pass > > but later on by cleanupcfg; > > https://gcc.gnu.org/pipermail/gcc-patches/2012-July/343239.html. > > Which is why this is allowed to be done. > > > > I added another testcase to check that the `if` is removed too. > > > > Bootstrapped and tested on x86_64-linux-gnu. > > > > gcc/ChangeLog: > > > > * tree-ssa-ccp.cc (optimize_unreachable): Don't check for forced > > labels. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/builtin-unreachable-7.c: New test. > I guess ultimately this isn't intersecting with the need to preserve > instructions with side effects before the unreachable, it's just about > detecting more cases where we can remove the controlling conditional.
Correct. The new check is still checking to see __builtin_unreachable() is the only thing in the BB. > > We still have to emit the forced label. Conceptually since it's > reaching a builtin_unreachable we can emit it anywhere that's convenient > as traversing into that code implies undefined behavior of some kind. Yes, the forced lablel is still emitted and in the case of other basic blocks going into the BB of builtin_unreachable (only) bb still exists. The overall thing what this code does after this patch is the following: ``` if (a) goto unreachable_only_bb; else goto other_bb; unreachable_only_bb: forced_label: [forced] __builtin_unreachable(); ``` changes into: ``` if (false) goto unreachable_only_bb; else goto other_bb; unreachable_only_bb: forced_label: [forced] __builtin_unreachable(); ``` And then cleanupcfg comes along and fixes/removes the condition to be: ``` goto other_bb; unreachable_only_bb: forced_label: [forced] __builtin_unreachable(); ``` And if unreachable_only_bb is unreachable because there is no computed goto (which has a successor to that unreachable_only_bb bb), cleanupcfg will remove the unreachable_only_bb bb and move the forced_lable somewhere else (it is undefined where it goes because there is no computed goto in the function). Thanks, Andrew > > Assuming that's the case. OK for the trunk. > > jeff
