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 "allocation.h"
12 #include "safepoint-table.h"
13 #include "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,
112 // Lifetime of operand inside the instruction.
114 // USED_AT_START operand is guaranteed to be live only at
115 // instruction start. Register allocator is free to assign the same register
116 // to some other operand used inside instruction (i.e. temporary or
120 // USED_AT_END operand is treated as live until the end of
121 // instruction. This means that register allocator will not reuse it's
122 // register for any other operand inside instruction.
126 explicit LUnallocated(ExtendedPolicy policy) : LOperand(UNALLOCATED, 0) {
127 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
128 value_ |= ExtendedPolicyField::encode(policy);
129 value_ |= LifetimeField::encode(USED_AT_END);
132 LUnallocated(BasicPolicy policy, int index) : LOperand(UNALLOCATED, 0) {
133 ASSERT(policy == FIXED_SLOT);
134 value_ |= BasicPolicyField::encode(policy);
135 value_ |= index << FixedSlotIndexField::kShift;
136 ASSERT(this->fixed_slot_index() == index);
139 LUnallocated(ExtendedPolicy policy, int index) : LOperand(UNALLOCATED, 0) {
140 ASSERT(policy == FIXED_REGISTER || policy == FIXED_DOUBLE_REGISTER);
141 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
142 value_ |= ExtendedPolicyField::encode(policy);
143 value_ |= LifetimeField::encode(USED_AT_END);
144 value_ |= FixedRegisterField::encode(index);
147 LUnallocated(ExtendedPolicy policy, Lifetime lifetime)
148 : LOperand(UNALLOCATED, 0) {
149 value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
150 value_ |= ExtendedPolicyField::encode(policy);
151 value_ |= LifetimeField::encode(lifetime);
154 LUnallocated* CopyUnconstrained(Zone* zone) {
155 LUnallocated* result = new(zone) LUnallocated(ANY);
156 result->set_virtual_register(virtual_register());
160 static LUnallocated* cast(LOperand* op) {
161 ASSERT(op->IsUnallocated());
162 return reinterpret_cast<LUnallocated*>(op);
165 // The encoding used for LUnallocated operands depends on the policy that is
166 // stored within the operand. The FIXED_SLOT policy uses a compact encoding
167 // because it accommodates a larger pay-load.
169 // For FIXED_SLOT policy:
170 // +-------------------------------------------+
171 // | slot_index | vreg | 0 | 0001 |
172 // +-------------------------------------------+
174 // For all other (extended) policies:
175 // +-------------------------------------------+
176 // | reg_index | L | PPP | vreg | 1 | 0001 | L ... Lifetime
177 // +-------------------------------------------+ P ... Policy
179 // The slot index is a signed value which requires us to decode it manually
180 // instead of using the BitField utility class.
182 // The superclass has a KindField.
183 STATIC_ASSERT(kKindFieldWidth == 4);
185 // BitFields for all unallocated operands.
186 class BasicPolicyField : public BitField<BasicPolicy, 4, 1> {};
187 class VirtualRegisterField : public BitField<unsigned, 5, 18> {};
189 // BitFields specific to BasicPolicy::FIXED_SLOT.
190 class FixedSlotIndexField : public BitField<int, 23, 9> {};
192 // BitFields specific to BasicPolicy::EXTENDED_POLICY.
193 class ExtendedPolicyField : public BitField<ExtendedPolicy, 23, 3> {};
194 class LifetimeField : public BitField<Lifetime, 26, 1> {};
195 class FixedRegisterField : public BitField<int, 27, 5> {};
197 static const int kMaxVirtualRegisters = VirtualRegisterField::kMax + 1;
198 static const int kFixedSlotIndexWidth = FixedSlotIndexField::kSize;
199 static const int kMaxFixedSlotIndex = (1 << (kFixedSlotIndexWidth - 1)) - 1;
200 static const int kMinFixedSlotIndex = -(1 << (kFixedSlotIndexWidth - 1));
202 // Predicates for the operand policy.
203 bool HasAnyPolicy() const {
204 return basic_policy() == EXTENDED_POLICY &&
205 extended_policy() == ANY;
207 bool HasFixedPolicy() const {
208 return basic_policy() == FIXED_SLOT ||
209 extended_policy() == FIXED_REGISTER ||
210 extended_policy() == FIXED_DOUBLE_REGISTER;
212 bool HasRegisterPolicy() const {
213 return basic_policy() == EXTENDED_POLICY && (
214 extended_policy() == WRITABLE_REGISTER ||
215 extended_policy() == MUST_HAVE_REGISTER);
217 bool HasSameAsInputPolicy() const {
218 return basic_policy() == EXTENDED_POLICY &&
219 extended_policy() == SAME_AS_FIRST_INPUT;
221 bool HasFixedSlotPolicy() const {
222 return basic_policy() == FIXED_SLOT;
224 bool HasFixedRegisterPolicy() const {
225 return basic_policy() == EXTENDED_POLICY &&
226 extended_policy() == FIXED_REGISTER;
228 bool HasFixedDoubleRegisterPolicy() const {
229 return basic_policy() == EXTENDED_POLICY &&
230 extended_policy() == FIXED_DOUBLE_REGISTER;
232 bool HasWritableRegisterPolicy() const {
233 return basic_policy() == EXTENDED_POLICY &&
234 extended_policy() == WRITABLE_REGISTER;
237 // [basic_policy]: Distinguish between FIXED_SLOT and all other policies.
238 BasicPolicy basic_policy() const {
239 return BasicPolicyField::decode(value_);
242 // [extended_policy]: Only for non-FIXED_SLOT. The finer-grained policy.
243 ExtendedPolicy extended_policy() const {
244 ASSERT(basic_policy() == EXTENDED_POLICY);
245 return ExtendedPolicyField::decode(value_);
248 // [fixed_slot_index]: Only for FIXED_SLOT.
249 int fixed_slot_index() const {
250 ASSERT(HasFixedSlotPolicy());
251 return static_cast<int>(value_) >> FixedSlotIndexField::kShift;
254 // [fixed_register_index]: Only for FIXED_REGISTER or FIXED_DOUBLE_REGISTER.
255 int fixed_register_index() const {
256 ASSERT(HasFixedRegisterPolicy() || HasFixedDoubleRegisterPolicy());
257 return FixedRegisterField::decode(value_);
260 // [virtual_register]: The virtual register ID for this operand.
261 int virtual_register() const {
262 return VirtualRegisterField::decode(value_);
264 void set_virtual_register(unsigned id) {
265 value_ = VirtualRegisterField::update(value_, id);
268 // [lifetime]: Only for non-FIXED_SLOT.
269 bool IsUsedAtStart() {
270 ASSERT(basic_policy() == EXTENDED_POLICY);
271 return LifetimeField::decode(value_) == USED_AT_START;
276 class LMoveOperands V8_FINAL BASE_EMBEDDED {
278 LMoveOperands(LOperand* source, LOperand* destination)
279 : source_(source), destination_(destination) {
282 LOperand* source() const { return source_; }
283 void set_source(LOperand* operand) { source_ = operand; }
285 LOperand* destination() const { return destination_; }
286 void set_destination(LOperand* operand) { destination_ = operand; }
288 // The gap resolver marks moves as "in-progress" by clearing the
289 // destination (but not the source).
290 bool IsPending() const {
291 return destination_ == NULL && source_ != NULL;
294 // True if this move a move into the given destination operand.
295 bool Blocks(LOperand* operand) const {
296 return !IsEliminated() && source()->Equals(operand);
299 // A move is redundant if it's been eliminated, if its source and
300 // destination are the same, or if its destination is unneeded.
301 bool IsRedundant() const {
302 return IsEliminated() || source_->Equals(destination_) || IsIgnored();
305 bool IsIgnored() const {
306 return destination_ != NULL && destination_->IsIgnored();
309 // We clear both operands to indicate move that's been eliminated.
310 void Eliminate() { source_ = destination_ = NULL; }
311 bool IsEliminated() const {
312 ASSERT(source_ != NULL || destination_ == NULL);
313 return source_ == NULL;
318 LOperand* destination_;
322 template<LOperand::Kind kOperandKind, int kNumCachedOperands>
323 class LSubKindOperand V8_FINAL : public LOperand {
325 static LSubKindOperand* Create(int index, Zone* zone) {
327 if (index < kNumCachedOperands) return &cache[index];
328 return new(zone) LSubKindOperand(index);
331 static LSubKindOperand* cast(LOperand* op) {
332 ASSERT(op->kind() == kOperandKind);
333 return reinterpret_cast<LSubKindOperand*>(op);
336 static void SetUpCache();
337 static void TearDownCache();
340 static LSubKindOperand* cache;
342 LSubKindOperand() : LOperand() { }
343 explicit LSubKindOperand(int index) : LOperand(kOperandKind, index) { }
347 #define LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS(name, type, number) \
348 typedef LSubKindOperand<LOperand::type, number> L##name;
349 LITHIUM_OPERAND_LIST(LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS)
350 #undef LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS
353 class LParallelMove V8_FINAL : public ZoneObject {
355 explicit LParallelMove(Zone* zone) : move_operands_(4, zone) { }
357 void AddMove(LOperand* from, LOperand* to, Zone* zone) {
358 move_operands_.Add(LMoveOperands(from, to), zone);
361 bool IsRedundant() const;
363 const ZoneList<LMoveOperands>* move_operands() const {
364 return &move_operands_;
367 void PrintDataTo(StringStream* stream) const;
370 ZoneList<LMoveOperands> move_operands_;
374 class LPointerMap V8_FINAL : public ZoneObject {
376 explicit LPointerMap(Zone* zone)
377 : pointer_operands_(8, zone),
378 untagged_operands_(0, zone),
379 lithium_position_(-1) { }
381 const ZoneList<LOperand*>* GetNormalizedOperands() {
382 for (int i = 0; i < untagged_operands_.length(); ++i) {
383 RemovePointer(untagged_operands_[i]);
385 untagged_operands_.Clear();
386 return &pointer_operands_;
388 int lithium_position() const { return lithium_position_; }
390 void set_lithium_position(int pos) {
391 ASSERT(lithium_position_ == -1);
392 lithium_position_ = pos;
395 void RecordPointer(LOperand* op, Zone* zone);
396 void RemovePointer(LOperand* op);
397 void RecordUntagged(LOperand* op, Zone* zone);
398 void PrintTo(StringStream* stream);
401 ZoneList<LOperand*> pointer_operands_;
402 ZoneList<LOperand*> untagged_operands_;
403 int lithium_position_;
407 class LEnvironment V8_FINAL : public ZoneObject {
409 LEnvironment(Handle<JSFunction> closure,
410 FrameType frame_type,
416 HEnterInlined* entry,
419 frame_type_(frame_type),
420 arguments_stack_height_(argument_count),
421 deoptimization_index_(Safepoint::kNoDeoptimizationIndex),
422 translation_index_(-1),
424 translation_size_(value_count),
425 parameter_count_(parameter_count),
427 values_(value_count, zone),
428 is_tagged_(value_count, zone),
429 is_uint32_(value_count, zone),
430 object_mapping_(0, zone),
434 has_been_used_(false) { }
436 Handle<JSFunction> closure() const { return closure_; }
437 FrameType frame_type() const { return frame_type_; }
438 int arguments_stack_height() const { return arguments_stack_height_; }
439 int deoptimization_index() const { return deoptimization_index_; }
440 int translation_index() const { return translation_index_; }
441 BailoutId ast_id() const { return ast_id_; }
442 int translation_size() const { return translation_size_; }
443 int parameter_count() const { return parameter_count_; }
444 int pc_offset() const { return pc_offset_; }
445 const ZoneList<LOperand*>* values() const { return &values_; }
446 LEnvironment* outer() const { return outer_; }
447 HEnterInlined* entry() { return entry_; }
448 Zone* zone() const { return zone_; }
450 bool has_been_used() const { return has_been_used_; }
451 void set_has_been_used() { has_been_used_ = true; }
453 void AddValue(LOperand* operand,
454 Representation representation,
456 values_.Add(operand, zone());
457 if (representation.IsSmiOrTagged()) {
459 is_tagged_.Add(values_.length() - 1, zone());
463 is_uint32_.Add(values_.length() - 1, zone());
467 bool HasTaggedValueAt(int index) const {
468 return is_tagged_.Contains(index);
471 bool HasUint32ValueAt(int index) const {
472 return is_uint32_.Contains(index);
475 void AddNewObject(int length, bool is_arguments) {
476 uint32_t encoded = LengthOrDupeField::encode(length) |
477 IsArgumentsField::encode(is_arguments) |
478 IsDuplicateField::encode(false);
479 object_mapping_.Add(encoded, zone());
482 void AddDuplicateObject(int dupe_of) {
483 uint32_t encoded = LengthOrDupeField::encode(dupe_of) |
484 IsDuplicateField::encode(true);
485 object_mapping_.Add(encoded, zone());
488 int ObjectDuplicateOfAt(int index) {
489 ASSERT(ObjectIsDuplicateAt(index));
490 return LengthOrDupeField::decode(object_mapping_[index]);
493 int ObjectLengthAt(int index) {
494 ASSERT(!ObjectIsDuplicateAt(index));
495 return LengthOrDupeField::decode(object_mapping_[index]);
498 bool ObjectIsArgumentsAt(int index) {
499 ASSERT(!ObjectIsDuplicateAt(index));
500 return IsArgumentsField::decode(object_mapping_[index]);
503 bool ObjectIsDuplicateAt(int index) {
504 return IsDuplicateField::decode(object_mapping_[index]);
507 void Register(int deoptimization_index,
508 int translation_index,
510 ASSERT(!HasBeenRegistered());
511 deoptimization_index_ = deoptimization_index;
512 translation_index_ = translation_index;
513 pc_offset_ = pc_offset;
515 bool HasBeenRegistered() const {
516 return deoptimization_index_ != Safepoint::kNoDeoptimizationIndex;
519 void PrintTo(StringStream* stream);
521 // Marker value indicating a de-materialized object.
522 static LOperand* materialization_marker() { return NULL; }
524 // Encoding used for the object_mapping map below.
525 class LengthOrDupeField : public BitField<int, 0, 30> { };
526 class IsArgumentsField : public BitField<bool, 30, 1> { };
527 class IsDuplicateField : public BitField<bool, 31, 1> { };
530 Handle<JSFunction> closure_;
531 FrameType frame_type_;
532 int arguments_stack_height_;
533 int deoptimization_index_;
534 int translation_index_;
536 int translation_size_;
537 int parameter_count_;
540 // Value array: [parameters] [locals] [expression stack] [de-materialized].
541 // |>--------- translation_size ---------<|
542 ZoneList<LOperand*> values_;
543 GrowableBitVector is_tagged_;
544 GrowableBitVector is_uint32_;
546 // Map with encoded information about materialization_marker operands.
547 ZoneList<uint32_t> object_mapping_;
549 LEnvironment* outer_;
550 HEnterInlined* entry_;
556 // Iterates over the non-null, non-constant operands in an environment.
557 class ShallowIterator V8_FINAL BASE_EMBEDDED {
559 explicit ShallowIterator(LEnvironment* env)
561 limit_(env != NULL ? env->values()->length() : 0),
566 bool Done() { return current_ >= limit_; }
568 LOperand* Current() {
570 ASSERT(env_->values()->at(current_) != NULL);
571 return env_->values()->at(current_);
580 LEnvironment* env() { return env_; }
583 bool ShouldSkip(LOperand* op) {
584 return op == NULL || op->IsConstantOperand();
587 // Skip until something interesting, beginning with and including current_.
588 void SkipUninteresting() {
589 while (current_ < limit_ && ShouldSkip(env_->values()->at(current_))) {
600 // Iterator for non-null, non-constant operands incl. outer environments.
601 class DeepIterator V8_FINAL BASE_EMBEDDED {
603 explicit DeepIterator(LEnvironment* env)
604 : current_iterator_(env) {
608 bool Done() { return current_iterator_.Done(); }
610 LOperand* Current() {
611 ASSERT(!current_iterator_.Done());
612 ASSERT(current_iterator_.Current() != NULL);
613 return current_iterator_.Current();
617 current_iterator_.Advance();
622 void SkipUninteresting() {
623 while (current_iterator_.env() != NULL && current_iterator_.Done()) {
624 current_iterator_ = ShallowIterator(current_iterator_.env()->outer());
628 ShallowIterator current_iterator_;
632 class LPlatformChunk;
636 // Superclass providing data and behavior common to all the
637 // arch-specific LPlatformChunk classes.
638 class LChunk : public ZoneObject {
640 static LChunk* NewChunk(HGraph* graph);
642 void AddInstruction(LInstruction* instruction, HBasicBlock* block);
643 LConstantOperand* DefineConstantOperand(HConstant* constant);
644 HConstant* LookupConstant(LConstantOperand* operand) const;
645 Representation LookupLiteralRepresentation(LConstantOperand* operand) const;
647 int ParameterAt(int index);
648 int GetParameterStackSlot(int index) const;
649 int spill_slot_count() const { return spill_slot_count_; }
650 CompilationInfo* info() const { return info_; }
651 HGraph* graph() const { return graph_; }
652 Isolate* isolate() const { return graph_->isolate(); }
653 const ZoneList<LInstruction*>* instructions() const { return &instructions_; }
654 void AddGapMove(int index, LOperand* from, LOperand* to);
655 LGap* GetGapAt(int index) const;
656 bool IsGapAt(int index) const;
657 int NearestGapPos(int index) const;
658 void MarkEmptyBlocks();
659 const ZoneList<LPointerMap*>* pointer_maps() const { return &pointer_maps_; }
660 LLabel* GetLabel(int block_id) const;
661 int LookupDestination(int block_id) const;
662 Label* GetAssemblyLabel(int block_id) const;
664 const ZoneList<Handle<JSFunction> >* inlined_closures() const {
665 return &inlined_closures_;
668 void AddInlinedClosure(Handle<JSFunction> closure) {
669 inlined_closures_.Add(closure, zone());
672 void AddDeprecationDependency(Handle<Map> map) {
673 ASSERT(!map->is_deprecated());
674 if (!map->CanBeDeprecated()) return;
675 ASSERT(!info_->IsStub());
676 deprecation_dependencies_.insert(map);
679 void AddStabilityDependency(Handle<Map> map) {
680 ASSERT(map->is_stable());
681 if (!map->CanTransition()) return;
682 ASSERT(!info_->IsStub());
683 stability_dependencies_.insert(map);
686 Zone* zone() const { return info_->zone(); }
688 Handle<Code> Codegen();
690 void set_allocated_double_registers(BitVector* allocated_registers);
691 BitVector* allocated_double_registers() {
692 return allocated_double_registers_;
696 LChunk(CompilationInfo* info, HGraph* graph);
698 int spill_slot_count_;
701 typedef std::less<Handle<Map> > MapLess;
702 typedef zone_allocator<Handle<Map> > MapAllocator;
703 typedef std::set<Handle<Map>, MapLess, MapAllocator> MapSet;
705 void CommitDependencies(Handle<Code> code) const;
707 CompilationInfo* info_;
708 HGraph* const graph_;
709 BitVector* allocated_double_registers_;
710 ZoneList<LInstruction*> instructions_;
711 ZoneList<LPointerMap*> pointer_maps_;
712 ZoneList<Handle<JSFunction> > inlined_closures_;
713 MapSet deprecation_dependencies_;
714 MapSet stability_dependencies_;
718 class LChunkBuilderBase BASE_EMBEDDED {
720 explicit LChunkBuilderBase(Zone* zone)
721 : argument_count_(0),
724 virtual ~LChunkBuilderBase() { }
727 // An input operand in register, stack slot or a constant operand.
728 // Will not be moved to a register even if one is freely available.
729 virtual MUST_USE_RESULT LOperand* UseAny(HValue* value) = 0;
731 LEnvironment* CreateEnvironment(HEnvironment* hydrogen_env,
732 int* argument_index_accumulator,
733 ZoneList<HValue*>* objects_to_materialize);
734 void AddObjectToMaterialize(HValue* value,
735 ZoneList<HValue*>* objects_to_materialize,
736 LEnvironment* result);
738 Zone* zone() const { return zone_; }
747 int StackSlotOffset(int index);
749 enum NumberUntagDMode {
750 NUMBER_CANDIDATE_IS_SMI,
751 NUMBER_CANDIDATE_IS_ANY_TAGGED
755 class LPhase : public CompilationPhase {
757 LPhase(const char* name, LChunk* chunk)
758 : CompilationPhase(name, chunk->info()),
765 DISALLOW_COPY_AND_ASSIGN(LPhase);
769 } } // namespace v8::internal
771 #endif // V8_LITHIUM_H_