clang-format all the GC related files (NFC)

Nothing interesting here...




git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226342 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Philip Reames 2015-01-16 23:16:12 +00:00
parent 61bd005d1b
commit 999412767a
14 changed files with 583 additions and 597 deletions

View File

@ -41,161 +41,159 @@
#include <memory> #include <memory>
namespace llvm { namespace llvm {
class AsmPrinter; class AsmPrinter;
class Constant; class Constant;
class MCSymbol; class MCSymbol;
/// GCPoint - Metadata for a collector-safe point in machine code. /// GCPoint - Metadata for a collector-safe point in machine code.
///
struct GCPoint {
GC::PointKind Kind; ///< The kind of the safe point.
MCSymbol *Label; ///< A label.
DebugLoc Loc;
GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL)
: Kind(K), Label(L), Loc(DL) {}
};
/// 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.
const Constant *Metadata; ///< Metadata straight from the call
///< to llvm.gcroot.
GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {}
};
/// Garbage collection metadata for a single function. Currently, this
/// information only applies to GCStrategies which use GCRoot.
class GCFunctionInfo {
public:
typedef std::vector<GCPoint>::iterator iterator;
typedef std::vector<GCRoot>::iterator roots_iterator;
typedef std::vector<GCRoot>::const_iterator live_iterator;
private:
const Function &F;
GCStrategy &S;
uint64_t FrameSize;
std::vector<GCRoot> Roots;
std::vector<GCPoint> 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:
GCFunctionInfo(const Function &F, GCStrategy &S);
~GCFunctionInfo();
/// getFunction - Return the function to which this metadata applies.
/// ///
struct GCPoint { const Function &getFunction() const { return F; }
GC::PointKind Kind; ///< The kind of the safe point.
MCSymbol *Label; ///< A label.
DebugLoc Loc;
GCPoint(GC::PointKind K, MCSymbol *L, DebugLoc DL) /// getStrategy - Return the GC strategy for the function.
: Kind(K), Label(L), Loc(DL) {} ///
}; GCStrategy &getStrategy() { return S; }
/// GCRoot - Metadata for a pointer to an object managed by the garbage /// addStackRoot - Registers a root that lives on the stack. Num is the
/// collector. /// stack object ID for the alloca (if the code generator is
struct GCRoot { // using MachineFrameInfo).
int Num; ///< Usually a frame index. void addStackRoot(int Num, const Constant *Metadata) {
int StackOffset; ///< Offset from the stack pointer. Roots.push_back(GCRoot(Num, Metadata));
const Constant *Metadata; ///< Metadata straight from the call }
///< to llvm.gcroot.
GCRoot(int N, const Constant *MD) : Num(N), StackOffset(-1), Metadata(MD) {} /// removeStackRoot - Removes a root.
}; roots_iterator removeStackRoot(roots_iterator position) {
return Roots.erase(position);
}
/// 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, MCSymbol *Label, DebugLoc DL) {
SafePoints.push_back(GCPoint(Kind, Label, DL));
}
/// Garbage collection metadata for a single function. Currently, this /// getFrameSize/setFrameSize - Records the function's frame size.
/// information only applies to GCStrategies which use GCRoot. ///
class GCFunctionInfo { uint64_t getFrameSize() const { return FrameSize; }
public: void setFrameSize(uint64_t S) { FrameSize = S; }
typedef std::vector<GCPoint>::iterator iterator;
typedef std::vector<GCRoot>::iterator roots_iterator;
typedef std::vector<GCRoot>::const_iterator live_iterator;
private: /// begin/end - Iterators for safe points.
const Function &F; ///
GCStrategy &S; iterator begin() { return SafePoints.begin(); }
uint64_t FrameSize; iterator end() { return SafePoints.end(); }
std::vector<GCRoot> Roots; size_t size() const { return SafePoints.size(); }
std::vector<GCPoint> SafePoints;
// FIXME: Liveness. A 2D BitVector, perhaps? /// roots_begin/roots_end - Iterators for all roots in the function.
// ///
// BitVector Liveness; roots_iterator roots_begin() { return Roots.begin(); }
// roots_iterator roots_end() { return Roots.end(); }
// bool islive(int point, int root) = size_t roots_size() const { return Roots.size(); }
// 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: /// live_begin/live_end - Iterators for live roots at a given safe point.
GCFunctionInfo(const Function &F, GCStrategy &S); ///
~GCFunctionInfo(); 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(); }
};
/// getFunction - Return the function to which this metadata applies. /// An analysis pass which caches information about the entire Module.
/// /// Records both the function level information used by GCRoots and a
const Function &getFunction() const { return F; } /// cache of the 'active' gc strategy objects for the current Module.
class GCModuleInfo : public ImmutablePass {
/// A list of GCStrategies which are active in this Module. These are
/// not owning pointers.
std::vector<GCStrategy *> StrategyList;
/// getStrategy - Return the GC strategy for the function. public:
/// /// List of per function info objects. In theory, Each of these
GCStrategy &getStrategy() { return S; } /// may be associated with a different GC.
typedef std::vector<std::unique_ptr<GCFunctionInfo>> FuncInfoVec;
/// addStackRoot - Registers a root that lives on the stack. Num is the FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
/// stack object ID for the alloca (if the code generator is FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
// using MachineFrameInfo).
void addStackRoot(int Num, const Constant *Metadata) {
Roots.push_back(GCRoot(Num, Metadata));
}
/// removeStackRoot - Removes a root. private:
roots_iterator removeStackRoot(roots_iterator position) { /// Owning list of all GCFunctionInfos associated with this Module
return Roots.erase(position); FuncInfoVec Functions;
}
/// addSafePoint - Notes the existence of a safe point. Num is the ID of the /// Non-owning map to bypass linear search when finding the GCFunctionInfo
/// label just prior to the safe point (if the code generator is using /// associated with a particular Function.
/// MachineModuleInfo). typedef DenseMap<const Function *, GCFunctionInfo *> finfo_map_type;
void addSafePoint(GC::PointKind Kind, MCSymbol *Label, DebugLoc DL) { finfo_map_type FInfoMap;
SafePoints.push_back(GCPoint(Kind, Label, DL));
}
/// getFrameSize/setFrameSize - Records the function's frame size. public:
/// typedef std::vector<GCStrategy *>::const_iterator iterator;
uint64_t getFrameSize() const { return FrameSize; }
void setFrameSize(uint64_t S) { FrameSize = S; }
/// begin/end - Iterators for safe points. static char ID;
///
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. GCModuleInfo();
///
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. /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
/// /// call it in doFinalization().
live_iterator live_begin(const iterator &p) { return roots_begin(); } ///
live_iterator live_end (const iterator &p) { return roots_end(); } void clear();
size_t live_size(const iterator &p) const { return roots_size(); }
};
/// An analysis pass which caches information about the entire Module. /// begin/end - Iterators for used strategies.
/// Records both the function level information used by GCRoots and a ///
/// cache of the 'active' gc strategy objects for the current Module. iterator begin() const { return StrategyList.begin(); }
class GCModuleInfo : public ImmutablePass { iterator end() const { return StrategyList.end(); }
/// A list of GCStrategies which are active in this Module. These are
/// not owning pointers.
std::vector<GCStrategy*> StrategyList;
public:
/// List of per function info objects. In theory, Each of these
/// may be associated with a different GC.
typedef std::vector<std::unique_ptr<GCFunctionInfo>> FuncInfoVec;
FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
private:
/// Owning list of all GCFunctionInfos associated with this Module
FuncInfoVec Functions;
/// Non-owning map to bypass linear search when finding the GCFunctionInfo
/// associated with a particular Function.
typedef DenseMap<const Function*,GCFunctionInfo*> finfo_map_type;
finfo_map_type FInfoMap;
public:
typedef std::vector<GCStrategy*>::const_iterator iterator;
static char ID;
GCModuleInfo();
/// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
/// call it in doFinalization().
///
void clear();
/// begin/end - Iterators for used strategies.
///
iterator begin() const { return StrategyList.begin(); }
iterator end() const { return StrategyList.end(); }
/// get - Look up function metadata. This is currently assumed
/// have the side effect of initializing the associated GCStrategy. That
/// will soon change.
GCFunctionInfo &getFunctionInfo(const Function &F);
};
/// get - Look up function metadata. This is currently assumed
/// have the side effect of initializing the associated GCStrategy. That
/// will soon change.
GCFunctionInfo &getFunctionInfo(const Function &F);
};
} }
#endif #endif

