-void BitcodeReaderValueList::resize(unsigned Desired) {
- if (Desired > Capacity) {
- // Since we expect many values to come from the bitcode file we better
- // allocate the double amount, so that the array size grows exponentially
- // at each reallocation. Also, add a small amount of 100 extra elements
- // each time, to reallocate less frequently when the array is still small.
- //
- Capacity = Desired * 2 + 100;
- Use *New = allocHungoffUses(Capacity);
- Use *Old = OperandList;
- unsigned Ops = getNumOperands();
- for (int i(Ops - 1); i >= 0; --i)
- New[i] = Old[i].get();
- OperandList = New;
- if (Old) Use::zap(Old, Old + Ops, true);
+
+void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
+ if (Idx == size()) {
+ push_back(V);
+ return;
+ }
+
+ if (Idx >= size())
+ resize(Idx+1);
+
+ WeakVH &OldV = ValuePtrs[Idx];
+ if (OldV == 0) {
+ OldV = V;
+ return;
+ }
+
+ // Handle constants and non-constants (e.g. instrs) differently for
+ // efficiency.
+ if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
+ ResolveConstants.push_back(std::make_pair(PHC, Idx));
+ OldV = V;
+ } else {
+ // If there was a forward reference to this value, replace it.
+ Value *PrevVal = OldV;
+ OldV->replaceAllUsesWith(V);
+ delete PrevVal;