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" |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 26 | #include "utils/arm/assembler_thumb2.h" |
| 27 | #include "utils/mips/assembler_mips.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 28 | |
| 29 | #include "optimizing/optimizing_cfi_test_expected.inc" |
| 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | // Run the tests only on host. |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 34 | #ifndef __ANDROID__ |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 35 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | class OptimizingCFITest : public CFITest { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 37 | public: |
| 38 | // Enable this flag to generate the expected outputs. |
| 39 | static constexpr bool kGenerateExpected = false; |
| 40 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 41 | OptimizingCFITest() |
| 42 | : pool_(), |
| 43 | allocator_(&pool_), |
| 44 | opts_(), |
| 45 | isa_features_(), |
| 46 | graph_(nullptr), |
| 47 | code_gen_(), |
| 48 | blocks_(allocator_.Adapter()) {} |
| 49 | |
| 50 | void SetUpFrame(InstructionSet isa) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 51 | // Setup simple context. |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 52 | std::string error; |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 53 | isa_features_.reset(InstructionSetFeatures::FromVariant(isa, "default", &error)); |
| 54 | graph_ = CreateGraph(&allocator_); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 55 | // Generate simple frame with some spills. |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 56 | code_gen_.reset(CodeGenerator::Create(graph_, isa, *isa_features_, opts_)); |
| 57 | code_gen_->GetAssembler()->cfi().SetEnabled(true); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 58 | const int frame_size = 64; |
| 59 | int core_reg = 0; |
| 60 | int fp_reg = 0; |
| 61 | for (int i = 0; i < 2; i++) { // Two registers of each kind. |
| 62 | for (; core_reg < 32; core_reg++) { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 63 | if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 64 | auto location = Location::RegisterLocation(core_reg); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 65 | code_gen_->AddAllocatedRegister(location); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 66 | core_reg++; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | for (; fp_reg < 32; fp_reg++) { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 71 | if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 72 | auto location = Location::FpuRegisterLocation(fp_reg); |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 73 | code_gen_->AddAllocatedRegister(location); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 74 | fp_reg++; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | } |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 79 | code_gen_->block_order_ = &blocks_; |
| 80 | code_gen_->ComputeSpillMask(); |
| 81 | code_gen_->SetFrameSize(frame_size); |
| 82 | code_gen_->GenerateFrameEntry(); |
| 83 | } |
| 84 | |
| 85 | void Finish() { |
| 86 | code_gen_->GenerateFrameExit(); |
| 87 | code_gen_->Finalize(&code_allocator_); |
| 88 | } |
| 89 | |
| 90 | void Check(InstructionSet isa, |
| 91 | const char* isa_str, |
| 92 | const std::vector<uint8_t>& expected_asm, |
| 93 | const std::vector<uint8_t>& expected_cfi) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 94 | // Get the outputs. |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 95 | const std::vector<uint8_t>& actual_asm = code_allocator_.GetMemory(); |
| 96 | Assembler* opt_asm = code_gen_->GetAssembler(); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 97 | const std::vector<uint8_t>& actual_cfi = *(opt_asm->cfi().data()); |
| 98 | |
| 99 | if (kGenerateExpected) { |
| 100 | GenerateExpected(stdout, isa, isa_str, actual_asm, actual_cfi); |
| 101 | } else { |
| 102 | EXPECT_EQ(expected_asm, actual_asm); |
| 103 | EXPECT_EQ(expected_cfi, actual_cfi); |
| 104 | } |
| 105 | } |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 106 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 107 | void TestImpl(InstructionSet isa, const char* |
| 108 | isa_str, |
| 109 | const std::vector<uint8_t>& expected_asm, |
| 110 | const std::vector<uint8_t>& expected_cfi) { |
| 111 | SetUpFrame(isa); |
| 112 | Finish(); |
| 113 | Check(isa, isa_str, expected_asm, expected_cfi); |
| 114 | } |
| 115 | |
| 116 | CodeGenerator* GetCodeGenerator() { |
| 117 | return code_gen_.get(); |
| 118 | } |
| 119 | |
David Srbecky | 46325a0 | 2015-04-09 22:51:56 +0100 | [diff] [blame] | 120 | private: |
| 121 | class InternalCodeAllocator : public CodeAllocator { |
| 122 | public: |
| 123 | InternalCodeAllocator() {} |
| 124 | |
| 125 | virtual uint8_t* Allocate(size_t size) { |
| 126 | memory_.resize(size); |
| 127 | return memory_.data(); |
| 128 | } |
| 129 | |
| 130 | const std::vector<uint8_t>& GetMemory() { return memory_; } |
| 131 | |
| 132 | private: |
| 133 | std::vector<uint8_t> memory_; |
| 134 | |
| 135 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
| 136 | }; |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 137 | |
| 138 | ArenaPool pool_; |
| 139 | ArenaAllocator allocator_; |
| 140 | CompilerOptions opts_; |
| 141 | std::unique_ptr<const InstructionSetFeatures> isa_features_; |
| 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 | 10ef694 | 2015-10-22 15:25:54 +0100 | [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(isa, #isa, expected_asm, expected_cfi); \ |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST_ISA(kThumb2) |
| 160 | TEST_ISA(kArm64) |
| 161 | TEST_ISA(kX86) |
| 162 | TEST_ISA(kX86_64) |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame^] | 163 | TEST_ISA(kMips) |
| 164 | TEST_ISA(kMips64) |
| 165 | |
| 166 | TEST_F(OptimizingCFITest, kThumb2Adjust) { |
| 167 | std::vector<uint8_t> expected_asm( |
| 168 | expected_asm_kThumb2_adjust, |
| 169 | expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust)); |
| 170 | std::vector<uint8_t> expected_cfi( |
| 171 | expected_cfi_kThumb2_adjust, |
| 172 | expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust)); |
| 173 | SetUpFrame(kThumb2); |
| 174 | #define __ down_cast<arm::Thumb2Assembler*>(GetCodeGenerator()->GetAssembler())-> |
| 175 | Label target; |
| 176 | __ CompareAndBranchIfZero(arm::R0, &target); |
| 177 | // Push the target out of range of CBZ. |
| 178 | for (size_t i = 0; i != 65; ++i) { |
| 179 | __ ldr(arm::R0, arm::Address(arm::R0)); |
| 180 | } |
| 181 | __ Bind(&target); |
| 182 | #undef __ |
| 183 | Finish(); |
| 184 | Check(kThumb2, "kThumb2_adjust", expected_asm, expected_cfi); |
| 185 | } |
| 186 | |
| 187 | TEST_F(OptimizingCFITest, kMipsAdjust) { |
| 188 | // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum. |
| 189 | static constexpr size_t kNumNops = 1u + (1u << 15); |
| 190 | std::vector<uint8_t> expected_asm( |
| 191 | expected_asm_kMips_adjust_head, |
| 192 | expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head)); |
| 193 | expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u); |
| 194 | expected_asm.insert( |
| 195 | expected_asm.end(), |
| 196 | expected_asm_kMips_adjust_tail, |
| 197 | expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail)); |
| 198 | std::vector<uint8_t> expected_cfi( |
| 199 | expected_cfi_kMips_adjust, |
| 200 | expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust)); |
| 201 | SetUpFrame(kMips); |
| 202 | #define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())-> |
| 203 | mips::MipsLabel target; |
| 204 | __ Beqz(mips::A0, &target); |
| 205 | // Push the target out of range of BEQZ. |
| 206 | for (size_t i = 0; i != kNumNops; ++i) { |
| 207 | __ Nop(); |
| 208 | } |
| 209 | __ Bind(&target); |
| 210 | #undef __ |
| 211 | Finish(); |
| 212 | Check(kMips, "kMips_adjust", expected_asm, expected_cfi); |
| 213 | } |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 214 | |
Andreas Gampe | c60e1b7 | 2015-07-30 08:57:50 -0700 | [diff] [blame] | 215 | #endif // __ANDROID__ |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 216 | |
| 217 | } // namespace art |