View File

@ -26,43 +26,39 @@
namespace llvm { namespace llvm {
class GCMetadataPrinter; class GCMetadataPrinter;
/// GCMetadataPrinterRegistry - The GC assembly printer registry uses all the /// GCMetadataPrinterRegistry - The GC assembly printer registry uses all the
/// defaults from Registry. /// defaults from Registry.
typedef Registry<GCMetadataPrinter> GCMetadataPrinterRegistry; typedef Registry<GCMetadataPrinter> GCMetadataPrinterRegistry;
/// GCMetadataPrinter - Emits GC metadata as assembly code. Instances are /// GCMetadataPrinter - Emits GC metadata as assembly code. Instances are
/// created, managed, and owned by the AsmPrinter. /// created, managed, and owned by the AsmPrinter.
class GCMetadataPrinter { class GCMetadataPrinter {
private: private:
GCStrategy *S; GCStrategy *S;
friend class AsmPrinter; friend class AsmPrinter;
protected: protected:
// May only be subclassed. // May only be subclassed.
GCMetadataPrinter(); GCMetadataPrinter();
private: private:
GCMetadataPrinter(const GCMetadataPrinter &) LLVM_DELETED_FUNCTION; GCMetadataPrinter(const GCMetadataPrinter &) LLVM_DELETED_FUNCTION;
GCMetadataPrinter & GCMetadataPrinter &operator=(const GCMetadataPrinter &) LLVM_DELETED_FUNCTION;
operator=(const GCMetadataPrinter &) LLVM_DELETED_FUNCTION;
public: public:
GCStrategy &getStrategy() { return *S; } GCStrategy &getStrategy() { return *S; }
/// Called before the assembly for the module is generated by /// Called before the assembly for the module is generated by
/// the AsmPrinter (but after target specific hooks.) /// the AsmPrinter (but after target specific hooks.)
virtual void beginAssembly(Module &M, GCModuleInfo &Info, virtual void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) {}
AsmPrinter &AP) {} /// Called after the assembly for the module is generated by
/// Called after the assembly for the module is generated by /// the AsmPrinter (but before target specific hooks)
/// the AsmPrinter (but before target specific hooks) virtual void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) {}
virtual void finishAssembly(Module &M, GCModuleInfo &Info,
AsmPrinter &AP) {}
virtual ~GCMetadataPrinter();
};
virtual ~GCMetadataPrinter();
};
} }
#endif #endif

View File

@ -15,29 +15,29 @@
#define LLVM_CODEGEN_GCS_H #define LLVM_CODEGEN_GCS_H
namespace llvm { namespace llvm {
class GCStrategy; class GCStrategy;
class GCMetadataPrinter; class GCMetadataPrinter;
/// FIXME: Collector instances are not useful on their own. These no longer /// FIXME: Collector instances are not useful on their own. These no longer
/// serve any purpose except to link in the plugins. /// serve any purpose except to link in the plugins.
/// Creates an ocaml-compatible garbage collector. /// Creates an ocaml-compatible garbage collector.
void linkOcamlGC(); void linkOcamlGC();
/// Creates an ocaml-compatible metadata printer. /// Creates an ocaml-compatible metadata printer.
void linkOcamlGCPrinter(); void linkOcamlGCPrinter();
/// Creates an erlang-compatible garbage collector. /// Creates an erlang-compatible garbage collector.
void linkErlangGC(); void linkErlangGC();
/// Creates an erlang-compatible metadata printer. /// Creates an erlang-compatible metadata printer.
void linkErlangGCPrinter(); void linkErlangGCPrinter();
/// Creates a shadow stack garbage collector. This collector requires no code /// Creates a shadow stack garbage collector. This collector requires no code
/// generator support. /// generator support.
void linkShadowStackGC(); void linkShadowStackGC();
void linkStatepointExampleGC(); void linkStatepointExampleGC();
} }
#endif #endif

View File

