[LLVMdev] How to write a new backend?

Misha Brukman brukman at uiuc.edu
Thu Jun 3 11:51:01 PDT 2004


On Thu, Jun 03, 2004 at 02:41:31PM +0400, Vladimir Prus wrote:
> I'm considering a possibility of writing an llvm backend for my
> research uses.  Unfortunately, I can't find much information on how do
> to it. I more or less understood now the C backend is implemented, but
> for real backend I'd need at least register allocation. I beleive
> there's already register allocator in LLVM and I would like to reuse
> it if possible. 
> 
> The question is how? The REAME.txt in x86 directory does not tell
> that, and I see no other information.

There is a document in progress which you may want to read:

  http://llvm.cs.uiuc.edu/docs/CodeGenerator.html

It describes some of the classes in code generation at a high level, but
it is not complete (yet).

> In general, what's the best way to write a backend?

In general, you want to follow the format of lib/Target/X86:

* Describe the target machine which you are compiling for
  (TargetMachine, TargetData need to be subclassed and implemented for
  your target)
* Describe the register set - see lib/Target/X86/X86RegisterInfo.*
  You need to implement a subclass of MRegisterInfo
* Describe the instruction set - see lib/Target/X86/X86InstrInfo.*
  You need to implement a subclass of TargetInstrInfo

Now, for static code generation you also need to write an instruction
selector for your platform: see lib/Target/X86/X86SimpInstrSelector.cpp
which is no longer "simple" but it gives you the idea: you have to be
able to create MachineInstrs for any given LLVM instruction, and produce
a MachineFunction with MachineBasicBlocks full of MachineInstrs for a
corresponding LLVM Function.  Creating an instruction selector is
perhaps the most time-consuming part of creating a back-end.

To create a JIT for your platform, you also need a subclass of
TargetJITInfo (see lib/Target/X86/X86JITInfo.h) and a machine code
emitter (see lib/Target/X86/X86CodeEmitter.cpp) .

Note that lib/target/PowerPC is a clean skeleton for a new target, so
you might want to start with that and adapt it for your target, and if
you are wondering how things are done, peek in the X86 target.

The PowerPC target is non-functional but provides the basic building
blocks you will need for your endeavor.  

I hope this has shed some light on the issue.  If something's not clear,
please ask again.

-- 
Misha Brukman :: http://misha.brukman.net :: http://llvm.cs.uiuc.edu




More information about the llvm-dev mailing list