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> |
| 18 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 19 | #include "arch/instruction_set.h" |
Calin Juravle | 3416601 | 2014-12-19 17:22:29 +0000 | [diff] [blame] | 20 | #include "arch/arm/instruction_set_features_arm.h" |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 21 | #include "base/macros.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 22 | #include "builder.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 23 | #include "code_generator_arm.h" |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 24 | #include "code_generator_arm64.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 25 | #include "code_generator_x86.h" |
| 26 | #include "code_generator_x86_64.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 27 | #include "common_compiler_test.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 28 | #include "dex_file.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 29 | #include "dex_instruction.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 30 | #include "driver/compiler_options.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 31 | #include "nodes.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 32 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 33 | #include "prepare_for_register_allocation.h" |
| 34 | #include "register_allocator.h" |
| 35 | #include "ssa_liveness_analysis.h" |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 36 | #include "utils.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 37 | |
| 38 | #include "gtest/gtest.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 42 | class InternalCodeAllocator : public CodeAllocator { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 43 | public: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 44 | InternalCodeAllocator() : size_(0) { } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 45 | |
| 46 | virtual uint8_t* Allocate(size_t size) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 47 | size_ = size; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 48 | memory_.reset(new uint8_t[size]); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 49 | return memory_.get(); |
| 50 | } |
| 51 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 52 | size_t GetSize() const { return size_; } |
| 53 | uint8_t* GetMemory() const { return memory_.get(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 54 | |
| 55 | private: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 56 | size_t size_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 57 | std::unique_ptr<uint8_t[]> memory_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 58 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 59 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 62 | template <typename Expected> |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 63 | static void Run(const InternalCodeAllocator& allocator, |
| 64 | const CodeGenerator& codegen, |
| 65 | bool has_result, |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 66 | Expected expected) { |
| 67 | typedef Expected (*fptr)(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 68 | CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 69 | fptr f = reinterpret_cast<fptr>(allocator.GetMemory()); |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 70 | if (codegen.GetInstructionSet() == kThumb2) { |
| 71 | // For thumb we need the bottom bit set. |
| 72 | f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1); |
| 73 | } |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 74 | Expected result = f(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 75 | if (has_result) { |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 76 | ASSERT_EQ(result, expected); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 80 | template <typename Expected> |
| 81 | static void RunCodeBaseline(HGraph* graph, bool has_result, Expected expected) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 82 | InternalCodeAllocator allocator; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 83 | |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 84 | CompilerOptions compiler_options; |
| 85 | x86::CodeGeneratorX86 codegenX86(graph, compiler_options); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 86 | // We avoid doing a stack overflow check that requires the runtime being setup, |
| 87 | // by making sure the compiler knows the methods we are running are leaf methods. |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 88 | codegenX86.CompileBaseline(&allocator, true); |
| 89 | if (kRuntimeISA == kX86) { |
| 90 | Run(allocator, codegenX86, has_result, expected); |
| 91 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 92 | |
Andreas Gampe | df64950 | 2015-01-06 14:13:52 -0800 | [diff] [blame] | 93 | std::unique_ptr<const ArmInstructionSetFeatures> features( |
| 94 | ArmInstructionSetFeatures::FromCppDefines()); |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 95 | arm::CodeGeneratorARM codegenARM(graph, *features.get(), compiler_options); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 96 | codegenARM.CompileBaseline(&allocator, true); |
| 97 | if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) { |
| 98 | Run(allocator, codegenARM, has_result, expected); |
| 99 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 100 | |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 101 | x86_64::CodeGeneratorX86_64 codegenX86_64(graph, compiler_options); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 102 | codegenX86_64.CompileBaseline(&allocator, true); |
| 103 | if (kRuntimeISA == kX86_64) { |
| 104 | Run(allocator, codegenX86_64, has_result, expected); |
| 105 | } |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 106 | |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 107 | arm64::CodeGeneratorARM64 codegenARM64(graph, compiler_options); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 108 | codegenARM64.CompileBaseline(&allocator, true); |
| 109 | if (kRuntimeISA == kArm64) { |
| 110 | Run(allocator, codegenARM64, has_result, expected); |
| 111 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 114 | template <typename Expected> |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 115 | static void RunCodeOptimized(CodeGenerator* codegen, |
| 116 | HGraph* graph, |
| 117 | std::function<void(HGraph*)> hook_before_codegen, |
| 118 | bool has_result, |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 119 | Expected expected) { |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 120 | SsaLivenessAnalysis liveness(*graph, codegen); |
| 121 | liveness.Analyze(); |
| 122 | |
| 123 | RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness); |
| 124 | register_allocator.AllocateRegisters(); |
| 125 | hook_before_codegen(graph); |
| 126 | |
| 127 | InternalCodeAllocator allocator; |
| 128 | codegen->CompileOptimized(&allocator); |
| 129 | Run(allocator, *codegen, has_result, expected); |
| 130 | } |
| 131 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 132 | template <typename Expected> |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 133 | static void RunCodeOptimized(HGraph* graph, |
| 134 | std::function<void(HGraph*)> hook_before_codegen, |
| 135 | bool has_result, |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 136 | Expected expected) { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 137 | CompilerOptions compiler_options; |
Alexandre Rames | 3e69f16 | 2014-12-10 10:36:50 +0000 | [diff] [blame] | 138 | if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 139 | arm::CodeGeneratorARM codegenARM(graph, |
| 140 | *ArmInstructionSetFeatures::FromCppDefines(), |
| 141 | compiler_options); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 142 | RunCodeOptimized(&codegenARM, graph, hook_before_codegen, has_result, expected); |
Alexandre Rames | 3e69f16 | 2014-12-10 10:36:50 +0000 | [diff] [blame] | 143 | } else if (kRuntimeISA == kArm64) { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 144 | arm64::CodeGeneratorARM64 codegenARM64(graph, compiler_options); |
Alexandre Rames | 3e69f16 | 2014-12-10 10:36:50 +0000 | [diff] [blame] | 145 | RunCodeOptimized(&codegenARM64, graph, hook_before_codegen, has_result, expected); |
| 146 | } else if (kRuntimeISA == kX86) { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 147 | x86::CodeGeneratorX86 codegenX86(graph, compiler_options); |
Alexandre Rames | 3e69f16 | 2014-12-10 10:36:50 +0000 | [diff] [blame] | 148 | RunCodeOptimized(&codegenX86, graph, hook_before_codegen, has_result, expected); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 149 | } else if (kRuntimeISA == kX86_64) { |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 150 | x86_64::CodeGeneratorX86_64 codegenX86_64(graph, compiler_options); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 151 | RunCodeOptimized(&codegenX86_64, graph, hook_before_codegen, has_result, expected); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) { |
| 156 | ArenaPool pool; |
| 157 | ArenaAllocator arena(&pool); |
| 158 | HGraphBuilder builder(&arena); |
| 159 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 160 | HGraph* graph = builder.BuildGraph(*item); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 161 | ASSERT_NE(graph, nullptr); |
Calin Juravle | 039b6e2 | 2014-10-23 12:32:11 +0100 | [diff] [blame] | 162 | // Remove suspend checks, they cannot be executed in this context. |
Calin Juravle | 48dee04 | 2014-10-22 15:54:12 +0100 | [diff] [blame] | 163 | RemoveSuspendChecks(graph); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 164 | RunCodeBaseline(graph, has_result, expected); |
| 165 | } |
| 166 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 167 | static void TestCodeLong(const uint16_t* data, bool has_result, int64_t expected) { |
| 168 | ArenaPool pool; |
| 169 | ArenaAllocator arena(&pool); |
| 170 | HGraphBuilder builder(&arena, Primitive::kPrimLong); |
| 171 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 172 | HGraph* graph = builder.BuildGraph(*item); |
| 173 | ASSERT_NE(graph, nullptr); |
| 174 | // Remove suspend checks, they cannot be executed in this context. |
| 175 | RemoveSuspendChecks(graph); |
| 176 | RunCodeBaseline(graph, has_result, expected); |
| 177 | } |
| 178 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 179 | TEST(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 180 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
| 181 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 184 | TEST(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 185 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 186 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 187 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 188 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 189 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 192 | TEST(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 193 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 194 | Instruction::GOTO | 0x100, |
| 195 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 196 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 197 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 198 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 201 | TEST(CodegenTest, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 202 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 203 | Instruction::GOTO | 0x200, |
| 204 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 205 | Instruction::GOTO | 0xFF00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 206 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 207 | TestCode(data1); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 208 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 209 | const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 210 | Instruction::GOTO_16, 3, |
| 211 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 212 | Instruction::GOTO_16, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 213 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 214 | TestCode(data2); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 215 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 216 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 217 | Instruction::GOTO_32, 4, 0, |
| 218 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 219 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 220 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 221 | TestCode(data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 224 | TEST(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 225 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 226 | Instruction::RETURN_VOID, |
| 227 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 228 | Instruction::GOTO | 0xFE00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 229 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 230 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 233 | TEST(CodegenTest, CFG5) { |
| 234 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 235 | Instruction::CONST_4 | 0 | 0, |
| 236 | Instruction::IF_EQ, 3, |
| 237 | Instruction::GOTO | 0x100, |
| 238 | Instruction::RETURN_VOID); |
| 239 | |
| 240 | TestCode(data); |
| 241 | } |
| 242 | |
| 243 | TEST(CodegenTest, IntConstant) { |
| 244 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 245 | Instruction::CONST_4 | 0 | 0, |
| 246 | Instruction::RETURN_VOID); |
| 247 | |
| 248 | TestCode(data); |
| 249 | } |
| 250 | |
| 251 | TEST(CodegenTest, Return1) { |
| 252 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 253 | Instruction::CONST_4 | 0 | 0, |
| 254 | Instruction::RETURN | 0); |
| 255 | |
| 256 | TestCode(data, true, 0); |
| 257 | } |
| 258 | |
| 259 | TEST(CodegenTest, Return2) { |
| 260 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 261 | Instruction::CONST_4 | 0 | 0, |
| 262 | Instruction::CONST_4 | 0 | 1 << 8, |
| 263 | Instruction::RETURN | 1 << 8); |
| 264 | |
| 265 | TestCode(data, true, 0); |
| 266 | } |
| 267 | |
| 268 | TEST(CodegenTest, Return3) { |
| 269 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 270 | Instruction::CONST_4 | 0 | 0, |
| 271 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 272 | Instruction::RETURN | 1 << 8); |
| 273 | |
| 274 | TestCode(data, true, 1); |
| 275 | } |
| 276 | |
| 277 | TEST(CodegenTest, ReturnIf1) { |
| 278 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 279 | Instruction::CONST_4 | 0 | 0, |
| 280 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 281 | Instruction::IF_EQ, 3, |
| 282 | Instruction::RETURN | 0 << 8, |
| 283 | Instruction::RETURN | 1 << 8); |
| 284 | |
| 285 | TestCode(data, true, 1); |
| 286 | } |
| 287 | |
| 288 | TEST(CodegenTest, ReturnIf2) { |
| 289 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 290 | Instruction::CONST_4 | 0 | 0, |
| 291 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 292 | Instruction::IF_EQ | 0 << 4 | 1 << 8, 3, |
| 293 | Instruction::RETURN | 0 << 8, |
| 294 | Instruction::RETURN | 1 << 8); |
| 295 | |
| 296 | TestCode(data, true, 0); |
| 297 | } |
| 298 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 299 | // Exercise bit-wise (one's complement) not-int instruction. |
| 300 | #define NOT_INT_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
| 301 | TEST(CodegenTest, TEST_NAME) { \ |
| 302 | const int32_t input = INPUT; \ |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 303 | const uint16_t input_lo = Low16Bits(input); \ |
| 304 | const uint16_t input_hi = High16Bits(input); \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 305 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \ |
| 306 | Instruction::CONST | 0 << 8, input_lo, input_hi, \ |
| 307 | Instruction::NOT_INT | 1 << 8 | 0 << 12 , \ |
| 308 | Instruction::RETURN | 1 << 8); \ |
| 309 | \ |
| 310 | TestCode(data, true, EXPECTED_OUTPUT); \ |
| 311 | } |
| 312 | |
| 313 | NOT_INT_TEST(ReturnNotIntMinus2, -2, 1) |
| 314 | NOT_INT_TEST(ReturnNotIntMinus1, -1, 0) |
| 315 | NOT_INT_TEST(ReturnNotInt0, 0, -1) |
| 316 | NOT_INT_TEST(ReturnNotInt1, 1, -2) |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 317 | NOT_INT_TEST(ReturnNotIntINT32_MIN, -2147483648, 2147483647) // (2^31) - 1 |
| 318 | NOT_INT_TEST(ReturnNotIntINT32_MINPlus1, -2147483647, 2147483646) // (2^31) - 2 |
| 319 | NOT_INT_TEST(ReturnNotIntINT32_MAXMinus1, 2147483646, -2147483647) // -(2^31) - 1 |
| 320 | NOT_INT_TEST(ReturnNotIntINT32_MAX, 2147483647, -2147483648) // -(2^31) |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 321 | |
| 322 | #undef NOT_INT_TEST |
| 323 | |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 324 | // Exercise bit-wise (one's complement) not-long instruction. |
| 325 | #define NOT_LONG_TEST(TEST_NAME, INPUT, EXPECTED_OUTPUT) \ |
| 326 | TEST(CodegenTest, TEST_NAME) { \ |
| 327 | const int64_t input = INPUT; \ |
| 328 | const uint16_t word0 = Low16Bits(Low32Bits(input)); /* LSW. */ \ |
| 329 | const uint16_t word1 = High16Bits(Low32Bits(input)); \ |
| 330 | const uint16_t word2 = Low16Bits(High32Bits(input)); \ |
| 331 | const uint16_t word3 = High16Bits(High32Bits(input)); /* MSW. */ \ |
| 332 | const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM( \ |
| 333 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, \ |
| 334 | Instruction::NOT_LONG | 2 << 8 | 0 << 12, \ |
| 335 | Instruction::RETURN_WIDE | 2 << 8); \ |
| 336 | \ |
| 337 | TestCodeLong(data, true, EXPECTED_OUTPUT); \ |
| 338 | } |
| 339 | |
| 340 | NOT_LONG_TEST(ReturnNotLongMinus2, INT64_C(-2), INT64_C(1)) |
| 341 | NOT_LONG_TEST(ReturnNotLongMinus1, INT64_C(-1), INT64_C(0)) |
| 342 | NOT_LONG_TEST(ReturnNotLong0, INT64_C(0), INT64_C(-1)) |
| 343 | NOT_LONG_TEST(ReturnNotLong1, INT64_C(1), INT64_C(-2)) |
| 344 | |
| 345 | NOT_LONG_TEST(ReturnNotLongINT32_MIN, |
| 346 | INT64_C(-2147483648), |
| 347 | INT64_C(2147483647)) // (2^31) - 1 |
| 348 | NOT_LONG_TEST(ReturnNotLongINT32_MINPlus1, |
| 349 | INT64_C(-2147483647), |
| 350 | INT64_C(2147483646)) // (2^31) - 2 |
| 351 | NOT_LONG_TEST(ReturnNotLongINT32_MAXMinus1, |
| 352 | INT64_C(2147483646), |
| 353 | INT64_C(-2147483647)) // -(2^31) - 1 |
| 354 | NOT_LONG_TEST(ReturnNotLongINT32_MAX, |
| 355 | INT64_C(2147483647), |
| 356 | INT64_C(-2147483648)) // -(2^31) |
| 357 | |
| 358 | // Note that the C++ compiler won't accept |
| 359 | // INT64_C(-9223372036854775808) (that is, INT64_MIN) as a valid |
| 360 | // int64_t literal, so we use INT64_C(-9223372036854775807)-1 instead. |
| 361 | NOT_LONG_TEST(ReturnNotINT64_MIN, |
| 362 | INT64_C(-9223372036854775807)-1, |
| 363 | INT64_C(9223372036854775807)); // (2^63) - 1 |
| 364 | NOT_LONG_TEST(ReturnNotINT64_MINPlus1, |
| 365 | INT64_C(-9223372036854775807), |
| 366 | INT64_C(9223372036854775806)); // (2^63) - 2 |
| 367 | NOT_LONG_TEST(ReturnNotLongINT64_MAXMinus1, |
| 368 | INT64_C(9223372036854775806), |
| 369 | INT64_C(-9223372036854775807)); // -(2^63) - 1 |
| 370 | NOT_LONG_TEST(ReturnNotLongINT64_MAX, |
| 371 | INT64_C(9223372036854775807), |
| 372 | INT64_C(-9223372036854775807)-1); // -(2^63) |
| 373 | |
| 374 | #undef NOT_LONG_TEST |
| 375 | |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 376 | TEST(CodegenTest, IntToLongOfLongToInt) { |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 377 | const int64_t input = INT64_C(4294967296); // 2^32 |
| 378 | const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW. |
| 379 | const uint16_t word1 = High16Bits(Low32Bits(input)); |
| 380 | const uint16_t word2 = Low16Bits(High32Bits(input)); |
| 381 | const uint16_t word3 = High16Bits(High32Bits(input)); // MSW. |
| 382 | const uint16_t data[] = FIVE_REGISTERS_CODE_ITEM( |
| 383 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, |
| 384 | Instruction::CONST_WIDE | 2 << 8, 1, 0, 0, 0, |
| 385 | Instruction::ADD_LONG | 0, 0 << 8 | 2, // v0 <- 2^32 + 1 |
| 386 | Instruction::LONG_TO_INT | 4 << 8 | 0 << 12, |
| 387 | Instruction::INT_TO_LONG | 2 << 8 | 4 << 12, |
| 388 | Instruction::RETURN_WIDE | 2 << 8); |
| 389 | |
| 390 | TestCodeLong(data, true, 1); |
| 391 | } |
| 392 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 393 | TEST(CodegenTest, ReturnAdd1) { |
| 394 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 395 | Instruction::CONST_4 | 3 << 12 | 0, |
| 396 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 397 | Instruction::ADD_INT, 1 << 8 | 0, |
| 398 | Instruction::RETURN); |
| 399 | |
| 400 | TestCode(data, true, 7); |
| 401 | } |
| 402 | |
| 403 | TEST(CodegenTest, ReturnAdd2) { |
| 404 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 405 | Instruction::CONST_4 | 3 << 12 | 0, |
| 406 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 407 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 408 | Instruction::RETURN); |
| 409 | |
| 410 | TestCode(data, true, 7); |
| 411 | } |
| 412 | |
| 413 | TEST(CodegenTest, ReturnAdd3) { |
| 414 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 415 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 416 | Instruction::ADD_INT_LIT8, 3 << 8 | 0, |
| 417 | Instruction::RETURN); |
| 418 | |
| 419 | TestCode(data, true, 7); |
| 420 | } |
| 421 | |
| 422 | TEST(CodegenTest, ReturnAdd4) { |
| 423 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 424 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 425 | Instruction::ADD_INT_LIT16, 3, |
| 426 | Instruction::RETURN); |
| 427 | |
| 428 | TestCode(data, true, 7); |
| 429 | } |
| 430 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 431 | TEST(CodegenTest, NonMaterializedCondition) { |
| 432 | ArenaPool pool; |
| 433 | ArenaAllocator allocator(&pool); |
| 434 | |
| 435 | HGraph* graph = new (&allocator) HGraph(&allocator); |
| 436 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 437 | graph->AddBlock(entry); |
| 438 | graph->SetEntryBlock(entry); |
| 439 | entry->AddInstruction(new (&allocator) HGoto()); |
| 440 | |
| 441 | HBasicBlock* first_block = new (&allocator) HBasicBlock(graph); |
| 442 | graph->AddBlock(first_block); |
| 443 | entry->AddSuccessor(first_block); |
| 444 | HIntConstant* constant0 = new (&allocator) HIntConstant(0); |
| 445 | entry->AddInstruction(constant0); |
| 446 | HIntConstant* constant1 = new (&allocator) HIntConstant(1); |
| 447 | entry->AddInstruction(constant1); |
| 448 | HEqual* equal = new (&allocator) HEqual(constant0, constant0); |
| 449 | first_block->AddInstruction(equal); |
| 450 | first_block->AddInstruction(new (&allocator) HIf(equal)); |
| 451 | |
| 452 | HBasicBlock* then = new (&allocator) HBasicBlock(graph); |
| 453 | HBasicBlock* else_ = new (&allocator) HBasicBlock(graph); |
| 454 | HBasicBlock* exit = new (&allocator) HBasicBlock(graph); |
| 455 | |
| 456 | graph->AddBlock(then); |
| 457 | graph->AddBlock(else_); |
| 458 | graph->AddBlock(exit); |
| 459 | first_block->AddSuccessor(then); |
| 460 | first_block->AddSuccessor(else_); |
| 461 | then->AddSuccessor(exit); |
| 462 | else_->AddSuccessor(exit); |
| 463 | |
| 464 | exit->AddInstruction(new (&allocator) HExit()); |
| 465 | then->AddInstruction(new (&allocator) HReturn(constant0)); |
| 466 | else_->AddInstruction(new (&allocator) HReturn(constant1)); |
| 467 | |
| 468 | ASSERT_TRUE(equal->NeedsMaterialization()); |
| 469 | graph->BuildDominatorTree(); |
| 470 | PrepareForRegisterAllocation(graph).Run(); |
| 471 | ASSERT_FALSE(equal->NeedsMaterialization()); |
| 472 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 473 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 474 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0); |
| 475 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 476 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 477 | }; |
| 478 | |
| 479 | RunCodeOptimized(graph, hook_before_codegen, true, 0); |
| 480 | } |
| 481 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 482 | #define MUL_TEST(TYPE, TEST_NAME) \ |
| 483 | TEST(CodegenTest, Return ## TEST_NAME) { \ |
| 484 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \ |
| 485 | Instruction::CONST_4 | 3 << 12 | 0, \ |
| 486 | Instruction::CONST_4 | 4 << 12 | 1 << 8, \ |
| 487 | Instruction::MUL_ ## TYPE, 1 << 8 | 0, \ |
| 488 | Instruction::RETURN); \ |
| 489 | \ |
| 490 | TestCode(data, true, 12); \ |
| 491 | } \ |
| 492 | \ |
| 493 | TEST(CodegenTest, Return ## TEST_NAME ## 2addr) { \ |
| 494 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( \ |
| 495 | Instruction::CONST_4 | 3 << 12 | 0, \ |
| 496 | Instruction::CONST_4 | 4 << 12 | 1 << 8, \ |
| 497 | Instruction::MUL_ ## TYPE ## _2ADDR | 1 << 12, \ |
| 498 | Instruction::RETURN); \ |
| 499 | \ |
| 500 | TestCode(data, true, 12); \ |
| 501 | } |
| 502 | |
| 503 | MUL_TEST(INT, MulInt); |
| 504 | MUL_TEST(LONG, MulLong); |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 505 | |
| 506 | TEST(CodegenTest, ReturnMulIntLit8) { |
| 507 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 508 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 509 | Instruction::MUL_INT_LIT8, 3 << 8 | 0, |
| 510 | Instruction::RETURN); |
| 511 | |
| 512 | TestCode(data, true, 12); |
| 513 | } |
| 514 | |
| 515 | TEST(CodegenTest, ReturnMulIntLit16) { |
| 516 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 517 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 518 | Instruction::MUL_INT_LIT16, 3, |
| 519 | Instruction::RETURN); |
| 520 | |
| 521 | TestCode(data, true, 12); |
| 522 | } |
| 523 | |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 524 | TEST(CodegenTest, MaterializedCondition1) { |
| 525 | // Check that condition are materialized correctly. A materialized condition |
| 526 | // should yield `1` if it evaluated to true, and `0` otherwise. |
| 527 | // We force the materialization of comparisons for different combinations of |
| 528 | // inputs and check the results. |
| 529 | |
| 530 | int lhs[] = {1, 2, -1, 2, 0xabc}; |
| 531 | int rhs[] = {2, 1, 2, -1, 0xabc}; |
| 532 | |
| 533 | for (size_t i = 0; i < arraysize(lhs); i++) { |
| 534 | ArenaPool pool; |
| 535 | ArenaAllocator allocator(&pool); |
| 536 | HGraph* graph = new (&allocator) HGraph(&allocator); |
| 537 | |
| 538 | HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph); |
| 539 | graph->AddBlock(entry_block); |
| 540 | graph->SetEntryBlock(entry_block); |
| 541 | entry_block->AddInstruction(new (&allocator) HGoto()); |
| 542 | HBasicBlock* code_block = new (&allocator) HBasicBlock(graph); |
| 543 | graph->AddBlock(code_block); |
| 544 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 545 | graph->AddBlock(exit_block); |
| 546 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 547 | |
| 548 | entry_block->AddSuccessor(code_block); |
| 549 | code_block->AddSuccessor(exit_block); |
| 550 | graph->SetExitBlock(exit_block); |
| 551 | |
| 552 | HIntConstant cst_lhs(lhs[i]); |
| 553 | code_block->AddInstruction(&cst_lhs); |
| 554 | HIntConstant cst_rhs(rhs[i]); |
| 555 | code_block->AddInstruction(&cst_rhs); |
| 556 | HLessThan cmp_lt(&cst_lhs, &cst_rhs); |
| 557 | code_block->AddInstruction(&cmp_lt); |
| 558 | HReturn ret(&cmp_lt); |
| 559 | code_block->AddInstruction(&ret); |
| 560 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 561 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 562 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0); |
| 563 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 564 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 565 | }; |
| 566 | |
| 567 | RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | TEST(CodegenTest, MaterializedCondition2) { |
| 572 | // Check that HIf correctly interprets a materialized condition. |
| 573 | // We force the materialization of comparisons for different combinations of |
| 574 | // inputs. An HIf takes the materialized combination as input and returns a |
| 575 | // value that we verify. |
| 576 | |
| 577 | int lhs[] = {1, 2, -1, 2, 0xabc}; |
| 578 | int rhs[] = {2, 1, 2, -1, 0xabc}; |
| 579 | |
| 580 | |
| 581 | for (size_t i = 0; i < arraysize(lhs); i++) { |
| 582 | ArenaPool pool; |
| 583 | ArenaAllocator allocator(&pool); |
| 584 | HGraph* graph = new (&allocator) HGraph(&allocator); |
| 585 | |
| 586 | HBasicBlock* entry_block = new (&allocator) HBasicBlock(graph); |
| 587 | graph->AddBlock(entry_block); |
| 588 | graph->SetEntryBlock(entry_block); |
| 589 | entry_block->AddInstruction(new (&allocator) HGoto()); |
| 590 | |
| 591 | HBasicBlock* if_block = new (&allocator) HBasicBlock(graph); |
| 592 | graph->AddBlock(if_block); |
| 593 | HBasicBlock* if_true_block = new (&allocator) HBasicBlock(graph); |
| 594 | graph->AddBlock(if_true_block); |
| 595 | HBasicBlock* if_false_block = new (&allocator) HBasicBlock(graph); |
| 596 | graph->AddBlock(if_false_block); |
| 597 | HBasicBlock* exit_block = new (&allocator) HBasicBlock(graph); |
| 598 | graph->AddBlock(exit_block); |
| 599 | exit_block->AddInstruction(new (&allocator) HExit()); |
| 600 | |
| 601 | graph->SetEntryBlock(entry_block); |
| 602 | entry_block->AddSuccessor(if_block); |
| 603 | if_block->AddSuccessor(if_true_block); |
| 604 | if_block->AddSuccessor(if_false_block); |
| 605 | if_true_block->AddSuccessor(exit_block); |
| 606 | if_false_block->AddSuccessor(exit_block); |
| 607 | graph->SetExitBlock(exit_block); |
| 608 | |
| 609 | HIntConstant cst_lhs(lhs[i]); |
| 610 | if_block->AddInstruction(&cst_lhs); |
| 611 | HIntConstant cst_rhs(rhs[i]); |
| 612 | if_block->AddInstruction(&cst_rhs); |
| 613 | HLessThan cmp_lt(&cst_lhs, &cst_rhs); |
| 614 | if_block->AddInstruction(&cmp_lt); |
| 615 | // We insert a temporary to separate the HIf from the HLessThan and force |
| 616 | // the materialization of the condition. |
| 617 | HTemporary force_materialization(0); |
| 618 | if_block->AddInstruction(&force_materialization); |
| 619 | HIf if_lt(&cmp_lt); |
| 620 | if_block->AddInstruction(&if_lt); |
| 621 | |
| 622 | HIntConstant cst_lt(1); |
| 623 | if_true_block->AddInstruction(&cst_lt); |
| 624 | HReturn ret_lt(&cst_lt); |
| 625 | if_true_block->AddInstruction(&ret_lt); |
| 626 | HIntConstant cst_ge(0); |
| 627 | if_false_block->AddInstruction(&cst_ge); |
| 628 | HReturn ret_ge(&cst_ge); |
| 629 | if_false_block->AddInstruction(&ret_ge); |
| 630 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 631 | auto hook_before_codegen = [](HGraph* graph_in) { |
| 632 | HBasicBlock* block = graph_in->GetEntryBlock()->GetSuccessors().Get(0); |
| 633 | HParallelMove* move = new (graph_in->GetArena()) HParallelMove(graph_in->GetArena()); |
Alexandre Rames | 9273074 | 2014-10-01 12:55:56 +0100 | [diff] [blame] | 634 | block->InsertInstructionBefore(move, block->GetLastInstruction()); |
| 635 | }; |
| 636 | |
| 637 | RunCodeOptimized(graph, hook_before_codegen, true, lhs[i] < rhs[i]); |
| 638 | } |
| 639 | } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 640 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 641 | TEST(CodegenTest, ReturnDivIntLit8) { |
| 642 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 643 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 644 | Instruction::DIV_INT_LIT8, 3 << 8 | 0, |
| 645 | Instruction::RETURN); |
| 646 | |
| 647 | TestCode(data, true, 1); |
| 648 | } |
| 649 | |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 650 | TEST(CodegenTest, ReturnDivInt2Addr) { |
Calin Juravle | 865fc88 | 2014-11-06 17:09:03 +0000 | [diff] [blame] | 651 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 652 | Instruction::CONST_4 | 4 << 12 | 0, |
| 653 | Instruction::CONST_4 | 2 << 12 | 1 << 8, |
| 654 | Instruction::DIV_INT_2ADDR | 1 << 12, |
| 655 | Instruction::RETURN); |
| 656 | |
| 657 | TestCode(data, true, 2); |
| 658 | } |
| 659 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 660 | } // namespace art |