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