Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / v8 / src / lithium.h
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.
4
5 #ifndef V8_LITHIUM_H_
6 #define V8_LITHIUM_H_
7
8 #include <set>
9
10 #include "src/allocation.h"
11 #include "src/bailout-reason.h"
12 #include "src/hydrogen.h"
13 #include "src/safepoint-table.h"
14 #include "src/zone-allocator.h"
15
16 namespace v8 {
17 namespace internal {
18
19 #define LITHIUM_OPERAND_LIST(V)                     \
20   V(ConstantOperand,    CONSTANT_OPERAND,     128)  \
21   V(StackSlot,          STACK_SLOT,           128)  \
22   V(DoubleStackSlot,    DOUBLE_STACK_SLOT,    128)  \
23   V(Float32x4StackSlot, FLOAT32x4_STACK_SLOT, 128)  \
24   V(Float64x2StackSlot, FLOAT64x2_STACK_SLOT, 128)  \
25   V(Int32x4StackSlot,   INT32x4_STACK_SLOT,   128)  \
26   V(Register,           REGISTER,             16)   \
27   V(DoubleRegister,     DOUBLE_REGISTER,      16)   \
28   V(Float32x4Register,  FLOAT32x4_REGISTER,   16)   \
29   V(Float64x2Register,  FLOAT64x2_REGISTER,   16)   \
30   V(Int32x4Register,    INT32x4_REGISTER,     16)
31
32 class LOperand : public ZoneObject {
33  public:
34   enum Kind {
35     INVALID,
36     UNALLOCATED,
37     CONSTANT_OPERAND,
38     STACK_SLOT,
39     DOUBLE_STACK_SLOT,
40     FLOAT32x4_STACK_SLOT,
41     FLOAT64x2_STACK_SLOT,
42     INT32x4_STACK_SLOT,
43     REGISTER,
44     DOUBLE_REGISTER,
45     FLOAT32x4_REGISTER,
46     FLOAT64x2_REGISTER,
47     INT32x4_REGISTER
48   };
49
50   LOperand() : value_(KindField::encode(INVALID)) { }
51
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;
63   }
64   bool IsSIMD128StackSlot() const {
65     return kind() == FLOAT32x4_STACK_SLOT || kind() == FLOAT64x2_STACK_SLOT ||
66            kind() == INT32x4_STACK_SLOT;
67   }
68   bool Equals(LOperand* other) const {
69     return value_ == other->value_ || (index() == other->index() &&
70         ((IsSIMD128Register() && other->IsSIMD128Register()) ||
71          (IsSIMD128StackSlot() && other->IsSIMD128StackSlot())));
72   }
73
74   void PrintTo(StringStream* stream);
75   void ConvertTo(Kind kind, int index) {
76     if (kind == REGISTER) DCHECK(index >= 0);
77     value_ = KindField::encode(kind);
78     value_ |= index << kKindFieldWidth;
79     DCHECK(this->index() == index);
80   }
81
82   // Calls SetUpCache()/TearDownCache() for each subclass.
83   static void SetUpCaches();
84   static void TearDownCaches();
85
86  protected:
87   static const int kKindFieldWidth = 4;
88   class KindField : public BitField<Kind, 0, kKindFieldWidth> { };
89
90   LOperand(Kind kind, int index) { ConvertTo(kind, index); }
91
92   unsigned value_;
93 };
94
95
96 class LUnallocated : public LOperand {
97  public:
98   enum BasicPolicy {
99     FIXED_SLOT,
100     EXTENDED_POLICY
101   };
102
103   enum ExtendedPolicy {
104     NONE,
105     ANY,
106     FIXED_REGISTER,
107     FIXED_DOUBLE_REGISTER,
108     MUST_HAVE_REGISTER,
109     MUST_HAVE_DOUBLE_REGISTER,
110     WRITABLE_REGISTER,
111     SAME_AS_FIRST_INPUT
112   };
113
114   // Lifetime of operand inside the instruction.
115   enum Lifetime {
116     // USED_AT_START operand is guaranteed to be live only at
117     // instruction start. Register allocator is free to assign the same register
118     // to some other operand used inside instruction (i.e. temporary or
119     // output).
120     USED_AT_START,
121
122     // USED_AT_END operand is treated as live until the end of
123     // instruction. This means that register allocator will not reuse it's
124     // register for any other operand inside instruction.
125     USED_AT_END
126   };
127
128   explicit LUnallocated(ExtendedPolicy policy) : LOperand(UNALLOCATED, 0) {
129     value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
130     value_ |= ExtendedPolicyField::encode(policy);
131     value_ |= LifetimeField::encode(USED_AT_END);
132   }
133
134   LUnallocated(BasicPolicy policy, int index) : LOperand(UNALLOCATED, 0) {
135     DCHECK(policy == FIXED_SLOT);
136     value_ |= BasicPolicyField::encode(policy);
137     value_ |= index << FixedSlotIndexField::kShift;
138     DCHECK(this->fixed_slot_index() == index);
139   }
140
141   LUnallocated(ExtendedPolicy policy, int index) : LOperand(UNALLOCATED, 0) {
142     DCHECK(policy == FIXED_REGISTER || policy == FIXED_DOUBLE_REGISTER);
143     value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
144     value_ |= ExtendedPolicyField::encode(policy);
145     value_ |= LifetimeField::encode(USED_AT_END);
146     value_ |= FixedRegisterField::encode(index);
147   }
148
149   LUnallocated(ExtendedPolicy policy, Lifetime lifetime)
150       : LOperand(UNALLOCATED, 0) {
151     value_ |= BasicPolicyField::encode(EXTENDED_POLICY);
152     value_ |= ExtendedPolicyField::encode(policy);
153     value_ |= LifetimeField::encode(lifetime);
154   }
155
156   LUnallocated* CopyUnconstrained(Zone* zone) {
157     LUnallocated* result = new(zone) LUnallocated(ANY);
158     result->set_virtual_register(virtual_register());
159     return result;
160   }
161
162   static LUnallocated* cast(LOperand* op) {
163     DCHECK(op->IsUnallocated());
164     return reinterpret_cast<LUnallocated*>(op);
165   }
166
167   // The encoding used for LUnallocated operands depends on the policy that is
168   // stored within the operand. The FIXED_SLOT policy uses a compact encoding
169   // because it accommodates a larger pay-load.
170   //
171   // For FIXED_SLOT policy:
172   //     +-------------------------------------------+
173   //     |       slot_index      |  vreg  | 0 | 0001 |
174   //     +-------------------------------------------+
175   //
176   // For all other (extended) policies:
177   //     +-------------------------------------------+
178   //     |  reg_index  | L | PPP |  vreg  | 1 | 0001 |    L ... Lifetime
179   //     +-------------------------------------------+    P ... Policy
180   //
181   // The slot index is a signed value which requires us to decode it manually
182   // instead of using the BitField utility class.
183
184   // The superclass has a KindField.
185   STATIC_ASSERT(kKindFieldWidth == 4);
186
187   // BitFields for all unallocated operands.
188   class BasicPolicyField     : public BitField<BasicPolicy,     4,  1> {};
189   class VirtualRegisterField : public BitField<unsigned,        5, 18> {};
190
191   // BitFields specific to BasicPolicy::FIXED_SLOT.
192   class FixedSlotIndexField  : public BitField<int,            23,  9> {};
193
194   // BitFields specific to BasicPolicy::EXTENDED_POLICY.
195   class ExtendedPolicyField  : public BitField<ExtendedPolicy, 23,  3> {};
196   class LifetimeField        : public BitField<Lifetime,       26,  1> {};
197   class FixedRegisterField   : public BitField<int,            27,  5> {};
198
199   static const int kMaxVirtualRegisters = VirtualRegisterField::kMax + 1;
200   static const int kFixedSlotIndexWidth = FixedSlotIndexField::kSize;
201   static const int kMaxFixedSlotIndex = (1 << (kFixedSlotIndexWidth - 1)) - 1;
202   static const int kMinFixedSlotIndex = -(1 << (kFixedSlotIndexWidth - 1));
203
204   // Predicates for the operand policy.
205   bool HasAnyPolicy() const {
206     return basic_policy() == EXTENDED_POLICY &&
207         extended_policy() == ANY;
208   }
209   bool HasFixedPolicy() const {
210     return basic_policy() == FIXED_SLOT ||
211         extended_policy() == FIXED_REGISTER ||
212         extended_policy() == FIXED_DOUBLE_REGISTER;
213   }
214   bool HasRegisterPolicy() const {
215     return basic_policy() == EXTENDED_POLICY && (
216         extended_policy() == WRITABLE_REGISTER ||
217         extended_policy() == MUST_HAVE_REGISTER);
218   }
219   bool HasDoubleRegisterPolicy() const {
220     return basic_policy() == EXTENDED_POLICY &&
221         extended_policy() == MUST_HAVE_DOUBLE_REGISTER;
222   }
223   bool HasSameAsInputPolicy() const {
224     return basic_policy() == EXTENDED_POLICY &&
225         extended_policy() == SAME_AS_FIRST_INPUT;
226   }
227   bool HasFixedSlotPolicy() const {
228     return basic_policy() == FIXED_SLOT;
229   }
230   bool HasFixedRegisterPolicy() const {
231     return basic_policy() == EXTENDED_POLICY &&
232         extended_policy() == FIXED_REGISTER;
233   }
234   bool HasFixedDoubleRegisterPolicy() const {
235     return basic_policy() == EXTENDED_POLICY &&
236         extended_policy() == FIXED_DOUBLE_REGISTER;
237   }
238   bool HasWritableRegisterPolicy() const {
239     return basic_policy() == EXTENDED_POLICY &&
240         extended_policy() == WRITABLE_REGISTER;
241   }
242
243   // [basic_policy]: Distinguish between FIXED_SLOT and all other policies.
244   BasicPolicy basic_policy() const {
245     return BasicPolicyField::decode(value_);
246   }
247
248   // [extended_policy]: Only for non-FIXED_SLOT. The finer-grained policy.
249   ExtendedPolicy extended_policy() const {
250     DCHECK(basic_policy() == EXTENDED_POLICY);
251     return ExtendedPolicyField::decode(value_);
252   }
253
254   // [fixed_slot_index]: Only for FIXED_SLOT.
255   int fixed_slot_index() const {
256     DCHECK(HasFixedSlotPolicy());
257     return static_cast<int>(value_) >> FixedSlotIndexField::kShift;
258   }
259
260   // [fixed_register_index]: Only for FIXED_REGISTER or FIXED_DOUBLE_REGISTER.
261   int fixed_register_index() const {
262     DCHECK(HasFixedRegisterPolicy() || HasFixedDoubleRegisterPolicy());
263     return FixedRegisterField::decode(value_);
264   }
265
266   // [virtual_register]: The virtual register ID for this operand.
267   int virtual_register() const {
268     return VirtualRegisterField::decode(value_);
269   }
270   void set_virtual_register(unsigned id) {
271     value_ = VirtualRegisterField::update(value_, id);
272   }
273
274   // [lifetime]: Only for non-FIXED_SLOT.
275   bool IsUsedAtStart() {
276     DCHECK(basic_policy() == EXTENDED_POLICY);
277     return LifetimeField::decode(value_) == USED_AT_START;
278   }
279 };
280
281
282 class LMoveOperands FINAL BASE_EMBEDDED {
283  public:
284   LMoveOperands(LOperand* source, LOperand* destination)
285       : source_(source), destination_(destination) {
286   }
287
288   LOperand* source() const { return source_; }
289   void set_source(LOperand* operand) { source_ = operand; }
290
291   LOperand* destination() const { return destination_; }
292   void set_destination(LOperand* operand) { destination_ = operand; }
293
294   // The gap resolver marks moves as "in-progress" by clearing the
295   // destination (but not the source).
296   bool IsPending() const {
297     return destination_ == NULL && source_ != NULL;
298   }
299
300   // True if this move a move into the given destination operand.
301   bool Blocks(LOperand* operand) const {
302     return !IsEliminated() && source()->Equals(operand);
303   }
304
305   // A move is redundant if it's been eliminated, if its source and
306   // destination are the same, or if its destination is unneeded or constant.
307   bool IsRedundant() const {
308     return IsEliminated() || source_->Equals(destination_) || IsIgnored() ||
309            (destination_ != NULL && destination_->IsConstantOperand());
310   }
311
312   bool IsIgnored() const {
313     return destination_ != NULL && destination_->IsIgnored();
314   }
315
316   // We clear both operands to indicate move that's been eliminated.
317   void Eliminate() { source_ = destination_ = NULL; }
318   bool IsEliminated() const {
319     DCHECK(source_ != NULL || destination_ == NULL);
320     return source_ == NULL;
321   }
322
323  private:
324   LOperand* source_;
325   LOperand* destination_;
326 };
327
328
329 template<LOperand::Kind kOperandKind, int kNumCachedOperands>
330 class LSubKindOperand FINAL : public LOperand {
331  public:
332   static LSubKindOperand* Create(int index, Zone* zone) {
333     DCHECK(index >= 0);
334     if (index < kNumCachedOperands) return &cache[index];
335     return new(zone) LSubKindOperand(index);
336   }
337
338   static LSubKindOperand* cast(LOperand* op) {
339     DCHECK(op->kind() == kOperandKind);
340     return reinterpret_cast<LSubKindOperand*>(op);
341   }
342
343   static void SetUpCache();
344   static void TearDownCache();
345
346  private:
347   static LSubKindOperand* cache;
348
349   LSubKindOperand() : LOperand() { }
350   explicit LSubKindOperand(int index) : LOperand(kOperandKind, index) { }
351 };
352
353
354 #define LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS(name, type, number)   \
355 typedef LSubKindOperand<LOperand::type, number> L##name;
356 LITHIUM_OPERAND_LIST(LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS)
357 #undef LITHIUM_TYPEDEF_SUBKIND_OPERAND_CLASS
358
359
360 class LParallelMove FINAL : public ZoneObject {
361  public:
362   explicit LParallelMove(Zone* zone) : move_operands_(4, zone) { }
363
364   void AddMove(LOperand* from, LOperand* to, Zone* zone) {
365     move_operands_.Add(LMoveOperands(from, to), zone);
366   }
367
368   bool IsRedundant() const;
369
370   ZoneList<LMoveOperands>* move_operands() { return &move_operands_; }
371
372   void PrintDataTo(StringStream* stream) const;
373
374  private:
375   ZoneList<LMoveOperands> move_operands_;
376 };
377
378
379 class LPointerMap FINAL : public ZoneObject {
380  public:
381   explicit LPointerMap(Zone* zone)
382       : pointer_operands_(8, zone),
383         untagged_operands_(0, zone),
384         lithium_position_(-1) { }
385
386   const ZoneList<LOperand*>* GetNormalizedOperands() {
387     for (int i = 0; i < untagged_operands_.length(); ++i) {
388       RemovePointer(untagged_operands_[i]);
389     }
390     untagged_operands_.Clear();
391     return &pointer_operands_;
392   }
393   int lithium_position() const { return lithium_position_; }
394
395   void set_lithium_position(int pos) {
396     DCHECK(lithium_position_ == -1);
397     lithium_position_ = pos;
398   }
399
400   void RecordPointer(LOperand* op, Zone* zone);
401   void RemovePointer(LOperand* op);
402   void RecordUntagged(LOperand* op, Zone* zone);
403   void PrintTo(StringStream* stream);
404
405  private:
406   ZoneList<LOperand*> pointer_operands_;
407   ZoneList<LOperand*> untagged_operands_;
408   int lithium_position_;
409 };
410
411
412 class LEnvironment FINAL : public ZoneObject {
413  public:
414   LEnvironment(Handle<JSFunction> closure,
415                FrameType frame_type,
416                BailoutId ast_id,
417                int parameter_count,
418                int argument_count,
419                int value_count,
420                LEnvironment* outer,
421                HEnterInlined* entry,
422                Zone* zone)
423       : closure_(closure),
424         frame_type_(frame_type),
425         arguments_stack_height_(argument_count),
426         deoptimization_index_(Safepoint::kNoDeoptimizationIndex),
427         translation_index_(-1),
428         ast_id_(ast_id),
429         translation_size_(value_count),
430         parameter_count_(parameter_count),
431         pc_offset_(-1),
432         values_(value_count, zone),
433         is_tagged_(value_count, zone),
434         is_uint32_(value_count, zone),
435         object_mapping_(0, zone),
436         outer_(outer),
437         entry_(entry),
438         zone_(zone),
439         has_been_used_(false) { }
440
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_; }
454
455   bool has_been_used() const { return has_been_used_; }
456   void set_has_been_used() { has_been_used_ = true; }
457
458   void AddValue(LOperand* operand,
459                 Representation representation,
460                 bool is_uint32) {
461     values_.Add(operand, zone());
462     if (representation.IsSmiOrTagged()) {
463       DCHECK(!is_uint32);
464       is_tagged_.Add(values_.length() - 1, zone());
465     }
466
467     if (is_uint32) {
468       is_uint32_.Add(values_.length() - 1, zone());
469     }
470   }
471
472   bool HasTaggedValueAt(int index) const {
473     return is_tagged_.Contains(index);
474   }
475
476   bool HasUint32ValueAt(int index) const {
477     return is_uint32_.Contains(index);
478   }
479
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());
485   }
486
487   void AddDuplicateObject(int dupe_of) {
488     uint32_t encoded = LengthOrDupeField::encode(dupe_of) |
489                        IsDuplicateField::encode(true);
490     object_mapping_.Add(encoded, zone());
491   }
492
493   int ObjectDuplicateOfAt(int index) {
494     DCHECK(ObjectIsDuplicateAt(index));
495     return LengthOrDupeField::decode(object_mapping_[index]);
496   }
497
498   int ObjectLengthAt(int index) {
499     DCHECK(!ObjectIsDuplicateAt(index));
500     return LengthOrDupeField::decode(object_mapping_[index]);
501   }
502
503   bool ObjectIsArgumentsAt(int index) {
504     DCHECK(!ObjectIsDuplicateAt(index));
505     return IsArgumentsField::decode(object_mapping_[index]);
506   }
507
508   bool ObjectIsDuplicateAt(int index) {
509     return IsDuplicateField::decode(object_mapping_[index]);
510   }
511
512   void Register(int deoptimization_index,
513                 int translation_index,
514                 int pc_offset) {
515     DCHECK(!HasBeenRegistered());
516     deoptimization_index_ = deoptimization_index;
517     translation_index_ = translation_index;
518     pc_offset_ = pc_offset;
519   }
520   bool HasBeenRegistered() const {
521     return deoptimization_index_ != Safepoint::kNoDeoptimizationIndex;
522   }
523
524   void PrintTo(StringStream* stream);
525
526   // Marker value indicating a de-materialized object.
527   static LOperand* materialization_marker() { return NULL; }
528
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> { };
533
534  private:
535   Handle<JSFunction> closure_;
536   FrameType frame_type_;
537   int arguments_stack_height_;
538   int deoptimization_index_;
539   int translation_index_;
540   BailoutId ast_id_;
541   int translation_size_;
542   int parameter_count_;
543   int pc_offset_;
544
545   // Value array: [parameters] [locals] [expression stack] [de-materialized].
546   //              |>--------- translation_size ---------<|
547   ZoneList<LOperand*> values_;
548   GrowableBitVector is_tagged_;
549   GrowableBitVector is_uint32_;
550
551   // Map with encoded information about materialization_marker operands.
552   ZoneList<uint32_t> object_mapping_;
553
554   LEnvironment* outer_;
555   HEnterInlined* entry_;
556   Zone* zone_;
557   bool has_been_used_;
558 };
559
560
561 // Iterates over the non-null, non-constant operands in an environment.
562 class ShallowIterator FINAL BASE_EMBEDDED {
563  public:
564   explicit ShallowIterator(LEnvironment* env)
565       : env_(env),
566         limit_(env != NULL ? env->values()->length() : 0),
567         current_(0) {
568     SkipUninteresting();
569   }
570
571   bool Done() { return current_ >= limit_; }
572
573   LOperand* Current() {
574     DCHECK(!Done());
575     DCHECK(env_->values()->at(current_) != NULL);
576     return env_->values()->at(current_);
577   }
578
579   void Advance() {
580     DCHECK(!Done());
581     ++current_;
582     SkipUninteresting();
583   }
584
585   LEnvironment* env() { return env_; }
586
587  private:
588   bool ShouldSkip(LOperand* op) {
589     return op == NULL || op->IsConstantOperand();
590   }
591
592   // Skip until something interesting, beginning with and including current_.
593   void SkipUninteresting() {
594     while (current_ < limit_ && ShouldSkip(env_->values()->at(current_))) {
595       ++current_;
596     }
597   }
598
599   LEnvironment* env_;
600   int limit_;
601   int current_;
602 };
603
604
605 // Iterator for non-null, non-constant operands incl. outer environments.
606 class DeepIterator FINAL BASE_EMBEDDED {
607  public:
608   explicit DeepIterator(LEnvironment* env)
609       : current_iterator_(env) {
610     SkipUninteresting();
611   }
612
613   bool Done() { return current_iterator_.Done(); }
614
615   LOperand* Current() {
616     DCHECK(!current_iterator_.Done());
617     DCHECK(current_iterator_.Current() != NULL);
618     return current_iterator_.Current();
619   }
620
621   void Advance() {
622     current_iterator_.Advance();
623     SkipUninteresting();
624   }
625
626  private:
627   void SkipUninteresting() {
628     while (current_iterator_.env() != NULL && current_iterator_.Done()) {
629       current_iterator_ = ShallowIterator(current_iterator_.env()->outer());
630     }
631   }
632
633   ShallowIterator current_iterator_;
634 };
635
636
637 class LPlatformChunk;
638 class LGap;
639 class LLabel;
640
641 // Superclass providing data and behavior common to all the
642 // arch-specific LPlatformChunk classes.
643 class LChunk : public ZoneObject {
644  public:
645   static LChunk* NewChunk(HGraph* graph);
646
647   void AddInstruction(LInstruction* instruction, HBasicBlock* block);
648   LConstantOperand* DefineConstantOperand(HConstant* constant);
649   HConstant* LookupConstant(LConstantOperand* operand) const;
650   Representation LookupLiteralRepresentation(LConstantOperand* operand) const;
651
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;
668
669   const ZoneList<Handle<JSFunction> >* inlined_closures() const {
670     return &inlined_closures_;
671   }
672
673   void AddInlinedClosure(Handle<JSFunction> closure) {
674     inlined_closures_.Add(closure, zone());
675   }
676
677   void AddDeprecationDependency(Handle<Map> map) {
678     DCHECK(!map->is_deprecated());
679     if (!map->CanBeDeprecated()) return;
680     DCHECK(!info_->IsStub());
681     deprecation_dependencies_.insert(map);
682   }
683
684   void AddStabilityDependency(Handle<Map> map) {
685     DCHECK(map->is_stable());
686     if (!map->CanTransition()) return;
687     DCHECK(!info_->IsStub());
688     stability_dependencies_.insert(map);
689   }
690
691   Zone* zone() const { return info_->zone(); }
692
693   Handle<Code> Codegen();
694
695   void set_allocated_double_registers(BitVector* allocated_registers);
696   BitVector* allocated_double_registers() {
697     return allocated_double_registers_;
698   }
699
700  protected:
701   LChunk(CompilationInfo* info, HGraph* graph);
702
703   int spill_slot_count_;
704
705  private:
706   typedef std::less<Handle<Map> > MapLess;
707   typedef zone_allocator<Handle<Map> > MapAllocator;
708   typedef std::set<Handle<Map>, MapLess, MapAllocator> MapSet;
709
710   void CommitDependencies(Handle<Code> code) const;
711
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_;
720 };
721
722
723 class LChunkBuilderBase BASE_EMBEDDED {
724  public:
725   explicit LChunkBuilderBase(CompilationInfo* info, HGraph* graph)
726       : argument_count_(0),
727         chunk_(NULL),
728         info_(info),
729         graph_(graph),
730         status_(UNUSED),
731         zone_(graph->zone()) {}
732
733   virtual ~LChunkBuilderBase() { }
734
735   void Abort(BailoutReason reason);
736   void Retry(BailoutReason reason);
737
738  protected:
739   enum Status { UNUSED, BUILDING, DONE, ABORTED };
740
741   LPlatformChunk* chunk() const { return chunk_; }
742   CompilationInfo* info() const { return info_; }
743   HGraph* graph() const { return graph_; }
744   int argument_count() const { return argument_count_; }
745   Isolate* isolate() const { return graph_->isolate(); }
746   Heap* heap() const { return isolate()->heap(); }
747
748   bool is_unused() const { return status_ == UNUSED; }
749   bool is_building() const { return status_ == BUILDING; }
750   bool is_done() const { return status_ == DONE; }
751   bool is_aborted() const { return status_ == ABORTED; }
752
753   // An input operand in register, stack slot or a constant operand.
754   // Will not be moved to a register even if one is freely available.
755   virtual MUST_USE_RESULT LOperand* UseAny(HValue* value) = 0;
756
757   LEnvironment* CreateEnvironment(HEnvironment* hydrogen_env,
758                                   int* argument_index_accumulator,
759                                   ZoneList<HValue*>* objects_to_materialize);
760   void AddObjectToMaterialize(HValue* value,
761                               ZoneList<HValue*>* objects_to_materialize,
762                               LEnvironment* result);
763
764   Zone* zone() const { return zone_; }
765
766   int argument_count_;
767   LPlatformChunk* chunk_;
768   CompilationInfo* info_;
769   HGraph* const graph_;
770   Status status_;
771
772  private:
773   Zone* zone_;
774 };
775
776
777 int StackSlotOffset(int index);
778
779 enum NumberUntagDMode {
780   NUMBER_CANDIDATE_IS_SMI,
781   NUMBER_CANDIDATE_IS_ANY_TAGGED
782 };
783
784
785 class LPhase : public CompilationPhase {
786  public:
787   LPhase(const char* name, LChunk* chunk)
788       : CompilationPhase(name, chunk->info()),
789         chunk_(chunk) { }
790   ~LPhase();
791
792  private:
793   LChunk* chunk_;
794
795   DISALLOW_COPY_AND_ASSIGN(LPhase);
796 };
797
798
799 // A register-allocator view of a Lithium instruction. It contains the id of
800 // the output operand and a list of input operand uses.
801 enum RegisterKind {
802   UNALLOCATED_REGISTERS,
803   GENERAL_REGISTERS,
804   DOUBLE_REGISTERS,
805   FLOAT32x4_REGISTERS,
806   FLOAT64x2_REGISTERS,
807   INT32x4_REGISTERS
808 };
809
810 // Iterator for non-null temp operands.
811 class TempIterator BASE_EMBEDDED {
812  public:
813   inline explicit TempIterator(LInstruction* instr);
814   inline bool Done();
815   inline LOperand* Current();
816   inline void Advance();
817
818  private:
819   inline void SkipUninteresting();
820   LInstruction* instr_;
821   int limit_;
822   int current_;
823 };
824
825
826 // Iterator for non-constant input operands.
827 class InputIterator BASE_EMBEDDED {
828  public:
829   inline explicit InputIterator(LInstruction* instr);
830   inline bool Done();
831   inline LOperand* Current();
832   inline void Advance();
833
834  private:
835   inline void SkipUninteresting();
836   LInstruction* instr_;
837   int limit_;
838   int current_;
839 };
840
841
842 class UseIterator BASE_EMBEDDED {
843  public:
844   inline explicit UseIterator(LInstruction* instr);
845   inline bool Done();
846   inline LOperand* Current();
847   inline void Advance();
848
849  private:
850   InputIterator input_iterator_;
851   DeepIterator env_iterator_;
852 };
853
854 class LInstruction;
855 class LCodeGen;
856 } }  // namespace v8::internal
857
858 #endif  // V8_LITHIUM_H_