1 // Copyright 2012 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/lithium.h"
9 #include "src/scopes.h"
10 #include "src/serialize.h"
12 #if V8_TARGET_ARCH_IA32
13 #include "src/ia32/lithium-ia32.h" // NOLINT
14 #include "src/ia32/lithium-codegen-ia32.h" // NOLINT
15 #elif V8_TARGET_ARCH_X64
16 #include "src/x64/lithium-x64.h" // NOLINT
17 #include "src/x64/lithium-codegen-x64.h" // NOLINT
18 #elif V8_TARGET_ARCH_ARM
19 #include "src/arm/lithium-arm.h" // NOLINT
20 #include "src/arm/lithium-codegen-arm.h" // NOLINT
21 #elif V8_TARGET_ARCH_PPC
22 #include "src/ppc/lithium-ppc.h" // NOLINT
23 #include "src/ppc/lithium-codegen-ppc.h" // NOLINT
24 #elif V8_TARGET_ARCH_MIPS
25 #include "src/mips/lithium-mips.h" // NOLINT
26 #include "src/mips/lithium-codegen-mips.h" // NOLINT
27 #elif V8_TARGET_ARCH_ARM64
28 #include "src/arm64/lithium-arm64.h" // NOLINT
29 #include "src/arm64/lithium-codegen-arm64.h" // NOLINT
30 #elif V8_TARGET_ARCH_MIPS64
31 #include "src/mips64/lithium-mips64.h" // NOLINT
32 #include "src/mips64/lithium-codegen-mips64.h" // NOLINT
33 #elif V8_TARGET_ARCH_X87
34 #include "src/x87/lithium-x87.h" // NOLINT
35 #include "src/x87/lithium-codegen-x87.h" // NOLINT
37 #error "Unknown architecture."
44 void LOperand::PrintTo(StringStream* stream) {
45 LUnallocated* unalloc = NULL;
51 unalloc = LUnallocated::cast(this);
52 stream->Add("v%d", unalloc->virtual_register());
53 if (unalloc->basic_policy() == LUnallocated::FIXED_SLOT) {
54 stream->Add("(=%dS)", unalloc->fixed_slot_index());
57 switch (unalloc->extended_policy()) {
58 case LUnallocated::NONE:
60 case LUnallocated::FIXED_REGISTER: {
61 int reg_index = unalloc->fixed_register_index();
63 reg_index >= Register::kMaxNumAllocatableRegisters) {
64 stream->Add("(=invalid_reg#%d)", reg_index);
66 const char* register_name =
67 Register::AllocationIndexToString(reg_index);
68 stream->Add("(=%s)", register_name);
72 case LUnallocated::FIXED_DOUBLE_REGISTER: {
73 int reg_index = unalloc->fixed_register_index();
75 reg_index >= DoubleRegister::kMaxNumAllocatableRegisters) {
76 stream->Add("(=invalid_double_reg#%d)", reg_index);
78 const char* double_register_name =
79 DoubleRegister::AllocationIndexToString(reg_index);
80 stream->Add("(=%s)", double_register_name);
84 case LUnallocated::MUST_HAVE_REGISTER:
87 case LUnallocated::MUST_HAVE_DOUBLE_REGISTER:
90 case LUnallocated::WRITABLE_REGISTER:
93 case LUnallocated::SAME_AS_FIRST_INPUT:
96 case LUnallocated::ANY:
101 case CONSTANT_OPERAND:
102 stream->Add("[constant:%d]", index());
105 stream->Add("[stack:%d]", index());
107 case DOUBLE_STACK_SLOT:
108 stream->Add("[double_stack:%d]", index());
111 int reg_index = index();
112 if (reg_index < 0 || reg_index >= Register::kMaxNumAllocatableRegisters) {
113 stream->Add("(=invalid_reg#%d|R)", reg_index);
115 stream->Add("[%s|R]", Register::AllocationIndexToString(reg_index));
119 case DOUBLE_REGISTER: {
120 int reg_index = index();
122 reg_index >= DoubleRegister::kMaxNumAllocatableRegisters) {
123 stream->Add("(=invalid_double_reg#%d|R)", reg_index);
125 stream->Add("[%s|R]",
126 DoubleRegister::AllocationIndexToString(reg_index));
134 template<LOperand::Kind kOperandKind, int kNumCachedOperands>
135 LSubKindOperand<kOperandKind, kNumCachedOperands>*
136 LSubKindOperand<kOperandKind, kNumCachedOperands>::cache = NULL;
139 template<LOperand::Kind kOperandKind, int kNumCachedOperands>
140 void LSubKindOperand<kOperandKind, kNumCachedOperands>::SetUpCache() {
142 cache = new LSubKindOperand[kNumCachedOperands];
143 for (int i = 0; i < kNumCachedOperands; i++) {
144 cache[i].ConvertTo(kOperandKind, i);
149 template<LOperand::Kind kOperandKind, int kNumCachedOperands>
150 void LSubKindOperand<kOperandKind, kNumCachedOperands>::TearDownCache() {
156 void LOperand::SetUpCaches() {
157 #define LITHIUM_OPERAND_SETUP(name, type, number) L##name::SetUpCache();
158 LITHIUM_OPERAND_LIST(LITHIUM_OPERAND_SETUP)
159 #undef LITHIUM_OPERAND_SETUP
163 void LOperand::TearDownCaches() {
164 #define LITHIUM_OPERAND_TEARDOWN(name, type, number) L##name::TearDownCache();
165 LITHIUM_OPERAND_LIST(LITHIUM_OPERAND_TEARDOWN)
166 #undef LITHIUM_OPERAND_TEARDOWN
170 bool LParallelMove::IsRedundant() const {
171 for (int i = 0; i < move_operands_.length(); ++i) {
172 if (!move_operands_[i].IsRedundant()) return false;
178 void LParallelMove::PrintDataTo(StringStream* stream) const {
180 for (int i = 0; i < move_operands_.length(); ++i) {
181 if (!move_operands_[i].IsEliminated()) {
182 LOperand* source = move_operands_[i].source();
183 LOperand* destination = move_operands_[i].destination();
184 if (!first) stream->Add(" ");
186 if (source->Equals(destination)) {
187 destination->PrintTo(stream);
189 destination->PrintTo(stream);
191 source->PrintTo(stream);
199 void LEnvironment::PrintTo(StringStream* stream) {
200 stream->Add("[id=%d|", ast_id().ToInt());
201 if (deoptimization_index() != Safepoint::kNoDeoptimizationIndex) {
202 stream->Add("deopt_id=%d|", deoptimization_index());
204 stream->Add("parameters=%d|", parameter_count());
205 stream->Add("arguments_stack_height=%d|", arguments_stack_height());
206 for (int i = 0; i < values_.length(); ++i) {
207 if (i != 0) stream->Add(";");
208 if (values_[i] == NULL) {
209 stream->Add("[hole]");
211 values_[i]->PrintTo(stream);
218 void LPointerMap::RecordPointer(LOperand* op, Zone* zone) {
219 // Do not record arguments as pointers.
220 if (op->IsStackSlot() && op->index() < 0) return;
221 DCHECK(!op->IsDoubleRegister() && !op->IsDoubleStackSlot());
222 pointer_operands_.Add(op, zone);
226 void LPointerMap::RemovePointer(LOperand* op) {
227 // Do not record arguments as pointers.
228 if (op->IsStackSlot() && op->index() < 0) return;
229 DCHECK(!op->IsDoubleRegister() && !op->IsDoubleStackSlot());
230 for (int i = 0; i < pointer_operands_.length(); ++i) {
231 if (pointer_operands_[i]->Equals(op)) {
232 pointer_operands_.Remove(i);
239 void LPointerMap::RecordUntagged(LOperand* op, Zone* zone) {
240 // Do not record arguments as pointers.
241 if (op->IsStackSlot() && op->index() < 0) return;
242 DCHECK(!op->IsDoubleRegister() && !op->IsDoubleStackSlot());
243 untagged_operands_.Add(op, zone);
247 void LPointerMap::PrintTo(StringStream* stream) {
249 for (int i = 0; i < pointer_operands_.length(); ++i) {
250 if (i != 0) stream->Add(";");
251 pointer_operands_[i]->PrintTo(stream);
257 int StackSlotOffset(int index) {
259 // Local or spill slot. Skip the frame pointer, function, and
260 // context in the fixed part of the frame.
261 return -(index + 1) * kPointerSize -
262 StandardFrameConstants::kFixedFrameSizeFromFp;
264 // Incoming parameter. Skip the return address.
265 return -(index + 1) * kPointerSize + kFPOnStackSize + kPCOnStackSize;
270 LChunk::LChunk(CompilationInfo* info, HGraph* graph)
271 : spill_slot_count_(0),
274 instructions_(32, info->zone()),
275 pointer_maps_(8, info->zone()),
276 inlined_closures_(1, info->zone()),
277 deprecation_dependencies_(MapLess(), MapAllocator(info->zone())),
278 stability_dependencies_(MapLess(), MapAllocator(info->zone())) {}
281 LLabel* LChunk::GetLabel(int block_id) const {
282 HBasicBlock* block = graph_->blocks()->at(block_id);
283 int first_instruction = block->first_instruction_index();
284 return LLabel::cast(instructions_[first_instruction]);
288 int LChunk::LookupDestination(int block_id) const {
289 LLabel* cur = GetLabel(block_id);
290 while (cur->replacement() != NULL) {
291 cur = cur->replacement();
293 return cur->block_id();
296 Label* LChunk::GetAssemblyLabel(int block_id) const {
297 LLabel* label = GetLabel(block_id);
298 DCHECK(!label->HasReplacement());
299 return label->label();
303 void LChunk::MarkEmptyBlocks() {
304 LPhase phase("L_Mark empty blocks", this);
305 for (int i = 0; i < graph()->blocks()->length(); ++i) {
306 HBasicBlock* block = graph()->blocks()->at(i);
307 int first = block->first_instruction_index();
308 int last = block->last_instruction_index();
309 LInstruction* first_instr = instructions()->at(first);
310 LInstruction* last_instr = instructions()->at(last);
312 LLabel* label = LLabel::cast(first_instr);
313 if (last_instr->IsGoto()) {
314 LGoto* goto_instr = LGoto::cast(last_instr);
315 if (label->IsRedundant() &&
316 !label->is_loop_header()) {
317 bool can_eliminate = true;
318 for (int i = first + 1; i < last && can_eliminate; ++i) {
319 LInstruction* cur = instructions()->at(i);
321 LGap* gap = LGap::cast(cur);
322 if (!gap->IsRedundant()) {
323 can_eliminate = false;
326 can_eliminate = false;
330 label->set_replacement(GetLabel(goto_instr->block_id()));
338 void LChunk::AddInstruction(LInstruction* instr, HBasicBlock* block) {
339 LInstructionGap* gap = new (zone()) LInstructionGap(block);
340 gap->set_hydrogen_value(instr->hydrogen_value());
342 if (instr->IsControl()) {
343 instructions_.Add(gap, zone());
344 index = instructions_.length();
345 instructions_.Add(instr, zone());
347 index = instructions_.length();
348 instructions_.Add(instr, zone());
349 instructions_.Add(gap, zone());
351 if (instr->HasPointerMap()) {
352 pointer_maps_.Add(instr->pointer_map(), zone());
353 instr->pointer_map()->set_lithium_position(index);
358 LConstantOperand* LChunk::DefineConstantOperand(HConstant* constant) {
359 return LConstantOperand::Create(constant->id(), zone());
363 int LChunk::GetParameterStackSlot(int index) const {
364 // The receiver is at index 0, the first parameter at index 1, so we
365 // shift all parameter indexes down by the number of parameters, and
366 // make sure they end up negative so they are distinguishable from
368 int result = index - info()->num_parameters() - 1;
375 // A parameter relative to ebp in the arguments stub.
376 int LChunk::ParameterAt(int index) {
377 DCHECK(-1 <= index); // -1 is the receiver.
378 return (1 + info()->scope()->num_parameters() - index) *
383 LGap* LChunk::GetGapAt(int index) const {
384 return LGap::cast(instructions_[index]);
388 bool LChunk::IsGapAt(int index) const {
389 return instructions_[index]->IsGap();
393 int LChunk::NearestGapPos(int index) const {
394 while (!IsGapAt(index)) index--;
399 void LChunk::AddGapMove(int index, LOperand* from, LOperand* to) {
400 GetGapAt(index)->GetOrCreateParallelMove(
401 LGap::START, zone())->AddMove(from, to, zone());
405 HConstant* LChunk::LookupConstant(LConstantOperand* operand) const {
406 return HConstant::cast(graph_->LookupValue(operand->index()));
410 Representation LChunk::LookupLiteralRepresentation(
411 LConstantOperand* operand) const {
412 return graph_->LookupValue(operand->index())->representation();
416 static void AddWeakObjectToCodeDependency(Isolate* isolate,
417 Handle<HeapObject> object,
419 Handle<WeakCell> cell = Code::WeakCellFor(code);
420 Heap* heap = isolate->heap();
421 Handle<DependentCode> dep(heap->LookupWeakObjectToCodeDependency(object));
422 dep = DependentCode::InsertWeakCode(dep, DependentCode::kWeakCodeGroup, cell);
423 heap->AddWeakObjectToCodeDependency(object, dep);
427 void LChunk::RegisterWeakObjectsInOptimizedCode(Handle<Code> code) const {
428 DCHECK(code->is_optimized_code());
429 ZoneList<Handle<Map> > maps(1, zone());
430 ZoneList<Handle<HeapObject> > objects(1, zone());
431 int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
432 RelocInfo::ModeMask(RelocInfo::CELL);
433 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) {
434 RelocInfo::Mode mode = it.rinfo()->rmode();
435 if (mode == RelocInfo::CELL &&
436 code->IsWeakObjectInOptimizedCode(it.rinfo()->target_cell())) {
437 objects.Add(Handle<HeapObject>(it.rinfo()->target_cell()), zone());
438 } else if (mode == RelocInfo::EMBEDDED_OBJECT &&
439 code->IsWeakObjectInOptimizedCode(it.rinfo()->target_object())) {
440 if (it.rinfo()->target_object()->IsMap()) {
441 Handle<Map> map(Map::cast(it.rinfo()->target_object()));
442 maps.Add(map, zone());
444 Handle<HeapObject> object(
445 HeapObject::cast(it.rinfo()->target_object()));
446 objects.Add(object, zone());
450 for (int i = 0; i < maps.length(); i++) {
451 Map::AddDependentCode(maps.at(i), DependentCode::kWeakCodeGroup, code);
453 for (int i = 0; i < objects.length(); i++) {
454 AddWeakObjectToCodeDependency(isolate(), objects.at(i), code);
456 if (FLAG_enable_ool_constant_pool) {
457 code->constant_pool()->set_weak_object_state(
458 ConstantPoolArray::WEAK_OBJECTS_IN_OPTIMIZED_CODE);
460 code->set_can_have_weak_objects(true);
464 void LChunk::CommitDependencies(Handle<Code> code) const {
465 if (!code->is_optimized_code()) return;
466 HandleScope scope(isolate());
468 for (MapSet::const_iterator it = deprecation_dependencies_.begin(),
469 iend = deprecation_dependencies_.end(); it != iend; ++it) {
470 Handle<Map> map = *it;
471 DCHECK(!map->is_deprecated());
472 DCHECK(map->CanBeDeprecated());
473 Map::AddDependentCode(map, DependentCode::kTransitionGroup, code);
476 for (MapSet::const_iterator it = stability_dependencies_.begin(),
477 iend = stability_dependencies_.end(); it != iend; ++it) {
478 Handle<Map> map = *it;
479 DCHECK(map->is_stable());
480 DCHECK(map->CanTransition());
481 Map::AddDependentCode(map, DependentCode::kPrototypeCheckGroup, code);
484 info_->CommitDependencies(code);
485 RegisterWeakObjectsInOptimizedCode(code);
489 LChunk* LChunk::NewChunk(HGraph* graph) {
490 DisallowHandleAllocation no_handles;
491 DisallowHeapAllocation no_gc;
492 graph->DisallowAddingNewValues();
493 int values = graph->GetMaximumValueID();
494 CompilationInfo* info = graph->info();
495 if (values > LUnallocated::kMaxVirtualRegisters) {
496 info->AbortOptimization(kNotEnoughVirtualRegistersForValues);
499 LAllocator allocator(values, graph);
500 LChunkBuilder builder(info, graph, &allocator);
501 LChunk* chunk = builder.Build();
502 if (chunk == NULL) return NULL;
504 if (!allocator.Allocate(chunk)) {
505 info->AbortOptimization(kNotEnoughVirtualRegistersRegalloc);
509 chunk->set_allocated_double_registers(
510 allocator.assigned_double_registers());
516 Handle<Code> LChunk::Codegen() {
517 MacroAssembler assembler(info()->isolate(), NULL, 0);
518 LOG_CODE_EVENT(info()->isolate(),
519 CodeStartLinePosInfoRecordEvent(
520 assembler.positions_recorder()));
521 // Code serializer only takes unoptimized code.
522 DCHECK(!info()->will_serialize());
523 LCodeGen generator(this, &assembler, info());
527 if (generator.GenerateCode()) {
528 generator.CheckEnvironmentUsage();
529 CodeGenerator::MakeCodePrologue(info(), "optimized");
530 Code::Flags flags = info()->flags();
532 CodeGenerator::MakeCodeEpilogue(&assembler, flags, info());
533 generator.FinishCode(code);
534 CommitDependencies(code);
535 code->set_is_crankshafted(true);
536 void* jit_handler_data =
537 assembler.positions_recorder()->DetachJITHandlerData();
538 LOG_CODE_EVENT(info()->isolate(),
539 CodeEndLinePosInfoRecordEvent(*code, jit_handler_data));
541 CodeGenerator::PrintCode(code, info());
542 DCHECK(!(info()->isolate()->serializer_enabled() &&
543 info()->GetMustNotHaveEagerFrame() &&
544 generator.NeedsEagerFrame()));
547 assembler.AbortedCodeGeneration();
548 return Handle<Code>::null();
552 void LChunk::set_allocated_double_registers(BitVector* allocated_registers) {
553 allocated_double_registers_ = allocated_registers;
554 BitVector* doubles = allocated_double_registers();
555 BitVector::Iterator iterator(doubles);
556 while (!iterator.Done()) {
557 if (info()->saves_caller_doubles()) {
558 if (kDoubleSize == kPointerSize * 2) {
559 spill_slot_count_ += 2;
569 void LChunkBuilderBase::Abort(BailoutReason reason) {
570 info()->AbortOptimization(reason);
575 void LChunkBuilderBase::Retry(BailoutReason reason) {
576 info()->RetryOptimization(reason);
581 LEnvironment* LChunkBuilderBase::CreateEnvironment(
582 HEnvironment* hydrogen_env, int* argument_index_accumulator,
583 ZoneList<HValue*>* objects_to_materialize) {
584 if (hydrogen_env == NULL) return NULL;
586 LEnvironment* outer =
587 CreateEnvironment(hydrogen_env->outer(), argument_index_accumulator,
588 objects_to_materialize);
589 BailoutId ast_id = hydrogen_env->ast_id();
590 DCHECK(!ast_id.IsNone() ||
591 hydrogen_env->frame_type() != JS_FUNCTION);
593 int omitted_count = (hydrogen_env->frame_type() == JS_FUNCTION)
595 : hydrogen_env->specials_count();
597 int value_count = hydrogen_env->length() - omitted_count;
598 LEnvironment* result =
599 new(zone()) LEnvironment(hydrogen_env->closure(),
600 hydrogen_env->frame_type(),
602 hydrogen_env->parameter_count(),
606 hydrogen_env->entry(),
608 int argument_index = *argument_index_accumulator;
610 // Store the environment description into the environment
611 // (with holes for nested objects)
612 for (int i = 0; i < hydrogen_env->length(); ++i) {
613 if (hydrogen_env->is_special_index(i) &&
614 hydrogen_env->frame_type() != JS_FUNCTION) {
618 HValue* value = hydrogen_env->values()->at(i);
619 CHECK(!value->IsPushArguments()); // Do not deopt outgoing arguments
620 if (value->IsArgumentsObject() || value->IsCapturedObject()) {
621 op = LEnvironment::materialization_marker();
626 value->representation(),
627 value->CheckFlag(HInstruction::kUint32));
630 // Recursively store the nested objects into the environment
631 for (int i = 0; i < hydrogen_env->length(); ++i) {
632 if (hydrogen_env->is_special_index(i)) continue;
634 HValue* value = hydrogen_env->values()->at(i);
635 if (value->IsArgumentsObject() || value->IsCapturedObject()) {
636 AddObjectToMaterialize(value, objects_to_materialize, result);
640 if (hydrogen_env->frame_type() == JS_FUNCTION) {
641 *argument_index_accumulator = argument_index;
648 // Add an object to the supplied environment and object materialization list.
652 // We are building three lists here:
654 // 1. In the result->object_mapping_ list (added to by the
655 // LEnvironment::Add*Object methods), we store the lengths (number
656 // of fields) of the captured objects in depth-first traversal order, or
657 // in case of duplicated objects, we store the index to the duplicate object
658 // (with a tag to differentiate between captured and duplicated objects).
660 // 2. The object fields are stored in the result->values_ list
661 // (added to by the LEnvironment.AddValue method) sequentially as lists
662 // of fields with holes for nested objects (the holes will be expanded
663 // later by LCodegen::AddToTranslation according to the
664 // LEnvironment.object_mapping_ list).
666 // 3. The auxiliary objects_to_materialize array stores the hydrogen values
667 // in the same order as result->object_mapping_ list. This is used
668 // to detect duplicate values and calculate the corresponding object index.
669 void LChunkBuilderBase::AddObjectToMaterialize(HValue* value,
670 ZoneList<HValue*>* objects_to_materialize, LEnvironment* result) {
671 int object_index = objects_to_materialize->length();
672 // Store the hydrogen value into the de-duplication array
673 objects_to_materialize->Add(value, zone());
674 // Find out whether we are storing a duplicated value
675 int previously_materialized_object = -1;
676 for (int prev = 0; prev < object_index; ++prev) {
677 if (objects_to_materialize->at(prev) == value) {
678 previously_materialized_object = prev;
682 // Store the captured object length (or duplicated object index)
683 // into the environment. For duplicated objects, we stop here.
684 int length = value->OperandCount();
685 bool is_arguments = value->IsArgumentsObject();
686 if (previously_materialized_object >= 0) {
687 result->AddDuplicateObject(previously_materialized_object);
690 result->AddNewObject(is_arguments ? length - 1 : length, is_arguments);
692 // Store the captured object's fields into the environment
693 for (int i = is_arguments ? 1 : 0; i < length; ++i) {
695 HValue* arg_value = value->OperandAt(i);
696 if (arg_value->IsArgumentsObject() || arg_value->IsCapturedObject()) {
697 // Insert a hole for nested objects
698 op = LEnvironment::materialization_marker();
700 DCHECK(!arg_value->IsPushArguments());
701 // For ordinary values, tell the register allocator we need the value
703 op = UseAny(arg_value);
706 arg_value->representation(),
707 arg_value->CheckFlag(HInstruction::kUint32));
709 // Recursively store all the nested captured objects into the environment
710 for (int i = is_arguments ? 1 : 0; i < length; ++i) {
711 HValue* arg_value = value->OperandAt(i);
712 if (arg_value->IsArgumentsObject() || arg_value->IsCapturedObject()) {
713 AddObjectToMaterialize(arg_value, objects_to_materialize, result);
720 if (ShouldProduceTraceOutput()) {
721 isolate()->GetHTracer()->TraceLithium(name(), chunk_);
726 } } // namespace v8::internal