summaryrefslogtreecommitdiffstats
path: root/test/463-checker-boolean-simplifier/src/Main.java
blob: 25f58b4b43966b16666800c69ee60b59cd4b0a18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public class Main {

  // Note #1: `javac` flips the conditions of If statements.
  // Note #2: In the optimizing compiler, the first input of Phi is always
  //          the fall-through path, i.e. the false branch.

  public static void assertBoolEquals(boolean expected, boolean result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  /*
   * Elementary test negating a boolean. Verifies that the condition is replaced,
   * blocks merged and empty branches removed.
   */

  // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (before)
  // CHECK-DAG:     [[Param:z\d+]]    ParameterValue
  // CHECK-DAG:     [[Const0:i\d+]]   IntConstant 0
  // CHECK-DAG:     [[Const1:i\d+]]   IntConstant 1
  // CHECK-DAG:     [[NotEq:z\d+]]    NotEqual [ [[Param]] [[Const0]] ]
  // CHECK-DAG:                       If [ [[NotEq]] ]
  // CHECK-DAG:     [[Phi:i\d+]]      Phi [ [[Const1]] [[Const0]] ]
  // CHECK-DAG:                       Return [ [[Phi]] ]

  // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (before)
  // CHECK:                           Goto
  // CHECK:                           Goto
  // CHECK:                           Goto
  // CHECK-NOT:                       Goto

  // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
  // CHECK-DAG:     [[Param:z\d+]]    ParameterValue
  // CHECK-DAG:     [[Const0:i\d+]]   IntConstant 0
  // CHECK-DAG:     [[Eq:z\d+]]       Equal [ [[Param]] [[Const0]] ]
  // CHECK-DAG:                       Return [ [[Eq]] ]

  // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
  // CHECK-NOT:                       NotEqual
  // CHECK-NOT:                       If
  // CHECK-NOT:                       Phi

  // CHECK-START: boolean Main.BooleanNot(boolean) boolean_simplifier (after)
  // CHECK:                           Goto
  // CHECK-NOT:                       Goto

  public static boolean BooleanNot(boolean x) {
    return !x;
  }

  /*
   * Program which only delegates the condition, i.e. returns 1 when True
   * and 0 when False.
   */

  // CHECK-START: boolean Main.GreaterThan(int, int) boolean_simplifier (before)
  // CHECK-DAG:     [[ParamX:i\d+]]   ParameterValue
  // CHECK-DAG:     [[ParamY:i\d+]]   ParameterValue
  // CHECK-DAG:     [[Const0:i\d+]]   IntConstant 0
  // CHECK-DAG:     [[Const1:i\d+]]   IntConstant 1
  // CHECK-DAG:     [[Cond:z\d+]]     GreaterThan [ [[ParamX]] [[ParamY]] ]
  // CHECK-DAG:                       If [ [[Cond]] ]
  // CHECK-DAG:     [[Phi:i\d+]]      Phi [ [[Const0]] [[Const1]] ]
  // CHECK-DAG:                       Return [ [[Phi]] ]

  // CHECK-START: boolean Main.GreaterThan(int, int) boolean_simplifier (after)
  // CHECK-DAG:     [[ParamX:i\d+]]   ParameterValue
  // CHECK-DAG:     [[ParamY:i\d+]]   ParameterValue
  // CHECK-DAG:     [[Const0:i\d+]]   IntConstant 0
  // CHECK-DAG:     [[Const1:i\d+]]   IntConstant 1
  // CHECK-DAG:     [[Cond:z\d+]]     GreaterThan [ [[ParamX]] [[ParamY]] ]
  // CHECK-DAG:                       Return [ [[Cond]] ]

  public static boolean GreaterThan(int x, int y) {
    return (x <= y) ? false : true;
  }

  /*
   * Program which negates a condition, i.e. returns 0 when True
   * and 1 when False.
   */

  // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (before)
  // CHECK-DAG:     [[ParamX:i\d+]]   ParameterValue
  // CHECK-DAG:     [[ParamY:i\d+]]   ParameterValue
  // CHECK-DAG:     [[Const0:i\d+]]   IntConstant 0
  // CHECK-DAG:     [[Const1:i\d+]]   IntConstant 1
  // CHECK-DAG:     [[Cond:z\d+]]     GreaterThanOrEqual [ [[ParamX]] [[ParamY]] ]
  // CHECK-DAG:                       If [ [[Cond]] ]
  // CHECK-DAG:     [[Phi:i\d+]]      Phi [ [[Const1]] [[Const0]] ]
  // CHECK-DAG:                       Return [ [[Phi]] ]

  // CHECK-START: boolean Main.LessThan(int, int) boolean_simplifier (after)
  // CHECK-DAG:     [[ParamX:i\d+]]   ParameterValue
  // CHECK-DAG:     [[ParamY:i\d+]]   ParameterValue
  // CHECK-DAG:     [[Const0:i\d+]]   IntConstant 0
  // CHECK-DAG:     [[Const1:i\d+]]   IntConstant 1
  // CHECK-DAG:     [[Cond:z\d+]]     LessThan [ [[ParamX]] [[ParamY]] ]
  // CHECK-DAG:                       Return [ [[Cond]] ]

  public static boolean LessThan(int x, int y) {
    return x < y;
  }

  /*
   * Program which further uses negated conditions.
   * Note that Phis are discovered retrospectively.
   */

  // CHECK-START: boolean Main.ValuesOrdered(int, int, int) boolean_simplifier (before)
  // CHECK-DAG:     [[ParamX:i\d+]]   ParameterValue
  // CHECK-DAG:     [[ParamY:i\d+]]   ParameterValue
  // CHECK-DAG:     [[ParamZ:i\d+]]   ParameterValue
  // CHECK-DAG:     [[Const0:i\d+]]   IntConstant 0
  // CHECK-DAG:     [[Const1:i\d+]]   IntConstant 1
  // CHECK-DAG:     [[CondXY:z\d+]]   GreaterThan [ [[ParamX]] [[ParamY]] ]
  // CHECK-DAG:                       If [ [[CondXY]] ]
  // CHECK-DAG:     [[CondYZ:z\d+]]   GreaterThan [ [[ParamY]] [[ParamZ]] ]
  // CHECK-DAG:                       If [ [[CondYZ]] ]
  // CHECK-DAG:     [[CondXYZ:z\d+]]  NotEqual [ [[PhiXY:i\d+]] [[PhiYZ:i\d+]] ]
  // CHECK-DAG:                       If [ [[CondXYZ]] ]
  // CHECK-DAG:                       Return [ [[PhiXYZ:i\d+]] ]
  // CHECK-DAG:     [[PhiXY]]         Phi [ [[Const1]] [[Const0]] ]
  // CHECK-DAG:     [[PhiYZ]]         Phi [ [[Const1]] [[Const0]] ]
  // CHECK-DAG:     [[PhiXYZ]]        Phi [ [[Const1]] [[Const0]] ]

  // CHECK-START: boolean Main.ValuesOrdered(int, int, int) boolean_simplifier (after)
  // CHECK-DAG:     [[ParamX:i\d+]]   ParameterValue
  // CHECK-DAG:     [[ParamY:i\d+]]   ParameterValue
  // CHECK-DAG:     [[ParamZ:i\d+]]   ParameterValue
  // CHECK-DAG:     [[CmpXY:z\d+]]    LessThanOrEqual [ [[ParamX]] [[ParamY]] ]
  // CHECK-DAG:     [[CmpYZ:z\d+]]    LessThanOrEqual [ [[ParamY]] [[ParamZ]] ]
  // CHECK-DAG:     [[CmpXYZ:z\d+]]   Equal [ [[CmpXY]] [[CmpYZ]] ]
  // CHECK-DAG:                       Return [ [[CmpXYZ]] ]

  public static boolean ValuesOrdered(int x, int y, int z) {
    return (x <= y) == (y <= z);
  }

  public static void main(String[] args) {
    assertBoolEquals(false, BooleanNot(true));
    assertBoolEquals(true, BooleanNot(false));
    assertBoolEquals(true, GreaterThan(10, 5));
    assertBoolEquals(false, GreaterThan(10, 10));
    assertBoolEquals(false, GreaterThan(5, 10));
    assertBoolEquals(true, LessThan(5, 10));
    assertBoolEquals(false, LessThan(10, 10));
    assertBoolEquals(false, LessThan(10, 5));
    assertBoolEquals(true, ValuesOrdered(1, 3, 5));
    assertBoolEquals(true, ValuesOrdered(5, 3, 1));
    assertBoolEquals(false, ValuesOrdered(1, 3, 2));
    assertBoolEquals(false, ValuesOrdered(2, 3, 1));
    assertBoolEquals(true, ValuesOrdered(3, 3, 3));
    assertBoolEquals(true, ValuesOrdered(3, 3, 5));
    assertBoolEquals(false, ValuesOrdered(5, 5, 3));
  }
}