Skip to content

[func-sig-opts] hasNonTrivialNonDebugUse => hasNonTrivialNonDebugTran… #15777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/swift/SIL/DebugUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ inline void eraseFromParentWithDebugInsts(SILInstruction *I) {
eraseFromParentWithDebugInsts(I, nullIter);
}

/// Return true if the def-use graph rooted at \p V contains any non-debug,
/// non-trivial users.
bool hasNonTrivialNonDebugTransitiveUsers(
PointerUnion<SILInstruction *, SILArgument *> V);

} // end namespace swift

#endif /* SWIFT_SIL_DEBUGUTILS_H */
3 changes: 0 additions & 3 deletions include/swift/SILOptimizer/Utils/FunctionSignatureOptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ bool canSpecializeFunction(SILFunction *F,
const CallerAnalysis::FunctionInfo *FuncInfo,
bool OptForPartialApply);

/// Return true if this argument is used in a non-trivial way.
bool hasNonTrivialNonDebugUse(SILArgument *Arg);

} // end namespace swift

#endif
1 change: 1 addition & 0 deletions lib/SIL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_swift_library(swiftSIL STATIC
AbstractionPattern.cpp
BasicBlockUtils.cpp
Bridging.cpp
DebugUtils.cpp
Dominance.cpp
DynamicCasts.cpp
InstructionUtils.cpp
Expand Down
66 changes: 66 additions & 0 deletions lib/SIL/DebugUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===--- DebugUtils.cpp ---------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "swift/SIL/DebugUtils.h"
#include "swift/Basic/STLExtras.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILInstruction.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/SmallPtrSet.h"

using namespace swift;

bool swift::hasNonTrivialNonDebugTransitiveUsers(
PointerUnion<SILInstruction *, SILArgument *> V) {
llvm::SmallVector<SILInstruction *, 8> Worklist;
llvm::SmallPtrSet<SILInstruction *, 8> SeenInsts;

// Initialize our worklist.
if (auto *A = V.dyn_cast<SILArgument *>()) {
for (Operand *Op : getNonDebugUses(SILValue(A))) {
auto *User = Op->getUser();
if (!SeenInsts.insert(User).second)
continue;
Worklist.push_back(User);
}
} else {
auto *I = V.get<SILInstruction *>();
SeenInsts.insert(I);
Worklist.push_back(I);
}

while (!Worklist.empty()) {
SILInstruction *U = Worklist.pop_back_val();
assert(SeenInsts.count(U) &&
"Worklist should only contain seen instructions?!");

// If U is a terminator inst, return false.
if (isa<TermInst>(U))
return true;

// If U has side effects...
if (U->mayHaveSideEffects())
return true;

// Otherwise add all non-debug uses of I that we have not seen yet to the
// worklist.
for (SILValue Result : U->getResults()) {
for (Operand *I : getNonDebugUses(Result)) {
auto *User = I->getUser();
if (!SeenInsts.insert(User).second)
continue;
Worklist.push_back(User);
}
}
}
return false;
}
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Transforms/FunctionSignatureOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ bool FunctionSignatureTransform::DeadArgumentAnalyzeParameters() {
}

// Check whether argument is dead.
if (!hasNonTrivialNonDebugUse(Args[i])) {
if (!hasNonTrivialNonDebugTransitiveUsers(Args[i])) {
A.IsEntirelyDead = true;
SignatureOptimize = true;
if (Args[i]->isSelf())
Expand Down
29 changes: 0 additions & 29 deletions lib/SILOptimizer/Utils/FunctionSignatureOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,6 @@ llvm::cl::opt<bool> FSOEnableGenerics(
llvm::cl::desc("Support function signature optimization "
"of generic functions"));

bool swift::hasNonTrivialNonDebugUse(SILArgument *Arg) {
llvm::SmallVector<SILInstruction *, 8> Worklist;
llvm::SmallPtrSet<SILInstruction *, 8> SeenInsts;

for (Operand *I : getNonDebugUses(SILValue(Arg)))
Worklist.push_back(I->getUser());

while (!Worklist.empty()) {
SILInstruction *U = Worklist.pop_back_val();
if (!SeenInsts.insert(U).second)
continue;

// If U is a terminator inst, return false.
if (isa<TermInst>(U))
return true;

// If U has side effects...
if (U->mayHaveSideEffects())
return true;

// Otherwise add all non-debug uses of I to the worklist.
for (auto result : U->getResults()) {
for (Operand *I : getNonDebugUses(result))
Worklist.push_back(I->getUser());
}
}
return false;
}

static bool isSpecializableRepresentation(SILFunctionTypeRepresentation Rep,
bool OptForPartialApply) {
switch (Rep) {
Expand Down