1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
|
//===--- MoveOnlyUtils.cpp ------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-move-only-checker"
#include "swift/AST/AccessScope.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/FrozenMultiMap.h"
#include "swift/Basic/SmallBitVector.h"
#include "swift/SIL/ApplySite.h"
#include "swift/SIL/BasicBlockBits.h"
#include "swift/SIL/BasicBlockData.h"
#include "swift/SIL/BasicBlockDatastructures.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/Consumption.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/FieldSensitivePrunedLiveness.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILArgumentConvention.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Analysis/ClosureScope.h"
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/Analysis/NonLocalAccessBlockAnalysis.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h"
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "MoveOnlyDiagnostics.h"
#include "MoveOnlyUtils.h"
using namespace swift;
using namespace swift::siloptimizer;
//===----------------------------------------------------------------------===//
// MARK: Missed Copy Diagnostic
//===----------------------------------------------------------------------===//
/// A small diagnostic helper that causes us to emit a diagnostic error upon any
/// copies we did not eliminate and ask the user for a test case.
void swift::siloptimizer::emitCheckerMissedCopyOfNonCopyableTypeErrors(
SILFunction *fn, DiagnosticEmitter &diagnosticEmitter) {
for (auto &block : *fn) {
for (auto &inst : block) {
if (auto *cvi = dyn_cast<CopyValueInst>(&inst)) {
if (cvi->getOperand()->getType().isMoveOnly()) {
LLVM_DEBUG(llvm::dbgs()
<< "Emitting missed copy error for: " << *cvi);
diagnosticEmitter.emitCheckedMissedCopyError(cvi);
}
continue;
}
if (auto *li = dyn_cast<LoadInst>(&inst)) {
if (li->getOwnershipQualifier() == LoadOwnershipQualifier::Copy &&
li->getType().isMoveOnly()) {
LLVM_DEBUG(llvm::dbgs() << "Emitting missed copy error for: " << *li);
diagnosticEmitter.emitCheckedMissedCopyError(li);
}
continue;
}
if (auto *copyAddr = dyn_cast<CopyAddrInst>(&inst)) {
if (!copyAddr->isTakeOfSrc() &&
copyAddr->getSrc()->getType().isMoveOnly()) {
LLVM_DEBUG(llvm::dbgs()
<< "Emitting missed copy error for: " << *copyAddr);
diagnosticEmitter.emitCheckedMissedCopyError(copyAddr);
}
continue;
}
}
}
}
//===----------------------------------------------------------------------===//
// MARK: Cleanup After Emitting Diagnostic
//===----------------------------------------------------------------------===//
bool swift::siloptimizer::cleanupNonCopyableCopiesAfterEmittingDiagnostic(
SILFunction *fn) {
bool changed = false;
for (auto &block : *fn) {
for (auto ii = block.begin(), ie = block.end(); ii != ie;) {
auto *inst = &*ii;
++ii;
// Convert load [copy] *MoveOnly -> load_borrow + explicit_copy_value.
if (auto *li = dyn_cast<LoadInst>(inst)) {
if (li->getOwnershipQualifier() == LoadOwnershipQualifier::Copy) {
if (!li->getType().isMoveOnly())
continue;
SILBuilderWithScope builder(li);
auto *lbi = builder.createLoadBorrow(li->getLoc(), li->getOperand());
auto *cvi = builder.createExplicitCopyValue(li->getLoc(), lbi);
builder.createEndBorrow(li->getLoc(), lbi);
li->replaceAllUsesWith(cvi);
li->eraseFromParent();
changed = true;
}
}
// Convert copy_addr !take MoveOnly ... -> explicit_copy_addr ...same...
// so we don't error.
if (auto *copyAddr = dyn_cast<CopyAddrInst>(inst)) {
if (!copyAddr->isTakeOfSrc()) {
if (!copyAddr->getSrc()->getType().isMoveOnly())
continue;
SILBuilderWithScope builder(copyAddr);
builder.createExplicitCopyAddr(
copyAddr->getLoc(), copyAddr->getSrc(), copyAddr->getDest(),
IsTake_t(copyAddr->isTakeOfSrc()),
IsInitialization_t(copyAddr->isInitializationOfDest()));
copyAddr->eraseFromParent();
changed = true;
}
}
// Convert any copy_value of MoveOnly type -> explicit_copy_value.
if (auto *cvi = dyn_cast<CopyValueInst>(inst)) {
if (!cvi->getOperand()->getType().isMoveOnly())
continue;
SILBuilderWithScope b(cvi);
auto *expCopy =
b.createExplicitCopyValue(cvi->getLoc(), cvi->getOperand());
cvi->replaceAllUsesWith(expCopy);
cvi->eraseFromParent();
changed = true;
continue;
}
if (auto *mmci = dyn_cast<MarkUnresolvedNonCopyableValueInst>(inst)) {
mmci->replaceAllUsesWith(mmci->getOperand());
mmci->eraseFromParent();
changed = true;
continue;
}
}
}
return changed;
}
//===----------------------------------------------------------------------===//
// MARK: Memory Utilities
//===----------------------------------------------------------------------===//
bool noncopyable::memInstMustInitialize(Operand *memOper) {
SILValue address = memOper->get();
SILInstruction *memInst = memOper->getUser();
switch (memInst->getKind()) {
default:
return false;
case SILInstructionKind::CopyAddrInst: {
auto *CAI = cast<CopyAddrInst>(memInst);
return CAI->getDest() == address && CAI->isInitializationOfDest();
}
case SILInstructionKind::ExplicitCopyAddrInst: {
auto *CAI = cast<ExplicitCopyAddrInst>(memInst);
return CAI->getDest() == address && CAI->isInitializationOfDest();
}
case SILInstructionKind::MarkUnresolvedMoveAddrInst: {
return cast<MarkUnresolvedMoveAddrInst>(memInst)->getDest() == address;
}
case SILInstructionKind::InitExistentialAddrInst:
case SILInstructionKind::InitEnumDataAddrInst:
case SILInstructionKind::InjectEnumAddrInst:
return true;
case SILInstructionKind::BeginApplyInst:
case SILInstructionKind::TryApplyInst:
case SILInstructionKind::ApplyInst: {
FullApplySite applySite(memInst);
return applySite.isIndirectResultOperand(*memOper);
}
case SILInstructionKind::StoreInst: {
auto qual = cast<StoreInst>(memInst)->getOwnershipQualifier();
return qual == StoreOwnershipQualifier::Init ||
qual == StoreOwnershipQualifier::Trivial;
}
case SILInstructionKind::BuiltinInst: {
auto bi = cast<BuiltinInst>(memInst);
if (bi->getBuiltinKind() == BuiltinValueKind::ZeroInitializer) {
// `zeroInitializer` with an address operand zeroes out the address operand
return true;
}
return false;
}
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst: \
return cast<Store##Name##Inst>(memInst)->isInitializationOfDest();
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::StoreBorrowInst:
return true;
}
}
bool noncopyable::memInstMustReinitialize(Operand *memOper) {
SILValue address = memOper->get();
SILInstruction *memInst = memOper->getUser();
switch (memInst->getKind()) {
default:
return false;
case SILInstructionKind::CopyAddrInst: {
auto *CAI = cast<CopyAddrInst>(memInst);
return CAI->getDest() == address && !CAI->isInitializationOfDest();
}
case SILInstructionKind::ExplicitCopyAddrInst: {
auto *CAI = cast<ExplicitCopyAddrInst>(memInst);
return CAI->getDest() == address && !CAI->isInitializationOfDest();
}
case SILInstructionKind::YieldInst: {
auto *yield = cast<YieldInst>(memInst);
return yield->getYieldInfoForOperand(*memOper).isIndirectInOut();
}
case SILInstructionKind::BeginApplyInst:
case SILInstructionKind::TryApplyInst:
case SILInstructionKind::ApplyInst: {
FullApplySite applySite(memInst);
return applySite.getCaptureConvention(*memOper).isInoutConvention();
}
case SILInstructionKind::StoreInst:
return cast<StoreInst>(memInst)->getOwnershipQualifier() ==
StoreOwnershipQualifier::Assign;
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst: \
return !cast<Store##Name##Inst>(memInst)->isInitializationOfDest();
#include "swift/AST/ReferenceStorage.def"
}
}
bool noncopyable::memInstMustConsume(Operand *memOper) {
SILValue address = memOper->get();
SILInstruction *memInst = memOper->getUser();
switch (memInst->getKind()) {
default:
return false;
case SILInstructionKind::ApplyInst:
case SILInstructionKind::BeginApplyInst:
case SILInstructionKind::TryApplyInst: {
FullApplySite applySite(memInst);
return applySite.getCaptureConvention(*memOper).isOwnedConvention();
}
case SILInstructionKind::BeginAccessInst:
return cast<BeginAccessInst>(memInst)->getAccessKind() ==
SILAccessKind::Deinit;
case SILInstructionKind::CopyAddrInst: {
auto *CAI = cast<CopyAddrInst>(memInst);
return (CAI->getSrc() == address && CAI->isTakeOfSrc()) ||
(CAI->getDest() == address && !CAI->isInitializationOfDest());
}
case SILInstructionKind::DestroyAddrInst:
return true;
case SILInstructionKind::DropDeinitInst:
assert(memOper->get()->getType().isValueTypeWithDeinit());
return true;
case SILInstructionKind::ExplicitCopyAddrInst: {
auto *CAI = cast<ExplicitCopyAddrInst>(memInst);
return (CAI->getSrc() == address && CAI->isTakeOfSrc()) ||
(CAI->getDest() == address && !CAI->isInitializationOfDest());
}
case SILInstructionKind::LoadInst:
return cast<LoadInst>(memInst)->getOwnershipQualifier() ==
LoadOwnershipQualifier::Take;
case SILInstructionKind::PartialApplyInst: {
// If we are on the stack or have an inout convention, we do not
// consume. Otherwise, we do.
auto *pai = cast<PartialApplyInst>(memInst);
if (pai->isOnStack())
return false;
ApplySite applySite(pai);
auto convention = applySite.getArgumentConvention(*memOper);
return !convention.isInoutConvention();
}
case SILInstructionKind::UncheckedTakeEnumDataAddrInst: {
auto *utedai = cast<UncheckedTakeEnumDataAddrInst>(memInst);
return utedai->isDestructive();
}
}
}
//===----------------------------------------------------------------------===//
// Simple Temporary AllocStack Elimination
//===----------------------------------------------------------------------===//
static bool isLetAllocation(MarkUnresolvedNonCopyableValueInst *mmci) {
if (auto *pbi = dyn_cast<ProjectBoxInst>(mmci)) {
auto *box = cast<AllocBoxInst>(stripBorrow(pbi->getOperand()));
return !box->getBoxType()->getLayout()->isMutable();
}
if (auto *asi = dyn_cast<AllocStackInst>(mmci->getOperand()))
return asi->isLet();
return false;
}
static bool walkUseToDefsStructTupleProjections(
SILValue startPoint, SILValue endPoint,
llvm::function_ref<void(SingleValueInstruction *)> visitor) {
while (startPoint != endPoint) {
if (auto *sei = dyn_cast<StructElementAddrInst>(startPoint)) {
visitor(sei);
startPoint = sei->getOperand();
continue;
}
if (auto *tei = dyn_cast<TupleElementAddrInst>(startPoint)) {
visitor(tei);
startPoint = tei->getOperand();
continue;
}
return false;
}
return true;
}
namespace {
struct SimpleTemporaryAllocStackElimState {
SmallVector<SingleValueInstruction *, 8> projectionList;
StackList<SILInstruction *> instsToDelete;
/// We use this set to walk from our initial copy to our final use and ensure
/// that there aren't any instructions we did not visit in between them. This
/// is to ensure that there aren't any instructions we didn't scan and
/// analyze.
InstructionSet visitedInsts;
SILValue rootAddress;
Operand *finalUse = nullptr;
SimpleTemporaryAllocStackElimState(SILValue rootAddress)
: instsToDelete(rootAddress->getFunction()),
visitedInsts(rootAddress->getFunction()), rootAddress(rootAddress) {}
bool setFinalUser(Operand *newFinalUse) {
if (finalUse)
return false;
finalUse = newFinalUse;
return true;
}
/// Walk from use->def pattern matching struct_element_addr/tuple_element_addr
/// from \p useAddress until it is \p allocationAddress. If we see a different
/// instruction, we return false to signal failure. Returns true if all
/// instructions along use->def walk are said instructions.
bool appendProjections(SILValue useAddress, SILValue allocationAddress) {
return walkUseToDefsStructTupleProjections(
useAddress, allocationAddress,
[&](SingleValueInstruction *sei) { projectionList.push_back(sei); });
}
};
struct SimpleTemporaryAllocStackElimVisitor
: public TransitiveAddressWalker<SimpleTemporaryAllocStackElimVisitor> {
SimpleTemporaryAllocStackElimState &state;
CopyAddrInst *caiToVisit;
CopyAddrInst *&nextCAI;
SimpleTemporaryAllocStackElimVisitor(
SimpleTemporaryAllocStackElimState &state, CopyAddrInst *cai,
CopyAddrInst *&nextCAI)
: state(state), caiToVisit(cai), nextCAI(nextCAI) {
assert(nextCAI == nullptr);
}
AllocStackInst *getAllocation() const {
return cast<AllocStackInst>(caiToVisit->getDest());
}
bool setNextCAI(CopyAddrInst *newCAI) {
// If we already have a CAI, bail. We should only ever have one.
if (nextCAI)
return false;
nextCAI = newCAI;
return true;
}
bool visitUse(Operand *op) {
LLVM_DEBUG(llvm::dbgs() << "SimpleTemporaryAllocStackElimVisitor visiting: "
<< *op->getUser());
state.visitedInsts.insert(op->getUser());
// We do not care about type dependent uses.
if (op->isTypeDependent())
return true;
auto *user = op->getUser();
// We should never see a debug_value use since this should be a temporary.
if (user->isDebugInstruction()) {
LLVM_DEBUG(llvm::dbgs() << "Found a debug_value?! This should be a "
"temporary which implies no debug info!\n");
return false;
}
// If we are visiting our own copy_addr, then just return true. We do not
// need to do any further work. If we successfully process this, then we
// shouldn't need anything.
if (user == caiToVisit) {
return true;
}
// Skip destroy_addr and dealloc_stack. We will remove them if we succeed in
// our mission. We require they are directly on the temporary allocation.
if (isa<DestroyAddrInst>(user) || isa<DeallocStackInst>(user)) {
if (op->get() != getAllocation())
return false;
return true;
}
// If our operand is an initialization, we bail. We never have multiple
// initializations for these sorts of temporaries. Our initial copy_addr is
// our single initialization.
if (noncopyable::memInstMustInitialize(op)) {
LLVM_DEBUG(llvm::dbgs()
<< "Found extra initializer! Bailing!: " << *user);
return false;
}
// We do not allow for reinitialization since we are working with
// specifically lets.
if (noncopyable::memInstMustReinitialize(op)) {
LLVM_DEBUG(llvm::dbgs() << "Found reinit: " << *user);
return false;
}
// If we see a consuming instruction, then this must be a final allocation
// in our chain of temporary allocations.
if (noncopyable::memInstMustConsume(op)) {
// If we already found a CAI, bail.
if (nextCAI)
return false;
// We do not append projections here since we handle the projections
// associated with the final user once we visit everything. This ensures
// that if we have multiple projections on the final allocation, we can
// tell these projections apart from projections from earlier allocations.
return state.setFinalUser(state.finalUse);
}
// If we see a load operation, stash it. This load operation will become a
// copy.
if (auto *li = dyn_cast<LoadInst>(user)) {
// If we already found a CAI, bail.
if (nextCAI)
return false;
// We do not handle takes for now.
if (li->getOwnershipQualifier() == LoadOwnershipQualifier::Take)
return false;
// We do not append projections here since we handle the projections
// associated with the final user once we visit everything. This ensures
// that if we have multiple projections on the final allocation, we can
// tell these projections apart from projections from earlier allocations.
return state.setFinalUser(op);
}
if (auto *cai = dyn_cast<CopyAddrInst>(user)) {
// If we already found a copy, bail. We always only visit one of these
// regardless if they are a final user or a temporary copy.
if (!setNextCAI(cai))
return false;
// If our address is not the src or we are taking the src, bail. We do not
// handle this.
if (cai->getSrc() != op->get() || cai->isTakeOfSrc() ||
cai->getSrc() == cai->getDest())
return false;
// If we are not initializing our dest, then we know that we do not have
// another iterative temporary. Treat this as a final user.
if (!cai->isInitializationOfDest()) {
return state.setFinalUser(op);
}
// Ok, we are initializing some other memory. Check if our dest is another
// temporary alloc_stack. If not, treat this as the final user.
auto *newTemp = dyn_cast<AllocStackInst>(cai->getDest());
if (!newTemp || newTemp->isLexical()) {
return state.setFinalUser(op);
}
// Ok, we have another temporary allocation copy, add the copy_addr to the
// worklist and add this allocation to the dead allocation list.
if (!state.appendProjections(cai->getSrc(), caiToVisit->getDest()))
return false;
state.instsToDelete.push_back(getAllocation());
return true;
}
// Otherwise, we have a potential liveness use. If the use writes to memory,
// bail, we do not support it.
if (user->mayWriteToMemory()) {
return false;
}
// Liveness use.
state.setFinalUser(op);
// We found an instruction that we did not understand.
return false;
}
};
} // namespace
/// Returns false if we saw something we did not understand and the copy_addr
/// should be inserted into UseState::copyInst to be conservative.
bool siloptimizer::eliminateTemporaryAllocationsFromLet(
MarkUnresolvedNonCopyableValueInst *markedInst) {
if (!isLetAllocation(markedInst))
return false;
StackList<CopyAddrInst *> copiesToVisit(markedInst->getFunction());
struct FindCopyAddrWalker
: public TransitiveAddressWalker<FindCopyAddrWalker> {
StackList<CopyAddrInst *> &copiesToVisit;
FindCopyAddrWalker(StackList<CopyAddrInst *> &copiesToVisit)
: TransitiveAddressWalker(), copiesToVisit(copiesToVisit) {}
bool visitUse(Operand *op) {
auto *cai = dyn_cast<CopyAddrInst>(op->getUser());
// We want copy_addr that are not a take of src and are an init of their
// dest.
if (!cai || cai->isTakeOfSrc() || !cai->isInitializationOfDest())
return true;
copiesToVisit.push_back(cai);
return true;
};
};
FindCopyAddrWalker walker(copiesToVisit);
std::move(walker).walk(markedInst);
bool madeChange = false;
while (!copiesToVisit.empty()) {
auto *initialCAI = copiesToVisit.pop_back_val();
SimpleTemporaryAllocStackElimState state(markedInst);
auto *asi = dyn_cast<AllocStackInst>(initialCAI->getDest());
if (!asi || asi->isLexical())
continue;
if ( // If we have that our dest/src are the same, just bail. We shouldn't
// see this, but lets just be careful.
initialCAI->getSrc() == initialCAI->getDest())
continue;
// For now, just handle if we have an init/no take. We should add support
// for a take in the future.
if (!initialCAI->isInitializationOfDest() || initialCAI->isTakeOfSrc())
continue;
AllocStackInst *finalAllocation = nullptr;
CopyAddrInst *nextCAI = initialCAI;
unsigned numLastProjections = 0;
unsigned numProjectionsPrevIteration = 0;
do {
auto *cai = nextCAI;
nextCAI = nullptr;
SimpleTemporaryAllocStackElimVisitor visitor(state, cai, nextCAI);
if (AddressUseKind::Unknown == std::move(visitor).walk(cai->getDest()))
return false;
// If we did not find a nextCAI, do not have a final use, and we already
// saw at least one allocation, make cai our last user. We treat that last
// allocation as our true last allocation, so we break.
//
// DISCUSSION: This occurs if we have a temporary copy that ends in a
// copyable address only type.
if (!nextCAI && !state.finalUse && finalAllocation) {
state.finalUse = &cai->getAllOperands()[CopyLikeInstruction::Src];
state.projectionList.pop_back_n(state.projectionList.size() -
numProjectionsPrevIteration);
break;
}
finalAllocation = cast<AllocStackInst>(cai->getDest());
numProjectionsPrevIteration = numLastProjections;
numLastProjections = state.projectionList.size();
} while (nextCAI);
assert(finalAllocation);
// If we did not actually find a final use, just bail. We can't rewrite.
auto *finalUse = state.finalUse;
if (!finalUse)
continue;
// Then check that our final use and initialCAI are in the same block and
// that all instructions in between them with side-effects are instructions
// that we visited. This is a soundness check.
if (finalUse->getParentBlock() != initialCAI->getParent() ||
llvm::any_of(llvm::make_range(initialCAI->getIterator(),
finalUse->getUser()->getIterator()),
[&](SILInstruction &inst) {
return !state.visitedInsts.contains(&inst) &&
inst.mayHaveSideEffects();
}))
continue;
// Now that we have succeeded in our analysis, we perform our
// transformation. First if we do not have a finalUse, just bail.
// Otherwise, begin rewriting. First see if we our final use is directly on
// the final allocation or if it has intervening projections. If it has
// intervening projections, we need to rewrite the final projection.
SingleValueInstruction *finalProjection = nullptr;
if (!walkUseToDefsStructTupleProjections(
finalUse->get(), finalAllocation,
[&](SingleValueInstruction *proj) { finalProjection = proj; }))
continue;
// Now that we have looked through all potential projections on our final
// use, now walk and fix up the projections.
auto &projList = state.projectionList;
madeChange = true;
// First set our initial projection to initialCAI.
if (!projList.empty()) {
(*projList.begin())->setOperand(0, initialCAI->getSrc());
} else {
if (finalProjection) {
finalProjection->setOperand(0, initialCAI->getSrc());
} else {
finalUse->set(initialCAI->getSrc());
}
}
for (auto ii = projList.begin(), ie = projList.end(); ii != ie;) {
auto *proj = *ii;
++ii;
// If next ii is ie, then make finalProjection/final use, use this
// value.
if (ii == ie) {
if (finalProjection) {
finalProjection->setOperand(0, proj);
} else {
finalUse->set(proj);
}
break;
}
// Otherwise, see if the next projection has this projection as its
// operand. If so, just continue, we do not need to update
// anything.
if ((*ii)->getOperand(0) == SILValue(proj))
continue;
// Otherwise, we jumped to another allocation, set ii's operand to proj.
(*ii)->setOperand(0, proj);
}
// Now go through all of the instructions to delete and delete them. They
// should consist only of alloc_stack, destroy_addr, and dealloc_stack.
InstructionDeleter deleter;
while (!state.instsToDelete.empty())
deleter.forceDeleteWithUsers(state.instsToDelete.pop_back_val());
deleter.forceDeleteWithUsers(finalAllocation);
}
return madeChange;
}
|