LLVM  13.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 instrinsics 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/IR/Type.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Pass.h"
25 
26 using namespace llvm;
27 
28 static bool stripGCRelocates(Function &F) {
29  // Nothing to do for declarations.
30  if (F.isDeclaration())
31  return false;
33  // TODO: We currently do not handle gc.relocates that are in landing pads,
34  // i.e. not bound to a single statepoint token.
35  for (Instruction &I : instructions(F)) {
36  if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
37  if (isa<GCStatepointInst>(GCR->getOperand(0)))
38  GCRelocates.push_back(GCR);
39  }
40  // All gc.relocates are bound to a single statepoint token. The order of
41  // visiting gc.relocates for deletion does not matter.
42  for (GCRelocateInst *GCRel : GCRelocates) {
43  Value *OrigPtr = GCRel->getDerivedPtr();
44  Value *ReplaceGCRel = OrigPtr;
45 
46  // All gc_relocates are i8 addrspace(1)* typed, we need a bitcast from i8
47  // addrspace(1)* to the type of the OrigPtr, if the are not the same.
48  if (GCRel->getType() != OrigPtr->getType())
49  ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel);
50 
51  // Replace all uses of gc.relocate and delete the gc.relocate
52  // There maybe unncessary bitcasts back to the OrigPtr type, an instcombine
53  // pass would clear this up.
54  GCRel->replaceAllUsesWith(ReplaceGCRel);
55  GCRel->eraseFromParent();
56  }
57  return !GCRelocates.empty();
58 }
59 
62  if (!stripGCRelocates(F))
63  return PreservedAnalyses::all();
64 
65  // Removing gc.relocate preserves the CFG, but most other analysis probably
66  // need to re-run.
69  return PA;
70 }
71 
72 namespace {
73 struct StripGCRelocatesLegacy : public FunctionPass {
74  static char ID; // Pass identification, replacement for typeid
75  StripGCRelocatesLegacy() : FunctionPass(ID) {
77  }
78 
79  void getAnalysisUsage(AnalysisUsage &Info) const override {}
80 
81  bool runOnFunction(Function &F) override { return ::stripGCRelocates(F); }
82 };
84 } // namespace
85 
86 INITIALIZE_PASS(StripGCRelocatesLegacy, "strip-gc-relocates",
87  "Strip gc.relocates inserted through RewriteStatepointsForGC",
88  true, false)
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
Definition: AllocatorList.h:23
InstIterator.h
llvm::Function
Definition: Function.h:61
Pass.h
llvm::BitCastInst
This class represents a no-op cast from one type to another.
Definition: Instructions.h:5166
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1167
F
#define F(x, y, z)
Definition: MD5.cpp:56
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:45
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
Statepoint.h
Type.h
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
llvm::GCRelocateInst
Represents calls to the gc.relocate intrinsic.
Definition: IntrinsicInst.h:1237
StripGCRelocates.h
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::initializeStripGCRelocatesLegacyPass
void initializeStripGCRelocatesLegacyPass(PassRegistry &)
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
llvm::CFGAnalyses
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:116
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:161
Function.h
Instructions.h
llvm::PreservedAnalyses::preserveSet
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:191
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
raw_ostream.h
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::StripGCRelocates::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: StripGCRelocates.cpp:60
stripGCRelocates
static bool stripGCRelocates(Function &F)
Definition: StripGCRelocates.cpp:28
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38