Skip to content

C++: Divide number of bounds between branches for phi nodes#21329

Open
paldepind wants to merge 3 commits intogithub:mainfrom
paldepind:cpp/simple-range-analysis-phi-divide
Open

C++: Divide number of bounds between branches for phi nodes#21329
paldepind wants to merge 3 commits intogithub:mainfrom
paldepind:cpp/simple-range-analysis-phi-divide

Conversation

@paldepind
Copy link
Contributor

@paldepind paldepind commented Feb 16, 2026

This PR changes the heuristic for how the number of bounds is calculated for a guard phi node.

This is an improvement in a few ways:

  • It avoids a special case where nodes that are both normal phi nodes and guard phi nodes needed special treatment.

  • I think this heuristic makes sense intuitively.

  • It fixes a problem where a series of if-else statements with guards on the same variable caused the number of bounds to exponentially increase while the actual range analysis did in fact not introduce any bounds.

    The first commit adds an example of this. In the last commit the change to the expected file around test.c:453 to test.c:463 show how we now handle this case better.

    This fixes a recent problem observed over in coding standards. See: Revert "C++: Accept test changes after github/codeql#21313." codeql-coding-standards#1041.

For reviewing the comment inside nrOfBoundsPhiGuard should help explain what is going on.

@github-actions github-actions bot added the C++ label Feb 16, 2026
@paldepind paldepind force-pushed the cpp/simple-range-analysis-phi-divide branch from f041b27 to e5c8f38 Compare February 16, 2026 13:23
@paldepind paldepind force-pushed the cpp/simple-range-analysis-phi-divide branch from e5c8f38 to d0681c6 Compare February 16, 2026 13:36
@paldepind paldepind marked this pull request as ready for review February 16, 2026 13:46
Copilot AI review requested due to automatic review settings February 16, 2026 13:46
@paldepind paldepind requested a review from a team as a code owner February 16, 2026 13:46
@paldepind paldepind requested a review from MathiasVP February 16, 2026 13:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the heuristic for calculating the number of bounds for guard phi nodes in C++ range analysis. The change addresses an exponential growth issue that occurred when analyzing series of if-else statements with guards on the same variable.

Changes:

  • Modified nrOfBoundsPhiGuard function to use a new heuristic: (varBounds + 1) / 2 instead of special-casing certain phi nodes
  • Added test case repeated_if_else_statements demonstrating the fixed scenario with 11 consecutive if-else statements
  • Added helper predicates countNrOfLowerBounds and countNrOfUpperBounds in SimpleRangeAnalysisInternal module
  • Updated test query nrOfBounds.ql to output actual bounds counts alongside estimates

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated no comments.

File Description
cpp/ql/lib/semmle/code/cpp/rangeanalysis/SimpleRangeAnalysis.qll Changed bounds estimation heuristic in nrOfBoundsPhiGuard from special-casing to dividing by 2; added helper predicates for counting actual bounds
cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/test.c Added repeated_if_else_statements function demonstrating the exponential growth scenario that is now fixed
cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.ql Enhanced query to also output actual lower and upper bounds counts for validation
cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/*.expected Updated expected test outputs to reflect the improved bounds estimates (line numbers shifted due to new test case)

@paldepind paldepind added the no-change-note-required This PR does not need a change note label Feb 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant