• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 
5 // This code is not run directly. It needs to be compiled to dex code.
6 // 'arithmetic.dex' is what is run.
7 
8 package cse;
9 
10 public class CommonSubexpressionElimination {
11 
divNoCatch(int a, int b, int c)12   public static int divNoCatch(int a, int b, int c) {
13     int d = c / (a - b);
14     System.out.println(d);
15     return c / (a - b);
16   }
17 
divNoCatch2(int a, int b, int c)18   public static int divNoCatch2(int a, int b, int c) {
19     int d = c / (a - b);
20     int e = c / (a - b);
21     System.out.println(d + " " + e);
22     return c / (a - b);
23   }
24 
divCatch(int a, int b, int c)25   public static int divCatch(int a, int b, int c) {
26     try {
27       int d = c / (a - b);
28       System.out.println(d);
29       return d;
30     } catch (Throwable t) {
31       return c / (a - b);
32     }
33   }
34 
divCatch2(int a, int b, int c)35   public static int divCatch2(int a, int b, int c) {
36     try {
37       int d = c / (a - b);
38       int e = c / (a - b);
39       System.out.println(d + " " + e);
40       return d;
41     } catch (Throwable t) {
42       return c / (a - b);
43     }
44   }
45 
divCatchCatch(int a, int b, int c)46   public static String divCatchCatch(int a, int b, int c) {
47     try {
48       int d = c / (a - b);
49       System.out.println(d);
50       return "X";
51     } catch (Throwable t) {
52       try {
53         return "" + c / (a - b);
54       } catch (Throwable t2) {
55         return "A";
56       }
57     }
58   }
59 
divCatchSharedCatchHandler(int a, int b, int c)60   public static String divCatchSharedCatchHandler(int a, int b, int c) {
61     try {
62       int d = c / (a - b);
63       System.out.println(d);
64       if (a == 0) {
65         int e = c / (a - b);
66         System.out.println(e);
67       } else {
68         int f = c / (a - b);
69         System.out.println(f);
70       }
71       return "X";
72     } catch (Throwable t) {
73       return "B";
74     }
75   }
76 
77 
main(String[] args)78   public static void main(String[] args) {
79     System.out.println(divNoCatch(1, 0, 1));
80     System.out.println(divNoCatch2(1, 0, 2));
81     System.out.println(divCatch(1, 0, 3));
82     System.out.println(divCatch2(1, 0, 4));
83 
84     System.out.println(divCatchCatch(0, 0, 1));
85     System.out.println(divCatchSharedCatchHandler(0, 0, 1));
86 
87     try {
88       divNoCatch(0, 0, 1);
89       throw new RuntimeException("UNEXPECTED");
90     } catch (ArithmeticException e) {
91       // Expected "divide by zero".
92     }
93 
94     try {
95       divNoCatch2(0, 0, 1);
96       throw new RuntimeException("UNEXPECTED");
97     } catch (ArithmeticException e) {
98       // Expected "divide by zero".
99     }
100 
101     try {
102       divCatch(0, 0, 1);
103       throw new RuntimeException("UNEXPECTED");
104     } catch (ArithmeticException e) {
105       // Expected "divide by zero".
106     }
107 
108     try {
109       divCatch2(0, 0, 1);
110       throw new RuntimeException("UNEXPECTED");
111     } catch (ArithmeticException e) {
112       // Expected "divide by zero".
113     }
114   }
115 }
116