blob: 6e68c6c80d6be29f572fec4fcbb1f188c66bcfbb [file] [log] [blame]
Artem Serov15f95b12018-06-29 15:30:36 +01001/*
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"
20#include "builder.h"
21#include "nodes.h"
22#include "optimizing_unit_test.h"
23#include "side_effects_analysis.h"
24
Vladimir Marko0a516052019-10-14 13:00:44 +000025namespace art {
Artem Serov15f95b12018-06-29 15:30:36 +010026
27class SelectGeneratorTest : public ImprovedOptimizingUnitTest {
Evgeny Astigeevich52506e22019-12-04 15:59:37 +000028 private:
29 void CreateParameters() override {
30 parameters_.push_back(new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
31 dex::TypeIndex(0),
32 0,
33 DataType::Type::kInt32));
34 }
35
Artem Serov15f95b12018-06-29 15:30:36 +010036 public:
37 void ConstructBasicGraphForSelect(HInstruction* instr) {
38 HBasicBlock* if_block = new (GetAllocator()) HBasicBlock(graph_);
39 HBasicBlock* then_block = new (GetAllocator()) HBasicBlock(graph_);
40 HBasicBlock* else_block = new (GetAllocator()) HBasicBlock(graph_);
41
42 graph_->AddBlock(if_block);
43 graph_->AddBlock(then_block);
44 graph_->AddBlock(else_block);
45
46 entry_block_->ReplaceSuccessor(return_block_, if_block);
47
48 if_block->AddSuccessor(then_block);
49 if_block->AddSuccessor(else_block);
50 then_block->AddSuccessor(return_block_);
51 else_block->AddSuccessor(return_block_);
52
53 HParameterValue* bool_param = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
54 dex::TypeIndex(0),
55 1,
56 DataType::Type::kBool);
57 entry_block_->AddInstruction(bool_param);
58 HIntConstant* const1 = graph_->GetIntConstant(1);
59
60 if_block->AddInstruction(new (GetAllocator()) HIf(bool_param));
61
62 then_block->AddInstruction(instr);
63 then_block->AddInstruction(new (GetAllocator()) HGoto());
64
65 else_block->AddInstruction(new (GetAllocator()) HGoto());
66
67 HPhi* phi = new (GetAllocator()) HPhi(GetAllocator(), 0, 0, DataType::Type::kInt32);
68 return_block_->AddPhi(phi);
69 phi->AddInput(instr);
70 phi->AddInput(const1);
71 }
72
73 bool CheckGraphAndTrySelectGenerator() {
74 graph_->BuildDominatorTree();
75 EXPECT_TRUE(CheckGraph());
76
77 SideEffectsAnalysis side_effects(graph_);
78 side_effects.Run();
79 return HSelectGenerator(graph_, /*handles*/ nullptr, /*stats*/ nullptr).Run();
80 }
81};
82
83// HDivZeroCheck might throw and should not be hoisted from the conditional to an unconditional.
84TEST_F(SelectGeneratorTest, testZeroCheck) {
85 InitGraph();
Evgeny Astigeevich52506e22019-12-04 15:59:37 +000086 HDivZeroCheck* instr = new (GetAllocator()) HDivZeroCheck(parameters_[0], 0);
Artem Serov15f95b12018-06-29 15:30:36 +010087 ConstructBasicGraphForSelect(instr);
88
Evgeny Astigeevich52506e22019-12-04 15:59:37 +000089 ArenaVector<HInstruction*> current_locals({parameters_[0], graph_->GetIntConstant(1)},
Artem Serov15f95b12018-06-29 15:30:36 +010090 GetAllocator()->Adapter(kArenaAllocInstruction));
91 ManuallyBuildEnvFor(instr, &current_locals);
92
93 EXPECT_FALSE(CheckGraphAndTrySelectGenerator());
94}
95
96// Test that SelectGenerator succeeds with HAdd.
97TEST_F(SelectGeneratorTest, testAdd) {
98 InitGraph();
Evgeny Astigeevich52506e22019-12-04 15:59:37 +000099 HAdd* instr = new (GetAllocator()) HAdd(DataType::Type::kInt32,
100 parameters_[0],
101 parameters_[0], 0);
Artem Serov15f95b12018-06-29 15:30:36 +0100102 ConstructBasicGraphForSelect(instr);
103 EXPECT_TRUE(CheckGraphAndTrySelectGenerator());
104}
105
106} // namespace art