Skip to content

Commit 3717512

Browse files
authored
Merge pull request #15777 from gottesmm/pr-bb748720540ac4e06fad7d37623faa9c10bb2599
[func-sig-opts] hasNonTrivialNonDebugUse => hasNonTrivialNonDebugTran…
2 parents 7ee32d4 + 88bc490 commit 3717512

File tree

6 files changed

+73
-33
lines changed

6 files changed

+73
-33
lines changed

include/swift/SIL/DebugUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ inline void eraseFromParentWithDebugInsts(SILInstruction *I) {
210210
eraseFromParentWithDebugInsts(I, nullIter);
211211
}
212212

213+
/// Return true if the def-use graph rooted at \p V contains any non-debug,
214+
/// non-trivial users.
215+
bool hasNonTrivialNonDebugTransitiveUsers(
216+
PointerUnion<SILInstruction *, SILArgument *> V);
217+
213218
} // end namespace swift
214219

215220
#endif /* SWIFT_SIL_DEBUGUTILS_H */

include/swift/SILOptimizer/Utils/FunctionSignatureOptUtils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ bool canSpecializeFunction(SILFunction *F,
200200
const CallerAnalysis::FunctionInfo *FuncInfo,
201201
bool OptForPartialApply);
202202

203-
/// Return true if this argument is used in a non-trivial way.
204-
bool hasNonTrivialNonDebugUse(SILArgument *Arg);
205-
206203
} // end namespace swift
207204

208205
#endif

lib/SIL/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_swift_library(swiftSIL STATIC
22
AbstractionPattern.cpp
33
BasicBlockUtils.cpp
44
Bridging.cpp
5+
DebugUtils.cpp
56
Dominance.cpp
67
DynamicCasts.cpp
78
InstructionUtils.cpp

lib/SIL/DebugUtils.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===--- DebugUtils.cpp ---------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/SIL/DebugUtils.h"
14+
#include "swift/Basic/STLExtras.h"
15+
#include "swift/SIL/SILArgument.h"
16+
#include "swift/SIL/SILInstruction.h"
17+
#include "llvm/ADT/PointerIntPair.h"
18+
#include "llvm/ADT/SmallPtrSet.h"
19+
20+
using namespace swift;
21+
22+
bool swift::hasNonTrivialNonDebugTransitiveUsers(
23+
PointerUnion<SILInstruction *, SILArgument *> V) {
24+
llvm::SmallVector<SILInstruction *, 8> Worklist;
25+
llvm::SmallPtrSet<SILInstruction *, 8> SeenInsts;
26+
27+
// Initialize our worklist.
28+
if (auto *A = V.dyn_cast<SILArgument *>()) {
29+
for (Operand *Op : getNonDebugUses(SILValue(A))) {
30+
auto *User = Op->getUser();
31+
if (!SeenInsts.insert(User).second)
32+
continue;
33+
Worklist.push_back(User);
34+
}
35+
} else {
36+
auto *I = V.get<SILInstruction *>();
37+
SeenInsts.insert(I);
38+
Worklist.push_back(I);
39+
}
40+
41+
while (!Worklist.empty()) {
42+
SILInstruction *U = Worklist.pop_back_val();
43+
assert(SeenInsts.count(U) &&
44+
"Worklist should only contain seen instructions?!");
45+
46+
// If U is a terminator inst, return false.
47+
if (isa<TermInst>(U))
48+
return true;
49+
50+
// If U has side effects...
51+
if (U->mayHaveSideEffects())
52+
return true;
53+
54+
// Otherwise add all non-debug uses of I that we have not seen yet to the
55+
// worklist.
56+
for (SILValue Result : U->getResults()) {
57+
for (Operand *I : getNonDebugUses(Result)) {
58+
auto *User = I->getUser();
59+
if (!SeenInsts.insert(User).second)
60+
continue;
61+
Worklist.push_back(User);
62+
}
63+
}
64+
}
65+
return false;
66+
}

lib/SILOptimizer/Transforms/FunctionSignatureOpts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ bool FunctionSignatureTransform::DeadArgumentAnalyzeParameters() {
837837
}
838838

839839
// Check whether argument is dead.
840-
if (!hasNonTrivialNonDebugUse(Args[i])) {
840+
if (!hasNonTrivialNonDebugTransitiveUsers(Args[i])) {
841841
A.IsEntirelyDead = true;
842842
SignatureOptimize = true;
843843
if (Args[i]->isSelf())

lib/SILOptimizer/Utils/FunctionSignatureOptUtils.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,6 @@ llvm::cl::opt<bool> FSOEnableGenerics(
2828
llvm::cl::desc("Support function signature optimization "
2929
"of generic functions"));
3030

31-
bool swift::hasNonTrivialNonDebugUse(SILArgument *Arg) {
32-
llvm::SmallVector<SILInstruction *, 8> Worklist;
33-
llvm::SmallPtrSet<SILInstruction *, 8> SeenInsts;
34-
35-
for (Operand *I : getNonDebugUses(SILValue(Arg)))
36-
Worklist.push_back(I->getUser());
37-
38-
while (!Worklist.empty()) {
39-
SILInstruction *U = Worklist.pop_back_val();
40-
if (!SeenInsts.insert(U).second)
41-
continue;
42-
43-
// If U is a terminator inst, return false.
44-
if (isa<TermInst>(U))
45-
return true;
46-
47-
// If U has side effects...
48-
if (U->mayHaveSideEffects())
49-
return true;
50-
51-
// Otherwise add all non-debug uses of I to the worklist.
52-
for (auto result : U->getResults()) {
53-
for (Operand *I : getNonDebugUses(result))
54-
Worklist.push_back(I->getUser());
55-
}
56-
}
57-
return false;
58-
}
59-
6031
static bool isSpecializableRepresentation(SILFunctionTypeRepresentation Rep,
6132
bool OptForPartialApply) {
6233
switch (Rep) {

0 commit comments

Comments
 (0)