Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This is a little utility pass that removes the gc.relocates inserted by
11
// RewriteStatepointsForGC. Note that the generated IR is incorrect,
12
// but this is useful as a single pass in itself, for analysis of IR, without
13
// the GC.relocates. The statepoint and gc.result instrinsics would still be
14
// present.
15
//===----------------------------------------------------------------------===//
16
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/Pass.h"
23
#include "llvm/Support/raw_ostream.h"
24
#include "llvm/Transforms/Scalar.h"
25
26
using namespace llvm;
27
28
namespace {
29
struct StripGCRelocates : public FunctionPass {
30
  static char ID; // Pass identification, replacement for typeid
31
1
  StripGCRelocates() : FunctionPass(ID) {
32
1
    initializeStripGCRelocatesPass(*PassRegistry::getPassRegistry());
33
1
  }
34
35
1
  void getAnalysisUsage(AnalysisUsage &Info) const override {}
36
37
  bool runOnFunction(Function &F) override;
38
39
};
40
char StripGCRelocates::ID = 0;
41
}
42
43
5
bool StripGCRelocates::runOnFunction(Function &F) {
44
5
  // Nothing to do for declarations.
45
5
  if (F.isDeclaration())
46
0
    return false;
47
5
  SmallVector<GCRelocateInst *, 20> GCRelocates;
48
5
  // TODO: We currently do not handle gc.relocates that are in landing pads,
49
5
  // i.e. not bound to a single statepoint token.
50
44
  for (Instruction &I : instructions(F)) {
51
44
    if (auto *GCR = dyn_cast<GCRelocateInst>(&I))
52
9
      
if (9
isStatepoint(GCR->getOperand(0))9
)
53
8
        GCRelocates.push_back(GCR);
54
44
  }
55
5
  // All gc.relocates are bound to a single statepoint token. The order of
56
5
  // visiting gc.relocates for deletion does not matter.
57
8
  for (GCRelocateInst *GCRel : GCRelocates) {
58
8
    Value *OrigPtr = GCRel->getDerivedPtr();
59
8
    Value *ReplaceGCRel = OrigPtr;
60
8
61
8
    // All gc_relocates are i8 addrspace(1)* typed, we need a bitcast from i8
62
8
    // addrspace(1)* to the type of the OrigPtr, if the are not the same.
63
8
    if (GCRel->getType() != OrigPtr->getType())
64
8
      ReplaceGCRel = new BitCastInst(OrigPtr, GCRel->getType(), "cast", GCRel);
65
8
66
8
    // Replace all uses of gc.relocate and delete the gc.relocate
67
8
    // There maybe unncessary bitcasts back to the OrigPtr type, an instcombine
68
8
    // pass would clear this up.
69
8
    GCRel->replaceAllUsesWith(ReplaceGCRel);
70
8
    GCRel->eraseFromParent();
71
8
  }
72
5
  return !GCRelocates.empty();
73
5
}
74
75
INITIALIZE_PASS(StripGCRelocates, "strip-gc-relocates",
76
                "Strip gc.relocates inserted through RewriteStatepointsForGC",
77
                true, false)
78
0
FunctionPass *llvm::createStripGCRelocatesPass() {
79
0
  return new StripGCRelocates();
80
0
}