Chao-ying Fu | c4013ea | 2015-04-22 10:51:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #include "dex/quick/quick_compiler.h" |
| 18 | #include "dex/pass_manager.h" |
| 19 | #include "dex/verification_results.h" |
| 20 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
| 21 | #include "runtime/dex_file.h" |
| 22 | #include "driver/compiler_options.h" |
| 23 | #include "driver/compiler_driver.h" |
| 24 | #include "codegen_x86.h" |
| 25 | #include "gtest/gtest.h" |
| 26 | #include "utils/assembler_test_base.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | class QuickAssembleX86TestBase : public testing::Test { |
| 31 | protected: |
| 32 | X86Mir2Lir* Prepare(InstructionSet target) { |
| 33 | isa_ = target; |
| 34 | pool_.reset(new ArenaPool()); |
| 35 | compiler_options_.reset(new CompilerOptions( |
| 36 | CompilerOptions::kDefaultCompilerFilter, |
| 37 | CompilerOptions::kDefaultHugeMethodThreshold, |
| 38 | CompilerOptions::kDefaultLargeMethodThreshold, |
| 39 | CompilerOptions::kDefaultSmallMethodThreshold, |
| 40 | CompilerOptions::kDefaultTinyMethodThreshold, |
| 41 | CompilerOptions::kDefaultNumDexMethodsThreshold, |
Calin Juravle | 0941b9d | 2015-07-29 18:59:13 +0100 | [diff] [blame] | 42 | CompilerOptions::kDefaultInlineDepthLimit, |
| 43 | CompilerOptions::kDefaultInlineMaxCodeUnits, |
Chao-ying Fu | c4013ea | 2015-04-22 10:51:21 -0700 | [diff] [blame] | 44 | false, |
| 45 | CompilerOptions::kDefaultTopKProfileThreshold, |
| 46 | false, |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 47 | CompilerOptions::kDefaultGenerateDebugInfo, |
Chao-ying Fu | c4013ea | 2015-04-22 10:51:21 -0700 | [diff] [blame] | 48 | false, |
| 49 | false, |
| 50 | false, |
| 51 | false, |
| 52 | nullptr, |
| 53 | new PassManagerOptions(), |
| 54 | nullptr, |
| 55 | false)); |
| 56 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
| 57 | method_inliner_map_.reset(new DexFileToMethodInlinerMap()); |
| 58 | compiler_driver_.reset(new CompilerDriver( |
| 59 | compiler_options_.get(), |
| 60 | verification_results_.get(), |
| 61 | method_inliner_map_.get(), |
| 62 | Compiler::kQuick, |
| 63 | isa_, |
| 64 | nullptr, |
| 65 | false, |
| 66 | nullptr, |
| 67 | nullptr, |
| 68 | nullptr, |
| 69 | 0, |
| 70 | false, |
| 71 | false, |
| 72 | "", |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame^] | 73 | false, |
Chao-ying Fu | c4013ea | 2015-04-22 10:51:21 -0700 | [diff] [blame] | 74 | 0, |
| 75 | -1, |
| 76 | "")); |
| 77 | cu_.reset(new CompilationUnit(pool_.get(), isa_, compiler_driver_.get(), nullptr)); |
| 78 | DexFile::CodeItem* code_item = static_cast<DexFile::CodeItem*>( |
| 79 | cu_->arena.Alloc(sizeof(DexFile::CodeItem), kArenaAllocMisc)); |
| 80 | memset(code_item, 0, sizeof(DexFile::CodeItem)); |
| 81 | cu_->mir_graph.reset(new MIRGraph(cu_.get(), &cu_->arena)); |
| 82 | cu_->mir_graph->current_code_item_ = code_item; |
| 83 | cu_->cg.reset(QuickCompiler::GetCodeGenerator(cu_.get(), nullptr)); |
| 84 | |
| 85 | test_helper_.reset(new AssemblerTestInfrastructure( |
| 86 | isa_ == kX86 ? "x86" : "x86_64", |
| 87 | "as", |
| 88 | isa_ == kX86 ? " --32" : "", |
| 89 | "objdump", |
| 90 | " -h", |
| 91 | "objdump", |
| 92 | isa_ == kX86 ? |
| 93 | " -D -bbinary -mi386 --no-show-raw-insn" : |
| 94 | " -D -bbinary -mi386:x86-64 -Mx86-64,addr64,data32 --no-show-raw-insn", |
| 95 | nullptr)); |
| 96 | |
| 97 | X86Mir2Lir* m2l = static_cast<X86Mir2Lir*>(cu_->cg.get()); |
| 98 | m2l->CompilerInitializeRegAlloc(); |
| 99 | return m2l; |
| 100 | } |
| 101 | |
| 102 | void Release() { |
| 103 | cu_.reset(); |
| 104 | compiler_driver_.reset(); |
| 105 | method_inliner_map_.reset(); |
| 106 | verification_results_.reset(); |
| 107 | compiler_options_.reset(); |
| 108 | pool_.reset(); |
| 109 | |
| 110 | test_helper_.reset(); |
| 111 | } |
| 112 | |
| 113 | void TearDown() OVERRIDE { |
| 114 | Release(); |
| 115 | } |
| 116 | |
| 117 | bool CheckTools(InstructionSet target) { |
| 118 | Prepare(target); |
| 119 | bool result = test_helper_->CheckTools(); |
| 120 | Release(); |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | std::unique_ptr<CompilationUnit> cu_; |
| 125 | std::unique_ptr<AssemblerTestInfrastructure> test_helper_; |
| 126 | |
| 127 | private: |
| 128 | InstructionSet isa_; |
| 129 | std::unique_ptr<ArenaPool> pool_; |
| 130 | std::unique_ptr<CompilerOptions> compiler_options_; |
| 131 | std::unique_ptr<VerificationResults> verification_results_; |
| 132 | std::unique_ptr<DexFileToMethodInlinerMap> method_inliner_map_; |
| 133 | std::unique_ptr<CompilerDriver> compiler_driver_; |
| 134 | }; |
| 135 | |
| 136 | class QuickAssembleX86LowLevelTest : public QuickAssembleX86TestBase { |
| 137 | protected: |
| 138 | void Test(InstructionSet target, std::string test_name, std::string gcc_asm, |
| 139 | int opcode, int op0 = 0, int op1 = 0, int op2 = 0, int op3 = 0, int op4 = 0) { |
| 140 | X86Mir2Lir* m2l = Prepare(target); |
| 141 | |
| 142 | LIR lir; |
| 143 | memset(&lir, 0, sizeof(LIR)); |
| 144 | lir.opcode = opcode; |
| 145 | lir.operands[0] = op0; |
| 146 | lir.operands[1] = op1; |
| 147 | lir.operands[2] = op2; |
| 148 | lir.operands[3] = op3; |
| 149 | lir.operands[4] = op4; |
| 150 | lir.flags.size = m2l->GetInsnSize(&lir); |
| 151 | |
| 152 | AssemblerStatus status = m2l->AssembleInstructions(&lir, 0); |
| 153 | // We don't expect a retry. |
| 154 | ASSERT_EQ(status, AssemblerStatus::kSuccess); |
| 155 | |
| 156 | // Need a "base" std::vector. |
| 157 | std::vector<uint8_t> buffer(m2l->code_buffer_.begin(), m2l->code_buffer_.end()); |
| 158 | test_helper_->Driver(buffer, gcc_asm, test_name); |
| 159 | |
| 160 | Release(); |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | TEST_F(QuickAssembleX86LowLevelTest, Addpd) { |
| 165 | Test(kX86, "Addpd", "addpd %xmm1, %xmm0\n", kX86AddpdRR, |
| 166 | RegStorage::Solo128(0).GetReg(), RegStorage::Solo128(1).GetReg()); |
| 167 | Test(kX86_64, "Addpd", "addpd %xmm1, %xmm0\n", kX86AddpdRR, |
| 168 | RegStorage::Solo128(0).GetReg(), RegStorage::Solo128(1).GetReg()); |
| 169 | } |
| 170 | |
| 171 | TEST_F(QuickAssembleX86LowLevelTest, Subpd) { |
| 172 | Test(kX86, "Subpd", "subpd %xmm1, %xmm0\n", kX86SubpdRR, |
| 173 | RegStorage::Solo128(0).GetReg(), RegStorage::Solo128(1).GetReg()); |
| 174 | Test(kX86_64, "Subpd", "subpd %xmm1, %xmm0\n", kX86SubpdRR, |
| 175 | RegStorage::Solo128(0).GetReg(), RegStorage::Solo128(1).GetReg()); |
| 176 | } |
| 177 | |
| 178 | TEST_F(QuickAssembleX86LowLevelTest, Mulpd) { |
| 179 | Test(kX86, "Mulpd", "mulpd %xmm1, %xmm0\n", kX86MulpdRR, |
| 180 | RegStorage::Solo128(0).GetReg(), RegStorage::Solo128(1).GetReg()); |
| 181 | Test(kX86_64, "Mulpd", "mulpd %xmm1, %xmm0\n", kX86MulpdRR, |
| 182 | RegStorage::Solo128(0).GetReg(), RegStorage::Solo128(1).GetReg()); |
| 183 | } |
| 184 | |
nikolay serdjuk | e0705f5 | 2015-04-27 17:52:57 +0600 | [diff] [blame] | 185 | TEST_F(QuickAssembleX86LowLevelTest, Pextrw) { |
| 186 | Test(kX86, "Pextrw", "pextrw $7, %xmm3, 8(%eax)\n", kX86PextrwMRI, |
| 187 | RegStorage::Solo32(r0).GetReg(), 8, RegStorage::Solo128(3).GetReg(), 7); |
| 188 | Test(kX86_64, "Pextrw", "pextrw $7, %xmm8, 8(%r10)\n", kX86PextrwMRI, |
| 189 | RegStorage::Solo64(r10q).GetReg(), 8, RegStorage::Solo128(8).GetReg(), 7); |
| 190 | } |
| 191 | |
Chao-ying Fu | c4013ea | 2015-04-22 10:51:21 -0700 | [diff] [blame] | 192 | class QuickAssembleX86MacroTest : public QuickAssembleX86TestBase { |
| 193 | protected: |
| 194 | typedef void (X86Mir2Lir::*AsmFn)(MIR*); |
| 195 | |
| 196 | void TestVectorFn(InstructionSet target, |
| 197 | Instruction::Code opcode, |
| 198 | AsmFn f, |
| 199 | std::string inst_string) { |
| 200 | X86Mir2Lir *m2l = Prepare(target); |
| 201 | |
| 202 | // Create a vector MIR. |
| 203 | MIR* mir = cu_->mir_graph->NewMIR(); |
| 204 | mir->dalvikInsn.opcode = opcode; |
| 205 | mir->dalvikInsn.vA = 0; // Destination and source. |
| 206 | mir->dalvikInsn.vB = 1; // Source. |
| 207 | int vector_size = 128; |
| 208 | int vector_type = kDouble; |
| 209 | mir->dalvikInsn.vC = (vector_type << 16) | vector_size; // Type size. |
| 210 | (m2l->*f)(mir); |
| 211 | m2l->AssembleLIR(); |
| 212 | |
| 213 | std::string gcc_asm = inst_string + " %xmm1, %xmm0\n"; |
| 214 | // Need a "base" std::vector. |
| 215 | std::vector<uint8_t> buffer(m2l->code_buffer_.begin(), m2l->code_buffer_.end()); |
| 216 | test_helper_->Driver(buffer, gcc_asm, inst_string); |
| 217 | |
| 218 | Release(); |
| 219 | } |
| 220 | |
| 221 | // Tests are member functions as many of the assembler functions are protected or private, |
| 222 | // and it would be inelegant to define ART_FRIEND_TEST for all the tests. |
| 223 | |
| 224 | void TestAddpd() { |
| 225 | TestVectorFn(kX86, |
| 226 | static_cast<Instruction::Code>(kMirOpPackedAddition), |
| 227 | &X86Mir2Lir::GenAddVector, |
| 228 | "addpd"); |
| 229 | TestVectorFn(kX86_64, |
| 230 | static_cast<Instruction::Code>(kMirOpPackedAddition), |
| 231 | &X86Mir2Lir::GenAddVector, |
| 232 | "addpd"); |
| 233 | } |
| 234 | |
| 235 | void TestSubpd() { |
| 236 | TestVectorFn(kX86, |
| 237 | static_cast<Instruction::Code>(kMirOpPackedSubtract), |
| 238 | &X86Mir2Lir::GenSubtractVector, |
| 239 | "subpd"); |
| 240 | TestVectorFn(kX86_64, |
| 241 | static_cast<Instruction::Code>(kMirOpPackedSubtract), |
| 242 | &X86Mir2Lir::GenSubtractVector, |
| 243 | "subpd"); |
| 244 | } |
| 245 | |
| 246 | void TestMulpd() { |
| 247 | TestVectorFn(kX86, |
| 248 | static_cast<Instruction::Code>(kMirOpPackedMultiply), |
| 249 | &X86Mir2Lir::GenMultiplyVector, |
| 250 | "mulpd"); |
| 251 | TestVectorFn(kX86_64, |
| 252 | static_cast<Instruction::Code>(kMirOpPackedMultiply), |
| 253 | &X86Mir2Lir::GenMultiplyVector, |
| 254 | "mulpd"); |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | TEST_F(QuickAssembleX86MacroTest, CheckTools) { |
| 259 | ASSERT_TRUE(CheckTools(kX86)) << "x86 tools not found."; |
| 260 | ASSERT_TRUE(CheckTools(kX86_64)) << "x86_64 tools not found."; |
| 261 | } |
| 262 | |
| 263 | #define DECLARE_TEST(name) \ |
| 264 | TEST_F(QuickAssembleX86MacroTest, name) { \ |
| 265 | Test ## name(); \ |
| 266 | } |
| 267 | |
| 268 | DECLARE_TEST(Addpd) |
| 269 | DECLARE_TEST(Subpd) |
| 270 | DECLARE_TEST(Mulpd) |
| 271 | |
| 272 | } // namespace art |