Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1206,27 +1206,25 @@ private extension ScopedInstruction {
case is BeginApplyInst:
return true // Has already been checked with other full applies.
case let loadBorrowInst as LoadBorrowInst:
for case let destroyAddrInst as DestroyAddrInst in analyzedInstructions.loopSideEffects {
if context.aliasAnalysis.mayAlias(loadBorrowInst.address, destroyAddrInst.destroyedAddress) {
if !scope.contains(destroyAddrInst) {
return false
}
for sideEffectInst in analyzedInstructions.loopSideEffects {
if let endBorrow = sideEffectInst as? EndBorrowInst,
let begin = endBorrow.borrow as? LoadBorrowInst,
begin == self
{
continue
}
}

for storeInst in analyzedInstructions.stores {
if storeInst.mayWrite(toAddress: loadBorrowInst.address, context.aliasAnalysis) {
if !scope.contains(storeInst) {
return false
}
if sideEffectInst.mayWrite(toAddress: loadBorrowInst.address, context.aliasAnalysis),
!scope.contains(sideEffectInst)
{
return false
}
}

fallthrough
case is BeginAccessInst:
return true

case let beginAccess as BeginAccessInst:
for fullApplyInst in analyzedInstructions.fullApplies {
guard mayWriteToMemory && fullApplyInst.mayReadOrWrite(address: operands.first!.value, context.aliasAnalysis) ||
!mayWriteToMemory && fullApplyInst.mayWrite(toAddress: operands.first!.value, context.aliasAnalysis) else {
guard mayWriteToMemory && fullApplyInst.mayReadOrWrite(address: beginAccess.address, context.aliasAnalysis) ||
!mayWriteToMemory && fullApplyInst.mayWrite(toAddress: beginAccess.address, context.aliasAnalysis) else {
continue
}

Expand All @@ -1236,7 +1234,7 @@ private extension ScopedInstruction {
}
}

switch operands.first!.value.accessPath.base {
switch beginAccess.address.accessPath.base {
case .class, .global:
for sideEffect in analyzedInstructions.loopSideEffects where sideEffect.mayRelease {
// Since a class might have a deinitializer, hoisting begin/end_access pair could violate
Expand Down
28 changes: 28 additions & 0 deletions test/SILOptimizer/licm.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,34 @@ bb3:
return %r : $()
}

// CHECK-LABEL: sil [ossa] @dont_hoist_load_borrow :
// CHECK: bb1:
// CHECK-NEXT: copy_addr
// CHECK-NEXT: load_borrow
// CHECK: } // end sil function 'dont_hoist_load_borrow'
sil [ossa] @dont_hoist_load_borrow : $@convention(thin) (@in_guaranteed String) -> () {
bb0(%0 : $*String):
%1 = alloc_stack $String
br bb1

bb1:
copy_addr %0 to [init] %1
%3 = load_borrow %1
fix_lifetime %3
end_borrow %3
%6 = load [take] %1
destroy_value %6
cond_br undef, bb2, bb3

bb2:
br bb1

bb3:
dealloc_stack %1
%r = tuple()
return %r : $()
}

// CHECK-LABEL: sil [ossa] @dont_hoist_struct :
// CHECK: bb1:
// CHECK-NEXT: struct $NonCopyable
Expand Down