1 /*
   2  * Copyright (c) 2016, 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 /**
  26  * @test
  27  * @bug 6869327
  28  * @summary Test that C2 flag UseCountedLoopSafepoints ensures a safepoint is kept in a CountedLoop
  29  * @library /test/lib /
  30  * @requires vm.compMode != "Xint" & vm.flavor == "server" & (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel == 4) & vm.debug == true
  31  * @requires !vm.emulatedClient
  32  * @modules java.base/jdk.internal.misc
  33  * @build sun.hotspot.WhiteBox
  34  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  35  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  36  * @run driver compiler.loopopts.UseCountedLoopSafepointsTest
  37  */
  38 
  39 package compiler.loopopts;
  40 
  41 import jdk.test.lib.Platform;
  42 import jdk.test.lib.process.ProcessTools;
  43 import jdk.test.lib.process.OutputAnalyzer;
  44 import java.util.List;
  45 import java.util.ArrayList;
  46 import java.util.Arrays;
  47 import java.util.Collections;
  48 import jdk.test.lib.Asserts;
  49 
  50 /* Idea of this test is to check if ideal graph has CountedLoopEnd->SafePoint edge in case
  51    of UseCountedLoopSafepoint enabled and has no such edge in case it's disabled. Restricting
  52    compilation to testMethod only will leave only one counted loop (the one in testedMethod) */
  53 public class UseCountedLoopSafepointsTest {
  54 
  55     public static void main (String args[]) {
  56         check(true); // check ideal graph with UseCountedLoopSafepoint enabled
  57         check(false); // ... and disabled
  58     }
  59 
  60     private static void check(boolean enabled) {
  61         OutputAnalyzer oa;
  62         try {
  63             oa = ProcessTools.executeTestJvm("-XX:+UnlockDiagnosticVMOptions", "-Xbootclasspath/a:.",
  64                                              "-XX:" + (enabled ? "+" : "-") + "UseCountedLoopSafepoints",
  65                                              "-XX:LoopStripMiningIter=" + (enabled ? "1" : "0"), "-XX:+WhiteBoxAPI",
  66                     "-XX:-Inline", "-Xbatch", "-XX:+PrintIdeal", "-XX:LoopUnrollLimit=0",
  67                     "-XX:CompileOnly=" + UseCountedLoopSafepoints.class.getName() + "::testMethod",
  68                     UseCountedLoopSafepoints.class.getName());
  69         } catch (Exception e) {
  70             throw new Error("Exception launching child for case enabled=" + enabled + " : " + e, e);
  71         }
  72         oa.shouldHaveExitValue(0);
  73         // parse output in seach of SafePoint and CountedLoopEnd nodes
  74         List<Node> safePoints = new ArrayList<>();
  75         List<Node> loopEnds = new ArrayList<>();
  76         for (String line : oa.getOutput().split("\\n")) {
  77             int separatorIndex = line.indexOf("\t===");
  78             if (separatorIndex > -1) {
  79                 String header = line.substring(0, separatorIndex);
  80                 if (header.endsWith("\tSafePoint")) {
  81                     safePoints.add(new Node("SafePoint", line));
  82                 } else if (header.endsWith("\tCountedLoopEnd")) {
  83                     loopEnds.add(new Node("CountedLoopEnd", line));
  84                 }
  85             }
  86         }
  87         // now, find CountedLoopEnd -> SafePoint edge
  88         boolean found = false;
  89         for (Node loopEnd : loopEnds) {
  90             found |= loopEnd.to.stream()
  91                                  .filter(id -> nodeListHasElementWithId(safePoints, id))
  92                                  .findAny()
  93                                  .isPresent();
  94         }
  95         Asserts.assertEQ(enabled, found, "Safepoint " + (found ? "" : "not ") + "found");
  96     }
  97 
  98     private static boolean nodeListHasElementWithId(List<Node> list, int id) {
  99         return list.stream()
 100                    .filter(node -> node.id == id)
 101                    .findAny()
 102                    .isPresent();
 103     }
 104 
 105     private static class Node {
 106         public final int id;
 107         public final List<Integer> from;
 108         public final List<Integer> to;
 109 
 110         public Node(String name, String str) {
 111             List<Integer> tmpFrom = new ArrayList<>();
 112             List<Integer> tmpTo = new ArrayList<>();
 113             // parse string like: " $id    $name       ===  $to1 $to2 ...   [[ $from1 $from2 ... ]] $anything"
 114             // example:  318    SafePoint       ===  317  1  304  1  1  10  308  [[ 97  74 ]]  ...
 115             id = Integer.parseInt(str.substring(1, str.indexOf(name)).trim());
 116             Arrays.stream(str.substring(str.indexOf("===") + 4, str.indexOf("[[")).trim().split("\\s+"))
 117                   .map(Integer::parseInt)
 118                   .forEach(tmpTo::add);
 119             Arrays.stream(str.substring(str.indexOf("[[") + 3, str.indexOf("]]")).trim().split("\\s+"))
 120                   .map(Integer::parseInt)
 121                   .forEach(tmpFrom::add);
 122             this.from = Collections.unmodifiableList(tmpFrom);
 123             this.to = Collections.unmodifiableList(tmpTo);
 124         }
 125     }
 126 }