@@ -1496,6 +1496,38 @@ static bool isApplyOfBuiltin(SILInstruction &I, BuiltinValueKind kind) {
1496
1496
return false ;
1497
1497
}
1498
1498
1499
+ static bool isApplyOfKnownAvailability (SILInstruction &I) {
1500
+ auto apply = FullApplySite::isa (&I);
1501
+ if (!apply)
1502
+ return false ;
1503
+ auto callee = apply.getReferencedFunctionOrNull ();
1504
+ if (!callee)
1505
+ return false ;
1506
+ if (!callee->hasSemanticsAttr (" availability.osversion" ))
1507
+ return false ;
1508
+ auto &context = I.getFunction ()->getASTContext ();
1509
+ auto deploymentAvailability =
1510
+ AvailabilityContext::forDeploymentTarget (context);
1511
+ if (apply.getNumArguments () != 3 )
1512
+ return false ;
1513
+ auto arg0 = dyn_cast<IntegerLiteralInst>(apply.getArgument (0 ));
1514
+ if (!arg0)
1515
+ return false ;
1516
+ auto arg1 = dyn_cast<IntegerLiteralInst>(apply.getArgument (1 ));
1517
+ if (!arg1)
1518
+ return false ;
1519
+ auto arg2 = dyn_cast<IntegerLiteralInst>(apply.getArgument (2 ));
1520
+ if (!arg2)
1521
+ return false ;
1522
+
1523
+ auto version = VersionRange::allGTE (llvm::VersionTuple (
1524
+ arg0->getValue ().getLimitedValue (), arg1->getValue ().getLimitedValue (),
1525
+ arg2->getValue ().getLimitedValue ()));
1526
+
1527
+ auto callAvailability = AvailabilityContext (version);
1528
+ return deploymentAvailability.isContainedIn (callAvailability);
1529
+ }
1530
+
1499
1531
static bool isApplyOfStringConcat (SILInstruction &I) {
1500
1532
if (auto *AI = dyn_cast<ApplyInst>(&I))
1501
1533
if (auto *Fn = AI->getReferencedFunctionOrNull ())
@@ -1590,6 +1622,11 @@ void ConstantFolder::initializeWorklist(SILFunction &f) {
1590
1622
continue ;
1591
1623
}
1592
1624
1625
+ if (isApplyOfKnownAvailability (*inst)) {
1626
+ WorkList.insert (inst);
1627
+ continue ;
1628
+ }
1629
+
1593
1630
if (isa<CheckedCastBranchInst>(inst) ||
1594
1631
isa<CheckedCastAddrBranchInst>(inst) ||
1595
1632
isa<UnconditionalCheckedCastInst>(inst) ||
@@ -1731,6 +1768,19 @@ ConstantFolder::processWorkList() {
1731
1768
continue ;
1732
1769
}
1733
1770
}
1771
+ // Replace a known availability.version semantic call.
1772
+ if (isApplyOfKnownAvailability (*I)) {
1773
+ if (auto apply = dyn_cast<ApplyInst>(I)) {
1774
+ SILBuilderWithScope B (I);
1775
+ auto tru = B.createIntegerLiteral (apply->getLoc (), apply->getType (), 1 );
1776
+ apply->replaceAllUsesWith (tru);
1777
+ eliminateDeadInstruction (
1778
+ I, [&](SILInstruction *DeadI) { WorkList.remove (DeadI); });
1779
+ WorkList.insert (tru);
1780
+ InvalidateInstructions = true ;
1781
+ }
1782
+ continue ;
1783
+ }
1734
1784
1735
1785
// If we have a cast instruction, try to optimize it.
1736
1786
if (isa<CheckedCastBranchInst>(I) || isa<CheckedCastAddrBranchInst>(I) ||
0 commit comments