blob: 20f2c96a2b56b8e187468540d49474166644ae3e [file] [log] [blame]
NAKAMURA Takumi9b32e0f2012-03-03 04:32:33 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
Chris Lattnerf689d572004-05-23 21:02:20 +00002 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Gordon Henriksen0076a672007-09-27 19:31:36 +00005 <meta http-equiv="Content-Type" Content="text/html; charset=UTF-8" >
Chris Lattnerf689d572004-05-23 21:02:20 +00006 <title>Accurate Garbage Collection with LLVM</title>
Daniel Dunbar46d611a2012-04-19 20:20:34 +00007 <link rel="stylesheet" href="_static/llvm.css" type="text/css">
Gordon Henriksen0076a672007-09-27 19:31:36 +00008 <style type="text/css">
9 .rowhead { text-align: left; background: inherit; }
10 .indent { padding-left: 1em; }
11 .optl { color: #BFBFBF; }
12 </style>
Chris Lattnerf689d572004-05-23 21:02:20 +000013</head>
14<body>
15
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +000016<h1>
Chris Lattnerf689d572004-05-23 21:02:20 +000017 Accurate Garbage Collection with LLVM
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +000018</h1>
Chris Lattnerf689d572004-05-23 21:02:20 +000019
20<ol>
21 <li><a href="#introduction">Introduction</a>
22 <ul>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +000023 <li><a href="#feature">Goals and non-goals</a></li>
Chris Lattnerf689d572004-05-23 21:02:20 +000024 </ul>
25 </li>
26
Gordon Henriksen29a25eb2009-03-02 03:47:20 +000027 <li><a href="#quickstart">Getting started</a>
Chris Lattnerf689d572004-05-23 21:02:20 +000028 <ul>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +000029 <li><a href="#quickstart-compiler">In your compiler</a></li>
30 <li><a href="#quickstart-runtime">In your runtime library</a></li>
31 <li><a href="#shadow-stack">About the shadow stack</a></li>
Gordon Henriksen0076a672007-09-27 19:31:36 +000032 </ul>
33 </li>
34
Gordon Henriksen7843c162007-12-11 00:30:17 +000035 <li><a href="#core">Core support</a>
Gordon Henriksen0076a672007-09-27 19:31:36 +000036 <ul>
Gordon Henriksen7843c162007-12-11 00:30:17 +000037 <li><a href="#gcattr">Specifying GC code generation:
38 <tt>gc "..."</tt></a></li>
Gordon Henriksen0076a672007-09-27 19:31:36 +000039 <li><a href="#gcroot">Identifying GC roots on the stack:
40 <tt>llvm.gcroot</tt></a></li>
41 <li><a href="#barriers">Reading and writing references in the heap</a>
42 <ul>
43 <li><a href="#gcwrite">Write barrier: <tt>llvm.gcwrite</tt></a></li>
44 <li><a href="#gcread">Read barrier: <tt>llvm.gcread</tt></a></li>
45 </ul>
46 </li>
47 </ul>
48 </li>
49
Gordon Henriksen29a25eb2009-03-02 03:47:20 +000050 <li><a href="#plugin">Compiler plugin interface</a>
Chris Lattnerf689d572004-05-23 21:02:20 +000051 <ul>
Gordon Henriksen0076a672007-09-27 19:31:36 +000052 <li><a href="#collector-algos">Overview of available features</a></li>
53 <li><a href="#stack-map">Computing stack maps</a></li>
54 <li><a href="#init-roots">Initializing roots to null:
55 <tt>InitRoots</tt></a></li>
56 <li><a href="#custom">Custom lowering of intrinsics: <tt>CustomRoots</tt>,
57 <tt>CustomReadBarriers</tt>, and <tt>CustomWriteBarriers</tt></a></li>
58 <li><a href="#safe-points">Generating safe points:
59 <tt>NeededSafePoints</tt></a></li>
60 <li><a href="#assembly">Emitting assembly code:
Gordon Henriksen75609c72008-08-24 03:18:23 +000061 <tt>GCMetadataPrinter</tt></a></li>
Chris Lattner3e5d2a62004-07-09 05:03:54 +000062 </ul>
Chris Lattner71679332004-05-27 05:52:10 +000063 </li>
Chris Lattnerf689d572004-05-23 21:02:20 +000064
Gordon Henriksen0076a672007-09-27 19:31:36 +000065 <li><a href="#runtime-impl">Implementing a collector runtime</a>
66 <ul>
67 <li><a href="#gcdescriptors">Tracing GC pointers from heap
68 objects</a></li>
69 </ul>
70 </li>
71
72 <li><a href="#references">References</a></li>
73
Chris Lattnerf689d572004-05-23 21:02:20 +000074</ol>
75
76<div class="doc_author">
Gordon Henriksen0076a672007-09-27 19:31:36 +000077 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a> and
78 Gordon Henriksen</p>
Chris Lattnerf689d572004-05-23 21:02:20 +000079</div>
80
81<!-- *********************************************************************** -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +000082<h2>
Chris Lattnerf689d572004-05-23 21:02:20 +000083 <a name="introduction">Introduction</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +000084</h2>
Chris Lattnerf689d572004-05-23 21:02:20 +000085<!-- *********************************************************************** -->
86
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +000087<div>
Chris Lattnerf689d572004-05-23 21:02:20 +000088
89<p>Garbage collection is a widely used technique that frees the programmer from
Gordon Henriksen0076a672007-09-27 19:31:36 +000090having to know the lifetimes of heap objects, making software easier to produce
91and maintain. Many programming languages rely on garbage collection for
92automatic memory management. There are two primary forms of garbage collection:
Chris Lattnerf689d572004-05-23 21:02:20 +000093conservative and accurate.</p>
94
95<p>Conservative garbage collection often does not require any special support
96from either the language or the compiler: it can handle non-type-safe
97programming languages (such as C/C++) and does not require any special
Gordon Henriksen0076a672007-09-27 19:31:36 +000098information from the compiler. The
Jeff Cohen43a2fb72007-04-18 17:26:14 +000099<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/">Boehm collector</a> is
100an example of a state-of-the-art conservative collector.</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000101
102<p>Accurate garbage collection requires the ability to identify all pointers in
103the program at run-time (which requires that the source-language be type-safe in
Gordon Henriksen0076a672007-09-27 19:31:36 +0000104most cases). Identifying pointers at run-time requires compiler support to
Chris Lattnerf689d572004-05-23 21:02:20 +0000105locate all places that hold live pointer variables at run-time, including the
Gordon Henriksen0076a672007-09-27 19:31:36 +0000106<a href="#gcroot">processor stack and registers</a>.</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000107
Gordon Henriksen0076a672007-09-27 19:31:36 +0000108<p>Conservative garbage collection is attractive because it does not require any
109special compiler support, but it does have problems. In particular, because the
Chris Lattnerf689d572004-05-23 21:02:20 +0000110conservative garbage collector cannot <i>know</i> that a particular word in the
111machine is a pointer, it cannot move live objects in the heap (preventing the
112use of compacting and generational GC algorithms) and it can occasionally suffer
113from memory leaks due to integer values that happen to point to objects in the
Gordon Henriksen0076a672007-09-27 19:31:36 +0000114program. In addition, some aggressive compiler transformations can break
115conservative garbage collectors (though these seem rare in practice).</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000116
Gordon Henriksen0076a672007-09-27 19:31:36 +0000117<p>Accurate garbage collectors do not suffer from any of these problems, but
118they can suffer from degraded scalar optimization of the program. In particular,
Chris Lattnerf689d572004-05-23 21:02:20 +0000119because the runtime must be able to identify and update all pointers active in
Gordon Henriksen0076a672007-09-27 19:31:36 +0000120the program, some optimizations are less effective. In practice, however, the
Chris Lattner784ae662009-05-13 18:02:09 +0000121locality and performance benefits of using aggressive garbage collection
Gordon Henriksen0076a672007-09-27 19:31:36 +0000122techniques dominates any low-level losses.</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000123
Gordon Henriksen0076a672007-09-27 19:31:36 +0000124<p>This document describes the mechanisms and interfaces provided by LLVM to
125support accurate garbage collection.</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000126
Chris Lattnerf689d572004-05-23 21:02:20 +0000127<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000128<h3>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000129 <a name="feature">Goals and non-goals</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000130</h3>
Chris Lattnerf689d572004-05-23 21:02:20 +0000131
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000132<div>
Chris Lattnerf689d572004-05-23 21:02:20 +0000133
Gordon Henriksen0076a672007-09-27 19:31:36 +0000134<p>LLVM's intermediate representation provides <a href="#intrinsics">garbage
Chris Lattner851b7712008-04-24 05:59:56 +0000135collection intrinsics</a> that offer support for a broad class of
Gordon Henriksen0076a672007-09-27 19:31:36 +0000136collector models. For instance, the intrinsics permit:</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000137
Gordon Henriksen0076a672007-09-27 19:31:36 +0000138<ul>
139 <li>semi-space collectors</li>
140 <li>mark-sweep collectors</li>
141 <li>generational collectors</li>
142 <li>reference counting</li>
143 <li>incremental collectors</li>
144 <li>concurrent collectors</li>
145 <li>cooperative collectors</li>
146</ul>
Chris Lattnerf689d572004-05-23 21:02:20 +0000147
Gordon Henriksen0076a672007-09-27 19:31:36 +0000148<p>We hope that the primitive support built into the LLVM IR is sufficient to
149support a broad class of garbage collected languages including Scheme, ML, Java,
150C#, Perl, Python, Lua, Ruby, other scripting languages, and more.</p>
151
NAKAMURA Takumi6bd36d52011-04-09 09:51:57 +0000152<p>However, LLVM does not itself provide a garbage collector&mdash;this should
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000153be part of your language's runtime library. LLVM provides a framework for
154compile time <a href="#plugin">code generation plugins</a>. The role of these
155plugins is to generate code and data structures which conforms to the <em>binary
156interface</em> specified by the <em>runtime library</em>. This is similar to the
157relationship between LLVM and DWARF debugging info, for example. The
158difference primarily lies in the lack of an established standard in the domain
NAKAMURA Takumi6bd36d52011-04-09 09:51:57 +0000159of garbage collection&mdash;thus the plugins.</p>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000160
161<p>The aspects of the binary interface with which LLVM's GC support is
162concerned are:</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000163
164<ul>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000165 <li>Creation of GC-safe points within code where collection is allowed to
166 execute safely.</li>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000167 <li>Computation of the stack map. For each safe point in the code, object
168 references within the stack frame must be identified so that the
169 collector may traverse and perhaps update them.</li>
170 <li>Write barriers when storing object references to the heap. These are
171 commonly used to optimize incremental scans in generational
172 collectors.</li>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000173 <li>Emission of read barriers when loading object references. These are
174 useful for interoperating with concurrent collectors.</li>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000175</ul>
Chris Lattnerf689d572004-05-23 21:02:20 +0000176
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000177<p>There are additional areas that LLVM does not directly address:</p>
178
179<ul>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000180 <li>Registration of global roots with the runtime.</li>
181 <li>Registration of stack map entries with the runtime.</li>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000182 <li>The functions used by the program to allocate memory, trigger a
183 collection, etc.</li>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000184 <li>Computation or compilation of type maps, or registration of them with
185 the runtime. These are used to crawl the heap for object
186 references.</li>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000187</ul>
188
189<p>In general, LLVM's support for GC does not include features which can be
190adequately addressed with other features of the IR and does not specify a
191particular binary interface. On the plus side, this means that you should be
192able to integrate LLVM with an existing runtime. On the other hand, it leaves
193a lot of work for the developer of a novel language. However, it's easy to get
194started quickly and scale up to a more sophisticated implementation as your
195compiler matures.</p>
196
Chris Lattnerf689d572004-05-23 21:02:20 +0000197</div>
198
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000199</div>
200
Chris Lattnerf689d572004-05-23 21:02:20 +0000201<!-- *********************************************************************** -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000202<h2>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000203 <a name="quickstart">Getting started</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000204</h2>
Chris Lattnerf689d572004-05-23 21:02:20 +0000205<!-- *********************************************************************** -->
206
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000207<div>
Chris Lattnerf689d572004-05-23 21:02:20 +0000208
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000209<p>Using a GC with LLVM implies many things, for example:</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000210
211<ul>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000212 <li>Write a runtime library or find an existing one which implements a GC
213 heap.<ol>
214 <li>Implement a memory allocator.</li>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000215 <li>Design a binary interface for the stack map, used to identify
216 references within a stack frame on the machine stack.*</li>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000217 <li>Implement a stack crawler to discover functions on the call stack.*</li>
218 <li>Implement a registry for global roots.</li>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000219 <li>Design a binary interface for type maps, used to identify references
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000220 within heap objects.</li>
221 <li>Implement a collection routine bringing together all of the above.</li>
222 </ol></li>
223 <li>Emit compatible code from your compiler.<ul>
224 <li>Initialization in the main function.</li>
225 <li>Use the <tt>gc "..."</tt> attribute to enable GC code generation
226 (or <tt>F.setGC("...")</tt>).</li>
227 <li>Use <tt>@llvm.gcroot</tt> to mark stack roots.</li>
228 <li>Use <tt>@llvm.gcread</tt> and/or <tt>@llvm.gcwrite</tt> to
229 manipulate GC references, if necessary.</li>
230 <li>Allocate memory using the GC allocation routine provided by the
231 runtime library.</li>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000232 <li>Generate type maps according to your runtime's binary interface.</li>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000233 </ul></li>
234 <li>Write a compiler plugin to interface LLVM with the runtime library.*<ul>
235 <li>Lower <tt>@llvm.gcread</tt> and <tt>@llvm.gcwrite</tt> to appropriate
236 code sequences.*</li>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000237 <li>Compile LLVM's stack map to the binary form expected by the
238 runtime.</li>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000239 </ul></li>
240 <li>Load the plugin into the compiler. Use <tt>llc -load</tt> or link the
241 plugin statically with your language's compiler.*</li>
242 <li>Link program executables with the runtime.</li>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000243</ul>
244
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000245<p>To help with several of these tasks (those indicated with a *), LLVM
246includes a highly portable, built-in ShadowStack code generator. It is compiled
247into <tt>llc</tt> and works even with the interpreter and C backends.</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000248
Chris Lattnerf689d572004-05-23 21:02:20 +0000249<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000250<h3>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000251 <a name="quickstart-compiler">In your compiler</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000252</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000253
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000254<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000255
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000256<p>To turn the shadow stack on for your functions, first call:</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000257
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000258<div class="doc_code"><pre
259>F.setGC("shadow-stack");</pre></div>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000260
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000261<p>for each function your compiler emits. Since the shadow stack is built into
262LLVM, you do not need to load a plugin.</p>
263
264<p>Your compiler must also use <tt>@llvm.gcroot</tt> as documented.
265Don't forget to create a root for each intermediate value that is generated
266when evaluating an expression. In <tt>h(f(), g())</tt>, the result of
267<tt>f()</tt> could easily be collected if evaluating <tt>g()</tt> triggers a
268collection.</p>
269
270<p>There's no need to use <tt>@llvm.gcread</tt> and <tt>@llvm.gcwrite</tt> over
271plain <tt>load</tt> and <tt>store</tt> for now. You will need them when
272switching to a more advanced GC.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000273
274</div>
275
276<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000277<h3>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000278 <a name="quickstart-runtime">In your runtime</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000279</h3>
Chris Lattnerf689d572004-05-23 21:02:20 +0000280
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000281<div>
Chris Lattnerf689d572004-05-23 21:02:20 +0000282
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000283<p>The shadow stack doesn't imply a memory allocation algorithm. A semispace
284collector or building atop <tt>malloc</tt> are great places to start, and can
285be implemented with very little code.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000286
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000287<p>When it comes time to collect, however, your runtime needs to traverse the
288stack roots, and for this it needs to integrate with the shadow stack. Luckily,
289doing so is very simple. (This code is heavily commented to help you
290understand the data structure, but there are only 20 lines of meaningful
291code.)</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000292
NAKAMURA Takumi0300d882011-08-12 06:17:17 +0000293<pre class="doc_code">
294/// @brief The map for a single function's stack frame. One of these is
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000295/// compiled as constant data into the executable for each function.
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000296///
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000297/// Storage of metadata values is elided if the %metadata parameter to
298/// @llvm.gcroot is null.
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000299struct FrameMap {
300 int32_t NumRoots; //&lt; Number of roots in stack frame.
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000301 int32_t NumMeta; //&lt; Number of metadata entries. May be &lt; NumRoots.
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000302 const void *Meta[0]; //&lt; Metadata for each root.
303};
304
305/// @brief A link in the dynamic shadow stack. One of these is embedded in the
306/// stack frame of each function on the call stack.
307struct StackEntry {
308 StackEntry *Next; //&lt; Link to next stack entry (the caller's).
309 const FrameMap *Map; //&lt; Pointer to constant FrameMap.
310 void *Roots[0]; //&lt; Stack roots (in-place array).
311};
312
313/// @brief The head of the singly-linked list of StackEntries. Functions push
314/// and pop onto this in their prologue and epilogue.
315///
316/// Since there is only a global list, this technique is not threadsafe.
317StackEntry *llvm_gc_root_chain;
318
319/// @brief Calls Visitor(root, meta) for each GC root on the stack.
320/// root and meta are exactly the values passed to
321/// <tt>@llvm.gcroot</tt>.
322///
323/// Visitor could be a function to recursively mark live objects. Or it
324/// might copy them to another heap or generation.
325///
326/// @param Visitor A function to invoke for every GC root on the stack.
327void visitGCRoots(void (*Visitor)(void **Root, const void *Meta)) {
328 for (StackEntry *R = llvm_gc_root_chain; R; R = R->Next) {
329 unsigned i = 0;
330
331 // For roots [0, NumMeta), the metadata pointer is in the FrameMap.
332 for (unsigned e = R->Map->NumMeta; i != e; ++i)
Benjamin Kramereaccdd32009-08-05 15:42:44 +0000333 Visitor(&amp;R->Roots[i], R->Map->Meta[i]);
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000334
335 // For roots [NumMeta, NumRoots), the metadata pointer is null.
336 for (unsigned e = R->Map->NumRoots; i != e; ++i)
Benjamin Kramereaccdd32009-08-05 15:42:44 +0000337 Visitor(&amp;R->Roots[i], NULL);
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000338 }
NAKAMURA Takumi0300d882011-08-12 06:17:17 +0000339}</pre>
340
341</div>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000342
Gordon Henriksen0076a672007-09-27 19:31:36 +0000343<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000344<h3>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000345 <a name="shadow-stack">About the shadow stack</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000346</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000347
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000348<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000349
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000350<p>Unlike many GC algorithms which rely on a cooperative code generator to
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000351compile stack maps, this algorithm carefully maintains a linked list of stack
352roots [<a href="#henderson02">Henderson2002</a>]. This so-called "shadow stack"
353mirrors the machine stack. Maintaining this data structure is slower than using
354a stack map compiled into the executable as constant data, but has a significant
355portability advantage because it requires no special support from the target
356code generator, and does not require tricky platform-specific code to crawl
357the machine stack.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000358
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000359<p>The tradeoff for this simplicity and portability is:</p>
360
361<ul>
362 <li>High overhead per function call.</li>
363 <li>Not thread-safe.</li>
364</ul>
365
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000366<p>Still, it's an easy way to get started. After your compiler and runtime are
367up and running, writing a <a href="#plugin">plugin</a> will allow you to take
368advantage of <a href="#collector-algos">more advanced GC features</a> of LLVM
369in order to improve performance.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000370
371</div>
372
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000373</div>
374
Gordon Henriksen0076a672007-09-27 19:31:36 +0000375<!-- *********************************************************************** -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000376<h2>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000377 <a name="core">IR features</a><a name="intrinsics"></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000378</h2>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000379<!-- *********************************************************************** -->
380
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000381<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000382
383<p>This section describes the garbage collection facilities provided by the
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000384<a href="LangRef.html">LLVM intermediate representation</a>. The exact behavior
385of these IR features is specified by the binary interface implemented by a
386<a href="#plugin">code generation plugin</a>, not by this document.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000387
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000388<p>These facilities are limited to those strictly necessary; they are not
389intended to be a complete interface to any garbage collector. A program will
390need to interface with the GC library using the facilities provided by that
391program.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000392
Gordon Henriksen0076a672007-09-27 19:31:36 +0000393<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000394<h3>
Gordon Henriksen7843c162007-12-11 00:30:17 +0000395 <a name="gcattr">Specifying GC code generation: <tt>gc "..."</tt></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000396</h3>
Gordon Henriksen7843c162007-12-11 00:30:17 +0000397
NAKAMURA Takumi0300d882011-08-12 06:17:17 +0000398<div>
399
Gordon Henriksen7843c162007-12-11 00:30:17 +0000400<div class="doc_code"><tt>
Benjamin Kramereaccdd32009-08-05 15:42:44 +0000401 define <i>ty</i> @<i>name</i>(...) <span style="text-decoration: underline">gc "<i>name</i>"</span> { ...
Gordon Henriksen7843c162007-12-11 00:30:17 +0000402</tt></div>
403
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000404<p>The <tt>gc</tt> function attribute is used to specify the desired GC style
405to the compiler. Its programmatic equivalent is the <tt>setGC</tt> method of
406<tt>Function</tt>.</p>
Gordon Henriksen7843c162007-12-11 00:30:17 +0000407
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000408<p>Setting <tt>gc "<i>name</i>"</tt> on a function triggers a search for a
409matching code generation plugin "<i>name</i>"; it is that plugin which defines
410the exact nature of the code generated to support GC. If none is found, the
411compiler will raise an error.</p>
412
413<p>Specifying the GC style on a per-function basis allows LLVM to link together
414programs that use different garbage collection algorithms (or none at all).</p>
Gordon Henriksen7843c162007-12-11 00:30:17 +0000415
416</div>
417
418<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000419<h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000420 <a name="gcroot">Identifying GC roots on the stack: <tt>llvm.gcroot</tt></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000421</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000422
NAKAMURA Takumi0300d882011-08-12 06:17:17 +0000423<div>
424
Chris Lattnerf689d572004-05-23 21:02:20 +0000425<div class="doc_code"><tt>
Chris Lattner0b9db522008-04-24 06:00:30 +0000426 void @llvm.gcroot(i8** %ptrloc, i8* %metadata)
Chris Lattnerf689d572004-05-23 21:02:20 +0000427</tt></div>
428
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000429<p>The <tt>llvm.gcroot</tt> intrinsic is used to inform LLVM that a stack
430variable references an object on the heap and is to be tracked for garbage
431collection. The exact impact on generated code is specified by a <a
Jia Liud9605a62012-03-02 11:30:51 +0000432href="#plugin">compiler plugin</a>. All calls to <tt>llvm.gcroot</tt> <b>must</b> reside
433 inside the first basic block.</p>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000434
435<p>A compiler which uses mem2reg to raise imperative code using <tt>alloca</tt>
436into SSA form need only add a call to <tt>@llvm.gcroot</tt> for those variables
437which a pointers into the GC heap.</p>
438
439<p>It is also important to mark intermediate values with <tt>llvm.gcroot</tt>.
440For example, consider <tt>h(f(), g())</tt>. Beware leaking the result of
Jia Liud9605a62012-03-02 11:30:51 +0000441<tt>f()</tt> in the case that <tt>g()</tt> triggers a collection. Note, that
442stack variables must be initialized and marked with <tt>llvm.gcroot</tt> in
443function's prologue.</p>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000444
445<p>The first argument <b>must</b> be a value referring to an alloca instruction
Gordon Henriksen0076a672007-09-27 19:31:36 +0000446or a bitcast of an alloca. The second contains a pointer to metadata that
447should be associated with the pointer, and <b>must</b> be a constant or global
448value address. If your target collector uses tags, use a null pointer for
449metadata.</p>
450
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000451<p>The <tt>%metadata</tt> argument can be used to avoid requiring heap objects
452to have 'isa' pointers or tag bits. [<a href="#appel89">Appel89</a>, <a
453href="#goldberg91">Goldberg91</a>, <a href="#tolmach94">Tolmach94</a>] If
454specified, its value will be tracked along with the location of the pointer in
455the stack frame.</p>
456
Gordon Henriksen0076a672007-09-27 19:31:36 +0000457<p>Consider the following fragment of Java code:</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000458
NAKAMURA Takumi0300d882011-08-12 06:17:17 +0000459<pre class="doc_code">
Chris Lattnerf689d572004-05-23 21:02:20 +0000460 {
461 Object X; // A null-initialized reference to an object
462 ...
463 }
464</pre>
465
Gordon Henriksen0076a672007-09-27 19:31:36 +0000466<p>This block (which may be located in the middle of a function or in a loop
467nest), could be compiled to this LLVM code:</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000468
NAKAMURA Takumi0300d882011-08-12 06:17:17 +0000469<pre class="doc_code">
Chris Lattnerf689d572004-05-23 21:02:20 +0000470Entry:
471 ;; In the entry block for the function, allocate the
472 ;; stack space for X, which is an LLVM pointer.
473 %X = alloca %Object*
Gordon Henriksen0076a672007-09-27 19:31:36 +0000474
475 ;; Tell LLVM that the stack space is a stack root.
476 ;; Java has type-tags on objects, so we pass null as metadata.
477 %tmp = bitcast %Object** %X to i8**
Duncan Sands85921272012-05-03 15:25:19 +0000478 call void @llvm.gcroot(i8** %tmp, i8* null)
Chris Lattnerf689d572004-05-23 21:02:20 +0000479 ...
480
481 ;; "CodeBlock" is the block corresponding to the start
Reid Spencerbbea18c2004-05-25 08:45:31 +0000482 ;; of the scope above.
Chris Lattnerf689d572004-05-23 21:02:20 +0000483CodeBlock:
Gordon Henriksen0076a672007-09-27 19:31:36 +0000484 ;; Java null-initializes pointers.
485 store %Object* null, %Object** %X
486
Chris Lattnerf689d572004-05-23 21:02:20 +0000487 ...
488
489 ;; As the pointer goes out of scope, store a null value into
490 ;; it, to indicate that the value is no longer live.
491 store %Object* null, %Object** %X
492 ...
493</pre>
494
495</div>
496
497<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000498<h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000499 <a name="barriers">Reading and writing references in the heap</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000500</h3>
Chris Lattnerf689d572004-05-23 21:02:20 +0000501
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000502<div>
Chris Lattnerf689d572004-05-23 21:02:20 +0000503
Gordon Henriksen0076a672007-09-27 19:31:36 +0000504<p>Some collectors need to be informed when the mutator (the program that needs
505garbage collection) either reads a pointer from or writes a pointer to a field
506of a heap object. The code fragments inserted at these points are called
507<em>read barriers</em> and <em>write barriers</em>, respectively. The amount of
508code that needs to be executed is usually quite small and not on the critical
509path of any computation, so the overall performance impact of the barrier is
510tolerable.</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000511
Gordon Henriksen0076a672007-09-27 19:31:36 +0000512<p>Barriers often require access to the <em>object pointer</em> rather than the
513<em>derived pointer</em> (which is a pointer to the field within the
514object). Accordingly, these intrinsics take both pointers as separate arguments
515for completeness. In this snippet, <tt>%object</tt> is the object pointer, and
516<tt>%derived</tt> is the derived pointer:</p>
517
Chris Lattner851b7712008-04-24 05:59:56 +0000518<blockquote><pre>
519 ;; An array type.
Gordon Henriksen0076a672007-09-27 19:31:36 +0000520 %class.Array = type { %class.Object, i32, [0 x %class.Object*] }
Chris Lattner851b7712008-04-24 05:59:56 +0000521 ...
Gordon Henriksen0076a672007-09-27 19:31:36 +0000522
523 ;; Load the object pointer from a gcroot.
524 %object = load %class.Array** %object_addr
525
526 ;; Compute the derived pointer.
Chris Lattner851b7712008-04-24 05:59:56 +0000527 %derived = getelementptr %object, i32 0, i32 2, i32 %n</pre></blockquote>
Chris Lattnerf689d572004-05-23 21:02:20 +0000528
Gordon Henriksen14248612009-03-06 02:42:47 +0000529<p>LLVM does not enforce this relationship between the object and derived
530pointer (although a <a href="#plugin">plugin</a> might). However, it would be
531an unusual collector that violated it.</p>
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000532
Gordon Henriksen14248612009-03-06 02:42:47 +0000533<p>The use of these intrinsics is naturally optional if the target GC does
534require the corresponding barrier. Such a GC plugin will replace the intrinsic
535calls with the corresponding <tt>load</tt> or <tt>store</tt> instruction if they
536are used.</p>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000537
Chris Lattnerf689d572004-05-23 21:02:20 +0000538<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000539<h4>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000540 <a name="gcwrite">Write barrier: <tt>llvm.gcwrite</tt></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000541</h4>
Chris Lattnerf689d572004-05-23 21:02:20 +0000542
NAKAMURA Takumi0300d882011-08-12 06:17:17 +0000543<div>
544
Gordon Henriksen0076a672007-09-27 19:31:36 +0000545<div class="doc_code"><tt>
546void @llvm.gcwrite(i8* %value, i8* %object, i8** %derived)
547</tt></div>
548
Gordon Henriksen0076a672007-09-27 19:31:36 +0000549<p>For write barriers, LLVM provides the <tt>llvm.gcwrite</tt> intrinsic
550function. It has exactly the same semantics as a non-volatile <tt>store</tt> to
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000551the derived pointer (the third argument). The exact code generated is specified
552by a <a href="#plugin">compiler plugin</a>.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000553
554<p>Many important algorithms require write barriers, including generational
555and concurrent collectors. Additionally, write barriers could be used to
556implement reference counting.</p>
557
Gordon Henriksen0076a672007-09-27 19:31:36 +0000558</div>
559
560<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000561<h4>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000562 <a name="gcread">Read barrier: <tt>llvm.gcread</tt></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000563</h4>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000564
NAKAMURA Takumi0300d882011-08-12 06:17:17 +0000565<div>
566
Chris Lattnerf689d572004-05-23 21:02:20 +0000567<div class="doc_code"><tt>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000568i8* @llvm.gcread(i8* %object, i8** %derived)<br>
Chris Lattnerf689d572004-05-23 21:02:20 +0000569</tt></div>
570
Gordon Henriksen0076a672007-09-27 19:31:36 +0000571<p>For read barriers, LLVM provides the <tt>llvm.gcread</tt> intrinsic function.
572It has exactly the same semantics as a non-volatile <tt>load</tt> from the
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000573derived pointer (the second argument). The exact code generated is specified by
574a <a href="#plugin">compiler plugin</a>.</p>
Chris Lattnerf689d572004-05-23 21:02:20 +0000575
Gordon Henriksen0076a672007-09-27 19:31:36 +0000576<p>Read barriers are needed by fewer algorithms than write barriers, and may
577have a greater performance impact since pointer reads are more frequent than
578writes.</p>
579
Gordon Henriksen0076a672007-09-27 19:31:36 +0000580</div>
581
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000582</div>
583
584</div>
585
Gordon Henriksen0076a672007-09-27 19:31:36 +0000586<!-- *********************************************************************** -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000587<h2>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000588 <a name="plugin">Implementing a collector plugin</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000589</h2>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000590<!-- *********************************************************************** -->
591
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000592<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000593
Gordon Henriksen75609c72008-08-24 03:18:23 +0000594<p>User code specifies which GC code generation to use with the <tt>gc</tt>
595function attribute or, equivalently, with the <tt>setGC</tt> method of
Gordon Henriksen7843c162007-12-11 00:30:17 +0000596<tt>Function</tt>.</p>
597
Gordon Henriksen75609c72008-08-24 03:18:23 +0000598<p>To implement a GC plugin, it is necessary to subclass
599<tt>llvm::GCStrategy</tt>, which can be accomplished in a few lines of
Gordon Henriksen0076a672007-09-27 19:31:36 +0000600boilerplate code. LLVM's infrastructure provides access to several important
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000601algorithms. For an uncontroversial collector, all that remains may be to
602compile LLVM's computed stack map to assembly code (using the binary
603representation expected by the runtime library). This can be accomplished in
604about 100 lines of code.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000605
Gordon Henriksen75609c72008-08-24 03:18:23 +0000606<p>This is not the appropriate place to implement a garbage collected heap or a
607garbage collector itself. That code should exist in the language's runtime
Gordon Henriksen29a25eb2009-03-02 03:47:20 +0000608library. The compiler plugin is responsible for generating code which
609conforms to the binary interface defined by library, most essentially the
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000610<a href="#stack-map">stack map</a>.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000611
Gordon Henriksen75609c72008-08-24 03:18:23 +0000612<p>To subclass <tt>llvm::GCStrategy</tt> and register it with the compiler:</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000613
Gordon Henriksen75609c72008-08-24 03:18:23 +0000614<blockquote><pre>// lib/MyGC/MyGC.cpp - Example LLVM GC plugin
615
616#include "llvm/CodeGen/GCStrategy.h"
617#include "llvm/CodeGen/GCMetadata.h"
Gordon Henriksen0076a672007-09-27 19:31:36 +0000618#include "llvm/Support/Compiler.h"
619
620using namespace llvm;
621
622namespace {
Duncan Sands6c5e4352010-05-11 20:16:09 +0000623 class LLVM_LIBRARY_VISIBILITY MyGC : public GCStrategy {
Gordon Henriksen0076a672007-09-27 19:31:36 +0000624 public:
Gordon Henriksen75609c72008-08-24 03:18:23 +0000625 MyGC() {}
Gordon Henriksen0076a672007-09-27 19:31:36 +0000626 };
627
Gordon Henriksen75609c72008-08-24 03:18:23 +0000628 GCRegistry::Add&lt;MyGC&gt;
Gordon Henriksen7843c162007-12-11 00:30:17 +0000629 X("mygc", "My bespoke garbage collector.");
Gordon Henriksen0076a672007-09-27 19:31:36 +0000630}</pre></blockquote>
631
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000632<p>This boilerplate collector does nothing. More specifically:</p>
633
634<ul>
635 <li><tt>llvm.gcread</tt> calls are replaced with the corresponding
636 <tt>load</tt> instruction.</li>
637 <li><tt>llvm.gcwrite</tt> calls are replaced with the corresponding
638 <tt>store</tt> instruction.</li>
639 <li>No safe points are added to the code.</li>
640 <li>The stack map is not compiled into the executable.</li>
641</ul>
642
Gordon Henriksen0076a672007-09-27 19:31:36 +0000643<p>Using the LLVM makefiles (like the <a
644href="http://llvm.org/viewvc/llvm-project/llvm/trunk/projects/sample/">sample
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000645project</a>), this code can be compiled as a plugin using a simple
646makefile:</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000647
648<blockquote><pre
649># lib/MyGC/Makefile
650
651LEVEL := ../..
652LIBRARYNAME = <var>MyGC</var>
653LOADABLE_MODULE = 1
654
655include $(LEVEL)/Makefile.common</pre></blockquote>
656
Gordon Henriksen7843c162007-12-11 00:30:17 +0000657<p>Once the plugin is compiled, code using it may be compiled using <tt>llc
658-load=<var>MyGC.so</var></tt> (though <var>MyGC.so</var> may have some other
659platform-specific extension):</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000660
661<blockquote><pre
Gordon Henriksen7843c162007-12-11 00:30:17 +0000662>$ cat sample.ll
663define void @f() gc "mygc" {
664entry:
665 ret void
666}
667$ llvm-as &lt; sample.ll | llc -load=MyGC.so</pre></blockquote>
668
669<p>It is also possible to statically link the collector plugin into tools, such
670as a language-specific compiler front-end.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000671
Gordon Henriksen0076a672007-09-27 19:31:36 +0000672<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000673<h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000674 <a name="collector-algos">Overview of available features</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000675</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000676
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000677<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000678
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000679<p><tt>GCStrategy</tt> provides a range of features through which a plugin
680may do useful work. Some of these are callbacks, some are algorithms that can
681be enabled, disabled, or customized. This matrix summarizes the supported (and
682planned) features and correlates them with the collection techniques which
683typically require them.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000684
685<table>
686 <tr>
687 <th>Algorithm</th>
688 <th>Done</th>
689 <th>shadow stack</th>
690 <th>refcount</th>
691 <th>mark-sweep</th>
692 <th>copying</th>
693 <th>incremental</th>
694 <th>threaded</th>
695 <th>concurrent</th>
696 </tr>
697 <tr>
698 <th class="rowhead"><a href="#stack-map">stack map</a></th>
699 <td>&#10004;</td>
700 <td></td>
701 <td></td>
702 <td>&#10008;</td>
703 <td>&#10008;</td>
704 <td>&#10008;</td>
705 <td>&#10008;</td>
706 <td>&#10008;</td>
707 </tr>
708 <tr>
709 <th class="rowhead"><a href="#init-roots">initialize roots</a></th>
710 <td>&#10004;</td>
711 <td>&#10008;</td>
712 <td>&#10008;</td>
713 <td>&#10008;</td>
714 <td>&#10008;</td>
715 <td>&#10008;</td>
716 <td>&#10008;</td>
717 <td>&#10008;</td>
718 </tr>
719 <tr class="doc_warning">
720 <th class="rowhead">derived pointers</th>
721 <td>NO</td>
722 <td></td>
723 <td></td>
724 <td></td>
725 <td></td>
726 <td></td>
727 <td>&#10008;*</td>
728 <td>&#10008;*</td>
729 </tr>
730 <tr>
731 <th class="rowhead"><em><a href="#custom">custom lowering</a></em></th>
732 <td>&#10004;</td>
733 <th></th>
734 <th></th>
735 <th></th>
736 <th></th>
737 <th></th>
738 <th></th>
739 <th></th>
740 </tr>
741 <tr>
742 <th class="rowhead indent">gcroot</th>
743 <td>&#10004;</td>
744 <td>&#10008;</td>
745 <td>&#10008;</td>
746 <td></td>
747 <td></td>
748 <td></td>
749 <td></td>
750 <td></td>
751 </tr>
752 <tr>
753 <th class="rowhead indent">gcwrite</th>
754 <td>&#10004;</td>
755 <td></td>
756 <td>&#10008;</td>
757 <td></td>
758 <td></td>
759 <td>&#10008;</td>
760 <td></td>
761 <td>&#10008;</td>
762 </tr>
763 <tr>
764 <th class="rowhead indent">gcread</th>
765 <td>&#10004;</td>
766 <td></td>
767 <td></td>
768 <td></td>
769 <td></td>
770 <td></td>
771 <td></td>
772 <td>&#10008;</td>
773 </tr>
774 <tr>
775 <th class="rowhead"><em><a href="#safe-points">safe points</a></em></th>
776 <td></td>
777 <th></th>
778 <th></th>
779 <th></th>
780 <th></th>
781 <th></th>
782 <th></th>
783 <th></th>
784 </tr>
785 <tr>
786 <th class="rowhead indent">in calls</th>
787 <td>&#10004;</td>
788 <td></td>
789 <td></td>
790 <td>&#10008;</td>
791 <td>&#10008;</td>
792 <td>&#10008;</td>
793 <td>&#10008;</td>
794 <td>&#10008;</td>
795 </tr>
796 <tr>
797 <th class="rowhead indent">before calls</th>
798 <td>&#10004;</td>
799 <td></td>
800 <td></td>
801 <td></td>
802 <td></td>
803 <td></td>
804 <td>&#10008;</td>
805 <td>&#10008;</td>
806 </tr>
807 <tr class="doc_warning">
808 <th class="rowhead indent">for loops</th>
809 <td>NO</td>
810 <td></td>
811 <td></td>
812 <td></td>
813 <td></td>
814 <td></td>
815 <td>&#10008;</td>
816 <td>&#10008;</td>
817 </tr>
818 <tr>
819 <th class="rowhead indent">before escape</th>
820 <td>&#10004;</td>
821 <td></td>
822 <td></td>
823 <td></td>
824 <td></td>
825 <td></td>
826 <td>&#10008;</td>
827 <td>&#10008;</td>
828 </tr>
829 <tr class="doc_warning">
830 <th class="rowhead">emit code at safe points</th>
831 <td>NO</td>
832 <td></td>
833 <td></td>
834 <td></td>
835 <td></td>
836 <td></td>
837 <td>&#10008;</td>
838 <td>&#10008;</td>
839 </tr>
840 <tr>
841 <th class="rowhead"><em>output</em></th>
842 <td></td>
843 <th></th>
844 <th></th>
845 <th></th>
846 <th></th>
847 <th></th>
848 <th></th>
849 <th></th>
850 </tr>
851 <tr>
852 <th class="rowhead indent"><a href="#assembly">assembly</a></th>
853 <td>&#10004;</td>
854 <td></td>
855 <td></td>
856 <td>&#10008;</td>
857 <td>&#10008;</td>
858 <td>&#10008;</td>
859 <td>&#10008;</td>
860 <td>&#10008;</td>
861 </tr>
862 <tr class="doc_warning">
863 <th class="rowhead indent">JIT</th>
864 <td>NO</td>
865 <td></td>
866 <td></td>
867 <td class="optl">&#10008;</td>
868 <td class="optl">&#10008;</td>
869 <td class="optl">&#10008;</td>
870 <td class="optl">&#10008;</td>
871 <td class="optl">&#10008;</td>
872 </tr>
873 <tr class="doc_warning">
874 <th class="rowhead indent">obj</th>
875 <td>NO</td>
876 <td></td>
877 <td></td>
878 <td class="optl">&#10008;</td>
879 <td class="optl">&#10008;</td>
880 <td class="optl">&#10008;</td>
881 <td class="optl">&#10008;</td>
882 <td class="optl">&#10008;</td>
883 </tr>
884 <tr class="doc_warning">
885 <th class="rowhead">live analysis</th>
886 <td>NO</td>
887 <td></td>
888 <td></td>
889 <td class="optl">&#10008;</td>
890 <td class="optl">&#10008;</td>
891 <td class="optl">&#10008;</td>
892 <td class="optl">&#10008;</td>
893 <td class="optl">&#10008;</td>
894 </tr>
895 <tr class="doc_warning">
896 <th class="rowhead">register map</th>
897 <td>NO</td>
898 <td></td>
899 <td></td>
900 <td class="optl">&#10008;</td>
901 <td class="optl">&#10008;</td>
902 <td class="optl">&#10008;</td>
903 <td class="optl">&#10008;</td>
904 <td class="optl">&#10008;</td>
905 </tr>
906 <tr>
907 <td colspan="10">
908 <div><span class="doc_warning">*</span> Derived pointers only pose a
909 hazard to copying collectors.</div>
910 <div><span class="optl">&#10008;</span> in gray denotes a feature which
911 could be utilized if available.</div>
912 </td>
913 </tr>
914</table>
915
916<p>To be clear, the collection techniques above are defined as:</p>
917
918<dl>
919 <dt>Shadow Stack</dt>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000920 <dd>The mutator carefully maintains a linked list of stack roots.</dd>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000921 <dt>Reference Counting</dt>
922 <dd>The mutator maintains a reference count for each object and frees an
923 object when its count falls to zero.</dd>
924 <dt>Mark-Sweep</dt>
925 <dd>When the heap is exhausted, the collector marks reachable objects starting
926 from the roots, then deallocates unreachable objects in a sweep
927 phase.</dd>
928 <dt>Copying</dt>
929 <dd>As reachability analysis proceeds, the collector copies objects from one
930 heap area to another, compacting them in the process. Copying collectors
931 enable highly efficient "bump pointer" allocation and can improve locality
932 of reference.</dd>
933 <dt>Incremental</dt>
934 <dd>(Including generational collectors.) Incremental collectors generally have
935 all the properties of a copying collector (regardless of whether the
936 mature heap is compacting), but bring the added complexity of requiring
937 write barriers.</dd>
938 <dt>Threaded</dt>
939 <dd>Denotes a multithreaded mutator; the collector must still stop the mutator
940 ("stop the world") before beginning reachability analysis. Stopping a
941 multithreaded mutator is a complicated problem. It generally requires
942 highly platform specific code in the runtime, and the production of
943 carefully designed machine code at safe points.</dd>
944 <dt>Concurrent</dt>
945 <dd>In this technique, the mutator and the collector run concurrently, with
946 the goal of eliminating pause times. In a <em>cooperative</em> collector,
947 the mutator further aids with collection should a pause occur, allowing
948 collection to take advantage of multiprocessor hosts. The "stop the world"
949 problem of threaded collectors is generally still present to a limited
950 extent. Sophisticated marking algorithms are necessary. Read barriers may
951 be necessary.</dd>
952</dl>
953
954<p>As the matrix indicates, LLVM's garbage collection infrastructure is already
955suitable for a wide variety of collectors, but does not currently extend to
956multithreaded programs. This will be added in the future as there is
957interest.</p>
958
959</div>
960
961<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000962<h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000963 <a name="stack-map">Computing stack maps</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000964</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000965
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000966<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +0000967
Gordon Henriksen3519d5d2009-03-06 01:57:32 +0000968<p>LLVM automatically computes a stack map. One of the most important features
969of a <tt>GCStrategy</tt> is to compile this information into the executable in
970the binary representation expected by the runtime library.</p>
971
972<p>The stack map consists of the location and identity of each GC root in the
973each function in the module. For each root:</p>
974
975<ul>
976 <li><tt>RootNum</tt>: The index of the root.</li>
977 <li><tt>StackOffset</tt>: The offset of the object relative to the frame
978 pointer.</li>
979 <li><tt>RootMetadata</tt>: The value passed as the <tt>%metadata</tt>
980 parameter to the <a href="#gcroot"><tt>@llvm.gcroot</tt></a> intrinsic.</li>
981</ul>
982
983<p>Also, for the function as a whole:</p>
984
985<ul>
986 <li><tt>getFrameSize()</tt>: The overall size of the function's initial
987 stack frame, not accounting for any dynamic allocation.</li>
988 <li><tt>roots_size()</tt>: The count of roots in the function.</li>
989</ul>
990
991<p>To access the stack map, use <tt>GCFunctionMetadata::roots_begin()</tt> and
992-<tt>end()</tt> from the <tt><a
993href="#assembly">GCMetadataPrinter</a></tt>:</p>
994
Gordon Henriksen0076a672007-09-27 19:31:36 +0000995<blockquote><pre
Gordon Henriksen7843c162007-12-11 00:30:17 +0000996>for (iterator I = begin(), E = end(); I != E; ++I) {
Gordon Henriksen75609c72008-08-24 03:18:23 +0000997 GCFunctionInfo *FI = *I;
998 unsigned FrameSize = FI-&gt;getFrameSize();
999 size_t RootCount = FI-&gt;roots_size();
Gordon Henriksen0076a672007-09-27 19:31:36 +00001000
Gordon Henriksen75609c72008-08-24 03:18:23 +00001001 for (GCFunctionInfo::roots_iterator RI = FI-&gt;roots_begin(),
1002 RE = FI-&gt;roots_end();
1003 RI != RE; ++RI) {
Gordon Henriksen7843c162007-12-11 00:30:17 +00001004 int RootNum = RI->Num;
1005 int RootStackOffset = RI->StackOffset;
1006 Constant *RootMetadata = RI->Metadata;
1007 }
Gordon Henriksen0076a672007-09-27 19:31:36 +00001008}</pre></blockquote>
1009
Gordon Henriksen3519d5d2009-03-06 01:57:32 +00001010<p>If the <tt>llvm.gcroot</tt> intrinsic is eliminated before code generation by
1011a custom lowering pass, LLVM will compute an empty stack map. This may be useful
1012for collector plugins which implement reference counting or a shadow stack.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001013
1014</div>
1015
1016
1017<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001018<h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001019 <a name="init-roots">Initializing roots to null: <tt>InitRoots</tt></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001020</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001021
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +00001022<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001023
1024<blockquote><pre
Gordon Henriksen75609c72008-08-24 03:18:23 +00001025>MyGC::MyGC() {
Gordon Henriksen0076a672007-09-27 19:31:36 +00001026 InitRoots = true;
1027}</pre></blockquote>
1028
1029<p>When set, LLVM will automatically initialize each root to <tt>null</tt> upon
Gordon Henriksen75609c72008-08-24 03:18:23 +00001030entry to the function. This prevents the GC's sweep phase from visiting
1031uninitialized pointers, which will almost certainly cause it to crash. This
1032initialization occurs before custom lowering, so the two may be used
1033together.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001034
Gordon Henriksen75609c72008-08-24 03:18:23 +00001035<p>Since LLVM does not yet compute liveness information, there is no means of
1036distinguishing an uninitialized stack root from an initialized one. Therefore,
1037this feature should be used by all GC plugins. It is enabled by default.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001038
1039</div>
1040
1041
1042<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001043<h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001044 <a name="custom">Custom lowering of intrinsics: <tt>CustomRoots</tt>,
1045 <tt>CustomReadBarriers</tt>, and <tt>CustomWriteBarriers</tt></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001046</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001047
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +00001048<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001049
Gordon Henriksen75609c72008-08-24 03:18:23 +00001050<p>For GCs which use barriers or unusual treatment of stack roots, these
1051flags allow the collector to perform arbitrary transformations of the LLVM
Gordon Henriksen0076a672007-09-27 19:31:36 +00001052IR:</p>
1053
1054<blockquote><pre
Gordon Henriksen75609c72008-08-24 03:18:23 +00001055>class MyGC : public GCStrategy {
Gordon Henriksen0076a672007-09-27 19:31:36 +00001056public:
Gordon Henriksen75609c72008-08-24 03:18:23 +00001057 MyGC() {
Gordon Henriksen0076a672007-09-27 19:31:36 +00001058 CustomRoots = true;
1059 CustomReadBarriers = true;
1060 CustomWriteBarriers = true;
1061 }
1062
Gordon Henriksen7843c162007-12-11 00:30:17 +00001063 virtual bool initializeCustomLowering(Module &amp;M);
1064 virtual bool performCustomLowering(Function &amp;F);
Gordon Henriksen0076a672007-09-27 19:31:36 +00001065};</pre></blockquote>
1066
1067<p>If any of these flags are set, then LLVM suppresses its default lowering for
Gordon Henriksen75609c72008-08-24 03:18:23 +00001068the corresponding intrinsics and instead calls
1069<tt>performCustomLowering</tt>.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001070
1071<p>LLVM's default action for each intrinsic is as follows:</p>
1072
1073<ul>
Gordon Henriksen3519d5d2009-03-06 01:57:32 +00001074 <li><tt>llvm.gcroot</tt>: Leave it alone. The code generator must see it
1075 or the stack map will not be computed.</li>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001076 <li><tt>llvm.gcread</tt>: Substitute a <tt>load</tt> instruction.</li>
1077 <li><tt>llvm.gcwrite</tt>: Substitute a <tt>store</tt> instruction.</li>
1078</ul>
1079
1080<p>If <tt>CustomReadBarriers</tt> or <tt>CustomWriteBarriers</tt> are specified,
Gordon Henriksen7843c162007-12-11 00:30:17 +00001081then <tt>performCustomLowering</tt> <strong>must</strong> eliminate the
1082corresponding barriers.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001083
Gordon Henriksen75609c72008-08-24 03:18:23 +00001084<p><tt>performCustomLowering</tt> must comply with the same restrictions as <a
1085href="WritingAnLLVMPass.html#runOnFunction"><tt
1086>FunctionPass::runOnFunction</tt></a>.
1087Likewise, <tt>initializeCustomLowering</tt> has the same semantics as <a
1088href="WritingAnLLVMPass.html#doInitialization_mod"><tt
1089>Pass::doInitialization(Module&amp;)</tt></a>.</p>
Gordon Henriksen7843c162007-12-11 00:30:17 +00001090
1091<p>The following can be used as a template:</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001092
1093<blockquote><pre
Gordon Henriksen7843c162007-12-11 00:30:17 +00001094>#include "llvm/Module.h"
Gordon Henriksen8790a222007-12-22 23:32:32 +00001095#include "llvm/IntrinsicInst.h"
Gordon Henriksen0076a672007-09-27 19:31:36 +00001096
Gordon Henriksen75609c72008-08-24 03:18:23 +00001097bool MyGC::initializeCustomLowering(Module &amp;M) {
Gordon Henriksen7843c162007-12-11 00:30:17 +00001098 return false;
1099}
Gordon Henriksen0076a672007-09-27 19:31:36 +00001100
Gordon Henriksen75609c72008-08-24 03:18:23 +00001101bool MyGC::performCustomLowering(Function &amp;F) {
Gordon Henriksen7843c162007-12-11 00:30:17 +00001102 bool MadeChange = false;
1103
1104 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Gordon Henriksene09a4f82007-12-22 23:34:26 +00001105 for (BasicBlock::iterator II = BB-&gt;begin(), E = BB-&gt;end(); II != E; )
1106 if (IntrinsicInst *CI = dyn_cast&lt;IntrinsicInst&gt;(II++))
Gordon Henriksen8790a222007-12-22 23:32:32 +00001107 if (Function *F = CI-&gt;getCalledFunction())
1108 switch (F-&gt;getIntrinsicID()) {
1109 case Intrinsic::gcwrite:
Gordon Henriksen7843c162007-12-11 00:30:17 +00001110 // Handle llvm.gcwrite.
Gordon Henriksen8790a222007-12-22 23:32:32 +00001111 CI-&gt;eraseFromParent();
Gordon Henriksen7843c162007-12-11 00:30:17 +00001112 MadeChange = true;
Gordon Henriksen8790a222007-12-22 23:32:32 +00001113 break;
1114 case Intrinsic::gcread:
Gordon Henriksen7843c162007-12-11 00:30:17 +00001115 // Handle llvm.gcread.
Gordon Henriksen8790a222007-12-22 23:32:32 +00001116 CI-&gt;eraseFromParent();
Gordon Henriksen7843c162007-12-11 00:30:17 +00001117 MadeChange = true;
Gordon Henriksen8790a222007-12-22 23:32:32 +00001118 break;
1119 case Intrinsic::gcroot:
Gordon Henriksen7843c162007-12-11 00:30:17 +00001120 // Handle llvm.gcroot.
Gordon Henriksen8790a222007-12-22 23:32:32 +00001121 CI-&gt;eraseFromParent();
Gordon Henriksen7843c162007-12-11 00:30:17 +00001122 MadeChange = true;
Gordon Henriksen8790a222007-12-22 23:32:32 +00001123 break;
Gordon Henriksen7843c162007-12-11 00:30:17 +00001124 }
1125
1126 return MadeChange;
Gordon Henriksen0076a672007-09-27 19:31:36 +00001127}</pre></blockquote>
1128
1129</div>
1130
1131
1132<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001133<h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001134 <a name="safe-points">Generating safe points: <tt>NeededSafePoints</tt></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001135</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001136
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +00001137<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001138
1139<p>LLVM can compute four kinds of safe points:</p>
1140
1141<blockquote><pre
1142>namespace GC {
1143 /// PointKind - The type of a collector-safe point.
1144 ///
1145 enum PointKind {
1146 Loop, //&lt; Instr is a loop (backwards branch).
1147 Return, //&lt; Instr is a return instruction.
1148 PreCall, //&lt; Instr is a call instruction.
1149 PostCall //&lt; Instr is the return address of a call.
1150 };
1151}</pre></blockquote>
1152
1153<p>A collector can request any combination of the four by setting the
1154<tt>NeededSafePoints</tt> mask:</p>
1155
1156<blockquote><pre
Gordon Henriksen75609c72008-08-24 03:18:23 +00001157>MyGC::MyGC() {
Gordon Henriksen0076a672007-09-27 19:31:36 +00001158 NeededSafePoints = 1 &lt;&lt; GC::Loop
1159 | 1 &lt;&lt; GC::Return
1160 | 1 &lt;&lt; GC::PreCall
1161 | 1 &lt;&lt; GC::PostCall;
1162}</pre></blockquote>
1163
1164<p>It can then use the following routines to access safe points.</p>
1165
Gordon Henriksen7843c162007-12-11 00:30:17 +00001166<blockquote><pre
1167>for (iterator I = begin(), E = end(); I != E; ++I) {
Gordon Henriksen75609c72008-08-24 03:18:23 +00001168 GCFunctionInfo *MD = *I;
Gordon Henriksen7843c162007-12-11 00:30:17 +00001169 size_t PointCount = MD-&gt;size();
Gordon Henriksen0076a672007-09-27 19:31:36 +00001170
Gordon Henriksen75609c72008-08-24 03:18:23 +00001171 for (GCFunctionInfo::iterator PI = MD-&gt;begin(),
1172 PE = MD-&gt;end(); PI != PE; ++PI) {
Gordon Henriksen7843c162007-12-11 00:30:17 +00001173 GC::PointKind PointKind = PI-&gt;Kind;
1174 unsigned PointNum = PI-&gt;Num;
1175 }
1176}
1177</pre></blockquote>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001178
1179<p>Almost every collector requires <tt>PostCall</tt> safe points, since these
1180correspond to the moments when the function is suspended during a call to a
1181subroutine.</p>
1182
1183<p>Threaded programs generally require <tt>Loop</tt> safe points to guarantee
1184that the application will reach a safe point within a bounded amount of time,
1185even if it is executing a long-running loop which contains no function
1186calls.</p>
1187
1188<p>Threaded collectors may also require <tt>Return</tt> and <tt>PreCall</tt>
1189safe points to implement "stop the world" techniques using self-modifying code,
1190where it is important that the program not exit the function without reaching a
1191safe point (because only the topmost function has been patched).</p>
1192
1193</div>
1194
1195
1196<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001197<h3>
Gordon Henriksen75609c72008-08-24 03:18:23 +00001198 <a name="assembly">Emitting assembly code: <tt>GCMetadataPrinter</tt></a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001199</h3>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001200
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +00001201<div>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001202
Gordon Henriksen3519d5d2009-03-06 01:57:32 +00001203<p>LLVM allows a plugin to print arbitrary assembly code before and after the
1204rest of a module's assembly code. At the end of the module, the GC can compile
1205the LLVM stack map into assembly code. (At the beginning, this information is not
Gordon Henriksen75609c72008-08-24 03:18:23 +00001206yet computed.)</p>
1207
1208<p>Since AsmWriter and CodeGen are separate components of LLVM, a separate
1209abstract base class and registry is provided for printing assembly code, the
Gordon Henriksen29a25eb2009-03-02 03:47:20 +00001210<tt>GCMetadaPrinter</tt> and <tt>GCMetadataPrinterRegistry</tt>. The AsmWriter
Gordon Henriksen75609c72008-08-24 03:18:23 +00001211will look for such a subclass if the <tt>GCStrategy</tt> sets
1212<tt>UsesMetadata</tt>:</p>
1213
1214<blockquote><pre
1215>MyGC::MyGC() {
1216 UsesMetadata = true;
1217}</pre></blockquote>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001218
Gordon Henriksen3519d5d2009-03-06 01:57:32 +00001219<p>This separation allows JIT-only clients to be smaller.</p>
1220
Gordon Henriksen7843c162007-12-11 00:30:17 +00001221<p>Note that LLVM does not currently have analogous APIs to support code
1222generation in the JIT, nor using the object writers.</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001223
1224<blockquote><pre
Gordon Henriksen75609c72008-08-24 03:18:23 +00001225>// lib/MyGC/MyGCPrinter.cpp - Example LLVM GC printer
Gordon Henriksen0076a672007-09-27 19:31:36 +00001226
Gordon Henriksen75609c72008-08-24 03:18:23 +00001227#include "llvm/CodeGen/GCMetadataPrinter.h"
1228#include "llvm/Support/Compiler.h"
1229
1230using namespace llvm;
1231
1232namespace {
Duncan Sands6c5e4352010-05-11 20:16:09 +00001233 class LLVM_LIBRARY_VISIBILITY MyGCPrinter : public GCMetadataPrinter {
Gordon Henriksen75609c72008-08-24 03:18:23 +00001234 public:
1235 virtual void beginAssembly(std::ostream &amp;OS, AsmPrinter &amp;AP,
1236 const TargetAsmInfo &amp;TAI);
1237
1238 virtual void finishAssembly(std::ostream &amp;OS, AsmPrinter &amp;AP,
1239 const TargetAsmInfo &amp;TAI);
1240 };
1241
1242 GCMetadataPrinterRegistry::Add&lt;MyGCPrinter&gt;
1243 X("mygc", "My bespoke garbage collector.");
Gordon Henriksen0076a672007-09-27 19:31:36 +00001244}</pre></blockquote>
1245
1246<p>The collector should use <tt>AsmPrinter</tt> and <tt>TargetAsmInfo</tt> to
Gordon Henriksen7843c162007-12-11 00:30:17 +00001247print portable assembly code to the <tt>std::ostream</tt>. The collector itself
1248contains the stack map for the entire module, and may access the
Gordon Henriksen75609c72008-08-24 03:18:23 +00001249<tt>GCFunctionInfo</tt> using its own <tt>begin()</tt> and <tt>end()</tt>
Gordon Henriksen7843c162007-12-11 00:30:17 +00001250methods. Here's a realistic example:</p>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001251
1252<blockquote><pre
1253>#include "llvm/CodeGen/AsmPrinter.h"
1254#include "llvm/Function.h"
Gordon Henriksen7843c162007-12-11 00:30:17 +00001255#include "llvm/Target/TargetMachine.h"
1256#include "llvm/Target/TargetData.h"
Gordon Henriksen0076a672007-09-27 19:31:36 +00001257#include "llvm/Target/TargetAsmInfo.h"
1258
Gordon Henriksen75609c72008-08-24 03:18:23 +00001259void MyGCPrinter::beginAssembly(std::ostream &amp;OS, AsmPrinter &amp;AP,
Gordon Henriksen7843c162007-12-11 00:30:17 +00001260 const TargetAsmInfo &amp;TAI) {
1261 // Nothing to do.
1262}
1263
Gordon Henriksen75609c72008-08-24 03:18:23 +00001264void MyGCPrinter::finishAssembly(std::ostream &amp;OS, AsmPrinter &amp;AP,
Gordon Henriksen7843c162007-12-11 00:30:17 +00001265 const TargetAsmInfo &amp;TAI) {
Gordon Henriksen0076a672007-09-27 19:31:36 +00001266 // Set up for emitting addresses.
1267 const char *AddressDirective;
1268 int AddressAlignLog;
Gordon Henriksen7843c162007-12-11 00:30:17 +00001269 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
Gordon Henriksen0076a672007-09-27 19:31:36 +00001270 AddressDirective = TAI.getData32bitsDirective();
1271 AddressAlignLog = 2;
1272 } else {
1273 AddressDirective = TAI.getData64bitsDirective();
1274 AddressAlignLog = 3;
1275 }
1276
1277 // Put this in the data section.
1278 AP.SwitchToDataSection(TAI.getDataSection());
1279
1280 // For each function...
Gordon Henriksen7843c162007-12-11 00:30:17 +00001281 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
Gordon Henriksen75609c72008-08-24 03:18:23 +00001282 GCFunctionInfo &amp;MD = **FI;
Gordon Henriksen0076a672007-09-27 19:31:36 +00001283
1284 // Emit this data structure:
1285 //
1286 // struct {
1287 // int32_t PointCount;
1288 // struct {
1289 // void *SafePointAddress;
1290 // int32_t LiveCount;
1291 // int32_t LiveOffsets[LiveCount];
1292 // } Points[PointCount];
1293 // } __gcmap_&lt;FUNCTIONNAME&gt;;
1294
1295 // Align to address width.
1296 AP.EmitAlignment(AddressAlignLog);
1297
Gordon Henriksen3519d5d2009-03-06 01:57:32 +00001298 // Emit the symbol by which the stack map entry can be found.
Gordon Henriksen0076a672007-09-27 19:31:36 +00001299 std::string Symbol;
1300 Symbol += TAI.getGlobalPrefix();
1301 Symbol += "__gcmap_";
1302 Symbol += MD.getFunction().getName();
1303 if (const char *GlobalDirective = TAI.getGlobalDirective())
1304 OS &lt;&lt; GlobalDirective &lt;&lt; Symbol &lt;&lt; "\n";
1305 OS &lt;&lt; TAI.getGlobalPrefix() &lt;&lt; Symbol &lt;&lt; ":\n";
1306
1307 // Emit PointCount.
1308 AP.EmitInt32(MD.size());
1309 AP.EOL("safe point count");
1310
1311 // And each safe point...
Gordon Henriksen75609c72008-08-24 03:18:23 +00001312 for (GCFunctionInfo::iterator PI = MD.begin(),
Gordon Henriksen0076a672007-09-27 19:31:36 +00001313 PE = MD.end(); PI != PE; ++PI) {
1314 // Align to address width.
1315 AP.EmitAlignment(AddressAlignLog);
1316
1317 // Emit the address of the safe point.
1318 OS &lt;&lt; AddressDirective
1319 &lt;&lt; TAI.getPrivateGlobalPrefix() &lt;&lt; "label" &lt;&lt; PI-&gt;Num;
1320 AP.EOL("safe point address");
1321
1322 // Emit the stack frame size.
1323 AP.EmitInt32(MD.getFrameSize());
1324 AP.EOL("stack frame size");
1325
1326 // Emit the number of live roots in the function.
1327 AP.EmitInt32(MD.live_size(PI));
1328 AP.EOL("live root count");
1329
1330 // And for each live root...
Gordon Henriksen75609c72008-08-24 03:18:23 +00001331 for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
Gordon Henriksen0076a672007-09-27 19:31:36 +00001332 LE = MD.live_end(PI);
1333 LI != LE; ++LI) {
1334 // Print its offset within the stack frame.
1335 AP.EmitInt32(LI-&gt;StackOffset);
1336 AP.EOL("stack offset");
1337 }
1338 }
1339 }
1340}
1341</pre></blockquote>
1342
1343</div>
1344
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +00001345</div>
Gordon Henriksen0076a672007-09-27 19:31:36 +00001346
1347<!-- *********************************************************************** -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001348<h2>
Chris Lattner71679332004-05-27 05:52:10 +00001349 <a name="references">References</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +00001350</h2>
Chris Lattner71679332004-05-27 05:52:10 +00001351<!-- *********************************************************************** -->
1352
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +00001353<div>
Chris Lattner71679332004-05-27 05:52:10 +00001354
1355<p><a name="appel89">[Appel89]</a> Runtime Tags Aren't Necessary. Andrew
1356W. Appel. Lisp and Symbolic Computation 19(7):703-705, July 1989.</p>
1357
1358<p><a name="goldberg91">[Goldberg91]</a> Tag-free garbage collection for
Gordon Henriksen0076a672007-09-27 19:31:36 +00001359strongly typed programming languages. Benjamin Goldberg. ACM SIGPLAN
Chris Lattner71679332004-05-27 05:52:10 +00001360PLDI'91.</p>
1361
1362<p><a name="tolmach94">[Tolmach94]</a> Tag-free garbage collection using
Gordon Henriksen0076a672007-09-27 19:31:36 +00001363explicit type parameters. Andrew Tolmach. Proceedings of the 1994 ACM
Chris Lattner71679332004-05-27 05:52:10 +00001364conference on LISP and functional programming.</p>
1365
Gordon Henriksen0076a672007-09-27 19:31:36 +00001366<p><a name="henderson02">[Henderson2002]</a> <a
1367href="http://citeseer.ist.psu.edu/henderson02accurate.html">
1368Accurate Garbage Collection in an Uncooperative Environment</a>.
1369Fergus Henderson. International Symposium on Memory Management 2002.</p>
1370
Chris Lattner71679332004-05-27 05:52:10 +00001371</div>
Chris Lattnerf689d572004-05-23 21:02:20 +00001372
Gordon Henriksen0076a672007-09-27 19:31:36 +00001373
Chris Lattnerf689d572004-05-23 21:02:20 +00001374<!-- *********************************************************************** -->
1375
1376<hr>
1377<address>
1378 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman86242e12008-12-11 17:34:48 +00001379 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Chris Lattnerf689d572004-05-23 21:02:20 +00001380 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman86242e12008-12-11 17:34:48 +00001381 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Chris Lattnerf689d572004-05-23 21:02:20 +00001382
1383 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
NAKAMURA Takumica46f5a2011-04-09 02:13:37 +00001384 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Chris Lattnerf689d572004-05-23 21:02:20 +00001385 Last modified: $Date$
1386</address>
1387
1388</body>
1389</html>