Repository: apple/swift
File: lib/IRGen/LoadableByAddress.cpp (C++)

Code before:
    SILValue currOperand = operand.get();
    currOperand = getOperandTypeWithCastIfNecessary(
        applyInst, currOperand, *currIRMod, applyBuilder, MapperCache);
    callArgs.push_back(currOperand);
  }
  // Recreate apply with new operands due to substitution-type cache
  switch (applyInst->getKind()) {
  case SILInstructionKind::ApplyInst: {
    auto *castedApply = cast<ApplyInst>(applyInst);
    SILValue newApply =
      applyBuilder.createApply(castedApply->getLoc(), callee,
                               applySite.getSubstitutionMap(),
                               callArgs,
                               castedApply->getApplyOptions());
    castedApply->replaceAllUsesWith(newApply);

    // For guaranteed address results, replace the pre-allocated stack slot
    // with the returned address
    if (modNonFuncTypeResultType(genEnv, origSILFunctionType, *currIRMod)) {
      auto allocIt = allApplyRetToAllocMap.find(applyInst);
      if (allocIt != allApplyRetToAllocMap.end()) {
        // Check if the new function returns a guaranteed address
        if (newSILFunctionType->getNumResults() == 1) {
          auto resultInfo = newSILFunctionType->getSingleResult();
          if (resultInfo.getConvention() ==
              ResultConvention::GuaranteedAddress) {
            auto *stackAlloc = cast<AllocStackInst>(allocIt->second);

            // Delete all dealloc_stack instructions for this allocation
            for (auto *use : stackAlloc->getUses()) {
              if (auto *dealloc = dyn_cast<DeallocStackInst>(use->getUser())) {
                dealloc->eraseFromParent();
              }
            }

            stackAlloc->replaceAllUsesWith(newApply);
            stackAlloc->eraseFromParent();
          }
        }
      }
    }

    break;
  }
  case SILInstructionKind::TryApplyInst: {
    auto *castedApply = cast<TryApplyInst>(applyInst);
    auto *newTryApply = applyBuilder.createTryApply(
        castedApply->getLoc(), callee, applySite.getSubstitutionMap(), callArgs,
        castedApply->getNormalBB(), castedApply->getErrorBB(),
        castedApply->getApplyOptions());


Review comment (suggestion):
A function with `@guaranteed_address` result will always have a single result. You can add this invariant as an `assert`  instead of checking above

Code after:
  for (Operand &operand : applySite.getArgumentOperands()) {
    SILValue currOperand = operand.get();
    currOperand = getOperandTypeWithCastIfNecessary(
        applyInst, currOperand, *currIRMod, applyBuilder, MapperCache);
    callArgs.push_back(currOperand);
  }
  // Recreate apply with new operands due to substitution-type cache
  switch (applyInst->getKind()) {
  case SILInstructionKind::ApplyInst: {
    auto *castedApply = cast<ApplyInst>(applyInst);
    SILValue newApply =
      applyBuilder.createApply(castedApply->getLoc(), callee,
                               applySite.getSubstitutionMap(),
                               callArgs,
                               castedApply->getApplyOptions());
    castedApply->replaceAllUsesWith(newApply);

    // For guaranteed address results, replace the pre-allocated stack slot
    // with the returned address
    if (modNonFuncTypeResultType(genEnv, origSILFunctionType, *currIRMod)) {
      auto allocIt = allApplyRetToAllocMap.find(applyInst);
      ASSERT(allocIt != allApplyRetToAllocMap.end());

      if (newSILFunctionType->hasGuaranteedAddressResult()) {
        // Delete all dealloc_stack instructions for this allocation
        auto *stackAlloc = cast<AllocStackInst>(allocIt->second);
        for (auto *use : stackAlloc->getUses()) {
          if (auto *dealloc = dyn_cast<DeallocStackInst>(use->getUser())) {
            dealloc->eraseFromParent();
          }
        }
        stackAlloc->replaceAllUsesWith(newApply);
        stackAlloc->eraseFromParent();
      }
    }

    break;
  }
  case SILInstructionKind::TryApplyInst: {
    auto *castedApply = cast<TryApplyInst>(applyInst);
    applyBuilder.createTryApply(
        castedApply->getLoc(), callee, applySite.getSubstitutionMap(), callArgs,
        castedApply->getNormalBB(), castedApply->getErrorBB(),
        castedApply->getApplyOptions());

    break;
  }
  case SILInstructionKind::BeginApplyInst: {
    auto oldApply = cast<BeginApplyInst>(applyInst);
    auto newApply =
      applyBuilder.createBeginApply(oldApply->getLoc(), callee,