[llvm-dev] Doubt about adding a new instruction

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Tue Apr 30 05:14:17 PDT 2019


On Tue, 30 Apr 2019 at 11:18, Ajumal Abdul Majeed via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> I was using inline assembly code and done some changes in the assembler (as mentioned in https://nitish2112.github.io/post/adding-instruction-riscv/) to get the above result, But now I want the instruction to be recognized by the clang itself and give me the above result.

>From the link it doesn't look like the encoding chosen fits perfectly
into any of the existing classes, so you'll probably have to edit both
lib/Target/RISCV/RISCVInstrFormats.td and RISCVInstrInfo.td.

Your end goal is to create an instance of RVInst
(RISCVInstrFormats.td, line 79) with the fields populated correctly.
You could just do that directly, but it's probably best to at least
try to fit in with the existing scheme, if nothing else as a much
better learning exercise in how LLVM really works.

So the key bits you should be looking at are:

InstrFormats, line 57ish. These definitions go into bits 6-2 of the
"RVInst". Your new instruction doesn't match any of these values, so
you'll need to create an extra one.

InstrInfo, line 310. ALU_rr is closest in effect to the instruction
you want, and if you look at how it relates to RVInstR (InstrFormats
again) you can see that it gives you the ability to set most of the
fields you need to. But it obviously uses a wrong OPC_* from above. So
you should probably duplicate that class with a slightly adjusted name
and using your new OPC_WHATEVER instead.

Finally, looking at the instructions that use ALU_rr, you see they
just provide the remaining fixed bits of the instruction and the
mnemonic (for example "def SRL" on line 405). Yours should do the
same.

That should be enough to get Clang's assembler and llvm-objdump to
know about the new instruction.

Cheers.

Tim.


More information about the llvm-dev mailing list