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