StripGCRelocates.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===- StripGCRelocates.cpp - Remove gc.relocates inserted by RewriteStatePoints===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This is a little utility pass that removes the gc.relocates inserted by
  10. // RewriteStatepointsForGC. Note that the generated IR is incorrect,
  11. // but this is useful as a single pass in itself, for analysis of IR, without
  12. // the GC.relocates. The statepoint and gc.result instrinsics would still be
  13. // present.
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Transforms/Utils/StripGCRelocates.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/InstIterator.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/Statepoint.h"
  20. #include "llvm/IR/Type.h"
  21. #include "llvm/InitializePasses.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. using namespace llvm;
  25. static bool stripGCRelocates(Function &F) {
  26. // Nothing to do for declarations.
  27. if (F.isDeclaration())
  28. return false;
  29. SmallVector<GCRelocateInst *, 20> GCRelocates;
  30. // TODO: We currently do not handle gc.relocates that are in landing pads,
  31. // i.e. not bound to a single statepoint token.
  32. for (Instruction &I : instructions(F)) {
  33. if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
  34. if (isa<GCStatepointInst>(GCR->getOperand(0)))
  35. GCRelocates.push_back(GCR);
  36. }
  37. // All gc.relocates are bound to a single statepoint token. The order of
  38. // visiting gc.relocates for deletion does not matter.
  39. for (GCRelocateInst *GCRel : GCRelocates) {
  40. Value *OrigPtr = GCRel->getDerivedPtr();
  41. Value *ReplaceGCRel = OrigPtr;
  42. // All gc_relocates are i8 addrspace(1)* typed, we need a bitcast from i8
  43. // addrspace(1)* to the type of the OrigPtr, if the are not the same.
  44. if (GCRel->getType() != OrigPtr->getType())
  45. ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel);
  46. // Replace all uses of gc.relocate and delete the gc.relocate
  47. // There maybe unncessary bitcasts back to the OrigPtr type, an instcombine
  48. // pass would clear this up.
  49. GCRel->replaceAllUsesWith(ReplaceGCRel);
  50. GCRel->eraseFromParent();
  51. }
  52. return !GCRelocates.empty();
  53. }
  54. PreservedAnalyses StripGCRelocates::run(Function &F,
  55. FunctionAnalysisManager &AM) {
  56. if (!stripGCRelocates(F))
  57. return PreservedAnalyses::all();
  58. // Removing gc.relocate preserves the CFG, but most other analysis probably
  59. // need to re-run.
  60. PreservedAnalyses PA;
  61. PA.preserveSet<CFGAnalyses>();
  62. return PA;
  63. }
  64. namespace {
  65. struct StripGCRelocatesLegacy : public FunctionPass {
  66. static char ID; // Pass identification, replacement for typeid
  67. StripGCRelocatesLegacy() : FunctionPass(ID) {
  68. initializeStripGCRelocatesLegacyPass(*PassRegistry::getPassRegistry());
  69. }
  70. void getAnalysisUsage(AnalysisUsage &Info) const override {}
  71. bool runOnFunction(Function &F) override { return ::stripGCRelocates(F); }
  72. };
  73. char StripGCRelocatesLegacy::ID = 0;
  74. } // namespace
  75. INITIALIZE_PASS(StripGCRelocatesLegacy, "strip-gc-relocates",
  76. "Strip gc.relocates inserted through RewriteStatepointsForGC",
  77. true, false)