<div class="__aliyun_email_body_block"><div  style="line-height:1.7;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;"><div  style="clear:both;">Hi Dear GC devs,<br ></div><div  style="clear:both;"><div  style="clear:both;"><br ></div><div  style="clear:both;">I found a case where GC barriers cannot be optimized out. I wonder if anyone could enlighten me on this code:</div><div  style="clear:both;"><br ></div><div  style="clear:both;">> G1BarrierSetC2::g1_can_remove_pre_barrier (or ShenandoahBarrierSetC2::satb_can_remove_pre_barrier)</div><div  style="clear:both;"><br ></div><div  style="clear:both;">where there is a condition:</div><div  style="clear:both;"><br ></div><div  style="clear:both;">> (captured_store == nullptr || captured_store == st_init->zero_memory())</div><div  style="clear:both;"><br ></div><div  style="clear:both;">on the store that can be optimized out. The comment says:</div><div  style="clear:both;"><br ></div><div  style="clear:both;">> The compiler needs to determine that the object in which a field is about</div><div  style="clear:both;">> to be written is newly allocated, and that no prior store to the same field</div><div  style="clear:both;">> has happened since the allocation.</div><div  style="clear:both;"><br ></div><div  style="clear:both;">But my understanding is satb barriers of any number of stores immediately (i.e., no in-between safepoints) after an allocation can be optimized out, same field or not.</div><div  style="clear:both;">The "no prior store" condition confuses me.</div><div  style="clear:both;"><br ></div><div  style="clear:both;">What's more, failing to optimize one satb barrier will prevent further barrier optimization that otherwise would be done (maybe due to control flow complexity from the satb barrier).</div><div  style="clear:both;"><br ></div><div  style="clear:both;">An example would be:</div><div  style="clear:both;"><br ></div><div  style="clear:both;"></div><span >public static class TwoFieldObject {</span><div  style="clear:both;">  public Object ref;</div><div  style="clear:both;">  public Object ref2;</div><div  style="clear:both;">  public TwoFieldObject(Object a) {</div><div  style="clear:both;">    ref = a;</div><div  style="clear:both;">  }</div><div  style="clear:both;">}</div><div  style="clear:both;">public static Object testWrite(Object a, Object b, Object c) {</div><div  style="clear:both;">  TwoFieldObject tfo = new TwoFieldObject(a);</div><div  style="clear:both;">  tfo.ref = b; // satb barrier of this store cannot be optimized out, and because of its existence, post barrier will also not be optimized out</div><div  style="clear:both;">  tfo.ref2 = c; // because of the previous store's barriers, pre/post barriers of this store will not be optimized out</div><div  style="clear:both;">  return tfo;</div><span >}</span><span ></span></div></div></div>