Add comdat key field to llvm.global_ctors and llvm.global_dtors

This allows us to put dynamic initializers for weak data into the same
comdat group as the data being initialized.  This is necessary for MSVC
ABI compatibility.  Once we have comdats for guard variables, we can use
the combination to help GlobalOpt fire more often for weak data with
guarded initialization on other platforms.

Reviewers: nlewycky

Differential Revision: http://reviews.llvm.org/D3499

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209015 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Kleckner
2014-05-16 20:39:27 +00:00
parent dedd6203ad
commit 1ce3088669
16 changed files with 289 additions and 69 deletions

View File

@@ -170,7 +170,59 @@ bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
return Upgraded;
}
static bool UpgradeGlobalStructors(GlobalVariable *GV) {
ArrayType *ATy = dyn_cast<ArrayType>(GV->getType()->getElementType());
StructType *OldTy =
ATy ? dyn_cast<StructType>(ATy->getElementType()) : nullptr;
// Only upgrade an array of a two field struct with the appropriate field
// types.
if (!OldTy || OldTy->getNumElements() != 2)
return false;
// Get the upgraded 3 element type.
PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo();
Type *Tys[3] = {
OldTy->getElementType(0),
OldTy->getElementType(1),
VoidPtrTy
};
StructType *NewTy =
StructType::get(GV->getContext(), Tys, /*isPacked=*/false);
// Build new constants with a null third field filled in.
ConstantArray *OldInit = dyn_cast<ConstantArray>(GV->getInitializer());
if (!OldInit)
return false;
std::vector<Constant *> Initializers;
for (Use &U : OldInit->operands()) {
ConstantStruct *Init = cast<ConstantStruct>(&U);
Constant *NewInit =
ConstantStruct::get(NewTy, Init->getOperand(0), Init->getOperand(1),
Constant::getNullValue(VoidPtrTy), nullptr);
Initializers.push_back(NewInit);
}
assert(Initializers.size() == ATy->getNumElements());
// Replace the old GV with a new one.
ATy = ArrayType::get(NewTy, Initializers.size());
Constant *NewInit = ConstantArray::get(ATy, Initializers);
GlobalVariable *NewGV = new GlobalVariable(
*GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "",
GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(),
GV->isExternallyInitialized());
NewGV->copyAttributesFrom(GV);
NewGV->takeName(GV);
assert(GV->use_empty() && "program cannot use initializer list");
GV->eraseFromParent();
return true;
}
bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
if (GV->getName() == "llvm.global_ctors" ||
GV->getName() == "llvm.global_dtors")
return UpgradeGlobalStructors(GV);
// Nothing to do yet.
return false;
}

View File

@@ -396,14 +396,22 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
"invalid linkage for intrinsic global variable", &GV);
// Don't worry about emitting an error for it not being an array,
// visitGlobalValue will complain on appending non-array.
if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getType())) {
if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getType()->getElementType())) {
StructType *STy = dyn_cast<StructType>(ATy->getElementType());
PointerType *FuncPtrTy =
FunctionType::get(Type::getVoidTy(*Context), false)->getPointerTo();
Assert1(STy && STy->getNumElements() == 2 &&
// FIXME: Reject the 2-field form in LLVM 4.0.
Assert1(STy && (STy->getNumElements() == 2 ||
STy->getNumElements() == 3) &&
STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
STy->getTypeAtIndex(1) == FuncPtrTy,
"wrong type for intrinsic global variable", &GV);
if (STy->getNumElements() == 3) {
Type *ETy = STy->getTypeAtIndex(2);
Assert1(ETy->isPointerTy() &&
cast<PointerType>(ETy)->getElementType()->isIntegerTy(8),
"wrong type for intrinsic global variable", &GV);
}
}
}