57 } else {
58 x = 2;
59 sideEffect = null;
60 }
61 sideEffect = null;
62 // can shift
63 return a * x * 4;
64 }
65
66 public static int test2Snippet(int a) {
67 int x = 0;
68 if (a > 0) {
69 x = 1;
70 sideEffect = null;
71 } else {
72 x = 2;
73 sideEffect = null;
74 }
75 sideEffect = null;
76 // cannot shift
77 return a * x * 3;
78 }
79
80 public static final int ITERATIONS_LOOP_1 = 128;
81 public static final int ITERATIONS_LOOP_2 = 256;
82
83 public static int testLoop01(int a) {
84 int res = 0;
85 for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_1, i < a); i++) {
86 res += i;
87 }
88 return res;
89 }
90
91 public static int testLoop02(int a) {
92 int res = 0;
93 for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_2, i < a); i++) {
94 res += i;
95 }
96 return res;
97 }
281 Assert.assertEquals(50, gc1.finalCycles, 25);
282 }
283
284 @Test
285 public void testFieldLoad() {
286 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldLoad"));
287 HighTierContext htc = getDefaultHighTierContext();
288 new CanonicalizerPhase().apply(g1, htc);
289 GraphCostPhase gc1 = new GraphCostPhase();
290 gc1.apply(g1, htc);
291 Assert.assertEquals(30, gc1.finalCycles, 25);
292 }
293
294 @Test
295 public void testFieldStore() {
296 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldStore"));
297 HighTierContext htc = getDefaultHighTierContext();
298 new CanonicalizerPhase().apply(g1, htc);
299 GraphCostPhase gc1 = new GraphCostPhase();
300 gc1.apply(g1, htc);
301 Assert.assertEquals(120, gc1.finalCycles, 25);
302 }
303
304 static class ImprovementSavingCanonicalizer extends CustomCanonicalizer {
305 private int savedCycles;
306 private final NodeCostProvider nodeCostProvider;
307
308 ImprovementSavingCanonicalizer(NodeCostProvider nodeCostProvider) {
309 this.nodeCostProvider = nodeCostProvider;
310 }
311
312 @Override
313 public void simplify(Node node, SimplifierTool tool) {
314 if (node instanceof Canonicalizable.Binary<?>) {
315 @SuppressWarnings("unchecked")
316 Canonicalizable.Binary<ValueNode> bc = (Canonicalizable.Binary<ValueNode>) node;
317 Node canonicalized = bc.canonical(tool, bc.getX(), bc.getY());
318 if (canonicalized != node) {
319 savedCycles += nodeCostProvider.getEstimatedCPUCycles(node) - nodeCostProvider.getEstimatedCPUCycles(canonicalized);
320 }
321 }
|
57 } else {
58 x = 2;
59 sideEffect = null;
60 }
61 sideEffect = null;
62 // can shift
63 return a * x * 4;
64 }
65
66 public static int test2Snippet(int a) {
67 int x = 0;
68 if (a > 0) {
69 x = 1;
70 sideEffect = null;
71 } else {
72 x = 2;
73 sideEffect = null;
74 }
75 sideEffect = null;
76 // cannot shift
77 return a * x * 41;
78 }
79
80 public static final int ITERATIONS_LOOP_1 = 128;
81 public static final int ITERATIONS_LOOP_2 = 256;
82
83 public static int testLoop01(int a) {
84 int res = 0;
85 for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_1, i < a); i++) {
86 res += i;
87 }
88 return res;
89 }
90
91 public static int testLoop02(int a) {
92 int res = 0;
93 for (int i = 0; GraalDirectives.injectIterationCount(ITERATIONS_LOOP_2, i < a); i++) {
94 res += i;
95 }
96 return res;
97 }
281 Assert.assertEquals(50, gc1.finalCycles, 25);
282 }
283
284 @Test
285 public void testFieldLoad() {
286 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldLoad"));
287 HighTierContext htc = getDefaultHighTierContext();
288 new CanonicalizerPhase().apply(g1, htc);
289 GraphCostPhase gc1 = new GraphCostPhase();
290 gc1.apply(g1, htc);
291 Assert.assertEquals(30, gc1.finalCycles, 25);
292 }
293
294 @Test
295 public void testFieldStore() {
296 StructuredGraph g1 = parseForCompile(getResolvedJavaMethod("fieldStore"));
297 HighTierContext htc = getDefaultHighTierContext();
298 new CanonicalizerPhase().apply(g1, htc);
299 GraphCostPhase gc1 = new GraphCostPhase();
300 gc1.apply(g1, htc);
301 Assert.assertEquals(40, gc1.finalCycles, 25);
302 }
303
304 static class ImprovementSavingCanonicalizer extends CustomCanonicalizer {
305 private int savedCycles;
306 private final NodeCostProvider nodeCostProvider;
307
308 ImprovementSavingCanonicalizer(NodeCostProvider nodeCostProvider) {
309 this.nodeCostProvider = nodeCostProvider;
310 }
311
312 @Override
313 public void simplify(Node node, SimplifierTool tool) {
314 if (node instanceof Canonicalizable.Binary<?>) {
315 @SuppressWarnings("unchecked")
316 Canonicalizable.Binary<ValueNode> bc = (Canonicalizable.Binary<ValueNode>) node;
317 Node canonicalized = bc.canonical(tool, bc.getX(), bc.getY());
318 if (canonicalized != node) {
319 savedCycles += nodeCostProvider.getEstimatedCPUCycles(node) - nodeCostProvider.getEstimatedCPUCycles(canonicalized);
320 }
321 }
|