Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/IPO/GlobalSplit.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- GlobalSplit.cpp - global variable splitter -------------------------===//
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 pass uses inrange annotations on GEP indices to split globals where
10
// beneficial. Clang currently attaches these annotations to references to
11
// virtual table globals under the Itanium ABI for the benefit of the
12
// whole-program virtual call optimization and control flow integrity passes.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "llvm/Transforms/IPO/GlobalSplit.h"
17
#include "llvm/ADT/SmallVector.h"
18
#include "llvm/ADT/StringExtras.h"
19
#include "llvm/IR/Constant.h"
20
#include "llvm/IR/Constants.h"
21
#include "llvm/IR/DataLayout.h"
22
#include "llvm/IR/Function.h"
23
#include "llvm/IR/GlobalValue.h"
24
#include "llvm/IR/GlobalVariable.h"
25
#include "llvm/IR/Intrinsics.h"
26
#include "llvm/IR/LLVMContext.h"
27
#include "llvm/IR/Metadata.h"
28
#include "llvm/IR/Module.h"
29
#include "llvm/IR/Operator.h"
30
#include "llvm/IR/Type.h"
31
#include "llvm/IR/User.h"
32
#include "llvm/Pass.h"
33
#include "llvm/Support/Casting.h"
34
#include "llvm/Transforms/IPO.h"
35
#include <cstdint>
36
#include <vector>
37
38
using namespace llvm;
39
40
5
static bool splitGlobal(GlobalVariable &GV) {
41
5
  // If the address of the global is taken outside of the module, we cannot
42
5
  // apply this transformation.
43
5
  if (!GV.hasLocalLinkage())
44
3
    return false;
45
2
46
2
  // We currently only know how to split ConstantStructs.
47
2
  auto *Init = dyn_cast_or_null<ConstantStruct>(GV.getInitializer());
48
2
  if (!Init)
49
0
    return false;
50
2
51
2
  // Verify that each user of the global is an inrange getelementptr constant.
52
2
  // From this it follows that any loads from or stores to that global must use
53
2
  // a pointer derived from an inrange getelementptr constant, which is
54
2
  // sufficient to allow us to apply the splitting transform.
55
14
  
for (User *U : GV.users())2
{
56
14
    if (!isa<Constant>(U))
57
0
      return false;
58
14
59
14
    auto *GEP = dyn_cast<GEPOperator>(U);
60
14
    if (!GEP || !GEP->getInRangeIndex() || *GEP->getInRangeIndex() != 1 ||
61
14
        !isa<ConstantInt>(GEP->getOperand(1)) ||
62
14
        !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
63
14
        !isa<ConstantInt>(GEP->getOperand(2)))
64
0
      return false;
65
14
  }
66
2
67
2
  SmallVector<MDNode *, 2> Types;
68
2
  GV.getMetadata(LLVMContext::MD_type, Types);
69
2
70
2
  const DataLayout &DL = GV.getParent()->getDataLayout();
71
2
  const StructLayout *SL = DL.getStructLayout(Init->getType());
72
2
73
2
  IntegerType *Int32Ty = Type::getInt32Ty(GV.getContext());
74
2
75
2
  std::vector<GlobalVariable *> SplitGlobals(Init->getNumOperands());
76
6
  for (unsigned I = 0; I != Init->getNumOperands(); 
++I4
) {
77
4
    // Build a global representing this split piece.
78
4
    auto *SplitGV =
79
4
        new GlobalVariable(*GV.getParent(), Init->getOperand(I)->getType(),
80
4
                           GV.isConstant(), GlobalValue::PrivateLinkage,
81
4
                           Init->getOperand(I), GV.getName() + "." + utostr(I));
82
4
    SplitGlobals[I] = SplitGV;
83
4
84
4
    unsigned SplitBegin = SL->getElementOffset(I);
85
4
    unsigned SplitEnd = (I == Init->getNumOperands() - 1)
86
4
                            ? 
SL->getSizeInBytes()2
87
4
                            : 
SL->getElementOffset(I + 1)2
;
88
4
89
4
    // Rebuild type metadata, adjusting by the split offset.
90
4
    // FIXME: See if we can use DW_OP_piece to preserve debug metadata here.
91
20
    for (MDNode *Type : Types) {
92
20
      uint64_t ByteOffset = cast<ConstantInt>(
93
20
              cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
94
20
              ->getZExtValue();
95
20
      // Type metadata may be attached one byte after the end of the vtable, for
96
20
      // classes without virtual methods in Itanium ABI. AFAIK, it is never
97
20
      // attached to the first byte of a vtable. Subtract one to get the right
98
20
      // slice.
99
20
      // This is making an assumption that vtable groups are the only kinds of
100
20
      // global variables that !type metadata can be attached to, and that they
101
20
      // are either Itanium ABI vtable groups or contain a single vtable (i.e.
102
20
      // Microsoft ABI vtables).
103
20
      uint64_t AttachedTo = (ByteOffset == 0) ? 
ByteOffset4
:
ByteOffset - 116
;
104
20
      if (AttachedTo < SplitBegin || 
AttachedTo >= SplitEnd14
)
105
10
        continue;
106
10
      SplitGV->addMetadata(
107
10
          LLVMContext::MD_type,
108
10
          *MDNode::get(GV.getContext(),
109
10
                       {ConstantAsMetadata::get(
110
10
                            ConstantInt::get(Int32Ty, ByteOffset - SplitBegin)),
111
10
                        Type->getOperand(1)}));
112
10
    }
113
4
  }
114
2
115
14
  for (User *U : GV.users()) {
116
14
    auto *GEP = cast<GEPOperator>(U);
117
14
    unsigned I = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue();
118
14
    if (I >= SplitGlobals.size())
119
0
      continue;
120
14
121
14
    SmallVector<Value *, 4> Ops;
122
14
    Ops.push_back(ConstantInt::get(Int32Ty, 0));
123
28
    for (unsigned I = 3; I != GEP->getNumOperands(); 
++I14
)
124
14
      Ops.push_back(GEP->getOperand(I));
125
14
126
14
    auto *NewGEP = ConstantExpr::getGetElementPtr(
127
14
        SplitGlobals[I]->getInitializer()->getType(), SplitGlobals[I], Ops,
128
14
        GEP->isInBounds());
129
14
    GEP->replaceAllUsesWith(NewGEP);
130
14
  }
131
2
132
2
  // Finally, remove the original global. Any remaining uses refer to invalid
133
2
  // elements of the global, so replace with undef.
134
2
  if (!GV.use_empty())
135
2
    GV.replaceAllUsesWith(UndefValue::get(GV.getType()));
136
2
  GV.eraseFromParent();
137
2
  return true;
138
2
}
139
140
485
static bool splitGlobals(Module &M) {
141
485
  // First, see if the module uses either of the llvm.type.test or
142
485
  // llvm.type.checked.load intrinsics, which indicates that splitting globals
143
485
  // may be beneficial.
144
485
  Function *TypeTestFunc =
145
485
      M.getFunction(Intrinsic::getName(Intrinsic::type_test));
146
485
  Function *TypeCheckedLoadFunc =
147
485
      M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
148
485
  if ((!TypeTestFunc || 
TypeTestFunc->use_empty()3
) &&
149
485
      
(482
!TypeCheckedLoadFunc482
||
TypeCheckedLoadFunc->use_empty()0
))
150
482
    return false;
151
3
152
3
  bool Changed = false;
153
8
  for (auto I = M.global_begin(); I != M.global_end();) {
154
5
    GlobalVariable &GV = *I;
155
5
    ++I;
156
5
    Changed |= splitGlobal(GV);
157
5
  }
158
3
  return Changed;
159
3
}
160
161
namespace {
162
163
struct GlobalSplit : public ModulePass {
164
  static char ID;
165
166
462
  GlobalSplit() : ModulePass(ID) {
167
462
    initializeGlobalSplitPass(*PassRegistry::getPassRegistry());
168
462
  }
169
170
462
  bool runOnModule(Module &M) override {
171
462
    if (skipModule(M))
172
0
      return false;
173
462
174
462
    return splitGlobals(M);
175
462
  }
176
};
177
178
} // end anonymous namespace
179
180
char GlobalSplit::ID = 0;
181
182
INITIALIZE_PASS(GlobalSplit, "globalsplit", "Global splitter", false, false)
183
184
459
ModulePass *llvm::createGlobalSplitPass() {
185
459
  return new GlobalSplit;
186
459
}
187
188
23
PreservedAnalyses GlobalSplitPass::run(Module &M, ModuleAnalysisManager &AM) {
189
23
  if (!splitGlobals(M))
190
22
    return PreservedAnalyses::all();
191
1
  return PreservedAnalyses::none();
192
1
}