A step towards geting linux ppc to work (see PR 3099)
[oota-llvm.git] / docs / GarbageCollection.html
index 90f42bb1458b76266f177a2aa326c71d7c9bdbc0..3909fb616bf1d680412f8603c0c967426a15505b 100644 (file)
     </ul>
   </li>
 
-  <li><a href="#intrinsics">Collection intrinsics</a>
+  <li><a href="#core">Core support</a>
     <ul>
+    <li><a href="#gcattr">Specifying GC code generation:
+      <tt>gc "..."</tt></a></li>
     <li><a href="#gcroot">Identifying GC roots on the stack:
       <tt>llvm.gcroot</tt></a></li>
     <li><a href="#barriers">Reading and writing references in the heap</a>
@@ -73,7 +75,7 @@
     <li><a href="#safe-points">Generating safe points:
       <tt>NeededSafePoints</tt></a></li>
     <li><a href="#assembly">Emitting assembly code:
-      <tt>beginAssembly</tt> and <tt>finishAssembly</tt></a></li>
+      <tt>GCMetadataPrinter</tt></a></li>
     </ul>
   </li>
 
@@ -149,7 +151,7 @@ support accurate garbage collection.</p>
 <div class="doc_text">
 
 <p>LLVM's intermediate representation provides <a href="#intrinsics">garbage
-collection intrinsics</a> which offer support for a broad class of
+collection intrinsics</a> that offer support for a broad class of
 collector models. For instance, the intrinsics permit:</p>
 
 <ul>
@@ -198,11 +200,12 @@ garbage collector implementations in two manners:</p>
 
 <ul>
   <li>Emitting compatible code, including initialization in the main
-      program.</li>
+      program if necessary.</li>
   <li>Loading a compiler plugin if the collector is not statically linked with
       your compiler. For <tt>llc</tt>, use the <tt>-load</tt> option.</li>
-  <li>Selecting the collection algorithm with <tt>llc -gc=</tt> or by setting 
-      <tt>llvm::TheCollector</tt>.</li>
+  <li>Selecting the collection algorithm by applying the <tt>gc "..."</tt> 
+      attribute to your garbage collected functions, or equivalently with
+      the <tt>setGC</tt> method.</li>
   <li>Linking your final executable with the garbage collector runtime.</li>
 </ul>
 
@@ -211,7 +214,7 @@ garbage collector implementations in two manners:</p>
 <table>
   <tr>
     <th>Collector</th>
-    <th><tt>llc</tt> arguments</th>
+    <th><tt>gc</tt> attribute</th>
     <th>Linkage</th>
     <th><tt>gcroot</tt></th>
     <th><tt>gcread</tt></th>
@@ -219,7 +222,7 @@ garbage collector implementations in two manners:</p>
   </tr>
   <tr valign="baseline">
     <td><a href="#semispace">SemiSpace</a></td>
-    <td><tt>-gc=shadow-stack</tt></td>
+    <td><tt>gc "shadow-stack"</tt></td>
     <td>TODO FIXME</td>
     <td>required</td>
     <td>optional</td>
@@ -227,7 +230,7 @@ garbage collector implementations in two manners:</p>
   </tr>
   <tr valign="baseline">
     <td><a href="#ocaml">Ocaml</a></td>
-    <td><tt>-gc=ocaml</tt></td>
+    <td><tt>gc "ocaml"</tt></td>
     <td><i>provided by ocamlopt</i></td>
     <td>required</td>
     <td>optional</td>
@@ -252,11 +255,12 @@ collectors may require user programs to utilize.</p>
 
 <div class="doc_text">
 
-<p>The ShadowStack collector is invoked with <tt>llc -gc=shadow-stack</tt>.
+<p>The ShadowStack backend is invoked with the <tt>gc "shadow-stack"</tt>
+function attribute.
 Unlike many collectors which rely on a cooperative code generator to generate
 stack maps, this algorithm carefully maintains a linked list of stack root
 descriptors [<a href="#henderson02">Henderson2002</a>]. This so-called "shadow
-stack," mirrors the machine stack. Maintaining this data structure is slower
+stack" mirrors the machine stack. Maintaining this data structure is slower
 than using stack maps, but has a significant portability advantage because it
 requires no special support from the target code generator.</p>
 
