Extending LLVM: Adding instructions, intrinsics, types, etc.

1. Introduction and Warning
2. Adding a new intrinsic function
3. Adding a new instruction
4. Adding a new type
   1. Adding a new fundamental type
   2. Adding a new derived type

Written by Misha Brukman, Brad Jones, and Chris Lattner

Introduction and Warning

During the course of using LLVM, you may wish to customize it for your research project or for experimentation. At this point, you may realize that you need to add something to LLVM, whether it be a new fundamental type, a new intrinsic function, or a whole new instruction.

When you come to this realization, stop and think. Do you really need to extend LLVM? Is it a new fundamental capability that LLVM does not support at its current incarnation or can it be synthesized from already pre-existing LLVM elements? If you are not sure, ask on the LLVM-dev list. The reason is that extending LLVM will get involved as you need to update all the different passes that you intend to use with your extension, and there are many LLVM analyses and transformations, so it may be quite a bit of work.

Adding an intrinsic function is easier than adding an instruction, and is transparent to optimization passes which treat it as an unanalyzable function. If your added functionality can be expressed as a function call, an intrinsic function is the method of choice for LLVM extension.

Before you invest a significant amount of effort into a non-trivial extension, ask on the list if what you are looking to do can be done with already-existing infrastructure, or if maybe someone else is already working on it. You will save yourself a lot of time and effort by doing so.

Adding a new intrinsic function

Adding a new intrinsic function to LLVM is much easier than adding a new instruction. Almost all extensions to LLVM should start as an intrinsic function and then be turned into an instruction if warranted.

1. `llvm/docs/LangRef.html`: Document the intrinsic. Decide whether it is code generator specific and what the restrictions are. Talk to other people about it so that you are sure it's a good idea.
2. `llvm/include/llvm/Intrinsics.h`: add an enum in the `llvm::Intrinsic` namespace
3. `llvm/lib/VMCore/Verifier.cpp`: Add code to check the invariants of the intrinsic are respected.
4. `llvm/lib/VMCore/Function.cpp (`Function::getIntrinsicID()`)`: Identify the new intrinsic function, returning the enum for the intrinsic that you added.
5. `llvm/lib/Analysis/BasicAliasAnalysis.cpp`: If the new intrinsic does not access memory or does not write to memory, add it to the relevant list of functions.
6. `llvm/lib/Transforms/Utils/Local.cpp`: If it is possible to constant fold your intrinsic, add support to it in the `canConstantFoldCallTo` and `ConstantFoldCall` functions.
7. Test your intrinsic
8. `llvm/test/Regression/*`: add your test cases to the test suite

Once the intrinsic has been added to the system, you must add code generator support for it. Generally you must do the following steps:

Add support to the C backend in `lib/Target/CBackend/`
  Depending on the intrinsic, there are a few ways to implement this. First, if it makes sense to lower the intrinsic to an expanded sequence of C code in all cases, just emit the expansion in `visitCallInst`. Second, if the intrinsic has some way to express it with GCC (or any other compiler) extensions, it can be conditionally supported based on the compiler compiling the CBE output (see llvm.prefetch for an example). Third, if the intrinsic really has no way to be lowered, just have the code generator emit code that prints an error message and calls abort if executed.
Add a enum value for the SelectionDAG node in `include/llvm/CodeGen/SelectionDAGNodes.h`
  Also, add code to `lib/CodeGen/SelectionDAG/SelectionDAG.cpp` (and `SelectionDAGPrinter.cpp`) to print the node.
Add code to `SelectionDAG/SelectionDAGISel.cpp` to recognize the intrinsic.
  Presumably the intrinsic should be recognized and turned into the node you added above.
Add code to `SelectionDAG/LegalizeDAG.cpp` to legalize, promote, and expand the node as necessary.
  If the intrinsic can be expanded to primitive operations, legalize can break the node down into other elementary operations that are be supported.
Add target-specific support to specific code generators.
  Extend the code generators you are interested in to recognize and support the node, emitting the code you want.

Unfortunately, the process of extending the code generator to support a new node is not extremely well documented. As such, it is often helpful to look at other intrinsics (e.g. `llvm.ctpop`) to see how they are recognized and turned into a node by `SelectionDAGISel.cpp`, legalized by `LegalizeDAG.cpp`, then finally emitted by the various code generators.

Adding a new instruction

WARNING: adding instructions changes the bytecode format, and it will take some effort to maintain compatibility with the previous version. Only add an instruction if it is absolutely necessary.

1. `llvm/include/llvm/Instruction.def`: add a number for your instruction and an enum name
2. `llvm/include/llvm/Instructions.h`: add a definition for the class that will represent your instruction
3. `llvm/include/llvm/Support/InstVisitor.h`: add a prototype for a visitor to your new instruction type
4. `llvm/lib/AsmParser/Lexer.l`: add a new token to parse your instruction from assembly text file
5. `llvm/lib/AsmParser/llvmAsmParser.y`: add the grammar on how your instruction can be read and what it will construct as a result
6. `llvm/lib/Bytecode/Reader/Reader.cpp`: add a case for your instruction and how it will be parsed from bytecode
7. `llvm/lib/VMCore/Instruction.cpp`: add a case for how your instruction will be printed out to assembly
8. `llvm/lib/VMCore/Instructions.cpp`: implement the class you defined in `llvm/include/llvm/Instructions.h`
9. Test your instruction
10. `llvm/lib/Target/*`: Add support for your instruction to code generators, or add a lowering pass.
11. `llvm/test/Regression/*`: add your test cases to the test suite.

Also, you need to implement (or modify) any analyses or passes that you want to understand this new instruction.

Adding a new type

WARNING: adding new types changes the bytecode format, and will break compatibility with currently-existing LLVM installations. Only add new types if it is absolutely necessary.

Adding a fundamental type

1. `llvm/include/llvm/Type.h`: add enum for the new type; add static `Type*` for this type
2. `llvm/lib/VMCore/Type.cpp`: add mapping from `TypeID` => `Type*`; initialize the static `Type*`
3. `llvm/lib/AsmReader/Lexer.l`: add ability to parse in the type from text assembly
4. `llvm/lib/AsmReader/llvmAsmParser.y`: add a token for that type

Adding a derived type

1. `llvm/include/llvm/Type.h`: add enum for the new type; add a forward declaration of the type also
2. `llvm/include/llvm/DerivedType.h`: add new class to represent new class in the hierarchy; add forward declaration to the TypeMap value type
3. `llvm/lib/VMCore/Type.cpp`: add support for derived type to:

       std::string getTypeDescription(const Type &Ty,
         std::vector<const Type*> &TypeStack)
       bool TypesEqual(const Type *Ty, const Type *Ty2,
         std::map<const Type*, const Type*> & EqTypes)

   add necessary member functions for type, and factory methods
4. `llvm/lib/AsmReader/Lexer.l`: add ability to parse in the type from text assembly
5. `llvm/lib/ByteCode/Writer/Writer.cpp`: modify `void BytecodeWriter::outputType(const Type *T)` to serialize your type
6. `llvm/lib/ByteCode/Reader/Reader.cpp`: modify `const Type *BytecodeReader::ParseType()` to read your data type
7. `llvm/lib/VMCore/AsmWriter.cpp`: modify

       void calcTypeName(const Type *Ty,
                         std::vector<const Type*> &TypeStack,
                         std::map<const Type*,std::string> &TypeNames,
                         std::string & Result)

   to output the new derived type