@ -16,11 +16,11 @@
// GCStrategy is relevant for implementations using either gc.root or // GCStrategy is relevant for implementations using either gc.root or
// gc.statepoint based lowering strategies, but is currently focused mostly on // gc.statepoint based lowering strategies, but is currently focused mostly on
// options for gc.root. This will change over time. // options for gc.root. This will change over time.
// //
// When requested by a subclass of GCStrategy, the gc.root implementation will // When requested by a subclass of GCStrategy, the gc.root implementation will
// populate GCModuleInfo and GCFunctionInfo with that about each Function in // populate GCModuleInfo and GCFunctionInfo with that about each Function in
// the Module that opts in to garbage collection. Specifically: // the Module that opts in to garbage collection. Specifically:
// //
// - Safe points // - Safe points
// Garbage collection is generally only possible at certain points in code. // Garbage collection is generally only possible at certain points in code.
// GCStrategy can request that the collector insert such points: // GCStrategy can request that the collector insert such points:
@ -28,7 +28,7 @@
// - At and after any call to a subroutine // - At and after any call to a subroutine
// - Before returning from the current function // - Before returning from the current function
// - Before backwards branches (loops) // - Before backwards branches (loops)
// //
// - Roots // - Roots
// When a reference to a GC-allocated object exists on the stack, it must be // When a reference to a GC-allocated object exists on the stack, it must be
// stored in an alloca registered with llvm.gcoot. // stored in an alloca registered with llvm.gcoot.
@ -42,7 +42,7 @@
// insertion support is planned. gc.statepoint does not currently support // insertion support is planned. gc.statepoint does not currently support
// custom stack map formats; such can be generated by parsing the standard // custom stack map formats; such can be generated by parsing the standard
// stack map section if desired. // stack map section if desired.
// //
// The read and write barrier support can be used with either implementation. // The read and write barrier support can be used with either implementation.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -59,138 +59,135 @@
#include <string> #include <string>
namespace llvm { namespace llvm {
namespace GC { namespace GC {
/// PointKind - The type of a collector-safe point. /// PointKind - The type of a collector-safe point.
/// ///
enum PointKind { enum PointKind {
Loop, ///< Instr is a loop (backwards branch). Loop, ///< Instr is a loop (backwards branch).
Return, ///< Instr is a return instruction. Return, ///< Instr is a return instruction.
PreCall, ///< Instr is a call instruction. PreCall, ///< Instr is a call instruction.
PostCall ///< Instr is the return address of a call. PostCall ///< Instr is the return address of a call.
}; };
}
/// GCStrategy describes a garbage collector algorithm's code generation
/// requirements, and provides overridable hooks for those needs which cannot
/// be abstractly described. GCStrategy objects must be looked up through
/// the Function. The objects themselves are owned by the Context and must
/// be immutable.
class GCStrategy {
private:
std::string Name;
friend class LLVMContextImpl;
protected:
bool UseStatepoints; /// Uses gc.statepoints as opposed to gc.roots,
/// if set, none of the other options can be
/// anything but their default values.
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:
GCStrategy();
virtual ~GCStrategy() {}
/// Return the name of the GC strategy. This is the value of the collector
/// name string specified on functions which use this strategy.
const std::string &getName() const { return Name; }
/// By default, write barriers are replaced with simple store
/// instructions. If true, then performCustomLowering must instead lower
/// them.
bool customWriteBarrier() const { return CustomWriteBarriers; }
/// By default, read barriers are replaced with simple load
/// instructions. If true, then performCustomLowering must instead lower
/// them.
bool customReadBarrier() const { return CustomReadBarriers; }
/// Returns true if this strategy is expecting the use of gc.statepoints,
/// and false otherwise.
bool useStatepoints() const { return UseStatepoints; }
/** @name Statepoint Specific Properties */
///@{
/// If the value specified can be reliably distinguished, returns true for
/// pointers to GC managed locations and false for pointers to non-GC
/// managed locations. Note a GCStrategy can always return 'None' (i.e. an
/// empty optional indicating it can't reliably distinguish.
virtual Optional<bool> isGCManagedPointer(const Value *V) const {
return None;
}
///@}
/** @name GCRoot Specific Properties
* These properties and overrides only apply to collector strategies using
* GCRoot.
*/
///@{
/// True if safe points of any kind are required. By default, none are
/// recorded.
bool needsSafePoints() const { return NeededSafePoints != 0; }
/// True if the given kind of safe point is required. By default, none are
/// recorded.
bool needsSafePoint(GC::PointKind Kind) const {
return (NeededSafePoints & 1 << Kind) != 0;
} }
/// By default, roots are left for the code generator so it can generate a
/// GCStrategy describes a garbage collector algorithm's code generation /// stack map. If true, then performCustomLowering must delete them.
/// requirements, and provides overridable hooks for those needs which cannot bool customRoots() const { return CustomRoots; }
/// be abstractly described. GCStrategy objects must be looked up through
/// the Function. The objects themselves are owned by the Context and must
/// be immutable.
class GCStrategy {
private:
std::string Name;
friend class LLVMContextImpl;
protected:
bool UseStatepoints; /// Uses gc.statepoints as opposed to gc.roots,
/// if set, none of the other options can be
/// anything but their default values.
unsigned NeededSafePoints; ///< Bitmask of required safe points. /// If set, gcroot intrinsics should initialize their allocas to null
bool CustomReadBarriers; ///< Default is to insert loads. /// before the first use. This is necessary for most GCs and is enabled by
bool CustomWriteBarriers; ///< Default is to insert stores. /// default.
bool CustomRoots; ///< Default is to pass through to backend. bool initializeRoots() const { return InitRoots; }
bool InitRoots; ///< If set, roots are nulled during lowering.
bool UsesMetadata; ///< If set, backend must emit metadata tables.
public:
GCStrategy();
virtual ~GCStrategy() {}
/// Return the name of the GC strategy. This is the value of the collector
/// name string specified on functions which use this strategy.
const std::string &getName() const { return Name; }
/// By default, write barriers are replaced with simple store /// If set, appropriate metadata tables must be emitted by the back-end
/// instructions. If true, then performCustomLowering must instead lower /// (assembler, JIT, or otherwise). For statepoint, this method is
/// them. /// currently unsupported. The stackmap information can be found in the
bool customWriteBarrier() const { return CustomWriteBarriers; } /// StackMap section as described in the documentation.
bool usesMetadata() const { return UsesMetadata; }
/// By default, read barriers are replaced with simple load
/// instructions. If true, then performCustomLowering must instead lower
/// them.
bool customReadBarrier() const { return CustomReadBarriers; }
/// Returns true if this strategy is expecting the use of gc.statepoints, ///@}
/// and false otherwise.
bool useStatepoints() const { return UseStatepoints; }
/** @name Statepoint Specific Properties */ /// initializeCustomLowering/performCustomLowering - If any of the actions
///@{ /// are set to custom, performCustomLowering must be overriden to transform
/// the corresponding actions to LLVM IR. initializeCustomLowering is
/// optional to override. These are the only GCStrategy methods through
/// which the LLVM IR can be modified. These methods apply mostly to
/// gc.root based implementations, but can be overriden to provide custom
/// barrier lowerings with gc.statepoint as well.
///@{
virtual bool initializeCustomLowering(Module &F) {
// No changes made
return false;
}
virtual bool performCustomLowering(Function &F) {
llvm_unreachable("GCStrategy subclass specified a configuration which"
"requires a custom lowering without providing one");
}
};
/// If the value specified can be reliably distinguished, returns true for /// Subclasses of GCStrategy are made available for use during compilation by
/// pointers to GC managed locations and false for pointers to non-GC /// adding them to the global GCRegistry. This can done either within the
/// managed locations. Note a GCStrategy can always return 'None' (i.e. an /// LLVM source tree or via a loadable plugin. An example registeration
/// empty optional indicating it can't reliably distinguish. /// would be:
virtual Optional<bool> isGCManagedPointer(const Value *V) const { /// static GCRegistry::Add<CustomGC> X("custom-name",
return None; /// "my custom supper fancy gc strategy");
} ///
///@} /// Note that to use a custom GCMetadataPrinter w/gc.roots, you must also
/// register your GCMetadataPrinter subclass with the
/** @name GCRoot Specific Properties /// GCMetadataPrinterRegistery as well.
* These properties and overrides only apply to collector strategies using typedef Registry<GCStrategy> GCRegistry;
* GCRoot.
*/
///@{
/// True if safe points of any kind are required. By default, none are
/// recorded.
bool needsSafePoints() const {
return NeededSafePoints != 0;
}
/// True if the given kind of safe point is required. By default, none are
/// recorded.
bool needsSafePoint(GC::PointKind Kind) const {
return (NeededSafePoints & 1 << Kind) != 0;
}
/// By default, roots are left for the code generator so it can generate a
/// stack map. If true, then performCustomLowering must delete them.
bool customRoots() const { return CustomRoots; }
/// If set, gcroot intrinsics should initialize their allocas to null
/// before the first use. This is necessary for most GCs and is enabled by
/// default.
bool initializeRoots() const { return InitRoots; }
/// If set, appropriate metadata tables must be emitted by the back-end
/// (assembler, JIT, or otherwise). For statepoint, this method is
/// currently unsupported. The stackmap information can be found in the
/// StackMap section as described in the documentation.
bool usesMetadata() const { return UsesMetadata; }
///@}
/// initializeCustomLowering/performCustomLowering - If any of the actions
/// are set to custom, performCustomLowering must be overriden to transform
/// the corresponding actions to LLVM IR. initializeCustomLowering is
/// optional to override. These are the only GCStrategy methods through
/// which the LLVM IR can be modified. These methods apply mostly to
/// gc.root based implementations, but can be overriden to provide custom
/// barrier lowerings with gc.statepoint as well.
///@{
virtual bool initializeCustomLowering(Module &F) {
// No changes made
return false;
}
virtual bool performCustomLowering(Function &F) {
llvm_unreachable("GCStrategy subclass specified a configuration which"
"requires a custom lowering without providing one");
}
};
/// Subclasses of GCStrategy are made available for use during compilation by
/// adding them to the global GCRegistry. This can done either within the
/// LLVM source tree or via a loadable plugin. An example registeration
/// would be:
/// static GCRegistry::Add<CustomGC> X("custom-name",
/// "my custom supper fancy gc strategy");
///
/// Note that to use a custom GCMetadataPrinter w/gc.roots, you must also
/// register your GCMetadataPrinter subclass with the
/// GCMetadataPrinterRegistery as well.
typedef Registry<GCStrategy> GCRegistry;
} }
#endif #endif

View File

