From 5a29c9eed157af51a8d338b5a225b146881819e8 Mon Sep 17 00:00:00 2001 From: Gordon Henriksen Date: Sun, 17 Aug 2008 12:56:54 +0000 Subject: Factor GC metadata table assembly generation out of Collector in preparation for splitting AsmPrinter into its own library. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54881 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/Collector.h | 167 --------- include/llvm/CodeGen/CollectorMetadata.h | 194 ----------- include/llvm/CodeGen/Collectors.h | 44 --- include/llvm/CodeGen/GCMedataPrinter.h | 167 +++++++++ include/llvm/CodeGen/GCMetadata.h | 194 +++++++++++ include/llvm/CodeGen/GCStrategy.h | 167 +++++++++ include/llvm/CodeGen/GCs.h | 47 +++ include/llvm/CodeGen/LinkAllCodegenComponents.h | 3 +- lib/CodeGen/AsmPrinter.cpp | 6 +- lib/CodeGen/Collector.cpp | 407 ---------------------- lib/CodeGen/CollectorMetadata.cpp | 212 ------------ lib/CodeGen/Collectors.cpp | 28 -- lib/CodeGen/GCMetadata.cpp | 212 ++++++++++++ lib/CodeGen/GCMetadataPrinter.cpp | 31 ++ lib/CodeGen/GCStrategy.cpp | 389 +++++++++++++++++++++ lib/CodeGen/GCs.cpp | 28 ++ lib/CodeGen/LLVMTargetMachine.cpp | 2 +- lib/CodeGen/OcamlCollector.cpp | 178 ---------- lib/CodeGen/OcamlGC.cpp | 46 +++ lib/CodeGen/OcamlGCPrinter.cpp | 163 +++++++++ lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 2 +- lib/CodeGen/ShadowStackCollector.cpp | 441 ------------------------ lib/CodeGen/ShadowStackGC.cpp | 441 ++++++++++++++++++++++++ 23 files changed, 1892 insertions(+), 1677 deletions(-) delete mode 100644 include/llvm/CodeGen/Collector.h delete mode 100644 include/llvm/CodeGen/CollectorMetadata.h delete mode 100644 include/llvm/CodeGen/Collectors.h create mode 100644 include/llvm/CodeGen/GCMedataPrinter.h create mode 100644 include/llvm/CodeGen/GCMetadata.h create mode 100644 include/llvm/CodeGen/GCStrategy.h create mode 100644 include/llvm/CodeGen/GCs.h delete mode 100644 lib/CodeGen/Collector.cpp delete mode 100644 lib/CodeGen/CollectorMetadata.cpp delete mode 100644 lib/CodeGen/Collectors.cpp create mode 100644 lib/CodeGen/GCMetadata.cpp create mode 100644 lib/CodeGen/GCMetadataPrinter.cpp create mode 100644 lib/CodeGen/GCStrategy.cpp create mode 100644 lib/CodeGen/GCs.cpp delete mode 100644 lib/CodeGen/OcamlCollector.cpp create mode 100644 lib/CodeGen/OcamlGC.cpp create mode 100644 lib/CodeGen/OcamlGCPrinter.cpp delete mode 100644 lib/CodeGen/ShadowStackCollector.cpp create mode 100644 lib/CodeGen/ShadowStackGC.cpp diff --git a/include/llvm/CodeGen/Collector.h b/include/llvm/CodeGen/Collector.h deleted file mode 100644 index 20b470296e..0000000000 --- a/include/llvm/CodeGen/Collector.h +++ /dev/null @@ -1,167 +0,0 @@ -//===-- llvm/CodeGen/Collector.h - Garbage collection -----------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Collector records sufficient information about a machine function to enable -// accurate garbage collectors. Specifically: -// -// - Safe points -// Garbage collection is only possible at certain points in code. Code -// generators should record points: -// -// - At and after any call to a subroutine -// - Before returning from the current function -// - Before backwards branches (loops) -// -// - Roots -// When a reference to a GC-allocated object exists on the stack, it must be -// stored in an alloca registered with llvm.gcoot. -// -// This generic information should used by ABI-specific passes to emit support -// tables for the runtime garbage collector. -// -// MachineCodeAnalysis identifies the GC safe points in the machine code. (Roots -// are identified in SelectionDAGISel.) -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CODEGEN_COLLECTOR_H -#define LLVM_CODEGEN_COLLECTOR_H - -#include "llvm/CodeGen/CollectorMetadata.h" -#include -#include - -namespace llvm { - - /// Collector describes a garbage collector's code generation requirements, - /// and provides overridable hooks for those needs which cannot be abstractly - /// described. - class Collector { - public: - typedef std::vector list_type; - typedef list_type::iterator iterator; - - private: - friend class CollectorModuleMetadata; - const Module *M; - std::string Name; - - list_type Functions; - - protected: - unsigned NeededSafePoints; //< Bitmask of required safe points. - bool CustomReadBarriers; //< Default is to insert loads. - bool CustomWriteBarriers; //< Default is to insert stores. - bool CustomRoots; //< Default is to pass through to backend. - bool InitRoots; //< If set, roots are nulled during lowering. - bool UsesMetadata; //< If set, backend must emit metadata tables. - - public: - Collector(); - - virtual ~Collector(); - - - /// getName - The name of the collector, for debugging. - /// - const std::string &getName() const { return Name; } - - /// getModule - The module upon which the collector is operating. - /// - const Module &getModule() const { return *M; } - - /// True if this collector requires safe points of any kind. By default, - /// none are recorded. - bool needsSafePoints() const { return NeededSafePoints != 0; } - - /// True if the collector requires the given kind of safe point. By default, - /// none are recorded. - bool needsSafePoint(GC::PointKind Kind) const { - return (NeededSafePoints & 1 << Kind) != 0; - } - - /// By default, write barriers are replaced with simple store instructions. - /// If true, then addPassesToCustomLowerIntrinsics must instead process - /// them. - bool customWriteBarrier() const { return CustomWriteBarriers; } - - /// By default, read barriers are replaced with simple load instructions. - /// If true, then addPassesToCustomLowerIntrinsics must instead process - /// them. - bool customReadBarrier() const { return CustomReadBarriers; } - - /// By default, roots are left for the code generator. If Custom, then - /// addPassesToCustomLowerIntrinsics must add passes to delete them. - bool customRoots() const { return CustomRoots; } - - /// If set, gcroot intrinsics should initialize their allocas to null. This - /// is necessary for most collectors. - bool initializeRoots() const { return InitRoots; } - - /// If set, appropriate metadata tables must be emitted by the back-end - /// (assembler, JIT, or otherwise). - bool usesMetadata() const { return UsesMetadata; } - - /// begin/end - Iterators for function metadata. - /// - iterator begin() { return Functions.begin(); } - iterator end() { return Functions.end(); } - - /// insertFunctionMetadata - Creates metadata for a function. - /// - CollectorMetadata *insertFunctionMetadata(const Function &F); - - /// initializeCustomLowering/performCustomLowering - If any of the actions - /// are set to custom, performCustomLowering must be overriden to create a - /// transform to lower those actions to LLVM IR. initializeCustomLowering - /// is optional to override. These are the only Collector methods through - /// which the LLVM IR can be modified. - virtual bool initializeCustomLowering(Module &F); - virtual bool performCustomLowering(Function &F); - }; - - // GCMetadataPrinter - Emits GC metadata as assembly code. - class GCMetadataPrinter { - public: - typedef Collector::list_type list_type; - typedef Collector::iterator iterator; - - private: - Collector *Coll; - - friend class AsmPrinter; - - protected: - // May only be subclassed. - GCMetadataPrinter(); - - // Do not implement. - GCMetadataPrinter(const GCMetadataPrinter &); - GCMetadataPrinter &operator=(const GCMetadataPrinter &); - - public: - Collector &getCollector() { return *Coll; } - const Module &getModule() const { return Coll->getModule(); } - - iterator begin() { return Coll->begin(); } - iterator end() { return Coll->end(); } - - /// beginAssembly/finishAssembly - Emit module metadata as assembly code. - virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI); - - virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI); - - virtual ~GCMetadataPrinter(); - }; - -} - -#endif diff --git a/include/llvm/CodeGen/CollectorMetadata.h b/include/llvm/CodeGen/CollectorMetadata.h deleted file mode 100644 index 1fb3b502ee..0000000000 --- a/include/llvm/CodeGen/CollectorMetadata.h +++ /dev/null @@ -1,194 +0,0 @@ -//===-- CollectorMetadata.h - Garbage collector metadata ------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file declares the CollectorMetadata and CollectorModuleMetadata classes, -// which are used as a communication channel from the target code generator -// to the target garbage collectors. This interface allows code generators and -// garbage collectors to be developed independently. -// -// The CollectorMetadata class records the data necessary to build a type -// accurate stack map. Roots are specified in the LLVM IR using the llvm.gcroot -// intrinsic, which the code generator understands. The code generator records -// the stack offset for each GC root. Safe points are generated by the code -// generator according to the collector's declared needs (generally at function -// calls). -// -// Safe points and roots are sufficient to build type-accurate stack maps. As a -// refinement, liveness analysis calculates the set of live roots at each safe -// point. Liveness analysis is not presently performed, so all roots are assumed -// live. -// -// CollectorModuleMetadata simply collects CollectorMetadata structures for each -// Function as it is compiled. This is necessary for collectors which must emit -// a stack map for the entire compilation unit. CollectorMetadata outlives the -// MachineFunction from which it is derived, so must not refer to any code -// generator data structures. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CODEGEN_COLLECTORMETADATA_H -#define LLVM_CODEGEN_COLLECTORMETADATA_H - -#include "llvm/Pass.h" -#include "llvm/ADT/DenseMap.h" -#include "llvm/ADT/StringMap.h" - -namespace llvm { - - class AsmPrinter; - class Collector; - class Constant; - class TargetAsmInfo; - - - namespace GC { - /// PointKind - The type of a collector-safe point. - /// - enum PointKind { - Loop, //< Instr is a loop (backwards branch). - Return, //< Instr is a return instruction. - PreCall, //< Instr is a call instruction. - PostCall //< Instr is the return address of a call. - }; - } - - /// GCPoint - Metadata for a collector-safe point in machine code. - /// - struct GCPoint { - GC::PointKind Kind; //< The kind of the safe point. - unsigned Num; //< Usually a label. - - GCPoint(GC::PointKind K, unsigned N) : Kind(K), Num(N) {} - }; - - /// GCRoot - Metadata for a pointer to an object managed by the garbage - /// collector. - struct GCRoot { - int Num; //< Usually a frame index. - int StackOffset; //< Offset from the stack pointer. - Constant *Metadata; //< Metadata straight from the call to llvm.gcroot. - - GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {} - }; - - - /// CollectorMetadata - Garbage collection metadata for a function. - /// - class CollectorMetadata { - public: - typedef std::vector::iterator iterator; - typedef std::vector::iterator roots_iterator; - typedef std::vector::const_iterator live_iterator; - - private: - const Function &F; - Collector &C; - uint64_t FrameSize; - std::vector Roots; - std::vector SafePoints; - - // FIXME: Liveness. A 2D BitVector, perhaps? - // - // BitVector Liveness; - // - // bool islive(int point, int root) = - // Liveness[point * SafePoints.size() + root] - // - // The bit vector is the more compact representation where >3.2% of roots - // are live per safe point (1.5% on 64-bit hosts). - - public: - CollectorMetadata(const Function &F, Collector &C); - ~CollectorMetadata(); - - /// getFunction - Return the function to which this metadata applies. - /// - const Function &getFunction() const { return F; } - - /// getCollector - Return the collector for the function. - /// - Collector &getCollector() { return C; } - - /// addStackRoot - Registers a root that lives on the stack. Num is the - /// stack object ID for the alloca (if the code generator is using - /// MachineFrameInfo). - void addStackRoot(int Num, Constant *Metadata) { - Roots.push_back(GCRoot(Num, Metadata)); - } - - /// addSafePoint - Notes the existence of a safe point. Num is the ID of the - /// label just prior to the safe point (if the code generator is using - /// MachineModuleInfo). - void addSafePoint(GC::PointKind Kind, unsigned Num) { - SafePoints.push_back(GCPoint(Kind, Num)); - } - - /// getFrameSize/setFrameSize - Records the function's frame size. - /// - uint64_t getFrameSize() const { return FrameSize; } - void setFrameSize(uint64_t S) { FrameSize = S; } - - /// begin/end - Iterators for safe points. - /// - iterator begin() { return SafePoints.begin(); } - iterator end() { return SafePoints.end(); } - size_t size() const { return SafePoints.size(); } - - /// roots_begin/roots_end - Iterators for all roots in the function. - /// - roots_iterator roots_begin() { return Roots.begin(); } - roots_iterator roots_end () { return Roots.end(); } - size_t roots_size() const { return Roots.size(); } - - /// live_begin/live_end - Iterators for live roots at a given safe point. - /// - live_iterator live_begin(const iterator &p) { return roots_begin(); } - live_iterator live_end (const iterator &p) { return roots_end(); } - size_t live_size(const iterator &p) const { return roots_size(); } - }; - - - /// CollectorModuleMetadata - Garbage collection metadata for a whole module. - /// - class CollectorModuleMetadata : public ImmutablePass { - typedef StringMap collector_map_type; - typedef std::vector list_type; - typedef DenseMap function_map_type; - - collector_map_type NameMap; - list_type Collectors; - function_map_type Map; - - Collector *getOrCreateCollector(const Module *M, const std::string &Name); - - public: - typedef list_type::const_iterator iterator; - - static char ID; - - CollectorModuleMetadata(); - ~CollectorModuleMetadata(); - - /// clear - Used to delete module metadata. The metadata deleter pass calls - /// this. - void clear(); - - /// begin/end - Iterators for collectors. - /// - iterator begin() const { return Collectors.begin(); } - iterator end() const { return Collectors.end(); } - - /// get - Look up function metadata. - /// - CollectorMetadata &get(const Function &F); - }; - -} - -#endif diff --git a/include/llvm/CodeGen/Collectors.h b/include/llvm/CodeGen/Collectors.h deleted file mode 100644 index 1658da1489..0000000000 --- a/include/llvm/CodeGen/Collectors.h +++ /dev/null @@ -1,44 +0,0 @@ -//===-- Collectors.h - Garbage collector registry -------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file declares the CollectorRegistry class, which is used to discover -// pluggable garbage collectors. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CODEGEN_COLLECTORS_H -#define LLVM_CODEGEN_COLLECTORS_H - -#include "llvm/Support/Registry.h" - -namespace llvm { - - class Collector; - class GCMetadataPrinter; - - /// The collector registry uses all the defaults from Registry. - /// - typedef Registry CollectorRegistry; - - /// The GC assembly printer registry uses all the defaults from Registry. - /// - typedef Registry GCMetadataPrinterRegistry; - - /// FIXME: Collector instances are not useful on their own. These no longer - /// serve any purpose except to link in the plugins. - - /// Creates an ocaml-compatible garbage collector. - Collector *createOcamlCollector(); - - /// Creates a shadow stack garbage collector. This collector requires no code - /// generator support. - Collector *createShadowStackCollector(); -} - -#endif diff --git a/include/llvm/CodeGen/GCMedataPrinter.h b/include/llvm/CodeGen/GCMedataPrinter.h new file mode 100644 index 0000000000..20b470296e --- /dev/null +++ b/include/llvm/CodeGen/GCMedataPrinter.h @@ -0,0 +1,167 @@ +//===-- llvm/CodeGen/Collector.h - Garbage collection -----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Collector records sufficient information about a machine function to enable +// accurate garbage collectors. Specifically: +// +// - Safe points +// Garbage collection is only possible at certain points in code. Code +// generators should record points: +// +// - At and after any call to a subroutine +// - Before returning from the current function +// - Before backwards branches (loops) +// +// - Roots +// When a reference to a GC-allocated object exists on the stack, it must be +// stored in an alloca registered with llvm.gcoot. +// +// This generic information should used by ABI-specific passes to emit support +// tables for the runtime garbage collector. +// +// MachineCodeAnalysis identifies the GC safe points in the machine code. (Roots +// are identified in SelectionDAGISel.) +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_COLLECTOR_H +#define LLVM_CODEGEN_COLLECTOR_H + +#include "llvm/CodeGen/CollectorMetadata.h" +#include +#include + +namespace llvm { + + /// Collector describes a garbage collector's code generation requirements, + /// and provides overridable hooks for those needs which cannot be abstractly + /// described. + class Collector { + public: + typedef std::vector list_type; + typedef list_type::iterator iterator; + + private: + friend class CollectorModuleMetadata; + const Module *M; + std::string Name; + + list_type Functions; + + protected: + unsigned NeededSafePoints; //< Bitmask of required safe points. + bool CustomReadBarriers; //< Default is to insert loads. + bool CustomWriteBarriers; //< Default is to insert stores. + bool CustomRoots; //< Default is to pass through to backend. + bool InitRoots; //< If set, roots are nulled during lowering. + bool UsesMetadata; //< If set, backend must emit metadata tables. + + public: + Collector(); + + virtual ~Collector(); + + + /// getName - The name of the collector, for debugging. + /// + const std::string &getName() const { return Name; } + + /// getModule - The module upon which the collector is operating. + /// + const Module &getModule() const { return *M; } + + /// True if this collector requires safe points of any kind. By default, + /// none are recorded. + bool needsSafePoints() const { return NeededSafePoints != 0; } + + /// True if the collector requires the given kind of safe point. By default, + /// none are recorded. + bool needsSafePoint(GC::PointKind Kind) const { + return (NeededSafePoints & 1 << Kind) != 0; + } + + /// By default, write barriers are replaced with simple store instructions. + /// If true, then addPassesToCustomLowerIntrinsics must instead process + /// them. + bool customWriteBarrier() const { return CustomWriteBarriers; } + + /// By default, read barriers are replaced with simple load instructions. + /// If true, then addPassesToCustomLowerIntrinsics must instead process + /// them. + bool customReadBarrier() const { return CustomReadBarriers; } + + /// By default, roots are left for the code generator. If Custom, then + /// addPassesToCustomLowerIntrinsics must add passes to delete them. + bool customRoots() const { return CustomRoots; } + + /// If set, gcroot intrinsics should initialize their allocas to null. This + /// is necessary for most collectors. + bool initializeRoots() const { return InitRoots; } + + /// If set, appropriate metadata tables must be emitted by the back-end + /// (assembler, JIT, or otherwise). + bool usesMetadata() const { return UsesMetadata; } + + /// begin/end - Iterators for function metadata. + /// + iterator begin() { return Functions.begin(); } + iterator end() { return Functions.end(); } + + /// insertFunctionMetadata - Creates metadata for a function. + /// + CollectorMetadata *insertFunctionMetadata(const Function &F); + + /// initializeCustomLowering/performCustomLowering - If any of the actions + /// are set to custom, performCustomLowering must be overriden to create a + /// transform to lower those actions to LLVM IR. initializeCustomLowering + /// is optional to override. These are the only Collector methods through + /// which the LLVM IR can be modified. + virtual bool initializeCustomLowering(Module &F); + virtual bool performCustomLowering(Function &F); + }; + + // GCMetadataPrinter - Emits GC metadata as assembly code. + class GCMetadataPrinter { + public: + typedef Collector::list_type list_type; + typedef Collector::iterator iterator; + + private: + Collector *Coll; + + friend class AsmPrinter; + + protected: + // May only be subclassed. + GCMetadataPrinter(); + + // Do not implement. + GCMetadataPrinter(const GCMetadataPrinter &); + GCMetadataPrinter &operator=(const GCMetadataPrinter &); + + public: + Collector &getCollector() { return *Coll; } + const Module &getModule() const { return Coll->getModule(); } + + iterator begin() { return Coll->begin(); } + iterator end() { return Coll->end(); } + + /// beginAssembly/finishAssembly - Emit module metadata as assembly code. + virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI); + + virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI); + + virtual ~GCMetadataPrinter(); + }; + +} + +#endif diff --git a/include/llvm/CodeGen/GCMetadata.h b/include/llvm/CodeGen/GCMetadata.h new file mode 100644 index 0000000000..1fb3b502ee --- /dev/null +++ b/include/llvm/CodeGen/GCMetadata.h @@ -0,0 +1,194 @@ +//===-- CollectorMetadata.h - Garbage collector metadata ------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file declares the CollectorMetadata and CollectorModuleMetadata classes, +// which are used as a communication channel from the target code generator +// to the target garbage collectors. This interface allows code generators and +// garbage collectors to be developed independently. +// +// The CollectorMetadata class records the data necessary to build a type +// accurate stack map. Roots are specified in the LLVM IR using the llvm.gcroot +// intrinsic, which the code generator understands. The code generator records +// the stack offset for each GC root. Safe points are generated by the code +// generator according to the collector's declared needs (generally at function +// calls). +// +// Safe points and roots are sufficient to build type-accurate stack maps. As a +// refinement, liveness analysis calculates the set of live roots at each safe +// point. Liveness analysis is not presently performed, so all roots are assumed +// live. +// +// CollectorModuleMetadata simply collects CollectorMetadata structures for each +// Function as it is compiled. This is necessary for collectors which must emit +// a stack map for the entire compilation unit. CollectorMetadata outlives the +// MachineFunction from which it is derived, so must not refer to any code +// generator data structures. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_COLLECTORMETADATA_H +#define LLVM_CODEGEN_COLLECTORMETADATA_H + +#include "llvm/Pass.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/StringMap.h" + +namespace llvm { + + class AsmPrinter; + class Collector; + class Constant; + class TargetAsmInfo; + + + namespace GC { + /// PointKind - The type of a collector-safe point. + /// + enum PointKind { + Loop, //< Instr is a loop (backwards branch). + Return, //< Instr is a return instruction. + PreCall, //< Instr is a call instruction. + PostCall //< Instr is the return address of a call. + }; + } + + /// GCPoint - Metadata for a collector-safe point in machine code. + /// + struct GCPoint { + GC::PointKind Kind; //< The kind of the safe point. + unsigned Num; //< Usually a label. + + GCPoint(GC::PointKind K, unsigned N) : Kind(K), Num(N) {} + }; + + /// GCRoot - Metadata for a pointer to an object managed by the garbage + /// collector. + struct GCRoot { + int Num; //< Usually a frame index. + int StackOffset; //< Offset from the stack pointer. + Constant *Metadata; //< Metadata straight from the call to llvm.gcroot. + + GCRoot(int N, Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {} + }; + + + /// CollectorMetadata - Garbage collection metadata for a function. + /// + class CollectorMetadata { + public: + typedef std::vector::iterator iterator; + typedef std::vector::iterator roots_iterator; + typedef std::vector::const_iterator live_iterator; + + private: + const Function &F; + Collector &C; + uint64_t FrameSize; + std::vector Roots; + std::vector SafePoints; + + // FIXME: Liveness. A 2D BitVector, perhaps? + // + // BitVector Liveness; + // + // bool islive(int point, int root) = + // Liveness[point * SafePoints.size() + root] + // + // The bit vector is the more compact representation where >3.2% of roots + // are live per safe point (1.5% on 64-bit hosts). + + public: + CollectorMetadata(const Function &F, Collector &C); + ~CollectorMetadata(); + + /// getFunction - Return the function to which this metadata applies. + /// + const Function &getFunction() const { return F; } + + /// getCollector - Return the collector for the function. + /// + Collector &getCollector() { return C; } + + /// addStackRoot - Registers a root that lives on the stack. Num is the + /// stack object ID for the alloca (if the code generator is using + /// MachineFrameInfo). + void addStackRoot(int Num, Constant *Metadata) { + Roots.push_back(GCRoot(Num, Metadata)); + } + + /// addSafePoint - Notes the existence of a safe point. Num is the ID of the + /// label just prior to the safe point (if the code generator is using + /// MachineModuleInfo). + void addSafePoint(GC::PointKind Kind, unsigned Num) { + SafePoints.push_back(GCPoint(Kind, Num)); + } + + /// getFrameSize/setFrameSize - Records the function's frame size. + /// + uint64_t getFrameSize() const { return FrameSize; } + void setFrameSize(uint64_t S) { FrameSize = S; } + + /// begin/end - Iterators for safe points. + /// + iterator begin() { return SafePoints.begin(); } + iterator end() { return SafePoints.end(); } + size_t size() const { return SafePoints.size(); } + + /// roots_begin/roots_end - Iterators for all roots in the function. + /// + roots_iterator roots_begin() { return Roots.begin(); } + roots_iterator roots_end () { return Roots.end(); } + size_t roots_size() const { return Roots.size(); } + + /// live_begin/live_end - Iterators for live roots at a given safe point. + /// + live_iterator live_begin(const iterator &p) { return roots_begin(); } + live_iterator live_end (const iterator &p) { return roots_end(); } + size_t live_size(const iterator &p) const { return roots_size(); } + }; + + + /// CollectorModuleMetadata - Garbage collection metadata for a whole module. + /// + class CollectorModuleMetadata : public ImmutablePass { + typedef StringMap collector_map_type; + typedef std::vector list_type; + typedef DenseMap function_map_type; + + collector_map_type NameMap; + list_type Collectors; + function_map_type Map; + + Collector *getOrCreateCollector(const Module *M, const std::string &Name); + + public: + typedef list_type::const_iterator iterator; + + static char ID; + + CollectorModuleMetadata(); + ~CollectorModuleMetadata(); + + /// clear - Used to delete module metadata. The metadata deleter pass calls + /// this. + void clear(); + + /// begin/end - Iterators for collectors. + /// + iterator begin() const { return Collectors.begin(); } + iterator end() const { return Collectors.end(); } + + /// get - Look up function metadata. + /// + CollectorMetadata &get(const Function &F); + }; + +} + +#endif diff --git a/include/llvm/CodeGen/GCStrategy.h b/include/llvm/CodeGen/GCStrategy.h new file mode 100644 index 0000000000..dea0785ef8 --- /dev/null +++ b/include/llvm/CodeGen/GCStrategy.h @@ -0,0 +1,167 @@ +//===-- llvm/CodeGen/Collector.h - Garbage collection -----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Collector records sufficient information about a machine function to enable +// accurate garbage collectors. Specifically: +// +// - Safe points +// Garbage collection is only possible at certain points in code. Code +// generators should record points: +// +// - At and after any call to a subroutine +// - Before returning from the current function +// - Before backwards branches (loops) +// +// - Roots +// When a reference to a GC-allocated object exists on the stack, it must be +// stored in an alloca registered with llvm.gcoot. +// +// This generic information should used by ABI-specific passes to emit support +// tables for the runtime garbage collector. +// +// MachineCodeAnalysis identifies the GC safe points in the machine code. (Roots +// are identified in SelectionDAGISel.) +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_COLLECTOR_H +#define LLVM_CODEGEN_COLLECTOR_H + +#include "llvm/CodeGen/GCMetadata.h" +#include +#include + +namespace llvm { + + /// Collector describes a garbage collector's code generation requirements, + /// and provides overridable hooks for those needs which cannot be abstractly + /// described. + class Collector { + public: + typedef std::vector list_type; + typedef list_type::iterator iterator; + + private: + friend class CollectorModuleMetadata; + const Module *M; + std::string Name; + + list_type Functions; + + protected: + unsigned NeededSafePoints; //< Bitmask of required safe points. + bool CustomReadBarriers; //< Default is to insert loads. + bool CustomWriteBarriers; //< Default is to insert stores. + bool CustomRoots; //< Default is to pass through to backend. + bool InitRoots; //< If set, roots are nulled during lowering. + bool UsesMetadata; //< If set, backend must emit metadata tables. + + public: + Collector(); + + virtual ~Collector(); + + + /// getName - The name of the collector, for debugging. + /// + const std::string &getName() const { return Name; } + + /// getModule - The module upon which the collector is operating. + /// + const Module &getModule() const { return *M; } + + /// True if this collector requires safe points of any kind. By default, + /// none are recorded. + bool needsSafePoints() const { return NeededSafePoints != 0; } + + /// True if the collector requires the given kind of safe point. By default, + /// none are recorded. + bool needsSafePoint(GC::PointKind Kind) const { + return (NeededSafePoints & 1 << Kind) != 0; + } + + /// By default, write barriers are replaced with simple store instructions. + /// If true, then addPassesToCustomLowerIntrinsics must instead process + /// them. + bool customWriteBarrier() const { return CustomWriteBarriers; } + + /// By default, read barriers are replaced with simple load instructions. + /// If true, then addPassesToCustomLowerIntrinsics must instead process + /// them. + bool customReadBarrier() const { return CustomReadBarriers; } + + /// By default, roots are left for the code generator. If Custom, then + /// addPassesToCustomLowerIntrinsics must add passes to delete them. + bool customRoots() const { return CustomRoots; } + + /// If set, gcroot intrinsics should initialize their allocas to null. This + /// is necessary for most collectors. + bool initializeRoots() const { return InitRoots; } + + /// If set, appropriate metadata tables must be emitted by the back-end + /// (assembler, JIT, or otherwise). + bool usesMetadata() const { return UsesMetadata; } + + /// begin/end - Iterators for function metadata. + /// + iterator begin() { return Functions.begin(); } + iterator end() { return Functions.end(); } + + /// insertFunctionMetadata - Creates metadata for a function. + /// + CollectorMetadata *insertFunctionMetadata(const Function &F); + + /// initializeCustomLowering/performCustomLowering - If any of the actions + /// are set to custom, performCustomLowering must be overriden to create a + /// transform to lower those actions to LLVM IR. initializeCustomLowering + /// is optional to override. These are the only Collector methods through + /// which the LLVM IR can be modified. + virtual bool initializeCustomLowering(Module &F); + virtual bool performCustomLowering(Function &F); + }; + + // GCMetadataPrinter - Emits GC metadata as assembly code. + class GCMetadataPrinter { + public: + typedef Collector::list_type list_type; + typedef Collector::iterator iterator; + + private: + Collector *Coll; + + friend class AsmPrinter; + + protected: + // May only be subclassed. + GCMetadataPrinter(); + + // Do not implement. + GCMetadataPrinter(const GCMetadataPrinter &); + GCMetadataPrinter &operator=(const GCMetadataPrinter &); + + public: + Collector &getCollector() { return *Coll; } + const Module &getModule() const { return Coll->getModule(); } + + iterator begin() { return Coll->begin(); } + iterator end() { return Coll->end(); } + + /// beginAssembly/finishAssembly - Emit module metadata as assembly code. + virtual void beginAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI); + + virtual void finishAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI); + + virtual ~GCMetadataPrinter(); + }; + +} + +#endif diff --git a/include/llvm/CodeGen/GCs.h b/include/llvm/CodeGen/GCs.h new file mode 100644 index 0000000000..7850ddcd82 --- /dev/null +++ b/include/llvm/CodeGen/GCs.h @@ -0,0 +1,47 @@ +//===-- Collectors.h - Garbage collector registry -------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file declares the CollectorRegistry class, which is used to discover +// pluggable garbage collectors. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_GCS_H +#define LLVM_CODEGEN_GCS_H + +#include "llvm/Support/Registry.h" + +namespace llvm { + + class Collector; + class GCMetadataPrinter; + + /// The collector registry uses all the defaults from Registry. + /// + typedef Registry CollectorRegistry; + + /// The GC assembly printer registry uses all the defaults from Registry. + /// + typedef Registry GCMetadataPrinterRegistry; + + /// FIXME: Collector instances are not useful on their own. These no longer + /// serve any purpose except to link in the plugins. + + /// Creates an ocaml-compatible garbage collector. + Collector *createOcamlCollector(); + + /// Creates an ocaml-compatible metadata printer. + GCMetadataPrinter *createOcamlMetadataPrinter(); + + /// Creates a shadow stack garbage collector. This collector requires no code + /// generator support. + Collector *createShadowStackCollector(); +} + +#endif diff --git a/include/llvm/CodeGen/LinkAllCodegenComponents.h b/include/llvm/CodeGen/LinkAllCodegenComponents.h index 39df7337ba..8f2e5c1ae8 100644 --- a/include/llvm/CodeGen/LinkAllCodegenComponents.h +++ b/include/llvm/CodeGen/LinkAllCodegenComponents.h @@ -17,7 +17,7 @@ #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/ScheduleDAG.h" -#include "llvm/CodeGen/Collectors.h" +#include "llvm/CodeGen/GCs.h" namespace { struct ForceCodegenLinking { @@ -37,6 +37,7 @@ namespace { (void) llvm::createSimpleRegisterCoalescer(); (void) llvm::createOcamlCollector(); + (void) llvm::createOcamlMetadataPrinter(); (void) llvm::createShadowStackCollector(); (void) llvm::createBURRListDAGScheduler(NULL, NULL, NULL, false); diff --git a/lib/CodeGen/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter.cpp index 3b2dcb31d2..de31840d70 100644 --- a/lib/CodeGen/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter.cpp @@ -16,9 +16,9 @@ #include "llvm/DerivedTypes.h" #include "llvm/Constants.h" #include "llvm/Module.h" -#include "llvm/CodeGen/Collector.h" -#include "llvm/CodeGen/CollectorMetadata.h" -#include "llvm/CodeGen/Collectors.h" +#include "llvm/CodeGen/GCStrategy.h" +#include "llvm/CodeGen/GCMetadata.h" +#include "llvm/CodeGen/GCs.h" #include "llvm/CodeGen/MachineConstantPool.h" #include "llvm/CodeGen/MachineJumpTableInfo.h" #include "llvm/CodeGen/MachineModuleInfo.h" diff --git a/lib/CodeGen/Collector.cpp b/lib/CodeGen/Collector.cpp deleted file mode 100644 index 6fd018961a..0000000000 --- a/lib/CodeGen/Collector.cpp +++ /dev/null @@ -1,407 +0,0 @@ -//===-- Collector.cpp - Garbage collection infrastructure -----------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements target- and collector-independent garbage collection -// infrastructure. -// -//===----------------------------------------------------------------------===// - -#include "llvm/CodeGen/Collector.h" -#include "llvm/CodeGen/Passes.h" -#include "llvm/IntrinsicInst.h" -#include "llvm/Module.h" -#include "llvm/PassManager.h" -#include "llvm/ADT/SmallPtrSet.h" -#include "llvm/CodeGen/MachineFrameInfo.h" -#include "llvm/CodeGen/MachineFunctionPass.h" -#include "llvm/CodeGen/MachineInstrBuilder.h" -#include "llvm/CodeGen/MachineModuleInfo.h" -#include "llvm/Target/TargetFrameInfo.h" -#include "llvm/Target/TargetInstrInfo.h" -#include "llvm/Target/TargetMachine.h" -#include "llvm/Support/Compiler.h" - -using namespace llvm; - -namespace { - - /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or - /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as - /// directed by the Collector. It also performs automatic root initialization - /// and custom intrinsic lowering. - class VISIBILITY_HIDDEN LowerIntrinsics : public FunctionPass { - static bool NeedsDefaultLoweringPass(const Collector &C); - static bool NeedsCustomLoweringPass(const Collector &C); - static bool CouldBecomeSafePoint(Instruction *I); - bool PerformDefaultLowering(Function &F, Collector &Coll); - static bool InsertRootInitializers(Function &F, - AllocaInst **Roots, unsigned Count); - - public: - static char ID; - - LowerIntrinsics(); - const char *getPassName() const; - void getAnalysisUsage(AnalysisUsage &AU) const; - - bool doInitialization(Module &M); - bool runOnFunction(Function &F); - }; - - - /// MachineCodeAnalysis - This is a target-independent pass over the machine - /// function representation to identify safe points for the garbage collector - /// in the machine code. It inserts labels at safe points and populates a - /// CollectorMetadata record for each function. - class VISIBILITY_HIDDEN MachineCodeAnalysis : public MachineFunctionPass { - const TargetMachine *TM; - CollectorMetadata *MD; - MachineModuleInfo *MMI; - const TargetInstrInfo *TII; - MachineFrameInfo *MFI; - - void FindSafePoints(MachineFunction &MF); - void VisitCallPoint(MachineBasicBlock::iterator MI); - unsigned InsertLabel(MachineBasicBlock &MBB, - MachineBasicBlock::iterator MI) const; - - void FindStackOffsets(MachineFunction &MF); - - public: - static char ID; - - MachineCodeAnalysis(); - const char *getPassName() const; - void getAnalysisUsage(AnalysisUsage &AU) const; - - bool runOnMachineFunction(MachineFunction &MF); - }; - -} - -// ----------------------------------------------------------------------------- - -Collector::Collector() : - NeededSafePoints(0), - CustomReadBarriers(false), - CustomWriteBarriers(false), - CustomRoots(false), - InitRoots(true), - UsesMetadata(false) -{} - -Collector::~Collector() { - for (iterator I = begin(), E = end(); I != E; ++I) - delete *I; - - Functions.clear(); -} - -bool Collector::initializeCustomLowering(Module &M) { return false; } - -bool Collector::performCustomLowering(Function &F) { - cerr << "gc " << getName() << " must override performCustomLowering.\n"; - abort(); - return 0; -} - -CollectorMetadata *Collector::insertFunctionMetadata(const Function &F) { - CollectorMetadata *CM = new CollectorMetadata(F, *this); - Functions.push_back(CM); - return CM; -} - -// ----------------------------------------------------------------------------- - -GCMetadataPrinter::GCMetadataPrinter() { } - -GCMetadataPrinter::~GCMetadataPrinter() { } - -void GCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI) { - // Default is no action. -} - -void GCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI) { - // Default is no action. -} - -// ----------------------------------------------------------------------------- - -FunctionPass *llvm::createGCLoweringPass() { - return new LowerIntrinsics(); -} - -char LowerIntrinsics::ID = 0; - -LowerIntrinsics::LowerIntrinsics() - : FunctionPass((intptr_t)&ID) {} - -const char *LowerIntrinsics::getPassName() const { - return "Lower Garbage Collection Instructions"; -} - -void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const { - FunctionPass::getAnalysisUsage(AU); - AU.addRequired(); -} - -/// doInitialization - If this module uses the GC intrinsics, find them now. -bool LowerIntrinsics::doInitialization(Module &M) { - // FIXME: This is rather antisocial in the context of a JIT since it performs - // work against the entire module. But this cannot be done at - // runFunction time (initializeCustomLowering likely needs to change - // the module). - CollectorModuleMetadata *CMM = getAnalysisToUpdate(); - assert(CMM && "LowerIntrinsics didn't require CollectorModuleMetadata!?"); - for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) - if (I->hasCollector()) - CMM->get(*I); // Instantiate the Collector. - - bool MadeChange = false; - for (CollectorModuleMetadata::iterator I = CMM->begin(), - E = CMM->end(); I != E; ++I) - if (NeedsCustomLoweringPass(**I)) - if ((*I)->initializeCustomLowering(M)) - MadeChange = true; - - return MadeChange; -} - -bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, - unsigned Count) { - // Scroll past alloca instructions. - BasicBlock::iterator IP = F.getEntryBlock().begin(); - while (isa(IP)) ++IP; - - // Search for initializers in the initial BB. - SmallPtrSet InitedRoots; - for (; !CouldBecomeSafePoint(IP); ++IP) - if (StoreInst *SI = dyn_cast(IP)) - if (AllocaInst *AI = - dyn_cast(SI->getOperand(1)->stripPointerCasts())) - InitedRoots.insert(AI); - - // Add root initializers. - bool MadeChange = false; - - for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I) - if (!InitedRoots.count(*I)) { - new StoreInst(ConstantPointerNull::get(cast( - cast((*I)->getType())->getElementType())), - *I, IP); - MadeChange = true; - } - - return MadeChange; -} - -bool LowerIntrinsics::NeedsDefaultLoweringPass(const Collector &C) { - // Default lowering is necessary only if read or write barriers have a default - // action. The default for roots is no action. - return !C.customWriteBarrier() - || !C.customReadBarrier() - || C.initializeRoots(); -} - -bool LowerIntrinsics::NeedsCustomLoweringPass(const Collector &C) { - // Custom lowering is only necessary if enabled for some action. - return C.customWriteBarrier() - || C.customReadBarrier() - || C.customRoots(); -} - -/// CouldBecomeSafePoint - Predicate to conservatively determine whether the -/// instruction could introduce a safe point. -bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) { - // The natural definition of instructions which could introduce safe points - // are: - // - // - call, invoke (AfterCall, BeforeCall) - // - phis (Loops) - // - invoke, ret, unwind (Exit) - // - // However, instructions as seemingly inoccuous as arithmetic can become - // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead - // it is necessary to take a conservative approach. - - if (isa(I) || isa(I) || - isa(I) || isa(I)) - return false; - - // llvm.gcroot is safe because it doesn't do anything at runtime. - if (CallInst *CI = dyn_cast(I)) - if (Function *F = CI->getCalledFunction()) - if (unsigned IID = F->getIntrinsicID()) - if (IID == Intrinsic::gcroot) - return false; - - return true; -} - -/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores. -/// Leave gcroot intrinsics; the code generator needs to see those. -bool LowerIntrinsics::runOnFunction(Function &F) { - // Quick exit for functions that do not use GC. - if (!F.hasCollector()) return false; - - CollectorMetadata &MD = getAnalysis().get(F); - Collector &Coll = MD.getCollector(); - - bool MadeChange = false; - - if (NeedsDefaultLoweringPass(Coll)) - MadeChange |= PerformDefaultLowering(F, Coll); - - if (NeedsCustomLoweringPass(Coll)) - MadeChange |= Coll.performCustomLowering(F); - - return MadeChange; -} - -bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) { - bool LowerWr = !Coll.customWriteBarrier(); - bool LowerRd = !Coll.customReadBarrier(); - bool InitRoots = Coll.initializeRoots(); - - SmallVector Roots; - - 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 (IntrinsicInst *CI = dyn_cast(II++)) { - Function *F = CI->getCalledFunction(); - switch (F->getIntrinsicID()) { - case Intrinsic::gcwrite: - if (LowerWr) { - // Replace a write barrier with a simple store. - Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI); - CI->replaceAllUsesWith(St); - CI->eraseFromParent(); - } - break; - case Intrinsic::gcread: - if (LowerRd) { - // Replace a read barrier with a simple load. - Value *Ld = new LoadInst(CI->getOperand(2), "", CI); - Ld->takeName(CI); - CI->replaceAllUsesWith(Ld); - CI->eraseFromParent(); - } - break; - case Intrinsic::gcroot: - if (InitRoots) { - // Initialize the GC root, but do not delete the intrinsic. The - // backend needs the intrinsic to flag the stack slot. - Roots.push_back(cast( - CI->getOperand(1)->stripPointerCasts())); - } - break; - default: - continue; - } - - MadeChange = true; - } - } - } - - if (Roots.size()) - MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size()); - - return MadeChange; -} - -// ----------------------------------------------------------------------------- - -FunctionPass *llvm::createGCMachineCodeAnalysisPass() { - return new MachineCodeAnalysis(); -} - -char MachineCodeAnalysis::ID = 0; - -MachineCodeAnalysis::MachineCodeAnalysis() - : MachineFunctionPass(intptr_t(&ID)) {} - -const char *MachineCodeAnalysis::getPassName() const { - return "Analyze Machine Code For Garbage Collection"; -} - -void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { - MachineFunctionPass::getAnalysisUsage(AU); - AU.setPreservesAll(); - AU.addRequired(); - AU.addRequired(); -} - -unsigned MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB, - MachineBasicBlock::iterator MI) const { - unsigned Label = MMI->NextLabelID(); - BuildMI(MBB, MI, TII->get(TargetInstrInfo::GC_LABEL)).addImm(Label); - return Label; -} - -void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) { - // Find the return address (next instruction), too, so as to bracket the call - // instruction. - MachineBasicBlock::iterator RAI = CI; - ++RAI; - - if (MD->getCollector().needsSafePoint(GC::PreCall)) - MD->addSafePoint(GC::PreCall, InsertLabel(*CI->getParent(), CI)); - - if (MD->getCollector().needsSafePoint(GC::PostCall)) - MD->addSafePoint(GC::PostCall, InsertLabel(*CI->getParent(), RAI)); -} - -void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) { - for (MachineFunction::iterator BBI = MF.begin(), - BBE = MF.end(); BBI != BBE; ++BBI) - for (MachineBasicBlock::iterator MI = BBI->begin(), - ME = BBI->end(); MI != ME; ++MI) - if (MI->getDesc().isCall()) - VisitCallPoint(MI); -} - -void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) { - uint64_t StackSize = MFI->getStackSize(); - uint64_t OffsetAdjustment = MFI->getOffsetAdjustment(); - uint64_t OffsetOfLocalArea = TM->getFrameInfo()->getOffsetOfLocalArea(); - - for (CollectorMetadata::roots_iterator RI = MD->roots_begin(), - RE = MD->roots_end(); RI != RE; ++RI) - RI->StackOffset = MFI->getObjectOffset(RI->Num) + StackSize - - OffsetOfLocalArea + OffsetAdjustment; -} - -bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) { - // Quick exit for functions that do not use GC. - if (!MF.getFunction()->hasCollector()) return false; - - MD = &getAnalysis().get(*MF.getFunction()); - if (!MD->getCollector().needsSafePoints()) - return false; - - TM = &MF.getTarget(); - MMI = &getAnalysis(); - TII = TM->getInstrInfo(); - MFI = MF.getFrameInfo(); - - // Find the size of the stack frame. - MD->setFrameSize(MFI->getStackSize()); - - // Find all safe points. - FindSafePoints(MF); - - // Find the stack offsets for all roots. - FindStackOffsets(MF); - - return false; -} diff --git a/lib/CodeGen/CollectorMetadata.cpp b/lib/CodeGen/CollectorMetadata.cpp deleted file mode 100644 index 5b8c7c7999..0000000000 --- a/lib/CodeGen/CollectorMetadata.cpp +++ /dev/null @@ -1,212 +0,0 @@ -//===-- CollectorMetadata.cpp - Garbage collector metadata ----------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the CollectorMetadata and CollectorModuleMetadata -// classes. -// -//===----------------------------------------------------------------------===// - -#include "llvm/CodeGen/CollectorMetadata.h" -#include "llvm/CodeGen/Collector.h" -#include "llvm/CodeGen/Collectors.h" -#include "llvm/CodeGen/MachineFrameInfo.h" -#include "llvm/Pass.h" -#include "llvm/CodeGen/Passes.h" -#include "llvm/Function.h" -#include "llvm/Support/Compiler.h" - -using namespace llvm; - -namespace { - - class VISIBILITY_HIDDEN Printer : public FunctionPass { - static char ID; - std::ostream &OS; - - public: - explicit Printer(std::ostream &OS = *cerr); - - const char *getPassName() const; - void getAnalysisUsage(AnalysisUsage &AU) const; - - bool runOnFunction(Function &F); - }; - - class VISIBILITY_HIDDEN Deleter : public FunctionPass { - static char ID; - - public: - Deleter(); - - const char *getPassName() const; - void getAnalysisUsage(AnalysisUsage &AU) const; - - bool runOnFunction(Function &F); - bool doFinalization(Module &M); - }; - -} - -static RegisterPass -X("collector-metadata", "Create Garbage Collector Module Metadata"); - -// ----------------------------------------------------------------------------- - -CollectorMetadata::CollectorMetadata(const Function &F, Collector &C) - : F(F), C(C), FrameSize(~0LL) {} - -CollectorMetadata::~CollectorMetadata() {} - -// ----------------------------------------------------------------------------- - -char CollectorModuleMetadata::ID = 0; - -CollectorModuleMetadata::CollectorModuleMetadata() - : ImmutablePass((intptr_t)&ID) {} - -CollectorModuleMetadata::~CollectorModuleMetadata() { - clear(); -} - -Collector *CollectorModuleMetadata:: -getOrCreateCollector(const Module *M, const std::string &Name) { - const char *Start = Name.c_str(); - - collector_map_type::iterator NMI = NameMap.find(Start, Start + Name.size()); - if (NMI != NameMap.end()) - return NMI->getValue(); - - for (CollectorRegistry::iterator I = CollectorRegistry::begin(), - E = CollectorRegistry::end(); I != E; ++I) { - if (strcmp(Start, I->getName()) == 0) { - Collector *C = I->instantiate(); - C->M = M; - C->Name = Name; - NameMap.GetOrCreateValue(Start, Start + Name.size()).setValue(C); - Collectors.push_back(C); - return C; - } - } - - cerr << "unsupported collector: " << Name << "\n"; - abort(); -} - -CollectorMetadata &CollectorModuleMetadata::get(const Function &F) { - assert(F.hasCollector()); - function_map_type::iterator I = Map.find(&F); - if (I != Map.end()) - return *I->second; - - Collector *C = getOrCreateCollector(F.getParent(), F.getCollector()); - CollectorMetadata *MD = C->insertFunctionMetadata(F); - Map[&F] = MD; - return *MD; -} - -void CollectorModuleMetadata::clear() { - Map.clear(); - NameMap.clear(); - - for (iterator I = begin(), E = end(); I != E; ++I) - delete *I; - Collectors.clear(); -} - -// ----------------------------------------------------------------------------- - -char Printer::ID = 0; - -FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) { - return new Printer(OS); -} - -Printer::Printer(std::ostream &OS) - : FunctionPass(intptr_t(&ID)), OS(OS) {} - -const char *Printer::getPassName() const { - return "Print Garbage Collector Information"; -} - -void Printer::getAnalysisUsage(AnalysisUsage &AU) const { - FunctionPass::getAnalysisUsage(AU); - AU.setPreservesAll(); - AU.addRequired(); -} - -static const char *DescKind(GC::PointKind Kind) { - switch (Kind) { - default: assert(0 && "Unknown GC point kind"); - case GC::Loop: return "loop"; - case GC::Return: return "return"; - case GC::PreCall: return "pre-call"; - case GC::PostCall: return "post-call"; - } -} - -bool Printer::runOnFunction(Function &F) { - if (F.hasCollector()) { - CollectorMetadata *FD = &getAnalysis().get(F); - - OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n"; - for (CollectorMetadata::roots_iterator RI = FD->roots_begin(), - RE = FD->roots_end(); - RI != RE; ++RI) - OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; - - OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n"; - for (CollectorMetadata::iterator PI = FD->begin(), - PE = FD->end(); PI != PE; ++PI) { - - OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {"; - - for (CollectorMetadata::live_iterator RI = FD->live_begin(PI), - RE = FD->live_end(PI);;) { - OS << " " << RI->Num; - if (++RI == RE) - break; - OS << ","; - } - - OS << " }\n"; - } - } - - return false; -} - -// ----------------------------------------------------------------------------- - -char Deleter::ID = 0; - -FunctionPass *llvm::createCollectorMetadataDeleter() { - return new Deleter(); -} - -Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {} - -const char *Deleter::getPassName() const { - return "Delete Garbage Collector Information"; -} - -void Deleter::getAnalysisUsage(AnalysisUsage &AU) const { - AU.setPreservesAll(); - AU.addRequired(); -} - -bool Deleter::runOnFunction(Function &MF) { - return false; -} - -bool Deleter::doFinalization(Module &M) { - CollectorModuleMetadata *CMM = getAnalysisToUpdate(); - assert(CMM && "Deleter didn't require CollectorModuleMetadata?!"); - CMM->clear(); - return false; -} diff --git a/lib/CodeGen/Collectors.cpp b/lib/CodeGen/Collectors.cpp deleted file mode 100644 index 3b0c554daf..0000000000 --- a/lib/CodeGen/Collectors.cpp +++ /dev/null @@ -1,28 +0,0 @@ -//===-- Collectors.cpp - Garbage collector registry -----------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the static data members of the CollectorRegistry class. -// -//===----------------------------------------------------------------------===// - -#include "llvm/CodeGen/Collectors.h" - -using namespace llvm; - -template<> CollectorRegistry::node *CollectorRegistry::Head = 0; -template<> CollectorRegistry::node *CollectorRegistry::Tail = 0; -template<> CollectorRegistry::listener *CollectorRegistry::ListenerHead = 0; -template<> CollectorRegistry::listener *CollectorRegistry::ListenerTail = 0; - -template<> GCMetadataPrinterRegistry::node *GCMetadataPrinterRegistry::Head = 0; -template<> GCMetadataPrinterRegistry::node *GCMetadataPrinterRegistry::Tail = 0; -template<> GCMetadataPrinterRegistry::listener * -GCMetadataPrinterRegistry::ListenerHead = 0; -template<> GCMetadataPrinterRegistry::listener * -GCMetadataPrinterRegistry::ListenerTail = 0; diff --git a/lib/CodeGen/GCMetadata.cpp b/lib/CodeGen/GCMetadata.cpp new file mode 100644 index 0000000000..c02a73b708 --- /dev/null +++ b/lib/CodeGen/GCMetadata.cpp @@ -0,0 +1,212 @@ +//===-- CollectorMetadata.cpp - Garbage collector metadata ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the CollectorMetadata and CollectorModuleMetadata +// classes. +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/GCMetadata.h" +#include "llvm/CodeGen/GCStrategy.h" +#include "llvm/CodeGen/GCs.h" +#include "llvm/CodeGen/MachineFrameInfo.h" +#include "llvm/Pass.h" +#include "llvm/CodeGen/Passes.h" +#include "llvm/Function.h" +#include "llvm/Support/Compiler.h" + +using namespace llvm; + +namespace { + + class VISIBILITY_HIDDEN Printer : public FunctionPass { + static char ID; + std::ostream &OS; + + public: + explicit Printer(std::ostream &OS = *cerr); + + const char *getPassName() const; + void getAnalysisUsage(AnalysisUsage &AU) const; + + bool runOnFunction(Function &F); + }; + + class VISIBILITY_HIDDEN Deleter : public FunctionPass { + static char ID; + + public: + Deleter(); + + const char *getPassName() const; + void getAnalysisUsage(AnalysisUsage &AU) const; + + bool runOnFunction(Function &F); + bool doFinalization(Module &M); + }; + +} + +static RegisterPass +X("collector-metadata", "Create Garbage Collector Module Metadata"); + +// ----------------------------------------------------------------------------- + +CollectorMetadata::CollectorMetadata(const Function &F, Collector &C) + : F(F), C(C), FrameSize(~0LL) {} + +CollectorMetadata::~CollectorMetadata() {} + +// ----------------------------------------------------------------------------- + +char CollectorModuleMetadata::ID = 0; + +CollectorModuleMetadata::CollectorModuleMetadata() + : ImmutablePass((intptr_t)&ID) {} + +CollectorModuleMetadata::~CollectorModuleMetadata() { + clear(); +} + +Collector *CollectorModuleMetadata:: +getOrCreateCollector(const Module *M, const std::string &Name) { + const char *Start = Name.c_str(); + + collector_map_type::iterator NMI = NameMap.find(Start, Start + Name.size()); + if (NMI != NameMap.end()) + return NMI->getValue(); + + for (CollectorRegistry::iterator I = CollectorRegistry::begin(), + E = CollectorRegistry::end(); I != E; ++I) { + if (strcmp(Start, I->getName()) == 0) { + Collector *C = I->instantiate(); + C->M = M; + C->Name = Name; + NameMap.GetOrCreateValue(Start, Start + Name.size()).setValue(C); + Collectors.push_back(C); + return C; + } + } + + cerr << "unsupported collector: " << Name << "\n"; + abort(); +} + +CollectorMetadata &CollectorModuleMetadata::get(const Function &F) { + assert(F.hasCollector()); + function_map_type::iterator I = Map.find(&F); + if (I != Map.end()) + return *I->second; + + Collector *C = getOrCreateCollector(F.getParent(), F.getCollector()); + CollectorMetadata *MD = C->insertFunctionMetadata(F); + Map[&F] = MD; + return *MD; +} + +void CollectorModuleMetadata::clear() { + Map.clear(); + NameMap.clear(); + + for (iterator I = begin(), E = end(); I != E; ++I) + delete *I; + Collectors.clear(); +} + +// ----------------------------------------------------------------------------- + +char Printer::ID = 0; + +FunctionPass *llvm::createCollectorMetadataPrinter(std::ostream &OS) { + return new Printer(OS); +} + +Printer::Printer(std::ostream &OS) + : FunctionPass(intptr_t(&ID)), OS(OS) {} + +const char *Printer::getPassName() const { + return "Print Garbage Collector Information"; +} + +void Printer::getAnalysisUsage(AnalysisUsage &AU) const { + FunctionPass::getAnalysisUsage(AU); + AU.setPreservesAll(); + AU.addRequired(); +} + +static const char *DescKind(GC::PointKind Kind) { + switch (Kind) { + default: assert(0 && "Unknown GC point kind"); + case GC::Loop: return "loop"; + case GC::Return: return "return"; + case GC::PreCall: return "pre-call"; + case GC::PostCall: return "post-call"; + } +} + +bool Printer::runOnFunction(Function &F) { + if (F.hasCollector()) { + CollectorMetadata *FD = &getAnalysis().get(F); + + OS << "GC roots for " << FD->getFunction().getNameStart() << ":\n"; + for (CollectorMetadata::roots_iterator RI = FD->roots_begin(), + RE = FD->roots_end(); + RI != RE; ++RI) + OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; + + OS << "GC safe points for " << FD->getFunction().getNameStart() << ":\n"; + for (CollectorMetadata::iterator PI = FD->begin(), + PE = FD->end(); PI != PE; ++PI) { + + OS << "\tlabel " << PI->Num << ": " << DescKind(PI->Kind) << ", live = {"; + + for (CollectorMetadata::live_iterator RI = FD->live_begin(PI), + RE = FD->live_end(PI);;) { + OS << " " << RI->Num; + if (++RI == RE) + break; + OS << ","; + } + + OS << " }\n"; + } + } + + return false; +} + +// ----------------------------------------------------------------------------- + +char Deleter::ID = 0; + +FunctionPass *llvm::createCollectorMetadataDeleter() { + return new Deleter(); +} + +Deleter::Deleter() : FunctionPass(intptr_t(&ID)) {} + +const char *Deleter::getPassName() const { + return "Delete Garbage Collector Information"; +} + +void Deleter::getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired(); +} + +bool Deleter::runOnFunction(Function &MF) { + return false; +} + +bool Deleter::doFinalization(Module &M) { + CollectorModuleMetadata *CMM = getAnalysisToUpdate(); + assert(CMM && "Deleter didn't require CollectorModuleMetadata?!"); + CMM->clear(); + return false; +} diff --git a/lib/CodeGen/GCMetadataPrinter.cpp b/lib/CodeGen/GCMetadataPrinter.cpp new file mode 100644 index 0000000000..b16d873520 --- /dev/null +++ b/lib/CodeGen/GCMetadataPrinter.cpp @@ -0,0 +1,31 @@ +//===-- Collector.cpp - Garbage collection infrastructure -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements target- and collector-independent garbage collection +// infrastructure. +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/GCStrategy.h" + +using namespace llvm; + +GCMetadataPrinter::GCMetadataPrinter() { } + +GCMetadataPrinter::~GCMetadataPrinter() { } + +void GCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI) { + // Default is no action. +} + +void GCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI) { + // Default is no action. +} diff --git a/lib/CodeGen/GCStrategy.cpp b/lib/CodeGen/GCStrategy.cpp new file mode 100644 index 0000000000..629a2e13de --- /dev/null +++ b/lib/CodeGen/GCStrategy.cpp @@ -0,0 +1,389 @@ +//===-- Collector.cpp - Garbage collection infrastructure -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements target- and collector-independent garbage collection +// infrastructure. +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/GCStrategy.h" +#include "llvm/CodeGen/Passes.h" +#include "llvm/IntrinsicInst.h" +#include "llvm/Module.h" +#include "llvm/CodeGen/MachineFrameInfo.h" +#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/MachineInstrBuilder.h" +#include "llvm/CodeGen/MachineModuleInfo.h" +#include "llvm/Target/TargetFrameInfo.h" +#include "llvm/Target/TargetInstrInfo.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Support/Compiler.h" + +using namespace llvm; + +namespace { + + /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or + /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as + /// directed by the Collector. It also performs automatic root initialization + /// and custom intrinsic lowering. + class VISIBILITY_HIDDEN LowerIntrinsics : public FunctionPass { + static bool NeedsDefaultLoweringPass(const Collector &C); + static bool NeedsCustomLoweringPass(const Collector &C); + static bool CouldBecomeSafePoint(Instruction *I); + bool PerformDefaultLowering(Function &F, Collector &Coll); + static bool InsertRootInitializers(Function &F, + AllocaInst **Roots, unsigned Count); + + public: + static char ID; + + LowerIntrinsics(); + const char *getPassName() const; + void getAnalysisUsage(AnalysisUsage &AU) const; + + bool doInitialization(Module &M); + bool runOnFunction(Function &F); + }; + + + /// MachineCodeAnalysis - This is a target-independent pass over the machine + /// function representation to identify safe points for the garbage collector + /// in the machine code. It inserts labels at safe points and populates a + /// CollectorMetadata record for each function. + class VISIBILITY_HIDDEN MachineCodeAnalysis : public MachineFunctionPass { + const TargetMachine *TM; + CollectorMetadata *MD; + MachineModuleInfo *MMI; + const TargetInstrInfo *TII; + MachineFrameInfo *MFI; + + void FindSafePoints(MachineFunction &MF); + void VisitCallPoint(MachineBasicBlock::iterator MI); + unsigned InsertLabel(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MI) const; + + void FindStackOffsets(MachineFunction &MF); + + public: + static char ID; + + MachineCodeAnalysis(); + const char *getPassName() const; + void getAnalysisUsage(AnalysisUsage &AU) const; + + bool runOnMachineFunction(MachineFunction &MF); + }; + +} + +// ----------------------------------------------------------------------------- + +Collector::Collector() : + NeededSafePoints(0), + CustomReadBarriers(false), + CustomWriteBarriers(false), + CustomRoots(false), + InitRoots(true), + UsesMetadata(false) +{} + +Collector::~Collector() { + for (iterator I = begin(), E = end(); I != E; ++I) + delete *I; + + Functions.clear(); +} + +bool Collector::initializeCustomLowering(Module &M) { return false; } + +bool Collector::performCustomLowering(Function &F) { + cerr << "gc " << getName() << " must override performCustomLowering.\n"; + abort(); + return 0; +} + +CollectorMetadata *Collector::insertFunctionMetadata(const Function &F) { + CollectorMetadata *CM = new CollectorMetadata(F, *this); + Functions.push_back(CM); + return CM; +} + +// ----------------------------------------------------------------------------- + +FunctionPass *llvm::createGCLoweringPass() { + return new LowerIntrinsics(); +} + +char LowerIntrinsics::ID = 0; + +LowerIntrinsics::LowerIntrinsics() + : FunctionPass((intptr_t)&ID) {} + +const char *LowerIntrinsics::getPassName() const { + return "Lower Garbage Collection Instructions"; +} + +void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const { + FunctionPass::getAnalysisUsage(AU); + AU.addRequired(); +} + +/// doInitialization - If this module uses the GC intrinsics, find them now. +bool LowerIntrinsics::doInitialization(Module &M) { + // FIXME: This is rather antisocial in the context of a JIT since it performs + // work against the entire module. But this cannot be done at + // runFunction time (initializeCustomLowering likely needs to change + // the module). + CollectorModuleMetadata *CMM = getAnalysisToUpdate(); + assert(CMM && "LowerIntrinsics didn't require CollectorModuleMetadata!?"); + for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) + if (I->hasCollector()) + CMM->get(*I); // Instantiate the Collector. + + bool MadeChange = false; + for (CollectorModuleMetadata::iterator I = CMM->begin(), + E = CMM->end(); I != E; ++I) + if (NeedsCustomLoweringPass(**I)) + if ((*I)->initializeCustomLowering(M)) + MadeChange = true; + + return MadeChange; +} + +bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, + unsigned Count) { + // Scroll past alloca instructions. + BasicBlock::iterator IP = F.getEntryBlock().begin(); + while (isa(IP)) ++IP; + + // Search for initializers in the initial BB. + SmallPtrSet InitedRoots; + for (; !CouldBecomeSafePoint(IP); ++IP) + if (StoreInst *SI = dyn_cast(IP)) + if (AllocaInst *AI = + dyn_cast(SI->getOperand(1)->stripPointerCasts())) + InitedRoots.insert(AI); + + // Add root initializers. + bool MadeChange = false; + + for (AllocaInst **I = Roots, **E = Roots + Count; I != E; ++I) + if (!InitedRoots.count(*I)) { + new StoreInst(ConstantPointerNull::get(cast( + cast((*I)->getType())->getElementType())), + *I, IP); + MadeChange = true; + } + + return MadeChange; +} + +bool LowerIntrinsics::NeedsDefaultLoweringPass(const Collector &C) { + // Default lowering is necessary only if read or write barriers have a default + // action. The default for roots is no action. + return !C.customWriteBarrier() + || !C.customReadBarrier() + || C.initializeRoots(); +} + +bool LowerIntrinsics::NeedsCustomLoweringPass(const Collector &C) { + // Custom lowering is only necessary if enabled for some action. + return C.customWriteBarrier() + || C.customReadBarrier() + || C.customRoots(); +} + +/// CouldBecomeSafePoint - Predicate to conservatively determine whether the +/// instruction could introduce a safe point. +bool LowerIntrinsics::CouldBecomeSafePoint(Instruction *I) { + // The natural definition of instructions which could introduce safe points + // are: + // + // - call, invoke (AfterCall, BeforeCall) + // - phis (Loops) + // - invoke, ret, unwind (Exit) + // + // However, instructions as seemingly inoccuous as arithmetic can become + // libcalls upon lowering (e.g., div i64 on a 32-bit platform), so instead + // it is necessary to take a conservative approach. + + if (isa(I) || isa(I) || + isa(I) || isa(I)) + return false; + + // llvm.gcroot is safe because it doesn't do anything at runtime. + if (CallInst *CI = dyn_cast(I)) + if (Function *F = CI->getCalledFunction()) + if (unsigned IID = F->getIntrinsicID()) + if (IID == Intrinsic::gcroot) + return false; + + return true; +} + +/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores. +/// Leave gcroot intrinsics; the code generator needs to see those. +bool LowerIntrinsics::runOnFunction(Function &F) { + // Quick exit for functions that do not use GC. + if (!F.hasCollector()) return false; + + CollectorMetadata &MD = getAnalysis().get(F); + Collector &Coll = MD.getCollector(); + + bool MadeChange = false; + + if (NeedsDefaultLoweringPass(Coll)) + MadeChange |= PerformDefaultLowering(F, Coll); + + if (NeedsCustomLoweringPass(Coll)) + MadeChange |= Coll.performCustomLowering(F); + + return MadeChange; +} + +bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) { + bool LowerWr = !Coll.customWriteBarrier(); + bool LowerRd = !Coll.customReadBarrier(); + bool InitRoots = Coll.initializeRoots(); + + SmallVector Roots; + + 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 (IntrinsicInst *CI = dyn_cast(II++)) { + Function *F = CI->getCalledFunction(); + switch (F->getIntrinsicID()) { + case Intrinsic::gcwrite: + if (LowerWr) { + // Replace a write barrier with a simple store. + Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI); + CI->replaceAllUsesWith(St); + CI->eraseFromParent(); + } + break; + case Intrinsic::gcread: + if (LowerRd) { + // Replace a read barrier with a simple load. + Value *Ld = new LoadInst(CI->getOperand(2), "", CI); + Ld->takeName(CI); + CI->replaceAllUsesWith(Ld); + CI->eraseFromParent(); + } + break; + case Intrinsic::gcroot: + if (InitRoots) { + // Initialize the GC root, but do not delete the intrinsic. The + // backend needs the intrinsic to flag the stack slot. + Roots.push_back(cast( + CI->getOperand(1)->stripPointerCasts())); + } + break; + default: + continue; + } + + MadeChange = true; + } + } + } + + if (Roots.size()) + MadeChange |= InsertRootInitializers(F, Roots.begin(), Roots.size()); + + return MadeChange; +} + +// ----------------------------------------------------------------------------- + +FunctionPass *llvm::createGCMachineCodeAnalysisPass() { + return new MachineCodeAnalysis(); +} + +char MachineCodeAnalysis::ID = 0; + +MachineCodeAnalysis::MachineCodeAnalysis() + : MachineFunctionPass(intptr_t(&ID)) {} + +const char *MachineCodeAnalysis::getPassName() const { + return "Analyze Machine Code For Garbage Collection"; +} + +void MachineCodeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { + MachineFunctionPass::getAnalysisUsage(AU); + AU.setPreservesAll(); + AU.addRequired(); + AU.addRequired(); +} + +unsigned MachineCodeAnalysis::InsertLabel(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MI) const { + unsigned Label = MMI->NextLabelID(); + BuildMI(MBB, MI, TII->get(TargetInstrInfo::GC_LABEL)).addImm(Label); + return Label; +} + +void MachineCodeAnalysis::VisitCallPoint(MachineBasicBlock::iterator CI) { + // Find the return address (next instruction), too, so as to bracket the call + // instruction. + MachineBasicBlock::iterator RAI = CI; + ++RAI; + + if (MD->getCollector().needsSafePoint(GC::PreCall)) + MD->addSafePoint(GC::PreCall, InsertLabel(*CI->getParent(), CI)); + + if (MD->getCollector().needsSafePoint(GC::PostCall)) + MD->addSafePoint(GC::PostCall, InsertLabel(*CI->getParent(), RAI)); +} + +void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) { + for (MachineFunction::iterator BBI = MF.begin(), + BBE = MF.end(); BBI != BBE; ++BBI) + for (MachineBasicBlock::iterator MI = BBI->begin(), + ME = BBI->end(); MI != ME; ++MI) + if (MI->getDesc().isCall()) + VisitCallPoint(MI); +} + +void MachineCodeAnalysis::FindStackOffsets(MachineFunction &MF) { + uint64_t StackSize = MFI->getStackSize(); + uint64_t OffsetAdjustment = MFI->getOffsetAdjustment(); + uint64_t OffsetOfLocalArea = TM->getFrameInfo()->getOffsetOfLocalArea(); + + for (CollectorMetadata::roots_iterator RI = MD->roots_begin(), + RE = MD->roots_end(); RI != RE; ++RI) + RI->StackOffset = MFI->getObjectOffset(RI->Num) + StackSize + - OffsetOfLocalArea + OffsetAdjustment; +} + +bool MachineCodeAnalysis::runOnMachineFunction(MachineFunction &MF) { + // Quick exit for functions that do not use GC. + if (!MF.getFunction()->hasCollector()) return false; + + MD = &getAnalysis().get(*MF.getFunction()); + if (!MD->getCollector().needsSafePoints()) + return false; + + TM = &MF.getTarget(); + MMI = &getAnalysis(); + TII = TM->getInstrInfo(); + MFI = MF.getFrameInfo(); + + // Find the size of the stack frame. + MD->setFrameSize(MFI->getStackSize()); + + // Find all safe points. + FindSafePoints(MF); + + // Find the stack offsets for all roots. + FindStackOffsets(MF); + + return false; +} diff --git a/lib/CodeGen/GCs.cpp b/lib/CodeGen/GCs.cpp new file mode 100644 index 0000000000..124725358e --- /dev/null +++ b/lib/CodeGen/GCs.cpp @@ -0,0 +1,28 @@ +//===-- Collectors.cpp - Garbage collector registry -----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the static data members of the CollectorRegistry class. +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/GCs.h" + +using namespace llvm; + +template<> CollectorRegistry::node *CollectorRegistry::Head = 0; +template<> CollectorRegistry::node *CollectorRegistry::Tail = 0; +template<> CollectorRegistry::listener *CollectorRegistry::ListenerHead = 0; +template<> CollectorRegistry::listener *CollectorRegistry::ListenerTail = 0; + +template<> GCMetadataPrinterRegistry::node *GCMetadataPrinterRegistry::Head = 0; +template<> GCMetadataPrinterRegistry::node *GCMetadataPrinterRegistry::Tail = 0; +template<> GCMetadataPrinterRegistry::listener * +GCMetadataPrinterRegistry::ListenerHead = 0; +template<> GCMetadataPrinterRegistry::listener * +GCMetadataPrinterRegistry::ListenerTail = 0; diff --git a/lib/CodeGen/LLVMTargetMachine.cpp b/lib/CodeGen/LLVMTargetMachine.cpp index 97fe35c2f4..d420ffb7d9 100644 --- a/lib/CodeGen/LLVMTargetMachine.cpp +++ b/lib/CodeGen/LLVMTargetMachine.cpp @@ -17,7 +17,7 @@ #include "llvm/Assembly/PrintModulePass.h" #include "llvm/Analysis/LoopPass.h" #include "llvm/CodeGen/Passes.h" -#include "llvm/CodeGen/Collector.h" +#include "llvm/CodeGen/GCStrategy.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Target/TargetAsmInfo.h" #include "llvm/Transforms/Scalar.h" diff --git a/lib/CodeGen/OcamlCollector.cpp b/lib/CodeGen/OcamlCollector.cpp deleted file mode 100644 index 7f58f58c29..0000000000 --- a/lib/CodeGen/OcamlCollector.cpp +++ /dev/null @@ -1,178 +0,0 @@ -//===-- OcamlCollector.cpp - Ocaml frametable emitter ---------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements lowering for the llvm.gc* intrinsics compatible with -// Objective Caml 3.10.0, which uses a liveness-accurate static stack map. -// -//===----------------------------------------------------------------------===// - -#include "llvm/CodeGen/Collectors.h" -#include "llvm/CodeGen/AsmPrinter.h" -#include "llvm/CodeGen/Collector.h" -#include "llvm/Module.h" -#include "llvm/Target/TargetAsmInfo.h" -#include "llvm/Target/TargetData.h" -#include "llvm/Target/TargetMachine.h" - -using namespace llvm; - -namespace { - - class VISIBILITY_HIDDEN OcamlCollector : public Collector { - public: - OcamlCollector(); - }; - - class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter { - public: - void beginAssembly(std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI); - - void finishAssembly(std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI); - }; - -} - -static CollectorRegistry::Add -X("ocaml", "ocaml 3.10-compatible collector"); - -static GCMetadataPrinterRegistry::Add -Y("ocaml", "ocaml 3.10-compatible collector"); - -// ----------------------------------------------------------------------------- - -static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI, const char *Id) { - const std::string &MId = M.getModuleIdentifier(); - - std::string Mangled; - Mangled += TAI.getGlobalPrefix(); - Mangled += "caml"; - size_t Letter = Mangled.size(); - Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.')); - Mangled += "__"; - Mangled += Id; - - // Capitalize the first letter of the module name. - Mangled[Letter] = toupper(Mangled[Letter]); - - if (const char *GlobalDirective = TAI.getGlobalDirective()) - OS << GlobalDirective << Mangled << "\n"; - OS << Mangled << ":\n"; -} - -Collector *llvm::createOcamlCollector() { - return new OcamlCollector(); -} - -OcamlCollector::OcamlCollector() { - NeededSafePoints = 1 << GC::PostCall; - UsesMetadata = true; -} - -void OcamlGCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI) { - AP.SwitchToTextSection(TAI.getTextSection()); - EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin"); - - AP.SwitchToDataSection(TAI.getDataSection()); - EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin"); -} - -/// emitAssembly - Print the frametable. The ocaml frametable format is thus: -/// -/// extern "C" struct align(sizeof(intptr_t)) { -/// uint16_t NumDescriptors; -/// struct align(sizeof(intptr_t)) { -/// void *ReturnAddress; -/// uint16_t FrameSize; -/// uint16_t NumLiveOffsets; -/// uint16_t LiveOffsets[NumLiveOffsets]; -/// } Descriptors[NumDescriptors]; -/// } caml${module}__frametable; -/// -/// Note that this precludes programs from stack frames larger than 64K -/// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if -/// either condition is detected in a function which uses the collector. -/// -void OcamlGCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP, - const TargetAsmInfo &TAI) { - const char *AddressDirective; - int AddressAlignLog; - if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) { - AddressDirective = TAI.getData32bitsDirective(); - AddressAlignLog = 2; - } else { - AddressDirective = TAI.getData64bitsDirective(); - AddressAlignLog = 3; - } - - AP.SwitchToTextSection(TAI.getTextSection()); - EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end"); - - AP.SwitchToDataSection(TAI.getDataSection()); - EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end"); - - OS << AddressDirective << 0; // FIXME: Why does ocaml emit this?? - AP.EOL(); - - AP.SwitchToDataSection(TAI.getDataSection()); - EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable"); - - for (iterator FI = begin(), FE = end(); FI != FE; ++FI) { - CollectorMetadata &MD = **FI; - - OS << "\t" << TAI.getCommentString() << " live roots for " - << MD.getFunction().getNameStart() << "\n"; - - for (CollectorMetadata::iterator PI = MD.begin(), - PE = MD.end(); PI != PE; ++PI) { - - uint64_t FrameSize = MD.getFrameSize(); - if (FrameSize >= 1<<16) { - cerr << "Function '" << MD.getFunction().getNameStart() - << "' is too large for the ocaml collector! " - << "Frame size " << FrameSize << " >= 65536.\n"; - abort(); // Very rude! - } - - size_t LiveCount = MD.live_size(PI); - if (LiveCount >= 1<<16) { - cerr << "Function '" << MD.getFunction().getNameStart() - << "' is too large for the ocaml collector! " - << "Live root count " << LiveCount << " >= 65536.\n"; - abort(); // Very rude! - } - - OS << AddressDirective - << TAI.getPrivateGlobalPrefix() << "label" << PI->Num; - AP.EOL("call return address"); - - AP.EmitInt16(FrameSize); - AP.EOL("stack frame size"); - - AP.EmitInt16(LiveCount); - AP.EOL("live root count"); - - for (CollectorMetadata::live_iterator LI = MD.live_begin(PI), - LE = MD.live_end(PI); - LI != LE; ++LI) { - assert(LI->StackOffset < 1<<16 && - "GC root stack offset is outside of fixed stack frame and out " - "of range for Ocaml collector!"); - - OS << "\t.word\t" << LI->StackOffset; - AP.EOL("stack offset"); - } - - AP.EmitAlignment(AddressAlignLog); - } - } -} diff --git a/lib/CodeGen/OcamlGC.cpp b/lib/CodeGen/OcamlGC.cpp new file mode 100644 index 0000000000..88499cbd66 --- /dev/null +++ b/lib/CodeGen/OcamlGC.cpp @@ -0,0 +1,46 @@ +//===-- OcamlCollector.cpp - Ocaml frametable emitter ---------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements lowering for the llvm.gc* intrinsics compatible with +// Objective Caml 3.10.0, which uses a liveness-accurate static stack map. +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/GCs.h" +#include "llvm/CodeGen/AsmPrinter.h" +#include "llvm/CodeGen/GCStrategy.h" +#include "llvm/Module.h" +#include "llvm/Target/TargetAsmInfo.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Target/TargetMachine.h" + +using namespace llvm; + +namespace { + + class VISIBILITY_HIDDEN OcamlCollector : public Collector { + public: + OcamlCollector(); + }; + +} + +static CollectorRegistry::Add +X("ocaml", "ocaml 3.10-compatible collector"); + +// ----------------------------------------------------------------------------- + +Collector *llvm::createOcamlCollector() { + return new OcamlCollector(); +} + +OcamlCollector::OcamlCollector() { + NeededSafePoints = 1 << GC::PostCall; + UsesMetadata = true; +} diff --git a/lib/CodeGen/OcamlGCPrinter.cpp b/lib/CodeGen/OcamlGCPrinter.cpp new file mode 100644 index 0000000000..efa7f67276 --- /dev/null +++ b/lib/CodeGen/OcamlGCPrinter.cpp @@ -0,0 +1,163 @@ +//===-- OcamlCollector.cpp - Ocaml frametable emitter ---------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements lowering for the llvm.gc* intrinsics compatible with +// Objective Caml 3.10.0, which uses a liveness-accurate static stack map. +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/GCs.h" +#include "llvm/CodeGen/AsmPrinter.h" +#include "llvm/CodeGen/GCStrategy.h" +#include "llvm/Module.h" +#include "llvm/Target/TargetAsmInfo.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Target/TargetMachine.h" + +using namespace llvm; + +namespace { + + class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter { + public: + void beginAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI); + + void finishAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI); + }; + +} + +static GCMetadataPrinterRegistry::Add +Y("ocaml", "ocaml 3.10-compatible collector"); + +GCMetadataPrinter *llvm::createOcamlMetadataPrinter() { + return new OcamlGCMetadataPrinter(); +} + +static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI, const char *Id) { + const std::string &MId = M.getModuleIdentifier(); + + std::string Mangled; + Mangled += TAI.getGlobalPrefix(); + Mangled += "caml"; + size_t Letter = Mangled.size(); + Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.')); + Mangled += "__"; + Mangled += Id; + + // Capitalize the first letter of the module name. + Mangled[Letter] = toupper(Mangled[Letter]); + + if (const char *GlobalDirective = TAI.getGlobalDirective()) + OS << GlobalDirective << Mangled << "\n"; + OS << Mangled << ":\n"; +} + +void OcamlGCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI) { + AP.SwitchToTextSection(TAI.getTextSection()); + EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin"); + + AP.SwitchToDataSection(TAI.getDataSection()); + EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin"); +} + +/// emitAssembly - Print the frametable. The ocaml frametable format is thus: +/// +/// extern "C" struct align(sizeof(intptr_t)) { +/// uint16_t NumDescriptors; +/// struct align(sizeof(intptr_t)) { +/// void *ReturnAddress; +/// uint16_t FrameSize; +/// uint16_t NumLiveOffsets; +/// uint16_t LiveOffsets[NumLiveOffsets]; +/// } Descriptors[NumDescriptors]; +/// } caml${module}__frametable; +/// +/// Note that this precludes programs from stack frames larger than 64K +/// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if +/// either condition is detected in a function which uses the collector. +/// +void OcamlGCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP, + const TargetAsmInfo &TAI) { + const char *AddressDirective; + int AddressAlignLog; + if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) { + AddressDirective = TAI.getData32bitsDirective(); + AddressAlignLog = 2; + } else { + AddressDirective = TAI.getData64bitsDirective(); + AddressAlignLog = 3; + } + + AP.SwitchToTextSection(TAI.getTextSection()); + EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end"); + + AP.SwitchToDataSection(TAI.getDataSection()); + EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end"); + + OS << AddressDirective << 0; // FIXME: Why does ocaml emit this?? + AP.EOL(); + + AP.SwitchToDataSection(TAI.getDataSection()); + EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable"); + + for (iterator FI = begin(), FE = end(); FI != FE; ++FI) { + CollectorMetadata &MD = **FI; + + OS << "\t" << TAI.getCommentString() << " live roots for " + << MD.getFunction().getNameStart() << "\n"; + + for (CollectorMetadata::iterator PI = MD.begin(), + PE = MD.end(); PI != PE; ++PI) { + + uint64_t FrameSize = MD.getFrameSize(); + if (FrameSize >= 1<<16) { + cerr << "Function '" << MD.getFunction().getNameStart() + << "' is too large for the ocaml collector! " + << "Frame size " << FrameSize << " >= 65536.\n"; + abort(); // Very rude! + } + + size_t LiveCount = MD.live_size(PI); + if (LiveCount >= 1<<16) { + cerr << "Function '" << MD.getFunction().getNameStart() + << "' is too large for the ocaml collector! " + << "Live root count " << LiveCount << " >= 65536.\n"; + abort(); // Very rude! + } + + OS << AddressDirective + << TAI.getPrivateGlobalPrefix() << "label" << PI->Num; + AP.EOL("call return address"); + + AP.EmitInt16(FrameSize); + AP.EOL("stack frame size"); + + AP.EmitInt16(LiveCount); + AP.EOL("live root count"); + + for (CollectorMetadata::live_iterator LI = MD.live_begin(PI), + LE = MD.live_end(PI); + LI != LE; ++LI) { + assert(LI->StackOffset < 1<<16 && + "GC root stack offset is outside of fixed stack frame and out " + "of range for Ocaml collector!"); + + OS << "\t.word\t" << LI->StackOffset; + AP.EOL("stack offset"); + } + + AP.EmitAlignment(AddressAlignLog); + } + } +} diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index 815fdb7cc7..012e7d0992 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -26,7 +26,7 @@ #include "llvm/Intrinsics.h" #include "llvm/IntrinsicInst.h" #include "llvm/ParameterAttributes.h" -#include "llvm/CodeGen/Collector.h" +#include "llvm/CodeGen/GCStrategy.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineInstrBuilder.h" diff --git a/lib/CodeGen/ShadowStackCollector.cpp b/lib/CodeGen/ShadowStackCollector.cpp deleted file mode 100644 index 4d275a6eb9..0000000000 --- a/lib/CodeGen/ShadowStackCollector.cpp +++ /dev/null @@ -1,441 +0,0 @@ -//===-- ShadowStackCollector.cpp - GC support for uncooperative targets ---===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements lowering for the llvm.gc* intrinsics for targets that do -// not natively support them (which includes the C backend). Note that the code -// generated is not quite as efficient as collectors which generate stack maps -// to identify roots. -// -// This pass implements the code transformation described in this paper: -// "Accurate Garbage Collection in an Uncooperative Environment" -// Fergus Henderson, ISMM, 2002 -// -// In runtime/GC/SemiSpace.cpp is a prototype runtime which is compatible with -// this collector. -// -// In order to support this particular transformation, all stack roots are -// coallocated in the stack. This allows a fully target-independent stack map -// while introducing only minor runtime overhead. -// -//===----------------------------------------------------------------------===// - -#define DEBUG_TYPE "shadowstackgc" -#include "llvm/CodeGen/Collectors.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/CodeGen/Collector.h" -#include "llvm/IntrinsicInst.h" -#include "llvm/Module.h" -#include "llvm/Support/IRBuilder.h" - -using namespace llvm; - -namespace { - - class VISIBILITY_HIDDEN ShadowStackCollector : public Collector { - /// RootChain - This is the global linked-list that contains the chain of GC - /// roots. - GlobalVariable *Head; - - /// StackEntryTy - Abstract type of a link in the shadow stack. - /// - const StructType *StackEntryTy; - - /// Roots - GC roots in the current function. Each is a pair of the - /// intrinsic call and its corresponding alloca. - std::vector > Roots; - - public: - ShadowStackCollector(); - - bool initializeCustomLowering(Module &M); - bool performCustomLowering(Function &F); - - private: - bool IsNullValue(Value *V); - Constant *GetFrameMap(Function &F); - const Type* GetConcreteStackEntryType(Function &F); - void CollectRoots(Function &F); - static GetElementPtrInst *CreateGEP(IRBuilder<> &B, Value *BasePtr, - int Idx1, const char *Name); - static GetElementPtrInst *CreateGEP(IRBuilder<> &B, Value *BasePtr, - int Idx1, int Idx2, const char *Name); - }; - -} - -static CollectorRegistry::Add -Y("shadow-stack", - "Very portable collector for uncooperative code generators"); - -namespace { - /// EscapeEnumerator - This is a little algorithm to find all escape points - /// from a function so that "finally"-style code can be inserted. In addition - /// to finding the existing return and unwind instructions, it also (if - /// necessary) transforms any call instructions into invokes and sends them to - /// a landing pad. - /// - /// It's wrapped up in a state machine using the same transform C# uses for - /// 'yield return' enumerators, This transform allows it to be non-allocating. - class VISIBILITY_HIDDEN EscapeEnumerator { - Function &F; - const char *CleanupBBName; - - // State. - int State; - Function::iterator StateBB, StateE; - IRBuilder<> Builder; - - public: - EscapeEnumerator(Function &F, const char *N = "cleanup") - : F(F), CleanupBBName(N), State(0) {} - - IRBuilder<> *Next() { - switch (State) { - default: - return 0; - - case 0: - StateBB = F.begin(); - StateE = F.end(); - State = 1; - - case 1: - // Find all 'return' and 'unwind' instructions. - while (StateBB != StateE) { - BasicBlock *CurBB = StateBB++; - - // Branches and invokes do not escape, only unwind and return do. - TerminatorInst *TI = CurBB->getTerminator(); - if (!isa(TI) && !isa(TI)) - continue; - - Builder.SetInsertPoint(TI->getParent(), TI); - return &Builder; - } - - State = 2; - - // Find all 'call' instructions. - SmallVector Calls; - for (Function::iterator BB = F.begin(), - E = F.end(); BB != E; ++BB) - for (BasicBlock::iterator II = BB->begin(), - EE = BB->end(); II != EE; ++II) - if (CallInst *CI = dyn_cast(II)) - if (!CI->getCalledFunction() || - !CI->getCalledFunction()->getIntrinsicID()) - Calls.push_back(CI); - - if (Calls.empty()) - return 0; - - // Create a cleanup block. - BasicBlock *CleanupBB = BasicBlock::Create(CleanupBBName, &F); - UnwindInst *UI = new UnwindInst(CleanupBB); - - // Transform the 'call' instructions into 'invoke's branching to the - // cleanup block. Go in reverse order to make prettier BB names. - SmallVector Args; - for (unsigned I = Calls.size(); I != 0; ) { - CallInst *CI = cast(Calls[--I]); - - // Split the basic block containing the function call. - BasicBlock *CallBB = CI->getParent(); - BasicBlock *NewBB = - CallBB->splitBasicBlock(CI, CallBB->getName() + ".cont"); - - // Remove the unconditional branch inserted at the end of CallBB. - CallBB->getInstList().pop_back(); - NewBB->getInstList().remove(CI); - - // Create a new invoke instruction. - Args.clear(); - Args.append(CI->op_begin() + 1, CI->op_end()); - - InvokeInst *II = InvokeInst::Create(CI->getOperand(0), - NewBB, CleanupBB, - Args.begin(), Args.end(), - CI->getName(), CallBB); - II->setCallingConv(CI->getCallingConv()); - II->setParamAttrs(CI->getParamAttrs()); - CI->replaceAllUsesWith(II); - delete CI; - } - - Builder.SetInsertPoint(UI->getParent(), UI); - return &Builder; - } - } - }; - -} - -// ----------------------------------------------------------------------------- - -Collector *llvm::createShadowStackCollector() { - return new ShadowStackCollector(); -} - -ShadowStackCollector::ShadowStackCollector() : Head(0), StackEntryTy(0) { - InitRoots = true; - CustomRoots = true; -} - -Constant *ShadowStackCollector::GetFrameMap(Function &F) { - // doInitialization creates the abstract type of this value. - - Type *VoidPtr = PointerType::getUnqual(Type::Int8Ty); - - // Truncate the ShadowStackDescriptor if some metadata is null. - unsigned NumMeta = 0; - SmallVector Metadata; - for (unsigned I = 0; I != Roots.size(); ++I) { - Constant *C = cast(Roots[I].first->getOperand(2)); - if (!C->isNullValue()) - NumMeta = I + 1; - Metadata.push_back(ConstantExpr::getBitCast(C, VoidPtr)); - } - - Constant *BaseElts[] = { - ConstantInt::get(Type::Int32Ty, Roots.size(), false), - ConstantInt::get(Type::Int32Ty, NumMeta, false), - }; - - Constant *DescriptorElts[] = { - ConstantStruct::get(BaseElts, 2), - ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), - Metadata.begin(), NumMeta) - }; - - Constant *FrameMap = ConstantStruct::get(DescriptorElts, 2); - - std::string TypeName("gc_map."); - TypeName += utostr(NumMeta); - F.getParent()->addTypeName(TypeName, FrameMap->getType()); - - // FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems - // that, short of multithreaded LLVM, it should be safe; all that is - // necessary is that a simple Module::iterator loop not be invalidated. - // Appending to the GlobalVariable list is safe in that sense. - // - // All of the output passes emit globals last. The ExecutionEngine - // explicitly supports adding globals to the module after - // initialization. - // - // Still, if it isn't deemed acceptable, then this transformation needs - // to be a ModulePass (which means it cannot be in the 'llc' pipeline - // (which uses a FunctionPassManager (which segfaults (not asserts) if - // provided a ModulePass))). - Constant *GV = new GlobalVariable(FrameMap->getType(), true, - GlobalVariable::InternalLinkage, - FrameMap, "__gc_" + F.getName(), - F.getParent()); - - Constant *GEPIndices[2] = { ConstantInt::get(Type::Int32Ty, 0), - ConstantInt::get(Type::Int32Ty, 0) }; - return ConstantExpr::getGetElementPtr(GV, GEPIndices, 2); -} - -const Type* ShadowStackCollector::GetConcreteStackEntryType(Function &F) { - // doInitialization creates the generic version of this type. - std::vector EltTys; - EltTys.push_back(StackEntryTy); - for (size_t I = 0; I != Roots.size(); I++) - EltTys.push_back(Roots[I].second->getAllocatedType()); - Type *Ty = StructType::get(EltTys); - - std::string TypeName("gc_stackentry."); - TypeName += F.getName(); - F.getParent()->addTypeName(TypeName, Ty); - - return Ty; -} - -/// doInitialization - If this module uses the GC intrinsics, find them now. If -/// not, exit fast. -bool ShadowStackCollector::initializeCustomLowering(Module &M) { - // struct FrameMap { - // int32_t NumRoots; // Number of roots in stack frame. - // int32_t NumMeta; // Number of metadata descriptors. May be < NumRoots. - // void *Meta[]; // May be absent for roots without metadata. - // }; - std::vector EltTys; - EltTys.push_back(Type::Int32Ty); // 32 bits is ok up to a 32GB stack frame. :) - EltTys.push_back(Type::Int32Ty); // Specifies length of variable length array. - StructType *FrameMapTy = StructType::get(EltTys); - M.addTypeName("gc_map", FrameMapTy); - PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy); - - // struct StackEntry { - // ShadowStackEntry *Next; // Caller's stack entry. - // FrameMap *Map; // Pointer to constant FrameMap. - // void *Roots[]; // Stack roots (in-place array, so we pretend). - // }; - OpaqueType *RecursiveTy = OpaqueType::get(); - - EltTys.clear(); - EltTys.push_back(PointerType::getUnqual(RecursiveTy)); - EltTys.push_back(FrameMapPtrTy); - PATypeHolder LinkTyH = StructType::get(EltTys); - - RecursiveTy->refineAbstractTypeTo(LinkTyH.get()); - StackEntryTy = cast(LinkTyH.get()); - const PointerType *StackEntryPtrTy = PointerType::getUnqual(StackEntryTy); - M.addTypeName("gc_stackentry", LinkTyH.get()); // FIXME: Is this safe from - // a FunctionPass? - - // Get the root chain if it already exists. - Head = M.getGlobalVariable("llvm_gc_root_chain"); - if (!Head) { - // If the root chain does not exist, insert a new one with linkonce - // linkage! - Head = new GlobalVariable(StackEntryPtrTy, false, - GlobalValue::LinkOnceLinkage, - Constant::getNullValue(StackEntryPtrTy), - "llvm_gc_root_chain", &M); - } else if (Head->hasExternalLinkage() && Head->isDeclaration()) { - Head->setInitializer(Constant::getNullValue(StackEntryPtrTy)); - Head->setLinkage(GlobalValue::LinkOnceLinkage); - } - - return true; -} - -bool ShadowStackCollector::IsNullValue(Value *V) { - if (Constant *C = dyn_cast(V)) - return C->isNullValue(); - return false; -} - -void ShadowStackCollector::CollectRoots(Function &F) { - // FIXME: Account for original alignment. Could fragment the root array. - // Approach 1: Null initialize empty slots at runtime. Yuck. - // Approach 2: Emit a map of the array instead of just a count. - - assert(Roots.empty() && "Not cleaned up?"); - - SmallVector,16> MetaRoots; - - for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) - for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) - if (IntrinsicInst *CI = dyn_cast(II++)) - if (Function *F = CI->getCalledFunction()) - if (F->getIntrinsicID() == Intrinsic::gcroot) { - std::pair Pair = std::make_pair( - CI, cast(CI->getOperand(1)->stripPointerCasts())); - if (IsNullValue(CI->getOperand(2))) - Roots.push_back(Pair); - else - MetaRoots.push_back(Pair); - } - - // Number roots with metadata (usually empty) at the beginning, so that the - // FrameMap::Meta array can be elided. - Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end()); -} - -GetElementPtrInst * -ShadowStackCollector::CreateGEP(IRBuilder<> &B, Value *BasePtr, - int Idx, int Idx2, const char *Name) { - Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0), - ConstantInt::get(Type::Int32Ty, Idx), - ConstantInt::get(Type::Int32Ty, Idx2) }; - Value* Val = B.CreateGEP(BasePtr, Indices, Indices + 3, Name); - - assert(isa(Val) && "Unexpected folded constant"); - - return dyn_cast(Val); -} - -GetElementPtrInst * -ShadowStackCollector::CreateGEP(IRBuilder<> &B, Value *BasePtr, - int Idx, const char *Name) { - Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0), - ConstantInt::get(Type::Int32Ty, Idx) }; - Value *Val = B.CreateGEP(BasePtr, Indices, Indices + 2, Name); - - assert(isa(Val) && "Unexpected folded constant"); - - return dyn_cast(Val); -} - -/// runOnFunction - Insert code to maintain the shadow stack. -bool ShadowStackCollector::performCustomLowering(Function &F) { - // Find calls to llvm.gcroot. - CollectRoots(F); - - // If there are no roots in this function, then there is no need to add a - // stack map entry for it. - if (Roots.empty()) - return false; - - // Build the constant map and figure the type of the shadow stack entry. - Value *FrameMap = GetFrameMap(F); - const Type *ConcreteStackEntryTy = GetConcreteStackEntryType(F); - - // Build the shadow stack entry at the very start of the function. - BasicBlock::iterator IP = F.getEntryBlock().begin(); - IRBuilder<> AtEntry(IP->getParent(), IP); - - Instruction *StackEntry = AtEntry.CreateAlloca(ConcreteStackEntryTy, 0, - "gc_frame"); - - while (isa(IP)) ++IP; - AtEntry.SetInsertPoint(IP->getParent(), IP); - - // Initialize the map pointer and load the current head of the shadow stack. - Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead"); - Instruction *EntryMapPtr = CreateGEP(AtEntry, StackEntry,0,1,"gc_frame.map"); - AtEntry.CreateStore(FrameMap, EntryMapPtr); - - // After all the allocas... - for (unsigned I = 0, E = Roots.size(); I != E; ++I) { - // For each root, find the corresponding slot in the aggregate... - Value *SlotPtr = CreateGEP(AtEntry, StackEntry, 1 + I, "gc_root"); - - // And use it in lieu of the alloca. - AllocaInst *OriginalAlloca = Roots[I].second; - SlotPtr->takeName(OriginalAlloca); - OriginalAlloca->replaceAllUsesWith(SlotPtr); - } - - // Move past the original stores inserted by Collector::InitRoots. This isn't - // really necessary (the collector would never see the intermediate state), - // but it's nicer not to push the half-initialized entry onto the stack. - while (isa(IP)) ++IP; - AtEntry.SetInsertPoint(IP->getParent(), IP); - - // Push the entry onto the shadow stack. - Instruction *EntryNextPtr = CreateGEP(AtEntry,StackEntry,0,0,"gc_frame.next"); - Instruction *NewHeadVal = CreateGEP(AtEntry,StackEntry, 0, "gc_newhead"); - AtEntry.CreateStore(CurrentHead, EntryNextPtr); - AtEntry.CreateStore(NewHeadVal, Head); - - // For each instruction that escapes... - EscapeEnumerator EE(F, "gc_cleanup"); - while (IRBuilder<> *AtExit = EE.Next()) { - // Pop the entry from the shadow stack. Don't reuse CurrentHead from - // AtEntry, since that would make the value live for the entire function. - Instruction *EntryNextPtr2 = CreateGEP(*AtExit, StackEntry, 0, 0, - "gc_frame.next"); - Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead"); - AtExit->CreateStore(SavedHead, Head); - } - - // Delete the original allocas (which are no longer used) and the intrinsic - // calls (which are no longer valid). Doing this last avoids invalidating - // iterators. - for (unsigned I = 0, E = Roots.size(); I != E; ++I) { - Roots[I].first->eraseFromParent(); - Roots[I].second->eraseFromParent(); - } - - Roots.clear(); - return true; -} diff --git a/lib/CodeGen/ShadowStackGC.cpp b/lib/CodeGen/ShadowStackGC.cpp new file mode 100644 index 0000000000..850c00542c --- /dev/null +++ b/lib/CodeGen/ShadowStackGC.cpp @@ -0,0 +1,441 @@ +//===-- ShadowStackCollector.cpp - GC support for uncooperative targets ---===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements lowering for the llvm.gc* intrinsics for targets that do +// not natively support them (which includes the C backend). Note that the code +// generated is not quite as efficient as collectors which generate stack maps +// to identify roots. +// +// This pass implements the code transformation described in this paper: +// "Accurate Garbage Collection in an Uncooperative Environment" +// Fergus Henderson, ISMM, 2002 +// +// In runtime/GC/SemiSpace.cpp is a prototype runtime which is compatible with +// this collector. +// +// In order to support this particular transformation, all stack roots are +// coallocated in the stack. This allows a fully target-independent stack map +// while introducing only minor runtime overhead. +// +//===----------------------------------------------------------------------===// + +#define DEBUG_TYPE "shadowstackgc" +#include "llvm/CodeGen/GCs.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/CodeGen/GCStrategy.h" +#include "llvm/IntrinsicInst.h" +#include "llvm/Module.h" +#include "llvm/Support/IRBuilder.h" + +using namespace llvm; + +namespace { + + class VISIBILITY_HIDDEN ShadowStackCollector : public Collector { + /// RootChain - This is the global linked-list that contains the chain of GC + /// roots. + GlobalVariable *Head; + + /// StackEntryTy - Abstract type of a link in the shadow stack. + /// + const StructType *StackEntryTy; + + /// Roots - GC roots in the current function. Each is a pair of the + /// intrinsic call and its corresponding alloca. + std::vector > Roots; + + public: + ShadowStackCollector(); + + bool initializeCustomLowering(Module &M); + bool performCustomLowering(Function &F); + + private: + bool IsNullValue(Value *V); + Constant *GetFrameMap(Function &F); + const Type* GetConcreteStackEntryType(Function &F); + void CollectRoots(Function &F); + static GetElementPtrInst *CreateGEP(IRBuilder<> &B, Value *BasePtr, + int Idx1, const char *Name); + static GetElementPtrInst *CreateGEP(IRBuilder<> &B, Value *BasePtr, + int Idx1, int Idx2, const char *Name); + }; + +} + +static CollectorRegistry::Add +Y("shadow-stack", + "Very portable collector for uncooperative code generators"); + +namespace { + /// EscapeEnumerator - This is a little algorithm to find all escape points + /// from a function so that "finally"-style code can be inserted. In addition + /// to finding the existing return and unwind instructions, it also (if + /// necessary) transforms any call instructions into invokes and sends them to + /// a landing pad. + /// + /// It's wrapped up in a state machine using the same transform C# uses for + /// 'yield return' enumerators, This transform allows it to be non-allocating. + class VISIBILITY_HIDDEN EscapeEnumerator { + Function &F; + const char *CleanupBBName; + + // State. + int State; + Function::iterator StateBB, StateE; + IRBuilder<> Builder; + + public: + EscapeEnumerator(Function &F, const char *N = "cleanup") + : F(F), CleanupBBName(N), State(0) {} + + IRBuilder<> *Next() { + switch (State) { + default: + return 0; + + case 0: + StateBB = F.begin(); + StateE = F.end(); + State = 1; + + case 1: + // Find all 'return' and 'unwind' instructions. + while (StateBB != StateE) { + BasicBlock *CurBB = StateBB++; + + // Branches and invokes do not escape, only unwind and return do. + TerminatorInst *TI = CurBB->getTerminator(); + if (!isa(TI) && !isa(TI)) + continue; + + Builder.SetInsertPoint(TI->getParent(), TI); + return &Builder; + } + + State = 2; + + // Find all 'call' instructions. + SmallVector Calls; + for (Function::iterator BB = F.begin(), + E = F.end(); BB != E; ++BB) + for (BasicBlock::iterator II = BB->begin(), + EE = BB->end(); II != EE; ++II) + if (CallInst *CI = dyn_cast(II)) + if (!CI->getCalledFunction() || + !CI->getCalledFunction()->getIntrinsicID()) + Calls.push_back(CI); + + if (Calls.empty()) + return 0; + + // Create a cleanup block. + BasicBlock *CleanupBB = BasicBlock::Create(CleanupBBName, &F); + UnwindInst *UI = new UnwindInst(CleanupBB); + + // Transform the 'call' instructions into 'invoke's branching to the + // cleanup block. Go in reverse order to make prettier BB names. + SmallVector Args; + for (unsigned I = Calls.size(); I != 0; ) { + CallInst *CI = cast(Calls[--I]); + + // Split the basic block containing the function call. + BasicBlock *CallBB = CI->getParent(); + BasicBlock *NewBB = + CallBB->splitBasicBlock(CI, CallBB->getName() + ".cont"); + + // Remove the unconditional branch inserted at the end of CallBB. + CallBB->getInstList().pop_back(); + NewBB->getInstList().remove(CI); + + // Create a new invoke instruction. + Args.clear(); + Args.append(CI->op_begin() + 1, CI->op_end()); + + InvokeInst *II = InvokeInst::Create(CI->getOperand(0), + NewBB, CleanupBB, + Args.begin(), Args.end(), + CI->getName(), CallBB); + II->setCallingConv(CI->getCallingConv()); + II->setParamAttrs(CI->getParamAttrs()); + CI->replaceAllUsesWith(II); + delete CI; + } + + Builder.SetInsertPoint(UI->getParent(), UI); + return &Builder; + } + } + }; + +} + +// ----------------------------------------------------------------------------- + +Collector *llvm::createShadowStackCollector() { + return new ShadowStackCollector(); +} + +ShadowStackCollector::ShadowStackCollector() : Head(0), StackEntryTy(0) { + InitRoots = true; + CustomRoots = true; +} + +Constant *ShadowStackCollector::GetFrameMap(Function &F) { + // doInitialization creates the abstract type of this value. + + Type *VoidPtr = PointerType::getUnqual(Type::Int8Ty); + + // Truncate the ShadowStackDescriptor if some metadata is null. + unsigned NumMeta = 0; + SmallVector Metadata; + for (unsigned I = 0; I != Roots.size(); ++I) { + Constant *C = cast(Roots[I].first->getOperand(2)); + if (!C->isNullValue()) + NumMeta = I + 1; + Metadata.push_back(ConstantExpr::getBitCast(C, VoidPtr)); + } + + Constant *BaseElts[] = { + ConstantInt::get(Type::Int32Ty, Roots.size(), false), + ConstantInt::get(Type::Int32Ty, NumMeta, false), + }; + + Constant *DescriptorElts[] = { + ConstantStruct::get(BaseElts, 2), + ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), + Metadata.begin(), NumMeta) + }; + + Constant *FrameMap = ConstantStruct::get(DescriptorElts, 2); + + std::string TypeName("gc_map."); + TypeName += utostr(NumMeta); + F.getParent()->addTypeName(TypeName, FrameMap->getType()); + + // FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems + // that, short of multithreaded LLVM, it should be safe; all that is + // necessary is that a simple Module::iterator loop not be invalidated. + // Appending to the GlobalVariable list is safe in that sense. + // + // All of the output passes emit globals last. The ExecutionEngine + // explicitly supports adding globals to the module after + // initialization. + // + // Still, if it isn't deemed acceptable, then this transformation needs + // to be a ModulePass (which means it cannot be in the 'llc' pipeline + // (which uses a FunctionPassManager (which segfaults (not asserts) if + // provided a ModulePass))). + Constant *GV = new GlobalVariable(FrameMap->getType(), true, + GlobalVariable::InternalLinkage, + FrameMap, "__gc_" + F.getName(), + F.getParent()); + + Constant *GEPIndices[2] = { ConstantInt::get(Type::Int32Ty, 0), + ConstantInt::get(Type::Int32Ty, 0) }; + return ConstantExpr::getGetElementPtr(GV, GEPIndices, 2); +} + +const Type* ShadowStackCollector::GetConcreteStackEntryType(Function &F) { + // doInitialization creates the generic version of this type. + std::vector EltTys; + EltTys.push_back(StackEntryTy); + for (size_t I = 0; I != Roots.size(); I++) + EltTys.push_back(Roots[I].second->getAllocatedType()); + Type *Ty = StructType::get(EltTys); + + std::string TypeName("gc_stackentry."); + TypeName += F.getName(); + F.getParent()->addTypeName(TypeName, Ty); + + return Ty; +} + +/// doInitialization - If this module uses the GC intrinsics, find them now. If +/// not, exit fast. +bool ShadowStackCollector::initializeCustomLowering(Module &M) { + // struct FrameMap { + // int32_t NumRoots; // Number of roots in stack frame. + // int32_t NumMeta; // Number of metadata descriptors. May be < NumRoots. + // void *Meta[]; // May be absent for roots without metadata. + // }; + std::vector EltTys; + EltTys.push_back(Type::Int32Ty); // 32 bits is ok up to a 32GB stack frame. :) + EltTys.push_back(Type::Int32Ty); // Specifies length of variable length array. + StructType *FrameMapTy = StructType::get(EltTys); + M.addTypeName("gc_map", FrameMapTy); + PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy); + + // struct StackEntry { + // ShadowStackEntry *Next; // Caller's stack entry. + // FrameMap *Map; // Pointer to constant FrameMap. + // void *Roots[]; // Stack roots (in-place array, so we pretend). + // }; + OpaqueType *RecursiveTy = OpaqueType::get(); + + EltTys.clear(); + EltTys.push_back(PointerType::getUnqual(RecursiveTy)); + EltTys.push_back(FrameMapPtrTy); + PATypeHolder LinkTyH = StructType::get(EltTys); + + RecursiveTy->refineAbstractTypeTo(LinkTyH.get()); + StackEntryTy = cast(LinkTyH.get()); + const PointerType *StackEntryPtrTy = PointerType::getUnqual(StackEntryTy); + M.addTypeName("gc_stackentry", LinkTyH.get()); // FIXME: Is this safe from + // a FunctionPass? + + // Get the root chain if it already exists. + Head = M.getGlobalVariable("llvm_gc_root_chain"); + if (!Head) { + // If the root chain does not exist, insert a new one with linkonce + // linkage! + Head = new GlobalVariable(StackEntryPtrTy, false, + GlobalValue::LinkOnceLinkage, + Constant::getNullValue(StackEntryPtrTy), + "llvm_gc_root_chain", &M); + } else if (Head->hasExternalLinkage() && Head->isDeclaration()) { + Head->setInitializer(Constant::getNullValue(StackEntryPtrTy)); + Head->setLinkage(GlobalValue::LinkOnceLinkage); + } + + return true; +} + +bool ShadowStackCollector::IsNullValue(Value *V) { + if (Constant *C = dyn_cast(V)) + return C->isNullValue(); + return false; +} + +void ShadowStackCollector::CollectRoots(Function &F) { + // FIXME: Account for original alignment. Could fragment the root array. + // Approach 1: Null initialize empty slots at runtime. Yuck. + // Approach 2: Emit a map of the array instead of just a count. + + assert(Roots.empty() && "Not cleaned up?"); + + SmallVector,16> MetaRoots; + + for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) + for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) + if (IntrinsicInst *CI = dyn_cast(II++)) + if (Function *F = CI->getCalledFunction()) + if (F->getIntrinsicID() == Intrinsic::gcroot) { + std::pair Pair = std::make_pair( + CI, cast(CI->getOperand(1)->stripPointerCasts())); + if (IsNullValue(CI->getOperand(2))) + Roots.push_back(Pair); + else + MetaRoots.push_back(Pair); + } + + // Number roots with metadata (usually empty) at the beginning, so that the + // FrameMap::Meta array can be elided. + Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end()); +} + +GetElementPtrInst * +ShadowStackCollector::CreateGEP(IRBuilder<> &B, Value *BasePtr, + int Idx, int Idx2, const char *Name) { + Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0), + ConstantInt::get(Type::Int32Ty, Idx), + ConstantInt::get(Type::Int32Ty, Idx2) }; + Value* Val = B.CreateGEP(BasePtr, Indices, Indices + 3, Name); + + assert(isa(Val) && "Unexpected folded constant"); + + return dyn_cast(Val); +} + +GetElementPtrInst * +ShadowStackCollector::CreateGEP(IRBuilder<> &B, Value *BasePtr, + int Idx, const char *Name) { + Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0), + ConstantInt::get(Type::Int32Ty, Idx) }; + Value *Val = B.CreateGEP(BasePtr, Indices, Indices + 2, Name); + + assert(isa(Val) && "Unexpected folded constant"); + + return dyn_cast(Val); +} + +/// runOnFunction - Insert code to maintain the shadow stack. +bool ShadowStackCollector::performCustomLowering(Function &F) { + // Find calls to llvm.gcroot. + CollectRoots(F); + + // If there are no roots in this function, then there is no need to add a + // stack map entry for it. + if (Roots.empty()) + return false; + + // Build the constant map and figure the type of the shadow stack entry. + Value *FrameMap = GetFrameMap(F); + const Type *ConcreteStackEntryTy = GetConcreteStackEntryType(F); + + // Build the shadow stack entry at the very start of the function. + BasicBlock::iterator IP = F.getEntryBlock().begin(); + IRBuilder<> AtEntry(IP->getParent(), IP); + + Instruction *StackEntry = AtEntry.CreateAlloca(ConcreteStackEntryTy, 0, + "gc_frame"); + + while (isa(IP)) ++IP; + AtEntry.SetInsertPoint(IP->getParent(), IP); + + // Initialize the map pointer and load the current head of the shadow stack. + Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead"); + Instruction *EntryMapPtr = CreateGEP(AtEntry, StackEntry,0,1,"gc_frame.map"); + AtEntry.CreateStore(FrameMap, EntryMapPtr); + + // After all the allocas... + for (unsigned I = 0, E = Roots.size(); I != E; ++I) { + // For each root, find the corresponding slot in the aggregate... + Value *SlotPtr = CreateGEP(AtEntry, StackEntry, 1 + I, "gc_root"); + + // And use it in lieu of the alloca. + AllocaInst *OriginalAlloca = Roots[I].second; + SlotPtr->takeName(OriginalAlloca); + OriginalAlloca->replaceAllUsesWith(SlotPtr); + } + + // Move past the original stores inserted by Collector::InitRoots. This isn't + // really necessary (the collector would never see the intermediate state), + // but it's nicer not to push the half-initialized entry onto the stack. + while (isa(IP)) ++IP; + AtEntry.SetInsertPoint(IP->getParent(), IP); + + // Push the entry onto the shadow stack. + Instruction *EntryNextPtr = CreateGEP(AtEntry,StackEntry,0,0,"gc_frame.next"); + Instruction *NewHeadVal = CreateGEP(AtEntry,StackEntry, 0, "gc_newhead"); + AtEntry.CreateStore(CurrentHead, EntryNextPtr); + AtEntry.CreateStore(NewHeadVal, Head); + + // For each instruction that escapes... + EscapeEnumerator EE(F, "gc_cleanup"); + while (IRBuilder<> *AtExit = EE.Next()) { + // Pop the entry from the shadow stack. Don't reuse CurrentHead from + // AtEntry, since that would make the value live for the entire function. + Instruction *EntryNextPtr2 = CreateGEP(*AtExit, StackEntry, 0, 0, + "gc_frame.next"); + Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead"); + AtExit->CreateStore(SavedHead, Head); + } + + // Delete the original allocas (which are no longer used) and the intrinsic + // calls (which are no longer valid). Doing this last avoids invalidating + // iterators. + for (unsigned I = 0, E = Roots.size(); I != E; ++I) { + Roots[I].first->eraseFromParent(); + Roots[I].second->eraseFromParent(); + } + + Roots.clear(); + return true; +} -- cgit v1.2.3