Move invariant writes outside loop

Change-Id: Ia254fe382a00be13a631fcc1b23f940352d8d3ba