blob: 34f1fe5949f0718f4d735346ebf5c3af12f57fd7 [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"
22#include "gtest/gtest.h"
23#include "optimizing/code_generator.h"
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010024#include "optimizing/optimizing_unit_test.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010025#include "utils/assembler.h"
Vladimir Marko10ef6942015-10-22 15:25:54 +010026#include "utils/arm/assembler_thumb2.h"
27#include "utils/mips/assembler_mips.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010028
29#include "optimizing/optimizing_cfi_test_expected.inc"
30
31namespace art {
32
33// Run the tests only on host.
Andreas Gampec60e1b72015-07-30 08:57:50 -070034#ifndef __ANDROID__
David Srbeckyc6b4dd82015-04-07 20:32:43 +010035
Mathieu Chartiere401d142015-04-22 13:56:20 -070036class OptimizingCFITest : public CFITest {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010037 public:
38 // Enable this flag to generate the expected outputs.
39 static constexpr bool kGenerateExpected = false;
40
Vladimir Marko10ef6942015-10-22 15:25:54 +010041 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 Srbeckyc6b4dd82015-04-07 20:32:43 +010051 // Setup simple context.
David Srbeckyc6b4dd82015-04-07 20:32:43 +010052 std::string error;
Vladimir Marko10ef6942015-10-22 15:25:54 +010053 isa_features_.reset(InstructionSetFeatures::FromVariant(isa, "default", &error));
54 graph_ = CreateGraph(&allocator_);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010055 // Generate simple frame with some spills.
Vladimir Marko10ef6942015-10-22 15:25:54 +010056 code_gen_.reset(CodeGenerator::Create(graph_, isa, *isa_features_, opts_));
57 code_gen_->GetAssembler()->cfi().SetEnabled(true);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010058 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 Marko10ef6942015-10-22 15:25:54 +010063 if (code_gen_->IsCoreCalleeSaveRegister(core_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010064 auto location = Location::RegisterLocation(core_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010065 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010066 core_reg++;
67 break;
68 }
69 }
70 for (; fp_reg < 32; fp_reg++) {
Vladimir Marko10ef6942015-10-22 15:25:54 +010071 if (code_gen_->IsFloatingPointCalleeSaveRegister(fp_reg)) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +010072 auto location = Location::FpuRegisterLocation(fp_reg);
Vladimir Marko10ef6942015-10-22 15:25:54 +010073 code_gen_->AddAllocatedRegister(location);
David Srbeckyc6b4dd82015-04-07 20:32:43 +010074 fp_reg++;
75 break;
76 }
77 }
78 }
Vladimir Marko10ef6942015-10-22 15:25:54 +010079 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 Srbeckyc6b4dd82015-04-07 20:32:43 +010094 // Get the outputs.
Vladimir Marko10ef6942015-10-22 15:25:54 +010095 const std::vector<uint8_t>& actual_asm = code_allocator_.GetMemory();
96 Assembler* opt_asm = code_gen_->GetAssembler();
David Srbeckyc6b4dd82015-04-07 20:32:43 +010097 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 Srbecky46325a02015-04-09 22:51:56 +0100106
Vladimir Marko10ef6942015-10-22 15:25:54 +0100107 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 Srbecky46325a02015-04-09 22:51:56 +0100120 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 Marko10ef6942015-10-22 15:25:54 +0100137
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 Srbeckyc6b4dd82015-04-07 20:32:43 +0100146};
147
Vladimir Marko10ef6942015-10-22 15:25:54 +0100148#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 Srbeckyc6b4dd82015-04-07 20:32:43 +0100157 }
158
159TEST_ISA(kThumb2)
160TEST_ISA(kArm64)
161TEST_ISA(kX86)
162TEST_ISA(kX86_64)
Vladimir Marko10ef6942015-10-22 15:25:54 +0100163TEST_ISA(kMips)
164TEST_ISA(kMips64)
165
166TEST_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
187TEST_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 Srbeckyc6b4dd82015-04-07 20:32:43 +0100214
Andreas Gampec60e1b72015-07-30 08:57:50 -0700215#endif // __ANDROID__
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100216
217} // namespace art