1fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray/*
2fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray *
4fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * you may not use this file except in compliance with the License.
6fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * You may obtain a copy of the License at
7fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray *
8fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray *
10fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * See the License for the specific language governing permissions and
14fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * limitations under the License.
15fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray */
16fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
17fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray#include "builder.h"
18fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray#include "dex_instruction.h"
19fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray#include "nodes.h"
20fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray#include "optimizing_unit_test.h"
21badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil#include "pretty_printer.h"
22fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
23fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray#include "gtest/gtest.h"
24fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
25fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffraynamespace art {
26fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
27fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray/**
28fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray * Check that the HGraphBuilder adds suspend checks to backward branches.
29fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray */
30fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
31fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffraystatic void TestCode(const uint16_t* data) {
32fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  ArenaPool pool;
33fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  ArenaAllocator allocator(&pool);
34badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil  HGraph* graph = CreateCFG(&allocator, data);
35badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil  HBasicBlock* first_block = graph->GetEntryBlock()->GetSingleSuccessor();
36badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil  HBasicBlock* loop_header = first_block->GetSingleSuccessor();
37badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil  ASSERT_TRUE(loop_header->IsLoopHeader());
38badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil  ASSERT_EQ(loop_header->GetLoopInformation()->GetPreHeader(), first_block);
39badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil  ASSERT_TRUE(loop_header->GetFirstInstruction()->IsSuspendCheck());
40fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray}
41fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
42badd826664896d4a9628a5a89b78016894aa414bDavid Brazdilclass SuspendCheckTest : public CommonCompilerTest {};
43badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil
44badd826664896d4a9628a5a89b78016894aa414bDavid BrazdilTEST_F(SuspendCheckTest, CFG1) {
45fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
46fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::NOP,
47fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::GOTO | 0xFF00);
48fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
49fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  TestCode(data);
50fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray}
51fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
52badd826664896d4a9628a5a89b78016894aa414bDavid BrazdilTEST_F(SuspendCheckTest, CFG2) {
53fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
54fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::GOTO_32, 0, 0);
55fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
56fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  TestCode(data);
57fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray}
58fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
59badd826664896d4a9628a5a89b78016894aa414bDavid BrazdilTEST_F(SuspendCheckTest, CFG3) {
60fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
61fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::CONST_4 | 0 | 0,
62fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::IF_EQ, 0xFFFF,
63fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::RETURN_VOID);
64fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
65fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  TestCode(data);
66fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray}
67fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
68badd826664896d4a9628a5a89b78016894aa414bDavid BrazdilTEST_F(SuspendCheckTest, CFG4) {
69fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
70fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::CONST_4 | 0 | 0,
71fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::IF_NE, 0xFFFF,
72fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::RETURN_VOID);
73fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
74fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  TestCode(data);
75fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray}
76fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
77badd826664896d4a9628a5a89b78016894aa414bDavid BrazdilTEST_F(SuspendCheckTest, CFG5) {
78fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
79fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::CONST_4 | 0 | 0,
80fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::IF_EQZ, 0xFFFF,
81fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::RETURN_VOID);
82fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
83fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  TestCode(data);
84fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray}
85fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
86badd826664896d4a9628a5a89b78016894aa414bDavid BrazdilTEST_F(SuspendCheckTest, CFG6) {
87fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
88fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::CONST_4 | 0 | 0,
89fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::IF_NEZ, 0xFFFF,
90fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray    Instruction::RETURN_VOID);
91fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray
92fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray  TestCode(data);
93fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray}
94fbc695f9b8e2084697e19c1355ab925f99f0d235Nicolas Geoffray}  // namespace art
95