blob: ac0b519f4a77f87651c561d2134e502db4df8a50 [file] [log] [blame]
Anna Thomas08602592016-10-21 18:43:16 +00001//===- StripGCRelocates.cpp - Remove gc.relocates inserted by RewriteStatePoints===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This is a little utility pass that removes the gc.relocates inserted by
11// RewriteStatepointsForGC. Note that the generated IR is incorrect,
12// but this is useful as a single pass in itself, for analysis of IR, without
13// the GC.relocates. The statepoint and gc.result instrinsics would still be
14// present.
15//===----------------------------------------------------------------------===//
16
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/Pass.h"
Anna Thomas08602592016-10-21 18:43:16 +000023#include "llvm/Support/raw_ostream.h"
Anna Thomas08602592016-10-21 18:43:16 +000024
25using namespace llvm;
26
27namespace {
28struct StripGCRelocates : public FunctionPass {
29 static char ID; // Pass identification, replacement for typeid
30 StripGCRelocates() : FunctionPass(ID) {
31 initializeStripGCRelocatesPass(*PassRegistry::getPassRegistry());
32 }
33
34 void getAnalysisUsage(AnalysisUsage &Info) const override {}
35
36 bool runOnFunction(Function &F) override;
37
38};
39char StripGCRelocates::ID = 0;
40}
41
42bool StripGCRelocates::runOnFunction(Function &F) {
43 // Nothing to do for declarations.
44 if (F.isDeclaration())
45 return false;
46 SmallVector<GCRelocateInst *, 20> GCRelocates;
47 // TODO: We currently do not handle gc.relocates that are in landing pads,
48 // i.e. not bound to a single statepoint token.
49 for (Instruction &I : instructions(F)) {
50 if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
51 if (isStatepoint(GCR->getOperand(0)))
52 GCRelocates.push_back(GCR);
53 }
54 // All gc.relocates are bound to a single statepoint token. The order of
55 // visiting gc.relocates for deletion does not matter.
56 for (GCRelocateInst *GCRel : GCRelocates) {
57 Value *OrigPtr = GCRel->getDerivedPtr();
58 Value *ReplaceGCRel = OrigPtr;
59
60 // All gc_relocates are i8 addrspace(1)* typed, we need a bitcast from i8
61 // addrspace(1)* to the type of the OrigPtr, if the are not the same.
62 if (GCRel->getType() != OrigPtr->getType())
63 ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel);
64
65 // Replace all uses of gc.relocate and delete the gc.relocate
66 // There maybe unncessary bitcasts back to the OrigPtr type, an instcombine
67 // pass would clear this up.
68 GCRel->replaceAllUsesWith(ReplaceGCRel);
69 GCRel->eraseFromParent();
70 }
71 return !GCRelocates.empty();
72}
73
74INITIALIZE_PASS(StripGCRelocates, "strip-gc-relocates",
75 "Strip gc.relocates inserted through RewriteStatepointsForGC",
76 true, false)