blob: b18d41abbba20b7b10d18fc94396d1884fa10f54 [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
Vladimir Marko5d2311a2020-05-13 17:30:32 +010027class SelectGeneratorTest : public OptimizingUnitTest {
28 protected:
29 void InitGraphAndParameters() {
30 InitGraph();
31 AddParameter(new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
32 dex::TypeIndex(0),
33 0,
34 DataType::Type::kInt32));
Evgeny Astigeevich52506e22019-12-04 15:59:37 +000035 }
36
Artem Serov15f95b12018-06-29 15:30:36 +010037 void ConstructBasicGraphForSelect(HInstruction* instr) {
Vladimir Marko5d2311a2020-05-13 17:30:32 +010038 HBasicBlock* if_block = AddNewBlock();
39 HBasicBlock* then_block = AddNewBlock();
40 HBasicBlock* else_block = AddNewBlock();
Artem Serov15f95b12018-06-29 15:30:36 +010041
42 entry_block_->ReplaceSuccessor(return_block_, if_block);
43
44 if_block->AddSuccessor(then_block);
45 if_block->AddSuccessor(else_block);
46 then_block->AddSuccessor(return_block_);
47 else_block->AddSuccessor(return_block_);
48
49 HParameterValue* bool_param = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
50 dex::TypeIndex(0),
51 1,
52 DataType::Type::kBool);
53 entry_block_->AddInstruction(bool_param);
54 HIntConstant* const1 = graph_->GetIntConstant(1);
55
56 if_block->AddInstruction(new (GetAllocator()) HIf(bool_param));
57
58 then_block->AddInstruction(instr);
59 then_block->AddInstruction(new (GetAllocator()) HGoto());
60
61 else_block->AddInstruction(new (GetAllocator()) HGoto());
62
63 HPhi* phi = new (GetAllocator()) HPhi(GetAllocator(), 0, 0, DataType::Type::kInt32);
64 return_block_->AddPhi(phi);
65 phi->AddInput(instr);
66 phi->AddInput(const1);
67 }
68
69 bool CheckGraphAndTrySelectGenerator() {
70 graph_->BuildDominatorTree();
71 EXPECT_TRUE(CheckGraph());
72
73 SideEffectsAnalysis side_effects(graph_);
74 side_effects.Run();
75 return HSelectGenerator(graph_, /*handles*/ nullptr, /*stats*/ nullptr).Run();
76 }
77};
78
79// HDivZeroCheck might throw and should not be hoisted from the conditional to an unconditional.
80TEST_F(SelectGeneratorTest, testZeroCheck) {
Vladimir Marko5d2311a2020-05-13 17:30:32 +010081 InitGraphAndParameters();
Evgeny Astigeevich52506e22019-12-04 15:59:37 +000082 HDivZeroCheck* instr = new (GetAllocator()) HDivZeroCheck(parameters_[0], 0);
Artem Serov15f95b12018-06-29 15:30:36 +010083 ConstructBasicGraphForSelect(instr);
84
Evgeny Astigeevich52506e22019-12-04 15:59:37 +000085 ArenaVector<HInstruction*> current_locals({parameters_[0], graph_->GetIntConstant(1)},
Artem Serov15f95b12018-06-29 15:30:36 +010086 GetAllocator()->Adapter(kArenaAllocInstruction));
87 ManuallyBuildEnvFor(instr, &current_locals);
88
89 EXPECT_FALSE(CheckGraphAndTrySelectGenerator());
90}
91
92// Test that SelectGenerator succeeds with HAdd.
93TEST_F(SelectGeneratorTest, testAdd) {
Vladimir Marko5d2311a2020-05-13 17:30:32 +010094 InitGraphAndParameters();
Evgeny Astigeevich52506e22019-12-04 15:59:37 +000095 HAdd* instr = new (GetAllocator()) HAdd(DataType::Type::kInt32,
96 parameters_[0],
97 parameters_[0], 0);
Artem Serov15f95b12018-06-29 15:30:36 +010098 ConstructBasicGraphForSelect(instr);
99 EXPECT_TRUE(CheckGraphAndTrySelectGenerator());
100}
101
102} // namespace art