Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "select_generator.h" |
| 18 | |
| 19 | #include "base/arena_allocator.h" |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 20 | #include "base/macros.h" |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 21 | #include "builder.h" |
| 22 | #include "nodes.h" |
| 23 | #include "optimizing_unit_test.h" |
| 24 | #include "side_effects_analysis.h" |
| 25 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 26 | namespace art HIDDEN { |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 27 | |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 28 | class SelectGeneratorTest : public OptimizingUnitTest { |
| 29 | protected: |
| 30 | void InitGraphAndParameters() { |
| 31 | InitGraph(); |
| 32 | AddParameter(new (GetAllocator()) HParameterValue(graph_->GetDexFile(), |
| 33 | dex::TypeIndex(0), |
| 34 | 0, |
| 35 | DataType::Type::kInt32)); |
Evgeny Astigeevich | 52506e2 | 2019-12-04 15:59:37 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 38 | void ConstructBasicGraphForSelect(HInstruction* instr) { |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 39 | HBasicBlock* if_block = AddNewBlock(); |
| 40 | HBasicBlock* then_block = AddNewBlock(); |
| 41 | HBasicBlock* else_block = AddNewBlock(); |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 42 | |
| 43 | entry_block_->ReplaceSuccessor(return_block_, if_block); |
| 44 | |
| 45 | if_block->AddSuccessor(then_block); |
| 46 | if_block->AddSuccessor(else_block); |
| 47 | then_block->AddSuccessor(return_block_); |
| 48 | else_block->AddSuccessor(return_block_); |
| 49 | |
| 50 | HParameterValue* bool_param = new (GetAllocator()) HParameterValue(graph_->GetDexFile(), |
| 51 | dex::TypeIndex(0), |
| 52 | 1, |
| 53 | DataType::Type::kBool); |
| 54 | entry_block_->AddInstruction(bool_param); |
| 55 | HIntConstant* const1 = graph_->GetIntConstant(1); |
| 56 | |
| 57 | if_block->AddInstruction(new (GetAllocator()) HIf(bool_param)); |
| 58 | |
| 59 | then_block->AddInstruction(instr); |
| 60 | then_block->AddInstruction(new (GetAllocator()) HGoto()); |
| 61 | |
| 62 | else_block->AddInstruction(new (GetAllocator()) HGoto()); |
| 63 | |
| 64 | HPhi* phi = new (GetAllocator()) HPhi(GetAllocator(), 0, 0, DataType::Type::kInt32); |
| 65 | return_block_->AddPhi(phi); |
| 66 | phi->AddInput(instr); |
| 67 | phi->AddInput(const1); |
| 68 | } |
| 69 | |
| 70 | bool CheckGraphAndTrySelectGenerator() { |
| 71 | graph_->BuildDominatorTree(); |
| 72 | EXPECT_TRUE(CheckGraph()); |
| 73 | |
| 74 | SideEffectsAnalysis side_effects(graph_); |
| 75 | side_effects.Run(); |
| 76 | return HSelectGenerator(graph_, /*handles*/ nullptr, /*stats*/ nullptr).Run(); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | // HDivZeroCheck might throw and should not be hoisted from the conditional to an unconditional. |
| 81 | TEST_F(SelectGeneratorTest, testZeroCheck) { |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 82 | InitGraphAndParameters(); |
Evgeny Astigeevich | 52506e2 | 2019-12-04 15:59:37 +0000 | [diff] [blame] | 83 | HDivZeroCheck* instr = new (GetAllocator()) HDivZeroCheck(parameters_[0], 0); |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 84 | ConstructBasicGraphForSelect(instr); |
| 85 | |
Evgeny Astigeevich | 52506e2 | 2019-12-04 15:59:37 +0000 | [diff] [blame] | 86 | ArenaVector<HInstruction*> current_locals({parameters_[0], graph_->GetIntConstant(1)}, |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 87 | GetAllocator()->Adapter(kArenaAllocInstruction)); |
| 88 | ManuallyBuildEnvFor(instr, ¤t_locals); |
| 89 | |
| 90 | EXPECT_FALSE(CheckGraphAndTrySelectGenerator()); |
| 91 | } |
| 92 | |
| 93 | // Test that SelectGenerator succeeds with HAdd. |
| 94 | TEST_F(SelectGeneratorTest, testAdd) { |
Vladimir Marko | 5d2311a | 2020-05-13 17:30:32 +0100 | [diff] [blame] | 95 | InitGraphAndParameters(); |
Evgeny Astigeevich | 52506e2 | 2019-12-04 15:59:37 +0000 | [diff] [blame] | 96 | HAdd* instr = new (GetAllocator()) HAdd(DataType::Type::kInt32, |
| 97 | parameters_[0], |
| 98 | parameters_[0], 0); |
Artem Serov | 15f95b1 | 2018-06-29 15:30:36 +0100 | [diff] [blame] | 99 | ConstructBasicGraphForSelect(instr); |
| 100 | EXPECT_TRUE(CheckGraphAndTrySelectGenerator()); |
| 101 | } |
| 102 | |
| 103 | } // namespace art |