llvm.org GIT mirror llvm / ad93c4f
CollectorMetadata and Collector are rejiggered to get along with per-function collector model. Collector is now the factory for CollectorMetadata, so the latter may be subclassed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44827 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 11 years ago
7 changed file(s) with 438 addition(s) and 309 deletion(s). Raw diff Collapse all Expand all
3535
3636
3737
38
  • Collection intrinsics
  • 38
  • Core support
  • 3939
    40
  • Specifying GC code generation:
  • 41 gc "..."
    4042
  • Identifying GC roots on the stack:
  • 4143 llvm.gcroot
    4244
  • Reading and writing references in the heap
  • 197199
    198200
    199201
  • Emitting compatible code, including initialization in the main
  • 200 program.
    202 program if necessary.
    201203
  • Loading a compiler plugin if the collector is not statically linked with
  • 202204 your compiler. For llc, use the -load option.
    203
  • Selecting the collection algorithm with llc -gc= or by setting
  • 204 llvm::TheCollector.
    205
  • Selecting the collection algorithm by applying the gc "..."
  • 206 attribute to your garbage collected functions, or equivalently with
    207 the setCollector method.
    205208
  • Linking your final executable with the garbage collector runtime.
  • 206209
    207210
    210213
    211214
    212215 Collector
    213 llc arguments
    216 gc attribute
    214217 Linkage
    215218 gcroot
    216219 gcread
    218221
    219222
    220223 SemiSpace
    221 -gc=shadow-stack
    224 gc "shadow-stack"
    222225 TODO FIXME
    223226 required
    224227 optional
    226229
    227230
    228231 Ocaml
    229 -gc=ocaml
    232 gc "ocaml"
    230233 provided by ocamlopt
    231234 required
    232235 optional
    251254
    252255
    253256
    254

    The ShadowStack collector is invoked with llc -gc=shadow-stack.

    257

    The ShadowStack backend is invoked with the gc "shadow-stack"

    258 function attribute.
    255259 Unlike many collectors which rely on a cooperative code generator to generate
    256260 stack maps, this algorithm carefully maintains a linked list of stack root
    257261 descriptors [Henderson2002]. This so-called "shadow
    258 stack," mirrors the machine stack. Maintaining this data structure is slower
    262 stack" mirrors the machine stack. Maintaining this data structure is slower
    259263 than using stack maps, but has a significant portability advantage because it
    260264 requires no special support from the target code generator.

    261265
    263267 program may use load and store instead of llvm.gcread
    264268 and llvm.gcwrite.

    265269
    266

    The ShadowStack collector is a compiler plugin only. It must be paired with a

    270

    ShadowStack is a code generator plugin only. It must be paired with a

    267271 compatible runtime.

    268272
    269273
    276280
    277281
    278282

    The SemiSpace runtime implements with the suggested

    279 runtime interface and is compatible the ShadowStack collector's code
    280 generation.

    283 runtime interface and is compatible the ShadowStack backend.

    281284
    282285

    SemiSpace is a very simple copying collector. When it starts up, it

    283286 allocates two blocks of memory for the heap. It uses a simple bump-pointer
    301304
    302305
    303306
    304

    The ocaml collector is invoked with llc -gc=ocaml. It supports the

    307

    The ocaml backend is invoked with the gc "ocaml" function attribute.

    308 It supports the
    305309 Objective Caml language runtime by emitting
    306310 a type-accurate stack map in the form of an ocaml 3.10.0-compatible frametable.
    307311 The linkage requirements are satisfied automatically by the ocamlopt
    316320
    317321
    318322
    319 Collection intrinsics
    323 Core support
    320324
    321325
    322326
    331335 will also need to interface with the runtime, using either the
    332336 suggested runtime interface or another interface
    333337 specified by the runtime.

    338
    339
    340
    341
    342
    343 Specifying GC code generation: gc "..."
    344
    345
    346
    347 define ty @name(...) gc "collector" { ...
    348
    349
    350
    351
    352

    The gc function attribute is used to specify the desired collector

    353 algorithm to the compiler. It is equivalent to specify the collector name
    354 programmatically using the setCollector method of
    355 Function.

    356
    357

    Specifying the collector on a per-function basis allows LLVM to link together

    358 programs which use different garbage collection algorithms.

    334359
    335360
    336361
    590615
    591616
    592617
    618

    User code specifies which collector plugin to use with the gc

    619 function attribute or, equivalently, with the setCollector method of
    620 Function.

    621
    593622

    To implement a collector plugin, it is necessary to subclass

    594623 llvm::Collector, which can be accomplished in a few lines of
    595624 boilerplate code. LLVM's infrastructure provides access to several important
    615644 };
    616645
    617646 CollectorRegistry::Add<MyCollector>
    618 X("mygc", "My custom garbage collector.");
    647 X("mygc", "My bespoke garbage collector.");
    619648 }
    620649
    621650

    Using the LLVM makefiles (like the

    631660
    632661 include $(LEVEL)/Makefile.common
    633662
    663

    Once the plugin is compiled, code using it may be compiled using llc

    664 -load=MyGC.so (though MyGC.so may have some other
    665 platform-specific extension):

    666
    634667
                      
                    
    635 >
    636
    637

    Once the plugin is compiled, user code may be compiled using llc

    638 -load=MyGC.so -gc=mygc (though MyGC.so may have some
    639 other platform-specific extension).

    640
    641
    642

    To use a collector in a tool other than llc, simply assign a

    643 Collector to the llvm::TheCollector variable:

    644
    645
                      
                    
    646 >TheCollector = new MyGC();
    647
    668 >$ cat sample.ll
    669 define void @f() gc "mygc" {
    670 entry:
    671 ret void
    672 }
    673 $ llvm-as < sample.ll | llc -load=MyGC.so
    674
    675

    It is also possible to statically link the collector plugin into tools, such

    676 as a language-specific compiler front-end.

    648677
    649678
    650679
    955984
    956985
    957986
                      
                    
    958 >CollectorMetadata &MD = ...;
    959 unsigned FrameSize = MD.getFrameSize();
    960 size_t RootCount = MD.roots_size();
    961
    962 for (CollectorMetadata::roots_iterator RI = MD.roots_begin(),
    963 RE = MD.roots_end(); RI != RE; ++RI) {
    964 int RootNum = RI->Num;
    965 int RootStackOffset = RI->StackOffset;
    966 Constant *RootMetadata = RI->Metadata;
    987 >for (iterator I = begin(), E = end(); I != E; ++I) {
    988 CollectorMetadata *MD = *I;
    989 unsigned FrameSize = MD->getFrameSize();
    990 size_t RootCount = MD->roots_size();
    991
    992 for (CollectorMetadata::roots_iterator RI = MD->roots_begin(),
    993 RE = MD->roots_end();
    994 RI != RE; ++RI) {
    995 int RootNum = RI->Num;
    996 int RootStackOffset = RI->StackOffset;
    997 Constant *RootMetadata = RI->Metadata;
    998 }
    967999 }
    9681000
    9691001

    LLVM automatically computes a stack map. All a Collector needs to do

    10201052 CustomWriteBarriers = true;
    10211053 }
    10221054
    1023 protected:
    1024 virtual Pass *createCustomLoweringPass() const {
    1025 return new MyGCLoweringFunctionPass();
    1026 }
    1055 virtual bool initializeCustomLowering(Module &M);
    1056 virtual bool performCustomLowering(Function &F);
    10271057 };
    10281058
    10291059

    If any of these flags are set, then LLVM suppresses its default lowering for

    10401070
    10411071
    10421072

    If CustomReadBarriers or CustomWriteBarriers are specified,

    1043 the custom lowering pass must eliminate the corresponding
    1044 barriers.

    1045
    1046

    This template can be used as a starting point for a lowering pass:

    1073 then performCustomLowering must eliminate the
    1074 corresponding barriers.

    1075
    1076

    performCustomLowering, must comply with the same restrictions as

    1077 href="WritingAnLLVMPass.html#runOnFunction">runOnFunction, and
    1078 that initializeCustomLowering has the same semantics as
    1079 href="WritingAnLLVMPass.html#doInitialization_mod">doInitialization(Module
    1080 &).

    1081
    1082

    The following can be used as a template:

    10471083
    10481084
                      
                    
    1049 >#include "llvm/Function.h"
    1050 #include "llvm/Module.h"
    1085 >#include "llvm/Module.h"
    10511086 #include "llvm/Instructions.h"
    10521087
    1053 namespace {
    1054 class VISIBILITY_HIDDEN MyGCLoweringFunctionPass : public FunctionPass {
    1055 static char ID;
    1056 public:
    1057 MyGCLoweringFunctionPass() : FunctionPass(intptr_t(&ID)) {}
    1058
    1059 const char *getPassName() const { return "Lower GC Intrinsics"; }
    1060
    1061 bool runOnFunction(Function &F) {
    1062 Module *M = F.getParent();
    1063
    1064 Function *GCReadInt = M->getFunction("llvm.gcread"),
    1065 *GCWriteInt = M->getFunction("llvm.gcwrite"),
    1066 *GCRootInt = M->getFunction("llvm.gcroot");
    1067
    1068 bool MadeChange = false;
    1069
    1070 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
    1071 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
    1072 if (CallInst *CI = dyn_cast<CallInst>(II++))
    1073 if (Function *F = CI->getCalledFunction())
    1074 if (F == GCWriteInt) {
    1075 // Handle llvm.gcwrite.
    1076 CI->eraseFromParent();
    1077 MadeChange = true;
    1078 } else if (F == GCReadInt) {
    1079 // Handle llvm.gcread.
    1080 CI->eraseFromParent();
    1081 MadeChange = true;
    1082 } else if (F == GCRootInt) {
    1083 // Handle llvm.gcroot.
    1084 CI->eraseFromParent();
    1085 MadeChange = true;
    1086 }
    1087
    1088 return MadeChange;
    1089 }
    1090 };
    1091
    1092 char MyGCLoweringFunctionPass::ID = 0;
    1088 bool MyCollector::initializeCustomLowering(Module &M) {
    1089 return false;
    1090 }
    1091
    1092 bool MyCollector::performCustomLowering(Function &F) {
    1093 const Module *M = F.getParent();
    1094
    1095 Function *GCReadInt = M->getFunction("llvm.gcread"),
    1096 *GCWriteInt = M->getFunction("llvm.gcwrite"),
    1097 *GCRootInt = M->getFunction("llvm.gcroot");
    1098
    1099 bool MadeChange = false;
    1100
    1101 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
    1102 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
    1103 if (CallInst *CI = dyn_cast<CallInst>(II++))
    1104 if (Function *F = CI->getCalledFunction())
    1105 if (F == GCWriteInt) {
    1106 // Handle llvm.gcwrite.
    1107 CI->eraseFromParent();
    1108 MadeChange = true;
    1109 } else if (F == GCReadInt) {
    1110 // Handle llvm.gcread.
    1111 CI->eraseFromParent();
    1112 MadeChange = true;
    1113 } else if (F == GCRootInt) {
    1114 // Handle llvm.gcroot.
    1115 CI->eraseFromParent();
    1116 MadeChange = true;
    1117 }
    1118
    1119 return MadeChange;
    10931120 }
    10941121
    10951122
    11291156
    11301157

    It can then use the following routines to access safe points.

    11311158
    1132
    
                      
                    
    1133 CollectorMetadata &MD = ...;
    1134 size_t PointCount = MD.size();
    1135
    1136 for (CollectorMetadata::iterator PI = MD.begin(),
    1137 PE = MD.end(); PI != PE; ++PI) {
    1138 GC::PointKind PointKind = PI->Kind;
    1139 unsigned PointNum = PI->Num;
    1140 }
    1159
    1160 >for (iterator I = begin(), E = end(); I != E; ++I) {
    1161 CollectorMetadata *MD = *I;
    1162 size_t PointCount = MD->size();
    1163
    1164 for (CollectorMetadata::iterator PI = MD->begin(),
    1165 PE = MD->end(); PI != PE; ++PI) {
    1166 GC::PointKind PointKind = PI->Kind;
    1167 unsigned PointNum = PI->Num;
    1168 }
    1169 }
    1170
    11411171
    11421172

    Almost every collector requires PostCall safe points, since these

    11431173 correspond to the moments when the function is suspended during a call to a
    11661196
    11671197

    LLVM allows a collector to print arbitrary assembly code before and after

    11681198 the rest of a module's assembly code. From the latter callback, the collector
    1169 can print stack maps from CollectorModuleMetadata populated by the code
    1170 generator.

    1171
    1172

    Note that LLVM does not currently support garbage collection code generation

    1173 in the JIT, nor using the object writers.

    1199 can print stack maps built by the code generator.

    1200
    1201

    Note that LLVM does not currently have analogous APIs to support code

    1202 generation in the JIT, nor using the object writers.

    11741203
    11751204
                      
                    
    11761205 >class MyCollector : public Collector {
    1177 virtual void beginAssembly(Module &M, std::ostream &OS, AsmPrinter &AP,
    1178 const TargetAsmInfo &TAI) const;
    1179
    1180 virtual void finishAssembly(Module &M, CollectorModuleMetadata &MMD,
    1181 std::ostream &OS, AsmPrinter &AP,
    1182 const TargetAsmInfo &TAI) const;
    1206 public:
    1207 virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP,
    1208 const TargetAsmInfo &TAI);
    1209
    1210 virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP,
    1211 const TargetAsmInfo &TAI);
    11831212 }
    11841213
    11851214

    The collector should use AsmPrinter and TargetAsmInfo to

    1186 print portable assembly code to the std::ostream. The collector may
    1187 access the stack maps for the entire module using the methods of
    1188 CollectorModuleMetadata. Here's a realistic example:

    1215 print portable assembly code to the std::ostream. The collector itself
    1216 contains the stack map for the entire module, and may access the
    1217 CollectorMetadata using its own begin() and end()
    1218 methods. Here's a realistic example:

    11891219
    11901220
                      
                    
    11911221 >#include "llvm/CodeGen/AsmPrinter.h"
    11921222 #include "llvm/Function.h"
    1223 #include "llvm/Target/TargetMachine.h"
    1224 #include "llvm/Target/TargetData.h"
    11931225 #include "llvm/Target/TargetAsmInfo.h"
    11941226
    1195 void MyCollector::finishAssembly(Module &M,
    1196 CollectorModuleMetadata &MMD,
    1197 std::ostream &OS, AsmPrinter &AP,
    1198 const TargetAsmInfo &TAI) const {
    1227 void MyCollector::beginAssembly(std::ostream &OS, AsmPrinter &AP,
    1228 const TargetAsmInfo &TAI) {
    1229 // Nothing to do.
    1230 }
    1231
    1232 void MyCollector::finishAssembly(std::ostream &OS, AsmPrinter &AP,
    1233 const TargetAsmInfo &TAI) {
    11991234 // Set up for emitting addresses.
    12001235 const char *AddressDirective;
    12011236 int AddressAlignLog;
    1202 if (TAI.getAddressSize() == sizeof(int32_t)) {
    1237 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
    12031238 AddressDirective = TAI.getData32bitsDirective();
    12041239 AddressAlignLog = 2;
    12051240 } else {
    12111246 AP.SwitchToDataSection(TAI.getDataSection());
    12121247
    12131248 // For each function...
    1214 for (CollectorModuleMetadata::iterator FI = MMD.begin(),
    1215 FE = MMD.end(); FI != FE; ++FI) {
    1249 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
    12161250 CollectorMetadata &MD = **FI;
    12171251
    12181252 // Emit this data structure:
    66 //
    77 //===----------------------------------------------------------------------===//
    88 //
    9 // GCInfo records sufficient information about a machine function to enable
    9 // Collector records sufficient information about a machine function to enable
    1010 // accurate garbage collectors. Specifically:
    1111 //
    1212 // - Safe points
    2424 // This generic information should used by ABI-specific passes to emit support
    2525 // tables for the runtime garbage collector.
    2626 //
    27 // GCSafePointPass identifies the GC safe points in the machine code. (Roots are
    28 // identified in SelectionDAGISel.)
    27 // MachineCodeAnalysis identifies the GC safe points in the machine code. (Roots
    28 // are identified in SelectionDAGISel.)
    2929 //
    3030 //===----------------------------------------------------------------------===//
    3131
    3434
    3535 #include "llvm/CodeGen/CollectorMetadata.h"
    3636 #include
    37 #include
    3738
    3839 namespace llvm {
    39
    40 class AsmPrinter;
    41 class FunctionPassManager;
    42 class PassManager;
    43 class TargetAsmInfo;
    44
    4540
    4641 /// Collector describes a garbage collector's code generation requirements,
    4742 /// and provides overridable hooks for those needs which cannot be abstractly
    4843 /// described.
    4944 class Collector {
    45 public:
    46 typedef std::vector list_type;
    47 typedef list_type::iterator iterator;
    48
    49 private:
    50 friend class CollectorModuleMetadata;
    51 const Module *M;
    52 std::string Name;
    53
    54 list_type Functions;
    55
    5056 protected:
    5157 unsigned NeededSafePoints; //< Bitmask of required safe points.
    5258 bool CustomReadBarriers; //< Default is to insert loads.
    5460 bool CustomRoots; //< Default is to pass through to backend.
    5561 bool InitRoots; //< If set, roots are nulled during lowering.
    5662
    57 /// If any of the actions are set to Custom, this is expected to be
    58 /// overriden to create a transform to lower those actions to LLVM IR.
    59 virtual Pass *createCustomLoweringPass() const;
    60
    6163 public:
    6264 Collector();
    6365
    6466 virtual ~Collector();
    6567
    6668
    69 /// getName - The name of the collector, for debugging.
    70 ///
    71 const std::string &getName() const { return Name; }
    72
    73 /// getModule - The module upon which the collector is operating.
    74 ///
    75 const Module &getModule() const { return *M; }
    76
    6777 /// True if this collector requires safe points of any kind. By default,
    6878 /// none are recorded.
    6979 bool needsSafePoints() const { return NeededSafePoints != 0; }
    93103 bool initializeRoots() const { return InitRoots; }
    94104
    95105
    96 /// Adds LLVM IR transforms to handle collection intrinsics. By default,
    97 /// read- and write barriers are replaced with direct memory accesses, and
    98 /// roots are passed on to the code generator.
    99 void addLoweringPasses(FunctionPassManager &PM) const;
    106 /// beginAssembly/finishAssembly - Emit module metadata as assembly code.
    107 virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP,
    108 const TargetAsmInfo &TAI);
    109 virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP,
    110 const TargetAsmInfo &TAI);
    100111
    101 /// Same as addLoweringPasses(FunctionPassManager &), except uses a
    102 /// PassManager for compatibility with unusual backends (such as MSIL or
    103 /// CBackend).
    104 void addLoweringPasses(PassManager &PM) const;
    105
    106 /// Adds target-independent MachineFunction pass to mark safe points. This
    107 /// is added very late during code generation, just prior to output, and
    108 /// importantly after all CFG transformations (like branch folding).
    109 void addGenericMachineCodePass(FunctionPassManager &PM,
    110 const TargetMachine &TM, bool Fast) const;
    111
    112 /// beginAssembly/finishAssembly - Emit module metadata as assembly code.
    113 virtual void beginAssembly(Module &M, std::ostream &OS, AsmPrinter &AP,
    114 const TargetAsmInfo &TAI) const;
    115 virtual void finishAssembly(Module &M, CollectorModuleMetadata &CMM,
    116 std::ostream &OS, AsmPrinter &AP,
    117 const TargetAsmInfo &TAI) const;
    118
    119 private:
    120 bool NeedsDefaultLoweringPass() const;
    121 bool NeedsCustomLoweringPass() const;
    122
    112 /// begin/end - Iterators for function metadata.
    113 ///
    114 iterator begin() { return Functions.begin(); }
    115 iterator end() { return Functions.end(); }
    116
    117 /// insertFunctionMetadata - Creates metadata for a function.
    118 ///
    119 CollectorMetadata *insertFunctionMetadata(const Function &F);
    120
    121 /// initializeCustomLowering/performCustomLowering - If any of the actions
    122 /// are set to custom, performCustomLowering must be overriden to create a
    123 /// transform to lower those actions to LLVM IR. initializeCustomLowering
    124 /// is optional to override. These are the only Collector methods through
    125 /// which the LLVM IR can be modified.
    126 virtual bool initializeCustomLowering(Module &F);
    127 virtual bool performCustomLowering(Function &F);
    123128 };
    124
    125
    126 /// If set, the code generator should generate garbage collection as specified
    127 /// by the collector properties.
    128 extern const Collector *TheCollector; // FIXME: Find a better home!
    129129
    130130 }
    131131
    88 //
    99 // This file declares the CollectorMetadata and CollectorModuleMetadata classes,
    1010 // which are used as a communication channel from the target code generator
    11 // to the target garbage collector. This interface allows code generators and
    11 // to the target garbage collectors. This interface allows code generators and
    1212 // garbage collectors to be developed independently.
    1313 //
    1414 // The CollectorMetadata class records the data necessary to build a type
    3636
    3737 #include "llvm/Pass.h"
    3838 #include "llvm/ADT/DenseMap.h"
    39 #include "llvm/ADT/StringMap.h"
    3940
    4041 namespace llvm {
    4142
    43 class AsmPrinter;
    44 class Collector;
    4245 class Constant;
    43
    44
    45 /// Creates a pass to print collector metadata.
    46 ///
    47 Pass *createCollectorMetadataPrinter(std::ostream &OS);
    48
    49 /// Creates a pass to destroy collector metadata.
    50 ///
    51 Pass *createCollectorMetadataDeleter();
    46 class TargetAsmInfo;
    5247
    5348
    5449 namespace GC {
    7671 struct GCRoot {
    7772 int Num; //< Usually a frame index.
    7873 int StackOffset; //< Offset from the stack pointer.
    79 Constant *Metadata; //< From the call to llvm.gcroot.
    74 Constant *Metadata; //< Metadata straight from the call to llvm.gcroot.
    8075
    8176 GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
    8277 };
    9287
    9388 private:
    9489 const Function &F;
    90 Collector &C;
    9591 uint64_t FrameSize;
    9692 std::vector Roots;
    9793 std::vector SafePoints;
    106102 // The bit vector is the more compact representation where >3.2% of roots
    107103 // are live per safe point (1.5% on 64-bit hosts).
    108104
    109 friend class CollectorModuleMetadata;
    110 CollectorMetadata(const Function &F);
    111
    112105 public:
    106 CollectorMetadata(const Function &F, Collector &C);
    113107 ~CollectorMetadata();
    114108
    109 /// getFunction - Return the function to which this metadata applies.
    110 ///
    115111 const Function &getFunction() const { return F; }
    112
    113 /// getCollector - Return the collector for the function.
    114 ///
    115 Collector &getCollector() { return C; }
    116116
    117117 /// addStackRoot - Registers a root that lives on the stack. Num is the
    118118 /// stack object ID for the alloca (if the code generator is using
    156156 /// CollectorModuleMetadata - Garbage collection metadata for a whole module.
    157157 ///
    158158 class CollectorModuleMetadata : public ImmutablePass {
    159 typedef std::vector list_type;
    160 typedef DenseMap map_type;
    159 typedef StringMap collector_map_type;
    160 typedef std::vector list_type;
    161 typedef DenseMap function_map_type;
    161162
    162 Module *Mod;
    163 list_type Functions;
    164 map_type Map;
    163 collector_map_type NameMap;
    164 list_type Collectors;
    165 function_map_type Map;
    166
    167 Collector *getOrCreateCollector(const Module *M, const std::string &Name);
    165168
    166169 public:
    167 typedef list_type::iterator iterator;
    170 typedef list_type::const_iterator iterator;
    168171
    169172 static char ID;
    170173
    171174 CollectorModuleMetadata();
    172175 ~CollectorModuleMetadata();
    173176
    174 /// clear - Used to delete module metadata. Collector invokes this as
    175 /// necessary.
    177 /// clear - Used to delete module metadata. The metadata deleter pass calls
    178 /// this.
    176179 void clear();
    177180
    178 /// begin/end - Iterators for function metadata.
    181 /// begin/end - Iterators for collectors.
    179182 ///
    180 iterator begin() { return Functions.begin(); }
    181 iterator end() { return Functions.end(); }
    183 iterator begin() const { return Collectors.begin(); }
    184 iterator end() const { return Collectors.end(); }
    182185
    183 /// insert - Creates metadata for a function.
    186 /// get - Look up function metadata.
    184187 ///
    185 CollectorMetadata& insert(const Function *F);
    186
    187 /// get - Looks up existing function metadata.
    188 ///
    189 CollectorMetadata* get(const Function *F) const;
    188 CollectorMetadata &get(const Function &F);
    190189 };
    191190
    192191 }
    2424 ///
    2525 typedef Registry CollectorRegistry;
    2626
    27 /// FIXME: Collector instances are not useful on their own. These no longer
    28 /// serve any purpose except to link in the plugins.
    29
    2730 /// Creates an ocaml-compatible garbage collector.
    2831 Collector *createOcamlCollector();
    2932
    134134 /// for the Sparc.
    135135 FunctionPass *getRegisterAllocator(TargetMachine &T);
    136136
    137 /// IntrinsicLowering Pass - Performs target-independent LLVM IR
    138 /// transformations for highly portable collectors.
    139 FunctionPass *createGCLoweringPass();
    140
    141 /// MachineCodeAnalysis Pass - Target-independent pass to mark safe points in
    142 /// machine code. Must be added very late during code generation, just prior
    143 /// to output, and importantly after all CFG transformations (such as branch
    144 /// folding).
    145 FunctionPass *createGCMachineCodeAnalysisPass();
    146
    147 /// Deleter Pass - Releases collector metadata.
    148 ///
    149 FunctionPass *createCollectorMetadataDeleter();
    150
    151 /// Creates a pass to print collector metadata.
    152 ///
    153 FunctionPass *createCollectorMetadataPrinter(std::ostream &OS);
    154
    137155 /// createMachineLICMPass - This pass performs LICM on machine instructions.
    138156 ///
    139157 FunctionPass *createMachineLICMPass();
    1212 //===----------------------------------------------------------------------===//
    1313
    1414 #include "llvm/CodeGen/Collector.h"
    15 #include "llvm/CodeGen/Passes.h"
    1516 #include "llvm/IntrinsicInst.h"
    1617 #include "llvm/Module.h"
    1718 #include "llvm/PassManager.h"
    2829
    2930 namespace {
    3031
    31 /// This pass rewrites calls to the llvm.gcread or llvm.gcwrite intrinsics,
    32 /// replacing them with simple loads and stores as directed by the Collector.
    33 /// This is useful for most garbage collectors.
    32 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
    33 /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
    34 /// directed by the Collector. It also performs automatic root initialization
    35 /// and custom intrinsic lowering.
    3436 class VISIBILITY_HIDDEN LowerIntrinsics : public FunctionPass {
    35 const Collector &Coll;
    36
    3737 /// GCRootInt, GCReadInt, GCWriteInt - The function prototypes for the
    3838 /// llvm.gc* intrinsics.
    3939 Function *GCRootInt, *GCReadInt, *GCWriteInt;
    4040
    41 static bool NeedsDefaultLoweringPass(const Collector &C);
    42 static bool NeedsCustomLoweringPass(const Collector &C);
    4143 static bool CouldBecomeSafePoint(Instruction *I);
    42 static void InsertRootInitializers(Function &F,
    44 bool PerformDefaultLowering(Function &F, Collector &Coll);
    45 static bool InsertRootInitializers(Function &F,
    4346 AllocaInst **Roots, unsigned Count);
    4447
    4548 public:
    4649 static char ID;
    4750
    48 LowerIntrinsics(const Collector &GC);
    51 LowerIntrinsics();
    4952 const char *getPassName() const;
    53 void getAnalysisUsage(AnalysisUsage &AU) const;
    5054
    5155 bool doInitialization(Module &M);
    5256 bool runOnFunction(Function &F);
    5357 };
    5458
    5559
    56 /// This is a target-independent pass over the machine function representation
    57 /// to identify safe points for the garbage collector in the machine code. It
    58 /// inserts labels at safe points and populates the GCInfo class.
    60 /// MachineCodeAnalysis - This is a target-independent pass over the machine
    61 /// function representation to identify safe points for the garbage collector
    62 /// in the machine code. It inserts labels at safe points and populates a
    63 /// CollectorMetadata record for each function.
    5964 class VISIBILITY_HIDDEN MachineCodeAnalysis : public MachineFunctionPass {
    60 const Collector &Coll;
    61 const TargetMachine &Targ;
    62
    65 const TargetMachine *TM;
    6366 CollectorMetadata *MD;
    6467 MachineModuleInfo *MMI;
    6568 const TargetInstrInfo *TII;
    7578 public:
    7679 static char ID;
    7780
    78 MachineCodeAnalysis(const Collector &C, const TargetMachine &T);
    81 MachineCodeAnalysis();
    7982 const char *getPassName() const;
    8083 void getAnalysisUsage(AnalysisUsage &AU) const;
    8184
    8588 }
    8689
    8790 // -----------------------------------------------------------------------------
    88
    89 const Collector *llvm::TheCollector = 0;
    9091
    9192 Collector::Collector() :
    9293 NeededSafePoints(0),
    9697 InitRoots(true)
    9798 {}
    9899
    99 Collector::~Collector() {}
    100
    101 void Collector::addLoweringPasses(FunctionPassManager &PM) const {
    102 if (NeedsDefaultLoweringPass())
    103 PM.add(new LowerIntrinsics(*this));
    104
    105 if (NeedsCustomLoweringPass())
    106 PM.add(createCustomLoweringPass());
    107 }
    108
    109 void Collector::addLoweringPasses(PassManager &PM) const {
    110 if (NeedsDefaultLoweringPass())
    111 PM.add(new LowerIntrinsics(*this));
    112
    113 if (NeedsCustomLoweringPass())
    114 PM.add(createCustomLoweringPass());
    115 }
    116
    117 void Collector::addGenericMachineCodePass(FunctionPassManager &PM,
    118 const TargetMachine &TM,
    119 bool Fast) const {
    120 if (needsSafePoints())
    121 PM.add(new MachineCodeAnalysis(*this, TM));
    122 }
    123
    124 bool Collector::NeedsDefaultLoweringPass() const {
    125 // Default lowering is necessary only if read or write barriers have a default
    126 // action. The default for roots is no action.
    127 return !customWriteBarrier()
    128 || !customReadBarrier()
    129 || initializeRoots();
    130 }
    131
    132 bool Collector::NeedsCustomLoweringPass() const {
    133 // Custom lowering is only necessary if enabled for some action.
    134 return customWriteBarrier()
    135 || customReadBarrier()
    136 || customRoots();
    137 }
    138
    139 Pass *Collector::createCustomLoweringPass() const {
    140 cerr << "Collector must override createCustomLoweringPass.\n";
    100 Collector::~Collector() {
    101 for (iterator I = begin(), E = end(); I != E; ++I)
    102 delete *I;
    103
    104 Functions.clear();
    105 }
    106
    107 bool Collector::initializeCustomLowering(Module &M) { return false; }
    108
    109 bool Collector::performCustomLowering(Function &F) {
    110 cerr << "gc " << getName() << " must override performCustomLowering.\n";
    141111 abort();
    142112 return 0;
    143113 }
    144114
    145 void Collector::beginAssembly(Module &M, std::ostream &OS, AsmPrinter &AP,
    146 const TargetAsmInfo &TAI) const {
    115 void Collector::beginAssembly(std::ostream &OS, AsmPrinter &AP,
    116 const TargetAsmInfo &TAI) {
    147117 // Default is no action.
    148118 }
    149119
    150 void Collector::finishAssembly(Module &M, CollectorModuleMetadata &CMM,
    151 std::ostream &OS, AsmPrinter &AP,
    152 const TargetAsmInfo &TAI) const {
    120 void Collector::finishAssembly(std::ostream &OS, AsmPrinter &AP,
    121 const TargetAsmInfo &TAI) {
    153122 // Default is no action.
    154123 }
    124
    125 CollectorMetadata *Collector::insertFunctionMetadata(const Function &F) {
    126 CollectorMetadata *CM = new CollectorMetadata(F, *this);
    127 Functions.push_back(CM);
    128 return CM;
    129 }
    155130
    156131 // -----------------------------------------------------------------------------
    157132
    133 FunctionPass *llvm::createGCLoweringPass() {
    134 return new LowerIntrinsics();
    135 }
    136
    158137 char LowerIntrinsics::ID = 0;
    159138
    160 LowerIntrinsics::LowerIntrinsics(const Collector &C)
    161 : FunctionPass((intptr_t)&ID), Coll(C),
    139 LowerIntrinsics::LowerIntrinsics()
    140 : FunctionPass((intptr_t)&ID),
    162141 GCRootInt(0), GCReadInt(0), GCWriteInt(0) {}
    163142
    164143 const char *LowerIntrinsics::getPassName() const {
    165144 return "Lower Garbage Collection Instructions";
    166145 }
    167146
    168 /// doInitialization - If this module uses the GC intrinsics, find them now. If
    169 /// not, this pass does not do anything.
    147 void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
    148 FunctionPass::getAnalysisUsage(AU);
    149 AU.addRequired();
    150 }
    151
    152 /// doInitialization - If this module uses the GC intrinsics, find them now.
    170153 bool LowerIntrinsics::doInitialization(Module &M) {
    171154 GCReadInt = M.getFunction("llvm.gcread");
    172155 GCWriteInt = M.getFunction("llvm.gcwrite");
    173156 GCRootInt = M.getFunction("llvm.gcroot");
    174 return false;
    175 }
    176
    177 void LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
    157
    158 // FIXME: This is rather antisocial in the context of a JIT since it performs
    159 // work against the entire module. But this cannot be done at
    160 // runFunction time (initializeCustomLowering likely needs to change
    161 // the module).
    162 CollectorModuleMetadata *CMM = getAnalysisToUpdate();
    163 assert(CMM && "LowerIntrinsics didn't require CollectorModuleMetadata!?");
    164 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
    165 if (I->hasCollector())
    166 CMM->get(*I); // Instantiate the Collector.
    167
    168 bool MadeChange = false;
    169 for (CollectorModuleMetadata::iterator I = CMM->begin(),
    170 E = CMM->end(); I != E; ++I)
    171 if (NeedsCustomLoweringPass(**I))
    172 if ((*I)->initializeCustomLowering(M))
    173 MadeChange = true;
    174
    175 return MadeChange;
    176 }
    177
    178 bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots,
    178179 unsigned Count) {
    179180 // Scroll past alloca instructions.
    180181 BasicBlock::iterator IP = F.getEntryBlock().begin();
    189190 InitedRoots.insert(AI);
    190191
    191192 // Add root initializers.
    193 bool MadeChange = false;
    194
    192195 for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I)
    193 if (!InitedRoots.count(*I))
    196 if (!InitedRoots.count(*I)) {
    194197 new StoreInst(ConstantPointerNull::get(cast(
    195198 cast((*I)->getType())->getElementType())),
    196199 *I, IP);
    200 MadeChange = true;
    201 }
    202
    203 return MadeChange;
    204 }
    205
    206 bool LowerIntrinsics::NeedsDefaultLoweringPass(const Collector &C) {
    207 // Default lowering is necessary only if read or write barriers have a default
    208 // action. The default for roots is no action.
    209 return !C.customWriteBarrier()
    210 || !C.customReadBarrier()
    211 || C.initializeRoots();
    212 }
    213
    214 bool LowerIntrinsics::NeedsCustomLoweringPass(const Collector &C) {
    215 // Custom lowering is only necessary if enabled for some action.
    216 return C.customWriteBarrier()
    217 || C.customReadBarrier()
    218 || C.customRoots();
    197219 }
    198220
    199221 /// CouldBecomeSafePoint - Predicate to conservatively determine whether the
    227249 /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
    228250 /// Leave gcroot intrinsics; the code generator needs to see those.
    229251 bool LowerIntrinsics::runOnFunction(Function &F) {
    230 // Quick exit for programs that do not declare the intrinsics.
    231 if (!GCReadInt && !GCWriteInt && !GCRootInt) return false;
    232
    252 // Quick exit for functions that do not use GC.
    253 if (!F.hasCollector()) return false;
    254
    255 CollectorMetadata &MD = getAnalysis().get(F);
    256 Collector &Coll = MD.getCollector();
    257
    258 bool MadeChange = false;
    259
    260 if (NeedsDefaultLoweringPass(Coll))
    261 MadeChange |= PerformDefaultLowering(F, Coll);
    262
    263 if (NeedsCustomLoweringPass(Coll))
    264 MadeChange |= Coll.performCustomLowering(F);
    265
    266 return MadeChange;
    267 }
    268
    269 bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) {
    233270 bool LowerWr = !Coll.customWriteBarrier();
    234271 bool LowerRd = !Coll.customReadBarrier();
    235272 bool InitRoots = Coll.initializeRoots();
    267304 }
    268305
    269306 if (Roots.size())
    270 InsertRootInitializers(F, Roots.begin(), Roots.size());
    307 MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size());
    271308
    272309 return MadeChange;
    273310 }
    274311
    275312 // -----------------------------------------------------------------------------
    276313
    314 FunctionPass *llvm::createGCMachineCodeAnalysisPass() {
    315 return new MachineCodeAnalysis();
    316 }
    317
    277318 char MachineCodeAnalysis::ID = 0;
    278319
    279 MachineCodeAnalysis::MachineCodeAnalysis(const Collector &C, const TargetMachine &T)
    280 : MachineFunctionPass(intptr_t(&ID)), Coll(C), Targ(T) {}
    320 MachineCodeAnalysis::MachineCodeAnalysis()
    321 : MachineFunctionPass(intptr_t(&ID)) {}
    281322
    282323 const char *MachineCodeAnalysis::getPassName() const {
    283324 return "Analyze Machine Code For Garbage Collection";
    303344 MachineBasicBlock::iterator RAI = CI;
    304345 ++RAI;
    305346
    306 if (Coll.needsSafePoint(GC::PreCall))
    347 if (MD->getCollector().needsSafePoint(GC::PreCall))
    307348 MD->addSafePoint(GC::PreCall, InsertLabel(*CI->getParent(), CI));
    308349
    309 if (Coll.needsSafePoint(GC::PostCall))
    350 if (MD->getCollector().needsSafePoint(GC::PostCall))
    310351 MD->addSafePoint(GC::PostCall, InsertLabel(*CI->getParent(), RAI));
    311352 }
    312353
    322363 void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) {
    323364 uint64_t StackSize = MFI->getStackSize();
    324365 uint64_t OffsetAdjustment = MFI->getOffsetAdjustment();
    325 uint64_t OffsetOfLocalArea = Targ.getFrameInfo()->getOffsetOfLocalArea();
    366 uint64_t OffsetOfLocalArea = TM->getFrameInfo()->getOffsetOfLocalArea();
    326367
    327368 for (CollectorMetadata::roots_iterator RI = MD->roots_begin(),
    328369 RE = MD->roots_end(); RI != RE; ++RI)
    331372 }
    332373
    333374 bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) {
    334 if (!Coll.needsSafePoints())
    375 // Quick exit for functions that do not use GC.
    376 if (!MF.getFunction()->hasCollector()) return false;
    377
    378 MD = &getAnalysis().get(*MF.getFunction());
    379 if (!MD->getCollector().needsSafePoints())
    335380 return false;
    336381
    337 MD = getAnalysis().get(MF.getFunction());
    382 TM = &MF.getTarget();
    338383 MMI = &getAnalysis();
    339 TII = MF.getTarget().getInstrInfo();
    384 TII = TM->getInstrInfo();
    340385 MFI = MF.getFrameInfo();
    341386
    342387 // Find the size of the stack frame.
    1212 //===----------------------------------------------------------------------===//
    1313
    1414 #include "llvm/CodeGen/CollectorMetadata.h"
    15 #include "llvm/CodeGen/Collector.h"
    16 #include "llvm/CodeGen/Collectors.h"
    1517 #include "llvm/CodeGen/MachineFrameInfo.h"
    16 #include "llvm/CodeGen/MachineFunctionPass.h"
    18 #include "llvm/Pass.h"
    19 #include "llvm/CodeGen/Passes.h"
    1720 #include "llvm/Function.h"
    1821 #include "llvm/Support/Compiler.h"
    1922
    2124
    2225 namespace {
    2326
    24 class VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
    27 class VISIBILITY_HIDDEN Printer : public FunctionPass {
    2528 static char ID;
    2629 std::ostream &OS;
    2730
    3134 const char *getPassName() const;
    3235 void getAnalysisUsage(AnalysisUsage &AU) const;
    3336
    34 bool runOnMachineFunction(MachineFunction &MF);
    37 bool runOnFunction(Function &F);
    3538 };
    3639
    37 class VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
    40 class VISIBILITY_HIDDEN Deleter : public FunctionPass {
    3841 static char ID;
    3942
    4043 public:
    4346 const char *getPassName() const;
    4447 void getAnalysisUsage(AnalysisUsage &AU) const;
    4548
    46 bool runOnMachineFunction(MachineFunction &MF);
    49 bool runOnFunction(Function &F);
    4750 bool doFinalization(Module &M);
    4851 };
    4952
    5457
    5558 // -----------------------------------------------------------------------------
    5659
    57 CollectorMetadata::CollectorMetadata(const Function &F)
    58 : F(F), FrameSize(~0LL) {}
    60 CollectorMetadata::CollectorMetadata(const Function &F, Collector &C)
    61 : F(F), C(C), FrameSize(~0LL) {}
    5962
    6063 CollectorMetadata::~CollectorMetadata() {}
    6164
    7073 clear();
    7174 }
    7275
    73 CollectorMetadata& CollectorModuleMetadata::insert(const Function *F) {
    74 assert(Map.find(F) == Map.end() && "Function GC metadata already exists!");
    75 CollectorMetadata *FMD = new CollectorMetadata(*F);
    76 Functions.push_back(FMD);
    77 Map[F] = FMD;
    78 return *FMD;
    79 }
    80
    81 CollectorMetadata* CollectorModuleMetadata::get(const Function *F) const {
    82 map_type::iterator I = Map.find(F);
    83 if (I == Map.end())
    84 return 0;
    85 return I->second;
    76 Collector *CollectorModuleMetadata::
    77 getOrCreateCollector(const Module *M, const std::string &Name) {
    78 const char *Start = Name.c_str();
    79
    80 collector_map_type::iterator NMI = NameMap.find(Start, Start + Name.size());
    81 if (NMI != NameMap.end())
    82 return NMI->getValue();
    83
    84 for (CollectorRegistry::iterator I = CollectorRegistry::begin(),
    85 E = CollectorRegistry::end(); I != E; ++I) {
    86 if (strcmp(Start, I->getName()) == 0) {
    87 Collector *C = I->instantiate();
    88 C->M = M;
    89 C->Name = Name;
    90 NameMap.GetOrCreateValue(Start, Start + Name.size()).setValue(C);
    91 Collectors.push_back(C);
    92 return C;
    93 }
    94 }
    95
    96 cerr << "unsupported collector: " << Name << "\n";
    97 abort();
    98 }
    99
    100 CollectorMetadata &CollectorModuleMetadata::get(const Function &F) {
    101 assert(F.hasCollector());
    102 function_map_type::iterator I = Map.find(&F);
    103 if (I != Map.end())
    104 return *I->second;
    105
    106 Collector *C = getOrCreateCollector(F.getParent(), F.getCollector());
    107 CollectorMetadata *MD = C->insertFunctionMetadata(F);
    108 Map[&F] = MD;
    109 return *MD;
    86110 }
    87111
    88112 void CollectorModuleMetadata::clear() {
    113 Map.clear();
    114
    115 // TODO: StringMap should provide a clear method.
    116 while (!NameMap.empty())
    117 NameMap.erase(NameMap.begin());
    118
    89119 for (iterator I = begin(), E = end(); I != E; ++I)
    90120 delete *I;
    91
    92 Functions.clear();
    93 Map.clear();
    121 Collectors.clear();
    94122 }
    95123
    96124 // -----------------------------------------------------------------------------
    97125
    98126 char Printer::ID = 0;
    99127
    100 Pass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
    128 FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) {
    101129 return new Printer(OS);
    102130 }
    103131
    104132 Printer::Printer(std::ostream &OS)
    105 : MachineFunctionPass(intptr_t(&ID)), OS(OS) {}
    133 : FunctionPass(intptr_t(&ID)), OS(OS) {}
    106134
    107135 const char *Printer::getPassName() const {
    108136 return "Print Garbage Collector Information";
    109137 }
    110138
    111139 void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
    112 MachineFunctionPass::getAnalysisUsage(AU);
    140 FunctionPass::getAnalysisUsage(AU);
    113141 AU.setPreservesAll();
    114142 AU.addRequired();
    115143 }
    124152 }
    125153 }
    126154
    127 bool Printer::runOnMachineFunction(MachineFunction &MF) {
    128 if (CollectorMetadata *FD =
    129 getAnalysis().get(MF.getFunction())) {
    155 bool Printer::runOnFunction(Function &F) {
    156 if (F.hasCollector()) {
    157 CollectorMetadata *FD = &getAnalysis().get(F);
    130158
    131159 OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n";
    132160 for (CollectorMetadata::roots_iterator RI = FD->roots_begin(),
    159187
    160188 char Deleter::ID = 0;
    161189
    162 Pass *llvm::createCollectorMetadataDeleter() {
    190 FunctionPass *llvm::createCollectorMetadataDeleter() {
    163191 return new Deleter();
    164192 }
    165193
    166 Deleter::Deleter() : MachineFunctionPass(intptr_t(&ID)) {}
    194 Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {}
    167195
    168196 const char *Deleter::getPassName() const {
    169197 return "Delete Garbage Collector Information";
    174202 AU.addRequired();
    175203 }
    176204
    177 bool Deleter::runOnMachineFunction(MachineFunction &MF) {
    205 bool Deleter::runOnFunction(Function &MF) {
    178206 return false;
    179207 }
    180208
    181209 bool Deleter::doFinalization(Module &M) {
    182 getAnalysis().clear();
    210 CollectorModuleMetadata *CMM = getAnalysisToUpdate();
    211 assert(CMM && "Deleter didn't require CollectorModuleMetadata?!");
    212 CMM->clear();
    183213 return false;
    184214 }