blob: a1e923bd73038acb00fd73325651492e2ce32ea3 [file] [log] [blame]
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001/*
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 Marko3a21e382016-09-02 12:38:38 +010022#include "driver/compiler_options.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010023#include "gtest/gtest.h"
24#include "optimizing/code_generator.h"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010025#include "optimizing/optimizing_unit_test.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010026#include "utils/assembler.h"
Vladimir Marko10ef6942015-10-22 15:25:54 +010027#include "utils/arm/assembler_thumb2.h"
28#include "utils/mips/assembler_mips.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070029#include "utils/mips64/assembler_mips64.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010030
31#include "optimizing/optimizing_cfi_test_expected.inc"
32
33namespace art {
34
35// Run the tests only on host.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010036#ifndef ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +010037
Mathieu Chartiere401d142015-04-22 13:56:20 -070038class OptimizingCFITest : public CFITest {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010039 public:
40 // Enable this flag to generate the expected outputs.
41 static constexpr bool kGenerateExpected = false;
42
Vladimir Marko10ef6942015-10-22 15:25:54 +010043 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 Srbeckyc6b4dd82015-04-07 20:32:43 +010053 // Setup simple context.
David Srbeckyc6b4dd82015-04-07 20:32:43 +010054 std::string error;
Vladimir Marko10ef6942015-10-22 15:25:54 +010055 isa_features_.reset(InstructionSetFeatures::FromVariant(isa, "default", &error));
56 graph_ = CreateGraph(&allocator_);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010057 // Generate simple frame with some spills.
Vladimir Markod58b8372016-04-12 18:51:43 +010058 code_gen_ = CodeGenerator::Create(graph_, isa, *isa_features_, opts_);
Vladimir Marko10ef6942015-10-22 15:25:54 +010059 code_gen_->GetAssembler()->cfi().SetEnabled(true);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010060 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 Marko10ef6942015-10-22 15:25:54 +010065 if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010066 auto location = Location::RegisterLocation(core_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010067 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010068 core_reg++;
69 break;
70 }
71 }
72 for (; fp_reg < 32; fp_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010073 if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010074 auto location = Location::FpuRegisterLocation(fp_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010075 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010076 fp_reg++;
77 break;
78 }
79 }
80 }
Vladimir Marko10ef6942015-10-22 15:25:54 +010081 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 Srbeckyc6b4dd82015-04-07 20:32:43 +010096 // Get the outputs.
Vladimir Marko10ef6942015-10-22 15:25:54 +010097 const std::vector<uint8_t>& actual_asm = code_allocator_.GetMemory();
98 Assembler* opt_asm = code_gen_->GetAssembler();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010099 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 Srbecky46325a02015-04-09 22:51:56 +0100108
Vladimir Marko10ef6942015-10-22 15:25:54 +0100109 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 Srbecky46325a02015-04-09 22:51:56 +0100122 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 Marko10ef6942015-10-22 15:25:54 +0100139
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 Srbeckyc6b4dd82015-04-07 20:32:43 +0100148};
149
Vladimir Marko10ef6942015-10-22 15:25:54 +0100150#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 Srbeckyc6b4dd82015-04-07 20:32:43 +0100159 }
160
Colin Crossa75b01a2016-08-18 13:45:24 -0700161#ifdef ART_ENABLE_CODEGEN_arm
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100162TEST_ISA(kThumb2)
Colin Crossa75b01a2016-08-18 13:45:24 -0700163#endif
164#ifdef ART_ENABLE_CODEGEN_arm64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100165TEST_ISA(kArm64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700166#endif
167#ifdef ART_ENABLE_CODEGEN_x86
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100168TEST_ISA(kX86)
Colin Crossa75b01a2016-08-18 13:45:24 -0700169#endif
170#ifdef ART_ENABLE_CODEGEN_x86_64
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100171TEST_ISA(kX86_64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700172#endif
173#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100174TEST_ISA(kMips)
Colin Crossa75b01a2016-08-18 13:45:24 -0700175#endif
176#ifdef ART_ENABLE_CODEGEN_mips64
Vladimir Marko10ef6942015-10-22 15:25:54 +0100177TEST_ISA(kMips64)
Colin Crossa75b01a2016-08-18 13:45:24 -0700178#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100179
Colin Crossa75b01a2016-08-18 13:45:24 -0700180#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Marko10ef6942015-10-22 15:25:54 +0100181TEST_F(OptimizingCFITest, kThumb2Adjust) {
182 std::vector<uint8_t> expected_asm(
183 expected_asm_kThumb2_adjust,
184 expected_asm_kThumb2_adjust + arraysize(expected_asm_kThumb2_adjust));
185 std::vector<uint8_t> expected_cfi(
186 expected_cfi_kThumb2_adjust,
187 expected_cfi_kThumb2_adjust + arraysize(expected_cfi_kThumb2_adjust));
188 SetUpFrame(kThumb2);
189#define __ down_cast<arm::Thumb2Assembler*>(GetCodeGenerator()->GetAssembler())->
190 Label target;
191 __ CompareAndBranchIfZero(arm::R0, &target);
192 // Push the target out of range of CBZ.
193 for (size_t i = 0; i != 65; ++i) {
194 __ ldr(arm::R0, arm::Address(arm::R0));
195 }
196 __ Bind(&target);
197#undef __
198 Finish();
199 Check(kThumb2, "kThumb2_adjust", expected_asm, expected_cfi);
200}
Colin Crossa75b01a2016-08-18 13:45:24 -0700201#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +0100202
Colin Crossa75b01a2016-08-18 13:45:24 -0700203#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko10ef6942015-10-22 15:25:54 +0100204TEST_F(OptimizingCFITest, kMipsAdjust) {
205 // One NOP in delay slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
206 static constexpr size_t kNumNops = 1u + (1u << 15);
207 std::vector<uint8_t> expected_asm(
208 expected_asm_kMips_adjust_head,
209 expected_asm_kMips_adjust_head + arraysize(expected_asm_kMips_adjust_head));
210 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
211 expected_asm.insert(
212 expected_asm.end(),
213 expected_asm_kMips_adjust_tail,
214 expected_asm_kMips_adjust_tail + arraysize(expected_asm_kMips_adjust_tail));
215 std::vector<uint8_t> expected_cfi(
216 expected_cfi_kMips_adjust,
217 expected_cfi_kMips_adjust + arraysize(expected_cfi_kMips_adjust));
218 SetUpFrame(kMips);
219#define __ down_cast<mips::MipsAssembler*>(GetCodeGenerator()->GetAssembler())->
220 mips::MipsLabel target;
221 __ Beqz(mips::A0, &target);
222 // Push the target out of range of BEQZ.
223 for (size_t i = 0; i != kNumNops; ++i) {
224 __ Nop();
225 }
226 __ Bind(&target);
227#undef __
228 Finish();
229 Check(kMips, "kMips_adjust", expected_asm, expected_cfi);
230}
Colin Crossa75b01a2016-08-18 13:45:24 -0700231#endif
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100232
Colin Crossa75b01a2016-08-18 13:45:24 -0700233#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700234TEST_F(OptimizingCFITest, kMips64Adjust) {
235 // One NOP in forbidden slot, 1 << 15 NOPS have size 1 << 17 which exceeds 18-bit signed maximum.
236 static constexpr size_t kNumNops = 1u + (1u << 15);
237 std::vector<uint8_t> expected_asm(
238 expected_asm_kMips64_adjust_head,
239 expected_asm_kMips64_adjust_head + arraysize(expected_asm_kMips64_adjust_head));
240 expected_asm.resize(expected_asm.size() + kNumNops * 4u, 0u);
241 expected_asm.insert(
242 expected_asm.end(),
243 expected_asm_kMips64_adjust_tail,
244 expected_asm_kMips64_adjust_tail + arraysize(expected_asm_kMips64_adjust_tail));
245 std::vector<uint8_t> expected_cfi(
246 expected_cfi_kMips64_adjust,
247 expected_cfi_kMips64_adjust + arraysize(expected_cfi_kMips64_adjust));
248 SetUpFrame(kMips64);
249#define __ down_cast<mips64::Mips64Assembler*>(GetCodeGenerator()->GetAssembler())->
250 mips64::Mips64Label target;
251 __ Beqc(mips64::A1, mips64::A2, &target);
252 // Push the target out of range of BEQC.
253 for (size_t i = 0; i != kNumNops; ++i) {
254 __ Nop();
255 }
256 __ Bind(&target);
257#undef __
258 Finish();
259 Check(kMips64, "kMips64_adjust", expected_asm, expected_cfi);
260}
Colin Crossa75b01a2016-08-18 13:45:24 -0700261#endif
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700262
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100263#endif // ART_TARGET_ANDROID
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100264
265} // namespace art