+ // Update auto-upgraded malloc calls from "autoupgrade_malloc" to "malloc".
+ if (MallocF) {
+ MallocF->setName("malloc");
+ // If setName() does not set the name to "malloc", then there is already a
+ // declaration of "malloc". In that case, iterate over all calls to MallocF
+ // and get them to call the declared "malloc" instead.
+ if (MallocF->getName() != "malloc") {
+ Function* realMallocF = M->getFunction("malloc");
+ for (User::use_iterator UI = MallocF->use_begin(), UE= MallocF->use_end();
+ UI != UE; ) {
+ User* user = *UI;
+ UI++;
+ if (CallInst *Call = dyn_cast<CallInst>(user))
+ Call->setCalledFunction(realMallocF);
+ }
+ if (!realMallocF->doesNotAlias(0)) realMallocF->setDoesNotAlias(0);
+ MallocF->eraseFromParent();
+ MallocF = NULL;
+ }
+ }
+