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