blob: 76e7e0c32c2098e03c122efd28fd76ace20d23cb [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
Vladimír Marko434d9682022-11-04 14:04:17 +000017#include "base/macros.h"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000018#include "builder.h"
David Sehr9e734c72018-01-04 17:56:19 -080019#include "dex/dex_instruction.h"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000020#include "nodes.h"
21#include "optimizing_unit_test.h"
David Brazdilbadd8262016-02-02 16:28:56 +000022#include "pretty_printer.h"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000023
24#include "gtest/gtest.h"
25
Vladimír Marko434d9682022-11-04 14:04:17 +000026namespace art HIDDEN {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000027
28/**
29 * Check that the HGraphBuilder adds suspend checks to backward branches.
30 */
31
Santiago Aboy Solanes2c50b3a2023-02-10 10:25:31 +000032class SuspendCheckTest : public CommonCompilerTest, public OptimizingUnitTestHelper {
Vladimir Markoca6fff82017-10-03 14:49:14 +010033 protected:
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080034 void TestCode(const std::vector<uint16_t>& data);
Vladimir Markoca6fff82017-10-03 14:49:14 +010035};
36
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080037void SuspendCheckTest::TestCode(const std::vector<uint16_t>& data) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010038 HGraph* graph = CreateCFG(data);
David Brazdilbadd8262016-02-02 16:28:56 +000039 HBasicBlock* first_block = graph->GetEntryBlock()->GetSingleSuccessor();
40 HBasicBlock* loop_header = first_block->GetSingleSuccessor();
41 ASSERT_TRUE(loop_header->IsLoopHeader());
42 ASSERT_EQ(loop_header->GetLoopInformation()->GetPreHeader(), first_block);
43 ASSERT_TRUE(loop_header->GetFirstInstruction()->IsSuspendCheck());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000044}
45
David Brazdilbadd8262016-02-02 16:28:56 +000046TEST_F(SuspendCheckTest, CFG1) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080047 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000048 Instruction::NOP,
49 Instruction::GOTO | 0xFF00);
50
51 TestCode(data);
52}
53
David Brazdilbadd8262016-02-02 16:28:56 +000054TEST_F(SuspendCheckTest, CFG2) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080055 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000056 Instruction::GOTO_32, 0, 0);
57
58 TestCode(data);
59}
60
David Brazdilbadd8262016-02-02 16:28:56 +000061TEST_F(SuspendCheckTest, CFG3) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080062 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000063 Instruction::CONST_4 | 0 | 0,
64 Instruction::IF_EQ, 0xFFFF,
65 Instruction::RETURN_VOID);
66
67 TestCode(data);
68}
69
David Brazdilbadd8262016-02-02 16:28:56 +000070TEST_F(SuspendCheckTest, CFG4) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080071 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000072 Instruction::CONST_4 | 0 | 0,
73 Instruction::IF_NE, 0xFFFF,
74 Instruction::RETURN_VOID);
75
76 TestCode(data);
77}
78
David Brazdilbadd8262016-02-02 16:28:56 +000079TEST_F(SuspendCheckTest, CFG5) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080080 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000081 Instruction::CONST_4 | 0 | 0,
82 Instruction::IF_EQZ, 0xFFFF,
83 Instruction::RETURN_VOID);
84
85 TestCode(data);
86}
87
David Brazdilbadd8262016-02-02 16:28:56 +000088TEST_F(SuspendCheckTest, CFG6) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080089 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000090 Instruction::CONST_4 | 0 | 0,
91 Instruction::IF_NEZ, 0xFFFF,
92 Instruction::RETURN_VOID);
93
94 TestCode(data);
95}
96} // namespace art