Modifying x86 indirect jumps in LLVM backend

I’m looking to tweak the LLVM compiler for x86. My goal is to change indirect jumps like jmp *(ecx) into a two-step process: mov *(ecx), edx followed by jmp *edx.

First, I want to add some logging to track when these indirect jumps appear. Then I’ll work on the actual replacement.

I think I need to mess with the x86asmprinter in the backend, but I’m not sure where to start. Any tips or resources would be great.

This is just step one. Later, I want to handle more complex cases with indirect jumps and pop instructions. But for now, I’m trying to get my feet wet with the backend.

Here’s a simple code example of what I’m aiming for:

; Before
jmp *(ecx)

; After
mov *(ecx), edx
jmp *edx

Any guidance on how to approach this in LLVM would be super helpful!

yo silvia, have u checked out the x86instructionselection.cpp file? it might be a good place to start. u could try adding some debug prints there to see when those indirect jumps pop up. then maybe look into creating a custom pass to do the actual swapping. just a thought!

hey, i wonder if u tried exploring x86iselLowering.cpp for clues? perhaps a dedicated pass after instruction selection might work. i’m curious though, what got u interested in altering these jumps?

For modifying indirect jumps in LLVM’s x86 backend, you might want to look into the X86TargetLowering class. This class handles instruction selection and lowering for x86-specific operations. You could potentially override the LowerCall or LowerJumpTable methods to implement your custom indirect jump transformation. Additionally, creating a new MachineFunctionPass could give you more control over the modification process. This would allow you to iterate through all instructions, identify indirect jumps, and replace them with your desired two-step process. Remember to thoroughly test your changes, as altering jump instructions can significantly impact program behavior and performance.