- const PointerType *NewPT = PointerType::get(NewTy);
- Res = new StoreInst(NewVal, Constant::getNullValue(NewPT));
- VMC.ExprMap[I] = Res;
- Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), NewPT, VMC));
+ // Check to see if operand #1 has already been converted...
+ ValueMapCache::ExprMapTy::iterator VMCI =
+ VMC.ExprMap.find(I->getOperand(1));
+ if (VMCI != VMC.ExprMap.end()) {
+ // Comments describing this stuff are in the OperandConvertableToType
+ // switch statement for Store...
+ //
+ const Type *ElTy =
+ cast<PointerType>(VMCI->second->getType())->getElementType();
+
+ Value *SrcPtr = VMCI->second;
+
+ if (ElTy != NewTy) {
+ // We check that this is a struct in the initial scan...
+ const StructType *SElTy = cast<StructType>(ElTy);
+
+ std::vector<Value*> Indices;
+ Indices.push_back(Constant::getNullValue(Type::LongTy));
+
+ unsigned Offset = 0;
+ const Type *Ty = getStructOffsetType(ElTy, Offset, Indices, false);
+ assert(Offset == 0 && "Offset changed!");
+ assert(NewTy == Ty && "Did not convert to correct type!");
+
+ // Insert the GEP instruction before this store.
+ SrcPtr = new GetElementPtrInst(SrcPtr, Indices,
+ SrcPtr->getName()+".idx", I);
+ }
+ Res = new StoreInst(NewVal, SrcPtr);
+
+ VMC.ExprMap[I] = Res;
+ } else {
+ // Otherwise, we haven't converted Operand #1 over yet...
+ const PointerType *NewPT = PointerType::get(NewTy);
+ Res = new StoreInst(NewVal, Constant::getNullValue(NewPT));
+ VMC.ExprMap[I] = Res;
+ Res->setOperand(1, ConvertExpressionToType(I->getOperand(1),
+ NewPT, VMC));
+ }