Selection of multiple instructions

Hi,

I'm currently trying to modify LLVM to include runtime checks into X86
binaries. I've looked into some of the possibilities during the phases
happening in LLVM and have the impression that inserting runtime checks
during selection would be great, since lots of optimizations are already
done and I can work directly with X86 instructions.

I've read through the documentation for code generation
(http://www.llvm.org/docs/CodeGenerator.html) and am unsure if I understand
it correctly, because I have trouble figuring out what actually to do in the
code to accomplish my aim.

There are the tablegen generated functions and custom C++ code (I guess that
will be X86DAGToDAGISel::Select(SDValue)) that deal with selection.

The runtime checks I want to add concern indirect jumps. So for example for
all calls that jump to register values like

call eax

I want to insert a check

... (some test) ...# perform test
jne error_label # jump if test fails
call eax # otherwise execute original call

So the check is a sequence of instructions before the call instruction.

Is it possible to do that at the selection level? If yes, could you give a
simple example or some hint how it would be done? I only find examples where
exactly one instruction is emitted (and/or returned).

Do you think it's the right approach or is there a better place?

I'd really appreciate your help.
Artjom Kochtchi

Hi,

I'm currently trying to modify LLVM to include runtime checks into X86
binaries. I've looked into some of the possibilities during the phases
happening in LLVM and have the impression that inserting runtime checks
during selection would be great, since lots of optimizations are already
done and I can work directly with X86 instructions.

I've read through the documentation for code generation
(http://www.llvm.org/docs/CodeGenerator.html) and am unsure if I understand
it correctly, because I have trouble figuring out what actually to do in the
code to accomplish my aim.

There are the tablegen generated functions and custom C++ code (I guess that
will be X86DAGToDAGISel::Select(SDValue)) that deal with selection.

The runtime checks I want to add concern indirect jumps. So for example for
all calls that jump to register values like

call eax

I want to insert a check

... (some test) ...# perform test
jne error_label # jump if test fails
call eax # otherwise execute original call

So the check is a sequence of instructions before the call instruction.

Is it possible to do that at the selection level? If yes, could you give a
simple example or some hint how it would be done? I only find examples where
exactly one instruction is emitted (and/or returned).

It's not. It requires updating CFG. I'd do this as a separate pass. Another possibility is to use the usesCustomDAGSchedInserter hack. That is, isel to a pseudo instruction and expand it to a series of instructions and update cfg at scheduling time. Look for the usesCustomDAGSchedInserter in X86InstrInfo.td and EmitInstrWithCustomInserter in X86ISelLowering.cpp.

Evan

Evan Cheng-2 wrote:

It's not. It requires updating CFG. I'd do this as a separate pass.
Another possibility is to use the usesCustomDAGSchedInserter hack.

I've looked into this and it appears that a pass is probably better because
I want to add the check to every indirect call/jump/branch instruction which
-- using the hack -- would lead to rather many pseudo instructions.

In the runtime check I need to access the address of the jump target. That's
why it probably needs to happen after instruction selection. So is it the
right spot to put the pass between selection and scheduling (so the register
or memory locations that during runtime will contain the jump target is
already known)? Do I just create a PassManager, put my pass in and run it or
is there another approach to run passes at more or less arbitrary places?
Examples on that are rather sparse...

Artjom