diff options
Diffstat (limited to 'llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp')
| -rw-r--r-- | llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp | 262 | 
1 files changed, 125 insertions, 137 deletions
diff --git a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp index 61d519d8ae88..54c51b6e7161 100644 --- a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp +++ b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp @@ -22,16 +22,17 @@  #include "llvm/IR/Argument.h"  #include "llvm/IR/Attributes.h"  #include "llvm/IR/BasicBlock.h" -#include "llvm/IR/CallSite.h"  #include "llvm/IR/Constants.h"  #include "llvm/IR/DerivedTypes.h"  #include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h"  #include "llvm/IR/InstrTypes.h"  #include "llvm/IR/Instruction.h"  #include "llvm/IR/Instructions.h"  #include "llvm/IR/IntrinsicInst.h"  #include "llvm/IR/Intrinsics.h"  #include "llvm/IR/Module.h" +#include "llvm/IR/NoFolder.h"  #include "llvm/IR/PassManager.h"  #include "llvm/IR/Type.h"  #include "llvm/IR/Use.h" @@ -175,16 +176,15 @@ bool DeadArgumentEliminationPass::DeleteDeadVarargs(Function &Fn) {    //    std::vector<Value *> Args;    for (Value::user_iterator I = Fn.user_begin(), E = Fn.user_end(); I != E; ) { -    CallSite CS(*I++); -    if (!CS) +    CallBase *CB = dyn_cast<CallBase>(*I++); +    if (!CB)        continue; -    Instruction *Call = CS.getInstruction();      // Pass all the same arguments. -    Args.assign(CS.arg_begin(), CS.arg_begin() + NumArgs); +    Args.assign(CB->arg_begin(), CB->arg_begin() + NumArgs);      // Drop any attributes that were on the vararg arguments. -    AttributeList PAL = CS.getAttributes(); +    AttributeList PAL = CB->getAttributes();      if (!PAL.isEmpty()) {        SmallVector<AttributeSet, 8> ArgAttrs;        for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo) @@ -194,34 +194,31 @@ bool DeadArgumentEliminationPass::DeleteDeadVarargs(Function &Fn) {      }      SmallVector<OperandBundleDef, 1> OpBundles; -    CS.getOperandBundlesAsDefs(OpBundles); +    CB->getOperandBundlesAsDefs(OpBundles); -    CallSite NewCS; -    if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { -      NewCS = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), -                                 Args, OpBundles, "", Call); +    CallBase *NewCB = nullptr; +    if (InvokeInst *II = dyn_cast<InvokeInst>(CB)) { +      NewCB = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), +                                 Args, OpBundles, "", CB);      } else { -      NewCS = CallInst::Create(NF, Args, OpBundles, "", Call); -      cast<CallInst>(NewCS.getInstruction()) -          ->setTailCallKind(cast<CallInst>(Call)->getTailCallKind()); +      NewCB = CallInst::Create(NF, Args, OpBundles, "", CB); +      cast<CallInst>(NewCB)->setTailCallKind( +          cast<CallInst>(CB)->getTailCallKind());      } -    NewCS.setCallingConv(CS.getCallingConv()); -    NewCS.setAttributes(PAL); -    NewCS->setDebugLoc(Call->getDebugLoc()); -    uint64_t W; -    if (Call->extractProfTotalWeight(W)) -      NewCS->setProfWeight(W); +    NewCB->setCallingConv(CB->getCallingConv()); +    NewCB->setAttributes(PAL); +    NewCB->copyMetadata(*CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});      Args.clear(); -    if (!Call->use_empty()) -      Call->replaceAllUsesWith(NewCS.getInstruction()); +    if (!CB->use_empty()) +      CB->replaceAllUsesWith(NewCB); -    NewCS->takeName(Call); +    NewCB->takeName(CB);      // Finally, remove the old call from the program, reducing the use-count of      // F. -    Call->eraseFromParent(); +    CB->eraseFromParent();    }    // Since we have now created the new function, splice the body of the old @@ -291,7 +288,8 @@ bool DeadArgumentEliminationPass::RemoveDeadArgumentsFromCallers(Function &Fn) {    bool Changed = false;    for (Argument &Arg : Fn.args()) { -    if (!Arg.hasSwiftErrorAttr() && Arg.use_empty() && !Arg.hasByValOrInAllocaAttr()) { +    if (!Arg.hasSwiftErrorAttr() && Arg.use_empty() && +        !Arg.hasPassPointeeByValueAttr()) {        if (Arg.isUsedByMetadata()) {          Arg.replaceAllUsesWith(UndefValue::get(Arg.getType()));          Changed = true; @@ -304,16 +302,16 @@ bool DeadArgumentEliminationPass::RemoveDeadArgumentsFromCallers(Function &Fn) {      return false;    for (Use &U : Fn.uses()) { -    CallSite CS(U.getUser()); -    if (!CS || !CS.isCallee(&U)) +    CallBase *CB = dyn_cast<CallBase>(U.getUser()); +    if (!CB || !CB->isCallee(&U))        continue;      // Now go through all unused args and replace them with "undef".      for (unsigned I = 0, E = UnusedArgs.size(); I != E; ++I) {        unsigned ArgNo = UnusedArgs[I]; -      Value *Arg = CS.getArgument(ArgNo); -      CS.setArgument(ArgNo, UndefValue::get(Arg->getType())); +      Value *Arg = CB->getArgOperand(ArgNo); +      CB->setArgOperand(ArgNo, UndefValue::get(Arg->getType()));        ++NumArgumentsReplacedWithUndef;        Changed = true;      } @@ -391,8 +389,8 @@ DeadArgumentEliminationPass::SurveyUse(const Use *U, UseVector &MaybeLiveUses,          return MarkIfNotLive(Use, MaybeLiveUses);        } else {          DeadArgumentEliminationPass::Liveness Result = MaybeLive; -        for (unsigned i = 0; i < NumRetVals(F); ++i) { -          RetOrArg Use = CreateRet(F, i); +        for (unsigned Ri = 0; Ri < NumRetVals(F); ++Ri) { +          RetOrArg Use = CreateRet(F, Ri);            // We might be live, depending on the liveness of Use. If any            // sub-value is live, then the entire value is considered live. This            // is a conservative choice, and better tracking is possible. @@ -424,28 +422,27 @@ DeadArgumentEliminationPass::SurveyUse(const Use *U, UseVector &MaybeLiveUses,        return Result;      } -    if (auto CS = ImmutableCallSite(V)) { -      const Function *F = CS.getCalledFunction(); +    if (const auto *CB = dyn_cast<CallBase>(V)) { +      const Function *F = CB->getCalledFunction();        if (F) {          // Used in a direct call.          // The function argument is live if it is used as a bundle operand. -        if (CS.isBundleOperand(U)) +        if (CB->isBundleOperand(U))            return Live;          // Find the argument number. We know for sure that this use is an          // argument, since if it was the function argument this would be an          // indirect call and the we know can't be looking at a value of the          // label type (for the invoke instruction). -        unsigned ArgNo = CS.getArgumentNo(U); +        unsigned ArgNo = CB->getArgOperandNo(U);          if (ArgNo >= F->getFunctionType()->getNumParams())            // The value is passed in through a vararg! Must be live.            return Live; -        assert(CS.getArgument(ArgNo) -               == CS->getOperand(U->getOperandNo()) -               && "Argument is not where we expected it"); +        assert(CB->getArgOperand(ArgNo) == CB->getOperand(U->getOperandNo()) && +               "Argument is not where we expected it");          // Value passed to a normal call. It's only live when the corresponding          // argument to the called function turns out live. @@ -485,9 +482,10 @@ DeadArgumentEliminationPass::SurveyUses(const Value *V,  // We consider arguments of non-internal functions to be intrinsically alive as  // well as arguments to functions which have their "address taken".  void DeadArgumentEliminationPass::SurveyFunction(const Function &F) { -  // Functions with inalloca parameters are expecting args in a particular -  // register and memory layout. -  if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca)) { +  // Functions with inalloca/preallocated parameters are expecting args in a +  // particular register and memory layout. +  if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) || +      F.getAttributes().hasAttrSomewhere(Attribute::Preallocated)) {      MarkLive(F);      return;    } @@ -555,24 +553,17 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {    for (const Use &U : F.uses()) {      // If the function is PASSED IN as an argument, its address has been      // taken. -    ImmutableCallSite CS(U.getUser()); -    if (!CS || !CS.isCallee(&U)) { +    const auto *CB = dyn_cast<CallBase>(U.getUser()); +    if (!CB || !CB->isCallee(&U)) {        MarkLive(F);        return;      }      // The number of arguments for `musttail` call must match the number of      // arguments of the caller -    if (CS.isMustTailCall()) +    if (CB->isMustTailCall())        HasMustTailCallers = true; -    // If this use is anything other than a call site, the function is alive. -    const Instruction *TheCall = CS.getInstruction(); -    if (!TheCall) {   // Not a direct call site? -      MarkLive(F); -      return; -    } -      // If we end up here, we are looking at a direct call to our function.      // Now, check how our return value(s) is/are used in this caller. Don't @@ -581,7 +572,7 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {        continue;      // Check all uses of the return value. -    for (const Use &U : TheCall->uses()) { +    for (const Use &U : CB->uses()) {        if (ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(U.getUser())) {          // This use uses a part of our return value, survey the uses of          // that part and store the results for this index only. @@ -600,10 +591,10 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {            RetValLiveness.assign(RetCount, Live);            break;          } else { -          for (unsigned i = 0; i != RetCount; ++i) { -            if (RetValLiveness[i] != Live) -              MaybeLiveRetUses[i].append(MaybeLiveAggregateUses.begin(), -                                         MaybeLiveAggregateUses.end()); +          for (unsigned Ri = 0; Ri != RetCount; ++Ri) { +            if (RetValLiveness[Ri] != Live) +              MaybeLiveRetUses[Ri].append(MaybeLiveAggregateUses.begin(), +                                          MaybeLiveAggregateUses.end());            }          }        } @@ -616,17 +607,17 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {    }    // Now we've inspected all callers, record the liveness of our return values. -  for (unsigned i = 0; i != RetCount; ++i) -    MarkValue(CreateRet(&F, i), RetValLiveness[i], MaybeLiveRetUses[i]); +  for (unsigned Ri = 0; Ri != RetCount; ++Ri) +    MarkValue(CreateRet(&F, Ri), RetValLiveness[Ri], MaybeLiveRetUses[Ri]);    LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Inspecting args for fn: "                      << F.getName() << "\n");    // Now, check all of our arguments. -  unsigned i = 0; +  unsigned ArgI = 0;    UseVector MaybeLiveArgUses; -  for (Function::const_arg_iterator AI = F.arg_begin(), -       E = F.arg_end(); AI != E; ++AI, ++i) { +  for (Function::const_arg_iterator AI = F.arg_begin(), E = F.arg_end(); +       AI != E; ++AI, ++ArgI) {      Liveness Result;      if (F.getFunctionType()->isVarArg() || HasMustTailCallers ||          HasMustTailCalls) { @@ -649,7 +640,7 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {      }      // Mark the result. -    MarkValue(CreateArg(&F, i), Result, MaybeLiveArgUses); +    MarkValue(CreateArg(&F, ArgI), Result, MaybeLiveArgUses);      // Clear the vector again for the next iteration.      MaybeLiveArgUses.clear();    } @@ -684,11 +675,11 @@ void DeadArgumentEliminationPass::MarkLive(const Function &F) {    // Mark the function as live.    LiveFunctions.insert(&F);    // Mark all arguments as live. -  for (unsigned i = 0, e = F.arg_size(); i != e; ++i) -    PropagateLiveness(CreateArg(&F, i)); +  for (unsigned ArgI = 0, E = F.arg_size(); ArgI != E; ++ArgI) +    PropagateLiveness(CreateArg(&F, ArgI));    // Mark all return values as live. -  for (unsigned i = 0, e = NumRetVals(&F); i != e; ++i) -    PropagateLiveness(CreateRet(&F, i)); +  for (unsigned Ri = 0, E = NumRetVals(&F); Ri != E; ++Ri) +    PropagateLiveness(CreateRet(&F, Ri));  }  /// MarkLive - Mark the given return value or argument as live. Additionally, @@ -749,19 +740,19 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {    // Construct the new parameter list from non-dead arguments. Also construct    // a new set of parameter attributes to correspond. Skip the first parameter    // attribute, since that belongs to the return value. -  unsigned i = 0; -  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); -       I != E; ++I, ++i) { -    RetOrArg Arg = CreateArg(F, i); +  unsigned ArgI = 0; +  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; +       ++I, ++ArgI) { +    RetOrArg Arg = CreateArg(F, ArgI);      if (LiveValues.erase(Arg)) {        Params.push_back(I->getType()); -      ArgAlive[i] = true; -      ArgAttrVec.push_back(PAL.getParamAttributes(i)); -      HasLiveReturnedArg |= PAL.hasParamAttribute(i, Attribute::Returned); +      ArgAlive[ArgI] = true; +      ArgAttrVec.push_back(PAL.getParamAttributes(ArgI)); +      HasLiveReturnedArg |= PAL.hasParamAttribute(ArgI, Attribute::Returned);      } else {        ++NumArgumentsEliminated;        LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing argument " -                        << i << " (" << I->getName() << ") from " +                        << ArgI << " (" << I->getName() << ") from "                          << F->getName() << "\n");      }    } @@ -798,16 +789,16 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {      NRetTy = RetTy;    } else {      // Look at each of the original return values individually. -    for (unsigned i = 0; i != RetCount; ++i) { -      RetOrArg Ret = CreateRet(F, i); +    for (unsigned Ri = 0; Ri != RetCount; ++Ri) { +      RetOrArg Ret = CreateRet(F, Ri);        if (LiveValues.erase(Ret)) { -        RetTypes.push_back(getRetComponentType(F, i)); -        NewRetIdxs[i] = RetTypes.size() - 1; +        RetTypes.push_back(getRetComponentType(F, Ri)); +        NewRetIdxs[Ri] = RetTypes.size() - 1;        } else {          ++NumRetValsEliminated;          LLVM_DEBUG(              dbgs() << "DeadArgumentEliminationPass - Removing return value " -                   << i << " from " << F->getName() << "\n"); +                   << Ri << " from " << F->getName() << "\n");        }      }      if (RetTypes.size() > 1) { @@ -876,11 +867,10 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {    // to pass in a smaller number of arguments into the new function.    std::vector<Value*> Args;    while (!F->use_empty()) { -    CallSite CS(F->user_back()); -    Instruction *Call = CS.getInstruction(); +    CallBase &CB = cast<CallBase>(*F->user_back());      ArgAttrVec.clear(); -    const AttributeList &CallPAL = CS.getAttributes(); +    const AttributeList &CallPAL = CB.getAttributes();      // Adjust the call return attributes in case the function was changed to      // return void. @@ -890,15 +880,15 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {      // Declare these outside of the loops, so we can reuse them for the second      // loop, which loops the varargs. -    CallSite::arg_iterator I = CS.arg_begin(); -    unsigned i = 0; +    auto I = CB.arg_begin(); +    unsigned Pi = 0;      // Loop over those operands, corresponding to the normal arguments to the      // original function, and add those that are still alive. -    for (unsigned e = FTy->getNumParams(); i != e; ++I, ++i) -      if (ArgAlive[i]) { +    for (unsigned E = FTy->getNumParams(); Pi != E; ++I, ++Pi) +      if (ArgAlive[Pi]) {          Args.push_back(*I);          // Get original parameter attributes, but skip return attributes. -        AttributeSet Attrs = CallPAL.getParamAttributes(i); +        AttributeSet Attrs = CallPAL.getParamAttributes(Pi);          if (NRetTy != RetTy && Attrs.hasAttribute(Attribute::Returned)) {            // If the return type has changed, then get rid of 'returned' on the            // call site. The alternative is to make all 'returned' attributes on @@ -915,9 +905,9 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {        }      // Push any varargs arguments on the list. Don't forget their attributes. -    for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) { +    for (auto E = CB.arg_end(); I != E; ++I, ++Pi) {        Args.push_back(*I); -      ArgAttrVec.push_back(CallPAL.getParamAttributes(i)); +      ArgAttrVec.push_back(CallPAL.getParamAttributes(Pi));      }      // Reconstruct the AttributesList based on the vector we constructed. @@ -932,44 +922,41 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {          F->getContext(), FnAttrs, RetAttrs, ArgAttrVec);      SmallVector<OperandBundleDef, 1> OpBundles; -    CS.getOperandBundlesAsDefs(OpBundles); +    CB.getOperandBundlesAsDefs(OpBundles); -    CallSite NewCS; -    if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { -      NewCS = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), -                                 Args, OpBundles, "", Call->getParent()); +    CallBase *NewCB = nullptr; +    if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) { +      NewCB = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(), +                                 Args, OpBundles, "", CB.getParent());      } else { -      NewCS = CallInst::Create(NFTy, NF, Args, OpBundles, "", Call); -      cast<CallInst>(NewCS.getInstruction()) -          ->setTailCallKind(cast<CallInst>(Call)->getTailCallKind()); +      NewCB = CallInst::Create(NFTy, NF, Args, OpBundles, "", &CB); +      cast<CallInst>(NewCB)->setTailCallKind( +          cast<CallInst>(&CB)->getTailCallKind());      } -    NewCS.setCallingConv(CS.getCallingConv()); -    NewCS.setAttributes(NewCallPAL); -    NewCS->setDebugLoc(Call->getDebugLoc()); -    uint64_t W; -    if (Call->extractProfTotalWeight(W)) -      NewCS->setProfWeight(W); +    NewCB->setCallingConv(CB.getCallingConv()); +    NewCB->setAttributes(NewCallPAL); +    NewCB->copyMetadata(CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});      Args.clear();      ArgAttrVec.clear(); -    Instruction *New = NewCS.getInstruction(); -    if (!Call->use_empty() || Call->isUsedByMetadata()) { -      if (New->getType() == Call->getType()) { +    if (!CB.use_empty() || CB.isUsedByMetadata()) { +      if (NewCB->getType() == CB.getType()) {          // Return type not changed? Just replace users then. -        Call->replaceAllUsesWith(New); -        New->takeName(Call); -      } else if (New->getType()->isVoidTy()) { +        CB.replaceAllUsesWith(NewCB); +        NewCB->takeName(&CB); +      } else if (NewCB->getType()->isVoidTy()) {          // If the return value is dead, replace any uses of it with undef          // (any non-debug value uses will get removed later on). -        if (!Call->getType()->isX86_MMXTy()) -          Call->replaceAllUsesWith(UndefValue::get(Call->getType())); +        if (!CB.getType()->isX86_MMXTy()) +          CB.replaceAllUsesWith(UndefValue::get(CB.getType()));        } else {          assert((RetTy->isStructTy() || RetTy->isArrayTy()) &&                 "Return type changed, but not into a void. The old return type"                 " must have been a struct or an array!"); -        Instruction *InsertPt = Call; -        if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) { -          BasicBlock *NewEdge = SplitEdge(New->getParent(), II->getNormalDest()); +        Instruction *InsertPt = &CB; +        if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) { +          BasicBlock *NewEdge = +              SplitEdge(NewCB->getParent(), II->getNormalDest());            InsertPt = &*NewEdge->getFirstInsertionPt();          } @@ -979,30 +966,30 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {          //          // Start out building up our return value from undef          Value *RetVal = UndefValue::get(RetTy); -        for (unsigned i = 0; i != RetCount; ++i) -          if (NewRetIdxs[i] != -1) { +        for (unsigned Ri = 0; Ri != RetCount; ++Ri) +          if (NewRetIdxs[Ri] != -1) {              Value *V; +            IRBuilder<NoFolder> IRB(InsertPt);              if (RetTypes.size() > 1)                // We are still returning a struct, so extract the value from our                // return value -              V = ExtractValueInst::Create(New, NewRetIdxs[i], "newret", -                                           InsertPt); +              V = IRB.CreateExtractValue(NewCB, NewRetIdxs[Ri], "newret");              else                // We are now returning a single element, so just insert that -              V = New; +              V = NewCB;              // Insert the value at the old position -            RetVal = InsertValueInst::Create(RetVal, V, i, "oldret", InsertPt); +            RetVal = IRB.CreateInsertValue(RetVal, V, Ri, "oldret");            }          // Now, replace all uses of the old call instruction with the return          // struct we built -        Call->replaceAllUsesWith(RetVal); -        New->takeName(Call); +        CB.replaceAllUsesWith(RetVal); +        NewCB->takeName(&CB);        }      }      // Finally, remove the old call from the program, reducing the use-count of      // F. -    Call->eraseFromParent(); +    CB.eraseFromParent();    }    // Since we have now created the new function, splice the body of the old @@ -1012,10 +999,11 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {    // Loop over the argument list, transferring uses of the old arguments over to    // the new arguments, also transferring over the names as well. -  i = 0; +  ArgI = 0;    for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), -       I2 = NF->arg_begin(); I != E; ++I, ++i) -    if (ArgAlive[i]) { +                              I2 = NF->arg_begin(); +       I != E; ++I, ++ArgI) +    if (ArgAlive[ArgI]) {        // If this is a live argument, move the name and users over to the new        // version.        I->replaceAllUsesWith(&*I2); @@ -1033,11 +1021,10 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {    if (F->getReturnType() != NF->getReturnType())      for (BasicBlock &BB : *NF)        if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) { -        Value *RetVal; +        IRBuilder<NoFolder> IRB(RI); +        Value *RetVal = nullptr; -        if (NFTy->getReturnType()->isVoidTy()) { -          RetVal = nullptr; -        } else { +        if (!NFTy->getReturnType()->isVoidTy()) {            assert(RetTy->isStructTy() || RetTy->isArrayTy());            // The original return value was a struct or array, insert            // extractvalue/insertvalue chains to extract only the values we need @@ -1047,16 +1034,16 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {            Value *OldRet = RI->getOperand(0);            // Start out building up our return value from undef            RetVal = UndefValue::get(NRetTy); -          for (unsigned i = 0; i != RetCount; ++i) -            if (NewRetIdxs[i] != -1) { -              ExtractValueInst *EV = ExtractValueInst::Create(OldRet, i, -                                                              "oldret", RI); +          for (unsigned RetI = 0; RetI != RetCount; ++RetI) +            if (NewRetIdxs[RetI] != -1) { +              Value *EV = IRB.CreateExtractValue(OldRet, RetI, "oldret"); +                if (RetTypes.size() > 1) {                  // We're still returning a struct, so reinsert the value into                  // our new return value at the new index -                RetVal = InsertValueInst::Create(RetVal, EV, NewRetIdxs[i], -                                                 "newret", RI); +                RetVal = IRB.CreateInsertValue(RetVal, EV, NewRetIdxs[RetI], +                                               "newret");                } else {                  // We are now only returning a simple value, so just return the                  // extracted value. @@ -1066,7 +1053,8 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {          }          // Replace the return instruction with one returning the new return          // value (possibly 0 if we became void). -        ReturnInst::Create(F->getContext(), RetVal, RI); +        auto *NewRet = ReturnInst::Create(F->getContext(), RetVal, RI); +        NewRet->setDebugLoc(RI->getDebugLoc());          BB.getInstList().erase(RI);        }  | 
