Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-15147] Improve elision of integer overflow checks #57473

Open
Lukasa opened this issue Sep 3, 2021 · 2 comments
Open

[SR-15147] Improve elision of integer overflow checks #57473

Lukasa opened this issue Sep 3, 2021 · 2 comments

Comments

@Lukasa
Copy link
Contributor

@Lukasa Lukasa commented Sep 3, 2021

Previous ID SR-15147
Radar rdar://problem/82716181
Original Reporter @Lukasa
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, Performance
Assignee None
Priority Medium

md5: 476598b20bd5b80905c31e6907cb796c

Issue Description:

Consider the following code:

func countTrue(_ elements: [Bool]) -> Int {
    var count = 0

    for element in elements {
        if element {
            count += 1
        }
    }

    return count
}

By simple code inspection we can observe that the addition in count += 1 cannot possibly overflow: the maximum possible value of count is elements.count, and that is stored in an Int, so we cannot possibly exceed that value.

It seems as though it might be useful to have an optimisation pass to observe these kinds of changes. The branches for overflow detection don't really incur a cost in performance in-and-of-themselves, but they either cause increased code size (when using -Osize) or inhibit vectorisation (when using -O).

@Lukasa
Copy link
Contributor Author

@Lukasa Lukasa commented Sep 3, 2021

@swift-ci create

@eeckstein
Copy link
Member

@eeckstein eeckstein commented Sep 9, 2021

This is something which is best done on LLVM level

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants