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" |
Andreas Gampe | 2a5d728 | 2018-01-02 11:53:35 -0800 | [diff] [blame] | 21 | #include "base/runtime_debug.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 22 | #include "cfi_test.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 23 | #include "driver/compiler_options.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 24 | #include "gtest/gtest.h" |
| 25 | #include "optimizing/code_generator.h" |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 26 | #include "optimizing/optimizing_unit_test.h" |
Andreas Gampe | 217488a | 2017-09-18 08:34:42 -0700 | [diff] [blame] | 27 | #include "read_barrier_config.h" |
Nicolas Geoffray | 467d94a | 2017-03-16 10:24:17 +0000 | [diff] [blame] | 28 | #include "utils/arm/assembler_arm_vixl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 29 | #include "utils/assembler.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 30 | |
| 31 | #include "optimizing/optimizing_cfi_test_expected.inc" |
| 32 | |
Scott Wakeling | 90ab673 | 2016-12-08 10:25:03 +0000 | [diff] [blame] | 33 | namespace vixl32 = vixl::aarch32; |
| 34 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 35 | namespace art { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 36 | |
| 37 | // Run the tests only on host. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 38 | #ifndef ART_TARGET_ANDROID |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 39 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 40 | class OptimizingCFITest : public CFITest, public OptimizingUnitTestHelper { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 41 | public: |
| 42 | // Enable this flag to generate the expected outputs. |
| 43 | static constexpr bool kGenerateExpected = false; |
| 44 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 45 | OptimizingCFITest() |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 46 | : graph_(nullptr), |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 47 | code_gen_(), |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 48 | blocks_(GetAllocator()->Adapter()) {} |
| 49 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 50 | void SetUpFrame(InstructionSet isa) { |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 51 | OverrideInstructionSetFeatures(isa, "default"); |
| 52 | |
Andreas Gampe | 2a5d728 | 2018-01-02 11:53:35 -0800 | [diff] [blame] | 53 | // Ensure that slow-debug is off, so that there is no unexpected read-barrier check emitted. |
| 54 | SetRuntimeDebugFlagsEnabled(false); |
| 55 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 56 | // Setup simple context. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 57 | graph_ = CreateGraph(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 58 | // Generate simple frame with some spills. |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 59 | code_gen_ = CodeGenerator::Create(graph_, *compiler_options_); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 60 | code_gen_->GetAssembler()->cfi().SetEnabled(true); |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 61 | code_gen_->InitializeCodeGenerationData(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 62 | const int frame_size = 64; |
| 63 | int core_reg = 0; |
| 64 | int fp_reg = 0; |
| 65 | for (int i = 0; i < 2; i++) { // Two registers of each kind. |
| 66 | for (; core_reg < 32; core_reg++) { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 67 | if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 68 | auto location = Location::RegisterLocation(core_reg); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 69 | code_gen_->AddAllocatedRegister(location); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 70 | core_reg++; |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | for (; fp_reg < 32; fp_reg++) { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 75 | if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 76 | auto location = Location::FpuRegisterLocation(fp_reg); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 77 | code_gen_->AddAllocatedRegister(location); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 78 | fp_reg++; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | } |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 83 | code_gen_->block_order_ = &blocks_; |
| 84 | code_gen_->ComputeSpillMask(); |
| 85 | code_gen_->SetFrameSize(frame_size); |
| 86 | code_gen_->GenerateFrameEntry(); |
| 87 | } |
| 88 | |
| 89 | void Finish() { |
| 90 | code_gen_->GenerateFrameExit(); |
| 91 | code_gen_->Finalize(&code_allocator_); |
| 92 | } |
| 93 | |
| 94 | void Check(InstructionSet isa, |
| 95 | const char* isa_str, |
| 96 | const std::vector<uint8_t>& expected_asm, |
| 97 | const std::vector<uint8_t>& expected_cfi) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 98 | // Get the outputs. |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 99 | ArrayRef<const uint8_t> actual_asm = code_allocator_.GetMemory(); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 100 | Assembler* opt_asm = code_gen_->GetAssembler(); |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 101 | ArrayRef<const uint8_t> actual_cfi(*(opt_asm->cfi().data())); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 102 | |
| 103 | if (kGenerateExpected) { |
| 104 | GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi); |
| 105 | } else { |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 106 | EXPECT_EQ(ArrayRef<const uint8_t>(expected_asm), actual_asm); |
| 107 | EXPECT_EQ(ArrayRef<const uint8_t>(expected_cfi), actual_cfi); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 110 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 111 | void TestImpl(InstructionSet isa, const char* |
| 112 | isa_str, |
| 113 | const std::vector<uint8_t>& expected_asm, |
| 114 | const std::vector<uint8_t>& expected_cfi) { |
| 115 | SetUpFrame(isa); |
| 116 | Finish(); |
| 117 | Check(isa, isa_str, expected_asm, expected_cfi); |
| 118 | } |
| 119 | |
| 120 | CodeGenerator* GetCodeGenerator() { |
| 121 | return code_gen_.get(); |
| 122 | } |
| 123 | |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 124 | private: |
| 125 | class InternalCodeAllocator : public CodeAllocator { |
| 126 | public: |
| 127 | InternalCodeAllocator() {} |
| 128 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 129 | uint8_t* Allocate(size_t size) override { |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 130 | memory_.resize(size); |
| 131 | return memory_.data(); |
| 132 | } |
| 133 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 134 | ArrayRef<const uint8_t> GetMemory() const override { return ArrayRef<const uint8_t>(memory_); } |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | std::vector<uint8_t> memory_; |
| 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
| 140 | }; |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 141 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 142 | HGraph* graph_; |
| 143 | std::unique_ptr<CodeGenerator> code_gen_; |
| 144 | ArenaVector<HBasicBlock*> blocks_; |
| 145 | InternalCodeAllocator code_allocator_; |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 148 | #define TEST_ISA(isa) \ |
| 149 | TEST_F(OptimizingCFITest, isa) { \ |
| 150 | std::vector<uint8_t> expected_asm( \ |
| 151 | expected_asm_##isa, \ |
| 152 | expected_asm_##isa + arraysize(expected_asm_##isa)); \ |
| 153 | std::vector<uint8_t> expected_cfi( \ |
| 154 | expected_cfi_##isa, \ |
| 155 | expected_cfi_##isa + arraysize(expected_cfi_##isa)); \ |
| 156 | TestImpl(InstructionSet::isa, #isa, expected_asm, expected_cfi); \ |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Scott Wakeling | 90ab673 | 2016-12-08 10:25:03 +0000 | [diff] [blame] | 159 | #ifdef ART_ENABLE_CODEGEN_arm |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 160 | TEST_ISA(kThumb2) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 161 | #endif |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 162 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 163 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 164 | // Run the tests for ARM64 only with Baker read barriers, as the |
| 165 | // expected generated code saves and restore X21 and X22 (instead of |
| 166 | // X20 and X21), as X20 is used as Marking Register in the Baker read |
| 167 | // barrier configuration, and as such is removed from the set of |
| 168 | // callee-save registers in the ARM64 code generator of the Optimizing |
| 169 | // compiler. |
| 170 | #if defined(USE_READ_BARRIER) && defined(USE_BAKER_READ_BARRIER) |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 171 | TEST_ISA(kArm64) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 172 | #endif |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 173 | #endif |
| 174 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 175 | #ifdef ART_ENABLE_CODEGEN_x86 |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 176 | TEST_ISA(kX86) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 177 | #endif |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 178 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 179 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 180 | TEST_ISA(kX86_64) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 181 | #endif |
Roland Levillain | af24def | 2017-07-12 13:18:01 +0100 | [diff] [blame] | 182 | |
Scott Wakeling | 90ab673 | 2016-12-08 10:25:03 +0000 | [diff] [blame] | 183 | #ifdef ART_ENABLE_CODEGEN_arm |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 184 | TEST_F(OptimizingCFITest, kThumb2Adjust) { |
Andreas Gampe | bd39d14 | 2018-07-19 11:14:42 -0700 | [diff] [blame] | 185 | using vixl32::r0; |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 186 | std::vector<uint8_t> expected_asm( |
| 187 | expected_asm_kThumb2_adjust, |
| 188 | expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust)); |
| 189 | std::vector<uint8_t> expected_cfi( |
| 190 | expected_cfi_kThumb2_adjust, |
| 191 | expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust)); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 192 | SetUpFrame(InstructionSet::kThumb2); |
Scott Wakeling | 90ab673 | 2016-12-08 10:25:03 +0000 | [diff] [blame] | 193 | #define __ down_cast<arm::ArmVIXLAssembler*>(GetCodeGenerator() \ |
| 194 | ->GetAssembler())->GetVIXLAssembler()-> |
| 195 | vixl32::Label target; |
| 196 | __ CompareAndBranchIfZero(r0, &target); |
| 197 | // Push the target out of range of CBZ. |
| 198 | for (size_t i = 0; i != 65; ++i) { |
| 199 | __ Ldr(r0, vixl32::MemOperand(r0)); |
| 200 | } |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 201 | __ Bind(&target); |
| 202 | #undef __ |
| 203 | Finish(); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 204 | Check(InstructionSet::kThumb2, "kThumb2_adjust", expected_asm, expected_cfi); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 205 | } |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 206 | #endif |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 207 | |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 208 | #endif // ART_TARGET_ANDROID |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 209 | |
| 210 | } // namespace art |