@ -34,18 +34,16 @@ using namespace llvm;
namespace { namespace {
class ErlangGCPrinter : public GCMetadataPrinter { class ErlangGCPrinter : public GCMetadataPrinter {
public: public:
void finishAssembly(Module &M, GCModuleInfo &Info, void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
AsmPrinter &AP) override; };
};
} }
static GCMetadataPrinterRegistry::Add<ErlangGCPrinter> static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
X("erlang", "erlang-compatible garbage collector"); X("erlang", "erlang-compatible garbage collector");
void llvm::linkErlangGCPrinter() { } void llvm::linkErlangGCPrinter() {}
void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info, void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
AsmPrinter &AP) { AsmPrinter &AP) {
@ -54,13 +52,13 @@ void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize(); AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
// Put this in a custom .note section. // Put this in a custom .note section.
AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getContext() AP.OutStreamer.SwitchSection(
.getELFSection(".note.gc", ELF::SHT_PROGBITS, 0, AP.getObjFileLowering().getContext().getELFSection(
SectionKind::getDataRel())); ".note.gc", ELF::SHT_PROGBITS, 0, SectionKind::getDataRel()));
// For each function... // For each function...
for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(), for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(),
IE = Info.funcinfo_end(); IE = Info.funcinfo_end();
FI != IE; ++FI) { FI != IE; ++FI) {
GCFunctionInfo &MD = **FI; GCFunctionInfo &MD = **FI;
if (MD.getStrategy().getName() != getStrategy().getName()) if (MD.getStrategy().getName() != getStrategy().getName())
@ -91,7 +89,7 @@ void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
// Emit the address of the safe point. // Emit the address of the safe point.
OS.AddComment("safe point address"); OS.AddComment("safe point address");
MCSymbol *Label = PI->Label; MCSymbol *Label = PI->Label;
AP.EmitLabelPlusOffset(Label/*Hi*/, 0/*Offset*/, 4/*Size*/); AP.EmitLabelPlusOffset(Label /*Hi*/, 0 /*Offset*/, 4 /*Size*/);
} }
// Stack information never change in safe points! Only print info from the // Stack information never change in safe points! Only print info from the
@ -104,8 +102,9 @@ void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
// Emit stack arity, i.e. the number of stacked arguments. // Emit stack arity, i.e. the number of stacked arguments.
unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6; unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs ? unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs
MD.getFunction().arg_size() - RegisteredArgs : 0; ? MD.getFunction().arg_size() - RegisteredArgs
: 0;
OS.AddComment("stack arity"); OS.AddComment("stack arity");
AP.EmitInt16(StackArity); AP.EmitInt16(StackArity);
@ -116,7 +115,7 @@ void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
// And for each live root... // And for each live root...
for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI), for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
LE = MD.live_end(PI); LE = MD.live_end(PI);
LI != LE; ++LI) { LI != LE; ++LI) {
// Emit live root's offset within the stack frame. // Emit live root's offset within the stack frame.
OS.AddComment("stack index (offset / wordsize)"); OS.AddComment("stack index (offset / wordsize)");
AP.EmitInt16(LI->StackOffset / IntPtrSize); AP.EmitInt16(LI->StackOffset / IntPtrSize);

View File

@ -32,20 +32,17 @@ using namespace llvm;
namespace { namespace {
class OcamlGCMetadataPrinter : public GCMetadataPrinter { class OcamlGCMetadataPrinter : public GCMetadataPrinter {
public: public:
void beginAssembly(Module &M, GCModuleInfo &Info, void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
AsmPrinter &AP) override; void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
void finishAssembly(Module &M, GCModuleInfo &Info, };
AsmPrinter &AP) override;
};
} }
static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter> static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
Y("ocaml", "ocaml 3.10-compatible collector"); Y("ocaml", "ocaml 3.10-compatible collector");
void llvm::linkOcamlGCPrinter() { } void llvm::linkOcamlGCPrinter() {}
static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) { static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
const std::string &MId = M.getModuleIdentifier(); const std::string &MId = M.getModuleIdentifier();
@ -113,7 +110,8 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
int NumDescriptors = 0; int NumDescriptors = 0;
for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(), for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
IE = Info.funcinfo_end(); I != IE; ++I) { IE = Info.funcinfo_end();
I != IE; ++I) {
GCFunctionInfo &FI = **I; GCFunctionInfo &FI = **I;
if (FI.getStrategy().getName() != getStrategy().getName()) if (FI.getStrategy().getName() != getStrategy().getName())
// this function is managed by some other GC // this function is managed by some other GC
@ -123,7 +121,7 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
} }
} }
if (NumDescriptors >= 1<<16) { if (NumDescriptors >= 1 << 16) {
// Very rude! // Very rude!
report_fatal_error(" Too much descriptor for ocaml GC"); report_fatal_error(" Too much descriptor for ocaml GC");
} }
@ -131,19 +129,22 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3); AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(), for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
IE = Info.funcinfo_end(); I != IE; ++I) { IE = Info.funcinfo_end();
I != IE; ++I) {
GCFunctionInfo &FI = **I; GCFunctionInfo &FI = **I;
if (FI.getStrategy().getName() != getStrategy().getName()) if (FI.getStrategy().getName() != getStrategy().getName())
// this function is managed by some other GC // this function is managed by some other GC
continue; continue;
uint64_t FrameSize = FI.getFrameSize(); uint64_t FrameSize = FI.getFrameSize();
if (FrameSize >= 1<<16) { if (FrameSize >= 1 << 16) {
// Very rude! // Very rude!
report_fatal_error("Function '" + FI.getFunction().getName() + report_fatal_error("Function '" + FI.getFunction().getName() +
"' is too large for the ocaml GC! " "' is too large for the ocaml GC! "
"Frame size " + Twine(FrameSize) + ">= 65536.\n" "Frame size " +
"(" + Twine(uintptr_t(&FI)) + ")"); Twine(FrameSize) + ">= 65536.\n"
"(" +
Twine(uintptr_t(&FI)) + ")");
} }
AP.OutStreamer.AddComment("live roots for " + AP.OutStreamer.AddComment("live roots for " +
@ -152,11 +153,12 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) { for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
size_t LiveCount = FI.live_size(J); size_t LiveCount = FI.live_size(J);
if (LiveCount >= 1<<16) { if (LiveCount >= 1 << 16) {
// Very rude! // Very rude!
report_fatal_error("Function '" + FI.getFunction().getName() + report_fatal_error("Function '" + FI.getFunction().getName() +
"' is too large for the ocaml GC! " "' is too large for the ocaml GC! "
"Live root count "+Twine(LiveCount)+" >= 65536."); "Live root count " +
Twine(LiveCount) + " >= 65536.");
} }
AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize); AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize);
@ -164,12 +166,13 @@ void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
AP.EmitInt16(LiveCount); AP.EmitInt16(LiveCount);
for (GCFunctionInfo::live_iterator K = FI.live_begin(J), for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
KE = FI.live_end(J); K != KE; ++K) { KE = FI.live_end(J);
if (K->StackOffset >= 1<<16) { K != KE; ++K) {
if (K->StackOffset >= 1 << 16) {
// Very rude! // Very rude!
report_fatal_error( report_fatal_error(
"GC root stack offset is outside of fixed stack frame and out " "GC root stack offset is outside of fixed stack frame and out "
"of range for ocaml GC!"); "of range for ocaml GC!");
} }
AP.EmitInt16(K->StackOffset); AP.EmitInt16(K->StackOffset);
} }

View File

