LLVM  15.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"
18 #include "llvm/IR/InstIterator.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Statepoint.h"
21 #include "llvm/InitializePasses.h"
22 #include "llvm/Pass.h"
23 
24 using namespace llvm;
25 
26 static bool stripGCRelocates(Function &F) {
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))
61  return PreservedAnalyses::all();
62 
63  // Removing gc.relocate preserves the CFG, but most other analysis probably
64  // need to re-run.
67  return PA;
68 }
69 
70 namespace {
71 struct 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 };
82 } // namespace
83 
84 INITIALIZE_PASS(StripGCRelocatesLegacy, "strip-gc-relocates",
85  "Strip gc.relocates inserted through RewriteStatepointsForGC",
86  true, false)
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
InstIterator.h
llvm::Function
Definition: Function.h:60
Pass.h
llvm::BitCastInst
This class represents a no-op cast from one type to another.
Definition: Instructions.h:5225
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:37
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::Instruction
Definition: Instruction.h:42
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
Statepoint.h
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
llvm::GCRelocateInst
Represents calls to the gc.relocate intrinsic.
Definition: IntrinsicInst.h:1369
StripGCRelocates.h
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::initializeStripGCRelocatesLegacyPass
void initializeStripGCRelocatesLegacyPass(PassRegistry &)
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::CFGAnalyses
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:113
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
Function.h
Instructions.h
llvm::PreservedAnalyses::preserveSet
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:188
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::StripGCRelocates::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: StripGCRelocates.cpp:58
stripGCRelocates
static bool stripGCRelocates(Function &F)
Definition: StripGCRelocates.cpp:26
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37