-void DAE::MarkReturnInstArgumentLive(ReturnInst *RI) {
- Value *Op = RI->getOperand(0);
- if (Argument *A = dyn_cast<Argument>(Op)) {
- MarkArgumentLive(A);
- } else if (CallInst *CI = dyn_cast<CallInst>(Op)) {
- if (Function *F = CI->getCalledFunction())
- MarkRetValLive(F);
- } else if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
- if (Function *F = II->getCalledFunction())
- MarkRetValLive(F);
+ // Find out the new return value.
+
+ const Type *RetTy = FTy->getReturnType();
+ const Type *NRetTy = NULL;
+ unsigned RetCount = NumRetVals(F);
+ // Explicitly track if anything changed, for debugging.
+ bool Changed = false;
+ // -1 means unused, other numbers are the new index
+ SmallVector<int, 5> NewRetIdxs(RetCount, -1);
+ std::vector<const Type*> RetTypes;
+ if (RetTy == Type::VoidTy) {
+ NRetTy = Type::VoidTy;
+ } else {
+ const StructType *STy = dyn_cast<StructType>(RetTy);
+ if (STy)
+ // Look at each of the original return values individually.
+ for (unsigned i = 0; i != RetCount; ++i) {
+ RetOrArg Ret = CreateRet(F, i);
+ if (LiveValues.erase(Ret)) {
+ RetTypes.push_back(STy->getElementType(i));
+ NewRetIdxs[i] = RetTypes.size() - 1;
+ } else {
+ ++NumRetValsEliminated;
+ DOUT << "DAE - Removing return value " << i << " from "
+ << F->getNameStart() << "\n";
+ Changed = true;
+ }
+ }
+ else
+ // We used to return a single value.
+ if (LiveValues.erase(CreateRet(F, 0))) {
+ RetTypes.push_back(RetTy);
+ NewRetIdxs[0] = 0;
+ } else {
+ DOUT << "DAE - Removing return value from " << F->getNameStart()
+ << "\n";
+ ++NumRetValsEliminated;
+ Changed = true;
+ }
+ if (RetTypes.size() > 1 || (STy && STy->getNumElements()==RetTypes.size()))
+ // More than one return type? Return a struct with them. Also, if we used
+ // to return a struct and didn't change the number of return values,
+ // return a struct again. This prevents changing {something} into
+ // something and {} into void.
+ // Make the new struct packed if we used to return a packed struct
+ // already.
+ NRetTy = StructType::get(RetTypes, STy->isPacked());
+ else if (RetTypes.size() == 1)
+ // One return type? Just a simple value then, but only if we didn't use to
+ // return a struct with that simple value before.
+ NRetTy = RetTypes.front();
+ else if (RetTypes.size() == 0)
+ // No return types? Make it void, but only if we didn't use to return {}.
+ NRetTy = Type::VoidTy;