mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
This reduces the diff in between -Onone output when stripping before/after serialization. We support load_borrow by translating it to the load [copy] case. Specifically, for +1, we normally perform the following transform. store %1 to [init] %0 ... %2 = load [copy] %0 ... use(%2) ... destroy_value %2 => %1a = copy_value %1 store %1 to [init] %0 ... use(%1a) ... destroy_value %1a We analogously can optimize load_borrow by replacing the load with a begin_borrow: store %1 to [init] %0 ... %2 = load_borrow %0 ... use(%2) ... end_borrow %2 => %1a = copy_value %1 store %1 to [init] %0 ... %2 = begin_borrow %1a ... use(%2) ... end_borrow %2 destroy_value %1a The store from outside a loop being used by a load_borrow inside a loop is a similar transformation as the +0 version except that we use a begin_borrow inside the loop instead of a copy_value (making it even more efficient).
173 lines
5.2 KiB
C++
173 lines
5.2 KiB
C++
//===--- OwnershipUtils.h ------------------------------------*- C++ -*----===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2018 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_SIL_OWNERSHIPUTILS_H
|
|
#define SWIFT_SIL_OWNERSHIPUTILS_H
|
|
|
|
#include "swift/Basic/LLVM.h"
|
|
#include "swift/SIL/SILValue.h"
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
namespace swift {
|
|
|
|
class SILBasicBlock;
|
|
class SILInstruction;
|
|
class SILModule;
|
|
class SILValue;
|
|
class DeadEndBlocks;
|
|
class BranchPropagatedUser;
|
|
|
|
namespace ownership {
|
|
|
|
struct ErrorBehaviorKind {
|
|
enum inner_t {
|
|
Invalid = 0,
|
|
ReturnFalse = 1,
|
|
PrintMessage = 2,
|
|
Assert = 4,
|
|
ReturnFalseOnLeak = 8,
|
|
PrintMessageAndReturnFalse = PrintMessage | ReturnFalse,
|
|
PrintMessageAndAssert = PrintMessage | Assert,
|
|
ReturnFalseOnLeakAssertOtherwise = ReturnFalseOnLeak | Assert,
|
|
} Value;
|
|
|
|
ErrorBehaviorKind() : Value(Invalid) {}
|
|
ErrorBehaviorKind(inner_t Inner) : Value(Inner) { assert(Value != Invalid); }
|
|
|
|
bool shouldAssert() const {
|
|
assert(Value != Invalid);
|
|
return Value & Assert;
|
|
}
|
|
|
|
bool shouldReturnFalseOnLeak() const {
|
|
assert(Value != Invalid);
|
|
return Value & ReturnFalseOnLeak;
|
|
}
|
|
|
|
bool shouldPrintMessage() const {
|
|
assert(Value != Invalid);
|
|
return Value & PrintMessage;
|
|
}
|
|
|
|
bool shouldReturnFalse() const {
|
|
assert(Value != Invalid);
|
|
return Value & ReturnFalse;
|
|
}
|
|
};
|
|
|
|
} // end namespace ownership
|
|
|
|
class LinearLifetimeError {
|
|
ownership::ErrorBehaviorKind errorBehavior;
|
|
bool foundUseAfterFree = false;
|
|
bool foundLeak = false;
|
|
bool foundOverConsume = false;
|
|
|
|
public:
|
|
LinearLifetimeError(ownership::ErrorBehaviorKind errorBehavior)
|
|
: errorBehavior(errorBehavior) {}
|
|
|
|
bool getFoundError() const {
|
|
return foundUseAfterFree || foundLeak || foundOverConsume;
|
|
}
|
|
|
|
bool getFoundLeak() const { return foundLeak; }
|
|
|
|
bool getFoundUseAfterFree() const { return foundUseAfterFree; }
|
|
|
|
bool getFoundOverConsume() const { return foundOverConsume; }
|
|
|
|
void handleLeak(llvm::function_ref<void()> &&messagePrinterFunc) {
|
|
foundLeak = true;
|
|
|
|
if (errorBehavior.shouldPrintMessage())
|
|
messagePrinterFunc();
|
|
|
|
if (errorBehavior.shouldReturnFalseOnLeak())
|
|
return;
|
|
|
|
// We already printed out our error if we needed to, so don't pass it along.
|
|
handleError([]() {});
|
|
}
|
|
|
|
void handleOverConsume(llvm::function_ref<void()> &&messagePrinterFunc) {
|
|
foundOverConsume = true;
|
|
handleError(std::move(messagePrinterFunc));
|
|
}
|
|
|
|
void handleUseAfterFree(llvm::function_ref<void()> &&messagePrinterFunc) {
|
|
foundUseAfterFree = true;
|
|
handleError(std::move(messagePrinterFunc));
|
|
}
|
|
|
|
private:
|
|
void handleError(llvm::function_ref<void()> &&messagePrinterFunc) {
|
|
if (errorBehavior.shouldPrintMessage())
|
|
messagePrinterFunc();
|
|
|
|
if (errorBehavior.shouldReturnFalse()) {
|
|
return;
|
|
}
|
|
|
|
assert(errorBehavior.shouldAssert() && "At this point, we should assert");
|
|
llvm_unreachable("triggering standard assertion failure routine");
|
|
}
|
|
};
|
|
|
|
/// Returns true if:
|
|
///
|
|
/// 1. No consuming uses are reachable from any other consuming use, from any
|
|
/// non-consuming uses, or from the producer instruction.
|
|
/// 2. The consuming use set jointly post dominates producers and all non
|
|
/// consuming uses.
|
|
///
|
|
/// \p value The value whose lifetime we are checking.
|
|
/// \p consumingUses the array of users that destroy or consume a value.
|
|
/// \p nonConsumingUses regular uses
|
|
/// \p deadEndBlocks a cache for the dead end block computation
|
|
/// \p errorBehavior If we detect an error, should we return false or hard
|
|
/// error.
|
|
/// \p leakingBlocks If non-null a list of blocks where the value was detected
|
|
/// to leak. Can be used to insert missing destroys.
|
|
LinearLifetimeError valueHasLinearLifetime(
|
|
SILValue value, ArrayRef<BranchPropagatedUser> consumingUses,
|
|
ArrayRef<BranchPropagatedUser> nonConsumingUses,
|
|
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
|
|
DeadEndBlocks &deadEndBlocks, ownership::ErrorBehaviorKind errorBehavior,
|
|
SmallVectorImpl<SILBasicBlock *> *leakingBlocks = nullptr);
|
|
|
|
/// Returns true if v is an address or trivial.
|
|
bool isValueAddressOrTrivial(SILValue v, SILModule &m);
|
|
|
|
/// These operations forward both owned and guaranteed ownership.
|
|
bool isOwnershipForwardingValueKind(SILNodeKind kind);
|
|
|
|
/// These operations forward guaranteed ownership, but don't necessarily forward
|
|
/// owned values.
|
|
bool isGuaranteedForwardingValueKind(SILNodeKind kind);
|
|
|
|
bool isGuaranteedForwardingValue(SILValue value);
|
|
|
|
bool isOwnershipForwardingInst(SILInstruction *i);
|
|
|
|
bool isGuaranteedForwardingInst(SILInstruction *i);
|
|
|
|
/// Look up through the def-use chain of \p inputValue, recording any "borrow"
|
|
/// introducers that we find into \p out.
|
|
bool getUnderlyingBorrowIntroducers(SILValue inputValue,
|
|
SmallVectorImpl<SILValue> &out);
|
|
|
|
} // namespace swift
|
|
|
|
#endif
|