@@ -264,7 +268,7 @@ requires no special support from the target code generator.</p>
 program may use <tt>load</tt> and <tt>store</tt> instead of <tt>llvm.gcread</tt>
 and <tt>llvm.gcwrite</tt>.</p>
 
-<p>The ShadowStack collector is a compiler plugin only. It must be paired with a
+<p>ShadowStack is a code generator plugin only. It must be paired with a
 compatible runtime.</p>
 
 </div>
@@ -276,9 +280,8 @@ compatible runtime.</p>
 
 <div class="doc_text">
 
-<p>The SemiSpace runtime implements with the <a href="runtime">suggested
-runtime interface</a> and is compatible the ShadowStack collector's code
-generation.</p>
+<p>The SemiSpace runtime implements the <a href="runtime">suggested
+runtime interface</a> and is compatible with the ShadowStack backend.</p>
 
 <p>SemiSpace is a very simple copying collector. When it starts up, it
 allocates two blocks of memory for the heap. It uses a simple bump-pointer
@@ -302,7 +305,8 @@ Enhancements would be welcomed.</p>
 
 <div class="doc_text">
 
-<p>The ocaml collector is invoked with <tt>llc -gc=ocaml</tt>. It supports the
+<p>The ocaml backend is invoked with the <tt>gc "ocaml"</tt> function attribute.
+It supports the
 <a href="http://caml.inria.fr/">Objective Caml</a> language runtime by emitting
 a type-accurate stack map in the form of an ocaml 3.10.0-compatible frametable.
 The linkage requirements are satisfied automatically by the <tt>ocamlopt</tt>
@@ -317,7 +321,7 @@ may use <tt>load</tt> and <tt>store</tt> instead of <tt>llvm.gcread</tt> and
 
 <!-- *********************************************************************** -->
 <div class="doc_section">
-  <a name="intrinsics">Collection intrinsics</a>
+  <a name="core">Core support</a><a name="intrinsics"></a>
 </div>
 <!-- *********************************************************************** -->
 
@@ -335,19 +339,39 @@ specified by the runtime.</p>
 
 </div>
 
+<!-- ======================================================================= -->
+<div class="doc_subsection">
+  <a name="gcattr">Specifying GC code generation: <tt>gc "..."</tt></a>
+</div>
+
+<div class="doc_code"><tt>
+  define <i>ty</i> @<i>name</i>(...) <u>gc "<i>collector</i>"</u> { ...
+</tt></div>
+
+<div class="doc_text">
+
+<p>The <tt>gc</tt> function attribute is used to specify the desired collector
+algorithm to the compiler. It is equivalent to specifying the collector name
+programmatically using the <tt>setGC</tt> method of <tt>Function</tt>.</p>
+
+<p>Specifying the collector on a per-function basis allows LLVM to link together
+programs that use different garbage collection algorithms.</p>
+
+</div>
+
 <!-- ======================================================================= -->
 <div class="doc_subsection">
   <a name="gcroot">Identifying GC roots on the stack: <tt>llvm.gcroot</tt></a>
 </div>
 
 <div class="doc_code"><tt>
-  void %llvm.gcroot(i8** %ptrloc, i8* %metadata)
+  void @llvm.gcroot(i8** %ptrloc, i8* %metadata)
 </tt></div>
 
 <div class="doc_text">
 
 <p>The <tt>llvm.gcroot</tt> intrinsic is used to inform LLVM of a pointer
-variable on the stack. The first argument <b>must</b> be an alloca instruction
+variable on the stack. The first argument <b>must</b> be a value referring to an alloca instruction
 or a bitcast of an alloca. The second contains a pointer to metadata that
 should be associated with the pointer, and <b>must</b> be a constant or global
 value address. If your target collector uses tags, use a null pointer for
@@ -374,7 +398,7 @@ Entry:
    ;; Tell LLVM that the stack space is a stack root.
    ;; Java has type-tags on objects, so we pass null as metadata.
    %tmp = bitcast %Object** %X to i8**
-   call void %llvm.gcroot(%i8** %X, i8* null)
+   call void @llvm.gcroot(i8** %X, i8* null)
    ...
 
    ;; "CodeBlock" is the block corresponding to the start
@@ -414,16 +438,16 @@ object). Accordingly, these intrinsics take both pointers as separate arguments
 for completeness. In this snippet, <tt>%object</tt> is the object pointer, and 
 <tt>%derived</tt> is the derived pointer:</p>
 
