changeset 3190:ce2952ab2028

New optimization phase example.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Thu, 07 Jul 2011 20:46:20 +0200
parents 78b183d482ba
children 3d68684b7161
files graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/gen/LIRGenerator.java graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/graph/IR.java graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/extensions/InliningExample.java graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/extensions/Optimizer.java graal/com.oracle.max.graal.examples/bin/META-INF/services/com.oracle.max.graal.extensions.Optimizer graal/com.oracle.max.graal.examples/src/META-INF/services/com.oracle.max.graal.extensions.Optimizer graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/Main.java graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/inlining/InliningExample.java graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/intrinsics/SafeAdd.java graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/intrinsics/SafeAddExample.java graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/opt/OptimizationExample.java graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/opt/OptimizerImpl.java runexamples.sh runexamplescompare.sh
diffstat 14 files changed, 274 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/gen/LIRGenerator.java	Thu Jul 07 19:58:00 2011 +0200
+++ b/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/gen/LIRGenerator.java	Thu Jul 07 20:46:20 2011 +0200
@@ -1240,7 +1240,7 @@
         }
     }
 
-    protected void arithmeticOpInt(int code, CiValue result, CiValue left, CiValue right, CiValue tmp) {
+    public void arithmeticOpInt(int code, CiValue result, CiValue left, CiValue right, CiValue tmp) {
         CiValue leftOp = left;
 
         if (isTwoOperand && leftOp != result) {
--- a/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/graph/IR.java	Thu Jul 07 19:58:00 2011 +0200
+++ b/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/graph/IR.java	Thu Jul 07 20:46:20 2011 +0200
@@ -31,6 +31,7 @@
 import com.oracle.max.graal.compiler.phases.*;
 import com.oracle.max.graal.compiler.schedule.*;
 import com.oracle.max.graal.compiler.value.*;
+import com.oracle.max.graal.extensions.*;
 import com.oracle.max.graal.graph.*;
 
 /**
@@ -102,6 +103,10 @@
             new DeadCodeEliminationPhase().apply(graph);
         }
 
+        if (GraalOptions.Extend) {
+            extensionOptimizations(graph);
+        }
+
         if (GraalOptions.OptLoops) {
             new LoopPhase().apply(graph);
         }
@@ -198,6 +203,23 @@
 
     }
 
+
+
+    public static ThreadLocal<ServiceLoader<Optimizer>> optimizerLoader = new ThreadLocal<ServiceLoader<Optimizer>>();
+
+    private void extensionOptimizations(Graph graph) {
+
+        ServiceLoader<Optimizer> serviceLoader = optimizerLoader.get();
+        if (serviceLoader == null) {
+            serviceLoader = ServiceLoader.load(Optimizer.class);
+            optimizerLoader.set(serviceLoader);
+        }
+
+        for (Optimizer o : serviceLoader) {
+            o.optimize(compilation.runtime, graph);
+        }
+    }
+
     /**
      * Gets the linear scan ordering of blocks as a list.
      * @return the blocks in linear scan order
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/extensions/InliningExample.java	Thu Jul 07 20:46:20 2011 +0200
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.max.graal.extensions;
+
+
+public class InliningExample {
+
+    public static void run() {
+        System.out.println(test());
+        long start = System.currentTimeMillis();
+        System.out.println(testFib());
+        System.out.println(System.currentTimeMillis() - start);
+    }
+
+    private static int test() {
+        return alwaysInline(30);
+    }
+
+    public static int testFib() {
+        int sum = 0;
+        for (int i = 0; i < 10000000; ++i) {
+            sum += fib(5);
+        }
+        return sum;
+    }
+
+    public static int alwaysInline(int value) {
+        if (value == 0) {
+            return neverInline(value);
+        }
+        return alwaysInline(value - 1);
+    }
+
+    public static int neverInline(int value) {
+        if (value == 0) {
+            return 0;
+        }
+        return neverInline(value - 1);
+    }
+
+    public static int fib(int val) {
+        if (val == 0 || val == 1) {
+            return 1;
+        }
+        return fib(val - 1) + fib(val - 2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/extensions/Optimizer.java	Thu Jul 07 20:46:20 2011 +0200
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.max.graal.extensions;
+
+import com.oracle.max.graal.graph.*;
+import com.sun.cri.ri.*;
+
+
+public interface Optimizer {
+    void optimize(RiRuntime runtime, Graph graph);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.max.graal.examples/bin/META-INF/services/com.oracle.max.graal.extensions.Optimizer	Thu Jul 07 20:46:20 2011 +0200
@@ -0,0 +1,1 @@
+com.oracle.max.graal.examples.opt.OptimizerImpl
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.max.graal.examples/src/META-INF/services/com.oracle.max.graal.extensions.Optimizer	Thu Jul 07 20:46:20 2011 +0200
@@ -0,0 +1,1 @@
+com.oracle.max.graal.examples.opt.OptimizerImpl
\ No newline at end of file
--- a/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/Main.java	Thu Jul 07 19:58:00 2011 +0200
+++ b/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/Main.java	Thu Jul 07 20:46:20 2011 +0200
@@ -22,14 +22,17 @@
  */
 package com.oracle.max.graal.examples;
 
+import com.oracle.max.graal.examples.inlining.*;
 import com.oracle.max.graal.examples.intrinsics.*;
+import com.oracle.max.graal.examples.opt.*;
 
 
 public class Main {
 
     public static void main(String[] args) {
-//        InliningExample.run();
+        InliningExample.run();
         SafeAddExample.run();
+        OptimizationExample.run();
     }
 
 }
--- a/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/inlining/InliningExample.java	Thu Jul 07 19:58:00 2011 +0200
+++ b/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/inlining/InliningExample.java	Thu Jul 07 20:46:20 2011 +0200
@@ -26,10 +26,13 @@
 public class InliningExample {
 
     public static void run() {
-        System.out.println(test());
+        System.out.println();
+        System.out.println();
+        System.out.println("Running Inlining Example");
         long start = System.currentTimeMillis();
-        System.out.println(testFib());
-        System.out.println(System.currentTimeMillis() - start);
+        System.out.println("result1=" + test());
+        System.out.println("result2=" + testFib());
+        System.out.println("time=" + (System.currentTimeMillis() - start) + "ms");
     }
 
     private static int test() {
@@ -38,7 +41,7 @@
 
     public static int testFib() {
         int sum = 0;
-        for (int i = 0; i < 10000000; ++i) {
+        for (int i = 0; i < 100000000; ++i) {
             sum += fib(5);
         }
         return sum;
--- a/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/intrinsics/SafeAdd.java	Thu Jul 07 19:58:00 2011 +0200
+++ b/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/intrinsics/SafeAdd.java	Thu Jul 07 20:46:20 2011 +0200
@@ -31,7 +31,7 @@
 
 public final class SafeAdd extends IntegerArithmetic {
     public SafeAdd(Value x, Value y, Graph graph) {
-        super(CiKind.Long, Bytecodes.LADD, x, y, graph);
+        super(CiKind.Int, Bytecodes.LADD, x, y, graph);
     }
 
     @Override
@@ -57,7 +57,7 @@
         @Override
         public void generate(Node n, LIRGenerator generator) {
             SafeAdd add = (SafeAdd) n;
-            generator.arithmeticOpLong(Bytecodes.LADD, generator.createResultVariable(add), generator.load(add.x()), generator.load(add.y()));
+            generator.arithmeticOpInt(Bytecodes.IADD, generator.createResultVariable(add), generator.load(add.x()), generator.load(add.y()), CiValue.IllegalValue);
             generator.deoptimizeOn(Condition.OF);
         }
     };
--- a/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/intrinsics/SafeAddExample.java	Thu Jul 07 19:58:00 2011 +0200
+++ b/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/intrinsics/SafeAddExample.java	Thu Jul 07 20:46:20 2011 +0200
@@ -28,21 +28,27 @@
     public static final int N = 100000000;
 
     public static void run() {
+        System.out.println();
+        System.out.println();
+        System.out.println("Running SafeAdd Example");
         long start = System.currentTimeMillis();
-        System.out.println(test());
-        System.out.println(System.currentTimeMillis() - start);
+        System.out.println("result=" + test());
+        System.out.println("time=" + (System.currentTimeMillis() - start) + "ms");
     }
 
-    private static long test() {
-        long sum = 0;
-        for (long i = -N; i < N; ++i) {
+    private static int test() {
+        int sum = 0;
+        int j = N;
+        for (int i = -N; i < N; ++i) {
             sum = safeAdd(sum, i);
+            sum = safeAdd(sum, j);
+            --j;
         }
         return sum;
     }
 
-    private static long safeAdd(long a, long b) {
-        long result = a + b;
+    public static int safeAdd(int a, int b) {
+        int result = a + b;
         if (b < 0 && result > a) {
             throw new IllegalStateException("underflow when adding " + a + " and " + b);
         } else if (b > 0 && result < a) {
--- a/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/opt/OptimizationExample.java	Thu Jul 07 19:58:00 2011 +0200
+++ b/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/opt/OptimizationExample.java	Thu Jul 07 20:46:20 2011 +0200
@@ -22,20 +22,25 @@
  */
 package com.oracle.max.graal.examples.opt;
 
-import java.math.*;
+import com.oracle.max.graal.examples.intrinsics.*;
 
 
 public class OptimizationExample {
 
     public static void run() {
+        System.out.println();
+        System.out.println();
+        System.out.println("Running Optimization Example");
         long start = System.currentTimeMillis();
-        System.out.println(test(1, 2));
-        System.out.println(System.currentTimeMillis() - start);
+        System.out.println("result=" + test(1000000000));
+        System.out.println("time=" + (System.currentTimeMillis() - start) + "ms");
     }
 
-    private static long test(long a, long b) {
-        BigInteger bigA = BigInteger.valueOf(a);
-        BigInteger bigB = BigInteger.valueOf(b);
-        return bigA.add(bigB).longValue();
+    private static long test(int n) {
+        long sum = 0;
+        for (int i = 0; i < n; i = SafeAddExample.safeAdd(i, 1)) {
+            sum = sum + i;
+        }
+        return sum;
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.max.graal.examples/src/com/oracle/max/graal/examples/opt/OptimizerImpl.java	Thu Jul 07 20:46:20 2011 +0200
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.max.graal.examples.opt;
+
+import com.oracle.max.graal.compiler.debug.*;
+import com.oracle.max.graal.compiler.ir.*;
+import com.oracle.max.graal.compiler.util.*;
+import com.oracle.max.graal.examples.intrinsics.*;
+import com.oracle.max.graal.extensions.*;
+import com.oracle.max.graal.graph.*;
+import com.sun.cri.ci.*;
+import com.sun.cri.ri.*;
+
+
+public class OptimizerImpl implements Optimizer {
+
+    @Override
+    public void optimize(RiRuntime runtime, Graph graph) {
+        for (SafeAdd safeAdd : graph.getNodes(SafeAdd.class)) {
+            if (safeAdd.y().isConstant() && safeAdd.y().asConstant().asLong() == 1) {
+                if (safeAdd.x() instanceof Phi) {
+                    Phi phi = (Phi) safeAdd.x();
+                    if (phi.merge() instanceof LoopBegin && phi.valueAt(1) == safeAdd) {
+                        LoopBegin loopBegin = (LoopBegin) phi.merge();
+                        if (!canOverflow(phi, loopBegin)) {
+                            IntegerAdd add = new IntegerAdd(CiKind.Int, safeAdd.x(), safeAdd.y(), graph);
+                            safeAdd.replaceAndDelete(add);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private boolean canOverflow(Phi phi, LoopBegin loopBegin) {
+
+        NodeBitMap nodes = LoopUtil.computeLoopNodes(loopBegin);
+        NodeBitMap exits = LoopUtil.computeLoopExits(loopBegin, nodes);
+        for (Node exit : exits) {
+            TTY.println("exit: " + exit);
+            Node pred = exit.predecessors().get(0);
+            if (pred instanceof If) {
+                If ifNode = (If) pred;
+                if (ifNode.compare() instanceof Compare) {
+                    Compare compare = (Compare) ifNode.compare();
+                    Condition cond = compare.condition();
+                    Value x = compare.x();
+                    Value y = compare.y();
+                    if (ifNode.trueSuccessor() == pred) {
+                        cond = cond.negate();
+                    }
+                    if (cond == Condition.LT && x == phi) {
+                        return false;
+                    }
+                    if (cond == Condition.GT && y == phi) {
+                        return false;
+                    }
+                }
+
+            }
+        }
+        TTY.println("can overflow");
+        return true;
+    }
+}
--- a/runexamples.sh	Thu Jul 07 19:58:00 2011 +0200
+++ b/runexamples.sh	Thu Jul 07 20:46:20 2011 +0200
@@ -16,6 +16,6 @@
   exit 1;
 fi
 ant -f create_examples.xml
-COMMAND="${JDK7}/bin/java -client -d64 -graal -Xms1g -Xmx2g -esa -G:Extend -Xcomp -XX:CompileOnly=examples -XX:+PrintCompilation $* -jar examples.jar"
+COMMAND="${JDK7}/bin/java -client -d64 -graal -Xms1g -Xmx2g -esa -G:Extend -Xcomp -XX:CompileOnly=examples $* -jar examples.jar"
 echo $COMMAND
 $COMMAND
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runexamplescompare.sh	Thu Jul 07 20:46:20 2011 +0200
@@ -0,0 +1,27 @@
+#!/bin/bash
+if [ -z "${JDK7}" ]; then
+  echo "JDK7 is not defined."
+  exit 1;
+fi
+if [ -z "${MAXINE}" ]; then
+  echo "MAXINE is not defined. It must point to a maxine repository directory."
+  exit 1;
+fi
+if [ -z "${GRAAL}" ]; then
+  echo "GRAAL is not defined. It must point to a maxine repository directory."
+  exit 1;
+fi
+if [ -z "${DACAPO}" ]; then
+  echo "DACAPO is not defined. It must point to a Dacapo benchmark directory."
+  exit 1;
+fi
+ant -f create_examples.xml
+COMMAND="${JDK7}/bin/java -client -d64 -graal -Xms1g -Xmx2g -esa -G:Extend -Xcomp -XX:CompileOnly=examples $* -jar examples.jar"
+echo $COMMAND
+$COMMAND
+COMMAND="${JDK7}/bin/java -client -d64 -Xms1g -Xmx2g -esa -Xcomp -XX:CompileOnly=examples $* -jar examples.jar"
+echo $COMMAND
+$COMMAND
+COMMAND="${JDK7}/bin/java -server -d64 -Xms1g -Xmx2g -esa -Xcomp -XX:CompileOnly=examples $* -jar examples.jar"
+echo $COMMAND
+$COMMAND