1. A method of compiling a program, the method comprising: performing a program flow analysis on a software transactional memory (STM) transaction to be modified into a compiled STM transaction including STM primitives that call functions in a STM library, the program flow analysis tracks a possible shadow copy pointer for first and second memory accesses to a memory location in the STM transaction, the possible shadow copy pointer is either a base address of the memory location or a shadow copy pointer to a shadow copy of the memory location based on the STM transaction, the functions of the STM library return results to the compiled STM transaction including in a direct write mode of operation returning the base address and in a buffered write mode of operation returning the possible shadow copy pointer; and identifying an STM optimization for the second memory access in the compiled STM transaction using the program flow analysis.