-<blockquote><pre
->    ;; An array type.
+<blockquote><pre>
+    ;; An array type.
     %class.Array = type { %class.Object, i32, [0 x %class.Object*] }
-...
+    ...
 
     ;; Load the object pointer from a gcroot.
     %object = load %class.Array** %object_addr
 
     ;; Compute the derived pointer.
-    %derived = getelementptr %obj, i32 0, i32 2, i32 %n</pre></blockquote>
+    %derived = getelementptr %object, i32 0, i32 2, i32 %n</pre></blockquote>
 
 </div>
 
@@ -569,7 +593,7 @@ The <tt>llvm_cg_walk_gcroots</tt> function is a function provided by the code
 generator that iterates through all of the GC roots on the stack, calling the
 specified function pointer with each record. For each GC root, the address of
 the pointer and the meta-data (from the <a
-href="#roots"><tt>llvm.gcroot</tt></a> intrinsic) are provided.
+href="#gcroot"><tt>llvm.gcroot</tt></a> intrinsic) are provided.
 </p>
 </div>
 
@@ -591,32 +615,40 @@ TODO
 
 <div class="doc_text">
 
-<p>To implement a collector plugin, it is necessary to subclass
-<tt>llvm::Collector</tt>, which can be accomplished in a few lines of
+<p>User code specifies which GC code generation to use with the <tt>gc</tt>
+function attribute or, equivalently, with the <tt>setGC</tt> method of
+<tt>Function</tt>.</p>
+
+<p>To implement a GC plugin, it is necessary to subclass
+<tt>llvm::GCStrategy</tt>, which can be accomplished in a few lines of
 boilerplate code. LLVM's infrastructure provides access to several important
 algorithms. For an uncontroversial collector, all that remains may be to emit
 the assembly code for the collector's unique stack map data structure, which
 might be accomplished in as few as 100 LOC.</p>
 
-<p>To subclass <tt>llvm::Collector</tt> and register a collector:</p>
+<p>This is not the appropriate place to implement a garbage collected heap or a
+garbage collector itself. That code should exist in the language's runtime
+library. The compiler plugin is responsible for generating code which is
+compatible with that runtime library.</p>
+
+<p>To subclass <tt>llvm::GCStrategy</tt> and register it with the compiler:</p>
 
-<blockquote><pre>// lib/MyGC/MyGC.cpp - Example LLVM collector plugin
+<blockquote><pre>// lib/MyGC/MyGC.cpp - Example LLVM GC plugin
 
