Skip to content

Commit 2b8efce

Browse files
committed
Merge similar functions in TypeExpansionAnalysis.
1 parent 24032f9 commit 2b8efce

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

include/swift/SILAnalysis/TypeExpansionAnalysis.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,28 @@
1313
#define SWIFT_SILANALYSIS_TYPEEXPANSIONANALYSIS_H
1414

1515
#include "swift/SIL/Projection.h"
16+
#include "swift/SIL/SILType.h"
1617
#include "swift/SIL/SILValue.h"
1718
#include "swift/SILAnalysis/Analysis.h"
1819
#include "llvm/ADT/DenseMap.h"
1920

2021
namespace swift {
2122

23+
/// Type expansion kind.
24+
enum class TEKind {
25+
TELeaf, // Leaf nodes expansion.
26+
TENode // Intermediate and leaf nodes expansion.
27+
};
28+
29+
using TypeExpansionMap = llvm::DenseMap<SILType, ProjectionPathList>;
30+
2231
/// This analysis determines memory effects during destruction.
2332
class TypeExpansionAnalysis : public SILAnalysis {
24-
using TypeExpansionMap = llvm::DenseMap<SILType, ProjectionPathList>;
2533
/// Caches the type to leaf node expansion.
26-
TypeExpansionMap LeafTECache;
34+
TypeExpansionMap TELeafCache;
2735
/// Caches the type to each node expansion, including intermediate nodes as
2836
/// well as leaf nodes in the type tree.
29-
TypeExpansionMap NodeTECache;
37+
TypeExpansionMap TENodeCache;
3038

3139
public:
3240
TypeExpansionAnalysis(SILModule *M)
@@ -36,11 +44,10 @@ class TypeExpansionAnalysis : public SILAnalysis {
3644
return S->getKind() == AnalysisKind::TypeExpansion;
3745
}
3846

39-
/// Return ProjectionPath to every leaf node of the given type.
40-
const ProjectionPathList &getTypeLeafExpansion(SILType B, SILModule *Mod);
41-
/// Returns the ProjectionPath to every leaf and intermediate node of the
42-
/// given type.
43-
const ProjectionPathList &getTypeNodeExpansion(SILType B, SILModule *Mod);
47+
/// Return ProjectionPath to every leaf or intermediate node of the given type.
48+
const ProjectionPathList &getTypeExpansionProjectionPaths(SILType B,
49+
SILModule *Mod,
50+
TEKind K);
4451
};
4552
}
4653
#endif

