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.
10 #include "src/allocation.h"
11 #include "src/hydrogen.h"
12 #include "src/safepoint-table.h"
13 #include "src/zone-allocator.h"
18 #define LITHIUM_OPERAND_LIST(V) \
19 V(ConstantOperand, CONSTANT_OPERAND, 128) \
20 V(StackSlot, STACK_SLOT, 128) \
21 V(DoubleStackSlot, DOUBLE_STACK_SLOT, 128) \
22 V(Float32x4StackSlot, FLOAT32x4_STACK_SLOT, 128) \
23 V(Float64x2StackSlot, FLOAT64x2_STACK_SLOT, 128) \
24 V(Int32x4StackSlot, INT32x4_STACK_SLOT, 128) \
25 V(Register, REGISTER, 16) \
26 V(DoubleRegister, DOUBLE_REGISTER, 16) \
27 V(Float32x4Register, FLOAT32x4_REGISTER, 16) \
28 V(Float64x2Register, FLOAT64x2_REGISTER, 16) \
29 V(Int32x4Register, INT32x4_REGISTER, 16)
32 class LOperand : public ZoneObject {
50 LOperand() : value_(KindField::encode(INVALID)) { }
52 Kind kind() const { return KindField::decode(value_); }
53 int index() const { return static_cast<int>(value_) >> kKindFieldWidth; }
54 #define LITHIUM_OPERAND_PREDICATE(name, type, number) \
55 bool Is##name() const { return kind() == type; }
56 LITHIUM_OPERAND_LIST(LITHIUM_OPERAND_PREDICATE)
57 LITHIUM_OPERAND_PREDICATE(Unallocated, UNALLOCATED, 0)
58 LITHIUM_OPERAND_PREDICATE(Ignored, INVALID, 0)
59 #undef LITHIUM_OPERAND_PREDICATE
60 bool IsSIMD128Register() const {
61 return kind() == FLOAT32x4_REGISTER || kind() == FLOAT64x2_REGISTER ||
62 kind() == INT32x4_REGISTER;
64 bool IsSIMD128StackSlot() const {
65 return kind() == FLOAT32x4_STACK_SLOT || kind() == FLOAT64x2_STACK_SLOT ||
66 kind() == INT32x4_STACK_SLOT;
68 bool Equals(LOperand* other) const {
69 return value_ == other->value_ || (index() == other->index() &&
70 ((IsSIMD128Register() && other->IsSIMD128Register()) ||
71 (IsSIMD128StackSlot() && other->IsSIMD128StackSlot())));
74 void PrintTo(StringStream* stream);
75 void ConvertTo(Kind kind, int index) {
76 value_ = KindField::encode(kind);
77 value_ |= index << kKindFieldWidth;
78 ASSERT(this->index() == index);
81 // Calls SetUpCache()/TearDownCache() for each subclass.
82 static void SetUpCaches();
83 static void TearDownCaches();
86 static const int kKindFieldWidth = 4;
87 class KindField : public BitField<Kind, 0, kKindFieldWidth> { };
89 LOperand(Kind kind, int index) { ConvertTo(kind, index); }
95 class LUnallocated : public LOperand {
102 enum ExtendedPolicy {
106 FIXED_DOUBLE_REGISTER,
108 MUST_HAVE_DOUBLE_REGISTER,
113 // Lifetime of operand inside the instruction.
115 // USED_AT_START operand is guaranteed to be live only at
116 // instruction start. Register allocator is free to assign the same register
117 // to some other operand used inside instruction (i.e. temporary or
121 // USED_AT_END operand is treated as live until the end of
122 // instruction. This means that register allocator will not reuse it's
123 // register for any other operand inside instruction.
127 explicit LUnallocated(ExtendedPolicy policy) : LOperand(UNALLOCATED, 0) {
128 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
129 value_ |= ExtendedPolicyField::encode(policy);
130 value_ |= LifetimeField::encode(USED_AT_END);
133 LUnallocated(BasicPolicy policy, int index) : LOperand(UNALLOCATED, 0) {
134 ASSERT(policy == FIXED_SLOT);
135 value_ |= BasicPolicyField::encode(policy);
136 value_ |= index << FixedSlotIndexField::kShift;
137 ASSERT(this->fixed_slot_index() == index);
140 LUnallocated(ExtendedPolicy policy, int index) : LOperand(UNALLOCATED, 0) {
141 ASSERT(policy == FIXED_REGISTER || policy == FIXED_DOUBLE_REGISTER);
142 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
143 value_ |= ExtendedPolicyField::encode(policy);
144 value_ |= LifetimeField::encode(USED_AT_END);
145 value_ |= FixedRegisterField::encode(index);
148 LUnallocated(ExtendedPolicy policy, Lifetime lifetime)
149 : LOperand(UNALLOCATED, 0) {
150 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
151 value_ |= ExtendedPolicyField::encode(policy);
152 value_ |= LifetimeField::encode(lifetime);
155 LUnallocated* CopyUnconstrained(Zone* zone) {
156 LUnallocated* result = new(zone) LUnallocated(ANY);
157 result->set_virtual_register(virtual_register());
161 static LUnallocated* cast(LOperand* op) {
162 ASSERT(op->IsUnallocated());
163 return reinterpret_cast<LUnallocated*>(op);
166 // The encoding used for LUnallocated operands depends on the policy that is
167 // stored within the operand. The FIXED_SLOT policy uses a compact encoding
168 // because it accommodates a larger pay-load.
170 // For FIXED_SLOT policy:
171 // +-------------------------------------------+
172 // | slot_index | vreg | 0 | 0001 |
173 // +-------------------------------------------+
175 // For all other (extended) policies:
176 // +-------------------------------------------+
177 // | reg_index | L | PPP | vreg | 1 | 0001 | L ... Lifetime
178 // +-------------------------------------------+ P ... Policy
180 // The slot index is a signed value which requires us to decode it manually
181 // instead of using the BitField utility class.
183 // The superclass has a KindField.
184 STATIC_ASSERT(kKindFieldWidth == 4);
186 // BitFields for all unallocated operands.
187 class BasicPolicyField : public BitField<BasicPolicy, 4, 1> {};
188 class VirtualRegisterField : public BitField<unsigned, 5, 18> {};
190 // BitFields specific to BasicPolicy::FIXED_SLOT.
191 class FixedSlotIndexField : public BitField<int, 23, 9> {};
193 // BitFields specific to BasicPolicy::EXTENDED_POLICY.
194 class ExtendedPolicyField : public BitField<ExtendedPolicy, 23, 3> {};
195 class LifetimeField : public BitField<Lifetime, 26, 1> {};
196 class FixedRegisterField : public BitField<int, 27, 5> {};
198 static const int kMaxVirtualRegisters = VirtualRegisterField::kMax + 1;
199 static const int kFixedSlotIndexWidth = FixedSlotIndexField::kSize;
200 static const int kMaxFixedSlotIndex = (1 << (kFixedSlotIndexWidth - 1)) - 1;
201 static const int kMinFixedSlotIndex = -(1 << (kFixedSlotIndexWidth - 1));
203 // Predicates for the operand policy.
204 bool HasAnyPolicy() const {
205 return basic_policy() == EXTENDED_POLICY &&
206 extended_policy() == ANY;
208 bool HasFixedPolicy() const {
209 return basic_policy() == FIXED_SLOT ||
210 extended_policy() == FIXED_REGISTER ||
211 extended_policy() == FIXED_DOUBLE_REGISTER;
213 bool HasRegisterPolicy() const {
214 return basic_policy() == EXTENDED_POLICY && (
215 extended_policy() == WRITABLE_REGISTER ||
216 extended_policy() == MUST_HAVE_REGISTER);
218 bool HasDoubleRegisterPolicy() const {
219 return basic_policy() == EXTENDED_POLICY &&
220 extended_policy() == MUST_HAVE_DOUBLE_REGISTER;
222 bool HasSameAsInputPolicy() const {
223 return basic_policy() == EXTENDED_POLICY &&
224 extended_policy() == SAME_AS_FIRST_INPUT;
226 bool HasFixedSlotPolicy() const {
227 return basic_policy() == FIXED_SLOT;
229 bool HasFixedRegisterPolicy() const {
230 return basic_policy() == EXTENDED_POLICY &&
231 extended_policy() == FIXED_REGISTER;
233 bool HasFixedDoubleRegisterPolicy() const {
234 return basic_policy() == EXTENDED_POLICY &&
235 extended_policy() == FIXED_DOUBLE_REGISTER;
237 bool HasWritableRegisterPolicy() const {
238 return basic_policy() == EXTENDED_POLICY &&
239 extended_policy() == WRITABLE_REGISTER;
242 // [basic_policy]: Distinguish between FIXED_SLOT and all other policies.
243 BasicPolicy basic_policy() const {
244 return BasicPolicyField::decode(value_);
247 // [extended_policy]: Only for non-FIXED_SLOT. The finer-grained policy.
248 ExtendedPolicy extended_policy() const {
249 ASSERT(basic_policy() == EXTENDED_POLICY);
250 return ExtendedPolicyField::decode(value_);
253 // [fixed_slot_index]: Only for FIXED_SLOT.
254 int fixed_slot_index() const {
255 ASSERT(HasFixedSlotPolicy());
256 return static_cast<int>(value_) >> FixedSlotIndexField::kShift;
259 // [fixed_register_index]: Only for FIXED_REGISTER or FIXED_DOUBLE_REGISTER.
260 int fixed_register_index() const {
261 ASSERT(HasFixedRegisterPolicy() || HasFixedDoubleRegisterPolicy());
262 return FixedRegisterField::decode(value_);
265 // [virtual_register]: The virtual register ID for this operand.
266 int virtual_register() const {
267 return VirtualRegisterField::decode(value_);
269 void set_virtual_register(unsigned id) {
270 value_ = VirtualRegisterField::update(value_, id);
273 // [lifetime]: Only for non-FIXED_SLOT.
274 bool IsUsedAtStart() {
275 ASSERT(basic_policy() == EXTENDED_POLICY);
276 return LifetimeField::decode(value_) == USED_AT_START;
281 class LMoveOperands V8_FINAL BASE_EMBEDDED {
283 LMoveOperands(LOperand* source, LOperand* destination)
284 : source_(source), destination_(destination) {
287 LOperand* source() const { return source_; }
288 void set_source(LOperand* operand) { source_ = operand; }
290 LOperand* destination() const { return destination_; }
291 void set_destination(LOperand* operand) { destination_ = operand; }
293 // The gap resolver marks moves as "in-progress" by clearing the
294 // destination (but not the source).
295 bool IsPending() const {
296 return destination_ == NULL && source_ != NULL;
299 // True if this move a move into the given destination operand.
300 bool Blocks(LOperand* operand) const {
301 return !IsEliminated() && source()->Equals(operand);
304 // A move is redundant if it's been eliminated, if its source and
305 // destination are the same, or if its destination is unneeded.
306 bool IsRedundant() const {
307 return IsEliminated() || source_->Equals(destination_) || IsIgnored();
310 bool IsIgnored() const {
311 return destination_ != NULL && destination_->IsIgnored();
314 // We clear both operands to indicate move that's been eliminated.
315 void Eliminate() { source_ = destination_ = NULL; }
316 bool IsEliminated() const {
317 ASSERT(source_ != NULL || destination_ == NULL);
318 return source_ == NULL;
323 LOperand* destination_;
327 template<LOperand::Kind kOperandKind, int kNumCachedOperands>
328 class LSubKindOperand V8_FINAL : public LOperand {
330 static LSubKindOperand* Create(int index, Zone* zone) {
332 if (index < kNumCachedOperands) return &cache[index];
333 return new(zone) LSubKindOperand(index);
336 static LSubKindOperand* cast(LOperand* op) {
337 ASSERT(op->kind() == kOperandKind);
338 return reinterpret_cast<LSubKindOperand*>(op);
341 static void SetUpCache();
342 static void TearDownCache();
345 static LSubKindOperand* cache;
347 LSubKindOperand() : LOperand() { }
348 explicit LSubKindOperand(int index) : LOperand(kOperandKind, index) { }
352 #define LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS(name, type, number) \
353 typedef LSubKindOperand<LOperand::type, number> L##name;
354 LITHIUM_OPERAND_LIST(LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS)
355 #undef LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS
358 class LParallelMove V8_FINAL : public ZoneObject {
360 explicit LParallelMove(Zone* zone) : move_operands_(4, zone) { }
362 void AddMove(LOperand* from, LOperand* to, Zone* zone) {
363 move_operands_.Add(LMoveOperands(from, to), zone);
366 bool IsRedundant() const;
368 const ZoneList<LMoveOperands>* move_operands() const {
369 return &move_operands_;
372 void PrintDataTo(StringStream* stream) const;
375 ZoneList<LMoveOperands> move_operands_;
379 class LPointerMap V8_FINAL : public ZoneObject {
381 explicit LPointerMap(Zone* zone)
382 : pointer_operands_(8, zone),
383 untagged_operands_(0, zone),
384 lithium_position_(-1) { }
386 const ZoneList<LOperand*>* GetNormalizedOperands() {
387 for (int i = 0; i < untagged_operands_.length(); ++i) {
388 RemovePointer(untagged_operands_[i]);
390 untagged_operands_.Clear();
391 return &pointer_operands_;
393 int lithium_position() const { return lithium_position_; }
395 void set_lithium_position(int pos) {
396 ASSERT(lithium_position_ == -1);
397 lithium_position_ = pos;
400 void RecordPointer(LOperand* op, Zone* zone);
401 void RemovePointer(LOperand* op);
402 void RecordUntagged(LOperand* op, Zone* zone);
403 void PrintTo(StringStream* stream);
406 ZoneList<LOperand*> pointer_operands_;
407 ZoneList<LOperand*> untagged_operands_;
408 int lithium_position_;
412 class LEnvironment V8_FINAL : public ZoneObject {
414 LEnvironment(Handle<JSFunction> closure,
415 FrameType frame_type,
421 HEnterInlined* entry,
424 frame_type_(frame_type),
425 arguments_stack_height_(argument_count),
426 deoptimization_index_(Safepoint::kNoDeoptimizationIndex),
427 translation_index_(-1),
429 translation_size_(value_count),
430 parameter_count_(parameter_count),
432 values_(value_count, zone),
433 is_tagged_(value_count, zone),
434 is_uint32_(value_count, zone),
435 object_mapping_(0, zone),
439 has_been_used_(false) { }
441 Handle<JSFunction> closure() const { return closure_; }
442 FrameType frame_type() const { return frame_type_; }
443 int arguments_stack_height() const { return arguments_stack_height_; }
444 int deoptimization_index() const { return deoptimization_index_; }
445 int translation_index() const { return translation_index_; }
446 BailoutId ast_id() const { return ast_id_; }
447 int translation_size() const { return translation_size_; }
448 int parameter_count() const { return parameter_count_; }
449 int pc_offset() const { return pc_offset_; }
450 const ZoneList<LOperand*>* values() const { return &values_; }
451 LEnvironment* outer() const { return outer_; }
452 HEnterInlined* entry() { return entry_; }
453 Zone* zone() const { return zone_; }
455 bool has_been_used() const { return has_been_used_; }
456 void set_has_been_used() { has_been_used_ = true; }
458 void AddValue(LOperand* operand,
459 Representation representation,
461 values_.Add(operand, zone());
462 if (representation.IsSmiOrTagged()) {
464 is_tagged_.Add(values_.length() - 1, zone());
468 is_uint32_.Add(values_.length() - 1, zone());
472 bool HasTaggedValueAt(int index) const {
473 return is_tagged_.Contains(index);
476 bool HasUint32ValueAt(int index) const {
477 return is_uint32_.Contains(index);
480 void AddNewObject(int length, bool is_arguments) {
481 uint32_t encoded = LengthOrDupeField::encode(length) |
482 IsArgumentsField::encode(is_arguments) |
483 IsDuplicateField::encode(false);
484 object_mapping_.Add(encoded, zone());
487 void AddDuplicateObject(int dupe_of) {
488 uint32_t encoded = LengthOrDupeField::encode(dupe_of) |
489 IsDuplicateField::encode(true);
490 object_mapping_.Add(encoded, zone());
493 int ObjectDuplicateOfAt(int index) {
494 ASSERT(ObjectIsDuplicateAt(index));
495 return LengthOrDupeField::decode(object_mapping_[index]);
498 int ObjectLengthAt(int index) {
499 ASSERT(!ObjectIsDuplicateAt(index));
500 return LengthOrDupeField::decode(object_mapping_[index]);
503 bool ObjectIsArgumentsAt(int index) {
504 ASSERT(!ObjectIsDuplicateAt(index));
505 return IsArgumentsField::decode(object_mapping_[index]);
508 bool ObjectIsDuplicateAt(int index) {
509 return IsDuplicateField::decode(object_mapping_[index]);
512 void Register(int deoptimization_index,
513 int translation_index,
515 ASSERT(!HasBeenRegistered());
516 deoptimization_index_ = deoptimization_index;
517 translation_index_ = translation_index;
518 pc_offset_ = pc_offset;
520 bool HasBeenRegistered() const {
521 return deoptimization_index_ != Safepoint::kNoDeoptimizationIndex;
524 void PrintTo(StringStream* stream);
526 // Marker value indicating a de-materialized object.
527 static LOperand* materialization_marker() { return NULL; }
529 // Encoding used for the object_mapping map below.
530 class LengthOrDupeField : public BitField<int, 0, 30> { };
531 class IsArgumentsField : public BitField<bool, 30, 1> { };
532 class IsDuplicateField : public BitField<bool, 31, 1> { };
535 Handle<JSFunction> closure_;
536 FrameType frame_type_;
537 int arguments_stack_height_;
538 int deoptimization_index_;
539 int translation_index_;
541 int translation_size_;
542 int parameter_count_;
545 // Value array: [parameters] [locals] [expression stack] [de-materialized].
546 // |>--------- translation_size ---------<|
547 ZoneList<LOperand*> values_;
548 GrowableBitVector is_tagged_;
549 GrowableBitVector is_uint32_;
551 // Map with encoded information about materialization_marker operands.
552 ZoneList<uint32_t> object_mapping_;
554 LEnvironment* outer_;
555 HEnterInlined* entry_;
561 // Iterates over the non-null, non-constant operands in an environment.
562 class ShallowIterator V8_FINAL BASE_EMBEDDED {
564 explicit ShallowIterator(LEnvironment* env)
566 limit_(env != NULL ? env->values()->length() : 0),
571 bool Done() { return current_ >= limit_; }
573 LOperand* Current() {
575 ASSERT(env_->values()->at(current_) != NULL);
576 return env_->values()->at(current_);
585 LEnvironment* env() { return env_; }
588 bool ShouldSkip(LOperand* op) {
589 return op == NULL || op->IsConstantOperand();
592 // Skip until something interesting, beginning with and including current_.
593 void SkipUninteresting() {
594 while (current_ < limit_ && ShouldSkip(env_->values()->at(current_))) {
605 // Iterator for non-null, non-constant operands incl. outer environments.
606 class DeepIterator V8_FINAL BASE_EMBEDDED {
608 explicit DeepIterator(LEnvironment* env)
609 : current_iterator_(env) {
613 bool Done() { return current_iterator_.Done(); }
615 LOperand* Current() {
616 ASSERT(!current_iterator_.Done());
617 ASSERT(current_iterator_.Current() != NULL);
618 return current_iterator_.Current();
622 current_iterator_.Advance();
627 void SkipUninteresting() {
628 while (current_iterator_.env() != NULL && current_iterator_.Done()) {
629 current_iterator_ = ShallowIterator(current_iterator_.env()->outer());
633 ShallowIterator current_iterator_;
637 class LPlatformChunk;
641 // Superclass providing data and behavior common to all the
642 // arch-specific LPlatformChunk classes.
643 class LChunk : public ZoneObject {
645 static LChunk* NewChunk(HGraph* graph);
647 void AddInstruction(LInstruction* instruction, HBasicBlock* block);
648 LConstantOperand* DefineConstantOperand(HConstant* constant);
649 HConstant* LookupConstant(LConstantOperand* operand) const;
650 Representation LookupLiteralRepresentation(LConstantOperand* operand) const;
652 int ParameterAt(int index);
653 int GetParameterStackSlot(int index) const;
654 int spill_slot_count() const { return spill_slot_count_; }
655 CompilationInfo* info() const { return info_; }
656 HGraph* graph() const { return graph_; }
657 Isolate* isolate() const { return graph_->isolate(); }
658 const ZoneList<LInstruction*>* instructions() const { return &instructions_; }
659 void AddGapMove(int index, LOperand* from, LOperand* to);
660 LGap* GetGapAt(int index) const;
661 bool IsGapAt(int index) const;
662 int NearestGapPos(int index) const;
663 void MarkEmptyBlocks();
664 const ZoneList<LPointerMap*>* pointer_maps() const { return &pointer_maps_; }
665 LLabel* GetLabel(int block_id) const;
666 int LookupDestination(int block_id) const;
667 Label* GetAssemblyLabel(int block_id) const;
669 const ZoneList<Handle<JSFunction> >* inlined_closures() const {
670 return &inlined_closures_;
673 void AddInlinedClosure(Handle<JSFunction> closure) {
674 inlined_closures_.Add(closure, zone());
677 void AddDeprecationDependency(Handle<Map> map) {
678 ASSERT(!map->is_deprecated());
679 if (!map->CanBeDeprecated()) return;
680 ASSERT(!info_->IsStub());
681 deprecation_dependencies_.insert(map);
684 void AddStabilityDependency(Handle<Map> map) {
685 ASSERT(map->is_stable());
686 if (!map->CanTransition()) return;
687 ASSERT(!info_->IsStub());
688 stability_dependencies_.insert(map);
691 Zone* zone() const { return info_->zone(); }
693 Handle<Code> Codegen();
695 void set_allocated_double_registers(BitVector* allocated_registers);
696 BitVector* allocated_double_registers() {
697 return allocated_double_registers_;
701 LChunk(CompilationInfo* info, HGraph* graph);
703 int spill_slot_count_;
706 typedef std::less<Handle<Map> > MapLess;
707 typedef zone_allocator<Handle<Map> > MapAllocator;
708 typedef std::set<Handle<Map>, MapLess, MapAllocator> MapSet;
710 void CommitDependencies(Handle<Code> code) const;
712 CompilationInfo* info_;
713 HGraph* const graph_;
714 BitVector* allocated_double_registers_;
715 ZoneList<LInstruction*> instructions_;
716 ZoneList<LPointerMap*> pointer_maps_;
717 ZoneList<Handle<JSFunction> > inlined_closures_;
718 MapSet deprecation_dependencies_;
719 MapSet stability_dependencies_;
723 class LChunkBuilderBase BASE_EMBEDDED {
725 explicit LChunkBuilderBase(Zone* zone)
726 : argument_count_(0),
729 virtual ~LChunkBuilderBase() { }
732 // An input operand in register, stack slot or a constant operand.
733 // Will not be moved to a register even if one is freely available.
734 virtual MUST_USE_RESULT LOperand* UseAny(HValue* value) = 0;
736 LEnvironment* CreateEnvironment(HEnvironment* hydrogen_env,
737 int* argument_index_accumulator,
738 ZoneList<HValue*>* objects_to_materialize);
739 void AddObjectToMaterialize(HValue* value,
740 ZoneList<HValue*>* objects_to_materialize,
741 LEnvironment* result);
743 Zone* zone() const { return zone_; }
752 int StackSlotOffset(int index);
754 enum NumberUntagDMode {
755 NUMBER_CANDIDATE_IS_SMI,
756 NUMBER_CANDIDATE_IS_ANY_TAGGED
760 class LPhase : public CompilationPhase {
762 LPhase(const char* name, LChunk* chunk)
763 : CompilationPhase(name, chunk->info()),
770 DISALLOW_COPY_AND_ASSIGN(LPhase);
774 } } // namespace v8::internal
776 #endif // V8_LITHIUM_H_