blob: e12a12c95bf81e1bc7015f220c141c0f00702209 [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
David Brazdil769c9e52015-04-27 13:54:09 +010017// This optimization recognizes two common patterns:
18// (a) Boolean selection: Casting a boolean to an integer or negating it is
19// carried out with an If statement selecting from zero/one integer
20// constants. Because Boolean values are represented as zero/one, the
21// pattern can be replaced with the condition instruction itself or its
22// negation, depending on the layout.
23// (b) Negated condition: Instruction simplifier may replace an If's condition
24// with a boolean value. If this value is the result of a Boolean negation,
25// the true/false branches can be swapped and negation removed.
David Brazdil46e2a392015-03-16 17:31:52 +000026
27// Example: Negating a boolean value
28// B1:
29// z1 ParameterValue
30// i2 IntConstant 0
31// i3 IntConstant 1
32// v4 Goto B2
33// B2:
34// z5 NotEquals [ z1 i2 ]
35// v6 If [ z5 ] then B3 else B4
36// B3:
37// v7 Goto B5
38// B4:
39// v8 Goto B5
40// B5:
41// i9 Phi [ i3 i2 ]
42// v10 Return [ i9 ]
43// turns into
44// B1:
45// z1 ParameterValue
46// i2 IntConstant 0
47// v4 Goto B2
48// B2:
49// z11 Equals [ z1 i2 ]
50// v10 Return [ z11 ]
51// B3, B4, B5: removed
52
53// Note: in order to recognize empty blocks, this optimization must be run
54// after the instruction simplifier has removed redundant suspend checks.
55
56#ifndef ART_COMPILER_OPTIMIZING_BOOLEAN_SIMPLIFIER_H_
57#define ART_COMPILER_OPTIMIZING_BOOLEAN_SIMPLIFIER_H_
58
59#include "optimization.h"
60
61namespace art {
62
63class HBooleanSimplifier : public HOptimization {
64 public:
65 explicit HBooleanSimplifier(HGraph* graph)
David Brazdil69ba7b72015-06-23 18:27:30 +010066 : HOptimization(graph, kBooleanSimplifierPassName) {}
David Brazdil46e2a392015-03-16 17:31:52 +000067
68 void Run() OVERRIDE;
69
70 static constexpr const char* kBooleanSimplifierPassName = "boolean_simplifier";
71
72 private:
David Brazdil769c9e52015-04-27 13:54:09 +010073 void TryRemovingNegatedCondition(HBasicBlock* block);
74 void TryRemovingBooleanSelection(HBasicBlock* block);
75
David Brazdil46e2a392015-03-16 17:31:52 +000076 DISALLOW_COPY_AND_ASSIGN(HBooleanSimplifier);
77};
78
79} // namespace art
80
81#endif // ART_COMPILER_OPTIMIZING_BOOLEAN_SIMPLIFIER_H_