blob: 30ac8a86eb516137d5b16ca0a528d4b8f87a3272 [file] [log] [blame]
David Brazdil74eb1b22015-12-14 11:44:01 +00001/*
2 * Copyright (C) 2016 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
17/*
18 * This optimization recognizes the common diamond selection pattern and
19 * replaces it with an instance of the HSelect instruction.
20 *
Mads Ager16e52892017-07-14 13:11:37 +020021 * Recognized patterns:
David Brazdil74eb1b22015-12-14 11:44:01 +000022 *
23 * If [ Condition ]
24 * / \
25 * false branch true branch
26 * \ /
27 * Phi [FalseValue, TrueValue]
28 *
Mads Ager16e52892017-07-14 13:11:37 +020029 * and
30 *
31 * If [ Condition ]
32 * / \
33 * false branch true branch
34 * return FalseValue return TrueValue
35 *
David Brazdil74eb1b22015-12-14 11:44:01 +000036 * The pattern will be simplified if `true_branch` and `false_branch` each
37 * contain at most one instruction without any side effects.
38 *
Mads Ager16e52892017-07-14 13:11:37 +020039 * Blocks are merged into one and Select replaces the If and the Phi.
40 *
41 * For the first pattern it simplifies to:
42 *
David Brazdil74eb1b22015-12-14 11:44:01 +000043 * true branch
44 * false branch
45 * Select [FalseValue, TrueValue, Condition]
46 *
Mads Ager16e52892017-07-14 13:11:37 +020047 * For the second pattern it simplifies to:
48 *
49 * true branch
50 * false branch
51 * return Select [FalseValue, TrueValue, Condition]
52 *
David Brazdil74eb1b22015-12-14 11:44:01 +000053 * Note: In order to recognize no side-effect blocks, this optimization must be
54 * run after the instruction simplifier has removed redundant suspend checks.
55 */
56
57#ifndef ART_COMPILER_OPTIMIZING_SELECT_GENERATOR_H_
58#define ART_COMPILER_OPTIMIZING_SELECT_GENERATOR_H_
59
60#include "optimization.h"
61
Vladimir Marko0a516052019-10-14 13:00:44 +000062namespace art {
David Brazdil74eb1b22015-12-14 11:44:01 +000063
64class HSelectGenerator : public HOptimization {
65 public:
Mads Ager16e52892017-07-14 13:11:37 +020066 HSelectGenerator(HGraph* graph,
Aart Bik2ca10eb2017-11-15 15:17:53 -080067 OptimizingCompilerStats* stats,
68 const char* name = kSelectGeneratorPassName);
David Brazdil74eb1b22015-12-14 11:44:01 +000069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010070 bool Run() override;
David Brazdil74eb1b22015-12-14 11:44:01 +000071
72 static constexpr const char* kSelectGeneratorPassName = "select_generator";
73
74 private:
David Brazdil74eb1b22015-12-14 11:44:01 +000075 DISALLOW_COPY_AND_ASSIGN(HSelectGenerator);
76};
77
78} // namespace art
79
80#endif // ART_COMPILER_OPTIMIZING_SELECT_GENERATOR_H_