Skip to content

Commit 129e1a8

Browse files
Combine new calculated ranges with existing range.
When a range is recalculated, retain what was previously known as IL changes can produce different results from un-executed code. This also paves the way for external injection of ranges. gcc/ PR tree-optimization/97737 PR tree-optimization/97741 * gimple-range.cc: (gimple_ranger::range_of_stmt): Intersect newly calculated ranges with the existing known global range. gcc/testsuite/ * gcc.dg/pr97737.c: New. * gcc.dg/pr97741.c: New.
1 parent 25126a2 commit 129e1a8

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

gcc/gimple-range.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,14 @@ gimple_ranger::range_of_stmt (irange &r, gimple *s, tree name)
10261026
if (m_cache.get_non_stale_global_range (r, name))
10271027
return true;
10281028

1029-
// Otherwise calculate a new value and save it.
1030-
calc_stmt (r, s, name);
1029+
// Otherwise calculate a new value.
1030+
int_range_max tmp;
1031+
calc_stmt (tmp, s, name);
1032+
1033+
// Combine the new value with the old value. This is required because
1034+
// the way value propagation works, when the IL changes on the fly we
1035+
// can sometimes get different results. See PR 97741.
1036+
r.intersect (tmp);
10311037
m_cache.set_global_range (name, r);
10321038
return true;
10331039
}

gcc/testsuite/gcc.dg/pr97737.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-O2" } */
3+
4+
int a = 1, b, c;
5+
6+
void d() {
7+
int e = 1;
8+
L1:
9+
b = e;
10+
L2:
11+
e = e / a;
12+
if (!(e || c || e - 1))
13+
goto L1;
14+
if (!b)
15+
goto L2;
16+
}

gcc/testsuite/gcc.dg/pr97741.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-Wall -Wextra -fno-strict-aliasing -fwrapv -Os -fno-toplevel-reorder -fno-tree-ccp -fno-tree-fre" } */
3+
4+
short a = 0;
5+
long b = 0;
6+
char c = 0;
7+
void d() {
8+
int e = 0;
9+
f:
10+
for (a = 6; a;)
11+
c = e;
12+
e = 0;
13+
for (; e == 20; ++e)
14+
for (; b;)
15+
goto f;
16+
}
17+
int main() { return 0; }

0 commit comments

Comments
 (0)