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