- // Fill values with the modified operands of the constant array. Also,
- // compute whether this turns into an all-zeros array.
- bool isAllZeros = false;
- unsigned NumUpdated = 0;
- if (!ToC->isNullValue()) {
- for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
- O != E; ++O) {
- Constant *Val = cast<Constant>(O->get());
- if (Val == From) {
- Val = ToC;
- ++NumUpdated;
- }
- Values.push_back(Val);
- }
- } else {
- isAllZeros = true;
- for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
- O != E; ++O) {
- Constant *Val = cast<Constant>(O->get());
- if (Val == From) {
- Val = ToC;
- ++NumUpdated;
- }
- Values.push_back(Val);
- if (isAllZeros) isAllZeros = Val->isNullValue();
- }
- }
-
- Constant *Replacement = 0;
- if (isAllZeros) {
- Replacement = Context.getConstantAggregateZero(CA->getType());
- } else {
- // Check to see if we have this array type already.
- sys::SmartScopedWriter<true> Writer(ConstantsLock);
- bool Exists;
- ArrayConstantsTy::MapTy::iterator I =
- ArrayConstants.InsertOrGetItem(Lookup, Exists);
-
- if (Exists) {
- Replacement = I->second;
- } else {
- // Okay, the new shape doesn't exist in the system yet. Instead of
- // creating a new constant array, inserting it, replaceallusesof'ing the
- // old with the new, then deleting the old... just update the current one
- // in place!
- ArrayConstants.MoveConstantToNewSlot(CA, I);
-
- // Update to the new value. Optimize for the case when we have a single
- // operand that we're changing, but handle bulk updates efficiently.
- if (NumUpdated == 1) {
- unsigned OperandToUpdate = U - CA->OperandList;
- assert(CA->getOperand(OperandToUpdate) == From &&
- "ReplaceAllUsesWith broken!");
- CA->setOperand(OperandToUpdate, ToC);
- } else {
- for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
- if (CA->getOperand(i) == From)
- CA->setOperand(i, ToC);
- }
- return 0;
- }
- }
-
- return Replacement;
-}
-
-Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantStruct *CS,
- Value *From, Value *To, Use *U) {
- assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
- Constant *ToC = cast<Constant>(To);
-
- unsigned OperandToUpdate = U - CS->OperandList;
- assert(CS->getOperand(OperandToUpdate) == From &&
- "ReplaceAllUsesWith broken!");
-
- std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
- Lookup.first.first = CS->getType();
- Lookup.second = CS;
- std::vector<Constant*> &Values = Lookup.first.second;
- Values.reserve(CS->getNumOperands()); // Build replacement struct.
-
-
- // Fill values with the modified operands of the constant struct. Also,
- // compute whether this turns into an all-zeros struct.
- bool isAllZeros = false;
- if (!ToC->isNullValue()) {
- for (Use *O = CS->OperandList, *E = CS->OperandList + CS->getNumOperands();
- O != E; ++O)
- Values.push_back(cast<Constant>(O->get()));
- } else {
- isAllZeros = true;
- for (Use *O = CS->OperandList, *E = CS->OperandList + CS->getNumOperands();
- O != E; ++O) {
- Constant *Val = cast<Constant>(O->get());
- Values.push_back(Val);
- if (isAllZeros) isAllZeros = Val->isNullValue();
- }
- }
- Values[OperandToUpdate] = ToC;
-
- Constant *Replacement = 0;
- if (isAllZeros) {
- Replacement = Context.getConstantAggregateZero(CS->getType());
- } else {
- // Check to see if we have this array type already.
- sys::SmartScopedWriter<true> Writer(ConstantsLock);
- bool Exists;
- StructConstantsTy::MapTy::iterator I =
- StructConstants.InsertOrGetItem(Lookup, Exists);
-
- if (Exists) {
- Replacement = I->second;
- } else {
- // Okay, the new shape doesn't exist in the system yet. Instead of
- // creating a new constant struct, inserting it, replaceallusesof'ing the
- // old with the new, then deleting the old... just update the current one
- // in place!
- StructConstants.MoveConstantToNewSlot(CS, I);
-
- // Update to the new value.
- CS->setOperand(OperandToUpdate, ToC);
- return 0;
- }
- }
-
- assert(Replacement != CS && "I didn't contain From!");
-
- return Replacement;
-}