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 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
|
//===--- PrunedLiveness.cpp - Compute liveness from selected uses ---------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/PrunedLiveness.h"
#include "swift/AST/TypeExpansionContext.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/SIL/BasicBlockDatastructures.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/ScopedAddressUtils.h"
#include "swift/SIL/Test.h"
using namespace swift;
void PrunedLiveBlocks::computeUseBlockLiveness(SILBasicBlock *userBB) {
// If, we are visiting this block, then it is not already LiveOut. Mark it
// LiveWithin to indicate a liveness boundary within the block.
markBlockLive(userBB, LiveWithin);
BasicBlockWorklist worklist(userBB->getFunction());
worklist.push(userBB);
while (auto *block = worklist.pop()) {
// The popped `bb` is live; now mark all its predecessors LiveOut.
//
// Traversal terminates at any previously visited block, including the
// blocks initialized as definition blocks.
for (auto *predBlock : block->getPredecessorBlocks()) {
switch (getBlockLiveness(predBlock)) {
case Dead:
worklist.pushIfNotVisited(predBlock);
LLVM_FALLTHROUGH;
case LiveWithin:
markBlockLive(predBlock, LiveOut);
break;
case LiveOut:
break;
}
}
}
}
//===----------------------------------------------------------------------===//
// PrunedLiveBlocks and PrunedLiveness
//===----------------------------------------------------------------------===//
llvm::StringRef PrunedLiveBlocks::getStringRef(IsLive isLive) const {
switch (isLive) {
case Dead:
return "Dead";
case LiveWithin:
return "LiveWithin";
case LiveOut:
return "LiveOut";
}
}
void PrunedLiveBlocks::print(llvm::raw_ostream &OS) const {
if (!discoveredBlocks) {
OS << "No deterministic live block list\n";
return;
}
SmallVector<IsLive, 8> isLive;
for (auto *block : *discoveredBlocks) {
block->printAsOperand(OS);
OS << ": " << getStringRef(this->getBlockLiveness(block)) << "\n";
}
}
void PrunedLiveBlocks::dump() const {
print(llvm::dbgs());
}
void PrunedLiveness::print(llvm::raw_ostream &OS) const {
liveBlocks.print(OS);
for (auto &userAndIsLifetimeEnding : users) {
switch (userAndIsLifetimeEnding.second) {
case LifetimeEnding::Value::NonUse:
OS << "non-user: ";
break;
case LifetimeEnding::Value::Ending:
OS << "lifetime-ending user: ";
break;
case LifetimeEnding::Value::NonEnding:
OS << "regular user: ";
break;
}
userAndIsLifetimeEnding.first->print(OS);
}
}
void PrunedLiveness::dump() const {
print(llvm::dbgs());
}
//===----------------------------------------------------------------------===//
// PrunedLivenessBoundary
//===----------------------------------------------------------------------===//
void PrunedLivenessBoundary::print(llvm::raw_ostream &OS) const {
for (auto *user : lastUsers) {
OS << "last user: " << *user;
}
for (auto *block : boundaryEdges) {
OS << "boundary edge: ";
block->printAsOperand(OS);
OS << "\n";
}
if (!deadDefs.empty()) {
for (auto *deadDef : deadDefs) {
OS << "dead def: " << *deadDef;
}
}
}
void PrunedLivenessBoundary::dump() const {
print(llvm::dbgs());
}
void PrunedLivenessBoundary::visitInsertionPoints(
llvm::function_ref<void(SILBasicBlock::iterator insertPt)> visitor,
DeadEndBlocks *deBlocks) {
// Control flow merge blocks used as insertion points.
SmallPtrSet<SILBasicBlock *, 4> mergeBlocks;
for (SILInstruction *user : lastUsers) {
if (!isa<TermInst>(user)) {
visitor(std::next(user->getIterator()));
continue;
}
auto *predBB = user->getParent();
for (SILBasicBlock *succ : predBB->getSuccessors()) {
if (!succ->getSinglePredecessorBlock()) {
assert(predBB->getSingleSuccessorBlock() == succ);
if (!mergeBlocks.insert(succ).second) {
continue;
}
} else {
assert(succ->getSinglePredecessorBlock() == predBB);
}
if (deBlocks && deBlocks->isDeadEnd(succ))
continue;
visitor(succ->begin());
}
}
for (SILBasicBlock *edge : boundaryEdges) {
if (deBlocks && deBlocks->isDeadEnd(edge))
continue;
visitor(edge->begin());
}
for (SILNode *deadDef : deadDefs) {
if (auto *arg = dyn_cast<SILArgument>(deadDef))
visitor(arg->getParent()->begin());
else
visitor(std::next(cast<SILInstruction>(deadDef)->getIterator()));
}
}
namespace swift::test {
// Arguments:
// - variadic list of - instruction: a last user
// Dumps:
// - the insertion points
static FunctionTest
PrunedLivenessBoundaryWithListOfLastUsersInsertionPointsTest(
"pruned_liveness_boundary_with_list_of_last_users_insertion_points",
[](auto &function, auto &arguments, auto &test) {
PrunedLivenessBoundary boundary;
while (arguments.hasUntaken()) {
boundary.lastUsers.push_back(arguments.takeInstruction());
}
boundary.visitInsertionPoints([](SILBasicBlock::iterator point) {
point->print(llvm::outs());
});
});
} // end namespace swift::test
//===----------------------------------------------------------------------===//
// PrunedLiveRange
//===----------------------------------------------------------------------===//
static PrunedLiveness::LifetimeEnding
branchMeet(PrunedLiveness::LifetimeEnding const lhs,
PrunedLiveness::LifetimeEnding const rhs) {
enum BranchLifetimeEnding {
Ending,
NonEnding,
NonUse,
};
auto toBranch =
[](PrunedLiveness::LifetimeEnding const ending) -> BranchLifetimeEnding {
switch (ending) {
case PrunedLiveness::LifetimeEnding::Value::NonEnding:
return NonEnding;
case PrunedLiveness::LifetimeEnding::Value::Ending:
return Ending;
case PrunedLiveness::LifetimeEnding::Value::NonUse:
return NonUse;
}
};
auto toRegular =
[](BranchLifetimeEnding const ending) -> PrunedLiveness::LifetimeEnding {
switch (ending) {
case NonEnding:
return PrunedLiveness::LifetimeEnding::Value::NonEnding;
case Ending:
return PrunedLiveness::LifetimeEnding::Value::Ending;
case NonUse:
return PrunedLiveness::LifetimeEnding::Value::NonUse;
}
};
return toRegular(std::min(toBranch(lhs), toBranch(rhs)));
}
static void branchMeetInPlace(PrunedLiveness::LifetimeEnding &that,
PrunedLiveness::LifetimeEnding const other) {
that = branchMeet(that, other);
}
template <typename LivenessWithDefs>
void PrunedLiveRange<LivenessWithDefs>::updateForUse(
SILInstruction *user,
PrunedLiveRange<LivenessWithDefs>::LifetimeEnding lifetimeEnding) {
liveBlocks.updateForUse(user, asImpl().isUserBeforeDef(user));
// Note that a user may use the current value from multiple operands. If any
// of the uses are non-lifetime-ending, then we must consider the user
// itself non-lifetime-ending; it cannot be a final destroy point because
// the value of the non-lifetime-ending operand must be kept alive until the
// end of the user. Consider a call that takes the same value using
// different conventions:
//
// apply %f(%val, %val) : $(@guaranteed, @owned) -> ()
//
// This call is not considered the end of %val's lifetime. The @owned
// argument must be copied.
auto iterAndSuccess = users.insert({user, lifetimeEnding});
if (!iterAndSuccess.second) {
if (isa<BranchInst>(user)) {
branchMeetInPlace(iterAndSuccess.first->second, lifetimeEnding);
} else {
iterAndSuccess.first->second.meetInPlace(lifetimeEnding);
}
}
}
template <typename LivenessWithDefs>
void PrunedLiveRange<LivenessWithDefs>::updateForUse(SILInstruction *user,
bool lifetimeEnding) {
updateForUse(user, LifetimeEnding::forUse(lifetimeEnding));
}
template <typename LivenessWithDefs>
void PrunedLiveRange<LivenessWithDefs>::extendToNonUse(SILInstruction *inst) {
updateForUse(inst, LifetimeEnding::Value::NonUse);
}
template <typename LivenessWithDefs>
InnerBorrowKind
PrunedLiveRange<LivenessWithDefs>::updateForBorrowingOperand(Operand *operand) {
assert(operand->getOperandOwnership() == OperandOwnership::Borrow);
// A nested borrow scope is considered a use-point at each scope ending
// instruction.
//
// Note: Ownership liveness should follow reborrows that are dominated by the
// ownership definition.
auto innerBorrowKind = InnerBorrowKind::Contained;
BorrowingOperand(operand).visitScopeEndingUses(
[&](Operand *end) {
if (end->getOperandOwnership() == OperandOwnership::Reborrow) {
innerBorrowKind = InnerBorrowKind::Reborrowed;
}
updateForUse(end->getUser(), /*lifetimeEnding*/ false);
return true;
}, [&](Operand *unknownUse) {
updateForUse(unknownUse->getUser(), /*lifetimeEnding*/ false);
innerBorrowKind = InnerBorrowKind::Escaped;
return true;
});
return innerBorrowKind;
}
template <typename LivenessWithDefs>
AddressUseKind PrunedLiveRange<LivenessWithDefs>::checkAndUpdateInteriorPointer(
Operand *operand) {
assert(operand->getOperandOwnership() == OperandOwnership::InteriorPointer
|| operand->getOperandOwnership() == OperandOwnership::AnyInteriorPointer);
if (auto scopedAddress = ScopedAddressValue::forUse(operand)) {
scopedAddress.visitScopeEndingUses([this](Operand *end) {
updateForUse(end->getUser(), /*lifetimeEnding*/ false);
return true;
});
return AddressUseKind::NonEscaping;
}
// FIXME: findTransitiveUses should be a visitor so we're not recursively
// allocating use vectors and potentially merging the use points.
SmallVector<Operand *, 8> uses;
auto useKind = InteriorPointerOperand(operand).findTransitiveUses(&uses);
for (auto *use : uses) {
updateForUse(use->getUser(), /*lifetimeEnding*/ false);
}
if (uses.empty()) {
// Handle a dead address
updateForUse(operand->getUser(), /*lifetimeEnding*/ false);
}
return useKind;
}
template <typename LivenessWithDefs>
void PrunedLiveRange<LivenessWithDefs>::extendAcrossLiveness(
PrunedLiveness &otherLiveness) {
// update this liveness for all the interesting users in otherLiveness.
for (std::pair<SILInstruction *, LifetimeEnding> userAndEnd :
otherLiveness.getAllUsers()) {
updateForUse(userAndEnd.first, userAndEnd.second);
}
}
template <typename LivenessWithDefs>
LiveRangeSummary PrunedLiveRange<LivenessWithDefs>::updateForDef(SILValue def) {
ValueSet visited(def->getFunction());
return recursivelyUpdateForDef(def, visited, def);
}
template <typename LivenessWithDefs>
LiveRangeSummary PrunedLiveRange<LivenessWithDefs>::recursivelyUpdateForDef(
SILValue initialDef, ValueSet &visited, SILValue value) {
LiveRangeSummary summary;
if (!visited.insert(value))
return summary;
// Note: Uses with OperandOwnership::NonUse cannot be considered normal uses
// for liveness. Otherwise, liveness would need to separately track non-uses
// everywhere. Non-uses cannot be treated like normal non-lifetime-ending uses
// because they can occur on both applies, which need to extend liveness to
// the return point, and on forwarding instructions, like
// init_existential_ref, which need to consume their use even when
// type-dependent operands exist.
for (Operand *use : value->getUses()) {
switch (use->getOperandOwnership()) {
case OperandOwnership::NonUse:
break;
case OperandOwnership::Borrow:
summary.meet(updateForBorrowingOperand(use));
break;
case OperandOwnership::PointerEscape:
summary.meet(AddressUseKind::PointerEscape);
break;
case OperandOwnership::InteriorPointer:
case OperandOwnership::AnyInteriorPointer:
summary.meet(checkAndUpdateInteriorPointer(use));
break;
case OperandOwnership::GuaranteedForwarding: {
updateForUse(use->getUser(), /*lifetimeEnding*/false);
if (auto phiOper = PhiOperand(use)) {
SILValue phi = phiOper.getValue();
// If 'def' is any of the enclosing defs, then it must dominate the phi
// and all phi uses should be handled recursively.
if (!visitEnclosingDefs(phi, [initialDef](SILValue enclosingDef) {
return enclosingDef != initialDef;
})) {
// At least one enclosing def was 'def'.
summary.meet(recursivelyUpdateForDef(initialDef, visited, phi));
}
// Otherwise all enclosing defs are protected by separate reborrow
// scopes, which are not included in "simple" liveness.
break;
}
ForwardingOperand(use).visitForwardedValues([&](SILValue result) {
// Do not include transitive uses with 'none' ownership
if (result->getOwnershipKind() != OwnershipKind::None) {
summary.meet(recursivelyUpdateForDef(initialDef, visited, result));
}
return true;
});
break;
}
case OperandOwnership::TrivialUse: {
if (auto scopedAddress = ScopedAddressValue::forUse(use)) {
scopedAddress.visitScopeEndingUses([this](Operand *end) {
updateForUse(end->getUser(), /*lifetimeEnding*/false);
return true;
});
}
updateForUse(use->getUser(), /*lifetimeEnding*/false);
break;
}
default:
// Note: An outer reborrow ends the outer lifetime here.
updateForUse(use->getUser(), use->isLifetimeEnding());
break;
}
}
return summary;
}
namespace swift::test {
// Arguments:
// - SILValue: value to a analyze
// Dumps:
// - the liveness result and boundary
static FunctionTest SSALivenessTest("ssa_liveness", [](auto &function,
auto &arguments,
auto &test) {
auto value = arguments.takeValue();
assert(!arguments.hasUntaken());
llvm::outs() << "SSA lifetime analysis: " << value;
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
SSAPrunedLiveness liveness(value->getFunction(), &discoveredBlocks);
liveness.initializeDef(value);
LiveRangeSummary summary = liveness.computeSimple();
if (summary.innerBorrowKind == InnerBorrowKind::Reborrowed)
llvm::outs() << "Incomplete liveness: Reborrowed inner scope\n";
if (summary.addressUseKind == AddressUseKind::PointerEscape)
llvm::outs() << "Incomplete liveness: Escaping address\n";
else if (summary.addressUseKind == AddressUseKind::Unknown)
llvm::outs() << "Incomplete liveness: Unknown address use\n";
liveness.print(llvm::outs());
PrunedLivenessBoundary boundary;
liveness.computeBoundary(boundary);
boundary.print(llvm::outs());
});
// Arguments:
// - SILValue: def whose pruned liveness will be calculated
// - the string "uses:"
// - variadic list of live-range user instructions
// Dumps:
// -
static FunctionTest SSAUseLivenessTest("ssa_use_liveness", [](auto &function,
auto &arguments,
auto &test) {
auto value = arguments.takeValue();
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
SSAPrunedLiveness liveness(&function, &discoveredBlocks);
liveness.initializeDef(value);
auto argument = arguments.takeArgument();
if (cast<StringArgument>(argument).getValue() != "uses:") {
llvm::report_fatal_error("test specification expects the 'uses:' label\n");
}
while (arguments.hasUntaken()) {
auto *inst = arguments.takeInstruction();
auto kindString = arguments.takeString();
enum Kind {
NonUse,
Ending,
NonEnding,
};
auto kind = llvm::StringSwitch<std::optional<Kind>>(kindString)
.Case("non-use", Kind::NonUse)
.Case("ending", Kind::Ending)
.Case("non-ending", Kind::NonEnding)
.Default(std::nullopt);
if (!kind.has_value()) {
llvm::errs() << "Unknown kind: " << kindString << "\n";
llvm::report_fatal_error("Bad user kind. Value must be one of "
"'non-use', 'ending', 'non-ending'");
}
switch (kind.value()) {
case Kind::NonUse:
liveness.extendToNonUse(inst);
break;
case Kind::Ending:
liveness.updateForUse(inst, /*lifetimeEnding*/ true);
break;
case Kind::NonEnding:
liveness.updateForUse(inst, /*lifetimeEnding*/ false);
break;
}
}
liveness.print(llvm::outs());
PrunedLivenessBoundary boundary;
liveness.computeBoundary(boundary);
boundary.print(llvm::outs());
});
} // end namespace swift::test
template <typename LivenessWithDefs>
bool PrunedLiveRange<LivenessWithDefs>::isWithinLivenessBoundary(
SILInstruction *inst) const {
assert(asImpl().isInitialized());
auto *block = inst->getParent();
auto blockLiveness = getBlockLiveness(block);
if (blockLiveness == PrunedLiveBlocks::Dead)
return false;
bool isLive = blockLiveness == PrunedLiveBlocks::LiveOut;
if (isLive && !asImpl().isDefBlock(block))
return true;
return isInstructionLive(inst, isLive);
}
template <typename LivenessWithDefs>
bool PrunedLiveRange<LivenessWithDefs>::isInstructionLive(SILInstruction *inst,
bool isLive) const {
auto *block = inst->getParent();
// Check if instruction is between a last use and a definition
for (SILInstruction &it : llvm::reverse(*block)) {
// the def itself is not within the boundary, so cancel liveness before
// matching 'inst'.
if (asImpl().isDef(&it)) {
isLive = false;
}
if (&it == inst) {
return isLive;
}
if (!isLive && isInterestingUser(&it)) {
isLive = true;
}
}
llvm_unreachable("instruction must be in its parent block");
}
template <typename LivenessWithDefs>
bool PrunedLiveRange<LivenessWithDefs>::isAvailableOut(
SILBasicBlock *block, DeadEndBlocks &deadEndBlocks) const {
assert(getBlockLiveness(block) == PrunedLiveBlocks::LiveWithin);
assert(deadEndBlocks.isDeadEnd(block));
for (SILInstruction &inst : llvm::reverse(*block)) {
if (asImpl().isDef(&inst)) {
return true;
}
switch (isInterestingUser(&inst)) {
case PrunedLiveness::NonUser:
continue;
case PrunedLiveness::NonLifetimeEndingUse:
return true;
case PrunedLiveness::LifetimeEndingUse:
return false;
}
}
assert(asImpl().isDefBlock(block));
assert(llvm::any_of(block->getArguments(), [this](SILArgument *arg) {
return asImpl().isDef(arg);
}));
return true;
}
template <typename LivenessWithDefs>
bool PrunedLiveRange<LivenessWithDefs>::isInstructionAvailable(
SILInstruction *user, DeadEndBlocks &deadEndBlocks) const {
auto *parent = user->getParent();
assert(getBlockLiveness(parent) == PrunedLiveBlocks::LiveWithin);
assert(deadEndBlocks.isDeadEnd(parent));
return isInstructionLive(user, isAvailableOut(parent, deadEndBlocks));
}
template <typename LivenessWithDefs>
bool PrunedLiveRange<LivenessWithDefs>::isWithinBoundary(
SILInstruction *inst, DeadEndBlocks *deadEndBlocks) const {
if (deadEndBlocks) {
return asImpl().isWithinExtendedBoundary(inst, *deadEndBlocks);
} else {
return asImpl().isWithinLivenessBoundary(inst);
}
}
template <typename LivenessWithDefs>
bool PrunedLiveRange<LivenessWithDefs>::isWithinExtendedBoundary(
SILInstruction *inst, DeadEndBlocks &deadEndBlocks) const {
// A value has a pruned live region, a live region and an available region.
// (Note: PrunedLiveness does not distinguish between the pruned live region
// and the live region; the pruned live region coincides with the live region
// whenever consuming uses are considered.) This method refers to a FOURTH
// region: the "extended region" which MAY be different from the others.
// (Terminological note: this isn't intended to gain regular usage, hence its
// lack of specificity.)
//
// Before _defining_ the extended region, consider the following example:
//
// def = ...
// inst_1
// use %def // added to pruned liveness
// inst_2
// cond_br %c1, die, normal
// die:
// inst_3
// unreachable
// normal:
// inst_4
// destroy %def // NOT added to pruned liveness
// inst_5
//
// This table describes which regions the `inst_i`s are in:
// +------+----+------+--------+---------+
// | |live|pruned|extended|available|
// +------+----+------+--------+---------+
// |inst_1| yes| yes | yes | yes |
// +------+----+------+--------+---------+
// |inst_2| yes| no | yes | yes |
// +------+----+------+--------+---------+
// |inst_3| no | no | yes | yes |
// +------+----+------+--------+---------+
// |inst_4| yes| no | no | yes |
// +------+----+------+--------+---------+
// |inst_5| no | no | no | no |
// +------+----+------+--------+---------+
//
// This example demonstrates that
// pruned live ≠ extended ≠ available
// and indicates the fact that
// pruned live ⊆ extended ⊆ available
//
// The "extended region" is the pruned live region availability-extended into
// dead-end regions. In more detail, it's obtained by (1) unioning the
// dead-end regions adjacent to the pruned live region (the portions of those
// adjacent dead-end regions which are forward reachable from the pruned live
// region) and (2) intersecting the result with the availability region.
//
// That this region is of interest is another result of lacking complete
// OSSA lifetimes.
if (asImpl().isWithinLivenessBoundary(inst)) {
// The extended region is a superset of the pruned live region.
return true;
}
SILBasicBlock *parent = inst->getParent();
if (!deadEndBlocks.isDeadEnd(parent)) {
// The extended region intersected with the non-dead-end region is equal to
// the pruned live region.
return false;
}
switch (liveBlocks.getBlockLiveness(parent)) {
case PrunedLiveBlocks::Dead:
break;
case PrunedLiveBlocks::LiveWithin:
// Dead defs may result in LiveWithin but AvailableOut blocks.
return isInstructionAvailable(inst, deadEndBlocks);
case PrunedLiveBlocks::LiveOut:
// The instruction is not within the boundary, but its parent is LiveOut;
// therefore it must be a def block.
assert(asImpl().isDefBlock(parent));
// Where within the block might the instruction be?
// - before the first def: return false (outside the extended region).
// - between a def and a use: unreachable (withinBoundary would have
// returned true).
// - between a def and another def: unreachable (withinBoundary would have
// returned true)
// - between a use and a def: return false (outside the extended region).
// - after the final def: unreachable (withinBoundary would have returned
// true)
return false;
}
// Check whether `parent` is in the extended region: walk backwards within
// the dead portion of the dead-end region up _through_ the first block which
// is either not dead or not dead-end.
//
// During the walk, if ANY reached block satisfies one of
// (1) dead-end, LiveWithin, !AvailableOut
// (2) NOT dead-end, NOT LiveOut
// then the `parent` is not in the extended region.
//
// Otherwise, ALL reached blocks satisfied one of the following:
// (a) dead-end, Dead
// (b) dead-end, LiveWithin, AvailableOut
// (b) MAYBE dead-end, LiveOut
// In this case, `parent` is in the extended region.
BasicBlockWorklist worklist(parent->getFunction());
worklist.push(parent);
while (auto *block = worklist.pop()) {
auto isLive = liveBlocks.getBlockLiveness(block);
if (!deadEndBlocks.isDeadEnd(block)) {
// The first block beyond the dead-end region has been reached.
if (isLive != PrunedLiveBlocks::LiveOut) {
// Cases (2) above.
return false;
}
// Stop walking. (No longer in the dead portion of the dead-end region.)
continue;
}
switch (isLive) {
case PrunedLiveBlocks::Dead:
// Still within the dead portion of the dead-end region. Keep walking.
for (auto *predecessor : block->getPredecessorBlocks()) {
worklist.pushIfNotVisited(predecessor);
}
continue;
case PrunedLiveBlocks::LiveWithin:
// Availability may have ended in this block. Check whether the block is
// "AvailableOut".
if (!isAvailableOut(block, deadEndBlocks)) {
// Case (1) above.
return false;
}
// Stop walking. (No longer in the dead portion of the dead-end region.)
continue;
case PrunedLiveBlocks::LiveOut:
// Stop walking. (No longer in the dead portion of the dead-end region.)
continue;
}
}
return true;
}
namespace swift::test {
// Arguments:
// - string: "def:"
// - SILValue: value to be analyzed
// - string: "liveness-uses:"
// - variadic list of - SILInstruction: user to pass to updateForUse
// - string: non-ending/ending/non-use
// - string: "uses:"
// - variadic list of - SILInstruction: the instruction to pass to
// areUsesWithinBoundary Dumps:
// - true/false
static FunctionTest SSAPrunedLiveness__areUsesWithinBoundary(
"SSAPrunedLiveness__areUsesWithinBoundary",
[](auto &function, auto &arguments, auto &test) {
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
SSAPrunedLiveness liveness(&function, &discoveredBlocks);
llvm::outs() << "SSAPrunedLiveness:\n";
if (arguments.takeString() != "def:") {
llvm::report_fatal_error("test expects the 'def:' label\n");
}
auto def = arguments.takeValue();
liveness.initializeDef(def);
llvm::outs() << "\tdef: " << def;
if (arguments.takeString() != "liveness-uses:") {
llvm::report_fatal_error("test expects the 'def:' label\n");
}
llvm::outs() << "\tuses:\n";
while (true) {
auto argument = arguments.takeArgument();
if (isa<StringArgument>(argument)) {
auto string = cast<StringArgument>(argument);
if (string.getValue() != "uses:") {
llvm::report_fatal_error("test expects the 'inst:' label\n");
}
break;
}
auto *instruction = cast<InstructionArgument>(argument).getValue();
auto string = arguments.takeString();
PrunedLiveness::LifetimeEnding::Value kind =
llvm::StringSwitch<PrunedLiveness::LifetimeEnding::Value>(string)
.Case("non-ending",
PrunedLiveness::LifetimeEnding::Value::NonEnding)
.Case("ending", PrunedLiveness::LifetimeEnding::Value::Ending)
.Case("non-use", PrunedLiveness::LifetimeEnding::Value::NonUse);
llvm::outs() << "\t\t" << string << " " << *instruction;
liveness.updateForUse(instruction, kind);
}
liveness.print(llvm::outs());
PrunedLivenessBoundary boundary;
liveness.computeBoundary(boundary);
boundary.print(llvm::outs());
llvm::outs() << "\noperands:\n";
SmallVector<Operand *, 4> operands;
while (arguments.hasUntaken()) {
auto *operand = arguments.takeOperand();
operands.push_back(operand);
operand->print(llvm::outs());
}
auto result =
liveness.areUsesWithinBoundary(operands, test.getDeadEndBlocks());
llvm::outs() << "RESULT: " << StringRef(result ? "true" : "false")
<< "\n";
});
} // end namespace swift::test
template <typename LivenessWithDefs>
bool PrunedLiveRange<LivenessWithDefs>::areUsesWithinBoundary(
ArrayRef<Operand *> uses, DeadEndBlocks *deadEndBlocks) const {
assert(asImpl().isInitialized());
for (auto *use : uses) {
auto *user = use->getUser();
if (!isWithinBoundary(user, deadEndBlocks))
return false;
}
return true;
}
template <typename LivenessWithDefs>
bool PrunedLiveRange<LivenessWithDefs>::areUsesOutsideBoundary(
ArrayRef<Operand *> uses, DeadEndBlocks *deadEndBlocks) const {
assert(asImpl().isInitialized());
for (auto *use : uses) {
auto *user = use->getUser();
if (isWithinBoundary(user, deadEndBlocks))
return false;
}
return true;
}
template <typename LivenessWithDefs>
void PrunedLiveRange<LivenessWithDefs>::computeBoundary(
AnyPrunedLivenessBoundary &boundary) const {
assert(asImpl().isInitialized());
for (SILBasicBlock *block : getDiscoveredBlocks()) {
// Process each block that has not been visited and is not LiveOut.
switch (getBlockLiveness(block)) {
case PrunedLiveBlocks::LiveOut:
for (SILBasicBlock *succBB : block->getSuccessors()) {
if (getBlockLiveness(succBB) == PrunedLiveBlocks::Dead) {
boundary.boundaryEdges.push_back(succBB);
}
}
asImpl().findBoundariesInBlock(block, /*isLiveOut*/ true, boundary);
break;
case PrunedLiveBlocks::LiveWithin: {
asImpl().findBoundariesInBlock(block, /*isLiveOut*/ false, boundary);
break;
}
case PrunedLiveBlocks::Dead:
llvm_unreachable("All discovered blocks must be live");
}
}
}
template <typename LivenessWithDefs>
void PrunedLiveRange<LivenessWithDefs>::computeBoundary(
PrunedLivenessBoundary &boundary,
ArrayRef<SILBasicBlock *> postDomBlocks) const {
assert(asImpl().isInitialized());
if (postDomBlocks.empty())
return; // all paths must be dead-ends or infinite loops
BasicBlockWorklist blockWorklist(postDomBlocks[0]->getParent());
// Visit each post-dominating block as the starting point for a
// backward CFG traversal.
for (auto *block : postDomBlocks) {
blockWorklist.pushIfNotVisited(block);
}
while (auto *block = blockWorklist.pop()) {
// Process each block that has not been visited and is not LiveOut.
switch (getBlockLiveness(block)) {
case PrunedLiveBlocks::LiveOut:
asImpl().findBoundariesInBlock(block, /*isLiveOut*/ true, boundary);
break;
case PrunedLiveBlocks::LiveWithin: {
asImpl().findBoundariesInBlock(block, /*isLiveOut*/ false, boundary);
break;
}
case PrunedLiveBlocks::Dead:
// Continue searching upward to find the pruned liveness boundary.
for (auto *predBB : block->getPredecessorBlocks()) {
if (getBlockLiveness(predBB) == PrunedLiveBlocks::LiveOut) {
boundary.boundaryEdges.push_back(block);
} else {
blockWorklist.pushIfNotVisited(predBB);
}
}
break;
}
}
}
namespace swift {
template class PrunedLiveRange<SSAPrunedLiveness>;
template class PrunedLiveRange<MultiDefPrunedLiveness>;
} // namespace swift
//===----------------------------------------------------------------------===//
// SSAPrunedLiveness
//===----------------------------------------------------------------------===//
/// Given live-within (non-live-out) \p block, find the last user.
void PrunedLivenessBoundary::findBoundaryInNonDefBlock(
SILBasicBlock *block, const PrunedLiveness &liveness) {
assert(liveness.getBlockLiveness(block) == PrunedLiveBlocks::LiveWithin);
for (SILInstruction &inst : llvm::reverse(*block)) {
if (liveness.isInterestingUser(&inst)) {
lastUsers.push_back(&inst);
return;
}
}
llvm_unreachable("live-within block must contain an interesting use");
}
void PrunedLivenessBlockBoundary::findBoundaryInNonDefBlock(
SILBasicBlock *block, const PrunedLiveness &liveness) {
assert(liveness.getBlockLiveness(block) == PrunedLiveBlocks::LiveWithin);
endBlocks.push_back(block);
}
/// Given a live-within \p block that contains an SSA definition, and knowledge
/// that all live uses are dominated by that single definition, find either the
/// last user or a dead def.
///
/// A live range with a single definition cannot have any uses above that
/// definition in the same block. This even holds for unreachable self-loops.
void PrunedLivenessBoundary::findBoundaryInSSADefBlock(
SILNode *ssaDef, const PrunedLiveness &liveness) {
// defInst is null for argument defs.
SILInstruction *defInst = dyn_cast<SILInstruction>(ssaDef);
for (SILInstruction &inst : llvm::reverse(*ssaDef->getParentBlock())) {
if (&inst == defInst) {
deadDefs.push_back(cast<SILNode>(&inst));
return;
}
if (liveness.isInterestingUser(&inst)) {
lastUsers.push_back(&inst);
return;
}
}
auto *deadArg = dyn_cast<SILArgument>(ssaDef);
assert(deadArg
&& "findBoundariesInBlock must be called on a live block");
deadDefs.push_back(deadArg);
}
void PrunedLivenessBlockBoundary::findBoundaryInSSADefBlock(
SILNode *ssaDef, const PrunedLiveness &liveness) {
endBlocks.push_back(ssaDef->getParentBlock());
}
void SSAPrunedLiveness::findBoundariesInBlock(
SILBasicBlock *block, bool isLiveOut,
AnyPrunedLivenessBoundary &boundary) const {
assert(isInitialized());
// For SSA, a live-out block cannot have a boundary.
if (isLiveOut)
return;
// Handle live-within block
if (!isDefBlock(block)) {
boundary.findBoundaryInNonDefBlock(block, *this);
return;
}
// Find either the last user or a dead def
auto *defInst = def->getDefiningInstruction();
SILNode *defNode = defInst ? cast<SILNode>(defInst) : cast<SILArgument>(def);
boundary.findBoundaryInSSADefBlock(defNode, *this);
}
//===----------------------------------------------------------------------===//
// MultiDefPrunedLiveness
//===----------------------------------------------------------------------===//
bool MultiDefPrunedLiveness::isUserBeforeDef(SILInstruction *user) const {
auto *block = user->getParent();
if (!isDefBlock(block))
return false;
if (llvm::any_of(block->getArguments(), [this](SILArgument *arg) {
return isDef(arg);
})) {
return false;
}
auto *current = user;
while (true) {
// If user is also a def, then the use is considered before the def.
current = current->getPreviousInstruction();
if (!current)
return true;
if (isDef(current))
return false;
}
}
namespace swift::test {
// Arguments:
// - the string "defs:"
// - list of live-range defining values or instructions
// - the string "uses:"
// - variadic list of live-range user instructions
// Dumps:
// - the liveness result and boundary
//
// Computes liveness for the specified def nodes by considering only the
// specified uses. The actual uses of the def nodes are ignored.
//
// This is useful for testing non-ssa liveness, for example, of memory
// locations. In that case, the def nodes may be stores and the uses may be
// destroy_addrs.
static FunctionTest MultiDefUseLivenessTest(
"multidefuse_liveness", [](auto &function, auto &arguments, auto &test) {
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
MultiDefPrunedLiveness liveness(&function, &discoveredBlocks);
llvm::outs() << "MultiDef lifetime analysis:\n";
if (arguments.takeString() != "defs:") {
llvm::report_fatal_error(
"test specification expects the 'defs:' label\n");
}
while (true) {
auto argument = arguments.takeArgument();
if (isa<InstructionArgument>(argument)) {
auto *instruction = cast<InstructionArgument>(argument).getValue();
llvm::outs() << " def instruction: " << *instruction;
liveness.initializeDef(instruction);
continue;
}
if (isa<ValueArgument>(argument)) {
SILValue value = cast<ValueArgument>(argument).getValue();
llvm::outs() << " def value: " << value;
liveness.initializeDef(value);
continue;
}
if (cast<StringArgument>(argument).getValue() != "uses:") {
llvm::report_fatal_error(
"test specification expects the 'uses:' label\n");
}
break;
}
while (arguments.hasUntaken()) {
auto *inst = arguments.takeInstruction();
// lifetimeEnding has no effects on liveness, it's only a cache for the
// caller.
liveness.updateForUse(inst, /*lifetimeEnding*/ false);
}
liveness.print(llvm::outs());
PrunedLivenessBoundary boundary;
liveness.computeBoundary(boundary);
boundary.print(llvm::outs());
});
} // end namespace swift::test
void MultiDefPrunedLiveness::findBoundariesInBlock(
SILBasicBlock *block, bool isLiveOut,
AnyPrunedLivenessBoundary &boundary) const {
assert(isInitialized());
if (!isDefBlock(block)) {
// A live-out block with no defs cannot have a boundary.
if (!isLiveOut) {
boundary.findBoundaryInNonDefBlock(block, *this);
}
return;
}
// Handle def blocks...
//
// First, check for an SSA live range
if (++defs.begin() == defs.end()) {
// For SSA, a live-out block cannot have a boundary.
if (!isLiveOut) {
boundary.findBoundaryInSSADefBlock(*defs.begin(), *this);
}
return;
}
boundary.findBoundaryInMultiDefBlock(block, isLiveOut, *this);
}
void PrunedLivenessBoundary::findBoundaryInMultiDefBlock(
SILBasicBlock *block, bool isLiveOut,
const MultiDefPrunedLiveness &liveness) {
// Handle a live-out or live-within block with potentially multiple defs
unsigned prevCount = deadDefs.size() + lastUsers.size();
(void)prevCount;
bool isLive = isLiveOut;
for (auto &inst : llvm::reverse(*block)) {
// Check if the instruction is a def before checking whether it is a
// use. The same instruction can be both a dead def and boundary use.
if (liveness.isDef(&inst)) {
if (!isLive) {
deadDefs.push_back(cast<SILNode>(&inst));
}
isLive = false;
}
// Note: the same instruction could potentially be both a dead def and last
// user. The liveness boundary supports this, although it won't happen in
// any context where we care about inserting code on the boundary.
if (!isLive && liveness.isInterestingUser(&inst)) {
lastUsers.push_back(&inst);
isLive = true;
}
}
if (!isLive) {
for (SILArgument *deadArg : block->getArguments()) {
if (liveness.defs.contains(deadArg)) {
deadDefs.push_back(deadArg);
}
}
if (auto *predBB = block->getSinglePredecessorBlock()) {
if (liveness.getBlockLiveness(predBB) == PrunedLiveBlocks::LiveOut) {
boundaryEdges.push_back(block);
}
}
}
// All live-within blocks must contain a boundary.
assert(isLiveOut ||
(prevCount < deadDefs.size() + lastUsers.size()) &&
"findBoundariesInBlock must be called on a live block");
}
void PrunedLivenessBlockBoundary::findBoundaryInMultiDefBlock(
SILBasicBlock *block, bool isLiveOut,
const MultiDefPrunedLiveness &liveness) {
bool isLive = isLiveOut;
for (auto &inst : llvm::reverse(*block)) {
// Check if the instruction is a def before checking whether it is a
// use. The same instruction can be both a dead def and boundary use.
if (liveness.isDef(&inst)) {
if (!isLive) {
endBlocks.push_back(block);
return;
}
isLive = false;
}
if (!isLive && liveness.isInterestingUser(&inst)) {
endBlocks.push_back(block);
return;
}
}
if (!isLive) {
for (SILArgument *deadArg : block->getArguments()) {
if (liveness.defs.contains(deadArg)) {
endBlocks.push_back(block);
return;
}
}
if (auto *predBB = block->getSinglePredecessorBlock()) {
if (liveness.getBlockLiveness(predBB) == PrunedLiveBlocks::LiveOut) {
boundaryEdges.push_back(block);
return;
}
}
}
}
LiveRangeSummary MultiDefPrunedLiveness::computeSimple() {
assert(isInitialized() && "defs uninitialized");
LiveRangeSummary summary;
for (SILNode *defNode : defs) {
if (auto *arg = dyn_cast<SILArgument>(defNode))
summary.meet(updateForDef(arg));
else {
for (auto result : cast<SILInstruction>(defNode)->getResults()) {
summary.meet(updateForDef(result));
}
}
}
return summary;
}
namespace swift::test {
// Arguments:
// - variadic list of live-range defining values or instructions
// Dumps:
// - the liveness result and boundary
//
// Computes liveness for the specified def nodes by finding all their direct SSA
// uses. If the def is an instruction, then all results are considered.
static FunctionTest MultiDefLivenessTest(
"multidef_liveness", [](auto &function, auto &arguments, auto &test) {
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
MultiDefPrunedLiveness liveness(&function, &discoveredBlocks);
llvm::outs() << "MultiDef lifetime analysis:\n";
while (arguments.hasUntaken()) {
auto argument = arguments.takeArgument();
if (isa<InstructionArgument>(argument)) {
auto *instruction = cast<InstructionArgument>(argument).getValue();
llvm::outs() << " def instruction: " << instruction;
liveness.initializeDef(instruction);
} else {
SILValue value = cast<ValueArgument>(argument).getValue();
llvm::outs() << " def value: " << value;
liveness.initializeDef(value);
}
}
liveness.computeSimple();
liveness.print(llvm::outs());
PrunedLivenessBoundary boundary;
liveness.computeBoundary(boundary);
boundary.print(llvm::outs());
});
} // end namespace swift::test
//===----------------------------------------------------------------------===//
// DiagnosticPrunedLiveness
//===----------------------------------------------------------------------===//
// FIXME: This is wrong. Why is nonLifetimeEndingUsesInLiveOut inside
// PrunedLiveness, and what does it mean? Blocks may transition to LiveOut
// later. Or they may already be LiveOut from a previous use. After computing
// liveness, clients should check uses that are in PrunedLivenessBoundary.
void DiagnosticPrunedLiveness::
updateForUse(SILInstruction *user, bool lifetimeEnding) {
SSAPrunedLiveness::updateForUse(user, 0);
auto useBlockLive = getBlockLiveness(user->getParent());
// Record all uses of blocks on the liveness boundary. For blocks marked
// LiveWithin, the boundary is considered to be the last use in the block.
if (!lifetimeEnding && useBlockLive == PrunedLiveBlocks::LiveOut) {
if (nonLifetimeEndingUsesInLiveOut)
nonLifetimeEndingUsesInLiveOut->insert(user);
return;
}
}
|