Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/base/utils/random-number-generator.h" |
| 6 | #include "src/compiler/pipeline.h" |
| 7 | #include "test/unittests/compiler/instruction-sequence-unittest.h" |
| 8 | #include "test/unittests/test-utils.h" |
| 9 | #include "testing/gmock/include/gmock/gmock.h" |
| 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | namespace compiler { |
| 14 | |
| 15 | static const char* |
| 16 | general_register_names_[RegisterConfiguration::kMaxGeneralRegisters]; |
| 17 | static const char* |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 18 | double_register_names_[RegisterConfiguration::kMaxFPRegisters]; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 19 | static char register_names_[10 * (RegisterConfiguration::kMaxGeneralRegisters + |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 20 | RegisterConfiguration::kMaxFPRegisters)]; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 21 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | static int allocatable_codes[InstructionSequenceTest::kDefaultNRegs] = { |
| 24 | 0, 1, 2, 3, 4, 5, 6, 7}; |
| 25 | static int allocatable_double_codes[InstructionSequenceTest::kDefaultNRegs] = { |
| 26 | 0, 1, 2, 3, 4, 5, 6, 7}; |
| 27 | } |
| 28 | |
| 29 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 30 | static void InitializeRegisterNames() { |
| 31 | char* loc = register_names_; |
| 32 | for (int i = 0; i < RegisterConfiguration::kMaxGeneralRegisters; ++i) { |
| 33 | general_register_names_[i] = loc; |
| 34 | loc += base::OS::SNPrintF(loc, 100, "gp_%d", i); |
| 35 | *loc++ = 0; |
| 36 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 37 | for (int i = 0; i < RegisterConfiguration::kMaxFPRegisters; ++i) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 38 | double_register_names_[i] = loc; |
| 39 | loc += base::OS::SNPrintF(loc, 100, "fp_%d", i) + 1; |
| 40 | *loc++ = 0; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | InstructionSequenceTest::InstructionSequenceTest() |
| 46 | : sequence_(nullptr), |
| 47 | num_general_registers_(kDefaultNRegs), |
| 48 | num_double_registers_(kDefaultNRegs), |
| 49 | instruction_blocks_(zone()), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 50 | current_block_(nullptr), |
| 51 | block_returns_(false) { |
| 52 | InitializeRegisterNames(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void InstructionSequenceTest::SetNumRegs(int num_general_registers, |
| 57 | int num_double_registers) { |
| 58 | CHECK(config_.is_empty()); |
| 59 | CHECK(instructions_.empty()); |
| 60 | CHECK(instruction_blocks_.empty()); |
| 61 | num_general_registers_ = num_general_registers; |
| 62 | num_double_registers_ = num_double_registers; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | RegisterConfiguration* InstructionSequenceTest::config() { |
| 67 | if (config_.is_empty()) { |
| 68 | config_.Reset(new RegisterConfiguration( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | num_general_registers_, num_double_registers_, num_general_registers_, |
| 70 | num_double_registers_, num_double_registers_, allocatable_codes, |
| 71 | allocatable_double_codes, general_register_names_, |
| 72 | double_register_names_)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 73 | } |
| 74 | return config_.get(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | InstructionSequence* InstructionSequenceTest::sequence() { |
| 79 | if (sequence_ == nullptr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 80 | sequence_ = new (zone()) |
| 81 | InstructionSequence(isolate(), zone(), &instruction_blocks_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 82 | } |
| 83 | return sequence_; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void InstructionSequenceTest::StartLoop(int loop_blocks) { |
| 88 | CHECK(current_block_ == nullptr); |
| 89 | if (!loop_blocks_.empty()) { |
| 90 | CHECK(!loop_blocks_.back().loop_header_.IsValid()); |
| 91 | } |
| 92 | LoopData loop_data = {Rpo::Invalid(), loop_blocks}; |
| 93 | loop_blocks_.push_back(loop_data); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void InstructionSequenceTest::EndLoop() { |
| 98 | CHECK(current_block_ == nullptr); |
| 99 | CHECK(!loop_blocks_.empty()); |
| 100 | CHECK_EQ(0, loop_blocks_.back().expected_blocks_); |
| 101 | loop_blocks_.pop_back(); |
| 102 | } |
| 103 | |
| 104 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | void InstructionSequenceTest::StartBlock(bool deferred) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 106 | block_returns_ = false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 107 | NewBlock(deferred); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 111 | Instruction* InstructionSequenceTest::EndBlock(BlockCompletion completion) { |
| 112 | Instruction* result = nullptr; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 113 | if (block_returns_) { |
| 114 | CHECK(completion.type_ == kBlockEnd || completion.type_ == kFallThrough); |
| 115 | completion.type_ = kBlockEnd; |
| 116 | } |
| 117 | switch (completion.type_) { |
| 118 | case kBlockEnd: |
| 119 | break; |
| 120 | case kFallThrough: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | result = EmitJump(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 122 | break; |
| 123 | case kJump: |
| 124 | CHECK(!block_returns_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | result = EmitJump(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 126 | break; |
| 127 | case kBranch: |
| 128 | CHECK(!block_returns_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 129 | result = EmitBranch(completion.op_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 130 | break; |
| 131 | } |
| 132 | completions_.push_back(completion); |
| 133 | CHECK(current_block_ != nullptr); |
| 134 | sequence()->EndBlock(current_block_->rpo_number()); |
| 135 | current_block_ = nullptr; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 136 | return result; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | InstructionSequenceTest::TestOperand InstructionSequenceTest::Imm(int32_t imm) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | return TestOperand(kImmediate, imm); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | InstructionSequenceTest::VReg InstructionSequenceTest::Define( |
| 146 | TestOperand output_op) { |
| 147 | VReg vreg = NewReg(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 148 | InstructionOperand outputs[1]{ConvertOutputOp(vreg, output_op)}; |
| 149 | Emit(kArchNop, 1, outputs); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 150 | return vreg; |
| 151 | } |
| 152 | |
| 153 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 154 | Instruction* InstructionSequenceTest::Return(TestOperand input_op_0) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 155 | block_returns_ = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 156 | InstructionOperand inputs[1]{ConvertInputOp(input_op_0)}; |
| 157 | return Emit(kArchRet, 0, nullptr, 1, inputs); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | PhiInstruction* InstructionSequenceTest::Phi(VReg incoming_vreg_0, |
| 162 | VReg incoming_vreg_1, |
| 163 | VReg incoming_vreg_2, |
| 164 | VReg incoming_vreg_3) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 165 | VReg inputs[] = {incoming_vreg_0, incoming_vreg_1, incoming_vreg_2, |
| 166 | incoming_vreg_3}; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 167 | size_t input_count = 0; |
| 168 | for (; input_count < arraysize(inputs); ++input_count) { |
| 169 | if (inputs[input_count].value_ == kNoValue) break; |
| 170 | } |
| 171 | CHECK(input_count > 0); |
| 172 | auto phi = new (zone()) PhiInstruction(zone(), NewReg().value_, input_count); |
| 173 | for (size_t i = 0; i < input_count; ++i) { |
| 174 | SetInput(phi, i, inputs[i]); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 175 | } |
| 176 | current_block_->AddPhi(phi); |
| 177 | return phi; |
| 178 | } |
| 179 | |
| 180 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | PhiInstruction* InstructionSequenceTest::Phi(VReg incoming_vreg_0, |
| 182 | size_t input_count) { |
| 183 | auto phi = new (zone()) PhiInstruction(zone(), NewReg().value_, input_count); |
| 184 | SetInput(phi, 0, incoming_vreg_0); |
| 185 | current_block_->AddPhi(phi); |
| 186 | return phi; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | void InstructionSequenceTest::SetInput(PhiInstruction* phi, size_t input, |
| 191 | VReg vreg) { |
| 192 | CHECK(vreg.value_ != kNoValue); |
| 193 | phi->SetInput(input, vreg.value_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
| 197 | InstructionSequenceTest::VReg InstructionSequenceTest::DefineConstant( |
| 198 | int32_t imm) { |
| 199 | VReg vreg = NewReg(); |
| 200 | sequence()->AddConstant(vreg.value_, Constant(imm)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 201 | InstructionOperand outputs[1]{ConstantOperand(vreg.value_)}; |
| 202 | Emit(kArchNop, 1, outputs); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 203 | return vreg; |
| 204 | } |
| 205 | |
| 206 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 207 | Instruction* InstructionSequenceTest::EmitNop() { return Emit(kArchNop); } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 208 | |
| 209 | |
| 210 | static size_t CountInputs(size_t size, |
| 211 | InstructionSequenceTest::TestOperand* inputs) { |
| 212 | size_t i = 0; |
| 213 | for (; i < size; ++i) { |
| 214 | if (inputs[i].type_ == InstructionSequenceTest::kInvalid) break; |
| 215 | } |
| 216 | return i; |
| 217 | } |
| 218 | |
| 219 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 220 | Instruction* InstructionSequenceTest::EmitI(size_t input_size, |
| 221 | TestOperand* inputs) { |
| 222 | InstructionOperand* mapped_inputs = ConvertInputs(input_size, inputs); |
| 223 | return Emit(kArchNop, 0, nullptr, input_size, mapped_inputs); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 227 | Instruction* InstructionSequenceTest::EmitI(TestOperand input_op_0, |
| 228 | TestOperand input_op_1, |
| 229 | TestOperand input_op_2, |
| 230 | TestOperand input_op_3) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 231 | TestOperand inputs[] = {input_op_0, input_op_1, input_op_2, input_op_3}; |
| 232 | return EmitI(CountInputs(arraysize(inputs), inputs), inputs); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | InstructionSequenceTest::VReg InstructionSequenceTest::EmitOI( |
| 237 | TestOperand output_op, size_t input_size, TestOperand* inputs) { |
| 238 | VReg output_vreg = NewReg(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 239 | InstructionOperand outputs[1]{ConvertOutputOp(output_vreg, output_op)}; |
| 240 | InstructionOperand* mapped_inputs = ConvertInputs(input_size, inputs); |
| 241 | Emit(kArchNop, 1, outputs, input_size, mapped_inputs); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 242 | return output_vreg; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | InstructionSequenceTest::VReg InstructionSequenceTest::EmitOI( |
| 247 | TestOperand output_op, TestOperand input_op_0, TestOperand input_op_1, |
| 248 | TestOperand input_op_2, TestOperand input_op_3) { |
| 249 | TestOperand inputs[] = {input_op_0, input_op_1, input_op_2, input_op_3}; |
| 250 | return EmitOI(output_op, CountInputs(arraysize(inputs), inputs), inputs); |
| 251 | } |
| 252 | |
| 253 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 254 | InstructionSequenceTest::VRegPair InstructionSequenceTest::EmitOOI( |
| 255 | TestOperand output_op_0, TestOperand output_op_1, size_t input_size, |
| 256 | TestOperand* inputs) { |
| 257 | VRegPair output_vregs = std::make_pair(NewReg(), NewReg()); |
| 258 | InstructionOperand outputs[2]{ |
| 259 | ConvertOutputOp(output_vregs.first, output_op_0), |
| 260 | ConvertOutputOp(output_vregs.second, output_op_1)}; |
| 261 | InstructionOperand* mapped_inputs = ConvertInputs(input_size, inputs); |
| 262 | Emit(kArchNop, 2, outputs, input_size, mapped_inputs); |
| 263 | return output_vregs; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | InstructionSequenceTest::VRegPair InstructionSequenceTest::EmitOOI( |
| 268 | TestOperand output_op_0, TestOperand output_op_1, TestOperand input_op_0, |
| 269 | TestOperand input_op_1, TestOperand input_op_2, TestOperand input_op_3) { |
| 270 | TestOperand inputs[] = {input_op_0, input_op_1, input_op_2, input_op_3}; |
| 271 | return EmitOOI(output_op_0, output_op_1, |
| 272 | CountInputs(arraysize(inputs), inputs), inputs); |
| 273 | } |
| 274 | |
| 275 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 276 | InstructionSequenceTest::VReg InstructionSequenceTest::EmitCall( |
| 277 | TestOperand output_op, size_t input_size, TestOperand* inputs) { |
| 278 | VReg output_vreg = NewReg(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 279 | InstructionOperand outputs[1]{ConvertOutputOp(output_vreg, output_op)}; |
| 280 | CHECK(UnallocatedOperand::cast(outputs[0]).HasFixedPolicy()); |
| 281 | InstructionOperand* mapped_inputs = ConvertInputs(input_size, inputs); |
| 282 | Emit(kArchCallCodeObject, 1, outputs, input_size, mapped_inputs, 0, nullptr, |
| 283 | true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 284 | return output_vreg; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | InstructionSequenceTest::VReg InstructionSequenceTest::EmitCall( |
| 289 | TestOperand output_op, TestOperand input_op_0, TestOperand input_op_1, |
| 290 | TestOperand input_op_2, TestOperand input_op_3) { |
| 291 | TestOperand inputs[] = {input_op_0, input_op_1, input_op_2, input_op_3}; |
| 292 | return EmitCall(output_op, CountInputs(arraysize(inputs), inputs), inputs); |
| 293 | } |
| 294 | |
| 295 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 296 | Instruction* InstructionSequenceTest::EmitBranch(TestOperand input_op) { |
| 297 | InstructionOperand inputs[4]{ConvertInputOp(input_op), ConvertInputOp(Imm()), |
| 298 | ConvertInputOp(Imm()), ConvertInputOp(Imm())}; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 299 | InstructionCode opcode = kArchJmp | FlagsModeField::encode(kFlags_branch) | |
| 300 | FlagsConditionField::encode(kEqual); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 301 | auto instruction = NewInstruction(opcode, 0, nullptr, 4, inputs); |
| 302 | return AddInstruction(instruction); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 306 | Instruction* InstructionSequenceTest::EmitFallThrough() { |
| 307 | auto instruction = NewInstruction(kArchNop, 0, nullptr); |
| 308 | return AddInstruction(instruction); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 312 | Instruction* InstructionSequenceTest::EmitJump() { |
| 313 | InstructionOperand inputs[1]{ConvertInputOp(Imm())}; |
| 314 | auto instruction = NewInstruction(kArchJmp, 0, nullptr, 1, inputs); |
| 315 | return AddInstruction(instruction); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | |
| 319 | Instruction* InstructionSequenceTest::NewInstruction( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 320 | InstructionCode code, size_t outputs_size, InstructionOperand* outputs, |
| 321 | size_t inputs_size, InstructionOperand* inputs, size_t temps_size, |
| 322 | InstructionOperand* temps) { |
| 323 | CHECK(current_block_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 324 | return Instruction::New(zone(), code, outputs_size, outputs, inputs_size, |
| 325 | inputs, temps_size, temps); |
| 326 | } |
| 327 | |
| 328 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 329 | InstructionOperand InstructionSequenceTest::Unallocated( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 330 | TestOperand op, UnallocatedOperand::ExtendedPolicy policy) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 331 | return UnallocatedOperand(policy, op.vreg_.value_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 335 | InstructionOperand InstructionSequenceTest::Unallocated( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 336 | TestOperand op, UnallocatedOperand::ExtendedPolicy policy, |
| 337 | UnallocatedOperand::Lifetime lifetime) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 338 | return UnallocatedOperand(policy, lifetime, op.vreg_.value_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 342 | InstructionOperand InstructionSequenceTest::Unallocated( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 343 | TestOperand op, UnallocatedOperand::ExtendedPolicy policy, int index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 344 | return UnallocatedOperand(policy, index, op.vreg_.value_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 348 | InstructionOperand InstructionSequenceTest::Unallocated( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 349 | TestOperand op, UnallocatedOperand::BasicPolicy policy, int index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 350 | return UnallocatedOperand(policy, index, op.vreg_.value_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 354 | InstructionOperand* InstructionSequenceTest::ConvertInputs( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 355 | size_t input_size, TestOperand* inputs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 356 | InstructionOperand* mapped_inputs = |
| 357 | zone()->NewArray<InstructionOperand>(static_cast<int>(input_size)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 358 | for (size_t i = 0; i < input_size; ++i) { |
| 359 | mapped_inputs[i] = ConvertInputOp(inputs[i]); |
| 360 | } |
| 361 | return mapped_inputs; |
| 362 | } |
| 363 | |
| 364 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 365 | InstructionOperand InstructionSequenceTest::ConvertInputOp(TestOperand op) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 366 | if (op.type_ == kImmediate) { |
| 367 | CHECK_EQ(op.vreg_.value_, kNoValue); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 368 | return ImmediateOperand(ImmediateOperand::INLINE, op.value_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 369 | } |
| 370 | CHECK_NE(op.vreg_.value_, kNoValue); |
| 371 | switch (op.type_) { |
| 372 | case kNone: |
| 373 | return Unallocated(op, UnallocatedOperand::NONE, |
| 374 | UnallocatedOperand::USED_AT_START); |
| 375 | case kUnique: |
| 376 | return Unallocated(op, UnallocatedOperand::NONE); |
| 377 | case kUniqueRegister: |
| 378 | return Unallocated(op, UnallocatedOperand::MUST_HAVE_REGISTER); |
| 379 | case kRegister: |
| 380 | return Unallocated(op, UnallocatedOperand::MUST_HAVE_REGISTER, |
| 381 | UnallocatedOperand::USED_AT_START); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 382 | case kSlot: |
| 383 | return Unallocated(op, UnallocatedOperand::MUST_HAVE_SLOT, |
| 384 | UnallocatedOperand::USED_AT_START); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 385 | case kFixedRegister: |
| 386 | CHECK(0 <= op.value_ && op.value_ < num_general_registers_); |
| 387 | return Unallocated(op, UnallocatedOperand::FIXED_REGISTER, op.value_); |
| 388 | case kFixedSlot: |
| 389 | return Unallocated(op, UnallocatedOperand::FIXED_SLOT, op.value_); |
| 390 | default: |
| 391 | break; |
| 392 | } |
| 393 | CHECK(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 394 | return InstructionOperand(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 398 | InstructionOperand InstructionSequenceTest::ConvertOutputOp(VReg vreg, |
| 399 | TestOperand op) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 400 | CHECK_EQ(op.vreg_.value_, kNoValue); |
| 401 | op.vreg_ = vreg; |
| 402 | switch (op.type_) { |
| 403 | case kSameAsFirst: |
| 404 | return Unallocated(op, UnallocatedOperand::SAME_AS_FIRST_INPUT); |
| 405 | case kRegister: |
| 406 | return Unallocated(op, UnallocatedOperand::MUST_HAVE_REGISTER); |
| 407 | case kFixedSlot: |
| 408 | return Unallocated(op, UnallocatedOperand::FIXED_SLOT, op.value_); |
| 409 | case kFixedRegister: |
| 410 | CHECK(0 <= op.value_ && op.value_ < num_general_registers_); |
| 411 | return Unallocated(op, UnallocatedOperand::FIXED_REGISTER, op.value_); |
| 412 | default: |
| 413 | break; |
| 414 | } |
| 415 | CHECK(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 416 | return InstructionOperand(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 420 | InstructionBlock* InstructionSequenceTest::NewBlock(bool deferred) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 421 | CHECK(current_block_ == nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 422 | Rpo rpo = Rpo::FromInt(static_cast<int>(instruction_blocks_.size())); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 423 | Rpo loop_header = Rpo::Invalid(); |
| 424 | Rpo loop_end = Rpo::Invalid(); |
| 425 | if (!loop_blocks_.empty()) { |
| 426 | auto& loop_data = loop_blocks_.back(); |
| 427 | // This is a loop header. |
| 428 | if (!loop_data.loop_header_.IsValid()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 429 | loop_end = Rpo::FromInt(rpo.ToInt() + loop_data.expected_blocks_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 430 | loop_data.expected_blocks_--; |
| 431 | loop_data.loop_header_ = rpo; |
| 432 | } else { |
| 433 | // This is a loop body. |
| 434 | CHECK_NE(0, loop_data.expected_blocks_); |
| 435 | // TODO(dcarney): handle nested loops. |
| 436 | loop_data.expected_blocks_--; |
| 437 | loop_header = loop_data.loop_header_; |
| 438 | } |
| 439 | } |
| 440 | // Construct instruction block. |
| 441 | auto instruction_block = new (zone()) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 442 | InstructionBlock(zone(), rpo, loop_header, loop_end, deferred, false); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 443 | instruction_blocks_.push_back(instruction_block); |
| 444 | current_block_ = instruction_block; |
| 445 | sequence()->StartBlock(rpo); |
| 446 | return instruction_block; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | void InstructionSequenceTest::WireBlocks() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 451 | CHECK(!current_block()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 452 | CHECK(instruction_blocks_.size() == completions_.size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 453 | CHECK(loop_blocks_.empty()); |
| 454 | // Wire in end block to look like a scheduler produced cfg. |
| 455 | auto end_block = NewBlock(); |
| 456 | current_block_ = nullptr; |
| 457 | sequence()->EndBlock(end_block->rpo_number()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 458 | size_t offset = 0; |
| 459 | for (const auto& completion : completions_) { |
| 460 | switch (completion.type_) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 461 | case kBlockEnd: { |
| 462 | auto block = instruction_blocks_[offset]; |
| 463 | block->successors().push_back(end_block->rpo_number()); |
| 464 | end_block->predecessors().push_back(block->rpo_number()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 465 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 466 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 467 | case kFallThrough: // Fallthrough. |
| 468 | case kJump: |
| 469 | WireBlock(offset, completion.offset_0_); |
| 470 | break; |
| 471 | case kBranch: |
| 472 | WireBlock(offset, completion.offset_0_); |
| 473 | WireBlock(offset, completion.offset_1_); |
| 474 | break; |
| 475 | } |
| 476 | ++offset; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | |
| 481 | void InstructionSequenceTest::WireBlock(size_t block_offset, int jump_offset) { |
| 482 | size_t target_block_offset = block_offset + static_cast<size_t>(jump_offset); |
| 483 | CHECK(block_offset < instruction_blocks_.size()); |
| 484 | CHECK(target_block_offset < instruction_blocks_.size()); |
| 485 | auto block = instruction_blocks_[block_offset]; |
| 486 | auto target = instruction_blocks_[target_block_offset]; |
| 487 | block->successors().push_back(target->rpo_number()); |
| 488 | target->predecessors().push_back(block->rpo_number()); |
| 489 | } |
| 490 | |
| 491 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 492 | Instruction* InstructionSequenceTest::Emit( |
| 493 | InstructionCode code, size_t outputs_size, InstructionOperand* outputs, |
| 494 | size_t inputs_size, InstructionOperand* inputs, size_t temps_size, |
| 495 | InstructionOperand* temps, bool is_call) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 496 | auto instruction = NewInstruction(code, outputs_size, outputs, inputs_size, |
| 497 | inputs, temps_size, temps); |
| 498 | if (is_call) instruction->MarkAsCall(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 499 | return AddInstruction(instruction); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 503 | Instruction* InstructionSequenceTest::AddInstruction(Instruction* instruction) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 504 | sequence()->AddInstruction(instruction); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 505 | return instruction; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | } // namespace compiler |
| 509 | } // namespace internal |
| 510 | } // namespace v8 |