StripGCRelocates.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 intrinsics 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/InitializePasses.h"
  21. #include "llvm/Pass.h"
  22. using namespace llvm;
  23. static bool stripGCRelocates(Function &F) {
  24. // Nothing to do for declarations.
  25. if (F.isDeclaration())
  26. return false;
  27. SmallVector<GCRelocateInst *, 20> GCRelocates;
  28. // TODO: We currently do not handle gc.relocates that are in landing pads,
  29. // i.e. not bound to a single statepoint token.
  30. for (Instruction &I : instructions(F)) {
  31. if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
  32. if (isa<GCStatepointInst>(GCR->getOperand(0)))
  33. GCRelocates.push_back(GCR);
  34. }
  35. // All gc.relocates are bound to a single statepoint token. The order of
  36. // visiting gc.relocates for deletion does not matter.
  37. for (GCRelocateInst *GCRel : GCRelocates) {
  38. Value *OrigPtr = GCRel->getDerivedPtr();
  39. Value *ReplaceGCRel = OrigPtr;
  40. // All gc_relocates are i8 addrspace(1)* typed, we need a bitcast from i8
  41. // addrspace(1)* to the type of the OrigPtr, if the are not the same.
  42. if (GCRel->getType() != OrigPtr->getType())
  43. ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel);
  44. // Replace all uses of gc.relocate and delete the gc.relocate
  45. // There maybe unncessary bitcasts back to the OrigPtr type, an instcombine
  46. // pass would clear this up.
  47. GCRel->replaceAllUsesWith(ReplaceGCRel);
  48. GCRel->eraseFromParent();
  49. }
  50. return !GCRelocates.empty();
  51. }
  52. PreservedAnalyses StripGCRelocates::run(Function &F,
  53. FunctionAnalysisManager &AM) {
  54. if (!stripGCRelocates(F))
  55. return PreservedAnalyses::all();
  56. // Removing gc.relocate preserves the CFG, but most other analysis probably
  57. // need to re-run.
  58. PreservedAnalyses PA;
  59. PA.preserveSet<CFGAnalyses>();
  60. return PA;
  61. }
  62. namespace {
  63. struct StripGCRelocatesLegacy : public FunctionPass {
  64. static char ID; // Pass identification, replacement for typeid
  65. StripGCRelocatesLegacy() : FunctionPass(ID) {
  66. initializeStripGCRelocatesLegacyPass(*PassRegistry::getPassRegistry());
  67. }
  68. void getAnalysisUsage(AnalysisUsage &Info) const override {}
  69. bool runOnFunction(Function &F) override { return ::stripGCRelocates(F); }
  70. };
  71. char StripGCRelocatesLegacy::ID = 0;
  72. } // namespace
  73. INITIALIZE_PASS(StripGCRelocatesLegacy, "strip-gc-relocates",
  74. "Strip gc.relocates inserted through RewriteStatepointsForGC",
  75. true, false)