Remove usage of typedef
[oota-llvm.git] / lib / Target / X86 / README.txt
1 //===- README.txt - Information about the X86 backend and related files ---===//
2 //
3 // This file contains random notes and points of interest about the X86 backend.
4 //
5 // Snippets of this document will probably become the final report for CS497
6 //
7 //===----------------------------------------------------------------------===//
8
9 ===========
10 I. Overview
11 ===========
12
13 This directory contains a machine description for the X86 processor.  Currently
14 this machine description is used for a high performance code generator used by a
15 LLVM JIT.  One of the main objectives that we would like to support with this
16 project is to build a nice clean code generator that may be extended in the
17 future in a variety of ways: new targets, new optimizations, new
18 transformations, etc.
19
20 This document describes the current state of the LLVM JIT, along with
21 implementation notes, design decisions, and other stuff.
22
23
24 ===================================
25 II. Architecture / Design Decisions
26 ===================================
27
28 We designed the infrastructure into the generic LLVM machine specific
29 representation, which allows us to support as many targets as possible with our
30 framework.  This framework should allow us to share many common machine specific
31 transformations (register allocation, instruction scheduling, etc...) among all
32 of the backends that may eventually be supported by LLVM, and ensures that the
33 JIT and static compiler backends are largely shared.
34
35 At the high-level, LLVM code is translated to a machine specific representation
36 formed out of MachineFunction, MachineBasicBlock, and MachineInstr instances
37 (defined in include/llvm/CodeGen).  This representation is completely target
38 agnostic, representing instructions in their most abstract form: an opcode, a
39 destination, and a series of operands.  This representation is designed to
40 support both SSA representation for machine code, as well as a register
41 allocated, non-SSA form.
42
43 Because the Machine* representation must work regardless of the target machine,
44 it contains very little semantic information about the program.  To get semantic
45 information about the program, a layer of Target description datastructures are
46 used, defined in include/llvm/Target.
47
48 Note that there is some amount of complexity that the X86 backend contains due
49 to the Sparc backend's legacy requirements.  These should eventually fade away
50 as the project progresses.
51
52
53 SSA Instruction Representation
54 ------------------------------
55 Target machine instructions are represented as instances of MachineInstr, and
56 all specific machine instruction types should have an entry in the
57 InstructionInfo table defined through X86InstrInfo.def.  In the X86 backend,
58 there are two particularly interesting forms of machine instruction: those that
59 produce a value (such as add), and those that do not (such as a store).
60
61 Instructions that produce a value use Operand #0 as the "destination" register.
62 When printing the assembly code with the built-in machine instruction printer,
63 these destination registers will be printed to the left side of an '=' sign, as
64 in: %reg1027 = addl %reg1026, %reg1025
65
66 This 'addl' MachineInstruction contains three "operands": the first is the
67 destination register (#1027), the second is the first source register (#1026)
68 and the third is the second source register (#1025).  Never forget the
69 destination register will show up in the MachineInstr operands vector.  The code
70 to generate this instruction looks like this:
71
72   BuildMI(BB, X86::ADDrr32, 2, 1027).addReg(1026).addReg(1025);
73
74 The first argument to BuildMI is the basic block to append the machine
75 instruction to, the second is the opcode, the third is the number of operands,
76 the fourth is the destination register.  The two addReg calls specify operands
77 in order.
78
79 MachineInstrs that do not produce a value do not have this implicit first
80 operand, they simply have #operands = #uses.  To create them, simply do not
81 specify a destination register to the BuildMI call.
82
83
84 ======================================
85 III. Lazy Function Resolution in Jello
86 ======================================
87
88 Jello is a designed to be a JIT compiler for LLVM code.  This implies that call
89 instructions may be emitted before the function they call is compiled.  In order
90 to support this, Jello currently emits unresolved call instructions to call to a
91 null pointer.  When the call instruction is executed, a segmentation fault will
92 be generated.
93
94 Jello installs a trap handler for SIGSEGV, in order to trap these events.  When
95 a SIGSEGV occurs, first we check to see if it's due to lazy function resolution,
96 if so, we look up the return address of the function call (which was pushed onto
97 the stack by the call instruction).  Given the return address of the call, we
98 consult a map to figure out which function was supposed to be called from that
99 location.
100
101 If the function has not been code generated yet, it is at this time.  Finally,
102 the EIP of the process is modified to point to the real function address, the
103 original call instruction is updated, and the SIGSEGV handler returns, causing
104 execution to start in the called function.  Because we update the original call
105 instruction, we should only get at most one signal for each call site.
106
107 Note that this approach does not work for indirect calls.  The problem with
108 indirect calls is that taking the address of a function would not cause a fault
109 (it would simply copy null into a register), so we would only find out about the
110 problem when the indirect call itself was made.  At this point we would have no
111 way of knowing what the intended function destination was.  Because of this, we
112 immediately code generate functions whenever they have their address taken,
113 side-stepping the problem completely.
114
115
116 ======================
117 IV. Source Code Layout
118 ======================
119
120 The LLVM-JIT is composed of source files primarily in the following locations:
121
122 include/llvm/CodeGen
123 --------------------
124 This directory contains header files that are used to represent the program in a
125 machine specific representation.  It currently also contains a bunch of stuff
126 used by the Sparc backend that we don't want to get mixed up in, such as
127 register allocation internals.
128
129 include/llvm/Target
130 -------------------
131 This directory contains header files that are used to interpret the machine
132 specific representation of the program.  This allows us to write generic
133 transformations that will work on any target that implements the interfaces
134 defined in this directory.  The only classes used by the X86 backend so far are
135 the TargetMachine, TargetData, MachineInstrInfo, and MRegisterInfo classes.
136
137 lib/CodeGen
138 -----------
139 This directory will contain all of the target independent transformations (for
140 example, register allocation) that we write.  These transformations should only
141 use information exposed through the Target interface, they should not include
142 any target specific header files.
143
144 lib/Target/X86
145 --------------
146 This directory contains the machine description for X86 that is required to the
147 rest of the compiler working.  It contains any code that is truly specific to
148 the X86 backend, for example the instruction selector and machine code emitter.
149
150 tools/jello
151 -----------
152 This directory contains the top-level code for the JIT compiler.  This code
153 basically boils down to a call to TargetMachine::addPassesToJITCompile.  As we
154 progress with the project, this will also contain the compile-dispatch-recompile
155 loop.
156
157 test/Regression/Jello
158 ---------------------
159 This directory contains regression tests for the JIT.  Initially it contains a
160 bunch of really trivial testcases that we should build up to supporting.
161
162
163 ==================================================
164 V. Strange Things, or, Things That Should Be Known
165 ==================================================
166
167 Representing memory in MachineInstrs
168 ------------------------------------
169
170 The x86 has a very, uhm, flexible, way of accessing memory.  It is capable of
171 addressing memory addresses of the following form directly in integer
172 instructions (which use ModR/M addressing):
173
174    Base+[1,2,4,8]*IndexReg+Disp32
175
176 Wow, that's crazy.  In order to represent this, LLVM tracks no less that 4
177 operands for each memory operand of this form.  This means that the "load" form
178 of 'mov' has the following "Operands" in this order:
179
180 Index:        0     |    1        2       3           4
181 Meaning:   DestReg, | BaseReg,  Scale, IndexReg, Displacement
182 OperandTy: VirtReg, | VirtReg, UnsImm, VirtReg,   SignExtImm
183
184 Stores and all other instructions treat the four memory operands in the same
185 way, in the same order.
186
187
188 ==========================
189 VI. TODO / Future Projects
190 ==========================
191
192 There are a large number of things remaining to do.  Here is a partial list:
193
194 Critical path:
195 -------------
196
197 1. Finish dumb instruction selector
198
199 Next Phase:
200 -----------
201 1. Implement linear time optimal instruction selector
202 2. Implement smarter (linear scan?) register allocator
203
204 After this project:
205 -------------------
206 1. Implement lots of nifty runtime optimizations
207 2. Implement a static compiler backend for x86 (might come almost for free...)
208 3. Implement new targets: IA64? X86-64? M68k? MMIX?  Who knows...
209
210 Infrastructure Improvements:
211 ----------------------------
212
213 1. Bytecode is designed to be able to read particular functions from the
214    bytecode without having to read the whole program.  Bytecode reader should be
215    extended to allow on-demand loading of functions.
216
217 2. PassManager needs to be able to run just a single function through a pipeline
218    of FunctionPass's.
219
220 3. llvmgcc needs to be modified to output 32-bit little endian LLVM files.
221    Preferably it will be parameterizable so that multiple binaries need not
222    exist.  Until this happens, we will be restricted to using type safe
223    programs (most of the Olden suite and many smaller tests), which should be
224    sufficient for our 497 project.  Additionally there are a few places in the
225    LLVM infrastructure where we assume Sparc TargetData layout.  These should
226    be easy to factor out and identify though.