Skip to content

Commit 68a0dcc

Browse files
committed
Add file-check tests for bounds-check optimizations on Span
And apply the unsigned-compare optimization one other place that I missed the first time, as revealed by the new tests.
1 parent ac2f81f commit 68a0dcc

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===--- BoundsCheckOptimization.swift ------------------------*- swift -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// RUN: %target-swift-frontend -primary-file %s -O -emit-assembly | %FileCheck %s --check-prefix CHECK --check-prefix CHECK-%target-cpu
14+
15+
import Swift
16+
17+
func read(index: Int, span: Span<UInt8>) -> UInt8 {
18+
span[index]
19+
}
20+
// CHECK: s22BoundsCheckElimination4read5index4spans5UInt8VSi_s4SpanVyAFGtF:
21+
22+
// CHECK-arm64-NOT: tbnz
23+
// CHECK-arm64: cmp
24+
// all unsigned comparison spellings?
25+
// CHECK-arm64-NEXT: b.{{hs|hi|ls|lo|cc|cs}}
26+
// CHECK-arm64-NEXT: ldrb
27+
// CHECK-arm64-NEXT: ret
28+
29+
// CHECK-x86_64-NOT: test
30+
// CHECK-x86_64: cmp
31+
// all unsigned comparison spellings?
32+
// CHECK-x86_64-NEXT: j{{a|ae|b|be|c|na|nae|nb|nbe|nc}}
33+
// CHECK-x86_64-NEXT: movzb
34+
// x86_64 might have a frame pointer operation before ret
35+
36+
func write(value: UInt8, index: Int, span: inout MutableSpan<UInt8>) {
37+
span[index] = value
38+
}
39+
// CHECK: s22BoundsCheckElimination5write5value5index4spanys5UInt8V_Sis11MutableSpanVyAGGztF:
40+
41+
// CHECK-arm64-NOT: tbnz
42+
// CHECK-arm64: cmp
43+
// all unsigned comparison spellings?
44+
// CHECK-arm64-NEXT: b.{{hs|hi|ls|lo|cc|cs}}
45+
// no second compare
46+
// CHECK-arm64-NOT: cmp
47+
// no test after compare
48+
// CHECK-arm64-NOT: tbnz
49+
// CHECK-arm64: strb
50+
// CHECK-arm64-NEXT: ret
51+
52+
// CHECK-x86_64-NOT: test
53+
// CHECK-x86_64: cmp
54+
// all unsigned comparison spellings?
55+
// CHECK-x86_64-NEXT: j{{a|ae|b|be|c|na|nae|nb|nbe|nc}}
56+
// no second compare
57+
// CHECK-x86_64-NOT: cmp
58+
// no test after compare
59+
// CHECK-x86_64-NOT: test
60+
// CHECK-x86_64: movb
61+
// x86_64 might have a frame pointer operation before ret

0 commit comments

Comments
 (0)