Need guidance regarding MachineFunctionPass implementation

Hello all,

I need to modify llvm bytecode / native code just before it is emitted i.e. after all the regular operations and optimizations are done. I was told that this can be achieved by implementing a MachineFunctionPass.
I searched the internet for some tutorials or some example MachineFunctionPass but found nothing. Kindly point some links, or anything that may help. Thank you.


Hi Sachin,

You could check the code in lib/Target/ARM/ARMOptimizeBarriersPass.cpp for an example of a MachineFunctionPass that runs very late. Look in lib/Target/ARM/ARMTargetMachine.cpp to see where it is added to the pass pipeline.


– Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation

Dear Sachin,

You will want to read up on the LLVM Code Generator infrastructure:

Also, a MachineFunctionPass is nearly the same as an LLVM FunctionPass except that it operations on MachineFunction objects instead of Function objects. If you read the "Writing an LLVM Pass" document ( and understand how to use MachineInstr's, MachineBasicBlocks, and MachineInstr's, you should be able to figure it out.

Finally, take a look at the doxygen documentation anytime you need details on how to use a specific LLVM class, and as David suggested, take a look at existing code.


John Criswell

Thank you Mr. John and Mr. David !