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