LLVM 19.0.0git
Go to the documentation of this file.
1//===- StripGCRelocates.cpp - Remove gc.relocates inserted by RewriteStatePoints===//
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
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.
17#include "llvm/IR/Function.h"
20#include "llvm/IR/Statepoint.h"
22using namespace llvm;
25 // Nothing to do for declarations.
26 if (F.isDeclaration())
27 return false;
29 // TODO: We currently do not handle gc.relocates that are in landing pads,
30 // i.e. not bound to a single statepoint token.
31 for (Instruction &I : instructions(F)) {
32 if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
33 if (isa<GCStatepointInst>(GCR->getOperand(0)))
34 GCRelocates.push_back(GCR);
35 }
36 // All gc.relocates are bound to a single statepoint token. The order of
37 // visiting gc.relocates for deletion does not matter.
38 for (GCRelocateInst *GCRel : GCRelocates) {
39 Value *OrigPtr = GCRel->getDerivedPtr();
40 Value *ReplaceGCRel = OrigPtr;
42 // All gc_relocates are i8 addrspace(1)* typed, we need a bitcast from i8
43 // addrspace(1)* to the type of the OrigPtr, if the are not the same.
44 if (GCRel->getType() != OrigPtr->getType())
45 ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel->getIterator());
47 // Replace all uses of gc.relocate and delete the gc.relocate
48 // There maybe unncessary bitcasts back to the OrigPtr type, an instcombine
49 // pass would clear this up.
50 GCRel->replaceAllUsesWith(ReplaceGCRel);
51 GCRel->eraseFromParent();
52 }
53 return !GCRelocates.empty();
58 if (!stripGCRelocates(F))
61 // Removing gc.relocate preserves the CFG, but most other analysis probably
62 // need to re-run.
65 return PA;
Expand Atomic instructions
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static bool stripGCRelocates(Function &F)
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
This class represents a no-op cast from one type to another.
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:70
Represents calls to the gc.relocate intrinsic.
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:144
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18