Skip to content

Commit 94b79df

Browse files
authored
Merge pull request #3050 from eeckstein/irgen-refactoring
2 parents 60a6522 + 7fcfa78 commit 94b79df

File tree

1 file changed

+31
-50
lines changed

1 file changed

+31
-50
lines changed

lib/IRGen/IRGenSIL.cpp

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,9 +3362,8 @@ visitIsUniqueOrPinnedInst(swift::IsUniqueOrPinnedInst *i) {
33623362
}
33633363

33643364
static bool tryDeferFixedSizeBufferInitialization(IRGenSILFunction &IGF,
3365-
const SILInstruction *allocInst,
3365+
SILInstruction *allocInst,
33663366
const TypeInfo &ti,
3367-
SILValue addressValue,
33683367
Address fixedSizeBuffer,
33693368
const llvm::Twine &name) {
33703369
// There's no point in doing this for fixed-sized types, since we'll allocate
@@ -3384,8 +3383,8 @@ static bool tryDeferFixedSizeBufferInitialization(IRGenSILFunction &IGF,
33843383
// Does this instruction use the allocation? If not, continue.
33853384
auto Ops = inst->getAllOperands();
33863385
if (std::none_of(Ops.begin(), Ops.end(),
3387-
[&addressValue](const Operand &Op) {
3388-
return Op.get() == addressValue;
3386+
[allocInst](const Operand &Op) {
3387+
return Op.get() == allocInst;
33893388
}))
33903389
continue;
33913390

@@ -3409,7 +3408,7 @@ static bool tryDeferFixedSizeBufferInitialization(IRGenSILFunction &IGF,
34093408
IGF.Builder.CreateLifetimeStart(fixedSizeBuffer,
34103409
getFixedBufferSize(IGF.IGM));
34113410
}
3412-
IGF.setContainerOfUnallocatedAddress(addressValue, fixedSizeBuffer);
3411+
IGF.setContainerOfUnallocatedAddress(allocInst, fixedSizeBuffer);
34133412
return true;
34143413
}
34153414

@@ -3456,10 +3455,7 @@ void IRGenSILFunction::visitAllocStackInst(swift::AllocStackInst *i) {
34563455
// If a dynamic alloc_stack is immediately initialized by a copy_addr
34573456
// operation, we can combine the allocation and initialization using an
34583457
// optimized value witness.
3459-
if (tryDeferFixedSizeBufferInitialization(*this, i, type,
3460-
i,
3461-
Address(),
3462-
dbgname))
3458+
if (tryDeferFixedSizeBufferInitialization(*this, i, type, Address(), dbgname))
34633459
return;
34643460

34653461
auto addr = type.allocateStack(*this,
@@ -4257,7 +4253,7 @@ void IRGenSILFunction::visitInitExistentialAddrInst(swift::InitExistentialAddrIn
42574253
auto &srcTI = getTypeInfo(i->getLoweredConcreteType());
42584254

42594255
// See if we can defer initialization of the buffer to a copy_addr into it.
4260-
if (tryDeferFixedSizeBufferInitialization(*this, i, srcTI, i, buffer, ""))
4256+
if (tryDeferFixedSizeBufferInitialization(*this, i, srcTI, buffer, ""))
42614257
return;
42624258

42634259
// Allocate in the destination fixed-size buffer.
@@ -4460,52 +4456,37 @@ void IRGenSILFunction::setAllocatedAddressForBuffer(SILValue v,
44604456

44614457
void IRGenSILFunction::visitCopyAddrInst(swift::CopyAddrInst *i) {
44624458
SILType addrTy = i->getSrc()->getType();
4459+
const TypeInfo &addrTI = getTypeInfo(addrTy);
44634460
Address src = getLoweredAddress(i->getSrc());
4464-
Address dest;
4465-
bool isFixedBufferInitialization;
44664461
// See whether we have a deferred fixed-size buffer initialization.
44674462
auto &loweredDest = getLoweredValue(i->getDest());
44684463
if (loweredDest.isUnallocatedAddressInBuffer()) {
4469-
isFixedBufferInitialization = true;
4470-
dest = loweredDest.getContainerOfAddress();
4471-
} else {
4472-
isFixedBufferInitialization = false;
4473-
dest = loweredDest.getAddress();
4474-
}
4475-
4476-
const TypeInfo &addrTI = getTypeInfo(addrTy);
4477-
4478-
unsigned takeAndOrInitialize =
4479-
(i->isTakeOfSrc() << 1U) | i->isInitializationOfDest();
4480-
static const unsigned COPY = 0, TAKE = 2, ASSIGN = 0, INITIALIZE = 1;
4481-
4482-
switch (takeAndOrInitialize) {
4483-
case ASSIGN | COPY:
4484-
assert(!isFixedBufferInitialization
4485-
&& "can't assign into an unallocated buffer");
4486-
addrTI.assignWithCopy(*this, dest, src, addrTy);
4487-
break;
4488-
case INITIALIZE | COPY:
4489-
if (isFixedBufferInitialization) {
4490-
Address addr = addrTI.initializeBufferWithCopy(*this, dest, src, addrTy);
4464+
assert(i->isInitializationOfDest()
4465+
&& "need to initialize an unallocated buffer");
4466+
Address cont = loweredDest.getContainerOfAddress();
4467+
if (i->isTakeOfSrc()) {
4468+
Address addr = addrTI.initializeBufferWithTake(*this, cont, src, addrTy);
44914469
setAllocatedAddressForBuffer(i->getDest(), addr);
4492-
} else
4493-
addrTI.initializeWithCopy(*this, dest, src, addrTy);
4494-
break;
4495-
case ASSIGN | TAKE:
4496-
assert(!isFixedBufferInitialization
4497-
&& "can't assign into an unallocated buffer");
4498-
addrTI.assignWithTake(*this, dest, src, addrTy);
4499-
break;
4500-
case INITIALIZE | TAKE:
4501-
if (isFixedBufferInitialization) {
4502-
Address addr = addrTI.initializeBufferWithTake(*this, dest, src, addrTy);
4470+
} else {
4471+
Address addr = addrTI.initializeBufferWithCopy(*this, cont, src, addrTy);
45034472
setAllocatedAddressForBuffer(i->getDest(), addr);
4504-
} else
4505-
addrTI.initializeWithTake(*this, dest, src, addrTy);
4506-
break;
4507-
default:
4508-
llvm_unreachable("unexpected take/initialize attribute combination?!");
4473+
}
4474+
} else {
4475+
Address dest = loweredDest.getAddress();
4476+
4477+
if (i->isInitializationOfDest()) {
4478+
if (i->isTakeOfSrc()) {
4479+
addrTI.initializeWithTake(*this, dest, src, addrTy);
4480+
} else {
4481+
addrTI.initializeWithCopy(*this, dest, src, addrTy);
4482+
}
4483+
} else {
4484+
if (i->isTakeOfSrc()) {
4485+
addrTI.assignWithTake(*this, dest, src, addrTy);
4486+
} else {
4487+
addrTI.assignWithCopy(*this, dest, src, addrTy);
4488+
}
4489+
}
45094490
}
45104491
}
45114492

0 commit comments

Comments
 (0)