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