Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/Transforms/Utils/StripGCRelocates.cpp
Line
Count
Source (jump to first uncovered line)
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
16
#include "llvm/Transforms/Utils/StripGCRelocates.h"
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
22
using namespace llvm;
23
24
0
static bool stripGCRelocates(Function &F) {
25
  // Nothing to do for declarations.
26
0
  if (F.isDeclaration())
27
0
    return false;
28
0
  SmallVector<GCRelocateInst *, 20> GCRelocates;
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
0
  for (Instruction &I : instructions(F)) {
32
0
    if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
33
0
      if (isa<GCStatepointInst>(GCR->getOperand(0)))
34
0
        GCRelocates.push_back(GCR);
35
0
  }
36
  // All gc.relocates are bound to a single statepoint token. The order of
37
  // visiting gc.relocates for deletion does not matter.
38
0
  for (GCRelocateInst *GCRel : GCRelocates) {
39
0
    Value *OrigPtr = GCRel->getDerivedPtr();
40
0
    Value *ReplaceGCRel = OrigPtr;
41
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
0
    if (GCRel->getType() != OrigPtr->getType())
45
0
      ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel);
46
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
0
    GCRel->replaceAllUsesWith(ReplaceGCRel);
51
0
    GCRel->eraseFromParent();
52
0
  }
53
0
  return !GCRelocates.empty();
54
0
}
55
56
PreservedAnalyses StripGCRelocates::run(Function &F,
57
0
                                        FunctionAnalysisManager &AM) {
58
0
  if (!stripGCRelocates(F))
59
0
    return PreservedAnalyses::all();
60
61
  // Removing gc.relocate preserves the CFG, but most other analysis probably
62
  // need to re-run.
63
0
  PreservedAnalyses PA;
64
0
  PA.preserveSet<CFGAnalyses>();
65
0
  return PA;
66
0
}