This patch fixes the atomicrmw result value to be the value before the operation instead of the value after the operation. This was a bug, left as a FIXME in the code (see D97127).
From the LangRef:
The contents of memory at the location specified by the ‘<pointer>’ operand are atomically read, modified, and written back. The original value at the location is returned.
Doing this expansion early allows the register allocator to arrange registers in such a way that commutable operations are simply swapped around as needed, which results in shorter code while still being correct.
Also, do we need to make a helper function int getRegScratch(void) in AVRSubtarget.h, just like I have done as getIORegRAMPZ(void) ? The new helper function will return r16 for avr-tiny devices.
Also, do we need a helper function int getIORegSREG(void) instead of hard coded 0x3f ?