Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 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 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 17 | #include "base/macros.h" |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 18 | #include "builder.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 19 | #include "dex/dex_instruction.h" |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 20 | #include "nodes.h" |
| 21 | #include "optimizing_unit_test.h" |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 22 | #include "pretty_printer.h" |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 23 | |
| 24 | #include "gtest/gtest.h" |
| 25 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 26 | namespace art HIDDEN { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Check that the HGraphBuilder adds suspend checks to backward branches. |
| 30 | */ |
| 31 | |
Santiago Aboy Solanes | 2c50b3a | 2023-02-10 10:25:31 +0000 | [diff] [blame] | 32 | class SuspendCheckTest : public CommonCompilerTest, public OptimizingUnitTestHelper { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 33 | protected: |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 34 | void TestCode(const std::vector<uint16_t>& data); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 37 | void SuspendCheckTest::TestCode(const std::vector<uint16_t>& data) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 38 | HGraph* graph = CreateCFG(data); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 39 | 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 Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 44 | } |
| 45 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 46 | TEST_F(SuspendCheckTest, CFG1) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 47 | const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 48 | Instruction::NOP, |
| 49 | Instruction::GOTO | 0xFF00); |
| 50 | |
| 51 | TestCode(data); |
| 52 | } |
| 53 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 54 | TEST_F(SuspendCheckTest, CFG2) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 55 | const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 56 | Instruction::GOTO_32, 0, 0); |
| 57 | |
| 58 | TestCode(data); |
| 59 | } |
| 60 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 61 | TEST_F(SuspendCheckTest, CFG3) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 62 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 63 | Instruction::CONST_4 | 0 | 0, |
| 64 | Instruction::IF_EQ, 0xFFFF, |
| 65 | Instruction::RETURN_VOID); |
| 66 | |
| 67 | TestCode(data); |
| 68 | } |
| 69 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 70 | TEST_F(SuspendCheckTest, CFG4) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 71 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 72 | Instruction::CONST_4 | 0 | 0, |
| 73 | Instruction::IF_NE, 0xFFFF, |
| 74 | Instruction::RETURN_VOID); |
| 75 | |
| 76 | TestCode(data); |
| 77 | } |
| 78 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 79 | TEST_F(SuspendCheckTest, CFG5) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 80 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 81 | Instruction::CONST_4 | 0 | 0, |
| 82 | Instruction::IF_EQZ, 0xFFFF, |
| 83 | Instruction::RETURN_VOID); |
| 84 | |
| 85 | TestCode(data); |
| 86 | } |
| 87 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 88 | TEST_F(SuspendCheckTest, CFG6) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 89 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 90 | Instruction::CONST_4 | 0 | 0, |
| 91 | Instruction::IF_NEZ, 0xFFFF, |
| 92 | Instruction::RETURN_VOID); |
| 93 | |
| 94 | TestCode(data); |
| 95 | } |
| 96 | } // namespace art |