blob: 6e6549737c6049ada519c669e3ccc855b2ab851b [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
25namespace art {
26
27class SelectGeneratorTest : public ImprovedOptimizingUnitTest {
28 public:
29 void ConstructBasicGraphForSelect(HInstruction* instr) {
30 HBasicBlock* if_block = new (GetAllocator()) HBasicBlock(graph_);
31 HBasicBlock* then_block = new (GetAllocator()) HBasicBlock(graph_);
32 HBasicBlock* else_block = new (GetAllocator()) HBasicBlock(graph_);
33
34 graph_->AddBlock(if_block);
35 graph_->AddBlock(then_block);
36 graph_->AddBlock(else_block);
37
38 entry_block_->ReplaceSuccessor(return_block_, if_block);
39
40 if_block->AddSuccessor(then_block);
41 if_block->AddSuccessor(else_block);
42 then_block->AddSuccessor(return_block_);
43 else_block->AddSuccessor(return_block_);
44
45 HParameterValue* bool_param = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
46 dex::TypeIndex(0),
47 1,
48 DataType::Type::kBool);
49 entry_block_->AddInstruction(bool_param);
50 HIntConstant* const1 = graph_->GetIntConstant(1);
51
52 if_block->AddInstruction(new (GetAllocator()) HIf(bool_param));
53
54 then_block->AddInstruction(instr);
55 then_block->AddInstruction(new (GetAllocator()) HGoto());
56
57 else_block->AddInstruction(new (GetAllocator()) HGoto());
58
59 HPhi* phi = new (GetAllocator()) HPhi(GetAllocator(), 0, 0, DataType::Type::kInt32);
60 return_block_->AddPhi(phi);
61 phi->AddInput(instr);
62 phi->AddInput(const1);
63 }
64
65 bool CheckGraphAndTrySelectGenerator() {
66 graph_->BuildDominatorTree();
67 EXPECT_TRUE(CheckGraph());
68
69 SideEffectsAnalysis side_effects(graph_);
70 side_effects.Run();
71 return HSelectGenerator(graph_, /*handles*/ nullptr, /*stats*/ nullptr).Run();
72 }
73};
74
75// HDivZeroCheck might throw and should not be hoisted from the conditional to an unconditional.
76TEST_F(SelectGeneratorTest, testZeroCheck) {
77 InitGraph();
78 HDivZeroCheck* instr = new (GetAllocator()) HDivZeroCheck(parameter_, 0);
79 ConstructBasicGraphForSelect(instr);
80
81 ArenaVector<HInstruction*> current_locals({parameter_, graph_->GetIntConstant(1)},
82 GetAllocator()->Adapter(kArenaAllocInstruction));
83 ManuallyBuildEnvFor(instr, &current_locals);
84
85 EXPECT_FALSE(CheckGraphAndTrySelectGenerator());
86}
87
88// Test that SelectGenerator succeeds with HAdd.
89TEST_F(SelectGeneratorTest, testAdd) {
90 InitGraph();
91 HAdd* instr = new (GetAllocator()) HAdd(DataType::Type::kInt32, parameter_, parameter_, 0);
92 ConstructBasicGraphForSelect(instr);
93 EXPECT_TRUE(CheckGraphAndTrySelectGenerator());
94}
95
96} // namespace art