How to load and store same memory?

I have a problem when access memory with llvm’s llc.
My backend uses load and store to access memory. Because of

1. pipeline
2. load is faster than store

I have to insert nop when access the same memory .Such as:

store gr0 sp
load gr1 sp

#gr4 and sp has same value
store gr0 gr4
load gr1 sp

I can use a pass to check MI in CASE A, then insert nop when load and store use same register.
But I don’t know how to deal with CASE B. I need some advise.

CASE B doesn’t really seem any harder to make correct than CASE A; you just have to assume the addresses are equal unless you can prove otherwise.

Avoiding the insertion of unnecessary nops is, of course, the hard part. MachineInstr::mayAlias() might be helpful for this. (But watch out; it assumes the address register isn’t modified. You might need to adapt the implementation a bit.)

Thanks a lot.
mayAlias is helpful. I don’t get it. when register is modified if I just use mayAlias.

From the documentation comment for mayAlias:

Assumes any physical registers used to compute addresses have the same value for both instructions.

This API was originally designed for scheduling (i.e. rearranging instructions), where a write to the register would block rearrangement anyway.

If you dig into the implementation, you’ll see that the requirement comes from areMemAccessesTriviallyDisjoint, which assumes that, for example, that “load gr1 gr4” doesn’t alias “load gr2 gr4+16”. If gr4 is modified between the two instructions, though, they could alias.