compiler: avoid introducing redundant write barriers

The traversal used by the write barrier insertion phase can sometimes
wind up visiting new statements inserted during the traversal, which
then results in duplicate / redundant write barrier guards. Example
program to reproduce:

  package small
  type S struct {
	N *S
	K int
  var G *S = &S{N: nil, K: 101}

This patch changes the traversal code to keep track of statements
already added and avoid processing them again later in the traversal.

Fixes golang/go#25867

Change-Id: Ie7a81b49e815aff7379d3d4ef7c7a864cebcff5b
Reviewed-by: Ian Lance Taylor <>
3 files changed