Skip to content

Commit c25ed8c

Browse files
committed
EscapeAnalysis: possibility to create a summary graph for a function.
Not used yet, but in preparation for a better update mechanism.
1 parent 88baeea commit c25ed8c

File tree

3 files changed

+194
-145
lines changed

3 files changed

+194
-145
lines changed

include/swift/SILAnalysis/EscapeAnalysis.h

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,34 @@ class EscapeAnalysis : public SILAnalysis {
281281

282282
/// Mapping from nodes in a calleee-graph to nodes in a caller-graph.
283283
class CGNodeMap {
284+
/// The map itself.
284285
llvm::DenseMap<CGNode *, CGNode *> Map;
286+
287+
/// The list of source nodes (= keys in Map), which is used as a work-list.
288+
llvm::SmallVector<CGNode *, 8> MappedNodes;
285289
public:
286-
void insert(CGNode *From, CGNode *To) {
287-
assert(!From->isMerged && !To->isMerged);
290+
291+
/// Adds a mapping and pushes the \p From node into the work-list
292+
/// MappedNodes.
293+
void add(CGNode *From, CGNode *To) {
294+
assert(From && To && !From->isMerged && !To->isMerged);
288295
Map[From] = To;
296+
if (!From->isInWorkList) {
297+
MappedNodes.push_back(From);
298+
From->isInWorkList = true;
299+
}
289300
}
301+
/// Looks up a node in the mapping.
290302
CGNode *get(CGNode *From) const {
291303
auto Iter = Map.find(From);
292304
if (Iter == Map.end())
293305
return nullptr;
294306

295307
return Iter->second->getMergeTarget();
296308
}
309+
const SmallVectorImpl<CGNode *> &getMappedNodes() const {
310+
return MappedNodes;
311+
}
297312
};
298313

299314
public:
@@ -387,9 +402,13 @@ class EscapeAnalysis : public SILAnalysis {
387402
/// content node is scheduled to be merged with \p pointsTo.
388403
void updatePointsTo(CGNode *InitialNode, CGNode *pointsTo);
389404

390-
/// Merges all defer-edges from the callee graph.
391-
bool addDeferEdgesFromCallee(CGNode *CalleeSource,
392-
const CGNodeMap &Callee2CallerMapping);
405+
/// Utility function to clear the isInWorkList flags of all nodes in
406+
/// \p WorkList.
407+
static void clearWorkListFlags(const SmallVectorImpl<CGNode *> &WorkList) {
408+
for (CGNode *Node : WorkList) {
409+
Node->isInWorkList = false;
410+
}
411+
}
393412

394413
public:
395414
/// Constructs a connection graph for a function.
@@ -434,14 +453,19 @@ class EscapeAnalysis : public SILAnalysis {
434453
CGNode *getContentNode(CGNode *AddrNode);
435454

436455
/// Get or creates a pseudo node for the function return value.
437-
CGNode *getReturnNode(ReturnInst *RI) {
456+
CGNode *getReturnNode() {
438457
if (!ReturnNode) {
439-
ReturnNode = allocNode(RI, NodeType::Return);
458+
ReturnNode = allocNode(nullptr, NodeType::Return);
440459
ReturnNode->mergeEscapeState(EscapeState::Arguments);
441460
}
442461
return ReturnNode;
443462
}
444463

464+
/// Returns the node for the function return value if present.
465+
CGNode *getReturnNodeOrNull() const {
466+
return ReturnNode;
467+
}
468+
445469
/// Returns the node of the "exact" value \p V (no projections are skipped)
446470
/// if one exists.
447471
CGNode *getNodeOrNull(ValueBase *V) {
@@ -504,8 +528,9 @@ class EscapeAnalysis : public SILAnalysis {
504528
/// e.g. release or apply instructions.
505529
void getUsePoints(llvm::SmallPtrSetImpl<ValueBase *> &Values, CGNode *Node);
506530

507-
/// Merges the graph of a callee function (called by \p FAS) into this graph.
508-
bool mergeCalleeGraph(FullApplySite FAS, ConnectionGraph *CalleeGraph);
531+
/// Merges the \p SourceGraph into this graph. The \p Mapping contains the
532+
/// initial node mapping of the nodes to start the merge.
533+
bool mergeFrom(ConnectionGraph *SourceGraph, CGNodeMap &Mapping);
509534

510535
/// Propagates the escape states through the graph.
511536
void propagateEscapeStates();
@@ -584,6 +609,17 @@ class EscapeAnalysis : public SILAnalysis {
584609
/// Merge the graphs of all known callees into this graph.
585610
bool mergeAllCallees(ConnectionGraph *ConGraph, CallGraph &CG);
586611

612+
/// Merges the graph of a callee function into the graph of
613+
/// a caller function, whereas \p FAS is the call-site.
614+
bool mergeCalleeGraph(FullApplySite FAS,
615+
ConnectionGraph *CallerGraph,
616+
ConnectionGraph *CalleeGraph);
617+
618+
/// Create a summary graph \p SummaryGraph from \p Graph. It just contains
619+
/// the content nodes of \p Graph.
620+
void createSummaryGraph(ConnectionGraph *SummaryGraph,
621+
ConnectionGraph *Graph);
622+
587623
/// Set all arguments and return values of all callees to global escaping.
588624
void finalizeGraphConservatively(ConnectionGraph *ConGraph);
589625

0 commit comments

Comments
 (0)