1  /* PR tree-optimization/61839.  */
       2  /* { dg-do compile } */
       3  /* { dg-options "-O2 -fdump-tree-evrp" } */
       4  /* { dg-require-effective-target int32plus } */
       5  
       6  __attribute__ ((noinline))
       7  int foo ()
       8  {
       9    int a = -1;
      10    volatile unsigned b = 1U;
      11    int c = 1;
      12    c = (a + 972195718) / (b ? 2 : 0);
      13    if (c == 486097858)
      14      ;
      15    else
      16      __builtin_abort ();
      17    return 0;
      18  }
      19  
      20  __attribute__ ((noinline))
      21  int bar ()
      22  {
      23    int a = -1;
      24    volatile unsigned b = 1U;
      25    int c = 1;
      26    c = (a + 972195718) % (b ? 2 : 0);
      27    if (c == 1)
      28      ;
      29    else
      30      __builtin_abort ();
      31    return 0;
      32  }
      33  
      34  __attribute__ ((noinline))
      35  int bar2 ()
      36  {
      37    int a = -1;
      38    volatile unsigned b = 1U;
      39    int c = 1;
      40    c = (a + 972195716) % (b ? 1 : 2);
      41    if (c == 0)
      42      ;
      43    else
      44      __builtin_abort ();
      45    return 0;
      46  }
      47  
      48  /* Ensure we are folding modulus sub-ranges properly.  */
      49  __attribute__ ((noinline))
      50  int mod (int a, int b)
      51  {
      52    int v1, v2;
      53    v1 = (a < 10) ? 12 : 24;
      54    v2 = (b > 20) ? 3 : 6;
      55  
      56    if (a > 20)
      57      v1 = v1 * 2;
      58    if (b > 20)
      59      v2 = v2 * 2;
      60  
      61    if (a == b)
      62      v2 = 0;
      63      
      64    /* v1 == 12, 24, or 48.  v2 == 0, 3, 6, or 12. */
      65    int c = v1 % v2;
      66    if (c == 0)
      67      ;
      68    else
      69      __builtin_abort ();
      70    return 0;
      71  }
      72  
      73  /* EVRP now makes transformations in all functions, leaving a single
      74   * builtin_abort call in bar2. */
      75  /* { dg-final { scan-tree-dump-times "__builtin_abort" 1 "evrp" } } */
      76  
      77  /* Make sure to optimize 972195717 / 0 in function foo.  */
      78  /* { dg-final { scan-tree-dump-times "972195717 / " 0  "evrp" } } */
      79  /* Make sure  to optimize 972195717 % 0 in function bar.  */
      80  /* { dg-final { scan-tree-dump-times "972195717 % " 0 "evrp" } } */
      81  /* Make sure to optimize 972195717 % [1,2] function bar2.  */
      82  /* { dg-final { scan-tree-dump-times "972195715 % " 0 "evrp" } } */
      83  /* [12,12][24,24][48,48] % [0,0][3,3][6,6][12,12] == [0,0] */
      84  /* { dg-final { scan-tree-dump-times "%" 0 "evrp" } } */