Skip to content

Commit c9a36a0

Browse files
committed
[NFC] Add a utility for this load take/borrow dance
1 parent c948c8a commit c9a36a0

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

lib/SILGen/RValue.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,8 @@ class ExplodeTupleValue
9494

9595
void visitType(CanType formalType, ManagedValue v) {
9696
// If we have a loadable type that has not been loaded, actually load it.
97-
if (!v.getType().isObject() && v.getType().isLoadable(SGF.F)) {
98-
if (v.isPlusOne(SGF)) {
99-
v = SGF.B.createLoadTake(loc, v);
100-
} else {
101-
v = SGF.B.createLoadBorrow(loc, v);
102-
}
97+
if (!v.getType().isObject()) {
98+
v = SGF.B.createLoadIfLoadable(loc, v);
10399
}
104100

105101
values.push_back(v);

lib/SILGen/SILGenBuilder.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,23 @@ ManagedValue SILGenBuilder::createUncheckedTakeEnumDataAddr(
437437
return cloner.clone(result);
438438
}
439439

440+
ManagedValue
441+
SILGenBuilder::createLoadIfLoadable(SILLocation loc, ManagedValue addr) {
442+
assert(addr.getType().isAddress());
443+
if (!addr.getType().isLoadable(SGF.F))
444+
return addr;
445+
return createLoadWithSameOwnership(loc, addr);
446+
}
447+
448+
ManagedValue
449+
SILGenBuilder::createLoadWithSameOwnership(SILLocation loc,
450+
ManagedValue addr) {
451+
if (addr.isPlusOne(SGF))
452+
return createLoadTake(loc, addr);
453+
else
454+
return createLoadBorrow(loc, addr);
455+
}
456+
440457
ManagedValue SILGenBuilder::createLoadTake(SILLocation loc, ManagedValue v) {
441458
auto &lowering = SGF.getTypeLowering(v.getType());
442459
return createLoadTake(loc, v, lowering);

lib/SILGen/SILGenBuilder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ class SILGenBuilder : public SILBuilder {
236236
ManagedValue createUncheckedTakeEnumDataAddr(SILLocation loc, ManagedValue operand,
237237
EnumElementDecl *element, SILType ty);
238238

239+
/// Given the address of a value, load a scalar value from it if the type
240+
/// is loadable. Most general routines in SILGen expect to work with
241+
/// values with the canonical scalar-ness for their type.
242+
ManagedValue createLoadIfLoadable(SILLocation loc, ManagedValue addr);
243+
244+
/// Given the address of a loadable value, load the value but don't
245+
/// change the ownership.
246+
ManagedValue createLoadWithSameOwnership(SILLocation loc, ManagedValue addr);
247+
239248
ManagedValue createLoadTake(SILLocation loc, ManagedValue addr);
240249
ManagedValue createLoadTake(SILLocation loc, ManagedValue addr,
241250
const TypeLowering &lowering);

lib/SILGen/SILGenProlog.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,7 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
263263
bool argIsLoadable = argType.isLoadable(SGF.F);
264264
if (argIsLoadable) {
265265
if (argType.isAddress()) {
266-
if (mv.isPlusOne(SGF))
267-
mv = SGF.B.createLoadTake(loc, mv);
268-
else
269-
mv = SGF.B.createLoadBorrow(loc, mv);
266+
mv = SGF.B.createLoadWithSameOwnership(loc, mv);
270267
argType = argType.getObjectType();
271268
}
272269
}

0 commit comments

Comments
 (0)