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