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_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 
TestCode(const uint16_t * data)31 static void TestCode(const uint16_t* data) {
32   ArenaPool pool;
33   ArenaAllocator allocator(&pool);
34   HGraph* graph = CreateCFG(&allocator, data);
35   HBasicBlock* first_block = graph->GetEntryBlock()->GetSingleSuccessor();
36   HBasicBlock* loop_header = first_block->GetSingleSuccessor();
37   ASSERT_TRUE(loop_header->IsLoopHeader());
38   ASSERT_EQ(loop_header->GetLoopInformation()->GetPreHeader(), first_block);
39   ASSERT_TRUE(loop_header->GetFirstInstruction()->IsSuspendCheck());
40 }
41 
42 class SuspendCheckTest : public CommonCompilerTest {};
43 
TEST_F(SuspendCheckTest,CFG1)44 TEST_F(SuspendCheckTest, CFG1) {
45   const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
46     Instruction::NOP,
47     Instruction::GOTO | 0xFF00);
48 
49   TestCode(data);
50 }
51 
TEST_F(SuspendCheckTest,CFG2)52 TEST_F(SuspendCheckTest, CFG2) {
53   const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
54     Instruction::GOTO_32, 0, 0);
55 
56   TestCode(data);
57 }
58 
TEST_F(SuspendCheckTest,CFG3)59 TEST_F(SuspendCheckTest, CFG3) {
60   const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
61     Instruction::CONST_4 | 0 | 0,
62     Instruction::IF_EQ, 0xFFFF,
63     Instruction::RETURN_VOID);
64 
65   TestCode(data);
66 }
67 
TEST_F(SuspendCheckTest,CFG4)68 TEST_F(SuspendCheckTest, CFG4) {
69   const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
70     Instruction::CONST_4 | 0 | 0,
71     Instruction::IF_NE, 0xFFFF,
72     Instruction::RETURN_VOID);
73 
74   TestCode(data);
75 }
76 
TEST_F(SuspendCheckTest,CFG5)77 TEST_F(SuspendCheckTest, CFG5) {
78   const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
79     Instruction::CONST_4 | 0 | 0,
80     Instruction::IF_EQZ, 0xFFFF,
81     Instruction::RETURN_VOID);
82 
83   TestCode(data);
84 }
85 
TEST_F(SuspendCheckTest,CFG6)86 TEST_F(SuspendCheckTest, CFG6) {
87   const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
88     Instruction::CONST_4 | 0 | 0,
89     Instruction::IF_NEZ, 0xFFFF,
90     Instruction::RETURN_VOID);
91 
92   TestCode(data);
93 }
94 }  // namespace art
95