annotate graal/com.oracle.graal.lir/src/com/oracle/graal/lir/SwitchStrategy.java @ 21708:6df25b1418be

moved com.oracle.asm.** to jvmci-util.jar (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 03 Jun 2015 18:06:44 +0200
parents 48c1ebd24120
children d915361cc3a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
1 /*
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
4 *
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
7 * published by the Free Software Foundation.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
8 *
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
13 * accompanied this code).
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
14 *
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
18 *
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
21 * questions.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
22 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
23 package com.oracle.graal.lir;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
24
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
25 import java.util.*;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
26
15200
97eed257999b Move Condition to graal.compiler.common.
Josef Eisl <josef.eisl@jku.at>
parents: 14032
diff changeset
27 import com.oracle.graal.compiler.common.calc.*;
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
28 import com.oracle.graal.lir.asm.*;
21708
6df25b1418be moved com.oracle.asm.** to jvmci-util.jar (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21556
diff changeset
29 import com.oracle.jvmci.asm.*;
21556
48c1ebd24120 renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 19507
diff changeset
30 import com.oracle.jvmci.meta.*;
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
31
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
32 /**
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
33 * This class encapsulates different strategies on how to generate code for switch instructions.
19507
1cde96b96673 Fixed code format issues.
Roland Schatz <roland.schatz@oracle.com>
parents: 18187
diff changeset
34 *
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
35 * The {@link #getBestStrategy(double[], JavaConstant[], LabelRef[])} method can be used to get
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
36 * strategy with the smallest average effort (average number of comparisons until a decision is
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
37 * reached). The strategy returned by this method will have its averageEffort set, while a strategy
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
38 * constructed directly will not.
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
39 */
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
40 public abstract class SwitchStrategy {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
41
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
42 private interface SwitchClosure {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
43 /**
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
44 * Generates a conditional or unconditional jump. The jump will be unconditional if
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
45 * condition is null. If defaultTarget is true, then the jump will go the the default.
19507
1cde96b96673 Fixed code format issues.
Roland Schatz <roland.schatz@oracle.com>
parents: 18187
diff changeset
46 *
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
47 * @param index Index of the value and the jump target (only used if defaultTarget == false)
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
48 * @param condition The condition on which to jump (can be null)
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
49 * @param defaultTarget true if the jump should go to the default target, false if index
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
50 * should be used.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
51 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
52 void conditionalJump(int index, Condition condition, boolean defaultTarget);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
53
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
54 /**
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
55 * Generates a conditional jump to the target with the specified index. The fall through
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
56 * should go to the default target.
19507
1cde96b96673 Fixed code format issues.
Roland Schatz <roland.schatz@oracle.com>
parents: 18187
diff changeset
57 *
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
58 * @param index Index of the value and the jump target
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
59 * @param condition The condition on which to jump
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
60 * @param canFallThrough true if this is the last instruction in the switch statement, to
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
61 * allow for fall-through optimizations.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
62 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
63 void conditionalJumpOrDefault(int index, Condition condition, boolean canFallThrough);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
64
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
65 /**
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
66 * Create a new label and generate a conditional jump to it.
19507
1cde96b96673 Fixed code format issues.
Roland Schatz <roland.schatz@oracle.com>
parents: 18187
diff changeset
67 *
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
68 * @param index Index of the value and the jump target
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
69 * @param condition The condition on which to jump
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
70 * @return a new Label
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
71 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
72 Label conditionalJump(int index, Condition condition);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
73
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
74 /**
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
75 * Binds a label returned by {@link #conditionalJump(int, Condition)}.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
76 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
77 void bind(Label label);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
78
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
79 /**
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
80 * Return true iff the target of both indexes is the same.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
81 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
82 boolean isSameTarget(int index1, int index2);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
83 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
84
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
85 /**
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
86 * Backends can subclass this abstract class and generate code for switch strategies by
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
87 * implementing the {@link #conditionalJump(int, Condition, Label)} method.
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
88 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
89 public abstract static class BaseSwitchClosure implements SwitchClosure {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
90
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
91 private final CompilationResultBuilder crb;
14032
d1c1f103d42c renamed com.oracle.graal.asm.AbstractAssembler to com.oracle.graal.asm.Assembler
twisti
parents: 13318
diff changeset
92 private final Assembler masm;
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
93 private final LabelRef[] keyTargets;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
94 private final LabelRef defaultTarget;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
95
14032
d1c1f103d42c renamed com.oracle.graal.asm.AbstractAssembler to com.oracle.graal.asm.Assembler
twisti
parents: 13318
diff changeset
96 public BaseSwitchClosure(CompilationResultBuilder crb, Assembler masm, LabelRef[] keyTargets, LabelRef defaultTarget) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
97 this.crb = crb;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
98 this.masm = masm;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
99 this.keyTargets = keyTargets;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
100 this.defaultTarget = defaultTarget;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
101 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
102
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
103 /**
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
104 * This method generates code for a comparison between the actual value and the constant at
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
105 * the given index and a condition jump to target.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
106 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
107 protected abstract void conditionalJump(int index, Condition condition, Label target);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
108
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
109 public void conditionalJump(int index, Condition condition, boolean targetDefault) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
110 Label target = targetDefault ? defaultTarget.label() : keyTargets[index].label();
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
111 if (condition == null) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
112 masm.jmp(target);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
113 } else {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
114 conditionalJump(index, condition, target);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
115 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
116 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
117
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
118 public void conditionalJumpOrDefault(int index, Condition condition, boolean canFallThrough) {
13318
da0851712519 moved emitting code for LIR and queries about whether an edge goes to its lexical successor "inside" CompilationResultBuilder
Doug Simon <doug.simon@oracle.com>
parents: 13302
diff changeset
119 if (canFallThrough && crb.isSuccessorEdge(defaultTarget)) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
120 conditionalJump(index, condition, keyTargets[index].label());
13318
da0851712519 moved emitting code for LIR and queries about whether an edge goes to its lexical successor "inside" CompilationResultBuilder
Doug Simon <doug.simon@oracle.com>
parents: 13302
diff changeset
121 } else if (canFallThrough && crb.isSuccessorEdge(keyTargets[index])) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
122 conditionalJump(index, condition.negate(), defaultTarget.label());
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
123 } else {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
124 conditionalJump(index, condition, keyTargets[index].label());
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
125 masm.jmp(defaultTarget.label());
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
126 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
127 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
128
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
129 public Label conditionalJump(int index, Condition condition) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
130 Label label = new Label();
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
131 conditionalJump(index, condition, label);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
132 return label;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
133 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
134
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
135 public void bind(Label label) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
136 masm.bind(label);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
137 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
138
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
139 public boolean isSameTarget(int index1, int index2) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
140 return keyTargets[index1] == keyTargets[index2];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
141 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
142
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
143 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
144
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
145 /**
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
146 * This closure is used internally to determine the average effort for a certain strategy on a
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
147 * given switch instruction.
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
148 */
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
149 private class EffortClosure implements SwitchClosure {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
150
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
151 private int defaultEffort;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
152 private int defaultCount;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
153 private final int[] keyEfforts = new int[keyConstants.length];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
154 private final int[] keyCounts = new int[keyConstants.length];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
155 private final LabelRef[] keyTargets;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
156
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
157 public EffortClosure(LabelRef[] keyTargets) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
158 this.keyTargets = keyTargets;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
159 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
160
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
161 public void conditionalJump(int index, Condition condition, boolean defaultTarget) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
162 // nothing to do
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
163 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
164
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
165 public void conditionalJumpOrDefault(int index, Condition condition, boolean canFallThrough) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
166 // nothing to do
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
167 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
168
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
169 public Label conditionalJump(int index, Condition condition) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
170 // nothing to do
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
171 return null;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
172 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
173
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
174 public void bind(Label label) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
175 // nothing to do
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
176 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
177
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
178 public boolean isSameTarget(int index1, int index2) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
179 return keyTargets[index1] == keyTargets[index2];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
180 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
181
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
182 public double getAverageEffort() {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
183 double defaultProbability = 1;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
184 double effort = 0;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
185 for (int i = 0; i < keyConstants.length; i++) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
186 effort += keyEfforts[i] * keyProbabilities[i] / keyCounts[i];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
187 defaultProbability -= keyProbabilities[i];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
188 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
189 return effort + defaultEffort * defaultProbability / defaultCount;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
190 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
191 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
192
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
193 public final double[] keyProbabilities;
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
194 public final JavaConstant[] keyConstants;
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
195 private double averageEffort = -1;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
196 private EffortClosure effortClosure;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
197
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
198 public SwitchStrategy(double[] keyProbabilities, JavaConstant[] keyConstants) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
199 assert keyConstants.length == keyProbabilities.length && keyConstants.length >= 2;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
200 this.keyProbabilities = keyProbabilities;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
201 this.keyConstants = keyConstants;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
202 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
203
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
204 public double getAverageEffort() {
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
205 assert averageEffort >= 0 : "average effort was not calculated yet for this strategy";
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
206 return averageEffort;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
207 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
208
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
209 /**
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
210 * Looks for the end of a stretch of key constants that are successive numbers and have the same
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
211 * target.
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
212 */
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
213 protected int getSliceEnd(SwitchClosure closure, int pos) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
214 int slice = pos;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
215 while (slice < (keyConstants.length - 1) && keyConstants[slice + 1].asLong() == keyConstants[slice].asLong() + 1 && closure.isSameTarget(slice, slice + 1)) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
216 slice++;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
217 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
218 return slice;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
219 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
220
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
221 /**
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
222 * Tells the system that the given (inclusive) range of keys is reached after depth number of
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
223 * comparisons, which is used to calculate the average effort.
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
224 */
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
225 protected void registerEffort(int rangeStart, int rangeEnd, int depth) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
226 if (effortClosure != null) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
227 for (int i = rangeStart; i <= rangeEnd; i++) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
228 effortClosure.keyEfforts[i] += depth;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
229 effortClosure.keyCounts[i]++;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
230 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
231 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
232 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
233
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
234 /**
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
235 * Tells the system that the default successor is reached after depth number of comparisons,
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
236 * which is used to calculate average effort.
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
237 */
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
238 protected void registerDefaultEffort(int depth) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
239 if (effortClosure != null) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
240 effortClosure.defaultEffort += depth;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
241 effortClosure.defaultCount++;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
242 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
243 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
244
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
245 @Override
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
246 public String toString() {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
247 return getClass().getSimpleName() + "[avgEffort=" + averageEffort + "]";
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
248 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
249
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
250 /**
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
251 * This strategy orders the keys according to their probability and creates one equality
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
252 * comparison per key.
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
253 */
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
254 public static class SequentialStrategy extends SwitchStrategy {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
255 private final Integer[] indexes;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
256
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
257 public SequentialStrategy(final double[] keyProbabilities, JavaConstant[] keyConstants) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
258 super(keyProbabilities, keyConstants);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
259
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
260 int keyCount = keyConstants.length;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
261 indexes = new Integer[keyCount];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
262 for (int i = 0; i < keyCount; i++) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
263 indexes[i] = i;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
264 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
265 Arrays.sort(indexes, new Comparator<Integer>() {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
266 @Override
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
267 public int compare(Integer o1, Integer o2) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
268 return keyProbabilities[o1] < keyProbabilities[o2] ? 1 : keyProbabilities[o1] > keyProbabilities[o2] ? -1 : 0;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
269 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
270 });
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
271 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
272
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
273 @Override
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
274 public void run(SwitchClosure closure) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
275 for (int i = 0; i < keyConstants.length - 1; i++) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
276 closure.conditionalJump(indexes[i], Condition.EQ, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
277 registerEffort(indexes[i], indexes[i], i + 1);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
278 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
279 closure.conditionalJumpOrDefault(indexes[keyConstants.length - 1], Condition.EQ, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
280 registerEffort(indexes[keyConstants.length - 1], indexes[keyConstants.length - 1], keyConstants.length);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
281 registerDefaultEffort(keyConstants.length);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
282 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
283 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
284
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
285 /**
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
286 * This strategy divides the keys into ranges of successive keys with the same target and
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
287 * creates comparisons for these ranges.
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
288 */
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
289 public static class RangesStrategy extends SwitchStrategy {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
290 private final Integer[] indexes;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
291
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
292 public RangesStrategy(final double[] keyProbabilities, JavaConstant[] keyConstants) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
293 super(keyProbabilities, keyConstants);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
295 int keyCount = keyConstants.length;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
296 indexes = new Integer[keyCount];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
297 for (int i = 0; i < keyCount; i++) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
298 indexes[i] = i;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
299 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
300 Arrays.sort(indexes, new Comparator<Integer>() {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
301 @Override
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
302 public int compare(Integer o1, Integer o2) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
303 return keyProbabilities[o1] < keyProbabilities[o2] ? 1 : keyProbabilities[o1] > keyProbabilities[o2] ? -1 : 0;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
304 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
305 });
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
306 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
307
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
308 @Override
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
309 public void run(SwitchClosure closure) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
310 int depth = 0;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
311 closure.conditionalJump(0, Condition.LT, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
312 registerDefaultEffort(++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
313 int rangeStart = 0;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
314 int rangeEnd = getSliceEnd(closure, rangeStart);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
315 while (rangeEnd != keyConstants.length - 1) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
316 if (rangeStart == rangeEnd) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
317 closure.conditionalJump(rangeStart, Condition.EQ, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
318 registerEffort(rangeStart, rangeEnd, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
319 } else {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
320 if (rangeStart == 0 || keyConstants[rangeStart - 1].asLong() + 1 != keyConstants[rangeStart].asLong()) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
321 closure.conditionalJump(rangeStart, Condition.LT, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
322 registerDefaultEffort(++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
323 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
324 closure.conditionalJump(rangeEnd, Condition.LE, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
325 registerEffort(rangeStart, rangeEnd, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
326 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
327 rangeStart = rangeEnd + 1;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
328 rangeEnd = getSliceEnd(closure, rangeStart);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
329 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
330 if (rangeStart == rangeEnd) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
331 closure.conditionalJumpOrDefault(rangeStart, Condition.EQ, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
332 registerEffort(rangeStart, rangeEnd, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
333 registerDefaultEffort(depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
334 } else {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
335 if (rangeStart == 0 || keyConstants[rangeStart - 1].asLong() + 1 != keyConstants[rangeStart].asLong()) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
336 closure.conditionalJump(rangeStart, Condition.LT, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
337 registerDefaultEffort(++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
338 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
339 closure.conditionalJumpOrDefault(rangeEnd, Condition.LE, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
340 registerEffort(rangeStart, rangeEnd, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
341 registerDefaultEffort(depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
342 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
343 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
344 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
345
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
346 /**
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
347 * This strategy recursively subdivides the list of keys to create a binary search based on
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
348 * probabilities.
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
349 */
13300
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
350 public static class BinaryStrategy extends SwitchStrategy {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
351
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
352 private static final double MIN_PROBABILITY = 0.00001;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
353
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
354 private final double[] probabilitySums;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
355
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
356 public BinaryStrategy(double[] keyProbabilities, JavaConstant[] keyConstants) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
357 super(keyProbabilities, keyConstants);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
358 probabilitySums = new double[keyProbabilities.length + 1];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
359 double sum = 0;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
360 for (int i = 0; i < keyConstants.length; i++) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
361 sum += Math.max(keyProbabilities[i], MIN_PROBABILITY);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
362 probabilitySums[i + 1] = sum;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
363 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
364 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
365
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
366 @Override
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
367 public void run(SwitchClosure closure) {
13300
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
368 recurseBinarySwitch(closure, 0, keyConstants.length - 1, 0);
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
369 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
370
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
371 /**
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
372 * Recursively generate a list of comparisons that always subdivides the keys in the given
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
373 * (inclusive) range in the middle (in terms of probability, not index). If left is bigger
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
374 * than zero, then we always know that the value is equal to or bigger than the left key.
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
375 * This does not hold for the right key, as there may be a gap afterwards.
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
376 */
13300
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
377 private void recurseBinarySwitch(SwitchClosure closure, int left, int right, int startDepth) {
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
378 assert startDepth < keyConstants.length * 3 : "runaway recursion in binary switch";
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
379 int depth = startDepth;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
380 boolean leftBorder = left == 0;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
381 boolean rightBorder = right == keyConstants.length - 1;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
382
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
383 if (left + 1 == right) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
384 // only two possible values
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
385 if (leftBorder || rightBorder || keyConstants[right].asLong() + 1 != keyConstants[right + 1].asLong() || keyConstants[left].asLong() + 1 != keyConstants[right].asLong()) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
386 closure.conditionalJump(left, Condition.EQ, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
387 registerEffort(left, left, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
388 closure.conditionalJumpOrDefault(right, Condition.EQ, rightBorder);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
389 registerEffort(right, right, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
390 registerDefaultEffort(depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
391 } else {
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
392 // here we know that the value can only be one of these two keys in the range
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
393 closure.conditionalJump(left, Condition.EQ, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
394 registerEffort(left, left, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
395 closure.conditionalJump(right, null, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
396 registerEffort(right, right, depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
397 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
398 return;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
399 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
400 double probabilityStart = probabilitySums[left];
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
401 double probabilityMiddle = (probabilityStart + probabilitySums[right + 1]) / 2;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
402 assert probabilityStart >= probabilityStart;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
403 int middle = left;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
404 while (getSliceEnd(closure, middle + 1) < right && probabilitySums[getSliceEnd(closure, middle + 1)] < probabilityMiddle) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
405 middle = getSliceEnd(closure, middle + 1);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
406 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
407 middle = getSliceEnd(closure, middle);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
408 assert middle < keyConstants.length - 1;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
409
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
410 if (getSliceEnd(closure, left) == middle) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
411 if (left == 0) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
412 closure.conditionalJump(0, Condition.LT, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
413 registerDefaultEffort(++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
414 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
415 closure.conditionalJump(middle, Condition.LE, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
416 registerEffort(left, middle, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
417
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
418 if (middle + 1 == right) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
419 closure.conditionalJumpOrDefault(right, Condition.EQ, rightBorder);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
420 registerEffort(right, right, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
421 registerDefaultEffort(depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
422 } else {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
423 if (keyConstants[middle].asLong() + 1 != keyConstants[middle + 1].asLong()) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
424 closure.conditionalJump(middle + 1, Condition.LT, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
425 registerDefaultEffort(++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
426 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
427 if (getSliceEnd(closure, middle + 1) == right) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
428 if (right == keyConstants.length - 1 || keyConstants[right].asLong() + 1 != keyConstants[right + 1].asLong()) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
429 closure.conditionalJumpOrDefault(right, Condition.LE, rightBorder);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
430 registerEffort(middle + 1, right, ++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
431 registerDefaultEffort(depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
432 } else {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
433 closure.conditionalJump(middle + 1, null, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
434 registerEffort(middle + 1, right, depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
435 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
436 } else {
13300
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
437 recurseBinarySwitch(closure, middle + 1, right, depth);
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
438 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
439 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
440 } else if (getSliceEnd(closure, middle + 1) == right) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
441 if (rightBorder || keyConstants[right].asLong() + 1 != keyConstants[right + 1].asLong()) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
442 closure.conditionalJump(right, Condition.GT, true);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
443 registerDefaultEffort(++depth);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
444 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
445 closure.conditionalJump(middle + 1, Condition.GE, false);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
446 registerEffort(middle + 1, right, ++depth);
13300
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
447 recurseBinarySwitch(closure, left, middle, depth);
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
448 } else {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
449 Label label = closure.conditionalJump(middle + 1, Condition.GE);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
450 depth++;
13300
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
451 recurseBinarySwitch(closure, left, middle, depth);
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
452 closure.bind(label);
13300
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
453 recurseBinarySwitch(closure, middle + 1, right, depth);
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
454 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
455 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
456 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
457
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
458 public abstract void run(SwitchClosure closure);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
459
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
460 private static SwitchStrategy[] getStrategies(double[] keyProbabilities, JavaConstant[] keyConstants, LabelRef[] keyTargets) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
461 SwitchStrategy[] strategies = new SwitchStrategy[]{new SequentialStrategy(keyProbabilities, keyConstants), new RangesStrategy(keyProbabilities, keyConstants),
13300
d5e65a244f7d rename BooleanSwitch to BinarySwitch
Lukas Stadler <lukas.stadler@jku.at>
parents: 13294
diff changeset
462 new BinaryStrategy(keyProbabilities, keyConstants)};
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
463 for (SwitchStrategy strategy : strategies) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
464 strategy.effortClosure = strategy.new EffortClosure(keyTargets);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
465 strategy.run(strategy.effortClosure);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
466 strategy.averageEffort = strategy.effortClosure.getAverageEffort();
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
467 strategy.effortClosure = null;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
468 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
469 return strategies;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
470 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
471
13302
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
472 /**
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
473 * Creates all switch strategies for the given switch, evaluates them (based on average effort)
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
474 * and returns the best one.
094f4ee93977 some javadoc for switch strategies
Lukas Stadler <lukas.stadler@jku.at>
parents: 13300
diff changeset
475 */
18187
9619ba4daf4c Rename Constant to JavaConstant.
Roland Schatz <roland.schatz@oracle.com>
parents: 15200
diff changeset
476 public static SwitchStrategy getBestStrategy(double[] keyProbabilities, JavaConstant[] keyConstants, LabelRef[] keyTargets) {
13294
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
477 SwitchStrategy[] strategies = getStrategies(keyProbabilities, keyConstants, keyTargets);
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
478 double bestEffort = Integer.MAX_VALUE;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
479 SwitchStrategy bestStrategy = null;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
480 for (SwitchStrategy strategy : strategies) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
481 if (strategy.getAverageEffort() < bestEffort) {
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
482 bestEffort = strategy.getAverageEffort();
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
483 bestStrategy = strategy;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
484 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
485 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
486 return bestStrategy;
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
487 }
29907e69ae8d rework of switch generation: move code into platform independent SwitchStrategy, add boolean switch strategy
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
488 }