aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/gcc.dg/tree-ssa/pr21029.c
blob: de2595aee55cc75310102c8efb51f8577acb00d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* { dg-do run } */
/* { dg-options "-O2 -fwrapv" } */

/* PR tree-optimization/21029

   f() used to get optimized to an infinite loop by tree-vrp, because
   j is assumed to be non-negative.  Even though the conversion from
   unsigned to signed has unspecified results if the expression value
   is not representable in the signed type, the compiler itself (e.g.,
   the Ada front end) depends on wrap-around behavior.  */

unsigned int f(void) {
  unsigned char i = 123;
  signed char j;

  do
    if ((j = (signed char) i) < 0)
      break;
    else
      i++;
  while (1);

  return i;
}

/* Now let's torture it a bit further.  Narrowing conversions need
   similar treatment.  */

unsigned int f1 (void) {
  unsigned short i = 123;
  signed char j;

  do
    if ((j = (signed char) i) < 0)
      break;
    else
      i++;
  while (1);

  return i;
}

/* And so do widening conversions.  */

unsigned int f2 (void) {
  unsigned char i = 123;
  signed short j;

  do
    if ((j = (signed short) (signed char) i) < 0)
      break;
    else
      i++;
  while (1);

  return i;
}

/* Check same-sign truncations with an increment that turns into
   decrements.  */

unsigned int f3 (void) {
  signed short i = 5;
  signed char j;

  do
    if ((j = (signed char) i) < 0)
      break;
    else
      i += 255;
  while (1);

  return i;
}

/* Check that the truncation above doesn't confuse the result of the
   test after a widening conversion.  */

unsigned int f4 (void) {
  signed short i = -123;
  signed int j;

  do
    if ((j = (signed int) (signed char) i) > 0)
      break;
    else
      i += 255;
  while (1);

  return i;
}

/* Even if we omit the widening truncation, the narrowing truncation
   is implementation-defined.  */

unsigned int f5 (void) {
  signed long i = -123;
  signed char j;

  do
    if ((j = (signed char) i) > 0)
      break;
    else
      i += 255;
  while (1);

  return i;
}

int main (void) {
  f ();
  f1 ();
  f2 ();
  f3 ();
  f4 ();
  f5 ();
  return 0;
}