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
16#include "llvm/Transforms/Utils/StripGCRelocates.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/InstIterator.h"
19#include "llvm/IR/Statepoint.h"
20
21using namespace llvm;
22
23static 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>(Val: &I))
32 if (isa<GCStatepointInst>(Val: GCR->getOperand(i_nocapture: 0)))
33 GCRelocates.push_back(Elt: 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 GCRel->replaceAllUsesWith(V: GCRel->getDerivedPtr());
39 GCRel->eraseFromParent();
40 }
41 return !GCRelocates.empty();
42}
43
44PreservedAnalyses StripGCRelocates::run(Function &F,
45 FunctionAnalysisManager &AM) {
46 if (!stripGCRelocates(F))
47 return PreservedAnalyses::all();
48
49 // Removing gc.relocate preserves the CFG, but most other analysis probably
50 // need to re-run.
51 PreservedAnalyses PA;
52 PA.preserveSet<CFGAnalyses>();
53 return PA;
54}
55