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