How to write LLVM backend pass after every LLVM's internal pass?

Currently I’m writing a X86 backend pass for my research project. I inserted a few instructions in MachineBasicBlocks, but then llc optimized my code, and deleted some of it. So how to so backend pass after all machine level optimizations?

I don't have enough details to really help out here but my first assumption is that costs of selections are being merged in the llc.
You should note that's normal and generally a good thing or other forms of combining or merging instructions in the llc.

So based on that I'm curious as to why your concerned about the llc deleted parts of it,


Because I need to use reverse engineering to patched generated binary. But llc deleted some function call I injected, so I couldn’t find these instructions in radare2(reverse engine I used to patch binary).

For example:
Mov edi, 0x1 mov edi, 0x2
     > >
     V V
Call func call func

Will be opted into:

Mov edi, 0x1. Mov edi, 0x2
     > >
     > <——————
Call func

I write my MachineFunctionPass in lib/Targets/X86 directory. Will lib/CodeGen optimize my injected code?

If I understand your diagram yes as those are basically the same instructions excluding moving
at 0x1 address versus 0x2 address. Combining would be normal here if and to my knowledge
x86 can do mov with two addresses. What you want to figure out is how to inject earlier
probably in the middle end and then trace it to see if the instructions are similar enough to
be doing this.