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