swiftlang/swift

[SR-7842] Inconsistent codegen for closed ranges vs bit-manipulation

Open

#50,378 opened on 2018年6月1日

GitHub で見る
 (15 comments) (0 reactions) (0 assignees)Swift (69,989 stars) (10,719 forks)batch import
SILOptimizerbugcompilergood first issueperformancestandard library

説明

Previous ID SR-7842
Radar rdar://problem/40725067
Original Reporter @milseman
Type Bug

Attachment: Download

Votes 0
Component/s Compiler
Labels Bug, Optimizer, Performance, StarterBug
Assignee None
Priority Medium

md5: 223093f3dc3457d4c7f42d8a0e46938f

Issue Description:

ClosedRange<UInt8> demonstrates poor codegen compared to explicit bit manipulation.

@usableFromInline
func _isContinuation_bits(_ x: UInt8) -> Bool {
  return x & 0b1100_0000 == 0b1000_0000
}

@usableFromInline
func _isContinuation_range(_ x: UInt8) -> Bool {
  let continuation: ClosedRange<UInt8> = 0x80...0xBF
  return continuation.contains(x)
}

for i in 0...UInt8.max {
  guard _isContinuation_bits(i) == _isContinuation_range(i) else { fatalError("mismatch") }
}
print("done")

The code generated for _isContinuation_bits is superior:

                     _$S3foo20_isContinuation_bitsySbs5UInt8VF:
000000010000701c         and        w8, w0, #&#8203;0xc0
0000000100007020         cmp        w8, #&#8203;0x80
0000000100007024         cset       w0, eq
0000000100007028         ret
                        ; endp

                     _$S3foo21_isContinuation_rangeySbs5UInt8VF:
000000010000702c         and        w8, w0, #&#8203;0xff
0000000100007030         sxtb       w9, w0
0000000100007034         cmp        w9, #&#8203;0x0
0000000100007038         cset       w9, lt
000000010000703c         cmp        w8, #&#8203;0xc0
0000000100007040         cset       w8, lo
0000000100007044         and        w0, w9, w8
0000000100007048         ret
                        ; endp

コントリビューターガイド