1 /*
2  * Copyright (C) 2014 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 "builder.h"
18 #include "dex/dex_instruction.h"
19 #include "nodes.h"
20 #include "optimizing_unit_test.h"
21 #include "pretty_printer.h"
22 
23 #include "gtest/gtest.h"
24 
25 namespace art {
26 
27 /**
28  * Check that the HGraphBuilder adds suspend checks to backward branches.
29  */
30 
31 class SuspendCheckTest : public OptimizingUnitTest {
32  protected:
33   void TestCode(const std::vector<uint16_t>& data);
34 };
35 
TestCode(const std::vector<uint16_t> & data)36 void SuspendCheckTest::TestCode(const std::vector<uint16_t>& data) {
37   HGraph* graph = CreateCFG(data);
38   HBasicBlock* first_block = graph->GetEntryBlock()->GetSingleSuccessor();
39   HBasicBlock* loop_header = first_block->GetSingleSuccessor();
40   ASSERT_TRUE(loop_header->IsLoopHeader());
41   ASSERT_EQ(loop_header->GetLoopInformation()->GetPreHeader(), first_block);
42   ASSERT_TRUE(loop_header->GetFirstInstruction()->IsSuspendCheck());
43 }
44 
TEST_F(SuspendCheckTest,CFG1)45 TEST_F(SuspendCheckTest, CFG1) {
46   const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
47     Instruction::NOP,
48     Instruction::GOTO | 0xFF00);
49 
50   TestCode(data);
51 }
52 
TEST_F(SuspendCheckTest,CFG2)53 TEST_F(SuspendCheckTest, CFG2) {
54   const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
55     Instruction::GOTO_32, 0, 0);
56 
57   TestCode(data);
58 }
59 
TEST_F(SuspendCheckTest,CFG3)60 TEST_F(SuspendCheckTest, CFG3) {
61   const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
62     Instruction::CONST_4 | 0 | 0,
63     Instruction::IF_EQ, 0xFFFF,
64     Instruction::RETURN_VOID);
65 
66   TestCode(data);
67 }
68 
TEST_F(SuspendCheckTest,CFG4)69 TEST_F(SuspendCheckTest, CFG4) {
70   const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
71     Instruction::CONST_4 | 0 | 0,
72     Instruction::IF_NE, 0xFFFF,
73     Instruction::RETURN_VOID);
74 
75   TestCode(data);
76 }
77 
TEST_F(SuspendCheckTest,CFG5)78 TEST_F(SuspendCheckTest, CFG5) {
79   const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
80     Instruction::CONST_4 | 0 | 0,
81     Instruction::IF_EQZ, 0xFFFF,
82     Instruction::RETURN_VOID);
83 
84   TestCode(data);
85 }
86 
TEST_F(SuspendCheckTest,CFG6)87 TEST_F(SuspendCheckTest, CFG6) {
88   const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
89     Instruction::CONST_4 | 0 | 0,
90     Instruction::IF_NEZ, 0xFFFF,
91     Instruction::RETURN_VOID);
92 
93   TestCode(data);
94 }
95 }  // namespace art
96