| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +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 | |
| Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 17 | #include <functional> |
| Anton Kirilov | 3a2e78e | 2017-01-06 13:33:42 +0000 | [diff] [blame] | 18 | #include <memory> |
| Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 19 | |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 20 | #include "base/macros.h" |
| David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 21 | #include "base/utils.h" |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 22 | #include "builder.h" |
| Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 23 | #include "codegen_test_utils.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 24 | #include "dex/dex_file.h" |
| 25 | #include "dex/dex_instruction.h" |
| Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 26 | #include "driver/compiler_options.h" |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 27 | #include "nodes.h" |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 28 | #include "optimizing_unit_test.h" |
| Matthew Gharrity | e928885 | 2016-07-14 14:08:16 -0700 | [diff] [blame] | 29 | #include "register_allocator_linear_scan.h" |
| Scott Wakeling | fe88546 | 2016-09-22 10:24:38 +0100 | [diff] [blame] | 30 | #include "utils/arm/assembler_arm_vixl.h" |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 31 | #include "utils/arm/managed_register_arm.h" |
| 32 | #include "utils/x86/managed_register_x86.h" |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 33 | |
| 34 | #include "gtest/gtest.h" |
| Nicolas Geoffray | e636228 | 2015-01-26 13:57:30 +0000 | [diff] [blame] | 35 | |
| Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 36 | namespace art { |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 37 | |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 38 | // Return all combinations of ISA and code generator that are executable on |
| 39 | // hardware, or on simulator, and that we'd like to test. |
| 40 | static ::std::vector<CodegenTargetConfig> GetTargetConfigs() { |
| 41 | ::std::vector<CodegenTargetConfig> v; |
| 42 | ::std::vector<CodegenTargetConfig> test_config_candidates = { |
| 43 | #ifdef ART_ENABLE_CODEGEN_arm |
| Roland Levillain | 9983e30 | 2017-07-14 14:34:22 +0100 | [diff] [blame] | 44 | // TODO: Should't this be `kThumb2` instead of `kArm` here? |
| Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 45 | CodegenTargetConfig(InstructionSet::kArm, create_codegen_arm_vixl32), |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 46 | #endif |
| 47 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 48 | CodegenTargetConfig(InstructionSet::kArm64, create_codegen_arm64), |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 49 | #endif |
| 50 | #ifdef ART_ENABLE_CODEGEN_x86 |
| Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 51 | CodegenTargetConfig(InstructionSet::kX86, create_codegen_x86), |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 52 | #endif |
| 53 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
| Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 54 | CodegenTargetConfig(InstructionSet::kX86_64, create_codegen_x86_64), |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 55 | #endif |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 58 | for (const CodegenTargetConfig& test_config : test_config_candidates) { |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 59 | if (CanExecute(test_config.GetInstructionSet())) { |
| 60 | v.push_back(test_config); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | return v; |
| 65 | } |
| 66 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 67 | class CodegenTest : public OptimizingUnitTest { |
| 68 | protected: |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 69 | void TestCode(const std::vector<uint16_t>& data, bool has_result = false, int32_t expected = 0); |
| 70 | void TestCodeLong(const std::vector<uint16_t>& data, bool has_result, int64_t expected); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 71 | void TestComparison(IfCondition condition, |
| 72 | int64_t i, |
| 73 | int64_t j, |
| 74 | DataType::Type type, |
| 75 | const CodegenTargetConfig target_config); |
| 76 | }; |
| 77 | |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 78 | void CodegenTest::TestCode(const std::vector<uint16_t>& data, bool has_result, int32_t expected) { |
| Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 79 | for (const CodegenTargetConfig& target_config : GetTargetConfigs()) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 80 | ResetPoolAndAllocator(); |
| 81 | HGraph* graph = CreateCFG(data); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 82 | // Remove suspend checks, they cannot be executed in this context. |
| 83 | RemoveSuspendChecks(graph); |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 84 | OverrideInstructionSetFeatures(target_config.GetInstructionSet(), "default"); |
| 85 | RunCode(target_config, *compiler_options_, graph, [](HGraph*) {}, has_result, expected); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 86 | } |
| Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 89 | void CodegenTest::TestCodeLong(const std::vector<uint16_t>& data, |
| 90 | bool has_result, int64_t expected) { |
| Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 91 | for (const CodegenTargetConfig& target_config : GetTargetConfigs()) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 92 | ResetPoolAndAllocator(); |
| 93 | HGraph* graph = CreateCFG(data, DataType::Type::kInt64); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 94 | // Remove suspend checks, they cannot be executed in this context. |
| 95 | RemoveSuspendChecks(graph); |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 96 | OverrideInstructionSetFeatures(target_config.GetInstructionSet(), "default"); |
| 97 | RunCode(target_config, *compiler_options_, graph, [](HGraph*) {}, has_result, expected); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 98 | } |
| Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 101 | TEST_F(CodegenTest, ReturnVoid) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 102 | const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 103 | TestCode(data); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 106 | TEST_F(CodegenTest, CFG1) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 107 | const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 108 | Instruction::GOTO | 0x100, |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 109 | Instruction::RETURN_VOID); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 110 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 111 | TestCode(data); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 114 | TEST_F(CodegenTest, CFG2) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 115 | const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 116 | Instruction::GOTO | 0x100, |
| 117 | Instruction::GOTO | 0x100, |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 118 | Instruction::RETURN_VOID); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 119 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 120 | TestCode(data); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 123 | TEST_F(CodegenTest, CFG3) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 124 | const std::vector<uint16_t> data1 = ZERO_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 125 | Instruction::GOTO | 0x200, |
| 126 | Instruction::RETURN_VOID, |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 127 | Instruction::GOTO | 0xFF00); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 128 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 129 | TestCode(data1); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 130 | |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 131 | const std::vector<uint16_t> data2 = ZERO_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 132 | Instruction::GOTO_16, 3, |
| 133 | Instruction::RETURN_VOID, |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 134 | Instruction::GOTO_16, 0xFFFF); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 135 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 136 | TestCode(data2); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 137 | |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 138 | const std::vector<uint16_t> data3 = ZERO_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 139 | Instruction::GOTO_32, 4, 0, |
| 140 | Instruction::RETURN_VOID, |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 141 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 142 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 143 | TestCode(data3); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 146 | TEST_F(CodegenTest, CFG4) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 147 | const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 148 | Instruction::RETURN_VOID, |
| 149 | Instruction::GOTO | 0x100, |
| Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 150 | Instruction::GOTO | 0xFE00); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 151 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 152 | TestCode(data); |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 155 | TEST_F(CodegenTest, CFG5) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 156 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 157 | Instruction::CONST_4 | 0 | 0, |
| 158 | Instruction::IF_EQ, 3, |
| 159 | Instruction::GOTO | 0x100, |
| 160 | Instruction::RETURN_VOID); |
| 161 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 162 | TestCode(data); |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 165 | TEST_F(CodegenTest, IntConstant) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 166 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 167 | Instruction::CONST_4 | 0 | 0, |
| 168 | Instruction::RETURN_VOID); |
| 169 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 170 | TestCode(data); |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 173 | TEST_F(CodegenTest, Return1) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 174 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 175 | Instruction::CONST_4 | 0 | 0, |
| 176 | Instruction::RETURN | 0); |
| 177 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 178 | TestCode(data, true, 0); |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 181 | TEST_F(CodegenTest, Return2) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 182 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 183 | Instruction::CONST_4 | 0 | 0, |
| 184 | Instruction::CONST_4 | 0 | 1 << 8, |
| 185 | Instruction::RETURN | 1 << 8); |
| 186 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 187 | TestCode(data, true, 0); |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 190 | TEST_F(CodegenTest, Return3) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 191 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 192 | Instruction::CONST_4 | 0 | 0, |
| 193 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 194 | Instruction::RETURN | 1 << 8); |
| 195 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 196 | TestCode(data, true, 1); |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 199 | TEST_F(CodegenTest, ReturnIf1) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 200 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 201 | Instruction::CONST_4 | 0 | 0, |
| 202 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 203 | Instruction::IF_EQ, 3, |
| 204 | Instruction::RETURN | 0 << 8, |
| 205 | Instruction::RETURN | 1 << 8); |
| 206 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 207 | TestCode(data, true, 1); |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 210 | TEST_F(CodegenTest, ReturnIf2) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 211 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 212 | Instruction::CONST_4 | 0 | 0, |
| 213 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 214 | Instruction::IF_EQ | 0 << 4 | 1 << 8, 3, |
| 215 | Instruction::RETURN | 0 << 8, |
| 216 | Instruction::RETURN | 1 << 8); |
| 217 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 218 | TestCode(data, true, 0); |
| Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 221 | // Exercise bit-wise (one's complement) not-int instruction. |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 222 | #define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
| 223 | TEST_F(CodegenTest, TEST_NAME) { \ |
| 224 | const int32_t input = INPUT; \ |
| 225 | const uint16_t input_lo = Low16Bits(input); \ |
| 226 | const uint16_t input_hi = High16Bits(input); \ |
| 227 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( \ |
| 228 | Instruction::CONST | 0 << 8, input_lo, input_hi, \ |
| 229 | Instruction::NOT_INT | 1 << 8 | 0 << 12 , \ |
| 230 | Instruction::RETURN | 1 << 8); \ |
| 231 | \ |
| 232 | TestCode(data, true, EXPECTED_OUTPUT); \ |
| Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | NOT_INT_TEST(ReturnNotIntMinus2, -2, 1) |
| 236 | NOT_INT_TEST(ReturnNotIntMinus1, -1, 0) |
| 237 | NOT_INT_TEST(ReturnNotInt0, 0, -1) |
| 238 | NOT_INT_TEST(ReturnNotInt1, 1, -2) |
| Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 239 | NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1 |
| 240 | NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2 |
| 241 | NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1 |
| 242 | NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31) |
| Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 243 | |
| 244 | #undef NOT_INT_TEST |
| 245 | |
| Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 246 | // Exercise bit-wise (one's complement) not-long instruction. |
| 247 | #define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 248 | TEST_F(CodegenTest, TEST_NAME) { \ |
| Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 249 | const int64_t input = INPUT; \ |
| 250 | const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \ |
| 251 | const uint16_t word1 = High16Bits(Low32Bits(input)); \ |
| 252 | const uint16_t word2 = Low16Bits(High32Bits(input)); \ |
| 253 | const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \ |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 254 | const std::vector<uint16_t> data = FOUR_REGISTERS_CODE_ITEM( \ |
| Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 255 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \ |
| 256 | Instruction::NOT_LONG | 2 << 8 | 0 << 12, \ |
| 257 | Instruction::RETURN_WIDE | 2 << 8); \ |
| 258 | \ |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 259 | TestCodeLong(data, true, EXPECTED_OUTPUT); \ |
| Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1)) |
| 263 | NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0)) |
| 264 | NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1)) |
| 265 | NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2)) |
| 266 | |
| 267 | NOT_LONG_TEST(ReturnNotLongINT32_MIN, |
| 268 | INT64_C(-2147483648), |
| 269 | INT64_C(2147483647)) // (2^31) - 1 |
| 270 | NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1, |
| 271 | INT64_C(-2147483647), |
| 272 | INT64_C(2147483646)) // (2^31) - 2 |
| 273 | NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1, |
| 274 | INT64_C(2147483646), |
| 275 | INT64_C(-2147483647)) // -(2^31) - 1 |
| 276 | NOT_LONG_TEST(ReturnNotLongINT32_MAX, |
| 277 | INT64_C(2147483647), |
| 278 | INT64_C(-2147483648)) // -(2^31) |
| 279 | |
| 280 | // Note that the C++ compiler won't accept |
| 281 | // INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid |
| 282 | // int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead. |
| 283 | NOT_LONG_TEST(ReturnNotINT64_MIN, |
| 284 | INT64_C(-9223372036854775807)-1, |
| 285 | INT64_C(9223372036854775807)); // (2^63) - 1 |
| 286 | NOT_LONG_TEST(ReturnNotINT64_MINPlus1, |
| 287 | INT64_C(-9223372036854775807), |
| 288 | INT64_C(9223372036854775806)); // (2^63) - 2 |
| 289 | NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1, |
| 290 | INT64_C(9223372036854775806), |
| 291 | INT64_C(-9223372036854775807)); // -(2^63) - 1 |
| 292 | NOT_LONG_TEST(ReturnNotLongINT64_MAX, |
| 293 | INT64_C(9223372036854775807), |
| 294 | INT64_C(-9223372036854775807)-1); // -(2^63) |
| 295 | |
| 296 | #undef NOT_LONG_TEST |
| 297 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 298 | TEST_F(CodegenTest, IntToLongOfLongToInt) { |
| Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 299 | const int64_t input = INT64_C(4294967296); // 2^32 |
| 300 | const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW. |
| 301 | const uint16_t word1 = High16Bits(Low32Bits(input)); |
| 302 | const uint16_t word2 = Low16Bits(High32Bits(input)); |
| 303 | const uint16_t word3 = High16Bits(High32Bits(input)); // MSW. |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 304 | const std::vector<uint16_t> data = FIVE_REGISTERS_CODE_ITEM( |
| Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 305 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, |
| 306 | Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0, |
| 307 | Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1 |
| 308 | Instruction::LONG_TO_INT | 4 << 8 | 0 << 12, |
| 309 | Instruction::INT_TO_LONG | 2 << 8 | 4 << 12, |
| 310 | Instruction::RETURN_WIDE | 2 << 8); |
| 311 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 312 | TestCodeLong(data, true, 1); |
| Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 315 | TEST_F(CodegenTest, ReturnAdd1) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 316 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 317 | Instruction::CONST_4 | 3 << 12 | 0, |
| 318 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 319 | Instruction::ADD_INT, 1 << 8 | 0, |
| 320 | Instruction::RETURN); |
| 321 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 322 | TestCode(data, true, 7); |
| Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 325 | TEST_F(CodegenTest, ReturnAdd2) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 326 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 327 | Instruction::CONST_4 | 3 << 12 | 0, |
| 328 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 329 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 330 | Instruction::RETURN); |
| 331 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 332 | TestCode(data, true, 7); |
| Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 335 | TEST_F(CodegenTest, ReturnAdd3) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 336 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 337 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 338 | Instruction::ADD_INT_LIT8, 3 << 8 | 0, |
| 339 | Instruction::RETURN); |
| 340 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 341 | TestCode(data, true, 7); |
| Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 344 | TEST_F(CodegenTest, ReturnAdd4) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 345 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
| Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 346 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 347 | Instruction::ADD_INT_LIT16, 3, |
| 348 | Instruction::RETURN); |
| 349 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 350 | TestCode(data, true, 7); |
| Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 353 | TEST_F(CodegenTest, ReturnMulInt) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 354 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 355 | Instruction::CONST_4 | 3 << 12 | 0, |
| 356 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 357 | Instruction::MUL_INT, 1 << 8 | 0, |
| 358 | Instruction::RETURN); |
| Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 359 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 360 | TestCode(data, true, 12); |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 363 | TEST_F(CodegenTest, ReturnMulInt2addr) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 364 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 365 | Instruction::CONST_4 | 3 << 12 | 0, |
| 366 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 367 | Instruction::MUL_INT_2ADDR | 1 << 12, |
| 368 | Instruction::RETURN); |
| 369 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 370 | TestCode(data, true, 12); |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 373 | TEST_F(CodegenTest, ReturnMulLong) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 374 | const std::vector<uint16_t> data = FOUR_REGISTERS_CODE_ITEM( |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 375 | Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0, |
| 376 | Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0, |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 377 | Instruction::MUL_LONG, 2 << 8 | 0, |
| 378 | Instruction::RETURN_WIDE); |
| 379 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 380 | TestCodeLong(data, true, 12); |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 383 | TEST_F(CodegenTest, ReturnMulLong2addr) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 384 | const std::vector<uint16_t> data = FOUR_REGISTERS_CODE_ITEM( |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 385 | Instruction::CONST_WIDE | 0 << 8, 3, 0, 0, 0, |
| 386 | Instruction::CONST_WIDE | 2 << 8, 4, 0, 0, 0, |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 387 | Instruction::MUL_LONG_2ADDR | 2 << 12, |
| 388 | Instruction::RETURN_WIDE); |
| 389 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 390 | TestCodeLong(data, true, 12); |
| Nicolas Geoffray | 5da2180 | 2015-04-20 09:29:18 +0100 | [diff] [blame] | 391 | } |
| Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 392 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 393 | TEST_F(CodegenTest, ReturnMulIntLit8) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 394 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
| Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 395 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 396 | Instruction::MUL_INT_LIT8, 3 << 8 | 0, |
| 397 | Instruction::RETURN); |
| 398 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 399 | TestCode(data, true, 12); |
| Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 402 | TEST_F(CodegenTest, ReturnMulIntLit16) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 403 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
| Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 404 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 405 | Instruction::MUL_INT_LIT16, 3, |
| 406 | Instruction::RETURN); |
| 407 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 408 | TestCode(data, true, 12); |
| Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 411 | TEST_F(CodegenTest, NonMaterializedCondition) { |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 412 | for (CodegenTargetConfig target_config : GetTargetConfigs()) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 413 | HGraph* graph = CreateGraph(); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 414 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 415 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 416 | graph->AddBlock(entry); |
| 417 | graph->SetEntryBlock(entry); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 418 | entry->AddInstruction(new (GetAllocator()) HGoto()); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 419 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 420 | HBasicBlock* first_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 421 | graph->AddBlock(first_block); |
| 422 | entry->AddSuccessor(first_block); |
| 423 | HIntConstant* constant0 = graph->GetIntConstant(0); |
| 424 | HIntConstant* constant1 = graph->GetIntConstant(1); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 425 | HEqual* equal = new (GetAllocator()) HEqual(constant0, constant0); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 426 | first_block->AddInstruction(equal); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 427 | first_block->AddInstruction(new (GetAllocator()) HIf(equal)); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 428 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 429 | HBasicBlock* then_block = new (GetAllocator()) HBasicBlock(graph); |
| 430 | HBasicBlock* else_block = new (GetAllocator()) HBasicBlock(graph); |
| 431 | HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 432 | graph->SetExitBlock(exit_block); |
| 433 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 434 | graph->AddBlock(then_block); |
| 435 | graph->AddBlock(else_block); |
| 436 | graph->AddBlock(exit_block); |
| 437 | first_block->AddSuccessor(then_block); |
| 438 | first_block->AddSuccessor(else_block); |
| 439 | then_block->AddSuccessor(exit_block); |
| 440 | else_block->AddSuccessor(exit_block); |
| 441 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 442 | exit_block->AddInstruction(new (GetAllocator()) HExit()); |
| 443 | then_block->AddInstruction(new (GetAllocator()) HReturn(constant0)); |
| 444 | else_block->AddInstruction(new (GetAllocator()) HReturn(constant1)); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 445 | |
| David Brazdil | b11b072 | 2016-01-28 16:22:40 +0000 | [diff] [blame] | 446 | ASSERT_FALSE(equal->IsEmittedAtUseSite()); |
| David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 447 | graph->BuildDominatorTree(); |
| Nicolas Geoffray | 61ba8d2 | 2018-08-07 09:55:57 +0100 | [diff] [blame] | 448 | PrepareForRegisterAllocation(graph, *compiler_options_).Run(); |
| David Brazdil | b11b072 | 2016-01-28 16:22:40 +0000 | [diff] [blame] | 449 | ASSERT_TRUE(equal->IsEmittedAtUseSite()); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 450 | |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 451 | auto hook_before_codegen = [](HGraph* graph_in) { |
| Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 452 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 453 | HParallelMove* move = new (graph_in->GetAllocator()) HParallelMove(graph_in->GetAllocator()); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 454 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 455 | }; |
| 456 | |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 457 | OverrideInstructionSetFeatures(target_config.GetInstructionSet(), "default"); |
| 458 | RunCode(target_config, *compiler_options_, graph, hook_before_codegen, true, 0); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 462 | TEST_F(CodegenTest, MaterializedCondition1) { |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 463 | for (CodegenTargetConfig target_config : GetTargetConfigs()) { |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 464 | // Check that condition are materialized correctly. A materialized condition |
| 465 | // should yield `1` if it evaluated to true, and `0` otherwise. |
| 466 | // We force the materialization of comparisons for different combinations of |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 467 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 468 | // inputs and check the results. |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 469 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 470 | int lhs[] = {1, 2, -1, 2, 0xabc}; |
| 471 | int rhs[] = {2, 1, 2, -1, 0xabc}; |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 472 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 473 | for (size_t i = 0; i < arraysize(lhs); i++) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 474 | HGraph* graph = CreateGraph(); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 475 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 476 | HBasicBlock* entry_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 477 | graph->AddBlock(entry_block); |
| 478 | graph->SetEntryBlock(entry_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 479 | entry_block->AddInstruction(new (GetAllocator()) HGoto()); |
| 480 | HBasicBlock* code_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 481 | graph->AddBlock(code_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 482 | HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 483 | graph->AddBlock(exit_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 484 | exit_block->AddInstruction(new (GetAllocator()) HExit()); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 485 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 486 | entry_block->AddSuccessor(code_block); |
| 487 | code_block->AddSuccessor(exit_block); |
| 488 | graph->SetExitBlock(exit_block); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 489 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 490 | HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]); |
| 491 | HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]); |
| 492 | HLessThan cmp_lt(cst_lhs, cst_rhs); |
| 493 | code_block->AddInstruction(&cmp_lt); |
| 494 | HReturn ret(&cmp_lt); |
| 495 | code_block->AddInstruction(&ret); |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 496 | |
| David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 497 | graph->BuildDominatorTree(); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 498 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 499 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 500 | HParallelMove* move = |
| 501 | new (graph_in->GetAllocator()) HParallelMove(graph_in->GetAllocator()); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 502 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 503 | }; |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 504 | OverrideInstructionSetFeatures(target_config.GetInstructionSet(), "default"); |
| 505 | RunCode(target_config, *compiler_options_, graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 506 | } |
| Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 507 | } |
| 508 | } |
| Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 509 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 510 | TEST_F(CodegenTest, MaterializedCondition2) { |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 511 | for (CodegenTargetConfig target_config : GetTargetConfigs()) { |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 512 | // Check that HIf correctly interprets a materialized condition. |
| 513 | // We force the materialization of comparisons for different combinations of |
| 514 | // inputs. An HIf takes the materialized combination as input and returns a |
| 515 | // value that we verify. |
| 516 | |
| 517 | int lhs[] = {1, 2, -1, 2, 0xabc}; |
| 518 | int rhs[] = {2, 1, 2, -1, 0xabc}; |
| 519 | |
| 520 | |
| 521 | for (size_t i = 0; i < arraysize(lhs); i++) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 522 | HGraph* graph = CreateGraph(); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 523 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 524 | HBasicBlock* entry_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 525 | graph->AddBlock(entry_block); |
| 526 | graph->SetEntryBlock(entry_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 527 | entry_block->AddInstruction(new (GetAllocator()) HGoto()); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 528 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 529 | HBasicBlock* if_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 530 | graph->AddBlock(if_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 531 | HBasicBlock* if_true_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 532 | graph->AddBlock(if_true_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 533 | HBasicBlock* if_false_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 534 | graph->AddBlock(if_false_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 535 | HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 536 | graph->AddBlock(exit_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 537 | exit_block->AddInstruction(new (GetAllocator()) HExit()); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 538 | |
| 539 | graph->SetEntryBlock(entry_block); |
| 540 | entry_block->AddSuccessor(if_block); |
| 541 | if_block->AddSuccessor(if_true_block); |
| 542 | if_block->AddSuccessor(if_false_block); |
| 543 | if_true_block->AddSuccessor(exit_block); |
| 544 | if_false_block->AddSuccessor(exit_block); |
| 545 | graph->SetExitBlock(exit_block); |
| 546 | |
| 547 | HIntConstant* cst_lhs = graph->GetIntConstant(lhs[i]); |
| 548 | HIntConstant* cst_rhs = graph->GetIntConstant(rhs[i]); |
| 549 | HLessThan cmp_lt(cst_lhs, cst_rhs); |
| 550 | if_block->AddInstruction(&cmp_lt); |
| David Brazdil | 6e33252 | 2016-02-02 16:15:27 +0000 | [diff] [blame] | 551 | // We insert a dummy instruction to separate the HIf from the HLessThan |
| 552 | // and force the materialization of the condition. |
| 553 | HMemoryBarrier force_materialization(MemBarrierKind::kAnyAny, 0); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 554 | if_block->AddInstruction(&force_materialization); |
| 555 | HIf if_lt(&cmp_lt); |
| 556 | if_block->AddInstruction(&if_lt); |
| 557 | |
| 558 | HIntConstant* cst_lt = graph->GetIntConstant(1); |
| 559 | HReturn ret_lt(cst_lt); |
| 560 | if_true_block->AddInstruction(&ret_lt); |
| 561 | HIntConstant* cst_ge = graph->GetIntConstant(0); |
| 562 | HReturn ret_ge(cst_ge); |
| 563 | if_false_block->AddInstruction(&ret_ge); |
| 564 | |
| David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 565 | graph->BuildDominatorTree(); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 566 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 567 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors()[0]; |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 568 | HParallelMove* move = |
| 569 | new (graph_in->GetAllocator()) HParallelMove(graph_in->GetAllocator()); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 570 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 571 | }; |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 572 | OverrideInstructionSetFeatures(target_config.GetInstructionSet(), "default"); |
| 573 | RunCode(target_config, *compiler_options_, graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | TEST_F(CodegenTest, ReturnDivIntLit8) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 579 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
| Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 580 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 581 | Instruction::DIV_INT_LIT8, 3 << 8 | 0, |
| 582 | Instruction::RETURN); |
| 583 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 584 | TestCode(data, true, 1); |
| Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 587 | TEST_F(CodegenTest, ReturnDivInt2Addr) { |
| Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 588 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
| Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 589 | Instruction::CONST_4 | 4 << 12 | 0, |
| 590 | Instruction::CONST_4 | 2 << 12 | 1 << 8, |
| 591 | Instruction::DIV_INT_2ADDR | 1 << 12, |
| 592 | Instruction::RETURN); |
| 593 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 594 | TestCode(data, true, 2); |
| Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 597 | // Helper method. |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 598 | void CodegenTest::TestComparison(IfCondition condition, |
| 599 | int64_t i, |
| 600 | int64_t j, |
| 601 | DataType::Type type, |
| 602 | const CodegenTargetConfig target_config) { |
| 603 | HGraph* graph = CreateGraph(); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 604 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 605 | HBasicBlock* entry_block = new (GetAllocator()) HBasicBlock(graph); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 606 | graph->AddBlock(entry_block); |
| 607 | graph->SetEntryBlock(entry_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 608 | entry_block->AddInstruction(new (GetAllocator()) HGoto()); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 609 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 610 | HBasicBlock* block = new (GetAllocator()) HBasicBlock(graph); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 611 | graph->AddBlock(block); |
| 612 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 613 | HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 614 | graph->AddBlock(exit_block); |
| 615 | graph->SetExitBlock(exit_block); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 616 | exit_block->AddInstruction(new (GetAllocator()) HExit()); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 617 | |
| 618 | entry_block->AddSuccessor(block); |
| 619 | block->AddSuccessor(exit_block); |
| 620 | |
| 621 | HInstruction* op1; |
| 622 | HInstruction* op2; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 623 | if (type == DataType::Type::kInt32) { |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 624 | op1 = graph->GetIntConstant(i); |
| 625 | op2 = graph->GetIntConstant(j); |
| 626 | } else { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 627 | DCHECK_EQ(type, DataType::Type::kInt64); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 628 | op1 = graph->GetLongConstant(i); |
| 629 | op2 = graph->GetLongConstant(j); |
| 630 | } |
| 631 | |
| 632 | HInstruction* comparison = nullptr; |
| 633 | bool expected_result = false; |
| 634 | const uint64_t x = i; |
| 635 | const uint64_t y = j; |
| 636 | switch (condition) { |
| 637 | case kCondEQ: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 638 | comparison = new (GetAllocator()) HEqual(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 639 | expected_result = (i == j); |
| 640 | break; |
| 641 | case kCondNE: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 642 | comparison = new (GetAllocator()) HNotEqual(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 643 | expected_result = (i != j); |
| 644 | break; |
| 645 | case kCondLT: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 646 | comparison = new (GetAllocator()) HLessThan(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 647 | expected_result = (i < j); |
| 648 | break; |
| 649 | case kCondLE: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 650 | comparison = new (GetAllocator()) HLessThanOrEqual(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 651 | expected_result = (i <= j); |
| 652 | break; |
| 653 | case kCondGT: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 654 | comparison = new (GetAllocator()) HGreaterThan(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 655 | expected_result = (i > j); |
| 656 | break; |
| 657 | case kCondGE: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 658 | comparison = new (GetAllocator()) HGreaterThanOrEqual(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 659 | expected_result = (i >= j); |
| 660 | break; |
| 661 | case kCondB: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 662 | comparison = new (GetAllocator()) HBelow(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 663 | expected_result = (x < y); |
| 664 | break; |
| 665 | case kCondBE: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 666 | comparison = new (GetAllocator()) HBelowOrEqual(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 667 | expected_result = (x <= y); |
| 668 | break; |
| 669 | case kCondA: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 670 | comparison = new (GetAllocator()) HAbove(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 671 | expected_result = (x > y); |
| 672 | break; |
| 673 | case kCondAE: |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 674 | comparison = new (GetAllocator()) HAboveOrEqual(op1, op2); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 675 | expected_result = (x >= y); |
| 676 | break; |
| 677 | } |
| 678 | block->AddInstruction(comparison); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 679 | block->AddInstruction(new (GetAllocator()) HReturn(comparison)); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 680 | |
| David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 681 | graph->BuildDominatorTree(); |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 682 | OverrideInstructionSetFeatures(target_config.GetInstructionSet(), "default"); |
| 683 | RunCode(target_config, *compiler_options_, graph, [](HGraph*) {}, true, expected_result); |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 686 | TEST_F(CodegenTest, ComparisonsInt) { |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 687 | for (CodegenTargetConfig target_config : GetTargetConfigs()) { |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 688 | for (int64_t i = -1; i <= 1; i++) { |
| 689 | for (int64_t j = -1; j <= 1; j++) { |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 690 | for (int cond = kCondFirst; cond <= kCondLast; cond++) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 691 | TestComparison( |
| 692 | static_cast<IfCondition>(cond), i, j, DataType::Type::kInt32, target_config); |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 693 | } |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 694 | } |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 699 | TEST_F(CodegenTest, ComparisonsLong) { |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 700 | for (CodegenTargetConfig target_config : GetTargetConfigs()) { |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 701 | for (int64_t i = -1; i <= 1; i++) { |
| 702 | for (int64_t j = -1; j <= 1; j++) { |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 703 | for (int cond = kCondFirst; cond <= kCondLast; cond++) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 704 | TestComparison( |
| 705 | static_cast<IfCondition>(cond), i, j, DataType::Type::kInt64, target_config); |
| Scott Wakeling | 2c76e06 | 2016-08-31 09:48:54 +0100 | [diff] [blame] | 706 | } |
| David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 707 | } |
| Aart Bik | e9f3760 | 2015-10-09 11:15:55 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| Artem Serov | 4593f7d | 2016-12-29 16:21:49 +0000 | [diff] [blame] | 712 | #ifdef ART_ENABLE_CODEGEN_arm |
| 713 | TEST_F(CodegenTest, ARMVIXLParallelMoveResolver) { |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 714 | OverrideInstructionSetFeatures(InstructionSet::kThumb2, "default"); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 715 | HGraph* graph = CreateGraph(); |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 716 | arm::CodeGeneratorARMVIXL codegen(graph, *compiler_options_); |
| Artem Serov | 4593f7d | 2016-12-29 16:21:49 +0000 | [diff] [blame] | 717 | |
| 718 | codegen.Initialize(); |
| 719 | |
| 720 | // This will result in calling EmitSwap -> void ParallelMoveResolverARMVIXL::Exchange(int mem1, |
| 721 | // int mem2) which was faulty (before the fix). So previously GPR and FP scratch registers were |
| 722 | // used as temps; however GPR scratch register is required for big stack offsets which don't fit |
| 723 | // LDR encoding. So the following code is a regression test for that situation. |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 724 | HParallelMove* move = new (graph->GetAllocator()) HParallelMove(graph->GetAllocator()); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 725 | move->AddMove(Location::StackSlot(0), Location::StackSlot(8192), DataType::Type::kInt32, nullptr); |
| 726 | move->AddMove(Location::StackSlot(8192), Location::StackSlot(0), DataType::Type::kInt32, nullptr); |
| Artem Serov | 4593f7d | 2016-12-29 16:21:49 +0000 | [diff] [blame] | 727 | codegen.GetMoveResolver()->EmitNativeCode(move); |
| 728 | |
| 729 | InternalCodeAllocator code_allocator; |
| 730 | codegen.Finalize(&code_allocator); |
| 731 | } |
| 732 | #endif |
| 733 | |
| Roland Levillain | 558dea1 | 2017-01-27 19:40:44 +0000 | [diff] [blame] | 734 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 735 | // Regression test for b/34760542. |
| 736 | TEST_F(CodegenTest, ARM64ParallelMoveResolverB34760542) { |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 737 | OverrideInstructionSetFeatures(InstructionSet::kArm64, "default"); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 738 | HGraph* graph = CreateGraph(); |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 739 | arm64::CodeGeneratorARM64 codegen(graph, *compiler_options_); |
| Roland Levillain | 558dea1 | 2017-01-27 19:40:44 +0000 | [diff] [blame] | 740 | |
| 741 | codegen.Initialize(); |
| 742 | |
| 743 | // The following ParallelMove used to fail this assertion: |
| 744 | // |
| 745 | // Assertion failed (!available->IsEmpty()) |
| 746 | // |
| Roland Levillain | 952b235 | 2017-05-03 19:49:14 +0100 | [diff] [blame] | 747 | // in vixl::aarch64::UseScratchRegisterScope::AcquireNextAvailable, |
| 748 | // because of the following situation: |
| 749 | // |
| 750 | // 1. a temp register (IP0) is allocated as a scratch register by |
| 751 | // the parallel move resolver to solve a cycle (swap): |
| 752 | // |
| 753 | // [ source=DS0 destination=DS257 type=PrimDouble instruction=null ] |
| 754 | // [ source=DS257 destination=DS0 type=PrimDouble instruction=null ] |
| 755 | // |
| 756 | // 2. within CodeGeneratorARM64::MoveLocation, another temp |
| 757 | // register (IP1) is allocated to generate the swap between two |
| 758 | // double stack slots; |
| 759 | // |
| 760 | // 3. VIXL requires a third temp register to emit the `Ldr` or |
| 761 | // `Str` operation from CodeGeneratorARM64::MoveLocation (as |
| 762 | // one of the stack slots' offsets cannot be encoded as an |
| 763 | // immediate), but the pool of (core) temp registers is now |
| 764 | // empty. |
| 765 | // |
| 766 | // The solution used so far is to use a floating-point temp register |
| 767 | // (D31) in step #2, so that IP1 is available for step #3. |
| 768 | |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 769 | HParallelMove* move = new (graph->GetAllocator()) HParallelMove(graph->GetAllocator()); |
| Roland Levillain | 558dea1 | 2017-01-27 19:40:44 +0000 | [diff] [blame] | 770 | move->AddMove(Location::DoubleStackSlot(0), |
| 771 | Location::DoubleStackSlot(257), |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 772 | DataType::Type::kFloat64, |
| Roland Levillain | 558dea1 | 2017-01-27 19:40:44 +0000 | [diff] [blame] | 773 | nullptr); |
| 774 | move->AddMove(Location::DoubleStackSlot(257), |
| 775 | Location::DoubleStackSlot(0), |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 776 | DataType::Type::kFloat64, |
| Roland Levillain | 558dea1 | 2017-01-27 19:40:44 +0000 | [diff] [blame] | 777 | nullptr); |
| 778 | codegen.GetMoveResolver()->EmitNativeCode(move); |
| 779 | |
| 780 | InternalCodeAllocator code_allocator; |
| 781 | codegen.Finalize(&code_allocator); |
| 782 | } |
| Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 783 | |
| 784 | // Check that ParallelMoveResolver works fine for ARM64 for both cases when SIMD is on and off. |
| 785 | TEST_F(CodegenTest, ARM64ParallelMoveResolverSIMD) { |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 786 | OverrideInstructionSetFeatures(InstructionSet::kArm64, "default"); |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 787 | HGraph* graph = CreateGraph(); |
| Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 788 | arm64::CodeGeneratorARM64 codegen(graph, *compiler_options_); |
| Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 789 | |
| 790 | codegen.Initialize(); |
| 791 | |
| 792 | graph->SetHasSIMD(true); |
| 793 | for (int i = 0; i < 2; i++) { |
| Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 794 | HParallelMove* move = new (graph->GetAllocator()) HParallelMove(graph->GetAllocator()); |
| Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 795 | move->AddMove(Location::SIMDStackSlot(0), |
| 796 | Location::SIMDStackSlot(257), |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 797 | DataType::Type::kFloat64, |
| Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 798 | nullptr); |
| 799 | move->AddMove(Location::SIMDStackSlot(257), |
| 800 | Location::SIMDStackSlot(0), |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 801 | DataType::Type::kFloat64, |
| Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 802 | nullptr); |
| 803 | move->AddMove(Location::FpuRegisterLocation(0), |
| 804 | Location::FpuRegisterLocation(1), |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 805 | DataType::Type::kFloat64, |
| Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 806 | nullptr); |
| 807 | move->AddMove(Location::FpuRegisterLocation(1), |
| 808 | Location::FpuRegisterLocation(0), |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 809 | DataType::Type::kFloat64, |
| Artem Serov | d4bccf1 | 2017-04-03 18:47:32 +0100 | [diff] [blame] | 810 | nullptr); |
| 811 | codegen.GetMoveResolver()->EmitNativeCode(move); |
| 812 | graph->SetHasSIMD(false); |
| 813 | } |
| 814 | |
| 815 | InternalCodeAllocator code_allocator; |
| 816 | codegen.Finalize(&code_allocator); |
| 817 | } |
| Artem Serov | aa6f483 | 2018-11-21 18:57:54 +0000 | [diff] [blame] | 818 | |
| 819 | // Check that ART ISA Features are propagated to VIXL for arm64 (using cortex-a75 as example). |
| 820 | TEST_F(CodegenTest, ARM64IsaVIXLFeaturesA75) { |
| 821 | OverrideInstructionSetFeatures(InstructionSet::kArm64, "cortex-a75"); |
| 822 | HGraph* graph = CreateGraph(); |
| 823 | arm64::CodeGeneratorARM64 codegen(graph, *compiler_options_); |
| 824 | vixl::CPUFeatures* features = codegen.GetVIXLAssembler()->GetCPUFeatures(); |
| 825 | |
| 826 | EXPECT_TRUE(features->Has(vixl::CPUFeatures::kCRC32)); |
| 827 | EXPECT_TRUE(features->Has(vixl::CPUFeatures::kDotProduct)); |
| 828 | EXPECT_TRUE(features->Has(vixl::CPUFeatures::kFPHalf)); |
| Usama Arif | 142816a | 2019-11-06 16:15:31 +0000 | [diff] [blame] | 829 | EXPECT_TRUE(features->Has(vixl::CPUFeatures::kNEONHalf)); |
| Artem Serov | aa6f483 | 2018-11-21 18:57:54 +0000 | [diff] [blame] | 830 | EXPECT_TRUE(features->Has(vixl::CPUFeatures::kAtomics)); |
| 831 | } |
| 832 | |
| 833 | // Check that ART ISA Features are propagated to VIXL for arm64 (using cortex-a53 as example). |
| 834 | TEST_F(CodegenTest, ARM64IsaVIXLFeaturesA53) { |
| 835 | OverrideInstructionSetFeatures(InstructionSet::kArm64, "cortex-a53"); |
| 836 | HGraph* graph = CreateGraph(); |
| 837 | arm64::CodeGeneratorARM64 codegen(graph, *compiler_options_); |
| 838 | vixl::CPUFeatures* features = codegen.GetVIXLAssembler()->GetCPUFeatures(); |
| 839 | |
| 840 | EXPECT_TRUE(features->Has(vixl::CPUFeatures::kCRC32)); |
| 841 | EXPECT_FALSE(features->Has(vixl::CPUFeatures::kDotProduct)); |
| 842 | EXPECT_FALSE(features->Has(vixl::CPUFeatures::kFPHalf)); |
| Usama Arif | 142816a | 2019-11-06 16:15:31 +0000 | [diff] [blame] | 843 | EXPECT_FALSE(features->Has(vixl::CPUFeatures::kNEONHalf)); |
| Artem Serov | aa6f483 | 2018-11-21 18:57:54 +0000 | [diff] [blame] | 844 | EXPECT_FALSE(features->Has(vixl::CPUFeatures::kAtomics)); |
| 845 | } |
| 846 | |
| Artem Serov | 6a0b657 | 2019-07-26 20:38:37 +0100 | [diff] [blame] | 847 | constexpr static size_t kExpectedFPSpillSize = 8 * vixl::aarch64::kDRegSizeInBytes; |
| 848 | |
| 849 | // The following two tests check that for both SIMD and non-SIMD graphs exactly 64-bit is |
| 850 | // allocated on stack per callee-saved FP register to be preserved in the frame entry as |
| 851 | // ABI states. |
| 852 | TEST_F(CodegenTest, ARM64FrameSizeSIMD) { |
| 853 | OverrideInstructionSetFeatures(InstructionSet::kArm64, "default"); |
| 854 | HGraph* graph = CreateGraph(); |
| 855 | arm64::CodeGeneratorARM64 codegen(graph, *compiler_options_); |
| 856 | |
| 857 | codegen.Initialize(); |
| 858 | graph->SetHasSIMD(true); |
| 859 | |
| 860 | DCHECK_EQ(arm64::callee_saved_fp_registers.GetCount(), 8); |
| 861 | vixl::aarch64::CPURegList reg_list = arm64::callee_saved_fp_registers; |
| 862 | while (!reg_list.IsEmpty()) { |
| 863 | uint32_t reg_code = reg_list.PopLowestIndex().GetCode(); |
| 864 | codegen.AddAllocatedRegister(Location::FpuRegisterLocation(reg_code)); |
| 865 | } |
| 866 | codegen.ComputeSpillMask(); |
| 867 | |
| 868 | EXPECT_EQ(codegen.GetFpuSpillSize(), kExpectedFPSpillSize); |
| 869 | } |
| 870 | |
| 871 | TEST_F(CodegenTest, ARM64FrameSizeNoSIMD) { |
| 872 | OverrideInstructionSetFeatures(InstructionSet::kArm64, "default"); |
| 873 | HGraph* graph = CreateGraph(); |
| 874 | arm64::CodeGeneratorARM64 codegen(graph, *compiler_options_); |
| 875 | |
| 876 | codegen.Initialize(); |
| 877 | graph->SetHasSIMD(false); |
| 878 | |
| 879 | DCHECK_EQ(arm64::callee_saved_fp_registers.GetCount(), 8); |
| 880 | vixl::aarch64::CPURegList reg_list = arm64::callee_saved_fp_registers; |
| 881 | while (!reg_list.IsEmpty()) { |
| 882 | uint32_t reg_code = reg_list.PopLowestIndex().GetCode(); |
| 883 | codegen.AddAllocatedRegister(Location::FpuRegisterLocation(reg_code)); |
| 884 | } |
| 885 | codegen.ComputeSpillMask(); |
| 886 | |
| 887 | EXPECT_EQ(codegen.GetFpuSpillSize(), kExpectedFPSpillSize); |
| 888 | } |
| 889 | |
| Roland Levillain | 558dea1 | 2017-01-27 19:40:44 +0000 | [diff] [blame] | 890 | #endif |
| 891 | |
| Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 892 | } // namespace art |