How to cross-compile a C code to MIPS R2000?

I’m doing some experiments on the machines that have a unified operator to calculate quotient and remainder.

I found that MIPS R2000 has that property, so I build the LLVM project by cmake -DLLVM_TARGETS_TO_BUILD=Mips ../llvm

I check the target by command clang -print-targets and it produce some target names contain “mips”.

And I compile a C code by using clang -target mips test.c -S but it seems that the division cannot calculate quotient and remainder one time.

How can I generate the MIPS R2000 assembly code for my requirement?

Caveat: Not a mips expert but here’s roughly how you’d find out.

I don’t think llvm has any knowledge of the R2000 specifically. Looking at the list in llvm/lib/Target/Mips/ (maybe you saw the same list from -print-targets, there is some way to see it from clang but I don’t recall)

So what you could try is using one of the Procs that are architecture revisions not CPU names. Whatever the R2000 was, if we do indeed support it. If you wanted to do that it would be something like clang -target mips -mcpu=<the name of the proc>.

Finally, if by operator you mean instruction, you should check if llvm implements that instruction. Usually grepping llvm/lib/Target/Mips will do the job, make it case insensitive. You should find some reference to it. You can also check the test files in llvm/test/MC/Mips (which usually include useful flags too that you can adapt to use with clang instead of llvm-mc).

The underlying instruction set can. LLVM code generation won’t take advantage of that, though. Filed MIPS backend doesn't combine div+rem optimally · Issue #54991 · llvm/llvm-project · GitHub .

I do some experiments on this and I think the reason is a function " hasDivRemOp().

It will return True if the target has unified operator to calculate quotient and remainder.

But if the target is Mips, it will return False, and I think that doesn’t make sense.

The function is applied in DivRemPairs.cpp. If it returns False, this pass will decompose the rem instruction to mul and sub.

If you compile your code to LLVM IR and optimize without the pass div-rem-pairs, and using llc to compile to assembly code, it may generate the code you want.