David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [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 <memory> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "arch/instruction_set.h" |
| 21 | #include "cfi_test.h" |
| 22 | #include "gtest/gtest.h" |
| 23 | #include "optimizing/code_generator.h" |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 24 | #include "optimizing/optimizing_unit_test.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 25 | #include "utils/assembler.h" |
| 26 | |
| 27 | #include "optimizing/optimizing_cfi_test_expected.inc" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | // Run the tests only on host. |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 32 | #ifndef __ANDROID__ |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 33 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | class OptimizingCFITest : public CFITest { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 35 | public: |
| 36 | // Enable this flag to generate the expected outputs. |
| 37 | static constexpr bool kGenerateExpected = false; |
| 38 | |
| 39 | void TestImpl(InstructionSet isa, const char* isa_str, |
| 40 | const std::vector<uint8_t>& expected_asm, |
| 41 | const std::vector<uint8_t>& expected_cfi) { |
| 42 | // Setup simple context. |
| 43 | ArenaPool pool; |
| 44 | ArenaAllocator allocator(&pool); |
| 45 | CompilerOptions opts; |
| 46 | std::unique_ptr<const InstructionSetFeatures> isa_features; |
| 47 | std::string error; |
| 48 | isa_features.reset(InstructionSetFeatures::FromVariant(isa, "default", &error)); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 49 | HGraph* graph = CreateGraph(&allocator); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 50 | // Generate simple frame with some spills. |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 51 | std::unique_ptr<CodeGenerator> code_gen( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 52 | CodeGenerator::Create(graph, isa, *isa_features.get(), opts)); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 53 | const int frame_size = 64; |
| 54 | int core_reg = 0; |
| 55 | int fp_reg = 0; |
| 56 | for (int i = 0; i < 2; i++) { // Two registers of each kind. |
| 57 | for (; core_reg < 32; core_reg++) { |
| 58 | if (code_gen->IsCoreCalleeSaveRegister(core_reg)) { |
| 59 | auto location = Location::RegisterLocation(core_reg); |
| 60 | code_gen->AddAllocatedRegister(location); |
| 61 | core_reg++; |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | for (; fp_reg < 32; fp_reg++) { |
| 66 | if (code_gen->IsFloatingPointCalleeSaveRegister(fp_reg)) { |
| 67 | auto location = Location::FpuRegisterLocation(fp_reg); |
| 68 | code_gen->AddAllocatedRegister(location); |
| 69 | fp_reg++; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 74 | ArenaVector<HBasicBlock*> blocks(allocator.Adapter()); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 75 | code_gen->block_order_ = &blocks; |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 76 | code_gen->ComputeSpillMask(); |
| 77 | code_gen->SetFrameSize(frame_size); |
| 78 | code_gen->GenerateFrameEntry(); |
David Srbecky | c34dc93 | 2015-04-12 09:27:43 +0100 | [diff] [blame] | 79 | code_gen->GenerateFrameExit(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 80 | // Get the outputs. |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 81 | InternalCodeAllocator code_allocator; |
| 82 | code_gen->Finalize(&code_allocator); |
| 83 | const std::vector<uint8_t>& actual_asm = code_allocator.GetMemory(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 84 | Assembler* opt_asm = code_gen->GetAssembler(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 85 | const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data()); |
| 86 | |
| 87 | if (kGenerateExpected) { |
| 88 | GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi); |
| 89 | } else { |
| 90 | EXPECT_EQ(expected_asm, actual_asm); |
| 91 | EXPECT_EQ(expected_cfi, actual_cfi); |
| 92 | } |
| 93 | } |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | class InternalCodeAllocator : public CodeAllocator { |
| 97 | public: |
| 98 | InternalCodeAllocator() {} |
| 99 | |
| 100 | virtual uint8_t* Allocate(size_t size) { |
| 101 | memory_.resize(size); |
| 102 | return memory_.data(); |
| 103 | } |
| 104 | |
| 105 | const std::vector<uint8_t>& GetMemory() { return memory_; } |
| 106 | |
| 107 | private: |
| 108 | std::vector<uint8_t> memory_; |
| 109 | |
| 110 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
| 111 | }; |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | #define TEST_ISA(isa) \ |
| 115 | TEST_F(OptimizingCFITest, isa) { \ |
| 116 | std::vector<uint8_t> expected_asm(expected_asm_##isa, \ |
| 117 | expected_asm_##isa + arraysize(expected_asm_##isa)); \ |
| 118 | std::vector<uint8_t> expected_cfi(expected_cfi_##isa, \ |
| 119 | expected_cfi_##isa + arraysize(expected_cfi_##isa)); \ |
| 120 | TestImpl(isa, #isa, expected_asm, expected_cfi); \ |
| 121 | } |
| 122 | |
| 123 | TEST_ISA(kThumb2) |
| 124 | TEST_ISA(kArm64) |
| 125 | TEST_ISA(kX86) |
| 126 | TEST_ISA(kX86_64) |
| 127 | |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 128 | #endif // __ANDROID__ |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 129 | |
| 130 | } // namespace art |