This CL is a manual backport of CLs 403837 and 404914 to Go 1.18.
CL 403837 was intended just as a simplification CL, but evidently it
also fixed#51840. However, for backporting to Go 1.18, the existing
logic needs to be preserved to support -G=0 mode (which still relies
Run-TryBot: Matthew Dempsky <email@example.com>
TryBot-Result: Gopher Robot <firstname.lastname@example.org>
Reviewed-by: Keith Randall <email@example.com>
Reviewed-by: Keith Randall <firstname.lastname@example.org>