@ -27,17 +27,16 @@ using namespace llvm;
namespace { namespace {
class ErlangGC : public GCStrategy { class ErlangGC : public GCStrategy {
public: public:
ErlangGC(); ErlangGC();
}; };
} }
static GCRegistry::Add<ErlangGC> static GCRegistry::Add<ErlangGC> X("erlang",
X("erlang", "erlang-compatible garbage collector"); "erlang-compatible garbage collector");
void llvm::linkErlangGC() { } void llvm::linkErlangGC() {}
ErlangGC::ErlangGC() { ErlangGC::ErlangGC() {
InitRoots = false; InitRoots = false;

View File

@ -24,22 +24,20 @@
using namespace llvm; using namespace llvm;
namespace { namespace {
class Printer : public FunctionPass {
static char ID;
raw_ostream &OS;
public:
explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
class Printer : public FunctionPass {
static char ID;
raw_ostream &OS;
const char *getPassName() const override; public:
void getAnalysisUsage(AnalysisUsage &AU) const override; explicit Printer(raw_ostream &OS) : FunctionPass(ID), OS(OS) {}
bool runOnFunction(Function &F) override; const char *getPassName() const override;
bool doFinalization(Module &M) override; void getAnalysisUsage(AnalysisUsage &AU) const override;
};
bool runOnFunction(Function &F) override;
bool doFinalization(Module &M) override;
};
} }
INITIALIZE_PASS(GCModuleInfo, "collector-metadata", INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
@ -48,7 +46,7 @@ INITIALIZE_PASS(GCModuleInfo, "collector-metadata",
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S) GCFunctionInfo::GCFunctionInfo(const Function &F, GCStrategy &S)
: F(F), S(S), FrameSize(~0LL) {} : F(F), S(S), FrameSize(~0LL) {}
GCFunctionInfo::~GCFunctionInfo() {} GCFunctionInfo::~GCFunctionInfo() {}
@ -56,19 +54,18 @@ GCFunctionInfo::~GCFunctionInfo() {}
char GCModuleInfo::ID = 0; char GCModuleInfo::ID = 0;
GCModuleInfo::GCModuleInfo() GCModuleInfo::GCModuleInfo() : ImmutablePass(ID) {
: ImmutablePass(ID) {
initializeGCModuleInfoPass(*PassRegistry::getPassRegistry()); initializeGCModuleInfoPass(*PassRegistry::getPassRegistry());
} }
GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) { GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!"); assert(!F.isDeclaration() && "Can only get GCFunctionInfo for a definition!");
assert(F.hasGC()); assert(F.hasGC());
finfo_map_type::iterator I = FInfoMap.find(&F); finfo_map_type::iterator I = FInfoMap.find(&F);
if (I != FInfoMap.end()) if (I != FInfoMap.end())
return *I->second; return *I->second;
GCStrategy *S = F.getGCStrategy(); GCStrategy *S = F.getGCStrategy();
if (!S) { if (!S) {
std::string error = std::string("unsupported GC: ") + F.getGC(); std::string error = std::string("unsupported GC: ") + F.getGC();
@ -76,7 +73,7 @@ GCFunctionInfo &GCModuleInfo::getFunctionInfo(const Function &F) {
} }
// Save the fact this strategy is associated with this module. Note that // Save the fact this strategy is associated with this module. Note that
// these are non-owning references, the GCStrategy remains owned by the // these are non-owning references, the GCStrategy remains owned by the
// Context. // Context.
StrategyList.push_back(S); StrategyList.push_back(S);
Functions.push_back(make_unique<GCFunctionInfo>(F, *S)); Functions.push_back(make_unique<GCFunctionInfo>(F, *S));
GCFunctionInfo *GFI = Functions.back().get(); GCFunctionInfo *GFI = Functions.back().get();
@ -98,7 +95,6 @@ FunctionPass *llvm::createGCInfoPrinter(raw_ostream &OS) {
return new Printer(OS); return new Printer(OS);
} }
const char *Printer::getPassName() const { const char *Printer::getPassName() const {
return "Print Garbage Collector Information"; return "Print Garbage Collector Information";
} }
@ -111,42 +107,49 @@ void Printer::getAnalysisUsage(AnalysisUsage &AU) const {
static const char *DescKind(GC::PointKind Kind) { static const char *DescKind(GC::PointKind Kind) {
switch (Kind) { switch (Kind) {
case GC::Loop: return "loop"; case GC::Loop:
case GC::Return: return "return"; return "loop";
case GC::PreCall: return "pre-call"; case GC::Return:
case GC::PostCall: return "post-call"; return "return";
case GC::PreCall:
return "pre-call";
case GC::PostCall:
return "post-call";
} }
llvm_unreachable("Invalid point kind"); llvm_unreachable("Invalid point kind");
} }
bool Printer::runOnFunction(Function &F) { bool Printer::runOnFunction(Function &F) {
if (F.hasGC()) return false; if (F.hasGC())
return false;
GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F); GCFunctionInfo *FD = &getAnalysis<GCModuleInfo>().getFunctionInfo(F);
OS << "GC roots for " << FD->getFunction().getName() << ":\n"; OS << "GC roots for " << FD->getFunction().getName() << ":\n";
for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(), for (GCFunctionInfo::roots_iterator RI = FD->roots_begin(),
RE = FD->roots_end(); RI != RE; ++RI) RE = FD->roots_end();
RI != RE; ++RI)
OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n"; OS << "\t" << RI->Num << "\t" << RI->StackOffset << "[sp]\n";
OS << "GC safe points for " << FD->getFunction().getName() << ":\n"; OS << "GC safe points for " << FD->getFunction().getName() << ":\n";
for (GCFunctionInfo::iterator PI = FD->begin(), for (GCFunctionInfo::iterator PI = FD->begin(), PE = FD->end(); PI != PE;
PE = FD->end(); PI != PE; ++PI) { ++PI) {
OS << "\t" << PI->Label->getName() << ": " OS << "\t" << PI->Label->getName() << ": " << DescKind(PI->Kind)
<< DescKind(PI->Kind) << ", live = {"; << ", live = {";
for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI), for (GCFunctionInfo::live_iterator RI = FD->live_begin(PI),
RE = FD->live_end(PI);;) { RE = FD->live_end(PI);
;) {
OS << " " << RI->Num; OS << " " << RI->Num;
if (++RI == RE) if (++RI == RE)
break; break;
OS << ","; OS << ",";
} }
OS << " }\n"; OS << " }\n";
} }
return false; return false;
} }

View File

@ -14,6 +14,6 @@
#include "llvm/CodeGen/GCMetadataPrinter.h" #include "llvm/CodeGen/GCMetadataPrinter.h"
using namespace llvm; using namespace llvm;
GCMetadataPrinter::GCMetadataPrinter() { } GCMetadataPrinter::GCMetadataPrinter() {}
GCMetadataPrinter::~GCMetadataPrinter() { } GCMetadataPrinter::~GCMetadataPrinter() {}

View File

