How can I get an LLVM Backend to generate function calls for mul instructions?

Viewed 233

I'm trying to write a back-end for an ISA that does not have a multiply instruction, so multiplies need to be done with a loop of adds.

I am following this guide.

I tried simply not defining any multiplies in the InstrInfo.td files, but this just causes it to fail when it reaches it. It's a completely 1:1 translation, so is there a way that I can just tell the compiler to replace any mul instruction with Multiply() fucntion call if I have it written somewhere?

2 Answers

Every architecture comes with some limitations.

If an operation or operation of specific type is not supported by the architecture, then they can be set to expand in LLVM. The code generator will turn them to either libcalls or break them into smaller supported operations.

These expansion call can be found in XXXISelLowering.cpp

setOperationAction(ISD::FDIV, MVT::f32, Expand);

For the example above, F32 Div will be expanded to __divsf3 call.

Similarly for your case i32 Multiplication will be expanded to __mulsi3 function in glibc.

  • Check all such libcalls in lib/CodeGen/TargetLoweringBase.cpp and include/llvm/IR/RuntimeLibcalls.def

You can write an IR to IR transformation pass which replaces mul instructions by equivalent function call you propose. This way you wont have to change code generator.

Related