Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 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 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 20 | #include "base/arena_containers.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 21 | #include "base/arena_object.h" |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 22 | #include "dex/compiler_enums.h" |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 23 | #include "entrypoints/quick/quick_entrypoints_enum.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 24 | #include "handle.h" |
| 25 | #include "handle_scope.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 26 | #include "invoke_type.h" |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 27 | #include "locations.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 28 | #include "mirror/class.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 29 | #include "offsets.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 30 | #include "primitive.h" |
Nicolas Geoffray | 0e33643 | 2014-02-26 18:24:38 +0000 | [diff] [blame] | 31 | #include "utils/arena_bit_vector.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 32 | #include "utils/growable_array.h" |
| 33 | |
| 34 | namespace art { |
| 35 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 36 | class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 37 | class HBasicBlock; |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 38 | class HCurrentMethod; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 39 | class HDoubleConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 40 | class HEnvironment; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 41 | class HFloatConstant; |
| 42 | class HGraphVisitor; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 43 | class HInstruction; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 44 | class HIntConstant; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 45 | class HInvoke; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 46 | class HLongConstant; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 47 | class HNullConstant; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 48 | class HPhi; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 49 | class HSuspendCheck; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 50 | class LiveInterval; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 51 | class LocationSummary; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 52 | class SlowPathCode; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 53 | class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 54 | |
| 55 | static const int kDefaultNumberOfBlocks = 8; |
| 56 | static const int kDefaultNumberOfSuccessors = 2; |
| 57 | static const int kDefaultNumberOfPredecessors = 2; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 58 | static const int kDefaultNumberOfDominatedBlocks = 1; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 59 | static const int kDefaultNumberOfBackEdges = 1; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 60 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 61 | static constexpr uint32_t kMaxIntShiftValue = 0x1f; |
| 62 | static constexpr uint64_t kMaxLongShiftValue = 0x3f; |
| 63 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 64 | static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1); |
| 65 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 66 | static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1); |
| 67 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 68 | enum IfCondition { |
| 69 | kCondEQ, |
| 70 | kCondNE, |
| 71 | kCondLT, |
| 72 | kCondLE, |
| 73 | kCondGT, |
| 74 | kCondGE, |
| 75 | }; |
| 76 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 77 | class HInstructionList { |
| 78 | public: |
| 79 | HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {} |
| 80 | |
| 81 | void AddInstruction(HInstruction* instruction); |
| 82 | void RemoveInstruction(HInstruction* instruction); |
| 83 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 84 | // Insert `instruction` before/after an existing instruction `cursor`. |
| 85 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
| 86 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
| 87 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 88 | // Return true if this list contains `instruction`. |
| 89 | bool Contains(HInstruction* instruction) const; |
| 90 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 91 | // Return true if `instruction1` is found before `instruction2` in |
| 92 | // this instruction list and false otherwise. Abort if none |
| 93 | // of these instructions is found. |
| 94 | bool FoundBefore(const HInstruction* instruction1, |
| 95 | const HInstruction* instruction2) const; |
| 96 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 97 | bool IsEmpty() const { return first_instruction_ == nullptr; } |
| 98 | void Clear() { first_instruction_ = last_instruction_ = nullptr; } |
| 99 | |
| 100 | // Update the block of all instructions to be `block`. |
| 101 | void SetBlockOfInstructions(HBasicBlock* block) const; |
| 102 | |
| 103 | void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list); |
| 104 | void Add(const HInstructionList& instruction_list); |
| 105 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 106 | // Return the number of instructions in the list. This is an expensive operation. |
| 107 | size_t CountSize() const; |
| 108 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 109 | private: |
| 110 | HInstruction* first_instruction_; |
| 111 | HInstruction* last_instruction_; |
| 112 | |
| 113 | friend class HBasicBlock; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 114 | friend class HGraph; |
| 115 | friend class HInstruction; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 116 | friend class HInstructionIterator; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 117 | friend class HBackwardInstructionIterator; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 118 | |
| 119 | DISALLOW_COPY_AND_ASSIGN(HInstructionList); |
| 120 | }; |
| 121 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 122 | // Control-flow graph of a method. Contains a list of basic blocks. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 123 | class HGraph : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 124 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 125 | HGraph(ArenaAllocator* arena, |
| 126 | const DexFile& dex_file, |
| 127 | uint32_t method_idx, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 128 | bool should_generate_constructor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 129 | InstructionSet instruction_set, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 130 | InvokeType invoke_type = kInvalidInvokeType, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 131 | bool debuggable = false, |
| 132 | int start_instruction_id = 0) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 133 | : arena_(arena), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 134 | blocks_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 135 | reverse_post_order_(arena, kDefaultNumberOfBlocks), |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 136 | linear_order_(arena, kDefaultNumberOfBlocks), |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 137 | entry_block_(nullptr), |
| 138 | exit_block_(nullptr), |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 139 | maximum_number_of_out_vregs_(0), |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 140 | number_of_vregs_(0), |
| 141 | number_of_in_vregs_(0), |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 142 | temporaries_vreg_slots_(0), |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 143 | has_bounds_checks_(false), |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 144 | debuggable_(debuggable), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 145 | current_instruction_id_(start_instruction_id), |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 146 | dex_file_(dex_file), |
| 147 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 148 | invoke_type_(invoke_type), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 149 | in_ssa_form_(false), |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 150 | should_generate_constructor_barrier_(should_generate_constructor_barrier), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 151 | instruction_set_(instruction_set), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 152 | cached_null_constant_(nullptr), |
| 153 | cached_int_constants_(std::less<int32_t>(), arena->Adapter()), |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 154 | cached_float_constants_(std::less<int32_t>(), arena->Adapter()), |
| 155 | cached_long_constants_(std::less<int64_t>(), arena->Adapter()), |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 156 | cached_double_constants_(std::less<int64_t>(), arena->Adapter()), |
| 157 | cached_current_method_(nullptr) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 158 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 159 | ArenaAllocator* GetArena() const { return arena_; } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 160 | const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; } |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 161 | HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 162 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 163 | HBasicBlock* GetEntryBlock() const { return entry_block_; } |
| 164 | HBasicBlock* GetExitBlock() const { return exit_block_; } |
David Brazdil | c7af85d | 2015-05-26 12:05:55 +0100 | [diff] [blame] | 165 | bool HasExitBlock() const { return exit_block_ != nullptr; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 166 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 167 | void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; } |
| 168 | void SetExitBlock(HBasicBlock* block) { exit_block_ = block; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 169 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 170 | void AddBlock(HBasicBlock* block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 171 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 172 | // Try building the SSA form of this graph, with dominance computation and loop |
| 173 | // recognition. Returns whether it was successful in doing all these steps. |
| 174 | bool TryBuildingSsa() { |
| 175 | BuildDominatorTree(); |
Nicolas Geoffray | d335083 | 2015-03-12 11:16:23 +0000 | [diff] [blame] | 176 | // The SSA builder requires loops to all be natural. Specifically, the dead phi |
| 177 | // elimination phase checks the consistency of the graph when doing a post-order |
| 178 | // visit for eliminating dead phis: a dead phi can only have loop header phi |
| 179 | // users remaining when being visited. |
| 180 | if (!AnalyzeNaturalLoops()) return false; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 181 | TransformToSsa(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 182 | in_ssa_form_ = true; |
Nicolas Geoffray | d335083 | 2015-03-12 11:16:23 +0000 | [diff] [blame] | 183 | return true; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 186 | void BuildDominatorTree(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 187 | void TransformToSsa(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 188 | void SimplifyCFG(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 189 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 190 | // Analyze all natural loops in this graph. Returns false if one |
| 191 | // loop is not natural, that is the header does not dominate the |
| 192 | // back edge. |
| 193 | bool AnalyzeNaturalLoops() const; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 194 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 195 | // Inline this graph in `outer_graph`, replacing the given `invoke` instruction. |
| 196 | void InlineInto(HGraph* outer_graph, HInvoke* invoke); |
| 197 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 198 | // Removes `block` from the graph. |
| 199 | void DeleteDeadBlock(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 200 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 201 | void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor); |
| 202 | void SimplifyLoop(HBasicBlock* header); |
| 203 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 204 | int32_t GetNextInstructionId() { |
| 205 | DCHECK_NE(current_instruction_id_, INT32_MAX); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 206 | return current_instruction_id_++; |
| 207 | } |
| 208 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 209 | int32_t GetCurrentInstructionId() const { |
| 210 | return current_instruction_id_; |
| 211 | } |
| 212 | |
| 213 | void SetCurrentInstructionId(int32_t id) { |
| 214 | current_instruction_id_ = id; |
| 215 | } |
| 216 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 217 | uint16_t GetMaximumNumberOfOutVRegs() const { |
| 218 | return maximum_number_of_out_vregs_; |
| 219 | } |
| 220 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 221 | void SetMaximumNumberOfOutVRegs(uint16_t new_value) { |
| 222 | maximum_number_of_out_vregs_ = new_value; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 225 | void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) { |
| 226 | maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value); |
| 227 | } |
| 228 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 229 | void UpdateTemporariesVRegSlots(size_t slots) { |
| 230 | temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 233 | size_t GetTemporariesVRegSlots() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 234 | DCHECK(!in_ssa_form_); |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 235 | return temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 238 | void SetNumberOfVRegs(uint16_t number_of_vregs) { |
| 239 | number_of_vregs_ = number_of_vregs; |
| 240 | } |
| 241 | |
| 242 | uint16_t GetNumberOfVRegs() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 243 | DCHECK(!in_ssa_form_); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 244 | return number_of_vregs_; |
| 245 | } |
| 246 | |
| 247 | void SetNumberOfInVRegs(uint16_t value) { |
| 248 | number_of_in_vregs_ = value; |
| 249 | } |
| 250 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 251 | uint16_t GetNumberOfLocalVRegs() const { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 252 | DCHECK(!in_ssa_form_); |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 253 | return number_of_vregs_ - number_of_in_vregs_; |
| 254 | } |
| 255 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 256 | const GrowableArray<HBasicBlock*>& GetReversePostOrder() const { |
| 257 | return reverse_post_order_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 258 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 259 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 260 | const GrowableArray<HBasicBlock*>& GetLinearOrder() const { |
| 261 | return linear_order_; |
| 262 | } |
| 263 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 264 | bool HasBoundsChecks() const { |
| 265 | return has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 268 | void SetHasBoundsChecks(bool value) { |
| 269 | has_bounds_checks_ = value; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 272 | bool ShouldGenerateConstructorBarrier() const { |
| 273 | return should_generate_constructor_barrier_; |
| 274 | } |
| 275 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 276 | bool IsDebuggable() const { return debuggable_; } |
| 277 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 278 | // Returns a constant of the given type and value. If it does not exist |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 279 | // already, it is created and inserted into the graph. This method is only for |
| 280 | // integral types. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 281 | HConstant* GetConstant(Primitive::Type type, int64_t value); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 282 | HNullConstant* GetNullConstant(); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 283 | HIntConstant* GetIntConstant(int32_t value) { |
| 284 | return CreateConstant(value, &cached_int_constants_); |
| 285 | } |
| 286 | HLongConstant* GetLongConstant(int64_t value) { |
| 287 | return CreateConstant(value, &cached_long_constants_); |
| 288 | } |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 289 | HFloatConstant* GetFloatConstant(float value) { |
| 290 | return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_); |
| 291 | } |
| 292 | HDoubleConstant* GetDoubleConstant(double value) { |
| 293 | return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_); |
| 294 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 295 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 296 | HCurrentMethod* GetCurrentMethod(); |
| 297 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 298 | HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 299 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 300 | const DexFile& GetDexFile() const { |
| 301 | return dex_file_; |
| 302 | } |
| 303 | |
| 304 | uint32_t GetMethodIdx() const { |
| 305 | return method_idx_; |
| 306 | } |
| 307 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 308 | InvokeType GetInvokeType() const { |
| 309 | return invoke_type_; |
| 310 | } |
| 311 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 312 | private: |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 313 | void VisitBlockForDominatorTree(HBasicBlock* block, |
| 314 | HBasicBlock* predecessor, |
| 315 | GrowableArray<size_t>* visits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 316 | void FindBackEdges(ArenaBitVector* visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 317 | void VisitBlockForBackEdges(HBasicBlock* block, |
| 318 | ArenaBitVector* visited, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 319 | ArenaBitVector* visiting); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 320 | void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 321 | void RemoveDeadBlocks(const ArenaBitVector& visited); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 322 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 323 | template <class InstructionType, typename ValueType> |
| 324 | InstructionType* CreateConstant(ValueType value, |
| 325 | ArenaSafeMap<ValueType, InstructionType*>* cache) { |
| 326 | // Try to find an existing constant of the given value. |
| 327 | InstructionType* constant = nullptr; |
| 328 | auto cached_constant = cache->find(value); |
| 329 | if (cached_constant != cache->end()) { |
| 330 | constant = cached_constant->second; |
| 331 | } |
| 332 | |
| 333 | // If not found or previously deleted, create and cache a new instruction. |
| 334 | if (constant == nullptr || constant->GetBlock() == nullptr) { |
| 335 | constant = new (arena_) InstructionType(value); |
| 336 | cache->Overwrite(value, constant); |
| 337 | InsertConstant(constant); |
| 338 | } |
| 339 | return constant; |
| 340 | } |
| 341 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 342 | void InsertConstant(HConstant* instruction); |
| 343 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 344 | // Cache a float constant into the graph. This method should only be |
| 345 | // called by the SsaBuilder when creating "equivalent" instructions. |
| 346 | void CacheFloatConstant(HFloatConstant* constant); |
| 347 | |
| 348 | // See CacheFloatConstant comment. |
| 349 | void CacheDoubleConstant(HDoubleConstant* constant); |
| 350 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 351 | ArenaAllocator* const arena_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 352 | |
| 353 | // List of blocks in insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 354 | GrowableArray<HBasicBlock*> blocks_; |
| 355 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 356 | // List of blocks to perform a reverse post order tree traversal. |
| 357 | GrowableArray<HBasicBlock*> reverse_post_order_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 358 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 359 | // List of blocks to perform a linear order tree traversal. |
| 360 | GrowableArray<HBasicBlock*> linear_order_; |
| 361 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 362 | HBasicBlock* entry_block_; |
| 363 | HBasicBlock* exit_block_; |
| 364 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 365 | // The maximum number of virtual registers arguments passed to a HInvoke in this graph. |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 366 | uint16_t maximum_number_of_out_vregs_; |
| 367 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 368 | // The number of virtual registers in this method. Contains the parameters. |
| 369 | uint16_t number_of_vregs_; |
| 370 | |
| 371 | // The number of virtual registers used by parameters of this method. |
| 372 | uint16_t number_of_in_vregs_; |
| 373 | |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 374 | // Number of vreg size slots that the temporaries use (used in baseline compiler). |
| 375 | size_t temporaries_vreg_slots_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 376 | |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 377 | // Has bounds checks. We can totally skip BCE if it's false. |
| 378 | bool has_bounds_checks_; |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 379 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 380 | // Indicates whether the graph should be compiled in a way that |
| 381 | // ensures full debuggability. If false, we can apply more |
| 382 | // aggressive optimizations that may limit the level of debugging. |
| 383 | const bool debuggable_; |
| 384 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 385 | // The current id to assign to a newly added instruction. See HInstruction.id_. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 386 | int32_t current_instruction_id_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 387 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 388 | // The dex file from which the method is from. |
| 389 | const DexFile& dex_file_; |
| 390 | |
| 391 | // The method index in the dex file. |
| 392 | const uint32_t method_idx_; |
| 393 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 394 | // If inlined, this encodes how the callee is being invoked. |
| 395 | const InvokeType invoke_type_; |
| 396 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 397 | // Whether the graph has been transformed to SSA form. Only used |
| 398 | // in debug mode to ensure we are not using properties only valid |
| 399 | // for non-SSA form (like the number of temporaries). |
| 400 | bool in_ssa_form_; |
| 401 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 402 | const bool should_generate_constructor_barrier_; |
| 403 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 404 | const InstructionSet instruction_set_; |
| 405 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 406 | // Cached constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 407 | HNullConstant* cached_null_constant_; |
| 408 | ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 409 | ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 410 | ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 411 | ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 412 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 413 | HCurrentMethod* cached_current_method_; |
| 414 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 415 | friend class SsaBuilder; // For caching constants. |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 416 | friend class SsaLivenessAnalysis; // For the linear order. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 417 | ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 418 | DISALLOW_COPY_AND_ASSIGN(HGraph); |
| 419 | }; |
| 420 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 421 | class HLoopInformation : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 422 | public: |
| 423 | HLoopInformation(HBasicBlock* header, HGraph* graph) |
| 424 | : header_(header), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 425 | suspend_check_(nullptr), |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 426 | back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges), |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 427 | // Make bit vector growable, as the number of blocks may change. |
| 428 | blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {} |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 429 | |
| 430 | HBasicBlock* GetHeader() const { |
| 431 | return header_; |
| 432 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 433 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 434 | void SetHeader(HBasicBlock* block) { |
| 435 | header_ = block; |
| 436 | } |
| 437 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 438 | HSuspendCheck* GetSuspendCheck() const { return suspend_check_; } |
| 439 | void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; } |
| 440 | bool HasSuspendCheck() const { return suspend_check_ != nullptr; } |
| 441 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 442 | void AddBackEdge(HBasicBlock* back_edge) { |
| 443 | back_edges_.Add(back_edge); |
| 444 | } |
| 445 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 446 | void RemoveBackEdge(HBasicBlock* back_edge) { |
| 447 | back_edges_.Delete(back_edge); |
| 448 | } |
| 449 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 450 | bool IsBackEdge(const HBasicBlock& block) const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 451 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 452 | if (back_edges_.Get(i) == &block) return true; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 453 | } |
| 454 | return false; |
| 455 | } |
| 456 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 457 | size_t NumberOfBackEdges() const { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 458 | return back_edges_.Size(); |
| 459 | } |
| 460 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 461 | HBasicBlock* GetPreHeader() const; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 462 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 463 | const GrowableArray<HBasicBlock*>& GetBackEdges() const { |
| 464 | return back_edges_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 467 | // Returns the lifetime position of the back edge that has the |
| 468 | // greatest lifetime position. |
| 469 | size_t GetLifetimeEnd() const; |
| 470 | |
| 471 | void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) { |
| 472 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
| 473 | if (back_edges_.Get(i) == existing) { |
| 474 | back_edges_.Put(i, new_back_edge); |
| 475 | return; |
| 476 | } |
| 477 | } |
| 478 | UNREACHABLE(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 481 | // Finds blocks that are part of this loop. Returns whether the loop is a natural loop, |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 482 | // that is the header dominates the back edge. |
| 483 | bool Populate(); |
| 484 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 485 | // Reanalyzes the loop by removing loop info from its blocks and re-running |
| 486 | // Populate(). If there are no back edges left, the loop info is completely |
| 487 | // removed as well as its SuspendCheck instruction. It must be run on nested |
| 488 | // inner loops first. |
| 489 | void Update(); |
| 490 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 491 | // Returns whether this loop information contains `block`. |
| 492 | // Note that this loop information *must* be populated before entering this function. |
| 493 | bool Contains(const HBasicBlock& block) const; |
| 494 | |
| 495 | // Returns whether this loop information is an inner loop of `other`. |
| 496 | // Note that `other` *must* be populated before entering this function. |
| 497 | bool IsIn(const HLoopInformation& other) const; |
| 498 | |
| 499 | const ArenaBitVector& GetBlocks() const { return blocks_; } |
| 500 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 501 | void Add(HBasicBlock* block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 502 | void Remove(HBasicBlock* block); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 503 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 504 | private: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 505 | // Internal recursive implementation of `Populate`. |
| 506 | void PopulateRecursive(HBasicBlock* block); |
| 507 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 508 | HBasicBlock* header_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 509 | HSuspendCheck* suspend_check_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 510 | GrowableArray<HBasicBlock*> back_edges_; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 511 | ArenaBitVector blocks_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 512 | |
| 513 | DISALLOW_COPY_AND_ASSIGN(HLoopInformation); |
| 514 | }; |
| 515 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 516 | static constexpr size_t kNoLifetime = -1; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 517 | static constexpr uint32_t kNoDexPc = -1; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 518 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 519 | // A block in a method. Contains the list of instructions represented |
| 520 | // as a double linked list. Each block knows its predecessors and |
| 521 | // successors. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 522 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 523 | class HBasicBlock : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 524 | public: |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 525 | explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc) |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 526 | : graph_(graph), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 527 | predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors), |
| 528 | successors_(graph->GetArena(), kDefaultNumberOfSuccessors), |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 529 | loop_information_(nullptr), |
| 530 | dominator_(nullptr), |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 531 | dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 532 | block_id_(-1), |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 533 | dex_pc_(dex_pc), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 534 | lifetime_start_(kNoLifetime), |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 535 | lifetime_end_(kNoLifetime), |
| 536 | is_catch_block_(false) {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 537 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 538 | const GrowableArray<HBasicBlock*>& GetPredecessors() const { |
| 539 | return predecessors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 542 | const GrowableArray<HBasicBlock*>& GetSuccessors() const { |
| 543 | return successors_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 546 | const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const { |
| 547 | return dominated_blocks_; |
| 548 | } |
| 549 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 550 | bool IsEntryBlock() const { |
| 551 | return graph_->GetEntryBlock() == this; |
| 552 | } |
| 553 | |
| 554 | bool IsExitBlock() const { |
| 555 | return graph_->GetExitBlock() == this; |
| 556 | } |
| 557 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 558 | bool IsSingleGoto() const; |
| 559 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 560 | void AddBackEdge(HBasicBlock* back_edge) { |
| 561 | if (loop_information_ == nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 562 | loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 563 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 564 | DCHECK_EQ(loop_information_->GetHeader(), this); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 565 | loop_information_->AddBackEdge(back_edge); |
| 566 | } |
| 567 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 568 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 569 | void SetGraph(HGraph* graph) { graph_ = graph; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 570 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 571 | int GetBlockId() const { return block_id_; } |
| 572 | void SetBlockId(int id) { block_id_ = id; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 573 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 574 | HBasicBlock* GetDominator() const { return dominator_; } |
| 575 | void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 576 | void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 577 | void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 578 | void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) { |
| 579 | for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) { |
| 580 | if (dominated_blocks_.Get(i) == existing) { |
| 581 | dominated_blocks_.Put(i, new_block); |
| 582 | return; |
| 583 | } |
| 584 | } |
| 585 | LOG(FATAL) << "Unreachable"; |
| 586 | UNREACHABLE(); |
| 587 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 588 | |
| 589 | int NumberOfBackEdges() const { |
| 590 | return loop_information_ == nullptr |
| 591 | ? 0 |
| 592 | : loop_information_->NumberOfBackEdges(); |
| 593 | } |
| 594 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 595 | HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; } |
| 596 | HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 597 | const HInstructionList& GetInstructions() const { return instructions_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 598 | HInstruction* GetFirstPhi() const { return phis_.first_instruction_; } |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 599 | HInstruction* GetLastPhi() const { return phis_.last_instruction_; } |
| 600 | const HInstructionList& GetPhis() const { return phis_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 601 | |
| 602 | void AddSuccessor(HBasicBlock* block) { |
| 603 | successors_.Add(block); |
| 604 | block->predecessors_.Add(this); |
| 605 | } |
| 606 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 607 | void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 608 | size_t successor_index = GetSuccessorIndexOf(existing); |
| 609 | DCHECK_NE(successor_index, static_cast<size_t>(-1)); |
| 610 | existing->RemovePredecessor(this); |
| 611 | new_block->predecessors_.Add(this); |
| 612 | successors_.Put(successor_index, new_block); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 615 | void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) { |
| 616 | size_t predecessor_index = GetPredecessorIndexOf(existing); |
| 617 | DCHECK_NE(predecessor_index, static_cast<size_t>(-1)); |
| 618 | existing->RemoveSuccessor(this); |
| 619 | new_block->successors_.Add(this); |
| 620 | predecessors_.Put(predecessor_index, new_block); |
| 621 | } |
| 622 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 623 | void RemovePredecessor(HBasicBlock* block) { |
| 624 | predecessors_.Delete(block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 627 | void RemoveSuccessor(HBasicBlock* block) { |
| 628 | successors_.Delete(block); |
| 629 | } |
| 630 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 631 | void ClearAllPredecessors() { |
| 632 | predecessors_.Reset(); |
| 633 | } |
| 634 | |
| 635 | void AddPredecessor(HBasicBlock* block) { |
| 636 | predecessors_.Add(block); |
| 637 | block->successors_.Add(this); |
| 638 | } |
| 639 | |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 640 | void SwapPredecessors() { |
Nicolas Geoffray | c83d441 | 2014-09-18 16:46:20 +0100 | [diff] [blame] | 641 | DCHECK_EQ(predecessors_.Size(), 2u); |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 642 | HBasicBlock* temp = predecessors_.Get(0); |
| 643 | predecessors_.Put(0, predecessors_.Get(1)); |
| 644 | predecessors_.Put(1, temp); |
| 645 | } |
| 646 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 647 | void SwapSuccessors() { |
| 648 | DCHECK_EQ(successors_.Size(), 2u); |
| 649 | HBasicBlock* temp = successors_.Get(0); |
| 650 | successors_.Put(0, successors_.Get(1)); |
| 651 | successors_.Put(1, temp); |
| 652 | } |
| 653 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 654 | size_t GetPredecessorIndexOf(HBasicBlock* predecessor) { |
| 655 | for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) { |
| 656 | if (predecessors_.Get(i) == predecessor) { |
| 657 | return i; |
| 658 | } |
| 659 | } |
| 660 | return -1; |
| 661 | } |
| 662 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 663 | size_t GetSuccessorIndexOf(HBasicBlock* successor) { |
| 664 | for (size_t i = 0, e = successors_.Size(); i < e; ++i) { |
| 665 | if (successors_.Get(i) == successor) { |
| 666 | return i; |
| 667 | } |
| 668 | } |
| 669 | return -1; |
| 670 | } |
| 671 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 672 | // Split the block into two blocks just after `cursor`. Returns the newly |
| 673 | // created block. Note that this method just updates raw block information, |
| 674 | // like predecessors, successors, dominators, and instruction list. It does not |
| 675 | // update the graph, reverse post order, loop information, nor make sure the |
| 676 | // blocks are consistent (for example ending with a control flow instruction). |
| 677 | HBasicBlock* SplitAfter(HInstruction* cursor); |
| 678 | |
| 679 | // Merge `other` at the end of `this`. Successors and dominated blocks of |
| 680 | // `other` are changed to be successors and dominated blocks of `this`. Note |
| 681 | // that this method does not update the graph, reverse post order, loop |
| 682 | // information, nor make sure the blocks are consistent (for example ending |
| 683 | // with a control flow instruction). |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 684 | void MergeWithInlined(HBasicBlock* other); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 685 | |
| 686 | // Replace `this` with `other`. Predecessors, successors, and dominated blocks |
| 687 | // of `this` are moved to `other`. |
| 688 | // Note that this method does not update the graph, reverse post order, loop |
| 689 | // information, nor make sure the blocks are consistent (for example ending |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 690 | // with a control flow instruction). |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 691 | void ReplaceWith(HBasicBlock* other); |
| 692 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 693 | // Merge `other` at the end of `this`. This method updates loops, reverse post |
| 694 | // order, links to predecessors, successors, dominators and deletes the block |
| 695 | // from the graph. The two blocks must be successive, i.e. `this` the only |
| 696 | // predecessor of `other` and vice versa. |
| 697 | void MergeWith(HBasicBlock* other); |
| 698 | |
| 699 | // Disconnects `this` from all its predecessors, successors and dominator, |
| 700 | // removes it from all loops it is included in and eventually from the graph. |
| 701 | // The block must not dominate any other block. Predecessors and successors |
| 702 | // are safely updated. |
| 703 | void DisconnectAndDelete(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 704 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 705 | void AddInstruction(HInstruction* instruction); |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 706 | // Insert `instruction` before/after an existing instruction `cursor`. |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 707 | void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor); |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 708 | void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 709 | // Replace instruction `initial` with `replacement` within this block. |
| 710 | void ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 711 | HInstruction* replacement); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 712 | void AddPhi(HPhi* phi); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 713 | void InsertPhiAfter(HPhi* instruction, HPhi* cursor); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 714 | // RemoveInstruction and RemovePhi delete a given instruction from the respective |
| 715 | // instruction list. With 'ensure_safety' set to true, it verifies that the |
| 716 | // instruction is not in use and removes it from the use lists of its inputs. |
| 717 | void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true); |
| 718 | void RemovePhi(HPhi* phi, bool ensure_safety = true); |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 719 | void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 720 | |
| 721 | bool IsLoopHeader() const { |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 722 | return IsInLoop() && (loop_information_->GetHeader() == this); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 725 | bool IsLoopPreHeaderFirstPredecessor() const { |
| 726 | DCHECK(IsLoopHeader()); |
| 727 | DCHECK(!GetPredecessors().IsEmpty()); |
| 728 | return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader(); |
| 729 | } |
| 730 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 731 | HLoopInformation* GetLoopInformation() const { |
| 732 | return loop_information_; |
| 733 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 734 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 735 | // Set the loop_information_ on this block. Overrides the current |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 736 | // loop_information if it is an outer loop of the passed loop information. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 737 | // Note that this method is called while creating the loop information. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 738 | void SetInLoop(HLoopInformation* info) { |
| 739 | if (IsLoopHeader()) { |
| 740 | // Nothing to do. This just means `info` is an outer loop. |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 741 | } else if (!IsInLoop()) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 742 | loop_information_ = info; |
| 743 | } else if (loop_information_->Contains(*info->GetHeader())) { |
| 744 | // Block is currently part of an outer loop. Make it part of this inner loop. |
| 745 | // Note that a non loop header having a loop information means this loop information |
| 746 | // has already been populated |
| 747 | loop_information_ = info; |
| 748 | } else { |
| 749 | // Block is part of an inner loop. Do not update the loop information. |
| 750 | // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()` |
| 751 | // at this point, because this method is being called while populating `info`. |
| 752 | } |
| 753 | } |
| 754 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 755 | // Raw update of the loop information. |
| 756 | void SetLoopInformation(HLoopInformation* info) { |
| 757 | loop_information_ = info; |
| 758 | } |
| 759 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 760 | bool IsInLoop() const { return loop_information_ != nullptr; } |
| 761 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 762 | // Returns whether this block dominates the blocked passed as parameter. |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 763 | bool Dominates(HBasicBlock* block) const; |
| 764 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 765 | size_t GetLifetimeStart() const { return lifetime_start_; } |
| 766 | size_t GetLifetimeEnd() const { return lifetime_end_; } |
| 767 | |
| 768 | void SetLifetimeStart(size_t start) { lifetime_start_ = start; } |
| 769 | void SetLifetimeEnd(size_t end) { lifetime_end_ = end; } |
| 770 | |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 771 | uint32_t GetDexPc() const { return dex_pc_; } |
| 772 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 773 | bool IsCatchBlock() const { return is_catch_block_; } |
| 774 | void SetIsCatchBlock() { is_catch_block_ = true; } |
| 775 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 776 | bool EndsWithControlFlowInstruction() const; |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 777 | bool EndsWithIf() const; |
| 778 | bool HasSinglePhi() const; |
| 779 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 780 | private: |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 781 | HGraph* graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 782 | GrowableArray<HBasicBlock*> predecessors_; |
| 783 | GrowableArray<HBasicBlock*> successors_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 784 | HInstructionList instructions_; |
| 785 | HInstructionList phis_; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 786 | HLoopInformation* loop_information_; |
| 787 | HBasicBlock* dominator_; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 788 | GrowableArray<HBasicBlock*> dominated_blocks_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 789 | int block_id_; |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 790 | // The dex program counter of the first instruction of this block. |
| 791 | const uint32_t dex_pc_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 792 | size_t lifetime_start_; |
| 793 | size_t lifetime_end_; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 794 | bool is_catch_block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 795 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 796 | friend class HGraph; |
| 797 | friend class HInstruction; |
| 798 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 799 | DISALLOW_COPY_AND_ASSIGN(HBasicBlock); |
| 800 | }; |
| 801 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 802 | // Iterates over the LoopInformation of all loops which contain 'block' |
| 803 | // from the innermost to the outermost. |
| 804 | class HLoopInformationOutwardIterator : public ValueObject { |
| 805 | public: |
| 806 | explicit HLoopInformationOutwardIterator(const HBasicBlock& block) |
| 807 | : current_(block.GetLoopInformation()) {} |
| 808 | |
| 809 | bool Done() const { return current_ == nullptr; } |
| 810 | |
| 811 | void Advance() { |
| 812 | DCHECK(!Done()); |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 813 | current_ = current_->GetPreHeader()->GetLoopInformation(); |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | HLoopInformation* Current() const { |
| 817 | DCHECK(!Done()); |
| 818 | return current_; |
| 819 | } |
| 820 | |
| 821 | private: |
| 822 | HLoopInformation* current_; |
| 823 | |
| 824 | DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator); |
| 825 | }; |
| 826 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame^] | 827 | #define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 828 | M(Add, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 829 | M(And, BinaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 830 | M(ArrayGet, Instruction) \ |
| 831 | M(ArrayLength, Instruction) \ |
| 832 | M(ArraySet, Instruction) \ |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 833 | M(BooleanNot, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 834 | M(BoundsCheck, Instruction) \ |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 835 | M(BoundType, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 836 | M(CheckCast, Instruction) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 837 | M(ClinitCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 838 | M(Compare, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 839 | M(Condition, BinaryOperation) \ |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 840 | M(CurrentMethod, Instruction) \ |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 841 | M(Deoptimize, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 842 | M(Div, BinaryOperation) \ |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 843 | M(DivZeroCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 844 | M(DoubleConstant, Constant) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 845 | M(Equal, Condition) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 846 | M(Exit, Instruction) \ |
| 847 | M(FloatConstant, Constant) \ |
| 848 | M(Goto, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 849 | M(GreaterThan, Condition) \ |
| 850 | M(GreaterThanOrEqual, Condition) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 851 | M(If, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 852 | M(InstanceFieldGet, Instruction) \ |
| 853 | M(InstanceFieldSet, Instruction) \ |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 854 | M(InstanceOf, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 855 | M(IntConstant, Constant) \ |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 856 | M(InvokeInterface, Invoke) \ |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 857 | M(InvokeStaticOrDirect, Invoke) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 858 | M(InvokeVirtual, Invoke) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 859 | M(LessThan, Condition) \ |
| 860 | M(LessThanOrEqual, Condition) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 861 | M(LoadClass, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 862 | M(LoadException, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 863 | M(LoadLocal, Instruction) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 864 | M(LoadString, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 865 | M(Local, Instruction) \ |
| 866 | M(LongConstant, Constant) \ |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 867 | M(MemoryBarrier, Instruction) \ |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 868 | M(MonitorOperation, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 869 | M(Mul, BinaryOperation) \ |
| 870 | M(Neg, UnaryOperation) \ |
| 871 | M(NewArray, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 872 | M(NewInstance, Instruction) \ |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 873 | M(Not, UnaryOperation) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 874 | M(NotEqual, Condition) \ |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 875 | M(NullConstant, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 876 | M(NullCheck, Instruction) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 877 | M(Or, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 878 | M(ParallelMove, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 879 | M(ParameterValue, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 880 | M(Phi, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 881 | M(Rem, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 882 | M(Return, Instruction) \ |
| 883 | M(ReturnVoid, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 884 | M(Shl, BinaryOperation) \ |
| 885 | M(Shr, BinaryOperation) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 886 | M(StaticFieldGet, Instruction) \ |
| 887 | M(StaticFieldSet, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 888 | M(StoreLocal, Instruction) \ |
| 889 | M(Sub, BinaryOperation) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 890 | M(SuspendCheck, Instruction) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 891 | M(Temporary, Instruction) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 892 | M(Throw, Instruction) \ |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 893 | M(TypeConversion, Instruction) \ |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 894 | M(UShr, BinaryOperation) \ |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 895 | M(Xor, BinaryOperation) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 896 | |
Alexandre Rames | ef20f71 | 2015-06-09 10:29:30 +0100 | [diff] [blame^] | 897 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) |
| 898 | |
| 899 | #define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) |
| 900 | |
| 901 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) |
| 902 | |
| 903 | #define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) |
| 904 | |
| 905 | #define FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 906 | FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \ |
| 907 | FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \ |
| 908 | FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \ |
| 909 | FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \ |
| 910 | FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) |
| 911 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 912 | #define FOR_EACH_INSTRUCTION(M) \ |
| 913 | FOR_EACH_CONCRETE_INSTRUCTION(M) \ |
| 914 | M(Constant, Instruction) \ |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 915 | M(UnaryOperation, Instruction) \ |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 916 | M(BinaryOperation, Instruction) \ |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 917 | M(Invoke, Instruction) |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 918 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 919 | #define FORWARD_DECLARATION(type, super) class H##type; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 920 | FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) |
| 921 | #undef FORWARD_DECLARATION |
| 922 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 923 | #define DECLARE_INSTRUCTION(type) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 924 | InstructionKind GetKind() const OVERRIDE { return k##type; } \ |
| 925 | const char* DebugName() const OVERRIDE { return #type; } \ |
| 926 | const H##type* As##type() const OVERRIDE { return this; } \ |
| 927 | H##type* As##type() OVERRIDE { return this; } \ |
| 928 | bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 929 | return other->Is##type(); \ |
| 930 | } \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 931 | void Accept(HGraphVisitor* visitor) OVERRIDE |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 932 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 933 | template <typename T> class HUseList; |
| 934 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 935 | template <typename T> |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 936 | class HUseListNode : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 937 | public: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 938 | HUseListNode* GetPrevious() const { return prev_; } |
| 939 | HUseListNode* GetNext() const { return next_; } |
| 940 | T GetUser() const { return user_; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 941 | size_t GetIndex() const { return index_; } |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 942 | void SetIndex(size_t index) { index_ = index; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 943 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 944 | private: |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 945 | HUseListNode(T user, size_t index) |
| 946 | : user_(user), index_(index), prev_(nullptr), next_(nullptr) {} |
| 947 | |
| 948 | T const user_; |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 949 | size_t index_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 950 | HUseListNode<T>* prev_; |
| 951 | HUseListNode<T>* next_; |
| 952 | |
| 953 | friend class HUseList<T>; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 954 | |
| 955 | DISALLOW_COPY_AND_ASSIGN(HUseListNode); |
| 956 | }; |
| 957 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 958 | template <typename T> |
| 959 | class HUseList : public ValueObject { |
| 960 | public: |
| 961 | HUseList() : first_(nullptr) {} |
| 962 | |
| 963 | void Clear() { |
| 964 | first_ = nullptr; |
| 965 | } |
| 966 | |
| 967 | // Adds a new entry at the beginning of the use list and returns |
| 968 | // the newly created node. |
| 969 | HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) { |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 970 | HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 971 | if (IsEmpty()) { |
| 972 | first_ = new_node; |
| 973 | } else { |
| 974 | first_->prev_ = new_node; |
| 975 | new_node->next_ = first_; |
| 976 | first_ = new_node; |
| 977 | } |
| 978 | return new_node; |
| 979 | } |
| 980 | |
| 981 | HUseListNode<T>* GetFirst() const { |
| 982 | return first_; |
| 983 | } |
| 984 | |
| 985 | void Remove(HUseListNode<T>* node) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 986 | DCHECK(node != nullptr); |
| 987 | DCHECK(Contains(node)); |
| 988 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 989 | if (node->prev_ != nullptr) { |
| 990 | node->prev_->next_ = node->next_; |
| 991 | } |
| 992 | if (node->next_ != nullptr) { |
| 993 | node->next_->prev_ = node->prev_; |
| 994 | } |
| 995 | if (node == first_) { |
| 996 | first_ = node->next_; |
| 997 | } |
| 998 | } |
| 999 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1000 | bool Contains(const HUseListNode<T>* node) const { |
| 1001 | if (node == nullptr) { |
| 1002 | return false; |
| 1003 | } |
| 1004 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 1005 | if (current == node) { |
| 1006 | return true; |
| 1007 | } |
| 1008 | } |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1012 | bool IsEmpty() const { |
| 1013 | return first_ == nullptr; |
| 1014 | } |
| 1015 | |
| 1016 | bool HasOnlyOneUse() const { |
| 1017 | return first_ != nullptr && first_->next_ == nullptr; |
| 1018 | } |
| 1019 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1020 | size_t SizeSlow() const { |
| 1021 | size_t count = 0; |
| 1022 | for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) { |
| 1023 | ++count; |
| 1024 | } |
| 1025 | return count; |
| 1026 | } |
| 1027 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1028 | private: |
| 1029 | HUseListNode<T>* first_; |
| 1030 | }; |
| 1031 | |
| 1032 | template<typename T> |
| 1033 | class HUseIterator : public ValueObject { |
| 1034 | public: |
| 1035 | explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {} |
| 1036 | |
| 1037 | bool Done() const { return current_ == nullptr; } |
| 1038 | |
| 1039 | void Advance() { |
| 1040 | DCHECK(!Done()); |
| 1041 | current_ = current_->GetNext(); |
| 1042 | } |
| 1043 | |
| 1044 | HUseListNode<T>* Current() const { |
| 1045 | DCHECK(!Done()); |
| 1046 | return current_; |
| 1047 | } |
| 1048 | |
| 1049 | private: |
| 1050 | HUseListNode<T>* current_; |
| 1051 | |
| 1052 | friend class HValue; |
| 1053 | }; |
| 1054 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1055 | // This class is used by HEnvironment and HInstruction classes to record the |
| 1056 | // instructions they use and pointers to the corresponding HUseListNodes kept |
| 1057 | // by the used instructions. |
| 1058 | template <typename T> |
| 1059 | class HUserRecord : public ValueObject { |
| 1060 | public: |
| 1061 | HUserRecord() : instruction_(nullptr), use_node_(nullptr) {} |
| 1062 | explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {} |
| 1063 | |
| 1064 | HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node) |
| 1065 | : instruction_(old_record.instruction_), use_node_(use_node) { |
| 1066 | DCHECK(instruction_ != nullptr); |
| 1067 | DCHECK(use_node_ != nullptr); |
| 1068 | DCHECK(old_record.use_node_ == nullptr); |
| 1069 | } |
| 1070 | |
| 1071 | HInstruction* GetInstruction() const { return instruction_; } |
| 1072 | HUseListNode<T>* GetUseNode() const { return use_node_; } |
| 1073 | |
| 1074 | private: |
| 1075 | // Instruction used by the user. |
| 1076 | HInstruction* instruction_; |
| 1077 | |
| 1078 | // Corresponding entry in the use list kept by 'instruction_'. |
| 1079 | HUseListNode<T>* use_node_; |
| 1080 | }; |
| 1081 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 1082 | // TODO: Add better documentation to this class and maybe refactor with more suggestive names. |
| 1083 | // - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething |
| 1084 | // flag is consider. |
| 1085 | // - DependsOn suggests that there is a real dependency between side effects but it only |
| 1086 | // checks DependendsOnSomething flag. |
| 1087 | // |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1088 | // Represents the side effects an instruction may have. |
| 1089 | class SideEffects : public ValueObject { |
| 1090 | public: |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1091 | SideEffects() : flags_(0) {} |
| 1092 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1093 | static SideEffects None() { |
| 1094 | return SideEffects(0); |
| 1095 | } |
| 1096 | |
| 1097 | static SideEffects All() { |
| 1098 | return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_); |
| 1099 | } |
| 1100 | |
| 1101 | static SideEffects ChangesSomething() { |
| 1102 | return SideEffects((1 << kFlagChangesCount) - 1); |
| 1103 | } |
| 1104 | |
| 1105 | static SideEffects DependsOnSomething() { |
| 1106 | int count = kFlagDependsOnCount - kFlagChangesCount; |
| 1107 | return SideEffects(((1 << count) - 1) << kFlagChangesCount); |
| 1108 | } |
| 1109 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1110 | SideEffects Union(SideEffects other) const { |
| 1111 | return SideEffects(flags_ | other.flags_); |
| 1112 | } |
| 1113 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1114 | bool HasSideEffects() const { |
| 1115 | size_t all_bits_set = (1 << kFlagChangesCount) - 1; |
| 1116 | return (flags_ & all_bits_set) != 0; |
| 1117 | } |
| 1118 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1119 | bool HasAllSideEffects() const { |
| 1120 | size_t all_bits_set = (1 << kFlagChangesCount) - 1; |
| 1121 | return all_bits_set == (flags_ & all_bits_set); |
| 1122 | } |
| 1123 | |
| 1124 | bool DependsOn(SideEffects other) const { |
| 1125 | size_t depends_flags = other.ComputeDependsFlags(); |
| 1126 | return (flags_ & depends_flags) != 0; |
| 1127 | } |
| 1128 | |
| 1129 | bool HasDependencies() const { |
| 1130 | int count = kFlagDependsOnCount - kFlagChangesCount; |
| 1131 | size_t all_bits_set = (1 << count) - 1; |
| 1132 | return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0; |
| 1133 | } |
| 1134 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1135 | private: |
| 1136 | static constexpr int kFlagChangesSomething = 0; |
| 1137 | static constexpr int kFlagChangesCount = kFlagChangesSomething + 1; |
| 1138 | |
| 1139 | static constexpr int kFlagDependsOnSomething = kFlagChangesCount; |
| 1140 | static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1; |
| 1141 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1142 | explicit SideEffects(size_t flags) : flags_(flags) {} |
| 1143 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1144 | size_t ComputeDependsFlags() const { |
| 1145 | return flags_ << kFlagChangesCount; |
| 1146 | } |
| 1147 | |
| 1148 | size_t flags_; |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1149 | }; |
| 1150 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1151 | // A HEnvironment object contains the values of virtual registers at a given location. |
| 1152 | class HEnvironment : public ArenaObject<kArenaAllocMisc> { |
| 1153 | public: |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1154 | HEnvironment(ArenaAllocator* arena, |
| 1155 | size_t number_of_vregs, |
| 1156 | const DexFile& dex_file, |
| 1157 | uint32_t method_idx, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1158 | uint32_t dex_pc, |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1159 | InvokeType invoke_type, |
| 1160 | HInstruction* holder) |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1161 | : vregs_(arena, number_of_vregs), |
| 1162 | locations_(arena, number_of_vregs), |
| 1163 | parent_(nullptr), |
| 1164 | dex_file_(dex_file), |
| 1165 | method_idx_(method_idx), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1166 | dex_pc_(dex_pc), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1167 | invoke_type_(invoke_type), |
| 1168 | holder_(holder) { |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1169 | vregs_.SetSize(number_of_vregs); |
| 1170 | for (size_t i = 0; i < number_of_vregs; i++) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1171 | vregs_.Put(i, HUserRecord<HEnvironment*>()); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1172 | } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1173 | |
| 1174 | locations_.SetSize(number_of_vregs); |
| 1175 | for (size_t i = 0; i < number_of_vregs; ++i) { |
| 1176 | locations_.Put(i, Location()); |
| 1177 | } |
| 1178 | } |
| 1179 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1180 | HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1181 | : HEnvironment(arena, |
| 1182 | to_copy.Size(), |
| 1183 | to_copy.GetDexFile(), |
| 1184 | to_copy.GetMethodIdx(), |
| 1185 | to_copy.GetDexPc(), |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1186 | to_copy.GetInvokeType(), |
| 1187 | holder) {} |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1188 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1189 | void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1190 | if (parent_ != nullptr) { |
| 1191 | parent_->SetAndCopyParentChain(allocator, parent); |
| 1192 | } else { |
| 1193 | parent_ = new (allocator) HEnvironment(allocator, *parent, holder_); |
| 1194 | parent_->CopyFrom(parent); |
| 1195 | if (parent->GetParent() != nullptr) { |
| 1196 | parent_->SetAndCopyParentChain(allocator, parent->GetParent()); |
| 1197 | } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1198 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 1201 | void CopyFrom(const GrowableArray<HInstruction*>& locals); |
| 1202 | void CopyFrom(HEnvironment* environment); |
| 1203 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1204 | // Copy from `env`. If it's a loop phi for `loop_header`, copy the first |
| 1205 | // input to the loop phi instead. This is for inserting instructions that |
| 1206 | // require an environment (like HDeoptimization) in the loop pre-header. |
| 1207 | void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1208 | |
| 1209 | void SetRawEnvAt(size_t index, HInstruction* instruction) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1210 | vregs_.Put(index, HUserRecord<HEnvironment*>(instruction)); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | HInstruction* GetInstructionAt(size_t index) const { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1214 | return vregs_.Get(index).GetInstruction(); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1217 | void RemoveAsUserOfInput(size_t index) const; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1218 | |
| 1219 | size_t Size() const { return vregs_.Size(); } |
| 1220 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1221 | HEnvironment* GetParent() const { return parent_; } |
| 1222 | |
| 1223 | void SetLocationAt(size_t index, Location location) { |
| 1224 | locations_.Put(index, location); |
| 1225 | } |
| 1226 | |
| 1227 | Location GetLocationAt(size_t index) const { |
| 1228 | return locations_.Get(index); |
| 1229 | } |
| 1230 | |
| 1231 | uint32_t GetDexPc() const { |
| 1232 | return dex_pc_; |
| 1233 | } |
| 1234 | |
| 1235 | uint32_t GetMethodIdx() const { |
| 1236 | return method_idx_; |
| 1237 | } |
| 1238 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1239 | InvokeType GetInvokeType() const { |
| 1240 | return invoke_type_; |
| 1241 | } |
| 1242 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1243 | const DexFile& GetDexFile() const { |
| 1244 | return dex_file_; |
| 1245 | } |
| 1246 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1247 | HInstruction* GetHolder() const { |
| 1248 | return holder_; |
| 1249 | } |
| 1250 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1251 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1252 | // Record instructions' use entries of this environment for constant-time removal. |
| 1253 | // It should only be called by HInstruction when a new environment use is added. |
| 1254 | void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) { |
| 1255 | DCHECK(env_use->GetUser() == this); |
| 1256 | size_t index = env_use->GetIndex(); |
| 1257 | vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use)); |
| 1258 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1259 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1260 | GrowableArray<HUserRecord<HEnvironment*> > vregs_; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1261 | GrowableArray<Location> locations_; |
| 1262 | HEnvironment* parent_; |
| 1263 | const DexFile& dex_file_; |
| 1264 | const uint32_t method_idx_; |
| 1265 | const uint32_t dex_pc_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1266 | const InvokeType invoke_type_; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1267 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1268 | // The instruction that holds this environment. Only used in debug mode |
| 1269 | // to ensure the graph is consistent. |
| 1270 | HInstruction* const holder_; |
| 1271 | |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 1272 | friend class HInstruction; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1273 | |
| 1274 | DISALLOW_COPY_AND_ASSIGN(HEnvironment); |
| 1275 | }; |
| 1276 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1277 | class ReferenceTypeInfo : ValueObject { |
| 1278 | public: |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1279 | typedef Handle<mirror::Class> TypeHandle; |
| 1280 | |
| 1281 | static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) |
| 1282 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1283 | if (type_handle->IsObjectClass()) { |
| 1284 | // Override the type handle to be consistent with the case when we get to |
| 1285 | // Top but don't have the Object class available. It avoids having to guess |
| 1286 | // what value the type_handle has when it's Top. |
| 1287 | return ReferenceTypeInfo(TypeHandle(), is_exact, true); |
| 1288 | } else { |
| 1289 | return ReferenceTypeInfo(type_handle, is_exact, false); |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | static ReferenceTypeInfo CreateTop(bool is_exact) { |
| 1294 | return ReferenceTypeInfo(TypeHandle(), is_exact, true); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | bool IsExact() const { return is_exact_; } |
| 1298 | bool IsTop() const { return is_top_; } |
| 1299 | |
| 1300 | Handle<mirror::Class> GetTypeHandle() const { return type_handle_; } |
| 1301 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1302 | bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1303 | if (IsTop()) { |
| 1304 | // Top (equivalent for java.lang.Object) is supertype of anything. |
| 1305 | return true; |
| 1306 | } |
| 1307 | if (rti.IsTop()) { |
| 1308 | // If we get here `this` is not Top() so it can't be a supertype. |
| 1309 | return false; |
| 1310 | } |
| 1311 | return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get()); |
| 1312 | } |
| 1313 | |
| 1314 | // Returns true if the type information provide the same amount of details. |
| 1315 | // Note that it does not mean that the instructions have the same actual type |
| 1316 | // (e.g. tops are equal but they can be the result of a merge). |
| 1317 | bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1318 | if (IsExact() != rti.IsExact()) { |
| 1319 | return false; |
| 1320 | } |
| 1321 | if (IsTop() && rti.IsTop()) { |
| 1322 | // `Top` means java.lang.Object, so the types are equivalent. |
| 1323 | return true; |
| 1324 | } |
| 1325 | if (IsTop() || rti.IsTop()) { |
| 1326 | // If only one is top or object than they are not equivalent. |
| 1327 | // NB: We need this extra check because the type_handle of `Top` is invalid |
| 1328 | // and we cannot inspect its reference. |
| 1329 | return false; |
| 1330 | } |
| 1331 | |
| 1332 | // Finally check the types. |
| 1333 | return GetTypeHandle().Get() == rti.GetTypeHandle().Get(); |
| 1334 | } |
| 1335 | |
| 1336 | private: |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1337 | ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {} |
| 1338 | ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top) |
| 1339 | : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {} |
| 1340 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1341 | // The class of the object. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1342 | TypeHandle type_handle_; |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1343 | // Whether or not the type is exact or a superclass of the actual type. |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1344 | // Whether or not we have any information about this type. |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1345 | bool is_exact_; |
| 1346 | // A true value here means that the object type should be java.lang.Object. |
| 1347 | // We don't have access to the corresponding mirror object every time so this |
| 1348 | // flag acts as a substitute. When true, the TypeHandle refers to a null |
| 1349 | // pointer and should not be used. |
| 1350 | bool is_top_; |
| 1351 | }; |
| 1352 | |
| 1353 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs); |
| 1354 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1355 | class HInstruction : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1356 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1357 | explicit HInstruction(SideEffects side_effects) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1358 | : previous_(nullptr), |
| 1359 | next_(nullptr), |
| 1360 | block_(nullptr), |
| 1361 | id_(-1), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1362 | ssa_index_(-1), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1363 | environment_(nullptr), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1364 | locations_(nullptr), |
| 1365 | live_interval_(nullptr), |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1366 | lifetime_position_(kNoLifetime), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 1367 | side_effects_(side_effects), |
| 1368 | reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {} |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1369 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1370 | virtual ~HInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1371 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1372 | #define DECLARE_KIND(type, super) k##type, |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1373 | enum InstructionKind { |
| 1374 | FOR_EACH_INSTRUCTION(DECLARE_KIND) |
| 1375 | }; |
| 1376 | #undef DECLARE_KIND |
| 1377 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1378 | HInstruction* GetNext() const { return next_; } |
| 1379 | HInstruction* GetPrevious() const { return previous_; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1380 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1381 | HInstruction* GetNextDisregardingMoves() const; |
| 1382 | HInstruction* GetPreviousDisregardingMoves() const; |
| 1383 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1384 | HBasicBlock* GetBlock() const { return block_; } |
| 1385 | void SetBlock(HBasicBlock* block) { block_ = block; } |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 1386 | bool IsInBlock() const { return block_ != nullptr; } |
| 1387 | bool IsInLoop() const { return block_->IsInLoop(); } |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 1388 | bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1389 | |
Roland Levillain | 6b879dd | 2014-09-22 17:13:44 +0100 | [diff] [blame] | 1390 | virtual size_t InputCount() const = 0; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1391 | HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1392 | |
| 1393 | virtual void Accept(HGraphVisitor* visitor) = 0; |
| 1394 | virtual const char* DebugName() const = 0; |
| 1395 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1396 | virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; } |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1397 | void SetRawInputAt(size_t index, HInstruction* input) { |
| 1398 | SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input)); |
| 1399 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1400 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1401 | virtual bool NeedsEnvironment() const { return false; } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1402 | virtual uint32_t GetDexPc() const { |
| 1403 | LOG(FATAL) << "GetDexPc() cannot be called on an instruction that" |
| 1404 | " does not need an environment"; |
| 1405 | UNREACHABLE(); |
| 1406 | } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1407 | virtual bool IsControlFlow() const { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 1408 | virtual bool CanThrow() const { return false; } |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1409 | bool HasSideEffects() const { return side_effects_.HasSideEffects(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1410 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1411 | // Does not apply for all instructions, but having this at top level greatly |
| 1412 | // simplifies the null check elimination. |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 1413 | virtual bool CanBeNull() const { |
| 1414 | DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types"; |
| 1415 | return true; |
| 1416 | } |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1417 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 1418 | virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const { |
| 1419 | UNUSED(obj); |
| 1420 | return false; |
| 1421 | } |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1422 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1423 | void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1424 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1425 | reference_type_info_ = reference_type_info; |
| 1426 | } |
| 1427 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 1428 | ReferenceTypeInfo GetReferenceTypeInfo() const { |
| 1429 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
| 1430 | return reference_type_info_; |
| 1431 | } |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1432 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1433 | void AddUseAt(HInstruction* user, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1434 | DCHECK(user != nullptr); |
| 1435 | HUseListNode<HInstruction*>* use = |
| 1436 | uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1437 | user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use)); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1440 | void AddEnvUseAt(HEnvironment* user, size_t index) { |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 1441 | DCHECK(user != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1442 | HUseListNode<HEnvironment*>* env_use = |
| 1443 | env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena()); |
| 1444 | user->RecordEnvUse(env_use); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1445 | } |
| 1446 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1447 | void RemoveAsUserOfInput(size_t input) { |
| 1448 | HUserRecord<HInstruction*> input_use = InputRecordAt(input); |
| 1449 | input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode()); |
| 1450 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1451 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1452 | const HUseList<HInstruction*>& GetUses() const { return uses_; } |
| 1453 | const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1454 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1455 | bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); } |
| 1456 | bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); } |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 1457 | bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); } |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1458 | bool HasOnlyOneNonEnvironmentUse() const { |
| 1459 | return !HasEnvironmentUses() && GetUses().HasOnlyOneUse(); |
| 1460 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1461 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 1462 | // Does this instruction strictly dominate `other_instruction`? |
| 1463 | // Returns false if this instruction and `other_instruction` are the same. |
| 1464 | // Aborts if this instruction and `other_instruction` are both phis. |
| 1465 | bool StrictlyDominates(HInstruction* other_instruction) const; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1466 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1467 | int GetId() const { return id_; } |
| 1468 | void SetId(int id) { id_ = id; } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1469 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1470 | int GetSsaIndex() const { return ssa_index_; } |
| 1471 | void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; } |
| 1472 | bool HasSsaIndex() const { return ssa_index_ != -1; } |
| 1473 | |
| 1474 | bool HasEnvironment() const { return environment_ != nullptr; } |
| 1475 | HEnvironment* GetEnvironment() const { return environment_; } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1476 | // Set the `environment_` field. Raw because this method does not |
| 1477 | // update the uses lists. |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1478 | void SetRawEnvironment(HEnvironment* environment) { |
| 1479 | DCHECK(environment_ == nullptr); |
| 1480 | DCHECK_EQ(environment->GetHolder(), this); |
| 1481 | environment_ = environment; |
| 1482 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1483 | |
| 1484 | // Set the environment of this instruction, copying it from `environment`. While |
| 1485 | // copying, the uses lists are being updated. |
| 1486 | void CopyEnvironmentFrom(HEnvironment* environment) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1487 | DCHECK(environment_ == nullptr); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1488 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1489 | environment_ = new (allocator) HEnvironment(allocator, *environment, this); |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1490 | environment_->CopyFrom(environment); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1491 | if (environment->GetParent() != nullptr) { |
| 1492 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 1493 | } |
Nicolas Geoffray | 3dcd58c | 2015-04-03 11:02:38 +0100 | [diff] [blame] | 1494 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1495 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1496 | void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment, |
| 1497 | HBasicBlock* block) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1498 | DCHECK(environment_ == nullptr); |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1499 | ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1500 | environment_ = new (allocator) HEnvironment(allocator, *environment, this); |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 1501 | environment_->CopyFromWithLoopPhiAdjustment(environment, block); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1502 | if (environment->GetParent() != nullptr) { |
| 1503 | environment_->SetAndCopyParentChain(allocator, environment->GetParent()); |
| 1504 | } |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1507 | // Returns the number of entries in the environment. Typically, that is the |
| 1508 | // number of dex registers in a method. It could be more in case of inlining. |
| 1509 | size_t EnvironmentSize() const; |
| 1510 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1511 | LocationSummary* GetLocations() const { return locations_; } |
| 1512 | void SetLocations(LocationSummary* locations) { locations_ = locations; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1513 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1514 | void ReplaceWith(HInstruction* instruction); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1515 | void ReplaceInput(HInstruction* replacement, size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1516 | |
Alexandre Rames | 188d431 | 2015-04-09 18:30:21 +0100 | [diff] [blame] | 1517 | // This is almost the same as doing `ReplaceWith()`. But in this helper, the |
| 1518 | // uses of this instruction by `other` are *not* updated. |
| 1519 | void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) { |
| 1520 | ReplaceWith(other); |
| 1521 | other->ReplaceInput(this, use_index); |
| 1522 | } |
| 1523 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1524 | // Move `this` instruction before `cursor`. |
| 1525 | void MoveBefore(HInstruction* cursor); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1526 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1527 | #define INSTRUCTION_TYPE_CHECK(type, super) \ |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 1528 | bool Is##type() const { return (As##type() != nullptr); } \ |
| 1529 | virtual const H##type* As##type() const { return nullptr; } \ |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1530 | virtual H##type* As##type() { return nullptr; } |
| 1531 | |
| 1532 | FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK) |
| 1533 | #undef INSTRUCTION_TYPE_CHECK |
| 1534 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1535 | // Returns whether the instruction can be moved within the graph. |
| 1536 | virtual bool CanBeMoved() const { return false; } |
| 1537 | |
| 1538 | // Returns whether the two instructions are of the same kind. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1539 | virtual bool InstructionTypeEquals(HInstruction* other) const { |
| 1540 | UNUSED(other); |
| 1541 | return false; |
| 1542 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1543 | |
| 1544 | // Returns whether any data encoded in the two instructions is equal. |
| 1545 | // This method does not look at the inputs. Both instructions must be |
| 1546 | // of the same type, otherwise the method has undefined behavior. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1547 | virtual bool InstructionDataEquals(HInstruction* other) const { |
| 1548 | UNUSED(other); |
| 1549 | return false; |
| 1550 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1551 | |
| 1552 | // Returns whether two instructions are equal, that is: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 1553 | // 1) They have the same type and contain the same data (InstructionDataEquals). |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1554 | // 2) Their inputs are identical. |
| 1555 | bool Equals(HInstruction* other) const; |
| 1556 | |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1557 | virtual InstructionKind GetKind() const = 0; |
| 1558 | |
| 1559 | virtual size_t ComputeHashCode() const { |
| 1560 | size_t result = GetKind(); |
| 1561 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 1562 | result = (result * 31) + InputAt(i)->GetId(); |
| 1563 | } |
| 1564 | return result; |
| 1565 | } |
| 1566 | |
| 1567 | SideEffects GetSideEffects() const { return side_effects_; } |
| 1568 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1569 | size_t GetLifetimePosition() const { return lifetime_position_; } |
| 1570 | void SetLifetimePosition(size_t position) { lifetime_position_ = position; } |
| 1571 | LiveInterval* GetLiveInterval() const { return live_interval_; } |
| 1572 | void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; } |
| 1573 | bool HasLiveInterval() const { return live_interval_ != nullptr; } |
| 1574 | |
Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 1575 | bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); } |
| 1576 | |
| 1577 | // Returns whether the code generation of the instruction will require to have access |
| 1578 | // to the current method. Such instructions are: |
| 1579 | // (1): Instructions that require an environment, as calling the runtime requires |
| 1580 | // to walk the stack and have the current method stored at a specific stack address. |
| 1581 | // (2): Object literals like classes and strings, that are loaded from the dex cache |
| 1582 | // fields of the current method. |
| 1583 | bool NeedsCurrentMethod() const { |
| 1584 | return NeedsEnvironment() || IsLoadClass() || IsLoadString(); |
| 1585 | } |
| 1586 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1587 | virtual bool NeedsDexCache() const { return false; } |
| 1588 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1589 | protected: |
| 1590 | virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0; |
| 1591 | virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0; |
| 1592 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1593 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1594 | void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); } |
| 1595 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1596 | HInstruction* previous_; |
| 1597 | HInstruction* next_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1598 | HBasicBlock* block_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1599 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1600 | // An instruction gets an id when it is added to the graph. |
| 1601 | // It reflects creation order. A negative id means the instruction |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1602 | // has not been added to the graph. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1603 | int id_; |
| 1604 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1605 | // When doing liveness analysis, instructions that have uses get an SSA index. |
| 1606 | int ssa_index_; |
| 1607 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1608 | // List of instructions that have this instruction as input. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1609 | HUseList<HInstruction*> uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1610 | |
| 1611 | // List of environments that contain this instruction. |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 1612 | HUseList<HEnvironment*> env_uses_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1613 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1614 | // The environment associated with this instruction. Not null if the instruction |
| 1615 | // might jump out of the method. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1616 | HEnvironment* environment_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1617 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1618 | // Set by the code generator. |
| 1619 | LocationSummary* locations_; |
| 1620 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1621 | // Set by the liveness analysis. |
| 1622 | LiveInterval* live_interval_; |
| 1623 | |
| 1624 | // Set by the liveness analysis, this is the position in a linear |
| 1625 | // order of blocks where this instruction's live interval start. |
| 1626 | size_t lifetime_position_; |
| 1627 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1628 | const SideEffects side_effects_; |
| 1629 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1630 | // TODO: for primitive types this should be marked as invalid. |
| 1631 | ReferenceTypeInfo reference_type_info_; |
| 1632 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1633 | friend class GraphChecker; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1634 | friend class HBasicBlock; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1635 | friend class HEnvironment; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1636 | friend class HGraph; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1637 | friend class HInstructionList; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1638 | |
| 1639 | DISALLOW_COPY_AND_ASSIGN(HInstruction); |
| 1640 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1641 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1642 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1643 | class HInputIterator : public ValueObject { |
| 1644 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1645 | explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1646 | |
| 1647 | bool Done() const { return index_ == instruction_->InputCount(); } |
| 1648 | HInstruction* Current() const { return instruction_->InputAt(index_); } |
| 1649 | void Advance() { index_++; } |
| 1650 | |
| 1651 | private: |
| 1652 | HInstruction* instruction_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1653 | size_t index_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1654 | |
| 1655 | DISALLOW_COPY_AND_ASSIGN(HInputIterator); |
| 1656 | }; |
| 1657 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1658 | class HInstructionIterator : public ValueObject { |
| 1659 | public: |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1660 | explicit HInstructionIterator(const HInstructionList& instructions) |
| 1661 | : instruction_(instructions.first_instruction_) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1662 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1665 | bool Done() const { return instruction_ == nullptr; } |
| 1666 | HInstruction* Current() const { return instruction_; } |
| 1667 | void Advance() { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1668 | instruction_ = next_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1669 | next_ = Done() ? nullptr : instruction_->GetNext(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | private: |
| 1673 | HInstruction* instruction_; |
| 1674 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1675 | |
| 1676 | DISALLOW_COPY_AND_ASSIGN(HInstructionIterator); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1677 | }; |
| 1678 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1679 | class HBackwardInstructionIterator : public ValueObject { |
| 1680 | public: |
| 1681 | explicit HBackwardInstructionIterator(const HInstructionList& instructions) |
| 1682 | : instruction_(instructions.last_instruction_) { |
| 1683 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 1684 | } |
| 1685 | |
| 1686 | bool Done() const { return instruction_ == nullptr; } |
| 1687 | HInstruction* Current() const { return instruction_; } |
| 1688 | void Advance() { |
| 1689 | instruction_ = next_; |
| 1690 | next_ = Done() ? nullptr : instruction_->GetPrevious(); |
| 1691 | } |
| 1692 | |
| 1693 | private: |
| 1694 | HInstruction* instruction_; |
| 1695 | HInstruction* next_; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1696 | |
| 1697 | DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1698 | }; |
| 1699 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1700 | // An embedded container with N elements of type T. Used (with partial |
| 1701 | // specialization for N=0) because embedded arrays cannot have size 0. |
| 1702 | template<typename T, intptr_t N> |
| 1703 | class EmbeddedArray { |
| 1704 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1705 | EmbeddedArray() : elements_() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1706 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1707 | intptr_t GetLength() const { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1708 | |
| 1709 | const T& operator[](intptr_t i) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1710 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1711 | return elements_[i]; |
| 1712 | } |
| 1713 | |
| 1714 | T& operator[](intptr_t i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1715 | DCHECK_LT(i, GetLength()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1716 | return elements_[i]; |
| 1717 | } |
| 1718 | |
| 1719 | const T& At(intptr_t i) const { |
| 1720 | return (*this)[i]; |
| 1721 | } |
| 1722 | |
| 1723 | void SetAt(intptr_t i, const T& val) { |
| 1724 | (*this)[i] = val; |
| 1725 | } |
| 1726 | |
| 1727 | private: |
| 1728 | T elements_[N]; |
| 1729 | }; |
| 1730 | |
| 1731 | template<typename T> |
| 1732 | class EmbeddedArray<T, 0> { |
| 1733 | public: |
| 1734 | intptr_t length() const { return 0; } |
| 1735 | const T& operator[](intptr_t i) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1736 | UNUSED(i); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1737 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1738 | UNREACHABLE(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1739 | } |
| 1740 | T& operator[](intptr_t i) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1741 | UNUSED(i); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1742 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1743 | UNREACHABLE(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1744 | } |
| 1745 | }; |
| 1746 | |
| 1747 | template<intptr_t N> |
| 1748 | class HTemplateInstruction: public HInstruction { |
| 1749 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1750 | HTemplateInstruction<N>(SideEffects side_effects) |
| 1751 | : HInstruction(side_effects), inputs_() {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1752 | virtual ~HTemplateInstruction() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1753 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1754 | size_t InputCount() const OVERRIDE { return N; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1755 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1756 | protected: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1757 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; } |
| 1758 | |
| 1759 | void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 1760 | inputs_[i] = input; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1763 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 1764 | EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1765 | |
| 1766 | friend class SsaBuilder; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1767 | }; |
| 1768 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1769 | template<intptr_t N> |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1770 | class HExpression : public HTemplateInstruction<N> { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1771 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1772 | HExpression<N>(Primitive::Type type, SideEffects side_effects) |
| 1773 | : HTemplateInstruction<N>(side_effects), type_(type) {} |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1774 | virtual ~HExpression() {} |
| 1775 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1776 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1777 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1778 | protected: |
| 1779 | Primitive::Type type_; |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1780 | }; |
| 1781 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1782 | // Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow |
| 1783 | // instruction that branches to the exit block. |
| 1784 | class HReturnVoid : public HTemplateInstruction<0> { |
| 1785 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1786 | HReturnVoid() : HTemplateInstruction(SideEffects::None()) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1787 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1788 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1789 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1790 | DECLARE_INSTRUCTION(ReturnVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1791 | |
| 1792 | private: |
| 1793 | DISALLOW_COPY_AND_ASSIGN(HReturnVoid); |
| 1794 | }; |
| 1795 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1796 | // Represents dex's RETURN opcodes. A HReturn is a control flow |
| 1797 | // instruction that branches to the exit block. |
| 1798 | class HReturn : public HTemplateInstruction<1> { |
| 1799 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1800 | explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1801 | SetRawInputAt(0, value); |
| 1802 | } |
| 1803 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1804 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1805 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1806 | DECLARE_INSTRUCTION(Return); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1807 | |
| 1808 | private: |
| 1809 | DISALLOW_COPY_AND_ASSIGN(HReturn); |
| 1810 | }; |
| 1811 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1812 | // The exit instruction is the only instruction of the exit block. |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1813 | // Instructions aborting the method (HThrow and HReturn) must branch to the |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1814 | // exit block. |
| 1815 | class HExit : public HTemplateInstruction<0> { |
| 1816 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1817 | HExit() : HTemplateInstruction(SideEffects::None()) {} |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 1818 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1819 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1820 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1821 | DECLARE_INSTRUCTION(Exit); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1822 | |
| 1823 | private: |
| 1824 | DISALLOW_COPY_AND_ASSIGN(HExit); |
| 1825 | }; |
| 1826 | |
| 1827 | // Jumps from one block to another. |
| 1828 | class HGoto : public HTemplateInstruction<0> { |
| 1829 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1830 | HGoto() : HTemplateInstruction(SideEffects::None()) {} |
| 1831 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1832 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1833 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1834 | HBasicBlock* GetSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1835 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1838 | DECLARE_INSTRUCTION(Goto); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1839 | |
| 1840 | private: |
| 1841 | DISALLOW_COPY_AND_ASSIGN(HGoto); |
| 1842 | }; |
| 1843 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1844 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1845 | // Conditional branch. A block ending with an HIf instruction must have |
| 1846 | // two successors. |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1847 | class HIf : public HTemplateInstruction<1> { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1848 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1849 | explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1850 | SetRawInputAt(0, input); |
| 1851 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1852 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 1853 | bool IsControlFlow() const OVERRIDE { return true; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1854 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1855 | HBasicBlock* IfTrueSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1856 | return GetBlock()->GetSuccessors().Get(0); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | HBasicBlock* IfFalseSuccessor() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 1860 | return GetBlock()->GetSuccessors().Get(1); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1863 | DECLARE_INSTRUCTION(If); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1864 | |
| 1865 | private: |
| 1866 | DISALLOW_COPY_AND_ASSIGN(HIf); |
| 1867 | }; |
| 1868 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1869 | // Deoptimize to interpreter, upon checking a condition. |
| 1870 | class HDeoptimize : public HTemplateInstruction<1> { |
| 1871 | public: |
| 1872 | HDeoptimize(HInstruction* cond, uint32_t dex_pc) |
| 1873 | : HTemplateInstruction(SideEffects::None()), |
| 1874 | dex_pc_(dex_pc) { |
| 1875 | SetRawInputAt(0, cond); |
| 1876 | } |
| 1877 | |
| 1878 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 1879 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1880 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1881 | |
| 1882 | DECLARE_INSTRUCTION(Deoptimize); |
| 1883 | |
| 1884 | private: |
| 1885 | uint32_t dex_pc_; |
| 1886 | |
| 1887 | DISALLOW_COPY_AND_ASSIGN(HDeoptimize); |
| 1888 | }; |
| 1889 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 1890 | // Represents the ArtMethod that was passed as a first argument to |
| 1891 | // the method. It is used by instructions that depend on it, like |
| 1892 | // instructions that work with the dex cache. |
| 1893 | class HCurrentMethod : public HExpression<0> { |
| 1894 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1895 | explicit HCurrentMethod(Primitive::Type type) : HExpression(type, SideEffects::None()) {} |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 1896 | |
| 1897 | DECLARE_INSTRUCTION(CurrentMethod); |
| 1898 | |
| 1899 | private: |
| 1900 | DISALLOW_COPY_AND_ASSIGN(HCurrentMethod); |
| 1901 | }; |
| 1902 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1903 | class HUnaryOperation : public HExpression<1> { |
| 1904 | public: |
| 1905 | HUnaryOperation(Primitive::Type result_type, HInstruction* input) |
| 1906 | : HExpression(result_type, SideEffects::None()) { |
| 1907 | SetRawInputAt(0, input); |
| 1908 | } |
| 1909 | |
| 1910 | HInstruction* GetInput() const { return InputAt(0); } |
| 1911 | Primitive::Type GetResultType() const { return GetType(); } |
| 1912 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1913 | bool CanBeMoved() const OVERRIDE { return true; } |
| 1914 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1915 | UNUSED(other); |
| 1916 | return true; |
| 1917 | } |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1918 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1919 | // Try to statically evaluate `operation` and return a HConstant |
| 1920 | // containing the result of this evaluation. If `operation` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1921 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1922 | HConstant* TryStaticEvaluation() const; |
| 1923 | |
| 1924 | // Apply this operation to `x`. |
| 1925 | virtual int32_t Evaluate(int32_t x) const = 0; |
| 1926 | virtual int64_t Evaluate(int64_t x) const = 0; |
| 1927 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 1928 | DECLARE_INSTRUCTION(UnaryOperation); |
| 1929 | |
| 1930 | private: |
| 1931 | DISALLOW_COPY_AND_ASSIGN(HUnaryOperation); |
| 1932 | }; |
| 1933 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1934 | class HBinaryOperation : public HExpression<2> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1935 | public: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1936 | HBinaryOperation(Primitive::Type result_type, |
| 1937 | HInstruction* left, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1938 | HInstruction* right) : HExpression(result_type, SideEffects::None()) { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1939 | SetRawInputAt(0, left); |
| 1940 | SetRawInputAt(1, right); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1943 | HInstruction* GetLeft() const { return InputAt(0); } |
| 1944 | HInstruction* GetRight() const { return InputAt(1); } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1945 | Primitive::Type GetResultType() const { return GetType(); } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1946 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 1947 | virtual bool IsCommutative() const { return false; } |
| 1948 | |
| 1949 | // Put constant on the right. |
| 1950 | // Returns whether order is changed. |
| 1951 | bool OrderInputsWithConstantOnTheRight() { |
| 1952 | HInstruction* left = InputAt(0); |
| 1953 | HInstruction* right = InputAt(1); |
| 1954 | if (left->IsConstant() && !right->IsConstant()) { |
| 1955 | ReplaceInput(right, 0); |
| 1956 | ReplaceInput(left, 1); |
| 1957 | return true; |
| 1958 | } |
| 1959 | return false; |
| 1960 | } |
| 1961 | |
| 1962 | // Order inputs by instruction id, but favor constant on the right side. |
| 1963 | // This helps GVN for commutative ops. |
| 1964 | void OrderInputs() { |
| 1965 | DCHECK(IsCommutative()); |
| 1966 | HInstruction* left = InputAt(0); |
| 1967 | HInstruction* right = InputAt(1); |
| 1968 | if (left == right || (!left->IsConstant() && right->IsConstant())) { |
| 1969 | return; |
| 1970 | } |
| 1971 | if (OrderInputsWithConstantOnTheRight()) { |
| 1972 | return; |
| 1973 | } |
| 1974 | // Order according to instruction id. |
| 1975 | if (left->GetId() > right->GetId()) { |
| 1976 | ReplaceInput(right, 0); |
| 1977 | ReplaceInput(left, 1); |
| 1978 | } |
| 1979 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 1980 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 1981 | bool CanBeMoved() const OVERRIDE { return true; } |
| 1982 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1983 | UNUSED(other); |
| 1984 | return true; |
| 1985 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1986 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1987 | // Try to statically evaluate `operation` and return a HConstant |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1988 | // containing the result of this evaluation. If `operation` cannot |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1989 | // be evaluated as a constant, return null. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1990 | HConstant* TryStaticEvaluation() const; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1991 | |
| 1992 | // Apply this operation to `x` and `y`. |
| 1993 | virtual int32_t Evaluate(int32_t x, int32_t y) const = 0; |
| 1994 | virtual int64_t Evaluate(int64_t x, int64_t y) const = 0; |
| 1995 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1996 | // Returns an input that can legally be used as the right input and is |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1997 | // constant, or null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1998 | HConstant* GetConstantRight() const; |
| 1999 | |
| 2000 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 2001 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2002 | HInstruction* GetLeastConstantLeft() const; |
| 2003 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 2004 | DECLARE_INSTRUCTION(BinaryOperation); |
| 2005 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2006 | private: |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2007 | DISALLOW_COPY_AND_ASSIGN(HBinaryOperation); |
| 2008 | }; |
| 2009 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2010 | class HCondition : public HBinaryOperation { |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2011 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2012 | HCondition(HInstruction* first, HInstruction* second) |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2013 | : HBinaryOperation(Primitive::kPrimBoolean, first, second), |
| 2014 | needs_materialization_(true) {} |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2015 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2016 | bool NeedsMaterialization() const { return needs_materialization_; } |
| 2017 | void ClearNeedsMaterialization() { needs_materialization_ = false; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2018 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 2019 | // For code generation purposes, returns whether this instruction is just before |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 2020 | // `instruction`, and disregard moves in between. |
| 2021 | bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const; |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 2022 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2023 | DECLARE_INSTRUCTION(Condition); |
| 2024 | |
| 2025 | virtual IfCondition GetCondition() const = 0; |
| 2026 | |
| 2027 | private: |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2028 | // For register allocation purposes, returns whether this instruction needs to be |
| 2029 | // materialized (that is, not just be in the processor flags). |
| 2030 | bool needs_materialization_; |
| 2031 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2032 | DISALLOW_COPY_AND_ASSIGN(HCondition); |
| 2033 | }; |
| 2034 | |
| 2035 | // Instruction to check if two inputs are equal to each other. |
| 2036 | class HEqual : public HCondition { |
| 2037 | public: |
| 2038 | HEqual(HInstruction* first, HInstruction* second) |
| 2039 | : HCondition(first, second) {} |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2040 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2041 | bool IsCommutative() const OVERRIDE { return true; } |
| 2042 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2043 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2044 | return x == y ? 1 : 0; |
| 2045 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2046 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2047 | return x == y ? 1 : 0; |
| 2048 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2049 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2050 | DECLARE_INSTRUCTION(Equal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2051 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2052 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2053 | return kCondEQ; |
| 2054 | } |
| 2055 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2056 | private: |
| 2057 | DISALLOW_COPY_AND_ASSIGN(HEqual); |
| 2058 | }; |
| 2059 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2060 | class HNotEqual : public HCondition { |
| 2061 | public: |
| 2062 | HNotEqual(HInstruction* first, HInstruction* second) |
| 2063 | : HCondition(first, second) {} |
| 2064 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2065 | bool IsCommutative() const OVERRIDE { return true; } |
| 2066 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2067 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2068 | return x != y ? 1 : 0; |
| 2069 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2070 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2071 | return x != y ? 1 : 0; |
| 2072 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2073 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2074 | DECLARE_INSTRUCTION(NotEqual); |
| 2075 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2076 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2077 | return kCondNE; |
| 2078 | } |
| 2079 | |
| 2080 | private: |
| 2081 | DISALLOW_COPY_AND_ASSIGN(HNotEqual); |
| 2082 | }; |
| 2083 | |
| 2084 | class HLessThan : public HCondition { |
| 2085 | public: |
| 2086 | HLessThan(HInstruction* first, HInstruction* second) |
| 2087 | : HCondition(first, second) {} |
| 2088 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2089 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2090 | return x < y ? 1 : 0; |
| 2091 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2092 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2093 | return x < y ? 1 : 0; |
| 2094 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2095 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2096 | DECLARE_INSTRUCTION(LessThan); |
| 2097 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2098 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2099 | return kCondLT; |
| 2100 | } |
| 2101 | |
| 2102 | private: |
| 2103 | DISALLOW_COPY_AND_ASSIGN(HLessThan); |
| 2104 | }; |
| 2105 | |
| 2106 | class HLessThanOrEqual : public HCondition { |
| 2107 | public: |
| 2108 | HLessThanOrEqual(HInstruction* first, HInstruction* second) |
| 2109 | : HCondition(first, second) {} |
| 2110 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2111 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2112 | return x <= y ? 1 : 0; |
| 2113 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2114 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2115 | return x <= y ? 1 : 0; |
| 2116 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2117 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2118 | DECLARE_INSTRUCTION(LessThanOrEqual); |
| 2119 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2120 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2121 | return kCondLE; |
| 2122 | } |
| 2123 | |
| 2124 | private: |
| 2125 | DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual); |
| 2126 | }; |
| 2127 | |
| 2128 | class HGreaterThan : public HCondition { |
| 2129 | public: |
| 2130 | HGreaterThan(HInstruction* first, HInstruction* second) |
| 2131 | : HCondition(first, second) {} |
| 2132 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2133 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2134 | return x > y ? 1 : 0; |
| 2135 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2136 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2137 | return x > y ? 1 : 0; |
| 2138 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2139 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2140 | DECLARE_INSTRUCTION(GreaterThan); |
| 2141 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2142 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2143 | return kCondGT; |
| 2144 | } |
| 2145 | |
| 2146 | private: |
| 2147 | DISALLOW_COPY_AND_ASSIGN(HGreaterThan); |
| 2148 | }; |
| 2149 | |
| 2150 | class HGreaterThanOrEqual : public HCondition { |
| 2151 | public: |
| 2152 | HGreaterThanOrEqual(HInstruction* first, HInstruction* second) |
| 2153 | : HCondition(first, second) {} |
| 2154 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2155 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2156 | return x >= y ? 1 : 0; |
| 2157 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2158 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2159 | return x >= y ? 1 : 0; |
| 2160 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2161 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2162 | DECLARE_INSTRUCTION(GreaterThanOrEqual); |
| 2163 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2164 | IfCondition GetCondition() const OVERRIDE { |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2165 | return kCondGE; |
| 2166 | } |
| 2167 | |
| 2168 | private: |
| 2169 | DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual); |
| 2170 | }; |
| 2171 | |
| 2172 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2173 | // Instruction to check how two inputs compare to each other. |
| 2174 | // Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1. |
| 2175 | class HCompare : public HBinaryOperation { |
| 2176 | public: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2177 | // The bias applies for floating point operations and indicates how NaN |
| 2178 | // comparisons are treated: |
| 2179 | enum Bias { |
| 2180 | kNoBias, // bias is not applicable (i.e. for long operation) |
| 2181 | kGtBias, // return 1 for NaN comparisons |
| 2182 | kLtBias, // return -1 for NaN comparisons |
| 2183 | }; |
| 2184 | |
| 2185 | HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias) |
| 2186 | : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2187 | DCHECK_EQ(type, first->GetType()); |
| 2188 | DCHECK_EQ(type, second->GetType()); |
| 2189 | } |
| 2190 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2191 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2192 | return |
| 2193 | x == y ? 0 : |
| 2194 | x > y ? 1 : |
| 2195 | -1; |
| 2196 | } |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2197 | |
| 2198 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2199 | return |
| 2200 | x == y ? 0 : |
| 2201 | x > y ? 1 : |
| 2202 | -1; |
| 2203 | } |
| 2204 | |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2205 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2206 | return bias_ == other->AsCompare()->bias_; |
| 2207 | } |
| 2208 | |
| 2209 | bool IsGtBias() { return bias_ == kGtBias; } |
| 2210 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2211 | DECLARE_INSTRUCTION(Compare); |
| 2212 | |
| 2213 | private: |
Calin Juravle | ddb7df2 | 2014-11-25 20:56:51 +0000 | [diff] [blame] | 2214 | const Bias bias_; |
| 2215 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 2216 | DISALLOW_COPY_AND_ASSIGN(HCompare); |
| 2217 | }; |
| 2218 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2219 | // A local in the graph. Corresponds to a Dex register. |
| 2220 | class HLocal : public HTemplateInstruction<0> { |
| 2221 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2222 | explicit HLocal(uint16_t reg_number) |
| 2223 | : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {} |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2224 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2225 | DECLARE_INSTRUCTION(Local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2226 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2227 | uint16_t GetRegNumber() const { return reg_number_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2228 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2229 | private: |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2230 | // The Dex register number. |
| 2231 | const uint16_t reg_number_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2232 | |
| 2233 | DISALLOW_COPY_AND_ASSIGN(HLocal); |
| 2234 | }; |
| 2235 | |
| 2236 | // Load a given local. The local is an input of this instruction. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 2237 | class HLoadLocal : public HExpression<1> { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2238 | public: |
Roland Levillain | 5799fc0 | 2014-09-25 12:15:20 +0100 | [diff] [blame] | 2239 | HLoadLocal(HLocal* local, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2240 | : HExpression(type, SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2241 | SetRawInputAt(0, local); |
| 2242 | } |
| 2243 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2244 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 2245 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2246 | DECLARE_INSTRUCTION(LoadLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2247 | |
| 2248 | private: |
| 2249 | DISALLOW_COPY_AND_ASSIGN(HLoadLocal); |
| 2250 | }; |
| 2251 | |
| 2252 | // Store a value in a given local. This instruction has two inputs: the value |
| 2253 | // and the local. |
| 2254 | class HStoreLocal : public HTemplateInstruction<2> { |
| 2255 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2256 | HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2257 | SetRawInputAt(0, local); |
| 2258 | SetRawInputAt(1, value); |
| 2259 | } |
| 2260 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2261 | HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); } |
| 2262 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2263 | DECLARE_INSTRUCTION(StoreLocal); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2264 | |
| 2265 | private: |
| 2266 | DISALLOW_COPY_AND_ASSIGN(HStoreLocal); |
| 2267 | }; |
| 2268 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2269 | class HConstant : public HExpression<0> { |
| 2270 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2271 | explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {} |
| 2272 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2273 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2274 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2275 | virtual bool IsMinusOne() const { return false; } |
| 2276 | virtual bool IsZero() const { return false; } |
| 2277 | virtual bool IsOne() const { return false; } |
| 2278 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2279 | DECLARE_INSTRUCTION(Constant); |
| 2280 | |
| 2281 | private: |
| 2282 | DISALLOW_COPY_AND_ASSIGN(HConstant); |
| 2283 | }; |
| 2284 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2285 | class HFloatConstant : public HConstant { |
| 2286 | public: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2287 | float GetValue() const { return value_; } |
| 2288 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2289 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 2290 | return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) == |
| 2291 | bit_cast<uint32_t, float>(value_); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2292 | } |
| 2293 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2294 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2295 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2296 | bool IsMinusOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2297 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f)); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2298 | } |
| 2299 | bool IsZero() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2300 | return value_ == 0.0f; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2301 | } |
| 2302 | bool IsOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2303 | return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f); |
| 2304 | } |
| 2305 | bool IsNaN() const { |
| 2306 | return std::isnan(value_); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2309 | DECLARE_INSTRUCTION(FloatConstant); |
| 2310 | |
| 2311 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2312 | explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {} |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2313 | explicit HFloatConstant(int32_t value) |
| 2314 | : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {} |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2315 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2316 | const float value_; |
| 2317 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2318 | // Only the SsaBuilder and HGraph can create floating-point constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2319 | friend class SsaBuilder; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2320 | friend class HGraph; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2321 | DISALLOW_COPY_AND_ASSIGN(HFloatConstant); |
| 2322 | }; |
| 2323 | |
| 2324 | class HDoubleConstant : public HConstant { |
| 2325 | public: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2326 | double GetValue() const { return value_; } |
| 2327 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2328 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 2329 | return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) == |
| 2330 | bit_cast<uint64_t, double>(value_); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2331 | } |
| 2332 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2333 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2334 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2335 | bool IsMinusOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2336 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0)); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2337 | } |
| 2338 | bool IsZero() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2339 | return value_ == 0.0; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2340 | } |
| 2341 | bool IsOne() const OVERRIDE { |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 2342 | return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0); |
| 2343 | } |
| 2344 | bool IsNaN() const { |
| 2345 | return std::isnan(value_); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2348 | DECLARE_INSTRUCTION(DoubleConstant); |
| 2349 | |
| 2350 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2351 | explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {} |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2352 | explicit HDoubleConstant(int64_t value) |
| 2353 | : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {} |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2354 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2355 | const double value_; |
| 2356 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2357 | // Only the SsaBuilder and HGraph can create floating-point constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2358 | friend class SsaBuilder; |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 2359 | friend class HGraph; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 2360 | DISALLOW_COPY_AND_ASSIGN(HDoubleConstant); |
| 2361 | }; |
| 2362 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2363 | class HNullConstant : public HConstant { |
| 2364 | public: |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2365 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 2366 | return true; |
| 2367 | } |
| 2368 | |
| 2369 | size_t ComputeHashCode() const OVERRIDE { return 0; } |
| 2370 | |
| 2371 | DECLARE_INSTRUCTION(NullConstant); |
| 2372 | |
| 2373 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2374 | HNullConstant() : HConstant(Primitive::kPrimNot) {} |
| 2375 | |
| 2376 | friend class HGraph; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 2377 | DISALLOW_COPY_AND_ASSIGN(HNullConstant); |
| 2378 | }; |
| 2379 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2380 | // Constants of the type int. Those can be from Dex instructions, or |
| 2381 | // synthesized (for example with the if-eqz instruction). |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2382 | class HIntConstant : public HConstant { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2383 | public: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 2384 | int32_t GetValue() const { return value_; } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 2385 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 2386 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2387 | return other->AsIntConstant()->value_ == value_; |
| 2388 | } |
| 2389 | |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 2390 | size_t ComputeHashCode() const OVERRIDE { return GetValue(); } |
| 2391 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2392 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2393 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2394 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2395 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2396 | DECLARE_INSTRUCTION(IntConstant); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2397 | |
| 2398 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2399 | explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {} |
| 2400 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2401 | const int32_t value_; |
| 2402 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2403 | friend class HGraph; |
| 2404 | ART_FRIEND_TEST(GraphTest, InsertInstructionBefore); |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 2405 | ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 2406 | DISALLOW_COPY_AND_ASSIGN(HIntConstant); |
| 2407 | }; |
| 2408 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 2409 | class HLongConstant : public HConstant { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2410 | public: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2411 | int64_t GetValue() const { return value_; } |
| 2412 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2413 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2414 | return other->AsLongConstant()->value_ == value_; |
| 2415 | } |
| 2416 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2417 | size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 2418 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 2419 | bool IsMinusOne() const OVERRIDE { return GetValue() == -1; } |
| 2420 | bool IsZero() const OVERRIDE { return GetValue() == 0; } |
| 2421 | bool IsOne() const OVERRIDE { return GetValue() == 1; } |
| 2422 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2423 | DECLARE_INSTRUCTION(LongConstant); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2424 | |
| 2425 | private: |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2426 | explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {} |
| 2427 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2428 | const int64_t value_; |
| 2429 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 2430 | friend class HGraph; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2431 | DISALLOW_COPY_AND_ASSIGN(HLongConstant); |
| 2432 | }; |
| 2433 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2434 | enum class Intrinsics { |
| 2435 | #define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name, |
| 2436 | #include "intrinsics_list.h" |
| 2437 | kNone, |
| 2438 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 2439 | #undef INTRINSICS_LIST |
| 2440 | #undef OPTIMIZING_INTRINSICS |
| 2441 | }; |
| 2442 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic); |
| 2443 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2444 | class HInvoke : public HInstruction { |
| 2445 | public: |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2446 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2447 | |
| 2448 | // Runtime needs to walk the stack, so Dex -> Dex calls need to |
| 2449 | // know their environment. |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2450 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2451 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 2452 | void SetArgumentAt(size_t index, HInstruction* argument) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2453 | SetRawInputAt(index, argument); |
| 2454 | } |
| 2455 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2456 | // Return the number of arguments. This number can be lower than |
| 2457 | // the number of inputs returned by InputCount(), as some invoke |
| 2458 | // instructions (e.g. HInvokeStaticOrDirect) can have non-argument |
| 2459 | // inputs at the end of their list of inputs. |
| 2460 | uint32_t GetNumberOfArguments() const { return number_of_arguments_; } |
| 2461 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2462 | Primitive::Type GetType() const OVERRIDE { return return_type_; } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2463 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2464 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2465 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2466 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 2467 | const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2468 | |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2469 | InvokeType GetOriginalInvokeType() const { return original_invoke_type_; } |
| 2470 | |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 2471 | Intrinsics GetIntrinsic() const { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2472 | return intrinsic_; |
| 2473 | } |
| 2474 | |
| 2475 | void SetIntrinsic(Intrinsics intrinsic) { |
| 2476 | intrinsic_ = intrinsic; |
| 2477 | } |
| 2478 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 2479 | bool IsInlined() const { |
| 2480 | return GetEnvironment()->GetParent() != nullptr; |
| 2481 | } |
| 2482 | |
| 2483 | bool CanThrow() const OVERRIDE { return true; } |
| 2484 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 2485 | DECLARE_INSTRUCTION(Invoke); |
| 2486 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2487 | protected: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2488 | HInvoke(ArenaAllocator* arena, |
| 2489 | uint32_t number_of_arguments, |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2490 | uint32_t number_of_other_inputs, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2491 | Primitive::Type return_type, |
| 2492 | uint32_t dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2493 | uint32_t dex_method_index, |
| 2494 | InvokeType original_invoke_type) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2495 | : HInstruction(SideEffects::All()), |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2496 | number_of_arguments_(number_of_arguments), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2497 | inputs_(arena, number_of_arguments), |
| 2498 | return_type_(return_type), |
| 2499 | dex_pc_(dex_pc), |
| 2500 | dex_method_index_(dex_method_index), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2501 | original_invoke_type_(original_invoke_type), |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2502 | intrinsic_(Intrinsics::kNone) { |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2503 | uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs; |
| 2504 | inputs_.SetSize(number_of_inputs); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2505 | } |
| 2506 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2507 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 2508 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 2509 | inputs_.Put(index, input); |
| 2510 | } |
| 2511 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2512 | uint32_t number_of_arguments_; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 2513 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 2514 | const Primitive::Type return_type_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2515 | const uint32_t dex_pc_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2516 | const uint32_t dex_method_index_; |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2517 | const InvokeType original_invoke_type_; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2518 | Intrinsics intrinsic_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2519 | |
| 2520 | private: |
| 2521 | DISALLOW_COPY_AND_ASSIGN(HInvoke); |
| 2522 | }; |
| 2523 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2524 | class HInvokeStaticOrDirect : public HInvoke { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2525 | public: |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2526 | // Requirements of this method call regarding the class |
| 2527 | // initialization (clinit) check of its declaring class. |
| 2528 | enum class ClinitCheckRequirement { |
| 2529 | kNone, // Class already initialized. |
| 2530 | kExplicit, // Static call having explicit clinit check as last input. |
| 2531 | kImplicit, // Static call implicitly requiring a clinit check. |
| 2532 | }; |
| 2533 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2534 | HInvokeStaticOrDirect(ArenaAllocator* arena, |
| 2535 | uint32_t number_of_arguments, |
| 2536 | Primitive::Type return_type, |
| 2537 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2538 | uint32_t dex_method_index, |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2539 | bool is_recursive, |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2540 | int32_t string_init_offset, |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 2541 | InvokeType original_invoke_type, |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2542 | InvokeType invoke_type, |
| 2543 | ClinitCheckRequirement clinit_check_requirement) |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2544 | : HInvoke(arena, |
| 2545 | number_of_arguments, |
Nicolas Geoffray | 38207af | 2015-06-01 15:46:22 +0100 | [diff] [blame] | 2546 | // There is one extra argument for the HCurrentMethod node, and |
| 2547 | // potentially one other if the clinit check is explicit. |
| 2548 | clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 2u : 1u, |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2549 | return_type, |
| 2550 | dex_pc, |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2551 | dex_method_index, |
| 2552 | original_invoke_type), |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2553 | invoke_type_(invoke_type), |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2554 | is_recursive_(is_recursive), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2555 | clinit_check_requirement_(clinit_check_requirement), |
| 2556 | string_init_offset_(string_init_offset) {} |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2557 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2558 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 2559 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2560 | // We access the method via the dex cache so we can't do an implicit null check. |
| 2561 | // TODO: for intrinsics we can generate implicit null checks. |
| 2562 | return false; |
| 2563 | } |
| 2564 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2565 | InvokeType GetInvokeType() const { return invoke_type_; } |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2566 | bool IsRecursive() const { return is_recursive_; } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 2567 | bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2568 | bool IsStringInit() const { return string_init_offset_ != 0; } |
| 2569 | int32_t GetStringInitOffset() const { return string_init_offset_; } |
Nicolas Geoffray | 38207af | 2015-06-01 15:46:22 +0100 | [diff] [blame] | 2570 | uint32_t GetCurrentMethodInputIndex() const { return GetNumberOfArguments(); } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2571 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2572 | // Is this instruction a call to a static method? |
| 2573 | bool IsStatic() const { |
| 2574 | return GetInvokeType() == kStatic; |
| 2575 | } |
| 2576 | |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2577 | // Remove the art::HLoadClass instruction set as last input by |
| 2578 | // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of |
| 2579 | // the initial art::HClinitCheck instruction (only relevant for |
| 2580 | // static calls with explicit clinit check). |
| 2581 | void RemoveLoadClassAsLastInput() { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2582 | DCHECK(IsStaticWithExplicitClinitCheck()); |
| 2583 | size_t last_input_index = InputCount() - 1; |
| 2584 | HInstruction* last_input = InputAt(last_input_index); |
| 2585 | DCHECK(last_input != nullptr); |
Roland Levillain | 3e3d733 | 2015-04-28 11:00:54 +0100 | [diff] [blame] | 2586 | DCHECK(last_input->IsLoadClass()) << last_input->DebugName(); |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2587 | RemoveAsUserOfInput(last_input_index); |
| 2588 | inputs_.DeleteAt(last_input_index); |
| 2589 | clinit_check_requirement_ = ClinitCheckRequirement::kImplicit; |
| 2590 | DCHECK(IsStaticWithImplicitClinitCheck()); |
| 2591 | } |
| 2592 | |
| 2593 | // Is this a call to a static method whose declaring class has an |
| 2594 | // explicit intialization check in the graph? |
| 2595 | bool IsStaticWithExplicitClinitCheck() const { |
| 2596 | return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit); |
| 2597 | } |
| 2598 | |
| 2599 | // Is this a call to a static method whose declaring class has an |
| 2600 | // implicit intialization check requirement? |
| 2601 | bool IsStaticWithImplicitClinitCheck() const { |
| 2602 | return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit); |
| 2603 | } |
| 2604 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2605 | DECLARE_INSTRUCTION(InvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2606 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2607 | protected: |
| 2608 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { |
| 2609 | const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i); |
| 2610 | if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) { |
| 2611 | HInstruction* input = input_record.GetInstruction(); |
| 2612 | // `input` is the last input of a static invoke marked as having |
| 2613 | // an explicit clinit check. It must either be: |
| 2614 | // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or |
| 2615 | // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation. |
| 2616 | DCHECK(input != nullptr); |
| 2617 | DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName(); |
| 2618 | } |
| 2619 | return input_record; |
| 2620 | } |
| 2621 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2622 | private: |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2623 | const InvokeType invoke_type_; |
Nicolas Geoffray | 1cf9528 | 2014-12-12 19:22:03 +0000 | [diff] [blame] | 2624 | const bool is_recursive_; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 2625 | ClinitCheckRequirement clinit_check_requirement_; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2626 | // Thread entrypoint offset for string init method if this is a string init invoke. |
| 2627 | // Note that there are multiple string init methods, each having its own offset. |
| 2628 | int32_t string_init_offset_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2629 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2630 | DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 2631 | }; |
| 2632 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2633 | class HInvokeVirtual : public HInvoke { |
| 2634 | public: |
| 2635 | HInvokeVirtual(ArenaAllocator* arena, |
| 2636 | uint32_t number_of_arguments, |
| 2637 | Primitive::Type return_type, |
| 2638 | uint32_t dex_pc, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 2639 | uint32_t dex_method_index, |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2640 | uint32_t vtable_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2641 | : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual), |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2642 | vtable_index_(vtable_index) {} |
| 2643 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2644 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2645 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2646 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 2649 | uint32_t GetVTableIndex() const { return vtable_index_; } |
| 2650 | |
| 2651 | DECLARE_INSTRUCTION(InvokeVirtual); |
| 2652 | |
| 2653 | private: |
| 2654 | const uint32_t vtable_index_; |
| 2655 | |
| 2656 | DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual); |
| 2657 | }; |
| 2658 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2659 | class HInvokeInterface : public HInvoke { |
| 2660 | public: |
| 2661 | HInvokeInterface(ArenaAllocator* arena, |
| 2662 | uint32_t number_of_arguments, |
| 2663 | Primitive::Type return_type, |
| 2664 | uint32_t dex_pc, |
| 2665 | uint32_t dex_method_index, |
| 2666 | uint32_t imt_index) |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 2667 | : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface), |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2668 | imt_index_(imt_index) {} |
| 2669 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2670 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2671 | // TODO: Add implicit null checks in intrinsics. |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 2672 | return (obj == InputAt(0)) && !GetLocations()->Intrinsified(); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2675 | uint32_t GetImtIndex() const { return imt_index_; } |
| 2676 | uint32_t GetDexMethodIndex() const { return dex_method_index_; } |
| 2677 | |
| 2678 | DECLARE_INSTRUCTION(InvokeInterface); |
| 2679 | |
| 2680 | private: |
Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 2681 | const uint32_t imt_index_; |
| 2682 | |
| 2683 | DISALLOW_COPY_AND_ASSIGN(HInvokeInterface); |
| 2684 | }; |
| 2685 | |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 2686 | class HNewInstance : public HExpression<1> { |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2687 | public: |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 2688 | HNewInstance(HCurrentMethod* current_method, |
| 2689 | uint32_t dex_pc, |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 2690 | uint16_t type_index, |
| 2691 | const DexFile& dex_file, |
| 2692 | QuickEntrypointEnum entrypoint) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 2693 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2694 | dex_pc_(dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2695 | type_index_(type_index), |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 2696 | dex_file_(dex_file), |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 2697 | entrypoint_(entrypoint) { |
| 2698 | SetRawInputAt(0, current_method); |
| 2699 | } |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2700 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2701 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2702 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 2703 | const DexFile& GetDexFile() const { return dex_file_; } |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2704 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2705 | // Calls runtime so needs an environment. |
Calin Juravle | 92a6ed2 | 2014-12-02 18:58:03 +0000 | [diff] [blame] | 2706 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2707 | // It may throw when called on: |
| 2708 | // - interfaces |
| 2709 | // - abstract/innaccessible/unknown classes |
| 2710 | // TODO: optimize when possible. |
| 2711 | bool CanThrow() const OVERRIDE { return true; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 2712 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2713 | bool CanBeNull() const OVERRIDE { return false; } |
| 2714 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2715 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 2716 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 2717 | DECLARE_INSTRUCTION(NewInstance); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2718 | |
| 2719 | private: |
| 2720 | const uint32_t dex_pc_; |
| 2721 | const uint16_t type_index_; |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 2722 | const DexFile& dex_file_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2723 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 2724 | |
| 2725 | DISALLOW_COPY_AND_ASSIGN(HNewInstance); |
| 2726 | }; |
| 2727 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2728 | class HNeg : public HUnaryOperation { |
| 2729 | public: |
| 2730 | explicit HNeg(Primitive::Type result_type, HInstruction* input) |
| 2731 | : HUnaryOperation(result_type, input) {} |
| 2732 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2733 | int32_t Evaluate(int32_t x) const OVERRIDE { return -x; } |
| 2734 | int64_t Evaluate(int64_t x) const OVERRIDE { return -x; } |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 2735 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 2736 | DECLARE_INSTRUCTION(Neg); |
| 2737 | |
| 2738 | private: |
| 2739 | DISALLOW_COPY_AND_ASSIGN(HNeg); |
| 2740 | }; |
| 2741 | |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 2742 | class HNewArray : public HExpression<2> { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2743 | public: |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2744 | HNewArray(HInstruction* length, |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 2745 | HCurrentMethod* current_method, |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2746 | uint32_t dex_pc, |
| 2747 | uint16_t type_index, |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 2748 | const DexFile& dex_file, |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2749 | QuickEntrypointEnum entrypoint) |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2750 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 2751 | dex_pc_(dex_pc), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2752 | type_index_(type_index), |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 2753 | dex_file_(dex_file), |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2754 | entrypoint_(entrypoint) { |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2755 | SetRawInputAt(0, length); |
Nicolas Geoffray | 69aa601 | 2015-06-09 10:34:25 +0100 | [diff] [blame] | 2756 | SetRawInputAt(1, current_method); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2757 | } |
| 2758 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2759 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2760 | uint16_t GetTypeIndex() const { return type_index_; } |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 2761 | const DexFile& GetDexFile() const { return dex_file_; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2762 | |
| 2763 | // Calls runtime so needs an environment. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2764 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2765 | |
Mingyao Yang | 0c365e6 | 2015-03-31 15:09:29 -0700 | [diff] [blame] | 2766 | // May throw NegativeArraySizeException, OutOfMemoryError, etc. |
| 2767 | bool CanThrow() const OVERRIDE { return true; } |
| 2768 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 2769 | bool CanBeNull() const OVERRIDE { return false; } |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2770 | |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2771 | QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; } |
| 2772 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2773 | DECLARE_INSTRUCTION(NewArray); |
| 2774 | |
| 2775 | private: |
| 2776 | const uint32_t dex_pc_; |
| 2777 | const uint16_t type_index_; |
Guillaume "Vermeille" Sanchez | 81d804a | 2015-05-20 12:42:25 +0100 | [diff] [blame] | 2778 | const DexFile& dex_file_; |
Nicolas Geoffray | cb1b00a | 2015-01-28 14:50:01 +0000 | [diff] [blame] | 2779 | const QuickEntrypointEnum entrypoint_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 2780 | |
| 2781 | DISALLOW_COPY_AND_ASSIGN(HNewArray); |
| 2782 | }; |
| 2783 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2784 | class HAdd : public HBinaryOperation { |
| 2785 | public: |
| 2786 | HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2787 | : HBinaryOperation(result_type, left, right) {} |
| 2788 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2789 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2790 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2791 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2792 | return x + y; |
| 2793 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2794 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2795 | return x + y; |
| 2796 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2797 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 2798 | DECLARE_INSTRUCTION(Add); |
| 2799 | |
| 2800 | private: |
| 2801 | DISALLOW_COPY_AND_ASSIGN(HAdd); |
| 2802 | }; |
| 2803 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2804 | class HSub : public HBinaryOperation { |
| 2805 | public: |
| 2806 | HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2807 | : HBinaryOperation(result_type, left, right) {} |
| 2808 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2809 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2810 | return x - y; |
| 2811 | } |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2812 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 2813 | return x - y; |
| 2814 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 2815 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 2816 | DECLARE_INSTRUCTION(Sub); |
| 2817 | |
| 2818 | private: |
| 2819 | DISALLOW_COPY_AND_ASSIGN(HSub); |
| 2820 | }; |
| 2821 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2822 | class HMul : public HBinaryOperation { |
| 2823 | public: |
| 2824 | HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2825 | : HBinaryOperation(result_type, left, right) {} |
| 2826 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2827 | bool IsCommutative() const OVERRIDE { return true; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2828 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2829 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; } |
| 2830 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; } |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 2831 | |
| 2832 | DECLARE_INSTRUCTION(Mul); |
| 2833 | |
| 2834 | private: |
| 2835 | DISALLOW_COPY_AND_ASSIGN(HMul); |
| 2836 | }; |
| 2837 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2838 | class HDiv : public HBinaryOperation { |
| 2839 | public: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2840 | HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc) |
| 2841 | : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {} |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2842 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2843 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 2844 | // Our graph structure ensures we never have 0 for `y` during constant folding. |
| 2845 | DCHECK_NE(y, 0); |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2846 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
Nicolas Geoffray | cd2de0c | 2014-11-06 15:59:38 +0000 | [diff] [blame] | 2847 | return (y == -1) ? -x : x / y; |
| 2848 | } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2849 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2850 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2851 | DCHECK_NE(y, 0); |
| 2852 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2853 | return (y == -1) ? -x : x / y; |
| 2854 | } |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2855 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2856 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2857 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2858 | DECLARE_INSTRUCTION(Div); |
| 2859 | |
| 2860 | private: |
Calin Juravle | d6fb6cf | 2014-11-11 19:07:44 +0000 | [diff] [blame] | 2861 | const uint32_t dex_pc_; |
| 2862 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 2863 | DISALLOW_COPY_AND_ASSIGN(HDiv); |
| 2864 | }; |
| 2865 | |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2866 | class HRem : public HBinaryOperation { |
| 2867 | public: |
| 2868 | HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc) |
| 2869 | : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {} |
| 2870 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2871 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2872 | DCHECK_NE(y, 0); |
| 2873 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2874 | return (y == -1) ? 0 : x % y; |
| 2875 | } |
| 2876 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 2877 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2878 | DCHECK_NE(y, 0); |
| 2879 | // Special case -1 to avoid getting a SIGFPE on x86(_64). |
| 2880 | return (y == -1) ? 0 : x % y; |
| 2881 | } |
| 2882 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2883 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Calin Juravle | bacfec3 | 2014-11-14 15:54:36 +0000 | [diff] [blame] | 2884 | |
| 2885 | DECLARE_INSTRUCTION(Rem); |
| 2886 | |
| 2887 | private: |
| 2888 | const uint32_t dex_pc_; |
| 2889 | |
| 2890 | DISALLOW_COPY_AND_ASSIGN(HRem); |
| 2891 | }; |
| 2892 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 2893 | class HDivZeroCheck : public HExpression<1> { |
| 2894 | public: |
| 2895 | HDivZeroCheck(HInstruction* value, uint32_t dex_pc) |
| 2896 | : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
| 2897 | SetRawInputAt(0, value); |
| 2898 | } |
| 2899 | |
| 2900 | bool CanBeMoved() const OVERRIDE { return true; } |
| 2901 | |
| 2902 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 2903 | UNUSED(other); |
| 2904 | return true; |
| 2905 | } |
| 2906 | |
| 2907 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 2908 | bool CanThrow() const OVERRIDE { return true; } |
| 2909 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 2910 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 2911 | |
| 2912 | DECLARE_INSTRUCTION(DivZeroCheck); |
| 2913 | |
| 2914 | private: |
| 2915 | const uint32_t dex_pc_; |
| 2916 | |
| 2917 | DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck); |
| 2918 | }; |
| 2919 | |
Calin Juravle | 9aec02f | 2014-11-18 23:06:35 +0000 | [diff] [blame] | 2920 | class HShl : public HBinaryOperation { |
| 2921 | public: |
| 2922 | HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2923 | : HBinaryOperation(result_type, left, right) {} |
| 2924 | |
| 2925 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); } |
| 2926 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); } |
| 2927 | |
| 2928 | DECLARE_INSTRUCTION(Shl); |
| 2929 | |
| 2930 | private: |
| 2931 | DISALLOW_COPY_AND_ASSIGN(HShl); |
| 2932 | }; |
| 2933 | |
| 2934 | class HShr : public HBinaryOperation { |
| 2935 | public: |
| 2936 | HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2937 | : HBinaryOperation(result_type, left, right) {} |
| 2938 | |
| 2939 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); } |
| 2940 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); } |
| 2941 | |
| 2942 | DECLARE_INSTRUCTION(Shr); |
| 2943 | |
| 2944 | private: |
| 2945 | DISALLOW_COPY_AND_ASSIGN(HShr); |
| 2946 | }; |
| 2947 | |
| 2948 | class HUShr : public HBinaryOperation { |
| 2949 | public: |
| 2950 | HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2951 | : HBinaryOperation(result_type, left, right) {} |
| 2952 | |
| 2953 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { |
| 2954 | uint32_t ux = static_cast<uint32_t>(x); |
| 2955 | uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue; |
| 2956 | return static_cast<int32_t>(ux >> uy); |
| 2957 | } |
| 2958 | |
| 2959 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { |
| 2960 | uint64_t ux = static_cast<uint64_t>(x); |
| 2961 | uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue; |
| 2962 | return static_cast<int64_t>(ux >> uy); |
| 2963 | } |
| 2964 | |
| 2965 | DECLARE_INSTRUCTION(UShr); |
| 2966 | |
| 2967 | private: |
| 2968 | DISALLOW_COPY_AND_ASSIGN(HUShr); |
| 2969 | }; |
| 2970 | |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2971 | class HAnd : public HBinaryOperation { |
| 2972 | public: |
| 2973 | HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2974 | : HBinaryOperation(result_type, left, right) {} |
| 2975 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2976 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2977 | |
| 2978 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; } |
| 2979 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; } |
| 2980 | |
| 2981 | DECLARE_INSTRUCTION(And); |
| 2982 | |
| 2983 | private: |
| 2984 | DISALLOW_COPY_AND_ASSIGN(HAnd); |
| 2985 | }; |
| 2986 | |
| 2987 | class HOr : public HBinaryOperation { |
| 2988 | public: |
| 2989 | HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 2990 | : HBinaryOperation(result_type, left, right) {} |
| 2991 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 2992 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 2993 | |
| 2994 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; } |
| 2995 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; } |
| 2996 | |
| 2997 | DECLARE_INSTRUCTION(Or); |
| 2998 | |
| 2999 | private: |
| 3000 | DISALLOW_COPY_AND_ASSIGN(HOr); |
| 3001 | }; |
| 3002 | |
| 3003 | class HXor : public HBinaryOperation { |
| 3004 | public: |
| 3005 | HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right) |
| 3006 | : HBinaryOperation(result_type, left, right) {} |
| 3007 | |
Mingyao Yang | dc5ac73 | 2015-02-25 11:28:05 -0800 | [diff] [blame] | 3008 | bool IsCommutative() const OVERRIDE { return true; } |
Nicolas Geoffray | 9574c4b | 2014-11-12 13:19:37 +0000 | [diff] [blame] | 3009 | |
| 3010 | int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; } |
| 3011 | int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; } |
| 3012 | |
| 3013 | DECLARE_INSTRUCTION(Xor); |
| 3014 | |
| 3015 | private: |
| 3016 | DISALLOW_COPY_AND_ASSIGN(HXor); |
| 3017 | }; |
| 3018 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3019 | // The value of a parameter in this method. Its location depends on |
| 3020 | // the calling convention. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 3021 | class HParameterValue : public HExpression<0> { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3022 | public: |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3023 | HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false) |
| 3024 | : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {} |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3025 | |
| 3026 | uint8_t GetIndex() const { return index_; } |
| 3027 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3028 | bool CanBeNull() const OVERRIDE { return !is_this_; } |
| 3029 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 3030 | bool IsThis() const { return is_this_; } |
| 3031 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3032 | DECLARE_INSTRUCTION(ParameterValue); |
| 3033 | |
| 3034 | private: |
| 3035 | // The index of this parameter in the parameters list. Must be less |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3036 | // than HGraph::number_of_in_vregs_. |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3037 | const uint8_t index_; |
| 3038 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3039 | // Whether or not the parameter value corresponds to 'this' argument. |
| 3040 | const bool is_this_; |
| 3041 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 3042 | DISALLOW_COPY_AND_ASSIGN(HParameterValue); |
| 3043 | }; |
| 3044 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 3045 | class HNot : public HUnaryOperation { |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3046 | public: |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 3047 | explicit HNot(Primitive::Type result_type, HInstruction* input) |
| 3048 | : HUnaryOperation(result_type, input) {} |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3049 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3050 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3051 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3052 | UNUSED(other); |
| 3053 | return true; |
| 3054 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3055 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3056 | int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; } |
| 3057 | int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; } |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 3058 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 3059 | DECLARE_INSTRUCTION(Not); |
| 3060 | |
| 3061 | private: |
| 3062 | DISALLOW_COPY_AND_ASSIGN(HNot); |
| 3063 | }; |
| 3064 | |
David Brazdil | 66d126e | 2015-04-03 16:02:44 +0100 | [diff] [blame] | 3065 | class HBooleanNot : public HUnaryOperation { |
| 3066 | public: |
| 3067 | explicit HBooleanNot(HInstruction* input) |
| 3068 | : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {} |
| 3069 | |
| 3070 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3071 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3072 | UNUSED(other); |
| 3073 | return true; |
| 3074 | } |
| 3075 | |
| 3076 | int32_t Evaluate(int32_t x) const OVERRIDE { |
| 3077 | DCHECK(IsUint<1>(x)); |
| 3078 | return !x; |
| 3079 | } |
| 3080 | |
| 3081 | int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3082 | LOG(FATAL) << DebugName() << " cannot be used with 64-bit values"; |
| 3083 | UNREACHABLE(); |
| 3084 | } |
| 3085 | |
| 3086 | DECLARE_INSTRUCTION(BooleanNot); |
| 3087 | |
| 3088 | private: |
| 3089 | DISALLOW_COPY_AND_ASSIGN(HBooleanNot); |
| 3090 | }; |
| 3091 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3092 | class HTypeConversion : public HExpression<1> { |
| 3093 | public: |
| 3094 | // Instantiate a type conversion of `input` to `result_type`. |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3095 | HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc) |
| 3096 | : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) { |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3097 | SetRawInputAt(0, input); |
| 3098 | DCHECK_NE(input->GetType(), result_type); |
| 3099 | } |
| 3100 | |
| 3101 | HInstruction* GetInput() const { return InputAt(0); } |
| 3102 | Primitive::Type GetInputType() const { return GetInput()->GetType(); } |
| 3103 | Primitive::Type GetResultType() const { return GetType(); } |
| 3104 | |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3105 | // Required by the x86 and ARM code generators when producing calls |
| 3106 | // to the runtime. |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3107 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3108 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3109 | bool CanBeMoved() const OVERRIDE { return true; } |
Roland Levillain | ed9b195 | 2014-11-06 11:10:17 +0000 | [diff] [blame] | 3110 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; } |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3111 | |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 3112 | // Try to statically evaluate the conversion and return a HConstant |
| 3113 | // containing the result. If the input cannot be converted, return nullptr. |
| 3114 | HConstant* TryStaticEvaluation() const; |
| 3115 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3116 | DECLARE_INSTRUCTION(TypeConversion); |
| 3117 | |
| 3118 | private: |
Roland Levillain | 624279f | 2014-12-04 11:54:28 +0000 | [diff] [blame] | 3119 | const uint32_t dex_pc_; |
| 3120 | |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 3121 | DISALLOW_COPY_AND_ASSIGN(HTypeConversion); |
| 3122 | }; |
| 3123 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 3124 | static constexpr uint32_t kNoRegNumber = -1; |
| 3125 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3126 | class HPhi : public HInstruction { |
| 3127 | public: |
| 3128 | HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3129 | : HInstruction(SideEffects::None()), |
| 3130 | inputs_(arena, number_of_inputs), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3131 | reg_number_(reg_number), |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 3132 | type_(type), |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3133 | is_live_(false), |
| 3134 | can_be_null_(true) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3135 | inputs_.SetSize(number_of_inputs); |
| 3136 | } |
| 3137 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 3138 | // Returns a type equivalent to the given `type`, but that a `HPhi` can hold. |
| 3139 | static Primitive::Type ToPhiType(Primitive::Type type) { |
| 3140 | switch (type) { |
| 3141 | case Primitive::kPrimBoolean: |
| 3142 | case Primitive::kPrimByte: |
| 3143 | case Primitive::kPrimShort: |
| 3144 | case Primitive::kPrimChar: |
| 3145 | return Primitive::kPrimInt; |
| 3146 | default: |
| 3147 | return type; |
| 3148 | } |
| 3149 | } |
| 3150 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3151 | size_t InputCount() const OVERRIDE { return inputs_.Size(); } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3152 | |
| 3153 | void AddInput(HInstruction* input); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 3154 | void RemoveInputAt(size_t index); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3155 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3156 | Primitive::Type GetType() const OVERRIDE { return type_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3157 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3158 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3159 | bool CanBeNull() const OVERRIDE { return can_be_null_; } |
| 3160 | void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; } |
| 3161 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3162 | uint32_t GetRegNumber() const { return reg_number_; } |
| 3163 | |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 3164 | void SetDead() { is_live_ = false; } |
| 3165 | void SetLive() { is_live_ = true; } |
| 3166 | bool IsDead() const { return !is_live_; } |
| 3167 | bool IsLive() const { return is_live_; } |
| 3168 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 3169 | // Returns the next equivalent phi (starting from the current one) or null if there is none. |
| 3170 | // An equivalent phi is a phi having the same dex register and type. |
| 3171 | // It assumes that phis with the same dex register are adjacent. |
| 3172 | HPhi* GetNextEquivalentPhiWithSameType() { |
| 3173 | HInstruction* next = GetNext(); |
| 3174 | while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) { |
| 3175 | if (next->GetType() == GetType()) { |
| 3176 | return next->AsPhi(); |
| 3177 | } |
| 3178 | next = next->GetNext(); |
| 3179 | } |
| 3180 | return nullptr; |
| 3181 | } |
| 3182 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3183 | DECLARE_INSTRUCTION(Phi); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3184 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3185 | protected: |
| 3186 | const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); } |
| 3187 | |
| 3188 | void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE { |
| 3189 | inputs_.Put(index, input); |
| 3190 | } |
| 3191 | |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 3192 | private: |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 3193 | GrowableArray<HUserRecord<HInstruction*> > inputs_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3194 | const uint32_t reg_number_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 3195 | Primitive::Type type_; |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 3196 | bool is_live_; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3197 | bool can_be_null_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3198 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 3199 | DISALLOW_COPY_AND_ASSIGN(HPhi); |
| 3200 | }; |
| 3201 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3202 | class HNullCheck : public HExpression<1> { |
| 3203 | public: |
| 3204 | HNullCheck(HInstruction* value, uint32_t dex_pc) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3205 | : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3206 | SetRawInputAt(0, value); |
| 3207 | } |
| 3208 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3209 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3210 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3211 | UNUSED(other); |
| 3212 | return true; |
| 3213 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3214 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3215 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3216 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 3217 | bool CanThrow() const OVERRIDE { return true; } |
| 3218 | |
| 3219 | bool CanBeNull() const OVERRIDE { return false; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 3220 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3221 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3222 | |
| 3223 | DECLARE_INSTRUCTION(NullCheck); |
| 3224 | |
| 3225 | private: |
| 3226 | const uint32_t dex_pc_; |
| 3227 | |
| 3228 | DISALLOW_COPY_AND_ASSIGN(HNullCheck); |
| 3229 | }; |
| 3230 | |
| 3231 | class FieldInfo : public ValueObject { |
| 3232 | public: |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3233 | FieldInfo(MemberOffset field_offset, |
| 3234 | Primitive::Type field_type, |
| 3235 | bool is_volatile, |
| 3236 | uint32_t index, |
| 3237 | const DexFile& dex_file) |
| 3238 | : field_offset_(field_offset), |
| 3239 | field_type_(field_type), |
| 3240 | is_volatile_(is_volatile), |
| 3241 | index_(index), |
| 3242 | dex_file_(dex_file) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3243 | |
| 3244 | MemberOffset GetFieldOffset() const { return field_offset_; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3245 | Primitive::Type GetFieldType() const { return field_type_; } |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3246 | uint32_t GetFieldIndex() const { return index_; } |
| 3247 | const DexFile& GetDexFile() const { return dex_file_; } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3248 | bool IsVolatile() const { return is_volatile_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3249 | |
| 3250 | private: |
| 3251 | const MemberOffset field_offset_; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3252 | const Primitive::Type field_type_; |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3253 | const bool is_volatile_; |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3254 | uint32_t index_; |
| 3255 | const DexFile& dex_file_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3256 | }; |
| 3257 | |
| 3258 | class HInstanceFieldGet : public HExpression<1> { |
| 3259 | public: |
| 3260 | HInstanceFieldGet(HInstruction* value, |
| 3261 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3262 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3263 | bool is_volatile, |
| 3264 | uint32_t field_idx, |
| 3265 | const DexFile& dex_file) |
Nicolas Geoffray | 2af2307 | 2015-04-30 11:15:40 +0000 | [diff] [blame] | 3266 | : HExpression(field_type, SideEffects::DependsOnSomething()), |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3267 | field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3268 | SetRawInputAt(0, value); |
| 3269 | } |
| 3270 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 3271 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3272 | |
| 3273 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3274 | HInstanceFieldGet* other_get = other->AsInstanceFieldGet(); |
| 3275 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3276 | } |
| 3277 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3278 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3279 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | size_t ComputeHashCode() const OVERRIDE { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 3283 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 3284 | } |
| 3285 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3286 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3287 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3288 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3289 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3290 | |
| 3291 | DECLARE_INSTRUCTION(InstanceFieldGet); |
| 3292 | |
| 3293 | private: |
| 3294 | const FieldInfo field_info_; |
| 3295 | |
| 3296 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet); |
| 3297 | }; |
| 3298 | |
| 3299 | class HInstanceFieldSet : public HTemplateInstruction<2> { |
| 3300 | public: |
| 3301 | HInstanceFieldSet(HInstruction* object, |
| 3302 | HInstruction* value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3303 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3304 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3305 | bool is_volatile, |
| 3306 | uint32_t field_idx, |
| 3307 | const DexFile& dex_file) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3308 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3309 | field_info_(field_offset, field_type, is_volatile, field_idx, dex_file), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3310 | value_can_be_null_(true) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3311 | SetRawInputAt(0, object); |
| 3312 | SetRawInputAt(1, value); |
| 3313 | } |
| 3314 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3315 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3316 | return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize; |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3319 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3320 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3321 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3322 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3323 | HInstruction* GetValue() const { return InputAt(1); } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3324 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
| 3325 | void ClearValueCanBeNull() { value_can_be_null_ = false; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3326 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3327 | DECLARE_INSTRUCTION(InstanceFieldSet); |
| 3328 | |
| 3329 | private: |
| 3330 | const FieldInfo field_info_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3331 | bool value_can_be_null_; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3332 | |
| 3333 | DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet); |
| 3334 | }; |
| 3335 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3336 | class HArrayGet : public HExpression<2> { |
| 3337 | public: |
| 3338 | HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3339 | : HExpression(type, SideEffects::DependsOnSomething()) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3340 | SetRawInputAt(0, array); |
| 3341 | SetRawInputAt(1, index); |
| 3342 | } |
| 3343 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3344 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3345 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3346 | UNUSED(other); |
| 3347 | return true; |
| 3348 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3349 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3350 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3351 | // TODO: We can be smarter here. |
| 3352 | // Currently, the array access is always preceded by an ArrayLength or a NullCheck |
| 3353 | // which generates the implicit null check. There are cases when these can be removed |
| 3354 | // to produce better code. If we ever add optimizations to do so we should allow an |
| 3355 | // implicit check here (as long as the address falls in the first page). |
| 3356 | return false; |
| 3357 | } |
| 3358 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 3359 | void SetType(Primitive::Type type) { type_ = type; } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3360 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3361 | HInstruction* GetArray() const { return InputAt(0); } |
| 3362 | HInstruction* GetIndex() const { return InputAt(1); } |
| 3363 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3364 | DECLARE_INSTRUCTION(ArrayGet); |
| 3365 | |
| 3366 | private: |
| 3367 | DISALLOW_COPY_AND_ASSIGN(HArrayGet); |
| 3368 | }; |
| 3369 | |
| 3370 | class HArraySet : public HTemplateInstruction<3> { |
| 3371 | public: |
| 3372 | HArraySet(HInstruction* array, |
| 3373 | HInstruction* index, |
| 3374 | HInstruction* value, |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 3375 | Primitive::Type expected_component_type, |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3376 | uint32_t dex_pc) |
| 3377 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
| 3378 | dex_pc_(dex_pc), |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3379 | expected_component_type_(expected_component_type), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3380 | needs_type_check_(value->GetType() == Primitive::kPrimNot), |
| 3381 | value_can_be_null_(true) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3382 | SetRawInputAt(0, array); |
| 3383 | SetRawInputAt(1, index); |
| 3384 | SetRawInputAt(2, value); |
| 3385 | } |
| 3386 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3387 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3388 | // We currently always call a runtime method to catch array store |
| 3389 | // exceptions. |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3390 | return needs_type_check_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3391 | } |
| 3392 | |
Mingyao Yang | 81014cb | 2015-06-02 03:16:27 -0700 | [diff] [blame] | 3393 | // Can throw ArrayStoreException. |
| 3394 | bool CanThrow() const OVERRIDE { return needs_type_check_; } |
| 3395 | |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3396 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3397 | UNUSED(obj); |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3398 | // TODO: Same as for ArrayGet. |
| 3399 | return false; |
| 3400 | } |
| 3401 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3402 | void ClearNeedsTypeCheck() { |
| 3403 | needs_type_check_ = false; |
| 3404 | } |
| 3405 | |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3406 | void ClearValueCanBeNull() { |
| 3407 | value_can_be_null_ = false; |
| 3408 | } |
| 3409 | |
| 3410 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3411 | bool NeedsTypeCheck() const { return needs_type_check_; } |
| 3412 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3413 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3414 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3415 | HInstruction* GetArray() const { return InputAt(0); } |
| 3416 | HInstruction* GetIndex() const { return InputAt(1); } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 3417 | HInstruction* GetValue() const { return InputAt(2); } |
| 3418 | |
| 3419 | Primitive::Type GetComponentType() const { |
| 3420 | // The Dex format does not type floating point index operations. Since the |
| 3421 | // `expected_component_type_` is set during building and can therefore not |
| 3422 | // be correct, we also check what is the value type. If it is a floating |
| 3423 | // point type, we must use that type. |
| 3424 | Primitive::Type value_type = GetValue()->GetType(); |
| 3425 | return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble)) |
| 3426 | ? value_type |
| 3427 | : expected_component_type_; |
| 3428 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 3429 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3430 | DECLARE_INSTRUCTION(ArraySet); |
| 3431 | |
| 3432 | private: |
| 3433 | const uint32_t dex_pc_; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 3434 | const Primitive::Type expected_component_type_; |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3435 | bool needs_type_check_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3436 | bool value_can_be_null_; |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3437 | |
| 3438 | DISALLOW_COPY_AND_ASSIGN(HArraySet); |
| 3439 | }; |
| 3440 | |
| 3441 | class HArrayLength : public HExpression<1> { |
| 3442 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3443 | explicit HArrayLength(HInstruction* array) |
| 3444 | : HExpression(Primitive::kPrimInt, SideEffects::None()) { |
| 3445 | // Note that arrays do not change length, so the instruction does not |
| 3446 | // depend on any write. |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3447 | SetRawInputAt(0, array); |
| 3448 | } |
| 3449 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 3450 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3451 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3452 | UNUSED(other); |
| 3453 | return true; |
| 3454 | } |
Calin Juravle | 641547a | 2015-04-21 22:08:51 +0100 | [diff] [blame] | 3455 | bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE { |
| 3456 | return obj == InputAt(0); |
| 3457 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3458 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3459 | DECLARE_INSTRUCTION(ArrayLength); |
| 3460 | |
| 3461 | private: |
| 3462 | DISALLOW_COPY_AND_ASSIGN(HArrayLength); |
| 3463 | }; |
| 3464 | |
| 3465 | class HBoundsCheck : public HExpression<2> { |
| 3466 | public: |
| 3467 | HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc) |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3468 | : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) { |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3469 | DCHECK(index->GetType() == Primitive::kPrimInt); |
| 3470 | SetRawInputAt(0, index); |
| 3471 | SetRawInputAt(1, length); |
| 3472 | } |
| 3473 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3474 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3475 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3476 | UNUSED(other); |
| 3477 | return true; |
| 3478 | } |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3479 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3480 | bool NeedsEnvironment() const OVERRIDE { return true; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3481 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3482 | bool CanThrow() const OVERRIDE { return true; } |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 3483 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3484 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 3485 | |
| 3486 | DECLARE_INSTRUCTION(BoundsCheck); |
| 3487 | |
| 3488 | private: |
| 3489 | const uint32_t dex_pc_; |
| 3490 | |
| 3491 | DISALLOW_COPY_AND_ASSIGN(HBoundsCheck); |
| 3492 | }; |
| 3493 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3494 | /** |
| 3495 | * Some DEX instructions are folded into multiple HInstructions that need |
| 3496 | * to stay live until the last HInstruction. This class |
| 3497 | * is used as a marker for the baseline compiler to ensure its preceding |
Calin Juravle | f97f9fb | 2014-11-11 15:38:19 +0000 | [diff] [blame] | 3498 | * HInstruction stays live. `index` represents the stack location index of the |
| 3499 | * instruction (the actual offset is computed as index * vreg_size). |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3500 | */ |
| 3501 | class HTemporary : public HTemplateInstruction<0> { |
| 3502 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 3503 | explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {} |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3504 | |
| 3505 | size_t GetIndex() const { return index_; } |
| 3506 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 3507 | Primitive::Type GetType() const OVERRIDE { |
| 3508 | // The previous instruction is the one that will be stored in the temporary location. |
| 3509 | DCHECK(GetPrevious() != nullptr); |
| 3510 | return GetPrevious()->GetType(); |
| 3511 | } |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 3512 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 3513 | DECLARE_INSTRUCTION(Temporary); |
| 3514 | |
| 3515 | private: |
| 3516 | const size_t index_; |
| 3517 | |
| 3518 | DISALLOW_COPY_AND_ASSIGN(HTemporary); |
| 3519 | }; |
| 3520 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3521 | class HSuspendCheck : public HTemplateInstruction<0> { |
| 3522 | public: |
| 3523 | explicit HSuspendCheck(uint32_t dex_pc) |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 3524 | : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {} |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3525 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 3526 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3527 | return true; |
| 3528 | } |
| 3529 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3530 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 3531 | void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; } |
| 3532 | SlowPathCode* GetSlowPath() const { return slow_path_; } |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3533 | |
| 3534 | DECLARE_INSTRUCTION(SuspendCheck); |
| 3535 | |
| 3536 | private: |
| 3537 | const uint32_t dex_pc_; |
| 3538 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 3539 | // Only used for code generation, in order to share the same slow path between back edges |
| 3540 | // of a same loop. |
| 3541 | SlowPathCode* slow_path_; |
| 3542 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 3543 | DISALLOW_COPY_AND_ASSIGN(HSuspendCheck); |
| 3544 | }; |
| 3545 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3546 | /** |
| 3547 | * Instruction to load a Class object. |
| 3548 | */ |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 3549 | class HLoadClass : public HExpression<1> { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3550 | public: |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 3551 | HLoadClass(HCurrentMethod* current_method, |
| 3552 | uint16_t type_index, |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3553 | const DexFile& dex_file, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3554 | bool is_referrers_class, |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3555 | uint32_t dex_pc) |
| 3556 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 3557 | type_index_(type_index), |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3558 | dex_file_(dex_file), |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3559 | is_referrers_class_(is_referrers_class), |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3560 | dex_pc_(dex_pc), |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3561 | generate_clinit_check_(false), |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 3562 | loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) { |
| 3563 | SetRawInputAt(0, current_method); |
| 3564 | } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3565 | |
| 3566 | bool CanBeMoved() const OVERRIDE { return true; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3567 | |
| 3568 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3569 | return other->AsLoadClass()->type_index_ == type_index_; |
| 3570 | } |
| 3571 | |
| 3572 | size_t ComputeHashCode() const OVERRIDE { return type_index_; } |
| 3573 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3574 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3575 | uint16_t GetTypeIndex() const { return type_index_; } |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3576 | bool IsReferrersClass() const { return is_referrers_class_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3577 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3578 | bool NeedsEnvironment() const OVERRIDE { |
| 3579 | // Will call runtime and load the class if the class is not loaded yet. |
| 3580 | // TODO: finer grain decision. |
| 3581 | return !is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3582 | } |
| 3583 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3584 | bool MustGenerateClinitCheck() const { |
| 3585 | return generate_clinit_check_; |
| 3586 | } |
| 3587 | |
Calin Juravle | 0ba218d | 2015-05-19 18:46:01 +0100 | [diff] [blame] | 3588 | void SetMustGenerateClinitCheck(bool generate_clinit_check) { |
| 3589 | generate_clinit_check_ = generate_clinit_check; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | bool CanCallRuntime() const { |
| 3593 | return MustGenerateClinitCheck() || !is_referrers_class_; |
| 3594 | } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3595 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3596 | bool CanThrow() const OVERRIDE { |
| 3597 | // May call runtime and and therefore can throw. |
| 3598 | // TODO: finer grain decision. |
| 3599 | return !is_referrers_class_; |
| 3600 | } |
| 3601 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 3602 | ReferenceTypeInfo GetLoadedClassRTI() { |
| 3603 | return loaded_class_rti_; |
| 3604 | } |
| 3605 | |
| 3606 | void SetLoadedClassRTI(ReferenceTypeInfo rti) { |
| 3607 | // Make sure we only set exact types (the loaded class should never be merged). |
| 3608 | DCHECK(rti.IsExact()); |
| 3609 | loaded_class_rti_ = rti; |
| 3610 | } |
| 3611 | |
| 3612 | bool IsResolved() { |
| 3613 | return loaded_class_rti_.IsExact(); |
| 3614 | } |
| 3615 | |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3616 | const DexFile& GetDexFile() { return dex_file_; } |
| 3617 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 3618 | bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; } |
| 3619 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3620 | DECLARE_INSTRUCTION(LoadClass); |
| 3621 | |
| 3622 | private: |
| 3623 | const uint16_t type_index_; |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 3624 | const DexFile& dex_file_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3625 | const bool is_referrers_class_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3626 | const uint32_t dex_pc_; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3627 | // Whether this instruction must generate the initialization check. |
| 3628 | // Used for code generation. |
| 3629 | bool generate_clinit_check_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3630 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 3631 | ReferenceTypeInfo loaded_class_rti_; |
| 3632 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3633 | DISALLOW_COPY_AND_ASSIGN(HLoadClass); |
| 3634 | }; |
| 3635 | |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 3636 | class HLoadString : public HExpression<1> { |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3637 | public: |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 3638 | HLoadString(HCurrentMethod* current_method, uint32_t string_index, uint32_t dex_pc) |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3639 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 3640 | string_index_(string_index), |
Nicolas Geoffray | fbdaa30 | 2015-05-29 12:06:56 +0100 | [diff] [blame] | 3641 | dex_pc_(dex_pc) { |
| 3642 | SetRawInputAt(0, current_method); |
| 3643 | } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3644 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3645 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3646 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3647 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3648 | return other->AsLoadString()->string_index_ == string_index_; |
| 3649 | } |
| 3650 | |
| 3651 | size_t ComputeHashCode() const OVERRIDE { return string_index_; } |
| 3652 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3653 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3654 | uint32_t GetStringIndex() const { return string_index_; } |
| 3655 | |
| 3656 | // TODO: Can we deopt or debug when we resolve a string? |
| 3657 | bool NeedsEnvironment() const OVERRIDE { return false; } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 3658 | bool NeedsDexCache() const OVERRIDE { return true; } |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 3659 | |
| 3660 | DECLARE_INSTRUCTION(LoadString); |
| 3661 | |
| 3662 | private: |
| 3663 | const uint32_t string_index_; |
| 3664 | const uint32_t dex_pc_; |
| 3665 | |
| 3666 | DISALLOW_COPY_AND_ASSIGN(HLoadString); |
| 3667 | }; |
| 3668 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3669 | /** |
| 3670 | * Performs an initialization check on its Class object input. |
| 3671 | */ |
| 3672 | class HClinitCheck : public HExpression<1> { |
| 3673 | public: |
| 3674 | explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc) |
Nicolas Geoffray | a0466e1 | 2015-03-27 15:00:40 +0000 | [diff] [blame] | 3675 | : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()), |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3676 | dex_pc_(dex_pc) { |
| 3677 | SetRawInputAt(0, constant); |
| 3678 | } |
| 3679 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 3680 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3681 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
| 3682 | UNUSED(other); |
| 3683 | return true; |
| 3684 | } |
| 3685 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3686 | bool NeedsEnvironment() const OVERRIDE { |
| 3687 | // May call runtime to initialize the class. |
| 3688 | return true; |
| 3689 | } |
| 3690 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3691 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3692 | |
| 3693 | HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); } |
| 3694 | |
| 3695 | DECLARE_INSTRUCTION(ClinitCheck); |
| 3696 | |
| 3697 | private: |
| 3698 | const uint32_t dex_pc_; |
| 3699 | |
| 3700 | DISALLOW_COPY_AND_ASSIGN(HClinitCheck); |
| 3701 | }; |
| 3702 | |
| 3703 | class HStaticFieldGet : public HExpression<1> { |
| 3704 | public: |
| 3705 | HStaticFieldGet(HInstruction* cls, |
| 3706 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3707 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3708 | bool is_volatile, |
| 3709 | uint32_t field_idx, |
| 3710 | const DexFile& dex_file) |
Nicolas Geoffray | 2af2307 | 2015-04-30 11:15:40 +0000 | [diff] [blame] | 3711 | : HExpression(field_type, SideEffects::DependsOnSomething()), |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3712 | field_info_(field_offset, field_type, is_volatile, field_idx, dex_file) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3713 | SetRawInputAt(0, cls); |
| 3714 | } |
| 3715 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3716 | |
Calin Juravle | 10c9cbe | 2014-12-19 10:50:19 +0000 | [diff] [blame] | 3717 | bool CanBeMoved() const OVERRIDE { return !IsVolatile(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3718 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3719 | bool InstructionDataEquals(HInstruction* other) const OVERRIDE { |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3720 | HStaticFieldGet* other_get = other->AsStaticFieldGet(); |
| 3721 | return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue(); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | size_t ComputeHashCode() const OVERRIDE { |
| 3725 | return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue(); |
| 3726 | } |
| 3727 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3728 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3729 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 3730 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3731 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3732 | |
| 3733 | DECLARE_INSTRUCTION(StaticFieldGet); |
| 3734 | |
| 3735 | private: |
| 3736 | const FieldInfo field_info_; |
| 3737 | |
| 3738 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet); |
| 3739 | }; |
| 3740 | |
| 3741 | class HStaticFieldSet : public HTemplateInstruction<2> { |
| 3742 | public: |
| 3743 | HStaticFieldSet(HInstruction* cls, |
| 3744 | HInstruction* value, |
| 3745 | Primitive::Type field_type, |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3746 | MemberOffset field_offset, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3747 | bool is_volatile, |
| 3748 | uint32_t field_idx, |
| 3749 | const DexFile& dex_file) |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3750 | : HTemplateInstruction(SideEffects::ChangesSomething()), |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 3751 | field_info_(field_offset, field_type, is_volatile, field_idx, dex_file), |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3752 | value_can_be_null_(true) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3753 | SetRawInputAt(0, cls); |
| 3754 | SetRawInputAt(1, value); |
| 3755 | } |
| 3756 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3757 | const FieldInfo& GetFieldInfo() const { return field_info_; } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3758 | MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); } |
| 3759 | Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); } |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3760 | bool IsVolatile() const { return field_info_.IsVolatile(); } |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3761 | |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3762 | HInstruction* GetValue() const { return InputAt(1); } |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3763 | bool GetValueCanBeNull() const { return value_can_be_null_; } |
| 3764 | void ClearValueCanBeNull() { value_can_be_null_ = false; } |
Nicolas Geoffray | af07bc1 | 2014-11-12 18:08:09 +0000 | [diff] [blame] | 3765 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3766 | DECLARE_INSTRUCTION(StaticFieldSet); |
| 3767 | |
| 3768 | private: |
| 3769 | const FieldInfo field_info_; |
Nicolas Geoffray | 07276db | 2015-05-18 14:22:09 +0100 | [diff] [blame] | 3770 | bool value_can_be_null_; |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 3771 | |
| 3772 | DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet); |
| 3773 | }; |
| 3774 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3775 | // Implement the move-exception DEX instruction. |
| 3776 | class HLoadException : public HExpression<0> { |
| 3777 | public: |
| 3778 | HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {} |
| 3779 | |
| 3780 | DECLARE_INSTRUCTION(LoadException); |
| 3781 | |
| 3782 | private: |
| 3783 | DISALLOW_COPY_AND_ASSIGN(HLoadException); |
| 3784 | }; |
| 3785 | |
| 3786 | class HThrow : public HTemplateInstruction<1> { |
| 3787 | public: |
| 3788 | HThrow(HInstruction* exception, uint32_t dex_pc) |
| 3789 | : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) { |
| 3790 | SetRawInputAt(0, exception); |
| 3791 | } |
| 3792 | |
| 3793 | bool IsControlFlow() const OVERRIDE { return true; } |
| 3794 | |
| 3795 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3796 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3797 | bool CanThrow() const OVERRIDE { return true; } |
| 3798 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3799 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3800 | |
| 3801 | DECLARE_INSTRUCTION(Throw); |
| 3802 | |
| 3803 | private: |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 3804 | const uint32_t dex_pc_; |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 3805 | |
| 3806 | DISALLOW_COPY_AND_ASSIGN(HThrow); |
| 3807 | }; |
| 3808 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3809 | class HInstanceOf : public HExpression<2> { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3810 | public: |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3811 | HInstanceOf(HInstruction* object, |
| 3812 | HLoadClass* constant, |
| 3813 | bool class_is_final, |
| 3814 | uint32_t dex_pc) |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3815 | : HExpression(Primitive::kPrimBoolean, SideEffects::None()), |
| 3816 | class_is_final_(class_is_final), |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3817 | must_do_null_check_(true), |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3818 | dex_pc_(dex_pc) { |
| 3819 | SetRawInputAt(0, object); |
| 3820 | SetRawInputAt(1, constant); |
| 3821 | } |
| 3822 | |
| 3823 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3824 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3825 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3826 | return true; |
| 3827 | } |
| 3828 | |
| 3829 | bool NeedsEnvironment() const OVERRIDE { |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3830 | return false; |
| 3831 | } |
| 3832 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3833 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3834 | |
| 3835 | bool IsClassFinal() const { return class_is_final_; } |
| 3836 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3837 | // Used only in code generation. |
| 3838 | bool MustDoNullCheck() const { return must_do_null_check_; } |
| 3839 | void ClearMustDoNullCheck() { must_do_null_check_ = false; } |
| 3840 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3841 | DECLARE_INSTRUCTION(InstanceOf); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3842 | |
| 3843 | private: |
| 3844 | const bool class_is_final_; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3845 | bool must_do_null_check_; |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3846 | const uint32_t dex_pc_; |
| 3847 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3848 | DISALLOW_COPY_AND_ASSIGN(HInstanceOf); |
| 3849 | }; |
| 3850 | |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3851 | class HBoundType : public HExpression<1> { |
| 3852 | public: |
| 3853 | HBoundType(HInstruction* input, ReferenceTypeInfo bound_type) |
| 3854 | : HExpression(Primitive::kPrimNot, SideEffects::None()), |
| 3855 | bound_type_(bound_type) { |
Calin Juravle | 61d544b | 2015-02-23 16:46:57 +0000 | [diff] [blame] | 3856 | DCHECK_EQ(input->GetType(), Primitive::kPrimNot); |
Calin Juravle | b1498f6 | 2015-02-16 13:13:29 +0000 | [diff] [blame] | 3857 | SetRawInputAt(0, input); |
| 3858 | } |
| 3859 | |
| 3860 | const ReferenceTypeInfo& GetBoundType() const { return bound_type_; } |
| 3861 | |
| 3862 | bool CanBeNull() const OVERRIDE { |
| 3863 | // `null instanceof ClassX` always return false so we can't be null. |
| 3864 | return false; |
| 3865 | } |
| 3866 | |
| 3867 | DECLARE_INSTRUCTION(BoundType); |
| 3868 | |
| 3869 | private: |
| 3870 | // Encodes the most upper class that this instruction can have. In other words |
| 3871 | // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()). |
| 3872 | // It is used to bound the type in cases like `if (x instanceof ClassX) {}` |
| 3873 | const ReferenceTypeInfo bound_type_; |
| 3874 | |
| 3875 | DISALLOW_COPY_AND_ASSIGN(HBoundType); |
| 3876 | }; |
| 3877 | |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3878 | class HCheckCast : public HTemplateInstruction<2> { |
| 3879 | public: |
| 3880 | HCheckCast(HInstruction* object, |
| 3881 | HLoadClass* constant, |
| 3882 | bool class_is_final, |
| 3883 | uint32_t dex_pc) |
| 3884 | : HTemplateInstruction(SideEffects::None()), |
| 3885 | class_is_final_(class_is_final), |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3886 | must_do_null_check_(true), |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3887 | dex_pc_(dex_pc) { |
| 3888 | SetRawInputAt(0, object); |
| 3889 | SetRawInputAt(1, constant); |
| 3890 | } |
| 3891 | |
| 3892 | bool CanBeMoved() const OVERRIDE { return true; } |
| 3893 | |
| 3894 | bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { |
| 3895 | return true; |
| 3896 | } |
| 3897 | |
| 3898 | bool NeedsEnvironment() const OVERRIDE { |
| 3899 | // Instruction may throw a CheckCastError. |
| 3900 | return true; |
| 3901 | } |
| 3902 | |
| 3903 | bool CanThrow() const OVERRIDE { return true; } |
| 3904 | |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3905 | bool MustDoNullCheck() const { return must_do_null_check_; } |
| 3906 | void ClearMustDoNullCheck() { must_do_null_check_ = false; } |
| 3907 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3908 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3909 | |
| 3910 | bool IsClassFinal() const { return class_is_final_; } |
| 3911 | |
| 3912 | DECLARE_INSTRUCTION(CheckCast); |
| 3913 | |
| 3914 | private: |
| 3915 | const bool class_is_final_; |
Guillaume "Vermeille" Sanchez | af88835 | 2015-04-20 14:41:30 +0100 | [diff] [blame] | 3916 | bool must_do_null_check_; |
Nicolas Geoffray | 57a88d4 | 2014-11-10 15:09:21 +0000 | [diff] [blame] | 3917 | const uint32_t dex_pc_; |
| 3918 | |
| 3919 | DISALLOW_COPY_AND_ASSIGN(HCheckCast); |
Nicolas Geoffray | 6f5c41f | 2014-11-06 08:59:20 +0000 | [diff] [blame] | 3920 | }; |
| 3921 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 3922 | class HMemoryBarrier : public HTemplateInstruction<0> { |
| 3923 | public: |
| 3924 | explicit HMemoryBarrier(MemBarrierKind barrier_kind) |
| 3925 | : HTemplateInstruction(SideEffects::None()), |
| 3926 | barrier_kind_(barrier_kind) {} |
| 3927 | |
| 3928 | MemBarrierKind GetBarrierKind() { return barrier_kind_; } |
| 3929 | |
| 3930 | DECLARE_INSTRUCTION(MemoryBarrier); |
| 3931 | |
| 3932 | private: |
| 3933 | const MemBarrierKind barrier_kind_; |
| 3934 | |
| 3935 | DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier); |
| 3936 | }; |
| 3937 | |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3938 | class HMonitorOperation : public HTemplateInstruction<1> { |
| 3939 | public: |
| 3940 | enum OperationKind { |
| 3941 | kEnter, |
| 3942 | kExit, |
| 3943 | }; |
| 3944 | |
| 3945 | HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc) |
| 3946 | : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) { |
| 3947 | SetRawInputAt(0, object); |
| 3948 | } |
| 3949 | |
| 3950 | // Instruction may throw a Java exception, so we need an environment. |
| 3951 | bool NeedsEnvironment() const OVERRIDE { return true; } |
| 3952 | bool CanThrow() const OVERRIDE { return true; } |
| 3953 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 3954 | uint32_t GetDexPc() const OVERRIDE { return dex_pc_; } |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3955 | |
| 3956 | bool IsEnter() const { return kind_ == kEnter; } |
| 3957 | |
| 3958 | DECLARE_INSTRUCTION(MonitorOperation); |
| 3959 | |
Calin Juravle | 52c4896 | 2014-12-16 17:02:57 +0000 | [diff] [blame] | 3960 | private: |
Nicolas Geoffray | b7baf5c | 2014-11-11 16:29:44 +0000 | [diff] [blame] | 3961 | const OperationKind kind_; |
| 3962 | const uint32_t dex_pc_; |
| 3963 | |
| 3964 | private: |
| 3965 | DISALLOW_COPY_AND_ASSIGN(HMonitorOperation); |
| 3966 | }; |
| 3967 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3968 | class MoveOperands : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3969 | public: |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 3970 | MoveOperands(Location source, |
| 3971 | Location destination, |
| 3972 | Primitive::Type type, |
| 3973 | HInstruction* instruction) |
| 3974 | : source_(source), destination_(destination), type_(type), instruction_(instruction) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 3975 | |
| 3976 | Location GetSource() const { return source_; } |
| 3977 | Location GetDestination() const { return destination_; } |
| 3978 | |
| 3979 | void SetSource(Location value) { source_ = value; } |
| 3980 | void SetDestination(Location value) { destination_ = value; } |
| 3981 | |
| 3982 | // The parallel move resolver marks moves as "in-progress" by clearing the |
| 3983 | // destination (but not the source). |
| 3984 | Location MarkPending() { |
| 3985 | DCHECK(!IsPending()); |
| 3986 | Location dest = destination_; |
| 3987 | destination_ = Location::NoLocation(); |
| 3988 | return dest; |
| 3989 | } |
| 3990 | |
| 3991 | void ClearPending(Location dest) { |
| 3992 | DCHECK(IsPending()); |
| 3993 | destination_ = dest; |
| 3994 | } |
| 3995 | |
| 3996 | bool IsPending() const { |
| 3997 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 3998 | return destination_.IsInvalid() && !source_.IsInvalid(); |
| 3999 | } |
| 4000 | |
| 4001 | // True if this blocks a move from the given location. |
| 4002 | bool Blocks(Location loc) const { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 4003 | return !IsEliminated() && source_.OverlapsWith(loc); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | // A move is redundant if it's been eliminated, if its source and |
| 4007 | // destination are the same, or if its destination is unneeded. |
| 4008 | bool IsRedundant() const { |
| 4009 | return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_); |
| 4010 | } |
| 4011 | |
| 4012 | // We clear both operands to indicate move that's been eliminated. |
| 4013 | void Eliminate() { |
| 4014 | source_ = destination_ = Location::NoLocation(); |
| 4015 | } |
| 4016 | |
| 4017 | bool IsEliminated() const { |
| 4018 | DCHECK(!source_.IsInvalid() || destination_.IsInvalid()); |
| 4019 | return source_.IsInvalid(); |
| 4020 | } |
| 4021 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4022 | bool Is64BitMove() const { |
| 4023 | return Primitive::Is64BitType(type_); |
| 4024 | } |
| 4025 | |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4026 | HInstruction* GetInstruction() const { return instruction_; } |
| 4027 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4028 | private: |
| 4029 | Location source_; |
| 4030 | Location destination_; |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4031 | // The type this move is for. |
| 4032 | Primitive::Type type_; |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4033 | // The instruction this move is assocatied with. Null when this move is |
| 4034 | // for moving an input in the expected locations of user (including a phi user). |
| 4035 | // This is only used in debug mode, to ensure we do not connect interval siblings |
| 4036 | // in the same parallel move. |
| 4037 | HInstruction* instruction_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4038 | }; |
| 4039 | |
| 4040 | static constexpr size_t kDefaultNumberOfMoves = 4; |
| 4041 | |
| 4042 | class HParallelMove : public HTemplateInstruction<0> { |
| 4043 | public: |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 4044 | explicit HParallelMove(ArenaAllocator* arena) |
| 4045 | : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {} |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4046 | |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4047 | void AddMove(Location source, |
| 4048 | Location destination, |
| 4049 | Primitive::Type type, |
| 4050 | HInstruction* instruction) { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 4051 | DCHECK(source.IsValid()); |
| 4052 | DCHECK(destination.IsValid()); |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4053 | if (kIsDebugBuild) { |
| 4054 | if (instruction != nullptr) { |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 4055 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 4056 | if (moves_.Get(i).GetInstruction() == instruction) { |
| 4057 | // Special case the situation where the move is for the spill slot |
| 4058 | // of the instruction. |
| 4059 | if ((GetPrevious() == instruction) |
| 4060 | || ((GetPrevious() == nullptr) |
| 4061 | && instruction->IsPhi() |
| 4062 | && instruction->GetBlock() == GetBlock())) { |
| 4063 | DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind()) |
| 4064 | << "Doing parallel moves for the same instruction."; |
| 4065 | } else { |
| 4066 | DCHECK(false) << "Doing parallel moves for the same instruction."; |
| 4067 | } |
| 4068 | } |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 4069 | } |
| 4070 | } |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4071 | for (size_t i = 0, e = moves_.Size(); i < e; ++i) { |
Zheng Xu | ad4450e | 2015-04-17 18:48:56 +0800 | [diff] [blame] | 4072 | DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination())) |
Guillaume "Vermeille" Sanchez | 8909baf | 2015-04-23 21:35:11 +0100 | [diff] [blame] | 4073 | << "Overlapped destination for two moves in a parallel move: " |
| 4074 | << moves_.Get(i).GetSource() << " ==> " << moves_.Get(i).GetDestination() << " and " |
| 4075 | << source << " ==> " << destination; |
Nicolas Geoffray | f7a0c4e | 2015-02-10 17:08:47 +0000 | [diff] [blame] | 4076 | } |
Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 4077 | } |
Nicolas Geoffray | 9021825 | 2015-04-15 11:56:51 +0100 | [diff] [blame] | 4078 | moves_.Add(MoveOperands(source, destination, type, instruction)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | MoveOperands* MoveOperandsAt(size_t index) const { |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 4082 | return moves_.GetRawStorage() + index; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | size_t NumMoves() const { return moves_.Size(); } |
| 4086 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 4087 | DECLARE_INSTRUCTION(ParallelMove); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4088 | |
| 4089 | private: |
Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 4090 | GrowableArray<MoveOperands> moves_; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 4091 | |
| 4092 | DISALLOW_COPY_AND_ASSIGN(HParallelMove); |
| 4093 | }; |
| 4094 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4095 | class HGraphVisitor : public ValueObject { |
| 4096 | public: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 4097 | explicit HGraphVisitor(HGraph* graph) : graph_(graph) {} |
| 4098 | virtual ~HGraphVisitor() {} |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4099 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 4100 | virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4101 | virtual void VisitBasicBlock(HBasicBlock* block); |
| 4102 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 4103 | // Visit the graph following basic block insertion order. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4104 | void VisitInsertionOrder(); |
| 4105 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 4106 | // Visit the graph following dominator tree reverse post-order. |
| 4107 | void VisitReversePostOrder(); |
| 4108 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 4109 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 4110 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4111 | // Visit functions for instruction classes. |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 4112 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4113 | virtual void Visit##name(H##name* instr) { VisitInstruction(instr); } |
| 4114 | |
| 4115 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 4116 | |
| 4117 | #undef DECLARE_VISIT_INSTRUCTION |
| 4118 | |
| 4119 | private: |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 4120 | HGraph* const graph_; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4121 | |
| 4122 | DISALLOW_COPY_AND_ASSIGN(HGraphVisitor); |
| 4123 | }; |
| 4124 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 4125 | class HGraphDelegateVisitor : public HGraphVisitor { |
| 4126 | public: |
| 4127 | explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {} |
| 4128 | virtual ~HGraphDelegateVisitor() {} |
| 4129 | |
| 4130 | // Visit functions that delegate to to super class. |
| 4131 | #define DECLARE_VISIT_INSTRUCTION(name, super) \ |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 4132 | void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); } |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 4133 | |
| 4134 | FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) |
| 4135 | |
| 4136 | #undef DECLARE_VISIT_INSTRUCTION |
| 4137 | |
| 4138 | private: |
| 4139 | DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor); |
| 4140 | }; |
| 4141 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4142 | class HInsertionOrderIterator : public ValueObject { |
| 4143 | public: |
| 4144 | explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {} |
| 4145 | |
| 4146 | bool Done() const { return index_ == graph_.GetBlocks().Size(); } |
| 4147 | HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); } |
| 4148 | void Advance() { ++index_; } |
| 4149 | |
| 4150 | private: |
| 4151 | const HGraph& graph_; |
| 4152 | size_t index_; |
| 4153 | |
| 4154 | DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator); |
| 4155 | }; |
| 4156 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4157 | class HReversePostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4158 | public: |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 4159 | explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) { |
| 4160 | // Check that reverse post order of the graph has been built. |
| 4161 | DCHECK(!graph.GetReversePostOrder().IsEmpty()); |
| 4162 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4163 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4164 | bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); } |
| 4165 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4166 | void Advance() { ++index_; } |
| 4167 | |
| 4168 | private: |
| 4169 | const HGraph& graph_; |
| 4170 | size_t index_; |
| 4171 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4172 | DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4173 | }; |
| 4174 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4175 | class HPostOrderIterator : public ValueObject { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4176 | public: |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4177 | explicit HPostOrderIterator(const HGraph& graph) |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 4178 | : graph_(graph), index_(graph_.GetReversePostOrder().Size()) { |
| 4179 | // Check that reverse post order of the graph has been built. |
| 4180 | DCHECK(!graph.GetReversePostOrder().IsEmpty()); |
| 4181 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4182 | |
| 4183 | bool Done() const { return index_ == 0; } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4184 | HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4185 | void Advance() { --index_; } |
| 4186 | |
| 4187 | private: |
| 4188 | const HGraph& graph_; |
| 4189 | size_t index_; |
| 4190 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 4191 | DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 4192 | }; |
| 4193 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 4194 | class HLinearPostOrderIterator : public ValueObject { |
| 4195 | public: |
| 4196 | explicit HLinearPostOrderIterator(const HGraph& graph) |
| 4197 | : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {} |
| 4198 | |
| 4199 | bool Done() const { return index_ == 0; } |
| 4200 | |
| 4201 | HBasicBlock* Current() const { return order_.Get(index_ -1); } |
| 4202 | |
| 4203 | void Advance() { |
| 4204 | --index_; |
| 4205 | DCHECK_GE(index_, 0U); |
| 4206 | } |
| 4207 | |
| 4208 | private: |
| 4209 | const GrowableArray<HBasicBlock*>& order_; |
| 4210 | size_t index_; |
| 4211 | |
| 4212 | DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator); |
| 4213 | }; |
| 4214 | |
| 4215 | class HLinearOrderIterator : public ValueObject { |
| 4216 | public: |
| 4217 | explicit HLinearOrderIterator(const HGraph& graph) |
| 4218 | : order_(graph.GetLinearOrder()), index_(0) {} |
| 4219 | |
| 4220 | bool Done() const { return index_ == order_.Size(); } |
| 4221 | HBasicBlock* Current() const { return order_.Get(index_); } |
| 4222 | void Advance() { ++index_; } |
| 4223 | |
| 4224 | private: |
| 4225 | const GrowableArray<HBasicBlock*>& order_; |
| 4226 | size_t index_; |
| 4227 | |
| 4228 | DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator); |
| 4229 | }; |
| 4230 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 4231 | // Iterator over the blocks that art part of the loop. Includes blocks part |
| 4232 | // of an inner loop. The order in which the blocks are iterated is on their |
| 4233 | // block id. |
| 4234 | class HBlocksInLoopIterator : public ValueObject { |
| 4235 | public: |
| 4236 | explicit HBlocksInLoopIterator(const HLoopInformation& info) |
| 4237 | : blocks_in_loop_(info.GetBlocks()), |
| 4238 | blocks_(info.GetHeader()->GetGraph()->GetBlocks()), |
| 4239 | index_(0) { |
| 4240 | if (!blocks_in_loop_.IsBitSet(index_)) { |
| 4241 | Advance(); |
| 4242 | } |
| 4243 | } |
| 4244 | |
| 4245 | bool Done() const { return index_ == blocks_.Size(); } |
| 4246 | HBasicBlock* Current() const { return blocks_.Get(index_); } |
| 4247 | void Advance() { |
| 4248 | ++index_; |
| 4249 | for (size_t e = blocks_.Size(); index_ < e; ++index_) { |
| 4250 | if (blocks_in_loop_.IsBitSet(index_)) { |
| 4251 | break; |
| 4252 | } |
| 4253 | } |
| 4254 | } |
| 4255 | |
| 4256 | private: |
| 4257 | const BitVector& blocks_in_loop_; |
| 4258 | const GrowableArray<HBasicBlock*>& blocks_; |
| 4259 | size_t index_; |
| 4260 | |
| 4261 | DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator); |
| 4262 | }; |
| 4263 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 4264 | inline int64_t Int64FromConstant(HConstant* constant) { |
| 4265 | DCHECK(constant->IsIntConstant() || constant->IsLongConstant()); |
| 4266 | return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue() |
| 4267 | : constant->AsLongConstant()->GetValue(); |
| 4268 | } |
| 4269 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 4270 | } // namespace art |
| 4271 | |
| 4272 | #endif // ART_COMPILER_OPTIMIZING_NODES_H_ |