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