blob: 3daf6934fa871919cb1a2351239c5d844e0e37bd [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 /*
David Brazdil0d13fee2015-04-17 14:52:19 +010030 * Elementary test negating a boolean. Verifies that blocks are merged and
31 * empty branches removed.
David Brazdil46e2a392015-03-16 17:31:52 +000032 */
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
David Brazdil0d13fee2015-04-17 14:52:19 +010038 // CHECK-DAG: If [ [[Param]] ]
David Brazdil46e2a392015-03-16 17:31:52 +000039 // CHECK-DAG: [[Phi:i\d+]] Phi [ [[Const1]] [[Const0]] ]
40 // CHECK-DAG: Return [ [[Phi]] ]
41
42 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (before)
43 // CHECK: Goto
44 // CHECK: Goto
45 // CHECK: Goto
46 // CHECK-NOT: Goto
47
48 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
49 // CHECK-DAG: [[Param:z\d+]] ParameterValue
50 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
David Brazdil0d13fee2015-04-17 14:52:19 +010051 // CHECK-DAG: [[NotParam:z\d+]] BooleanNot [ [[Param]] ]
52 // CHECK-DAG: Return [ [[NotParam]] ]
David Brazdil46e2a392015-03-16 17:31:52 +000053
54 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
David Brazdil46e2a392015-03-16 17:31:52 +000055 // CHECK-NOT: If
56 // CHECK-NOT: Phi
57
58 // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
59 // CHECK: Goto
60 // CHECK-NOT: Goto
61
62 public static boolean BooleanNot(boolean x) {
63 return !x;
64 }
65
66 /*
67 * Program which only delegates the condition, i.e. returns 1 when True
68 * and 0 when False.
69 */
70
71 // CHECK-START: boolean Main.GreaterThan(int, int) boolean_simplifier (before)
72 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
73 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
74 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
75 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
76 // CHECK-DAG: [[Cond:z\d+]] GreaterThan [ [[ParamX]] [[ParamY]] ]
77 // CHECK-DAG: If [ [[Cond]] ]
78 // CHECK-DAG: [[Phi:i\d+]] Phi [ [[Const0]] [[Const1]] ]
79 // CHECK-DAG: Return [ [[Phi]] ]
80
81 // CHECK-START: boolean Main.GreaterThan(int, int) boolean_simplifier (after)
82 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
83 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
84 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
85 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
86 // CHECK-DAG: [[Cond:z\d+]] GreaterThan [ [[ParamX]] [[ParamY]] ]
87 // CHECK-DAG: Return [ [[Cond]] ]
88
89 public static boolean GreaterThan(int x, int y) {
90 return (x <= y) ? false : true;
91 }
92
93 /*
94 * Program which negates a condition, i.e. returns 0 when True
95 * and 1 when False.
96 */
97
98 // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (before)
99 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
100 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
101 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
102 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
103 // CHECK-DAG: [[Cond:z\d+]] GreaterThanOrEqual [ [[ParamX]] [[ParamY]] ]
104 // CHECK-DAG: If [ [[Cond]] ]
105 // CHECK-DAG: [[Phi:i\d+]] Phi [ [[Const1]] [[Const0]] ]
106 // CHECK-DAG: Return [ [[Phi]] ]
107
108 // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (after)
109 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
110 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
111 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
112 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
113 // CHECK-DAG: [[Cond:z\d+]] LessThan [ [[ParamX]] [[ParamY]] ]
114 // CHECK-DAG: Return [ [[Cond]] ]
115
David Brazdil0d13fee2015-04-17 14:52:19 +0100116 // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (after)
117 // CHECK-NOT: GreaterThanOrEqual
118
David Brazdil46e2a392015-03-16 17:31:52 +0000119 public static boolean LessThan(int x, int y) {
David Brazdilb2bd1c52015-03-25 11:17:37 +0000120 return (x < y) ? true : false;
David Brazdil46e2a392015-03-16 17:31:52 +0000121 }
122
123 /*
124 * Program which further uses negated conditions.
125 * Note that Phis are discovered retrospectively.
126 */
127
128 // CHECK-START: boolean Main.ValuesOrdered(int, int, int) boolean_simplifier (before)
129 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
130 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
131 // CHECK-DAG: [[ParamZ:i\d+]] ParameterValue
132 // CHECK-DAG: [[Const0:i\d+]] IntConstant 0
133 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
134 // CHECK-DAG: [[CondXY:z\d+]] GreaterThan [ [[ParamX]] [[ParamY]] ]
135 // CHECK-DAG: If [ [[CondXY]] ]
136 // CHECK-DAG: [[CondYZ:z\d+]] GreaterThan [ [[ParamY]] [[ParamZ]] ]
137 // CHECK-DAG: If [ [[CondYZ]] ]
138 // CHECK-DAG: [[CondXYZ:z\d+]] NotEqual [ [[PhiXY:i\d+]] [[PhiYZ:i\d+]] ]
139 // CHECK-DAG: If [ [[CondXYZ]] ]
140 // CHECK-DAG: Return [ [[PhiXYZ:i\d+]] ]
141 // CHECK-DAG: [[PhiXY]] Phi [ [[Const1]] [[Const0]] ]
142 // CHECK-DAG: [[PhiYZ]] Phi [ [[Const1]] [[Const0]] ]
143 // CHECK-DAG: [[PhiXYZ]] Phi [ [[Const1]] [[Const0]] ]
144
145 // CHECK-START: boolean Main.ValuesOrdered(int, int, int) boolean_simplifier (after)
146 // CHECK-DAG: [[ParamX:i\d+]] ParameterValue
147 // CHECK-DAG: [[ParamY:i\d+]] ParameterValue
148 // CHECK-DAG: [[ParamZ:i\d+]] ParameterValue
149 // CHECK-DAG: [[CmpXY:z\d+]] LessThanOrEqual [ [[ParamX]] [[ParamY]] ]
150 // CHECK-DAG: [[CmpYZ:z\d+]] LessThanOrEqual [ [[ParamY]] [[ParamZ]] ]
151 // CHECK-DAG: [[CmpXYZ:z\d+]] Equal [ [[CmpXY]] [[CmpYZ]] ]
152 // CHECK-DAG: Return [ [[CmpXYZ]] ]
153
154 public static boolean ValuesOrdered(int x, int y, int z) {
155 return (x <= y) == (y <= z);
156 }
157
158 public static void main(String[] args) {
159 assertBoolEquals(false, BooleanNot(true));
160 assertBoolEquals(true, BooleanNot(false));
161 assertBoolEquals(true, GreaterThan(10, 5));
162 assertBoolEquals(false, GreaterThan(10, 10));
163 assertBoolEquals(false, GreaterThan(5, 10));
164 assertBoolEquals(true, LessThan(5, 10));
165 assertBoolEquals(false, LessThan(10, 10));
166 assertBoolEquals(false, LessThan(10, 5));
167 assertBoolEquals(true, ValuesOrdered(1, 3, 5));
168 assertBoolEquals(true, ValuesOrdered(5, 3, 1));
169 assertBoolEquals(false, ValuesOrdered(1, 3, 2));
170 assertBoolEquals(false, ValuesOrdered(2, 3, 1));
171 assertBoolEquals(true, ValuesOrdered(3, 3, 3));
172 assertBoolEquals(true, ValuesOrdered(3, 3, 5));
173 assertBoolEquals(false, ValuesOrdered(5, 5, 3));
174 }
175}