Y'all wanna see an excessively cute trick LLVM's optimizer can do?
Swift String contains roughly this method:
```
func _fastCStringContents() -> UnsafePointer<UInt8> {
if isASCII {
return contentsPointer
}
return nil
}
```
Where `isASCII` is defined as `(flags & 0x8000_0000_0000_0000) != 0`
Would you expect this to generate (solution in reply)
It generates 0 branches and 0 conditional moves, here's how:
Step 1: Note how the isASCII test checks the sign bit of flags
Step 2: Right-shift flags by 63, replacing every bit with the sign bit
Step 3: You now have a constant with 64 1s or 64 0s. Note x & allOnes == x
, and x & allZeroes == 0
Final result:
<br/>func _fastCStringContents() -> … {<br/> return contentsPointer & (flags >> 63)<br/>}<br/>
See https://github.com/llvm/llvm-project/commit/d545c9056e00988d2d146f8f1440b2dd192f306b