Repository navigation
Read the limit of a C loop on every pass when the loop counts down - #518
Open
j-modernc-org wants to merge 1 commit into
Open
j-modernc-org wants to merge 1 commit into
j-modernc-org wants to merge 1 commit into
Conversation
CheckSimpleIncrementLoop rewrites `for (i = 0; i < n; i++)`, with i not used in the body, as a count down from n. That reads n once, before the loop. In C the limit is read on every pass, so a local the body changes, a global, and a global a called function changes all ran the count they had when the loop began (totalspectrum/flexprop#119). For C, the rewrite now requires a limit that cannot change in the loop: a constant, or a local whose address is not taken and which the body does not modify. Spin and BASIC loops are rewritten as before. Test/cexec07.c prints 5 5 5 5 on master and 3 3 3 5 with this change, as gcc does.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the fix suggested in totalspectrum/flexprop#119, limited to C.
CheckSimpleIncrementLoop rewrites
for (i = 0; i < n; i++), withinot used in the body, as a count down fromn. That readsnonce, before the loop. In C the limit is read on every pass, so a local the body changes, a global, and a global that a called function changes all ran the count they had when the loop began.For C, the rewrite now requires a limit that cannot change in the loop: a constant, or a local whose address is not taken and which the body does not modify, or a sum or difference of those. A loop whose limit is such a local is still counted down. Spin and BASIC loops are rewritten as before, since reading the limit once may be what those languages mean.
Test/cexec07.c has the three loops from the issue and one whose limit the body leaves alone. It prints
5 5 5 5on master and3 3 3 5with this change, as gcc does.Checked, with flexspin built from this branch (master b5993a8 plus this commit):
make test_offline: the same tests pass as on master.Test/runtests_p2.shruns, built and compared as it does, on a P2-EDGE: all 29 pass, cexec07 included.