-#include "llvm/CodeGen/Collector.h"
-#include "llvm/CodeGen/Collectors.h"
-#include "llvm/CodeGen/CollectorMetadata.h"
+#include "llvm/CodeGen/GCStrategy.h"
+#include "llvm/CodeGen/GCMetadata.h"
 #include "llvm/Support/Compiler.h"
 
 using namespace llvm;
 
 namespace {
-  class VISIBILITY_HIDDEN MyCollector : public Collector {
+  class VISIBILITY_HIDDEN MyGC : public GCStrategy {
   public:
-    MyCollector() {}
+    MyGC() {}
   };
   
-  CollectorRegistry::Add&lt;MyCollector&gt;
-  X("mygc", "My custom garbage collector.");
+  GCRegistry::Add&lt;MyGC&gt;
+  X("mygc", "My bespoke garbage collector.");
 }</pre></blockquote>
 
 <p>Using the LLVM makefiles (like the <a
@@ -632,20 +664,20 @@ LOADABLE_MODULE = 1
 
 include $(LEVEL)/Makefile.common</pre></blockquote>
 
-<blockquote><pre
-></pre></blockquote>
-
-<p>Once the plugin is compiled, user code may be compiled using <tt>llc
--load=<var>MyGC.so</var> -gc=mygc</tt> (though <var>MyGC.so</var> may have some
-other platform-specific extension).</p>
-
-<!-- BEGIN FIXME: Gross -->
-<p>To use a collector in a tool other than <tt>llc</tt>, simply assign a
-<tt>Collector</tt> to the <tt>llvm::TheCollector</tt> variable:</p>
+<p>Once the plugin is compiled, code using it may be compiled using <tt>llc
+-load=<var>MyGC.so</var></tt> (though <var>MyGC.so</var> may have some other
+platform-specific extension):</p>
 
 <blockquote><pre
->TheCollector = new MyGC();</pre></blockquote>
-<!-- /FIXME GROSS -->
+>$ cat sample.ll
+define void @f() gc "mygc" {
+entry:
+        ret void
+}
+$ llvm-as &lt; sample.ll | llc -load=MyGC.so</pre></blockquote>
+
+<p>It is also possible to statically link the collector plugin into tools, such
+as a language-specific compiler front-end.</p>
 
 </div>
 
@@ -956,19 +988,22 @@ interest.</p>
 <div class="doc_text">
 
 <blockquote><pre
->CollectorMetadata &amp;MD = ...;
-unsigned FrameSize = MD.getFrameSize();
-size_t RootCount = MD.roots_size();
-
-for (CollectorMetadata::roots_iterator RI = MD.roots_begin(),
-                                       RE = MD.roots_end(); RI != RE; ++RI) {
-  int RootNum = RI->Num;
-  int RootStackOffset = RI->StackOffset;
-  Constant *RootMetadata = RI->Metadata;
+>for (iterator I = begin(), E = end(); I != E; ++I) {
+  GCFunctionInfo *FI = *I;
+  unsigned FrameSize = FI-&gt;getFrameSize();
+  size_t RootCount = FI-&gt;roots_size();
+
+  for (GCFunctionInfo::roots_iterator RI = FI-&gt;roots_begin(),
+                                      RE = FI-&gt;roots_end();
+                                      RI != RE; ++RI) {
+    int RootNum = RI->Num;
+    int RootStackOffset = RI->StackOffset;
+    Constant *RootMetadata = RI->Metadata;
+  }
 }</pre></blockquote>
 
-<p>LLVM automatically computes a stack map. All a <tt>Collector</tt> needs to do
-is access it using <tt>CollectorMetadata::roots_begin()</tt> and
+<p>LLVM automatically computes a stack map. All a <tt>GCStrategy</tt> needs to do
+is access it using <tt>GCFunctionMetadata::roots_begin()</tt> and
 -<tt>end()</tt>. If the <tt>llvm.gcroot</tt> intrinsic is eliminated before code
 generation by a custom lowering pass, LLVM's stack map will be empty.</p>
 
@@ -983,19 +1018,19 @@ generation by a custom lowering pass, LLVM's stack map will be empty.</p>
 <div class="doc_text">
 
 <blockquote><pre
->MyCollector::MyCollector() {
+>MyGC::MyGC() {
   InitRoots = true;
 }</pre></blockquote>
 
 <p>When set, LLVM will automatically initialize each root to <tt>null</tt> upon
-entry to the function. This prevents the reachability analysis from finding
-uninitialized values in stack roots at runtime, which will almost certainly
-cause it to segfault. This initialization occurs before custom lowering, so the
-two may be used together.</p>
+entry to the function. This prevents the GC's sweep phase from visiting
+uninitialized pointers, which will almost certainly cause it to crash. This
+initialization occurs before custom lowering, so the two may be used
+together.</p>
 
-<p>Since LLVM does not yet compute liveness information, this feature should be
-used by all collectors which do not custom lower <tt>llvm.gcroot</tt>, and even
-some that do.</p>
+<p>Since LLVM does not yet compute liveness information, there is no means of
+distinguishing an uninitialized stack root from an initialized one. Therefore,
+this feature should be used by all GC plugins. It is enabled by default.</p>
 
 </div>
 
@@ -1008,28 +1043,26 @@ some that do.</p>
 
 <div class="doc_text">
 
-<p>For collectors with barriers or unusual treatment of stack roots, these
-flags allow the collector to perform any required transformation on the LLVM
+<p>For GCs which use barriers or unusual treatment of stack roots, these
+flags allow the collector to perform arbitrary transformations of the LLVM
 IR:</p>
 
 <blockquote><pre
->class MyCollector : public Collector {
+>class MyGC : public GCStrategy {
 public:
-  MyCollector() {
+  MyGC() {
     CustomRoots = true;
     CustomReadBarriers = true;
     CustomWriteBarriers = true;
   }
   
-protected:
-  virtual Pass *createCustomLoweringPass() const {
-    return new MyGCLoweringFunctionPass();
-  }
+  virtual bool initializeCustomLowering(Module &amp;M);
+  virtual bool performCustomLowering(Function &amp;F);
 };</pre></blockquote>
 
 <p>If any of these flags are set, then LLVM suppresses its default lowering for
-the corresponding intrinsics and instead passes them on to a custom lowering
-pass specified by the collector.</p>
+the corresponding intrinsics and instead calls
+<tt>performCustomLowering</tt>.</p>
 
 <p>LLVM's default action for each intrinsic is as follows:</p>
 
@@ -1041,56 +1074,52 @@ pass specified by the collector.</p>
 </ul>
 
 <p>If <tt>CustomReadBarriers</tt> or <tt>CustomWriteBarriers</tt> are specified,
-the custom lowering pass <strong>must</strong> eliminate the corresponding
-barriers.</p>
+then <tt>performCustomLowering</tt> <strong>must</strong> eliminate the
+corresponding barriers.</p>
+
+<p><tt>performCustomLowering</tt> must comply with the same restrictions as <a
+href="WritingAnLLVMPass.html#runOnFunction"><tt
+>FunctionPass::runOnFunction</tt></a>.
+Likewise, <tt>initializeCustomLowering</tt> has the same semantics as <a
+href="WritingAnLLVMPass.html#doInitialization_mod"><tt
+>Pass::doInitialization(Module&amp;)</tt></a>.</p>
 
-<p>This template can be used as a starting point for a lowering pass:</p>
+<p>The following can be used as a template:</p>
 
 <blockquote><pre
->#include "llvm/Function.h"
-#include "llvm/Module.h"
-#include "llvm/Instructions.h"
+>#include "llvm/Module.h"
+#include "llvm/IntrinsicInst.h"
 
-namespace {
-  class VISIBILITY_HIDDEN MyGCLoweringFunctionPass : public FunctionPass {
-    static char ID;
-  public:
-    MyGCLoweringFunctionPass() : FunctionPass(intptr_t(&amp;ID)) {}
-    
-    const char *getPassName() const { return "Lower GC Intrinsics"; }
-    
-    bool runOnFunction(Function &amp;F) {
-      Module *M = F.getParent();
-      
-      Function *GCReadInt  = M-&gt;getFunction("llvm.gcread"),
-               *GCWriteInt = M-&gt;getFunction("llvm.gcwrite"),
-               *GCRootInt  = M-&gt;getFunction("llvm.gcroot");
-      
-      bool MadeChange = false;
-      
-      for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
-        for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
-          if (CallInst *CI = dyn_cast&lt;CallInst&gt;(II++))
-            if (Function *F = CI->getCalledFunction())
-              if (F == GCWriteInt) {
-                // Handle llvm.gcwrite.
-                CI->eraseFromParent();
-                MadeChange = true;
-              } else if (F == GCReadInt) {
-                // Handle llvm.gcread.
-                CI->eraseFromParent();
-                MadeChange = true;
-              } else if (F == GCRootInt) {
-                // Handle llvm.gcroot.
-                CI->eraseFromParent();
-                MadeChange = true;
-              }
-      
-      return MadeChange;
-    }
-  };
+bool MyGC::initializeCustomLowering(Module &amp;M) {
+  return false;
+}
 
-  char MyGCLoweringFunctionPass::ID = 0;
+bool MyGC::performCustomLowering(Function &amp;F) {
+  bool MadeChange = false;
+  
+  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
+    for (BasicBlock::iterator II = BB-&gt;begin(), E = BB-&gt;end(); II != E; )
+      if (IntrinsicInst *CI = dyn_cast&lt;IntrinsicInst&gt;(II++))
+        if (Function *F = CI-&gt;getCalledFunction())
+          switch (F-&gt;getIntrinsicID()) {
+          case Intrinsic::gcwrite:
+            // Handle llvm.gcwrite.
+            CI-&gt;eraseFromParent();
+            MadeChange = true;
+            break;
+          case Intrinsic::gcread:
+            // Handle llvm.gcread.
+            CI-&gt;eraseFromParent();
+            MadeChange = true;
+            break;
+          case Intrinsic::gcroot:
+            // Handle llvm.gcroot.
+            CI-&gt;eraseFromParent();
+            MadeChange = true;
+            break;
+          }
+  
+  return MadeChange;
 }</pre></blockquote>
 
 </div>
@@ -1121,7 +1150,7 @@ namespace {
 <tt>NeededSafePoints</tt> mask:</p>
 
 <blockquote><pre
->MyCollector::MyCollector() {
+>MyGC::MyGC() {
   NeededSafePoints = 1 &lt;&lt; GC::Loop
                    | 1 &lt;&lt; GC::Return
                    | 1 &lt;&lt; GC::PreCall
@@ -1130,15 +1159,18 @@ namespace {
 
 <p>It can then use the following routines to access safe points.</p>
 
-<blockquote><pre>
-CollectorMetadata &amp;MD = ...;
-size_t PointCount = MD.size();
-
-for (CollectorMetadata::iterator PI = MD.begin(),
-                                 PE = MD.end(); PI != PE; ++PI) {
-  GC::PointKind PointKind = PI-&gt;Kind;
-  unsigned PointNum = PI-&gt;Num;
-}</pre></blockquote>
+<blockquote><pre
+>for (iterator I = begin(), E = end(); I != E; ++I) {
+  GCFunctionInfo *MD = *I;
+  size_t PointCount = MD-&gt;size();
+
+  for (GCFunctionInfo::iterator PI = MD-&gt;begin(),
+                                PE = MD-&gt;end(); PI != PE; ++PI) {
+    GC::PointKind PointKind = PI-&gt;Kind;
+    unsigned PointNum = PI-&gt;Num;
+  }
+}
+</pre></blockquote>
 
 <p>Almost every collector requires <tt>PostCall</tt> safe points, since these
 correspond to the moments when the function is suspended during a call to a
@@ -1159,48 +1191,76 @@ safe point (because only the topmost function has been patched).</p>
 
 <!-- ======================================================================= -->
 <div class="doc_subsection">
-  <a name="assembly">Emitting assembly code:
-    <tt>beginAssembly</tt> and <tt>finishAssembly</tt></a>
+  <a name="assembly">Emitting assembly code: <tt>GCMetadataPrinter</tt></a>
 </div>
 
 <div class="doc_text">
 
-<p>LLVM allows a collector to print arbitrary assembly code before and after
-the rest of a module's assembly code. From the latter callback, the collector
-can print stack maps from <tt>CollectorModuleMetadata</tt> populated by the code
-generator.</p>
+<p>LLVM allows a GC to print arbitrary assembly code before and after the rest
+of a module's assembly code. At the end of the module, the GC can print stack
+maps built by the code generator. (At the beginning, this information is not
+yet computed.)</p>
 
-<p>Note that LLVM does not currently support garbage collection code generation
-in the JIT, nor using the object writers.</p>
+<p>Since AsmWriter and CodeGen are separate components of LLVM, a separate
+abstract base class and registry is provided for printing assembly code, the
+<tt>GCMetadaPrinter</tt> and <tt>GCMetadaPrinterRegistry</tt>. The AsmWriter
+will look for such a subclass if the <tt>GCStrategy</tt> sets
+<tt>UsesMetadata</tt>:</p>
 
 <blockquote><pre
->class MyCollector : public Collector {
-  virtual void beginAssembly(Module &amp;M, std::ostream &amp;OS, AsmPrinter &amp;AP,
-                             const TargetAsmInfo &amp;TAI) const;
+>MyGC::MyGC() {
+  UsesMetadata = true;
+}</pre></blockquote>
+
+<p>Note that LLVM does not currently have analogous APIs to support code
+generation in the JIT, nor using the object writers.</p>
+
+<blockquote><pre
+>// lib/MyGC/MyGCPrinter.cpp - Example LLVM GC printer
+
+#include "llvm/CodeGen/GCMetadataPrinter.h"
+#include "llvm/Support/Compiler.h"
+
+using namespace llvm;
 
-  virtual void finishAssembly(Module &amp;M, CollectorModuleMetadata &amp;MMD,
-                              std::ostream &amp;OS, AsmPrinter &amp;AP,
-                              const TargetAsmInfo &amp;TAI) const;
+namespace {
+  class VISIBILITY_HIDDEN MyGCPrinter : public GCMetadataPrinter {
+  public:
+    virtual void beginAssembly(std::ostream &amp;OS, AsmPrinter &amp;AP,
+                               const TargetAsmInfo &amp;TAI);
+  
+    virtual void finishAssembly(std::ostream &amp;OS, AsmPrinter &amp;AP,
+                                const TargetAsmInfo &amp;TAI);
+  };
+  
+  GCMetadataPrinterRegistry::Add&lt;MyGCPrinter&gt;
+  X("mygc", "My bespoke garbage collector.");
 }</pre></blockquote>
 
 <p>The collector should use <tt>AsmPrinter</tt> and <tt>TargetAsmInfo</tt> to
-print portable assembly code to the <tt>std::ostream</tt>. The collector may
-access the stack maps for the entire module using the methods of
-<tt>CollectorModuleMetadata</tt>. Here's a realistic example:</p>
+print portable assembly code to the <tt>std::ostream</tt>. The collector itself
+contains the stack map for the entire module, and may access the
+<tt>GCFunctionInfo</tt> using its own <tt>begin()</tt> and <tt>end()</tt>
+methods. Here's a realistic example:</p>
 
 <blockquote><pre
 >#include "llvm/CodeGen/AsmPrinter.h"
 #include "llvm/Function.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetAsmInfo.h"
 
-void MyCollector::finishAssembly(Module &amp;M,
-                                 CollectorModuleMetadata &amp;MMD,
-                                 std::ostream &amp;OS, AsmPrinter &amp;AP,
-                                 const TargetAsmInfo &amp;TAI) const {
+void MyGCPrinter::beginAssembly(std::ostream &amp;OS, AsmPrinter &amp;AP,
+                                const TargetAsmInfo &amp;TAI) {
+  // Nothing to do.
+}
+
+void MyGCPrinter::finishAssembly(std::ostream &amp;OS, AsmPrinter &amp;AP,
+                                 const TargetAsmInfo &amp;TAI) {
   // Set up for emitting addresses.
   const char *AddressDirective;
   int AddressAlignLog;
-  if (TAI.getAddressSize() == sizeof(int32_t)) {
+  if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
     AddressDirective = TAI.getData32bitsDirective();
     AddressAlignLog = 2;
   } else {
@@ -1212,9 +1272,8 @@ void MyCollector::finishAssembly(Module &amp;M,
   AP.SwitchToDataSection(TAI.getDataSection());
   
   // For each function...
-  for (CollectorModuleMetadata::iterator FI = MMD.begin(),
-                                         FE = MMD.end(); FI != FE; ++FI) {
-    CollectorMetadata &amp;MD = **FI;
+  for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
+    GCFunctionInfo &amp;MD = **FI;
     
     // Emit this data structure:
     // 
@@ -1244,7 +1303,7 @@ void MyCollector::finishAssembly(Module &amp;M,
     AP.EOL("safe point count");
     
     // And each safe point...
-    for (CollectorMetadata::iterator PI = MD.begin(),
+    for (GCFunctionInfo::iterator PI = MD.begin(),
                                      PE = MD.end(); PI != PE; ++PI) {
       // Align to address width.
       AP.EmitAlignment(AddressAlignLog);
@@ -1263,7 +1322,7 @@ void MyCollector::finishAssembly(Module &amp;M,
       AP.EOL("live root count");
       
       // And for each live root...
-      for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
+      for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
                                             LE = MD.live_end(PI);
                                             LI != LE; ++LI) {
         // Print its offset within the stack frame.
@@ -1297,7 +1356,7 @@ href="#gcdescriptors">where pointers are located in heap objects</a>.</p>
 <a href="#explicit"><tt>llvm_gc_collect</tt></a> functions. To do this, it will
 probably have to <a href="#traceroots">trace through the roots
 from the stack</a> and understand the <a href="#gcdescriptors">GC descriptors
-for heap objects</a>. Luckily, there are some <a href="#gcimpls">example
+for heap objects</a>. Luckily, there are some <a href="#usage">example
 implementations</a> available.
 </p>
 </div>
@@ -1334,7 +1393,7 @@ book-keeping is needed at all. This is common for Lisp-like languages.</li>
 <p>The LLVM garbage collectors are capable of supporting all of these styles of
 language, including ones that mix various implementations. To do this, it
 allows the source-language to associate meta-data with the <a
-href="#roots">stack roots</a>, and the heap tracing routines can propagate the
+href="#gcroot">stack roots</a>, and the heap tracing routines can propagate the
 information. In addition, LLVM allows the front-end to extract GC information
 in any form from a specific object pointer (this supports situations #1 and #3).
 </p>