MachineFunction Pass

I am familiarizing myself with llvm.

  1. I want to investigate all the instructions that write to e/rax register. First, I wish to print such instructions. Then, I would like to insert some instructions after the write to e/rax. Specifically, I want to invoke (or maybe inline) a function that will perform some checks. What would be the best way to go about this?

  2. I want to control the machine level instructions generated. I want to be able to inspect the generated code, and if some precondition is met, I want to request the code generator to pick a different set of opcodes. I want to repeat till the code is acceptable. For example, if the instructions generated are mov al, 0x1; cmp al, bl;, I wish to change it to (semantically equivalent) mov al, 0x1; cmp bl, al; — This example is from The goal is to eliminate ROP gadgets. What is the best way to go about it?

Thank you,