Skip to content

Commit 011803a

Browse files
committed
AliasAnalysis: compute more precise memory behavior for debug_value undef and the prepareInitialization and zeroInitializer builtins
1 parent 3114657 commit 011803a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ struct AliasAnalysis {
327327
return defaultEffects(of: endBorrow, on: memLoc)
328328

329329
case let debugValue as DebugValueInst:
330-
if debugValue.operand.value.type.isAddress && memLoc.mayAlias(with: debugValue.operand.value, self) {
330+
let v = debugValue.operand.value
331+
if v.type.isAddress, !(v is Undef), memLoc.mayAlias(with: v, self) {
331332
return .init(read: true)
332333
} else {
333334
return .noEffects
@@ -434,6 +435,11 @@ struct AliasAnalysis {
434435
}
435436
let callee = builtin.operands[1].value
436437
return context.calleeAnalysis.getSideEffects(ofCallee: callee).memory
438+
case .PrepareInitialization, .ZeroInitializer:
439+
if builtin.arguments.count == 1, memLoc.mayAlias(with: builtin.arguments[0], self) {
440+
return .init(write: true)
441+
}
442+
return .noEffects
437443
default:
438444
return defaultEffects(of: builtin, on: memLoc)
439445
}

test/SILOptimizer/mem-behavior.sil

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,11 +1250,16 @@ bb0(%0 : $Builtin.RawPointer):
12501250
// CHECK-NEXT: debug_value %0 : $*Int, let, name "x"
12511251
// CHECK-NEXT: %1 = alloc_stack $Int
12521252
// CHECK-NEXT: r=0,w=0
1253+
// CHECK: PAIR #2.
1254+
// CHECK-NEXT: debug_value undef : $*Int, let, name "y"
1255+
// CHECK-NEXT: %0 = alloc_stack $Int
1256+
// CHECK-NEXT: r=0,w=0
12531257
sil @test_debug_value : $@convention(thin) () -> () {
12541258
bb0:
12551259
%0 = alloc_stack $Int
12561260
%1 = alloc_stack $Int
12571261
debug_value %0 : $*Int, let, name "x"
1262+
debug_value undef : $*Int, let, name "y"
12581263
dealloc_stack %1 : $*Int
12591264
dealloc_stack %0 : $*Int
12601265
%r = tuple ()
@@ -1435,6 +1440,34 @@ bb0:
14351440
return %5 : $()
14361441
}
14371442

1443+
// CHECK-LABEL: @builtin_initializer_and_class
1444+
// CHECK: PAIR #0.
1445+
// CHECK-NEXT: %3 = builtin "zeroInitializer"(%2 : $*Int32) : $()
1446+
// CHECK-NEXT: %1 = ref_element_addr %0 : $C, #C.prop
1447+
// CHECK-NEXT: r=0,w=0
1448+
// CHECK: PAIR #1.
1449+
// CHECK-NEXT: %3 = builtin "zeroInitializer"(%2 : $*Int32) : $()
1450+
// CHECK-NEXT: %2 = alloc_stack $Int32
1451+
// CHECK-NEXT: r=0,w=1
1452+
// CHECK: PAIR #2.
1453+
// CHECK-NEXT: %4 = builtin "prepareInitialization"(%2 : $*Int32) : $()
1454+
// CHECK-NEXT: %1 = ref_element_addr %0 : $C, #C.prop
1455+
// CHECK-NEXT: r=0,w=0
1456+
// CHECK: PAIR #3.
1457+
// CHECK-NEXT: %4 = builtin "prepareInitialization"(%2 : $*Int32) : $()
1458+
// CHECK-NEXT: %2 = alloc_stack $Int32
1459+
// CHECK-NEXT: r=0,w=1
1460+
sil @builtin_initializer_and_class : $@convention(thin) (@guaranteed C) -> () {
1461+
bb0(%0 : $C):
1462+
%1 = ref_element_addr %0, #C.prop
1463+
%2 = alloc_stack $Int32
1464+
%3 = builtin "zeroInitializer"(%2 : $*Int32) : $()
1465+
%4 = builtin "prepareInitialization"(%2 : $*Int32) : $()
1466+
dealloc_stack %2
1467+
%6 = tuple ()
1468+
return %6
1469+
}
1470+
14381471
// CHECK-LABEL: @test_stored_pointer
14391472
// CHECK: PAIR #2.
14401473
// CHECK-NEXT: %5 = apply %4(%2) : $@convention(thin) (@in Builtin.RawPointer) -> ()

0 commit comments

Comments
 (0)