M, MergedTy, isConst, GlobalValue::PrivateLinkage, MergedInit,
"_MergedGlobals", nullptr, GlobalVariable::NotThreadLocal, AddrSpace);
- for (ssize_t k = i, idx = 0; k != j; k = GlobalSet.find_next(k)) {
+ for (ssize_t k = i, idx = 0; k != j; k = GlobalSet.find_next(k), ++idx) {
GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage();
std::string Name = Globals[k]->getName();
Constant *Idx[2] = {
ConstantInt::get(Int32Ty, 0),
- ConstantInt::get(Int32Ty, idx++)
+ ConstantInt::get(Int32Ty, idx),
};
Constant *GEP =
ConstantExpr::getInBoundsGetElementPtr(MergedTy, MergedGV, Idx);
// MergedGlobals variable) may be dead stripped at link time.
if (Linkage != GlobalValue::InternalLinkage ||
!TM->getTargetTriple().isOSBinFormatMachO()) {
- auto *PTy = cast<PointerType>(GEP->getType());
- GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
- Linkage, Name, GEP, &M);
+ GlobalAlias::create(Tys[idx], AddrSpace, Linkage, Name, GEP, &M);
}
NumMerged++;
const GlobalAlias *SGA) {
// If there is no linkage to be performed or we're linking from the source,
// bring over SGA.
- auto *PTy = cast<PointerType>(TypeMap.get(SGA->getType()));
- return GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
+ auto *Ty = TypeMap.get(SGA->getValueType());
+ return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
SGA->getLinkage(), SGA->getName(), &DstM);
}
const DataLayout &DL = M->getDataLayout();
for (GlobalVariable *G : Globals) {
GlobalInits.push_back(G->getInitializer());
- uint64_t InitSize = DL.getTypeAllocSize(G->getInitializer()->getType());
+ uint64_t InitSize = DL.getTypeAllocSize(G->getValueType());
// Compute the amount of padding required.
uint64_t Padding = NextPowerOf2(InitSize - 1) - InitSize;
if (!GlobalInits.empty())
GlobalInits.pop_back();
Constant *NewInit = ConstantStruct::getAnon(M->getContext(), GlobalInits);
- auto CombinedGlobal =
+ auto *CombinedGlobal =
new GlobalVariable(*M, NewInit->getType(), /*isConstant=*/true,
GlobalValue::PrivateLinkage, NewInit);
- const StructLayout *CombinedGlobalLayout =
- DL.getStructLayout(cast<StructType>(NewInit->getType()));
+ StructType *NewTy = cast<StructType>(NewInit->getType());
+ const StructLayout *CombinedGlobalLayout = DL.getStructLayout(NewTy);
// Compute the offsets of the original globals within the new global.
DenseMap<GlobalObject *, uint64_t> GlobalLayout;
if (LinkerSubsectionsViaSymbols) {
Globals[I]->replaceAllUsesWith(CombinedGlobalElemPtr);
} else {
- GlobalAlias *GAlias = GlobalAlias::create(
- Globals[I]->getType()->getElementType(),
- Globals[I]->getType()->getAddressSpace(), Globals[I]->getLinkage(),
- "", CombinedGlobalElemPtr, M);
+ assert(Globals[I]->getType()->getAddressSpace() == 0);
+ GlobalAlias *GAlias = GlobalAlias::create(NewTy->getElementType(I * 2), 0,
+ Globals[I]->getLinkage(), "",
+ CombinedGlobalElemPtr, M);
GAlias->setVisibility(Globals[I]->getVisibility());
GAlias->takeName(Globals[I]);
Globals[I]->replaceAllUsesWith(GAlias);
if (LinkerSubsectionsViaSymbols || Functions[I]->isDeclarationForLinker()) {
Functions[I]->replaceAllUsesWith(CombinedGlobalElemPtr);
} else {
- GlobalAlias *GAlias = GlobalAlias::create(
- Functions[I]->getType()->getElementType(),
- Functions[I]->getType()->getAddressSpace(),
- Functions[I]->getLinkage(), "", CombinedGlobalElemPtr, M);
+ assert(Functions[I]->getType()->getAddressSpace() == 0);
+ GlobalAlias *GAlias = GlobalAlias::create(Functions[I]->getValueType(), 0,
+ Functions[I]->getLinkage(), "",
+ CombinedGlobalElemPtr, M);
GAlias->setVisibility(Functions[I]->getVisibility());
GAlias->takeName(Functions[I]);
Functions[I]->replaceAllUsesWith(GAlias);
// Replace G with an alias to F and delete G.
void MergeFunctions::writeAlias(Function *F, Function *G) {
- PointerType *PTy = G->getType();
- auto *GA = GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
- G->getLinkage(), "", F);
+ auto *GA = GlobalAlias::create(G->getLinkage(), "", F);
F->setAlignment(std::max(F->getAlignment(), G->getAlignment()));
GA->takeName(G);
GA->setVisibility(G->getVisibility());
// correctness.
continue;
}
- auto *PTy = cast<PointerType>(I->getType());
- auto *GA =
- GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
- I->getLinkage(), I->getName(), New);
+ auto *GA = GlobalAlias::create(I->getValueType(),
+ I->getType()->getPointerAddressSpace(),
+ I->getLinkage(), I->getName(), New);
GA->copyAttributesFrom(I);
VMap[I] = GA;
}