Bug 126745 - a/b -> 1 if it is known that `a==b` in VRP without DOM
Summary: a/b -> 1 if it is known that `a==b` in VRP without DOM
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 17.0
: P3 enhancement
Target Milestone: 17.0
Assignee: Not yet assigned to anyone
URL:
Keywords: easyhack, missed-optimization
Depends on:
Blocks: VRP 126010
  Show dependency treegraph
 
Reported: 2026-08-09 00:53 UTC by Drea Pinski
Modified: 2026-08-24 20:30 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Drea Pinski 2026-08-09 00:53:05 UTC
Take:
```
int f(int a, int b)
{
    if (a != b) __builtin_unreachable();
    return a/b;
}
```

This only is optimized in dom. If we are going to remove dom, then it is best if the ranger optimizes it.


We need a op1_op2_relation_effect for the division operators.
Comment 1 Drea Pinski 2026-08-09 00:55:09 UTC
Need to handle % too.

That is:
```
int f1(int a, int b)
{
    if (a != b) __builtin_unreachable();
    return a%b;
}
```
is 0.
Comment 2 Drea Pinski 2026-08-10 04:23:16 UTC
For a hint on this one see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126743#c1 also.
Comment 3 GCC Commits 2026-08-24 18:30:12 UTC
The master branch has been updated by Daniel Barboza <dbarboza@gcc.gnu.org>:

https://gcc.gnu.org/g:7c25b09da5841ce372415a97c557509a8a0bb468

commit r17-3610-g7c25b09da5841ce372415a97c557509a8a0bb468
Author: Daniel Barboza <daniel.barboza@oss.qualcomm.com>
Date:   Mon Aug 17 11:17:39 2026 -0300

    range-op.cc: add VREL_EQ relation effect for div and mod [PR126745]
    
    For res = a / b and a == b, set 'res' to one.
    
    For res = a % b and a == b, set 'res' to zero.
    
    Both are done by dom2/dom3 already.  With this change we're now
    doing it earlier (with -O2) in evrp.
    
    Bootstrapped and regtested in x86_64.
    
            PR tree-optimization/126745
    
    gcc/ChangeLog:
    
            * range-op.cc (operator_div::op1_op2_relation_effect): add
            VREL_EQ range equal one for op1/op2 and op1%op2 if op1 == op2.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr126745.c: New test.
Comment 4 Drea Pinski 2026-08-24 20:30:46 UTC
Fixed.