1 /*
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  *
21  * Recognized pattern:
22  *
23  *          If [ Condition ]
24  *            /          \
25  *      false branch  true branch
26  *            \          /
27  *     Phi [FalseValue, TrueValue]
28  *
29  * The pattern will be simplified if `true_branch` and `false_branch` each
30  * contain at most one instruction without any side effects.
31  *
32  * Blocks are merged into one and Select replaces the If and the Phi:
33  *              true branch
34  *              false branch
35  *              Select [FalseValue, TrueValue, Condition]
36  *
37  * Note: In order to recognize no side-effect blocks, this optimization must be
38  * run after the instruction simplifier has removed redundant suspend checks.
39  */
40 
41 #ifndef ART_COMPILER_OPTIMIZING_SELECT_GENERATOR_H_
42 #define ART_COMPILER_OPTIMIZING_SELECT_GENERATOR_H_
43 
44 #include "optimization.h"
45 
46 namespace art {
47 
48 class HSelectGenerator : public HOptimization {
49  public:
HSelectGenerator(HGraph * graph,OptimizingCompilerStats * stats)50   HSelectGenerator(HGraph* graph, OptimizingCompilerStats* stats)
51     : HOptimization(graph, kSelectGeneratorPassName, stats) {}
52 
53   void Run() OVERRIDE;
54 
55   static constexpr const char* kSelectGeneratorPassName = "select_generator";
56 
57  private:
58   DISALLOW_COPY_AND_ASSIGN(HSelectGenerator);
59 };
60 
61 }  // namespace art
62 
63 #endif  // ART_COMPILER_OPTIMIZING_SELECT_GENERATOR_H_
64