1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8248552
  27  * @summary A Division/modulo node whose zero check was removed is split through an induction variable phi and executed before
  28  *          the loop limit check resulting in a SIGFPE because the divisor is zero.
  29  *
  30  * @run main/othervm -XX:CompileCommand=dontinline,compiler.c2.loopopts.TestSplitThruPhiDivMod::test* compiler.c2.loopopts.TestSplitThruPhiDivMod
  31  */
  32 package compiler.c2.loopopts;
  33 
  34 public class TestSplitThruPhiDivMod {
  35 
  36     int x;
  37 
  38     public int testMod() {
  39         int i1 = 2;
  40         for (int i = 5; i < 25; i++) {
  41             for (int j = 50; j > 1; j -= 2) {
  42                 /*
  43                  * Zero check is removed based on the type of the induction variable phi (variable j) since its always between 1 and 50.
  44                  * However, when splitting the modulo node through the phi, it can be executed right after the subtraction j-2 which can be
  45                  * 0 before evaluation the loop limit condition in the last iteration when j is 2: j-2 = 2-2 = 0. This results in a SIGFPE.
  46                  * The fix is to not split a division or modulo node 'n' through the induction variable phi if the zero check was removed
  47                  * earlier and the new inputs of the clones of 'n' after the split could be zero (i.e. the type of the clones of 'n' include 0).
  48                  */
  49                 x = (20 % j); // Problematic division as part of modulo. Results in a SIGFPE, even though j is always non-zero.
  50                 i1 = (i1 / i);
  51                 for (int k = 3; k > 1; k--) {
  52                     switch ((i % 4) + 22) {
  53                     case 22:
  54                         switch (j % 10) {
  55                         case 83:
  56                             x += 5;
  57                             break;
  58                         }
  59                     }
  60                 }
  61             }
  62         }
  63         return i1;
  64     }
  65 
  66     public int testDiv() {
  67         int i1 = 2;
  68         for (int i = 5; i < 25; i++) {
  69             for (int j = 50; j > 1; j -= 2) {
  70                 // Same issue as above but with a division node. See explanation above.
  71                 x = (20 / j); // Problematic division. Results in a SIGFPE, even though j is always non-zero.
  72                 i1 = (i1 / i);
  73                 for (int k = 3; k > 1; k--) {
  74                     switch ((i % 4) + 22) {
  75                     case 22:
  76                         switch (j % 10) {
  77                         case 83:
  78                             x += 5;
  79                             break;
  80                         }
  81                     }
  82                 }
  83             }
  84         }
  85         return i1;
  86     }
  87 
  88     public static void main(String[] strArr) {
  89         TestSplitThruPhiDivMod t = new TestSplitThruPhiDivMod();
  90         for (int i = 0; i < 10000; i++) {
  91             t.testDiv();
  92             t.testMod();
  93         }
  94     }
  95 }