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