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



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.


Adding a new intrinsic function


  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


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


  9. Test your instruction


Adding a new type


Adding a fundamental type
Adding a derived type
  2. llvm/include/llvm/DerivedType.h: add new class to represent new class in the hierarchy; add forward declaration to the TypeMap value type
    std::string getTypeDescription(const Type &Ty,
      std::vector<const Type*> &TypeStack)
    add necessary member functions for type, and factory methods
  7. llvm/lib/VMCore/AsmWriter.cpp: modify
    void calcTypeName(const Type *Ty,
                      std::vector<const Type*> &TypeStack,
                      std::string & Result)
    to output the new derived type

Valid CSS! Valid HTML 4.01! The LLVM Compiler Infrastructure
Last modified: $Date: 2005/05/18 16:04:43 $