+
+ // Handle PHI nodes. PHI nodes must be merging in the same values, plus
+ // potentially the original malloc. Insert phi nodes for each field, then
+ // process uses of the PHI.
+ PHINode *PN = cast<PHINode>(LoadUser);
+ std::vector<Value *> PHIsForField;
+ PHIsForField.resize(FieldGlobals.size());
+ for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
+ Value *LoadV = GetHeapSROALoad(Load, i, FieldGlobals, InsertedLoadsForPtr);
+
+ PHINode *FieldPN = new PHINode(LoadV->getType(),
+ PN->getName()+"."+utostr(i), PN);
+ // Fill in the predecessor values.
+ for (unsigned pred = 0, e = PN->getNumIncomingValues(); pred != e; ++pred) {
+ // Each predecessor either uses the load or the original malloc.
+ Value *InVal = PN->getIncomingValue(pred);
+ BasicBlock *BB = PN->getIncomingBlock(pred);
+ Value *NewVal;
+ if (isa<MallocInst>(InVal)) {
+ // Insert a reload from the global in the predecessor.
+ NewVal = GetHeapSROALoad(BB->getTerminator(), i, FieldGlobals,
+ PHIsForField);
+ } else {
+ NewVal = InsertedLoadsForPtr[i];
+ }
+ FieldPN->addIncoming(NewVal, BB);
+ }
+ PHIsForField[i] = FieldPN;
+ }
+
+ // Since PHIsForField specifies a phi for every input value, the lazy inserter
+ // will never insert a load.
+ while (!PN->use_empty())
+ RewriteHeapSROALoadUser(Load, PN->use_back(), FieldGlobals, PHIsForField);
+ PN->eraseFromParent();
+}
+
+/// RewriteUsesOfLoadForHeapSRoA - We are performing Heap SRoA on a global. Ptr
+/// is a value loaded from the global. Eliminate all uses of Ptr, making them
+/// use FieldGlobals instead. All uses of loaded values satisfy
+/// GlobalLoadUsesSimpleEnoughForHeapSRA.
+static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
+ const std::vector<GlobalVariable*> &FieldGlobals) {
+ std::vector<Value *> InsertedLoadsForPtr;
+ //InsertedLoadsForPtr.resize(FieldGlobals.size());
+ while (!Load->use_empty())
+ RewriteHeapSROALoadUser(Load, Load->use_back(),
+ FieldGlobals, InsertedLoadsForPtr);