@ -116,8 +116,6 @@ static bool NeedsCustomLoweringPass(const GCStrategy &C) {
return C.customWriteBarrier() || C.customReadBarrier() || C.customRoots(); return C.customWriteBarrier() || C.customReadBarrier() || C.customRoots();
} }
/// doInitialization - If this module uses the GC intrinsics, find them now. /// doInitialization - If this module uses the GC intrinsics, find them now.
bool LowerIntrinsics::doInitialization(Module &M) { bool LowerIntrinsics::doInitialization(Module &M) {
// FIXME: This is rather antisocial in the context of a JIT since it performs // FIXME: This is rather antisocial in the context of a JIT since it performs
@ -139,7 +137,6 @@ bool LowerIntrinsics::doInitialization(Module &M) {
return MadeChange; return MadeChange;
} }
/// CouldBecomeSafePoint - Predicate to conservatively determine whether the /// CouldBecomeSafePoint - Predicate to conservatively determine whether the
/// instruction could introduce a safe point. /// instruction could introduce a safe point.
static bool CouldBecomeSafePoint(Instruction *I) { static bool CouldBecomeSafePoint(Instruction *I) {
@ -199,7 +196,6 @@ static bool InsertRootInitializers(Function &F, AllocaInst **Roots,
return MadeChange; return MadeChange;
} }
/// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores. /// runOnFunction - Replace gcread/gcwrite intrinsics with loads and stores.
/// Leave gcroot intrinsics; the code generator needs to see those. /// Leave gcroot intrinsics; the code generator needs to see those.
bool LowerIntrinsics::runOnFunction(Function &F) { bool LowerIntrinsics::runOnFunction(Function &F) {

View File

@ -20,16 +20,15 @@
using namespace llvm; using namespace llvm;
namespace { namespace {
class OcamlGC : public GCStrategy { class OcamlGC : public GCStrategy {
public: public:
OcamlGC(); OcamlGC();
}; };
} }
static GCRegistry::Add<OcamlGC> static GCRegistry::Add<OcamlGC> X("ocaml", "ocaml 3.10-compatible GC");
X("ocaml", "ocaml 3.10-compatible GC");
void llvm::linkOcamlGC() { } void llvm::linkOcamlGC() {}
OcamlGC::OcamlGC() { OcamlGC::OcamlGC() {
NeededSafePoints = 1 << GC::PostCall; NeededSafePoints = 1 << GC::PostCall;

View File

@ -39,161 +39,156 @@ using namespace llvm;
namespace { namespace {
class ShadowStackGC : public GCStrategy { class ShadowStackGC : public GCStrategy {
/// RootChain - This is the global linked-list that contains the chain of GC /// RootChain - This is the global linked-list that contains the chain of GC
/// roots. /// roots.
GlobalVariable *Head; GlobalVariable *Head;
/// StackEntryTy - Abstract type of a link in the shadow stack. /// StackEntryTy - Abstract type of a link in the shadow stack.
/// ///
StructType *StackEntryTy; StructType *StackEntryTy;
StructType *FrameMapTy; StructType *FrameMapTy;
/// Roots - GC roots in the current function. Each is a pair of the /// Roots - GC roots in the current function. Each is a pair of the
/// intrinsic call and its corresponding alloca. /// intrinsic call and its corresponding alloca.
std::vector<std::pair<CallInst*,AllocaInst*> > Roots; std::vector<std::pair<CallInst *, AllocaInst *>> Roots;
public: public:
ShadowStackGC(); ShadowStackGC();
bool initializeCustomLowering(Module &M) override; bool initializeCustomLowering(Module &M) override;
bool performCustomLowering(Function &F) override; bool performCustomLowering(Function &F) override;
private:
bool IsNullValue(Value *V);
Constant *GetFrameMap(Function &F);
Type* GetConcreteStackEntryType(Function &F);
void CollectRoots(Function &F);
static GetElementPtrInst *CreateGEP(LLVMContext &Context,
IRBuilder<> &B, Value *BasePtr,
int Idx1, const char *Name);
static GetElementPtrInst *CreateGEP(LLVMContext &Context,
IRBuilder<> &B, Value *BasePtr,
int Idx1, int Idx2, const char *Name);
};
private:
bool IsNullValue(Value *V);
Constant *GetFrameMap(Function &F);
Type *GetConcreteStackEntryType(Function &F);
void CollectRoots(Function &F);
static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B,
Value *BasePtr, int Idx1,
const char *Name);
static GetElementPtrInst *CreateGEP(LLVMContext &Context, IRBuilder<> &B,
Value *BasePtr, int Idx1, int Idx2,
const char *Name);
};
} }
static GCRegistry::Add<ShadowStackGC> static GCRegistry::Add<ShadowStackGC>
X("shadow-stack", "Very portable GC for uncooperative code generators"); X("shadow-stack", "Very portable GC for uncooperative code generators");
namespace { namespace {
/// EscapeEnumerator - This is a little algorithm to find all escape points /// EscapeEnumerator - This is a little algorithm to find all escape points
/// from a function so that "finally"-style code can be inserted. In addition /// from a function so that "finally"-style code can be inserted. In addition
/// to finding the existing return and unwind instructions, it also (if /// to finding the existing return and unwind instructions, it also (if
/// necessary) transforms any call instructions into invokes and sends them to /// necessary) transforms any call instructions into invokes and sends them to
/// a landing pad. /// a landing pad.
/// ///
/// It's wrapped up in a state machine using the same transform C# uses for /// 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. /// 'yield return' enumerators, This transform allows it to be non-allocating.
class EscapeEnumerator { class EscapeEnumerator {
Function &F; Function &F;
const char *CleanupBBName; const char *CleanupBBName;
// State. // State.
int State; int State;
Function::iterator StateBB, StateE; Function::iterator StateBB, StateE;
IRBuilder<> Builder; IRBuilder<> Builder;
public: public:
EscapeEnumerator(Function &F, const char *N = "cleanup") EscapeEnumerator(Function &F, const char *N = "cleanup")
: F(F), CleanupBBName(N), State(0), Builder(F.getContext()) {} : F(F), CleanupBBName(N), State(0), Builder(F.getContext()) {}
IRBuilder<> *Next() { IRBuilder<> *Next() {
switch (State) { switch (State) {
default: default:
return nullptr; return nullptr;
case 0: case 0:
StateBB = F.begin(); StateBB = F.begin();
StateE = F.end(); StateE = F.end();
State = 1; State = 1;
case 1: case 1:
// Find all 'return', 'resume', and 'unwind' instructions. // Find all 'return', 'resume', and 'unwind' instructions.
while (StateBB != StateE) { while (StateBB != StateE) {
BasicBlock *CurBB = StateBB++; BasicBlock *CurBB = StateBB++;
// Branches and invokes do not escape, only unwind, resume, and return // Branches and invokes do not escape, only unwind, resume, and return
// do. // do.
TerminatorInst *TI = CurBB->getTerminator(); TerminatorInst *TI = CurBB->getTerminator();
if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI)) if (!isa<ReturnInst>(TI) && !isa<ResumeInst>(TI))
continue; continue;
Builder.SetInsertPoint(TI->getParent(), TI); Builder.SetInsertPoint(TI->getParent(), TI);
return &Builder;
}
State = 2;
// Find all 'call' instructions.
SmallVector<Instruction*,16> 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<CallInst>(II))
if (!CI->getCalledFunction() ||
!CI->getCalledFunction()->getIntrinsicID())
Calls.push_back(CI);
if (Calls.empty())
return nullptr;
// Create a cleanup block.
LLVMContext &C = F.getContext();
BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
Type *ExnTy = StructType::get(Type::getInt8PtrTy(C),
Type::getInt32Ty(C), nullptr);
Constant *PersFn =
F.getParent()->
getOrInsertFunction("__gcc_personality_v0",
FunctionType::get(Type::getInt32Ty(C), true));
LandingPadInst *LPad = LandingPadInst::Create(ExnTy, PersFn, 1,
"cleanup.lpad",
CleanupBB);
LPad->setCleanup(true);
ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
// Transform the 'call' instructions into 'invoke's branching to the
// cleanup block. Go in reverse order to make prettier BB names.
SmallVector<Value*,16> Args;
for (unsigned I = Calls.size(); I != 0; ) {
CallInst *CI = cast<CallInst>(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();
CallSite CS(CI);
Args.append(CS.arg_begin(), CS.arg_end());
InvokeInst *II = InvokeInst::Create(CI->getCalledValue(),
NewBB, CleanupBB,
Args, CI->getName(), CallBB);
II->setCallingConv(CI->getCallingConv());
II->setAttributes(CI->getAttributes());
CI->replaceAllUsesWith(II);
delete CI;
}
Builder.SetInsertPoint(RI->getParent(), RI);
return &Builder; return &Builder;
} }
State = 2;
// Find all 'call' instructions.
SmallVector<Instruction *, 16> 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<CallInst>(II))
if (!CI->getCalledFunction() ||
!CI->getCalledFunction()->getIntrinsicID())
Calls.push_back(CI);
if (Calls.empty())
return nullptr;
// Create a cleanup block.
LLVMContext &C = F.getContext();
BasicBlock *CleanupBB = BasicBlock::Create(C, CleanupBBName, &F);
Type *ExnTy =
StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C), nullptr);
Constant *PersFn = F.getParent()->getOrInsertFunction(
"__gcc_personality_v0", FunctionType::get(Type::getInt32Ty(C), true));
LandingPadInst *LPad =
LandingPadInst::Create(ExnTy, PersFn, 1, "cleanup.lpad", CleanupBB);
LPad->setCleanup(true);
ResumeInst *RI = ResumeInst::Create(LPad, CleanupBB);
// Transform the 'call' instructions into 'invoke's branching to the
// cleanup block. Go in reverse order to make prettier BB names.
SmallVector<Value *, 16> Args;
for (unsigned I = Calls.size(); I != 0;) {
CallInst *CI = cast<CallInst>(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();
CallSite CS(CI);
Args.append(CS.arg_begin(), CS.arg_end());
InvokeInst *II =
InvokeInst::Create(CI->getCalledValue(), NewBB, CleanupBB, Args,
CI->getName(), CallBB);
II->setCallingConv(CI->getCallingConv());
II->setAttributes(CI->getAttributes());
CI->replaceAllUsesWith(II);
delete CI;
}
Builder.SetInsertPoint(RI->getParent(), RI);
return &Builder;
} }
}; }
};
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void llvm::linkShadowStackGC() { } void llvm::linkShadowStackGC() {}
ShadowStackGC::ShadowStackGC() : Head(nullptr), StackEntryTy(nullptr) { ShadowStackGC::ShadowStackGC() : Head(nullptr), StackEntryTy(nullptr) {
InitRoots = true; InitRoots = true;
@ -206,7 +201,7 @@ Constant *ShadowStackGC::GetFrameMap(Function &F) {
// Truncate the ShadowStackDescriptor if some metadata is null. // Truncate the ShadowStackDescriptor if some metadata is null.
unsigned NumMeta = 0; unsigned NumMeta = 0;
SmallVector<Constant*, 16> Metadata; SmallVector<Constant *, 16> Metadata;
for (unsigned I = 0; I != Roots.size(); ++I) { for (unsigned I = 0; I != Roots.size(); ++I) {
Constant *C = cast<Constant>(Roots[I].first->getArgOperand(1)); Constant *C = cast<Constant>(Roots[I].first->getArgOperand(1));
if (!C->isNullValue()) if (!C->isNullValue())
@ -216,20 +211,19 @@ Constant *ShadowStackGC::GetFrameMap(Function &F) {
Metadata.resize(NumMeta); Metadata.resize(NumMeta);
Type *Int32Ty = Type::getInt32Ty(F.getContext()); Type *Int32Ty = Type::getInt32Ty(F.getContext());
Constant *BaseElts[] = { Constant *BaseElts[] = {
ConstantInt::get(Int32Ty, Roots.size(), false), ConstantInt::get(Int32Ty, Roots.size(), false),
ConstantInt::get(Int32Ty, NumMeta, false), ConstantInt::get(Int32Ty, NumMeta, false),
}; };
Constant *DescriptorElts[] = { Constant *DescriptorElts[] = {
ConstantStruct::get(FrameMapTy, BaseElts), ConstantStruct::get(FrameMapTy, BaseElts),
ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), Metadata) ConstantArray::get(ArrayType::get(VoidPtr, NumMeta), Metadata)};
};
Type *EltTys[] = {DescriptorElts[0]->getType(), DescriptorElts[1]->getType()};
StructType *STy = StructType::create(EltTys, "gc_map." + utostr(NumMeta));
Type *EltTys[] = { DescriptorElts[0]->getType(),DescriptorElts[1]->getType()};
StructType *STy = StructType::create(EltTys, "gc_map."+utostr(NumMeta));
Constant *FrameMap = ConstantStruct::get(STy, DescriptorElts); Constant *FrameMap = ConstantStruct::get(STy, DescriptorElts);
// FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems // FIXME: Is this actually dangerous as WritingAnLLVMPass.html claims? Seems
@ -246,24 +240,23 @@ Constant *ShadowStackGC::GetFrameMap(Function &F) {
// (which uses a FunctionPassManager (which segfaults (not asserts) if // (which uses a FunctionPassManager (which segfaults (not asserts) if
// provided a ModulePass))). // provided a ModulePass))).
Constant *GV = new GlobalVariable(*F.getParent(), FrameMap->getType(), true, Constant *GV = new GlobalVariable(*F.getParent(), FrameMap->getType(), true,
GlobalVariable::InternalLinkage, GlobalVariable::InternalLinkage, FrameMap,
FrameMap, "__gc_" + F.getName()); "__gc_" + F.getName());
Constant *GEPIndices[2] = { Constant *GEPIndices[2] = {
ConstantInt::get(Type::getInt32Ty(F.getContext()), 0), ConstantInt::get(Type::getInt32Ty(F.getContext()), 0),
ConstantInt::get(Type::getInt32Ty(F.getContext()), 0) ConstantInt::get(Type::getInt32Ty(F.getContext()), 0)};
};
return ConstantExpr::getGetElementPtr(GV, GEPIndices); return ConstantExpr::getGetElementPtr(GV, GEPIndices);
} }
Type* ShadowStackGC::GetConcreteStackEntryType(Function &F) { Type *ShadowStackGC::GetConcreteStackEntryType(Function &F) {
// doInitialization creates the generic version of this type. // doInitialization creates the generic version of this type.
std::vector<Type*> EltTys; std::vector<Type *> EltTys;
EltTys.push_back(StackEntryTy); EltTys.push_back(StackEntryTy);
for (size_t I = 0; I != Roots.size(); I++) for (size_t I = 0; I != Roots.size(); I++)
EltTys.push_back(Roots[I].second->getAllocatedType()); EltTys.push_back(Roots[I].second->getAllocatedType());
return StructType::create(EltTys, "gc_stackentry."+F.getName().str()); return StructType::create(EltTys, "gc_stackentry." + F.getName().str());
} }
/// doInitialization - If this module uses the GC intrinsics, find them now. If /// doInitialization - If this module uses the GC intrinsics, find them now. If
@ -274,10 +267,10 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) {
// int32_t NumMeta; // Number of metadata descriptors. May be < NumRoots. // int32_t NumMeta; // Number of metadata descriptors. May be < NumRoots.
// void *Meta[]; // May be absent for roots without metadata. // void *Meta[]; // May be absent for roots without metadata.
// }; // };
std::vector<Type*> EltTys; std::vector<Type *> EltTys;
// 32 bits is ok up to a 32GB stack frame. :) // 32 bits is ok up to a 32GB stack frame. :)
EltTys.push_back(Type::getInt32Ty(M.getContext())); EltTys.push_back(Type::getInt32Ty(M.getContext()));
// Specifies length of variable length array. // Specifies length of variable length array.
EltTys.push_back(Type::getInt32Ty(M.getContext())); EltTys.push_back(Type::getInt32Ty(M.getContext()));
FrameMapTy = StructType::create(EltTys, "gc_map"); FrameMapTy = StructType::create(EltTys, "gc_map");
PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy); PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy);
@ -287,9 +280,9 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) {
// FrameMap *Map; // Pointer to constant FrameMap. // FrameMap *Map; // Pointer to constant FrameMap.
// void *Roots[]; // Stack roots (in-place array, so we pretend). // void *Roots[]; // Stack roots (in-place array, so we pretend).
// }; // };
StackEntryTy = StructType::create(M.getContext(), "gc_stackentry"); StackEntryTy = StructType::create(M.getContext(), "gc_stackentry");
EltTys.clear(); EltTys.clear();
EltTys.push_back(PointerType::getUnqual(StackEntryTy)); EltTys.push_back(PointerType::getUnqual(StackEntryTy));
EltTys.push_back(FrameMapPtrTy); EltTys.push_back(FrameMapPtrTy);
@ -301,10 +294,9 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) {
if (!Head) { if (!Head) {
// If the root chain does not exist, insert a new one with linkonce // If the root chain does not exist, insert a new one with linkonce
// linkage! // linkage!
Head = new GlobalVariable(M, StackEntryPtrTy, false, Head = new GlobalVariable(
GlobalValue::LinkOnceAnyLinkage, M, StackEntryPtrTy, false, GlobalValue::LinkOnceAnyLinkage,
Constant::getNullValue(StackEntryPtrTy), Constant::getNullValue(StackEntryPtrTy), "llvm_gc_root_chain");
"llvm_gc_root_chain");
} else if (Head->hasExternalLinkage() && Head->isDeclaration()) { } else if (Head->hasExternalLinkage() && Head->isDeclaration()) {
Head->setInitializer(Constant::getNullValue(StackEntryPtrTy)); Head->setInitializer(Constant::getNullValue(StackEntryPtrTy));
Head->setLinkage(GlobalValue::LinkOnceAnyLinkage); Head->setLinkage(GlobalValue::LinkOnceAnyLinkage);
@ -326,15 +318,16 @@ void ShadowStackGC::CollectRoots(Function &F) {
assert(Roots.empty() && "Not cleaned up?"); assert(Roots.empty() && "Not cleaned up?");
SmallVector<std::pair<CallInst*, AllocaInst*>, 16> MetaRoots; SmallVector<std::pair<CallInst *, AllocaInst *>, 16> MetaRoots;
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++))
if (Function *F = CI->getCalledFunction()) if (Function *F = CI->getCalledFunction())
if (F->getIntrinsicID() == Intrinsic::gcroot) { if (F->getIntrinsicID() == Intrinsic::gcroot) {
std::pair<CallInst*, AllocaInst*> Pair = std::make_pair( std::pair<CallInst *, AllocaInst *> Pair = std::make_pair(
CI, cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts())); CI,
cast<AllocaInst>(CI->getArgOperand(0)->stripPointerCasts()));
if (IsNullValue(CI->getArgOperand(1))) if (IsNullValue(CI->getArgOperand(1)))
Roots.push_back(Pair); Roots.push_back(Pair);
else else
@ -346,24 +339,25 @@ void ShadowStackGC::CollectRoots(Function &F) {
Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end()); Roots.insert(Roots.begin(), MetaRoots.begin(), MetaRoots.end());
} }
GetElementPtrInst * GetElementPtrInst *ShadowStackGC::CreateGEP(LLVMContext &Context,
ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr, IRBuilder<> &B, Value *BasePtr,
int Idx, int Idx2, const char *Name) { int Idx, int Idx2,
Value *Indices[] = { ConstantInt::get(Type::getInt32Ty(Context), 0), const char *Name) {
ConstantInt::get(Type::getInt32Ty(Context), Idx), Value *Indices[] = {ConstantInt::get(Type::getInt32Ty(Context), 0),
ConstantInt::get(Type::getInt32Ty(Context), Idx2) }; ConstantInt::get(Type::getInt32Ty(Context), Idx),
Value* Val = B.CreateGEP(BasePtr, Indices, Name); ConstantInt::get(Type::getInt32Ty(Context), Idx2)};
Value *Val = B.CreateGEP(BasePtr, Indices, Name);
assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant"); assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant");
return dyn_cast<GetElementPtrInst>(Val); return dyn_cast<GetElementPtrInst>(Val);
} }
GetElementPtrInst * GetElementPtrInst *ShadowStackGC::CreateGEP(LLVMContext &Context,
ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr, IRBuilder<> &B, Value *BasePtr,
int Idx, const char *Name) { int Idx, const char *Name) {
Value *Indices[] = { ConstantInt::get(Type::getInt32Ty(Context), 0), Value *Indices[] = {ConstantInt::get(Type::getInt32Ty(Context), 0),
ConstantInt::get(Type::getInt32Ty(Context), Idx) }; ConstantInt::get(Type::getInt32Ty(Context), Idx)};
Value *Val = B.CreateGEP(BasePtr, Indices, Name); Value *Val = B.CreateGEP(BasePtr, Indices, Name);
assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant"); assert(isa<GetElementPtrInst>(Val) && "Unexpected folded constant");
@ -374,7 +368,7 @@ ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr,
/// runOnFunction - Insert code to maintain the shadow stack. /// runOnFunction - Insert code to maintain the shadow stack.
bool ShadowStackGC::performCustomLowering(Function &F) { bool ShadowStackGC::performCustomLowering(Function &F) {
LLVMContext &Context = F.getContext(); LLVMContext &Context = F.getContext();
// Find calls to llvm.gcroot. // Find calls to llvm.gcroot.
CollectRoots(F); CollectRoots(F);
@ -391,16 +385,17 @@ bool ShadowStackGC::performCustomLowering(Function &F) {
BasicBlock::iterator IP = F.getEntryBlock().begin(); BasicBlock::iterator IP = F.getEntryBlock().begin();
IRBuilder<> AtEntry(IP->getParent(), IP); IRBuilder<> AtEntry(IP->getParent(), IP);
Instruction *StackEntry = AtEntry.CreateAlloca(ConcreteStackEntryTy, nullptr, Instruction *StackEntry =
"gc_frame"); AtEntry.CreateAlloca(ConcreteStackEntryTy, nullptr, "gc_frame");
while (isa<AllocaInst>(IP)) ++IP; while (isa<AllocaInst>(IP))
++IP;
AtEntry.SetInsertPoint(IP->getParent(), IP); AtEntry.SetInsertPoint(IP->getParent(), IP);
// Initialize the map pointer and load the current head of the shadow stack. // Initialize the map pointer and load the current head of the shadow stack.
Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead"); Instruction *CurrentHead = AtEntry.CreateLoad(Head, "gc_currhead");
Instruction *EntryMapPtr = CreateGEP(Context, AtEntry, StackEntry, Instruction *EntryMapPtr =
0,1,"gc_frame.map"); CreateGEP(Context, AtEntry, StackEntry, 0, 1, "gc_frame.map");
AtEntry.CreateStore(FrameMap, EntryMapPtr); AtEntry.CreateStore(FrameMap, EntryMapPtr);
// After all the allocas... // After all the allocas...
@ -418,14 +413,15 @@ bool ShadowStackGC::performCustomLowering(Function &F) {
// really necessary (the collector would never see the intermediate state at // really necessary (the collector would never see the intermediate state at
// runtime), but it's nicer not to push the half-initialized entry onto the // runtime), but it's nicer not to push the half-initialized entry onto the
// shadow stack. // shadow stack.
while (isa<StoreInst>(IP)) ++IP; while (isa<StoreInst>(IP))
++IP;
AtEntry.SetInsertPoint(IP->getParent(), IP); AtEntry.SetInsertPoint(IP->getParent(), IP);
// Push the entry onto the shadow stack. // Push the entry onto the shadow stack.
Instruction *EntryNextPtr = CreateGEP(Context, AtEntry, Instruction *EntryNextPtr =
StackEntry,0,0,"gc_frame.next"); CreateGEP(Context, AtEntry, StackEntry, 0, 0, "gc_frame.next");
Instruction *NewHeadVal = CreateGEP(Context, AtEntry, Instruction *NewHeadVal =
StackEntry, 0, "gc_newhead"); CreateGEP(Context, AtEntry, StackEntry, 0, "gc_newhead");
AtEntry.CreateStore(CurrentHead, EntryNextPtr); AtEntry.CreateStore(CurrentHead, EntryNextPtr);
AtEntry.CreateStore(NewHeadVal, Head); AtEntry.CreateStore(NewHeadVal, Head);
@ -434,10 +430,10 @@ bool ShadowStackGC::performCustomLowering(Function &F) {
while (IRBuilder<> *AtExit = EE.Next()) { while (IRBuilder<> *AtExit = EE.Next()) {
// Pop the entry from the shadow stack. Don't reuse CurrentHead from // Pop the entry from the shadow stack. Don't reuse CurrentHead from
// AtEntry, since that would make the value live for the entire function. // AtEntry, since that would make the value live for the entire function.
Instruction *EntryNextPtr2 = CreateGEP(Context, *AtExit, StackEntry, 0, 0, Instruction *EntryNextPtr2 =
"gc_frame.next"); CreateGEP(Context, *AtExit, StackEntry, 0, 0, "gc_frame.next");
Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead"); Value *SavedHead = AtExit->CreateLoad(EntryNextPtr2, "gc_savedhead");
AtExit->CreateStore(SavedHead, Head); AtExit->CreateStore(SavedHead, Head);
} }
// Delete the original allocas (which are no longer used) and the intrinsic // Delete the original allocas (which are no longer used) and the intrinsic

View File

@ -12,7 +12,7 @@
// suitable as a default implementation usable with any collector which can // suitable as a default implementation usable with any collector which can
// consume the standard stackmap format generated by statepoints, uses the // consume the standard stackmap format generated by statepoints, uses the
// default addrespace to distinguish between gc managed and non-gc managed // default addrespace to distinguish between gc managed and non-gc managed
// pointers, and has reasonable relocation semantics. // pointers, and has reasonable relocation semantics.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -45,8 +45,8 @@ public:
}; };
} }
static GCRegistry::Add<StatepointGC> static GCRegistry::Add<StatepointGC> X("statepoint-example",
X("statepoint-example", "an example strategy for statepoint"); "an example strategy for statepoint");
namespace llvm { namespace llvm {
void linkStatepointExampleGC() {} void linkStatepointExampleGC() {}

View File

@ -18,5 +18,5 @@ using namespace llvm;
GCStrategy::GCStrategy() GCStrategy::GCStrategy()
: UseStatepoints(false), NeededSafePoints(0), CustomReadBarriers(false), : UseStatepoints(false), NeededSafePoints(0), CustomReadBarriers(false),
CustomWriteBarriers(false), CustomRoots(false), CustomWriteBarriers(false), CustomRoots(false), InitRoots(true),
InitRoots(true), UsesMetadata(false) {} UsesMetadata(false) {}