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