From 0eb4e54e512b5db8d4e761411fe56f906921d01b Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Mon, 27 Jan 2014 11:34:55 +0400 Subject: [PATCH] Fixes register allocation bugs --- .../org/teavm/model/util/LivenessAnalyzer.java | 2 +- .../org/teavm/model/util/RegisterAllocator.java | 15 +++++++++++---- .../org/teavm/optimization/ClassSetOptimizer.java | 3 +-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/teavm-core/src/main/java/org/teavm/model/util/LivenessAnalyzer.java b/teavm-core/src/main/java/org/teavm/model/util/LivenessAnalyzer.java index 97ad76396..f94bf4000 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/LivenessAnalyzer.java +++ b/teavm-core/src/main/java/org/teavm/model/util/LivenessAnalyzer.java @@ -118,7 +118,7 @@ public class LivenessAnalyzer { } private boolean dominates(int a, int b) { - return domLeft[a] <= domLeft[b] && domRight[a] >= domRight[b]; + return domLeft[a] < domLeft[b] && domRight[a] > domRight[b]; } private static class Task { diff --git a/teavm-core/src/main/java/org/teavm/model/util/RegisterAllocator.java b/teavm-core/src/main/java/org/teavm/model/util/RegisterAllocator.java index aff46bcf1..0d38f09a5 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/RegisterAllocator.java +++ b/teavm-core/src/main/java/org/teavm/model/util/RegisterAllocator.java @@ -15,9 +15,7 @@ */ package org.teavm.model.util; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; import org.teavm.common.DisjointSet; import org.teavm.common.Graph; import org.teavm.model.*; @@ -42,7 +40,7 @@ public class RegisterAllocator { int[] classArray = congruenceClasses.pack(program.variableCount()); int[] colors = new int[program.variableCount()]; Arrays.fill(colors, -1); - for (int i = 0; i < method.parameterCount(); ++i) { + for (int i = 0; i <= method.parameterCount(); ++i) { colors[i] = i; } GraphColorer colorer = new GraphColorer(); @@ -61,6 +59,7 @@ public class RegisterAllocator { private List insertPhiArgumentsCopies(Program program) { List copies = new ArrayList<>(); for (int i = 0; i < program.basicBlockCount(); ++i) { + Map blockMap = new HashMap<>(); for (final Phi phi : program.basicBlockAt(i).getPhis()) { for (Incoming incoming : phi.getIncomings()) { PhiArgumentCopy copy = new PhiArgumentCopy(); @@ -70,6 +69,12 @@ public class RegisterAllocator { copyInstruction.setReceiver(program.createVariable()); copyInstruction.setAssignee(incoming.getValue()); copy.var = copyInstruction.getReceiver().getIndex(); + BasicBlock source = blockMap.get(incoming.getSource()); + if (source == null) { + source = incoming.getSource(); + } else { + incoming.setSource(source); + } if (incoming.getSource().getLastInstruction() instanceof JumpInstruction) { copy.index = incoming.getSource().getInstructions().size() - 1; copy.block = incoming.getSource(); @@ -89,10 +94,12 @@ public class RegisterAllocator { } } }); + blockMap.put(source, copyBlock); incoming.setSource(copyBlock); copy.index = 0; copy.block = incoming.getSource(); } + incoming.setValue(copyInstruction.getReceiver()); copies.add(copy); } } diff --git a/teavm-core/src/main/java/org/teavm/optimization/ClassSetOptimizer.java b/teavm-core/src/main/java/org/teavm/optimization/ClassSetOptimizer.java index ece03aea7..e1b3999b4 100644 --- a/teavm-core/src/main/java/org/teavm/optimization/ClassSetOptimizer.java +++ b/teavm-core/src/main/java/org/teavm/optimization/ClassSetOptimizer.java @@ -27,8 +27,7 @@ import org.teavm.model.MethodHolder; */ public class ClassSetOptimizer { private List optimizations = Arrays.asList( - new CommonSubexpressionElimination(), new UnusedVariableElimination(), - new EmptyBlockElimination()); + new CommonSubexpressionElimination(), new UnusedVariableElimination()); public void optimizeAll(ListableClassHolderSource classSource) { for (String className : classSource.getClassNames()) {