ExtendingLLVM.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. ============================================================
  2. Extending LLVM: Adding instructions, intrinsics, types, etc.
  3. ============================================================
  4. Introduction and Warning
  5. ========================
  6. During the course of using LLVM, you may wish to customize it for your research
  7. project or for experimentation. At this point, you may realize that you need to
  8. add something to LLVM, whether it be a new fundamental type, a new intrinsic
  9. function, or a whole new instruction.
  10. When you come to this realization, stop and think. Do you really need to extend
  11. LLVM? Is it a new fundamental capability that LLVM does not support at its
  12. current incarnation or can it be synthesized from already pre-existing LLVM
  13. elements? If you are not sure, ask on the `LLVM-dev
  14. <http://lists.llvm.org/mailman/listinfo/llvm-dev>`_ list. The reason is that
  15. extending LLVM will get involved as you need to update all the different passes
  16. that you intend to use with your extension, and there are ``many`` LLVM analyses
  17. and transformations, so it may be quite a bit of work.
  18. Adding an `intrinsic function`_ is far easier than adding an
  19. instruction, and is transparent to optimization passes. If your added
  20. functionality can be expressed as a function call, an intrinsic function is the
  21. method of choice for LLVM extension.
  22. Before you invest a significant amount of effort into a non-trivial extension,
  23. **ask on the list** if what you are looking to do can be done with
  24. already-existing infrastructure, or if maybe someone else is already working on
  25. it. You will save yourself a lot of time and effort by doing so.
  26. .. _intrinsic function:
  27. Adding a new intrinsic function
  28. ===============================
  29. Adding a new intrinsic function to LLVM is much easier than adding a new
  30. instruction. Almost all extensions to LLVM should start as an intrinsic
  31. function and then be turned into an instruction if warranted.
  32. #. ``llvm/docs/LangRef.html``:
  33. Document the intrinsic. Decide whether it is code generator specific and
  34. what the restrictions are. Talk to other people about it so that you are
  35. sure it's a good idea.
  36. #. ``llvm/include/llvm/IR/Intrinsics*.td``:
  37. Add an entry for your intrinsic. Describe its memory access
  38. characteristics for optimization (this controls whether it will be
  39. DCE'd, CSE'd, etc). If any arguments need to be immediates, these
  40. must be indicated with the ImmArg property. Note that any intrinsic
  41. using one of the ``llvm_any*_ty`` types for an argument or return
  42. type will be deemed by ``tblgen`` as overloaded and the
  43. corresponding suffix will be required on the intrinsic's name.
  44. #. ``llvm/lib/Analysis/ConstantFolding.cpp``:
  45. If it is possible to constant fold your intrinsic, add support to it in the
  46. ``canConstantFoldCallTo`` and ``ConstantFoldCall`` functions.
  47. #. ``llvm/test/*``:
  48. Add test cases for your test cases to the test suite
  49. Once the intrinsic has been added to the system, you must add code generator
  50. support for it. Generally you must do the following steps:
  51. Add support to the .td file for the target(s) of your choice in
  52. ``lib/Target/*/*.td``.
  53. This is usually a matter of adding a pattern to the .td file that matches the
  54. intrinsic, though it may obviously require adding the instructions you want to
  55. generate as well. There are lots of examples in the PowerPC and X86 backend
  56. to follow.
  57. Adding a new SelectionDAG node
  58. ==============================
  59. As with intrinsics, adding a new SelectionDAG node to LLVM is much easier than
  60. adding a new instruction. New nodes are often added to help represent
  61. instructions common to many targets. These nodes often map to an LLVM
  62. instruction (add, sub) or intrinsic (byteswap, population count). In other
  63. cases, new nodes have been added to allow many targets to perform a common task
  64. (converting between floating point and integer representation) or capture more
  65. complicated behavior in a single node (rotate).
  66. #. ``include/llvm/CodeGen/ISDOpcodes.h``:
  67. Add an enum value for the new SelectionDAG node.
  68. #. ``lib/CodeGen/SelectionDAG/SelectionDAG.cpp``:
  69. Add code to print the node to ``getOperationName``. If your new node can be
  70. evaluated at compile time when given constant arguments (such as an add of a
  71. constant with another constant), find the ``getNode`` method that takes the
  72. appropriate number of arguments, and add a case for your node to the switch
  73. statement that performs constant folding for nodes that take the same number
  74. of arguments as your new node.
  75. #. ``lib/CodeGen/SelectionDAG/LegalizeDAG.cpp``:
  76. Add code to `legalize, promote, and expand
  77. <CodeGenerator.html#selectiondag_legalize>`_ the node as necessary. At a
  78. minimum, you will need to add a case statement for your node in
  79. ``LegalizeOp`` which calls LegalizeOp on the node's operands, and returns a
  80. new node if any of the operands changed as a result of being legalized. It
  81. is likely that not all targets supported by the SelectionDAG framework will
  82. natively support the new node. In this case, you must also add code in your
  83. node's case statement in ``LegalizeOp`` to Expand your node into simpler,
  84. legal operations. The case for ``ISD::UREM`` for expanding a remainder into
  85. a divide, multiply, and a subtract is a good example.
  86. #. ``lib/CodeGen/SelectionDAG/LegalizeDAG.cpp``:
  87. If targets may support the new node being added only at certain sizes, you
  88. will also need to add code to your node's case statement in ``LegalizeOp``
  89. to Promote your node's operands to a larger size, and perform the correct
  90. operation. You will also need to add code to ``PromoteOp`` to do this as
  91. well. For a good example, see ``ISD::BSWAP``, which promotes its operand to
  92. a wider size, performs the byteswap, and then shifts the correct bytes right
  93. to emulate the narrower byteswap in the wider type.
  94. #. ``lib/CodeGen/SelectionDAG/LegalizeDAG.cpp``:
  95. Add a case for your node in ``ExpandOp`` to teach the legalizer how to
  96. perform the action represented by the new node on a value that has been split
  97. into high and low halves. This case will be used to support your node with a
  98. 64 bit operand on a 32 bit target.
  99. #. ``lib/CodeGen/SelectionDAG/DAGCombiner.cpp``:
  100. If your node can be combined with itself, or other existing nodes in a
  101. peephole-like fashion, add a visit function for it, and call that function
  102. from. There are several good examples for simple combines you can do;
  103. ``visitFABS`` and ``visitSRL`` are good starting places.
  104. #. ``lib/Target/PowerPC/PPCISelLowering.cpp``:
  105. Each target has an implementation of the ``TargetLowering`` class, usually in
  106. its own file (although some targets include it in the same file as the
  107. DAGToDAGISel). The default behavior for a target is to assume that your new
  108. node is legal for all types that are legal for that target. If this target
  109. does not natively support your node, then tell the target to either Promote
  110. it (if it is supported at a larger type) or Expand it. This will cause the
  111. code you wrote in ``LegalizeOp`` above to decompose your new node into other
  112. legal nodes for this target.
  113. #. ``lib/Target/TargetSelectionDAG.td``:
  114. Most current targets supported by LLVM generate code using the DAGToDAG
  115. method, where SelectionDAG nodes are pattern matched to target-specific
  116. nodes, which represent individual instructions. In order for the targets to
  117. match an instruction to your new node, you must add a def for that node to
  118. the list in this file, with the appropriate type constraints. Look at
  119. ``add``, ``bswap``, and ``fadd`` for examples.
  120. #. ``lib/Target/PowerPC/PPCInstrInfo.td``:
  121. Each target has a tablegen file that describes the target's instruction set.
  122. For targets that use the DAGToDAG instruction selection framework, add a
  123. pattern for your new node that uses one or more target nodes. Documentation
  124. for this is a bit sparse right now, but there are several decent examples.
  125. See the patterns for ``rotl`` in ``PPCInstrInfo.td``.
  126. #. TODO: document complex patterns.
  127. #. ``llvm/test/CodeGen/*``:
  128. Add test cases for your new node to the test suite.
  129. ``llvm/test/CodeGen/X86/bswap.ll`` is a good example.
  130. Adding a new instruction
  131. ========================
  132. .. warning::
  133. Adding instructions changes the bitcode format, and it will take some effort
  134. to maintain compatibility with the previous version. Only add an instruction
  135. if it is absolutely necessary.
  136. #. ``llvm/include/llvm/IR/Instruction.def``:
  137. add a number for your instruction and an enum name
  138. #. ``llvm/include/llvm/IR/Instructions.h``:
  139. add a definition for the class that will represent your instruction
  140. #. ``llvm/include/llvm/IR/InstVisitor.h``:
  141. add a prototype for a visitor to your new instruction type
  142. #. ``llvm/lib/AsmParser/LLLexer.cpp``:
  143. add a new token to parse your instruction from assembly text file
  144. #. ``llvm/lib/AsmParser/LLParser.cpp``:
  145. add the grammar on how your instruction can be read and what it will
  146. construct as a result
  147. #. ``llvm/lib/Bitcode/Reader/BitcodeReader.cpp``:
  148. add a case for your instruction and how it will be parsed from bitcode
  149. #. ``llvm/lib/Bitcode/Writer/BitcodeWriter.cpp``:
  150. add a case for your instruction and how it will be parsed from bitcode
  151. #. ``llvm/lib/IR/Instruction.cpp``:
  152. add a case for how your instruction will be printed out to assembly
  153. #. ``llvm/lib/IR/Instructions.cpp``:
  154. implement the class you defined in ``llvm/include/llvm/Instructions.h``
  155. #. Test your instruction
  156. #. ``llvm/lib/Target/*``:
  157. add support for your instruction to code generators, or add a lowering pass.
  158. #. ``llvm/test/*``:
  159. add your test cases to the test suite.
  160. Also, you need to implement (or modify) any analyses or passes that you want to
  161. understand this new instruction.
  162. Adding a new type
  163. =================
  164. .. warning::
  165. Adding new types changes the bitcode format, and will break compatibility with
  166. currently-existing LLVM installations. Only add new types if it is absolutely
  167. necessary.
  168. Adding a fundamental type
  169. -------------------------
  170. #. ``llvm/include/llvm/IR/Type.h``:
  171. add enum for the new type; add static ``Type*`` for this type
  172. #. ``llvm/lib/IR/Type.cpp`` and ``llvm/lib/IR/ValueTypes.cpp``:
  173. add mapping from ``TypeID`` => ``Type*``; initialize the static ``Type*``
  174. #. ``llvm/llvm/llvm-c/Core.cpp``:
  175. add enum ``LLVMTypeKind`` and modify
  176. ``LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty)`` for the new type
  177. #. ``llvm/lib/AsmParser/LLLexer.cpp``:
  178. add ability to parse in the type from text assembly
  179. #. ``llvm/lib/AsmParser/LLParser.cpp``:
  180. add a token for that type
  181. #. ``llvm/lib/Bitcode/Writer/BitcodeWriter.cpp``:
  182. modify ``static void WriteTypeTable(const ValueEnumerator &VE,
  183. BitstreamWriter &Stream)`` to serialize your type
  184. #. ``llvm/lib/Bitcode/Reader/BitcodeReader.cpp``:
  185. modify ``bool BitcodeReader::ParseTypeType()`` to read your data type
  186. #. ``include/llvm/Bitcode/LLVMBitCodes.h``:
  187. add enum ``TypeCodes`` for the new type
  188. Adding a derived type
  189. ---------------------
  190. #. ``llvm/include/llvm/IR/Type.h``:
  191. add enum for the new type; add a forward declaration of the type also
  192. #. ``llvm/include/llvm/IR/DerivedTypes.h``:
  193. add new class to represent new class in the hierarchy; add forward
  194. declaration to the TypeMap value type
  195. #. ``llvm/lib/IR/Type.cpp`` and ``llvm/lib/IR/ValueTypes.cpp``:
  196. add support for derived type, notably `enum TypeID` and `is`, `get` methods.
  197. #. ``llvm/llvm/llvm-c/Core.cpp``:
  198. add enum ``LLVMTypeKind`` and modify
  199. `LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty)` for the new type
  200. #. ``llvm/lib/AsmParser/LLLexer.cpp``:
  201. modify ``lltok::Kind LLLexer::LexIdentifier()`` to add ability to
  202. parse in the type from text assembly
  203. #. ``llvm/lib/Bitcode/Writer/BitcodeWriter.cpp``:
  204. modify ``static void WriteTypeTable(const ValueEnumerator &VE,
  205. BitstreamWriter &Stream)`` to serialize your type
  206. #. ``llvm/lib/Bitcode/Reader/BitcodeReader.cpp``:
  207. modify ``bool BitcodeReader::ParseTypeType()`` to read your data type
  208. #. ``include/llvm/Bitcode/LLVMBitCodes.h``:
  209. add enum ``TypeCodes`` for the new type
  210. #. ``llvm/lib/IR/AsmWriter.cpp``:
  211. modify ``void TypePrinting::print(Type *Ty, raw_ostream &OS)``
  212. to output the new derived type