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