nodes.h revision 390f59f9bec64fd81b05e796dfaeb03ab6d4cc81
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
20#include "invoke_type.h"
21#include "locations.h"
22#include "offsets.h"
23#include "primitive.h"
24#include "utils/arena_object.h"
25#include "utils/arena_bit_vector.h"
26#include "utils/growable_array.h"
27
28namespace art {
29
30class HBasicBlock;
31class HEnvironment;
32class HInstruction;
33class HIntConstant;
34class HInvoke;
35class HGraphVisitor;
36class HPhi;
37class HSuspendCheck;
38class LiveInterval;
39class LocationSummary;
40
41static const int kDefaultNumberOfBlocks = 8;
42static const int kDefaultNumberOfSuccessors = 2;
43static const int kDefaultNumberOfPredecessors = 2;
44static const int kDefaultNumberOfDominatedBlocks = 1;
45static const int kDefaultNumberOfBackEdges = 1;
46
47static constexpr uint32_t kMaxIntShiftValue = 0x1f;
48static constexpr uint64_t kMaxLongShiftValue = 0x3f;
49
50enum IfCondition {
51  kCondEQ,
52  kCondNE,
53  kCondLT,
54  kCondLE,
55  kCondGT,
56  kCondGE,
57};
58
59class HInstructionList {
60 public:
61  HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
62
63  void AddInstruction(HInstruction* instruction);
64  void RemoveInstruction(HInstruction* instruction);
65
66  // Return true if this list contains `instruction`.
67  bool Contains(HInstruction* instruction) const;
68
69  // Return true if `instruction1` is found before `instruction2` in
70  // this instruction list and false otherwise.  Abort if none
71  // of these instructions is found.
72  bool FoundBefore(const HInstruction* instruction1,
73                   const HInstruction* instruction2) const;
74
75 private:
76  HInstruction* first_instruction_;
77  HInstruction* last_instruction_;
78
79  friend class HBasicBlock;
80  friend class HGraph;
81  friend class HInstruction;
82  friend class HInstructionIterator;
83  friend class HBackwardInstructionIterator;
84
85  DISALLOW_COPY_AND_ASSIGN(HInstructionList);
86};
87
88// Control-flow graph of a method. Contains a list of basic blocks.
89class HGraph : public ArenaObject<kArenaAllocMisc> {
90 public:
91  HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
92      : arena_(arena),
93        blocks_(arena, kDefaultNumberOfBlocks),
94        reverse_post_order_(arena, kDefaultNumberOfBlocks),
95        entry_block_(nullptr),
96        exit_block_(nullptr),
97        maximum_number_of_out_vregs_(0),
98        number_of_vregs_(0),
99        number_of_in_vregs_(0),
100        temporaries_vreg_slots_(0),
101        current_instruction_id_(start_instruction_id) {}
102
103  ArenaAllocator* GetArena() const { return arena_; }
104  const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
105  HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
106
107  HBasicBlock* GetEntryBlock() const { return entry_block_; }
108  HBasicBlock* GetExitBlock() const { return exit_block_; }
109
110  void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
111  void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
112
113  void AddBlock(HBasicBlock* block);
114
115  // Try building the SSA form of this graph, with dominance computation and loop
116  // recognition. Returns whether it was successful in doing all these steps.
117  bool TryBuildingSsa() {
118    BuildDominatorTree();
119    TransformToSsa();
120    return AnalyzeNaturalLoops();
121  }
122
123  void BuildDominatorTree();
124  void TransformToSsa();
125  void SimplifyCFG();
126
127  // Analyze all natural loops in this graph. Returns false if one
128  // loop is not natural, that is the header does not dominate the
129  // back edge.
130  bool AnalyzeNaturalLoops() const;
131
132  // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
133  void InlineInto(HGraph* outer_graph, HInvoke* invoke);
134
135  void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
136  void SimplifyLoop(HBasicBlock* header);
137
138  int32_t GetNextInstructionId() {
139    DCHECK_NE(current_instruction_id_, INT32_MAX);
140    return current_instruction_id_++;
141  }
142
143  int32_t GetCurrentInstructionId() const {
144    return current_instruction_id_;
145  }
146
147  void SetCurrentInstructionId(int32_t id) {
148    current_instruction_id_ = id;
149  }
150
151  uint16_t GetMaximumNumberOfOutVRegs() const {
152    return maximum_number_of_out_vregs_;
153  }
154
155  void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
156    maximum_number_of_out_vregs_ = new_value;
157  }
158
159  void UpdateTemporariesVRegSlots(size_t slots) {
160    temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
161  }
162
163  size_t GetTemporariesVRegSlots() const {
164    return temporaries_vreg_slots_;
165  }
166
167  void SetNumberOfVRegs(uint16_t number_of_vregs) {
168    number_of_vregs_ = number_of_vregs;
169  }
170
171  uint16_t GetNumberOfVRegs() const {
172    return number_of_vregs_;
173  }
174
175  void SetNumberOfInVRegs(uint16_t value) {
176    number_of_in_vregs_ = value;
177  }
178
179  uint16_t GetNumberOfLocalVRegs() const {
180    return number_of_vregs_ - number_of_in_vregs_;
181  }
182
183  const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
184    return reverse_post_order_;
185  }
186
187 private:
188  HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
189  void VisitBlockForDominatorTree(HBasicBlock* block,
190                                  HBasicBlock* predecessor,
191                                  GrowableArray<size_t>* visits);
192  void FindBackEdges(ArenaBitVector* visited);
193  void VisitBlockForBackEdges(HBasicBlock* block,
194                              ArenaBitVector* visited,
195                              ArenaBitVector* visiting);
196  void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
197  void RemoveDeadBlocks(const ArenaBitVector& visited) const;
198
199  ArenaAllocator* const arena_;
200
201  // List of blocks in insertion order.
202  GrowableArray<HBasicBlock*> blocks_;
203
204  // List of blocks to perform a reverse post order tree traversal.
205  GrowableArray<HBasicBlock*> reverse_post_order_;
206
207  HBasicBlock* entry_block_;
208  HBasicBlock* exit_block_;
209
210  // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
211  uint16_t maximum_number_of_out_vregs_;
212
213  // The number of virtual registers in this method. Contains the parameters.
214  uint16_t number_of_vregs_;
215
216  // The number of virtual registers used by parameters of this method.
217  uint16_t number_of_in_vregs_;
218
219  // Number of vreg size slots that the temporaries use (used in baseline compiler).
220  size_t temporaries_vreg_slots_;
221
222  // The current id to assign to a newly added instruction. See HInstruction.id_.
223  int32_t current_instruction_id_;
224
225  ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
226  DISALLOW_COPY_AND_ASSIGN(HGraph);
227};
228
229class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
230 public:
231  HLoopInformation(HBasicBlock* header, HGraph* graph)
232      : header_(header),
233        suspend_check_(nullptr),
234        back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
235        // Make bit vector growable, as the number of blocks may change.
236        blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
237
238  HBasicBlock* GetHeader() const {
239    return header_;
240  }
241
242  HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
243  void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
244  bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
245
246  void AddBackEdge(HBasicBlock* back_edge) {
247    back_edges_.Add(back_edge);
248  }
249
250  void RemoveBackEdge(HBasicBlock* back_edge) {
251    back_edges_.Delete(back_edge);
252  }
253
254  bool IsBackEdge(HBasicBlock* block) {
255    for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
256      if (back_edges_.Get(i) == block) return true;
257    }
258    return false;
259  }
260
261  size_t NumberOfBackEdges() const {
262    return back_edges_.Size();
263  }
264
265  HBasicBlock* GetPreHeader() const;
266
267  const GrowableArray<HBasicBlock*>& GetBackEdges() const {
268    return back_edges_;
269  }
270
271  void ClearBackEdges() {
272    back_edges_.Reset();
273  }
274
275  // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
276  // that is the header dominates the back edge.
277  bool Populate();
278
279  // Returns whether this loop information contains `block`.
280  // Note that this loop information *must* be populated before entering this function.
281  bool Contains(const HBasicBlock& block) const;
282
283  // Returns whether this loop information is an inner loop of `other`.
284  // Note that `other` *must* be populated before entering this function.
285  bool IsIn(const HLoopInformation& other) const;
286
287  const ArenaBitVector& GetBlocks() const { return blocks_; }
288
289 private:
290  // Internal recursive implementation of `Populate`.
291  void PopulateRecursive(HBasicBlock* block);
292
293  HBasicBlock* header_;
294  HSuspendCheck* suspend_check_;
295  GrowableArray<HBasicBlock*> back_edges_;
296  ArenaBitVector blocks_;
297
298  DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
299};
300
301static constexpr size_t kNoLifetime = -1;
302static constexpr uint32_t kNoDexPc = -1;
303
304// A block in a method. Contains the list of instructions represented
305// as a double linked list. Each block knows its predecessors and
306// successors.
307
308class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
309 public:
310  explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
311      : graph_(graph),
312        predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
313        successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
314        loop_information_(nullptr),
315        dominator_(nullptr),
316        dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
317        block_id_(-1),
318        dex_pc_(dex_pc),
319        lifetime_start_(kNoLifetime),
320        lifetime_end_(kNoLifetime),
321        is_catch_block_(false) {}
322
323  const GrowableArray<HBasicBlock*>& GetPredecessors() const {
324    return predecessors_;
325  }
326
327  const GrowableArray<HBasicBlock*>& GetSuccessors() const {
328    return successors_;
329  }
330
331  const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
332    return dominated_blocks_;
333  }
334
335  bool IsEntryBlock() const {
336    return graph_->GetEntryBlock() == this;
337  }
338
339  bool IsExitBlock() const {
340    return graph_->GetExitBlock() == this;
341  }
342
343  void AddBackEdge(HBasicBlock* back_edge) {
344    if (loop_information_ == nullptr) {
345      loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
346    }
347    DCHECK_EQ(loop_information_->GetHeader(), this);
348    loop_information_->AddBackEdge(back_edge);
349  }
350
351  HGraph* GetGraph() const { return graph_; }
352
353  int GetBlockId() const { return block_id_; }
354  void SetBlockId(int id) { block_id_ = id; }
355
356  HBasicBlock* GetDominator() const { return dominator_; }
357  void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
358  void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
359
360  int NumberOfBackEdges() const {
361    return loop_information_ == nullptr
362        ? 0
363        : loop_information_->NumberOfBackEdges();
364  }
365
366  HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
367  HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
368  const HInstructionList& GetInstructions() const { return instructions_; }
369  const HInstructionList& GetPhis() const { return phis_; }
370  HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
371
372  void AddSuccessor(HBasicBlock* block) {
373    successors_.Add(block);
374    block->predecessors_.Add(this);
375  }
376
377  void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
378    size_t successor_index = GetSuccessorIndexOf(existing);
379    DCHECK_NE(successor_index, static_cast<size_t>(-1));
380    existing->RemovePredecessor(this);
381    new_block->predecessors_.Add(this);
382    successors_.Put(successor_index, new_block);
383  }
384
385  void RemovePredecessor(HBasicBlock* block) {
386    predecessors_.Delete(block);
387  }
388
389  void ClearAllPredecessors() {
390    predecessors_.Reset();
391  }
392
393  void AddPredecessor(HBasicBlock* block) {
394    predecessors_.Add(block);
395    block->successors_.Add(this);
396  }
397
398  void SwapPredecessors() {
399    DCHECK_EQ(predecessors_.Size(), 2u);
400    HBasicBlock* temp = predecessors_.Get(0);
401    predecessors_.Put(0, predecessors_.Get(1));
402    predecessors_.Put(1, temp);
403  }
404
405  size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
406    for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
407      if (predecessors_.Get(i) == predecessor) {
408        return i;
409      }
410    }
411    return -1;
412  }
413
414  size_t GetSuccessorIndexOf(HBasicBlock* successor) {
415    for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
416      if (successors_.Get(i) == successor) {
417        return i;
418      }
419    }
420    return -1;
421  }
422
423  void AddInstruction(HInstruction* instruction);
424  void RemoveInstruction(HInstruction* instruction);
425  void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
426  // Replace instruction `initial` with `replacement` within this block.
427  void ReplaceAndRemoveInstructionWith(HInstruction* initial,
428                                       HInstruction* replacement);
429  void AddPhi(HPhi* phi);
430  void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
431  void RemovePhi(HPhi* phi);
432
433  bool IsLoopHeader() const {
434    return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
435  }
436
437  bool IsLoopPreHeaderFirstPredecessor() const {
438    DCHECK(IsLoopHeader());
439    DCHECK(!GetPredecessors().IsEmpty());
440    return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
441  }
442
443  HLoopInformation* GetLoopInformation() const {
444    return loop_information_;
445  }
446
447  // Set the loop_information_ on this block. This method overrides the current
448  // loop_information if it is an outer loop of the passed loop information.
449  void SetInLoop(HLoopInformation* info) {
450    if (IsLoopHeader()) {
451      // Nothing to do. This just means `info` is an outer loop.
452    } else if (loop_information_ == nullptr) {
453      loop_information_ = info;
454    } else if (loop_information_->Contains(*info->GetHeader())) {
455      // Block is currently part of an outer loop. Make it part of this inner loop.
456      // Note that a non loop header having a loop information means this loop information
457      // has already been populated
458      loop_information_ = info;
459    } else {
460      // Block is part of an inner loop. Do not update the loop information.
461      // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
462      // at this point, because this method is being called while populating `info`.
463    }
464  }
465
466  bool IsInLoop() const { return loop_information_ != nullptr; }
467
468  // Returns wheter this block dominates the blocked passed as parameter.
469  bool Dominates(HBasicBlock* block) const;
470
471  size_t GetLifetimeStart() const { return lifetime_start_; }
472  size_t GetLifetimeEnd() const { return lifetime_end_; }
473
474  void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
475  void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
476
477  uint32_t GetDexPc() const { return dex_pc_; }
478
479  bool IsCatchBlock() const { return is_catch_block_; }
480  void SetIsCatchBlock() { is_catch_block_ = true; }
481
482 private:
483  HGraph* const graph_;
484  GrowableArray<HBasicBlock*> predecessors_;
485  GrowableArray<HBasicBlock*> successors_;
486  HInstructionList instructions_;
487  HInstructionList phis_;
488  HLoopInformation* loop_information_;
489  HBasicBlock* dominator_;
490  GrowableArray<HBasicBlock*> dominated_blocks_;
491  int block_id_;
492  // The dex program counter of the first instruction of this block.
493  const uint32_t dex_pc_;
494  size_t lifetime_start_;
495  size_t lifetime_end_;
496  bool is_catch_block_;
497
498  friend class HGraph;
499  friend class HInstruction;
500
501  DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
502};
503
504#define FOR_EACH_CONCRETE_INSTRUCTION(M)                                \
505  M(Add, BinaryOperation)                                               \
506  M(And, BinaryOperation)                                               \
507  M(ArrayGet, Instruction)                                              \
508  M(ArrayLength, Instruction)                                           \
509  M(ArraySet, Instruction)                                              \
510  M(BoundsCheck, Instruction)                                           \
511  M(CheckCast, Instruction)                                             \
512  M(ClinitCheck, Instruction)                                           \
513  M(Compare, BinaryOperation)                                           \
514  M(Condition, BinaryOperation)                                         \
515  M(Div, BinaryOperation)                                               \
516  M(DivZeroCheck, Instruction)                                          \
517  M(DoubleConstant, Constant)                                           \
518  M(Equal, Condition)                                                   \
519  M(Exit, Instruction)                                                  \
520  M(FloatConstant, Constant)                                            \
521  M(Goto, Instruction)                                                  \
522  M(GreaterThan, Condition)                                             \
523  M(GreaterThanOrEqual, Condition)                                      \
524  M(If, Instruction)                                                    \
525  M(InstanceFieldGet, Instruction)                                      \
526  M(InstanceFieldSet, Instruction)                                      \
527  M(InstanceOf, Instruction)                                            \
528  M(IntConstant, Constant)                                              \
529  M(InvokeInterface, Invoke)                                            \
530  M(InvokeStaticOrDirect, Invoke)                                       \
531  M(InvokeVirtual, Invoke)                                              \
532  M(LessThan, Condition)                                                \
533  M(LessThanOrEqual, Condition)                                         \
534  M(LoadClass, Instruction)                                             \
535  M(LoadException, Instruction)                                         \
536  M(LoadLocal, Instruction)                                             \
537  M(LoadString, Instruction)                                            \
538  M(Local, Instruction)                                                 \
539  M(LongConstant, Constant)                                             \
540  M(MonitorOperation, Instruction)                                      \
541  M(Mul, BinaryOperation)                                               \
542  M(Neg, UnaryOperation)                                                \
543  M(NewArray, Instruction)                                              \
544  M(NewInstance, Instruction)                                           \
545  M(Not, UnaryOperation)                                                \
546  M(NotEqual, Condition)                                                \
547  M(NullCheck, Instruction)                                             \
548  M(Or, BinaryOperation)                                                \
549  M(ParallelMove, Instruction)                                          \
550  M(ParameterValue, Instruction)                                        \
551  M(Phi, Instruction)                                                   \
552  M(Rem, BinaryOperation)                                               \
553  M(Return, Instruction)                                                \
554  M(ReturnVoid, Instruction)                                            \
555  M(Shl, BinaryOperation)                                               \
556  M(Shr, BinaryOperation)                                               \
557  M(StaticFieldGet, Instruction)                                        \
558  M(StaticFieldSet, Instruction)                                        \
559  M(StoreLocal, Instruction)                                            \
560  M(Sub, BinaryOperation)                                               \
561  M(SuspendCheck, Instruction)                                          \
562  M(Temporary, Instruction)                                             \
563  M(Throw, Instruction)                                                 \
564  M(TypeConversion, Instruction)                                        \
565  M(UShr, BinaryOperation)                                              \
566  M(Xor, BinaryOperation)                                               \
567
568#define FOR_EACH_INSTRUCTION(M)                                         \
569  FOR_EACH_CONCRETE_INSTRUCTION(M)                                      \
570  M(Constant, Instruction)                                              \
571  M(UnaryOperation, Instruction)                                        \
572  M(BinaryOperation, Instruction)                                       \
573  M(Invoke, Instruction)
574
575#define FORWARD_DECLARATION(type, super) class H##type;
576FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
577#undef FORWARD_DECLARATION
578
579#define DECLARE_INSTRUCTION(type)                                       \
580  virtual InstructionKind GetKind() const { return k##type; }           \
581  virtual const char* DebugName() const { return #type; }               \
582  virtual const H##type* As##type() const OVERRIDE { return this; }     \
583  virtual H##type* As##type() OVERRIDE { return this; }                 \
584  virtual bool InstructionTypeEquals(HInstruction* other) const {       \
585    return other->Is##type();                                           \
586  }                                                                     \
587  virtual void Accept(HGraphVisitor* visitor)
588
589template <typename T>
590class HUseListNode : public ArenaObject<kArenaAllocMisc> {
591 public:
592  HUseListNode(T* user, size_t index, HUseListNode* tail)
593      : user_(user), index_(index), tail_(tail) {}
594
595  HUseListNode* GetTail() const { return tail_; }
596  T* GetUser() const { return user_; }
597  size_t GetIndex() const { return index_; }
598
599  void SetTail(HUseListNode<T>* node) { tail_ = node; }
600
601 private:
602  T* const user_;
603  const size_t index_;
604  HUseListNode<T>* tail_;
605
606  DISALLOW_COPY_AND_ASSIGN(HUseListNode);
607};
608
609// Represents the side effects an instruction may have.
610class SideEffects : public ValueObject {
611 public:
612  SideEffects() : flags_(0) {}
613
614  static SideEffects None() {
615    return SideEffects(0);
616  }
617
618  static SideEffects All() {
619    return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
620  }
621
622  static SideEffects ChangesSomething() {
623    return SideEffects((1 << kFlagChangesCount) - 1);
624  }
625
626  static SideEffects DependsOnSomething() {
627    int count = kFlagDependsOnCount - kFlagChangesCount;
628    return SideEffects(((1 << count) - 1) << kFlagChangesCount);
629  }
630
631  SideEffects Union(SideEffects other) const {
632    return SideEffects(flags_ | other.flags_);
633  }
634
635  bool HasSideEffects() const {
636    size_t all_bits_set = (1 << kFlagChangesCount) - 1;
637    return (flags_ & all_bits_set) != 0;
638  }
639
640  bool HasAllSideEffects() const {
641    size_t all_bits_set = (1 << kFlagChangesCount) - 1;
642    return all_bits_set == (flags_ & all_bits_set);
643  }
644
645  bool DependsOn(SideEffects other) const {
646    size_t depends_flags = other.ComputeDependsFlags();
647    return (flags_ & depends_flags) != 0;
648  }
649
650  bool HasDependencies() const {
651    int count = kFlagDependsOnCount - kFlagChangesCount;
652    size_t all_bits_set = (1 << count) - 1;
653    return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
654  }
655
656 private:
657  static constexpr int kFlagChangesSomething = 0;
658  static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
659
660  static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
661  static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
662
663  explicit SideEffects(size_t flags) : flags_(flags) {}
664
665  size_t ComputeDependsFlags() const {
666    return flags_ << kFlagChangesCount;
667  }
668
669  size_t flags_;
670};
671
672class HInstruction : public ArenaObject<kArenaAllocMisc> {
673 public:
674  explicit HInstruction(SideEffects side_effects)
675      : previous_(nullptr),
676        next_(nullptr),
677        block_(nullptr),
678        id_(-1),
679        ssa_index_(-1),
680        uses_(nullptr),
681        env_uses_(nullptr),
682        environment_(nullptr),
683        locations_(nullptr),
684        live_interval_(nullptr),
685        lifetime_position_(kNoLifetime),
686        side_effects_(side_effects) {}
687
688  virtual ~HInstruction() {}
689
690#define DECLARE_KIND(type, super) k##type,
691  enum InstructionKind {
692    FOR_EACH_INSTRUCTION(DECLARE_KIND)
693  };
694#undef DECLARE_KIND
695
696  HInstruction* GetNext() const { return next_; }
697  HInstruction* GetPrevious() const { return previous_; }
698
699  HBasicBlock* GetBlock() const { return block_; }
700  void SetBlock(HBasicBlock* block) { block_ = block; }
701  bool IsInBlock() const { return block_ != nullptr; }
702  bool IsInLoop() const { return block_->IsInLoop(); }
703  bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
704
705  virtual size_t InputCount() const = 0;
706  virtual HInstruction* InputAt(size_t i) const = 0;
707
708  virtual void Accept(HGraphVisitor* visitor) = 0;
709  virtual const char* DebugName() const = 0;
710
711  virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
712  virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
713
714  virtual bool NeedsEnvironment() const { return false; }
715  virtual bool IsControlFlow() const { return false; }
716  virtual bool CanThrow() const { return false; }
717  bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
718
719  void AddUseAt(HInstruction* user, size_t index) {
720    uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
721  }
722
723  void AddEnvUseAt(HEnvironment* user, size_t index) {
724    DCHECK(user != nullptr);
725    env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
726        user, index, env_uses_);
727  }
728
729  void RemoveUser(HInstruction* user, size_t index);
730  void RemoveEnvironmentUser(HEnvironment* user, size_t index);
731
732  HUseListNode<HInstruction>* GetUses() const { return uses_; }
733  HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
734
735  bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
736  bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
737
738  size_t NumberOfUses() const {
739    // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
740    size_t result = 0;
741    HUseListNode<HInstruction>* current = uses_;
742    while (current != nullptr) {
743      current = current->GetTail();
744      ++result;
745    }
746    return result;
747  }
748
749  // Does this instruction strictly dominate `other_instruction`?
750  // Returns false if this instruction and `other_instruction` are the same.
751  // Aborts if this instruction and `other_instruction` are both phis.
752  bool StrictlyDominates(HInstruction* other_instruction) const;
753
754  int GetId() const { return id_; }
755  void SetId(int id) { id_ = id; }
756
757  int GetSsaIndex() const { return ssa_index_; }
758  void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
759  bool HasSsaIndex() const { return ssa_index_ != -1; }
760
761  bool HasEnvironment() const { return environment_ != nullptr; }
762  HEnvironment* GetEnvironment() const { return environment_; }
763  void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
764
765  // Returns the number of entries in the environment. Typically, that is the
766  // number of dex registers in a method. It could be more in case of inlining.
767  size_t EnvironmentSize() const;
768
769  LocationSummary* GetLocations() const { return locations_; }
770  void SetLocations(LocationSummary* locations) { locations_ = locations; }
771
772  void ReplaceWith(HInstruction* instruction);
773  void ReplaceInput(HInstruction* replacement, size_t index);
774
775  // Insert `this` instruction in `cursor`'s graph, just before `cursor`.
776  void InsertBefore(HInstruction* cursor);
777
778  bool HasOnlyOneUse() const {
779    return uses_ != nullptr && uses_->GetTail() == nullptr;
780  }
781
782#define INSTRUCTION_TYPE_CHECK(type, super)                                    \
783  bool Is##type() const { return (As##type() != nullptr); }                    \
784  virtual const H##type* As##type() const { return nullptr; }                  \
785  virtual H##type* As##type() { return nullptr; }
786
787  FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
788#undef INSTRUCTION_TYPE_CHECK
789
790  // Returns whether the instruction can be moved within the graph.
791  virtual bool CanBeMoved() const { return false; }
792
793  // Returns whether the two instructions are of the same kind.
794  virtual bool InstructionTypeEquals(HInstruction* other) const {
795    UNUSED(other);
796    return false;
797  }
798
799  // Returns whether any data encoded in the two instructions is equal.
800  // This method does not look at the inputs. Both instructions must be
801  // of the same type, otherwise the method has undefined behavior.
802  virtual bool InstructionDataEquals(HInstruction* other) const {
803    UNUSED(other);
804    return false;
805  }
806
807  // Returns whether two instructions are equal, that is:
808  // 1) They have the same type and contain the same data (InstructionDataEquals).
809  // 2) Their inputs are identical.
810  bool Equals(HInstruction* other) const;
811
812  virtual InstructionKind GetKind() const = 0;
813
814  virtual size_t ComputeHashCode() const {
815    size_t result = GetKind();
816    for (size_t i = 0, e = InputCount(); i < e; ++i) {
817      result = (result * 31) + InputAt(i)->GetId();
818    }
819    return result;
820  }
821
822  SideEffects GetSideEffects() const { return side_effects_; }
823
824  size_t GetLifetimePosition() const { return lifetime_position_; }
825  void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
826  LiveInterval* GetLiveInterval() const { return live_interval_; }
827  void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
828  bool HasLiveInterval() const { return live_interval_ != nullptr; }
829
830 private:
831  HInstruction* previous_;
832  HInstruction* next_;
833  HBasicBlock* block_;
834
835  // An instruction gets an id when it is added to the graph.
836  // It reflects creation order. A negative id means the instruction
837  // has not been added to the graph.
838  int id_;
839
840  // When doing liveness analysis, instructions that have uses get an SSA index.
841  int ssa_index_;
842
843  // List of instructions that have this instruction as input.
844  HUseListNode<HInstruction>* uses_;
845
846  // List of environments that contain this instruction.
847  HUseListNode<HEnvironment>* env_uses_;
848
849  // The environment associated with this instruction. Not null if the instruction
850  // might jump out of the method.
851  HEnvironment* environment_;
852
853  // Set by the code generator.
854  LocationSummary* locations_;
855
856  // Set by the liveness analysis.
857  LiveInterval* live_interval_;
858
859  // Set by the liveness analysis, this is the position in a linear
860  // order of blocks where this instruction's live interval start.
861  size_t lifetime_position_;
862
863  const SideEffects side_effects_;
864
865  friend class HBasicBlock;
866  friend class HGraph;
867  friend class HInstructionList;
868
869  DISALLOW_COPY_AND_ASSIGN(HInstruction);
870};
871std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
872
873template<typename T>
874class HUseIterator : public ValueObject {
875 public:
876  explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
877
878  bool Done() const { return current_ == nullptr; }
879
880  void Advance() {
881    DCHECK(!Done());
882    current_ = current_->GetTail();
883  }
884
885  HUseListNode<T>* Current() const {
886    DCHECK(!Done());
887    return current_;
888  }
889
890 private:
891  HUseListNode<T>* current_;
892
893  friend class HValue;
894};
895
896// A HEnvironment object contains the values of virtual registers at a given location.
897class HEnvironment : public ArenaObject<kArenaAllocMisc> {
898 public:
899  HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
900    vregs_.SetSize(number_of_vregs);
901    for (size_t i = 0; i < number_of_vregs; i++) {
902      vregs_.Put(i, nullptr);
903    }
904  }
905
906  void Populate(const GrowableArray<HInstruction*>& env) {
907    for (size_t i = 0; i < env.Size(); i++) {
908      HInstruction* instruction = env.Get(i);
909      vregs_.Put(i, instruction);
910      if (instruction != nullptr) {
911        instruction->AddEnvUseAt(this, i);
912      }
913    }
914  }
915
916  void SetRawEnvAt(size_t index, HInstruction* instruction) {
917    vregs_.Put(index, instruction);
918  }
919
920  HInstruction* GetInstructionAt(size_t index) const {
921    return vregs_.Get(index);
922  }
923
924  GrowableArray<HInstruction*>* GetVRegs() {
925    return &vregs_;
926  }
927
928  size_t Size() const { return vregs_.Size(); }
929
930 private:
931  GrowableArray<HInstruction*> vregs_;
932
933  DISALLOW_COPY_AND_ASSIGN(HEnvironment);
934};
935
936class HInputIterator : public ValueObject {
937 public:
938  explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
939
940  bool Done() const { return index_ == instruction_->InputCount(); }
941  HInstruction* Current() const { return instruction_->InputAt(index_); }
942  void Advance() { index_++; }
943
944 private:
945  HInstruction* instruction_;
946  size_t index_;
947
948  DISALLOW_COPY_AND_ASSIGN(HInputIterator);
949};
950
951class HInstructionIterator : public ValueObject {
952 public:
953  explicit HInstructionIterator(const HInstructionList& instructions)
954      : instruction_(instructions.first_instruction_) {
955    next_ = Done() ? nullptr : instruction_->GetNext();
956  }
957
958  bool Done() const { return instruction_ == nullptr; }
959  HInstruction* Current() const { return instruction_; }
960  void Advance() {
961    instruction_ = next_;
962    next_ = Done() ? nullptr : instruction_->GetNext();
963  }
964
965 private:
966  HInstruction* instruction_;
967  HInstruction* next_;
968
969  DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
970};
971
972class HBackwardInstructionIterator : public ValueObject {
973 public:
974  explicit HBackwardInstructionIterator(const HInstructionList& instructions)
975      : instruction_(instructions.last_instruction_) {
976    next_ = Done() ? nullptr : instruction_->GetPrevious();
977  }
978
979  bool Done() const { return instruction_ == nullptr; }
980  HInstruction* Current() const { return instruction_; }
981  void Advance() {
982    instruction_ = next_;
983    next_ = Done() ? nullptr : instruction_->GetPrevious();
984  }
985
986 private:
987  HInstruction* instruction_;
988  HInstruction* next_;
989
990  DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
991};
992
993// An embedded container with N elements of type T.  Used (with partial
994// specialization for N=0) because embedded arrays cannot have size 0.
995template<typename T, intptr_t N>
996class EmbeddedArray {
997 public:
998  EmbeddedArray() : elements_() {}
999
1000  intptr_t GetLength() const { return N; }
1001
1002  const T& operator[](intptr_t i) const {
1003    DCHECK_LT(i, GetLength());
1004    return elements_[i];
1005  }
1006
1007  T& operator[](intptr_t i) {
1008    DCHECK_LT(i, GetLength());
1009    return elements_[i];
1010  }
1011
1012  const T& At(intptr_t i) const {
1013    return (*this)[i];
1014  }
1015
1016  void SetAt(intptr_t i, const T& val) {
1017    (*this)[i] = val;
1018  }
1019
1020 private:
1021  T elements_[N];
1022};
1023
1024template<typename T>
1025class EmbeddedArray<T, 0> {
1026 public:
1027  intptr_t length() const { return 0; }
1028  const T& operator[](intptr_t i) const {
1029    UNUSED(i);
1030    LOG(FATAL) << "Unreachable";
1031    UNREACHABLE();
1032  }
1033  T& operator[](intptr_t i) {
1034    UNUSED(i);
1035    LOG(FATAL) << "Unreachable";
1036    UNREACHABLE();
1037  }
1038};
1039
1040template<intptr_t N>
1041class HTemplateInstruction: public HInstruction {
1042 public:
1043  HTemplateInstruction<N>(SideEffects side_effects)
1044      : HInstruction(side_effects), inputs_() {}
1045  virtual ~HTemplateInstruction() {}
1046
1047  virtual size_t InputCount() const { return N; }
1048  virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
1049
1050 protected:
1051  virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
1052    inputs_[i] = instruction;
1053  }
1054
1055 private:
1056  EmbeddedArray<HInstruction*, N> inputs_;
1057
1058  friend class SsaBuilder;
1059};
1060
1061template<intptr_t N>
1062class HExpression : public HTemplateInstruction<N> {
1063 public:
1064  HExpression<N>(Primitive::Type type, SideEffects side_effects)
1065      : HTemplateInstruction<N>(side_effects), type_(type) {}
1066  virtual ~HExpression() {}
1067
1068  virtual Primitive::Type GetType() const { return type_; }
1069
1070 protected:
1071  Primitive::Type type_;
1072};
1073
1074// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1075// instruction that branches to the exit block.
1076class HReturnVoid : public HTemplateInstruction<0> {
1077 public:
1078  HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
1079
1080  virtual bool IsControlFlow() const { return true; }
1081
1082  DECLARE_INSTRUCTION(ReturnVoid);
1083
1084 private:
1085  DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1086};
1087
1088// Represents dex's RETURN opcodes. A HReturn is a control flow
1089// instruction that branches to the exit block.
1090class HReturn : public HTemplateInstruction<1> {
1091 public:
1092  explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
1093    SetRawInputAt(0, value);
1094  }
1095
1096  virtual bool IsControlFlow() const { return true; }
1097
1098  DECLARE_INSTRUCTION(Return);
1099
1100 private:
1101  DISALLOW_COPY_AND_ASSIGN(HReturn);
1102};
1103
1104// The exit instruction is the only instruction of the exit block.
1105// Instructions aborting the method (HThrow and HReturn) must branch to the
1106// exit block.
1107class HExit : public HTemplateInstruction<0> {
1108 public:
1109  HExit() : HTemplateInstruction(SideEffects::None()) {}
1110
1111  virtual bool IsControlFlow() const { return true; }
1112
1113  DECLARE_INSTRUCTION(Exit);
1114
1115 private:
1116  DISALLOW_COPY_AND_ASSIGN(HExit);
1117};
1118
1119// Jumps from one block to another.
1120class HGoto : public HTemplateInstruction<0> {
1121 public:
1122  HGoto() : HTemplateInstruction(SideEffects::None()) {}
1123
1124  bool IsControlFlow() const OVERRIDE { return true; }
1125
1126  HBasicBlock* GetSuccessor() const {
1127    return GetBlock()->GetSuccessors().Get(0);
1128  }
1129
1130  DECLARE_INSTRUCTION(Goto);
1131
1132 private:
1133  DISALLOW_COPY_AND_ASSIGN(HGoto);
1134};
1135
1136
1137// Conditional branch. A block ending with an HIf instruction must have
1138// two successors.
1139class HIf : public HTemplateInstruction<1> {
1140 public:
1141  explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
1142    SetRawInputAt(0, input);
1143  }
1144
1145  bool IsControlFlow() const OVERRIDE { return true; }
1146
1147  HBasicBlock* IfTrueSuccessor() const {
1148    return GetBlock()->GetSuccessors().Get(0);
1149  }
1150
1151  HBasicBlock* IfFalseSuccessor() const {
1152    return GetBlock()->GetSuccessors().Get(1);
1153  }
1154
1155  DECLARE_INSTRUCTION(If);
1156
1157  virtual bool IsIfInstruction() const { return true; }
1158
1159 private:
1160  DISALLOW_COPY_AND_ASSIGN(HIf);
1161};
1162
1163class HUnaryOperation : public HExpression<1> {
1164 public:
1165  HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1166      : HExpression(result_type, SideEffects::None()) {
1167    SetRawInputAt(0, input);
1168  }
1169
1170  HInstruction* GetInput() const { return InputAt(0); }
1171  Primitive::Type GetResultType() const { return GetType(); }
1172
1173  virtual bool CanBeMoved() const { return true; }
1174  virtual bool InstructionDataEquals(HInstruction* other) const {
1175    UNUSED(other);
1176    return true;
1177  }
1178
1179  // Try to statically evaluate `operation` and return a HConstant
1180  // containing the result of this evaluation.  If `operation` cannot
1181  // be evaluated as a constant, return nullptr.
1182  HConstant* TryStaticEvaluation() const;
1183
1184  // Apply this operation to `x`.
1185  virtual int32_t Evaluate(int32_t x) const = 0;
1186  virtual int64_t Evaluate(int64_t x) const = 0;
1187
1188  DECLARE_INSTRUCTION(UnaryOperation);
1189
1190 private:
1191  DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1192};
1193
1194class HBinaryOperation : public HExpression<2> {
1195 public:
1196  HBinaryOperation(Primitive::Type result_type,
1197                   HInstruction* left,
1198                   HInstruction* right) : HExpression(result_type, SideEffects::None()) {
1199    SetRawInputAt(0, left);
1200    SetRawInputAt(1, right);
1201  }
1202
1203  HInstruction* GetLeft() const { return InputAt(0); }
1204  HInstruction* GetRight() const { return InputAt(1); }
1205  Primitive::Type GetResultType() const { return GetType(); }
1206
1207  virtual bool IsCommutative() { return false; }
1208
1209  virtual bool CanBeMoved() const { return true; }
1210  virtual bool InstructionDataEquals(HInstruction* other) const {
1211    UNUSED(other);
1212    return true;
1213  }
1214
1215  // Try to statically evaluate `operation` and return a HConstant
1216  // containing the result of this evaluation.  If `operation` cannot
1217  // be evaluated as a constant, return nullptr.
1218  HConstant* TryStaticEvaluation() const;
1219
1220  // Apply this operation to `x` and `y`.
1221  virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1222  virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1223
1224  DECLARE_INSTRUCTION(BinaryOperation);
1225
1226 private:
1227  DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1228};
1229
1230class HCondition : public HBinaryOperation {
1231 public:
1232  HCondition(HInstruction* first, HInstruction* second)
1233      : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1234        needs_materialization_(true) {}
1235
1236  virtual bool IsCommutative() { return true; }
1237
1238  bool NeedsMaterialization() const { return needs_materialization_; }
1239  void ClearNeedsMaterialization() { needs_materialization_ = false; }
1240
1241  // For code generation purposes, returns whether this instruction is just before
1242  // `if_`, and disregard moves in between.
1243  bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1244
1245  DECLARE_INSTRUCTION(Condition);
1246
1247  virtual IfCondition GetCondition() const = 0;
1248
1249 private:
1250  // For register allocation purposes, returns whether this instruction needs to be
1251  // materialized (that is, not just be in the processor flags).
1252  bool needs_materialization_;
1253
1254  DISALLOW_COPY_AND_ASSIGN(HCondition);
1255};
1256
1257// Instruction to check if two inputs are equal to each other.
1258class HEqual : public HCondition {
1259 public:
1260  HEqual(HInstruction* first, HInstruction* second)
1261      : HCondition(first, second) {}
1262
1263  virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1264    return x == y ? 1 : 0;
1265  }
1266  virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1267    return x == y ? 1 : 0;
1268  }
1269
1270  DECLARE_INSTRUCTION(Equal);
1271
1272  virtual IfCondition GetCondition() const {
1273    return kCondEQ;
1274  }
1275
1276 private:
1277  DISALLOW_COPY_AND_ASSIGN(HEqual);
1278};
1279
1280class HNotEqual : public HCondition {
1281 public:
1282  HNotEqual(HInstruction* first, HInstruction* second)
1283      : HCondition(first, second) {}
1284
1285  virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1286    return x != y ? 1 : 0;
1287  }
1288  virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1289    return x != y ? 1 : 0;
1290  }
1291
1292  DECLARE_INSTRUCTION(NotEqual);
1293
1294  virtual IfCondition GetCondition() const {
1295    return kCondNE;
1296  }
1297
1298 private:
1299  DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1300};
1301
1302class HLessThan : public HCondition {
1303 public:
1304  HLessThan(HInstruction* first, HInstruction* second)
1305      : HCondition(first, second) {}
1306
1307  virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1308    return x < y ? 1 : 0;
1309  }
1310  virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1311    return x < y ? 1 : 0;
1312  }
1313
1314  DECLARE_INSTRUCTION(LessThan);
1315
1316  virtual IfCondition GetCondition() const {
1317    return kCondLT;
1318  }
1319
1320 private:
1321  DISALLOW_COPY_AND_ASSIGN(HLessThan);
1322};
1323
1324class HLessThanOrEqual : public HCondition {
1325 public:
1326  HLessThanOrEqual(HInstruction* first, HInstruction* second)
1327      : HCondition(first, second) {}
1328
1329  virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1330    return x <= y ? 1 : 0;
1331  }
1332  virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1333    return x <= y ? 1 : 0;
1334  }
1335
1336  DECLARE_INSTRUCTION(LessThanOrEqual);
1337
1338  virtual IfCondition GetCondition() const {
1339    return kCondLE;
1340  }
1341
1342 private:
1343  DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1344};
1345
1346class HGreaterThan : public HCondition {
1347 public:
1348  HGreaterThan(HInstruction* first, HInstruction* second)
1349      : HCondition(first, second) {}
1350
1351  virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1352    return x > y ? 1 : 0;
1353  }
1354  virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1355    return x > y ? 1 : 0;
1356  }
1357
1358  DECLARE_INSTRUCTION(GreaterThan);
1359
1360  virtual IfCondition GetCondition() const {
1361    return kCondGT;
1362  }
1363
1364 private:
1365  DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1366};
1367
1368class HGreaterThanOrEqual : public HCondition {
1369 public:
1370  HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1371      : HCondition(first, second) {}
1372
1373  virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1374    return x >= y ? 1 : 0;
1375  }
1376  virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1377    return x >= y ? 1 : 0;
1378  }
1379
1380  DECLARE_INSTRUCTION(GreaterThanOrEqual);
1381
1382  virtual IfCondition GetCondition() const {
1383    return kCondGE;
1384  }
1385
1386 private:
1387  DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1388};
1389
1390
1391// Instruction to check how two inputs compare to each other.
1392// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1393class HCompare : public HBinaryOperation {
1394 public:
1395  // The bias applies for floating point operations and indicates how NaN
1396  // comparisons are treated:
1397  enum Bias {
1398    kNoBias,  // bias is not applicable (i.e. for long operation)
1399    kGtBias,  // return 1 for NaN comparisons
1400    kLtBias,  // return -1 for NaN comparisons
1401  };
1402
1403  HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1404      : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
1405    DCHECK_EQ(type, first->GetType());
1406    DCHECK_EQ(type, second->GetType());
1407  }
1408
1409  int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1410    return
1411      x == y ? 0 :
1412      x > y ? 1 :
1413      -1;
1414  }
1415
1416  int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1417    return
1418      x == y ? 0 :
1419      x > y ? 1 :
1420      -1;
1421  }
1422
1423  bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1424    return bias_ == other->AsCompare()->bias_;
1425  }
1426
1427  bool IsGtBias() { return bias_ == kGtBias; }
1428
1429  DECLARE_INSTRUCTION(Compare);
1430
1431 private:
1432  const Bias bias_;
1433
1434  DISALLOW_COPY_AND_ASSIGN(HCompare);
1435};
1436
1437// A local in the graph. Corresponds to a Dex register.
1438class HLocal : public HTemplateInstruction<0> {
1439 public:
1440  explicit HLocal(uint16_t reg_number)
1441      : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
1442
1443  DECLARE_INSTRUCTION(Local);
1444
1445  uint16_t GetRegNumber() const { return reg_number_; }
1446
1447 private:
1448  // The Dex register number.
1449  const uint16_t reg_number_;
1450
1451  DISALLOW_COPY_AND_ASSIGN(HLocal);
1452};
1453
1454// Load a given local. The local is an input of this instruction.
1455class HLoadLocal : public HExpression<1> {
1456 public:
1457  HLoadLocal(HLocal* local, Primitive::Type type)
1458      : HExpression(type, SideEffects::None()) {
1459    SetRawInputAt(0, local);
1460  }
1461
1462  HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1463
1464  DECLARE_INSTRUCTION(LoadLocal);
1465
1466 private:
1467  DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1468};
1469
1470// Store a value in a given local. This instruction has two inputs: the value
1471// and the local.
1472class HStoreLocal : public HTemplateInstruction<2> {
1473 public:
1474  HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
1475    SetRawInputAt(0, local);
1476    SetRawInputAt(1, value);
1477  }
1478
1479  HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1480
1481  DECLARE_INSTRUCTION(StoreLocal);
1482
1483 private:
1484  DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1485};
1486
1487class HConstant : public HExpression<0> {
1488 public:
1489  explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1490
1491  virtual bool CanBeMoved() const { return true; }
1492
1493  DECLARE_INSTRUCTION(Constant);
1494
1495 private:
1496  DISALLOW_COPY_AND_ASSIGN(HConstant);
1497};
1498
1499class HFloatConstant : public HConstant {
1500 public:
1501  explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1502
1503  float GetValue() const { return value_; }
1504
1505  virtual bool InstructionDataEquals(HInstruction* other) const {
1506    return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1507        bit_cast<float, int32_t>(value_);
1508  }
1509
1510  virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1511
1512  DECLARE_INSTRUCTION(FloatConstant);
1513
1514 private:
1515  const float value_;
1516
1517  DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1518};
1519
1520class HDoubleConstant : public HConstant {
1521 public:
1522  explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1523
1524  double GetValue() const { return value_; }
1525
1526  virtual bool InstructionDataEquals(HInstruction* other) const {
1527    return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1528        bit_cast<double, int64_t>(value_);
1529  }
1530
1531  virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1532
1533  DECLARE_INSTRUCTION(DoubleConstant);
1534
1535 private:
1536  const double value_;
1537
1538  DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1539};
1540
1541// Constants of the type int. Those can be from Dex instructions, or
1542// synthesized (for example with the if-eqz instruction).
1543class HIntConstant : public HConstant {
1544 public:
1545  explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
1546
1547  int32_t GetValue() const { return value_; }
1548
1549  virtual bool InstructionDataEquals(HInstruction* other) const {
1550    return other->AsIntConstant()->value_ == value_;
1551  }
1552
1553  virtual size_t ComputeHashCode() const { return GetValue(); }
1554
1555  DECLARE_INSTRUCTION(IntConstant);
1556
1557 private:
1558  const int32_t value_;
1559
1560  DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1561};
1562
1563class HLongConstant : public HConstant {
1564 public:
1565  explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
1566
1567  int64_t GetValue() const { return value_; }
1568
1569  virtual bool InstructionDataEquals(HInstruction* other) const {
1570    return other->AsLongConstant()->value_ == value_;
1571  }
1572
1573  virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1574
1575  DECLARE_INSTRUCTION(LongConstant);
1576
1577 private:
1578  const int64_t value_;
1579
1580  DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1581};
1582
1583class HInvoke : public HInstruction {
1584 public:
1585  HInvoke(ArenaAllocator* arena,
1586          uint32_t number_of_arguments,
1587          Primitive::Type return_type,
1588          uint32_t dex_pc)
1589    : HInstruction(SideEffects::All()),
1590      inputs_(arena, number_of_arguments),
1591      return_type_(return_type),
1592      dex_pc_(dex_pc) {
1593    inputs_.SetSize(number_of_arguments);
1594  }
1595
1596  virtual size_t InputCount() const { return inputs_.Size(); }
1597  virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1598
1599  // Runtime needs to walk the stack, so Dex -> Dex calls need to
1600  // know their environment.
1601  virtual bool NeedsEnvironment() const { return true; }
1602
1603  void SetArgumentAt(size_t index, HInstruction* argument) {
1604    SetRawInputAt(index, argument);
1605  }
1606
1607  virtual void SetRawInputAt(size_t index, HInstruction* input) {
1608    inputs_.Put(index, input);
1609  }
1610
1611  virtual Primitive::Type GetType() const { return return_type_; }
1612
1613  uint32_t GetDexPc() const { return dex_pc_; }
1614
1615  DECLARE_INSTRUCTION(Invoke);
1616
1617 protected:
1618  GrowableArray<HInstruction*> inputs_;
1619  const Primitive::Type return_type_;
1620  const uint32_t dex_pc_;
1621
1622 private:
1623  DISALLOW_COPY_AND_ASSIGN(HInvoke);
1624};
1625
1626class HInvokeStaticOrDirect : public HInvoke {
1627 public:
1628  HInvokeStaticOrDirect(ArenaAllocator* arena,
1629                        uint32_t number_of_arguments,
1630                        Primitive::Type return_type,
1631                        uint32_t dex_pc,
1632                        uint32_t index_in_dex_cache,
1633                        bool is_recursive,
1634                        InvokeType invoke_type)
1635      : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1636        index_in_dex_cache_(index_in_dex_cache),
1637        invoke_type_(invoke_type),
1638        is_recursive_(is_recursive) {}
1639
1640  uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1641  InvokeType GetInvokeType() const { return invoke_type_; }
1642  bool GetIsRecursive() const { return is_recursive_; }
1643
1644  DECLARE_INSTRUCTION(InvokeStaticOrDirect);
1645
1646 private:
1647  const uint32_t index_in_dex_cache_;
1648  const InvokeType invoke_type_;
1649  const bool is_recursive_;
1650
1651  DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
1652};
1653
1654class HInvokeVirtual : public HInvoke {
1655 public:
1656  HInvokeVirtual(ArenaAllocator* arena,
1657                 uint32_t number_of_arguments,
1658                 Primitive::Type return_type,
1659                 uint32_t dex_pc,
1660                 uint32_t vtable_index)
1661      : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1662        vtable_index_(vtable_index) {}
1663
1664  uint32_t GetVTableIndex() const { return vtable_index_; }
1665
1666  DECLARE_INSTRUCTION(InvokeVirtual);
1667
1668 private:
1669  const uint32_t vtable_index_;
1670
1671  DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1672};
1673
1674class HInvokeInterface : public HInvoke {
1675 public:
1676  HInvokeInterface(ArenaAllocator* arena,
1677                   uint32_t number_of_arguments,
1678                   Primitive::Type return_type,
1679                   uint32_t dex_pc,
1680                   uint32_t dex_method_index,
1681                   uint32_t imt_index)
1682      : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1683        dex_method_index_(dex_method_index),
1684        imt_index_(imt_index) {}
1685
1686  uint32_t GetImtIndex() const { return imt_index_; }
1687  uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1688
1689  DECLARE_INSTRUCTION(InvokeInterface);
1690
1691 private:
1692  const uint32_t dex_method_index_;
1693  const uint32_t imt_index_;
1694
1695  DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1696};
1697
1698class HNewInstance : public HExpression<0> {
1699 public:
1700  HNewInstance(uint32_t dex_pc, uint16_t type_index)
1701      : HExpression(Primitive::kPrimNot, SideEffects::None()),
1702        dex_pc_(dex_pc),
1703        type_index_(type_index) {}
1704
1705  uint32_t GetDexPc() const { return dex_pc_; }
1706  uint16_t GetTypeIndex() const { return type_index_; }
1707
1708  // Calls runtime so needs an environment.
1709  bool NeedsEnvironment() const OVERRIDE { return true; }
1710  // It may throw when called on:
1711  //   - interfaces
1712  //   - abstract/innaccessible/unknown classes
1713  // TODO: optimize when possible.
1714  bool CanThrow() const OVERRIDE { return true; }
1715
1716  DECLARE_INSTRUCTION(NewInstance);
1717
1718 private:
1719  const uint32_t dex_pc_;
1720  const uint16_t type_index_;
1721
1722  DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1723};
1724
1725class HNeg : public HUnaryOperation {
1726 public:
1727  explicit HNeg(Primitive::Type result_type, HInstruction* input)
1728      : HUnaryOperation(result_type, input) {}
1729
1730  virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1731  virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1732
1733  DECLARE_INSTRUCTION(Neg);
1734
1735 private:
1736  DISALLOW_COPY_AND_ASSIGN(HNeg);
1737};
1738
1739class HNewArray : public HExpression<1> {
1740 public:
1741  HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1742      : HExpression(Primitive::kPrimNot, SideEffects::None()),
1743        dex_pc_(dex_pc),
1744        type_index_(type_index) {
1745    SetRawInputAt(0, length);
1746  }
1747
1748  uint32_t GetDexPc() const { return dex_pc_; }
1749  uint16_t GetTypeIndex() const { return type_index_; }
1750
1751  // Calls runtime so needs an environment.
1752  virtual bool NeedsEnvironment() const { return true; }
1753
1754  DECLARE_INSTRUCTION(NewArray);
1755
1756 private:
1757  const uint32_t dex_pc_;
1758  const uint16_t type_index_;
1759
1760  DISALLOW_COPY_AND_ASSIGN(HNewArray);
1761};
1762
1763class HAdd : public HBinaryOperation {
1764 public:
1765  HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1766      : HBinaryOperation(result_type, left, right) {}
1767
1768  virtual bool IsCommutative() { return true; }
1769
1770  virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1771    return x + y;
1772  }
1773  virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1774    return x + y;
1775  }
1776
1777  DECLARE_INSTRUCTION(Add);
1778
1779 private:
1780  DISALLOW_COPY_AND_ASSIGN(HAdd);
1781};
1782
1783class HSub : public HBinaryOperation {
1784 public:
1785  HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1786      : HBinaryOperation(result_type, left, right) {}
1787
1788  virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1789    return x - y;
1790  }
1791  virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1792    return x - y;
1793  }
1794
1795  DECLARE_INSTRUCTION(Sub);
1796
1797 private:
1798  DISALLOW_COPY_AND_ASSIGN(HSub);
1799};
1800
1801class HMul : public HBinaryOperation {
1802 public:
1803  HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1804      : HBinaryOperation(result_type, left, right) {}
1805
1806  virtual bool IsCommutative() { return true; }
1807
1808  virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1809  virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1810
1811  DECLARE_INSTRUCTION(Mul);
1812
1813 private:
1814  DISALLOW_COPY_AND_ASSIGN(HMul);
1815};
1816
1817class HDiv : public HBinaryOperation {
1818 public:
1819  HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1820      : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1821
1822  virtual int32_t Evaluate(int32_t x, int32_t y) const {
1823    // Our graph structure ensures we never have 0 for `y` during constant folding.
1824    DCHECK_NE(y, 0);
1825    // Special case -1 to avoid getting a SIGFPE on x86(_64).
1826    return (y == -1) ? -x : x / y;
1827  }
1828
1829  virtual int64_t Evaluate(int64_t x, int64_t y) const {
1830    DCHECK_NE(y, 0);
1831    // Special case -1 to avoid getting a SIGFPE on x86(_64).
1832    return (y == -1) ? -x : x / y;
1833  }
1834
1835  uint32_t GetDexPc() const { return dex_pc_; }
1836
1837  DECLARE_INSTRUCTION(Div);
1838
1839 private:
1840  const uint32_t dex_pc_;
1841
1842  DISALLOW_COPY_AND_ASSIGN(HDiv);
1843};
1844
1845class HRem : public HBinaryOperation {
1846 public:
1847  HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1848      : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1849
1850  virtual int32_t Evaluate(int32_t x, int32_t y) const {
1851    DCHECK_NE(y, 0);
1852    // Special case -1 to avoid getting a SIGFPE on x86(_64).
1853    return (y == -1) ? 0 : x % y;
1854  }
1855
1856  virtual int64_t Evaluate(int64_t x, int64_t y) const {
1857    DCHECK_NE(y, 0);
1858    // Special case -1 to avoid getting a SIGFPE on x86(_64).
1859    return (y == -1) ? 0 : x % y;
1860  }
1861
1862  uint32_t GetDexPc() const { return dex_pc_; }
1863
1864  DECLARE_INSTRUCTION(Rem);
1865
1866 private:
1867  const uint32_t dex_pc_;
1868
1869  DISALLOW_COPY_AND_ASSIGN(HRem);
1870};
1871
1872class HDivZeroCheck : public HExpression<1> {
1873 public:
1874  HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1875      : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1876    SetRawInputAt(0, value);
1877  }
1878
1879  bool CanBeMoved() const OVERRIDE { return true; }
1880
1881  bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1882    UNUSED(other);
1883    return true;
1884  }
1885
1886  bool NeedsEnvironment() const OVERRIDE { return true; }
1887  bool CanThrow() const OVERRIDE { return true; }
1888
1889  uint32_t GetDexPc() const { return dex_pc_; }
1890
1891  DECLARE_INSTRUCTION(DivZeroCheck);
1892
1893 private:
1894  const uint32_t dex_pc_;
1895
1896  DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1897};
1898
1899class HShl : public HBinaryOperation {
1900 public:
1901  HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1902      : HBinaryOperation(result_type, left, right) {}
1903
1904  int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1905  int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1906
1907  DECLARE_INSTRUCTION(Shl);
1908
1909 private:
1910  DISALLOW_COPY_AND_ASSIGN(HShl);
1911};
1912
1913class HShr : public HBinaryOperation {
1914 public:
1915  HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1916      : HBinaryOperation(result_type, left, right) {}
1917
1918  int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1919  int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1920
1921  DECLARE_INSTRUCTION(Shr);
1922
1923 private:
1924  DISALLOW_COPY_AND_ASSIGN(HShr);
1925};
1926
1927class HUShr : public HBinaryOperation {
1928 public:
1929  HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1930      : HBinaryOperation(result_type, left, right) {}
1931
1932  int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1933    uint32_t ux = static_cast<uint32_t>(x);
1934    uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1935    return static_cast<int32_t>(ux >> uy);
1936  }
1937
1938  int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1939    uint64_t ux = static_cast<uint64_t>(x);
1940    uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1941    return static_cast<int64_t>(ux >> uy);
1942  }
1943
1944  DECLARE_INSTRUCTION(UShr);
1945
1946 private:
1947  DISALLOW_COPY_AND_ASSIGN(HUShr);
1948};
1949
1950class HAnd : public HBinaryOperation {
1951 public:
1952  HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1953      : HBinaryOperation(result_type, left, right) {}
1954
1955  bool IsCommutative() OVERRIDE { return true; }
1956
1957  int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1958  int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1959
1960  DECLARE_INSTRUCTION(And);
1961
1962 private:
1963  DISALLOW_COPY_AND_ASSIGN(HAnd);
1964};
1965
1966class HOr : public HBinaryOperation {
1967 public:
1968  HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1969      : HBinaryOperation(result_type, left, right) {}
1970
1971  bool IsCommutative() OVERRIDE { return true; }
1972
1973  int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1974  int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1975
1976  DECLARE_INSTRUCTION(Or);
1977
1978 private:
1979  DISALLOW_COPY_AND_ASSIGN(HOr);
1980};
1981
1982class HXor : public HBinaryOperation {
1983 public:
1984  HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1985      : HBinaryOperation(result_type, left, right) {}
1986
1987  bool IsCommutative() OVERRIDE { return true; }
1988
1989  int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1990  int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1991
1992  DECLARE_INSTRUCTION(Xor);
1993
1994 private:
1995  DISALLOW_COPY_AND_ASSIGN(HXor);
1996};
1997
1998// The value of a parameter in this method. Its location depends on
1999// the calling convention.
2000class HParameterValue : public HExpression<0> {
2001 public:
2002  HParameterValue(uint8_t index, Primitive::Type parameter_type)
2003      : HExpression(parameter_type, SideEffects::None()), index_(index) {}
2004
2005  uint8_t GetIndex() const { return index_; }
2006
2007  DECLARE_INSTRUCTION(ParameterValue);
2008
2009 private:
2010  // The index of this parameter in the parameters list. Must be less
2011  // than HGraph::number_of_in_vregs_;
2012  const uint8_t index_;
2013
2014  DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2015};
2016
2017class HNot : public HUnaryOperation {
2018 public:
2019  explicit HNot(Primitive::Type result_type, HInstruction* input)
2020      : HUnaryOperation(result_type, input) {}
2021
2022  virtual bool CanBeMoved() const { return true; }
2023  virtual bool InstructionDataEquals(HInstruction* other) const {
2024    UNUSED(other);
2025    return true;
2026  }
2027
2028  virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2029  virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2030
2031  DECLARE_INSTRUCTION(Not);
2032
2033 private:
2034  DISALLOW_COPY_AND_ASSIGN(HNot);
2035};
2036
2037class HTypeConversion : public HExpression<1> {
2038 public:
2039  // Instantiate a type conversion of `input` to `result_type`.
2040  HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2041      : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
2042    SetRawInputAt(0, input);
2043    DCHECK_NE(input->GetType(), result_type);
2044  }
2045
2046  HInstruction* GetInput() const { return InputAt(0); }
2047  Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2048  Primitive::Type GetResultType() const { return GetType(); }
2049
2050  // Required by the x86 and ARM code generators when producing calls
2051  // to the runtime.
2052  uint32_t GetDexPc() const { return dex_pc_; }
2053
2054  bool CanBeMoved() const OVERRIDE { return true; }
2055  bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
2056
2057  DECLARE_INSTRUCTION(TypeConversion);
2058
2059 private:
2060  const uint32_t dex_pc_;
2061
2062  DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2063};
2064
2065class HPhi : public HInstruction {
2066 public:
2067  HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
2068      : HInstruction(SideEffects::None()),
2069        inputs_(arena, number_of_inputs),
2070        reg_number_(reg_number),
2071        type_(type),
2072        is_live_(false) {
2073    inputs_.SetSize(number_of_inputs);
2074  }
2075
2076  virtual size_t InputCount() const { return inputs_.Size(); }
2077  virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2078
2079  virtual void SetRawInputAt(size_t index, HInstruction* input) {
2080    inputs_.Put(index, input);
2081  }
2082
2083  void AddInput(HInstruction* input);
2084
2085  virtual Primitive::Type GetType() const { return type_; }
2086  void SetType(Primitive::Type type) { type_ = type; }
2087
2088  uint32_t GetRegNumber() const { return reg_number_; }
2089
2090  void SetDead() { is_live_ = false; }
2091  void SetLive() { is_live_ = true; }
2092  bool IsDead() const { return !is_live_; }
2093  bool IsLive() const { return is_live_; }
2094
2095  DECLARE_INSTRUCTION(Phi);
2096
2097 private:
2098  GrowableArray<HInstruction*> inputs_;
2099  const uint32_t reg_number_;
2100  Primitive::Type type_;
2101  bool is_live_;
2102
2103  DISALLOW_COPY_AND_ASSIGN(HPhi);
2104};
2105
2106class HNullCheck : public HExpression<1> {
2107 public:
2108  HNullCheck(HInstruction* value, uint32_t dex_pc)
2109      : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2110    SetRawInputAt(0, value);
2111  }
2112
2113  virtual bool CanBeMoved() const { return true; }
2114  virtual bool InstructionDataEquals(HInstruction* other) const {
2115    UNUSED(other);
2116    return true;
2117  }
2118
2119  virtual bool NeedsEnvironment() const { return true; }
2120
2121  virtual bool CanThrow() const { return true; }
2122
2123  uint32_t GetDexPc() const { return dex_pc_; }
2124
2125  DECLARE_INSTRUCTION(NullCheck);
2126
2127 private:
2128  const uint32_t dex_pc_;
2129
2130  DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2131};
2132
2133class FieldInfo : public ValueObject {
2134 public:
2135  FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
2136      : field_offset_(field_offset), field_type_(field_type) {}
2137
2138  MemberOffset GetFieldOffset() const { return field_offset_; }
2139  Primitive::Type GetFieldType() const { return field_type_; }
2140
2141 private:
2142  const MemberOffset field_offset_;
2143  const Primitive::Type field_type_;
2144};
2145
2146class HInstanceFieldGet : public HExpression<1> {
2147 public:
2148  HInstanceFieldGet(HInstruction* value,
2149                    Primitive::Type field_type,
2150                    MemberOffset field_offset)
2151      : HExpression(field_type, SideEffects::DependsOnSomething()),
2152        field_info_(field_offset, field_type) {
2153    SetRawInputAt(0, value);
2154  }
2155
2156  virtual bool CanBeMoved() const { return true; }
2157  virtual bool InstructionDataEquals(HInstruction* other) const {
2158    size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2159    return other_offset == GetFieldOffset().SizeValue();
2160  }
2161
2162  virtual size_t ComputeHashCode() const {
2163    return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2164  }
2165
2166  MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2167  Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2168
2169  DECLARE_INSTRUCTION(InstanceFieldGet);
2170
2171 private:
2172  const FieldInfo field_info_;
2173
2174  DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2175};
2176
2177class HInstanceFieldSet : public HTemplateInstruction<2> {
2178 public:
2179  HInstanceFieldSet(HInstruction* object,
2180                    HInstruction* value,
2181                    Primitive::Type field_type,
2182                    MemberOffset field_offset)
2183      : HTemplateInstruction(SideEffects::ChangesSomething()),
2184        field_info_(field_offset, field_type) {
2185    SetRawInputAt(0, object);
2186    SetRawInputAt(1, value);
2187  }
2188
2189  MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2190  Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2191
2192  HInstruction* GetValue() const { return InputAt(1); }
2193
2194  DECLARE_INSTRUCTION(InstanceFieldSet);
2195
2196 private:
2197  const FieldInfo field_info_;
2198
2199  DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2200};
2201
2202class HArrayGet : public HExpression<2> {
2203 public:
2204  HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
2205      : HExpression(type, SideEffects::DependsOnSomething()) {
2206    SetRawInputAt(0, array);
2207    SetRawInputAt(1, index);
2208  }
2209
2210  bool CanBeMoved() const OVERRIDE { return true; }
2211  bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2212    UNUSED(other);
2213    return true;
2214  }
2215  void SetType(Primitive::Type type) { type_ = type; }
2216
2217  HInstruction* GetArray() const { return InputAt(0); }
2218  HInstruction* GetIndex() const { return InputAt(1); }
2219
2220  DECLARE_INSTRUCTION(ArrayGet);
2221
2222 private:
2223  DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2224};
2225
2226class HArraySet : public HTemplateInstruction<3> {
2227 public:
2228  HArraySet(HInstruction* array,
2229            HInstruction* index,
2230            HInstruction* value,
2231            Primitive::Type expected_component_type,
2232            uint32_t dex_pc)
2233      : HTemplateInstruction(SideEffects::ChangesSomething()),
2234        dex_pc_(dex_pc),
2235        expected_component_type_(expected_component_type),
2236        needs_type_check_(value->GetType() == Primitive::kPrimNot) {
2237    SetRawInputAt(0, array);
2238    SetRawInputAt(1, index);
2239    SetRawInputAt(2, value);
2240  }
2241
2242  bool NeedsEnvironment() const {
2243    // We currently always call a runtime method to catch array store
2244    // exceptions.
2245    return needs_type_check_;
2246  }
2247
2248  void ClearNeedsTypeCheck() {
2249    needs_type_check_ = false;
2250  }
2251
2252  bool NeedsTypeCheck() const { return needs_type_check_; }
2253
2254  uint32_t GetDexPc() const { return dex_pc_; }
2255
2256  HInstruction* GetArray() const { return InputAt(0); }
2257  HInstruction* GetIndex() const { return InputAt(1); }
2258  HInstruction* GetValue() const { return InputAt(2); }
2259
2260  Primitive::Type GetComponentType() const {
2261    // The Dex format does not type floating point index operations. Since the
2262    // `expected_component_type_` is set during building and can therefore not
2263    // be correct, we also check what is the value type. If it is a floating
2264    // point type, we must use that type.
2265    Primitive::Type value_type = GetValue()->GetType();
2266    return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2267        ? value_type
2268        : expected_component_type_;
2269  }
2270
2271  DECLARE_INSTRUCTION(ArraySet);
2272
2273 private:
2274  const uint32_t dex_pc_;
2275  const Primitive::Type expected_component_type_;
2276  bool needs_type_check_;
2277
2278  DISALLOW_COPY_AND_ASSIGN(HArraySet);
2279};
2280
2281class HArrayLength : public HExpression<1> {
2282 public:
2283  explicit HArrayLength(HInstruction* array)
2284      : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2285    // Note that arrays do not change length, so the instruction does not
2286    // depend on any write.
2287    SetRawInputAt(0, array);
2288  }
2289
2290  virtual bool CanBeMoved() const { return true; }
2291  virtual bool InstructionDataEquals(HInstruction* other) const {
2292    UNUSED(other);
2293    return true;
2294  }
2295
2296  DECLARE_INSTRUCTION(ArrayLength);
2297
2298 private:
2299  DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2300};
2301
2302class HBoundsCheck : public HExpression<2> {
2303 public:
2304  HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
2305      : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2306    DCHECK(index->GetType() == Primitive::kPrimInt);
2307    SetRawInputAt(0, index);
2308    SetRawInputAt(1, length);
2309  }
2310
2311  virtual bool CanBeMoved() const { return true; }
2312  virtual bool InstructionDataEquals(HInstruction* other) const {
2313    UNUSED(other);
2314    return true;
2315  }
2316
2317  virtual bool NeedsEnvironment() const { return true; }
2318
2319  virtual bool CanThrow() const { return true; }
2320
2321  uint32_t GetDexPc() const { return dex_pc_; }
2322
2323  DECLARE_INSTRUCTION(BoundsCheck);
2324
2325 private:
2326  const uint32_t dex_pc_;
2327
2328  DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2329};
2330
2331/**
2332 * Some DEX instructions are folded into multiple HInstructions that need
2333 * to stay live until the last HInstruction. This class
2334 * is used as a marker for the baseline compiler to ensure its preceding
2335 * HInstruction stays live. `index` represents the stack location index of the
2336 * instruction (the actual offset is computed as index * vreg_size).
2337 */
2338class HTemporary : public HTemplateInstruction<0> {
2339 public:
2340  explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
2341
2342  size_t GetIndex() const { return index_; }
2343
2344  Primitive::Type GetType() const OVERRIDE {
2345    // The previous instruction is the one that will be stored in the temporary location.
2346    DCHECK(GetPrevious() != nullptr);
2347    return GetPrevious()->GetType();
2348  }
2349
2350  DECLARE_INSTRUCTION(Temporary);
2351
2352 private:
2353  const size_t index_;
2354
2355  DISALLOW_COPY_AND_ASSIGN(HTemporary);
2356};
2357
2358class HSuspendCheck : public HTemplateInstruction<0> {
2359 public:
2360  explicit HSuspendCheck(uint32_t dex_pc)
2361      : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
2362
2363  virtual bool NeedsEnvironment() const {
2364    return true;
2365  }
2366
2367  uint32_t GetDexPc() const { return dex_pc_; }
2368
2369  DECLARE_INSTRUCTION(SuspendCheck);
2370
2371 private:
2372  const uint32_t dex_pc_;
2373
2374  DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2375};
2376
2377/**
2378 * Instruction to load a Class object.
2379 */
2380class HLoadClass : public HExpression<0> {
2381 public:
2382  HLoadClass(uint16_t type_index,
2383             bool is_referrers_class,
2384             uint32_t dex_pc)
2385      : HExpression(Primitive::kPrimNot, SideEffects::None()),
2386        type_index_(type_index),
2387        is_referrers_class_(is_referrers_class),
2388        dex_pc_(dex_pc),
2389        generate_clinit_check_(false) {}
2390
2391  bool CanBeMoved() const OVERRIDE { return true; }
2392
2393  bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2394    return other->AsLoadClass()->type_index_ == type_index_;
2395  }
2396
2397  size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2398
2399  uint32_t GetDexPc() const { return dex_pc_; }
2400  uint16_t GetTypeIndex() const { return type_index_; }
2401  bool IsReferrersClass() const { return is_referrers_class_; }
2402
2403  bool NeedsEnvironment() const OVERRIDE {
2404    // Will call runtime and load the class if the class is not loaded yet.
2405    // TODO: finer grain decision.
2406    return !is_referrers_class_;
2407  }
2408
2409  bool MustGenerateClinitCheck() const {
2410    return generate_clinit_check_;
2411  }
2412
2413  void SetMustGenerateClinitCheck() {
2414    generate_clinit_check_ = true;
2415  }
2416
2417  bool CanCallRuntime() const {
2418    return MustGenerateClinitCheck() || !is_referrers_class_;
2419  }
2420
2421  DECLARE_INSTRUCTION(LoadClass);
2422
2423 private:
2424  const uint16_t type_index_;
2425  const bool is_referrers_class_;
2426  const uint32_t dex_pc_;
2427  // Whether this instruction must generate the initialization check.
2428  // Used for code generation.
2429  bool generate_clinit_check_;
2430
2431  DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2432};
2433
2434class HLoadString : public HExpression<0> {
2435 public:
2436  HLoadString(uint32_t string_index, uint32_t dex_pc)
2437      : HExpression(Primitive::kPrimNot, SideEffects::None()),
2438        string_index_(string_index),
2439        dex_pc_(dex_pc) {}
2440
2441  bool CanBeMoved() const OVERRIDE { return true; }
2442
2443  bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2444    return other->AsLoadString()->string_index_ == string_index_;
2445  }
2446
2447  size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2448
2449  uint32_t GetDexPc() const { return dex_pc_; }
2450  uint32_t GetStringIndex() const { return string_index_; }
2451
2452  // TODO: Can we deopt or debug when we resolve a string?
2453  bool NeedsEnvironment() const OVERRIDE { return false; }
2454
2455  DECLARE_INSTRUCTION(LoadString);
2456
2457 private:
2458  const uint32_t string_index_;
2459  const uint32_t dex_pc_;
2460
2461  DISALLOW_COPY_AND_ASSIGN(HLoadString);
2462};
2463
2464// TODO: Pass this check to HInvokeStaticOrDirect nodes.
2465/**
2466 * Performs an initialization check on its Class object input.
2467 */
2468class HClinitCheck : public HExpression<1> {
2469 public:
2470  explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2471      : HExpression(Primitive::kPrimNot, SideEffects::All()),
2472        dex_pc_(dex_pc) {
2473    SetRawInputAt(0, constant);
2474  }
2475
2476  bool CanBeMoved() const OVERRIDE { return true; }
2477  bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2478    UNUSED(other);
2479    return true;
2480  }
2481
2482  bool NeedsEnvironment() const OVERRIDE {
2483    // May call runtime to initialize the class.
2484    return true;
2485  }
2486
2487  uint32_t GetDexPc() const { return dex_pc_; }
2488
2489  HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2490
2491  DECLARE_INSTRUCTION(ClinitCheck);
2492
2493 private:
2494  const uint32_t dex_pc_;
2495
2496  DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2497};
2498
2499class HStaticFieldGet : public HExpression<1> {
2500 public:
2501  HStaticFieldGet(HInstruction* cls,
2502                  Primitive::Type field_type,
2503                  MemberOffset field_offset)
2504      : HExpression(field_type, SideEffects::DependsOnSomething()),
2505        field_info_(field_offset, field_type) {
2506    SetRawInputAt(0, cls);
2507  }
2508
2509  bool CanBeMoved() const OVERRIDE { return true; }
2510  bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2511    size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2512    return other_offset == GetFieldOffset().SizeValue();
2513  }
2514
2515  size_t ComputeHashCode() const OVERRIDE {
2516    return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2517  }
2518
2519  MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2520  Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2521
2522  DECLARE_INSTRUCTION(StaticFieldGet);
2523
2524 private:
2525  const FieldInfo field_info_;
2526
2527  DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2528};
2529
2530class HStaticFieldSet : public HTemplateInstruction<2> {
2531 public:
2532  HStaticFieldSet(HInstruction* cls,
2533                  HInstruction* value,
2534                  Primitive::Type field_type,
2535                  MemberOffset field_offset)
2536      : HTemplateInstruction(SideEffects::ChangesSomething()),
2537        field_info_(field_offset, field_type) {
2538    SetRawInputAt(0, cls);
2539    SetRawInputAt(1, value);
2540  }
2541
2542  MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2543  Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2544
2545  HInstruction* GetValue() const { return InputAt(1); }
2546
2547  DECLARE_INSTRUCTION(StaticFieldSet);
2548
2549 private:
2550  const FieldInfo field_info_;
2551
2552  DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2553};
2554
2555// Implement the move-exception DEX instruction.
2556class HLoadException : public HExpression<0> {
2557 public:
2558  HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2559
2560  DECLARE_INSTRUCTION(LoadException);
2561
2562 private:
2563  DISALLOW_COPY_AND_ASSIGN(HLoadException);
2564};
2565
2566class HThrow : public HTemplateInstruction<1> {
2567 public:
2568  HThrow(HInstruction* exception, uint32_t dex_pc)
2569      : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2570    SetRawInputAt(0, exception);
2571  }
2572
2573  bool IsControlFlow() const OVERRIDE { return true; }
2574
2575  bool NeedsEnvironment() const OVERRIDE { return true; }
2576
2577  uint32_t GetDexPc() const { return dex_pc_; }
2578
2579  DECLARE_INSTRUCTION(Throw);
2580
2581 private:
2582  uint32_t dex_pc_;
2583
2584  DISALLOW_COPY_AND_ASSIGN(HThrow);
2585};
2586
2587class HInstanceOf : public HExpression<2> {
2588 public:
2589  HInstanceOf(HInstruction* object,
2590              HLoadClass* constant,
2591              bool class_is_final,
2592              uint32_t dex_pc)
2593      : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2594        class_is_final_(class_is_final),
2595        dex_pc_(dex_pc) {
2596    SetRawInputAt(0, object);
2597    SetRawInputAt(1, constant);
2598  }
2599
2600  bool CanBeMoved() const OVERRIDE { return true; }
2601
2602  bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2603    return true;
2604  }
2605
2606  bool NeedsEnvironment() const OVERRIDE {
2607    return false;
2608  }
2609
2610  uint32_t GetDexPc() const { return dex_pc_; }
2611
2612  bool IsClassFinal() const { return class_is_final_; }
2613
2614  DECLARE_INSTRUCTION(InstanceOf);
2615
2616 private:
2617  const bool class_is_final_;
2618  const uint32_t dex_pc_;
2619
2620  DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2621};
2622
2623class HCheckCast : public HTemplateInstruction<2> {
2624 public:
2625  HCheckCast(HInstruction* object,
2626             HLoadClass* constant,
2627             bool class_is_final,
2628             uint32_t dex_pc)
2629      : HTemplateInstruction(SideEffects::None()),
2630        class_is_final_(class_is_final),
2631        dex_pc_(dex_pc) {
2632    SetRawInputAt(0, object);
2633    SetRawInputAt(1, constant);
2634  }
2635
2636  bool CanBeMoved() const OVERRIDE { return true; }
2637
2638  bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2639    return true;
2640  }
2641
2642  bool NeedsEnvironment() const OVERRIDE {
2643    // Instruction may throw a CheckCastError.
2644    return true;
2645  }
2646
2647  bool CanThrow() const OVERRIDE { return true; }
2648
2649  uint32_t GetDexPc() const { return dex_pc_; }
2650
2651  bool IsClassFinal() const { return class_is_final_; }
2652
2653  DECLARE_INSTRUCTION(CheckCast);
2654
2655 private:
2656  const bool class_is_final_;
2657  const uint32_t dex_pc_;
2658
2659  DISALLOW_COPY_AND_ASSIGN(HCheckCast);
2660};
2661
2662class HMonitorOperation : public HTemplateInstruction<1> {
2663 public:
2664  enum OperationKind {
2665    kEnter,
2666    kExit,
2667  };
2668
2669  HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2670    : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2671    SetRawInputAt(0, object);
2672  }
2673
2674  // Instruction may throw a Java exception, so we need an environment.
2675  bool NeedsEnvironment() const OVERRIDE { return true; }
2676  bool CanThrow() const OVERRIDE { return true; }
2677
2678  uint32_t GetDexPc() const { return dex_pc_; }
2679
2680  bool IsEnter() const { return kind_ == kEnter; }
2681
2682  DECLARE_INSTRUCTION(MonitorOperation);
2683
2684 protected:
2685  const OperationKind kind_;
2686  const uint32_t dex_pc_;
2687
2688 private:
2689  DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2690};
2691
2692
2693class MoveOperands : public ArenaObject<kArenaAllocMisc> {
2694 public:
2695  MoveOperands(Location source, Location destination, HInstruction* instruction)
2696      : source_(source), destination_(destination), instruction_(instruction) {}
2697
2698  Location GetSource() const { return source_; }
2699  Location GetDestination() const { return destination_; }
2700
2701  void SetSource(Location value) { source_ = value; }
2702  void SetDestination(Location value) { destination_ = value; }
2703
2704  // The parallel move resolver marks moves as "in-progress" by clearing the
2705  // destination (but not the source).
2706  Location MarkPending() {
2707    DCHECK(!IsPending());
2708    Location dest = destination_;
2709    destination_ = Location::NoLocation();
2710    return dest;
2711  }
2712
2713  void ClearPending(Location dest) {
2714    DCHECK(IsPending());
2715    destination_ = dest;
2716  }
2717
2718  bool IsPending() const {
2719    DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2720    return destination_.IsInvalid() && !source_.IsInvalid();
2721  }
2722
2723  // True if this blocks a move from the given location.
2724  bool Blocks(Location loc) const {
2725    return !IsEliminated() && source_.Equals(loc);
2726  }
2727
2728  // A move is redundant if it's been eliminated, if its source and
2729  // destination are the same, or if its destination is unneeded.
2730  bool IsRedundant() const {
2731    return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2732  }
2733
2734  // We clear both operands to indicate move that's been eliminated.
2735  void Eliminate() {
2736    source_ = destination_ = Location::NoLocation();
2737  }
2738
2739  bool IsEliminated() const {
2740    DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2741    return source_.IsInvalid();
2742  }
2743
2744  HInstruction* GetInstruction() const { return instruction_; }
2745
2746 private:
2747  Location source_;
2748  Location destination_;
2749  // The instruction this move is assocatied with. Null when this move is
2750  // for moving an input in the expected locations of user (including a phi user).
2751  // This is only used in debug mode, to ensure we do not connect interval siblings
2752  // in the same parallel move.
2753  HInstruction* instruction_;
2754
2755  DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2756};
2757
2758static constexpr size_t kDefaultNumberOfMoves = 4;
2759
2760class HParallelMove : public HTemplateInstruction<0> {
2761 public:
2762  explicit HParallelMove(ArenaAllocator* arena)
2763      : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
2764
2765  void AddMove(MoveOperands* move) {
2766    if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2767      for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2768        DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2769          << "Doing parallel moves for the same instruction.";
2770      }
2771    }
2772    moves_.Add(move);
2773  }
2774
2775  MoveOperands* MoveOperandsAt(size_t index) const {
2776    return moves_.Get(index);
2777  }
2778
2779  size_t NumMoves() const { return moves_.Size(); }
2780
2781  DECLARE_INSTRUCTION(ParallelMove);
2782
2783 private:
2784  GrowableArray<MoveOperands*> moves_;
2785
2786  DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2787};
2788
2789class HGraphVisitor : public ValueObject {
2790 public:
2791  explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2792  virtual ~HGraphVisitor() {}
2793
2794  virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
2795  virtual void VisitBasicBlock(HBasicBlock* block);
2796
2797  // Visit the graph following basic block insertion order.
2798  void VisitInsertionOrder();
2799
2800  // Visit the graph following dominator tree reverse post-order.
2801  void VisitReversePostOrder();
2802
2803  HGraph* GetGraph() const { return graph_; }
2804
2805  // Visit functions for instruction classes.
2806#define DECLARE_VISIT_INSTRUCTION(name, super)                                        \
2807  virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2808
2809  FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2810
2811#undef DECLARE_VISIT_INSTRUCTION
2812
2813 private:
2814  HGraph* const graph_;
2815
2816  DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2817};
2818
2819class HGraphDelegateVisitor : public HGraphVisitor {
2820 public:
2821  explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2822  virtual ~HGraphDelegateVisitor() {}
2823
2824  // Visit functions that delegate to to super class.
2825#define DECLARE_VISIT_INSTRUCTION(name, super)                                        \
2826  virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2827
2828  FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2829
2830#undef DECLARE_VISIT_INSTRUCTION
2831
2832 private:
2833  DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2834};
2835
2836class HInsertionOrderIterator : public ValueObject {
2837 public:
2838  explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2839
2840  bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2841  HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2842  void Advance() { ++index_; }
2843
2844 private:
2845  const HGraph& graph_;
2846  size_t index_;
2847
2848  DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2849};
2850
2851class HReversePostOrderIterator : public ValueObject {
2852 public:
2853  explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2854
2855  bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2856  HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
2857  void Advance() { ++index_; }
2858
2859 private:
2860  const HGraph& graph_;
2861  size_t index_;
2862
2863  DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
2864};
2865
2866class HPostOrderIterator : public ValueObject {
2867 public:
2868  explicit HPostOrderIterator(const HGraph& graph)
2869      : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
2870
2871  bool Done() const { return index_ == 0; }
2872  HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
2873  void Advance() { --index_; }
2874
2875 private:
2876  const HGraph& graph_;
2877  size_t index_;
2878
2879  DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
2880};
2881
2882}  // namespace art
2883
2884#endif  // ART_COMPILER_OPTIMIZING_NODES_H_
2885