How to add a pass to do zero check for all DIV instructions

Hi. I’m a new learner for LLVM. I want to write a pass to add some custom instructions to check zero for DIV. (X86-64 architecture)

before:


IDIV EBX

after:

TEST EBX, EBX
JNZ L1
MOV EBX, 1

L1:
IDIV EBX

I think it’s need to modify some code in instruction generate pass, but I don’t know where is the relation files.

If you write a pass that runs against IR, other optimisations may be able to prove when the value is zero and remove the test.