-// LinkAppendingVars - If there were any appending global variables, link them
-// together now. Return true on error.
-static bool LinkAppendingVars(Module *M,
- std::multimap<std::string, GlobalVariable *> &AppendingVars,
- std::string *ErrorMsg) {
- if (AppendingVars.empty()) return false; // Nothing to do.
-
- // Loop over the multimap of appending vars, processing any variables with the
- // same name, forming a new appending global variable with both of the
- // initializers merged together, then rewrite references to the old variables
- // and delete them.
- std::vector<Constant*> Inits;
- while (AppendingVars.size() > 1) {
- // Get the first two elements in the map...
- std::multimap<std::string,
- GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++;
-
- // If the first two elements are for different names, there is no pair...
- // Otherwise there is a pair, so link them together...
- if (First->first == Second->first) {
- GlobalVariable *G1 = First->second, *G2 = Second->second;
- const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType());
- const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType());
-
- // Check to see that they two arrays agree on type...
- if (T1->getElementType() != T2->getElementType())
- return Error(ErrorMsg,
- "Appending variables with different element types need to be linked!");
- if (G1->isConstant() != G2->isConstant())
- return Error(ErrorMsg,
- "Appending variables linked with different const'ness!");
-
- if (G1->getAlignment() != G2->getAlignment())
- return Error(ErrorMsg,
- "Appending variables with different alignment need to be linked!");
-
- if (G1->getVisibility() != G2->getVisibility())
- return Error(ErrorMsg,
- "Appending variables with different visibility need to be linked!");
-
- if (G1->getSection() != G2->getSection())
- return Error(ErrorMsg,
- "Appending variables with different section name need to be linked!");
-
- unsigned NewSize = T1->getNumElements() + T2->getNumElements();
- ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize);
-
- G1->setName(""); // Clear G1's name in case of a conflict!
-
- // Create the new global variable...
- GlobalVariable *NG =
- new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(),
- /*init*/0, First->first, M, G1->isThreadLocal(),
- G1->getType()->getAddressSpace());
-
- // Propagate alignment, visibility and section info.
- CopyGVAttributes(NG, G1);
-
- // Merge the initializer...
- Inits.reserve(NewSize);
- if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) {
- for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
- Inits.push_back(I->getOperand(i));
- } else {
- assert(isa<ConstantAggregateZero>(G1->getInitializer()));
- Constant *CV = Constant::getNullValue(T1->getElementType());
- for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
- Inits.push_back(CV);
- }
- if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) {
- for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
- Inits.push_back(I->getOperand(i));
- } else {
- assert(isa<ConstantAggregateZero>(G2->getInitializer()));
- Constant *CV = Constant::getNullValue(T2->getElementType());
- for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
- Inits.push_back(CV);
- }
- NG->setInitializer(ConstantArray::get(NewType, Inits));
- Inits.clear();
-
- // Replace any uses of the two global variables with uses of the new
- // global...
-
- // FIXME: This should rewrite simple/straight-forward uses such as
- // getelementptr instructions to not use the Cast!
- G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G1->getType()));
- G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G2->getType()));
-
- // Remove the two globals from the module now...
- M->getGlobalList().erase(G1);
- M->getGlobalList().erase(G2);
-
- // Put the new global into the AppendingVars map so that we can handle
- // linking of more than two vars...
- Second->second = NG;
- }
- AppendingVars.erase(First);