1 //===-- llvm/CodeGen/Collector.h - Garbage collection -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Gordon Henriksen and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // GCInfo records sufficient information about a machine function to enable
11 // accurate garbage collectors. Specifically:
14 // Garbage collection is only possible at certain points in code. Code
15 // generators should record points:
17 // - At and after any call to a subroutine
18 // - Before returning from the current function
19 // - Before backwards branches (loops)
22 // When a reference to a GC-allocated object exists on the stack, it must be
23 // stored in an alloca registered with llvm.gcoot.
25 // This generic information should used by ABI-specific passes to emit support
26 // tables for the runtime garbage collector.
28 // GCSafePointPass identifies the GC safe points in the machine code. (Roots are
29 // identified in SelectionDAGISel.)
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_CODEGEN_COLLECTOR_H
34 #define LLVM_CODEGEN_COLLECTOR_H
36 #include "llvm/CodeGen/CollectorMetadata.h"
42 class FunctionPassManager;
47 /// Collector describes a garbage collector's code generation requirements,
48 /// and provides overridable hooks for those needs which cannot be abstractly
52 unsigned NeededSafePoints; //< Bitmask of required safe points.
53 bool CustomReadBarriers; //< Default is to insert loads.
54 bool CustomWriteBarriers; //< Default is to insert stores.
55 bool CustomRoots; //< Default is to pass through to backend.
56 bool InitRoots; //< If set, roots are nulled during lowering.
58 /// If any of the actions are set to Custom, this is expected to be
59 /// overriden to create a transform to lower those actions to LLVM IR.
60 virtual Pass *createCustomLoweringPass() const;
68 /// True if this collector requires safe points of any kind. By default,
69 /// none are recorded.
70 bool needsSafePoints() const { return NeededSafePoints != 0; }
72 /// True if the collector requires the given kind of safe point. By default,
73 /// none are recorded.
74 bool needsSafePoint(GC::PointKind Kind) const {
75 return (NeededSafePoints & 1 << Kind) != 0;
78 /// By default, write barriers are replaced with simple store instructions.
79 /// If true, then addPassesToCustomLowerIntrinsics must instead process
81 bool customWriteBarrier() const { return CustomWriteBarriers; }
83 /// By default, read barriers are replaced with simple load instructions.
84 /// If true, then addPassesToCustomLowerIntrinsics must instead process
86 bool customReadBarrier() const { return CustomReadBarriers; }
88 /// By default, roots are left for the code generator. If Custom, then
89 /// addPassesToCustomLowerIntrinsics must add passes to delete them.
90 bool customRoots() const { return CustomRoots; }
92 /// If set, gcroot intrinsics should initialize their allocas to null. This
93 /// is necessary for most collectors.
94 bool initializeRoots() const { return InitRoots; }
97 /// Adds LLVM IR transforms to handle collection intrinsics. By default,
98 /// read- and write barriers are replaced with direct memory accesses, and
99 /// roots are passed on to the code generator.
100 void addLoweringPasses(FunctionPassManager &PM) const;
102 /// Same as addLoweringPasses(FunctionPassManager &), except uses a
103 /// PassManager for compatibility with unusual backends (such as MSIL or
105 void addLoweringPasses(PassManager &PM) const;
107 /// Adds target-independent MachineFunction pass to mark safe points. This
108 /// is added very late during code generation, just prior to output, and
109 /// importantly after all CFG transformations (like branch folding).
110 void addGenericMachineCodePass(FunctionPassManager &PM,
111 const TargetMachine &TM, bool Fast) const;
113 /// beginAssembly/finishAssembly - Emit module metadata as assembly code.
114 virtual void beginAssembly(Module &M, std::ostream &OS, AsmPrinter &AP,
115 const TargetAsmInfo &TAI) const;
116 virtual void finishAssembly(Module &M, CollectorModuleMetadata &CMM,
117 std::ostream &OS, AsmPrinter &AP,
118 const TargetAsmInfo &TAI) const;
121 bool NeedsDefaultLoweringPass() const;
122 bool NeedsCustomLoweringPass() const;
127 /// If set, the code generator should generate garbage collection as specified
128 /// by the collector properties.
129 extern const Collector *TheCollector; // FIXME: Find a better home!