[PredicateInfo][SCCP] Handle switch comprehensively #165258
Draft
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.
Key change: model constraints of predicate infos as
ConstangtRange, supporting multi-case destinations and default destination of switch.Previously, PredicateInfo and SCCP handled a very simple situation of
switchbranch, i.e., single-case dest.There were still TWO situations pending to handle, i.e., 1) default dest, and 2) multi-cases dest, as follows:
Previously, predicate infos of a renamed operand were only modeled as a
and-chain ofPredicateConstraint { Pred, OtherOp }, which cannot handle complex situations.or-chained, e.g.,x == 0 || x == 2here, which has not been supported.andchain ofne, e.g.,x!=0 && x!=2 && x!=3here. However, such handling would generate lots of intermediate no-opbitcast, especially for those bigswitchs.Although
PredicateConstrantis challenging to model multi-cases/default dest of aswitch,ConstantRangeis fortunately able to do so; Thus, this PR introduces a new way (i.e.,ConstantRange) to model constraints for PredicateInfo.E.g.,
switch -> %multcases(x == 0 || x == 2), we can conservatively derive a constant range[0, 3)via union ofemptyset ∪ {0} ∪ {2}.switch -> %default(x!=0 && x!=2 && x!=3), we can conservatively derive a constant range[1, MAX]via intersection offullset ∩ ~{0} ∩ ~{2} ∩ ~{3}Currently, we only use new constraints of
ConstantRangein SCCP and keep the semantics ofPredicateConstraintunchanged.For each successor of a
switch, we collect all its related case values, and constructPredicateConstraintonly ifNumCases == 1, to keep consistent with previous semantic;ConstantRangefor all situations related toswitch, for SCCP's better optimization.For
assume/branch, we give aConstrantRangeonly ifisa<ConstantInt>(OtherOp)inPredicateConstraint { Pred, OtherOp }.Negligible CompTime Impact: dtcxzyw/llvm-opt-benchmark#2990
Promising Optimization Impact: