blob: 50844cf9d1c54cc833c1d0d045b99366754c079b [file] [log] [blame]
Anna Thomas08602592016-10-21 18:43:16 +00001//===- StripGCRelocates.cpp - Remove gc.relocates inserted by RewriteStatePoints===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Anna Thomas08602592016-10-21 18:43:16 +00006//
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/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/Pass.h"
Anna Thomas08602592016-10-21 18:43:16 +000022#include "llvm/Support/raw_ostream.h"
Anna Thomas08602592016-10-21 18:43:16 +000023
24using namespace llvm;
25
26namespace {
27struct StripGCRelocates : public FunctionPass {
28 static char ID; // Pass identification, replacement for typeid
29 StripGCRelocates() : FunctionPass(ID) {
30 initializeStripGCRelocatesPass(*PassRegistry::getPassRegistry());
31 }
32
33 void getAnalysisUsage(AnalysisUsage &Info) const override {}
34
35 bool runOnFunction(Function &F) override;
36
37};
38char StripGCRelocates::ID = 0;
39}
40
41bool StripGCRelocates::runOnFunction(Function &F) {
42 // Nothing to do for declarations.
43 if (F.isDeclaration())
44 return false;
45 SmallVector<GCRelocateInst *, 20> GCRelocates;
46 // TODO: We currently do not handle gc.relocates that are in landing pads,
47 // i.e. not bound to a single statepoint token.
48 for (Instruction &I : instructions(F)) {
49 if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
50 if (isStatepoint(GCR->getOperand(0)))
51 GCRelocates.push_back(GCR);
52 }
53 // All gc.relocates are bound to a single statepoint token. The order of
54 // visiting gc.relocates for deletion does not matter.
55 for (GCRelocateInst *GCRel : GCRelocates) {
56 Value *OrigPtr = GCRel->getDerivedPtr();
57 Value *ReplaceGCRel = OrigPtr;
58
59 // All gc_relocates are i8 addrspace(1)* typed, we need a bitcast from i8
60 // addrspace(1)* to the type of the OrigPtr, if the are not the same.
61 if (GCRel->getType() != OrigPtr->getType())
62 ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel);
63
64 // Replace all uses of gc.relocate and delete the gc.relocate
65 // There maybe unncessary bitcasts back to the OrigPtr type, an instcombine
66 // pass would clear this up.
67 GCRel->replaceAllUsesWith(ReplaceGCRel);
68 GCRel->eraseFromParent();
69 }
70 return !GCRelocates.empty();
71}
72
73INITIALIZE_PASS(StripGCRelocates, "strip-gc-relocates",
74 "Strip gc.relocates inserted through RewriteStatepointsForGC",
75 true, false)