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 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
|
//===--- Devirtualize.cpp - Helper for devirtualizing apply ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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-devirtualize-utility"
#include "swift/SILOptimizer/Utils/Devirtualize.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/Types.h"
#include "swift/SIL/CalleeCache.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/OptimizationRemark.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILType.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Casting.h"
using namespace swift;
STATISTIC(NumClassDevirt, "Number of class_method applies devirtualized");
STATISTIC(NumWitnessDevirt, "Number of witness_method applies devirtualized");
//===----------------------------------------------------------------------===//
// Class Method Optimization
//===----------------------------------------------------------------------===//
void swift::getAllSubclasses(ClassHierarchyAnalysis *cha, ClassDecl *cd,
CanType classType, SILModule &module,
ClassHierarchyAnalysis::ClassList &subs) {
// Collect the direct and indirect subclasses for the class.
// Sort these subclasses in the order they should be tested by the
// speculative devirtualization. Different strategies could be used,
// E.g. breadth-first, depth-first, etc.
// Currently, let's use the breadth-first strategy.
// The exact static type of the instance should be tested first.
auto &directSubs = cha->getDirectSubClasses(cd);
auto &indirectSubs = cha->getIndirectSubClasses(cd);
subs.append(directSubs.begin(), directSubs.end());
subs.append(indirectSubs.begin(), indirectSubs.end());
// FIXME: This is wrong -- we could have a non-generic class nested
// inside a generic class
if (isa<BoundGenericClassType>(classType)) {
// Filter out any subclasses that do not inherit from this
// specific bound class.
auto removedIt =
std::remove_if(subs.begin(), subs.end(), [&classType](ClassDecl *sub) {
// FIXME: Add support for generic subclasses.
if (sub->isGenericContext())
return false;
auto subCanTy = sub->getDeclaredInterfaceType()->getCanonicalType();
// Handle the usual case here: the class in question
// should be a real subclass of a bound generic class.
return !classType->isBindableToSuperclassOf(subCanTy);
});
subs.erase(removedIt, subs.end());
}
}
/// Returns true, if a method implementation corresponding to
/// the class_method applied to an instance of the class cd is
/// effectively final, i.e. it is statically known to be not overridden
/// by any subclasses of the class cd.
///
/// \p applySite invocation instruction
/// \p classType type of the instance
/// \p cd static class of the instance whose method is being invoked
/// \p cha class hierarchy analysis
static bool isEffectivelyFinalMethod(FullApplySite applySite, CanType classType,
ClassDecl *cd,
ClassHierarchyAnalysis *cha) {
if (cd && cd->isFinal())
return true;
auto *cmi = cast<MethodInst>(applySite.getCallee());
if (!calleesAreStaticallyKnowable(applySite.getModule(), cmi->getMember()))
return false;
auto *method = cmi->getMember().getAbstractFunctionDecl();
assert(method && "Expected abstract function decl!");
assert(!method->isFinal() && "Unexpected indirect call to final method!");
// If this method is not overridden in the module,
// there is no other implementation.
if (!method->isOverridden())
return true;
// Class declaration may be nullptr, e.g. for cases like:
// func foo<C:Base>(c: C) {}, where C is a class, but
// it does not have a class decl.
if (!cd)
return false;
if (!cha)
return false;
// We can analyze the class hierarchy rooted at this class and
// eventually devirtualize a method call more efficiently.
ClassHierarchyAnalysis::ClassList subs;
getAllSubclasses(cha, cd, classType, applySite.getModule(), subs);
// This is the implementation of the method to be used
// if the exact class of the instance would be cd.
auto *ImplMethod = cd->findImplementingMethod(method);
// First, analyze all direct subclasses.
for (auto S : subs) {
// Check if the subclass overrides a method and provides
// a different implementation.
auto *ImplFD = S->findImplementingMethod(method);
if (ImplFD != ImplMethod)
return false;
}
return true;
}
/// Check if a given class is final in terms of a current
/// compilation, i.e.:
/// - it is really final
/// - or it is private and has not sub-classes
/// - or it is an internal class without sub-classes and
/// it is a whole-module compilation.
static bool isKnownFinalClass(ClassDecl *cd, SILModule &module,
ClassHierarchyAnalysis *cha) {
if (cd->isFinal())
return true;
// Only handle classes defined within the SILModule's associated context.
if (!cd->isChildContextOf(module.getAssociatedContext()))
return false;
if (!cd->hasAccess())
return false;
// Only consider 'private' members, unless we are in whole-module compilation.
switch (cd->getEffectiveAccess()) {
case AccessLevel::Open:
return false;
case AccessLevel::Public:
case AccessLevel::Package:
case AccessLevel::Internal:
if (!module.isWholeModule())
return false;
break;
case AccessLevel::FilePrivate:
case AccessLevel::Private:
break;
}
// Take the ClassHierarchyAnalysis into account.
// If a given class has no subclasses and
// - private
// - or internal and it is a WMO compilation
// then this class can be considered final for the purpose
// of devirtualization.
if (cha) {
if (!cha->hasKnownDirectSubclasses(cd)) {
switch (cd->getEffectiveAccess()) {
case AccessLevel::Open:
return false;
case AccessLevel::Public:
case AccessLevel::Package:
case AccessLevel::Internal:
if (!module.isWholeModule())
return false;
break;
case AccessLevel::FilePrivate:
case AccessLevel::Private:
break;
}
return true;
}
}
return false;
}
// Attempt to get the instance for S, whose static type is the same as
// its exact dynamic type, returning a null SILValue() if we cannot find it.
// The information that a static type is the same as the exact dynamic,
// can be derived e.g.:
// - from a constructor or
// - from a successful outcome of a checked_cast_br [exact] instruction.
SILValue swift::getInstanceWithExactDynamicType(SILValue instance,
ClassHierarchyAnalysis *cha) {
auto *f = instance->getFunction();
auto &module = f->getModule();
while (instance) {
instance = stripCasts(instance);
if (isa<AllocRefInst>(instance) || isa<MetatypeInst>(instance)) {
if (instance->getType().getASTType()->hasDynamicSelfType())
return SILValue();
return instance;
}
auto *arg = dyn_cast<SILArgument>(instance);
if (!arg)
break;
auto *singlePred = arg->getParent()->getSinglePredecessorBlock();
if (!singlePred) {
if (!isa<SILFunctionArgument>(arg))
break;
auto *cd = arg->getType().getClassOrBoundGenericClass();
// Check if this class is effectively final.
if (!cd || !isKnownFinalClass(cd, module, cha))
break;
return arg;
}
// Traverse the chain of predecessors.
if (isa<BranchInst>(singlePred->getTerminator())
|| isa<CondBranchInst>(singlePred->getTerminator())) {
instance = cast<SILPhiArgument>(arg)->getIncomingPhiValue(singlePred);
continue;
}
// If it is a BB argument received on a success branch
// of a checked_cast_br, then we know its exact type.
auto *ccbi = dyn_cast<CheckedCastBranchInst>(singlePred->getTerminator());
if (!ccbi)
break;
if (!ccbi->isExact() || ccbi->getSuccessBB() != arg->getParent())
break;
return instance;
}
return SILValue();
}
/// Try to determine the exact dynamic type of an object.
/// returns the exact dynamic type of the object, or an empty type if the exact
/// type could not be determined.
SILType swift::getExactDynamicType(SILValue instance,
ClassHierarchyAnalysis *cha,
bool forUnderlyingObject) {
auto *f = instance->getFunction();
auto &module = f->getModule();
// Set of values to be checked for their exact types.
SmallVector<SILValue, 8> worklist;
// The detected type of the underlying object.
SILType resultType;
// Set of processed values.
llvm::SmallSet<SILValue, 8> processed;
worklist.push_back(instance);
while (!worklist.empty()) {
auto v = worklist.pop_back_val();
if (!v)
return SILType();
if (processed.count(v))
continue;
processed.insert(v);
// For underlying object strip casts and projections.
// For the object itself, simply strip casts.
v = forUnderlyingObject ? getUnderlyingObject(v) : stripCasts(v);
if (isa<AllocRefInst>(v) || isa<MetatypeInst>(v)) {
if (resultType && resultType != v->getType())
return SILType();
resultType = v->getType();
continue;
}
if (isa<LiteralInst>(v)) {
if (resultType && resultType != v->getType())
return SILType();
resultType = v->getType();
continue;
}
if (isa<StructInst>(v) || isa<TupleInst>(v) || isa<EnumInst>(v)) {
if (resultType && resultType != v->getType())
return SILType();
resultType = v->getType();
continue;
}
if (forUnderlyingObject) {
if (isa<AllocationInst>(v)) {
if (resultType && resultType != v->getType())
return SILType();
resultType = v->getType();
continue;
}
}
auto arg = dyn_cast<SILArgument>(v);
if (!arg) {
// We don't know what it is.
return SILType();
}
if (auto *fArg = dyn_cast<SILFunctionArgument>(arg)) {
// Bail on metatypes for now.
if (fArg->getType().is<AnyMetatypeType>()) {
return SILType();
}
auto *cd = fArg->getType().getClassOrBoundGenericClass();
// If it is not class and it is a trivial type, then it
// should be the exact type.
if (!cd && fArg->getType().isTrivial(*f)) {
if (resultType && resultType != fArg->getType())
return SILType();
resultType = fArg->getType();
continue;
}
if (!cd) {
// It is not a class or a trivial type, so we don't know what it is.
return SILType();
}
// Check if this class is effectively final.
if (!isKnownFinalClass(cd, module, cha)) {
return SILType();
}
if (resultType && resultType != fArg->getType())
return SILType();
resultType = fArg->getType();
continue;
}
auto *singlePred = arg->getParent()->getSinglePredecessorBlock();
if (singlePred) {
// If it is a BB argument received on a success branch
// of a checked_cast_br, then we know its exact type.
auto *ccbi = dyn_cast<CheckedCastBranchInst>(singlePred->getTerminator());
if (ccbi && ccbi->isExact() && ccbi->getSuccessBB() == arg->getParent()) {
if (resultType && resultType != arg->getType())
return SILType();
resultType = arg->getType();
continue;
}
}
// It is a BB argument, look through incoming values. If they all have the
// same exact type, then we consider it to be the type of the BB argument.
SmallVector<SILValue, 4> incomingValues;
if (arg->getSingleTerminatorOperands(incomingValues)) {
for (auto inValue : incomingValues) {
worklist.push_back(inValue);
}
continue;
}
// The exact type is unknown.
return SILType();
}
return resultType;
}
/// Try to determine the exact dynamic type of the underlying object.
/// returns the exact dynamic type of a value, or an empty type if the exact
/// type could not be determined.
SILType
swift::getExactDynamicTypeOfUnderlyingObject(SILValue instance,
ClassHierarchyAnalysis *cha) {
return getExactDynamicType(instance, cha, /* forUnderlyingObject */ true);
}
// Start with the substitutions from the apply.
// Try to propagate them to find out the real substitutions required
// to invoke the method.
static SubstitutionMap
getSubstitutionsForCallee(SILModule &module, CanSILFunctionType baseCalleeType,
CanType derivedSelfType, FullApplySite applySite) {
// If the base method is not polymorphic, no substitutions are required,
// even if we originally had substitutions for calling the derived method.
if (!baseCalleeType->isPolymorphic())
return SubstitutionMap();
// Add any generic substitutions for the base class.
Type baseSelfType = baseCalleeType->getSelfParameter().getArgumentType(
module, baseCalleeType,
applySite.getFunction()->getTypeExpansionContext());
if (auto metatypeType = baseSelfType->getAs<MetatypeType>())
baseSelfType = metatypeType->getInstanceType();
auto *baseClassDecl = baseSelfType->getClassOrBoundGenericClass();
assert(baseClassDecl && "not a class method");
unsigned baseDepth = 0;
SubstitutionMap baseSubMap;
if (auto baseClassSig = baseClassDecl->getGenericSignatureOfContext()) {
baseDepth = baseClassSig.getGenericParams().back()->getDepth() + 1;
// Compute the type of the base class, starting from the
// derived class type and the type of the method's self
// parameter.
Type derivedClass = derivedSelfType;
if (auto metatypeType = derivedClass->getAs<MetatypeType>())
derivedClass = metatypeType->getInstanceType();
baseSubMap = derivedClass->getContextSubstitutionMap(
module.getSwiftModule(), baseClassDecl);
}
SubstitutionMap origSubMap = applySite.getSubstitutionMap();
Type calleeSelfType =
applySite.getOrigCalleeType()->getSelfParameter().getArgumentType(
module, applySite.getOrigCalleeType(),
applySite.getFunction()->getTypeExpansionContext());
if (auto metatypeType = calleeSelfType->getAs<MetatypeType>())
calleeSelfType = metatypeType->getInstanceType();
auto *calleeClassDecl = calleeSelfType->getClassOrBoundGenericClass();
assert(calleeClassDecl && "self is not a class type");
// Add generic parameters from the method itself, ignoring any generic
// parameters from the derived class.
unsigned origDepth = 0;
if (auto calleeClassSig = calleeClassDecl->getGenericSignatureOfContext())
origDepth = calleeClassSig.getGenericParams().back()->getDepth() + 1;
auto baseCalleeSig = baseCalleeType->getInvocationGenericSignature();
return
SubstitutionMap::combineSubstitutionMaps(baseSubMap,
origSubMap,
CombineSubstitutionMaps::AtDepth,
baseDepth,
origDepth,
baseCalleeSig);
}
// Return the new apply and true if a cast required CFG modification.
static std::pair<ApplyInst *, bool /* changedCFG */>
replaceApplyInst(SILBuilder &builder, SILLocation loc, ApplyInst *oldAI,
SILValue newFn, SubstitutionMap newSubs,
ArrayRef<SILValue> newArgs, ArrayRef<SILValue> newArgBorrows) {
auto *newAI =
builder.createApply(loc, newFn, newSubs, newArgs,
oldAI->getApplyOptions());
if (!newArgBorrows.empty()) {
for (SILValue arg : newArgBorrows) {
builder.createEndBorrow(loc, arg);
}
}
// Check if any casting is required for the return value. newAI cannot be a
// guaranteed value, so this cast cannot generate borrow scopes and it can be
// used anywhere the original oldAI was used.
auto castRes = castValueToABICompatibleType(
&builder, loc, newAI, newAI->getType(), oldAI->getType(), /*usePoints*/ {});
oldAI->replaceAllUsesWith(castRes.first);
return {newAI, castRes.second};
}
// Return the new try_apply and true if a cast required CFG modification.
static std::pair<TryApplyInst *, bool /* changedCFG */>
replaceTryApplyInst(SILBuilder &builder, SILLocation loc, TryApplyInst *oldTAI,
SILValue newFn, SubstitutionMap newSubs,
ArrayRef<SILValue> newArgs, SILFunctionConventions conv,
ArrayRef<SILValue> newArgBorrows) {
SILBasicBlock *normalBB = oldTAI->getNormalBB();
SILBasicBlock *resultBB = nullptr;
SILType newResultTy =
conv.getSILResultType(builder.getTypeExpansionContext());
// Does the result value need to be casted?
auto oldResultTy = normalBB->getArgument(0)->getType();
bool resultCastRequired = newResultTy != oldResultTy;
// Create a new normal BB only if the result of the new apply differs
// in type from the argument of the original normal BB.
if (!resultCastRequired) {
resultBB = normalBB;
} else {
resultBB = builder.getFunction().createBasicBlockBefore(normalBB);
resultBB->createPhiArgument(newResultTy, OwnershipKind::Owned);
}
// We can always just use the original error BB because we'll be
// deleting the edge to it from the old TAI.
SILBasicBlock *errorBB = oldTAI->getErrorBB();
// Insert a try_apply here.
// Note that this makes this block temporarily double-terminated!
// We won't fix that until deleteDevirtualizedApply.
auto newTAI =
builder.createTryApply(loc, newFn, newSubs, newArgs, resultBB, errorBB,
oldTAI->getApplyOptions());
if (!newArgBorrows.empty()) {
builder.setInsertionPoint(normalBB->begin());
for (SILValue arg : newArgBorrows) {
builder.createEndBorrow(loc, arg);
}
builder.setInsertionPoint(errorBB->begin());
for (SILValue arg : newArgBorrows) {
builder.createEndBorrow(loc, arg);
}
}
if (resultCastRequired) {
builder.setInsertionPoint(resultBB);
SILValue resultValue = resultBB->getArgument(0);
// resultValue cannot be a guaranteed value, so this cast cannot generate
// borrow scopes and it can be used anywhere the original oldAI was
// used--usePoints are not required.
std::tie(resultValue, std::ignore) = castValueToABICompatibleType(
&builder, loc, resultValue, newResultTy, oldResultTy, /*usePoints*/ {});
builder.createBranch(loc, normalBB, {resultValue});
}
builder.setInsertionPoint(normalBB->begin());
return {newTAI, resultCastRequired};
}
// Return the new begin_apply and true if a cast required CFG modification.
static std::pair<BeginApplyInst *, bool /* changedCFG */>
replaceBeginApplyInst(SILBuilder &builder, SILLocation loc,
BeginApplyInst *oldBAI, SILValue newFn,
SubstitutionMap newSubs, ArrayRef<SILValue> newArgs,
ArrayRef<SILValue> newArgBorrows) {
bool changedCFG = false;
auto *newBAI = builder.createBeginApply(loc, newFn, newSubs, newArgs,
oldBAI->getApplyOptions());
// Forward the token.
oldBAI->getTokenResult()->replaceAllUsesWith(newBAI->getTokenResult());
auto oldYields = oldBAI->getYieldedValues();
auto newYields = newBAI->getYieldedValues();
assert(oldYields.size() == newYields.size());
for (auto i : indices(oldYields)) {
auto oldYield = oldYields[i];
auto newYield = newYields[i];
// Insert any end_borrow if the yielded value before the token's uses.
SmallVector<SILInstruction *, 4> users(
makeUserIteratorRange(oldYield->getUses()));
auto yieldCastRes = castValueToABICompatibleType(
&builder, loc, newYield, newYield->getType(), oldYield->getType(),
users);
oldYield->replaceAllUsesWith(yieldCastRes.first);
changedCFG |= yieldCastRes.second;
}
if (newArgBorrows.empty())
return {newBAI, changedCFG};
SILValue token = newBAI->getTokenResult();
// The token will only be used by end_apply and abort_apply. Use that to
// insert the end_borrows we need /after/ those uses.
for (auto *use : token->getUses()) {
SILBuilderWithScope borrowBuilder(
&*std::next(use->getUser()->getIterator()),
builder.getBuilderContext());
for (SILValue borrow : newArgBorrows) {
borrowBuilder.createEndBorrow(loc, borrow);
}
}
return {newBAI, changedCFG};
}
// Return the new partial_apply and true if a cast required CFG modification.
static std::pair<PartialApplyInst *, bool /* changedCFG */>
replacePartialApplyInst(SILBuilder &builder, SILLocation loc,
PartialApplyInst *oldPAI, SILValue newFn,
SubstitutionMap newSubs, ArrayRef<SILValue> newArgs) {
auto convention = oldPAI->getCalleeConvention();
auto isolation = oldPAI->getResultIsolation();
auto *newPAI =
builder.createPartialApply(loc, newFn, newSubs, newArgs, convention,
isolation);
// Check if any casting is required for the partially-applied function.
// A non-guaranteed cast needs no usePoints.
assert(newPAI->getOwnershipKind() != OwnershipKind::Guaranteed);
auto castRes = castValueToABICompatibleType(
&builder, loc, newPAI, newPAI->getType(), oldPAI->getType(),
/*usePoints*/ {});
oldPAI->replaceAllUsesWith(castRes.first);
return {newPAI, castRes.second};
}
// Return the new apply and true if the CFG was also modified.
static std::pair<ApplySite, bool /* changedCFG */>
replaceApplySite(SILBuilder &builder, SILLocation loc, ApplySite oldAS,
SILValue newFn, SubstitutionMap newSubs,
ArrayRef<SILValue> newArgs, SILFunctionConventions conv,
ArrayRef<SILValue> newArgBorrows) {
switch (oldAS.getKind()) {
case ApplySiteKind::ApplyInst: {
auto *oldAI = cast<ApplyInst>(oldAS);
return replaceApplyInst(builder, loc, oldAI, newFn, newSubs, newArgs,
newArgBorrows);
}
case ApplySiteKind::TryApplyInst: {
auto *oldTAI = cast<TryApplyInst>(oldAS);
return replaceTryApplyInst(builder, loc, oldTAI, newFn, newSubs, newArgs,
conv, newArgBorrows);
}
case ApplySiteKind::BeginApplyInst: {
auto *oldBAI = dyn_cast<BeginApplyInst>(oldAS);
return replaceBeginApplyInst(builder, loc, oldBAI, newFn, newSubs, newArgs,
newArgBorrows);
}
case ApplySiteKind::PartialApplyInst: {
assert(newArgBorrows.empty());
auto *oldPAI = cast<PartialApplyInst>(oldAS);
return replacePartialApplyInst(builder, loc, oldPAI, newFn, newSubs,
newArgs);
}
}
llvm_unreachable("covered switch");
}
/// Delete an apply site that's been successfully devirtualized.
void swift::deleteDevirtualizedApply(ApplySite old) {
auto *oldApply = old.getInstruction();
recursivelyDeleteTriviallyDeadInstructions(oldApply, true);
}
SILFunction *swift::getTargetClassMethod(SILModule &module, ClassDecl *cd,
CanType classType, MethodInst *mi) {
assert((isa<ClassMethodInst>(mi) || isa<SuperMethodInst>(mi)) &&
"Only class_method and super_method instructions are supported");
SILDeclRef member = mi->getMember();
SILType silType = SILType::getPrimitiveObjectType(classType);
if (auto *vtable = module.lookUpSpecializedVTable(silType)) {
return vtable->getEntry(module, member)->getImplementation();
}
return module.lookUpFunctionInVTable(cd, member);
}
CanType swift::getSelfInstanceType(CanType classOrMetatypeType) {
if (auto metaType = dyn_cast<MetatypeType>(classOrMetatypeType))
classOrMetatypeType = metaType.getInstanceType();
if (auto selfType = dyn_cast<DynamicSelfType>(classOrMetatypeType))
classOrMetatypeType = selfType.getSelfType();
return classOrMetatypeType;
}
/// Check if it is possible to devirtualize an Apply instruction
/// and a class member obtained using the class_method instruction into
/// a direct call to a specific member of a specific class.
///
/// \p applySite is the apply to devirtualize.
/// \p cd is the class declaration we are devirtualizing for.
/// return true if it is possible to devirtualize, false - otherwise.
bool swift::canDevirtualizeClassMethod(FullApplySite applySite, ClassDecl *cd,
CanType classType,
OptRemark::Emitter *ore,
bool isEffectivelyFinalMethod) {
LLVM_DEBUG(llvm::dbgs() << " Trying to devirtualize : "
<< *applySite.getInstruction());
SILModule &module = applySite.getModule();
auto *mi = cast<MethodInst>(applySite.getCallee());
// Find the implementation of the member which should be invoked.
auto *f = getTargetClassMethod(module, cd, classType, mi);
// If we do not find any such function, we have no function to devirtualize
// to... so bail.
if (!f) {
LLVM_DEBUG(llvm::dbgs() << " FAIL: Could not find matching VTable "
"or vtable method for this class.\n");
return false;
}
// We need to disable the “effectively final” opt if a function is inlinable
if (isEffectivelyFinalMethod && applySite.getFunction()->isSerialized()) {
LLVM_DEBUG(llvm::dbgs() << " FAIL: Could not optimize function "
"because it is an effectively-final inlinable: "
<< applySite.getFunction()->getName() << "\n");
return false;
}
// Mandatory inlining does class method devirtualization. I'm not sure if this
// is really needed, but some test rely on this.
// So even for Onone functions we have to do it if the SILStage is raw.
if (f->getModule().getStage() != SILStage::Raw && !f->shouldOptimize()) {
// Do not consider functions that should not be optimized.
LLVM_DEBUG(llvm::dbgs()
<< " FAIL: Could not optimize function "
<< " because it is marked no-opt: " << f->getName() << "\n");
return false;
}
if (applySite.getFunction()->isAnySerialized()) {
// function_ref inside fragile function cannot reference a private or
// hidden symbol.
if (!f->hasValidLinkageForFragileRef(
applySite.getFunction()->getSerializedKind()))
return false;
}
// devirtualizeClassMethod below does not support this case. It currently
// assumes it can try_apply call the target.
if (!f->getLoweredFunctionType()->hasErrorResult()
&& isa<TryApplyInst>(applySite.getInstruction())) {
LLVM_DEBUG(llvm::dbgs() << " FAIL: Trying to devirtualize a "
"try_apply but vtable entry has no error result.\n");
return false;
}
return true;
}
/// Devirtualize an apply of a class method.
///
/// \p applySite is the apply to devirtualize.
/// \p ClassOrMetatype is a class value or metatype value that is the
/// self argument of the apply we will devirtualize.
/// return the result value of the new ApplyInst if created one or null.
///
/// Return the new apply and true if the CFG was also modified.
std::pair<FullApplySite, bool /* changedCFG */>
swift::devirtualizeClassMethod(FullApplySite applySite,
SILValue classOrMetatype, ClassDecl *cd,
CanType classType, OptRemark::Emitter *ore) {
bool changedCFG = false;
LLVM_DEBUG(llvm::dbgs() << " Trying to devirtualize : "
<< *applySite.getInstruction());
SILModule &module = applySite.getModule();
auto *mi = cast<MethodInst>(applySite.getCallee());
auto *f = getTargetClassMethod(module, cd, classType, mi);
CanSILFunctionType genCalleeType = f->getLoweredFunctionTypeInContext(
TypeExpansionContext(*applySite.getFunction()));
SubstitutionMap subs = getSubstitutionsForCallee(
module, genCalleeType, classOrMetatype->getType().getASTType(),
applySite);
CanSILFunctionType substCalleeType = genCalleeType;
if (genCalleeType->isPolymorphic())
substCalleeType = genCalleeType->substGenericArgs(
module, subs, TypeExpansionContext(*applySite.getFunction()));
SILFunctionConventions substConv(substCalleeType, module);
SILBuilderWithScope builder(applySite.getInstruction());
SILLocation loc = applySite.getLoc();
auto *fri = builder.createFunctionRefFor(loc, f);
// Create the argument list for the new apply, casting when needed
// in order to handle covariant indirect return types and
// contravariant argument types.
SmallVector<SILValue, 8> newArgs;
// If we have a value that is owned, but that we are going to use in as a
// guaranteed argument, we need to borrow/unborrow the argument. Otherwise, we
// will introduce new consuming uses. In contrast, if we have an owned value,
// we are ok due to the forwarding nature of upcasts.
SmallVector<SILValue, 8> newArgBorrows;
auto indirectResultArgIter = applySite.getIndirectSILResults().begin();
for (auto resultTy : substConv.getIndirectSILResultTypes(
applySite.getFunction()->getTypeExpansionContext())) {
auto castRes = castValueToABICompatibleType(
&builder, loc, *indirectResultArgIter, indirectResultArgIter->getType(),
resultTy, {applySite.getInstruction()});
newArgs.push_back(castRes.first);
changedCFG |= castRes.second;
++indirectResultArgIter;
}
if (SILType errorTy = substConv.getIndirectErrorResultType(applySite.getFunction()->getTypeExpansionContext())) {
auto errorArgs = applySite.getIndirectSILErrorResults();
assert(errorArgs.size() == 1);
SILValue errorArg = errorArgs[0];
auto castRes = castValueToABICompatibleType(
&builder, loc, errorArg, errorArg->getType(),
errorTy, {applySite.getInstruction()});
newArgs.push_back(castRes.first);
changedCFG |= castRes.second;
}
auto paramArgIter = applySite.getArgumentsWithoutIndirectResults().begin();
// Skip the last parameter, which is `self`. Add it below.
for (auto param : substConv.getParameters()) {
auto paramType =
substConv.getSILType(param, builder.getTypeExpansionContext());
SILValue arg = *paramArgIter;
if (builder.hasOwnership() && arg->getType().isObject() &&
arg->getOwnershipKind() == OwnershipKind::Owned &&
param.isGuaranteed()) {
SILBuilderWithScope borrowBuilder(applySite.getInstruction(), builder);
arg = borrowBuilder.createBeginBorrow(loc, arg);
newArgBorrows.push_back(arg);
}
auto argCastRes =
castValueToABICompatibleType(&builder, loc, arg,
paramArgIter->getType(), paramType,
{applySite.getInstruction()});
newArgs.push_back(argCastRes.first);
changedCFG |= argCastRes.second;
++paramArgIter;
}
ApplySite newAS;
bool neededCFGChange;
std::tie(newAS, neededCFGChange) = replaceApplySite(
builder, loc, applySite, fri, subs, newArgs, substConv, newArgBorrows);
changedCFG |= neededCFGChange;
FullApplySite newAI = FullApplySite::isa(newAS.getInstruction());
assert(newAI);
LLVM_DEBUG(llvm::dbgs() << " SUCCESS: " << f->getName() << "\n");
if (ore)
ore->emit([&]() {
using namespace OptRemark;
return RemarkPassed("ClassMethodDevirtualized",
*applySite.getInstruction())
<< "Devirtualized call to class method " << NV("Method", f);
});
++NumClassDevirt;
return {newAI, changedCFG};
}
std::pair<FullApplySite, bool> swift::tryDevirtualizeClassMethod(
FullApplySite applySite, SILValue classInstance, ClassDecl *cd,
CanType classType, OptRemark::Emitter *ore, bool isEffectivelyFinalMethod) {
if (!canDevirtualizeClassMethod(applySite, cd, classType, ore,
isEffectivelyFinalMethod))
return {FullApplySite(), false};
return devirtualizeClassMethod(applySite, classInstance, cd, classType, ore);
}
//===----------------------------------------------------------------------===//
// Witness Method Optimization
//===----------------------------------------------------------------------===//
/// Compute substitutions for making a direct call to a SIL function with
/// @convention(witness_method) convention.
///
/// Such functions have a substituted generic signature where the
/// abstract `Self` parameter from the original type of the protocol
/// requirement is replaced by a concrete type.
///
/// Thus, the original substitutions of the apply instruction that
/// are written in terms of the requirement's generic signature need
/// to be remapped to substitutions suitable for the witness signature.
///
/// Supported remappings are:
///
/// - (Concrete witness thunk) Original substitutions:
/// [Self := ConcreteType, R0 := X0, R1 := X1, ...]
/// - Requirement generic signature:
/// <Self : P, R0, R1, ...>
/// - Witness thunk generic signature:
/// <W0, W1, ...>
/// - Remapped substitutions:
/// [W0 := X0, W1 := X1, ...]
///
/// - (Class witness thunk) Original substitutions:
/// [Self := C<A0, A1>, T0 := X0, T1 := X1, ...]
/// - Requirement generic signature:
/// <Self : P, R0, R1, ...>
/// - Witness thunk generic signature:
/// <Self : C<B0, B1>, B0, B1, W0, W1, ...>
/// - Remapped substitutions:
/// [Self := C<B0, B1>, B0 := A0, B1 := A1, W0 := X0, W1 := X1]
///
/// - (Default witness thunk) Original substitutions:
/// [Self := ConcreteType, R0 := X0, R1 := X1, ...]
/// - Requirement generic signature:
/// <Self : P, R0, R1, ...>
/// - Witness thunk generic signature:
/// <Self : P, W0, W1, ...>
/// - Remapped substitutions:
/// [Self := ConcreteType, W0 := X0, W1 := X1, ...]
///
/// \param conformanceRef The (possibly-specialized) conformance
/// \param requirementSig The generic signature of the requirement
/// \param witnessThunkSig The generic signature of the witness method
/// \param origSubMap The substitutions from the call instruction
/// \param isSelfAbstract True if the Self type of the witness method is
/// still abstract (i.e., not a concrete type).
/// \param classWitness The ClassDecl if this is a class witness method
static SubstitutionMap
getWitnessMethodSubstitutions(
ASTContext &ctx,
ProtocolConformanceRef conformanceRef,
GenericSignature requirementSig,
GenericSignature witnessThunkSig,
SubstitutionMap origSubMap,
bool isSelfAbstract,
ClassDecl *classWitness) {
if (witnessThunkSig.isNull())
return SubstitutionMap();
if (isSelfAbstract && !classWitness)
return origSubMap;
assert(!conformanceRef.isAbstract());
auto conformance = conformanceRef.getConcrete();
auto selfType = conformance->getProtocol()->getSelfInterfaceType();
// If `Self` maps to a bound generic type, this gives us the
// substitutions for the concrete type's generic parameters.
auto baseSubMap = conformance->getSubstitutionMap();
unsigned baseDepth = 0;
auto *rootConformance = conformance->getRootConformance();
if (auto conformingTypeSig = rootConformance->getGenericSignature())
baseDepth = conformingTypeSig.getGenericParams().back()->getDepth() + 1;
// witnessThunkSig begins with the optional class 'Self', followed by the
// generic parameters of the concrete conforming type, followed by the
// generic parameters of the protocol requirement, if any.
//
// - The 'Self' parameter is replaced with the conforming type.
// - The conforming type's generic parameters are replaced by the
// conformance substitutions.
// - The protocol requirement's generic parameters are replaced from the
// substitution map at the call site.
return SubstitutionMap::get(
witnessThunkSig,
[&](SubstitutableType *type) {
auto *paramType = type->castTo<GenericTypeParamType>();
unsigned depth = paramType->getDepth();
if (classWitness != nullptr) {
if (depth == 0) {
assert(paramType->getIndex() == 0);
return selfType.subst(origSubMap);
}
--depth;
}
if (depth < baseDepth) {
paramType = GenericTypeParamType::get(
paramType->isParameterPack(),
depth, paramType->getIndex(), ctx);
return Type(paramType).subst(baseSubMap);
}
depth = depth - baseDepth + 1;
paramType = GenericTypeParamType::get(
paramType->isParameterPack(),
depth, paramType->getIndex(), ctx);
return Type(paramType).subst(origSubMap);
},
[&](CanType type, Type substType, ProtocolDecl *proto) {
auto *paramType = type->getRootGenericParam();
unsigned depth = paramType->getDepth();
if (classWitness != nullptr) {
if (depth == 0) {
assert(type->isEqual(paramType));
assert(paramType->getIndex() == 0);
return conformanceRef;
}
--depth;
}
if (depth < baseDepth) {
type = CanType(type.transform([&](Type t) -> Type {
if (t->isEqual(paramType)) {
return GenericTypeParamType::get(
paramType->isParameterPack(),
depth, paramType->getIndex(), ctx);
}
assert(!t->is<GenericTypeParamType>());
return t;
}));
return baseSubMap.lookupConformance(type, proto);
}
depth = depth - baseDepth + 1;
type = CanType(type.transform([&](Type t) -> Type {
if (t->isEqual(paramType)) {
return GenericTypeParamType::get(
paramType->isParameterPack(),
depth, paramType->getIndex(), ctx);
}
assert(!t->is<GenericTypeParamType>());
return t;
}));
return origSubMap.lookupConformance(type, proto);
});
}
SubstitutionMap
swift::getWitnessMethodSubstitutions(SILModule &module, ApplySite applySite,
SILFunction *f,
ProtocolConformanceRef cRef) {
auto witnessFnTy = f->getLoweredFunctionTypeInContext(
TypeExpansionContext(*applySite.getFunction()));
assert(witnessFnTy->getRepresentation() ==
SILFunctionTypeRepresentation::WitnessMethod);
auto requirementSig = applySite.getOrigCalleeType()->getInvocationGenericSignature();
auto witnessThunkSig = witnessFnTy->getInvocationGenericSignature();
SubstitutionMap origSubs = applySite.getSubstitutionMap();
auto &ctx = module.getASTContext();
bool isSelfAbstract =
witnessFnTy
->getSelfInstanceType(
module, applySite.getFunction()->getTypeExpansionContext())
->is<GenericTypeParamType>();
auto *classWitness = witnessFnTy->getWitnessMethodClass(
module, applySite.getFunction()->getTypeExpansionContext());
return ::getWitnessMethodSubstitutions(ctx, cRef, requirementSig,
witnessThunkSig, origSubs,
isSelfAbstract, classWitness);
}
/// Generate a new apply of a function_ref to replace an apply of a
/// witness_method when we've determined the actual function we'll end
/// up calling.
///
/// Return the new apply and true if the CFG was also modified.
static std::pair<ApplySite, bool>
devirtualizeWitnessMethod(ApplySite applySite, SILFunction *f,
ProtocolConformanceRef cRef,
OptRemark::Emitter *ore) {
bool changedCFG = false;
// We know the witness thunk and the corresponding set of substitutions
// required to invoke the protocol method at this point.
auto &module = applySite.getModule();
// Collect all the required substitutions.
//
// The complete set of substitutions may be different, e.g. because the found
// witness thunk f may have been created by a specialization pass and have
// additional generic parameters.
auto subMap = getWitnessMethodSubstitutions(module, applySite, f, cRef);
// Figure out the exact bound type of the function to be called by
// applying all substitutions.
auto typeExpansionContext =
applySite.getFunction()->getTypeExpansionContext();
auto calleeCanType = f->getLoweredFunctionTypeInContext(typeExpansionContext);
auto substCalleeCanType =
calleeCanType->substGenericArgs(module, subMap, typeExpansionContext);
// Collect arguments from the apply instruction.
SmallVector<SILValue, 4> arguments;
SmallVector<SILValue, 4> borrowedArgs;
// Iterate over the non self arguments and add them to the
// new argument list, upcasting when required.
SILBuilderWithScope argBuilder(applySite.getInstruction());
SILFunctionConventions substConv(substCalleeCanType, module);
unsigned substArgIdx = applySite.getCalleeArgIndexOfFirstAppliedArg();
for (auto arg : applySite.getArguments()) {
auto paramInfo = substConv.getSILArgumentConvention(substArgIdx);
auto paramType =
substConv.getSILArgumentType(substArgIdx++, typeExpansionContext);
if (arg->getType() != paramType) {
if (argBuilder.hasOwnership() &&
applySite.getKind() != ApplySiteKind::PartialApplyInst &&
arg->getType().isObject() &&
arg->getOwnershipKind() == OwnershipKind::Owned &&
paramInfo.isGuaranteedConvention()) {
SILBuilderWithScope borrowBuilder(applySite.getInstruction(),
argBuilder);
arg = borrowBuilder.createBeginBorrow(applySite.getLoc(), arg);
borrowedArgs.push_back(arg);
}
auto argCastRes = castValueToABICompatibleType(
&argBuilder, applySite.getLoc(), arg, arg->getType(), paramType,
applySite.getInstruction());
arg = argCastRes.first;
changedCFG |= argCastRes.second;
}
arguments.push_back(arg);
}
assert(substArgIdx == substConv.getNumSILArguments());
// Replace old apply instruction by a new apply instruction that invokes
// the witness thunk.
SILBuilderWithScope applyBuilder(applySite.getInstruction());
SILLocation loc = applySite.getLoc();
auto *fri = applyBuilder.createFunctionRefFor(loc, f);
ApplySite newApplySite;
bool neededCFGChange = false;
std::tie(newApplySite, neededCFGChange) =
replaceApplySite(applyBuilder, loc, applySite, fri, subMap, arguments,
substConv, borrowedArgs);
changedCFG |= neededCFGChange;
if (ore)
ore->emit([&]() {
using namespace OptRemark;
return RemarkPassed("WitnessMethodDevirtualized",
*applySite.getInstruction())
<< "Devirtualized call to " << NV("Method", f);
});
++NumWitnessDevirt;
return {newApplySite, changedCFG};
}
static bool isNonGenericThunkOfGenericExternalFunction(SILFunction *thunk) {
if (!thunk->isThunk())
return false;
if (thunk->getGenericSignature())
return false;
for (SILBasicBlock &block : *thunk) {
for (SILInstruction &inst : block) {
if (FullApplySite fas = FullApplySite::isa(&inst)) {
if (SILFunction *calledFunc = fas.getReferencedFunctionOrNull()) {
if (fas.hasSubstitutions() && !calledFunc->isDefinition())
return true;
}
}
}
}
return false;
}
static bool canDevirtualizeWitnessMethod(ApplySite applySite, bool isMandatory) {
SILFunction *f;
SILWitnessTable *wt;
auto *wmi = cast<WitnessMethodInst>(applySite.getCallee());
// Handle vanishing tuples: don't devirtualize a call to a tuple conformance
// if the lookup type can possibly be unwrapped after substitution.
if (auto tupleType = dyn_cast<TupleType>(wmi->getLookupType())) {
if (tupleType->containsPackExpansionType() &&
tupleType->getNumScalarElements() <= 1) {
return false;
}
}
std::tie(f, wt) = applySite.getModule().lookUpFunctionInWitnessTable(
wmi->getConformance(), wmi->getMember(), SILModule::LinkingMode::LinkAll);
if (!f)
return false;
// function_ref inside fragile function cannot reference a private or
// hidden symbol.
if (applySite.getFunction()->isAnySerialized() &&
!f->hasValidLinkageForFragileRef(applySite.getFunction()->getSerializedKind()))
return false;
// devirtualizeWitnessMethod below does not support this case. It currently
// assumes it can try_apply call the target.
if (!f->getLoweredFunctionType()->hasErrorResult()
&& isa<TryApplyInst>(applySite.getInstruction())) {
LLVM_DEBUG(llvm::dbgs() << " FAIL: Trying to devirtualize a "
"try_apply but wtable entry has no error result.\n");
return false;
}
// The following check is for performance reasons: if `f` is a non-generic thunk
// which calls a (not inlinable) generic function in the defining module, it's
// more efficient to not devirtualize, but call the non-generic thunk - even though
// it's done through the witness table.
// Example:
// ```
// protocol P {
// func f(x: [Int]) // not generic
// }
// struct S: P {
// func f(x: some RandomAccessCollection<Int>) { ... } // generic
// }
// ```
// In the defining module, the generic conformance can be specialized (which is not
// possible in the client module, because it's not inlinable).
if (!isMandatory && isNonGenericThunkOfGenericExternalFunction(f)) {
return false;
}
// FIXME: devirtualizeWitnessMethod does not support cases with covariant
// 'Self'-rooted type parameters nested inside a collection type, like
// '[Self]' or '[* : Self.A]', because it doesn't know how to deal with
// associated collection upcasts.
const Type interfaceTy = wmi->getMember()
.getDecl()
->getInterfaceType()
// Skip the 'self' parameter.
->castTo<AnyFunctionType>()
->getResult();
if (!interfaceTy->hasTypeParameter())
return true;
auto subs = getWitnessMethodSubstitutions(f->getModule(), applySite,
f, wmi->getConformance());
CanSILFunctionType substCalleTy = f->getLoweredFunctionType()->substGenericArgs(
f->getModule(), subs,
applySite.getFunction()->getTypeExpansionContext());
CanSILFunctionType applySubstCalleeTy = applySite.getSubstCalleeType();
// If the function types match, there is no problem.
if (substCalleTy == applySubstCalleeTy)
return true;
auto selfGP = wmi->getLookupProtocol()->getSelfInterfaceType();
auto isSelfRootedTypeParameter = [selfGP](Type T) -> bool {
if (!T->hasTypeParameter())
return false;
if (T->isTypeParameter()) {
return T->getRootGenericParam()->isEqual(selfGP);
}
return false;
};
return !interfaceTy.findIf([&](Type T) -> bool {
if (!T->hasTypeParameter())
return false;
if (T->isArray() || T->isDictionary()) {
return T.findIf(isSelfRootedTypeParameter);
}
if (auto *FT = T->getAs<FunctionType>()) {
for (const auto &Param : FT->getParams()) {
if (Param.isVariadic() && T.findIf(isSelfRootedTypeParameter))
return true;
}
}
return false;
});
}
/// In the cases where we can statically determine the function that
/// we'll call to, replace an apply of a witness_method with an apply
/// of a function_ref, returning the new apply.
std::pair<ApplySite, bool>
swift::tryDevirtualizeWitnessMethod(ApplySite applySite,
OptRemark::Emitter *ore,
bool isMandatory) {
if (!canDevirtualizeWitnessMethod(applySite, isMandatory))
return {ApplySite(), false};
SILFunction *f;
SILWitnessTable *wt;
auto *wmi = cast<WitnessMethodInst>(applySite.getCallee());
std::tie(f, wt) = applySite.getModule().lookUpFunctionInWitnessTable(
wmi->getConformance(), wmi->getMember(), SILModule::LinkingMode::LinkAll);
return devirtualizeWitnessMethod(applySite, f, wmi->getConformance(), ore);
}
//===----------------------------------------------------------------------===//
// Top Level Driver
//===----------------------------------------------------------------------===//
/// Attempt to devirtualize the given apply if possible, and return a
/// new instruction in that case, or nullptr otherwise.
///
/// Return the new apply and true if the CFG was also modified.
std::pair<ApplySite, bool>
swift::tryDevirtualizeApply(ApplySite applySite, ClassHierarchyAnalysis *cha,
OptRemark::Emitter *ore, bool isMandatory) {
LLVM_DEBUG(llvm::dbgs() << " Trying to devirtualize: "
<< *applySite.getInstruction());
// Devirtualize apply instructions that call witness_method instructions:
//
// %8 = witness_method $Optional<UInt16>, #LogicValue.boolValue!getter
// %9 = apply %8<Self = CodeUnit?>(%6#1) : ...
//
if (isa<WitnessMethodInst>(applySite.getCallee()))
return tryDevirtualizeWitnessMethod(applySite, ore, isMandatory);
// TODO: check if we can also de-virtualize partial applies of class methods.
FullApplySite fas = FullApplySite::isa(applySite.getInstruction());
if (!fas)
return {ApplySite(), false};
/// Optimize a class_method and alloc_ref pair into a direct function
/// reference:
///
/// \code
/// %XX = alloc_ref $Foo
/// %YY = class_method %XX : $Foo, #Foo.get : $@convention(method)...
/// \endcode
///
/// or
///
/// %XX = metatype $...
/// %YY = class_method %XX : ...
///
/// into
///
/// %YY = function_ref @...
if (auto *cmi = dyn_cast<ClassMethodInst>(fas.getCallee())) {
auto instance = stripUpCasts(cmi->getOperand());
auto classType = getSelfInstanceType(instance->getType().getASTType());
auto *cd = classType.getClassOrBoundGenericClass();
if (isEffectivelyFinalMethod(fas, classType, cd, cha))
return tryDevirtualizeClassMethod(fas, instance, cd, classType, ore,
true /*isEffectivelyFinalMethod*/);
// Try to check if the exact dynamic type of the instance is statically
// known.
if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha)) {
// Update the classDecl, because we are stripping casts more aggressively
// in getInstanceWithExactDynamicType than in stripUpCasts.
CanType classType = getSelfInstanceType(instance->getType().getASTType());
// This should never be null - make the check just to be on the safe side.
if (ClassDecl *cd = classType.getClassOrBoundGenericClass())
return tryDevirtualizeClassMethod(fas, instance, cd, classType, ore);
return {ApplySite(), false};
}
if (auto exactTy = getExactDynamicType(cmi->getOperand(), cha)) {
if (exactTy == cmi->getOperand()->getType())
return tryDevirtualizeClassMethod(fas, cmi->getOperand(), cd, classType,
ore);
}
}
if (isa<SuperMethodInst>(fas.getCallee())) {
auto instance = fas.getArguments().back();
auto classType = getSelfInstanceType(instance->getType().getASTType());
auto *cd = classType.getClassOrBoundGenericClass();
return tryDevirtualizeClassMethod(fas, instance, cd, classType, ore);
}
return {ApplySite(), false};
}
bool swift::canDevirtualizeApply(FullApplySite applySite,
ClassHierarchyAnalysis *cha) {
LLVM_DEBUG(llvm::dbgs() << " Trying to devirtualize: "
<< *applySite.getInstruction());
// Devirtualize apply instructions that call witness_method instructions:
//
// %8 = witness_method $Optional<UInt16>, #LogicValue.boolValue!getter
// %9 = apply %8<Self = CodeUnit?>(%6#1) : ...
//
if (isa<WitnessMethodInst>(applySite.getCallee()))
return canDevirtualizeWitnessMethod(applySite, /*isMandatory*/ false);
/// Optimize a class_method and alloc_ref pair into a direct function
/// reference:
///
/// \code
/// %XX = alloc_ref $Foo
/// %YY = class_method %XX : $Foo, #Foo.get : $@convention(method)...
/// \endcode
///
/// or
///
/// %XX = metatype $...
/// %YY = class_method %XX : ...
///
/// into
///
/// %YY = function_ref @...
if (auto *cmi = dyn_cast<ClassMethodInst>(applySite.getCallee())) {
auto instance = stripUpCasts(cmi->getOperand());
auto classType = getSelfInstanceType(instance->getType().getASTType());
auto *cd = classType.getClassOrBoundGenericClass();
if (isEffectivelyFinalMethod(applySite, classType, cd, cha))
return canDevirtualizeClassMethod(applySite, cd, classType,
nullptr /*ore*/,
true /*isEffectivelyFinalMethod*/);
// Try to check if the exact dynamic type of the instance is statically
// known.
if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha)) {
CanType classType = getSelfInstanceType(instance->getType().getASTType());
ClassDecl *cd = classType.getClassOrBoundGenericClass();
return cd && canDevirtualizeClassMethod(applySite, cd, classType);
}
if (auto exactTy = getExactDynamicType(cmi->getOperand(), cha)) {
if (exactTy == cmi->getOperand()->getType())
return canDevirtualizeClassMethod(applySite, cd, classType);
}
}
if (isa<SuperMethodInst>(applySite.getCallee())) {
auto instance = applySite.getArguments().back();
auto classType = getSelfInstanceType(instance->getType().getASTType());
auto *cd = classType.getClassOrBoundGenericClass();
return canDevirtualizeClassMethod(applySite, cd, classType);
}
return false;
}
|