|
| 1 | +//===- Extend GCStrategy of llvm/CodeGen/GCStrategy.h ---------------------===// |
| 2 | +// |
| 3 | +// We extend the base GCStrategy as follows: |
| 4 | +// - use gc.safepoints instead of (default) gc.roots. |
| 5 | +// - specify that the RewriteStatepointsForGC pass should rewrite the calls of |
| 6 | +// this function. |
| 7 | +// - pointers with address space != 0 are pointing to GC-managed memory. |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +// NOLINTBEGIN |
| 11 | + |
| 12 | +#include "kllvm/codegen/GCStrategy.h" |
| 13 | + |
| 14 | +#include "llvm/CodeGen/GCMetadata.h" |
| 15 | +#include "llvm/IR/DerivedTypes.h" |
| 16 | +#include "llvm/Support/Compiler.h" |
| 17 | + |
| 18 | +using namespace llvm; |
| 19 | +using namespace kllvm; |
| 20 | + |
| 21 | +LLVMBackendGCStrategy::LLVMBackendGCStrategy() { |
| 22 | + UseStatepoints = true; // Use gc.statepoints |
| 23 | +#if LLVM_VERSION_MAJOR != 15 |
| 24 | + UseRS4GC = true; // Rewrite the calls of a function that has this GCStrategy |
| 25 | +#endif |
| 26 | +} |
| 27 | + |
| 28 | +// Override |
| 29 | +#if LLVM_VERSION_MAJOR == 15 |
| 30 | +llvm::Optional<bool> |
| 31 | +LLVMBackendGCStrategy::isGCManagedPointer(Type const *Ty) const { |
| 32 | +#else |
| 33 | +std::optional<bool> |
| 34 | +LLVMBackendGCStrategy::isGCManagedPointer(Type const *Ty) const { |
| 35 | +#endif |
| 36 | + // Return false for any non-pointer type |
| 37 | + if (!Ty->isPointerTy()) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + // Any pointer with address space != 0 is to managed memory. |
| 41 | + PointerType const *PTy = dyn_cast<PointerType>(Ty); |
| 42 | + if (PTy->getAddressSpace()) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +// Add LLVMBackendGCStrategy to the global GCRegistry |
| 49 | +static GCRegistry::Add<LLVMBackendGCStrategy> |
| 50 | + X("gcs-llvm-backend", "GC Strategy for the LLVM Backend"); |
| 51 | + |
| 52 | +// NOLINTEND |
0 commit comments