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