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