Skip to content

Commit 780f41b

Browse files
committed
[AST] NFC: Repack misc TypeVariableType bits
1) This saves 16 bytes per type variable. 2) Please note that the 'ID' was "32 - NumTypeBaseBits" just a few weeks ago, so this isn't a real change.
1 parent c3ae675 commit 780f41b

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

include/swift/AST/Types.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,14 @@ class alignas(1 << TypeAlignInBits) TypeBase {
318318
unsigned : NumTypeBaseBits;
319319

320320
/// \brief The unique number assigned to this type variable.
321-
uint64_t ID : 64 - NumTypeBaseBits;
321+
uint64_t ID : 32 - NumTypeBaseBits;
322+
323+
/// Type variable options.
324+
unsigned Options : 3;
325+
326+
/// Index into the list of type variables, as used by the
327+
/// constraint graph.
328+
unsigned GraphIndex : 29;
322329
};
323330
NUMBITS(TypeVariableType, 64);
324331

lib/Sema/ConstraintSystem.h

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ enum TypeVariableOptions {
177177
/// to, what specific types it might be and, eventually, the fixed type to
178178
/// which it is assigned.
179179
class TypeVariableType::Implementation {
180-
/// Type variable options.
181-
unsigned Options : 3;
182-
183180
/// \brief The locator that describes where this type variable was generated.
184181
constraints::ConstraintLocator *locator;
185182

@@ -191,45 +188,53 @@ class TypeVariableType::Implementation {
191188
/// The corresponding node in the constraint graph.
192189
constraints::ConstraintGraphNode *GraphNode = nullptr;
193190

194-
/// Index into the list of type variables, as used by the
195-
/// constraint graph.
196-
unsigned GraphIndex;
197-
198191
friend class constraints::SavedTypeVariableBinding;
199192

200193
public:
194+
/// \brief Retrieve the type variable associated with this implementation.
195+
TypeVariableType *getTypeVariable() {
196+
return reinterpret_cast<TypeVariableType *>(this) - 1;
197+
}
198+
199+
/// \brief Retrieve the type variable associated with this implementation.
200+
const TypeVariableType *getTypeVariable() const {
201+
return reinterpret_cast<const TypeVariableType *>(this) - 1;
202+
}
203+
201204
explicit Implementation(constraints::ConstraintLocator *locator,
202205
unsigned options)
203-
: Options(options), locator(locator),
204-
ParentOrFixed(getTypeVariable()) { }
206+
: locator(locator), ParentOrFixed(getTypeVariable()) {
207+
getTypeVariable()->TypeVariableTypeBits.Options = options;
208+
}
205209

206210
/// \brief Retrieve the unique ID corresponding to this type variable.
207211
unsigned getID() const { return getTypeVariable()->getID(); }
208212

213+
unsigned getRawOptions() const {
214+
return getTypeVariable()->TypeVariableTypeBits.Options;
215+
}
216+
217+
void setRawOptions(unsigned bits) {
218+
getTypeVariable()->TypeVariableTypeBits.Options = bits;
219+
assert(getTypeVariable()->TypeVariableTypeBits.Options == bits
220+
&& "Trucation");
221+
}
222+
209223
/// Whether this type variable can bind to an lvalue type.
210-
bool canBindToLValue() const { return Options & TVO_CanBindToLValue; }
224+
bool canBindToLValue() const { return getRawOptions() & TVO_CanBindToLValue; }
211225

212226
/// Whether this type variable can bind to an inout type.
213-
bool canBindToInOut() const { return Options & TVO_CanBindToInOut; }
227+
bool canBindToInOut() const { return getRawOptions() & TVO_CanBindToInOut; }
214228

215229
/// Whether this type variable prefers a subtype binding over a supertype
216230
/// binding.
217231
bool prefersSubtypeBinding() const {
218-
return Options & TVO_PrefersSubtypeBinding;
232+
return getRawOptions() & TVO_PrefersSubtypeBinding;
219233
}
220234

221235
bool mustBeMaterializable() const {
222-
return !(Options & TVO_CanBindToInOut) && !(Options & TVO_CanBindToLValue);
223-
}
224-
225-
/// \brief Retrieve the type variable associated with this implementation.
226-
TypeVariableType *getTypeVariable() {
227-
return reinterpret_cast<TypeVariableType *>(this) - 1;
228-
}
229-
230-
/// \brief Retrieve the type variable associated with this implementation.
231-
const TypeVariableType *getTypeVariable() const {
232-
return reinterpret_cast<const TypeVariableType *>(this) - 1;
236+
return !(getRawOptions() & TVO_CanBindToInOut) &&
237+
!(getRawOptions() & TVO_CanBindToLValue);
233238
}
234239

235240
/// Retrieve the corresponding node in the constraint graph.
@@ -243,11 +248,13 @@ class TypeVariableType::Implementation {
243248
/// Retrieve the index into the constraint graph's list of type variables.
244249
unsigned getGraphIndex() const {
245250
assert(GraphNode && "Graph node isn't set");
246-
return GraphIndex;
251+
return getTypeVariable()->TypeVariableTypeBits.GraphIndex;
247252
}
248253

249254
/// Set the index into the constraint graph's list of type variables.
250-
void setGraphIndex(unsigned newIndex) { GraphIndex = newIndex; }
255+
void setGraphIndex(unsigned newIndex) {
256+
getTypeVariable()->TypeVariableTypeBits.GraphIndex = newIndex;
257+
}
251258

252259
/// \brief Check whether this type variable either has a representative that
253260
/// is not itself or has a fixed type binding.
@@ -339,8 +346,8 @@ class TypeVariableType::Implementation {
339346
if (!mustBeMaterializable() && otherRep->getImpl().mustBeMaterializable()) {
340347
if (record)
341348
recordBinding(*record);
342-
Options &= ~TVO_CanBindToLValue;
343-
Options &= ~TVO_CanBindToInOut;
349+
getTypeVariable()->TypeVariableTypeBits.Options &= ~TVO_CanBindToLValue;
350+
getTypeVariable()->TypeVariableTypeBits.Options &= ~TVO_CanBindToInOut;
344351
}
345352
}
346353

@@ -381,8 +388,10 @@ class TypeVariableType::Implementation {
381388
if (!rep->getImpl().mustBeMaterializable()) {
382389
if (record)
383390
rep->getImpl().recordBinding(*record);
384-
rep->getImpl().Options &= ~TVO_CanBindToLValue;
385-
rep->getImpl().Options &= ~TVO_CanBindToInOut;
391+
rep->getImpl().getTypeVariable()->TypeVariableTypeBits.Options
392+
&= ~TVO_CanBindToLValue;
393+
rep->getImpl().getTypeVariable()->TypeVariableTypeBits.Options
394+
&= ~TVO_CanBindToInOut;
386395
}
387396
}
388397

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ void TypeVariableType::Implementation::print(llvm::raw_ostream &OS) {
6161
}
6262

6363
SavedTypeVariableBinding::SavedTypeVariableBinding(TypeVariableType *typeVar)
64-
: TypeVarAndOptions(typeVar, typeVar->getImpl().Options),
64+
: TypeVarAndOptions(typeVar, typeVar->getImpl().getRawOptions()),
6565
ParentOrFixed(typeVar->getImpl().ParentOrFixed) { }
6666

6767
void SavedTypeVariableBinding::restore() {
6868
auto *typeVar = getTypeVariable();
69-
typeVar->getImpl().Options = getOptions();
69+
typeVar->getImpl().setRawOptions(getOptions());
7070
typeVar->getImpl().ParentOrFixed = ParentOrFixed;
7171
}
7272

0 commit comments

Comments
 (0)