mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
UnsafeGuaranteedPeephole: Use RCIdentityFunctionInfo when matching release
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
#include "swift/SIL/SILFunction.h"
|
||||
#include "swift/SIL/SILInstruction.h"
|
||||
#include "swift/SIL/SILModule.h"
|
||||
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
|
||||
#include "swift/SILOptimizer/PassManager/Transforms.h"
|
||||
#include "swift/SILOptimizer/Utils/Local.h"
|
||||
|
||||
@@ -92,18 +93,21 @@ getSingleUnsafeGuaranteedValueResult(BuiltinInst *BI) {
|
||||
/// strong_release %6 : $Foo // Ignore.
|
||||
/// %12 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
|
||||
///
|
||||
static SILBasicBlock::iterator
|
||||
findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
|
||||
SILValue UnsafeGuaranteedValue,
|
||||
SILBasicBlock &BB) {
|
||||
static SILBasicBlock::iterator findReleaseToMatchUnsafeGuaranteedValue(
|
||||
SILInstruction *UnsafeGuaranteedEndI, SILValue UnsafeGuaranteedValue,
|
||||
SILBasicBlock &BB, RCIdentityFunctionInfo &RCIA) {
|
||||
|
||||
auto UnsafeGuaranteedEndIIt = SILBasicBlock::iterator(UnsafeGuaranteedEndI);
|
||||
if (UnsafeGuaranteedEndIIt == BB.begin())
|
||||
return BB.end();
|
||||
auto LastReleaseIt = std::prev(UnsafeGuaranteedEndIIt);
|
||||
auto UnsafeGuaranteedRCIdentityRoot =
|
||||
RCIA.getRCIdentityRoot(UnsafeGuaranteedValue);
|
||||
while (LastReleaseIt != BB.begin() &&
|
||||
(((isa<StrongReleaseInst>(*LastReleaseIt) ||
|
||||
isa<ReleaseValueInst>(*LastReleaseIt)) &&
|
||||
LastReleaseIt->getOperand(0) != UnsafeGuaranteedValue &&
|
||||
RCIA.getRCIdentityRoot(LastReleaseIt->getOperand(0)) !=
|
||||
UnsafeGuaranteedRCIdentityRoot &&
|
||||
LastReleaseIt->getOperand(0) !=
|
||||
cast<SILInstruction>(UnsafeGuaranteedValue)->getOperand(0)) ||
|
||||
!LastReleaseIt->mayHaveSideEffects() ||
|
||||
@@ -112,7 +116,8 @@ findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
|
||||
--LastReleaseIt;
|
||||
if ((!isa<StrongReleaseInst>(*LastReleaseIt) &&
|
||||
!isa<ReleaseValueInst>(*LastReleaseIt)) ||
|
||||
(LastReleaseIt->getOperand(0) != UnsafeGuaranteedValue &&
|
||||
(RCIA.getRCIdentityRoot(LastReleaseIt->getOperand(0)) !=
|
||||
UnsafeGuaranteedRCIdentityRoot &&
|
||||
LastReleaseIt->getOperand(0) !=
|
||||
cast<SILInstruction>(UnsafeGuaranteedValue)->getOperand(0))) {
|
||||
return BB.end();
|
||||
@@ -122,7 +127,8 @@ findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
|
||||
|
||||
/// Remove retain/release pairs around builtin "unsafeGuaranteed" instruction
|
||||
/// sequences.
|
||||
static bool removeGuaranteedRetainReleasePairs(SILFunction &F) {
|
||||
static bool removeGuaranteedRetainReleasePairs(SILFunction &F,
|
||||
RCIdentityFunctionInfo &RCIA) {
|
||||
bool Changed = false;
|
||||
for (auto &BB : F) {
|
||||
auto It = BB.begin(), End = BB.end();
|
||||
@@ -216,7 +222,8 @@ static bool removeGuaranteedRetainReleasePairs(SILFunction &F) {
|
||||
// Find the release to match with the unsafeGuaranteedValue.
|
||||
auto &UnsafeGuaranteedEndBB = *UnsafeGuaranteedEndI->getParent();
|
||||
auto LastReleaseIt = findReleaseToMatchUnsafeGuaranteedValue(
|
||||
UnsafeGuaranteedEndI, UnsafeGuaranteedValue, UnsafeGuaranteedEndBB);
|
||||
UnsafeGuaranteedEndI, UnsafeGuaranteedValue, UnsafeGuaranteedEndBB,
|
||||
RCIA);
|
||||
if (LastReleaseIt == UnsafeGuaranteedEndBB.end()) {
|
||||
DEBUG(llvm::dbgs() << " no release before unsafeGuaranteedEnd found\n");
|
||||
continue;
|
||||
@@ -258,7 +265,8 @@ namespace {
|
||||
class UnsafeGuaranteedPeephole : public swift::SILFunctionTransform {
|
||||
|
||||
void run() override {
|
||||
if (removeGuaranteedRetainReleasePairs(*getFunction()))
|
||||
auto &RCIA = *getAnalysis<RCIdentityAnalysis>()->get(getFunction());
|
||||
if (removeGuaranteedRetainReleasePairs(*getFunction(), RCIA))
|
||||
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user