Skip to content

Commit baa94f2

Browse files
committed
BasicCalleeAnalysis: add dump() functions for debugging
1 parent 8c329fe commit baa94f2

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

include/swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h

+18-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class CalleeList {
5454
: CalleeFunctions(llvm::makeArrayRef(List.begin(), List.end())),
5555
IsIncomplete(IsIncomplete) {}
5656

57+
LLVM_ATTRIBUTE_DEPRECATED(void dump() const LLVM_ATTRIBUTE_USED,
58+
"Only for use in the debugger");
59+
60+
void print(llvm::raw_ostream &os) const;
61+
5762
/// Return an iterator for the beginning of the list.
5863
ArrayRef<SILFunction *>::iterator begin() const {
5964
return CalleeFunctions.begin();
@@ -67,7 +72,7 @@ class CalleeList {
6772
bool isIncomplete() const { return IsIncomplete; }
6873

6974
/// Returns true if all callees are known and not external.
70-
bool allCalleesVisible();
75+
bool allCalleesVisible() const;
7176
};
7277

7378
/// CalleeCache is a helper class that builds lists of potential
@@ -106,6 +111,8 @@ class CalleeCache {
106111
/// given instruction. E.g. it could be destructors.
107112
CalleeList getCalleeList(SILInstruction *I) const;
108113

114+
CalleeList getCalleeList(SILDeclRef Decl) const;
115+
109116
private:
110117
void enumerateFunctionsInModule();
111118
void sortAndUniqueCallees();
@@ -115,7 +122,6 @@ class CalleeCache {
115122
void computeWitnessMethodCalleesForWitnessTable(SILWitnessTable &WT);
116123
void computeMethodCallees();
117124
SILFunction *getSingleCalleeForWitnessMethod(WitnessMethodInst *WMI) const;
118-
CalleeList getCalleeList(SILDeclRef Decl) const;
119125
CalleeList getCalleeList(WitnessMethodInst *WMI) const;
120126
CalleeList getCalleeList(ClassMethodInst *CMI) const;
121127
CalleeList getCalleeListForCalleeKind(SILValue Callee) const;
@@ -162,17 +168,23 @@ class BasicCalleeAnalysis : public SILAnalysis {
162168
Cache.reset();
163169
}
164170

165-
CalleeList getCalleeList(FullApplySite FAS) {
171+
LLVM_ATTRIBUTE_DEPRECATED(void dump() const LLVM_ATTRIBUTE_USED,
172+
"Only for use in the debugger");
173+
174+
void print(llvm::raw_ostream &os) const;
175+
176+
void updateCache() {
166177
if (!Cache)
167178
Cache = llvm::make_unique<CalleeCache>(M);
179+
}
168180

181+
CalleeList getCalleeList(FullApplySite FAS) {
182+
updateCache();
169183
return Cache->getCalleeList(FAS);
170184
}
171185

172186
CalleeList getCalleeList(SILInstruction *I) {
173-
if (!Cache)
174-
Cache = llvm::make_unique<CalleeCache>(M);
175-
187+
updateCache();
176188
return Cache->getCalleeList(I);
177189
}
178190
};

lib/SILOptimizer/Analysis/BasicCalleeAnalysis.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,24 @@
2525

2626
using namespace swift;
2727

28-
bool CalleeList::allCalleesVisible() {
28+
void CalleeList::dump() const {
29+
print(llvm::errs());
30+
}
31+
32+
void CalleeList::print(llvm::raw_ostream &os) const {
33+
os << "Incomplete callee list? : "
34+
<< (isIncomplete() ? "Yes" : "No");
35+
if (!allCalleesVisible())
36+
os <<", not all callees visible";
37+
os << '\n';
38+
os << "Known callees:\n";
39+
for (auto *CalleeFn : *this) {
40+
os << " " << CalleeFn->getName() << "\n";
41+
}
42+
os << "\n";
43+
}
44+
45+
bool CalleeList::allCalleesVisible() const {
2946
if (isIncomplete())
3047
return false;
3148

@@ -284,3 +301,22 @@ CalleeList CalleeCache::getCalleeList(SILInstruction *I) const {
284301
SILDeclRef Destructor = SILDeclRef(Class->getDestructor());
285302
return getCalleeList(Destructor);
286303
}
304+
305+
void BasicCalleeAnalysis::dump() const {
306+
print(llvm::errs());
307+
}
308+
309+
void BasicCalleeAnalysis::print(llvm::raw_ostream &os) const {
310+
if (!Cache) {
311+
os << "<no cache>\n";
312+
}
313+
llvm::DenseSet<SILDeclRef> printed;
314+
for (auto &VTable : M.getVTableList()) {
315+
for (const SILVTable::Entry &entry : VTable.getEntries()) {
316+
if (printed.insert(entry.Method).second) {
317+
os << "callees for " << entry.Method << ":\n";
318+
Cache->getCalleeList(entry.Method).print(os);
319+
}
320+
}
321+
}
322+
}

lib/SILOptimizer/UtilityPasses/BasicCalleePrinter.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ class BasicCalleePrinterPass : public SILModuleTransform {
4040
llvm::outs() << *FAS.getInstruction();
4141

4242
auto Callees = BCA->getCalleeList(FAS);
43-
llvm::outs() << "Incomplete callee list? : "
44-
<< (Callees.isIncomplete() ? "Yes" : "No") << "\n";
45-
llvm::outs() << "Known callees:\n";
46-
for (auto *CalleeFn : Callees)
47-
llvm::outs() << CalleeFn->getName() << "\n";
48-
llvm::outs() << "\n";
43+
Callees.print(llvm::outs());
4944
}
5045

5146
/// The entry point to the transformation.

0 commit comments

Comments
 (0)