LLVM 17.0.0git
StripGCRelocates.cpp
Go to the documentation of this file.
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
17#include "llvm/IR/Function.h"
20#include "llvm/IR/Statepoint.h"
22#include "llvm/Pass.h"
23
24using namespace llvm;
25
27 // Nothing to do for declarations.
28 if (F.isDeclaration())
29 return false;
31 // TODO: We currently do not handle gc.relocates that are in landing pads,
32 // i.e. not bound to a single statepoint token.
33 for (Instruction &I : instructions(F)) {
34 if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
35 if (isa<GCStatepointInst>(GCR->getOperand(0)))
36 GCRelocates.push_back(GCR);
37 }
38 // All gc.relocates are bound to a single statepoint token. The order of
39 // visiting gc.relocates for deletion does not matter.
40 for (GCRelocateInst *GCRel : GCRelocates) {
41 Value *OrigPtr = GCRel->getDerivedPtr();
42 Value *ReplaceGCRel = OrigPtr;
43
44 // All gc_relocates are i8 addrspace(1)* typed, we need a bitcast from i8
45 // addrspace(1)* to the type of the OrigPtr, if the are not the same.
46 if (GCRel->getType() != OrigPtr->getType())
47 ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel);
48
49 // Replace all uses of gc.relocate and delete the gc.relocate
50 // There maybe unncessary bitcasts back to the OrigPtr type, an instcombine
51 // pass would clear this up.
52 GCRel->replaceAllUsesWith(ReplaceGCRel);
53 GCRel->eraseFromParent();
54 }
55 return !GCRelocates.empty();
56}
57
60 if (!stripGCRelocates(F))
62
63 // Removing gc.relocate preserves the CFG, but most other analysis probably
64 // need to re-run.
67 return PA;
68}
69
70namespace {
71struct StripGCRelocatesLegacy : public FunctionPass {
72 static char ID; // Pass identification, replacement for typeid
73 StripGCRelocatesLegacy() : FunctionPass(ID) {
75 }
76
77 void getAnalysisUsage(AnalysisUsage &Info) const override {}
78
79 bool runOnFunction(Function &F) override { return ::stripGCRelocates(F); }
80};
81char StripGCRelocatesLegacy::ID = 0;
82} // namespace
83
84INITIALIZE_PASS(StripGCRelocatesLegacy, "strip-gc-relocates",
85 "Strip gc.relocates inserted through RewriteStatepointsForGC",
86 true, false)
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static bool runOnFunction(Function &F, bool PostInlining)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
print must be executed print the must be executed context for all instructions
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
static bool stripGCRelocates(Function &F)
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
Represent the analysis usage information of a pass.
This class represents a no-op cast from one type to another.
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:113
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
Represents calls to the gc.relocate intrinsic.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:188
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeStripGCRelocatesLegacyPass(PassRegistry &)