David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | // This optimization recognizes a common pattern where a boolean value is |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 18 | // either cast to an integer or negated by selecting from zero/one integer |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 19 | // constants with an If statement. Because boolean values are internally |
| 20 | // represented as zero/one, we can safely replace the pattern with a suitable |
| 21 | // condition instruction. |
| 22 | |
| 23 | // Example: Negating a boolean value |
| 24 | // B1: |
| 25 | // z1 ParameterValue |
| 26 | // i2 IntConstant 0 |
| 27 | // i3 IntConstant 1 |
| 28 | // v4 Goto B2 |
| 29 | // B2: |
| 30 | // z5 NotEquals [ z1 i2 ] |
| 31 | // v6 If [ z5 ] then B3 else B4 |
| 32 | // B3: |
| 33 | // v7 Goto B5 |
| 34 | // B4: |
| 35 | // v8 Goto B5 |
| 36 | // B5: |
| 37 | // i9 Phi [ i3 i2 ] |
| 38 | // v10 Return [ i9 ] |
| 39 | // turns into |
| 40 | // B1: |
| 41 | // z1 ParameterValue |
| 42 | // i2 IntConstant 0 |
| 43 | // v4 Goto B2 |
| 44 | // B2: |
| 45 | // z11 Equals [ z1 i2 ] |
| 46 | // v10 Return [ z11 ] |
| 47 | // B3, B4, B5: removed |
| 48 | |
| 49 | // Note: in order to recognize empty blocks, this optimization must be run |
| 50 | // after the instruction simplifier has removed redundant suspend checks. |
| 51 | |
| 52 | #ifndef ART_COMPILER_OPTIMIZING_BOOLEAN_SIMPLIFIER_H_ |
| 53 | #define ART_COMPILER_OPTIMIZING_BOOLEAN_SIMPLIFIER_H_ |
| 54 | |
| 55 | #include "optimization.h" |
| 56 | |
| 57 | namespace art { |
| 58 | |
| 59 | class HBooleanSimplifier : public HOptimization { |
| 60 | public: |
| 61 | explicit HBooleanSimplifier(HGraph* graph) |
| 62 | : HOptimization(graph, true, kBooleanSimplifierPassName) {} |
| 63 | |
| 64 | void Run() OVERRIDE; |
| 65 | |
| 66 | static constexpr const char* kBooleanSimplifierPassName = "boolean_simplifier"; |
| 67 | |
| 68 | private: |
| 69 | DISALLOW_COPY_AND_ASSIGN(HBooleanSimplifier); |
| 70 | }; |
| 71 | |
| 72 | } // namespace art |
| 73 | |
| 74 | #endif // ART_COMPILER_OPTIMIZING_BOOLEAN_SIMPLIFIER_H_ |