blob: a5a0eb21149197a0f77640fec7fd79472c0d1c91 [file] [log] [blame]
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001/*
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
24namespace art {
25
26/**
27 * Check that the HGraphBuilder adds suspend checks to backward branches.
28 */
29
30static void TestCode(const uint16_t* data) {
31 ArenaPool pool;
32 ArenaAllocator allocator(&pool);
David Brazdil5e8b1372015-01-23 14:39:08 +000033 HGraph* graph = new (&allocator) HGraph(&allocator);
34 HGraphBuilder builder(graph);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000035 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +000036 bool graph_built = builder.BuildGraph(*item);
37 ASSERT_TRUE(graph_built);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000038
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
46TEST(CodegenTest, CFG1) {
47 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
48 Instruction::NOP,
49 Instruction::GOTO | 0xFF00);
50
51 TestCode(data);
52}
53
54TEST(CodegenTest, CFG2) {
55 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
56 Instruction::GOTO_32, 0, 0);
57
58 TestCode(data);
59}
60
61TEST(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
70TEST(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
79TEST(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
88TEST(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