2016-11-17 07:40:26 +08:00
|
|
|
//===- GlobalSplit.cpp - global variable splitter -------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-11-17 07:40:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass uses inrange annotations on GEP indices to split globals where
|
|
|
|
// beneficial. Clang currently attaches these annotations to references to
|
|
|
|
// virtual table globals under the Itanium ABI for the benefit of the
|
|
|
|
// whole-program virtual call optimization and control flow integrity passes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-11-21 08:28:23 +08:00
|
|
|
#include "llvm/Transforms/IPO/GlobalSplit.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-11-17 07:40:26 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/IR/Constant.h"
|
2016-11-17 07:40:26 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2016-11-17 07:40:26 +08:00
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2016-11-17 07:40:26 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/User.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2016-11-17 07:40:26 +08:00
|
|
|
#include "llvm/Pass.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
2016-11-17 07:40:26 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-10-11 06:49:55 +08:00
|
|
|
static bool splitGlobal(GlobalVariable &GV) {
|
2016-11-17 07:40:26 +08:00
|
|
|
// If the address of the global is taken outside of the module, we cannot
|
|
|
|
// apply this transformation.
|
|
|
|
if (!GV.hasLocalLinkage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We currently only know how to split ConstantStructs.
|
|
|
|
auto *Init = dyn_cast_or_null<ConstantStruct>(GV.getInitializer());
|
|
|
|
if (!Init)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Verify that each user of the global is an inrange getelementptr constant.
|
|
|
|
// From this it follows that any loads from or stores to that global must use
|
|
|
|
// a pointer derived from an inrange getelementptr constant, which is
|
|
|
|
// sufficient to allow us to apply the splitting transform.
|
|
|
|
for (User *U : GV.users()) {
|
|
|
|
if (!isa<Constant>(U))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto *GEP = dyn_cast<GEPOperator>(U);
|
|
|
|
if (!GEP || !GEP->getInRangeIndex() || *GEP->getInRangeIndex() != 1 ||
|
|
|
|
!isa<ConstantInt>(GEP->getOperand(1)) ||
|
|
|
|
!cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
|
|
|
|
!isa<ConstantInt>(GEP->getOperand(2)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<MDNode *, 2> Types;
|
|
|
|
GV.getMetadata(LLVMContext::MD_type, Types);
|
|
|
|
|
|
|
|
const DataLayout &DL = GV.getParent()->getDataLayout();
|
|
|
|
const StructLayout *SL = DL.getStructLayout(Init->getType());
|
|
|
|
|
|
|
|
IntegerType *Int32Ty = Type::getInt32Ty(GV.getContext());
|
|
|
|
|
|
|
|
std::vector<GlobalVariable *> SplitGlobals(Init->getNumOperands());
|
|
|
|
for (unsigned I = 0; I != Init->getNumOperands(); ++I) {
|
|
|
|
// Build a global representing this split piece.
|
|
|
|
auto *SplitGV =
|
|
|
|
new GlobalVariable(*GV.getParent(), Init->getOperand(I)->getType(),
|
|
|
|
GV.isConstant(), GlobalValue::PrivateLinkage,
|
|
|
|
Init->getOperand(I), GV.getName() + "." + utostr(I));
|
|
|
|
SplitGlobals[I] = SplitGV;
|
|
|
|
|
|
|
|
unsigned SplitBegin = SL->getElementOffset(I);
|
|
|
|
unsigned SplitEnd = (I == Init->getNumOperands() - 1)
|
|
|
|
? SL->getSizeInBytes()
|
|
|
|
: SL->getElementOffset(I + 1);
|
|
|
|
|
|
|
|
// Rebuild type metadata, adjusting by the split offset.
|
|
|
|
// FIXME: See if we can use DW_OP_piece to preserve debug metadata here.
|
|
|
|
for (MDNode *Type : Types) {
|
|
|
|
uint64_t ByteOffset = cast<ConstantInt>(
|
|
|
|
cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
|
|
|
|
->getZExtValue();
|
2017-03-08 06:18:48 +08:00
|
|
|
// Type metadata may be attached one byte after the end of the vtable, for
|
|
|
|
// classes without virtual methods in Itanium ABI. AFAIK, it is never
|
|
|
|
// attached to the first byte of a vtable. Subtract one to get the right
|
|
|
|
// slice.
|
|
|
|
// This is making an assumption that vtable groups are the only kinds of
|
|
|
|
// global variables that !type metadata can be attached to, and that they
|
|
|
|
// are either Itanium ABI vtable groups or contain a single vtable (i.e.
|
|
|
|
// Microsoft ABI vtables).
|
|
|
|
uint64_t AttachedTo = (ByteOffset == 0) ? ByteOffset : ByteOffset - 1;
|
|
|
|
if (AttachedTo < SplitBegin || AttachedTo >= SplitEnd)
|
2016-11-17 07:40:26 +08:00
|
|
|
continue;
|
|
|
|
SplitGV->addMetadata(
|
|
|
|
LLVMContext::MD_type,
|
|
|
|
*MDNode::get(GV.getContext(),
|
|
|
|
{ConstantAsMetadata::get(
|
|
|
|
ConstantInt::get(Int32Ty, ByteOffset - SplitBegin)),
|
|
|
|
Type->getOperand(1)}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (User *U : GV.users()) {
|
|
|
|
auto *GEP = cast<GEPOperator>(U);
|
|
|
|
unsigned I = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue();
|
|
|
|
if (I >= SplitGlobals.size())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SmallVector<Value *, 4> Ops;
|
|
|
|
Ops.push_back(ConstantInt::get(Int32Ty, 0));
|
|
|
|
for (unsigned I = 3; I != GEP->getNumOperands(); ++I)
|
|
|
|
Ops.push_back(GEP->getOperand(I));
|
|
|
|
|
|
|
|
auto *NewGEP = ConstantExpr::getGetElementPtr(
|
|
|
|
SplitGlobals[I]->getInitializer()->getType(), SplitGlobals[I], Ops,
|
|
|
|
GEP->isInBounds());
|
|
|
|
GEP->replaceAllUsesWith(NewGEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, remove the original global. Any remaining uses refer to invalid
|
|
|
|
// elements of the global, so replace with undef.
|
|
|
|
if (!GV.use_empty())
|
|
|
|
GV.replaceAllUsesWith(UndefValue::get(GV.getType()));
|
|
|
|
GV.eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-11 06:49:55 +08:00
|
|
|
static bool splitGlobals(Module &M) {
|
2016-11-17 07:40:26 +08:00
|
|
|
// First, see if the module uses either of the llvm.type.test or
|
|
|
|
// llvm.type.checked.load intrinsics, which indicates that splitting globals
|
|
|
|
// may be beneficial.
|
|
|
|
Function *TypeTestFunc =
|
|
|
|
M.getFunction(Intrinsic::getName(Intrinsic::type_test));
|
|
|
|
Function *TypeCheckedLoadFunc =
|
|
|
|
M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
|
|
|
|
if ((!TypeTestFunc || TypeTestFunc->use_empty()) &&
|
|
|
|
(!TypeCheckedLoadFunc || TypeCheckedLoadFunc->use_empty()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto I = M.global_begin(); I != M.global_end();) {
|
|
|
|
GlobalVariable &GV = *I;
|
|
|
|
++I;
|
|
|
|
Changed |= splitGlobal(GV);
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-10-11 06:49:55 +08:00
|
|
|
namespace {
|
|
|
|
|
2016-11-17 07:40:26 +08:00
|
|
|
struct GlobalSplit : public ModulePass {
|
|
|
|
static char ID;
|
2017-10-11 06:49:55 +08:00
|
|
|
|
2016-11-17 07:40:26 +08:00
|
|
|
GlobalSplit() : ModulePass(ID) {
|
|
|
|
initializeGlobalSplitPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2017-10-11 06:49:55 +08:00
|
|
|
|
|
|
|
bool runOnModule(Module &M) override {
|
2016-11-17 07:40:26 +08:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return splitGlobals(M);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-11 06:49:55 +08:00
|
|
|
} // end anonymous namespace
|
2016-11-17 07:40:26 +08:00
|
|
|
|
|
|
|
char GlobalSplit::ID = 0;
|
|
|
|
|
2017-10-11 06:49:55 +08:00
|
|
|
INITIALIZE_PASS(GlobalSplit, "globalsplit", "Global splitter", false, false)
|
|
|
|
|
2016-11-17 07:40:26 +08:00
|
|
|
ModulePass *llvm::createGlobalSplitPass() {
|
|
|
|
return new GlobalSplit;
|
|
|
|
}
|
2016-11-21 08:28:23 +08:00
|
|
|
|
|
|
|
PreservedAnalyses GlobalSplitPass::run(Module &M, ModuleAnalysisManager &AM) {
|
|
|
|
if (!splitGlobals(M))
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|