lib/SIL/MemLocation.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ void MemLocation::expand(MemLocation &Base, SILModule *M, MemLocationList &Locs,
116116
// Construct the MemLocation by appending the projection path from the
117117
// accessed node to the leaf nodes.
118118
ProjectionPath &BasePath = Base.getPath().getValue();
119-
for (const auto &P : TE->getTypeLeafExpansion(Base.getType(), M)) {
119+
for (const auto &P : TE->getTypeExpansionProjectionPaths(Base.getType(), M,
120+
TEKind::TELeaf)) {
120121
Locs.push_back(MemLocation(Base.getBase(), P.getValue(), BasePath));
121122
}
122123
}
@@ -127,7 +128,8 @@ void MemLocation::reduce(MemLocation &Base, SILModule *M, MemLocationSet &Locs,
127128
// accessed node to the leaf nodes.
128129
MemLocationList Nodes;
129130
ProjectionPath &BasePath = Base.getPath().getValue();
130-
for (const auto &P : TE->getTypeNodeExpansion(Base.getType(), M)) {
131+
for (const auto &P : TE->getTypeExpansionProjectionPaths(Base.getType(), M,
132+
TEKind::TENode)) {
131133
Nodes.push_back(MemLocation(Base.getBase(), P.getValue(), BasePath));
132134
}
133135

@@ -172,7 +174,8 @@ void MemLocation::expandWithValues(MemLocation &Base, SILValue &Val,
172174
// path
173175
// from the accessed node to the leaf nodes.
174176
ProjectionPath &BasePath = Base.getPath().getValue();
175-
for (const auto &P : TE->getTypeLeafExpansion(Base.getType(), M)) {
177+
for (const auto &P : TE->getTypeExpansionProjectionPaths(Base.getType(), M,
178+
TEKind::TELeaf)) {
176179
Locs.push_back(MemLocation(Base.getBase(), P.getValue(), BasePath));
177180
Vals.push_back(LoadStoreValue(Val, P.getValue()));
178181
}
@@ -190,7 +193,8 @@ SILValue MemLocation::reduceWithValues(MemLocation &Base, SILModule *M,
190193
// Base memory location.
191194
MemLocationList ALocs;
192195
ProjectionPath &BasePath = Base.getPath().getValue();
193-
for (const auto &P : TE->getTypeNodeExpansion(Base.getType(), M)) {
196+
for (const auto &P : TE->getTypeExpansionProjectionPaths(Base.getType(), M,
197+
TEKind::TENode)) {
194198
ALocs.push_back(MemLocation(Base.getBase(), P.getValue(), BasePath));
195199
}
196200

lib/SILAnalysis/TypeExpansionAnalysis.cpp

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,55 @@
1+
//===---------- TypeExpansionAnalysis.cpp - Type Expansion Analysis -------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#define DEBUG_TYPE "typeexpansion-analysis"
114
#include "swift/SILAnalysis/TypeExpansionAnalysis.h"
215
#include "swift/SIL/SILInstruction.h"
316
#include "swift/SIL/SILModule.h"
417
#include "llvm/Support/Debug.h"
518

6-
#define DEBUG_TYPE "typeexpansion-analysis"
7-
819
using namespace swift;
920

10-
// The MemoryBehavior Cache must not grow beyond this size.
21+
// The TypeExpansion Cache must not grow beyond this size.
1122
// We limit the size of the MB cache to 2**12 because we want to limit the
1223
// memory usage of this cache.
1324
static const int TypeExpansionAnalysisMaxCacheSize = 4096;
1425

15-
const ProjectionPathList &
16-
TypeExpansionAnalysis::getTypeLeafExpansion(SILType B, SILModule *Mod) {
26+
const ProjectionPathList &
27+
TypeExpansionAnalysis::getTypeExpansionProjectionPaths(SILType B, SILModule *Mod,
28+
TEKind Kind) {
29+
// Which cache we should be looking up.
30+
bool IsLeaf = Kind == TEKind::TELeaf;
31+
TypeExpansionMap &Cache = IsLeaf ? TELeafCache : TENodeCache;
32+
1733
// Check whether we have the type expansion.
18-
if (LeafTECache.find(B) != LeafTECache.end()) {
19-
return LeafTECache.find(B)->second;
34+
auto Iter = Cache.find(B);
35+
if (Iter != Cache.end()) {
36+
return Iter->second;
2037
}
2138

2239
// Flush the cache if the size of the cache is too large.
23-
if (LeafTECache.size() > TypeExpansionAnalysisMaxCacheSize) {
24-
LeafTECache.clear();
40+
if (Cache.size() > TypeExpansionAnalysisMaxCacheSize) {
41+
Cache.clear();
2542
}
2643

27-
// Need to build the type expansion.
28-
LeafTECache[B] = ProjectionPathList();
29-
ProjectionPath::expandTypeIntoLeafProjectionPaths(B, Mod, LeafTECache[B]);
30-
return LeafTECache[B];
31-
}
32-
33-
const ProjectionPathList &
34-
TypeExpansionAnalysis::getTypeNodeExpansion(SILType B, SILModule *Mod) {
35-
// Check whether we have the type expansion.
36-
if (NodeTECache.find(B) != NodeTECache.end()) {
37-
return NodeTECache.find(B)->second;
38-
}
39-
40-
// Flush the cache if the size of the cache is too large.
41-
if (NodeTECache.size() > TypeExpansionAnalysisMaxCacheSize) {
42-
NodeTECache.clear();
44+
// Build the type expansion for the leaf nodes.
45+
if (IsLeaf) {
46+
ProjectionPath::expandTypeIntoLeafProjectionPaths(B, Mod, Cache[B]);
47+
return Cache[B];
4348
}
4449

45-
// Need to build the type expansion.
46-
NodeTECache[B] = ProjectionPathList();
47-
ProjectionPath::expandTypeIntoNodeProjectionPaths(B, Mod, NodeTECache[B]);
48-
return NodeTECache[B];
50+
// Build the type expansion for the internal and leaf nodes.
51+
ProjectionPath::expandTypeIntoNodeProjectionPaths(B, Mod, Cache[B]);
52+
return Cache[B];
4953
}
5054

5155
SILAnalysis *swift::createTypeExpansionAnalysis(SILModule *M) {

0 commit comments

Comments
 (0)