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.
5 #include "src/compiler/instruction.h"
7 #include "src/compiler/common-operator.h"
13 OStream& operator<<(OStream& os, const InstructionOperand& op) {
15 case InstructionOperand::INVALID:
17 case InstructionOperand::UNALLOCATED: {
18 const UnallocatedOperand* unalloc = UnallocatedOperand::cast(&op);
19 os << "v" << unalloc->virtual_register();
20 if (unalloc->basic_policy() == UnallocatedOperand::FIXED_SLOT) {
21 return os << "(=" << unalloc->fixed_slot_index() << "S)";
23 switch (unalloc->extended_policy()) {
24 case UnallocatedOperand::NONE:
26 case UnallocatedOperand::FIXED_REGISTER:
27 return os << "(=" << Register::AllocationIndexToString(
28 unalloc->fixed_register_index()) << ")";
29 case UnallocatedOperand::FIXED_DOUBLE_REGISTER:
30 return os << "(=" << DoubleRegister::AllocationIndexToString(
31 unalloc->fixed_register_index()) << ")";
32 case UnallocatedOperand::MUST_HAVE_REGISTER:
34 case UnallocatedOperand::SAME_AS_FIRST_INPUT:
36 case UnallocatedOperand::ANY:
40 case InstructionOperand::CONSTANT:
41 return os << "[constant:" << op.index() << "]";
42 case InstructionOperand::IMMEDIATE:
43 return os << "[immediate:" << op.index() << "]";
44 case InstructionOperand::STACK_SLOT:
45 return os << "[stack:" << op.index() << "]";
46 case InstructionOperand::DOUBLE_STACK_SLOT:
47 return os << "[double_stack:" << op.index() << "]";
48 case InstructionOperand::REGISTER:
49 return os << "[" << Register::AllocationIndexToString(op.index())
51 case InstructionOperand::DOUBLE_REGISTER:
52 return os << "[" << DoubleRegister::AllocationIndexToString(op.index())
60 template <InstructionOperand::Kind kOperandKind, int kNumCachedOperands>
61 SubKindOperand<kOperandKind, kNumCachedOperands>*
62 SubKindOperand<kOperandKind, kNumCachedOperands>::cache = NULL;
65 template <InstructionOperand::Kind kOperandKind, int kNumCachedOperands>
66 void SubKindOperand<kOperandKind, kNumCachedOperands>::SetUpCache() {
68 cache = new SubKindOperand[kNumCachedOperands];
69 for (int i = 0; i < kNumCachedOperands; i++) {
70 cache[i].ConvertTo(kOperandKind, i);
75 template <InstructionOperand::Kind kOperandKind, int kNumCachedOperands>
76 void SubKindOperand<kOperandKind, kNumCachedOperands>::TearDownCache() {
82 void InstructionOperand::SetUpCaches() {
83 #define INSTRUCTION_OPERAND_SETUP(name, type, number) \
84 name##Operand::SetUpCache();
85 INSTRUCTION_OPERAND_LIST(INSTRUCTION_OPERAND_SETUP)
86 #undef INSTRUCTION_OPERAND_SETUP
90 void InstructionOperand::TearDownCaches() {
91 #define INSTRUCTION_OPERAND_TEARDOWN(name, type, number) \
92 name##Operand::TearDownCache();
93 INSTRUCTION_OPERAND_LIST(INSTRUCTION_OPERAND_TEARDOWN)
94 #undef INSTRUCTION_OPERAND_TEARDOWN
98 OStream& operator<<(OStream& os, const MoveOperands& mo) {
99 os << *mo.destination();
100 if (!mo.source()->Equals(mo.destination())) os << " = " << *mo.source();
105 bool ParallelMove::IsRedundant() const {
106 for (int i = 0; i < move_operands_.length(); ++i) {
107 if (!move_operands_[i].IsRedundant()) return false;
113 OStream& operator<<(OStream& os, const ParallelMove& pm) {
115 for (ZoneList<MoveOperands>::iterator move = pm.move_operands()->begin();
116 move != pm.move_operands()->end(); ++move) {
117 if (move->IsEliminated()) continue;
118 if (!first) os << " ";
126 void PointerMap::RecordPointer(InstructionOperand* op, Zone* zone) {
127 // Do not record arguments as pointers.
128 if (op->IsStackSlot() && op->index() < 0) return;
129 DCHECK(!op->IsDoubleRegister() && !op->IsDoubleStackSlot());
130 pointer_operands_.Add(op, zone);
134 void PointerMap::RemovePointer(InstructionOperand* op) {
135 // Do not record arguments as pointers.
136 if (op->IsStackSlot() && op->index() < 0) return;
137 DCHECK(!op->IsDoubleRegister() && !op->IsDoubleStackSlot());
138 for (int i = 0; i < pointer_operands_.length(); ++i) {
139 if (pointer_operands_[i]->Equals(op)) {
140 pointer_operands_.Remove(i);
147 void PointerMap::RecordUntagged(InstructionOperand* op, Zone* zone) {
148 // Do not record arguments as pointers.
149 if (op->IsStackSlot() && op->index() < 0) return;
150 DCHECK(!op->IsDoubleRegister() && !op->IsDoubleStackSlot());
151 untagged_operands_.Add(op, zone);
155 OStream& operator<<(OStream& os, const PointerMap& pm) {
157 for (ZoneList<InstructionOperand*>::iterator op =
158 pm.pointer_operands_.begin();
159 op != pm.pointer_operands_.end(); ++op) {
160 if (op != pm.pointer_operands_.begin()) os << ";";
167 OStream& operator<<(OStream& os, const ArchOpcode& ao) {
172 ARCH_OPCODE_LIST(CASE)
180 OStream& operator<<(OStream& os, const AddressingMode& am) {
187 TARGET_ADDRESSING_MODE_LIST(CASE)
195 OStream& operator<<(OStream& os, const FlagsMode& fm) {
200 return os << "branch";
209 OStream& operator<<(OStream& os, const FlagsCondition& fc) {
212 return os << "equal";
214 return os << "not equal";
215 case kSignedLessThan:
216 return os << "signed less than";
217 case kSignedGreaterThanOrEqual:
218 return os << "signed greater than or equal";
219 case kSignedLessThanOrEqual:
220 return os << "signed less than or equal";
221 case kSignedGreaterThan:
222 return os << "signed greater than";
223 case kUnsignedLessThan:
224 return os << "unsigned less than";
225 case kUnsignedGreaterThanOrEqual:
226 return os << "unsigned greater than or equal";
227 case kUnsignedLessThanOrEqual:
228 return os << "unsigned less than or equal";
229 case kUnsignedGreaterThan:
230 return os << "unsigned greater than";
231 case kUnorderedEqual:
232 return os << "unordered equal";
233 case kUnorderedNotEqual:
234 return os << "unordered not equal";
235 case kUnorderedLessThan:
236 return os << "unordered less than";
237 case kUnorderedGreaterThanOrEqual:
238 return os << "unordered greater than or equal";
239 case kUnorderedLessThanOrEqual:
240 return os << "unordered less than or equal";
241 case kUnorderedGreaterThan:
242 return os << "unordered greater than";
244 return os << "overflow";
246 return os << "not overflow";
253 OStream& operator<<(OStream& os, const Instruction& instr) {
254 if (instr.OutputCount() > 1) os << "(";
255 for (size_t i = 0; i < instr.OutputCount(); i++) {
256 if (i > 0) os << ", ";
257 os << *instr.OutputAt(i);
260 if (instr.OutputCount() > 1) os << ") = ";
261 if (instr.OutputCount() == 1) os << " = ";
263 if (instr.IsGapMoves()) {
264 const GapInstruction* gap = GapInstruction::cast(&instr);
265 os << (instr.IsBlockStart() ? " block-start" : "gap ");
266 for (int i = GapInstruction::FIRST_INNER_POSITION;
267 i <= GapInstruction::LAST_INNER_POSITION; i++) {
269 if (gap->parallel_moves_[i] != NULL) os << *gap->parallel_moves_[i];
272 } else if (instr.IsSourcePosition()) {
273 const SourcePositionInstruction* pos =
274 SourcePositionInstruction::cast(&instr);
275 os << "position (" << pos->source_position().raw() << ")";
277 os << ArchOpcodeField::decode(instr.opcode());
278 AddressingMode am = AddressingModeField::decode(instr.opcode());
279 if (am != kMode_None) {
280 os << " : " << AddressingModeField::decode(instr.opcode());
282 FlagsMode fm = FlagsModeField::decode(instr.opcode());
283 if (fm != kFlags_none) {
284 os << " && " << fm << " if "
285 << FlagsConditionField::decode(instr.opcode());
288 if (instr.InputCount() > 0) {
289 for (size_t i = 0; i < instr.InputCount(); i++) {
290 os << " " << *instr.InputAt(i);
297 OStream& operator<<(OStream& os, const Constant& constant) {
298 switch (constant.type()) {
299 case Constant::kInt32:
300 return os << constant.ToInt32();
301 case Constant::kInt64:
302 return os << constant.ToInt64() << "l";
303 case Constant::kFloat64:
304 return os << constant.ToFloat64();
305 case Constant::kExternalReference:
306 return os << constant.ToExternalReference().address();
307 case Constant::kHeapObject:
308 return os << Brief(*constant.ToHeapObject());
315 Label* InstructionSequence::GetLabel(BasicBlock* block) {
316 return GetBlockStart(block)->label();
320 BlockStartInstruction* InstructionSequence::GetBlockStart(BasicBlock* block) {
321 return BlockStartInstruction::cast(InstructionAt(block->code_start_));
325 void InstructionSequence::StartBlock(BasicBlock* block) {
326 block->code_start_ = static_cast<int>(instructions_.size());
327 BlockStartInstruction* block_start =
328 BlockStartInstruction::New(zone(), block);
329 AddInstruction(block_start, block);
333 void InstructionSequence::EndBlock(BasicBlock* block) {
334 int end = static_cast<int>(instructions_.size());
335 DCHECK(block->code_start_ >= 0 && block->code_start_ < end);
336 block->code_end_ = end;
340 int InstructionSequence::AddInstruction(Instruction* instr, BasicBlock* block) {
341 // TODO(titzer): the order of these gaps is a holdover from Lithium.
342 GapInstruction* gap = GapInstruction::New(zone());
343 if (instr->IsControl()) instructions_.push_back(gap);
344 int index = static_cast<int>(instructions_.size());
345 instructions_.push_back(instr);
346 if (!instr->IsControl()) instructions_.push_back(gap);
347 if (instr->NeedsPointerMap()) {
348 DCHECK(instr->pointer_map() == NULL);
349 PointerMap* pointer_map = new (zone()) PointerMap(zone());
350 pointer_map->set_instruction_position(index);
351 instr->set_pointer_map(pointer_map);
352 pointer_maps_.push_back(pointer_map);
358 BasicBlock* InstructionSequence::GetBasicBlock(int instruction_index) {
359 // TODO(turbofan): Optimize this.
361 DCHECK_LE(0, instruction_index);
362 Instruction* instruction = InstructionAt(instruction_index--);
363 if (instruction->IsBlockStart()) {
364 return BlockStartInstruction::cast(instruction)->block();
370 bool InstructionSequence::IsReference(int virtual_register) const {
371 return references_.find(virtual_register) != references_.end();
375 bool InstructionSequence::IsDouble(int virtual_register) const {
376 return doubles_.find(virtual_register) != doubles_.end();
380 void InstructionSequence::MarkAsReference(int virtual_register) {
381 references_.insert(virtual_register);
385 void InstructionSequence::MarkAsDouble(int virtual_register) {
386 doubles_.insert(virtual_register);
390 void InstructionSequence::AddGapMove(int index, InstructionOperand* from,
391 InstructionOperand* to) {
392 GapAt(index)->GetOrCreateParallelMove(GapInstruction::START, zone())->AddMove(
397 InstructionSequence::StateId InstructionSequence::AddFrameStateDescriptor(
398 FrameStateDescriptor* descriptor) {
399 int deoptimization_id = static_cast<int>(deoptimization_entries_.size());
400 deoptimization_entries_.push_back(descriptor);
401 return StateId::FromInt(deoptimization_id);
404 FrameStateDescriptor* InstructionSequence::GetFrameStateDescriptor(
405 InstructionSequence::StateId state_id) {
406 return deoptimization_entries_[state_id.ToInt()];
410 int InstructionSequence::GetFrameStateDescriptorCount() {
411 return static_cast<int>(deoptimization_entries_.size());
415 OStream& operator<<(OStream& os, const InstructionSequence& code) {
416 for (size_t i = 0; i < code.immediates_.size(); ++i) {
417 Constant constant = code.immediates_[i];
418 os << "IMM#" << i << ": " << constant << "\n";
421 for (ConstantMap::const_iterator it = code.constants_.begin();
422 it != code.constants_.end(); ++i, ++it) {
423 os << "CST#" << i << ": v" << it->first << " = " << it->second << "\n";
425 for (int i = 0; i < code.BasicBlockCount(); i++) {
426 BasicBlock* block = code.BlockAt(i);
428 int bid = block->id();
429 os << "RPO#" << block->rpo_number_ << ": B" << bid;
430 CHECK(block->rpo_number_ == i);
431 if (block->IsLoopHeader()) {
432 os << " loop blocks: [" << block->rpo_number_ << ", " << block->loop_end_
435 os << " instructions: [" << block->code_start_ << ", " << block->code_end_
436 << ")\n predecessors:";
438 BasicBlock::Predecessors predecessors = block->predecessors();
439 for (BasicBlock::Predecessors::iterator iter = predecessors.begin();
440 iter != predecessors.end(); ++iter) {
441 os << " B" << (*iter)->id();
445 for (BasicBlock::const_iterator j = block->begin(); j != block->end();
448 if (phi->opcode() != IrOpcode::kPhi) continue;
449 os << " phi: v" << phi->id() << " =";
450 Node::Inputs inputs = phi->inputs();
451 for (Node::Inputs::iterator iter(inputs.begin()); iter != inputs.end();
453 os << " v" << (*iter)->id();
458 ScopedVector<char> buf(32);
459 for (int j = block->first_instruction_index();
460 j <= block->last_instruction_index(); j++) {
461 // TODO(svenpanne) Add some basic formatting to our streams.
462 SNPrintF(buf, "%5d", j);
463 os << " " << buf.start() << ": " << *code.InstructionAt(j);
466 os << " " << block->control_;
468 if (block->control_input_ != NULL) {
469 os << " v" << block->control_input_->id();
472 BasicBlock::Successors successors = block->successors();
473 for (BasicBlock::Successors::iterator iter = successors.begin();
474 iter != successors.end(); ++iter) {
475 os << " B" << (*iter)->id();
482 } // namespace compiler
483 } // namespace internal