changeset 16544:1e8b758800fb

extract methods in TruffleCacheImpl
author Christian Wirth <christian.wirth@oracle.com>
date Thu, 17 Jul 2014 11:25:56 +0200
parents eb3209d37c50
children c667378e4699
files graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCacheImpl.java
diffstat 1 files changed, 63 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCacheImpl.java	Thu Jul 17 11:21:36 2014 +0200
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/TruffleCacheImpl.java	Thu Jul 17 11:25:56 2014 +0200
@@ -29,6 +29,7 @@
 
 import com.oracle.graal.api.code.*;
 import com.oracle.graal.api.meta.*;
+import com.oracle.graal.compiler.common.*;
 import com.oracle.graal.debug.*;
 import com.oracle.graal.debug.Debug.Scope;
 import com.oracle.graal.graph.Graph.Mark;
@@ -97,7 +98,6 @@
         return graph;
     }
 
-    @SuppressWarnings("unused")
     public StructuredGraph lookup(final ResolvedJavaMethod method, final NodeInputList<ValueNode> arguments, final Assumptions assumptions, final CanonicalizerPhase finalCanonicalizer,
                     boolean ignoreSlowPath) {
 
@@ -124,24 +124,7 @@
         }
 
         if (lastUsed.values().size() >= TruffleCompilerOptions.TruffleMaxCompilationCacheSize.getValue()) {
-            List<Long> lastUsedList = new ArrayList<>();
-            for (long l : lastUsed.values()) {
-                lastUsedList.add(l);
-            }
-            Collections.sort(lastUsedList);
-            long mid = lastUsedList.get(lastUsedList.size() / 2);
-
-            List<List<Object>> toRemoveList = new ArrayList<>();
-            for (Entry<List<Object>, Long> entry : lastUsed.entrySet()) {
-                if (entry.getValue() < mid) {
-                    toRemoveList.add(entry.getKey());
-                }
-            }
-
-            for (List<Object> entry : toRemoveList) {
-                cache.remove(entry);
-                lastUsed.remove(entry);
-            }
+            lookupExceedsMaxSize();
         }
 
         lastUsed.put(key, counter++);
@@ -183,42 +166,11 @@
                 boolean inliningProgress = false;
                 for (MethodCallTargetNode methodCallTarget : graph.getNodes(MethodCallTargetNode.class)) {
                     if (!graph.getMark().equals(mark)) {
-                        // Make sure macro substitutions such as
-                        // CompilerDirectives.transferToInterpreter get processed first.
-                        for (Node newNode : graph.getNewNodes(mark)) {
-                            if (newNode instanceof MethodCallTargetNode) {
-                                MethodCallTargetNode methodCallTargetNode = (MethodCallTargetNode) newNode;
-                                Class<? extends FixedWithNextNode> macroSubstitution = providers.getReplacements().getMacroSubstitution(methodCallTargetNode.targetMethod());
-                                if (macroSubstitution != null) {
-                                    InliningUtil.inlineMacroNode(methodCallTargetNode.invoke(), methodCallTargetNode.targetMethod(), macroSubstitution);
-                                } else {
-                                    tryCutOffRuntimeExceptionsAndErrors(methodCallTargetNode);
-                                }
-                            }
-                        }
-                        mark = graph.getMark();
+                        mark = lookupProcessMacroSubstitutions(graph, mark);
                     }
                     if (methodCallTarget.isAlive() && methodCallTarget.invoke() != null && shouldInline(methodCallTarget)) {
                         inliningProgress = true;
-                        List<Node> canonicalizerUsages = new ArrayList<Node>();
-                        for (Node n : methodCallTarget.invoke().asNode().usages()) {
-                            if (n instanceof Canonicalizable) {
-                                canonicalizerUsages.add(n);
-                            }
-                        }
-                        List<ValueNode> argumentSnapshot = methodCallTarget.arguments().snapshot();
-                        Mark beforeInvokeMark = graph.getMark();
-                        expandInvoke(methodCallTarget);
-                        for (Node arg : argumentSnapshot) {
-                            if (arg != null && arg.recordsUsages()) {
-                                for (Node argUsage : arg.usages()) {
-                                    if (graph.isNew(beforeInvokeMark, argUsage) && argUsage instanceof Canonicalizable) {
-                                        canonicalizerUsages.add(argUsage);
-                                    }
-                                }
-                            }
-                        }
-                        canonicalizerPhase.applyIncremental(graph, phaseContext, canonicalizerUsages);
+                        lookupDoInline(graph, phaseContext, canonicalizerPhase, methodCallTarget);
                     }
                 }
 
@@ -240,7 +192,66 @@
         } catch (Throwable e) {
             throw Debug.handle(e);
         }
+    }
 
+    private void lookupExceedsMaxSize() {
+        List<Long> lastUsedList = new ArrayList<>();
+        for (long l : lastUsed.values()) {
+            lastUsedList.add(l);
+        }
+        Collections.sort(lastUsedList);
+        long mid = lastUsedList.get(lastUsedList.size() / 2);
+
+        List<List<Object>> toRemoveList = new ArrayList<>();
+        for (Entry<List<Object>, Long> entry : lastUsed.entrySet()) {
+            if (entry.getValue() < mid) {
+                toRemoveList.add(entry.getKey());
+            }
+        }
+
+        for (List<Object> entry : toRemoveList) {
+            cache.remove(entry);
+            lastUsed.remove(entry);
+        }
+    }
+
+    private Mark lookupProcessMacroSubstitutions(final StructuredGraph graph, Mark mark) throws GraalInternalError {
+        // Make sure macro substitutions such as
+        // CompilerDirectives.transferToInterpreter get processed first.
+        for (Node newNode : graph.getNewNodes(mark)) {
+            if (newNode instanceof MethodCallTargetNode) {
+                MethodCallTargetNode methodCallTargetNode = (MethodCallTargetNode) newNode;
+                Class<? extends FixedWithNextNode> macroSubstitution = providers.getReplacements().getMacroSubstitution(methodCallTargetNode.targetMethod());
+                if (macroSubstitution != null) {
+                    InliningUtil.inlineMacroNode(methodCallTargetNode.invoke(), methodCallTargetNode.targetMethod(), macroSubstitution);
+                } else {
+                    tryCutOffRuntimeExceptionsAndErrors(methodCallTargetNode);
+                }
+            }
+        }
+        return graph.getMark();
+    }
+
+    private void lookupDoInline(final StructuredGraph graph, final PhaseContext phaseContext, CanonicalizerPhase canonicalizerPhase, MethodCallTargetNode methodCallTarget) {
+        List<Node> canonicalizerUsages = new ArrayList<>();
+        for (Node n : methodCallTarget.invoke().asNode().usages()) {
+            if (n instanceof Canonicalizable) {
+                canonicalizerUsages.add(n);
+            }
+        }
+        List<ValueNode> argumentSnapshot = methodCallTarget.arguments().snapshot();
+        Mark beforeInvokeMark = graph.getMark();
+        expandInvoke(methodCallTarget);
+        for (Node arg : argumentSnapshot) {
+            if (arg != null && arg.recordsUsages()) {
+                for (Node argUsage : arg.usages()) {
+                    if (graph.isNew(beforeInvokeMark, argUsage) && argUsage instanceof Canonicalizable) {
+                        canonicalizerUsages.add(argUsage);
+                    }
+                }
+            }
+        }
+        canonicalizerPhase.applyIncremental(graph, phaseContext, canonicalizerUsages);
     }
 
     private void expandInvoke(MethodCallTargetNode methodCallTargetNode) {