Skip to content

SILCombine: handle upcasts when converting from alloc_ref_dynamic to alloc_ref #5140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1238,34 +1238,44 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
// ->
// alloc_ref X
Builder.setCurrentDebugScope(ARDI->getDebugScope());
if (MetatypeInst *MI = dyn_cast<MetatypeInst>(ARDI->getOperand())) {

SILValue MDVal = ARDI->getOperand();
if (auto *UC = dyn_cast<UpcastInst>(MDVal))
MDVal = UC->getOperand();

SILInstruction *NewInst = nullptr;
if (MetatypeInst *MI = dyn_cast<MetatypeInst>(MDVal)) {
auto &Mod = ARDI->getModule();
auto SILInstanceTy = MI->getType().getMetatypeInstanceType(Mod);
auto *ARI = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,
ARDI->isObjC(), false);
return ARI;
}

// checked_cast_br [exact] $Y.Type to $X.Type, bbSuccess, bbFailure
// ...
// bbSuccess(%T: $X.Type)
// alloc_ref_dynamic %T : $X.Type, $X
// ->
// alloc_ref $X
if (isa<SILArgument>(ARDI->getOperand())) {
NewInst = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,
ARDI->isObjC(), false);

} else if (isa<SILArgument>(MDVal)) {

// checked_cast_br [exact] $Y.Type to $X.Type, bbSuccess, bbFailure
// ...
// bbSuccess(%T: $X.Type)
// alloc_ref_dynamic %T : $X.Type, $X
// ->
// alloc_ref $X
auto *PredBB = ARDI->getParent()->getSinglePredecessor();
if (!PredBB)
return nullptr;
auto *CCBI = dyn_cast<CheckedCastBranchInst>(PredBB->getTerminator());
if (CCBI && CCBI->isExact() && ARDI->getParent() == CCBI->getSuccessBB()) {
auto &Mod = ARDI->getModule();
auto SILInstanceTy = CCBI->getCastType().getMetatypeInstanceType(Mod);
auto *ARI = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,
NewInst = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,
ARDI->isObjC(), false);
return ARI;
}
}
return nullptr;
if (NewInst && NewInst->getType() != ARDI->getType()) {
// In case the argument was an upcast of the metatype, we have to upcast the
// resulting reference.
NewInst = Builder.createUpcast(ARDI->getLoc(), NewInst, ARDI->getType());
}
return NewInst;
}

SILInstruction *SILCombiner::visitEnumInst(EnumInst *EI) {
Expand Down
42 changes: 42 additions & 0 deletions test/SILOptimizer/sil_combine.sil
Original file line number Diff line number Diff line change
Expand Up @@ -2256,6 +2256,22 @@ sil @alloc_ref_dynamic_with_metatype : $() -> () {
return %4 : $()
}

// CHECK-LABEL: sil @alloc_ref_dynamic_with_upcast_metatype
// CHECK: bb0
// CHECK-NOT: alloc_ref_dynamic
// CHECK-NEXT: [[R:%[0-9]+]] = alloc_ref $E
// CHECK-NEXT: [[C:%[0-9]+]] = upcast [[R]] : $E to $B
// CHECK-NEXT: strong_release [[C]]
// CHECK: return
sil @alloc_ref_dynamic_with_upcast_metatype : $() -> () {
%1 = metatype $@thick E.Type
%2 = upcast %1 : $@thick E.Type to $@thick B.Type
%3 = alloc_ref_dynamic %2 : $@thick B.Type, $B
strong_release %3 : $B
%4 = tuple()
return %4 : $()
}

// CHECK-LABEL: @alloc_ref_dynamic_after_successful_checked_cast_br
// CHECK: checked_cast_br
// CHECK: bb1
Expand All @@ -2279,6 +2295,32 @@ bb3 (%10: $Builtin.Int32):
return %10 : $Builtin.Int32
}

// CHECK-LABEL: @alloc_ref_dynamic_upcast_after_successful_checked_cast_br
// CHECK: checked_cast_br
// CHECK: bb1
// CHECK-NOT: alloc_ref_dynamic
// CHECK: [[R:%[0-9]+]] = alloc_ref $E
// CHECK-NEXT: [[C:%[0-9]+]] = upcast [[R]] : $E to $B
// CHECK-NEXT: strong_release [[C]]
sil @alloc_ref_dynamic_upcast_after_successful_checked_cast_br : $(@thick B.Type) -> Builtin.Int32 {
bb0(%1 : $@thick B.Type):
checked_cast_br [exact] %1 : $@thick B.Type to $@thick E.Type, bb1, bb2

bb1(%2 : $@thick E.Type):
%3 = upcast %2 : $@thick E.Type to $@thick B.Type
%4 = alloc_ref_dynamic %3 : $@thick B.Type, $B
strong_release %4 : $B
%5 = integer_literal $Builtin.Int32, 1
br bb3 (%5 : $Builtin.Int32)

bb2:
%6 = integer_literal $Builtin.Int32, 2
br bb3 (%6 : $Builtin.Int32)

bb3 (%10: $Builtin.Int32):
return %10 : $Builtin.Int32
}

// CHECK-LABEL: sil @delete_dead_alloc_stack
// CHECK: bb0
// CHECK-NEXT: tuple
Expand Down