blob: 33823e2a11fc574430131508968f67ed0e8f2837 [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"
David Sehr9e734c72018-01-04 17:56:19 -080018#include "dex/dex_instruction.h"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000019#include "nodes.h"
20#include "optimizing_unit_test.h"
David Brazdilbadd8262016-02-02 16:28:56 +000021#include "pretty_printer.h"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000022
23#include "gtest/gtest.h"
24
25namespace art {
26
27/**
28 * Check that the HGraphBuilder adds suspend checks to backward branches.
29 */
30
Vladimir Markoca6fff82017-10-03 14:49:14 +010031class SuspendCheckTest : public OptimizingUnitTest {
32 protected:
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080033 void TestCode(const std::vector<uint16_t>& data);
Vladimir Markoca6fff82017-10-03 14:49:14 +010034};
35
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080036void SuspendCheckTest::TestCode(const std::vector<uint16_t>& data) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010037 HGraph* graph = CreateCFG(data);
David Brazdilbadd8262016-02-02 16:28:56 +000038 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());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000043}
44
David Brazdilbadd8262016-02-02 16:28:56 +000045TEST_F(SuspendCheckTest, CFG1) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080046 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000047 Instruction::NOP,
48 Instruction::GOTO | 0xFF00);
49
50 TestCode(data);
51}
52
David Brazdilbadd8262016-02-02 16:28:56 +000053TEST_F(SuspendCheckTest, CFG2) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080054 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000055 Instruction::GOTO_32, 0, 0);
56
57 TestCode(data);
58}
59
David Brazdilbadd8262016-02-02 16:28:56 +000060TEST_F(SuspendCheckTest, CFG3) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080061 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000062 Instruction::CONST_4 | 0 | 0,
63 Instruction::IF_EQ, 0xFFFF,
64 Instruction::RETURN_VOID);
65
66 TestCode(data);
67}
68
David Brazdilbadd8262016-02-02 16:28:56 +000069TEST_F(SuspendCheckTest, CFG4) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080070 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000071 Instruction::CONST_4 | 0 | 0,
72 Instruction::IF_NE, 0xFFFF,
73 Instruction::RETURN_VOID);
74
75 TestCode(data);
76}
77
David Brazdilbadd8262016-02-02 16:28:56 +000078TEST_F(SuspendCheckTest, CFG5) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080079 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000080 Instruction::CONST_4 | 0 | 0,
81 Instruction::IF_EQZ, 0xFFFF,
82 Instruction::RETURN_VOID);
83
84 TestCode(data);
85}
86
David Brazdilbadd8262016-02-02 16:28:56 +000087TEST_F(SuspendCheckTest, CFG6) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080088 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000089 Instruction::CONST_4 | 0 | 0,
90 Instruction::IF_NEZ, 0xFFFF,
91 Instruction::RETURN_VOID);
92
93 TestCode(data);
94}
95} // namespace art