Skip to content

Commit 0844b72

Browse files
authored
Merge pull request swiftlang#35475 from gottesmm/ossa-sil-combine-more-simple
[sil-combine] More simple OSSA updates
2 parents 160c5c8 + f823b24 commit 0844b72

File tree

2 files changed

+56
-66
lines changed

2 files changed

+56
-66
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -937,9 +937,6 @@ SILInstruction *SILCombiner::visitLoadInst(LoadInst *LI) {
937937
/// ->
938938
/// %2 = index_addr %ptr, x+y
939939
SILInstruction *SILCombiner::visitIndexAddrInst(IndexAddrInst *IA) {
940-
if (IA->getFunction()->hasOwnership())
941-
return nullptr;
942-
943940
unsigned index = 0;
944941
SILValue base = isConstIndexAddr(IA, index);
945942
if (!base)
@@ -1069,9 +1066,6 @@ SILInstruction *SILCombiner::visitRetainValueInst(RetainValueInst *RVI) {
10691066
}
10701067

10711068
SILInstruction *SILCombiner::visitCondFailInst(CondFailInst *CFI) {
1072-
if (CFI->getFunction()->hasOwnership())
1073-
return nullptr;
1074-
10751069
// Remove runtime asserts such as overflow checks and bounds checks.
10761070
if (RemoveCondFails)
10771071
return eraseInstFromFunction(*CFI);
@@ -1881,19 +1875,16 @@ SILInstruction *SILCombiner::visitSelectEnumInst(SelectEnumInst *SEI) {
18811875
}
18821876

18831877
SILInstruction *SILCombiner::visitTupleExtractInst(TupleExtractInst *TEI) {
1884-
if (TEI->getFunction()->hasOwnership())
1885-
return nullptr;
1886-
18871878
// tuple_extract(apply([add|sub|...]overflow(x, 0)), 1) -> 0
18881879
// if it can be proven that no overflow can happen.
1889-
if (TEI->getFieldIndex() != 1)
1890-
return nullptr;
1880+
if (TEI->getFieldIndex() == 1) {
1881+
Builder.setCurrentDebugScope(TEI->getDebugScope());
1882+
if (auto *BI = dyn_cast<BuiltinInst>(TEI->getOperand()))
1883+
if (!canOverflow(BI))
1884+
return Builder.createIntegerLiteral(TEI->getLoc(), TEI->getType(),
1885+
APInt(1, 0));
1886+
}
18911887

1892-
Builder.setCurrentDebugScope(TEI->getDebugScope());
1893-
if (auto *BI = dyn_cast<BuiltinInst>(TEI->getOperand()))
1894-
if (!canOverflow(BI))
1895-
return Builder.createIntegerLiteral(TEI->getLoc(), TEI->getType(),
1896-
APInt(1, 0));
18971888
return nullptr;
18981889
}
18991890

@@ -2036,26 +2027,20 @@ SILInstruction *SILCombiner::visitMarkDependenceInst(MarkDependenceInst *mdi) {
20362027
return nullptr;
20372028
}
20382029

2039-
2040-
SILInstruction *SILCombiner::
2041-
visitClassifyBridgeObjectInst(ClassifyBridgeObjectInst *CBOI) {
2042-
if (CBOI->getFunction()->hasOwnership())
2043-
return nullptr;
2044-
2045-
auto *URC = dyn_cast<UncheckedRefCastInst>(CBOI->getOperand());
2046-
if (!URC)
2030+
SILInstruction *
2031+
SILCombiner::visitClassifyBridgeObjectInst(ClassifyBridgeObjectInst *cboi) {
2032+
auto *urc = dyn_cast<UncheckedRefCastInst>(cboi->getOperand());
2033+
if (!urc)
20472034
return nullptr;
20482035

2049-
auto type = URC->getOperand()->getType().getASTType();
2036+
auto type = urc->getOperand()->getType().getASTType();
20502037
if (ClassDecl *cd = type->getClassOrBoundGenericClass()) {
20512038
if (!cd->isObjC()) {
20522039
auto int1Ty = SILType::getBuiltinIntegerType(1, Builder.getASTContext());
2053-
SILValue zero = Builder.createIntegerLiteral(CBOI->getLoc(),
2054-
int1Ty, 0);
2055-
return Builder.createTuple(CBOI->getLoc(), { zero, zero });
2040+
SILValue zero = Builder.createIntegerLiteral(cboi->getLoc(), int1Ty, 0);
2041+
return Builder.createTuple(cboi->getLoc(), {zero, zero});
20562042
}
20572043
}
20582044

20592045
return nullptr;
20602046
}
2061-

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ bb0:
410410
}
411411

412412
// CHECK-LABEL: sil [ossa] @optimize_nested_index_addr
413-
// XHECK: bb0(%0 : $Builtin.RawPointer):
414-
// XHECK-NEXT: %1 = pointer_to_address %0
415-
// XHECK-NEXT: %2 = integer_literal $Builtin.Word, 10
416-
// XHECK-NEXT: %3 = index_addr %1 : $*UInt8, %2
417-
// XHECK-NEXT: %4 = address_to_pointer %3
418-
// XHECK-NEXT: return %4
413+
// CHECK: bb0(%0 : $Builtin.RawPointer):
414+
// CHECK-NEXT: %1 = pointer_to_address %0
415+
// CHECK-NEXT: %2 = integer_literal $Builtin.Word, 10
416+
// CHECK-NEXT: %3 = index_addr %1 : $*UInt8, %2
417+
// CHECK-NEXT: %4 = address_to_pointer %3
418+
// CHECK-NEXT: return %4
419419
sil [ossa] @optimize_nested_index_addr : $@convention(thin) (Builtin.RawPointer) -> Builtin.RawPointer {
420420
bb0(%0 : $Builtin.RawPointer):
421421
%1 = integer_literal $Builtin.Word, 3
@@ -528,11 +528,12 @@ bb0(%0 : @owned $HeapBufferStorage<T_0_0, T_0_1>):
528528
return %3 : $HeapBufferStorage<T_0_0, T_0_1>
529529
}
530530

531-
// CHECK-LABEL: sil [ossa] @cond_fail_applied_to_zero_removal
532-
// XHECK: bb0
533-
// XHECK-NEXT: cond_fail
534-
// XHECK-NEXT: tuple ()
535-
// XHECK-NEXT: return
531+
// CHECK-LABEL: sil [ossa] @cond_fail_applied_to_zero_removal :
532+
// CHECK: bb0
533+
// CHECK-NEXT: cond_fail
534+
// CHECK-NEXT: tuple ()
535+
// CHECK-NEXT: return
536+
// CHECK: } // end sil function 'cond_fail_applied_to_zero_removal'
536537
sil [ossa] @cond_fail_applied_to_zero_removal : $@convention(thin) (Builtin.Int1) -> () {
537538
bb0(%0 : $Builtin.Int1):
538539
cond_fail %0 : $Builtin.Int1
@@ -2051,10 +2052,11 @@ bb0(%0 : $Int):
20512052
return %8 : $()
20522053
}
20532054

2054-
// CHECK-LABEL: @remove_pointer_compare_to_zero_NE
2055-
// XHECK-NOT: apply
2056-
// XHECK: cond_fail
2057-
// XHECK: unreachable
2055+
// CHECK-LABEL: @remove_pointer_compare_to_zero_NE :
2056+
// CHECK-NOT: apply
2057+
// CHECK: cond_fail
2058+
// CHECK: unreachable
2059+
// CHECK: } // end sil function 'remove_pointer_compare_to_zero_NE'
20582060
sil [ossa] @remove_pointer_compare_to_zero_NE : $@convention(thin) (Int) -> () {
20592061
bb0(%0 : $Int):
20602062
%1 = string_literal utf8 "ss"
@@ -2701,11 +2703,11 @@ bb0(%0 : $Builtin.RawPointer, %1 : $*Builtin.UnsafeValueBuffer, %2 : $*GenContai
27012703
}
27022704

27032705

2704-
// CHECK-LABEL: sil [ossa] @remove_dead_code_after_cond_fail
2705-
// XHECK: [[Ref:%.*]] = integer_literal $Builtin.Int1, -1
2706-
// XHECK-NEXT: cond_fail [[Ref]]
2707-
// XHECK-NEXT: unreachable
2708-
2706+
// CHECK-LABEL: sil [ossa] @remove_dead_code_after_cond_fail :
2707+
// CHECK: [[Ref:%.*]] = integer_literal $Builtin.Int1, -1
2708+
// CHECK-NEXT: cond_fail [[Ref]]
2709+
// CHECK-NEXT: unreachable
2710+
// CHECK: } // end sil function 'remove_dead_code_after_cond_fail'
27092711
sil [ossa] @remove_dead_code_after_cond_fail : $@convention(thin) () -> (Int32) {
27102712
bb0:
27112713
%0 = integer_literal $Builtin.Int32, -2
@@ -2717,12 +2719,12 @@ bb0:
27172719

27182720

27192721
// CHECK-LABEL: sil [ossa] @dont_remove_code_after_cond_fail
2720-
// XHECK: bb0([[Cond:%.*]] : $Builtin.Int1):
2721-
// XHECK-NEXT: [[Ref:%.*]] = integer_literal $Builtin.Int32, -2
2722-
// XHECK-NEXT: cond_fail [[Cond]]
2723-
// XHECK-NEXT: [[Ret:%.*]] = struct $Int32 ([[Ref]] : $Builtin.Int32)
2724-
// XHECK-NEXT: return [[Ret]]
2725-
2722+
// CHECK: bb0([[Cond:%.*]] : $Builtin.Int1):
2723+
// CHECK-NEXT: [[Ref:%.*]] = integer_literal $Builtin.Int32, -2
2724+
// CHECK-NEXT: cond_fail [[Cond]]
2725+
// CHECK-NEXT: [[Ret:%.*]] = struct $Int32 ([[Ref]] : $Builtin.Int32)
2726+
// CHECK-NEXT: return [[Ret]]
2727+
// CHECK: } // end sil function 'dont_remove_code_after_cond_fail'
27262728
sil [ossa] @dont_remove_code_after_cond_fail : $@convention(thin) (Builtin.Int1) -> (Int32) {
27272729
bb0(%0 : $Builtin.Int1):
27282730
%1 = integer_literal $Builtin.Int32, -2
@@ -4078,11 +4080,12 @@ bb0(%0 : $*Builtin.Int64, %1 : @owned $B):
40784080

40794081
// Test to see if we can constant fold a classify_bridge_object on a Swift class
40804082
// to false.
4081-
// CHECK-LABEL: sil [ossa] @test_classify_fold
4082-
// XHECK-NOT: classify_bridge_object
4083-
// XHECK: integer_literal $Builtin.Int1, 0
4084-
// XHECK-NEXT: destroy_value
4085-
// XHECK-NEXT: return
4083+
// CHECK-LABEL: sil [ossa] @test_classify_fold :
4084+
// CHECK-NOT: classify_bridge_object
4085+
// CHECK: destroy_value
4086+
// CHECK: [[LITERAL:%.*]] = integer_literal $Builtin.Int1, 0
4087+
// CHECK: return [[LITERAL]]
4088+
// CHECK: } // end sil function 'test_classify_fold'
40864089
sil [ossa] @test_classify_fold : $@convention(thin) (@owned _ContiguousArrayStorage<B>) -> Builtin.Int1 {
40874090
bb0(%0 : @owned $_ContiguousArrayStorage<B>):
40884091
%1 = unchecked_ref_cast %0 : $_ContiguousArrayStorage<B> to $Builtin.BridgeObject
@@ -4092,10 +4095,11 @@ bb0(%0 : @owned $_ContiguousArrayStorage<B>):
40924095
return %3 : $Builtin.Int1
40934096
}
40944097

4095-
// CHECK-LABEL: sil [ossa] @bridge_cast_noop_fold
4096-
// XHECK: bb0(%0
4097-
// XHECK-NEXT: %1 = upcast %0
4098-
// XHECK-NEXT: return %1
4098+
// CHECK-LABEL: sil [ossa] @bridge_cast_noop_fold :
4099+
// CHECK: bb0(%0
4100+
// CHECK-NEXT: %1 = upcast %0
4101+
// CHECK-NEXT: return %1
4102+
// CHECK: } // end sil function 'bridge_cast_noop_fold'
40994103
sil [ossa] @bridge_cast_noop_fold : $@convention(thin) (@owned _ContiguousArrayStorage<B>) -> @owned __ContiguousArrayStorageBase {
41004104
bb0(%0 : @owned $_ContiguousArrayStorage<B>):
41014105
%1 = unchecked_ref_cast %0 : $_ContiguousArrayStorage<B> to $Builtin.BridgeObject
@@ -4178,8 +4182,9 @@ bb0:
41784182
}
41794183

41804184
// CHECK-LABEL: sil [ossa] @conversion_to_and_from_bridge_object
4181-
// XHECK: bb0(%0 : $Builtin.Int64):
4182-
// XHECK-NEXT: return %0
4185+
// CHECK: bb0(%0 : $Builtin.Int64):
4186+
// CHECK-NEXT: return %0
4187+
// CHECK: } // end sil function 'conversion_to_and_from_bridge_object'
41834188
sil [ossa] @conversion_to_and_from_bridge_object : $@convention(thin) (Builtin.Int64) -> (Builtin.Int64) {
41844189
bb0(%0 : $Builtin.Int64):
41854190
%1 = struct $UInt64 (%0 : $Builtin.Int64)

0 commit comments

Comments
 (0)