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"
20 #include "base/macros.h"
21 #include "builder.h"
22 #include "nodes.h"
23 #include "optimizing_unit_test.h"
24 #include "side_effects_analysis.h"
25
26 namespace art HIDDEN {
27
28 class SelectGeneratorTest : public OptimizingUnitTest {
29 protected:
ConstructBasicGraphForSelect(HBasicBlock * return_block,HInstruction * instr)30 HPhi* ConstructBasicGraphForSelect(HBasicBlock* return_block, HInstruction* instr) {
31 HParameterValue* bool_param = MakeParam(DataType::Type::kBool);
32 HIntConstant* const1 = graph_->GetIntConstant(1);
33
34 auto [if_block, then_block, else_block] = CreateDiamondPattern(return_block, bool_param);
35
36 AddOrInsertInstruction(then_block, instr);
37 HPhi* phi = MakePhi(return_block, {instr, const1});
38 return phi;
39 }
40
CheckGraphAndTrySelectGenerator()41 bool CheckGraphAndTrySelectGenerator() {
42 graph_->BuildDominatorTree();
43 EXPECT_TRUE(CheckGraph());
44
45 SideEffectsAnalysis side_effects(graph_);
46 side_effects.Run();
47 return HSelectGenerator(graph_, /*handles*/ nullptr, /*stats*/ nullptr).Run();
48 }
49 };
50
51 // HDivZeroCheck might throw and should not be hoisted from the conditional to an unconditional.
TEST_F(SelectGeneratorTest,testZeroCheck)52 TEST_F(SelectGeneratorTest, testZeroCheck) {
53 HBasicBlock* return_block = InitEntryMainExitGraphWithReturnVoid();
54 HParameterValue* param = MakeParam(DataType::Type::kInt32);
55 HDivZeroCheck* instr = new (GetAllocator()) HDivZeroCheck(param, 0);
56 HPhi* phi = ConstructBasicGraphForSelect(return_block, instr);
57
58 ManuallyBuildEnvFor(instr, {param, graph_->GetIntConstant(1)});
59
60 EXPECT_FALSE(CheckGraphAndTrySelectGenerator());
61 EXPECT_FALSE(phi->GetBlock() == nullptr);
62 }
63
64 // Test that SelectGenerator succeeds with HAdd.
TEST_F(SelectGeneratorTest,testAdd)65 TEST_F(SelectGeneratorTest, testAdd) {
66 HBasicBlock* return_block = InitEntryMainExitGraphWithReturnVoid();
67 HParameterValue* param = MakeParam(DataType::Type::kInt32);
68 HAdd* instr = new (GetAllocator()) HAdd(DataType::Type::kInt32, param, param, /*dex_pc=*/ 0);
69 HPhi* phi = ConstructBasicGraphForSelect(return_block, instr);
70 EXPECT_TRUE(CheckGraphAndTrySelectGenerator());
71 EXPECT_TRUE(phi->GetBlock() == nullptr);
72 }
73
74 } // namespace art
75