blob: efe0d3f72900930d54252a8b68a1540c4395ef83 [file] [log] [blame]
David Brazdil46e2a392015-03-16 17:31:52 +00001/*
2* Copyright (C) 2015 The Android Open Source Project
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17public class Main {
18
19 // Note #1: `javac` flips the conditions of If statements.
20 // Note #2: In the optimizing compiler, the first input of Phi is always
21 // the fall-through path, i.e. the false branch.
22
23 public static void assertBoolEquals(boolean expected, boolean result) {
24 if (expected != result) {
25 throw new Error("Expected: " + expected + ", found: " + result);
26 }
27 }
28
29 /*
30 * Elementary test negating a boolean. Verifies that the condition is replaced,
31 * blocks merged and empty branches removed.
32 */
33
34 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (before)
35 // CHECK-DAG: [[Param:z\d+]] ParameterValue
36 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
37 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
38 // CHECK-DAG: [[NotEq:z\d+]] NotEqual [ [[Param]] [[Const0]] ]
39 // CHECK-DAG: If [ [[NotEq]] ]
40 // CHECK-DAG: [[Phi:i\d+]] Phi [ [[Const1]] [[Const0]] ]
41 // CHECK-DAG: Return [ [[Phi]] ]
42
43 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (before)
44 // CHECK: Goto
45 // CHECK: Goto
46 // CHECK: Goto
47 // CHECK-NOT: Goto
48
49 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
50 // CHECK-DAG: [[Param:z\d+]] ParameterValue
51 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
52 // CHECK-DAG: [[Eq:z\d+]] Equal [ [[Param]] [[Const0]] ]
53 // CHECK-DAG: Return [ [[Eq]] ]
54
55 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
56 // CHECK-NOT: NotEqual
57 // CHECK-NOT: If
58 // CHECK-NOT: Phi
59
60 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
61 // CHECK: Goto
62 // CHECK-NOT: Goto
63
64 public static boolean BooleanNot(boolean x) {
65 return !x;
66 }
67
68 /*
69 * Program which only delegates the condition, i.e. returns 1 when True
70 * and 0 when False.
71 */
72
73 // CHECK-START: boolean Main.GreaterThan(int, int) boolean_simplifier (before)
74 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
75 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
76 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
77 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
78 // CHECK-DAG: [[Cond:z\d+]] GreaterThan [ [[ParamX]] [[ParamY]] ]
79 // CHECK-DAG: If [ [[Cond]] ]
80 // CHECK-DAG: [[Phi:i\d+]] Phi [ [[Const0]] [[Const1]] ]
81 // CHECK-DAG: Return [ [[Phi]] ]
82
83 // CHECK-START: boolean Main.GreaterThan(int, int) boolean_simplifier (after)
84 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
85 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
86 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
87 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
88 // CHECK-DAG: [[Cond:z\d+]] GreaterThan [ [[ParamX]] [[ParamY]] ]
89 // CHECK-DAG: Return [ [[Cond]] ]
90
91 public static boolean GreaterThan(int x, int y) {
92 return (x <= y) ? false : true;
93 }
94
95 /*
96 * Program which negates a condition, i.e. returns 0 when True
97 * and 1 when False.
98 */
99
100 // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (before)
101 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
102 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
103 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
104 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
105 // CHECK-DAG: [[Cond:z\d+]] GreaterThanOrEqual [ [[ParamX]] [[ParamY]] ]
106 // CHECK-DAG: If [ [[Cond]] ]
107 // CHECK-DAG: [[Phi:i\d+]] Phi [ [[Const1]] [[Const0]] ]
108 // CHECK-DAG: Return [ [[Phi]] ]
109
110 // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (after)
111 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
112 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
113 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
114 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
115 // CHECK-DAG: [[Cond:z\d+]] LessThan [ [[ParamX]] [[ParamY]] ]
116 // CHECK-DAG: Return [ [[Cond]] ]
117
118 public static boolean LessThan(int x, int y) {
David Brazdilb2bd1c52015-03-25 11:17:37 +0000119 return (x < y) ? true : false;
David Brazdil46e2a392015-03-16 17:31:52 +0000120 }
121
122 /*
123 * Program which further uses negated conditions.
124 * Note that Phis are discovered retrospectively.
125 */
126
127 // CHECK-START: boolean Main.ValuesOrdered(int, int, int) boolean_simplifier (before)
128 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
129 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
130 // CHECK-DAG: [[ParamZ:i\d+]] ParameterValue
131 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
132 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
133 // CHECK-DAG: [[CondXY:z\d+]] GreaterThan [ [[ParamX]] [[ParamY]] ]
134 // CHECK-DAG: If [ [[CondXY]] ]
135 // CHECK-DAG: [[CondYZ:z\d+]] GreaterThan [ [[ParamY]] [[ParamZ]] ]
136 // CHECK-DAG: If [ [[CondYZ]] ]
137 // CHECK-DAG: [[CondXYZ:z\d+]] NotEqual [ [[PhiXY:i\d+]] [[PhiYZ:i\d+]] ]
138 // CHECK-DAG: If [ [[CondXYZ]] ]
139 // CHECK-DAG: Return [ [[PhiXYZ:i\d+]] ]
140 // CHECK-DAG: [[PhiXY]] Phi [ [[Const1]] [[Const0]] ]
141 // CHECK-DAG: [[PhiYZ]] Phi [ [[Const1]] [[Const0]] ]
142 // CHECK-DAG: [[PhiXYZ]] Phi [ [[Const1]] [[Const0]] ]
143
144 // CHECK-START: boolean Main.ValuesOrdered(int, int, int) boolean_simplifier (after)
145 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
146 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
147 // CHECK-DAG: [[ParamZ:i\d+]] ParameterValue
148 // CHECK-DAG: [[CmpXY:z\d+]] LessThanOrEqual [ [[ParamX]] [[ParamY]] ]
149 // CHECK-DAG: [[CmpYZ:z\d+]] LessThanOrEqual [ [[ParamY]] [[ParamZ]] ]
150 // CHECK-DAG: [[CmpXYZ:z\d+]] Equal [ [[CmpXY]] [[CmpYZ]] ]
151 // CHECK-DAG: Return [ [[CmpXYZ]] ]
152
153 public static boolean ValuesOrdered(int x, int y, int z) {
154 return (x <= y) == (y <= z);
155 }
156
157 public static void main(String[] args) {
158 assertBoolEquals(false, BooleanNot(true));
159 assertBoolEquals(true, BooleanNot(false));
160 assertBoolEquals(true, GreaterThan(10, 5));
161 assertBoolEquals(false, GreaterThan(10, 10));
162 assertBoolEquals(false, GreaterThan(5, 10));
163 assertBoolEquals(true, LessThan(5, 10));
164 assertBoolEquals(false, LessThan(10, 10));
165 assertBoolEquals(false, LessThan(10, 5));
166 assertBoolEquals(true, ValuesOrdered(1, 3, 5));
167 assertBoolEquals(true, ValuesOrdered(5, 3, 1));
168 assertBoolEquals(false, ValuesOrdered(1, 3, 2));
169 assertBoolEquals(false, ValuesOrdered(2, 3, 1));
170 assertBoolEquals(true, ValuesOrdered(3, 3, 3));
171 assertBoolEquals(true, ValuesOrdered(3, 3, 5));
172 assertBoolEquals(false, ValuesOrdered(5, 5, 3));
173 }
174}