You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the lookup table is filled with a single value (e.g. zero), like in foo(), clang is able to get rid of the second_idx >= MAX_DATA_INDEX branch, because it sees at compile time that the only possible value that second_idx can have is zero, which is less than MAX_DATA_INDEX.
But in bar(), where the lookup table is filled with other values besides 0, clang doesn't see that all possible values of second_idx satisfy second_idx < MAX_DATA_INDEX, even though this is also derivable at compile time. So the second_idx >= MAX_DATA_INDEX branch is also redundant in bar(), and ideally that branch would be deleted.
How hard would it be to add an optimization to clang/LLVM such that the second_idx >= MAX_DATA_INDEX branch in bar() gets pruned?
You could imagine some code that has a bunch of transformation layers through which it passes an initial index (first_idx -> second_idx -> third_idx -> ... -> last_idx). And when moving from one layer to the next, it does bounds checking to verify that the current index is less than MAX_DATA_INDEX. In this case, it seems like this kind of optimization could get rid of a pretty significant amount of effectively dead code. If this were to work with more complex predicates (and not just bounds checking), then the utility would be even greater.
The text was updated successfully, but these errors were encountered:
Context
Consider the following code:
And the assembly that gets generated with clang 19.1.0 (and trunk), with
-O3
enabled:Discussion
When the lookup table is filled with a single value (e.g. zero), like in
foo()
, clang is able to get rid of thesecond_idx >= MAX_DATA_INDEX
branch, because it sees at compile time that the only possible value thatsecond_idx
can have is zero, which is less thanMAX_DATA_INDEX
.But in
bar()
, where the lookup table is filled with other values besides 0, clang doesn't see that all possible values ofsecond_idx
satisfysecond_idx < MAX_DATA_INDEX
, even though this is also derivable at compile time. So thesecond_idx >= MAX_DATA_INDEX
branch is also redundant inbar()
, and ideally that branch would be deleted.How hard would it be to add an optimization to clang/LLVM such that the
second_idx >= MAX_DATA_INDEX
branch inbar()
gets pruned?You could imagine some code that has a bunch of transformation layers through which it passes an initial index (
first_idx -> second_idx -> third_idx -> ... -> last_idx
). And when moving from one layer to the next, it does bounds checking to verify that the current index is less thanMAX_DATA_INDEX
. In this case, it seems like this kind of optimization could get rid of a pretty significant amount of effectively dead code. If this were to work with more complex predicates (and not just bounds checking), then the utility would be even greater.The text was updated successfully, but these errors were encountered: