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 
22 #include "gtest/gtest.h"
23 
24 namespace art {
25 
26 /**
27  * Check that the HGraphBuilder adds suspend checks to backward branches.
28  */
29 
TestCode(const uint16_t * data)30 static void TestCode(const uint16_t* data) {
31   ArenaPool pool;
32   ArenaAllocator allocator(&pool);
33   HGraph* graph = CreateGraph(&allocator);
34   HGraphBuilder builder(graph);
35   const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
36   bool graph_built = builder.BuildGraph(*item);
37   ASSERT_TRUE(graph_built);
38 
39   HBasicBlock* first_block = graph->GetEntryBlock()->GetSuccessors().Get(0);
40   HInstruction* first_instruction = first_block->GetFirstInstruction();
41   // Account for some tests having a store local as first instruction.
42   ASSERT_TRUE(first_instruction->IsSuspendCheck()
43               || first_instruction->GetNext()->IsSuspendCheck());
44 }
45 
TEST(CodegenTest,CFG1)46 TEST(CodegenTest, CFG1) {
47   const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
48     Instruction::NOP,
49     Instruction::GOTO | 0xFF00);
50 
51   TestCode(data);
52 }
53 
TEST(CodegenTest,CFG2)54 TEST(CodegenTest, CFG2) {
55   const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
56     Instruction::GOTO_32, 0, 0);
57 
58   TestCode(data);
59 }
60 
TEST(CodegenTest,CFG3)61 TEST(CodegenTest, CFG3) {
62   const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
63     Instruction::CONST_4 | 0 | 0,
64     Instruction::IF_EQ, 0xFFFF,
65     Instruction::RETURN_VOID);
66 
67   TestCode(data);
68 }
69 
TEST(CodegenTest,CFG4)70 TEST(CodegenTest, CFG4) {
71   const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
72     Instruction::CONST_4 | 0 | 0,
73     Instruction::IF_NE, 0xFFFF,
74     Instruction::RETURN_VOID);
75 
76   TestCode(data);
77 }
78 
TEST(CodegenTest,CFG5)79 TEST(CodegenTest, CFG5) {
80   const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
81     Instruction::CONST_4 | 0 | 0,
82     Instruction::IF_EQZ, 0xFFFF,
83     Instruction::RETURN_VOID);
84 
85   TestCode(data);
86 }
87 
TEST(CodegenTest,CFG6)88 TEST(CodegenTest, CFG6) {
89   const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
90     Instruction::CONST_4 | 0 | 0,
91     Instruction::IF_NEZ, 0xFFFF,
92     Instruction::RETURN_VOID);
93 
94   TestCode(data);
95 }
96 }  // namespace art
97