Skip to content

Recognize dense switch statements involving negative and positive cases. - #598

Open
xavierleroy wants to merge 2 commits into
masterfrom
signed-switch
Open

xavierleroy wants to merge 2 commits into
masterfrom
signed-switch

Conversation

@xavierleroy

Copy link
Copy Markdown
Contributor

Currenty, switch statements with negative and positive cases, such as

switch (n) { case -1: ...  case 0: ... case 1: case 2: case 3: case 4: case 5: ... }

are never recognized as dense and never compiled to a jump table. That's because case labels are treated as unsigned, hence any switch involving negative and positive numbers has a very large span and looks very sparse.

This PR changes the computation of the span of a switch to treat labels as signed numbers. Owing to the joys of modulo arithmetic, very little else needs to be changed: the validation of jump tables is relaxed to allow wrap around at 0, which is semantically safe; the translation of decision trees to CminorSel code is unchanged.

Allow jump tables that wrap around 0, as long as they contain the correct
actions.  This is semantically correct.
…ation of the keys

It is common for dense switches to have both negative and positive keys,
e.g. -1, 0, 1, 2, 3.

This commit use signed min and signed max key values to determine when
to produce a jump table.
@xavierleroy

Copy link
Copy Markdown
Contributor Author

I also considered treating case labels as signed integers in all languages from CompCert C to Cminor, but that's a bigger change and there is no pressing need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant