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" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 22 | #include "driver/compiler_options.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 23 | #include "gtest/gtest.h" |
| 24 | #include "optimizing/code_generator.h" |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 25 | #include "optimizing/optimizing_unit_test.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 26 | #include "utils/assembler.h" |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 27 | #include "utils/arm/assembler_thumb2.h" |
| 28 | #include "utils/mips/assembler_mips.h" |
Alexey Frunze | a0e87b0 | 2015-09-24 22:57:20 -0700 | [diff] [blame] | 29 | #include "utils/mips64/assembler_mips64.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 30 | |
| 31 | #include "optimizing/optimizing_cfi_test_expected.inc" |
| 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | // Run the tests only on host. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 36 | #ifndef ART_TARGET_ANDROID |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 37 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 38 | class OptimizingCFITest : public CFITest { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 39 | public: |
| 40 | // Enable this flag to generate the expected outputs. |
| 41 | static constexpr bool kGenerateExpected = false; |
| 42 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 43 | OptimizingCFITest() |
| 44 | : pool_(), |
| 45 | allocator_(&pool_), |
| 46 | opts_(), |
| 47 | isa_features_(), |
| 48 | graph_(nullptr), |
| 49 | code_gen_(), |
| 50 | blocks_(allocator_.Adapter()) {} |
| 51 | |
| 52 | void SetUpFrame(InstructionSet isa) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 53 | // Setup simple context. |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 54 | std::string error; |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 55 | isa_features_.reset(InstructionSetFeatures::FromVariant(isa, "default", &error)); |
| 56 | graph_ = CreateGraph(&allocator_); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 57 | // Generate simple frame with some spills. |
Vladimir Marko | d58b837 | 2016-04-12 18:51:43 +0100 | [diff] [blame] | 58 | code_gen_ = CodeGenerator::Create(graph_, isa, *isa_features_, opts_); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 59 | code_gen_->GetAssembler()->cfi().SetEnabled(true); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 60 | const int frame_size = 64; |
| 61 | int core_reg = 0; |
| 62 | int fp_reg = 0; |
| 63 | for (int i = 0; i < 2; i++) { // Two registers of each kind. |
| 64 | for (; core_reg < 32; core_reg++) { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 65 | if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 66 | auto location = Location::RegisterLocation(core_reg); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 67 | code_gen_->AddAllocatedRegister(location); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 68 | core_reg++; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | for (; fp_reg < 32; fp_reg++) { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 73 | if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 74 | auto location = Location::FpuRegisterLocation(fp_reg); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 75 | code_gen_->AddAllocatedRegister(location); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 76 | fp_reg++; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 81 | code_gen_->block_order_ = &blocks_; |
| 82 | code_gen_->ComputeSpillMask(); |
| 83 | code_gen_->SetFrameSize(frame_size); |
| 84 | code_gen_->GenerateFrameEntry(); |
| 85 | } |
| 86 | |
| 87 | void Finish() { |
| 88 | code_gen_->GenerateFrameExit(); |
| 89 | code_gen_->Finalize(&code_allocator_); |
| 90 | } |
| 91 | |
| 92 | void Check(InstructionSet isa, |
| 93 | const char* isa_str, |
| 94 | const std::vector<uint8_t>& expected_asm, |
| 95 | const std::vector<uint8_t>& expected_cfi) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 96 | // Get the outputs. |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 97 | const std::vector<uint8_t>& actual_asm = code_allocator_.GetMemory(); |
| 98 | Assembler* opt_asm = code_gen_->GetAssembler(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 99 | const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data()); |
| 100 | |
| 101 | if (kGenerateExpected) { |
| 102 | GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi); |
| 103 | } else { |
| 104 | EXPECT_EQ(expected_asm, actual_asm); |
| 105 | EXPECT_EQ(expected_cfi, actual_cfi); |
| 106 | } |
| 107 | } |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 108 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 109 | void TestImpl(InstructionSet isa, const char* |
| 110 | isa_str, |
| 111 | const std::vector<uint8_t>& expected_asm, |
| 112 | const std::vector<uint8_t>& expected_cfi) { |
| 113 | SetUpFrame(isa); |
| 114 | Finish(); |
| 115 | Check(isa, isa_str, expected_asm, expected_cfi); |
| 116 | } |
| 117 | |
| 118 | CodeGenerator* GetCodeGenerator() { |
| 119 | return code_gen_.get(); |
| 120 | } |
| 121 | |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 122 | private: |
| 123 | class InternalCodeAllocator : public CodeAllocator { |
| 124 | public: |
| 125 | InternalCodeAllocator() {} |
| 126 | |
| 127 | virtual uint8_t* Allocate(size_t size) { |
| 128 | memory_.resize(size); |
| 129 | return memory_.data(); |
| 130 | } |
| 131 | |
| 132 | const std::vector<uint8_t>& GetMemory() { return memory_; } |
| 133 | |
| 134 | private: |
| 135 | std::vector<uint8_t> memory_; |
| 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
| 138 | }; |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 139 | |
| 140 | ArenaPool pool_; |
| 141 | ArenaAllocator allocator_; |
| 142 | CompilerOptions opts_; |
| 143 | std::unique_ptr<const InstructionSetFeatures> isa_features_; |
| 144 | HGraph* graph_; |
| 145 | std::unique_ptr<CodeGenerator> code_gen_; |
| 146 | ArenaVector<HBasicBlock*> blocks_; |
| 147 | InternalCodeAllocator code_allocator_; |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 150 | #define TEST_ISA(isa) \ |
| 151 | TEST_F(OptimizingCFITest, isa) { \ |
| 152 | std::vector<uint8_t> expected_asm( \ |
| 153 | expected_asm_##isa, \ |
| 154 | expected_asm_##isa + arraysize(expected_asm_##isa)); \ |
| 155 | std::vector<uint8_t> expected_cfi( \ |
| 156 | expected_cfi_##isa, \ |
| 157 | expected_cfi_##isa + arraysize(expected_cfi_##isa)); \ |
| 158 | TestImpl(isa, #isa, expected_asm, expected_cfi); \ |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Scott Wakeling | a7812ae | 2016-10-17 10:03:36 +0100 | [diff] [blame] | 161 | // TODO(VIXL): Support this test for the VIXL backend. |
| 162 | #if defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_USE_VIXL_ARM_BACKEND) |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 163 | TEST_ISA(kThumb2) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 164 | #endif |
| 165 | #ifdef ART_ENABLE_CODEGEN_arm64 |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 166 | TEST_ISA(kArm64) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 167 | #endif |
| 168 | #ifdef ART_ENABLE_CODEGEN_x86 |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 169 | TEST_ISA(kX86) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 170 | #endif |
| 171 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 172 | TEST_ISA(kX86_64) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 173 | #endif |
| 174 | #ifdef ART_ENABLE_CODEGEN_mips |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 175 | TEST_ISA(kMips) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 176 | #endif |
| 177 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 178 | TEST_ISA(kMips64) |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 179 | #endif |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 180 | |
Scott Wakeling | a7812ae | 2016-10-17 10:03:36 +0100 | [diff] [blame] | 181 | // TODO(VIXL): Support this test for the VIXL backend. |
| 182 | #if defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_USE_VIXL_ARM_BACKEND) |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 183 | TEST_F(OptimizingCFITest, kThumb2Adjust) { |
| 184 | std::vector<uint8_t> expected_asm( |
| 185 | expected_asm_kThumb2_adjust, |
| 186 | expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust)); |
| 187 | std::vector<uint8_t> expected_cfi( |
| 188 | expected_cfi_kThumb2_adjust, |
| 189 | expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust)); |
| 190 | SetUpFrame(kThumb2); |
| 191 | #define __ down_cast<arm::Thumb2Assembler*>(GetCodeGenerator()->GetAssembler())-> |
| 192 | Label target; |
| 193 | __ CompareAndBranchIfZero(arm::R0, &target); |
| 194 | // Push the target out of range of CBZ. |
| 195 | for (size_t i = 0; i != 65; ++i) { |
| 196 | __ ldr(arm::R0, arm::Address(arm::R0)); |
| 197 | } |
| 198 | __ Bind(&target); |
| 199 | #undef __ |
| 200 | Finish(); |
| 201 | Check(kThumb2, "kThumb2_adjust", expected_asm, expected_cfi); |
| 202 | } |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 203 | #endif |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 204 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 205 | #ifdef ART_ENABLE_CODEGEN_mips |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 206 | TEST_F(OptimizingCFITest, kMipsAdjust) { |
| 207 | // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum. |
| 208 | static constexpr size_t kNumNops = 1u + (1u << 15); |
| 209 | std::vector<uint8_t> expected_asm( |
| 210 | expected_asm_kMips_adjust_head, |
| 211 | expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head)); |
| 212 | expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u); |
| 213 | expected_asm.insert( |
| 214 | expected_asm.end(), |
| 215 | expected_asm_kMips_adjust_tail, |
| 216 | expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail)); |
| 217 | std::vector<uint8_t> expected_cfi( |
| 218 | expected_cfi_kMips_adjust, |
| 219 | expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust)); |
| 220 | SetUpFrame(kMips); |
| 221 | #define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())-> |
| 222 | mips::MipsLabel target; |
| 223 | __ Beqz(mips::A0, &target); |
| 224 | // Push the target out of range of BEQZ. |
| 225 | for (size_t i = 0; i != kNumNops; ++i) { |
| 226 | __ Nop(); |
| 227 | } |
| 228 | __ Bind(&target); |
| 229 | #undef __ |
| 230 | Finish(); |
| 231 | Check(kMips, "kMips_adjust", expected_asm, expected_cfi); |
| 232 | } |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 233 | #endif |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 234 | |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 235 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Alexey Frunze | a0e87b0 | 2015-09-24 22:57:20 -0700 | [diff] [blame] | 236 | TEST_F(OptimizingCFITest, kMips64Adjust) { |
| 237 | // One NOP in forbidden slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum. |
| 238 | static constexpr size_t kNumNops = 1u + (1u << 15); |
| 239 | std::vector<uint8_t> expected_asm( |
| 240 | expected_asm_kMips64_adjust_head, |
| 241 | expected_asm_kMips64_adjust_head + arraysize(expected_asm_kMips64_adjust_head)); |
| 242 | expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u); |
| 243 | expected_asm.insert( |
| 244 | expected_asm.end(), |
| 245 | expected_asm_kMips64_adjust_tail, |
| 246 | expected_asm_kMips64_adjust_tail + arraysize(expected_asm_kMips64_adjust_tail)); |
| 247 | std::vector<uint8_t> expected_cfi( |
| 248 | expected_cfi_kMips64_adjust, |
| 249 | expected_cfi_kMips64_adjust + arraysize(expected_cfi_kMips64_adjust)); |
| 250 | SetUpFrame(kMips64); |
| 251 | #define __ down_cast<mips64::Mips64Assembler*>(GetCodeGenerator()->GetAssembler())-> |
| 252 | mips64::Mips64Label target; |
| 253 | __ Beqc(mips64::A1, mips64::A2, &target); |
| 254 | // Push the target out of range of BEQC. |
| 255 | for (size_t i = 0; i != kNumNops; ++i) { |
| 256 | __ Nop(); |
| 257 | } |
| 258 | __ Bind(&target); |
| 259 | #undef __ |
| 260 | Finish(); |
| 261 | Check(kMips64, "kMips64_adjust", expected_asm, expected_cfi); |
| 262 | } |
Colin Cross | a75b01a | 2016-08-18 13:45:24 -0700 | [diff] [blame] | 263 | #endif |
Alexey Frunze | a0e87b0 | 2015-09-24 22:57:20 -0700 | [diff] [blame] | 264 | |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 265 | #endif // ART_TARGET_ANDROID |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 266 | |
| 267 | } // namespace art |