Need guidance regarding MachineFunctionPass

Hello,

I am writing a MachineFunctionPass that inspects the generated machine code, and examines each opcode and its corresponding operands. If the ‘instruction + operands’ match a particular sequence, then the pass should replace them with a fixed instruction + operands sequence. I tried using MachineInstr’s getOpcode and getOperand functions but the pass didn’t work as expected.

Kindly tell me how to proceed or point to appropriate references. Thank you !

Dear Sachin,

Your description of the problem contains too few details; no one will be able to diagnose the problem you're having. You need to provide more information on what, exactly, is not working as you expected.

Regards,

John Criswell

Respected Dr. Criswell,

My problem is that the output of following code looks nothing like any opcode:

for(MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); )
{
MachineInstr *mi = I;
int op = mi->getOpcode();
std::cout << op << ‘\t’;
}

I need to identify specific instructions in my MachineFunctionPass and I was hoping that I can do that by using getOpcode(). I am kinda stuck at this point. Kindly guide me ! Thank you !

Hi Sachin,

         int op = mi->getOpcode();

This is a target-specific opcode, and corresponds to one of the values
defined in (from a CMake build directory)
lib/Target/XYZ/XYZGenInstrInfo.inc.

The easiest way to find out which is actually to use
MachineInstr::dump (and/or "llc -debug" from the command-line to print
it out automatically); if it prints something like (to pick a
completely random example from AArch64):

%X1<def> = LDRXui <fi#3>, 0; mem:LD8[%result] dbg:blockstret.c:20:

Then you'd want to compare "op" against "AArch64::LDRXui"

Cheers.

Tim.

Thank you Mr. Tim. Is “AArch64” a namespace? Because when I tried “X86::MOV”, it gave me an error saying that first define X86 namespace. Sorry if I sound stupid but I am new to LLVM. Thank you again.

Hi Sachin,

Thank you Mr. Tim. Is "AArch64" a namespace? Because when I tried
"X86::MOV", it gave me an error saying that first define X86 namespace.
Sorry if I sound stupid but I am new to LLVM. Thank you again.

Yes, AArch64 is the namespace. X86 should work too. There are 2
possible issues: most simply, you need to include X86InstrInfo.h
directly or indirectly for these enumerators to be defined; if that's
not it, you're probably getting an error because X86::MOV doesn't
exist as a single opcode.

LLVM has many different MOV instructions to model x86's different
addressing modes, and you're going to have to find out which ones you
need to deal with by looking at their definitions in the .td files.

Cheers.

Tim.

Thank you Mr. Tim. I was wondering if you can tell me in which file ‘get()’ is defined. I am having trouble using this function. For example if I write ‘get(AArch64::LDRXui)’ then there is an error generated which says: use of undeclared identifier ‘get’. Thanks again !!

It's defined in the MCInstrInfo class, usually accessed via a
TargetInstrInfo instance (so you often see "TII.get(Whatever)" if it's
used outside a TargetInstrInfo subclass).

Cheers.

Tim.

Hello Mr. Tim,

Thanks to you and others from ‘llvmdev-list’, I have successfully implemented the MachineFunctionPass. But when I examine the output using ‘hexedit’ I see that only some portion of the .text section is effected by my pass. I was expecting the pass to modify large portion of the .text section. Kindly tell me if a MachineFunctionPass runs only on some selected portions of the .text section. Is there a way to make it run on larger portion of the .text section?

The skeleton of my code is:

bool MyPass::runOnMachineFunction(MachineFunction &FNC)

for (MachineFunction::iterator MFN = FNC.begin(), End = FNC.end(); MFN != End; ++MFN)

for (MachineBasicBlock::iterator I = MFN->begin(); I != MFN->end(); )
{
MachineBasicBlock::iterator NewI = std::next(I);
Do the Stuff
I = NewI;
}

Hi Sachin,

Kindly tell me if a MachineFunctionPass runs only on some selected
portions of the .text section. Is there a way to make it run on larger
portion of the .text section?

Your pass should have access to the vast majority of what LLVM puts in
the text section: every function compiled by LLVM. The linker may put
more things in there (either entire libraries if you're linking
statically, or possibly stubs otherwise).

I'm afraid it's impossible to say exactly what's going on in your case
without more details of what you're seeing and why that differs from
what you expect to see though.

Cheers.

Tim.