SwitchInst handling in backend

Hi,

if I want to know how switch instructions are handled in the backend, where do I have to look first?
I'm not familiar with the backend framework and I couldn't figure out the interface between the LLVM instruction 'SwitchInst' and whatever there is in the backend.

I would be very happy about every hint where I have to look to find the entry point of switch instructions in the backend (in particular in the MIPS backend.

Thanks a lot and kind regards,
Nico

SelectionDAGBuilder::visitSwitch is the general switch lowering...

-Eli

Hi,

if I want to know how switch instructions are handled in the backend, where do I have to look first?
I'm not familiar with the backend framework and I couldn't figure out the interface between the LLVM instruction 'SwitchInst' and whatever there is in the backend.

LLVM IR gets passed to the backend as a SelectionDAG, which is constructed by peephole expansion of the IR (see http://llvm.org/releases/3.0/docs/CodeGenerator.html#selectiondag_intro ). The relevant code for ‘select’ instructions in IR is the visitSelect() function in lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp; a select gets expanded into a ISD::SELECT (for scalar types) or ISD::VECTOR (for vector types) for each select operand, plus a ISD::MERGE_VALUES to multiplex them all together.

For the MIPS backend handling code, see lib/Target/Mips/MipsISelLowering.cpp. In particular, setOperation(ISD::SELECT, , Custom) means that the MIPS backend lowers SELECTS in C++ rather than a simple tablegen pattern. In LowerSELECT() in that same file, you can see that MIPS will create a conditional move if the select condition is set by a floating point comparison, and the “return Op;” it does otherwise means that other cases should be handled by the default machinery rather than custom lowering code.

The “default machinery” requires quite a bit of background knowledge to understand, but the Code Generator doc gives a good broad overview, and the above is the extent of custom select-handling code in the MIPS backend.

-Matt

Hi Eli,

Thank you for the quick reply.

Yes.

-Eli

I understand this lowering is target independent and there is no additional target dependent handling of switch instructions - right?

Well. Target can select whether it support e.g. indirect branches. If
it does not, then jump tables are not created and everything lowered
to comparison trees (plus some other stuff).

Only branches and jump tables are left after this lowering?

Yes.